blob: c99768bc3c753b9f2900047fcc4fa38e4be02931 [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.
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -070041 * Note that we do not test the eventHandler functions in the TopologyManager
42 * class.
43 * DatagridService, DataStoreService, eventChannel, and
44 * controllerRegistryService are mocked out.
weibitf7c31a42014-06-23 16:51:01 -070045 */
Ray Milkey38301352014-07-28 08:51:54 -070046public class TopologyManagerTest extends UnitTest {
weibitf7c31a42014-06-23 16:51:01 -070047 private TopologyManager theTopologyManager;
48 private final String eventChannelName = "onos.topology";
49 private IEventChannel<byte[], TopologyEvent> eventChannel;
50 private IDatagridService datagridService;
51 private TopologyDatastore dataStoreService;
52 private IControllerRegistryService registryService;
53 private CopyOnWriteArrayList<ITopologyListener> topologyListeners;
54 private Collection<TopologyEvent> allTopologyEvents;
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -070055 private OnosInstanceId onosInstanceId =
56 new OnosInstanceId("ONOS-Test-Instance-ID");
weibitf7c31a42014-06-23 16:51:01 -070057
58 @SuppressWarnings("unchecked")
59 @Before
60 public void setUp() throws Exception {
61 // Mock objects for testing
62 datagridService = EasyMock.createNiceMock(IDatagridService.class);
63 dataStoreService = EasyMock.createNiceMock(TopologyDatastore.class);
64 registryService = createMock(IControllerRegistryService.class);
65 eventChannel = EasyMock.createNiceMock(IEventChannel.class);
66
67 expect(datagridService.createChannel(
68 eq(eventChannelName),
69 eq(byte[].class),
70 eq(TopologyEvent.class)))
71 .andReturn(eventChannel).once();
72
73 expect(datagridService.addListener(
74 eq(eventChannelName),
75 anyObject(IEventChannelListener.class),
76 eq(byte[].class),
77 eq(TopologyEvent.class)))
78 .andReturn(eventChannel).once();
79
80 expect(dataStoreService.addSwitch(
81 anyObject(SwitchEvent.class),
82 anyObject(Collection.class)))
83 .andReturn(true).anyTimes();
84
85 expect(dataStoreService.deactivateSwitch(
86 anyObject(SwitchEvent.class),
87 anyObject(Collection.class)))
88 .andReturn(true).anyTimes();
89
90 expect(dataStoreService.addPort(
91 anyObject(PortEvent.class)))
92 .andReturn(true).anyTimes();
93
94 expect(dataStoreService.deactivatePort(
95 anyObject(PortEvent.class)))
96 .andReturn(true).anyTimes();
97
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070098 expect(dataStoreService.addHost(
99 anyObject(HostEvent.class)))
weibitf7c31a42014-06-23 16:51:01 -0700100 .andReturn(true).anyTimes();
101
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700102 expect(dataStoreService.removeHost(
103 anyObject(HostEvent.class)))
weibitf7c31a42014-06-23 16:51:01 -0700104 .andReturn(true).anyTimes();
105
106 expect(dataStoreService.addLink(
107 anyObject(LinkEvent.class)))
108 .andReturn(true).anyTimes();
109
110 expect(dataStoreService.removeLink(
111 anyObject(LinkEvent.class)))
112 .andReturn(true).anyTimes();
113
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700114 expect(registryService.getOnosInstanceId()).andReturn(onosInstanceId).anyTimes();
115
weibitf7c31a42014-06-23 16:51:01 -0700116 replay(datagridService);
117 replay(registryService);
118 replay(dataStoreService);
119
120 allTopologyEvents = new CopyOnWriteArrayList<>();
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700121 expect(eventChannel.getAllEntries())
122 .andReturn(allTopologyEvents).anyTimes();
123 }
weibitf7c31a42014-06-23 16:51:01 -0700124
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700125 private void setupTopologyManager() {
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700126 // Create a TopologyManager object for testing
weibitf7c31a42014-06-23 16:51:01 -0700127 topologyListeners = new CopyOnWriteArrayList<>();
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700128 theTopologyManager = new TopologyManager(registryService,
129 topologyListeners);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700130
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700131 // Replace the eventHandler to prevent the thread from starting
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700132 TestUtils.setField(theTopologyManager, "eventHandler",
133 EasyMock.createNiceMock(TopologyManager.EventHandler.class));
weibitf7c31a42014-06-23 16:51:01 -0700134 theTopologyManager.startup(datagridService);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700135
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700136 // Replace the data store with a mocked object
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700137 TestUtils.setField(theTopologyManager, "datastore", dataStoreService);
weibitf7c31a42014-06-23 16:51:01 -0700138 }
139
weibitf7c31a42014-06-23 16:51:01 -0700140 /**
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700141 * Test the Switch Mastership updated event.
142 */
143 @Test
144 public void testPutSwitchMastershipEvent() {
145 // Mock the eventChannel functions first
146 eventChannel.addEntry(anyObject(byte[].class),
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700147 anyObject(TopologyEvent.class));
148 EasyMock.expectLastCall().times(1, 1); // 1 event
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700149 replay(eventChannel);
150
151 setupTopologyManager();
152
153 // Generate a new Switch Mastership event
154 Dpid dpid = new Dpid(100L);
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700155 Role role = Role.MASTER;
156 MastershipEvent mastershipEvent =
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700157 new MastershipEvent(dpid, this.onosInstanceId, role);
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700158
159 // Call the topologyManager function for adding the event
160 theTopologyManager.putSwitchMastershipEvent(mastershipEvent);
161
162 // Verify the function calls
163 verify(eventChannel);
164 }
165
166 /**
167 * Test the Switch Mastership removed event.
168 */
169 @Test
170 public void testRemoveSwitchMastershipEvent() {
171 // Mock the eventChannel functions first
172 eventChannel.removeEntry(anyObject(byte[].class));
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700173 EasyMock.expectLastCall().times(1, 1); // 1 event
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700174 replay(eventChannel);
175
176 setupTopologyManager();
177
178 // Generate a new Switch Mastership event
179 Dpid dpid = new Dpid(100L);
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700180 Role role = Role.MASTER;
181 MastershipEvent mastershipEvent =
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700182 new MastershipEvent(dpid, this.onosInstanceId, role);
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700183
184 // Call the topologyManager function for removing the event
185 theTopologyManager.removeSwitchMastershipEvent(mastershipEvent);
186
187 // Verify the function calls
188 verify(eventChannel);
189 }
190
191 /**
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700192 * Test the Switch discovered and Port discovered functions.
193 */
194 @Test
195 public void testPutSwitchAndPortDiscoveryEvent() {
196 // Mock the eventChannel functions first
197 eventChannel.addEntry(anyObject(byte[].class),
198 anyObject(TopologyEvent.class));
199 EasyMock.expectLastCall().times(3, 3); // (1 switch + 1 port), 1 port
200 replay(eventChannel);
201
202 setupTopologyManager();
203
204 // mockSwitch has one port
205 Dpid swDpid = new Dpid(100L);
206 PortNumber portNumber = PortNumber.uint32(1);
207
208 // Generate a new Switch Event along with a Port Event
209 SwitchEvent switchEvent = new SwitchEvent(swDpid);
210
211 Collection<PortEvent> portEvents = new ArrayList<PortEvent>();
212 portEvents.add(new PortEvent(swDpid, portNumber));
213
214 // Call the topologyManager function for adding a switch
215 theTopologyManager.putSwitchDiscoveryEvent(switchEvent, portEvents);
216
217 for (PortEvent portEvent : portEvents) {
218 // Call the topologyManager function for adding a port
219 theTopologyManager.putPortDiscoveryEvent(portEvent);
220 }
221
222 // Verify the function calls
223 verify(eventChannel);
224
225 }
226
227 /**
228 * Test the switch and port removed functions.
229 */
230 @Test
231 public void testRemoveSwitchAndPortDiscoveryEvent() {
232 // Mock the eventChannel functions first
233 eventChannel.removeEntry(anyObject(byte[].class));
234 EasyMock.expectLastCall().times(2, 2); // 1 switch, 1 port
235 replay(eventChannel);
236
237 setupTopologyManager();
238
239 Dpid swDpid = new Dpid(100L);
240 PortNumber portNumber = PortNumber.uint32(1);
241
242 // Generate a Port Event
243 Collection<PortEvent> portEvents = new ArrayList<PortEvent>();
244 portEvents.add(new PortEvent(swDpid, portNumber));
245
246 // Call the topologyManager function for removing a port
247 for (PortEvent portEvent : portEvents) {
248 theTopologyManager.removePortDiscoveryEvent(portEvent);
249 }
250
251 // Call the topologyManager function for removing a switch
252 SwitchEvent switchEvent = new SwitchEvent(swDpid);
253 theTopologyManager.removeSwitchDiscoveryEvent(switchEvent);
254
255 // Verify the function calls
256 verify(eventChannel);
257
258 }
259
260 /**
weibitf7c31a42014-06-23 16:51:01 -0700261 * Test the link discovered function.
262 */
263 @Test
264 public void testPutLinkDiscoveryEvent() {
265 // Mock the eventChannel functions first
266 eventChannel.addEntry(anyObject(byte[].class),
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700267 anyObject(TopologyEvent.class));
268 EasyMock.expectLastCall().times(5, 5); // (2 switch + 2 port + 1 link)
weibitf7c31a42014-06-23 16:51:01 -0700269 replay(eventChannel);
270
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700271 setupTopologyManager();
272
weibitf7c31a42014-06-23 16:51:01 -0700273 // Assign the switch and port IDs
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700274 Dpid swDpid1 = new Dpid(100L);
275 PortNumber portNumber1 = PortNumber.uint32(1);
276 Dpid swDpid2 = new Dpid(200L);
277 PortNumber portNumber2 = PortNumber.uint32(2);
weibitf7c31a42014-06-23 16:51:01 -0700278
279 // Generate the switch and port events
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700280 SwitchEvent switchEvent1 = new SwitchEvent(swDpid1);
weibitf7c31a42014-06-23 16:51:01 -0700281 Collection<PortEvent> portEvents1 = new ArrayList<PortEvent>();
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700282 portEvents1.add(new PortEvent(swDpid1, portNumber1));
weibitf7c31a42014-06-23 16:51:01 -0700283
284 // Call the topologyManager function for adding a switch
285 theTopologyManager.putSwitchDiscoveryEvent(switchEvent1, portEvents1);
286
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700287 // Generate the Switch and Port Events
288 SwitchEvent switchEvent2 = new SwitchEvent(swDpid2);
weibitf7c31a42014-06-23 16:51:01 -0700289 Collection<PortEvent> portEvents2 = new ArrayList<PortEvent>();
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700290 portEvents2.add(new PortEvent(swDpid2, portNumber2));
weibitf7c31a42014-06-23 16:51:01 -0700291
292 // Call the topologyManager function for adding a switch
293 theTopologyManager.putSwitchDiscoveryEvent(switchEvent2, portEvents2);
294
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700295 // Create the Link Event
296 LinkEvent linkEvent =
297 new LinkEvent(new SwitchPort(swDpid1, portNumber1),
298 new SwitchPort(swDpid2, portNumber2));
weibitf7c31a42014-06-23 16:51:01 -0700299 theTopologyManager.putLinkDiscoveryEvent(linkEvent);
300
301 // Verify the function calls
302 verify(eventChannel);
303 }
304
305 /**
306 * Test the link removed function.
307 */
308 @Test
309 public void testRemoveLinkDiscoveryEvent() {
310 // Mock the eventChannel functions first
311 eventChannel.removeEntry(anyObject(byte[].class));
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700312 EasyMock.expectLastCall().times(1, 1); // (1 link)
weibitf7c31a42014-06-23 16:51:01 -0700313 replay(eventChannel);
314
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700315 setupTopologyManager();
316
weibitf7c31a42014-06-23 16:51:01 -0700317 // Assign the switch and port IDs
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700318 Dpid swDpid1 = new Dpid(100L);
319 PortNumber portNumber1 = PortNumber.uint32(1);
320 Dpid swDpid2 = new Dpid(200L);
321 PortNumber portNumber2 = PortNumber.uint32(2);
weibitf7c31a42014-06-23 16:51:01 -0700322
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700323 // Generate the Switch and Port Events
324 SwitchEvent switchEvent1 = new SwitchEvent(swDpid1);
weibitf7c31a42014-06-23 16:51:01 -0700325 Collection<PortEvent> portEvents1 = new ArrayList<PortEvent>();
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700326 portEvents1.add(new PortEvent(swDpid1, portNumber1));
weibitf7c31a42014-06-23 16:51:01 -0700327
328 // Call the topologyManager function for adding a switch
329 theTopologyManager.putSwitchDiscoveryEvent(switchEvent1, portEvents1);
330
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700331 // Generate the Switch and port Events
332 SwitchEvent switchEvent2 = new SwitchEvent(swDpid2);
weibitf7c31a42014-06-23 16:51:01 -0700333 Collection<PortEvent> portEvents2 = new ArrayList<PortEvent>();
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700334 portEvents2.add(new PortEvent(swDpid2, portNumber2));
weibitf7c31a42014-06-23 16:51:01 -0700335
336 // Call the topologyManager function for adding a switch
337 theTopologyManager.putSwitchDiscoveryEvent(switchEvent2, portEvents2);
338
339 // Remove the link
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700340 LinkEvent linkEventRemove =
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700341 new LinkEvent(new SwitchPort(swDpid1, portNumber1),
342 new SwitchPort(swDpid2, portNumber2));
weibitf7c31a42014-06-23 16:51:01 -0700343 theTopologyManager.removeLinkDiscoveryEvent(linkEventRemove);
344
345 // Verify the function calls
346 verify(eventChannel);
347 }
348
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700349 /**
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700350 * Test the host discovered function.
351 */
352 @Test
353 public void testPutHostDiscoveryEvent() {
354 // Mock the eventChannel functions first
355 eventChannel.addEntry(anyObject(byte[].class),
356 anyObject(TopologyEvent.class));
357 EasyMock.expectLastCall().times(1, 1); // 1 host
358 replay(eventChannel);
359
360 setupTopologyManager();
361
362 Dpid swDpid = new Dpid(100L);
363 PortNumber portNumber = PortNumber.uint32(1);
364
365 // Generate a new Host Event
366 MACAddress hostMac = MACAddress.valueOf("00:AA:11:BB:33:CC");
367 SwitchPort sp = new SwitchPort(swDpid, portNumber);
368 List<SwitchPort> spLists = new ArrayList<SwitchPort>();
369 spLists.add(sp);
370 HostEvent hostEvent = new HostEvent(hostMac);
371 hostEvent.setAttachmentPoints(spLists);
372
373 // Call the topologyManager function for adding a host
374 theTopologyManager.putHostDiscoveryEvent(hostEvent);
375
376 // Verify the function calls
377 verify(eventChannel);
378 }
379
380 /**
381 * Test the host removed function.
382 */
383 @Test
384 public void testRemoveHostDiscoveryEvent() {
385 // Mock the eventChannel functions first
386 eventChannel.removeEntry(anyObject(byte[].class));
387 EasyMock.expectLastCall().times(1, 1); // 1 host
388 replay(eventChannel);
389
390 setupTopologyManager();
391
392 Dpid swDpid = new Dpid(100L);
393 PortNumber portNumber = PortNumber.uint32(1);
394
395 // Generate a new Host Event
396 MACAddress hostMac = MACAddress.valueOf("00:AA:11:BB:33:CC");
397 SwitchPort sp = new SwitchPort(swDpid, portNumber);
398 List<SwitchPort> spLists = new ArrayList<SwitchPort>();
399 spLists.add(sp);
400 HostEvent hostEvent = new HostEvent(hostMac);
401 hostEvent.setAttachmentPoints(spLists);
402
403 // Call the topologyManager function for removing a host
404 theTopologyManager.removeHostDiscoveryEvent(hostEvent);
405
406 // Verify the function calls
407 verify(eventChannel);
408 }
409
410 /**
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700411 * Test to confirm topology replica transformation.
412 */
413 @Test
414 public void testAddSwitch() {
415 setupTopologyManager();
416
417 final Dpid dpid = new Dpid(1);
418 SwitchEvent sw = new SwitchEvent(dpid);
419 sw.createStringAttribute("foo", "bar");
420
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700421 TestUtils.callMethod(theTopologyManager, "addSwitch",
422 SwitchEvent.class, sw);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700423
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700424 // Check the topology structure
425 TopologyInternal topology =
426 (TopologyInternal) theTopologyManager.getTopology();
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700427 SwitchEvent swInTopo = topology.getSwitchEvent(dpid);
428 assertEquals(sw, swInTopo);
429 assertTrue(swInTopo.isFrozen());
430 assertEquals("bar", swInTopo.getStringAttribute("foo"));
431
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700432 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700433 List<SwitchEvent> apiAddedSwitchEvents
434 = TestUtils.getField(theTopologyManager, "apiAddedSwitchEvents");
435 assertThat(apiAddedSwitchEvents, hasItem(sw));
436 }
437
438 /**
439 * Test to confirm topology replica transformation.
440 */
441 @Test
442 public void testAddPort() {
443 setupTopologyManager();
444
445 final Dpid dpid = new Dpid(1);
446 SwitchEvent sw = new SwitchEvent(dpid);
447 sw.createStringAttribute("foo", "bar");
448
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700449 final PortNumber portNumber = PortNumber.uint32(2);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700450 PortEvent port = new PortEvent(dpid, portNumber);
451 port.createStringAttribute("fuzz", "buzz");
452
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700453 TestUtils.callMethod(theTopologyManager, "addSwitch",
454 SwitchEvent.class, sw);
455 TestUtils.callMethod(theTopologyManager, "addPort",
456 PortEvent.class, port);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700457
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700458 // Check the topology structure
459 TopologyInternal topology =
460 (TopologyInternal) theTopologyManager.getTopology();
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700461 SwitchEvent swInTopo = topology.getSwitchEvent(dpid);
462 assertEquals(sw, swInTopo);
463 assertTrue(swInTopo.isFrozen());
464 assertEquals("bar", swInTopo.getStringAttribute("foo"));
465
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700466 final SwitchPort switchPort = new SwitchPort(dpid, portNumber);
467 PortEvent portInTopo = topology.getPortEvent(switchPort);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700468 assertEquals(port, portInTopo);
469 assertTrue(portInTopo.isFrozen());
470 assertEquals("buzz", portInTopo.getStringAttribute("fuzz"));
471
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700472 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700473 List<PortEvent> apiAddedPortEvents
474 = TestUtils.getField(theTopologyManager, "apiAddedPortEvents");
475 assertThat(apiAddedPortEvents, hasItem(port));
476 }
477
478 /**
479 * Test to confirm topology replica transformation.
480 */
481 @Test
482 public void testRemovePortThenSwitch() {
483 setupTopologyManager();
484
485 final Dpid dpid = new Dpid(1);
486 SwitchEvent sw = new SwitchEvent(dpid);
487 sw.createStringAttribute("foo", "bar");
488
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700489 final PortNumber portNumber = PortNumber.uint32(2);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700490 PortEvent port = new PortEvent(dpid, portNumber);
491 port.createStringAttribute("fuzz", "buzz");
492
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700493 TestUtils.callMethod(theTopologyManager, "addSwitch",
494 SwitchEvent.class, sw);
495 TestUtils.callMethod(theTopologyManager, "addPort",
496 PortEvent.class, port);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700497
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700498 // Check the topology structure
499 TopologyInternal topology =
500 (TopologyInternal) theTopologyManager.getTopology();
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700501 SwitchEvent swInTopo = topology.getSwitchEvent(dpid);
502 assertEquals(sw, swInTopo);
503 assertTrue(swInTopo.isFrozen());
504 assertEquals("bar", swInTopo.getStringAttribute("foo"));
505
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700506 final SwitchPort switchPort = new SwitchPort(dpid, portNumber);
507 PortEvent portInTopo = topology.getPortEvent(switchPort);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700508 assertEquals(port, portInTopo);
509 assertTrue(portInTopo.isFrozen());
510 assertEquals("buzz", portInTopo.getStringAttribute("fuzz"));
511
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700512 // Remove in proper order
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700513 TestUtils.callMethod(theTopologyManager, "removePort",
514 PortEvent.class, new PortEvent(port));
515 TestUtils.callMethod(theTopologyManager, "removeSwitch",
516 SwitchEvent.class, new SwitchEvent(sw));
517
518
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700519 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700520 List<PortEvent> apiRemovedPortEvents
521 = TestUtils.getField(theTopologyManager, "apiRemovedPortEvents");
522 assertThat(apiRemovedPortEvents, hasItem(port));
523 List<SwitchEvent> apiRemovedSwitchEvents
524 = TestUtils.getField(theTopologyManager, "apiRemovedSwitchEvents");
525 assertThat(apiRemovedSwitchEvents, hasItem(sw));
526 }
527
528 /**
529 * Test to confirm topology replica transformation.
530 */
531 @Test
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700532 public void testRemoveSwitchWithoutPortRemoval() {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700533 setupTopologyManager();
534
535 final Dpid dpid = new Dpid(1);
536 SwitchEvent sw = new SwitchEvent(dpid);
537 sw.createStringAttribute("foo", "bar");
538
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700539 final PortNumber portNumber = PortNumber.uint32(2);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700540 PortEvent port = new PortEvent(dpid, portNumber);
541 port.createStringAttribute("fuzz", "buzz");
542
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700543 TestUtils.callMethod(theTopologyManager, "addSwitch",
544 SwitchEvent.class, sw);
545 TestUtils.callMethod(theTopologyManager, "addPort",
546 PortEvent.class, port);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700547
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700548 // Check the topology structure
549 TopologyInternal topology =
550 (TopologyInternal) theTopologyManager.getTopology();
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700551 SwitchEvent swInTopo = topology.getSwitchEvent(dpid);
552 assertEquals(sw, swInTopo);
553 assertTrue(swInTopo.isFrozen());
554 assertEquals("bar", swInTopo.getStringAttribute("foo"));
555
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700556 final SwitchPort switchPort = new SwitchPort(dpid, portNumber);
557 PortEvent portInTopo = topology.getPortEvent(switchPort);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700558 assertEquals(port, portInTopo);
559 assertTrue(portInTopo.isFrozen());
560 assertEquals("buzz", portInTopo.getStringAttribute("fuzz"));
561
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700562 // Remove in in-proper order
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700563// TestUtils.callMethod(theTopologyManager, "removePort",
564// PortEvent.class, new PortEvent(port));
565 TestUtils.callMethod(theTopologyManager, "removeSwitch",
566 SwitchEvent.class, new SwitchEvent(sw));
567
568
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700569 // Check the events to be fired
570 // The outcome should be the same as #testRemovePortThenSwitch
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700571 List<PortEvent> apiRemovedPortEvents
572 = TestUtils.getField(theTopologyManager, "apiRemovedPortEvents");
573 assertThat(apiRemovedPortEvents, hasItem(port));
574 List<SwitchEvent> apiRemovedSwitchEvents
575 = TestUtils.getField(theTopologyManager, "apiRemovedSwitchEvents");
576 assertThat(apiRemovedSwitchEvents, hasItem(sw));
577 }
578
579 /**
580 * Test to confirm topology replica transformation.
581 */
582 @Test
583 public void testAddLink() {
584 setupTopologyManager();
585
586 final Dpid dpid = new Dpid(1);
587 SwitchEvent sw = new SwitchEvent(dpid);
588 sw.createStringAttribute("foo", "bar");
589
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700590 final PortNumber portNumberA = PortNumber.uint32(2);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700591 PortEvent portA = new PortEvent(dpid, portNumberA);
592 portA.createStringAttribute("fuzz", "buzz");
593
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700594 final PortNumber portNumberB = PortNumber.uint32(3);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700595 PortEvent portB = new PortEvent(dpid, portNumberB);
596 portB.createStringAttribute("fizz", "buz");
597
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700598 LinkEvent linkA = new LinkEvent(portA.getSwitchPort(),
599 portB.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700600 linkA.createStringAttribute(TopologyElement.TYPE,
601 TopologyElement.TYPE_OPTICAL_LAYER);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700602 LinkEvent linkB = new LinkEvent(portB.getSwitchPort(),
603 portA.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700604 linkB.createStringAttribute(TopologyElement.TYPE,
605 TopologyElement.TYPE_OPTICAL_LAYER);
606
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700607 TestUtils.callMethod(theTopologyManager, "addSwitch",
608 SwitchEvent.class, sw);
609 TestUtils.callMethod(theTopologyManager, "addPort",
610 PortEvent.class, portA);
611 TestUtils.callMethod(theTopologyManager, "addPort",
612 PortEvent.class, portB);
613 TestUtils.callMethod(theTopologyManager, "addLink",
614 LinkEvent.class, linkA);
615 TestUtils.callMethod(theTopologyManager, "addLink",
616 LinkEvent.class, linkB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700617
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700618 // Check the topology structure
619 TopologyInternal topology =
620 (TopologyInternal) theTopologyManager.getTopology();
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700621 SwitchEvent swInTopo = topology.getSwitchEvent(dpid);
622 assertEquals(sw, swInTopo);
623 assertTrue(swInTopo.isFrozen());
624 assertEquals("bar", swInTopo.getStringAttribute("foo"));
625
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700626 final SwitchPort switchPortA = new SwitchPort(dpid, portNumberA);
627 PortEvent portAInTopo = topology.getPortEvent(switchPortA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700628 assertEquals(portA, portAInTopo);
629 assertTrue(portAInTopo.isFrozen());
630 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
631
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700632 final SwitchPort switchPortB = new SwitchPort(dpid, portNumberB);
633 PortEvent portBInTopo = topology.getPortEvent(switchPortB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700634 assertEquals(portB, portBInTopo);
635 assertTrue(portBInTopo.isFrozen());
636 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
637
638 LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple());
639 assertEquals(linkA, linkAInTopo);
640 assertTrue(linkAInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700641 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
642 linkAInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700643
644 LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple());
645 assertEquals(linkB, linkBInTopo);
646 assertTrue(linkBInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700647 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
648 linkBInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700649
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700650 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700651 List<LinkEvent> apiAddedLinkEvents
652 = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents");
653 assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB));
654 }
655
656 /**
657 * Test to confirm topology replica transformation.
658 */
659 @Test
660 public void testAddLinkKickingOffHost() {
661 setupTopologyManager();
662
663 final Dpid dpid = new Dpid(1);
664 SwitchEvent sw = new SwitchEvent(dpid);
665 sw.createStringAttribute("foo", "bar");
666
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700667 final PortNumber portNumberA = PortNumber.uint32(2);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700668 PortEvent portA = new PortEvent(dpid, portNumberA);
669 portA.createStringAttribute("fuzz", "buzz");
670
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700671 final PortNumber portNumberB = PortNumber.uint32(3);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700672 PortEvent portB = new PortEvent(dpid, portNumberB);
673 portB.createStringAttribute("fizz", "buz");
674
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700675 final PortNumber portNumberC = PortNumber.uint32(4);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700676 PortEvent portC = new PortEvent(dpid, portNumberC);
677 portC.createStringAttribute("fizz", "buz");
678
679 final MACAddress macA = MACAddress.valueOf(666L);
680 HostEvent hostA = new HostEvent(macA);
681 hostA.addAttachmentPoint(portA.getSwitchPort());
682 final long timestampA = 392893200L;
683 hostA.setLastSeenTime(timestampA);
684
685 final MACAddress macB = MACAddress.valueOf(999L);
686 HostEvent hostB = new HostEvent(macB);
687 hostB.addAttachmentPoint(portB.getSwitchPort());
688 hostB.addAttachmentPoint(portC.getSwitchPort());
689 final long timestampB = 392893201L;
690 hostB.setLastSeenTime(timestampB);
691
692
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700693 LinkEvent linkA = new LinkEvent(portA.getSwitchPort(),
694 portB.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700695 linkA.createStringAttribute(TopologyElement.TYPE,
696 TopologyElement.TYPE_OPTICAL_LAYER);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700697 LinkEvent linkB = new LinkEvent(portB.getSwitchPort(),
698 portA.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700699 linkB.createStringAttribute(TopologyElement.TYPE,
700 TopologyElement.TYPE_OPTICAL_LAYER);
701
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700702 TestUtils.callMethod(theTopologyManager, "addSwitch",
703 SwitchEvent.class, sw);
704 TestUtils.callMethod(theTopologyManager, "addPort",
705 PortEvent.class, portA);
706 TestUtils.callMethod(theTopologyManager, "addPort",
707 PortEvent.class, portB);
708 TestUtils.callMethod(theTopologyManager, "addPort",
709 PortEvent.class, portC);
710 TestUtils.callMethod(theTopologyManager, "addHost",
711 HostEvent.class, hostA);
712 TestUtils.callMethod(theTopologyManager, "addHost",
713 HostEvent.class, hostB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700714
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700715 TestUtils.callMethod(theTopologyManager, "addLink",
716 LinkEvent.class, linkA);
717 TestUtils.callMethod(theTopologyManager, "addLink",
718 LinkEvent.class, linkB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700719
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700720 // Check the topology structure
721 TopologyInternal topology =
722 (TopologyInternal) theTopologyManager.getTopology();
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700723 SwitchEvent swInTopo = topology.getSwitchEvent(dpid);
724 assertEquals(sw, swInTopo);
725 assertTrue(swInTopo.isFrozen());
726 assertEquals("bar", swInTopo.getStringAttribute("foo"));
727
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700728 final SwitchPort switchPortA = new SwitchPort(dpid, portNumberA);
729 PortEvent portAInTopo = topology.getPortEvent(switchPortA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700730 assertEquals(portA, portAInTopo);
731 assertTrue(portAInTopo.isFrozen());
732 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
733
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700734 final SwitchPort switchPortB = new SwitchPort(dpid, portNumberB);
735 PortEvent portBInTopo = topology.getPortEvent(switchPortB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700736 assertEquals(portB, portBInTopo);
737 assertTrue(portBInTopo.isFrozen());
738 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
739
740 // hostA expected to be removed
741 assertNull(topology.getHostEvent(macA));
742 // hostB expected to be there with reduced attachment point
743 HostEvent hostBrev = new HostEvent(macB);
744 hostBrev.addAttachmentPoint(portC.getSwitchPort());
745 hostBrev.setLastSeenTime(timestampB);
746 hostBrev.freeze();
747 assertEquals(hostBrev, topology.getHostEvent(macB));
748
749
750 LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple());
751 assertEquals(linkA, linkAInTopo);
752 assertTrue(linkAInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700753 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
754 linkAInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700755
756 LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple());
757 assertEquals(linkB, linkBInTopo);
758 assertTrue(linkBInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700759 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
760 linkBInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700761
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700762 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700763 List<HostEvent> apiAddedHostEvents
764 = TestUtils.getField(theTopologyManager, "apiAddedHostEvents");
765 assertThat(apiAddedHostEvents, hasItem(hostBrev));
766
767 List<HostEvent> apiRemovedHostEvents
768 = TestUtils.getField(theTopologyManager, "apiRemovedHostEvents");
769 assertThat(apiRemovedHostEvents, hasItem(hostA));
770 List<LinkEvent> apiAddedLinkEvents
771 = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents");
772 assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB));
773 }
774
775 /**
776 * Test to confirm topology replica transformation.
777 */
778 @Test
779 public void testRemoveLink() {
780 setupTopologyManager();
781
782 final Dpid dpid = new Dpid(1);
783 SwitchEvent sw = new SwitchEvent(dpid);
784 sw.createStringAttribute("foo", "bar");
785
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700786 final PortNumber portNumberA = PortNumber.uint32(2);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700787 PortEvent portA = new PortEvent(dpid, portNumberA);
788 portA.createStringAttribute("fuzz", "buzz");
789
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700790 final PortNumber portNumberB = PortNumber.uint32(3);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700791 PortEvent portB = new PortEvent(dpid, portNumberB);
792 portB.createStringAttribute("fizz", "buz");
793
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700794 LinkEvent linkA = new LinkEvent(portA.getSwitchPort(),
795 portB.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700796 linkA.createStringAttribute(TopologyElement.TYPE,
797 TopologyElement.TYPE_OPTICAL_LAYER);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700798 LinkEvent linkB = new LinkEvent(portB.getSwitchPort(),
799 portA.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700800 linkB.createStringAttribute(TopologyElement.TYPE,
801 TopologyElement.TYPE_OPTICAL_LAYER);
802
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700803 TestUtils.callMethod(theTopologyManager, "addSwitch",
804 SwitchEvent.class, sw);
805 TestUtils.callMethod(theTopologyManager, "addPort",
806 PortEvent.class, portA);
807 TestUtils.callMethod(theTopologyManager, "addPort",
808 PortEvent.class, portB);
809 TestUtils.callMethod(theTopologyManager, "addLink",
810 LinkEvent.class, linkA);
811 TestUtils.callMethod(theTopologyManager, "addLink",
812 LinkEvent.class, linkB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700813
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700814 // Check the topology structure
815 TopologyInternal topology =
816 (TopologyInternal) theTopologyManager.getTopology();
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700817 SwitchEvent swInTopo = topology.getSwitchEvent(dpid);
818 assertEquals(sw, swInTopo);
819 assertTrue(swInTopo.isFrozen());
820 assertEquals("bar", swInTopo.getStringAttribute("foo"));
821
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700822 final SwitchPort switchPortA = new SwitchPort(dpid, portNumberA);
823 PortEvent portAInTopo = topology.getPortEvent(switchPortA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700824 assertEquals(portA, portAInTopo);
825 assertTrue(portAInTopo.isFrozen());
826 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
827
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700828 final SwitchPort switchPortB = new SwitchPort(dpid, portNumberB);
829 PortEvent portBInTopo = topology.getPortEvent(switchPortB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700830 assertEquals(portB, portBInTopo);
831 assertTrue(portBInTopo.isFrozen());
832 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
833
834 LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple());
835 assertEquals(linkA, linkAInTopo);
836 assertTrue(linkAInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700837 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
838 linkAInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700839
840
841 LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple());
842 assertEquals(linkB, linkBInTopo);
843 assertTrue(linkBInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700844 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
845 linkBInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700846
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700847 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700848 // FIXME if link flapped (linkA in this scenario),
849 // linkA appears in both removed and added is this expected behavior?
850 List<LinkEvent> apiAddedLinkEvents
851 = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents");
852 assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB));
853
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700854 // Clear the events before removing the link
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700855 apiAddedLinkEvents.clear();
856
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700857 // Remove the link
858 TestUtils.callMethod(theTopologyManager, "removeLink",
859 LinkEvent.class, new LinkEvent(linkA));
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700860
861 LinkEvent linkANotInTopo = topology.getLinkEvent(linkA.getLinkTuple());
862 assertNull(linkANotInTopo);
863
864 List<LinkEvent> apiRemovedLinkEvents
865 = TestUtils.getField(theTopologyManager, "apiRemovedLinkEvents");
866 assertThat(apiRemovedLinkEvents, hasItem(linkA));
867 }
868
869 /**
870 * Test to confirm topology replica transformation.
871 */
872 @Test
873 public void testAddHostIgnoredByLink() {
874 setupTopologyManager();
875
876 final Dpid dpid = new Dpid(1);
877 SwitchEvent sw = new SwitchEvent(dpid);
878 sw.createStringAttribute("foo", "bar");
879
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700880 final PortNumber portNumberA = PortNumber.uint32(2);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700881 PortEvent portA = new PortEvent(dpid, portNumberA);
882 portA.createStringAttribute("fuzz", "buzz");
883
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700884 final PortNumber portNumberB = PortNumber.uint32(3);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700885 PortEvent portB = new PortEvent(dpid, portNumberB);
886 portB.createStringAttribute("fizz", "buz");
887
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700888 final PortNumber portNumberC = PortNumber.uint32(4);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700889 PortEvent portC = new PortEvent(dpid, portNumberC);
890 portC.createStringAttribute("fizz", "buz");
891
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700892 LinkEvent linkA = new LinkEvent(portA.getSwitchPort(),
893 portB.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700894 linkA.createStringAttribute(TopologyElement.TYPE,
895 TopologyElement.TYPE_OPTICAL_LAYER);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700896 LinkEvent linkB = new LinkEvent(portB.getSwitchPort(),
897 portA.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700898 linkB.createStringAttribute(TopologyElement.TYPE,
899 TopologyElement.TYPE_OPTICAL_LAYER);
900
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700901 TestUtils.callMethod(theTopologyManager, "addSwitch",
902 SwitchEvent.class, sw);
903 TestUtils.callMethod(theTopologyManager, "addPort",
904 PortEvent.class, portA);
905 TestUtils.callMethod(theTopologyManager, "addPort",
906 PortEvent.class, portB);
907 TestUtils.callMethod(theTopologyManager, "addPort",
908 PortEvent.class, portC);
909 TestUtils.callMethod(theTopologyManager, "addLink",
910 LinkEvent.class, linkA);
911 TestUtils.callMethod(theTopologyManager, "addLink",
912 LinkEvent.class, linkB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700913
914 // Add hostA attached to a port which already has a link
915 final MACAddress macA = MACAddress.valueOf(666L);
916 HostEvent hostA = new HostEvent(macA);
917 hostA.addAttachmentPoint(portA.getSwitchPort());
918 final long timestampA = 392893200L;
919 hostA.setLastSeenTime(timestampA);
920
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700921 TestUtils.callMethod(theTopologyManager, "addHost",
922 HostEvent.class, hostA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700923
924 // Add hostB attached to multiple ports,
925 // some of them which already has a link
926 final MACAddress macB = MACAddress.valueOf(999L);
927 HostEvent hostB = new HostEvent(macB);
928 hostB.addAttachmentPoint(portB.getSwitchPort());
929 hostB.addAttachmentPoint(portC.getSwitchPort());
930 final long timestampB = 392893201L;
931 hostB.setLastSeenTime(timestampB);
932
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700933 TestUtils.callMethod(theTopologyManager, "addHost",
934 HostEvent.class, hostB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700935
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700936 // Check the topology structure
937 TopologyInternal topology =
938 (TopologyInternal) theTopologyManager.getTopology();
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700939 SwitchEvent swInTopo = topology.getSwitchEvent(dpid);
940 assertEquals(sw, swInTopo);
941 assertTrue(swInTopo.isFrozen());
942 assertEquals("bar", swInTopo.getStringAttribute("foo"));
943
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700944 final SwitchPort switchPortA = new SwitchPort(dpid, portNumberA);
945 PortEvent portAInTopo = topology.getPortEvent(switchPortA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700946 assertEquals(portA, portAInTopo);
947 assertTrue(portAInTopo.isFrozen());
948 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
949
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700950 final SwitchPort switchPortB = new SwitchPort(dpid, portNumberB);
951 PortEvent portBInTopo = topology.getPortEvent(switchPortB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700952 assertEquals(portB, portBInTopo);
953 assertTrue(portBInTopo.isFrozen());
954 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
955
956 // hostA expected to be completely ignored
957 assertNull(topology.getHostEvent(macA));
958 // hostB expected to be there with reduced attachment point
959 HostEvent hostBrev = new HostEvent(macB);
960 hostBrev.addAttachmentPoint(portC.getSwitchPort());
961 hostBrev.setLastSeenTime(timestampB);
962 hostBrev.freeze();
963 assertEquals(hostBrev, topology.getHostEvent(macB));
964
965
966 LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple());
967 assertEquals(linkA, linkAInTopo);
968 assertTrue(linkAInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700969 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
970 linkAInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700971
972 LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple());
973 assertEquals(linkB, linkBInTopo);
974 assertTrue(linkBInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700975 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
976 linkBInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700977
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700978 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700979 // hostB should be added with reduced attachment points
980 List<HostEvent> apiAddedHostEvents
981 = TestUtils.getField(theTopologyManager, "apiAddedHostEvents");
982 assertThat(apiAddedHostEvents, hasItem(hostBrev));
983
984 // hostA should not be ignored
985 List<HostEvent> apiRemovedHostEvents
986 = TestUtils.getField(theTopologyManager, "apiRemovedHostEvents");
987 assertThat(apiRemovedHostEvents, not(hasItem(hostA)));
988
989 List<LinkEvent> apiAddedLinkEvents
990 = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents");
991 assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB));
992 }
993
994 /**
995 * Test to confirm topology replica transformation.
996 */
997 @Test
998 public void testAddHostMove() {
999 setupTopologyManager();
1000
1001 final Dpid dpid = new Dpid(1);
1002 SwitchEvent sw = new SwitchEvent(dpid);
1003 sw.createStringAttribute("foo", "bar");
1004
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001005 final PortNumber portNumberA = PortNumber.uint32(2);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001006 PortEvent portA = new PortEvent(dpid, portNumberA);
1007 portA.createStringAttribute("fuzz", "buzz");
1008
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001009 final PortNumber portNumberB = PortNumber.uint32(3);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001010 PortEvent portB = new PortEvent(dpid, portNumberB);
1011 portB.createStringAttribute("fizz", "buz");
1012
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001013 final PortNumber portNumberC = PortNumber.uint32(4);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001014 PortEvent portC = new PortEvent(dpid, portNumberC);
1015 portC.createStringAttribute("fizz", "buz");
1016
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001017 TestUtils.callMethod(theTopologyManager, "addSwitch",
1018 SwitchEvent.class, sw);
1019 TestUtils.callMethod(theTopologyManager, "addPort",
1020 PortEvent.class, portA);
1021 TestUtils.callMethod(theTopologyManager, "addPort",
1022 PortEvent.class, portB);
1023 TestUtils.callMethod(theTopologyManager, "addPort",
1024 PortEvent.class, portC);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001025
1026 // Add hostA attached to a port which already has a link
1027 final MACAddress macA = MACAddress.valueOf(666L);
1028 HostEvent hostA = new HostEvent(macA);
1029 hostA.addAttachmentPoint(portA.getSwitchPort());
1030 final long timestampA = 392893200L;
1031 hostA.setLastSeenTime(timestampA);
1032
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001033 TestUtils.callMethod(theTopologyManager, "addHost",
1034 HostEvent.class, hostA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001035
1036
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001037 // Check the topology structure
1038 TopologyInternal topology =
1039 (TopologyInternal) theTopologyManager.getTopology();
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001040 SwitchEvent swInTopo = topology.getSwitchEvent(dpid);
1041 assertEquals(sw, swInTopo);
1042 assertTrue(swInTopo.isFrozen());
1043 assertEquals("bar", swInTopo.getStringAttribute("foo"));
1044
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001045 final SwitchPort switchPortA = new SwitchPort(dpid, portNumberA);
1046 PortEvent portAInTopo = topology.getPortEvent(switchPortA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001047 assertEquals(portA, portAInTopo);
1048 assertTrue(portAInTopo.isFrozen());
1049 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
1050
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001051 final SwitchPort switchPortB = new SwitchPort(dpid, portNumberB);
1052 PortEvent portBInTopo = topology.getPortEvent(switchPortB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001053 assertEquals(portB, portBInTopo);
1054 assertTrue(portBInTopo.isFrozen());
1055 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
1056
1057 // hostA expected to be there
1058 assertEquals(hostA, topology.getHostEvent(macA));
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001059 assertEquals(timestampA,
1060 topology.getHostEvent(macA).getLastSeenTime());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001061
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001062 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001063 // hostA should be added
1064 List<HostEvent> apiAddedHostEvents
1065 = TestUtils.getField(theTopologyManager, "apiAddedHostEvents");
1066 assertThat(apiAddedHostEvents, hasItem(hostA));
1067
1068
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001069 // Clear the events before moving the host
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001070 apiAddedHostEvents.clear();
1071
1072 HostEvent hostAmoved = new HostEvent(macA);
1073 hostAmoved.addAttachmentPoint(portB.getSwitchPort());
1074 final long timestampAmoved = 392893201L;
1075 hostAmoved.setLastSeenTime(timestampAmoved);
1076
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001077 TestUtils.callMethod(theTopologyManager, "addHost",
1078 HostEvent.class, hostAmoved);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001079
1080 assertEquals(hostAmoved, topology.getHostEvent(macA));
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001081 assertEquals(timestampAmoved,
1082 topology.getHostEvent(macA).getLastSeenTime());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001083
1084 // hostA expected to be there with new attachment point
1085 apiAddedHostEvents
1086 = TestUtils.getField(theTopologyManager, "apiAddedHostEvents");
1087 assertThat(apiAddedHostEvents, hasItem(hostAmoved));
1088
1089 // hostA is updated not removed
1090 List<HostEvent> apiRemovedHostEvents
1091 = TestUtils.getField(theTopologyManager, "apiRemovedHostEvents");
1092 assertThat(apiRemovedHostEvents, not(hasItem(hostA)));
1093 }
weibitf7c31a42014-06-23 16:51:01 -07001094}