blob: 9856366ba39b1229ac3926b99b0630dd2cb718b9 [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;
63 private CopyOnWriteArrayList<ITopologyListener> topologyListeners;
64 private Collection<TopologyEvent> allTopologyEvents;
Pavlin Radoslavovc6236412014-08-01 20:37:46 -070065 private static final OnosInstanceId ONOS_INSTANCE_ID_1 =
66 new OnosInstanceId("ONOS-Instance-ID-1");
67 private static final OnosInstanceId ONOS_INSTANCE_ID_2 =
68 new OnosInstanceId("ONOS-Instance-ID-2");
69 private static final Dpid DPID_1 = new Dpid(1);
70 private static final Dpid DPID_2 = new Dpid(2);
71
72 /**
73 * Topology events listener.
74 */
75 private class TopologyListenerTest implements ITopologyListener {
76 private TopologyEvents topologyEvents;
77
78 @Override
79 public void topologyEvents(TopologyEvents events) {
80 this.topologyEvents = events;
81 }
82
83 /**
84 * Clears the Topology Listener state.
85 */
86 public void clear() {
87 this.topologyEvents = null;
88 }
89 }
weibitf7c31a42014-06-23 16:51:01 -070090
91 @SuppressWarnings("unchecked")
92 @Before
93 public void setUp() throws Exception {
94 // Mock objects for testing
95 datagridService = EasyMock.createNiceMock(IDatagridService.class);
96 dataStoreService = EasyMock.createNiceMock(TopologyDatastore.class);
97 registryService = createMock(IControllerRegistryService.class);
98 eventChannel = EasyMock.createNiceMock(IEventChannel.class);
99
100 expect(datagridService.createChannel(
101 eq(eventChannelName),
102 eq(byte[].class),
103 eq(TopologyEvent.class)))
104 .andReturn(eventChannel).once();
105
106 expect(datagridService.addListener(
107 eq(eventChannelName),
108 anyObject(IEventChannelListener.class),
109 eq(byte[].class),
110 eq(TopologyEvent.class)))
111 .andReturn(eventChannel).once();
112
113 expect(dataStoreService.addSwitch(
114 anyObject(SwitchEvent.class),
115 anyObject(Collection.class)))
116 .andReturn(true).anyTimes();
117
118 expect(dataStoreService.deactivateSwitch(
119 anyObject(SwitchEvent.class),
120 anyObject(Collection.class)))
121 .andReturn(true).anyTimes();
122
123 expect(dataStoreService.addPort(
124 anyObject(PortEvent.class)))
125 .andReturn(true).anyTimes();
126
127 expect(dataStoreService.deactivatePort(
128 anyObject(PortEvent.class)))
129 .andReturn(true).anyTimes();
130
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700131 expect(dataStoreService.addHost(
132 anyObject(HostEvent.class)))
weibitf7c31a42014-06-23 16:51:01 -0700133 .andReturn(true).anyTimes();
134
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700135 expect(dataStoreService.removeHost(
136 anyObject(HostEvent.class)))
weibitf7c31a42014-06-23 16:51:01 -0700137 .andReturn(true).anyTimes();
138
139 expect(dataStoreService.addLink(
140 anyObject(LinkEvent.class)))
141 .andReturn(true).anyTimes();
142
143 expect(dataStoreService.removeLink(
144 anyObject(LinkEvent.class)))
145 .andReturn(true).anyTimes();
146
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700147 // Setup the Registry Service
148 expect(registryService.getOnosInstanceId()).andReturn(ONOS_INSTANCE_ID_1).anyTimes();
149 try {
150 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();
154 } catch (RegistryException ex) {
155 throw new IllegalStateException(ex);
156 }
weibitf7c31a42014-06-23 16:51:01 -0700157
158 allTopologyEvents = new CopyOnWriteArrayList<>();
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700159 expect(eventChannel.getAllEntries())
160 .andReturn(allTopologyEvents).anyTimes();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700161
162 replay(datagridService);
163 replay(registryService);
164 replay(dataStoreService);
165 // replay(eventChannel);
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700166 }
weibitf7c31a42014-06-23 16:51:01 -0700167
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700168 /**
169 * Setup the Topology Manager.
170 */
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700171 private void setupTopologyManager() {
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700172 // Create a TopologyManager object for testing
weibitf7c31a42014-06-23 16:51:01 -0700173 topologyListeners = new CopyOnWriteArrayList<>();
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700174 theTopologyManager = new TopologyManager(registryService,
175 topologyListeners);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700176
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700177 // Replace the eventHandler to prevent the thread from starting
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700178 TestUtils.setField(theTopologyManager, "eventHandler",
179 EasyMock.createNiceMock(TopologyManager.EventHandler.class));
weibitf7c31a42014-06-23 16:51:01 -0700180 theTopologyManager.startup(datagridService);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700181
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700182 // Replace the data store with a mocked object
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700183 TestUtils.setField(theTopologyManager, "datastore", dataStoreService);
weibitf7c31a42014-06-23 16:51:01 -0700184 }
185
weibitf7c31a42014-06-23 16:51:01 -0700186 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700187 * Setup the Topology Manager with the Event Handler.
188 */
189 private void setupTopologyManagerWithEventHandler() {
190 // Create a TopologyManager object for testing
191 topologyListeners = new CopyOnWriteArrayList<>();
192 topologyListeners.add(theTopologyListener);
193 theTopologyManager = new TopologyManager(registryService,
194 topologyListeners);
195 // Allocate the Event Handler, so we can have direct access to it
196 theEventHandler = theTopologyManager.new EventHandler();
197 TestUtils.setField(theTopologyManager, "eventHandler",
198 theEventHandler);
199
200 // Replace the data store with a mocked object
201 TestUtils.setField(theTopologyManager, "datastore", dataStoreService);
202
203 replay(eventChannel);
204 //
205 // NOTE: Uncomment-out the line below if the startup() method needs
206 // to be called for some of the unit tests. For now it is commented-out
207 // to avoid any side effects of starting the eventHandler thread.
208 //
209 // theTopologyManager.startup(datagridService);
210 }
211
212 /**
213 * Test the Switch Mastership Updated Event.
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700214 */
215 @Test
216 public void testPutSwitchMastershipEvent() {
217 // Mock the eventChannel functions first
218 eventChannel.addEntry(anyObject(byte[].class),
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700219 anyObject(TopologyEvent.class));
220 EasyMock.expectLastCall().times(1, 1); // 1 event
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700221 replay(eventChannel);
222
223 setupTopologyManager();
224
225 // Generate a new Switch Mastership event
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700226 Role role = Role.MASTER;
227 MastershipEvent mastershipEvent =
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700228 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700229
230 // Call the topologyManager function for adding the event
231 theTopologyManager.putSwitchMastershipEvent(mastershipEvent);
232
233 // Verify the function calls
234 verify(eventChannel);
235 }
236
237 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700238 * Test the Switch Mastership Removed Event.
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700239 */
240 @Test
241 public void testRemoveSwitchMastershipEvent() {
242 // Mock the eventChannel functions first
243 eventChannel.removeEntry(anyObject(byte[].class));
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700244 EasyMock.expectLastCall().times(1, 1); // 1 event
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700245 replay(eventChannel);
246
247 setupTopologyManager();
248
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700249 // Generate a new Switch Mastership Event
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700250 Role role = Role.MASTER;
251 MastershipEvent mastershipEvent =
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700252 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700253
254 // Call the topologyManager function for removing the event
255 theTopologyManager.removeSwitchMastershipEvent(mastershipEvent);
256
257 // Verify the function calls
258 verify(eventChannel);
259 }
260
261 /**
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700262 * Test the Switch discovered and Port discovered functions.
263 */
264 @Test
265 public void testPutSwitchAndPortDiscoveryEvent() {
266 // Mock the eventChannel functions first
267 eventChannel.addEntry(anyObject(byte[].class),
268 anyObject(TopologyEvent.class));
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700269 EasyMock.expectLastCall().times(3, 3); // (1 Switch + 1 Port), 1 Port
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700270 replay(eventChannel);
271
272 setupTopologyManager();
273
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700274 // Mock Switch has one Port
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700275 PortNumber portNumber = PortNumber.uint32(1);
276
277 // Generate a new Switch Event along with a Port Event
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700278 SwitchEvent switchEvent = new SwitchEvent(DPID_1);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700279
280 Collection<PortEvent> portEvents = new ArrayList<PortEvent>();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700281 portEvents.add(new PortEvent(DPID_1, portNumber));
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700282
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700283 // Call the topologyManager function for adding a Switch
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700284 theTopologyManager.putSwitchDiscoveryEvent(switchEvent, portEvents);
285
286 for (PortEvent portEvent : portEvents) {
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700287 // Call the topologyManager function for adding a Port
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700288 theTopologyManager.putPortDiscoveryEvent(portEvent);
289 }
290
291 // Verify the function calls
292 verify(eventChannel);
293
294 }
295
296 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700297 * Test the Switch and Port removed functions.
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700298 */
299 @Test
300 public void testRemoveSwitchAndPortDiscoveryEvent() {
301 // Mock the eventChannel functions first
302 eventChannel.removeEntry(anyObject(byte[].class));
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700303 EasyMock.expectLastCall().times(2, 2); // 1 Switch, 1 Port
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700304 replay(eventChannel);
305
306 setupTopologyManager();
307
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700308 PortNumber portNumber = PortNumber.uint32(1);
309
310 // Generate a Port Event
311 Collection<PortEvent> portEvents = new ArrayList<PortEvent>();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700312 portEvents.add(new PortEvent(DPID_1, portNumber));
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700313
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700314 // Call the topologyManager function for removing a Port
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700315 for (PortEvent portEvent : portEvents) {
316 theTopologyManager.removePortDiscoveryEvent(portEvent);
317 }
318
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700319 // Call the topologyManager function for removing a Switch
320 SwitchEvent switchEvent = new SwitchEvent(DPID_1);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700321 theTopologyManager.removeSwitchDiscoveryEvent(switchEvent);
322
323 // Verify the function calls
324 verify(eventChannel);
325
326 }
327
328 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700329 * Test the Link discovered function.
weibitf7c31a42014-06-23 16:51:01 -0700330 */
331 @Test
332 public void testPutLinkDiscoveryEvent() {
333 // Mock the eventChannel functions first
334 eventChannel.addEntry(anyObject(byte[].class),
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700335 anyObject(TopologyEvent.class));
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700336 EasyMock.expectLastCall().times(5, 5); // (2 Switch + 2 Port + 1 Link)
weibitf7c31a42014-06-23 16:51:01 -0700337 replay(eventChannel);
338
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700339 setupTopologyManager();
340
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700341 // Generate the Switch and Port Events
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700342 PortNumber portNumber1 = PortNumber.uint32(1);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700343 SwitchEvent switchEvent1 = new SwitchEvent(DPID_1);
weibitf7c31a42014-06-23 16:51:01 -0700344 Collection<PortEvent> portEvents1 = new ArrayList<PortEvent>();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700345 portEvents1.add(new PortEvent(DPID_1, portNumber1));
weibitf7c31a42014-06-23 16:51:01 -0700346
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700347 // Call the topologyManager function for adding a Switch
weibitf7c31a42014-06-23 16:51:01 -0700348 theTopologyManager.putSwitchDiscoveryEvent(switchEvent1, portEvents1);
349
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700350 // Generate the Switch and Port Events
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700351 PortNumber portNumber2 = PortNumber.uint32(2);
352 SwitchEvent switchEvent2 = new SwitchEvent(DPID_2);
weibitf7c31a42014-06-23 16:51:01 -0700353 Collection<PortEvent> portEvents2 = new ArrayList<PortEvent>();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700354 portEvents2.add(new PortEvent(DPID_2, portNumber2));
weibitf7c31a42014-06-23 16:51:01 -0700355
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700356 // Call the topologyManager function for adding a Switch
weibitf7c31a42014-06-23 16:51:01 -0700357 theTopologyManager.putSwitchDiscoveryEvent(switchEvent2, portEvents2);
358
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700359 // Create the Link Event
360 LinkEvent linkEvent =
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700361 new LinkEvent(new SwitchPort(DPID_1, portNumber1),
362 new SwitchPort(DPID_2, portNumber2));
weibitf7c31a42014-06-23 16:51:01 -0700363 theTopologyManager.putLinkDiscoveryEvent(linkEvent);
364
365 // Verify the function calls
366 verify(eventChannel);
367 }
368
369 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700370 * Test the Link removed function.
weibitf7c31a42014-06-23 16:51:01 -0700371 */
372 @Test
373 public void testRemoveLinkDiscoveryEvent() {
374 // Mock the eventChannel functions first
375 eventChannel.removeEntry(anyObject(byte[].class));
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700376 EasyMock.expectLastCall().times(1, 1); // (1 Link)
weibitf7c31a42014-06-23 16:51:01 -0700377 replay(eventChannel);
378
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700379 setupTopologyManager();
380
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700381 // Generate the Switch and Port Events
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700382 PortNumber portNumber1 = PortNumber.uint32(1);
383 SwitchEvent switchEvent1 = new SwitchEvent(DPID_1);
weibitf7c31a42014-06-23 16:51:01 -0700384 Collection<PortEvent> portEvents1 = new ArrayList<PortEvent>();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700385 portEvents1.add(new PortEvent(DPID_1, portNumber1));
weibitf7c31a42014-06-23 16:51:01 -0700386
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700387 // Call the topologyManager function for adding a Switch
weibitf7c31a42014-06-23 16:51:01 -0700388 theTopologyManager.putSwitchDiscoveryEvent(switchEvent1, portEvents1);
389
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700390 // Generate the Switch and Port Events
391 PortNumber portNumber2 = PortNumber.uint32(2);
392 SwitchEvent switchEvent2 = new SwitchEvent(DPID_2);
weibitf7c31a42014-06-23 16:51:01 -0700393 Collection<PortEvent> portEvents2 = new ArrayList<PortEvent>();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700394 portEvents2.add(new PortEvent(DPID_2, portNumber2));
weibitf7c31a42014-06-23 16:51:01 -0700395
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700396 // Call the topologyManager function for adding a Switch
weibitf7c31a42014-06-23 16:51:01 -0700397 theTopologyManager.putSwitchDiscoveryEvent(switchEvent2, portEvents2);
398
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700399 // Remove the Link
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700400 LinkEvent linkEventRemove =
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700401 new LinkEvent(new SwitchPort(DPID_1, portNumber1),
402 new SwitchPort(DPID_2, portNumber2));
weibitf7c31a42014-06-23 16:51:01 -0700403 theTopologyManager.removeLinkDiscoveryEvent(linkEventRemove);
404
405 // Verify the function calls
406 verify(eventChannel);
407 }
408
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700409 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700410 * Test the Host discovered function.
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700411 */
412 @Test
413 public void testPutHostDiscoveryEvent() {
414 // Mock the eventChannel functions first
415 eventChannel.addEntry(anyObject(byte[].class),
416 anyObject(TopologyEvent.class));
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700417 EasyMock.expectLastCall().times(1, 1); // 1 Host
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700418 replay(eventChannel);
419
420 setupTopologyManager();
421
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700422 // Generate a new Host Event
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700423 PortNumber portNumber = PortNumber.uint32(1);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700424 MACAddress hostMac = MACAddress.valueOf("00:AA:11:BB:33:CC");
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700425 SwitchPort sp = new SwitchPort(DPID_1, portNumber);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700426 List<SwitchPort> spLists = new ArrayList<SwitchPort>();
427 spLists.add(sp);
428 HostEvent hostEvent = new HostEvent(hostMac);
429 hostEvent.setAttachmentPoints(spLists);
430
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700431 // Call the topologyManager function for adding a Host
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700432 theTopologyManager.putHostDiscoveryEvent(hostEvent);
433
434 // Verify the function calls
435 verify(eventChannel);
436 }
437
438 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700439 * Test the Host removed function.
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700440 */
441 @Test
442 public void testRemoveHostDiscoveryEvent() {
443 // Mock the eventChannel functions first
444 eventChannel.removeEntry(anyObject(byte[].class));
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700445 EasyMock.expectLastCall().times(1, 1); // 1 Host
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700446 replay(eventChannel);
447
448 setupTopologyManager();
449
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700450 // Generate a new Host Event
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700451 PortNumber portNumber = PortNumber.uint32(1);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700452 MACAddress hostMac = MACAddress.valueOf("00:AA:11:BB:33:CC");
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700453 SwitchPort sp = new SwitchPort(DPID_1, portNumber);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700454 List<SwitchPort> spLists = new ArrayList<SwitchPort>();
455 spLists.add(sp);
456 HostEvent hostEvent = new HostEvent(hostMac);
457 hostEvent.setAttachmentPoints(spLists);
458
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700459 // Call the topologyManager function for removing a Host
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700460 theTopologyManager.removeHostDiscoveryEvent(hostEvent);
461
462 // Verify the function calls
463 verify(eventChannel);
464 }
465
466 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700467 * Tests adding of a Switch Mastership event and the topology replica
468 * transformation.
469 */
470 @Test
471 public void testAddMastershipEvent() {
472 setupTopologyManager();
473
474 // Prepare the event
475 Role role = Role.MASTER;
476 MastershipEvent mastershipEvent =
477 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
478 // Add the event
479 TestUtils.callMethod(theTopologyManager, "addMastershipEvent",
480 MastershipEvent.class, mastershipEvent);
481
482 //
483 // NOTE: The topology itself doesn't contain the Mastership Events,
484 // hence we don't check the topology.
485 //
486
487 // Check the events to be fired
488 List<MastershipEvent> apiAddedMastershipEvents
489 = TestUtils.getField(theTopologyManager,
490 "apiAddedMastershipEvents");
491 assertThat(apiAddedMastershipEvents, hasItem(mastershipEvent));
492 }
493
494 /**
495 * Tests removing of a Switch Mastership event and the topology replica
496 * transformation.
497 */
498 @Test
499 public void testRemoveMastershipEvent() {
500 setupTopologyManager();
501
502 // Prepare the event
503 Role role = Role.MASTER;
504 MastershipEvent mastershipEvent =
505 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
506 // Add the event
507 TestUtils.callMethod(theTopologyManager, "addMastershipEvent",
508 MastershipEvent.class, mastershipEvent);
509
510 // Check the events to be fired
511 List<MastershipEvent> apiAddedMastershipEvents
512 = TestUtils.getField(theTopologyManager,
513 "apiAddedMastershipEvents");
514 assertThat(apiAddedMastershipEvents, hasItem(mastershipEvent));
515
516 // Remove the event
517 TestUtils.callMethod(theTopologyManager, "removeMastershipEvent",
518 MastershipEvent.class,
519 new MastershipEvent(mastershipEvent));
520
521 // Check the events to be fired
522 List<MastershipEvent> apiRemovedMastershipEvents
523 = TestUtils.getField(theTopologyManager,
524 "apiRemovedMastershipEvents");
525 assertThat(apiRemovedMastershipEvents, hasItem(mastershipEvent));
526 }
527
528 /**
529 * Tests adding of a Switch and the topology replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700530 */
531 @Test
532 public void testAddSwitch() {
533 setupTopologyManager();
534
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700535 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700536 sw.createStringAttribute("foo", "bar");
537
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700538 TestUtils.callMethod(theTopologyManager, "addSwitch",
539 SwitchEvent.class, sw);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700540
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700541 // Check the topology structure
542 TopologyInternal topology =
543 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700544 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700545 assertEquals(sw, swInTopo);
546 assertTrue(swInTopo.isFrozen());
547 assertEquals("bar", swInTopo.getStringAttribute("foo"));
548
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700549 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700550 List<SwitchEvent> apiAddedSwitchEvents
551 = TestUtils.getField(theTopologyManager, "apiAddedSwitchEvents");
552 assertThat(apiAddedSwitchEvents, hasItem(sw));
553 }
554
555 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700556 * Tests adding of a Port and the topology replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700557 */
558 @Test
559 public void testAddPort() {
560 setupTopologyManager();
561
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700562 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700563 sw.createStringAttribute("foo", "bar");
564
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700565 final PortNumber portNumber = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700566 PortEvent port = new PortEvent(DPID_1, portNumber);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700567 port.createStringAttribute("fuzz", "buzz");
568
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700569 TestUtils.callMethod(theTopologyManager, "addSwitch",
570 SwitchEvent.class, sw);
571 TestUtils.callMethod(theTopologyManager, "addPort",
572 PortEvent.class, port);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700573
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700574 // Check the topology structure
575 TopologyInternal topology =
576 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700577 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700578 assertEquals(sw, swInTopo);
579 assertTrue(swInTopo.isFrozen());
580 assertEquals("bar", swInTopo.getStringAttribute("foo"));
581
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700582 final SwitchPort switchPort = new SwitchPort(DPID_1, portNumber);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700583 PortEvent portInTopo = topology.getPortEvent(switchPort);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700584 assertEquals(port, portInTopo);
585 assertTrue(portInTopo.isFrozen());
586 assertEquals("buzz", portInTopo.getStringAttribute("fuzz"));
587
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700588 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700589 List<PortEvent> apiAddedPortEvents
590 = TestUtils.getField(theTopologyManager, "apiAddedPortEvents");
591 assertThat(apiAddedPortEvents, hasItem(port));
592 }
593
594 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700595 * Tests removing of a Port followed by removing of a Switch,
596 * and the topology replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700597 */
598 @Test
599 public void testRemovePortThenSwitch() {
600 setupTopologyManager();
601
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700602 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700603 sw.createStringAttribute("foo", "bar");
604
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700605 final PortNumber portNumber = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700606 PortEvent port = new PortEvent(DPID_1, portNumber);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700607 port.createStringAttribute("fuzz", "buzz");
608
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700609 TestUtils.callMethod(theTopologyManager, "addSwitch",
610 SwitchEvent.class, sw);
611 TestUtils.callMethod(theTopologyManager, "addPort",
612 PortEvent.class, port);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700613
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700614 // Check the topology structure
615 TopologyInternal topology =
616 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700617 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700618 assertEquals(sw, swInTopo);
619 assertTrue(swInTopo.isFrozen());
620 assertEquals("bar", swInTopo.getStringAttribute("foo"));
621
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700622 final SwitchPort switchPort = new SwitchPort(DPID_1, portNumber);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700623 PortEvent portInTopo = topology.getPortEvent(switchPort);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700624 assertEquals(port, portInTopo);
625 assertTrue(portInTopo.isFrozen());
626 assertEquals("buzz", portInTopo.getStringAttribute("fuzz"));
627
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700628 // Remove in proper order
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700629 TestUtils.callMethod(theTopologyManager, "removePort",
630 PortEvent.class, new PortEvent(port));
631 TestUtils.callMethod(theTopologyManager, "removeSwitch",
632 SwitchEvent.class, new SwitchEvent(sw));
633
634
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700635 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700636 List<PortEvent> apiRemovedPortEvents
637 = TestUtils.getField(theTopologyManager, "apiRemovedPortEvents");
638 assertThat(apiRemovedPortEvents, hasItem(port));
639 List<SwitchEvent> apiRemovedSwitchEvents
640 = TestUtils.getField(theTopologyManager, "apiRemovedSwitchEvents");
641 assertThat(apiRemovedSwitchEvents, hasItem(sw));
642 }
643
644 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700645 * Tests removing of a Switch without removing of a Port,
646 * and the topology replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700647 */
648 @Test
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700649 public void testRemoveSwitchWithoutPortRemoval() {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700650 setupTopologyManager();
651
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700652 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700653 sw.createStringAttribute("foo", "bar");
654
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700655 final PortNumber portNumber = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700656 PortEvent port = new PortEvent(DPID_1, portNumber);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700657 port.createStringAttribute("fuzz", "buzz");
658
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700659 TestUtils.callMethod(theTopologyManager, "addSwitch",
660 SwitchEvent.class, sw);
661 TestUtils.callMethod(theTopologyManager, "addPort",
662 PortEvent.class, port);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700663
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700664 // Check the topology structure
665 TopologyInternal topology =
666 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700667 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700668 assertEquals(sw, swInTopo);
669 assertTrue(swInTopo.isFrozen());
670 assertEquals("bar", swInTopo.getStringAttribute("foo"));
671
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700672 final SwitchPort switchPort = new SwitchPort(DPID_1, portNumber);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700673 PortEvent portInTopo = topology.getPortEvent(switchPort);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700674 assertEquals(port, portInTopo);
675 assertTrue(portInTopo.isFrozen());
676 assertEquals("buzz", portInTopo.getStringAttribute("fuzz"));
677
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700678 // Remove in in-proper order
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700679// TestUtils.callMethod(theTopologyManager, "removePort",
680// PortEvent.class, new PortEvent(port));
681 TestUtils.callMethod(theTopologyManager, "removeSwitch",
682 SwitchEvent.class, new SwitchEvent(sw));
683
684
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700685 // Check the events to be fired
686 // The outcome should be the same as #testRemovePortThenSwitch
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700687 List<PortEvent> apiRemovedPortEvents
688 = TestUtils.getField(theTopologyManager, "apiRemovedPortEvents");
689 assertThat(apiRemovedPortEvents, hasItem(port));
690 List<SwitchEvent> apiRemovedSwitchEvents
691 = TestUtils.getField(theTopologyManager, "apiRemovedSwitchEvents");
692 assertThat(apiRemovedSwitchEvents, hasItem(sw));
693 }
694
695 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700696 * Tests adding of a Link and the topology replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700697 */
698 @Test
699 public void testAddLink() {
700 setupTopologyManager();
701
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700702 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700703 sw.createStringAttribute("foo", "bar");
704
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700705 final PortNumber portNumberA = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700706 PortEvent portA = new PortEvent(DPID_1, portNumberA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700707 portA.createStringAttribute("fuzz", "buzz");
708
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700709 final PortNumber portNumberB = PortNumber.uint32(3);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700710 PortEvent portB = new PortEvent(DPID_1, portNumberB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700711 portB.createStringAttribute("fizz", "buz");
712
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700713 LinkEvent linkA = new LinkEvent(portA.getSwitchPort(),
714 portB.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700715 linkA.createStringAttribute(TopologyElement.TYPE,
716 TopologyElement.TYPE_OPTICAL_LAYER);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700717 LinkEvent linkB = new LinkEvent(portB.getSwitchPort(),
718 portA.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700719 linkB.createStringAttribute(TopologyElement.TYPE,
720 TopologyElement.TYPE_OPTICAL_LAYER);
721
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700722 TestUtils.callMethod(theTopologyManager, "addSwitch",
723 SwitchEvent.class, sw);
724 TestUtils.callMethod(theTopologyManager, "addPort",
725 PortEvent.class, portA);
726 TestUtils.callMethod(theTopologyManager, "addPort",
727 PortEvent.class, portB);
728 TestUtils.callMethod(theTopologyManager, "addLink",
729 LinkEvent.class, linkA);
730 TestUtils.callMethod(theTopologyManager, "addLink",
731 LinkEvent.class, linkB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700732
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700733 // Check the topology structure
734 TopologyInternal topology =
735 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700736 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700737 assertEquals(sw, swInTopo);
738 assertTrue(swInTopo.isFrozen());
739 assertEquals("bar", swInTopo.getStringAttribute("foo"));
740
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700741 final SwitchPort switchPortA = new SwitchPort(DPID_1, portNumberA);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700742 PortEvent portAInTopo = topology.getPortEvent(switchPortA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700743 assertEquals(portA, portAInTopo);
744 assertTrue(portAInTopo.isFrozen());
745 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
746
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700747 final SwitchPort switchPortB = new SwitchPort(DPID_1, portNumberB);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700748 PortEvent portBInTopo = topology.getPortEvent(switchPortB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700749 assertEquals(portB, portBInTopo);
750 assertTrue(portBInTopo.isFrozen());
751 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
752
753 LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple());
754 assertEquals(linkA, linkAInTopo);
755 assertTrue(linkAInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700756 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
757 linkAInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700758
759 LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple());
760 assertEquals(linkB, linkBInTopo);
761 assertTrue(linkBInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700762 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
763 linkBInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700764
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700765 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700766 List<LinkEvent> apiAddedLinkEvents
767 = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents");
768 assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB));
769 }
770
771 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700772 * Tests removing of a Link without removing of a Host, and the topology
773 * replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700774 */
775 @Test
776 public void testAddLinkKickingOffHost() {
777 setupTopologyManager();
778
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700779 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700780 sw.createStringAttribute("foo", "bar");
781
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700782 final PortNumber portNumberA = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700783 PortEvent portA = new PortEvent(DPID_1, portNumberA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700784 portA.createStringAttribute("fuzz", "buzz");
785
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700786 final PortNumber portNumberB = PortNumber.uint32(3);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700787 PortEvent portB = new PortEvent(DPID_1, portNumberB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700788 portB.createStringAttribute("fizz", "buz");
789
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700790 final PortNumber portNumberC = PortNumber.uint32(4);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700791 PortEvent portC = new PortEvent(DPID_1, portNumberC);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700792 portC.createStringAttribute("fizz", "buz");
793
794 final MACAddress macA = MACAddress.valueOf(666L);
795 HostEvent hostA = new HostEvent(macA);
796 hostA.addAttachmentPoint(portA.getSwitchPort());
797 final long timestampA = 392893200L;
798 hostA.setLastSeenTime(timestampA);
799
800 final MACAddress macB = MACAddress.valueOf(999L);
801 HostEvent hostB = new HostEvent(macB);
802 hostB.addAttachmentPoint(portB.getSwitchPort());
803 hostB.addAttachmentPoint(portC.getSwitchPort());
804 final long timestampB = 392893201L;
805 hostB.setLastSeenTime(timestampB);
806
807
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700808 LinkEvent linkA = new LinkEvent(portA.getSwitchPort(),
809 portB.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700810 linkA.createStringAttribute(TopologyElement.TYPE,
811 TopologyElement.TYPE_OPTICAL_LAYER);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700812 LinkEvent linkB = new LinkEvent(portB.getSwitchPort(),
813 portA.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700814 linkB.createStringAttribute(TopologyElement.TYPE,
815 TopologyElement.TYPE_OPTICAL_LAYER);
816
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700817 TestUtils.callMethod(theTopologyManager, "addSwitch",
818 SwitchEvent.class, sw);
819 TestUtils.callMethod(theTopologyManager, "addPort",
820 PortEvent.class, portA);
821 TestUtils.callMethod(theTopologyManager, "addPort",
822 PortEvent.class, portB);
823 TestUtils.callMethod(theTopologyManager, "addPort",
824 PortEvent.class, portC);
825 TestUtils.callMethod(theTopologyManager, "addHost",
826 HostEvent.class, hostA);
827 TestUtils.callMethod(theTopologyManager, "addHost",
828 HostEvent.class, hostB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700829
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700830 TestUtils.callMethod(theTopologyManager, "addLink",
831 LinkEvent.class, linkA);
832 TestUtils.callMethod(theTopologyManager, "addLink",
833 LinkEvent.class, linkB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700834
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700835 // Check the topology structure
836 TopologyInternal topology =
837 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700838 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700839 assertEquals(sw, swInTopo);
840 assertTrue(swInTopo.isFrozen());
841 assertEquals("bar", swInTopo.getStringAttribute("foo"));
842
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700843 final SwitchPort switchPortA = new SwitchPort(DPID_1, portNumberA);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700844 PortEvent portAInTopo = topology.getPortEvent(switchPortA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700845 assertEquals(portA, portAInTopo);
846 assertTrue(portAInTopo.isFrozen());
847 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
848
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700849 final SwitchPort switchPortB = new SwitchPort(DPID_1, portNumberB);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700850 PortEvent portBInTopo = topology.getPortEvent(switchPortB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700851 assertEquals(portB, portBInTopo);
852 assertTrue(portBInTopo.isFrozen());
853 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
854
855 // hostA expected to be removed
856 assertNull(topology.getHostEvent(macA));
857 // hostB expected to be there with reduced attachment point
858 HostEvent hostBrev = new HostEvent(macB);
859 hostBrev.addAttachmentPoint(portC.getSwitchPort());
860 hostBrev.setLastSeenTime(timestampB);
861 hostBrev.freeze();
862 assertEquals(hostBrev, topology.getHostEvent(macB));
863
864
865 LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple());
866 assertEquals(linkA, linkAInTopo);
867 assertTrue(linkAInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700868 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
869 linkAInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700870
871 LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple());
872 assertEquals(linkB, linkBInTopo);
873 assertTrue(linkBInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700874 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
875 linkBInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700876
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700877 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700878 List<HostEvent> apiAddedHostEvents
879 = TestUtils.getField(theTopologyManager, "apiAddedHostEvents");
880 assertThat(apiAddedHostEvents, hasItem(hostBrev));
881
882 List<HostEvent> apiRemovedHostEvents
883 = TestUtils.getField(theTopologyManager, "apiRemovedHostEvents");
884 assertThat(apiRemovedHostEvents, hasItem(hostA));
885 List<LinkEvent> apiAddedLinkEvents
886 = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents");
887 assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB));
888 }
889
890 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700891 * Tests removing of a Link and the topology replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700892 */
893 @Test
894 public void testRemoveLink() {
895 setupTopologyManager();
896
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700897 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700898 sw.createStringAttribute("foo", "bar");
899
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700900 final PortNumber portNumberA = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700901 PortEvent portA = new PortEvent(DPID_1, portNumberA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700902 portA.createStringAttribute("fuzz", "buzz");
903
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700904 final PortNumber portNumberB = PortNumber.uint32(3);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700905 PortEvent portB = new PortEvent(DPID_1, portNumberB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700906 portB.createStringAttribute("fizz", "buz");
907
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700908 LinkEvent linkA = new LinkEvent(portA.getSwitchPort(),
909 portB.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700910 linkA.createStringAttribute(TopologyElement.TYPE,
911 TopologyElement.TYPE_OPTICAL_LAYER);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700912 LinkEvent linkB = new LinkEvent(portB.getSwitchPort(),
913 portA.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700914 linkB.createStringAttribute(TopologyElement.TYPE,
915 TopologyElement.TYPE_OPTICAL_LAYER);
916
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700917 TestUtils.callMethod(theTopologyManager, "addSwitch",
918 SwitchEvent.class, sw);
919 TestUtils.callMethod(theTopologyManager, "addPort",
920 PortEvent.class, portA);
921 TestUtils.callMethod(theTopologyManager, "addPort",
922 PortEvent.class, portB);
923 TestUtils.callMethod(theTopologyManager, "addLink",
924 LinkEvent.class, linkA);
925 TestUtils.callMethod(theTopologyManager, "addLink",
926 LinkEvent.class, linkB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700927
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700928 // Check the topology structure
929 TopologyInternal topology =
930 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700931 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700932 assertEquals(sw, swInTopo);
933 assertTrue(swInTopo.isFrozen());
934 assertEquals("bar", swInTopo.getStringAttribute("foo"));
935
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700936 final SwitchPort switchPortA = new SwitchPort(DPID_1, portNumberA);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700937 PortEvent portAInTopo = topology.getPortEvent(switchPortA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700938 assertEquals(portA, portAInTopo);
939 assertTrue(portAInTopo.isFrozen());
940 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
941
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700942 final SwitchPort switchPortB = new SwitchPort(DPID_1, portNumberB);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700943 PortEvent portBInTopo = topology.getPortEvent(switchPortB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700944 assertEquals(portB, portBInTopo);
945 assertTrue(portBInTopo.isFrozen());
946 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
947
948 LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple());
949 assertEquals(linkA, linkAInTopo);
950 assertTrue(linkAInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700951 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
952 linkAInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700953
954
955 LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple());
956 assertEquals(linkB, linkBInTopo);
957 assertTrue(linkBInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700958 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
959 linkBInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700960
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700961 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700962 // FIXME if link flapped (linkA in this scenario),
963 // linkA appears in both removed and added is this expected behavior?
964 List<LinkEvent> apiAddedLinkEvents
965 = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents");
966 assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB));
967
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700968 // Clear the events before removing the link
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700969 apiAddedLinkEvents.clear();
970
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700971 // Remove the link
972 TestUtils.callMethod(theTopologyManager, "removeLink",
973 LinkEvent.class, new LinkEvent(linkA));
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700974
975 LinkEvent linkANotInTopo = topology.getLinkEvent(linkA.getLinkTuple());
976 assertNull(linkANotInTopo);
977
978 List<LinkEvent> apiRemovedLinkEvents
979 = TestUtils.getField(theTopologyManager, "apiRemovedLinkEvents");
980 assertThat(apiRemovedLinkEvents, hasItem(linkA));
981 }
982
983 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700984 * Tests adding of a Host without adding of a Link, and the topology
985 * replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700986 */
987 @Test
988 public void testAddHostIgnoredByLink() {
989 setupTopologyManager();
990
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700991 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700992 sw.createStringAttribute("foo", "bar");
993
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700994 final PortNumber portNumberA = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700995 PortEvent portA = new PortEvent(DPID_1, portNumberA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700996 portA.createStringAttribute("fuzz", "buzz");
997
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700998 final PortNumber portNumberB = PortNumber.uint32(3);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700999 PortEvent portB = new PortEvent(DPID_1, portNumberB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001000 portB.createStringAttribute("fizz", "buz");
1001
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001002 final PortNumber portNumberC = PortNumber.uint32(4);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001003 PortEvent portC = new PortEvent(DPID_1, portNumberC);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001004 portC.createStringAttribute("fizz", "buz");
1005
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001006 LinkEvent linkA = new LinkEvent(portA.getSwitchPort(),
1007 portB.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001008 linkA.createStringAttribute(TopologyElement.TYPE,
1009 TopologyElement.TYPE_OPTICAL_LAYER);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001010 LinkEvent linkB = new LinkEvent(portB.getSwitchPort(),
1011 portA.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001012 linkB.createStringAttribute(TopologyElement.TYPE,
1013 TopologyElement.TYPE_OPTICAL_LAYER);
1014
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001015 TestUtils.callMethod(theTopologyManager, "addSwitch",
1016 SwitchEvent.class, sw);
1017 TestUtils.callMethod(theTopologyManager, "addPort",
1018 PortEvent.class, portA);
1019 TestUtils.callMethod(theTopologyManager, "addPort",
1020 PortEvent.class, portB);
1021 TestUtils.callMethod(theTopologyManager, "addPort",
1022 PortEvent.class, portC);
1023 TestUtils.callMethod(theTopologyManager, "addLink",
1024 LinkEvent.class, linkA);
1025 TestUtils.callMethod(theTopologyManager, "addLink",
1026 LinkEvent.class, linkB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001027
1028 // Add hostA attached to a port which already has a link
1029 final MACAddress macA = MACAddress.valueOf(666L);
1030 HostEvent hostA = new HostEvent(macA);
1031 hostA.addAttachmentPoint(portA.getSwitchPort());
1032 final long timestampA = 392893200L;
1033 hostA.setLastSeenTime(timestampA);
1034
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001035 TestUtils.callMethod(theTopologyManager, "addHost",
1036 HostEvent.class, hostA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001037
1038 // Add hostB attached to multiple ports,
1039 // some of them which already has a link
1040 final MACAddress macB = MACAddress.valueOf(999L);
1041 HostEvent hostB = new HostEvent(macB);
1042 hostB.addAttachmentPoint(portB.getSwitchPort());
1043 hostB.addAttachmentPoint(portC.getSwitchPort());
1044 final long timestampB = 392893201L;
1045 hostB.setLastSeenTime(timestampB);
1046
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001047 TestUtils.callMethod(theTopologyManager, "addHost",
1048 HostEvent.class, hostB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001049
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001050 // Check the topology structure
1051 TopologyInternal topology =
1052 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001053 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001054 assertEquals(sw, swInTopo);
1055 assertTrue(swInTopo.isFrozen());
1056 assertEquals("bar", swInTopo.getStringAttribute("foo"));
1057
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001058 final SwitchPort switchPortA = new SwitchPort(DPID_1, portNumberA);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001059 PortEvent portAInTopo = topology.getPortEvent(switchPortA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001060 assertEquals(portA, portAInTopo);
1061 assertTrue(portAInTopo.isFrozen());
1062 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
1063
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001064 final SwitchPort switchPortB = new SwitchPort(DPID_1, portNumberB);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001065 PortEvent portBInTopo = topology.getPortEvent(switchPortB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001066 assertEquals(portB, portBInTopo);
1067 assertTrue(portBInTopo.isFrozen());
1068 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
1069
1070 // hostA expected to be completely ignored
1071 assertNull(topology.getHostEvent(macA));
1072 // hostB expected to be there with reduced attachment point
1073 HostEvent hostBrev = new HostEvent(macB);
1074 hostBrev.addAttachmentPoint(portC.getSwitchPort());
1075 hostBrev.setLastSeenTime(timestampB);
1076 hostBrev.freeze();
1077 assertEquals(hostBrev, topology.getHostEvent(macB));
1078
1079
1080 LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple());
1081 assertEquals(linkA, linkAInTopo);
1082 assertTrue(linkAInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001083 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
1084 linkAInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001085
1086 LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple());
1087 assertEquals(linkB, linkBInTopo);
1088 assertTrue(linkBInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001089 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
1090 linkBInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001091
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001092 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001093 // hostB should be added with reduced attachment points
1094 List<HostEvent> apiAddedHostEvents
1095 = TestUtils.getField(theTopologyManager, "apiAddedHostEvents");
1096 assertThat(apiAddedHostEvents, hasItem(hostBrev));
1097
1098 // hostA should not be ignored
1099 List<HostEvent> apiRemovedHostEvents
1100 = TestUtils.getField(theTopologyManager, "apiRemovedHostEvents");
1101 assertThat(apiRemovedHostEvents, not(hasItem(hostA)));
1102
1103 List<LinkEvent> apiAddedLinkEvents
1104 = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents");
1105 assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB));
1106 }
1107
1108 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001109 * Tests adding and moving of a Host, and the topology replica
1110 * transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001111 */
1112 @Test
1113 public void testAddHostMove() {
1114 setupTopologyManager();
1115
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001116 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001117 sw.createStringAttribute("foo", "bar");
1118
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001119 final PortNumber portNumberA = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001120 PortEvent portA = new PortEvent(DPID_1, portNumberA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001121 portA.createStringAttribute("fuzz", "buzz");
1122
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001123 final PortNumber portNumberB = PortNumber.uint32(3);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001124 PortEvent portB = new PortEvent(DPID_1, portNumberB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001125 portB.createStringAttribute("fizz", "buz");
1126
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001127 final PortNumber portNumberC = PortNumber.uint32(4);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001128 PortEvent portC = new PortEvent(DPID_1, portNumberC);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001129 portC.createStringAttribute("fizz", "buz");
1130
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001131 TestUtils.callMethod(theTopologyManager, "addSwitch",
1132 SwitchEvent.class, sw);
1133 TestUtils.callMethod(theTopologyManager, "addPort",
1134 PortEvent.class, portA);
1135 TestUtils.callMethod(theTopologyManager, "addPort",
1136 PortEvent.class, portB);
1137 TestUtils.callMethod(theTopologyManager, "addPort",
1138 PortEvent.class, portC);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001139
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001140 // Add hostA attached to a Port which already has a Link
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001141 final MACAddress macA = MACAddress.valueOf(666L);
1142 HostEvent hostA = new HostEvent(macA);
1143 hostA.addAttachmentPoint(portA.getSwitchPort());
1144 final long timestampA = 392893200L;
1145 hostA.setLastSeenTime(timestampA);
1146
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001147 TestUtils.callMethod(theTopologyManager, "addHost",
1148 HostEvent.class, hostA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001149
1150
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001151 // Check the topology structure
1152 TopologyInternal topology =
1153 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001154 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001155 assertEquals(sw, swInTopo);
1156 assertTrue(swInTopo.isFrozen());
1157 assertEquals("bar", swInTopo.getStringAttribute("foo"));
1158
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001159 final SwitchPort switchPortA = new SwitchPort(DPID_1, portNumberA);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001160 PortEvent portAInTopo = topology.getPortEvent(switchPortA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001161 assertEquals(portA, portAInTopo);
1162 assertTrue(portAInTopo.isFrozen());
1163 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
1164
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001165 final SwitchPort switchPortB = new SwitchPort(DPID_1, portNumberB);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001166 PortEvent portBInTopo = topology.getPortEvent(switchPortB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001167 assertEquals(portB, portBInTopo);
1168 assertTrue(portBInTopo.isFrozen());
1169 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
1170
1171 // hostA expected to be there
1172 assertEquals(hostA, topology.getHostEvent(macA));
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001173 assertEquals(timestampA,
1174 topology.getHostEvent(macA).getLastSeenTime());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001175
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001176 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001177 // hostA should be added
1178 List<HostEvent> apiAddedHostEvents
1179 = TestUtils.getField(theTopologyManager, "apiAddedHostEvents");
1180 assertThat(apiAddedHostEvents, hasItem(hostA));
1181
1182
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001183 // Clear the events before moving the Host
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001184 apiAddedHostEvents.clear();
1185
1186 HostEvent hostAmoved = new HostEvent(macA);
1187 hostAmoved.addAttachmentPoint(portB.getSwitchPort());
1188 final long timestampAmoved = 392893201L;
1189 hostAmoved.setLastSeenTime(timestampAmoved);
1190
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001191 TestUtils.callMethod(theTopologyManager, "addHost",
1192 HostEvent.class, hostAmoved);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001193
1194 assertEquals(hostAmoved, topology.getHostEvent(macA));
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001195 assertEquals(timestampAmoved,
1196 topology.getHostEvent(macA).getLastSeenTime());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001197
1198 // hostA expected to be there with new attachment point
1199 apiAddedHostEvents
1200 = TestUtils.getField(theTopologyManager, "apiAddedHostEvents");
1201 assertThat(apiAddedHostEvents, hasItem(hostAmoved));
1202
1203 // hostA is updated not removed
1204 List<HostEvent> apiRemovedHostEvents
1205 = TestUtils.getField(theTopologyManager, "apiRemovedHostEvents");
1206 assertThat(apiRemovedHostEvents, not(hasItem(hostA)));
1207 }
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001208
1209 /**
1210 * Tests processing of a Switch Mastership Event and the delivery of the
1211 * topology events.
1212 */
1213 @Test
1214 public void testProcessMastershipEvent() {
1215 List<EventEntry<TopologyEvent>> events = new LinkedList<>();
1216 EventEntry<TopologyEvent> eventEntry;
1217 TopologyEvent topologyEvent;
1218
1219 setupTopologyManagerWithEventHandler();
1220
1221 // Prepare the Mastership Event
1222 Role role = Role.MASTER;
1223 MastershipEvent mastershipEvent =
1224 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
1225 topologyEvent = new TopologyEvent(mastershipEvent, ONOS_INSTANCE_ID_1);
1226
1227 // Add the Mastership Event
1228 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1229 topologyEvent);
1230 events.add(eventEntry);
1231
1232 // Process the events
1233 TestUtils.callMethod(theEventHandler, "processEvents",
1234 List.class, events);
1235
1236 // Check the fired events
1237 TopologyEvents topologyEvents = theTopologyListener.topologyEvents;
1238 assertNotNull(topologyEvents);
1239 assertThat(topologyEvents.getAddedMastershipEvents(),
1240 hasItem(mastershipEvent));
1241 theTopologyListener.clear();
1242 }
1243
1244 /**
1245 * Tests processing of a Switch Event, and the delivery of the topology
1246 * events.
1247 *
1248 * We test the following scenario:
1249 * - Switch Mastership Event is processed along with a Switch Event - both
1250 * events should be delivered.
1251 */
1252 @Test
1253 public void testProcessSwitchEvent() {
1254 TopologyEvents topologyEvents;
1255 List<EventEntry<TopologyEvent>> events = new LinkedList<>();
1256 EventEntry<TopologyEvent> eventEntry;
1257 TopologyEvent topologyMastershipEvent;
1258 TopologyEvent topologySwitchEvent;
1259
1260 setupTopologyManagerWithEventHandler();
1261
1262 // Prepare the Mastership Event
1263 Role role = Role.MASTER;
1264 MastershipEvent mastershipEvent =
1265 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
1266 topologyMastershipEvent = new TopologyEvent(mastershipEvent,
1267 ONOS_INSTANCE_ID_1);
1268
1269 // Prepare the Switch Event
1270 SwitchEvent switchEvent = new SwitchEvent(DPID_1);
1271 topologySwitchEvent = new TopologyEvent(switchEvent,
1272 ONOS_INSTANCE_ID_1);
1273
1274 // Add the Mastership Event
1275 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1276 topologyMastershipEvent);
1277 events.add(eventEntry);
1278
1279 // Add the Switch Event
1280 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1281 topologySwitchEvent);
1282 events.add(eventEntry);
1283
1284 // Process the events
1285 TestUtils.callMethod(theEventHandler, "processEvents",
1286 List.class, events);
1287
1288 // Check the fired events
1289 topologyEvents = theTopologyListener.topologyEvents;
1290 assertNotNull(topologyEvents);
1291 assertThat(topologyEvents.getAddedMastershipEvents(),
1292 hasItem(mastershipEvent));
1293 assertThat(topologyEvents.getAddedSwitchEvents(),
1294 hasItem(switchEvent));
1295 theTopologyListener.clear();
1296 }
1297
1298 /**
1299 * Tests processing of a misordered Switch Event, and the delivery of the
1300 * topology events.
1301 *
1302 * We test the following scenario:
1303 * - Only a Switch Event is processed first, later followed by a Switch
1304 * Mastership Event - the Switch Event should be delivered after the
1305 * Switch Mastership Event is processed.
1306 */
1307 @Test
1308 public void testProcessMisorderedSwitchEvent() {
1309 TopologyEvents topologyEvents;
1310 List<EventEntry<TopologyEvent>> events = new LinkedList<>();
1311 EventEntry<TopologyEvent> eventEntry;
1312 TopologyEvent topologyMastershipEvent;
1313 TopologyEvent topologySwitchEvent;
1314
1315 setupTopologyManagerWithEventHandler();
1316
1317 // Prepare the Mastership Event
1318 Role role = Role.MASTER;
1319 MastershipEvent mastershipEvent =
1320 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
1321 topologyMastershipEvent = new TopologyEvent(mastershipEvent,
1322 ONOS_INSTANCE_ID_1);
1323
1324 // Prepare the Switch Event
1325 SwitchEvent switchEvent = new SwitchEvent(DPID_1);
1326 topologySwitchEvent = new TopologyEvent(switchEvent,
1327 ONOS_INSTANCE_ID_1);
1328
1329 // Add the Switch Event
1330 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1331 topologySwitchEvent);
1332 events.add(eventEntry);
1333
1334 // Process the events
1335 TestUtils.callMethod(theEventHandler, "processEvents",
1336 List.class, events);
1337
1338 // Check the fired events: no events should be fired
1339 topologyEvents = theTopologyListener.topologyEvents;
1340 assertNull(topologyEvents);
1341 theTopologyListener.clear();
1342 events.clear();
1343
1344 // Add the Mastership Event
1345 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1346 topologyMastershipEvent);
1347 events.add(eventEntry);
1348
1349 // Process the events
1350 TestUtils.callMethod(theEventHandler, "processEvents",
1351 List.class, events);
1352
1353 // Check the fired events: both events should be fired
1354 topologyEvents = theTopologyListener.topologyEvents;
1355 assertNotNull(topologyEvents);
1356 assertThat(topologyEvents.getAddedMastershipEvents(),
1357 hasItem(mastershipEvent));
1358 assertThat(topologyEvents.getAddedSwitchEvents(),
1359 hasItem(switchEvent));
1360 theTopologyListener.clear();
1361 }
1362
1363 /**
1364 * Tests processing of a Switch Event with Mastership Event from
1365 * another ONOS instance, and the delivery of the topology events.
1366 *
1367 * We test the following scenario:
1368 * - Only a Switch Event is processed first, later followed by a Switch
1369 * Mastership Event from another ONOS instance - only the Switch
1370 * Mastership Event should be delivered.
1371 */
1372 @Test
1373 public void testProcessSwitchEventNoMastership() {
1374 TopologyEvents topologyEvents;
1375 List<EventEntry<TopologyEvent>> events = new LinkedList<>();
1376 EventEntry<TopologyEvent> eventEntry;
1377 TopologyEvent topologyMastershipEvent;
1378 TopologyEvent topologySwitchEvent;
1379
1380 setupTopologyManagerWithEventHandler();
1381
1382 // Prepare the Mastership Event
1383 Role role = Role.MASTER;
1384 MastershipEvent mastershipEvent =
1385 new MastershipEvent(DPID_2, ONOS_INSTANCE_ID_2, role);
1386 topologyMastershipEvent = new TopologyEvent(mastershipEvent,
1387 ONOS_INSTANCE_ID_2);
1388
1389 // Prepare the Switch Event
1390 // NOTE: The originator (ONOS_INSTANCE_ID_1) is NOT the Master
1391 SwitchEvent switchEvent = new SwitchEvent(DPID_2);
1392 topologySwitchEvent = new TopologyEvent(switchEvent,
1393 ONOS_INSTANCE_ID_1);
1394
1395 // Add the Switch Event
1396 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1397 topologySwitchEvent);
1398 events.add(eventEntry);
1399
1400 // Process the events
1401 TestUtils.callMethod(theEventHandler, "processEvents",
1402 List.class, events);
1403
1404 // Check the fired events: no events should be fired
1405 topologyEvents = theTopologyListener.topologyEvents;
1406 assertNull(topologyEvents);
1407 theTopologyListener.clear();
1408 events.clear();
1409
1410 // Add the Mastership Event
1411 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1412 topologyMastershipEvent);
1413 events.add(eventEntry);
1414
1415 // Process the events
1416 TestUtils.callMethod(theEventHandler, "processEvents",
1417 List.class, events);
1418
1419 // Check the fired events: only the Mastership event should be fired
1420 topologyEvents = theTopologyListener.topologyEvents;
1421 assertNotNull(topologyEvents);
1422 assertThat(topologyEvents.getAddedMastershipEvents(),
1423 hasItem(mastershipEvent));
1424 assertThat(topologyEvents.getAddedSwitchEvents(), is(empty()));
1425 theTopologyListener.clear();
1426 }
1427
1428 /**
1429 * Tests processing of Switch Events with Mastership switchover between
1430 * two ONOS instance, and the delivery of the topology events.
1431 *
1432 * We test the following scenario:
1433 * - Initially, a Mastership Event and a Switch Event from one ONOS
1434 * instance are processed - both events should be delivered.
1435 * - Later, a Mastership Event and a Switch event from another ONOS
1436 * instances are processed - both events should be delivered.
1437 * - Finally, a REMOVE Switch Event is received from the first ONOS
1438 * instance - no event should be delivered.
1439 */
1440 @Test
1441 public void testProcessSwitchMastershipSwitchover() {
1442 TopologyEvents topologyEvents;
1443 List<EventEntry<TopologyEvent>> events = new LinkedList<>();
1444 EventEntry<TopologyEvent> eventEntry;
1445 TopologyEvent topologyMastershipEvent;
1446 TopologyEvent topologySwitchEvent;
1447
1448 setupTopologyManagerWithEventHandler();
1449
1450 // Prepare the Mastership Event from the first ONOS instance
1451 Role role = Role.MASTER;
1452 MastershipEvent mastershipEvent =
1453 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
1454 topologyMastershipEvent = new TopologyEvent(mastershipEvent,
1455 ONOS_INSTANCE_ID_1);
1456
1457 // Prepare the Switch Event from the first ONOS instance
1458 SwitchEvent switchEvent = new SwitchEvent(DPID_1);
1459 topologySwitchEvent = new TopologyEvent(switchEvent,
1460 ONOS_INSTANCE_ID_1);
1461
1462 // Add the Mastership Event
1463 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1464 topologyMastershipEvent);
1465 events.add(eventEntry);
1466
1467 // Add the Switch Event
1468 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1469 topologySwitchEvent);
1470 events.add(eventEntry);
1471
1472 // Process the events
1473 TestUtils.callMethod(theEventHandler, "processEvents",
1474 List.class, events);
1475
1476 // Check the fired events: both events should be fired
1477 topologyEvents = theTopologyListener.topologyEvents;
1478 assertNotNull(topologyEvents);
1479 assertThat(topologyEvents.getAddedMastershipEvents(),
1480 hasItem(mastershipEvent));
1481 assertThat(topologyEvents.getAddedSwitchEvents(),
1482 hasItem(switchEvent));
1483 theTopologyListener.clear();
1484 events.clear();
1485
1486 //
1487 // Update the Registry Service, so the second ONOS instance is the
1488 // Master.
1489 //
1490 reset(registryService);
1491 try {
1492 expect(registryService.getControllerForSwitch(DPID_1.value())).
1493 andReturn(ONOS_INSTANCE_ID_2.toString()).anyTimes();
1494 } catch (RegistryException ex) {
1495 throw new IllegalStateException(ex);
1496 }
1497 replay(registryService);
1498
1499 // Prepare the Mastership Event from the second ONOS instance
1500 role = Role.MASTER;
1501 mastershipEvent = new MastershipEvent(DPID_1,
1502 ONOS_INSTANCE_ID_2, role);
1503 topologyMastershipEvent = new TopologyEvent(mastershipEvent,
1504 ONOS_INSTANCE_ID_2);
1505
1506 // Prepare the Switch Event from second ONOS instance
1507 switchEvent = new SwitchEvent(DPID_1);
1508 topologySwitchEvent = new TopologyEvent(switchEvent,
1509 ONOS_INSTANCE_ID_2);
1510
1511 // Add the Mastership Event
1512 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1513 topologyMastershipEvent);
1514 events.add(eventEntry);
1515
1516 // Add the Switch Event
1517 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1518 topologySwitchEvent);
1519 events.add(eventEntry);
1520
1521 // Process the events
1522 TestUtils.callMethod(theEventHandler, "processEvents",
1523 List.class, events);
1524
1525 // Check the fired events: both events should be fired
1526 topologyEvents = theTopologyListener.topologyEvents;
1527 assertNotNull(topologyEvents);
1528 assertThat(topologyEvents.getAddedMastershipEvents(),
1529 hasItem(mastershipEvent));
1530 assertThat(topologyEvents.getAddedSwitchEvents(),
1531 hasItem(switchEvent));
1532 theTopologyListener.clear();
1533 events.clear();
1534
1535 // Prepare the REMOVE Switch Event from first ONOS instance
1536 switchEvent = new SwitchEvent(DPID_1);
1537 topologySwitchEvent = new TopologyEvent(switchEvent,
1538 ONOS_INSTANCE_ID_1);
1539 // Add the Switch Event
1540 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_REMOVE,
1541 topologySwitchEvent);
1542 events.add(eventEntry);
1543
1544 // Process the events
1545 TestUtils.callMethod(theEventHandler, "processEvents",
1546 List.class, events);
1547
1548 // Check the fired events: no events should be fired
1549 topologyEvents = theTopologyListener.topologyEvents;
1550 assertNull(topologyEvents);
1551 theTopologyListener.clear();
1552 events.clear();
1553 }
weibitf7c31a42014-06-23 16:51:01 -07001554}