blob: 79dfc892eec6f2c1061a2aff45e846a38fda11d2 [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;
25import net.onrc.onos.core.util.PortNumber;
26import net.onrc.onos.core.util.SwitchPort;
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070027import net.onrc.onos.core.util.TestUtils;
weibitf7c31a42014-06-23 16:51:01 -070028
Pavlin Radoslavovd4f40372014-07-18 16:58:40 -070029import com.codahale.metrics.MetricFilter;
weibitf7c31a42014-06-23 16:51:01 -070030import org.easymock.EasyMock;
31import org.junit.After;
32import org.junit.Before;
33import org.junit.Test;
34
35/**
36 * Unit tests for the TopologyManager class in the Topology module.
37 * These test cases only check the sanity of functions in the TopologyManager.
38 * Note that we do not test the eventHandler functions in the TopologyManager class.
39 * DatagridService, DataStoreService, eventChannel, and controllerRegistryService are mocked out.
40 */
41public class TopologyManagerTest {
42 private TopologyManager theTopologyManager;
43 private final String eventChannelName = "onos.topology";
44 private IEventChannel<byte[], TopologyEvent> eventChannel;
45 private IDatagridService datagridService;
46 private TopologyDatastore dataStoreService;
47 private IControllerRegistryService registryService;
48 private CopyOnWriteArrayList<ITopologyListener> topologyListeners;
49 private Collection<TopologyEvent> allTopologyEvents;
50
51 @SuppressWarnings("unchecked")
52 @Before
53 public void setUp() throws Exception {
54 // Mock objects for testing
55 datagridService = EasyMock.createNiceMock(IDatagridService.class);
56 dataStoreService = EasyMock.createNiceMock(TopologyDatastore.class);
57 registryService = createMock(IControllerRegistryService.class);
58 eventChannel = EasyMock.createNiceMock(IEventChannel.class);
59
60 expect(datagridService.createChannel(
61 eq(eventChannelName),
62 eq(byte[].class),
63 eq(TopologyEvent.class)))
64 .andReturn(eventChannel).once();
65
66 expect(datagridService.addListener(
67 eq(eventChannelName),
68 anyObject(IEventChannelListener.class),
69 eq(byte[].class),
70 eq(TopologyEvent.class)))
71 .andReturn(eventChannel).once();
72
73 expect(dataStoreService.addSwitch(
74 anyObject(SwitchEvent.class),
75 anyObject(Collection.class)))
76 .andReturn(true).anyTimes();
77
78 expect(dataStoreService.deactivateSwitch(
79 anyObject(SwitchEvent.class),
80 anyObject(Collection.class)))
81 .andReturn(true).anyTimes();
82
83 expect(dataStoreService.addPort(
84 anyObject(PortEvent.class)))
85 .andReturn(true).anyTimes();
86
87 expect(dataStoreService.deactivatePort(
88 anyObject(PortEvent.class)))
89 .andReturn(true).anyTimes();
90
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070091 expect(dataStoreService.addHost(
92 anyObject(HostEvent.class)))
weibitf7c31a42014-06-23 16:51:01 -070093 .andReturn(true).anyTimes();
94
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070095 expect(dataStoreService.removeHost(
96 anyObject(HostEvent.class)))
weibitf7c31a42014-06-23 16:51:01 -070097 .andReturn(true).anyTimes();
98
99 expect(dataStoreService.addLink(
100 anyObject(LinkEvent.class)))
101 .andReturn(true).anyTimes();
102
103 expect(dataStoreService.removeLink(
104 anyObject(LinkEvent.class)))
105 .andReturn(true).anyTimes();
106
107 replay(datagridService);
108 replay(registryService);
109 replay(dataStoreService);
110
111 allTopologyEvents = new CopyOnWriteArrayList<>();
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700112 expect(eventChannel.getAllEntries())
113 .andReturn(allTopologyEvents).anyTimes();
114 }
weibitf7c31a42014-06-23 16:51:01 -0700115
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700116 private void setupTopologyManager() {
weibitf7c31a42014-06-23 16:51:01 -0700117 // Create a topologyManager object for testing
118 topologyListeners = new CopyOnWriteArrayList<>();
119 theTopologyManager = new TopologyManager(registryService, topologyListeners);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700120
121 // replace EventHandler to avoid thread from starting
122 TestUtils.setField(theTopologyManager, "eventHandler",
123 EasyMock.createNiceMock(TopologyManager.EventHandler.class));
weibitf7c31a42014-06-23 16:51:01 -0700124 theTopologyManager.startup(datagridService);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700125
126 // replace data store with Mocked object
127 TestUtils.setField(theTopologyManager, "datastore", dataStoreService);
weibitf7c31a42014-06-23 16:51:01 -0700128 }
129
130 @After
131 public void tearDown() throws Exception {
Pavlin Radoslavovd4f40372014-07-18 16:58:40 -0700132 OnosMetrics.removeMatching(MetricFilter.ALL);
weibitf7c31a42014-06-23 16:51:01 -0700133 }
134
135 /**
136 * Test the Switch discovered and Port discovered functions.
137 */
138 @Test
139 public void testPutSwitchAndPortDiscoveryEvent() {
140 // Mock the eventChannel functions first
141 eventChannel.addEntry(anyObject(byte[].class),
142 anyObject(TopologyEvent.class));
143 EasyMock.expectLastCall().times(3, 3); // (1 switch + 1 port), 1 port
144 replay(eventChannel);
145
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700146 setupTopologyManager();
147
weibitf7c31a42014-06-23 16:51:01 -0700148 // mockSwitch has one port
149 Dpid swDPId = new Dpid(100L);
150 PortNumber portId = new PortNumber((short) 1);
151
152 // Generate a new switch event along with a port event
153 SwitchEvent switchEvent = new SwitchEvent(swDPId);
154
155 Collection<PortEvent> portEvents = new ArrayList<PortEvent>();
156 portEvents.add(new PortEvent(swDPId, portId));
157
158 // Call the topologyManager function for adding a switch
159 theTopologyManager.putSwitchDiscoveryEvent(switchEvent, portEvents);
160
161 for (PortEvent portEvent : portEvents) {
162 // Call the topologyManager function for adding a port
163 theTopologyManager.putPortDiscoveryEvent(portEvent);
164 }
165
166 // Verify the function calls
167 verify(eventChannel);
168
169 }
170
171 /**
172 * Test the switch and port removed functions.
173 */
174 @Test
175 public void testRemoveSwitchAndPortDiscoveryEvent() {
176 // Mock the eventChannel functions first
177 eventChannel.removeEntry(anyObject(byte[].class));
178 EasyMock.expectLastCall().times(2, 2); //1 switch, 1 port
179 replay(eventChannel);
180
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700181 setupTopologyManager();
182
weibitf7c31a42014-06-23 16:51:01 -0700183 Dpid swDPId = new Dpid(100L);
184 PortNumber portId = new PortNumber((short) 1);
185
186 // Generate a port event
187 Collection<PortEvent> portEvents = new ArrayList<PortEvent>();
188 portEvents.add(new PortEvent(swDPId, portId));
189
190 // Call the topologyManager function for removing a port
191 for (PortEvent portEvent : portEvents) {
192 theTopologyManager.removePortDiscoveryEvent(portEvent);
193 }
194
195 // Call the topologyManager function for removing a switch
196 SwitchEvent switchEvent = new SwitchEvent(swDPId);
197 theTopologyManager.removeSwitchDiscoveryEvent(switchEvent);
198
199 // Verify the function calls
200 verify(eventChannel);
201
202 }
203
204 /**
205 * Test the device discovered function.
206 */
207 @Test
208 public void testPutDeviceDiscoveryEvent() {
209 // Mock the eventChannel functions first
210 eventChannel.addEntry(anyObject(byte[].class),
211 anyObject(TopologyEvent.class));
212 EasyMock.expectLastCall().times(1, 1); // 1 device
213 replay(eventChannel);
214
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700215 setupTopologyManager();
216
weibitf7c31a42014-06-23 16:51:01 -0700217 long swDPId = 100L;
218 long portId = 1L;
219
220 // Generate a new device event
221 MACAddress devMac = MACAddress.valueOf("00:AA:11:BB:33:CC");
222 SwitchPort sp = new SwitchPort(swDPId, portId);
223 List<SwitchPort> spLists = new ArrayList<SwitchPort>();
224 spLists.add(sp);
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700225 HostEvent hostEvent = new HostEvent(devMac);
226 hostEvent.setAttachmentPoints(spLists);
weibitf7c31a42014-06-23 16:51:01 -0700227
228 // Call the topologyManager function for adding a device
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700229 theTopologyManager.putHostDiscoveryEvent(hostEvent);
weibitf7c31a42014-06-23 16:51:01 -0700230
231 // Verify the function calls
232 verify(eventChannel);
233 }
234
235 /**
236 * Test the device removed function.
237 */
238 @Test
239 public void testRemoveDeviceDiscoveryEvent() {
240 // Mock the eventChannel functions first
241 eventChannel.removeEntry(anyObject(byte[].class));
242 EasyMock.expectLastCall().times(1, 1); // 1 device
243 replay(eventChannel);
244
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700245 setupTopologyManager();
246
weibitf7c31a42014-06-23 16:51:01 -0700247 long swDPId = 100L;
248 long portId = 1L;
249
250 // Generate a new device event
251 MACAddress devMac = MACAddress.valueOf("00:AA:11:BB:33:CC");
252 SwitchPort sp = new SwitchPort(swDPId, portId);
253 List<SwitchPort> spLists = new ArrayList<SwitchPort>();
254 spLists.add(sp);
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700255 HostEvent hostEvent = new HostEvent(devMac);
256 hostEvent.setAttachmentPoints(spLists);
weibitf7c31a42014-06-23 16:51:01 -0700257
258 // Call the topologyManager function for removing a device
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700259 theTopologyManager.removeHostDiscoveryEvent(hostEvent);
weibitf7c31a42014-06-23 16:51:01 -0700260
261 // Verify the function calls
262 verify(eventChannel);
263 }
264
265 /**
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700266 * Test the Switch Mastership updated event.
267 */
268 @Test
269 public void testPutSwitchMastershipEvent() {
270 // Mock the eventChannel functions first
271 eventChannel.addEntry(anyObject(byte[].class),
272 anyObject(TopologyEvent.class));
273 EasyMock.expectLastCall().times(1, 1); // 1 event
274 replay(eventChannel);
275
276 setupTopologyManager();
277
278 // Generate a new Switch Mastership event
279 Dpid dpid = new Dpid(100L);
280 String onosInstanceId = "ONOS-Test-Instance-ID";
281 Role role = Role.MASTER;
282 MastershipEvent mastershipEvent =
283 new MastershipEvent(dpid, onosInstanceId, role);
284
285 // Call the topologyManager function for adding the event
286 theTopologyManager.putSwitchMastershipEvent(mastershipEvent);
287
288 // Verify the function calls
289 verify(eventChannel);
290 }
291
292 /**
293 * Test the Switch Mastership removed event.
294 */
295 @Test
296 public void testRemoveSwitchMastershipEvent() {
297 // Mock the eventChannel functions first
298 eventChannel.removeEntry(anyObject(byte[].class));
299 EasyMock.expectLastCall().times(1, 1); // 1 event
300 replay(eventChannel);
301
302 setupTopologyManager();
303
304 // Generate a new Switch Mastership event
305 Dpid dpid = new Dpid(100L);
306 String onosInstanceId = "ONOS-Test-Instance-ID";
307 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}