blob: ae783cc5ec19475575b9d53d4b7cdf88591739e7 [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;
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07009import net.onrc.onos.core.registry.RegistryException;
weibitf7c31a42014-06-23 16:51:01 -070010import net.onrc.onos.core.util.Dpid;
Pavlin Radoslavovc6236412014-08-01 20:37:46 -070011import net.onrc.onos.core.util.EventEntry;
Pavlin Radoslavov41633642014-08-11 14:24:52 -070012import static net.onrc.onos.core.util.ImmutableClassChecker.assertThatClassIsImmutable;
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -070013import net.onrc.onos.core.util.OnosInstanceId;
weibitf7c31a42014-06-23 16:51:01 -070014import net.onrc.onos.core.util.PortNumber;
15import net.onrc.onos.core.util.SwitchPort;
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070016import net.onrc.onos.core.util.TestUtils;
Ray Milkey38301352014-07-28 08:51:54 -070017import net.onrc.onos.core.util.UnitTest;
weibitf7c31a42014-06-23 16:51:01 -070018import org.easymock.EasyMock;
weibitf7c31a42014-06-23 16:51:01 -070019import org.junit.Before;
20import org.junit.Test;
21
Ray Milkey38301352014-07-28 08:51:54 -070022import java.util.ArrayList;
23import java.util.Collection;
Pavlin Radoslavovc6236412014-08-01 20:37:46 -070024import java.util.LinkedList;
Ray Milkey38301352014-07-28 08:51:54 -070025import java.util.List;
26import java.util.concurrent.CopyOnWriteArrayList;
27
28import static org.easymock.EasyMock.anyObject;
29import static org.easymock.EasyMock.createMock;
30import static org.easymock.EasyMock.eq;
31import static org.easymock.EasyMock.expect;
32import static org.easymock.EasyMock.replay;
Pavlin Radoslavovc6236412014-08-01 20:37:46 -070033import static org.easymock.EasyMock.reset;
Ray Milkey38301352014-07-28 08:51:54 -070034import static org.easymock.EasyMock.verify;
35import static org.hamcrest.Matchers.containsInAnyOrder;
Pavlin Radoslavovc6236412014-08-01 20:37:46 -070036import static org.hamcrest.Matchers.empty;
Ray Milkey38301352014-07-28 08:51:54 -070037import static org.hamcrest.Matchers.hasItem;
Pavlin Radoslavovc6236412014-08-01 20:37:46 -070038import static org.hamcrest.Matchers.is;
Ray Milkey38301352014-07-28 08:51:54 -070039import static org.hamcrest.Matchers.not;
40import static org.junit.Assert.assertEquals;
Pavlin Radoslavovc6236412014-08-01 20:37:46 -070041import static org.junit.Assert.assertNotNull;
Ray Milkey38301352014-07-28 08:51:54 -070042import static org.junit.Assert.assertNull;
43import static org.junit.Assert.assertThat;
44import static org.junit.Assert.assertTrue;
45
weibitf7c31a42014-06-23 16:51:01 -070046/**
47 * Unit tests for the TopologyManager class in the Topology module.
48 * These test cases only check the sanity of functions in the TopologyManager.
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -070049 * Note that we do not test the eventHandler functions in the TopologyManager
50 * class.
51 * DatagridService, DataStoreService, eventChannel, and
52 * controllerRegistryService are mocked out.
weibitf7c31a42014-06-23 16:51:01 -070053 */
Ray Milkey38301352014-07-28 08:51:54 -070054public class TopologyManagerTest extends UnitTest {
weibitf7c31a42014-06-23 16:51:01 -070055 private TopologyManager theTopologyManager;
Pavlin Radoslavovc6236412014-08-01 20:37:46 -070056 private TopologyManager.EventHandler theEventHandler;
57 private TopologyListenerTest theTopologyListener =
58 new TopologyListenerTest();
weibitf7c31a42014-06-23 16:51:01 -070059 private final String eventChannelName = "onos.topology";
60 private IEventChannel<byte[], TopologyEvent> eventChannel;
61 private IDatagridService datagridService;
62 private TopologyDatastore dataStoreService;
63 private IControllerRegistryService registryService;
weibitf7c31a42014-06-23 16:51:01 -070064 private Collection<TopologyEvent> allTopologyEvents;
Pavlin Radoslavovc6236412014-08-01 20:37:46 -070065 private static final OnosInstanceId ONOS_INSTANCE_ID_1 =
66 new OnosInstanceId("ONOS-Instance-ID-1");
67 private static final OnosInstanceId ONOS_INSTANCE_ID_2 =
68 new OnosInstanceId("ONOS-Instance-ID-2");
69 private static final Dpid DPID_1 = new Dpid(1);
70 private static final Dpid DPID_2 = new Dpid(2);
71
72 /**
73 * Topology events listener.
74 */
75 private class TopologyListenerTest implements ITopologyListener {
76 private TopologyEvents topologyEvents;
77
78 @Override
79 public void topologyEvents(TopologyEvents events) {
80 this.topologyEvents = events;
81 }
82
83 /**
84 * Clears the Topology Listener state.
85 */
86 public void clear() {
87 this.topologyEvents = null;
88 }
89 }
weibitf7c31a42014-06-23 16:51:01 -070090
91 @SuppressWarnings("unchecked")
92 @Before
93 public void setUp() throws Exception {
94 // Mock objects for testing
95 datagridService = EasyMock.createNiceMock(IDatagridService.class);
96 dataStoreService = EasyMock.createNiceMock(TopologyDatastore.class);
97 registryService = createMock(IControllerRegistryService.class);
98 eventChannel = EasyMock.createNiceMock(IEventChannel.class);
99
100 expect(datagridService.createChannel(
101 eq(eventChannelName),
102 eq(byte[].class),
103 eq(TopologyEvent.class)))
104 .andReturn(eventChannel).once();
105
106 expect(datagridService.addListener(
107 eq(eventChannelName),
108 anyObject(IEventChannelListener.class),
109 eq(byte[].class),
110 eq(TopologyEvent.class)))
111 .andReturn(eventChannel).once();
112
113 expect(dataStoreService.addSwitch(
114 anyObject(SwitchEvent.class),
115 anyObject(Collection.class)))
116 .andReturn(true).anyTimes();
117
118 expect(dataStoreService.deactivateSwitch(
119 anyObject(SwitchEvent.class),
120 anyObject(Collection.class)))
121 .andReturn(true).anyTimes();
122
123 expect(dataStoreService.addPort(
124 anyObject(PortEvent.class)))
125 .andReturn(true).anyTimes();
126
127 expect(dataStoreService.deactivatePort(
128 anyObject(PortEvent.class)))
129 .andReturn(true).anyTimes();
130
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700131 expect(dataStoreService.addHost(
132 anyObject(HostEvent.class)))
weibitf7c31a42014-06-23 16:51:01 -0700133 .andReturn(true).anyTimes();
134
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700135 expect(dataStoreService.removeHost(
136 anyObject(HostEvent.class)))
weibitf7c31a42014-06-23 16:51:01 -0700137 .andReturn(true).anyTimes();
138
139 expect(dataStoreService.addLink(
140 anyObject(LinkEvent.class)))
141 .andReturn(true).anyTimes();
142
143 expect(dataStoreService.removeLink(
144 anyObject(LinkEvent.class)))
145 .andReturn(true).anyTimes();
146
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700147 // Setup the Registry Service
148 expect(registryService.getOnosInstanceId()).andReturn(ONOS_INSTANCE_ID_1).anyTimes();
Pavlin Radoslavov054cd592014-08-07 20:57:16 -0700149 expect(registryService.getControllerForSwitch(DPID_1.value()))
150 .andReturn(ONOS_INSTANCE_ID_1.toString()).anyTimes();
151 expect(registryService.getControllerForSwitch(DPID_2.value()))
152 .andReturn(ONOS_INSTANCE_ID_2.toString()).anyTimes();
weibitf7c31a42014-06-23 16:51:01 -0700153
154 allTopologyEvents = new CopyOnWriteArrayList<>();
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700155 expect(eventChannel.getAllEntries())
156 .andReturn(allTopologyEvents).anyTimes();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700157
158 replay(datagridService);
159 replay(registryService);
160 replay(dataStoreService);
161 // replay(eventChannel);
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700162 }
weibitf7c31a42014-06-23 16:51:01 -0700163
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700164 /**
165 * Setup the Topology Manager.
166 */
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700167 private void setupTopologyManager() {
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700168 // Create a TopologyManager object for testing
Pavlin Radoslavov8a44b782014-08-07 12:53:27 -0700169 theTopologyManager = new TopologyManager(registryService);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700170
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700171 // Replace the eventHandler to prevent the thread from starting
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700172 TestUtils.setField(theTopologyManager, "eventHandler",
173 EasyMock.createNiceMock(TopologyManager.EventHandler.class));
weibitf7c31a42014-06-23 16:51:01 -0700174 theTopologyManager.startup(datagridService);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700175
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700176 // Replace the data store with a mocked object
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700177 TestUtils.setField(theTopologyManager, "datastore", dataStoreService);
weibitf7c31a42014-06-23 16:51:01 -0700178 }
179
weibitf7c31a42014-06-23 16:51:01 -0700180 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700181 * Setup the Topology Manager with the Event Handler.
182 */
183 private void setupTopologyManagerWithEventHandler() {
184 // Create a TopologyManager object for testing
Pavlin Radoslavov8a44b782014-08-07 12:53:27 -0700185 theTopologyManager = new TopologyManager(registryService);
Pavlin Radoslavov054cd592014-08-07 20:57:16 -0700186 theTopologyManager.addListener(theTopologyListener, true);
Pavlin Radoslavov8a44b782014-08-07 12:53:27 -0700187
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700188 // Allocate the Event Handler, so we can have direct access to it
189 theEventHandler = theTopologyManager.new EventHandler();
190 TestUtils.setField(theTopologyManager, "eventHandler",
191 theEventHandler);
192
193 // Replace the data store with a mocked object
194 TestUtils.setField(theTopologyManager, "datastore", dataStoreService);
195
196 replay(eventChannel);
197 //
198 // NOTE: Uncomment-out the line below if the startup() method needs
199 // to be called for some of the unit tests. For now it is commented-out
200 // to avoid any side effects of starting the eventHandler thread.
201 //
202 // theTopologyManager.startup(datagridService);
203 }
204
205 /**
Pavlin Radoslavov41633642014-08-11 14:24:52 -0700206 * Tests the immutability of {@link TopologyEvents}.
207 */
208 @Test
209 public void testImmutableTopologyEvents() {
210 assertThatClassIsImmutable(TopologyEvents.class);
211 }
212
213 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700214 * Test the Switch Mastership Updated Event.
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700215 */
216 @Test
217 public void testPutSwitchMastershipEvent() {
218 // Mock the eventChannel functions first
219 eventChannel.addEntry(anyObject(byte[].class),
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700220 anyObject(TopologyEvent.class));
221 EasyMock.expectLastCall().times(1, 1); // 1 event
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700222 replay(eventChannel);
223
224 setupTopologyManager();
225
226 // Generate a new Switch Mastership event
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700227 Role role = Role.MASTER;
228 MastershipEvent mastershipEvent =
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700229 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700230
231 // Call the topologyManager function for adding the event
232 theTopologyManager.putSwitchMastershipEvent(mastershipEvent);
233
234 // Verify the function calls
235 verify(eventChannel);
236 }
237
238 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700239 * Test the Switch Mastership Removed Event.
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700240 */
241 @Test
242 public void testRemoveSwitchMastershipEvent() {
243 // Mock the eventChannel functions first
244 eventChannel.removeEntry(anyObject(byte[].class));
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700245 EasyMock.expectLastCall().times(1, 1); // 1 event
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700246 replay(eventChannel);
247
248 setupTopologyManager();
249
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700250 // Generate a new Switch Mastership Event
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700251 Role role = Role.MASTER;
252 MastershipEvent mastershipEvent =
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700253 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700254
255 // Call the topologyManager function for removing the event
256 theTopologyManager.removeSwitchMastershipEvent(mastershipEvent);
257
258 // Verify the function calls
259 verify(eventChannel);
260 }
261
262 /**
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700263 * Test the Switch discovered and Port discovered functions.
264 */
265 @Test
266 public void testPutSwitchAndPortDiscoveryEvent() {
267 // Mock the eventChannel functions first
268 eventChannel.addEntry(anyObject(byte[].class),
269 anyObject(TopologyEvent.class));
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700270 EasyMock.expectLastCall().times(3, 3); // (1 Switch + 1 Port), 1 Port
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700271 replay(eventChannel);
272
273 setupTopologyManager();
274
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700275 // Mock Switch has one Port
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700276 PortNumber portNumber = PortNumber.uint32(1);
277
278 // Generate a new Switch Event along with a Port Event
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700279 SwitchEvent switchEvent = new SwitchEvent(DPID_1);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700280
281 Collection<PortEvent> portEvents = new ArrayList<PortEvent>();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700282 portEvents.add(new PortEvent(DPID_1, portNumber));
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700283
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700284 // Call the topologyManager function for adding a Switch
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700285 theTopologyManager.putSwitchDiscoveryEvent(switchEvent, portEvents);
286
287 for (PortEvent portEvent : portEvents) {
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700288 // Call the topologyManager function for adding a Port
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700289 theTopologyManager.putPortDiscoveryEvent(portEvent);
290 }
291
292 // Verify the function calls
293 verify(eventChannel);
294
295 }
296
297 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700298 * Test the Switch and Port removed functions.
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700299 */
300 @Test
301 public void testRemoveSwitchAndPortDiscoveryEvent() {
302 // Mock the eventChannel functions first
303 eventChannel.removeEntry(anyObject(byte[].class));
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700304 EasyMock.expectLastCall().times(2, 2); // 1 Switch, 1 Port
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700305 replay(eventChannel);
306
307 setupTopologyManager();
308
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700309 PortNumber portNumber = PortNumber.uint32(1);
310
311 // Generate a Port Event
312 Collection<PortEvent> portEvents = new ArrayList<PortEvent>();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700313 portEvents.add(new PortEvent(DPID_1, portNumber));
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700314
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700315 // Call the topologyManager function for removing a Port
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700316 for (PortEvent portEvent : portEvents) {
317 theTopologyManager.removePortDiscoveryEvent(portEvent);
318 }
319
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700320 // Call the topologyManager function for removing a Switch
321 SwitchEvent switchEvent = new SwitchEvent(DPID_1);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700322 theTopologyManager.removeSwitchDiscoveryEvent(switchEvent);
323
324 // Verify the function calls
325 verify(eventChannel);
326
327 }
328
329 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700330 * Test the Link discovered function.
weibitf7c31a42014-06-23 16:51:01 -0700331 */
332 @Test
333 public void testPutLinkDiscoveryEvent() {
334 // Mock the eventChannel functions first
335 eventChannel.addEntry(anyObject(byte[].class),
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700336 anyObject(TopologyEvent.class));
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700337 EasyMock.expectLastCall().times(5, 5); // (2 Switch + 2 Port + 1 Link)
weibitf7c31a42014-06-23 16:51:01 -0700338 replay(eventChannel);
339
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700340 setupTopologyManager();
341
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700342 // Generate the Switch and Port Events
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700343 PortNumber portNumber1 = PortNumber.uint32(1);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700344 SwitchEvent switchEvent1 = new SwitchEvent(DPID_1);
weibitf7c31a42014-06-23 16:51:01 -0700345 Collection<PortEvent> portEvents1 = new ArrayList<PortEvent>();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700346 portEvents1.add(new PortEvent(DPID_1, portNumber1));
weibitf7c31a42014-06-23 16:51:01 -0700347
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700348 // Call the topologyManager function for adding a Switch
weibitf7c31a42014-06-23 16:51:01 -0700349 theTopologyManager.putSwitchDiscoveryEvent(switchEvent1, portEvents1);
350
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700351 // Generate the Switch and Port Events
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700352 PortNumber portNumber2 = PortNumber.uint32(2);
353 SwitchEvent switchEvent2 = new SwitchEvent(DPID_2);
weibitf7c31a42014-06-23 16:51:01 -0700354 Collection<PortEvent> portEvents2 = new ArrayList<PortEvent>();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700355 portEvents2.add(new PortEvent(DPID_2, portNumber2));
weibitf7c31a42014-06-23 16:51:01 -0700356
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700357 // Call the topologyManager function for adding a Switch
weibitf7c31a42014-06-23 16:51:01 -0700358 theTopologyManager.putSwitchDiscoveryEvent(switchEvent2, portEvents2);
359
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700360 // Create the Link Event
361 LinkEvent linkEvent =
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700362 new LinkEvent(new SwitchPort(DPID_1, portNumber1),
363 new SwitchPort(DPID_2, portNumber2));
weibitf7c31a42014-06-23 16:51:01 -0700364 theTopologyManager.putLinkDiscoveryEvent(linkEvent);
365
366 // Verify the function calls
367 verify(eventChannel);
368 }
369
370 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700371 * Test the Link removed function.
weibitf7c31a42014-06-23 16:51:01 -0700372 */
373 @Test
374 public void testRemoveLinkDiscoveryEvent() {
375 // Mock the eventChannel functions first
376 eventChannel.removeEntry(anyObject(byte[].class));
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700377 EasyMock.expectLastCall().times(1, 1); // (1 Link)
weibitf7c31a42014-06-23 16:51:01 -0700378 replay(eventChannel);
379
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700380 setupTopologyManager();
381
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700382 // Generate the Switch and Port Events
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700383 PortNumber portNumber1 = PortNumber.uint32(1);
384 SwitchEvent switchEvent1 = new SwitchEvent(DPID_1);
weibitf7c31a42014-06-23 16:51:01 -0700385 Collection<PortEvent> portEvents1 = new ArrayList<PortEvent>();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700386 portEvents1.add(new PortEvent(DPID_1, portNumber1));
weibitf7c31a42014-06-23 16:51:01 -0700387
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700388 // Call the topologyManager function for adding a Switch
weibitf7c31a42014-06-23 16:51:01 -0700389 theTopologyManager.putSwitchDiscoveryEvent(switchEvent1, portEvents1);
390
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700391 // Generate the Switch and Port Events
392 PortNumber portNumber2 = PortNumber.uint32(2);
393 SwitchEvent switchEvent2 = new SwitchEvent(DPID_2);
weibitf7c31a42014-06-23 16:51:01 -0700394 Collection<PortEvent> portEvents2 = new ArrayList<PortEvent>();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700395 portEvents2.add(new PortEvent(DPID_2, portNumber2));
weibitf7c31a42014-06-23 16:51:01 -0700396
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700397 // Call the topologyManager function for adding a Switch
weibitf7c31a42014-06-23 16:51:01 -0700398 theTopologyManager.putSwitchDiscoveryEvent(switchEvent2, portEvents2);
399
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700400 // Remove the Link
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700401 LinkEvent linkEventRemove =
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700402 new LinkEvent(new SwitchPort(DPID_1, portNumber1),
403 new SwitchPort(DPID_2, portNumber2));
weibitf7c31a42014-06-23 16:51:01 -0700404 theTopologyManager.removeLinkDiscoveryEvent(linkEventRemove);
405
406 // Verify the function calls
407 verify(eventChannel);
408 }
409
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700410 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700411 * Test the Host discovered function.
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700412 */
413 @Test
414 public void testPutHostDiscoveryEvent() {
415 // Mock the eventChannel functions first
416 eventChannel.addEntry(anyObject(byte[].class),
417 anyObject(TopologyEvent.class));
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700418 EasyMock.expectLastCall().times(1, 1); // 1 Host
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700419 replay(eventChannel);
420
421 setupTopologyManager();
422
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700423 // Generate a new Host Event
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700424 PortNumber portNumber = PortNumber.uint32(1);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700425 MACAddress hostMac = MACAddress.valueOf("00:AA:11:BB:33:CC");
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700426 SwitchPort sp = new SwitchPort(DPID_1, portNumber);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700427 List<SwitchPort> spLists = new ArrayList<SwitchPort>();
428 spLists.add(sp);
429 HostEvent hostEvent = new HostEvent(hostMac);
430 hostEvent.setAttachmentPoints(spLists);
431
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700432 // Call the topologyManager function for adding a Host
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700433 theTopologyManager.putHostDiscoveryEvent(hostEvent);
434
435 // Verify the function calls
436 verify(eventChannel);
437 }
438
439 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700440 * Test the Host removed function.
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700441 */
442 @Test
443 public void testRemoveHostDiscoveryEvent() {
444 // Mock the eventChannel functions first
445 eventChannel.removeEntry(anyObject(byte[].class));
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700446 EasyMock.expectLastCall().times(1, 1); // 1 Host
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700447 replay(eventChannel);
448
449 setupTopologyManager();
450
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700451 // Generate a new Host Event
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700452 PortNumber portNumber = PortNumber.uint32(1);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700453 MACAddress hostMac = MACAddress.valueOf("00:AA:11:BB:33:CC");
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700454 SwitchPort sp = new SwitchPort(DPID_1, portNumber);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700455 List<SwitchPort> spLists = new ArrayList<SwitchPort>();
456 spLists.add(sp);
457 HostEvent hostEvent = new HostEvent(hostMac);
458 hostEvent.setAttachmentPoints(spLists);
459
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700460 // Call the topologyManager function for removing a Host
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700461 theTopologyManager.removeHostDiscoveryEvent(hostEvent);
462
463 // Verify the function calls
464 verify(eventChannel);
465 }
466
467 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700468 * Tests adding of a Switch Mastership event and the topology replica
469 * transformation.
470 */
471 @Test
472 public void testAddMastershipEvent() {
473 setupTopologyManager();
474
475 // Prepare the event
476 Role role = Role.MASTER;
477 MastershipEvent mastershipEvent =
478 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
479 // Add the event
480 TestUtils.callMethod(theTopologyManager, "addMastershipEvent",
481 MastershipEvent.class, mastershipEvent);
482
483 //
484 // NOTE: The topology itself doesn't contain the Mastership Events,
485 // hence we don't check the topology.
486 //
487
488 // Check the events to be fired
489 List<MastershipEvent> apiAddedMastershipEvents
490 = TestUtils.getField(theTopologyManager,
491 "apiAddedMastershipEvents");
492 assertThat(apiAddedMastershipEvents, hasItem(mastershipEvent));
493 }
494
495 /**
496 * Tests removing of a Switch Mastership event and the topology replica
497 * transformation.
498 */
499 @Test
500 public void testRemoveMastershipEvent() {
501 setupTopologyManager();
502
503 // Prepare the event
504 Role role = Role.MASTER;
505 MastershipEvent mastershipEvent =
506 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
507 // Add the event
508 TestUtils.callMethod(theTopologyManager, "addMastershipEvent",
509 MastershipEvent.class, mastershipEvent);
510
511 // Check the events to be fired
512 List<MastershipEvent> apiAddedMastershipEvents
513 = TestUtils.getField(theTopologyManager,
514 "apiAddedMastershipEvents");
515 assertThat(apiAddedMastershipEvents, hasItem(mastershipEvent));
516
517 // Remove the event
518 TestUtils.callMethod(theTopologyManager, "removeMastershipEvent",
519 MastershipEvent.class,
520 new MastershipEvent(mastershipEvent));
521
522 // Check the events to be fired
523 List<MastershipEvent> apiRemovedMastershipEvents
524 = TestUtils.getField(theTopologyManager,
525 "apiRemovedMastershipEvents");
526 assertThat(apiRemovedMastershipEvents, hasItem(mastershipEvent));
527 }
528
529 /**
530 * Tests adding of a Switch and the topology replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700531 */
532 @Test
533 public void testAddSwitch() {
534 setupTopologyManager();
535
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700536 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700537 sw.createStringAttribute("foo", "bar");
538
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700539 TestUtils.callMethod(theTopologyManager, "addSwitch",
540 SwitchEvent.class, sw);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700541
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700542 // Check the topology structure
543 TopologyInternal topology =
544 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700545 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700546 assertEquals(sw, swInTopo);
547 assertTrue(swInTopo.isFrozen());
548 assertEquals("bar", swInTopo.getStringAttribute("foo"));
549
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700550 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700551 List<SwitchEvent> apiAddedSwitchEvents
552 = TestUtils.getField(theTopologyManager, "apiAddedSwitchEvents");
553 assertThat(apiAddedSwitchEvents, hasItem(sw));
554 }
555
556 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700557 * Tests adding of a Port and the topology replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700558 */
559 @Test
560 public void testAddPort() {
561 setupTopologyManager();
562
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700563 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700564 sw.createStringAttribute("foo", "bar");
565
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700566 final PortNumber portNumber = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700567 PortEvent port = new PortEvent(DPID_1, portNumber);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700568 port.createStringAttribute("fuzz", "buzz");
569
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700570 TestUtils.callMethod(theTopologyManager, "addSwitch",
571 SwitchEvent.class, sw);
572 TestUtils.callMethod(theTopologyManager, "addPort",
573 PortEvent.class, port);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700574
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700575 // Check the topology structure
576 TopologyInternal topology =
577 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700578 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700579 assertEquals(sw, swInTopo);
580 assertTrue(swInTopo.isFrozen());
581 assertEquals("bar", swInTopo.getStringAttribute("foo"));
582
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700583 final SwitchPort switchPort = new SwitchPort(DPID_1, portNumber);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700584 PortEvent portInTopo = topology.getPortEvent(switchPort);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700585 assertEquals(port, portInTopo);
586 assertTrue(portInTopo.isFrozen());
587 assertEquals("buzz", portInTopo.getStringAttribute("fuzz"));
588
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700589 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700590 List<PortEvent> apiAddedPortEvents
591 = TestUtils.getField(theTopologyManager, "apiAddedPortEvents");
592 assertThat(apiAddedPortEvents, hasItem(port));
593 }
594
595 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700596 * Tests removing of a Port followed by removing of a Switch,
597 * and the topology replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700598 */
599 @Test
600 public void testRemovePortThenSwitch() {
601 setupTopologyManager();
602
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700603 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700604 sw.createStringAttribute("foo", "bar");
605
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700606 final PortNumber portNumber = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700607 PortEvent port = new PortEvent(DPID_1, portNumber);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700608 port.createStringAttribute("fuzz", "buzz");
609
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700610 TestUtils.callMethod(theTopologyManager, "addSwitch",
611 SwitchEvent.class, sw);
612 TestUtils.callMethod(theTopologyManager, "addPort",
613 PortEvent.class, port);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700614
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700615 // Check the topology structure
616 TopologyInternal topology =
617 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700618 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700619 assertEquals(sw, swInTopo);
620 assertTrue(swInTopo.isFrozen());
621 assertEquals("bar", swInTopo.getStringAttribute("foo"));
622
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700623 final SwitchPort switchPort = new SwitchPort(DPID_1, portNumber);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700624 PortEvent portInTopo = topology.getPortEvent(switchPort);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700625 assertEquals(port, portInTopo);
626 assertTrue(portInTopo.isFrozen());
627 assertEquals("buzz", portInTopo.getStringAttribute("fuzz"));
628
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700629 // Remove in proper order
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700630 TestUtils.callMethod(theTopologyManager, "removePort",
631 PortEvent.class, new PortEvent(port));
632 TestUtils.callMethod(theTopologyManager, "removeSwitch",
633 SwitchEvent.class, new SwitchEvent(sw));
634
635
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700636 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700637 List<PortEvent> apiRemovedPortEvents
638 = TestUtils.getField(theTopologyManager, "apiRemovedPortEvents");
639 assertThat(apiRemovedPortEvents, hasItem(port));
640 List<SwitchEvent> apiRemovedSwitchEvents
641 = TestUtils.getField(theTopologyManager, "apiRemovedSwitchEvents");
642 assertThat(apiRemovedSwitchEvents, hasItem(sw));
643 }
644
645 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700646 * Tests removing of a Switch without removing of a Port,
647 * and the topology replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700648 */
649 @Test
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700650 public void testRemoveSwitchWithoutPortRemoval() {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700651 setupTopologyManager();
652
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700653 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700654 sw.createStringAttribute("foo", "bar");
655
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700656 final PortNumber portNumber = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700657 PortEvent port = new PortEvent(DPID_1, portNumber);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700658 port.createStringAttribute("fuzz", "buzz");
659
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700660 TestUtils.callMethod(theTopologyManager, "addSwitch",
661 SwitchEvent.class, sw);
662 TestUtils.callMethod(theTopologyManager, "addPort",
663 PortEvent.class, port);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700664
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700665 // Check the topology structure
666 TopologyInternal topology =
667 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700668 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700669 assertEquals(sw, swInTopo);
670 assertTrue(swInTopo.isFrozen());
671 assertEquals("bar", swInTopo.getStringAttribute("foo"));
672
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700673 final SwitchPort switchPort = new SwitchPort(DPID_1, portNumber);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700674 PortEvent portInTopo = topology.getPortEvent(switchPort);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700675 assertEquals(port, portInTopo);
676 assertTrue(portInTopo.isFrozen());
677 assertEquals("buzz", portInTopo.getStringAttribute("fuzz"));
678
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700679 // Remove in in-proper order
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700680// TestUtils.callMethod(theTopologyManager, "removePort",
681// PortEvent.class, new PortEvent(port));
682 TestUtils.callMethod(theTopologyManager, "removeSwitch",
683 SwitchEvent.class, new SwitchEvent(sw));
684
685
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700686 // Check the events to be fired
687 // The outcome should be the same as #testRemovePortThenSwitch
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700688 List<PortEvent> apiRemovedPortEvents
689 = TestUtils.getField(theTopologyManager, "apiRemovedPortEvents");
690 assertThat(apiRemovedPortEvents, hasItem(port));
691 List<SwitchEvent> apiRemovedSwitchEvents
692 = TestUtils.getField(theTopologyManager, "apiRemovedSwitchEvents");
693 assertThat(apiRemovedSwitchEvents, hasItem(sw));
694 }
695
696 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700697 * Tests adding of a Link and the topology replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700698 */
699 @Test
700 public void testAddLink() {
701 setupTopologyManager();
702
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700703 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700704 sw.createStringAttribute("foo", "bar");
705
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700706 final PortNumber portNumberA = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700707 PortEvent portA = new PortEvent(DPID_1, portNumberA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700708 portA.createStringAttribute("fuzz", "buzz");
709
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700710 final PortNumber portNumberB = PortNumber.uint32(3);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700711 PortEvent portB = new PortEvent(DPID_1, portNumberB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700712 portB.createStringAttribute("fizz", "buz");
713
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700714 LinkEvent linkA = new LinkEvent(portA.getSwitchPort(),
715 portB.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700716 linkA.createStringAttribute(TopologyElement.TYPE,
717 TopologyElement.TYPE_OPTICAL_LAYER);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700718 LinkEvent linkB = new LinkEvent(portB.getSwitchPort(),
719 portA.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700720 linkB.createStringAttribute(TopologyElement.TYPE,
721 TopologyElement.TYPE_OPTICAL_LAYER);
722
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700723 TestUtils.callMethod(theTopologyManager, "addSwitch",
724 SwitchEvent.class, sw);
725 TestUtils.callMethod(theTopologyManager, "addPort",
726 PortEvent.class, portA);
727 TestUtils.callMethod(theTopologyManager, "addPort",
728 PortEvent.class, portB);
729 TestUtils.callMethod(theTopologyManager, "addLink",
730 LinkEvent.class, linkA);
731 TestUtils.callMethod(theTopologyManager, "addLink",
732 LinkEvent.class, linkB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700733
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700734 // Check the topology structure
735 TopologyInternal topology =
736 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700737 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700738 assertEquals(sw, swInTopo);
739 assertTrue(swInTopo.isFrozen());
740 assertEquals("bar", swInTopo.getStringAttribute("foo"));
741
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700742 final SwitchPort switchPortA = new SwitchPort(DPID_1, portNumberA);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700743 PortEvent portAInTopo = topology.getPortEvent(switchPortA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700744 assertEquals(portA, portAInTopo);
745 assertTrue(portAInTopo.isFrozen());
746 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
747
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700748 final SwitchPort switchPortB = new SwitchPort(DPID_1, portNumberB);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700749 PortEvent portBInTopo = topology.getPortEvent(switchPortB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700750 assertEquals(portB, portBInTopo);
751 assertTrue(portBInTopo.isFrozen());
752 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
753
754 LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple());
755 assertEquals(linkA, linkAInTopo);
756 assertTrue(linkAInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700757 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
758 linkAInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700759
760 LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple());
761 assertEquals(linkB, linkBInTopo);
762 assertTrue(linkBInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700763 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
764 linkBInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700765
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700766 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700767 List<LinkEvent> apiAddedLinkEvents
768 = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents");
769 assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB));
770 }
771
772 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700773 * Tests removing of a Link without removing of a Host, and the topology
774 * replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700775 */
776 @Test
777 public void testAddLinkKickingOffHost() {
778 setupTopologyManager();
779
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700780 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700781 sw.createStringAttribute("foo", "bar");
782
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700783 final PortNumber portNumberA = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700784 PortEvent portA = new PortEvent(DPID_1, portNumberA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700785 portA.createStringAttribute("fuzz", "buzz");
786
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700787 final PortNumber portNumberB = PortNumber.uint32(3);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700788 PortEvent portB = new PortEvent(DPID_1, portNumberB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700789 portB.createStringAttribute("fizz", "buz");
790
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700791 final PortNumber portNumberC = PortNumber.uint32(4);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700792 PortEvent portC = new PortEvent(DPID_1, portNumberC);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700793 portC.createStringAttribute("fizz", "buz");
794
795 final MACAddress macA = MACAddress.valueOf(666L);
796 HostEvent hostA = new HostEvent(macA);
797 hostA.addAttachmentPoint(portA.getSwitchPort());
798 final long timestampA = 392893200L;
799 hostA.setLastSeenTime(timestampA);
800
801 final MACAddress macB = MACAddress.valueOf(999L);
802 HostEvent hostB = new HostEvent(macB);
803 hostB.addAttachmentPoint(portB.getSwitchPort());
804 hostB.addAttachmentPoint(portC.getSwitchPort());
805 final long timestampB = 392893201L;
806 hostB.setLastSeenTime(timestampB);
807
808
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700809 LinkEvent linkA = new LinkEvent(portA.getSwitchPort(),
810 portB.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700811 linkA.createStringAttribute(TopologyElement.TYPE,
812 TopologyElement.TYPE_OPTICAL_LAYER);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700813 LinkEvent linkB = new LinkEvent(portB.getSwitchPort(),
814 portA.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700815 linkB.createStringAttribute(TopologyElement.TYPE,
816 TopologyElement.TYPE_OPTICAL_LAYER);
817
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700818 TestUtils.callMethod(theTopologyManager, "addSwitch",
819 SwitchEvent.class, sw);
820 TestUtils.callMethod(theTopologyManager, "addPort",
821 PortEvent.class, portA);
822 TestUtils.callMethod(theTopologyManager, "addPort",
823 PortEvent.class, portB);
824 TestUtils.callMethod(theTopologyManager, "addPort",
825 PortEvent.class, portC);
826 TestUtils.callMethod(theTopologyManager, "addHost",
827 HostEvent.class, hostA);
828 TestUtils.callMethod(theTopologyManager, "addHost",
829 HostEvent.class, hostB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700830
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700831 TestUtils.callMethod(theTopologyManager, "addLink",
832 LinkEvent.class, linkA);
833 TestUtils.callMethod(theTopologyManager, "addLink",
834 LinkEvent.class, linkB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700835
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700836 // Check the topology structure
837 TopologyInternal topology =
838 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700839 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700840 assertEquals(sw, swInTopo);
841 assertTrue(swInTopo.isFrozen());
842 assertEquals("bar", swInTopo.getStringAttribute("foo"));
843
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700844 final SwitchPort switchPortA = new SwitchPort(DPID_1, portNumberA);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700845 PortEvent portAInTopo = topology.getPortEvent(switchPortA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700846 assertEquals(portA, portAInTopo);
847 assertTrue(portAInTopo.isFrozen());
848 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
849
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700850 final SwitchPort switchPortB = new SwitchPort(DPID_1, portNumberB);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700851 PortEvent portBInTopo = topology.getPortEvent(switchPortB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700852 assertEquals(portB, portBInTopo);
853 assertTrue(portBInTopo.isFrozen());
854 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
855
856 // hostA expected to be removed
857 assertNull(topology.getHostEvent(macA));
858 // hostB expected to be there with reduced attachment point
859 HostEvent hostBrev = new HostEvent(macB);
860 hostBrev.addAttachmentPoint(portC.getSwitchPort());
861 hostBrev.setLastSeenTime(timestampB);
862 hostBrev.freeze();
863 assertEquals(hostBrev, topology.getHostEvent(macB));
864
865
866 LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple());
867 assertEquals(linkA, linkAInTopo);
868 assertTrue(linkAInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700869 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
870 linkAInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700871
872 LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple());
873 assertEquals(linkB, linkBInTopo);
874 assertTrue(linkBInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700875 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
876 linkBInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700877
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700878 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700879 List<HostEvent> apiAddedHostEvents
880 = TestUtils.getField(theTopologyManager, "apiAddedHostEvents");
881 assertThat(apiAddedHostEvents, hasItem(hostBrev));
882
883 List<HostEvent> apiRemovedHostEvents
884 = TestUtils.getField(theTopologyManager, "apiRemovedHostEvents");
885 assertThat(apiRemovedHostEvents, hasItem(hostA));
886 List<LinkEvent> apiAddedLinkEvents
887 = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents");
888 assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB));
889 }
890
891 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700892 * Tests removing of a Link and the topology replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700893 */
894 @Test
895 public void testRemoveLink() {
896 setupTopologyManager();
897
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700898 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700899 sw.createStringAttribute("foo", "bar");
900
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700901 final PortNumber portNumberA = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700902 PortEvent portA = new PortEvent(DPID_1, portNumberA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700903 portA.createStringAttribute("fuzz", "buzz");
904
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700905 final PortNumber portNumberB = PortNumber.uint32(3);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700906 PortEvent portB = new PortEvent(DPID_1, portNumberB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700907 portB.createStringAttribute("fizz", "buz");
908
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700909 LinkEvent linkA = new LinkEvent(portA.getSwitchPort(),
910 portB.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700911 linkA.createStringAttribute(TopologyElement.TYPE,
912 TopologyElement.TYPE_OPTICAL_LAYER);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700913 LinkEvent linkB = new LinkEvent(portB.getSwitchPort(),
914 portA.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700915 linkB.createStringAttribute(TopologyElement.TYPE,
916 TopologyElement.TYPE_OPTICAL_LAYER);
917
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700918 TestUtils.callMethod(theTopologyManager, "addSwitch",
919 SwitchEvent.class, sw);
920 TestUtils.callMethod(theTopologyManager, "addPort",
921 PortEvent.class, portA);
922 TestUtils.callMethod(theTopologyManager, "addPort",
923 PortEvent.class, portB);
924 TestUtils.callMethod(theTopologyManager, "addLink",
925 LinkEvent.class, linkA);
926 TestUtils.callMethod(theTopologyManager, "addLink",
927 LinkEvent.class, linkB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700928
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700929 // Check the topology structure
930 TopologyInternal topology =
931 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700932 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700933 assertEquals(sw, swInTopo);
934 assertTrue(swInTopo.isFrozen());
935 assertEquals("bar", swInTopo.getStringAttribute("foo"));
936
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700937 final SwitchPort switchPortA = new SwitchPort(DPID_1, portNumberA);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700938 PortEvent portAInTopo = topology.getPortEvent(switchPortA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700939 assertEquals(portA, portAInTopo);
940 assertTrue(portAInTopo.isFrozen());
941 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
942
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700943 final SwitchPort switchPortB = new SwitchPort(DPID_1, portNumberB);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700944 PortEvent portBInTopo = topology.getPortEvent(switchPortB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700945 assertEquals(portB, portBInTopo);
946 assertTrue(portBInTopo.isFrozen());
947 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
948
949 LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple());
950 assertEquals(linkA, linkAInTopo);
951 assertTrue(linkAInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700952 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
953 linkAInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700954
955
956 LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple());
957 assertEquals(linkB, linkBInTopo);
958 assertTrue(linkBInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700959 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
960 linkBInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700961
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700962 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700963 // FIXME if link flapped (linkA in this scenario),
964 // linkA appears in both removed and added is this expected behavior?
965 List<LinkEvent> apiAddedLinkEvents
966 = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents");
967 assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB));
968
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700969 // Clear the events before removing the link
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700970 apiAddedLinkEvents.clear();
971
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700972 // Remove the link
973 TestUtils.callMethod(theTopologyManager, "removeLink",
974 LinkEvent.class, new LinkEvent(linkA));
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700975
976 LinkEvent linkANotInTopo = topology.getLinkEvent(linkA.getLinkTuple());
977 assertNull(linkANotInTopo);
978
979 List<LinkEvent> apiRemovedLinkEvents
980 = TestUtils.getField(theTopologyManager, "apiRemovedLinkEvents");
981 assertThat(apiRemovedLinkEvents, hasItem(linkA));
982 }
983
984 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700985 * Tests adding of a Host without adding of a Link, and the topology
986 * replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700987 */
988 @Test
989 public void testAddHostIgnoredByLink() {
990 setupTopologyManager();
991
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700992 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700993 sw.createStringAttribute("foo", "bar");
994
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700995 final PortNumber portNumberA = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700996 PortEvent portA = new PortEvent(DPID_1, portNumberA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700997 portA.createStringAttribute("fuzz", "buzz");
998
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700999 final PortNumber portNumberB = PortNumber.uint32(3);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001000 PortEvent portB = new PortEvent(DPID_1, portNumberB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001001 portB.createStringAttribute("fizz", "buz");
1002
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001003 final PortNumber portNumberC = PortNumber.uint32(4);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001004 PortEvent portC = new PortEvent(DPID_1, portNumberC);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001005 portC.createStringAttribute("fizz", "buz");
1006
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001007 LinkEvent linkA = new LinkEvent(portA.getSwitchPort(),
1008 portB.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001009 linkA.createStringAttribute(TopologyElement.TYPE,
1010 TopologyElement.TYPE_OPTICAL_LAYER);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001011 LinkEvent linkB = new LinkEvent(portB.getSwitchPort(),
1012 portA.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001013 linkB.createStringAttribute(TopologyElement.TYPE,
1014 TopologyElement.TYPE_OPTICAL_LAYER);
1015
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001016 TestUtils.callMethod(theTopologyManager, "addSwitch",
1017 SwitchEvent.class, sw);
1018 TestUtils.callMethod(theTopologyManager, "addPort",
1019 PortEvent.class, portA);
1020 TestUtils.callMethod(theTopologyManager, "addPort",
1021 PortEvent.class, portB);
1022 TestUtils.callMethod(theTopologyManager, "addPort",
1023 PortEvent.class, portC);
1024 TestUtils.callMethod(theTopologyManager, "addLink",
1025 LinkEvent.class, linkA);
1026 TestUtils.callMethod(theTopologyManager, "addLink",
1027 LinkEvent.class, linkB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001028
1029 // Add hostA attached to a port which already has a link
1030 final MACAddress macA = MACAddress.valueOf(666L);
1031 HostEvent hostA = new HostEvent(macA);
1032 hostA.addAttachmentPoint(portA.getSwitchPort());
1033 final long timestampA = 392893200L;
1034 hostA.setLastSeenTime(timestampA);
1035
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001036 TestUtils.callMethod(theTopologyManager, "addHost",
1037 HostEvent.class, hostA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001038
1039 // Add hostB attached to multiple ports,
1040 // some of them which already has a link
1041 final MACAddress macB = MACAddress.valueOf(999L);
1042 HostEvent hostB = new HostEvent(macB);
1043 hostB.addAttachmentPoint(portB.getSwitchPort());
1044 hostB.addAttachmentPoint(portC.getSwitchPort());
1045 final long timestampB = 392893201L;
1046 hostB.setLastSeenTime(timestampB);
1047
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001048 TestUtils.callMethod(theTopologyManager, "addHost",
1049 HostEvent.class, hostB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001050
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001051 // Check the topology structure
1052 TopologyInternal topology =
1053 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001054 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001055 assertEquals(sw, swInTopo);
1056 assertTrue(swInTopo.isFrozen());
1057 assertEquals("bar", swInTopo.getStringAttribute("foo"));
1058
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001059 final SwitchPort switchPortA = new SwitchPort(DPID_1, portNumberA);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001060 PortEvent portAInTopo = topology.getPortEvent(switchPortA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001061 assertEquals(portA, portAInTopo);
1062 assertTrue(portAInTopo.isFrozen());
1063 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
1064
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001065 final SwitchPort switchPortB = new SwitchPort(DPID_1, portNumberB);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001066 PortEvent portBInTopo = topology.getPortEvent(switchPortB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001067 assertEquals(portB, portBInTopo);
1068 assertTrue(portBInTopo.isFrozen());
1069 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
1070
1071 // hostA expected to be completely ignored
1072 assertNull(topology.getHostEvent(macA));
1073 // hostB expected to be there with reduced attachment point
1074 HostEvent hostBrev = new HostEvent(macB);
1075 hostBrev.addAttachmentPoint(portC.getSwitchPort());
1076 hostBrev.setLastSeenTime(timestampB);
1077 hostBrev.freeze();
1078 assertEquals(hostBrev, topology.getHostEvent(macB));
1079
1080
1081 LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple());
1082 assertEquals(linkA, linkAInTopo);
1083 assertTrue(linkAInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001084 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
1085 linkAInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001086
1087 LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple());
1088 assertEquals(linkB, linkBInTopo);
1089 assertTrue(linkBInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001090 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
1091 linkBInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001092
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001093 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001094 // hostB should be added with reduced attachment points
1095 List<HostEvent> apiAddedHostEvents
1096 = TestUtils.getField(theTopologyManager, "apiAddedHostEvents");
1097 assertThat(apiAddedHostEvents, hasItem(hostBrev));
1098
1099 // hostA should not be ignored
1100 List<HostEvent> apiRemovedHostEvents
1101 = TestUtils.getField(theTopologyManager, "apiRemovedHostEvents");
1102 assertThat(apiRemovedHostEvents, not(hasItem(hostA)));
1103
1104 List<LinkEvent> apiAddedLinkEvents
1105 = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents");
1106 assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB));
1107 }
1108
1109 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001110 * Tests adding and moving of a Host, and the topology replica
1111 * transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001112 */
1113 @Test
1114 public void testAddHostMove() {
1115 setupTopologyManager();
1116
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001117 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001118 sw.createStringAttribute("foo", "bar");
1119
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001120 final PortNumber portNumberA = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001121 PortEvent portA = new PortEvent(DPID_1, portNumberA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001122 portA.createStringAttribute("fuzz", "buzz");
1123
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001124 final PortNumber portNumberB = PortNumber.uint32(3);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001125 PortEvent portB = new PortEvent(DPID_1, portNumberB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001126 portB.createStringAttribute("fizz", "buz");
1127
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001128 final PortNumber portNumberC = PortNumber.uint32(4);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001129 PortEvent portC = new PortEvent(DPID_1, portNumberC);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001130 portC.createStringAttribute("fizz", "buz");
1131
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001132 TestUtils.callMethod(theTopologyManager, "addSwitch",
1133 SwitchEvent.class, sw);
1134 TestUtils.callMethod(theTopologyManager, "addPort",
1135 PortEvent.class, portA);
1136 TestUtils.callMethod(theTopologyManager, "addPort",
1137 PortEvent.class, portB);
1138 TestUtils.callMethod(theTopologyManager, "addPort",
1139 PortEvent.class, portC);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001140
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001141 // Add hostA attached to a Port which already has a Link
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001142 final MACAddress macA = MACAddress.valueOf(666L);
1143 HostEvent hostA = new HostEvent(macA);
1144 hostA.addAttachmentPoint(portA.getSwitchPort());
1145 final long timestampA = 392893200L;
1146 hostA.setLastSeenTime(timestampA);
1147
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001148 TestUtils.callMethod(theTopologyManager, "addHost",
1149 HostEvent.class, hostA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001150
1151
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001152 // Check the topology structure
1153 TopologyInternal topology =
1154 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001155 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001156 assertEquals(sw, swInTopo);
1157 assertTrue(swInTopo.isFrozen());
1158 assertEquals("bar", swInTopo.getStringAttribute("foo"));
1159
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001160 final SwitchPort switchPortA = new SwitchPort(DPID_1, portNumberA);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001161 PortEvent portAInTopo = topology.getPortEvent(switchPortA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001162 assertEquals(portA, portAInTopo);
1163 assertTrue(portAInTopo.isFrozen());
1164 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
1165
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001166 final SwitchPort switchPortB = new SwitchPort(DPID_1, portNumberB);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001167 PortEvent portBInTopo = topology.getPortEvent(switchPortB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001168 assertEquals(portB, portBInTopo);
1169 assertTrue(portBInTopo.isFrozen());
1170 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
1171
1172 // hostA expected to be there
1173 assertEquals(hostA, topology.getHostEvent(macA));
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001174 assertEquals(timestampA,
1175 topology.getHostEvent(macA).getLastSeenTime());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001176
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001177 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001178 // hostA should be added
1179 List<HostEvent> apiAddedHostEvents
1180 = TestUtils.getField(theTopologyManager, "apiAddedHostEvents");
1181 assertThat(apiAddedHostEvents, hasItem(hostA));
1182
1183
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001184 // Clear the events before moving the Host
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001185 apiAddedHostEvents.clear();
1186
1187 HostEvent hostAmoved = new HostEvent(macA);
1188 hostAmoved.addAttachmentPoint(portB.getSwitchPort());
1189 final long timestampAmoved = 392893201L;
1190 hostAmoved.setLastSeenTime(timestampAmoved);
1191
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001192 TestUtils.callMethod(theTopologyManager, "addHost",
1193 HostEvent.class, hostAmoved);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001194
1195 assertEquals(hostAmoved, topology.getHostEvent(macA));
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001196 assertEquals(timestampAmoved,
1197 topology.getHostEvent(macA).getLastSeenTime());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001198
1199 // hostA expected to be there with new attachment point
1200 apiAddedHostEvents
1201 = TestUtils.getField(theTopologyManager, "apiAddedHostEvents");
1202 assertThat(apiAddedHostEvents, hasItem(hostAmoved));
1203
1204 // hostA is updated not removed
1205 List<HostEvent> apiRemovedHostEvents
1206 = TestUtils.getField(theTopologyManager, "apiRemovedHostEvents");
1207 assertThat(apiRemovedHostEvents, not(hasItem(hostA)));
1208 }
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001209
1210 /**
1211 * Tests processing of a Switch Mastership Event and the delivery of the
1212 * topology events.
1213 */
1214 @Test
1215 public void testProcessMastershipEvent() {
1216 List<EventEntry<TopologyEvent>> events = new LinkedList<>();
1217 EventEntry<TopologyEvent> eventEntry;
1218 TopologyEvent topologyEvent;
1219
1220 setupTopologyManagerWithEventHandler();
1221
1222 // Prepare the Mastership Event
1223 Role role = Role.MASTER;
1224 MastershipEvent mastershipEvent =
1225 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
1226 topologyEvent = new TopologyEvent(mastershipEvent, ONOS_INSTANCE_ID_1);
1227
1228 // Add the Mastership Event
1229 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1230 topologyEvent);
1231 events.add(eventEntry);
1232
1233 // Process the events
1234 TestUtils.callMethod(theEventHandler, "processEvents",
1235 List.class, events);
1236
1237 // Check the fired events
1238 TopologyEvents topologyEvents = theTopologyListener.topologyEvents;
1239 assertNotNull(topologyEvents);
1240 assertThat(topologyEvents.getAddedMastershipEvents(),
1241 hasItem(mastershipEvent));
1242 theTopologyListener.clear();
1243 }
1244
1245 /**
1246 * Tests processing of a Switch Event, and the delivery of the topology
1247 * events.
1248 *
1249 * We test the following scenario:
1250 * - Switch Mastership Event is processed along with a Switch Event - both
1251 * events should be delivered.
1252 */
1253 @Test
1254 public void testProcessSwitchEvent() {
1255 TopologyEvents topologyEvents;
1256 List<EventEntry<TopologyEvent>> events = new LinkedList<>();
1257 EventEntry<TopologyEvent> eventEntry;
1258 TopologyEvent topologyMastershipEvent;
1259 TopologyEvent topologySwitchEvent;
1260
1261 setupTopologyManagerWithEventHandler();
1262
1263 // Prepare the Mastership Event
1264 Role role = Role.MASTER;
1265 MastershipEvent mastershipEvent =
1266 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
1267 topologyMastershipEvent = new TopologyEvent(mastershipEvent,
1268 ONOS_INSTANCE_ID_1);
1269
1270 // Prepare the Switch Event
1271 SwitchEvent switchEvent = new SwitchEvent(DPID_1);
1272 topologySwitchEvent = new TopologyEvent(switchEvent,
1273 ONOS_INSTANCE_ID_1);
1274
1275 // Add the Mastership Event
1276 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1277 topologyMastershipEvent);
1278 events.add(eventEntry);
1279
1280 // Add the Switch Event
1281 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1282 topologySwitchEvent);
1283 events.add(eventEntry);
1284
1285 // Process the events
1286 TestUtils.callMethod(theEventHandler, "processEvents",
1287 List.class, events);
1288
1289 // Check the fired events
1290 topologyEvents = theTopologyListener.topologyEvents;
1291 assertNotNull(topologyEvents);
1292 assertThat(topologyEvents.getAddedMastershipEvents(),
1293 hasItem(mastershipEvent));
1294 assertThat(topologyEvents.getAddedSwitchEvents(),
1295 hasItem(switchEvent));
1296 theTopologyListener.clear();
1297 }
1298
1299 /**
1300 * Tests processing of a misordered Switch Event, and the delivery of the
1301 * topology events.
1302 *
1303 * We test the following scenario:
1304 * - Only a Switch Event is processed first, later followed by a Switch
1305 * Mastership Event - the Switch Event should be delivered after the
1306 * Switch Mastership Event is processed.
1307 */
1308 @Test
1309 public void testProcessMisorderedSwitchEvent() {
1310 TopologyEvents topologyEvents;
1311 List<EventEntry<TopologyEvent>> events = new LinkedList<>();
1312 EventEntry<TopologyEvent> eventEntry;
1313 TopologyEvent topologyMastershipEvent;
1314 TopologyEvent topologySwitchEvent;
1315
1316 setupTopologyManagerWithEventHandler();
1317
1318 // Prepare the Mastership Event
1319 Role role = Role.MASTER;
1320 MastershipEvent mastershipEvent =
1321 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
1322 topologyMastershipEvent = new TopologyEvent(mastershipEvent,
1323 ONOS_INSTANCE_ID_1);
1324
1325 // Prepare the Switch Event
1326 SwitchEvent switchEvent = new SwitchEvent(DPID_1);
1327 topologySwitchEvent = new TopologyEvent(switchEvent,
1328 ONOS_INSTANCE_ID_1);
1329
1330 // Add the Switch Event
1331 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1332 topologySwitchEvent);
1333 events.add(eventEntry);
1334
1335 // Process the events
1336 TestUtils.callMethod(theEventHandler, "processEvents",
1337 List.class, events);
1338
1339 // Check the fired events: no events should be fired
1340 topologyEvents = theTopologyListener.topologyEvents;
1341 assertNull(topologyEvents);
1342 theTopologyListener.clear();
1343 events.clear();
1344
1345 // Add the Mastership Event
1346 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1347 topologyMastershipEvent);
1348 events.add(eventEntry);
1349
1350 // Process the events
1351 TestUtils.callMethod(theEventHandler, "processEvents",
1352 List.class, events);
1353
1354 // Check the fired events: both events should be fired
1355 topologyEvents = theTopologyListener.topologyEvents;
1356 assertNotNull(topologyEvents);
1357 assertThat(topologyEvents.getAddedMastershipEvents(),
1358 hasItem(mastershipEvent));
1359 assertThat(topologyEvents.getAddedSwitchEvents(),
1360 hasItem(switchEvent));
1361 theTopologyListener.clear();
1362 }
1363
1364 /**
1365 * Tests processing of a Switch Event with Mastership Event from
1366 * another ONOS instance, and the delivery of the topology events.
1367 *
1368 * We test the following scenario:
1369 * - Only a Switch Event is processed first, later followed by a Switch
1370 * Mastership Event from another ONOS instance - only the Switch
1371 * Mastership Event should be delivered.
1372 */
1373 @Test
1374 public void testProcessSwitchEventNoMastership() {
1375 TopologyEvents topologyEvents;
1376 List<EventEntry<TopologyEvent>> events = new LinkedList<>();
1377 EventEntry<TopologyEvent> eventEntry;
1378 TopologyEvent topologyMastershipEvent;
1379 TopologyEvent topologySwitchEvent;
1380
1381 setupTopologyManagerWithEventHandler();
1382
1383 // Prepare the Mastership Event
1384 Role role = Role.MASTER;
1385 MastershipEvent mastershipEvent =
1386 new MastershipEvent(DPID_2, ONOS_INSTANCE_ID_2, role);
1387 topologyMastershipEvent = new TopologyEvent(mastershipEvent,
1388 ONOS_INSTANCE_ID_2);
1389
1390 // Prepare the Switch Event
1391 // NOTE: The originator (ONOS_INSTANCE_ID_1) is NOT the Master
1392 SwitchEvent switchEvent = new SwitchEvent(DPID_2);
1393 topologySwitchEvent = new TopologyEvent(switchEvent,
1394 ONOS_INSTANCE_ID_1);
1395
1396 // Add the Switch Event
1397 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1398 topologySwitchEvent);
1399 events.add(eventEntry);
1400
1401 // Process the events
1402 TestUtils.callMethod(theEventHandler, "processEvents",
1403 List.class, events);
1404
1405 // Check the fired events: no events should be fired
1406 topologyEvents = theTopologyListener.topologyEvents;
1407 assertNull(topologyEvents);
1408 theTopologyListener.clear();
1409 events.clear();
1410
1411 // Add the Mastership Event
1412 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1413 topologyMastershipEvent);
1414 events.add(eventEntry);
1415
1416 // Process the events
1417 TestUtils.callMethod(theEventHandler, "processEvents",
1418 List.class, events);
1419
1420 // Check the fired events: only the Mastership event should be fired
1421 topologyEvents = theTopologyListener.topologyEvents;
1422 assertNotNull(topologyEvents);
1423 assertThat(topologyEvents.getAddedMastershipEvents(),
1424 hasItem(mastershipEvent));
1425 assertThat(topologyEvents.getAddedSwitchEvents(), is(empty()));
1426 theTopologyListener.clear();
1427 }
1428
1429 /**
1430 * Tests processing of Switch Events with Mastership switchover between
1431 * two ONOS instance, and the delivery of the topology events.
1432 *
1433 * We test the following scenario:
1434 * - Initially, a Mastership Event and a Switch Event from one ONOS
1435 * instance are processed - both events should be delivered.
1436 * - Later, a Mastership Event and a Switch event from another ONOS
1437 * instances are processed - both events should be delivered.
1438 * - Finally, a REMOVE Switch Event is received from the first ONOS
1439 * instance - no event should be delivered.
Pavlin Radoslavov054cd592014-08-07 20:57:16 -07001440 *
1441 * @throws RegistryException
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001442 */
1443 @Test
Pavlin Radoslavov054cd592014-08-07 20:57:16 -07001444 public void testProcessSwitchMastershipSwitchover()
1445 throws RegistryException {
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001446 TopologyEvents topologyEvents;
1447 List<EventEntry<TopologyEvent>> events = new LinkedList<>();
1448 EventEntry<TopologyEvent> eventEntry;
1449 TopologyEvent topologyMastershipEvent;
1450 TopologyEvent topologySwitchEvent;
1451
1452 setupTopologyManagerWithEventHandler();
1453
1454 // Prepare the Mastership Event from the first ONOS instance
1455 Role role = Role.MASTER;
1456 MastershipEvent mastershipEvent =
1457 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
1458 topologyMastershipEvent = new TopologyEvent(mastershipEvent,
1459 ONOS_INSTANCE_ID_1);
1460
1461 // Prepare the Switch Event from the first ONOS instance
1462 SwitchEvent switchEvent = new SwitchEvent(DPID_1);
1463 topologySwitchEvent = new TopologyEvent(switchEvent,
1464 ONOS_INSTANCE_ID_1);
1465
1466 // Add the Mastership Event
1467 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1468 topologyMastershipEvent);
1469 events.add(eventEntry);
1470
1471 // Add the Switch Event
1472 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1473 topologySwitchEvent);
1474 events.add(eventEntry);
1475
1476 // Process the events
1477 TestUtils.callMethod(theEventHandler, "processEvents",
1478 List.class, events);
1479
1480 // Check the fired events: both events should be fired
1481 topologyEvents = theTopologyListener.topologyEvents;
1482 assertNotNull(topologyEvents);
1483 assertThat(topologyEvents.getAddedMastershipEvents(),
1484 hasItem(mastershipEvent));
1485 assertThat(topologyEvents.getAddedSwitchEvents(),
1486 hasItem(switchEvent));
1487 theTopologyListener.clear();
1488 events.clear();
1489
1490 //
1491 // Update the Registry Service, so the second ONOS instance is the
1492 // Master.
1493 //
1494 reset(registryService);
Pavlin Radoslavov054cd592014-08-07 20:57:16 -07001495 expect(registryService.getControllerForSwitch(DPID_1.value()))
1496 .andReturn(ONOS_INSTANCE_ID_2.toString()).anyTimes();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001497 replay(registryService);
1498
1499 // Prepare the Mastership Event from the second ONOS instance
1500 role = Role.MASTER;
1501 mastershipEvent = new MastershipEvent(DPID_1,
1502 ONOS_INSTANCE_ID_2, role);
1503 topologyMastershipEvent = new TopologyEvent(mastershipEvent,
1504 ONOS_INSTANCE_ID_2);
1505
1506 // Prepare the Switch Event from second ONOS instance
1507 switchEvent = new SwitchEvent(DPID_1);
1508 topologySwitchEvent = new TopologyEvent(switchEvent,
1509 ONOS_INSTANCE_ID_2);
1510
1511 // Add the Mastership Event
1512 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1513 topologyMastershipEvent);
1514 events.add(eventEntry);
1515
1516 // Add the Switch Event
1517 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1518 topologySwitchEvent);
1519 events.add(eventEntry);
1520
1521 // Process the events
1522 TestUtils.callMethod(theEventHandler, "processEvents",
1523 List.class, events);
1524
1525 // Check the fired events: both events should be fired
1526 topologyEvents = theTopologyListener.topologyEvents;
1527 assertNotNull(topologyEvents);
1528 assertThat(topologyEvents.getAddedMastershipEvents(),
1529 hasItem(mastershipEvent));
1530 assertThat(topologyEvents.getAddedSwitchEvents(),
1531 hasItem(switchEvent));
1532 theTopologyListener.clear();
1533 events.clear();
1534
1535 // Prepare the REMOVE Switch Event from first ONOS instance
1536 switchEvent = new SwitchEvent(DPID_1);
1537 topologySwitchEvent = new TopologyEvent(switchEvent,
1538 ONOS_INSTANCE_ID_1);
1539 // Add the Switch Event
1540 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_REMOVE,
1541 topologySwitchEvent);
1542 events.add(eventEntry);
1543
1544 // Process the events
1545 TestUtils.callMethod(theEventHandler, "processEvents",
1546 List.class, events);
1547
1548 // Check the fired events: no events should be fired
1549 topologyEvents = theTopologyListener.topologyEvents;
1550 assertNull(topologyEvents);
1551 theTopologyListener.clear();
1552 events.clear();
1553 }
weibitf7c31a42014-06-23 16:51:01 -07001554}