added test for lldp provider
Change-Id: I31e7c21840d48c38ec60ec7437657daccc185846
Showing
3 changed files
with
503 additions
and
1 deletions
... | @@ -24,7 +24,7 @@ public class DefaultInboundPacket implements InboundPacket { | ... | @@ -24,7 +24,7 @@ public class DefaultInboundPacket implements InboundPacket { |
24 | * @param parsed parsed ethernet frame | 24 | * @param parsed parsed ethernet frame |
25 | * @param unparsed unparsed raw bytes | 25 | * @param unparsed unparsed raw bytes |
26 | */ | 26 | */ |
27 | - public DefaultInboundPacket(ConnectPoint receivedFrom, Ethernet parsed, | 27 | + public DefaultInboundPacket(ConnectPoint receivedFrom, Ethernet parsed, |
28 | ByteBuffer unparsed) { | 28 | ByteBuffer unparsed) { |
29 | this.receivedFrom = receivedFrom; | 29 | this.receivedFrom = receivedFrom; |
30 | this.parsed = parsed; | 30 | this.parsed = parsed; | ... | ... |
... | @@ -83,6 +83,7 @@ public class LinkDiscovery implements TimerTask { | ... | @@ -83,6 +83,7 @@ public class LinkDiscovery implements TimerTask { |
83 | private final PacketService pktService; | 83 | private final PacketService pktService; |
84 | private final MastershipService mastershipService; | 84 | private final MastershipService mastershipService; |
85 | private Timeout timeout; | 85 | private Timeout timeout; |
86 | + private boolean isStopped; | ||
86 | 87 | ||
87 | /** | 88 | /** |
88 | * Instantiates discovery manager for the given physical switch. Creates a | 89 | * Instantiates discovery manager for the given physical switch. Creates a |
... | @@ -289,11 +290,13 @@ public class LinkDiscovery implements TimerTask { | ... | @@ -289,11 +290,13 @@ public class LinkDiscovery implements TimerTask { |
289 | 290 | ||
290 | public void stop() { | 291 | public void stop() { |
291 | timeout.cancel(); | 292 | timeout.cancel(); |
293 | + isStopped = true; | ||
292 | } | 294 | } |
293 | 295 | ||
294 | public void start() { | 296 | public void start() { |
295 | timeout = Timer.getTimer().newTimeout(this, 0, | 297 | timeout = Timer.getTimer().newTimeout(this, 0, |
296 | TimeUnit.MILLISECONDS); | 298 | TimeUnit.MILLISECONDS); |
299 | + isStopped = false; | ||
297 | } | 300 | } |
298 | 301 | ||
299 | /** | 302 | /** |
... | @@ -352,4 +355,15 @@ public class LinkDiscovery implements TimerTask { | ... | @@ -352,4 +355,15 @@ public class LinkDiscovery implements TimerTask { |
352 | } | 355 | } |
353 | } | 356 | } |
354 | 357 | ||
358 | + public boolean containsPort(Long portNumber) { | ||
359 | + if (slowPorts.contains(portNumber) || fastPorts.contains(portNumber)) { | ||
360 | + return true; | ||
361 | + } | ||
362 | + return false; | ||
363 | + } | ||
364 | + | ||
365 | + public boolean isStopped() { | ||
366 | + return isStopped; | ||
367 | + } | ||
368 | + | ||
355 | } | 369 | } | ... | ... |
1 | +package org.onlab.onos.provider.lldp.impl; | ||
2 | + | ||
3 | + | ||
4 | +import com.google.common.collect.ArrayListMultimap; | ||
5 | +import com.google.common.collect.Lists; | ||
6 | +import com.google.common.collect.Maps; | ||
7 | +import org.junit.After; | ||
8 | +import org.junit.Before; | ||
9 | +import org.junit.Test; | ||
10 | +import org.onlab.onos.cluster.NodeId; | ||
11 | +import org.onlab.onos.mastership.MastershipListener; | ||
12 | +import org.onlab.onos.mastership.MastershipService; | ||
13 | +import org.onlab.onos.mastership.MastershipTermService; | ||
14 | +import org.onlab.onos.net.ConnectPoint; | ||
15 | +import org.onlab.onos.net.DefaultDevice; | ||
16 | +import org.onlab.onos.net.DefaultPort; | ||
17 | +import org.onlab.onos.net.Device; | ||
18 | +import org.onlab.onos.net.DeviceId; | ||
19 | +import org.onlab.onos.net.MastershipRole; | ||
20 | +import org.onlab.onos.net.Port; | ||
21 | +import org.onlab.onos.net.PortNumber; | ||
22 | +import org.onlab.onos.net.device.DeviceEvent; | ||
23 | +import org.onlab.onos.net.device.DeviceListener; | ||
24 | +import org.onlab.onos.net.device.DeviceService; | ||
25 | +import org.onlab.onos.net.flow.TrafficTreatment; | ||
26 | +import org.onlab.onos.net.link.LinkDescription; | ||
27 | +import org.onlab.onos.net.link.LinkProvider; | ||
28 | +import org.onlab.onos.net.link.LinkProviderRegistry; | ||
29 | +import org.onlab.onos.net.link.LinkProviderService; | ||
30 | +import org.onlab.onos.net.packet.DefaultInboundPacket; | ||
31 | +import org.onlab.onos.net.packet.InboundPacket; | ||
32 | +import org.onlab.onos.net.packet.OutboundPacket; | ||
33 | +import org.onlab.onos.net.packet.PacketContext; | ||
34 | +import org.onlab.onos.net.packet.PacketProcessor; | ||
35 | +import org.onlab.onos.net.packet.PacketService; | ||
36 | +import org.onlab.onos.net.provider.AbstractProviderService; | ||
37 | +import org.onlab.onos.net.provider.ProviderId; | ||
38 | + | ||
39 | +import org.onlab.packet.ChassisId; | ||
40 | +import org.onlab.packet.Ethernet; | ||
41 | + | ||
42 | +import org.onlab.packet.ONOSLLDP; | ||
43 | + | ||
44 | +import java.nio.ByteBuffer; | ||
45 | +import java.util.HashMap; | ||
46 | + | ||
47 | +import java.util.List; | ||
48 | +import java.util.Map; | ||
49 | +import java.util.Set; | ||
50 | + | ||
51 | +import static org.junit.Assert.*; | ||
52 | +import static org.junit.Assert.assertFalse; | ||
53 | +import static org.junit.Assert.assertTrue; | ||
54 | + | ||
55 | +public class LLDPLinkProviderTest { | ||
56 | + | ||
57 | + private static final DeviceId DID1 = DeviceId.deviceId("of:0000000000000001"); | ||
58 | + private static final DeviceId DID2 = DeviceId.deviceId("of:0000000000000002"); | ||
59 | + | ||
60 | + private static Port pd1; | ||
61 | + private static Port pd2; | ||
62 | + private static Port pd3; | ||
63 | + private static Port pd4; | ||
64 | + | ||
65 | + private final LLDPLinkProvider provider = new LLDPLinkProvider(); | ||
66 | + private final TestLinkRegistry linkService = new TestLinkRegistry(); | ||
67 | + private final TestPacketService packetService = new TestPacketService(); | ||
68 | + private final TestDeviceService deviceService = new TestDeviceService(); | ||
69 | + private final TestMasterShipService masterService = new TestMasterShipService(); | ||
70 | + | ||
71 | + private TestLinkProviderService providerService; | ||
72 | + | ||
73 | + private PacketProcessor testProcessor; | ||
74 | + private DeviceListener deviceListener; | ||
75 | + | ||
76 | + @Before | ||
77 | + public void setUp() { | ||
78 | + | ||
79 | + provider.deviceService = deviceService; | ||
80 | + provider.packetSevice = packetService; | ||
81 | + provider.providerRegistry = linkService; | ||
82 | + provider.masterService = masterService; | ||
83 | + | ||
84 | + | ||
85 | + provider.activate(); | ||
86 | + } | ||
87 | + | ||
88 | + @Test | ||
89 | + public void basics() { | ||
90 | + assertNotNull("registration expected", providerService); | ||
91 | + assertEquals("incorrect provider", provider, providerService.provider()); | ||
92 | + } | ||
93 | + | ||
94 | + @Test | ||
95 | + public void switchAdd() { | ||
96 | + DeviceEvent de = deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID1); | ||
97 | + deviceListener.event(de); | ||
98 | + | ||
99 | + assertFalse("Device not added", provider.discoverers.isEmpty()); | ||
100 | + } | ||
101 | + | ||
102 | + @Test | ||
103 | + public void switchRemove() { | ||
104 | + deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID1)); | ||
105 | + deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_REMOVED, DID1)); | ||
106 | + | ||
107 | + assertTrue("Discoverer is not gone", provider.discoverers.get(DID1).isStopped()); | ||
108 | + assertTrue("Device is not gone.", vanishedDpid(DID1)); | ||
109 | + } | ||
110 | + | ||
111 | + @Test | ||
112 | + public void portUp() { | ||
113 | + deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID1)); | ||
114 | + deviceListener.event(portEvent(DeviceEvent.Type.PORT_ADDED, DID1, port(DID1, 3, true))); | ||
115 | + | ||
116 | + assertTrue("Port not added to discoverer", | ||
117 | + provider.discoverers.get(DID1).containsPort((long) 3)); | ||
118 | + } | ||
119 | + | ||
120 | + @Test | ||
121 | + public void portDown() { | ||
122 | + | ||
123 | + deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID1)); | ||
124 | + deviceListener.event(portEvent(DeviceEvent.Type.PORT_ADDED, DID1, port(DID1, 1, false))); | ||
125 | + | ||
126 | + | ||
127 | + | ||
128 | + assertFalse("Port added to discoverer", | ||
129 | + provider.discoverers.get(DID1).containsPort((long) 1)); | ||
130 | + assertTrue("Port is not gone.", vanishedPort((long) 1)); | ||
131 | + } | ||
132 | + | ||
133 | + @Test | ||
134 | + public void portUnknown() { | ||
135 | + deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID1)); | ||
136 | + deviceListener.event(portEvent(DeviceEvent.Type.PORT_ADDED, DID2, port(DID2, 1, false))); | ||
137 | + | ||
138 | + | ||
139 | + assertNull("DPID exists", | ||
140 | + provider.discoverers.get(DID2)); | ||
141 | + } | ||
142 | + | ||
143 | + @Test | ||
144 | + public void unknownPktCtx() { | ||
145 | + | ||
146 | + PacketContext pktCtx = new TestPacketContext(deviceService.getDevice(DID2)); | ||
147 | + | ||
148 | + testProcessor.process(pktCtx); | ||
149 | + assertFalse("Context should still be free", pktCtx.isHandled()); | ||
150 | + } | ||
151 | + | ||
152 | + @Test | ||
153 | + public void knownPktCtx() { | ||
154 | + deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID1)); | ||
155 | + deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID2)); | ||
156 | + PacketContext pktCtx = new TestPacketContext(deviceService.getDevice(DID2)); | ||
157 | + | ||
158 | + | ||
159 | + testProcessor.process(pktCtx); | ||
160 | + | ||
161 | + assertTrue("Link not detected", detectedLink(DID1, DID2)); | ||
162 | + | ||
163 | + } | ||
164 | + | ||
165 | + | ||
166 | + @After | ||
167 | + public void tearDown() { | ||
168 | + provider.deactivate(); | ||
169 | + provider.providerRegistry = null; | ||
170 | + provider.deviceService = null; | ||
171 | + provider.packetSevice = null; | ||
172 | + } | ||
173 | + | ||
174 | + private DeviceEvent deviceEvent(DeviceEvent.Type type, DeviceId did) { | ||
175 | + return new DeviceEvent(type, deviceService.getDevice(did)); | ||
176 | + | ||
177 | + } | ||
178 | + | ||
179 | + private DeviceEvent portEvent(DeviceEvent.Type type, DeviceId did, PortNumber port) { | ||
180 | + return new DeviceEvent(type, deviceService.getDevice(did), | ||
181 | + deviceService.getPort(did, port)); | ||
182 | + } | ||
183 | + | ||
184 | + private DeviceEvent portEvent(DeviceEvent.Type type, DeviceId did, Port port) { | ||
185 | + return new DeviceEvent(type, deviceService.getDevice(did), port); | ||
186 | + } | ||
187 | + | ||
188 | + private Port port(DeviceId did, long port, boolean enabled) { | ||
189 | + return new DefaultPort(deviceService.getDevice(did), | ||
190 | + PortNumber.portNumber(port), enabled); | ||
191 | + } | ||
192 | + | ||
193 | + | ||
194 | + private boolean vanishedDpid(DeviceId... dids) { | ||
195 | + for (int i = 0; i < dids.length; i++) { | ||
196 | + if (!providerService.vanishedDpid.contains(dids[i])) { | ||
197 | + return false; | ||
198 | + } | ||
199 | + } | ||
200 | + return true; | ||
201 | + } | ||
202 | + | ||
203 | + private boolean vanishedPort(Long... ports) { | ||
204 | + for (int i = 0; i < ports.length; i++) { | ||
205 | + if (!providerService.vanishedPort.contains(ports[i])) { | ||
206 | + return false; | ||
207 | + } | ||
208 | + } | ||
209 | + return true; | ||
210 | + } | ||
211 | + | ||
212 | + private boolean detectedLink(DeviceId src, DeviceId dst) { | ||
213 | + for (DeviceId key : providerService.discoveredLinks.keySet()) { | ||
214 | + if (key.equals(src)) { | ||
215 | + return providerService.discoveredLinks.get(src).equals(dst); | ||
216 | + } | ||
217 | + } | ||
218 | + return false; | ||
219 | + } | ||
220 | + | ||
221 | + | ||
222 | + private class TestLinkRegistry implements LinkProviderRegistry { | ||
223 | + | ||
224 | + @Override | ||
225 | + public LinkProviderService register(LinkProvider provider) { | ||
226 | + providerService = new TestLinkProviderService(provider); | ||
227 | + return providerService; | ||
228 | + } | ||
229 | + | ||
230 | + @Override | ||
231 | + public void unregister(LinkProvider provider) { | ||
232 | + } | ||
233 | + | ||
234 | + @Override | ||
235 | + public Set<ProviderId> getProviders() { | ||
236 | + return null; | ||
237 | + } | ||
238 | + | ||
239 | + } | ||
240 | + | ||
241 | + private class TestLinkProviderService | ||
242 | + extends AbstractProviderService<LinkProvider> | ||
243 | + implements LinkProviderService { | ||
244 | + | ||
245 | + List<DeviceId> vanishedDpid = Lists.newLinkedList(); | ||
246 | + List<Long> vanishedPort = Lists.newLinkedList(); | ||
247 | + Map<DeviceId, DeviceId> discoveredLinks = Maps.newHashMap(); | ||
248 | + | ||
249 | + protected TestLinkProviderService(LinkProvider provider) { | ||
250 | + super(provider); | ||
251 | + } | ||
252 | + | ||
253 | + @Override | ||
254 | + public void linkDetected(LinkDescription linkDescription) { | ||
255 | + DeviceId sDid = linkDescription.src().deviceId(); | ||
256 | + DeviceId dDid = linkDescription.dst().deviceId(); | ||
257 | + discoveredLinks.put(sDid, dDid); | ||
258 | + } | ||
259 | + | ||
260 | + @Override | ||
261 | + public void linkVanished(LinkDescription linkDescription) { | ||
262 | + // TODO Auto-generated method stub | ||
263 | + | ||
264 | + } | ||
265 | + | ||
266 | + @Override | ||
267 | + public void linksVanished(ConnectPoint connectPoint) { | ||
268 | + vanishedPort.add(connectPoint.port().toLong()); | ||
269 | + | ||
270 | + } | ||
271 | + | ||
272 | + @Override | ||
273 | + public void linksVanished(DeviceId deviceId) { | ||
274 | + vanishedDpid.add(deviceId); | ||
275 | + } | ||
276 | + | ||
277 | + | ||
278 | + } | ||
279 | + | ||
280 | + | ||
281 | + | ||
282 | + private class TestPacketContext implements PacketContext { | ||
283 | + | ||
284 | + protected Device device; | ||
285 | + protected boolean blocked = false; | ||
286 | + | ||
287 | + public TestPacketContext(Device dev) { | ||
288 | + device = dev; | ||
289 | + } | ||
290 | + | ||
291 | + @Override | ||
292 | + public long time() { | ||
293 | + return 0; | ||
294 | + } | ||
295 | + | ||
296 | + @Override | ||
297 | + public InboundPacket inPacket() { | ||
298 | + ONOSLLDP lldp = new ONOSLLDP(); | ||
299 | + lldp.setChassisId(device.chassisId()); | ||
300 | + lldp.setPortId((int) pd1.number().toLong()); | ||
301 | + lldp.setDevice(deviceService.getDevice(DID1).id().toString()); | ||
302 | + | ||
303 | + | ||
304 | + Ethernet ethPacket = new Ethernet(); | ||
305 | + ethPacket.setEtherType(Ethernet.TYPE_LLDP); | ||
306 | + ethPacket.setDestinationMACAddress(ONOSLLDP.LLDP_NICIRA); | ||
307 | + ethPacket.setPayload(lldp); | ||
308 | + ethPacket.setPad(true); | ||
309 | + | ||
310 | + | ||
311 | + | ||
312 | + ethPacket.setSourceMACAddress("DE:AD:BE:EF:BA:11"); | ||
313 | + | ||
314 | + ConnectPoint cp = new ConnectPoint(device.id(), pd3.number()); | ||
315 | + | ||
316 | + return new DefaultInboundPacket(cp, ethPacket, | ||
317 | + ByteBuffer.wrap(ethPacket.serialize())); | ||
318 | + | ||
319 | + } | ||
320 | + | ||
321 | + @Override | ||
322 | + public OutboundPacket outPacket() { | ||
323 | + return null; | ||
324 | + } | ||
325 | + | ||
326 | + @Override | ||
327 | + public TrafficTreatment.Builder treatmentBuilder() { | ||
328 | + return null; | ||
329 | + } | ||
330 | + | ||
331 | + @Override | ||
332 | + public void send() { | ||
333 | + | ||
334 | + } | ||
335 | + | ||
336 | + @Override | ||
337 | + public boolean block() { | ||
338 | + blocked = true; | ||
339 | + return blocked; | ||
340 | + } | ||
341 | + | ||
342 | + @Override | ||
343 | + public boolean isHandled() { | ||
344 | + return blocked; | ||
345 | + } | ||
346 | + | ||
347 | + } | ||
348 | + | ||
349 | + private class TestPacketService implements PacketService { | ||
350 | + | ||
351 | + @Override | ||
352 | + public void addProcessor(PacketProcessor processor, int priority) { | ||
353 | + testProcessor = processor; | ||
354 | + } | ||
355 | + | ||
356 | + @Override | ||
357 | + public void removeProcessor(PacketProcessor processor) { | ||
358 | + | ||
359 | + } | ||
360 | + | ||
361 | + @Override | ||
362 | + public void emit(OutboundPacket packet) { | ||
363 | + | ||
364 | + } | ||
365 | + } | ||
366 | + | ||
367 | + private class TestDeviceService implements DeviceService { | ||
368 | + | ||
369 | + private Map<DeviceId, Device> devices = new HashMap<>(); | ||
370 | + private final ArrayListMultimap<DeviceId, Port> ports = | ||
371 | + ArrayListMultimap.create(); | ||
372 | + | ||
373 | + public TestDeviceService() { | ||
374 | + Device d1 = new DefaultDevice(ProviderId.NONE, DID1, Device.Type.SWITCH, | ||
375 | + "TESTMF", "TESTHW", "TESTSW", "TESTSN", new ChassisId()); | ||
376 | + Device d2 = new DefaultDevice(ProviderId.NONE, DID2, Device.Type.SWITCH, | ||
377 | + "TESTMF", "TESTHW", "TESTSW", "TESTSN", new ChassisId()); | ||
378 | + devices.put(DID1, d1); | ||
379 | + devices.put(DID2, d2); | ||
380 | + | ||
381 | + pd1 = new DefaultPort(d1, PortNumber.portNumber(1), true); | ||
382 | + pd2 = new DefaultPort(d1, PortNumber.portNumber(2), true); | ||
383 | + pd3 = new DefaultPort(d2, PortNumber.portNumber(1), true); | ||
384 | + pd4 = new DefaultPort(d2, PortNumber.portNumber(2), true); | ||
385 | + | ||
386 | + ports.putAll(DID1, Lists.newArrayList(pd1, pd2)); | ||
387 | + ports.putAll(DID2, Lists.newArrayList(pd3, pd4)); | ||
388 | + | ||
389 | + | ||
390 | + } | ||
391 | + | ||
392 | + @Override | ||
393 | + public int getDeviceCount() { | ||
394 | + return devices.values().size(); | ||
395 | + } | ||
396 | + | ||
397 | + @Override | ||
398 | + public Iterable<Device> getDevices() { | ||
399 | + return devices.values(); | ||
400 | + } | ||
401 | + | ||
402 | + @Override | ||
403 | + public Device getDevice(DeviceId deviceId) { | ||
404 | + return devices.get(deviceId); | ||
405 | + } | ||
406 | + | ||
407 | + @Override | ||
408 | + public MastershipRole getRole(DeviceId deviceId) { | ||
409 | + return MastershipRole.MASTER; | ||
410 | + } | ||
411 | + | ||
412 | + @Override | ||
413 | + public List<Port> getPorts(DeviceId deviceId) { | ||
414 | + return ports.get(deviceId); | ||
415 | + } | ||
416 | + | ||
417 | + @Override | ||
418 | + public Port getPort(DeviceId deviceId, PortNumber portNumber) { | ||
419 | + for (Port p : ports.get(deviceId)) { | ||
420 | + if (p.number().equals(portNumber)) { | ||
421 | + return p; | ||
422 | + } | ||
423 | + } | ||
424 | + return null; | ||
425 | + } | ||
426 | + | ||
427 | + @Override | ||
428 | + public boolean isAvailable(DeviceId deviceId) { | ||
429 | + return true; | ||
430 | + } | ||
431 | + | ||
432 | + @Override | ||
433 | + public void addListener(DeviceListener listener) { | ||
434 | + deviceListener = listener; | ||
435 | + | ||
436 | + } | ||
437 | + | ||
438 | + @Override | ||
439 | + public void removeListener(DeviceListener listener) { | ||
440 | + | ||
441 | + } | ||
442 | + } | ||
443 | + | ||
444 | + private final class TestMasterShipService implements MastershipService { | ||
445 | + | ||
446 | + @Override | ||
447 | + public MastershipRole getLocalRole(DeviceId deviceId) { | ||
448 | + return MastershipRole.MASTER; | ||
449 | + } | ||
450 | + | ||
451 | + @Override | ||
452 | + public MastershipRole requestRoleFor(DeviceId deviceId) { | ||
453 | + return null; | ||
454 | + } | ||
455 | + | ||
456 | + @Override | ||
457 | + public void relinquishMastership(DeviceId deviceId) { | ||
458 | + | ||
459 | + } | ||
460 | + | ||
461 | + @Override | ||
462 | + public NodeId getMasterFor(DeviceId deviceId) { | ||
463 | + return null; | ||
464 | + } | ||
465 | + | ||
466 | + @Override | ||
467 | + public Set<DeviceId> getDevicesOf(NodeId nodeId) { | ||
468 | + return null; | ||
469 | + } | ||
470 | + | ||
471 | + @Override | ||
472 | + public MastershipTermService requestTermService() { | ||
473 | + return null; | ||
474 | + } | ||
475 | + | ||
476 | + @Override | ||
477 | + public void addListener(MastershipListener listener) { | ||
478 | + | ||
479 | + } | ||
480 | + | ||
481 | + @Override | ||
482 | + public void removeListener(MastershipListener listener) { | ||
483 | + | ||
484 | + } | ||
485 | + } | ||
486 | + | ||
487 | + | ||
488 | +} |
-
Please register or login to post a comment