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