tests for OFDeviceProvider
Change-Id: If9f2c1f78a940a5485e5390d6d098d74eb00ddc2
Showing
2 changed files
with
344 additions
and
7 deletions
1 | +package org.onlab.onos.provider.of.device.impl; | ||
2 | + | ||
3 | +import static org.junit.Assert.assertEquals; | ||
4 | +import static org.junit.Assert.assertNotNull; | ||
5 | +import static org.junit.Assert.assertNull; | ||
6 | +import static org.junit.Assert.assertTrue; | ||
7 | +import static org.onlab.onos.net.Device.Type.*; | ||
8 | +import static org.onlab.onos.net.MastershipRole.*; | ||
9 | + | ||
10 | +import java.util.ArrayList; | ||
11 | +import java.util.HashMap; | ||
12 | +import java.util.HashSet; | ||
13 | +import java.util.List; | ||
14 | +import java.util.Map; | ||
15 | +import java.util.Set; | ||
16 | + | ||
17 | +import org.junit.After; | ||
18 | +import org.junit.Before; | ||
19 | +import org.junit.Test; | ||
20 | +import org.onlab.onos.net.DefaultDevice; | ||
21 | +import org.onlab.onos.net.Device; | ||
22 | +import org.onlab.onos.net.DeviceId; | ||
23 | +import org.onlab.onos.net.device.DeviceDescription; | ||
24 | +import org.onlab.onos.net.device.DeviceProvider; | ||
25 | +import org.onlab.onos.net.device.DeviceProviderRegistry; | ||
26 | +import org.onlab.onos.net.device.DeviceProviderService; | ||
27 | +import org.onlab.onos.net.device.PortDescription; | ||
28 | +import org.onlab.onos.net.provider.ProviderId; | ||
29 | +import org.onlab.onos.openflow.controller.Dpid; | ||
30 | +import org.onlab.onos.openflow.controller.OpenFlowController; | ||
31 | +import org.onlab.onos.openflow.controller.OpenFlowEventListener; | ||
32 | +import org.onlab.onos.openflow.controller.OpenFlowSwitch; | ||
33 | +import org.onlab.onos.openflow.controller.OpenFlowSwitchListener; | ||
34 | +import org.onlab.onos.openflow.controller.PacketListener; | ||
35 | +import org.onlab.onos.openflow.controller.RoleState; | ||
36 | +import org.projectfloodlight.openflow.protocol.OFFactory; | ||
37 | +import org.projectfloodlight.openflow.protocol.OFMessage; | ||
38 | +import org.projectfloodlight.openflow.protocol.OFPortDesc; | ||
39 | +import org.projectfloodlight.openflow.protocol.OFPortReason; | ||
40 | +import org.projectfloodlight.openflow.protocol.OFPortStatus; | ||
41 | +import org.projectfloodlight.openflow.protocol.ver10.OFFactoryVer10; | ||
42 | +import org.projectfloodlight.openflow.types.OFPort; | ||
43 | + | ||
44 | +import com.google.common.collect.HashMultimap; | ||
45 | +import com.google.common.collect.Lists; | ||
46 | +import com.google.common.collect.Multimap; | ||
47 | + | ||
48 | +public class OpenFlowDeviceProviderTest { | ||
49 | + | ||
50 | + private static final ProviderId PID = new ProviderId("of", "test"); | ||
51 | + private static final DeviceId DID1 = DeviceId.deviceId("of:0000000000000001"); | ||
52 | + private static final Dpid DPID1 = Dpid.dpid(DID1.uri()); | ||
53 | + | ||
54 | + private static final OFPortDesc PD1 = portDesc(1); | ||
55 | + private static final OFPortDesc PD2 = portDesc(2); | ||
56 | + private static final OFPortDesc PD3 = portDesc(3); | ||
57 | + | ||
58 | + private static final List<OFPortDesc> PLIST = Lists.newArrayList(PD1, PD2); | ||
59 | + | ||
60 | + private static final Device DEV1 = | ||
61 | + new DefaultDevice(PID, DID1, SWITCH, "", "", "", ""); | ||
62 | + | ||
63 | + private static final TestOpenFlowSwitch SW1 = new TestOpenFlowSwitch(); | ||
64 | + | ||
65 | + private final OpenFlowDeviceProvider provider = new OpenFlowDeviceProvider(); | ||
66 | + private final TestDeviceRegistry registry = new TestDeviceRegistry(); | ||
67 | + private final TestController controller = new TestController(); | ||
68 | + | ||
69 | + @Before | ||
70 | + public void startUp() { | ||
71 | + provider.providerRegistry = registry; | ||
72 | + provider.controller = controller; | ||
73 | + controller.switchMap.put(DPID1, SW1); | ||
74 | + provider.activate(); | ||
75 | + assertNotNull("provider should be registered", registry.provider); | ||
76 | + assertNotNull("listener should be registered", controller.listener); | ||
77 | + assertEquals("devices not added", 1, registry.connected.size()); | ||
78 | + assertEquals("ports not added", 2, registry.ports.get(DID1).size()); | ||
79 | + } | ||
80 | + | ||
81 | + @After | ||
82 | + public void tearDown() { | ||
83 | + provider.deactivate(); | ||
84 | + assertTrue("devices should be removed", registry.connected.isEmpty()); | ||
85 | + assertNull("listener should be removed", controller.listener); | ||
86 | + provider.controller = null; | ||
87 | + provider.providerRegistry = null; | ||
88 | + } | ||
89 | + | ||
90 | + @Test | ||
91 | + public void roleChanged() { | ||
92 | + provider.roleChanged(DEV1, MASTER); | ||
93 | + assertEquals("Should be MASTER", RoleState.MASTER, controller.roleMap.get(DPID1)); | ||
94 | + provider.roleChanged(DEV1, STANDBY); | ||
95 | + assertEquals("Should be EQUAL", RoleState.EQUAL, controller.roleMap.get(DPID1)); | ||
96 | + provider.roleChanged(DEV1, NONE); | ||
97 | + assertEquals("Should be SLAVE", RoleState.SLAVE, controller.roleMap.get(DPID1)); | ||
98 | + } | ||
99 | + | ||
100 | + @Test | ||
101 | + public void triggerProbe() { | ||
102 | + // TODO | ||
103 | + } | ||
104 | + | ||
105 | + @Test | ||
106 | + public void switchRemoved() { | ||
107 | + controller.listener.switchRemoved(DPID1); | ||
108 | + assertTrue("device not removed", registry.connected.isEmpty()); | ||
109 | + } | ||
110 | + | ||
111 | + @Test | ||
112 | + public void portChanged() { | ||
113 | + OFPortStatus stat = SW1.factory().buildPortStatus() | ||
114 | + .setReason(OFPortReason.ADD) | ||
115 | + .setDesc(PD3) | ||
116 | + .build(); | ||
117 | + controller.listener.portChanged(DPID1, stat); | ||
118 | + assertEquals("port status unhandled", 3, registry.ports.get(DID1).size()); | ||
119 | + } | ||
120 | + | ||
121 | + private static OFPortDesc portDesc(int port) { | ||
122 | + OFPortDesc.Builder builder = OFFactoryVer10.INSTANCE.buildPortDesc(); | ||
123 | + builder.setPortNo(OFPort.of(port)); | ||
124 | + | ||
125 | + return builder.build(); | ||
126 | + } | ||
127 | + | ||
128 | + private class TestDeviceRegistry implements DeviceProviderRegistry { | ||
129 | + DeviceProvider provider; | ||
130 | + Set<DeviceId> connected = new HashSet<DeviceId>(); | ||
131 | + Multimap<DeviceId, PortDescription> ports = HashMultimap.create(); | ||
132 | + | ||
133 | + @Override | ||
134 | + public DeviceProviderService register(DeviceProvider provider) { | ||
135 | + this.provider = provider; | ||
136 | + return new TestProviderService(); | ||
137 | + } | ||
138 | + | ||
139 | + @Override | ||
140 | + public void unregister(DeviceProvider provider) { | ||
141 | + } | ||
142 | + | ||
143 | + @Override | ||
144 | + public Set<ProviderId> getProviders() { | ||
145 | + return null; | ||
146 | + } | ||
147 | + | ||
148 | + private class TestProviderService implements DeviceProviderService { | ||
149 | + | ||
150 | + @Override | ||
151 | + public DeviceProvider provider() { | ||
152 | + return null; | ||
153 | + } | ||
154 | + | ||
155 | + @Override | ||
156 | + public void deviceConnected(DeviceId deviceId, | ||
157 | + DeviceDescription deviceDescription) { | ||
158 | + connected.add(deviceId); | ||
159 | + } | ||
160 | + | ||
161 | + @Override | ||
162 | + public void deviceDisconnected(DeviceId deviceId) { | ||
163 | + connected.remove(deviceId); | ||
164 | + ports.removeAll(deviceId); | ||
165 | + } | ||
166 | + | ||
167 | + @Override | ||
168 | + public void updatePorts(DeviceId deviceId, | ||
169 | + List<PortDescription> portDescriptions) { | ||
170 | + for (PortDescription p : portDescriptions) { | ||
171 | + ports.put(deviceId, p); | ||
172 | + } | ||
173 | + } | ||
174 | + | ||
175 | + @Override | ||
176 | + public void portStatusChanged(DeviceId deviceId, | ||
177 | + PortDescription portDescription) { | ||
178 | + System.err.println(deviceId + ":" + portDescription.toString()); | ||
179 | + | ||
180 | + System.err.println(ports.put(deviceId, portDescription)); | ||
181 | + } | ||
182 | + | ||
183 | + } | ||
184 | + } | ||
185 | + | ||
186 | + private class TestController implements OpenFlowController { | ||
187 | + OpenFlowSwitchListener listener = null; | ||
188 | + Map<Dpid, RoleState> roleMap = new HashMap<Dpid, RoleState>(); | ||
189 | + Map<Dpid, OpenFlowSwitch> switchMap = new HashMap<Dpid, OpenFlowSwitch>(); | ||
190 | + | ||
191 | + @Override | ||
192 | + public Iterable<OpenFlowSwitch> getSwitches() { | ||
193 | + return switchMap.values(); | ||
194 | + } | ||
195 | + | ||
196 | + @Override | ||
197 | + public Iterable<OpenFlowSwitch> getMasterSwitches() { | ||
198 | + return null; | ||
199 | + } | ||
200 | + | ||
201 | + @Override | ||
202 | + public Iterable<OpenFlowSwitch> getEqualSwitches() { | ||
203 | + return null; | ||
204 | + } | ||
205 | + | ||
206 | + @Override | ||
207 | + public OpenFlowSwitch getSwitch(Dpid dpid) { | ||
208 | + return switchMap.get(dpid); | ||
209 | + } | ||
210 | + | ||
211 | + @Override | ||
212 | + public OpenFlowSwitch getMasterSwitch(Dpid dpid) { | ||
213 | + return null; | ||
214 | + } | ||
215 | + | ||
216 | + @Override | ||
217 | + public OpenFlowSwitch getEqualSwitch(Dpid dpid) { | ||
218 | + | ||
219 | + return null; | ||
220 | + } | ||
221 | + | ||
222 | + @Override | ||
223 | + public void addListener(OpenFlowSwitchListener listener) { | ||
224 | + this.listener = listener; | ||
225 | + } | ||
226 | + | ||
227 | + @Override | ||
228 | + public void removeListener(OpenFlowSwitchListener listener) { | ||
229 | + this.listener = null; | ||
230 | + } | ||
231 | + | ||
232 | + @Override | ||
233 | + public void addPacketListener(int priority, PacketListener listener) { | ||
234 | + } | ||
235 | + | ||
236 | + @Override | ||
237 | + public void removePacketListener(PacketListener listener) { | ||
238 | + } | ||
239 | + | ||
240 | + @Override | ||
241 | + public void addEventListener(OpenFlowEventListener listener) { | ||
242 | + } | ||
243 | + | ||
244 | + @Override | ||
245 | + public void removeEventListener(OpenFlowEventListener listener) { | ||
246 | + } | ||
247 | + | ||
248 | + @Override | ||
249 | + public void write(Dpid dpid, OFMessage msg) { | ||
250 | + } | ||
251 | + | ||
252 | + @Override | ||
253 | + public void processPacket(Dpid dpid, OFMessage msg) { | ||
254 | + } | ||
255 | + | ||
256 | + @Override | ||
257 | + public void setRole(Dpid dpid, RoleState role) { | ||
258 | + roleMap.put(dpid, role); | ||
259 | + } | ||
260 | + } | ||
261 | + | ||
262 | + private static class TestOpenFlowSwitch implements OpenFlowSwitch { | ||
263 | + | ||
264 | + RoleState state; | ||
265 | + List<OFMessage> sent = new ArrayList<OFMessage>(); | ||
266 | + OFFactory factory = OFFactoryVer10.INSTANCE; | ||
267 | + | ||
268 | + @Override | ||
269 | + public void sendMsg(OFMessage msg) { | ||
270 | + sent.add(msg); | ||
271 | + } | ||
272 | + | ||
273 | + @Override | ||
274 | + public void sendMsg(List<OFMessage> msgs) { | ||
275 | + } | ||
276 | + | ||
277 | + @Override | ||
278 | + public void handleMessage(OFMessage fromSwitch) { | ||
279 | + } | ||
280 | + | ||
281 | + @Override | ||
282 | + public void setRole(RoleState role) { | ||
283 | + state = role; | ||
284 | + } | ||
285 | + | ||
286 | + @Override | ||
287 | + public RoleState getRole() { | ||
288 | + return state; | ||
289 | + } | ||
290 | + | ||
291 | + @Override | ||
292 | + public List<OFPortDesc> getPorts() { | ||
293 | + return PLIST; | ||
294 | + } | ||
295 | + | ||
296 | + @Override | ||
297 | + public OFFactory factory() { | ||
298 | + return factory; | ||
299 | + } | ||
300 | + | ||
301 | + @Override | ||
302 | + public String getStringId() { | ||
303 | + return null; | ||
304 | + } | ||
305 | + | ||
306 | + @Override | ||
307 | + public long getId() { | ||
308 | + return DPID1.value(); | ||
309 | + } | ||
310 | + | ||
311 | + @Override | ||
312 | + public String manfacturerDescription() { | ||
313 | + return null; | ||
314 | + } | ||
315 | + | ||
316 | + @Override | ||
317 | + public String datapathDescription() { | ||
318 | + return null; | ||
319 | + } | ||
320 | + | ||
321 | + @Override | ||
322 | + public String hardwareDescription() { | ||
323 | + return null; | ||
324 | + } | ||
325 | + | ||
326 | + @Override | ||
327 | + public String softwareDescription() { | ||
328 | + return null; | ||
329 | + } | ||
330 | + | ||
331 | + @Override | ||
332 | + public String serialNumber() { | ||
333 | + return null; | ||
334 | + } | ||
335 | + | ||
336 | + @Override | ||
337 | + public void disconnectSwitch() { | ||
338 | + } | ||
339 | + | ||
340 | + } | ||
341 | + | ||
342 | +} |
... | @@ -80,7 +80,7 @@ public class OpenFlowPacketProviderTest { | ... | @@ -80,7 +80,7 @@ public class OpenFlowPacketProviderTest { |
80 | private final TestPacketRegistry registry = new TestPacketRegistry(); | 80 | private final TestPacketRegistry registry = new TestPacketRegistry(); |
81 | private final TestController controller = new TestController(); | 81 | private final TestController controller = new TestController(); |
82 | 82 | ||
83 | - private final TestOpenFlowSwitch sw = new TestOpenFlowSwitch(PLIST); | 83 | + private final TestOpenFlowSwitch sw = new TestOpenFlowSwitch(); |
84 | 84 | ||
85 | @Before | 85 | @Before |
86 | public void startUp() { | 86 | public void startUp() { |
... | @@ -317,15 +317,10 @@ public class OpenFlowPacketProviderTest { | ... | @@ -317,15 +317,10 @@ public class OpenFlowPacketProviderTest { |
317 | 317 | ||
318 | private class TestOpenFlowSwitch implements OpenFlowSwitch { | 318 | private class TestOpenFlowSwitch implements OpenFlowSwitch { |
319 | 319 | ||
320 | - List<OFPortDesc> ports; | ||
321 | RoleState state; | 320 | RoleState state; |
322 | List<OFMessage> sent = new ArrayList<OFMessage>(); | 321 | List<OFMessage> sent = new ArrayList<OFMessage>(); |
323 | OFFactory factory = OFFactoryVer10.INSTANCE; | 322 | OFFactory factory = OFFactoryVer10.INSTANCE; |
324 | 323 | ||
325 | - TestOpenFlowSwitch(List<OFPortDesc> p) { | ||
326 | - ports = p; | ||
327 | - } | ||
328 | - | ||
329 | @Override | 324 | @Override |
330 | public void sendMsg(OFMessage msg) { | 325 | public void sendMsg(OFMessage msg) { |
331 | sent.add(msg); | 326 | sent.add(msg); |
... | @@ -351,7 +346,7 @@ public class OpenFlowPacketProviderTest { | ... | @@ -351,7 +346,7 @@ public class OpenFlowPacketProviderTest { |
351 | 346 | ||
352 | @Override | 347 | @Override |
353 | public List<OFPortDesc> getPorts() { | 348 | public List<OFPortDesc> getPorts() { |
354 | - return ports; | 349 | + return PLIST; |
355 | } | 350 | } |
356 | 351 | ||
357 | @Override | 352 | @Override | ... | ... |
-
Please register or login to post a comment