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