blob: a84b00f79131ec88ec4e19c6e5ad07fdc94839cc [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 Radoslavov53b208a2014-07-28 13:16:11 -070012import net.onrc.onos.core.util.OnosInstanceId;
weibitf7c31a42014-06-23 16:51:01 -070013import net.onrc.onos.core.util.PortNumber;
14import net.onrc.onos.core.util.SwitchPort;
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070015import net.onrc.onos.core.util.TestUtils;
Ray Milkey38301352014-07-28 08:51:54 -070016import net.onrc.onos.core.util.UnitTest;
weibitf7c31a42014-06-23 16:51:01 -070017import org.easymock.EasyMock;
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;
29import static org.easymock.EasyMock.eq;
30import static org.easymock.EasyMock.expect;
31import static org.easymock.EasyMock.replay;
Pavlin Radoslavovc6236412014-08-01 20:37:46 -070032import static org.easymock.EasyMock.reset;
Ray Milkey38301352014-07-28 08:51:54 -070033import static org.easymock.EasyMock.verify;
34import static org.hamcrest.Matchers.containsInAnyOrder;
Pavlin Radoslavovc6236412014-08-01 20:37:46 -070035import static org.hamcrest.Matchers.empty;
Ray Milkey38301352014-07-28 08:51:54 -070036import static org.hamcrest.Matchers.hasItem;
Pavlin Radoslavovc6236412014-08-01 20:37:46 -070037import static org.hamcrest.Matchers.is;
Ray Milkey38301352014-07-28 08:51:54 -070038import static org.hamcrest.Matchers.not;
39import static org.junit.Assert.assertEquals;
Pavlin Radoslavovc6236412014-08-01 20:37:46 -070040import static org.junit.Assert.assertNotNull;
Ray Milkey38301352014-07-28 08:51:54 -070041import static org.junit.Assert.assertNull;
42import static org.junit.Assert.assertThat;
43import static org.junit.Assert.assertTrue;
44
weibitf7c31a42014-06-23 16:51:01 -070045/**
46 * Unit tests for the TopologyManager class in the Topology module.
47 * These test cases only check the sanity of functions in the TopologyManager.
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -070048 * Note that we do not test the eventHandler functions in the TopologyManager
49 * class.
50 * DatagridService, DataStoreService, eventChannel, and
51 * controllerRegistryService are mocked out.
weibitf7c31a42014-06-23 16:51:01 -070052 */
Ray Milkey38301352014-07-28 08:51:54 -070053public class TopologyManagerTest extends UnitTest {
weibitf7c31a42014-06-23 16:51:01 -070054 private TopologyManager theTopologyManager;
Pavlin Radoslavovc6236412014-08-01 20:37:46 -070055 private TopologyManager.EventHandler theEventHandler;
56 private TopologyListenerTest theTopologyListener =
57 new TopologyListenerTest();
weibitf7c31a42014-06-23 16:51:01 -070058 private final String eventChannelName = "onos.topology";
59 private IEventChannel<byte[], TopologyEvent> eventChannel;
60 private IDatagridService datagridService;
61 private TopologyDatastore dataStoreService;
62 private IControllerRegistryService registryService;
weibitf7c31a42014-06-23 16:51:01 -070063 private Collection<TopologyEvent> allTopologyEvents;
Pavlin Radoslavovc6236412014-08-01 20:37:46 -070064 private static final OnosInstanceId ONOS_INSTANCE_ID_1 =
65 new OnosInstanceId("ONOS-Instance-ID-1");
66 private static final OnosInstanceId ONOS_INSTANCE_ID_2 =
67 new OnosInstanceId("ONOS-Instance-ID-2");
68 private static final Dpid DPID_1 = new Dpid(1);
69 private static final Dpid DPID_2 = new Dpid(2);
70
71 /**
72 * Topology events listener.
73 */
74 private class TopologyListenerTest implements ITopologyListener {
75 private TopologyEvents topologyEvents;
76
77 @Override
78 public void topologyEvents(TopologyEvents events) {
79 this.topologyEvents = events;
80 }
81
82 /**
83 * Clears the Topology Listener state.
84 */
85 public void clear() {
86 this.topologyEvents = null;
87 }
88 }
weibitf7c31a42014-06-23 16:51:01 -070089
90 @SuppressWarnings("unchecked")
91 @Before
92 public void setUp() throws Exception {
93 // Mock objects for testing
94 datagridService = EasyMock.createNiceMock(IDatagridService.class);
95 dataStoreService = EasyMock.createNiceMock(TopologyDatastore.class);
96 registryService = createMock(IControllerRegistryService.class);
97 eventChannel = EasyMock.createNiceMock(IEventChannel.class);
98
99 expect(datagridService.createChannel(
100 eq(eventChannelName),
101 eq(byte[].class),
102 eq(TopologyEvent.class)))
103 .andReturn(eventChannel).once();
104
105 expect(datagridService.addListener(
106 eq(eventChannelName),
107 anyObject(IEventChannelListener.class),
108 eq(byte[].class),
109 eq(TopologyEvent.class)))
110 .andReturn(eventChannel).once();
111
112 expect(dataStoreService.addSwitch(
113 anyObject(SwitchEvent.class),
114 anyObject(Collection.class)))
115 .andReturn(true).anyTimes();
116
117 expect(dataStoreService.deactivateSwitch(
118 anyObject(SwitchEvent.class),
119 anyObject(Collection.class)))
120 .andReturn(true).anyTimes();
121
122 expect(dataStoreService.addPort(
123 anyObject(PortEvent.class)))
124 .andReturn(true).anyTimes();
125
126 expect(dataStoreService.deactivatePort(
127 anyObject(PortEvent.class)))
128 .andReturn(true).anyTimes();
129
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700130 expect(dataStoreService.addHost(
131 anyObject(HostEvent.class)))
weibitf7c31a42014-06-23 16:51:01 -0700132 .andReturn(true).anyTimes();
133
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700134 expect(dataStoreService.removeHost(
135 anyObject(HostEvent.class)))
weibitf7c31a42014-06-23 16:51:01 -0700136 .andReturn(true).anyTimes();
137
138 expect(dataStoreService.addLink(
139 anyObject(LinkEvent.class)))
140 .andReturn(true).anyTimes();
141
142 expect(dataStoreService.removeLink(
143 anyObject(LinkEvent.class)))
144 .andReturn(true).anyTimes();
145
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700146 // Setup the Registry Service
147 expect(registryService.getOnosInstanceId()).andReturn(ONOS_INSTANCE_ID_1).anyTimes();
Pavlin Radoslavov054cd592014-08-07 20:57:16 -0700148 expect(registryService.getControllerForSwitch(DPID_1.value()))
149 .andReturn(ONOS_INSTANCE_ID_1.toString()).anyTimes();
150 expect(registryService.getControllerForSwitch(DPID_2.value()))
151 .andReturn(ONOS_INSTANCE_ID_2.toString()).anyTimes();
weibitf7c31a42014-06-23 16:51:01 -0700152
153 allTopologyEvents = new CopyOnWriteArrayList<>();
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700154 expect(eventChannel.getAllEntries())
155 .andReturn(allTopologyEvents).anyTimes();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700156
157 replay(datagridService);
158 replay(registryService);
159 replay(dataStoreService);
160 // replay(eventChannel);
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700161 }
weibitf7c31a42014-06-23 16:51:01 -0700162
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700163 /**
164 * Setup the Topology Manager.
165 */
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700166 private void setupTopologyManager() {
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700167 // Create a TopologyManager object for testing
Pavlin Radoslavov8a44b782014-08-07 12:53:27 -0700168 theTopologyManager = new TopologyManager(registryService);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700169
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700170 // Replace the eventHandler to prevent the thread from starting
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700171 TestUtils.setField(theTopologyManager, "eventHandler",
172 EasyMock.createNiceMock(TopologyManager.EventHandler.class));
weibitf7c31a42014-06-23 16:51:01 -0700173 theTopologyManager.startup(datagridService);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700174
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700175 // Replace the data store with a mocked object
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700176 TestUtils.setField(theTopologyManager, "datastore", dataStoreService);
weibitf7c31a42014-06-23 16:51:01 -0700177 }
178
weibitf7c31a42014-06-23 16:51:01 -0700179 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700180 * Setup the Topology Manager with the Event Handler.
181 */
182 private void setupTopologyManagerWithEventHandler() {
183 // Create a TopologyManager object for testing
Pavlin Radoslavov8a44b782014-08-07 12:53:27 -0700184 theTopologyManager = new TopologyManager(registryService);
Pavlin Radoslavov054cd592014-08-07 20:57:16 -0700185 theTopologyManager.addListener(theTopologyListener, true);
Pavlin Radoslavov8a44b782014-08-07 12:53:27 -0700186
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700187 // Allocate the Event Handler, so we can have direct access to it
188 theEventHandler = theTopologyManager.new EventHandler();
189 TestUtils.setField(theTopologyManager, "eventHandler",
190 theEventHandler);
191
192 // Replace the data store with a mocked object
193 TestUtils.setField(theTopologyManager, "datastore", dataStoreService);
194
195 replay(eventChannel);
196 //
197 // NOTE: Uncomment-out the line below if the startup() method needs
198 // to be called for some of the unit tests. For now it is commented-out
199 // to avoid any side effects of starting the eventHandler thread.
200 //
201 // theTopologyManager.startup(datagridService);
202 }
203
204 /**
205 * Test the Switch Mastership Updated Event.
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700206 */
207 @Test
208 public void testPutSwitchMastershipEvent() {
209 // Mock the eventChannel functions first
210 eventChannel.addEntry(anyObject(byte[].class),
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700211 anyObject(TopologyEvent.class));
212 EasyMock.expectLastCall().times(1, 1); // 1 event
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700213 replay(eventChannel);
214
215 setupTopologyManager();
216
217 // Generate a new Switch Mastership event
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700218 Role role = Role.MASTER;
219 MastershipEvent mastershipEvent =
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700220 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700221
222 // Call the topologyManager function for adding the event
223 theTopologyManager.putSwitchMastershipEvent(mastershipEvent);
224
225 // Verify the function calls
226 verify(eventChannel);
227 }
228
229 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700230 * Test the Switch Mastership Removed Event.
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700231 */
232 @Test
233 public void testRemoveSwitchMastershipEvent() {
234 // Mock the eventChannel functions first
235 eventChannel.removeEntry(anyObject(byte[].class));
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700236 EasyMock.expectLastCall().times(1, 1); // 1 event
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700237 replay(eventChannel);
238
239 setupTopologyManager();
240
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700241 // Generate a new Switch Mastership Event
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700242 Role role = Role.MASTER;
243 MastershipEvent mastershipEvent =
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700244 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700245
246 // Call the topologyManager function for removing the event
247 theTopologyManager.removeSwitchMastershipEvent(mastershipEvent);
248
249 // Verify the function calls
250 verify(eventChannel);
251 }
252
253 /**
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700254 * Test the Switch discovered and Port discovered functions.
255 */
256 @Test
257 public void testPutSwitchAndPortDiscoveryEvent() {
258 // Mock the eventChannel functions first
259 eventChannel.addEntry(anyObject(byte[].class),
260 anyObject(TopologyEvent.class));
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700261 EasyMock.expectLastCall().times(3, 3); // (1 Switch + 1 Port), 1 Port
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700262 replay(eventChannel);
263
264 setupTopologyManager();
265
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700266 // Mock Switch has one Port
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700267 PortNumber portNumber = PortNumber.uint32(1);
268
269 // Generate a new Switch Event along with a Port Event
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700270 SwitchEvent switchEvent = new SwitchEvent(DPID_1);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700271
272 Collection<PortEvent> portEvents = new ArrayList<PortEvent>();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700273 portEvents.add(new PortEvent(DPID_1, portNumber));
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700274
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700275 // Call the topologyManager function for adding a Switch
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700276 theTopologyManager.putSwitchDiscoveryEvent(switchEvent, portEvents);
277
278 for (PortEvent portEvent : portEvents) {
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700279 // Call the topologyManager function for adding a Port
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700280 theTopologyManager.putPortDiscoveryEvent(portEvent);
281 }
282
283 // Verify the function calls
284 verify(eventChannel);
285
286 }
287
288 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700289 * Test the Switch and Port removed functions.
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700290 */
291 @Test
292 public void testRemoveSwitchAndPortDiscoveryEvent() {
293 // Mock the eventChannel functions first
294 eventChannel.removeEntry(anyObject(byte[].class));
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700295 EasyMock.expectLastCall().times(2, 2); // 1 Switch, 1 Port
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700296 replay(eventChannel);
297
298 setupTopologyManager();
299
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700300 PortNumber portNumber = PortNumber.uint32(1);
301
302 // Generate a Port Event
303 Collection<PortEvent> portEvents = new ArrayList<PortEvent>();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700304 portEvents.add(new PortEvent(DPID_1, portNumber));
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700305
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700306 // Call the topologyManager function for removing a Port
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700307 for (PortEvent portEvent : portEvents) {
308 theTopologyManager.removePortDiscoveryEvent(portEvent);
309 }
310
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700311 // Call the topologyManager function for removing a Switch
312 SwitchEvent switchEvent = new SwitchEvent(DPID_1);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700313 theTopologyManager.removeSwitchDiscoveryEvent(switchEvent);
314
315 // Verify the function calls
316 verify(eventChannel);
317
318 }
319
320 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700321 * Test the Link discovered function.
weibitf7c31a42014-06-23 16:51:01 -0700322 */
323 @Test
324 public void testPutLinkDiscoveryEvent() {
325 // Mock the eventChannel functions first
326 eventChannel.addEntry(anyObject(byte[].class),
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700327 anyObject(TopologyEvent.class));
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700328 EasyMock.expectLastCall().times(5, 5); // (2 Switch + 2 Port + 1 Link)
weibitf7c31a42014-06-23 16:51:01 -0700329 replay(eventChannel);
330
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700331 setupTopologyManager();
332
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700333 // Generate the Switch and Port Events
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700334 PortNumber portNumber1 = PortNumber.uint32(1);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700335 SwitchEvent switchEvent1 = new SwitchEvent(DPID_1);
weibitf7c31a42014-06-23 16:51:01 -0700336 Collection<PortEvent> portEvents1 = new ArrayList<PortEvent>();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700337 portEvents1.add(new PortEvent(DPID_1, portNumber1));
weibitf7c31a42014-06-23 16:51:01 -0700338
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700339 // Call the topologyManager function for adding a Switch
weibitf7c31a42014-06-23 16:51:01 -0700340 theTopologyManager.putSwitchDiscoveryEvent(switchEvent1, portEvents1);
341
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700342 // Generate the Switch and Port Events
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700343 PortNumber portNumber2 = PortNumber.uint32(2);
344 SwitchEvent switchEvent2 = new SwitchEvent(DPID_2);
weibitf7c31a42014-06-23 16:51:01 -0700345 Collection<PortEvent> portEvents2 = new ArrayList<PortEvent>();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700346 portEvents2.add(new PortEvent(DPID_2, portNumber2));
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(switchEvent2, portEvents2);
350
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700351 // Create the Link Event
352 LinkEvent linkEvent =
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700353 new LinkEvent(new SwitchPort(DPID_1, portNumber1),
354 new SwitchPort(DPID_2, portNumber2));
weibitf7c31a42014-06-23 16:51:01 -0700355 theTopologyManager.putLinkDiscoveryEvent(linkEvent);
356
357 // Verify the function calls
358 verify(eventChannel);
359 }
360
361 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700362 * Test the Link removed function.
weibitf7c31a42014-06-23 16:51:01 -0700363 */
364 @Test
365 public void testRemoveLinkDiscoveryEvent() {
366 // Mock the eventChannel functions first
367 eventChannel.removeEntry(anyObject(byte[].class));
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700368 EasyMock.expectLastCall().times(1, 1); // (1 Link)
weibitf7c31a42014-06-23 16:51:01 -0700369 replay(eventChannel);
370
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700371 setupTopologyManager();
372
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700373 // Generate the Switch and Port Events
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700374 PortNumber portNumber1 = PortNumber.uint32(1);
375 SwitchEvent switchEvent1 = new SwitchEvent(DPID_1);
weibitf7c31a42014-06-23 16:51:01 -0700376 Collection<PortEvent> portEvents1 = new ArrayList<PortEvent>();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700377 portEvents1.add(new PortEvent(DPID_1, portNumber1));
weibitf7c31a42014-06-23 16:51:01 -0700378
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700379 // Call the topologyManager function for adding a Switch
weibitf7c31a42014-06-23 16:51:01 -0700380 theTopologyManager.putSwitchDiscoveryEvent(switchEvent1, portEvents1);
381
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700382 // Generate the Switch and Port Events
383 PortNumber portNumber2 = PortNumber.uint32(2);
384 SwitchEvent switchEvent2 = new SwitchEvent(DPID_2);
weibitf7c31a42014-06-23 16:51:01 -0700385 Collection<PortEvent> portEvents2 = new ArrayList<PortEvent>();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700386 portEvents2.add(new PortEvent(DPID_2, portNumber2));
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(switchEvent2, portEvents2);
390
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700391 // Remove the Link
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700392 LinkEvent linkEventRemove =
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700393 new LinkEvent(new SwitchPort(DPID_1, portNumber1),
394 new SwitchPort(DPID_2, portNumber2));
weibitf7c31a42014-06-23 16:51:01 -0700395 theTopologyManager.removeLinkDiscoveryEvent(linkEventRemove);
396
397 // Verify the function calls
398 verify(eventChannel);
399 }
400
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700401 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700402 * Test the Host discovered function.
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700403 */
404 @Test
405 public void testPutHostDiscoveryEvent() {
406 // Mock the eventChannel functions first
407 eventChannel.addEntry(anyObject(byte[].class),
408 anyObject(TopologyEvent.class));
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700409 EasyMock.expectLastCall().times(1, 1); // 1 Host
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700410 replay(eventChannel);
411
412 setupTopologyManager();
413
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700414 // Generate a new Host Event
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700415 PortNumber portNumber = PortNumber.uint32(1);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700416 MACAddress hostMac = MACAddress.valueOf("00:AA:11:BB:33:CC");
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700417 SwitchPort sp = new SwitchPort(DPID_1, portNumber);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700418 List<SwitchPort> spLists = new ArrayList<SwitchPort>();
419 spLists.add(sp);
420 HostEvent hostEvent = new HostEvent(hostMac);
421 hostEvent.setAttachmentPoints(spLists);
422
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700423 // Call the topologyManager function for adding a Host
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700424 theTopologyManager.putHostDiscoveryEvent(hostEvent);
425
426 // Verify the function calls
427 verify(eventChannel);
428 }
429
430 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700431 * Test the Host removed function.
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700432 */
433 @Test
434 public void testRemoveHostDiscoveryEvent() {
435 // Mock the eventChannel functions first
436 eventChannel.removeEntry(anyObject(byte[].class));
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700437 EasyMock.expectLastCall().times(1, 1); // 1 Host
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700438 replay(eventChannel);
439
440 setupTopologyManager();
441
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700442 // Generate a new Host Event
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700443 PortNumber portNumber = PortNumber.uint32(1);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700444 MACAddress hostMac = MACAddress.valueOf("00:AA:11:BB:33:CC");
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700445 SwitchPort sp = new SwitchPort(DPID_1, portNumber);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700446 List<SwitchPort> spLists = new ArrayList<SwitchPort>();
447 spLists.add(sp);
448 HostEvent hostEvent = new HostEvent(hostMac);
449 hostEvent.setAttachmentPoints(spLists);
450
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700451 // Call the topologyManager function for removing a Host
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700452 theTopologyManager.removeHostDiscoveryEvent(hostEvent);
453
454 // Verify the function calls
455 verify(eventChannel);
456 }
457
458 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700459 * Tests adding of a Switch Mastership event and the topology replica
460 * transformation.
461 */
462 @Test
463 public void testAddMastershipEvent() {
464 setupTopologyManager();
465
466 // Prepare the event
467 Role role = Role.MASTER;
468 MastershipEvent mastershipEvent =
469 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
470 // Add the event
471 TestUtils.callMethod(theTopologyManager, "addMastershipEvent",
472 MastershipEvent.class, mastershipEvent);
473
474 //
475 // NOTE: The topology itself doesn't contain the Mastership Events,
476 // hence we don't check the topology.
477 //
478
479 // Check the events to be fired
480 List<MastershipEvent> apiAddedMastershipEvents
481 = TestUtils.getField(theTopologyManager,
482 "apiAddedMastershipEvents");
483 assertThat(apiAddedMastershipEvents, hasItem(mastershipEvent));
484 }
485
486 /**
487 * Tests removing of a Switch Mastership event and the topology replica
488 * transformation.
489 */
490 @Test
491 public void testRemoveMastershipEvent() {
492 setupTopologyManager();
493
494 // Prepare the event
495 Role role = Role.MASTER;
496 MastershipEvent mastershipEvent =
497 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
498 // Add the event
499 TestUtils.callMethod(theTopologyManager, "addMastershipEvent",
500 MastershipEvent.class, mastershipEvent);
501
502 // Check the events to be fired
503 List<MastershipEvent> apiAddedMastershipEvents
504 = TestUtils.getField(theTopologyManager,
505 "apiAddedMastershipEvents");
506 assertThat(apiAddedMastershipEvents, hasItem(mastershipEvent));
507
508 // Remove the event
509 TestUtils.callMethod(theTopologyManager, "removeMastershipEvent",
510 MastershipEvent.class,
511 new MastershipEvent(mastershipEvent));
512
513 // Check the events to be fired
514 List<MastershipEvent> apiRemovedMastershipEvents
515 = TestUtils.getField(theTopologyManager,
516 "apiRemovedMastershipEvents");
517 assertThat(apiRemovedMastershipEvents, hasItem(mastershipEvent));
518 }
519
520 /**
521 * Tests adding of a Switch and the topology replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700522 */
523 @Test
524 public void testAddSwitch() {
525 setupTopologyManager();
526
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700527 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700528 sw.createStringAttribute("foo", "bar");
529
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700530 TestUtils.callMethod(theTopologyManager, "addSwitch",
531 SwitchEvent.class, sw);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700532
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700533 // Check the topology structure
534 TopologyInternal topology =
535 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700536 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700537 assertEquals(sw, swInTopo);
538 assertTrue(swInTopo.isFrozen());
539 assertEquals("bar", swInTopo.getStringAttribute("foo"));
540
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700541 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700542 List<SwitchEvent> apiAddedSwitchEvents
543 = TestUtils.getField(theTopologyManager, "apiAddedSwitchEvents");
544 assertThat(apiAddedSwitchEvents, hasItem(sw));
545 }
546
547 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700548 * Tests adding of a Port and the topology replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700549 */
550 @Test
551 public void testAddPort() {
552 setupTopologyManager();
553
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700554 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700555 sw.createStringAttribute("foo", "bar");
556
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700557 final PortNumber portNumber = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700558 PortEvent port = new PortEvent(DPID_1, portNumber);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700559 port.createStringAttribute("fuzz", "buzz");
560
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700561 TestUtils.callMethod(theTopologyManager, "addSwitch",
562 SwitchEvent.class, sw);
563 TestUtils.callMethod(theTopologyManager, "addPort",
564 PortEvent.class, port);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700565
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700566 // Check the topology structure
567 TopologyInternal topology =
568 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700569 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700570 assertEquals(sw, swInTopo);
571 assertTrue(swInTopo.isFrozen());
572 assertEquals("bar", swInTopo.getStringAttribute("foo"));
573
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700574 final SwitchPort switchPort = new SwitchPort(DPID_1, portNumber);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700575 PortEvent portInTopo = topology.getPortEvent(switchPort);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700576 assertEquals(port, portInTopo);
577 assertTrue(portInTopo.isFrozen());
578 assertEquals("buzz", portInTopo.getStringAttribute("fuzz"));
579
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700580 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700581 List<PortEvent> apiAddedPortEvents
582 = TestUtils.getField(theTopologyManager, "apiAddedPortEvents");
583 assertThat(apiAddedPortEvents, hasItem(port));
584 }
585
586 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700587 * Tests removing of a Port followed by removing of a Switch,
588 * and the topology replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700589 */
590 @Test
591 public void testRemovePortThenSwitch() {
592 setupTopologyManager();
593
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700594 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700595 sw.createStringAttribute("foo", "bar");
596
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700597 final PortNumber portNumber = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700598 PortEvent port = new PortEvent(DPID_1, portNumber);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700599 port.createStringAttribute("fuzz", "buzz");
600
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700601 TestUtils.callMethod(theTopologyManager, "addSwitch",
602 SwitchEvent.class, sw);
603 TestUtils.callMethod(theTopologyManager, "addPort",
604 PortEvent.class, port);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700605
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700606 // Check the topology structure
607 TopologyInternal topology =
608 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700609 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700610 assertEquals(sw, swInTopo);
611 assertTrue(swInTopo.isFrozen());
612 assertEquals("bar", swInTopo.getStringAttribute("foo"));
613
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700614 final SwitchPort switchPort = new SwitchPort(DPID_1, portNumber);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700615 PortEvent portInTopo = topology.getPortEvent(switchPort);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700616 assertEquals(port, portInTopo);
617 assertTrue(portInTopo.isFrozen());
618 assertEquals("buzz", portInTopo.getStringAttribute("fuzz"));
619
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700620 // Remove in proper order
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700621 TestUtils.callMethod(theTopologyManager, "removePort",
622 PortEvent.class, new PortEvent(port));
623 TestUtils.callMethod(theTopologyManager, "removeSwitch",
624 SwitchEvent.class, new SwitchEvent(sw));
625
626
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700627 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700628 List<PortEvent> apiRemovedPortEvents
629 = TestUtils.getField(theTopologyManager, "apiRemovedPortEvents");
630 assertThat(apiRemovedPortEvents, hasItem(port));
631 List<SwitchEvent> apiRemovedSwitchEvents
632 = TestUtils.getField(theTopologyManager, "apiRemovedSwitchEvents");
633 assertThat(apiRemovedSwitchEvents, hasItem(sw));
634 }
635
636 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700637 * Tests removing of a Switch without removing of a Port,
638 * and the topology replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700639 */
640 @Test
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700641 public void testRemoveSwitchWithoutPortRemoval() {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700642 setupTopologyManager();
643
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700644 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700645 sw.createStringAttribute("foo", "bar");
646
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700647 final PortNumber portNumber = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700648 PortEvent port = new PortEvent(DPID_1, portNumber);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700649 port.createStringAttribute("fuzz", "buzz");
650
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700651 TestUtils.callMethod(theTopologyManager, "addSwitch",
652 SwitchEvent.class, sw);
653 TestUtils.callMethod(theTopologyManager, "addPort",
654 PortEvent.class, port);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700655
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700656 // Check the topology structure
657 TopologyInternal topology =
658 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700659 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700660 assertEquals(sw, swInTopo);
661 assertTrue(swInTopo.isFrozen());
662 assertEquals("bar", swInTopo.getStringAttribute("foo"));
663
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700664 final SwitchPort switchPort = new SwitchPort(DPID_1, portNumber);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700665 PortEvent portInTopo = topology.getPortEvent(switchPort);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700666 assertEquals(port, portInTopo);
667 assertTrue(portInTopo.isFrozen());
668 assertEquals("buzz", portInTopo.getStringAttribute("fuzz"));
669
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700670 // Remove in in-proper order
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700671// TestUtils.callMethod(theTopologyManager, "removePort",
672// PortEvent.class, new PortEvent(port));
673 TestUtils.callMethod(theTopologyManager, "removeSwitch",
674 SwitchEvent.class, new SwitchEvent(sw));
675
676
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700677 // Check the events to be fired
678 // The outcome should be the same as #testRemovePortThenSwitch
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700679 List<PortEvent> apiRemovedPortEvents
680 = TestUtils.getField(theTopologyManager, "apiRemovedPortEvents");
681 assertThat(apiRemovedPortEvents, hasItem(port));
682 List<SwitchEvent> apiRemovedSwitchEvents
683 = TestUtils.getField(theTopologyManager, "apiRemovedSwitchEvents");
684 assertThat(apiRemovedSwitchEvents, hasItem(sw));
685 }
686
687 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700688 * Tests adding of a Link and the topology replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700689 */
690 @Test
691 public void testAddLink() {
692 setupTopologyManager();
693
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700694 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700695 sw.createStringAttribute("foo", "bar");
696
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700697 final PortNumber portNumberA = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700698 PortEvent portA = new PortEvent(DPID_1, portNumberA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700699 portA.createStringAttribute("fuzz", "buzz");
700
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700701 final PortNumber portNumberB = PortNumber.uint32(3);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700702 PortEvent portB = new PortEvent(DPID_1, portNumberB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700703 portB.createStringAttribute("fizz", "buz");
704
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700705 LinkEvent linkA = new LinkEvent(portA.getSwitchPort(),
706 portB.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700707 linkA.createStringAttribute(TopologyElement.TYPE,
708 TopologyElement.TYPE_OPTICAL_LAYER);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700709 LinkEvent linkB = new LinkEvent(portB.getSwitchPort(),
710 portA.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700711 linkB.createStringAttribute(TopologyElement.TYPE,
712 TopologyElement.TYPE_OPTICAL_LAYER);
713
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700714 TestUtils.callMethod(theTopologyManager, "addSwitch",
715 SwitchEvent.class, sw);
716 TestUtils.callMethod(theTopologyManager, "addPort",
717 PortEvent.class, portA);
718 TestUtils.callMethod(theTopologyManager, "addPort",
719 PortEvent.class, portB);
720 TestUtils.callMethod(theTopologyManager, "addLink",
721 LinkEvent.class, linkA);
722 TestUtils.callMethod(theTopologyManager, "addLink",
723 LinkEvent.class, linkB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700724
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700725 // Check the topology structure
726 TopologyInternal topology =
727 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700728 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700729 assertEquals(sw, swInTopo);
730 assertTrue(swInTopo.isFrozen());
731 assertEquals("bar", swInTopo.getStringAttribute("foo"));
732
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700733 final SwitchPort switchPortA = new SwitchPort(DPID_1, portNumberA);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700734 PortEvent portAInTopo = topology.getPortEvent(switchPortA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700735 assertEquals(portA, portAInTopo);
736 assertTrue(portAInTopo.isFrozen());
737 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
738
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700739 final SwitchPort switchPortB = new SwitchPort(DPID_1, portNumberB);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700740 PortEvent portBInTopo = topology.getPortEvent(switchPortB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700741 assertEquals(portB, portBInTopo);
742 assertTrue(portBInTopo.isFrozen());
743 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
744
745 LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple());
746 assertEquals(linkA, linkAInTopo);
747 assertTrue(linkAInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700748 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
749 linkAInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700750
751 LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple());
752 assertEquals(linkB, linkBInTopo);
753 assertTrue(linkBInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700754 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
755 linkBInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700756
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700757 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700758 List<LinkEvent> apiAddedLinkEvents
759 = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents");
760 assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB));
761 }
762
763 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700764 * Tests removing of a Link without removing of a Host, and the topology
765 * replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700766 */
767 @Test
768 public void testAddLinkKickingOffHost() {
769 setupTopologyManager();
770
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700771 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700772 sw.createStringAttribute("foo", "bar");
773
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700774 final PortNumber portNumberA = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700775 PortEvent portA = new PortEvent(DPID_1, portNumberA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700776 portA.createStringAttribute("fuzz", "buzz");
777
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700778 final PortNumber portNumberB = PortNumber.uint32(3);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700779 PortEvent portB = new PortEvent(DPID_1, portNumberB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700780 portB.createStringAttribute("fizz", "buz");
781
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700782 final PortNumber portNumberC = PortNumber.uint32(4);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700783 PortEvent portC = new PortEvent(DPID_1, portNumberC);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700784 portC.createStringAttribute("fizz", "buz");
785
786 final MACAddress macA = MACAddress.valueOf(666L);
787 HostEvent hostA = new HostEvent(macA);
788 hostA.addAttachmentPoint(portA.getSwitchPort());
789 final long timestampA = 392893200L;
790 hostA.setLastSeenTime(timestampA);
791
792 final MACAddress macB = MACAddress.valueOf(999L);
793 HostEvent hostB = new HostEvent(macB);
794 hostB.addAttachmentPoint(portB.getSwitchPort());
795 hostB.addAttachmentPoint(portC.getSwitchPort());
796 final long timestampB = 392893201L;
797 hostB.setLastSeenTime(timestampB);
798
799
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700800 LinkEvent linkA = new LinkEvent(portA.getSwitchPort(),
801 portB.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700802 linkA.createStringAttribute(TopologyElement.TYPE,
803 TopologyElement.TYPE_OPTICAL_LAYER);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700804 LinkEvent linkB = new LinkEvent(portB.getSwitchPort(),
805 portA.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700806 linkB.createStringAttribute(TopologyElement.TYPE,
807 TopologyElement.TYPE_OPTICAL_LAYER);
808
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700809 TestUtils.callMethod(theTopologyManager, "addSwitch",
810 SwitchEvent.class, sw);
811 TestUtils.callMethod(theTopologyManager, "addPort",
812 PortEvent.class, portA);
813 TestUtils.callMethod(theTopologyManager, "addPort",
814 PortEvent.class, portB);
815 TestUtils.callMethod(theTopologyManager, "addPort",
816 PortEvent.class, portC);
817 TestUtils.callMethod(theTopologyManager, "addHost",
818 HostEvent.class, hostA);
819 TestUtils.callMethod(theTopologyManager, "addHost",
820 HostEvent.class, hostB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700821
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700822 TestUtils.callMethod(theTopologyManager, "addLink",
823 LinkEvent.class, linkA);
824 TestUtils.callMethod(theTopologyManager, "addLink",
825 LinkEvent.class, linkB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700826
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700827 // Check the topology structure
828 TopologyInternal topology =
829 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700830 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700831 assertEquals(sw, swInTopo);
832 assertTrue(swInTopo.isFrozen());
833 assertEquals("bar", swInTopo.getStringAttribute("foo"));
834
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700835 final SwitchPort switchPortA = new SwitchPort(DPID_1, portNumberA);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700836 PortEvent portAInTopo = topology.getPortEvent(switchPortA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700837 assertEquals(portA, portAInTopo);
838 assertTrue(portAInTopo.isFrozen());
839 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
840
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700841 final SwitchPort switchPortB = new SwitchPort(DPID_1, portNumberB);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700842 PortEvent portBInTopo = topology.getPortEvent(switchPortB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700843 assertEquals(portB, portBInTopo);
844 assertTrue(portBInTopo.isFrozen());
845 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
846
847 // hostA expected to be removed
848 assertNull(topology.getHostEvent(macA));
849 // hostB expected to be there with reduced attachment point
850 HostEvent hostBrev = new HostEvent(macB);
851 hostBrev.addAttachmentPoint(portC.getSwitchPort());
852 hostBrev.setLastSeenTime(timestampB);
853 hostBrev.freeze();
854 assertEquals(hostBrev, topology.getHostEvent(macB));
855
856
857 LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple());
858 assertEquals(linkA, linkAInTopo);
859 assertTrue(linkAInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700860 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
861 linkAInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700862
863 LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple());
864 assertEquals(linkB, linkBInTopo);
865 assertTrue(linkBInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700866 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
867 linkBInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700868
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700869 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700870 List<HostEvent> apiAddedHostEvents
871 = TestUtils.getField(theTopologyManager, "apiAddedHostEvents");
872 assertThat(apiAddedHostEvents, hasItem(hostBrev));
873
874 List<HostEvent> apiRemovedHostEvents
875 = TestUtils.getField(theTopologyManager, "apiRemovedHostEvents");
876 assertThat(apiRemovedHostEvents, hasItem(hostA));
877 List<LinkEvent> apiAddedLinkEvents
878 = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents");
879 assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB));
880 }
881
882 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700883 * Tests removing of a Link and the topology replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700884 */
885 @Test
886 public void testRemoveLink() {
887 setupTopologyManager();
888
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700889 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700890 sw.createStringAttribute("foo", "bar");
891
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700892 final PortNumber portNumberA = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700893 PortEvent portA = new PortEvent(DPID_1, portNumberA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700894 portA.createStringAttribute("fuzz", "buzz");
895
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700896 final PortNumber portNumberB = PortNumber.uint32(3);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700897 PortEvent portB = new PortEvent(DPID_1, portNumberB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700898 portB.createStringAttribute("fizz", "buz");
899
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700900 LinkEvent linkA = new LinkEvent(portA.getSwitchPort(),
901 portB.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700902 linkA.createStringAttribute(TopologyElement.TYPE,
903 TopologyElement.TYPE_OPTICAL_LAYER);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700904 LinkEvent linkB = new LinkEvent(portB.getSwitchPort(),
905 portA.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700906 linkB.createStringAttribute(TopologyElement.TYPE,
907 TopologyElement.TYPE_OPTICAL_LAYER);
908
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700909 TestUtils.callMethod(theTopologyManager, "addSwitch",
910 SwitchEvent.class, sw);
911 TestUtils.callMethod(theTopologyManager, "addPort",
912 PortEvent.class, portA);
913 TestUtils.callMethod(theTopologyManager, "addPort",
914 PortEvent.class, portB);
915 TestUtils.callMethod(theTopologyManager, "addLink",
916 LinkEvent.class, linkA);
917 TestUtils.callMethod(theTopologyManager, "addLink",
918 LinkEvent.class, linkB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700919
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700920 // Check the topology structure
921 TopologyInternal topology =
922 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700923 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700924 assertEquals(sw, swInTopo);
925 assertTrue(swInTopo.isFrozen());
926 assertEquals("bar", swInTopo.getStringAttribute("foo"));
927
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700928 final SwitchPort switchPortA = new SwitchPort(DPID_1, portNumberA);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700929 PortEvent portAInTopo = topology.getPortEvent(switchPortA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700930 assertEquals(portA, portAInTopo);
931 assertTrue(portAInTopo.isFrozen());
932 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
933
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700934 final SwitchPort switchPortB = new SwitchPort(DPID_1, portNumberB);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700935 PortEvent portBInTopo = topology.getPortEvent(switchPortB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700936 assertEquals(portB, portBInTopo);
937 assertTrue(portBInTopo.isFrozen());
938 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
939
940 LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple());
941 assertEquals(linkA, linkAInTopo);
942 assertTrue(linkAInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700943 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
944 linkAInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700945
946
947 LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple());
948 assertEquals(linkB, linkBInTopo);
949 assertTrue(linkBInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700950 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
951 linkBInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700952
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700953 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700954 // FIXME if link flapped (linkA in this scenario),
955 // linkA appears in both removed and added is this expected behavior?
956 List<LinkEvent> apiAddedLinkEvents
957 = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents");
958 assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB));
959
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700960 // Clear the events before removing the link
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700961 apiAddedLinkEvents.clear();
962
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700963 // Remove the link
964 TestUtils.callMethod(theTopologyManager, "removeLink",
965 LinkEvent.class, new LinkEvent(linkA));
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700966
967 LinkEvent linkANotInTopo = topology.getLinkEvent(linkA.getLinkTuple());
968 assertNull(linkANotInTopo);
969
970 List<LinkEvent> apiRemovedLinkEvents
971 = TestUtils.getField(theTopologyManager, "apiRemovedLinkEvents");
972 assertThat(apiRemovedLinkEvents, hasItem(linkA));
973 }
974
975 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700976 * Tests adding of a Host without adding of a Link, and the topology
977 * replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700978 */
979 @Test
980 public void testAddHostIgnoredByLink() {
981 setupTopologyManager();
982
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700983 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700984 sw.createStringAttribute("foo", "bar");
985
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700986 final PortNumber portNumberA = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700987 PortEvent portA = new PortEvent(DPID_1, portNumberA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700988 portA.createStringAttribute("fuzz", "buzz");
989
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700990 final PortNumber portNumberB = PortNumber.uint32(3);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700991 PortEvent portB = new PortEvent(DPID_1, portNumberB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700992 portB.createStringAttribute("fizz", "buz");
993
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700994 final PortNumber portNumberC = PortNumber.uint32(4);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700995 PortEvent portC = new PortEvent(DPID_1, portNumberC);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700996 portC.createStringAttribute("fizz", "buz");
997
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700998 LinkEvent linkA = new LinkEvent(portA.getSwitchPort(),
999 portB.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001000 linkA.createStringAttribute(TopologyElement.TYPE,
1001 TopologyElement.TYPE_OPTICAL_LAYER);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001002 LinkEvent linkB = new LinkEvent(portB.getSwitchPort(),
1003 portA.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001004 linkB.createStringAttribute(TopologyElement.TYPE,
1005 TopologyElement.TYPE_OPTICAL_LAYER);
1006
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001007 TestUtils.callMethod(theTopologyManager, "addSwitch",
1008 SwitchEvent.class, sw);
1009 TestUtils.callMethod(theTopologyManager, "addPort",
1010 PortEvent.class, portA);
1011 TestUtils.callMethod(theTopologyManager, "addPort",
1012 PortEvent.class, portB);
1013 TestUtils.callMethod(theTopologyManager, "addPort",
1014 PortEvent.class, portC);
1015 TestUtils.callMethod(theTopologyManager, "addLink",
1016 LinkEvent.class, linkA);
1017 TestUtils.callMethod(theTopologyManager, "addLink",
1018 LinkEvent.class, linkB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001019
1020 // Add hostA attached to a port which already has a link
1021 final MACAddress macA = MACAddress.valueOf(666L);
1022 HostEvent hostA = new HostEvent(macA);
1023 hostA.addAttachmentPoint(portA.getSwitchPort());
1024 final long timestampA = 392893200L;
1025 hostA.setLastSeenTime(timestampA);
1026
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001027 TestUtils.callMethod(theTopologyManager, "addHost",
1028 HostEvent.class, hostA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001029
1030 // Add hostB attached to multiple ports,
1031 // some of them which already has a link
1032 final MACAddress macB = MACAddress.valueOf(999L);
1033 HostEvent hostB = new HostEvent(macB);
1034 hostB.addAttachmentPoint(portB.getSwitchPort());
1035 hostB.addAttachmentPoint(portC.getSwitchPort());
1036 final long timestampB = 392893201L;
1037 hostB.setLastSeenTime(timestampB);
1038
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001039 TestUtils.callMethod(theTopologyManager, "addHost",
1040 HostEvent.class, hostB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001041
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001042 // Check the topology structure
1043 TopologyInternal topology =
1044 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001045 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001046 assertEquals(sw, swInTopo);
1047 assertTrue(swInTopo.isFrozen());
1048 assertEquals("bar", swInTopo.getStringAttribute("foo"));
1049
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001050 final SwitchPort switchPortA = new SwitchPort(DPID_1, portNumberA);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001051 PortEvent portAInTopo = topology.getPortEvent(switchPortA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001052 assertEquals(portA, portAInTopo);
1053 assertTrue(portAInTopo.isFrozen());
1054 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
1055
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001056 final SwitchPort switchPortB = new SwitchPort(DPID_1, portNumberB);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001057 PortEvent portBInTopo = topology.getPortEvent(switchPortB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001058 assertEquals(portB, portBInTopo);
1059 assertTrue(portBInTopo.isFrozen());
1060 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
1061
1062 // hostA expected to be completely ignored
1063 assertNull(topology.getHostEvent(macA));
1064 // hostB expected to be there with reduced attachment point
1065 HostEvent hostBrev = new HostEvent(macB);
1066 hostBrev.addAttachmentPoint(portC.getSwitchPort());
1067 hostBrev.setLastSeenTime(timestampB);
1068 hostBrev.freeze();
1069 assertEquals(hostBrev, topology.getHostEvent(macB));
1070
1071
1072 LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple());
1073 assertEquals(linkA, linkAInTopo);
1074 assertTrue(linkAInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001075 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
1076 linkAInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001077
1078 LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple());
1079 assertEquals(linkB, linkBInTopo);
1080 assertTrue(linkBInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001081 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
1082 linkBInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001083
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001084 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001085 // hostB should be added with reduced attachment points
1086 List<HostEvent> apiAddedHostEvents
1087 = TestUtils.getField(theTopologyManager, "apiAddedHostEvents");
1088 assertThat(apiAddedHostEvents, hasItem(hostBrev));
1089
1090 // hostA should not be ignored
1091 List<HostEvent> apiRemovedHostEvents
1092 = TestUtils.getField(theTopologyManager, "apiRemovedHostEvents");
1093 assertThat(apiRemovedHostEvents, not(hasItem(hostA)));
1094
1095 List<LinkEvent> apiAddedLinkEvents
1096 = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents");
1097 assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB));
1098 }
1099
1100 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001101 * Tests adding and moving of a Host, and the topology replica
1102 * transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001103 */
1104 @Test
1105 public void testAddHostMove() {
1106 setupTopologyManager();
1107
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001108 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001109 sw.createStringAttribute("foo", "bar");
1110
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001111 final PortNumber portNumberA = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001112 PortEvent portA = new PortEvent(DPID_1, portNumberA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001113 portA.createStringAttribute("fuzz", "buzz");
1114
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001115 final PortNumber portNumberB = PortNumber.uint32(3);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001116 PortEvent portB = new PortEvent(DPID_1, portNumberB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001117 portB.createStringAttribute("fizz", "buz");
1118
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001119 final PortNumber portNumberC = PortNumber.uint32(4);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001120 PortEvent portC = new PortEvent(DPID_1, portNumberC);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001121 portC.createStringAttribute("fizz", "buz");
1122
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001123 TestUtils.callMethod(theTopologyManager, "addSwitch",
1124 SwitchEvent.class, sw);
1125 TestUtils.callMethod(theTopologyManager, "addPort",
1126 PortEvent.class, portA);
1127 TestUtils.callMethod(theTopologyManager, "addPort",
1128 PortEvent.class, portB);
1129 TestUtils.callMethod(theTopologyManager, "addPort",
1130 PortEvent.class, portC);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001131
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001132 // Add hostA attached to a Port which already has a Link
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001133 final MACAddress macA = MACAddress.valueOf(666L);
1134 HostEvent hostA = new HostEvent(macA);
1135 hostA.addAttachmentPoint(portA.getSwitchPort());
1136 final long timestampA = 392893200L;
1137 hostA.setLastSeenTime(timestampA);
1138
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001139 TestUtils.callMethod(theTopologyManager, "addHost",
1140 HostEvent.class, hostA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001141
1142
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001143 // Check the topology structure
1144 TopologyInternal topology =
1145 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001146 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001147 assertEquals(sw, swInTopo);
1148 assertTrue(swInTopo.isFrozen());
1149 assertEquals("bar", swInTopo.getStringAttribute("foo"));
1150
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001151 final SwitchPort switchPortA = new SwitchPort(DPID_1, portNumberA);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001152 PortEvent portAInTopo = topology.getPortEvent(switchPortA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001153 assertEquals(portA, portAInTopo);
1154 assertTrue(portAInTopo.isFrozen());
1155 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
1156
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001157 final SwitchPort switchPortB = new SwitchPort(DPID_1, portNumberB);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001158 PortEvent portBInTopo = topology.getPortEvent(switchPortB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001159 assertEquals(portB, portBInTopo);
1160 assertTrue(portBInTopo.isFrozen());
1161 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
1162
1163 // hostA expected to be there
1164 assertEquals(hostA, topology.getHostEvent(macA));
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001165 assertEquals(timestampA,
1166 topology.getHostEvent(macA).getLastSeenTime());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001167
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001168 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001169 // hostA should be added
1170 List<HostEvent> apiAddedHostEvents
1171 = TestUtils.getField(theTopologyManager, "apiAddedHostEvents");
1172 assertThat(apiAddedHostEvents, hasItem(hostA));
1173
1174
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001175 // Clear the events before moving the Host
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001176 apiAddedHostEvents.clear();
1177
1178 HostEvent hostAmoved = new HostEvent(macA);
1179 hostAmoved.addAttachmentPoint(portB.getSwitchPort());
1180 final long timestampAmoved = 392893201L;
1181 hostAmoved.setLastSeenTime(timestampAmoved);
1182
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001183 TestUtils.callMethod(theTopologyManager, "addHost",
1184 HostEvent.class, hostAmoved);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001185
1186 assertEquals(hostAmoved, topology.getHostEvent(macA));
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001187 assertEquals(timestampAmoved,
1188 topology.getHostEvent(macA).getLastSeenTime());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001189
1190 // hostA expected to be there with new attachment point
1191 apiAddedHostEvents
1192 = TestUtils.getField(theTopologyManager, "apiAddedHostEvents");
1193 assertThat(apiAddedHostEvents, hasItem(hostAmoved));
1194
1195 // hostA is updated not removed
1196 List<HostEvent> apiRemovedHostEvents
1197 = TestUtils.getField(theTopologyManager, "apiRemovedHostEvents");
1198 assertThat(apiRemovedHostEvents, not(hasItem(hostA)));
1199 }
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001200
1201 /**
1202 * Tests processing of a Switch Mastership Event and the delivery of the
1203 * topology events.
1204 */
1205 @Test
1206 public void testProcessMastershipEvent() {
1207 List<EventEntry<TopologyEvent>> events = new LinkedList<>();
1208 EventEntry<TopologyEvent> eventEntry;
1209 TopologyEvent topologyEvent;
1210
1211 setupTopologyManagerWithEventHandler();
1212
1213 // Prepare the Mastership Event
1214 Role role = Role.MASTER;
1215 MastershipEvent mastershipEvent =
1216 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
1217 topologyEvent = new TopologyEvent(mastershipEvent, ONOS_INSTANCE_ID_1);
1218
1219 // Add the Mastership Event
1220 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1221 topologyEvent);
1222 events.add(eventEntry);
1223
1224 // Process the events
1225 TestUtils.callMethod(theEventHandler, "processEvents",
1226 List.class, events);
1227
1228 // Check the fired events
1229 TopologyEvents topologyEvents = theTopologyListener.topologyEvents;
1230 assertNotNull(topologyEvents);
1231 assertThat(topologyEvents.getAddedMastershipEvents(),
1232 hasItem(mastershipEvent));
1233 theTopologyListener.clear();
1234 }
1235
1236 /**
1237 * Tests processing of a Switch Event, and the delivery of the topology
1238 * events.
1239 *
1240 * We test the following scenario:
1241 * - Switch Mastership Event is processed along with a Switch Event - both
1242 * events should be delivered.
1243 */
1244 @Test
1245 public void testProcessSwitchEvent() {
1246 TopologyEvents topologyEvents;
1247 List<EventEntry<TopologyEvent>> events = new LinkedList<>();
1248 EventEntry<TopologyEvent> eventEntry;
1249 TopologyEvent topologyMastershipEvent;
1250 TopologyEvent topologySwitchEvent;
1251
1252 setupTopologyManagerWithEventHandler();
1253
1254 // Prepare the Mastership Event
1255 Role role = Role.MASTER;
1256 MastershipEvent mastershipEvent =
1257 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
1258 topologyMastershipEvent = new TopologyEvent(mastershipEvent,
1259 ONOS_INSTANCE_ID_1);
1260
1261 // Prepare the Switch Event
1262 SwitchEvent switchEvent = new SwitchEvent(DPID_1);
1263 topologySwitchEvent = new TopologyEvent(switchEvent,
1264 ONOS_INSTANCE_ID_1);
1265
1266 // Add the Mastership Event
1267 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1268 topologyMastershipEvent);
1269 events.add(eventEntry);
1270
1271 // Add the Switch Event
1272 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1273 topologySwitchEvent);
1274 events.add(eventEntry);
1275
1276 // Process the events
1277 TestUtils.callMethod(theEventHandler, "processEvents",
1278 List.class, events);
1279
1280 // Check the fired events
1281 topologyEvents = theTopologyListener.topologyEvents;
1282 assertNotNull(topologyEvents);
1283 assertThat(topologyEvents.getAddedMastershipEvents(),
1284 hasItem(mastershipEvent));
1285 assertThat(topologyEvents.getAddedSwitchEvents(),
1286 hasItem(switchEvent));
1287 theTopologyListener.clear();
1288 }
1289
1290 /**
1291 * Tests processing of a misordered Switch Event, and the delivery of the
1292 * topology events.
1293 *
1294 * We test the following scenario:
1295 * - Only a Switch Event is processed first, later followed by a Switch
1296 * Mastership Event - the Switch Event should be delivered after the
1297 * Switch Mastership Event is processed.
1298 */
1299 @Test
1300 public void testProcessMisorderedSwitchEvent() {
1301 TopologyEvents topologyEvents;
1302 List<EventEntry<TopologyEvent>> events = new LinkedList<>();
1303 EventEntry<TopologyEvent> eventEntry;
1304 TopologyEvent topologyMastershipEvent;
1305 TopologyEvent topologySwitchEvent;
1306
1307 setupTopologyManagerWithEventHandler();
1308
1309 // Prepare the Mastership Event
1310 Role role = Role.MASTER;
1311 MastershipEvent mastershipEvent =
1312 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
1313 topologyMastershipEvent = new TopologyEvent(mastershipEvent,
1314 ONOS_INSTANCE_ID_1);
1315
1316 // Prepare the Switch Event
1317 SwitchEvent switchEvent = new SwitchEvent(DPID_1);
1318 topologySwitchEvent = new TopologyEvent(switchEvent,
1319 ONOS_INSTANCE_ID_1);
1320
1321 // Add the Switch Event
1322 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1323 topologySwitchEvent);
1324 events.add(eventEntry);
1325
1326 // Process the events
1327 TestUtils.callMethod(theEventHandler, "processEvents",
1328 List.class, events);
1329
1330 // Check the fired events: no events should be fired
1331 topologyEvents = theTopologyListener.topologyEvents;
1332 assertNull(topologyEvents);
1333 theTopologyListener.clear();
1334 events.clear();
1335
1336 // Add the Mastership Event
1337 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1338 topologyMastershipEvent);
1339 events.add(eventEntry);
1340
1341 // Process the events
1342 TestUtils.callMethod(theEventHandler, "processEvents",
1343 List.class, events);
1344
1345 // Check the fired events: both events should be fired
1346 topologyEvents = theTopologyListener.topologyEvents;
1347 assertNotNull(topologyEvents);
1348 assertThat(topologyEvents.getAddedMastershipEvents(),
1349 hasItem(mastershipEvent));
1350 assertThat(topologyEvents.getAddedSwitchEvents(),
1351 hasItem(switchEvent));
1352 theTopologyListener.clear();
1353 }
1354
1355 /**
1356 * Tests processing of a Switch Event with Mastership Event from
1357 * another ONOS instance, and the delivery of the topology events.
1358 *
1359 * We test the following scenario:
1360 * - Only a Switch Event is processed first, later followed by a Switch
1361 * Mastership Event from another ONOS instance - only the Switch
1362 * Mastership Event should be delivered.
1363 */
1364 @Test
1365 public void testProcessSwitchEventNoMastership() {
1366 TopologyEvents topologyEvents;
1367 List<EventEntry<TopologyEvent>> events = new LinkedList<>();
1368 EventEntry<TopologyEvent> eventEntry;
1369 TopologyEvent topologyMastershipEvent;
1370 TopologyEvent topologySwitchEvent;
1371
1372 setupTopologyManagerWithEventHandler();
1373
1374 // Prepare the Mastership Event
1375 Role role = Role.MASTER;
1376 MastershipEvent mastershipEvent =
1377 new MastershipEvent(DPID_2, ONOS_INSTANCE_ID_2, role);
1378 topologyMastershipEvent = new TopologyEvent(mastershipEvent,
1379 ONOS_INSTANCE_ID_2);
1380
1381 // Prepare the Switch Event
1382 // NOTE: The originator (ONOS_INSTANCE_ID_1) is NOT the Master
1383 SwitchEvent switchEvent = new SwitchEvent(DPID_2);
1384 topologySwitchEvent = new TopologyEvent(switchEvent,
1385 ONOS_INSTANCE_ID_1);
1386
1387 // Add the Switch Event
1388 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1389 topologySwitchEvent);
1390 events.add(eventEntry);
1391
1392 // Process the events
1393 TestUtils.callMethod(theEventHandler, "processEvents",
1394 List.class, events);
1395
1396 // Check the fired events: no events should be fired
1397 topologyEvents = theTopologyListener.topologyEvents;
1398 assertNull(topologyEvents);
1399 theTopologyListener.clear();
1400 events.clear();
1401
1402 // Add the Mastership Event
1403 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1404 topologyMastershipEvent);
1405 events.add(eventEntry);
1406
1407 // Process the events
1408 TestUtils.callMethod(theEventHandler, "processEvents",
1409 List.class, events);
1410
1411 // Check the fired events: only the Mastership event should be fired
1412 topologyEvents = theTopologyListener.topologyEvents;
1413 assertNotNull(topologyEvents);
1414 assertThat(topologyEvents.getAddedMastershipEvents(),
1415 hasItem(mastershipEvent));
1416 assertThat(topologyEvents.getAddedSwitchEvents(), is(empty()));
1417 theTopologyListener.clear();
1418 }
1419
1420 /**
1421 * Tests processing of Switch Events with Mastership switchover between
1422 * two ONOS instance, and the delivery of the topology events.
1423 *
1424 * We test the following scenario:
1425 * - Initially, a Mastership Event and a Switch Event from one ONOS
1426 * instance are processed - both events should be delivered.
1427 * - Later, a Mastership Event and a Switch event from another ONOS
1428 * instances are processed - both events should be delivered.
1429 * - Finally, a REMOVE Switch Event is received from the first ONOS
1430 * instance - no event should be delivered.
Pavlin Radoslavov054cd592014-08-07 20:57:16 -07001431 *
1432 * @throws RegistryException
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001433 */
1434 @Test
Pavlin Radoslavov054cd592014-08-07 20:57:16 -07001435 public void testProcessSwitchMastershipSwitchover()
1436 throws RegistryException {
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001437 TopologyEvents topologyEvents;
1438 List<EventEntry<TopologyEvent>> events = new LinkedList<>();
1439 EventEntry<TopologyEvent> eventEntry;
1440 TopologyEvent topologyMastershipEvent;
1441 TopologyEvent topologySwitchEvent;
1442
1443 setupTopologyManagerWithEventHandler();
1444
1445 // Prepare the Mastership Event from the first ONOS instance
1446 Role role = Role.MASTER;
1447 MastershipEvent mastershipEvent =
1448 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
1449 topologyMastershipEvent = new TopologyEvent(mastershipEvent,
1450 ONOS_INSTANCE_ID_1);
1451
1452 // Prepare the Switch Event from the first ONOS instance
1453 SwitchEvent switchEvent = new SwitchEvent(DPID_1);
1454 topologySwitchEvent = new TopologyEvent(switchEvent,
1455 ONOS_INSTANCE_ID_1);
1456
1457 // Add the Mastership Event
1458 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1459 topologyMastershipEvent);
1460 events.add(eventEntry);
1461
1462 // Add the Switch Event
1463 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1464 topologySwitchEvent);
1465 events.add(eventEntry);
1466
1467 // Process the events
1468 TestUtils.callMethod(theEventHandler, "processEvents",
1469 List.class, events);
1470
1471 // Check the fired events: both events should be fired
1472 topologyEvents = theTopologyListener.topologyEvents;
1473 assertNotNull(topologyEvents);
1474 assertThat(topologyEvents.getAddedMastershipEvents(),
1475 hasItem(mastershipEvent));
1476 assertThat(topologyEvents.getAddedSwitchEvents(),
1477 hasItem(switchEvent));
1478 theTopologyListener.clear();
1479 events.clear();
1480
1481 //
1482 // Update the Registry Service, so the second ONOS instance is the
1483 // Master.
1484 //
1485 reset(registryService);
Pavlin Radoslavov054cd592014-08-07 20:57:16 -07001486 expect(registryService.getControllerForSwitch(DPID_1.value()))
1487 .andReturn(ONOS_INSTANCE_ID_2.toString()).anyTimes();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001488 replay(registryService);
1489
1490 // Prepare the Mastership Event from the second ONOS instance
1491 role = Role.MASTER;
1492 mastershipEvent = new MastershipEvent(DPID_1,
1493 ONOS_INSTANCE_ID_2, role);
1494 topologyMastershipEvent = new TopologyEvent(mastershipEvent,
1495 ONOS_INSTANCE_ID_2);
1496
1497 // Prepare the Switch Event from second ONOS instance
1498 switchEvent = new SwitchEvent(DPID_1);
1499 topologySwitchEvent = new TopologyEvent(switchEvent,
1500 ONOS_INSTANCE_ID_2);
1501
1502 // Add the Mastership Event
1503 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1504 topologyMastershipEvent);
1505 events.add(eventEntry);
1506
1507 // Add the Switch Event
1508 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1509 topologySwitchEvent);
1510 events.add(eventEntry);
1511
1512 // Process the events
1513 TestUtils.callMethod(theEventHandler, "processEvents",
1514 List.class, events);
1515
1516 // Check the fired events: both events should be fired
1517 topologyEvents = theTopologyListener.topologyEvents;
1518 assertNotNull(topologyEvents);
1519 assertThat(topologyEvents.getAddedMastershipEvents(),
1520 hasItem(mastershipEvent));
1521 assertThat(topologyEvents.getAddedSwitchEvents(),
1522 hasItem(switchEvent));
1523 theTopologyListener.clear();
1524 events.clear();
1525
1526 // Prepare the REMOVE Switch Event from first ONOS instance
1527 switchEvent = new SwitchEvent(DPID_1);
1528 topologySwitchEvent = new TopologyEvent(switchEvent,
1529 ONOS_INSTANCE_ID_1);
1530 // Add the Switch Event
1531 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_REMOVE,
1532 topologySwitchEvent);
1533 events.add(eventEntry);
1534
1535 // Process the events
1536 TestUtils.callMethod(theEventHandler, "processEvents",
1537 List.class, events);
1538
1539 // Check the fired events: no events should be fired
1540 topologyEvents = theTopologyListener.topologyEvents;
1541 assertNull(topologyEvents);
1542 theTopologyListener.clear();
1543 events.clear();
1544 }
weibitf7c31a42014-06-23 16:51:01 -07001545}