blob: 2f8f377a19459cba0cdf2a9d5d198d34761c6f7c [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.junit.Before;
19import org.junit.Test;
20
Ray Milkey38301352014-07-28 08:51:54 -070021import java.util.ArrayList;
22import java.util.Collection;
Pavlin Radoslavovc6236412014-08-01 20:37:46 -070023import java.util.LinkedList;
Ray Milkey38301352014-07-28 08:51:54 -070024import java.util.List;
25import java.util.concurrent.CopyOnWriteArrayList;
26
27import static org.easymock.EasyMock.anyObject;
28import static org.easymock.EasyMock.createMock;
Sho SHIMIZU107344e2014-08-13 16:11:53 -070029import static org.easymock.EasyMock.createNiceMock;
Ray Milkey38301352014-07-28 08:51:54 -070030import static org.easymock.EasyMock.eq;
31import static org.easymock.EasyMock.expect;
Sho SHIMIZU107344e2014-08-13 16:11:53 -070032import static org.easymock.EasyMock.expectLastCall;
Ray Milkey38301352014-07-28 08:51:54 -070033import static org.easymock.EasyMock.replay;
Pavlin Radoslavovc6236412014-08-01 20:37:46 -070034import static org.easymock.EasyMock.reset;
Ray Milkey38301352014-07-28 08:51:54 -070035import static org.easymock.EasyMock.verify;
36import static org.hamcrest.Matchers.containsInAnyOrder;
Pavlin Radoslavovc6236412014-08-01 20:37:46 -070037import static org.hamcrest.Matchers.empty;
Ray Milkey38301352014-07-28 08:51:54 -070038import static org.hamcrest.Matchers.hasItem;
Pavlin Radoslavovc6236412014-08-01 20:37:46 -070039import static org.hamcrest.Matchers.is;
Ray Milkey38301352014-07-28 08:51:54 -070040import static org.hamcrest.Matchers.not;
41import static org.junit.Assert.assertEquals;
Pavlin Radoslavovc6236412014-08-01 20:37:46 -070042import static org.junit.Assert.assertNotNull;
Ray Milkey38301352014-07-28 08:51:54 -070043import static org.junit.Assert.assertNull;
44import static org.junit.Assert.assertThat;
45import static org.junit.Assert.assertTrue;
46
weibitf7c31a42014-06-23 16:51:01 -070047/**
48 * Unit tests for the TopologyManager class in the Topology module.
49 * These test cases only check the sanity of functions in the TopologyManager.
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -070050 * Note that we do not test the eventHandler functions in the TopologyManager
51 * class.
52 * DatagridService, DataStoreService, eventChannel, and
53 * controllerRegistryService are mocked out.
weibitf7c31a42014-06-23 16:51:01 -070054 */
Ray Milkey38301352014-07-28 08:51:54 -070055public class TopologyManagerTest extends UnitTest {
weibitf7c31a42014-06-23 16:51:01 -070056 private TopologyManager theTopologyManager;
Pavlin Radoslavovc6236412014-08-01 20:37:46 -070057 private TopologyManager.EventHandler theEventHandler;
58 private TopologyListenerTest theTopologyListener =
59 new TopologyListenerTest();
weibitf7c31a42014-06-23 16:51:01 -070060 private final String eventChannelName = "onos.topology";
61 private IEventChannel<byte[], TopologyEvent> eventChannel;
62 private IDatagridService datagridService;
63 private TopologyDatastore dataStoreService;
64 private IControllerRegistryService registryService;
weibitf7c31a42014-06-23 16:51:01 -070065 private Collection<TopologyEvent> allTopologyEvents;
Pavlin Radoslavovc6236412014-08-01 20:37:46 -070066 private static final OnosInstanceId ONOS_INSTANCE_ID_1 =
67 new OnosInstanceId("ONOS-Instance-ID-1");
68 private static final OnosInstanceId ONOS_INSTANCE_ID_2 =
69 new OnosInstanceId("ONOS-Instance-ID-2");
70 private static final Dpid DPID_1 = new Dpid(1);
71 private static final Dpid DPID_2 = new Dpid(2);
72
73 /**
74 * Topology events listener.
75 */
76 private class TopologyListenerTest implements ITopologyListener {
77 private TopologyEvents topologyEvents;
78
79 @Override
80 public void topologyEvents(TopologyEvents events) {
81 this.topologyEvents = events;
82 }
83
84 /**
85 * Clears the Topology Listener state.
86 */
87 public void clear() {
88 this.topologyEvents = null;
89 }
90 }
weibitf7c31a42014-06-23 16:51:01 -070091
92 @SuppressWarnings("unchecked")
93 @Before
94 public void setUp() throws Exception {
95 // Mock objects for testing
Sho SHIMIZU107344e2014-08-13 16:11:53 -070096 datagridService = createNiceMock(IDatagridService.class);
97 dataStoreService = createNiceMock(TopologyDatastore.class);
weibitf7c31a42014-06-23 16:51:01 -070098 registryService = createMock(IControllerRegistryService.class);
Sho SHIMIZU107344e2014-08-13 16:11:53 -070099 eventChannel = createNiceMock(IEventChannel.class);
weibitf7c31a42014-06-23 16:51:01 -0700100
101 expect(datagridService.createChannel(
102 eq(eventChannelName),
103 eq(byte[].class),
104 eq(TopologyEvent.class)))
105 .andReturn(eventChannel).once();
106
107 expect(datagridService.addListener(
108 eq(eventChannelName),
109 anyObject(IEventChannelListener.class),
110 eq(byte[].class),
111 eq(TopologyEvent.class)))
112 .andReturn(eventChannel).once();
113
114 expect(dataStoreService.addSwitch(
115 anyObject(SwitchEvent.class),
116 anyObject(Collection.class)))
117 .andReturn(true).anyTimes();
118
119 expect(dataStoreService.deactivateSwitch(
120 anyObject(SwitchEvent.class),
121 anyObject(Collection.class)))
122 .andReturn(true).anyTimes();
123
124 expect(dataStoreService.addPort(
125 anyObject(PortEvent.class)))
126 .andReturn(true).anyTimes();
127
128 expect(dataStoreService.deactivatePort(
129 anyObject(PortEvent.class)))
130 .andReturn(true).anyTimes();
131
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700132 expect(dataStoreService.addHost(
133 anyObject(HostEvent.class)))
weibitf7c31a42014-06-23 16:51:01 -0700134 .andReturn(true).anyTimes();
135
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700136 expect(dataStoreService.removeHost(
137 anyObject(HostEvent.class)))
weibitf7c31a42014-06-23 16:51:01 -0700138 .andReturn(true).anyTimes();
139
140 expect(dataStoreService.addLink(
141 anyObject(LinkEvent.class)))
142 .andReturn(true).anyTimes();
143
144 expect(dataStoreService.removeLink(
145 anyObject(LinkEvent.class)))
146 .andReturn(true).anyTimes();
147
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700148 // Setup the Registry Service
149 expect(registryService.getOnosInstanceId()).andReturn(ONOS_INSTANCE_ID_1).anyTimes();
Pavlin Radoslavov054cd592014-08-07 20:57:16 -0700150 expect(registryService.getControllerForSwitch(DPID_1.value()))
151 .andReturn(ONOS_INSTANCE_ID_1.toString()).anyTimes();
152 expect(registryService.getControllerForSwitch(DPID_2.value()))
153 .andReturn(ONOS_INSTANCE_ID_2.toString()).anyTimes();
weibitf7c31a42014-06-23 16:51:01 -0700154
155 allTopologyEvents = new CopyOnWriteArrayList<>();
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700156 expect(eventChannel.getAllEntries())
157 .andReturn(allTopologyEvents).anyTimes();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700158
159 replay(datagridService);
160 replay(registryService);
161 replay(dataStoreService);
162 // replay(eventChannel);
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700163 }
weibitf7c31a42014-06-23 16:51:01 -0700164
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700165 /**
166 * Setup the Topology Manager.
167 */
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700168 private void setupTopologyManager() {
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700169 // Create a TopologyManager object for testing
Pavlin Radoslavov8a44b782014-08-07 12:53:27 -0700170 theTopologyManager = new TopologyManager(registryService);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700171
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700172 // Replace the eventHandler to prevent the thread from starting
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700173 TestUtils.setField(theTopologyManager, "eventHandler",
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700174 createNiceMock(TopologyManager.EventHandler.class));
weibitf7c31a42014-06-23 16:51:01 -0700175 theTopologyManager.startup(datagridService);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700176
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700177 // Replace the data store with a mocked object
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700178 TestUtils.setField(theTopologyManager, "datastore", dataStoreService);
weibitf7c31a42014-06-23 16:51:01 -0700179 }
180
weibitf7c31a42014-06-23 16:51:01 -0700181 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700182 * Setup the Topology Manager with the Event Handler.
183 */
184 private void setupTopologyManagerWithEventHandler() {
185 // Create a TopologyManager object for testing
Pavlin Radoslavov8a44b782014-08-07 12:53:27 -0700186 theTopologyManager = new TopologyManager(registryService);
Pavlin Radoslavov054cd592014-08-07 20:57:16 -0700187 theTopologyManager.addListener(theTopologyListener, true);
Pavlin Radoslavov8a44b782014-08-07 12:53:27 -0700188
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700189 // Allocate the Event Handler, so we can have direct access to it
190 theEventHandler = theTopologyManager.new EventHandler();
191 TestUtils.setField(theTopologyManager, "eventHandler",
192 theEventHandler);
193
194 // Replace the data store with a mocked object
195 TestUtils.setField(theTopologyManager, "datastore", dataStoreService);
196
197 replay(eventChannel);
198 //
199 // NOTE: Uncomment-out the line below if the startup() method needs
200 // to be called for some of the unit tests. For now it is commented-out
201 // to avoid any side effects of starting the eventHandler thread.
202 //
203 // theTopologyManager.startup(datagridService);
204 }
205
206 /**
Pavlin Radoslavov41633642014-08-11 14:24:52 -0700207 * Tests the immutability of {@link TopologyEvents}.
208 */
209 @Test
210 public void testImmutableTopologyEvents() {
211 assertThatClassIsImmutable(TopologyEvents.class);
212 }
213
214 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700215 * Test the Switch Mastership Updated Event.
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700216 */
217 @Test
218 public void testPutSwitchMastershipEvent() {
219 // Mock the eventChannel functions first
220 eventChannel.addEntry(anyObject(byte[].class),
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700221 anyObject(TopologyEvent.class));
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700222 expectLastCall().times(1, 1); // 1 event
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700223 replay(eventChannel);
224
225 setupTopologyManager();
226
227 // Generate a new Switch Mastership event
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700228 Role role = Role.MASTER;
229 MastershipEvent mastershipEvent =
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700230 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700231
232 // Call the topologyManager function for adding the event
233 theTopologyManager.putSwitchMastershipEvent(mastershipEvent);
234
235 // Verify the function calls
236 verify(eventChannel);
237 }
238
239 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700240 * Test the Switch Mastership Removed Event.
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700241 */
242 @Test
243 public void testRemoveSwitchMastershipEvent() {
244 // Mock the eventChannel functions first
245 eventChannel.removeEntry(anyObject(byte[].class));
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700246 expectLastCall().times(1, 1); // 1 event
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700247 replay(eventChannel);
248
249 setupTopologyManager();
250
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700251 // Generate a new Switch Mastership Event
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700252 Role role = Role.MASTER;
253 MastershipEvent mastershipEvent =
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700254 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700255
256 // Call the topologyManager function for removing the event
257 theTopologyManager.removeSwitchMastershipEvent(mastershipEvent);
258
259 // Verify the function calls
260 verify(eventChannel);
261 }
262
263 /**
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700264 * Test the Switch discovered and Port discovered functions.
265 */
266 @Test
267 public void testPutSwitchAndPortDiscoveryEvent() {
268 // Mock the eventChannel functions first
269 eventChannel.addEntry(anyObject(byte[].class),
270 anyObject(TopologyEvent.class));
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700271 expectLastCall().times(3, 3); // (1 Switch + 1 Port), 1 Port
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700272 replay(eventChannel);
273
274 setupTopologyManager();
275
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700276 // Mock Switch has one Port
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700277 PortNumber portNumber = PortNumber.uint32(1);
278
279 // Generate a new Switch Event along with a Port Event
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700280 SwitchEvent switchEvent = new SwitchEvent(DPID_1);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700281
282 Collection<PortEvent> portEvents = new ArrayList<PortEvent>();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700283 portEvents.add(new PortEvent(DPID_1, portNumber));
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700284
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700285 // Call the topologyManager function for adding a Switch
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700286 theTopologyManager.putSwitchDiscoveryEvent(switchEvent, portEvents);
287
288 for (PortEvent portEvent : portEvents) {
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700289 // Call the topologyManager function for adding a Port
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700290 theTopologyManager.putPortDiscoveryEvent(portEvent);
291 }
292
293 // Verify the function calls
294 verify(eventChannel);
295
296 }
297
298 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700299 * Test the Switch and Port removed functions.
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700300 */
301 @Test
302 public void testRemoveSwitchAndPortDiscoveryEvent() {
303 // Mock the eventChannel functions first
304 eventChannel.removeEntry(anyObject(byte[].class));
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700305 expectLastCall().times(2, 2); // 1 Switch, 1 Port
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700306 replay(eventChannel);
307
308 setupTopologyManager();
309
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700310 PortNumber portNumber = PortNumber.uint32(1);
311
312 // Generate a Port Event
313 Collection<PortEvent> portEvents = new ArrayList<PortEvent>();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700314 portEvents.add(new PortEvent(DPID_1, portNumber));
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700315
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700316 // Call the topologyManager function for removing a Port
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700317 for (PortEvent portEvent : portEvents) {
318 theTopologyManager.removePortDiscoveryEvent(portEvent);
319 }
320
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700321 // Call the topologyManager function for removing a Switch
322 SwitchEvent switchEvent = new SwitchEvent(DPID_1);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700323 theTopologyManager.removeSwitchDiscoveryEvent(switchEvent);
324
325 // Verify the function calls
326 verify(eventChannel);
327
328 }
329
330 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700331 * Test the Link discovered function.
weibitf7c31a42014-06-23 16:51:01 -0700332 */
333 @Test
334 public void testPutLinkDiscoveryEvent() {
335 // Mock the eventChannel functions first
336 eventChannel.addEntry(anyObject(byte[].class),
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700337 anyObject(TopologyEvent.class));
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700338 expectLastCall().times(5, 5); // (2 Switch + 2 Port + 1 Link)
weibitf7c31a42014-06-23 16:51:01 -0700339 replay(eventChannel);
340
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700341 setupTopologyManager();
342
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700343 // Generate the Switch and Port Events
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700344 PortNumber portNumber1 = PortNumber.uint32(1);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700345 SwitchEvent switchEvent1 = new SwitchEvent(DPID_1);
weibitf7c31a42014-06-23 16:51:01 -0700346 Collection<PortEvent> portEvents1 = new ArrayList<PortEvent>();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700347 portEvents1.add(new PortEvent(DPID_1, portNumber1));
weibitf7c31a42014-06-23 16:51:01 -0700348
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700349 // Call the topologyManager function for adding a Switch
weibitf7c31a42014-06-23 16:51:01 -0700350 theTopologyManager.putSwitchDiscoveryEvent(switchEvent1, portEvents1);
351
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700352 // Generate the Switch and Port Events
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700353 PortNumber portNumber2 = PortNumber.uint32(2);
354 SwitchEvent switchEvent2 = new SwitchEvent(DPID_2);
weibitf7c31a42014-06-23 16:51:01 -0700355 Collection<PortEvent> portEvents2 = new ArrayList<PortEvent>();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700356 portEvents2.add(new PortEvent(DPID_2, portNumber2));
weibitf7c31a42014-06-23 16:51:01 -0700357
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700358 // Call the topologyManager function for adding a Switch
weibitf7c31a42014-06-23 16:51:01 -0700359 theTopologyManager.putSwitchDiscoveryEvent(switchEvent2, portEvents2);
360
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700361 // Create the Link Event
362 LinkEvent linkEvent =
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700363 new LinkEvent(new SwitchPort(DPID_1, portNumber1),
364 new SwitchPort(DPID_2, portNumber2));
weibitf7c31a42014-06-23 16:51:01 -0700365 theTopologyManager.putLinkDiscoveryEvent(linkEvent);
366
367 // Verify the function calls
368 verify(eventChannel);
369 }
370
371 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700372 * Test the Link removed function.
weibitf7c31a42014-06-23 16:51:01 -0700373 */
374 @Test
375 public void testRemoveLinkDiscoveryEvent() {
376 // Mock the eventChannel functions first
377 eventChannel.removeEntry(anyObject(byte[].class));
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700378 expectLastCall().times(1, 1); // (1 Link)
weibitf7c31a42014-06-23 16:51:01 -0700379 replay(eventChannel);
380
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700381 setupTopologyManager();
382
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700383 // Generate the Switch and Port Events
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700384 PortNumber portNumber1 = PortNumber.uint32(1);
385 SwitchEvent switchEvent1 = new SwitchEvent(DPID_1);
weibitf7c31a42014-06-23 16:51:01 -0700386 Collection<PortEvent> portEvents1 = new ArrayList<PortEvent>();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700387 portEvents1.add(new PortEvent(DPID_1, portNumber1));
weibitf7c31a42014-06-23 16:51:01 -0700388
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700389 // Call the topologyManager function for adding a Switch
weibitf7c31a42014-06-23 16:51:01 -0700390 theTopologyManager.putSwitchDiscoveryEvent(switchEvent1, portEvents1);
391
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700392 // Generate the Switch and Port Events
393 PortNumber portNumber2 = PortNumber.uint32(2);
394 SwitchEvent switchEvent2 = new SwitchEvent(DPID_2);
weibitf7c31a42014-06-23 16:51:01 -0700395 Collection<PortEvent> portEvents2 = new ArrayList<PortEvent>();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700396 portEvents2.add(new PortEvent(DPID_2, portNumber2));
weibitf7c31a42014-06-23 16:51:01 -0700397
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700398 // Call the topologyManager function for adding a Switch
weibitf7c31a42014-06-23 16:51:01 -0700399 theTopologyManager.putSwitchDiscoveryEvent(switchEvent2, portEvents2);
400
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700401 // Remove the Link
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700402 LinkEvent linkEventRemove =
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700403 new LinkEvent(new SwitchPort(DPID_1, portNumber1),
404 new SwitchPort(DPID_2, portNumber2));
weibitf7c31a42014-06-23 16:51:01 -0700405 theTopologyManager.removeLinkDiscoveryEvent(linkEventRemove);
406
407 // Verify the function calls
408 verify(eventChannel);
409 }
410
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700411 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700412 * Test the Host discovered function.
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700413 */
414 @Test
415 public void testPutHostDiscoveryEvent() {
416 // Mock the eventChannel functions first
417 eventChannel.addEntry(anyObject(byte[].class),
418 anyObject(TopologyEvent.class));
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700419 expectLastCall().times(1, 1); // 1 Host
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700420 replay(eventChannel);
421
422 setupTopologyManager();
423
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700424 // Generate a new Host Event
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700425 PortNumber portNumber = PortNumber.uint32(1);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700426 MACAddress hostMac = MACAddress.valueOf("00:AA:11:BB:33:CC");
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700427 SwitchPort sp = new SwitchPort(DPID_1, portNumber);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700428 List<SwitchPort> spLists = new ArrayList<SwitchPort>();
429 spLists.add(sp);
430 HostEvent hostEvent = new HostEvent(hostMac);
431 hostEvent.setAttachmentPoints(spLists);
432
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700433 // Call the topologyManager function for adding a Host
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700434 theTopologyManager.putHostDiscoveryEvent(hostEvent);
435
436 // Verify the function calls
437 verify(eventChannel);
438 }
439
440 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700441 * Test the Host removed function.
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700442 */
443 @Test
444 public void testRemoveHostDiscoveryEvent() {
445 // Mock the eventChannel functions first
446 eventChannel.removeEntry(anyObject(byte[].class));
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700447 expectLastCall().times(1, 1); // 1 Host
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700448 replay(eventChannel);
449
450 setupTopologyManager();
451
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700452 // Generate a new Host Event
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700453 PortNumber portNumber = PortNumber.uint32(1);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700454 MACAddress hostMac = MACAddress.valueOf("00:AA:11:BB:33:CC");
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700455 SwitchPort sp = new SwitchPort(DPID_1, portNumber);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700456 List<SwitchPort> spLists = new ArrayList<SwitchPort>();
457 spLists.add(sp);
458 HostEvent hostEvent = new HostEvent(hostMac);
459 hostEvent.setAttachmentPoints(spLists);
460
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700461 // Call the topologyManager function for removing a Host
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700462 theTopologyManager.removeHostDiscoveryEvent(hostEvent);
463
464 // Verify the function calls
465 verify(eventChannel);
466 }
467
468 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700469 * Tests adding of a Switch Mastership event and the topology replica
470 * transformation.
471 */
472 @Test
473 public void testAddMastershipEvent() {
474 setupTopologyManager();
475
476 // Prepare the event
477 Role role = Role.MASTER;
478 MastershipEvent mastershipEvent =
479 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
480 // Add the event
481 TestUtils.callMethod(theTopologyManager, "addMastershipEvent",
482 MastershipEvent.class, mastershipEvent);
483
484 //
485 // NOTE: The topology itself doesn't contain the Mastership Events,
486 // hence we don't check the topology.
487 //
488
489 // Check the events to be fired
490 List<MastershipEvent> apiAddedMastershipEvents
491 = TestUtils.getField(theTopologyManager,
492 "apiAddedMastershipEvents");
493 assertThat(apiAddedMastershipEvents, hasItem(mastershipEvent));
494 }
495
496 /**
497 * Tests removing of a Switch Mastership event and the topology replica
498 * transformation.
499 */
500 @Test
501 public void testRemoveMastershipEvent() {
502 setupTopologyManager();
503
504 // Prepare the event
505 Role role = Role.MASTER;
506 MastershipEvent mastershipEvent =
507 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
508 // Add the event
509 TestUtils.callMethod(theTopologyManager, "addMastershipEvent",
510 MastershipEvent.class, mastershipEvent);
511
512 // Check the events to be fired
513 List<MastershipEvent> apiAddedMastershipEvents
514 = TestUtils.getField(theTopologyManager,
515 "apiAddedMastershipEvents");
516 assertThat(apiAddedMastershipEvents, hasItem(mastershipEvent));
517
518 // Remove the event
519 TestUtils.callMethod(theTopologyManager, "removeMastershipEvent",
520 MastershipEvent.class,
521 new MastershipEvent(mastershipEvent));
522
523 // Check the events to be fired
524 List<MastershipEvent> apiRemovedMastershipEvents
525 = TestUtils.getField(theTopologyManager,
526 "apiRemovedMastershipEvents");
527 assertThat(apiRemovedMastershipEvents, hasItem(mastershipEvent));
528 }
529
530 /**
531 * Tests adding of a Switch and the topology replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700532 */
533 @Test
534 public void testAddSwitch() {
535 setupTopologyManager();
536
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700537 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700538 sw.createStringAttribute("foo", "bar");
539
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700540 TestUtils.callMethod(theTopologyManager, "addSwitch",
541 SwitchEvent.class, sw);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700542
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700543 // Check the topology structure
544 TopologyInternal topology =
545 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700546 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700547 assertEquals(sw, swInTopo);
548 assertTrue(swInTopo.isFrozen());
549 assertEquals("bar", swInTopo.getStringAttribute("foo"));
550
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700551 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700552 List<SwitchEvent> apiAddedSwitchEvents
553 = TestUtils.getField(theTopologyManager, "apiAddedSwitchEvents");
554 assertThat(apiAddedSwitchEvents, hasItem(sw));
555 }
556
557 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700558 * Tests adding of a Port and the topology replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700559 */
560 @Test
561 public void testAddPort() {
562 setupTopologyManager();
563
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700564 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700565 sw.createStringAttribute("foo", "bar");
566
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700567 final PortNumber portNumber = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700568 PortEvent port = new PortEvent(DPID_1, portNumber);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700569 port.createStringAttribute("fuzz", "buzz");
570
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700571 TestUtils.callMethod(theTopologyManager, "addSwitch",
572 SwitchEvent.class, sw);
573 TestUtils.callMethod(theTopologyManager, "addPort",
574 PortEvent.class, port);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700575
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700576 // Check the topology structure
577 TopologyInternal topology =
578 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700579 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700580 assertEquals(sw, swInTopo);
581 assertTrue(swInTopo.isFrozen());
582 assertEquals("bar", swInTopo.getStringAttribute("foo"));
583
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700584 final SwitchPort switchPort = new SwitchPort(DPID_1, portNumber);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700585 PortEvent portInTopo = topology.getPortEvent(switchPort);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700586 assertEquals(port, portInTopo);
587 assertTrue(portInTopo.isFrozen());
588 assertEquals("buzz", portInTopo.getStringAttribute("fuzz"));
589
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700590 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700591 List<PortEvent> apiAddedPortEvents
592 = TestUtils.getField(theTopologyManager, "apiAddedPortEvents");
593 assertThat(apiAddedPortEvents, hasItem(port));
594 }
595
596 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700597 * Tests removing of a Port followed by removing of a Switch,
598 * and the topology replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700599 */
600 @Test
601 public void testRemovePortThenSwitch() {
602 setupTopologyManager();
603
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700604 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700605 sw.createStringAttribute("foo", "bar");
606
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700607 final PortNumber portNumber = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700608 PortEvent port = new PortEvent(DPID_1, portNumber);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700609 port.createStringAttribute("fuzz", "buzz");
610
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700611 TestUtils.callMethod(theTopologyManager, "addSwitch",
612 SwitchEvent.class, sw);
613 TestUtils.callMethod(theTopologyManager, "addPort",
614 PortEvent.class, port);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700615
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700616 // Check the topology structure
617 TopologyInternal topology =
618 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700619 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700620 assertEquals(sw, swInTopo);
621 assertTrue(swInTopo.isFrozen());
622 assertEquals("bar", swInTopo.getStringAttribute("foo"));
623
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700624 final SwitchPort switchPort = new SwitchPort(DPID_1, portNumber);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700625 PortEvent portInTopo = topology.getPortEvent(switchPort);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700626 assertEquals(port, portInTopo);
627 assertTrue(portInTopo.isFrozen());
628 assertEquals("buzz", portInTopo.getStringAttribute("fuzz"));
629
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700630 // Remove in proper order
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700631 TestUtils.callMethod(theTopologyManager, "removePort",
632 PortEvent.class, new PortEvent(port));
633 TestUtils.callMethod(theTopologyManager, "removeSwitch",
634 SwitchEvent.class, new SwitchEvent(sw));
635
636
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700637 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700638 List<PortEvent> apiRemovedPortEvents
639 = TestUtils.getField(theTopologyManager, "apiRemovedPortEvents");
640 assertThat(apiRemovedPortEvents, hasItem(port));
641 List<SwitchEvent> apiRemovedSwitchEvents
642 = TestUtils.getField(theTopologyManager, "apiRemovedSwitchEvents");
643 assertThat(apiRemovedSwitchEvents, hasItem(sw));
644 }
645
646 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700647 * Tests removing of a Switch without removing of a Port,
648 * and the topology replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700649 */
650 @Test
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700651 public void testRemoveSwitchWithoutPortRemoval() {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700652 setupTopologyManager();
653
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700654 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700655 sw.createStringAttribute("foo", "bar");
656
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700657 final PortNumber portNumber = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700658 PortEvent port = new PortEvent(DPID_1, portNumber);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700659 port.createStringAttribute("fuzz", "buzz");
660
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700661 TestUtils.callMethod(theTopologyManager, "addSwitch",
662 SwitchEvent.class, sw);
663 TestUtils.callMethod(theTopologyManager, "addPort",
664 PortEvent.class, port);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700665
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700666 // Check the topology structure
667 TopologyInternal topology =
668 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700669 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700670 assertEquals(sw, swInTopo);
671 assertTrue(swInTopo.isFrozen());
672 assertEquals("bar", swInTopo.getStringAttribute("foo"));
673
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700674 final SwitchPort switchPort = new SwitchPort(DPID_1, portNumber);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700675 PortEvent portInTopo = topology.getPortEvent(switchPort);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700676 assertEquals(port, portInTopo);
677 assertTrue(portInTopo.isFrozen());
678 assertEquals("buzz", portInTopo.getStringAttribute("fuzz"));
679
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700680 // Remove in in-proper order
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700681// TestUtils.callMethod(theTopologyManager, "removePort",
682// PortEvent.class, new PortEvent(port));
683 TestUtils.callMethod(theTopologyManager, "removeSwitch",
684 SwitchEvent.class, new SwitchEvent(sw));
685
686
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700687 // Check the events to be fired
688 // The outcome should be the same as #testRemovePortThenSwitch
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700689 List<PortEvent> apiRemovedPortEvents
690 = TestUtils.getField(theTopologyManager, "apiRemovedPortEvents");
691 assertThat(apiRemovedPortEvents, hasItem(port));
692 List<SwitchEvent> apiRemovedSwitchEvents
693 = TestUtils.getField(theTopologyManager, "apiRemovedSwitchEvents");
694 assertThat(apiRemovedSwitchEvents, hasItem(sw));
695 }
696
697 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700698 * Tests adding of a Link and the topology replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700699 */
700 @Test
701 public void testAddLink() {
702 setupTopologyManager();
703
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700704 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700705 sw.createStringAttribute("foo", "bar");
706
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700707 final PortNumber portNumberA = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700708 PortEvent portA = new PortEvent(DPID_1, portNumberA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700709 portA.createStringAttribute("fuzz", "buzz");
710
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700711 final PortNumber portNumberB = PortNumber.uint32(3);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700712 PortEvent portB = new PortEvent(DPID_1, portNumberB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700713 portB.createStringAttribute("fizz", "buz");
714
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700715 LinkEvent linkA = new LinkEvent(portA.getSwitchPort(),
716 portB.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700717 linkA.createStringAttribute(TopologyElement.TYPE,
718 TopologyElement.TYPE_OPTICAL_LAYER);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700719 LinkEvent linkB = new LinkEvent(portB.getSwitchPort(),
720 portA.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700721 linkB.createStringAttribute(TopologyElement.TYPE,
722 TopologyElement.TYPE_OPTICAL_LAYER);
723
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700724 TestUtils.callMethod(theTopologyManager, "addSwitch",
725 SwitchEvent.class, sw);
726 TestUtils.callMethod(theTopologyManager, "addPort",
727 PortEvent.class, portA);
728 TestUtils.callMethod(theTopologyManager, "addPort",
729 PortEvent.class, portB);
730 TestUtils.callMethod(theTopologyManager, "addLink",
731 LinkEvent.class, linkA);
732 TestUtils.callMethod(theTopologyManager, "addLink",
733 LinkEvent.class, linkB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700734
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700735 // Check the topology structure
736 TopologyInternal topology =
737 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700738 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700739 assertEquals(sw, swInTopo);
740 assertTrue(swInTopo.isFrozen());
741 assertEquals("bar", swInTopo.getStringAttribute("foo"));
742
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700743 final SwitchPort switchPortA = new SwitchPort(DPID_1, portNumberA);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700744 PortEvent portAInTopo = topology.getPortEvent(switchPortA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700745 assertEquals(portA, portAInTopo);
746 assertTrue(portAInTopo.isFrozen());
747 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
748
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700749 final SwitchPort switchPortB = new SwitchPort(DPID_1, portNumberB);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700750 PortEvent portBInTopo = topology.getPortEvent(switchPortB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700751 assertEquals(portB, portBInTopo);
752 assertTrue(portBInTopo.isFrozen());
753 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
754
755 LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple());
756 assertEquals(linkA, linkAInTopo);
757 assertTrue(linkAInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700758 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
759 linkAInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700760
761 LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple());
762 assertEquals(linkB, linkBInTopo);
763 assertTrue(linkBInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700764 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
765 linkBInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700766
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700767 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700768 List<LinkEvent> apiAddedLinkEvents
769 = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents");
770 assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB));
771 }
772
773 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700774 * Tests removing of a Link without removing of a Host, and the topology
775 * replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700776 */
777 @Test
778 public void testAddLinkKickingOffHost() {
779 setupTopologyManager();
780
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700781 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700782 sw.createStringAttribute("foo", "bar");
783
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700784 final PortNumber portNumberA = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700785 PortEvent portA = new PortEvent(DPID_1, portNumberA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700786 portA.createStringAttribute("fuzz", "buzz");
787
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700788 final PortNumber portNumberB = PortNumber.uint32(3);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700789 PortEvent portB = new PortEvent(DPID_1, portNumberB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700790 portB.createStringAttribute("fizz", "buz");
791
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700792 final PortNumber portNumberC = PortNumber.uint32(4);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700793 PortEvent portC = new PortEvent(DPID_1, portNumberC);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700794 portC.createStringAttribute("fizz", "buz");
795
796 final MACAddress macA = MACAddress.valueOf(666L);
797 HostEvent hostA = new HostEvent(macA);
798 hostA.addAttachmentPoint(portA.getSwitchPort());
799 final long timestampA = 392893200L;
800 hostA.setLastSeenTime(timestampA);
801
802 final MACAddress macB = MACAddress.valueOf(999L);
803 HostEvent hostB = new HostEvent(macB);
804 hostB.addAttachmentPoint(portB.getSwitchPort());
805 hostB.addAttachmentPoint(portC.getSwitchPort());
806 final long timestampB = 392893201L;
807 hostB.setLastSeenTime(timestampB);
808
809
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700810 LinkEvent linkA = new LinkEvent(portA.getSwitchPort(),
811 portB.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700812 linkA.createStringAttribute(TopologyElement.TYPE,
813 TopologyElement.TYPE_OPTICAL_LAYER);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700814 LinkEvent linkB = new LinkEvent(portB.getSwitchPort(),
815 portA.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700816 linkB.createStringAttribute(TopologyElement.TYPE,
817 TopologyElement.TYPE_OPTICAL_LAYER);
818
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700819 TestUtils.callMethod(theTopologyManager, "addSwitch",
820 SwitchEvent.class, sw);
821 TestUtils.callMethod(theTopologyManager, "addPort",
822 PortEvent.class, portA);
823 TestUtils.callMethod(theTopologyManager, "addPort",
824 PortEvent.class, portB);
825 TestUtils.callMethod(theTopologyManager, "addPort",
826 PortEvent.class, portC);
827 TestUtils.callMethod(theTopologyManager, "addHost",
828 HostEvent.class, hostA);
829 TestUtils.callMethod(theTopologyManager, "addHost",
830 HostEvent.class, hostB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700831
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700832 TestUtils.callMethod(theTopologyManager, "addLink",
833 LinkEvent.class, linkA);
834 TestUtils.callMethod(theTopologyManager, "addLink",
835 LinkEvent.class, linkB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700836
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700837 // Check the topology structure
838 TopologyInternal topology =
839 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700840 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700841 assertEquals(sw, swInTopo);
842 assertTrue(swInTopo.isFrozen());
843 assertEquals("bar", swInTopo.getStringAttribute("foo"));
844
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700845 final SwitchPort switchPortA = new SwitchPort(DPID_1, portNumberA);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700846 PortEvent portAInTopo = topology.getPortEvent(switchPortA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700847 assertEquals(portA, portAInTopo);
848 assertTrue(portAInTopo.isFrozen());
849 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
850
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700851 final SwitchPort switchPortB = new SwitchPort(DPID_1, portNumberB);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700852 PortEvent portBInTopo = topology.getPortEvent(switchPortB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700853 assertEquals(portB, portBInTopo);
854 assertTrue(portBInTopo.isFrozen());
855 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
856
857 // hostA expected to be removed
858 assertNull(topology.getHostEvent(macA));
859 // hostB expected to be there with reduced attachment point
860 HostEvent hostBrev = new HostEvent(macB);
861 hostBrev.addAttachmentPoint(portC.getSwitchPort());
862 hostBrev.setLastSeenTime(timestampB);
863 hostBrev.freeze();
864 assertEquals(hostBrev, topology.getHostEvent(macB));
865
866
867 LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple());
868 assertEquals(linkA, linkAInTopo);
869 assertTrue(linkAInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700870 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
871 linkAInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700872
873 LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple());
874 assertEquals(linkB, linkBInTopo);
875 assertTrue(linkBInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700876 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
877 linkBInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700878
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700879 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700880 List<HostEvent> apiAddedHostEvents
881 = TestUtils.getField(theTopologyManager, "apiAddedHostEvents");
882 assertThat(apiAddedHostEvents, hasItem(hostBrev));
883
884 List<HostEvent> apiRemovedHostEvents
885 = TestUtils.getField(theTopologyManager, "apiRemovedHostEvents");
886 assertThat(apiRemovedHostEvents, hasItem(hostA));
887 List<LinkEvent> apiAddedLinkEvents
888 = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents");
889 assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB));
890 }
891
892 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700893 * Tests removing of a Link and the topology replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700894 */
895 @Test
896 public void testRemoveLink() {
897 setupTopologyManager();
898
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700899 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700900 sw.createStringAttribute("foo", "bar");
901
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700902 final PortNumber portNumberA = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700903 PortEvent portA = new PortEvent(DPID_1, portNumberA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700904 portA.createStringAttribute("fuzz", "buzz");
905
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700906 final PortNumber portNumberB = PortNumber.uint32(3);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700907 PortEvent portB = new PortEvent(DPID_1, portNumberB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700908 portB.createStringAttribute("fizz", "buz");
909
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700910 LinkEvent linkA = new LinkEvent(portA.getSwitchPort(),
911 portB.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700912 linkA.createStringAttribute(TopologyElement.TYPE,
913 TopologyElement.TYPE_OPTICAL_LAYER);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700914 LinkEvent linkB = new LinkEvent(portB.getSwitchPort(),
915 portA.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700916 linkB.createStringAttribute(TopologyElement.TYPE,
917 TopologyElement.TYPE_OPTICAL_LAYER);
918
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700919 TestUtils.callMethod(theTopologyManager, "addSwitch",
920 SwitchEvent.class, sw);
921 TestUtils.callMethod(theTopologyManager, "addPort",
922 PortEvent.class, portA);
923 TestUtils.callMethod(theTopologyManager, "addPort",
924 PortEvent.class, portB);
925 TestUtils.callMethod(theTopologyManager, "addLink",
926 LinkEvent.class, linkA);
927 TestUtils.callMethod(theTopologyManager, "addLink",
928 LinkEvent.class, linkB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700929
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700930 // Check the topology structure
931 TopologyInternal topology =
932 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700933 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700934 assertEquals(sw, swInTopo);
935 assertTrue(swInTopo.isFrozen());
936 assertEquals("bar", swInTopo.getStringAttribute("foo"));
937
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700938 final SwitchPort switchPortA = new SwitchPort(DPID_1, portNumberA);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700939 PortEvent portAInTopo = topology.getPortEvent(switchPortA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700940 assertEquals(portA, portAInTopo);
941 assertTrue(portAInTopo.isFrozen());
942 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
943
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700944 final SwitchPort switchPortB = new SwitchPort(DPID_1, portNumberB);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700945 PortEvent portBInTopo = topology.getPortEvent(switchPortB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700946 assertEquals(portB, portBInTopo);
947 assertTrue(portBInTopo.isFrozen());
948 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
949
950 LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple());
951 assertEquals(linkA, linkAInTopo);
952 assertTrue(linkAInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700953 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
954 linkAInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700955
956
957 LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple());
958 assertEquals(linkB, linkBInTopo);
959 assertTrue(linkBInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700960 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
961 linkBInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700962
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700963 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700964 // FIXME if link flapped (linkA in this scenario),
965 // linkA appears in both removed and added is this expected behavior?
966 List<LinkEvent> apiAddedLinkEvents
967 = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents");
968 assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB));
969
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700970 // Clear the events before removing the link
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700971 apiAddedLinkEvents.clear();
972
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700973 // Remove the link
974 TestUtils.callMethod(theTopologyManager, "removeLink",
975 LinkEvent.class, new LinkEvent(linkA));
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700976
977 LinkEvent linkANotInTopo = topology.getLinkEvent(linkA.getLinkTuple());
978 assertNull(linkANotInTopo);
979
980 List<LinkEvent> apiRemovedLinkEvents
981 = TestUtils.getField(theTopologyManager, "apiRemovedLinkEvents");
982 assertThat(apiRemovedLinkEvents, hasItem(linkA));
983 }
984
985 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700986 * Tests adding of a Host without adding of a Link, and the topology
987 * replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700988 */
989 @Test
990 public void testAddHostIgnoredByLink() {
991 setupTopologyManager();
992
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700993 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700994 sw.createStringAttribute("foo", "bar");
995
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700996 final PortNumber portNumberA = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700997 PortEvent portA = new PortEvent(DPID_1, portNumberA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700998 portA.createStringAttribute("fuzz", "buzz");
999
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001000 final PortNumber portNumberB = PortNumber.uint32(3);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001001 PortEvent portB = new PortEvent(DPID_1, portNumberB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001002 portB.createStringAttribute("fizz", "buz");
1003
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001004 final PortNumber portNumberC = PortNumber.uint32(4);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001005 PortEvent portC = new PortEvent(DPID_1, portNumberC);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001006 portC.createStringAttribute("fizz", "buz");
1007
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001008 LinkEvent linkA = new LinkEvent(portA.getSwitchPort(),
1009 portB.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001010 linkA.createStringAttribute(TopologyElement.TYPE,
1011 TopologyElement.TYPE_OPTICAL_LAYER);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001012 LinkEvent linkB = new LinkEvent(portB.getSwitchPort(),
1013 portA.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001014 linkB.createStringAttribute(TopologyElement.TYPE,
1015 TopologyElement.TYPE_OPTICAL_LAYER);
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);
1025 TestUtils.callMethod(theTopologyManager, "addLink",
1026 LinkEvent.class, linkA);
1027 TestUtils.callMethod(theTopologyManager, "addLink",
1028 LinkEvent.class, linkB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001029
1030 // Add hostA attached to a port which already has a link
1031 final MACAddress macA = MACAddress.valueOf(666L);
1032 HostEvent hostA = new HostEvent(macA);
1033 hostA.addAttachmentPoint(portA.getSwitchPort());
1034 final long timestampA = 392893200L;
1035 hostA.setLastSeenTime(timestampA);
1036
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001037 TestUtils.callMethod(theTopologyManager, "addHost",
1038 HostEvent.class, hostA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001039
1040 // Add hostB attached to multiple ports,
1041 // some of them which already has a link
1042 final MACAddress macB = MACAddress.valueOf(999L);
1043 HostEvent hostB = new HostEvent(macB);
1044 hostB.addAttachmentPoint(portB.getSwitchPort());
1045 hostB.addAttachmentPoint(portC.getSwitchPort());
1046 final long timestampB = 392893201L;
1047 hostB.setLastSeenTime(timestampB);
1048
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001049 TestUtils.callMethod(theTopologyManager, "addHost",
1050 HostEvent.class, hostB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001051
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001052 // Check the topology structure
1053 TopologyInternal topology =
1054 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001055 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001056 assertEquals(sw, swInTopo);
1057 assertTrue(swInTopo.isFrozen());
1058 assertEquals("bar", swInTopo.getStringAttribute("foo"));
1059
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001060 final SwitchPort switchPortA = new SwitchPort(DPID_1, portNumberA);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001061 PortEvent portAInTopo = topology.getPortEvent(switchPortA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001062 assertEquals(portA, portAInTopo);
1063 assertTrue(portAInTopo.isFrozen());
1064 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
1065
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001066 final SwitchPort switchPortB = new SwitchPort(DPID_1, portNumberB);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001067 PortEvent portBInTopo = topology.getPortEvent(switchPortB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001068 assertEquals(portB, portBInTopo);
1069 assertTrue(portBInTopo.isFrozen());
1070 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
1071
1072 // hostA expected to be completely ignored
1073 assertNull(topology.getHostEvent(macA));
1074 // hostB expected to be there with reduced attachment point
1075 HostEvent hostBrev = new HostEvent(macB);
1076 hostBrev.addAttachmentPoint(portC.getSwitchPort());
1077 hostBrev.setLastSeenTime(timestampB);
1078 hostBrev.freeze();
1079 assertEquals(hostBrev, topology.getHostEvent(macB));
1080
1081
1082 LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple());
1083 assertEquals(linkA, linkAInTopo);
1084 assertTrue(linkAInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001085 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
1086 linkAInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001087
1088 LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple());
1089 assertEquals(linkB, linkBInTopo);
1090 assertTrue(linkBInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001091 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
1092 linkBInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001093
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001094 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001095 // hostB should be added with reduced attachment points
1096 List<HostEvent> apiAddedHostEvents
1097 = TestUtils.getField(theTopologyManager, "apiAddedHostEvents");
1098 assertThat(apiAddedHostEvents, hasItem(hostBrev));
1099
1100 // hostA should not be ignored
1101 List<HostEvent> apiRemovedHostEvents
1102 = TestUtils.getField(theTopologyManager, "apiRemovedHostEvents");
1103 assertThat(apiRemovedHostEvents, not(hasItem(hostA)));
1104
1105 List<LinkEvent> apiAddedLinkEvents
1106 = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents");
1107 assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB));
1108 }
1109
1110 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001111 * Tests adding and moving of a Host, and the topology replica
1112 * transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001113 */
1114 @Test
1115 public void testAddHostMove() {
1116 setupTopologyManager();
1117
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001118 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001119 sw.createStringAttribute("foo", "bar");
1120
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001121 final PortNumber portNumberA = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001122 PortEvent portA = new PortEvent(DPID_1, portNumberA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001123 portA.createStringAttribute("fuzz", "buzz");
1124
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001125 final PortNumber portNumberB = PortNumber.uint32(3);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001126 PortEvent portB = new PortEvent(DPID_1, portNumberB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001127 portB.createStringAttribute("fizz", "buz");
1128
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001129 final PortNumber portNumberC = PortNumber.uint32(4);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001130 PortEvent portC = new PortEvent(DPID_1, portNumberC);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001131 portC.createStringAttribute("fizz", "buz");
1132
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001133 TestUtils.callMethod(theTopologyManager, "addSwitch",
1134 SwitchEvent.class, sw);
1135 TestUtils.callMethod(theTopologyManager, "addPort",
1136 PortEvent.class, portA);
1137 TestUtils.callMethod(theTopologyManager, "addPort",
1138 PortEvent.class, portB);
1139 TestUtils.callMethod(theTopologyManager, "addPort",
1140 PortEvent.class, portC);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001141
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001142 // Add hostA attached to a Port which already has a Link
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001143 final MACAddress macA = MACAddress.valueOf(666L);
1144 HostEvent hostA = new HostEvent(macA);
1145 hostA.addAttachmentPoint(portA.getSwitchPort());
1146 final long timestampA = 392893200L;
1147 hostA.setLastSeenTime(timestampA);
1148
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001149 TestUtils.callMethod(theTopologyManager, "addHost",
1150 HostEvent.class, hostA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001151
1152
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001153 // Check the topology structure
1154 TopologyInternal topology =
1155 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001156 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001157 assertEquals(sw, swInTopo);
1158 assertTrue(swInTopo.isFrozen());
1159 assertEquals("bar", swInTopo.getStringAttribute("foo"));
1160
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001161 final SwitchPort switchPortA = new SwitchPort(DPID_1, portNumberA);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001162 PortEvent portAInTopo = topology.getPortEvent(switchPortA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001163 assertEquals(portA, portAInTopo);
1164 assertTrue(portAInTopo.isFrozen());
1165 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
1166
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001167 final SwitchPort switchPortB = new SwitchPort(DPID_1, portNumberB);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001168 PortEvent portBInTopo = topology.getPortEvent(switchPortB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001169 assertEquals(portB, portBInTopo);
1170 assertTrue(portBInTopo.isFrozen());
1171 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
1172
1173 // hostA expected to be there
1174 assertEquals(hostA, topology.getHostEvent(macA));
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001175 assertEquals(timestampA,
1176 topology.getHostEvent(macA).getLastSeenTime());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001177
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001178 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001179 // hostA should be added
1180 List<HostEvent> apiAddedHostEvents
1181 = TestUtils.getField(theTopologyManager, "apiAddedHostEvents");
1182 assertThat(apiAddedHostEvents, hasItem(hostA));
1183
1184
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001185 // Clear the events before moving the Host
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001186 apiAddedHostEvents.clear();
1187
1188 HostEvent hostAmoved = new HostEvent(macA);
1189 hostAmoved.addAttachmentPoint(portB.getSwitchPort());
1190 final long timestampAmoved = 392893201L;
1191 hostAmoved.setLastSeenTime(timestampAmoved);
1192
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001193 TestUtils.callMethod(theTopologyManager, "addHost",
1194 HostEvent.class, hostAmoved);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001195
1196 assertEquals(hostAmoved, topology.getHostEvent(macA));
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001197 assertEquals(timestampAmoved,
1198 topology.getHostEvent(macA).getLastSeenTime());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001199
1200 // hostA expected to be there with new attachment point
1201 apiAddedHostEvents
1202 = TestUtils.getField(theTopologyManager, "apiAddedHostEvents");
1203 assertThat(apiAddedHostEvents, hasItem(hostAmoved));
1204
1205 // hostA is updated not removed
1206 List<HostEvent> apiRemovedHostEvents
1207 = TestUtils.getField(theTopologyManager, "apiRemovedHostEvents");
1208 assertThat(apiRemovedHostEvents, not(hasItem(hostA)));
1209 }
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001210
1211 /**
1212 * Tests processing of a Switch Mastership Event and the delivery of the
1213 * topology events.
1214 */
1215 @Test
1216 public void testProcessMastershipEvent() {
1217 List<EventEntry<TopologyEvent>> events = new LinkedList<>();
1218 EventEntry<TopologyEvent> eventEntry;
1219 TopologyEvent topologyEvent;
1220
1221 setupTopologyManagerWithEventHandler();
1222
1223 // Prepare the Mastership Event
1224 Role role = Role.MASTER;
1225 MastershipEvent mastershipEvent =
1226 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
1227 topologyEvent = new TopologyEvent(mastershipEvent, ONOS_INSTANCE_ID_1);
1228
1229 // Add the Mastership Event
1230 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1231 topologyEvent);
1232 events.add(eventEntry);
1233
1234 // Process the events
1235 TestUtils.callMethod(theEventHandler, "processEvents",
1236 List.class, events);
1237
1238 // Check the fired events
1239 TopologyEvents topologyEvents = theTopologyListener.topologyEvents;
1240 assertNotNull(topologyEvents);
1241 assertThat(topologyEvents.getAddedMastershipEvents(),
1242 hasItem(mastershipEvent));
1243 theTopologyListener.clear();
1244 }
1245
1246 /**
1247 * Tests processing of a Switch Event, and the delivery of the topology
1248 * events.
1249 *
1250 * We test the following scenario:
1251 * - Switch Mastership Event is processed along with a Switch Event - both
1252 * events should be delivered.
1253 */
1254 @Test
1255 public void testProcessSwitchEvent() {
1256 TopologyEvents topologyEvents;
1257 List<EventEntry<TopologyEvent>> events = new LinkedList<>();
1258 EventEntry<TopologyEvent> eventEntry;
1259 TopologyEvent topologyMastershipEvent;
1260 TopologyEvent topologySwitchEvent;
1261
1262 setupTopologyManagerWithEventHandler();
1263
1264 // Prepare the Mastership Event
1265 Role role = Role.MASTER;
1266 MastershipEvent mastershipEvent =
1267 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
1268 topologyMastershipEvent = new TopologyEvent(mastershipEvent,
1269 ONOS_INSTANCE_ID_1);
1270
1271 // Prepare the Switch Event
1272 SwitchEvent switchEvent = new SwitchEvent(DPID_1);
1273 topologySwitchEvent = new TopologyEvent(switchEvent,
1274 ONOS_INSTANCE_ID_1);
1275
1276 // Add the Mastership Event
1277 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1278 topologyMastershipEvent);
1279 events.add(eventEntry);
1280
1281 // Add the Switch Event
1282 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1283 topologySwitchEvent);
1284 events.add(eventEntry);
1285
1286 // Process the events
1287 TestUtils.callMethod(theEventHandler, "processEvents",
1288 List.class, events);
1289
1290 // Check the fired events
1291 topologyEvents = theTopologyListener.topologyEvents;
1292 assertNotNull(topologyEvents);
1293 assertThat(topologyEvents.getAddedMastershipEvents(),
1294 hasItem(mastershipEvent));
1295 assertThat(topologyEvents.getAddedSwitchEvents(),
1296 hasItem(switchEvent));
1297 theTopologyListener.clear();
1298 }
1299
1300 /**
1301 * Tests processing of a misordered Switch Event, and the delivery of the
1302 * topology events.
1303 *
1304 * We test the following scenario:
1305 * - Only a Switch Event is processed first, later followed by a Switch
1306 * Mastership Event - the Switch Event should be delivered after the
1307 * Switch Mastership Event is processed.
1308 */
1309 @Test
1310 public void testProcessMisorderedSwitchEvent() {
1311 TopologyEvents topologyEvents;
1312 List<EventEntry<TopologyEvent>> events = new LinkedList<>();
1313 EventEntry<TopologyEvent> eventEntry;
1314 TopologyEvent topologyMastershipEvent;
1315 TopologyEvent topologySwitchEvent;
1316
1317 setupTopologyManagerWithEventHandler();
1318
1319 // Prepare the Mastership Event
1320 Role role = Role.MASTER;
1321 MastershipEvent mastershipEvent =
1322 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
1323 topologyMastershipEvent = new TopologyEvent(mastershipEvent,
1324 ONOS_INSTANCE_ID_1);
1325
1326 // Prepare the Switch Event
1327 SwitchEvent switchEvent = new SwitchEvent(DPID_1);
1328 topologySwitchEvent = new TopologyEvent(switchEvent,
1329 ONOS_INSTANCE_ID_1);
1330
1331 // Add the Switch Event
1332 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1333 topologySwitchEvent);
1334 events.add(eventEntry);
1335
1336 // Process the events
1337 TestUtils.callMethod(theEventHandler, "processEvents",
1338 List.class, events);
1339
1340 // Check the fired events: no events should be fired
1341 topologyEvents = theTopologyListener.topologyEvents;
1342 assertNull(topologyEvents);
1343 theTopologyListener.clear();
1344 events.clear();
1345
1346 // Add the Mastership Event
1347 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1348 topologyMastershipEvent);
1349 events.add(eventEntry);
1350
1351 // Process the events
1352 TestUtils.callMethod(theEventHandler, "processEvents",
1353 List.class, events);
1354
1355 // Check the fired events: both events should be fired
1356 topologyEvents = theTopologyListener.topologyEvents;
1357 assertNotNull(topologyEvents);
1358 assertThat(topologyEvents.getAddedMastershipEvents(),
1359 hasItem(mastershipEvent));
1360 assertThat(topologyEvents.getAddedSwitchEvents(),
1361 hasItem(switchEvent));
1362 theTopologyListener.clear();
1363 }
1364
1365 /**
1366 * Tests processing of a Switch Event with Mastership Event from
1367 * another ONOS instance, and the delivery of the topology events.
1368 *
1369 * We test the following scenario:
1370 * - Only a Switch Event is processed first, later followed by a Switch
1371 * Mastership Event from another ONOS instance - only the Switch
1372 * Mastership Event should be delivered.
1373 */
1374 @Test
1375 public void testProcessSwitchEventNoMastership() {
1376 TopologyEvents topologyEvents;
1377 List<EventEntry<TopologyEvent>> events = new LinkedList<>();
1378 EventEntry<TopologyEvent> eventEntry;
1379 TopologyEvent topologyMastershipEvent;
1380 TopologyEvent topologySwitchEvent;
1381
1382 setupTopologyManagerWithEventHandler();
1383
1384 // Prepare the Mastership Event
1385 Role role = Role.MASTER;
1386 MastershipEvent mastershipEvent =
1387 new MastershipEvent(DPID_2, ONOS_INSTANCE_ID_2, role);
1388 topologyMastershipEvent = new TopologyEvent(mastershipEvent,
1389 ONOS_INSTANCE_ID_2);
1390
1391 // Prepare the Switch Event
1392 // NOTE: The originator (ONOS_INSTANCE_ID_1) is NOT the Master
1393 SwitchEvent switchEvent = new SwitchEvent(DPID_2);
1394 topologySwitchEvent = new TopologyEvent(switchEvent,
1395 ONOS_INSTANCE_ID_1);
1396
1397 // Add the Switch Event
1398 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1399 topologySwitchEvent);
1400 events.add(eventEntry);
1401
1402 // Process the events
1403 TestUtils.callMethod(theEventHandler, "processEvents",
1404 List.class, events);
1405
1406 // Check the fired events: no events should be fired
1407 topologyEvents = theTopologyListener.topologyEvents;
1408 assertNull(topologyEvents);
1409 theTopologyListener.clear();
1410 events.clear();
1411
1412 // Add the Mastership Event
1413 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1414 topologyMastershipEvent);
1415 events.add(eventEntry);
1416
1417 // Process the events
1418 TestUtils.callMethod(theEventHandler, "processEvents",
1419 List.class, events);
1420
1421 // Check the fired events: only the Mastership event should be fired
1422 topologyEvents = theTopologyListener.topologyEvents;
1423 assertNotNull(topologyEvents);
1424 assertThat(topologyEvents.getAddedMastershipEvents(),
1425 hasItem(mastershipEvent));
1426 assertThat(topologyEvents.getAddedSwitchEvents(), is(empty()));
1427 theTopologyListener.clear();
1428 }
1429
1430 /**
1431 * Tests processing of Switch Events with Mastership switchover between
1432 * two ONOS instance, and the delivery of the topology events.
1433 *
1434 * We test the following scenario:
1435 * - Initially, a Mastership Event and a Switch Event from one ONOS
1436 * instance are processed - both events should be delivered.
1437 * - Later, a Mastership Event and a Switch event from another ONOS
1438 * instances are processed - both events should be delivered.
1439 * - Finally, a REMOVE Switch Event is received from the first ONOS
1440 * instance - no event should be delivered.
Pavlin Radoslavov054cd592014-08-07 20:57:16 -07001441 *
1442 * @throws RegistryException
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001443 */
1444 @Test
Pavlin Radoslavov054cd592014-08-07 20:57:16 -07001445 public void testProcessSwitchMastershipSwitchover()
1446 throws RegistryException {
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001447 TopologyEvents topologyEvents;
1448 List<EventEntry<TopologyEvent>> events = new LinkedList<>();
1449 EventEntry<TopologyEvent> eventEntry;
1450 TopologyEvent topologyMastershipEvent;
1451 TopologyEvent topologySwitchEvent;
1452
1453 setupTopologyManagerWithEventHandler();
1454
1455 // Prepare the Mastership Event from the first ONOS instance
1456 Role role = Role.MASTER;
1457 MastershipEvent mastershipEvent =
1458 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
1459 topologyMastershipEvent = new TopologyEvent(mastershipEvent,
1460 ONOS_INSTANCE_ID_1);
1461
1462 // Prepare the Switch Event from the first ONOS instance
1463 SwitchEvent switchEvent = new SwitchEvent(DPID_1);
1464 topologySwitchEvent = new TopologyEvent(switchEvent,
1465 ONOS_INSTANCE_ID_1);
1466
1467 // Add the Mastership Event
1468 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1469 topologyMastershipEvent);
1470 events.add(eventEntry);
1471
1472 // Add the Switch Event
1473 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1474 topologySwitchEvent);
1475 events.add(eventEntry);
1476
1477 // Process the events
1478 TestUtils.callMethod(theEventHandler, "processEvents",
1479 List.class, events);
1480
1481 // Check the fired events: both events should be fired
1482 topologyEvents = theTopologyListener.topologyEvents;
1483 assertNotNull(topologyEvents);
1484 assertThat(topologyEvents.getAddedMastershipEvents(),
1485 hasItem(mastershipEvent));
1486 assertThat(topologyEvents.getAddedSwitchEvents(),
1487 hasItem(switchEvent));
1488 theTopologyListener.clear();
1489 events.clear();
1490
1491 //
1492 // Update the Registry Service, so the second ONOS instance is the
1493 // Master.
1494 //
1495 reset(registryService);
Pavlin Radoslavov054cd592014-08-07 20:57:16 -07001496 expect(registryService.getControllerForSwitch(DPID_1.value()))
1497 .andReturn(ONOS_INSTANCE_ID_2.toString()).anyTimes();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001498 replay(registryService);
1499
1500 // Prepare the Mastership Event from the second ONOS instance
1501 role = Role.MASTER;
1502 mastershipEvent = new MastershipEvent(DPID_1,
1503 ONOS_INSTANCE_ID_2, role);
1504 topologyMastershipEvent = new TopologyEvent(mastershipEvent,
1505 ONOS_INSTANCE_ID_2);
1506
1507 // Prepare the Switch Event from second ONOS instance
1508 switchEvent = new SwitchEvent(DPID_1);
1509 topologySwitchEvent = new TopologyEvent(switchEvent,
1510 ONOS_INSTANCE_ID_2);
1511
1512 // Add the Mastership Event
1513 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1514 topologyMastershipEvent);
1515 events.add(eventEntry);
1516
1517 // Add the Switch Event
1518 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1519 topologySwitchEvent);
1520 events.add(eventEntry);
1521
1522 // Process the events
1523 TestUtils.callMethod(theEventHandler, "processEvents",
1524 List.class, events);
1525
1526 // Check the fired events: both events should be fired
1527 topologyEvents = theTopologyListener.topologyEvents;
1528 assertNotNull(topologyEvents);
1529 assertThat(topologyEvents.getAddedMastershipEvents(),
1530 hasItem(mastershipEvent));
1531 assertThat(topologyEvents.getAddedSwitchEvents(),
1532 hasItem(switchEvent));
1533 theTopologyListener.clear();
1534 events.clear();
1535
1536 // Prepare the REMOVE Switch Event from first ONOS instance
1537 switchEvent = new SwitchEvent(DPID_1);
1538 topologySwitchEvent = new TopologyEvent(switchEvent,
1539 ONOS_INSTANCE_ID_1);
1540 // Add the Switch Event
1541 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_REMOVE,
1542 topologySwitchEvent);
1543 events.add(eventEntry);
1544
1545 // Process the events
1546 TestUtils.callMethod(theEventHandler, "processEvents",
1547 List.class, events);
1548
1549 // Check the fired events: no events should be fired
1550 topologyEvents = theTopologyListener.topologyEvents;
1551 assertNull(topologyEvents);
1552 theTopologyListener.clear();
1553 events.clear();
1554 }
Pavlin Radoslavovae7e8482014-08-11 16:58:19 -07001555
1556 /**
1557 * Tests processing of Configured Switch Events with Mastership switchover
1558 * between two ONOS instance, and the delivery of the topology events.
1559 * <p/>
1560 * NOTE: This test is similar to testProcessSwitchMastershipSwitchover()
1561 * except that the topology and all events are considered as statically
1562 * configured.
1563 * <p/>
1564 * We test the following scenario:
1565 * - Initially, a Mastership Event and a Switch Event from one ONOS
1566 * instance are processed - both events should be delivered.
1567 * - Later, a Mastership Event and a Switch event from another ONOS
1568 * instances are processed - both events should be delivered.
1569 */
1570 @Test
1571 public void testProcessConfiguredSwitchMastershipSwitchover() {
1572 TopologyEvents topologyEvents;
1573 List<EventEntry<TopologyEvent>> events = new LinkedList<>();
1574 EventEntry<TopologyEvent> eventEntry;
1575 TopologyEvent topologyMastershipEvent;
1576 TopologyEvent topologySwitchEvent;
1577
1578 setupTopologyManagerWithEventHandler();
1579
1580 // Reset the Registry Service so it is not used
1581 reset(registryService);
1582
1583 // Prepare the Mastership Event from the first ONOS instance
1584 Role role = Role.MASTER;
1585 MastershipEvent mastershipEvent =
1586 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
1587 mastershipEvent.createStringAttribute(
1588 TopologyElement.ELEMENT_CONFIG_STATE,
1589 ConfigState.CONFIGURED.toString());
1590 topologyMastershipEvent = new TopologyEvent(mastershipEvent,
1591 ONOS_INSTANCE_ID_1);
1592
1593 // Prepare the Switch Event from the first ONOS instance
1594 SwitchEvent switchEvent = new SwitchEvent(DPID_1);
1595 switchEvent.createStringAttribute(
1596 TopologyElement.ELEMENT_CONFIG_STATE,
1597 ConfigState.CONFIGURED.toString());
1598 topologySwitchEvent = new TopologyEvent(switchEvent,
1599 ONOS_INSTANCE_ID_1);
1600
1601 // Add the Mastership Event
1602 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1603 topologyMastershipEvent);
1604 events.add(eventEntry);
1605
1606 // Add the Switch Event
1607 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1608 topologySwitchEvent);
1609 events.add(eventEntry);
1610
1611 // Process the events
1612 TestUtils.callMethod(theEventHandler, "processEvents",
1613 List.class, events);
1614
1615 // Check the fired events: both events should be fired
1616 topologyEvents = theTopologyListener.topologyEvents;
1617 assertNotNull(topologyEvents);
1618 assertThat(topologyEvents.getAddedMastershipEvents(),
1619 hasItem(mastershipEvent));
1620 assertThat(topologyEvents.getAddedSwitchEvents(),
1621 hasItem(switchEvent));
1622 theTopologyListener.clear();
1623 events.clear();
1624
1625 // Prepare the Mastership Event from the second ONOS instance
1626 role = Role.MASTER;
1627 mastershipEvent = new MastershipEvent(DPID_1,
1628 ONOS_INSTANCE_ID_2, role);
1629 mastershipEvent.createStringAttribute(
1630 TopologyElement.ELEMENT_CONFIG_STATE,
1631 ConfigState.CONFIGURED.toString());
1632 topologyMastershipEvent = new TopologyEvent(mastershipEvent,
1633 ONOS_INSTANCE_ID_2);
1634
1635 // Prepare the Switch Event from second ONOS instance
1636 switchEvent = new SwitchEvent(DPID_1);
1637 switchEvent.createStringAttribute(
1638 TopologyElement.ELEMENT_CONFIG_STATE,
1639 ConfigState.CONFIGURED.toString());
1640 topologySwitchEvent = new TopologyEvent(switchEvent,
1641 ONOS_INSTANCE_ID_2);
1642
1643 // Add the Mastership Event
1644 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1645 topologyMastershipEvent);
1646 events.add(eventEntry);
1647
1648 // Add the Switch Event
1649 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1650 topologySwitchEvent);
1651 events.add(eventEntry);
1652
1653 // Process the events
1654 TestUtils.callMethod(theEventHandler, "processEvents",
1655 List.class, events);
1656
1657 // Check the fired events: both events should be fired
1658 topologyEvents = theTopologyListener.topologyEvents;
1659 assertNotNull(topologyEvents);
1660 assertThat(topologyEvents.getAddedMastershipEvents(),
1661 hasItem(mastershipEvent));
1662 assertThat(topologyEvents.getAddedSwitchEvents(),
1663 hasItem(switchEvent));
1664 theTopologyListener.clear();
1665 events.clear();
1666
1667 // Prepare the REMOVE Switch Event from first ONOS instance
1668 //
1669 // NOTE: This event only is explicitly marked as NOT_CONFIGURED,
1670 // otherwise it will override the previous configuration events.
1671 //
1672 switchEvent = new SwitchEvent(DPID_1);
1673 switchEvent.createStringAttribute(
1674 TopologyElement.ELEMENT_CONFIG_STATE,
1675 ConfigState.NOT_CONFIGURED.toString());
1676 topologySwitchEvent = new TopologyEvent(switchEvent,
1677 ONOS_INSTANCE_ID_1);
1678 // Add the Switch Event
1679 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_REMOVE,
1680 topologySwitchEvent);
1681 events.add(eventEntry);
1682
1683 // Process the events
1684 TestUtils.callMethod(theEventHandler, "processEvents",
1685 List.class, events);
1686
1687 // Check the fired events: no events should be fired
1688 topologyEvents = theTopologyListener.topologyEvents;
1689 assertNull(topologyEvents);
1690 theTopologyListener.clear();
1691 events.clear();
1692 }
weibitf7c31a42014-06-23 16:51:01 -07001693}