blob: 309bfb487b827513b0e8f88b48d6b995676e5fc5 [file] [log] [blame]
weibitf7c31a42014-06-23 16:51:01 -07001package net.onrc.onos.core.topology;
2
3import static org.easymock.EasyMock.anyObject;
4import static org.easymock.EasyMock.createMock;
5import static org.easymock.EasyMock.eq;
6import static org.easymock.EasyMock.expect;
7import static org.easymock.EasyMock.replay;
8import static org.easymock.EasyMock.verify;
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07009import static org.junit.Assert.*;
10import static org.hamcrest.Matchers.*;
weibitf7c31a42014-06-23 16:51:01 -070011
12import java.util.ArrayList;
13import java.util.Collection;
14import java.util.List;
15import java.util.concurrent.CopyOnWriteArrayList;
16
17import net.floodlightcontroller.util.MACAddress;
18import net.onrc.onos.core.datagrid.IDatagridService;
19import net.onrc.onos.core.datagrid.IEventChannel;
20import net.onrc.onos.core.datagrid.IEventChannelListener;
21import net.onrc.onos.core.registry.IControllerRegistryService;
22import net.onrc.onos.core.util.Dpid;
23import net.onrc.onos.core.util.PortNumber;
24import net.onrc.onos.core.util.SwitchPort;
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070025import net.onrc.onos.core.util.TestUtils;
weibitf7c31a42014-06-23 16:51:01 -070026
27import org.easymock.EasyMock;
28import org.junit.After;
29import org.junit.Before;
30import org.junit.Test;
31
32/**
33 * Unit tests for the TopologyManager class in the Topology module.
34 * These test cases only check the sanity of functions in the TopologyManager.
35 * Note that we do not test the eventHandler functions in the TopologyManager class.
36 * DatagridService, DataStoreService, eventChannel, and controllerRegistryService are mocked out.
37 */
38public class TopologyManagerTest {
39 private TopologyManager theTopologyManager;
40 private final String eventChannelName = "onos.topology";
41 private IEventChannel<byte[], TopologyEvent> eventChannel;
42 private IDatagridService datagridService;
43 private TopologyDatastore dataStoreService;
44 private IControllerRegistryService registryService;
45 private CopyOnWriteArrayList<ITopologyListener> topologyListeners;
46 private Collection<TopologyEvent> allTopologyEvents;
47
48 @SuppressWarnings("unchecked")
49 @Before
50 public void setUp() throws Exception {
51 // Mock objects for testing
52 datagridService = EasyMock.createNiceMock(IDatagridService.class);
53 dataStoreService = EasyMock.createNiceMock(TopologyDatastore.class);
54 registryService = createMock(IControllerRegistryService.class);
55 eventChannel = EasyMock.createNiceMock(IEventChannel.class);
56
57 expect(datagridService.createChannel(
58 eq(eventChannelName),
59 eq(byte[].class),
60 eq(TopologyEvent.class)))
61 .andReturn(eventChannel).once();
62
63 expect(datagridService.addListener(
64 eq(eventChannelName),
65 anyObject(IEventChannelListener.class),
66 eq(byte[].class),
67 eq(TopologyEvent.class)))
68 .andReturn(eventChannel).once();
69
70 expect(dataStoreService.addSwitch(
71 anyObject(SwitchEvent.class),
72 anyObject(Collection.class)))
73 .andReturn(true).anyTimes();
74
75 expect(dataStoreService.deactivateSwitch(
76 anyObject(SwitchEvent.class),
77 anyObject(Collection.class)))
78 .andReturn(true).anyTimes();
79
80 expect(dataStoreService.addPort(
81 anyObject(PortEvent.class)))
82 .andReturn(true).anyTimes();
83
84 expect(dataStoreService.deactivatePort(
85 anyObject(PortEvent.class)))
86 .andReturn(true).anyTimes();
87
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070088 expect(dataStoreService.addHost(
89 anyObject(HostEvent.class)))
weibitf7c31a42014-06-23 16:51:01 -070090 .andReturn(true).anyTimes();
91
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070092 expect(dataStoreService.removeHost(
93 anyObject(HostEvent.class)))
weibitf7c31a42014-06-23 16:51:01 -070094 .andReturn(true).anyTimes();
95
96 expect(dataStoreService.addLink(
97 anyObject(LinkEvent.class)))
98 .andReturn(true).anyTimes();
99
100 expect(dataStoreService.removeLink(
101 anyObject(LinkEvent.class)))
102 .andReturn(true).anyTimes();
103
104 replay(datagridService);
105 replay(registryService);
106 replay(dataStoreService);
107
108 allTopologyEvents = new CopyOnWriteArrayList<>();
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700109 expect(eventChannel.getAllEntries())
110 .andReturn(allTopologyEvents).anyTimes();
111 }
weibitf7c31a42014-06-23 16:51:01 -0700112
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700113 private void setupTopologyManager() {
weibitf7c31a42014-06-23 16:51:01 -0700114 // Create a topologyManager object for testing
115 topologyListeners = new CopyOnWriteArrayList<>();
116 theTopologyManager = new TopologyManager(registryService, topologyListeners);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700117
118 // replace EventHandler to avoid thread from starting
119 TestUtils.setField(theTopologyManager, "eventHandler",
120 EasyMock.createNiceMock(TopologyManager.EventHandler.class));
weibitf7c31a42014-06-23 16:51:01 -0700121 theTopologyManager.startup(datagridService);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700122
123 // replace data store with Mocked object
124 TestUtils.setField(theTopologyManager, "datastore", dataStoreService);
weibitf7c31a42014-06-23 16:51:01 -0700125 }
126
127 @After
128 public void tearDown() throws Exception {
129
130 }
131
132 /**
133 * Test the Switch discovered and Port discovered functions.
134 */
135 @Test
136 public void testPutSwitchAndPortDiscoveryEvent() {
137 // Mock the eventChannel functions first
138 eventChannel.addEntry(anyObject(byte[].class),
139 anyObject(TopologyEvent.class));
140 EasyMock.expectLastCall().times(3, 3); // (1 switch + 1 port), 1 port
141 replay(eventChannel);
142
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700143 setupTopologyManager();
144
weibitf7c31a42014-06-23 16:51:01 -0700145 // mockSwitch has one port
146 Dpid swDPId = new Dpid(100L);
147 PortNumber portId = new PortNumber((short) 1);
148
149 // Generate a new switch event along with a port event
150 SwitchEvent switchEvent = new SwitchEvent(swDPId);
151
152 Collection<PortEvent> portEvents = new ArrayList<PortEvent>();
153 portEvents.add(new PortEvent(swDPId, portId));
154
155 // Call the topologyManager function for adding a switch
156 theTopologyManager.putSwitchDiscoveryEvent(switchEvent, portEvents);
157
158 for (PortEvent portEvent : portEvents) {
159 // Call the topologyManager function for adding a port
160 theTopologyManager.putPortDiscoveryEvent(portEvent);
161 }
162
163 // Verify the function calls
164 verify(eventChannel);
165
166 }
167
168 /**
169 * Test the switch and port removed functions.
170 */
171 @Test
172 public void testRemoveSwitchAndPortDiscoveryEvent() {
173 // Mock the eventChannel functions first
174 eventChannel.removeEntry(anyObject(byte[].class));
175 EasyMock.expectLastCall().times(2, 2); //1 switch, 1 port
176 replay(eventChannel);
177
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700178 setupTopologyManager();
179
weibitf7c31a42014-06-23 16:51:01 -0700180 Dpid swDPId = new Dpid(100L);
181 PortNumber portId = new PortNumber((short) 1);
182
183 // Generate a port event
184 Collection<PortEvent> portEvents = new ArrayList<PortEvent>();
185 portEvents.add(new PortEvent(swDPId, portId));
186
187 // Call the topologyManager function for removing a port
188 for (PortEvent portEvent : portEvents) {
189 theTopologyManager.removePortDiscoveryEvent(portEvent);
190 }
191
192 // Call the topologyManager function for removing a switch
193 SwitchEvent switchEvent = new SwitchEvent(swDPId);
194 theTopologyManager.removeSwitchDiscoveryEvent(switchEvent);
195
196 // Verify the function calls
197 verify(eventChannel);
198
199 }
200
201 /**
202 * Test the device discovered function.
203 */
204 @Test
205 public void testPutDeviceDiscoveryEvent() {
206 // Mock the eventChannel functions first
207 eventChannel.addEntry(anyObject(byte[].class),
208 anyObject(TopologyEvent.class));
209 EasyMock.expectLastCall().times(1, 1); // 1 device
210 replay(eventChannel);
211
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700212 setupTopologyManager();
213
weibitf7c31a42014-06-23 16:51:01 -0700214 long swDPId = 100L;
215 long portId = 1L;
216
217 // Generate a new device event
218 MACAddress devMac = MACAddress.valueOf("00:AA:11:BB:33:CC");
219 SwitchPort sp = new SwitchPort(swDPId, portId);
220 List<SwitchPort> spLists = new ArrayList<SwitchPort>();
221 spLists.add(sp);
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700222 HostEvent hostEvent = new HostEvent(devMac);
223 hostEvent.setAttachmentPoints(spLists);
weibitf7c31a42014-06-23 16:51:01 -0700224
225 // Call the topologyManager function for adding a device
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700226 theTopologyManager.putHostDiscoveryEvent(hostEvent);
weibitf7c31a42014-06-23 16:51:01 -0700227
228 // Verify the function calls
229 verify(eventChannel);
230 }
231
232 /**
233 * Test the device removed function.
234 */
235 @Test
236 public void testRemoveDeviceDiscoveryEvent() {
237 // Mock the eventChannel functions first
238 eventChannel.removeEntry(anyObject(byte[].class));
239 EasyMock.expectLastCall().times(1, 1); // 1 device
240 replay(eventChannel);
241
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700242 setupTopologyManager();
243
weibitf7c31a42014-06-23 16:51:01 -0700244 long swDPId = 100L;
245 long portId = 1L;
246
247 // Generate a new device event
248 MACAddress devMac = MACAddress.valueOf("00:AA:11:BB:33:CC");
249 SwitchPort sp = new SwitchPort(swDPId, portId);
250 List<SwitchPort> spLists = new ArrayList<SwitchPort>();
251 spLists.add(sp);
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700252 HostEvent hostEvent = new HostEvent(devMac);
253 hostEvent.setAttachmentPoints(spLists);
weibitf7c31a42014-06-23 16:51:01 -0700254
255 // Call the topologyManager function for removing a device
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700256 theTopologyManager.removeHostDiscoveryEvent(hostEvent);
weibitf7c31a42014-06-23 16:51:01 -0700257
258 // Verify the function calls
259 verify(eventChannel);
260 }
261
262 /**
263 * Test the link discovered function.
264 */
265 @Test
266 public void testPutLinkDiscoveryEvent() {
267 // Mock the eventChannel functions first
268 eventChannel.addEntry(anyObject(byte[].class),
269 anyObject(TopologyEvent.class));
270 EasyMock.expectLastCall().times(5, 5); // (2 switch + 2 port + 1 link)
271 replay(eventChannel);
272
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700273 setupTopologyManager();
274
weibitf7c31a42014-06-23 16:51:01 -0700275 // Assign the switch and port IDs
276 Dpid sw1DPId = new Dpid(100L);
277 PortNumber port1Id = new PortNumber((short) 1);
278 Dpid sw2DPId = new Dpid(200L);
279 PortNumber port2Id = new PortNumber((short) 2);
280
281 // Generate the switch and port events
282 SwitchEvent switchEvent1 = new SwitchEvent(sw1DPId);
283 Collection<PortEvent> portEvents1 = new ArrayList<PortEvent>();
284 portEvents1.add(new PortEvent(sw1DPId, port1Id));
285
286 // Call the topologyManager function for adding a switch
287 theTopologyManager.putSwitchDiscoveryEvent(switchEvent1, portEvents1);
288
289 // Generate the switch and port events
290 SwitchEvent switchEvent2 = new SwitchEvent(sw2DPId);
291 Collection<PortEvent> portEvents2 = new ArrayList<PortEvent>();
292 portEvents2.add(new PortEvent(sw2DPId, port2Id));
293
294 // Call the topologyManager function for adding a switch
295 theTopologyManager.putSwitchDiscoveryEvent(switchEvent2, portEvents2);
296
297 // Create the link
298 LinkEvent linkEvent = new LinkEvent(sw1DPId, port1Id, sw2DPId, port2Id);
299 theTopologyManager.putLinkDiscoveryEvent(linkEvent);
300
301 // Verify the function calls
302 verify(eventChannel);
303 }
304
305 /**
306 * Test the link removed function.
307 */
308 @Test
309 public void testRemoveLinkDiscoveryEvent() {
310 // Mock the eventChannel functions first
311 eventChannel.removeEntry(anyObject(byte[].class));
312 EasyMock.expectLastCall().times(1, 1); // (1 link)
313 replay(eventChannel);
314
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700315 setupTopologyManager();
316
weibitf7c31a42014-06-23 16:51:01 -0700317 // Assign the switch and port IDs
318 Dpid sw1DPId = new Dpid(100L);
319 PortNumber port1Id = new PortNumber((short) 1);
320 Dpid sw2DPId = new Dpid(200L);
321 PortNumber port2Id = new PortNumber((short) 2);
322
323 // Generate the switch and port events
324 SwitchEvent switchEvent1 = new SwitchEvent(sw1DPId);
325 Collection<PortEvent> portEvents1 = new ArrayList<PortEvent>();
326 portEvents1.add(new PortEvent(sw1DPId, port1Id));
327
328 // Call the topologyManager function for adding a switch
329 theTopologyManager.putSwitchDiscoveryEvent(switchEvent1, portEvents1);
330
331 // Generate the switch and port events
332 SwitchEvent switchEvent2 = new SwitchEvent(sw2DPId);
333 Collection<PortEvent> portEvents2 = new ArrayList<PortEvent>();
334 portEvents2.add(new PortEvent(sw2DPId, port2Id));
335
336 // Call the topologyManager function for adding a switch
337 theTopologyManager.putSwitchDiscoveryEvent(switchEvent2, portEvents2);
338
339 // Remove the link
340 LinkEvent linkEventRemove = new LinkEvent(sw1DPId, port1Id, sw2DPId, port2Id);
341 theTopologyManager.removeLinkDiscoveryEvent(linkEventRemove);
342
343 // Verify the function calls
344 verify(eventChannel);
345 }
346
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700347 /**
348 * Test to confirm topology replica transformation.
349 */
350 @Test
351 public void testAddSwitch() {
352 setupTopologyManager();
353
354 final Dpid dpid = new Dpid(1);
355 SwitchEvent sw = new SwitchEvent(dpid);
356 sw.createStringAttribute("foo", "bar");
357
358 TestUtils.callMethod(theTopologyManager, "addSwitch", SwitchEvent.class, sw);
359
360 // check topology structure
361 TopologyInternal topology = (TopologyInternal) theTopologyManager.getTopology();
362 SwitchEvent swInTopo = topology.getSwitchEvent(dpid);
363 assertEquals(sw, swInTopo);
364 assertTrue(swInTopo.isFrozen());
365 assertEquals("bar", swInTopo.getStringAttribute("foo"));
366
367 // check events to be fired
368 List<SwitchEvent> apiAddedSwitchEvents
369 = TestUtils.getField(theTopologyManager, "apiAddedSwitchEvents");
370 assertThat(apiAddedSwitchEvents, hasItem(sw));
371 }
372
373 /**
374 * Test to confirm topology replica transformation.
375 */
376 @Test
377 public void testAddPort() {
378 setupTopologyManager();
379
380 final Dpid dpid = new Dpid(1);
381 SwitchEvent sw = new SwitchEvent(dpid);
382 sw.createStringAttribute("foo", "bar");
383
384 final PortNumber portNumber = new PortNumber((short) 2);
385 PortEvent port = new PortEvent(dpid, portNumber);
386 port.createStringAttribute("fuzz", "buzz");
387
388 TestUtils.callMethod(theTopologyManager, "addSwitch", SwitchEvent.class, sw);
389 TestUtils.callMethod(theTopologyManager, "addPort", PortEvent.class, port);
390
391 // check topology structure
392 TopologyInternal topology = (TopologyInternal) theTopologyManager.getTopology();
393 SwitchEvent swInTopo = topology.getSwitchEvent(dpid);
394 assertEquals(sw, swInTopo);
395 assertTrue(swInTopo.isFrozen());
396 assertEquals("bar", swInTopo.getStringAttribute("foo"));
397
398 final SwitchPort portId = new SwitchPort(dpid, portNumber);
399 PortEvent portInTopo = topology.getPortEvent(portId);
400 assertEquals(port, portInTopo);
401 assertTrue(portInTopo.isFrozen());
402 assertEquals("buzz", portInTopo.getStringAttribute("fuzz"));
403
404 // check events to be fired
405 List<PortEvent> apiAddedPortEvents
406 = TestUtils.getField(theTopologyManager, "apiAddedPortEvents");
407 assertThat(apiAddedPortEvents, hasItem(port));
408 }
409
410 /**
411 * Test to confirm topology replica transformation.
412 */
413 @Test
414 public void testRemovePortThenSwitch() {
415 setupTopologyManager();
416
417 final Dpid dpid = new Dpid(1);
418 SwitchEvent sw = new SwitchEvent(dpid);
419 sw.createStringAttribute("foo", "bar");
420
421 final PortNumber portNumber = new PortNumber((short) 2);
422 PortEvent port = new PortEvent(dpid, portNumber);
423 port.createStringAttribute("fuzz", "buzz");
424
425 TestUtils.callMethod(theTopologyManager, "addSwitch", SwitchEvent.class, sw);
426 TestUtils.callMethod(theTopologyManager, "addPort", PortEvent.class, port);
427
428 // check topology structure
429 TopologyInternal topology = (TopologyInternal) theTopologyManager.getTopology();
430 SwitchEvent swInTopo = topology.getSwitchEvent(dpid);
431 assertEquals(sw, swInTopo);
432 assertTrue(swInTopo.isFrozen());
433 assertEquals("bar", swInTopo.getStringAttribute("foo"));
434
435 final SwitchPort portId = new SwitchPort(dpid, portNumber);
436 PortEvent portInTopo = topology.getPortEvent(portId);
437 assertEquals(port, portInTopo);
438 assertTrue(portInTopo.isFrozen());
439 assertEquals("buzz", portInTopo.getStringAttribute("fuzz"));
440
441 // remove in proper order
442 TestUtils.callMethod(theTopologyManager, "removePort",
443 PortEvent.class, new PortEvent(port));
444 TestUtils.callMethod(theTopologyManager, "removeSwitch",
445 SwitchEvent.class, new SwitchEvent(sw));
446
447
448 // check events to be fired
449 List<PortEvent> apiRemovedPortEvents
450 = TestUtils.getField(theTopologyManager, "apiRemovedPortEvents");
451 assertThat(apiRemovedPortEvents, hasItem(port));
452 List<SwitchEvent> apiRemovedSwitchEvents
453 = TestUtils.getField(theTopologyManager, "apiRemovedSwitchEvents");
454 assertThat(apiRemovedSwitchEvents, hasItem(sw));
455 }
456
457 /**
458 * Test to confirm topology replica transformation.
459 */
460 @Test
461 public void testRemoveSwitch() {
462 setupTopologyManager();
463
464 final Dpid dpid = new Dpid(1);
465 SwitchEvent sw = new SwitchEvent(dpid);
466 sw.createStringAttribute("foo", "bar");
467
468 final PortNumber portNumber = new PortNumber((short) 2);
469 PortEvent port = new PortEvent(dpid, portNumber);
470 port.createStringAttribute("fuzz", "buzz");
471
472 TestUtils.callMethod(theTopologyManager, "addSwitch", SwitchEvent.class, sw);
473 TestUtils.callMethod(theTopologyManager, "addPort", PortEvent.class, port);
474
475 // check topology structure
476 TopologyInternal topology = (TopologyInternal) theTopologyManager.getTopology();
477 SwitchEvent swInTopo = topology.getSwitchEvent(dpid);
478 assertEquals(sw, swInTopo);
479 assertTrue(swInTopo.isFrozen());
480 assertEquals("bar", swInTopo.getStringAttribute("foo"));
481
482 final SwitchPort portId = new SwitchPort(dpid, portNumber);
483 PortEvent portInTopo = topology.getPortEvent(portId);
484 assertEquals(port, portInTopo);
485 assertTrue(portInTopo.isFrozen());
486 assertEquals("buzz", portInTopo.getStringAttribute("fuzz"));
487
488 // remove in in-proper order
489// TestUtils.callMethod(theTopologyManager, "removePort",
490// PortEvent.class, new PortEvent(port));
491 TestUtils.callMethod(theTopologyManager, "removeSwitch",
492 SwitchEvent.class, new SwitchEvent(sw));
493
494
495 // check events to be fired
496 // outcome should be the same as #testRemovePortThenSwitch
497 List<PortEvent> apiRemovedPortEvents
498 = TestUtils.getField(theTopologyManager, "apiRemovedPortEvents");
499 assertThat(apiRemovedPortEvents, hasItem(port));
500 List<SwitchEvent> apiRemovedSwitchEvents
501 = TestUtils.getField(theTopologyManager, "apiRemovedSwitchEvents");
502 assertThat(apiRemovedSwitchEvents, hasItem(sw));
503 }
504
505 /**
506 * Test to confirm topology replica transformation.
507 */
508 @Test
509 public void testAddLink() {
510 setupTopologyManager();
511
512 final Dpid dpid = new Dpid(1);
513 SwitchEvent sw = new SwitchEvent(dpid);
514 sw.createStringAttribute("foo", "bar");
515
516 final PortNumber portNumberA = new PortNumber((short) 2);
517 PortEvent portA = new PortEvent(dpid, portNumberA);
518 portA.createStringAttribute("fuzz", "buzz");
519
520 final PortNumber portNumberB = new PortNumber((short) 3);
521 PortEvent portB = new PortEvent(dpid, portNumberB);
522 portB.createStringAttribute("fizz", "buz");
523
524 LinkEvent linkA = new LinkEvent(portA.getSwitchPort(), portB.getSwitchPort());
525 linkA.createStringAttribute(TopologyElement.TYPE,
526 TopologyElement.TYPE_OPTICAL_LAYER);
527 LinkEvent linkB = new LinkEvent(portB.getSwitchPort(), portA.getSwitchPort());
528 linkB.createStringAttribute(TopologyElement.TYPE,
529 TopologyElement.TYPE_OPTICAL_LAYER);
530
531 TestUtils.callMethod(theTopologyManager, "addSwitch", SwitchEvent.class, sw);
532 TestUtils.callMethod(theTopologyManager, "addPort", PortEvent.class, portA);
533 TestUtils.callMethod(theTopologyManager, "addPort", PortEvent.class, portB);
534 TestUtils.callMethod(theTopologyManager, "addLink", LinkEvent.class, linkA);
535 TestUtils.callMethod(theTopologyManager, "addLink", LinkEvent.class, linkB);
536
537 // check topology structure
538 TopologyInternal topology = (TopologyInternal) theTopologyManager.getTopology();
539 SwitchEvent swInTopo = topology.getSwitchEvent(dpid);
540 assertEquals(sw, swInTopo);
541 assertTrue(swInTopo.isFrozen());
542 assertEquals("bar", swInTopo.getStringAttribute("foo"));
543
544 final SwitchPort portIdA = new SwitchPort(dpid, portNumberA);
545 PortEvent portAInTopo = topology.getPortEvent(portIdA);
546 assertEquals(portA, portAInTopo);
547 assertTrue(portAInTopo.isFrozen());
548 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
549
550 final SwitchPort portIdB = new SwitchPort(dpid, portNumberB);
551 PortEvent portBInTopo = topology.getPortEvent(portIdB);
552 assertEquals(portB, portBInTopo);
553 assertTrue(portBInTopo.isFrozen());
554 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
555
556 LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple());
557 assertEquals(linkA, linkAInTopo);
558 assertTrue(linkAInTopo.isFrozen());
559 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER, linkAInTopo.getType());
560
561 LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple());
562 assertEquals(linkB, linkBInTopo);
563 assertTrue(linkBInTopo.isFrozen());
564 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER, linkBInTopo.getType());
565
566 // check events to be fired
567 List<LinkEvent> apiAddedLinkEvents
568 = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents");
569 assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB));
570 }
571
572 /**
573 * Test to confirm topology replica transformation.
574 */
575 @Test
576 public void testAddLinkKickingOffHost() {
577 setupTopologyManager();
578
579 final Dpid dpid = new Dpid(1);
580 SwitchEvent sw = new SwitchEvent(dpid);
581 sw.createStringAttribute("foo", "bar");
582
583 final PortNumber portNumberA = new PortNumber((short) 2);
584 PortEvent portA = new PortEvent(dpid, portNumberA);
585 portA.createStringAttribute("fuzz", "buzz");
586
587 final PortNumber portNumberB = new PortNumber((short) 3);
588 PortEvent portB = new PortEvent(dpid, portNumberB);
589 portB.createStringAttribute("fizz", "buz");
590
591 final PortNumber portNumberC = new PortNumber((short) 4);
592 PortEvent portC = new PortEvent(dpid, portNumberC);
593 portC.createStringAttribute("fizz", "buz");
594
595 final MACAddress macA = MACAddress.valueOf(666L);
596 HostEvent hostA = new HostEvent(macA);
597 hostA.addAttachmentPoint(portA.getSwitchPort());
598 final long timestampA = 392893200L;
599 hostA.setLastSeenTime(timestampA);
600
601 final MACAddress macB = MACAddress.valueOf(999L);
602 HostEvent hostB = new HostEvent(macB);
603 hostB.addAttachmentPoint(portB.getSwitchPort());
604 hostB.addAttachmentPoint(portC.getSwitchPort());
605 final long timestampB = 392893201L;
606 hostB.setLastSeenTime(timestampB);
607
608
609 LinkEvent linkA = new LinkEvent(portA.getSwitchPort(), portB.getSwitchPort());
610 linkA.createStringAttribute(TopologyElement.TYPE,
611 TopologyElement.TYPE_OPTICAL_LAYER);
612 LinkEvent linkB = new LinkEvent(portB.getSwitchPort(), portA.getSwitchPort());
613 linkB.createStringAttribute(TopologyElement.TYPE,
614 TopologyElement.TYPE_OPTICAL_LAYER);
615
616 TestUtils.callMethod(theTopologyManager, "addSwitch", SwitchEvent.class, sw);
617 TestUtils.callMethod(theTopologyManager, "addPort", PortEvent.class, portA);
618 TestUtils.callMethod(theTopologyManager, "addPort", PortEvent.class, portB);
619 TestUtils.callMethod(theTopologyManager, "addPort", PortEvent.class, portC);
620 TestUtils.callMethod(theTopologyManager, "addHost", HostEvent.class, hostA);
621 TestUtils.callMethod(theTopologyManager, "addHost", HostEvent.class, hostB);
622
623 TestUtils.callMethod(theTopologyManager, "addLink", LinkEvent.class, linkA);
624 TestUtils.callMethod(theTopologyManager, "addLink", LinkEvent.class, linkB);
625
626 // check topology structure
627 TopologyInternal topology = (TopologyInternal) theTopologyManager.getTopology();
628 SwitchEvent swInTopo = topology.getSwitchEvent(dpid);
629 assertEquals(sw, swInTopo);
630 assertTrue(swInTopo.isFrozen());
631 assertEquals("bar", swInTopo.getStringAttribute("foo"));
632
633 final SwitchPort portIdA = new SwitchPort(dpid, portNumberA);
634 PortEvent portAInTopo = topology.getPortEvent(portIdA);
635 assertEquals(portA, portAInTopo);
636 assertTrue(portAInTopo.isFrozen());
637 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
638
639 final SwitchPort portIdB = new SwitchPort(dpid, portNumberB);
640 PortEvent portBInTopo = topology.getPortEvent(portIdB);
641 assertEquals(portB, portBInTopo);
642 assertTrue(portBInTopo.isFrozen());
643 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
644
645 // hostA expected to be removed
646 assertNull(topology.getHostEvent(macA));
647 // hostB expected to be there with reduced attachment point
648 HostEvent hostBrev = new HostEvent(macB);
649 hostBrev.addAttachmentPoint(portC.getSwitchPort());
650 hostBrev.setLastSeenTime(timestampB);
651 hostBrev.freeze();
652 assertEquals(hostBrev, topology.getHostEvent(macB));
653
654
655 LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple());
656 assertEquals(linkA, linkAInTopo);
657 assertTrue(linkAInTopo.isFrozen());
658 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER, linkAInTopo.getType());
659
660 LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple());
661 assertEquals(linkB, linkBInTopo);
662 assertTrue(linkBInTopo.isFrozen());
663 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER, linkBInTopo.getType());
664
665 // check events to be fired
666 List<HostEvent> apiAddedHostEvents
667 = TestUtils.getField(theTopologyManager, "apiAddedHostEvents");
668 assertThat(apiAddedHostEvents, hasItem(hostBrev));
669
670 List<HostEvent> apiRemovedHostEvents
671 = TestUtils.getField(theTopologyManager, "apiRemovedHostEvents");
672 assertThat(apiRemovedHostEvents, hasItem(hostA));
673 List<LinkEvent> apiAddedLinkEvents
674 = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents");
675 assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB));
676 }
677
678 /**
679 * Test to confirm topology replica transformation.
680 */
681 @Test
682 public void testRemoveLink() {
683 setupTopologyManager();
684
685 final Dpid dpid = new Dpid(1);
686 SwitchEvent sw = new SwitchEvent(dpid);
687 sw.createStringAttribute("foo", "bar");
688
689 final PortNumber portNumberA = new PortNumber((short) 2);
690 PortEvent portA = new PortEvent(dpid, portNumberA);
691 portA.createStringAttribute("fuzz", "buzz");
692
693 final PortNumber portNumberB = new PortNumber((short) 3);
694 PortEvent portB = new PortEvent(dpid, portNumberB);
695 portB.createStringAttribute("fizz", "buz");
696
697 LinkEvent linkA = new LinkEvent(portA.getSwitchPort(), portB.getSwitchPort());
698 linkA.createStringAttribute(TopologyElement.TYPE,
699 TopologyElement.TYPE_OPTICAL_LAYER);
700 LinkEvent linkB = new LinkEvent(portB.getSwitchPort(), portA.getSwitchPort());
701 linkB.createStringAttribute(TopologyElement.TYPE,
702 TopologyElement.TYPE_OPTICAL_LAYER);
703
704 TestUtils.callMethod(theTopologyManager, "addSwitch", SwitchEvent.class, sw);
705 TestUtils.callMethod(theTopologyManager, "addPort", PortEvent.class, portA);
706 TestUtils.callMethod(theTopologyManager, "addPort", PortEvent.class, portB);
707 TestUtils.callMethod(theTopologyManager, "addLink", LinkEvent.class, linkA);
708 TestUtils.callMethod(theTopologyManager, "addLink", LinkEvent.class, linkB);
709
710 // check topology structure
711 TopologyInternal topology = (TopologyInternal) theTopologyManager.getTopology();
712 SwitchEvent swInTopo = topology.getSwitchEvent(dpid);
713 assertEquals(sw, swInTopo);
714 assertTrue(swInTopo.isFrozen());
715 assertEquals("bar", swInTopo.getStringAttribute("foo"));
716
717 final SwitchPort portIdA = new SwitchPort(dpid, portNumberA);
718 PortEvent portAInTopo = topology.getPortEvent(portIdA);
719 assertEquals(portA, portAInTopo);
720 assertTrue(portAInTopo.isFrozen());
721 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
722
723 final SwitchPort portIdB = new SwitchPort(dpid, portNumberB);
724 PortEvent portBInTopo = topology.getPortEvent(portIdB);
725 assertEquals(portB, portBInTopo);
726 assertTrue(portBInTopo.isFrozen());
727 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
728
729 LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple());
730 assertEquals(linkA, linkAInTopo);
731 assertTrue(linkAInTopo.isFrozen());
732 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER, linkAInTopo.getType());
733
734
735 LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple());
736 assertEquals(linkB, linkBInTopo);
737 assertTrue(linkBInTopo.isFrozen());
738 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER, linkBInTopo.getType());
739
740 // check events to be fired
741 // FIXME if link flapped (linkA in this scenario),
742 // linkA appears in both removed and added is this expected behavior?
743 List<LinkEvent> apiAddedLinkEvents
744 = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents");
745 assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB));
746
747 // clear event before removing Link
748 apiAddedLinkEvents.clear();
749
750 // remove link
751 TestUtils.callMethod(theTopologyManager, "removeLink", LinkEvent.class, new LinkEvent(linkA));
752
753 LinkEvent linkANotInTopo = topology.getLinkEvent(linkA.getLinkTuple());
754 assertNull(linkANotInTopo);
755
756 List<LinkEvent> apiRemovedLinkEvents
757 = TestUtils.getField(theTopologyManager, "apiRemovedLinkEvents");
758 assertThat(apiRemovedLinkEvents, hasItem(linkA));
759 }
760
761 /**
762 * Test to confirm topology replica transformation.
763 */
764 @Test
765 public void testAddHostIgnoredByLink() {
766 setupTopologyManager();
767
768 final Dpid dpid = new Dpid(1);
769 SwitchEvent sw = new SwitchEvent(dpid);
770 sw.createStringAttribute("foo", "bar");
771
772 final PortNumber portNumberA = new PortNumber((short) 2);
773 PortEvent portA = new PortEvent(dpid, portNumberA);
774 portA.createStringAttribute("fuzz", "buzz");
775
776 final PortNumber portNumberB = new PortNumber((short) 3);
777 PortEvent portB = new PortEvent(dpid, portNumberB);
778 portB.createStringAttribute("fizz", "buz");
779
780 final PortNumber portNumberC = new PortNumber((short) 4);
781 PortEvent portC = new PortEvent(dpid, portNumberC);
782 portC.createStringAttribute("fizz", "buz");
783
784 LinkEvent linkA = new LinkEvent(portA.getSwitchPort(), portB.getSwitchPort());
785 linkA.createStringAttribute(TopologyElement.TYPE,
786 TopologyElement.TYPE_OPTICAL_LAYER);
787 LinkEvent linkB = new LinkEvent(portB.getSwitchPort(), portA.getSwitchPort());
788 linkB.createStringAttribute(TopologyElement.TYPE,
789 TopologyElement.TYPE_OPTICAL_LAYER);
790
791 TestUtils.callMethod(theTopologyManager, "addSwitch", SwitchEvent.class, sw);
792 TestUtils.callMethod(theTopologyManager, "addPort", PortEvent.class, portA);
793 TestUtils.callMethod(theTopologyManager, "addPort", PortEvent.class, portB);
794 TestUtils.callMethod(theTopologyManager, "addPort", PortEvent.class, portC);
795 TestUtils.callMethod(theTopologyManager, "addLink", LinkEvent.class, linkA);
796 TestUtils.callMethod(theTopologyManager, "addLink", LinkEvent.class, linkB);
797
798 // Add hostA attached to a port which already has a link
799 final MACAddress macA = MACAddress.valueOf(666L);
800 HostEvent hostA = new HostEvent(macA);
801 hostA.addAttachmentPoint(portA.getSwitchPort());
802 final long timestampA = 392893200L;
803 hostA.setLastSeenTime(timestampA);
804
805 TestUtils.callMethod(theTopologyManager, "addHost", HostEvent.class, hostA);
806
807 // Add hostB attached to multiple ports,
808 // some of them which already has a link
809 final MACAddress macB = MACAddress.valueOf(999L);
810 HostEvent hostB = new HostEvent(macB);
811 hostB.addAttachmentPoint(portB.getSwitchPort());
812 hostB.addAttachmentPoint(portC.getSwitchPort());
813 final long timestampB = 392893201L;
814 hostB.setLastSeenTime(timestampB);
815
816 TestUtils.callMethod(theTopologyManager, "addHost", HostEvent.class, hostB);
817
818 // check topology structure
819 TopologyInternal topology = (TopologyInternal) theTopologyManager.getTopology();
820 SwitchEvent swInTopo = topology.getSwitchEvent(dpid);
821 assertEquals(sw, swInTopo);
822 assertTrue(swInTopo.isFrozen());
823 assertEquals("bar", swInTopo.getStringAttribute("foo"));
824
825 final SwitchPort portIdA = new SwitchPort(dpid, portNumberA);
826 PortEvent portAInTopo = topology.getPortEvent(portIdA);
827 assertEquals(portA, portAInTopo);
828 assertTrue(portAInTopo.isFrozen());
829 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
830
831 final SwitchPort portIdB = new SwitchPort(dpid, portNumberB);
832 PortEvent portBInTopo = topology.getPortEvent(portIdB);
833 assertEquals(portB, portBInTopo);
834 assertTrue(portBInTopo.isFrozen());
835 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
836
837 // hostA expected to be completely ignored
838 assertNull(topology.getHostEvent(macA));
839 // hostB expected to be there with reduced attachment point
840 HostEvent hostBrev = new HostEvent(macB);
841 hostBrev.addAttachmentPoint(portC.getSwitchPort());
842 hostBrev.setLastSeenTime(timestampB);
843 hostBrev.freeze();
844 assertEquals(hostBrev, topology.getHostEvent(macB));
845
846
847 LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple());
848 assertEquals(linkA, linkAInTopo);
849 assertTrue(linkAInTopo.isFrozen());
850 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER, linkAInTopo.getType());
851
852 LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple());
853 assertEquals(linkB, linkBInTopo);
854 assertTrue(linkBInTopo.isFrozen());
855 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER, linkBInTopo.getType());
856
857 // check events to be fired
858 // hostB should be added with reduced attachment points
859 List<HostEvent> apiAddedHostEvents
860 = TestUtils.getField(theTopologyManager, "apiAddedHostEvents");
861 assertThat(apiAddedHostEvents, hasItem(hostBrev));
862
863 // hostA should not be ignored
864 List<HostEvent> apiRemovedHostEvents
865 = TestUtils.getField(theTopologyManager, "apiRemovedHostEvents");
866 assertThat(apiRemovedHostEvents, not(hasItem(hostA)));
867
868 List<LinkEvent> apiAddedLinkEvents
869 = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents");
870 assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB));
871 }
872
873 /**
874 * Test to confirm topology replica transformation.
875 */
876 @Test
877 public void testAddHostMove() {
878 setupTopologyManager();
879
880 final Dpid dpid = new Dpid(1);
881 SwitchEvent sw = new SwitchEvent(dpid);
882 sw.createStringAttribute("foo", "bar");
883
884 final PortNumber portNumberA = new PortNumber((short) 2);
885 PortEvent portA = new PortEvent(dpid, portNumberA);
886 portA.createStringAttribute("fuzz", "buzz");
887
888 final PortNumber portNumberB = new PortNumber((short) 3);
889 PortEvent portB = new PortEvent(dpid, portNumberB);
890 portB.createStringAttribute("fizz", "buz");
891
892 final PortNumber portNumberC = new PortNumber((short) 4);
893 PortEvent portC = new PortEvent(dpid, portNumberC);
894 portC.createStringAttribute("fizz", "buz");
895
896 TestUtils.callMethod(theTopologyManager, "addSwitch", SwitchEvent.class, sw);
897 TestUtils.callMethod(theTopologyManager, "addPort", PortEvent.class, portA);
898 TestUtils.callMethod(theTopologyManager, "addPort", PortEvent.class, portB);
899 TestUtils.callMethod(theTopologyManager, "addPort", PortEvent.class, portC);
900
901 // Add hostA attached to a port which already has a link
902 final MACAddress macA = MACAddress.valueOf(666L);
903 HostEvent hostA = new HostEvent(macA);
904 hostA.addAttachmentPoint(portA.getSwitchPort());
905 final long timestampA = 392893200L;
906 hostA.setLastSeenTime(timestampA);
907
908 TestUtils.callMethod(theTopologyManager, "addHost", HostEvent.class, hostA);
909
910
911 // check topology structure
912 TopologyInternal topology = (TopologyInternal) theTopologyManager.getTopology();
913 SwitchEvent swInTopo = topology.getSwitchEvent(dpid);
914 assertEquals(sw, swInTopo);
915 assertTrue(swInTopo.isFrozen());
916 assertEquals("bar", swInTopo.getStringAttribute("foo"));
917
918 final SwitchPort portIdA = new SwitchPort(dpid, portNumberA);
919 PortEvent portAInTopo = topology.getPortEvent(portIdA);
920 assertEquals(portA, portAInTopo);
921 assertTrue(portAInTopo.isFrozen());
922 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
923
924 final SwitchPort portIdB = new SwitchPort(dpid, portNumberB);
925 PortEvent portBInTopo = topology.getPortEvent(portIdB);
926 assertEquals(portB, portBInTopo);
927 assertTrue(portBInTopo.isFrozen());
928 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
929
930 // hostA expected to be there
931 assertEquals(hostA, topology.getHostEvent(macA));
932 assertEquals(timestampA, topology.getHostEvent(macA).getLastSeenTime());
933
934 // check events to be fired
935 // hostA should be added
936 List<HostEvent> apiAddedHostEvents
937 = TestUtils.getField(theTopologyManager, "apiAddedHostEvents");
938 assertThat(apiAddedHostEvents, hasItem(hostA));
939
940
941 // clear event before moving host
942 apiAddedHostEvents.clear();
943
944 HostEvent hostAmoved = new HostEvent(macA);
945 hostAmoved.addAttachmentPoint(portB.getSwitchPort());
946 final long timestampAmoved = 392893201L;
947 hostAmoved.setLastSeenTime(timestampAmoved);
948
949 TestUtils.callMethod(theTopologyManager, "addHost", HostEvent.class, hostAmoved);
950
951 assertEquals(hostAmoved, topology.getHostEvent(macA));
952 assertEquals(timestampAmoved, topology.getHostEvent(macA).getLastSeenTime());
953
954 // hostA expected to be there with new attachment point
955 apiAddedHostEvents
956 = TestUtils.getField(theTopologyManager, "apiAddedHostEvents");
957 assertThat(apiAddedHostEvents, hasItem(hostAmoved));
958
959 // hostA is updated not removed
960 List<HostEvent> apiRemovedHostEvents
961 = TestUtils.getField(theTopologyManager, "apiRemovedHostEvents");
962 assertThat(apiRemovedHostEvents, not(hasItem(hostA)));
963 }
weibitf7c31a42014-06-23 16:51:01 -0700964}