blob: 231ba6f8934e33c27cd5aa01c149dd2100b7b32a [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();
148 try {
149 expect(registryService.getControllerForSwitch(DPID_1.value())).
150 andReturn(ONOS_INSTANCE_ID_1.toString()).anyTimes();
151 expect(registryService.getControllerForSwitch(DPID_2.value())).
152 andReturn(ONOS_INSTANCE_ID_2.toString()).anyTimes();
153 } catch (RegistryException ex) {
154 throw new IllegalStateException(ex);
155 }
weibitf7c31a42014-06-23 16:51:01 -0700156
157 allTopologyEvents = new CopyOnWriteArrayList<>();
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700158 expect(eventChannel.getAllEntries())
159 .andReturn(allTopologyEvents).anyTimes();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700160
161 replay(datagridService);
162 replay(registryService);
163 replay(dataStoreService);
164 // replay(eventChannel);
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700165 }
weibitf7c31a42014-06-23 16:51:01 -0700166
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700167 /**
168 * Setup the Topology Manager.
169 */
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700170 private void setupTopologyManager() {
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700171 // Create a TopologyManager object for testing
Pavlin Radoslavov8a44b782014-08-07 12:53:27 -0700172 theTopologyManager = new TopologyManager(registryService);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700173
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700174 // Replace the eventHandler to prevent the thread from starting
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700175 TestUtils.setField(theTopologyManager, "eventHandler",
176 EasyMock.createNiceMock(TopologyManager.EventHandler.class));
weibitf7c31a42014-06-23 16:51:01 -0700177 theTopologyManager.startup(datagridService);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700178
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700179 // Replace the data store with a mocked object
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700180 TestUtils.setField(theTopologyManager, "datastore", dataStoreService);
weibitf7c31a42014-06-23 16:51:01 -0700181 }
182
weibitf7c31a42014-06-23 16:51:01 -0700183 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700184 * Setup the Topology Manager with the Event Handler.
185 */
186 private void setupTopologyManagerWithEventHandler() {
187 // Create a TopologyManager object for testing
Pavlin Radoslavov8a44b782014-08-07 12:53:27 -0700188 theTopologyManager = new TopologyManager(registryService);
189 theTopologyManager.registerTopologyListener(theTopologyListener);
190
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700191 // Allocate the Event Handler, so we can have direct access to it
192 theEventHandler = theTopologyManager.new EventHandler();
193 TestUtils.setField(theTopologyManager, "eventHandler",
194 theEventHandler);
195
196 // Replace the data store with a mocked object
197 TestUtils.setField(theTopologyManager, "datastore", dataStoreService);
198
199 replay(eventChannel);
200 //
201 // NOTE: Uncomment-out the line below if the startup() method needs
202 // to be called for some of the unit tests. For now it is commented-out
203 // to avoid any side effects of starting the eventHandler thread.
204 //
205 // theTopologyManager.startup(datagridService);
206 }
207
208 /**
209 * Test the Switch Mastership Updated Event.
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700210 */
211 @Test
212 public void testPutSwitchMastershipEvent() {
213 // Mock the eventChannel functions first
214 eventChannel.addEntry(anyObject(byte[].class),
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700215 anyObject(TopologyEvent.class));
216 EasyMock.expectLastCall().times(1, 1); // 1 event
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700217 replay(eventChannel);
218
219 setupTopologyManager();
220
221 // Generate a new Switch Mastership event
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700222 Role role = Role.MASTER;
223 MastershipEvent mastershipEvent =
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700224 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700225
226 // Call the topologyManager function for adding the event
227 theTopologyManager.putSwitchMastershipEvent(mastershipEvent);
228
229 // Verify the function calls
230 verify(eventChannel);
231 }
232
233 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700234 * Test the Switch Mastership Removed Event.
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700235 */
236 @Test
237 public void testRemoveSwitchMastershipEvent() {
238 // Mock the eventChannel functions first
239 eventChannel.removeEntry(anyObject(byte[].class));
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700240 EasyMock.expectLastCall().times(1, 1); // 1 event
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700241 replay(eventChannel);
242
243 setupTopologyManager();
244
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700245 // Generate a new Switch Mastership Event
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700246 Role role = Role.MASTER;
247 MastershipEvent mastershipEvent =
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700248 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700249
250 // Call the topologyManager function for removing the event
251 theTopologyManager.removeSwitchMastershipEvent(mastershipEvent);
252
253 // Verify the function calls
254 verify(eventChannel);
255 }
256
257 /**
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700258 * Test the Switch discovered and Port discovered functions.
259 */
260 @Test
261 public void testPutSwitchAndPortDiscoveryEvent() {
262 // Mock the eventChannel functions first
263 eventChannel.addEntry(anyObject(byte[].class),
264 anyObject(TopologyEvent.class));
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700265 EasyMock.expectLastCall().times(3, 3); // (1 Switch + 1 Port), 1 Port
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700266 replay(eventChannel);
267
268 setupTopologyManager();
269
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700270 // Mock Switch has one Port
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700271 PortNumber portNumber = PortNumber.uint32(1);
272
273 // Generate a new Switch Event along with a Port Event
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700274 SwitchEvent switchEvent = new SwitchEvent(DPID_1);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700275
276 Collection<PortEvent> portEvents = new ArrayList<PortEvent>();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700277 portEvents.add(new PortEvent(DPID_1, portNumber));
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700278
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700279 // Call the topologyManager function for adding a Switch
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700280 theTopologyManager.putSwitchDiscoveryEvent(switchEvent, portEvents);
281
282 for (PortEvent portEvent : portEvents) {
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700283 // Call the topologyManager function for adding a Port
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700284 theTopologyManager.putPortDiscoveryEvent(portEvent);
285 }
286
287 // Verify the function calls
288 verify(eventChannel);
289
290 }
291
292 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700293 * Test the Switch and Port removed functions.
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700294 */
295 @Test
296 public void testRemoveSwitchAndPortDiscoveryEvent() {
297 // Mock the eventChannel functions first
298 eventChannel.removeEntry(anyObject(byte[].class));
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700299 EasyMock.expectLastCall().times(2, 2); // 1 Switch, 1 Port
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700300 replay(eventChannel);
301
302 setupTopologyManager();
303
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700304 PortNumber portNumber = PortNumber.uint32(1);
305
306 // Generate a Port Event
307 Collection<PortEvent> portEvents = new ArrayList<PortEvent>();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700308 portEvents.add(new PortEvent(DPID_1, portNumber));
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700309
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700310 // Call the topologyManager function for removing a Port
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700311 for (PortEvent portEvent : portEvents) {
312 theTopologyManager.removePortDiscoveryEvent(portEvent);
313 }
314
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700315 // Call the topologyManager function for removing a Switch
316 SwitchEvent switchEvent = new SwitchEvent(DPID_1);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700317 theTopologyManager.removeSwitchDiscoveryEvent(switchEvent);
318
319 // Verify the function calls
320 verify(eventChannel);
321
322 }
323
324 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700325 * Test the Link discovered function.
weibitf7c31a42014-06-23 16:51:01 -0700326 */
327 @Test
328 public void testPutLinkDiscoveryEvent() {
329 // Mock the eventChannel functions first
330 eventChannel.addEntry(anyObject(byte[].class),
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700331 anyObject(TopologyEvent.class));
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700332 EasyMock.expectLastCall().times(5, 5); // (2 Switch + 2 Port + 1 Link)
weibitf7c31a42014-06-23 16:51:01 -0700333 replay(eventChannel);
334
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700335 setupTopologyManager();
336
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700337 // Generate the Switch and Port Events
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700338 PortNumber portNumber1 = PortNumber.uint32(1);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700339 SwitchEvent switchEvent1 = new SwitchEvent(DPID_1);
weibitf7c31a42014-06-23 16:51:01 -0700340 Collection<PortEvent> portEvents1 = new ArrayList<PortEvent>();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700341 portEvents1.add(new PortEvent(DPID_1, portNumber1));
weibitf7c31a42014-06-23 16:51:01 -0700342
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700343 // Call the topologyManager function for adding a Switch
weibitf7c31a42014-06-23 16:51:01 -0700344 theTopologyManager.putSwitchDiscoveryEvent(switchEvent1, portEvents1);
345
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700346 // Generate the Switch and Port Events
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700347 PortNumber portNumber2 = PortNumber.uint32(2);
348 SwitchEvent switchEvent2 = new SwitchEvent(DPID_2);
weibitf7c31a42014-06-23 16:51:01 -0700349 Collection<PortEvent> portEvents2 = new ArrayList<PortEvent>();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700350 portEvents2.add(new PortEvent(DPID_2, portNumber2));
weibitf7c31a42014-06-23 16:51:01 -0700351
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700352 // Call the topologyManager function for adding a Switch
weibitf7c31a42014-06-23 16:51:01 -0700353 theTopologyManager.putSwitchDiscoveryEvent(switchEvent2, portEvents2);
354
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700355 // Create the Link Event
356 LinkEvent linkEvent =
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700357 new LinkEvent(new SwitchPort(DPID_1, portNumber1),
358 new SwitchPort(DPID_2, portNumber2));
weibitf7c31a42014-06-23 16:51:01 -0700359 theTopologyManager.putLinkDiscoveryEvent(linkEvent);
360
361 // Verify the function calls
362 verify(eventChannel);
363 }
364
365 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700366 * Test the Link removed function.
weibitf7c31a42014-06-23 16:51:01 -0700367 */
368 @Test
369 public void testRemoveLinkDiscoveryEvent() {
370 // Mock the eventChannel functions first
371 eventChannel.removeEntry(anyObject(byte[].class));
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700372 EasyMock.expectLastCall().times(1, 1); // (1 Link)
weibitf7c31a42014-06-23 16:51:01 -0700373 replay(eventChannel);
374
Yuta HIGUCHI81d8b9e2014-07-14 23:56:34 -0700375 setupTopologyManager();
376
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700377 // Generate the Switch and Port Events
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700378 PortNumber portNumber1 = PortNumber.uint32(1);
379 SwitchEvent switchEvent1 = new SwitchEvent(DPID_1);
weibitf7c31a42014-06-23 16:51:01 -0700380 Collection<PortEvent> portEvents1 = new ArrayList<PortEvent>();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700381 portEvents1.add(new PortEvent(DPID_1, portNumber1));
weibitf7c31a42014-06-23 16:51:01 -0700382
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700383 // Call the topologyManager function for adding a Switch
weibitf7c31a42014-06-23 16:51:01 -0700384 theTopologyManager.putSwitchDiscoveryEvent(switchEvent1, portEvents1);
385
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700386 // Generate the Switch and Port Events
387 PortNumber portNumber2 = PortNumber.uint32(2);
388 SwitchEvent switchEvent2 = new SwitchEvent(DPID_2);
weibitf7c31a42014-06-23 16:51:01 -0700389 Collection<PortEvent> portEvents2 = new ArrayList<PortEvent>();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700390 portEvents2.add(new PortEvent(DPID_2, portNumber2));
weibitf7c31a42014-06-23 16:51:01 -0700391
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700392 // Call the topologyManager function for adding a Switch
weibitf7c31a42014-06-23 16:51:01 -0700393 theTopologyManager.putSwitchDiscoveryEvent(switchEvent2, portEvents2);
394
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700395 // Remove the Link
Pavlin Radoslavova5637c02014-07-30 15:55:11 -0700396 LinkEvent linkEventRemove =
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700397 new LinkEvent(new SwitchPort(DPID_1, portNumber1),
398 new SwitchPort(DPID_2, portNumber2));
weibitf7c31a42014-06-23 16:51:01 -0700399 theTopologyManager.removeLinkDiscoveryEvent(linkEventRemove);
400
401 // Verify the function calls
402 verify(eventChannel);
403 }
404
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700405 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700406 * Test the Host discovered function.
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700407 */
408 @Test
409 public void testPutHostDiscoveryEvent() {
410 // Mock the eventChannel functions first
411 eventChannel.addEntry(anyObject(byte[].class),
412 anyObject(TopologyEvent.class));
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700413 EasyMock.expectLastCall().times(1, 1); // 1 Host
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700414 replay(eventChannel);
415
416 setupTopologyManager();
417
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700418 // Generate a new Host Event
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700419 PortNumber portNumber = PortNumber.uint32(1);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700420 MACAddress hostMac = MACAddress.valueOf("00:AA:11:BB:33:CC");
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700421 SwitchPort sp = new SwitchPort(DPID_1, portNumber);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700422 List<SwitchPort> spLists = new ArrayList<SwitchPort>();
423 spLists.add(sp);
424 HostEvent hostEvent = new HostEvent(hostMac);
425 hostEvent.setAttachmentPoints(spLists);
426
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700427 // Call the topologyManager function for adding a Host
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700428 theTopologyManager.putHostDiscoveryEvent(hostEvent);
429
430 // Verify the function calls
431 verify(eventChannel);
432 }
433
434 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700435 * Test the Host removed function.
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700436 */
437 @Test
438 public void testRemoveHostDiscoveryEvent() {
439 // Mock the eventChannel functions first
440 eventChannel.removeEntry(anyObject(byte[].class));
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700441 EasyMock.expectLastCall().times(1, 1); // 1 Host
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700442 replay(eventChannel);
443
444 setupTopologyManager();
445
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700446 // Generate a new Host Event
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700447 PortNumber portNumber = PortNumber.uint32(1);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700448 MACAddress hostMac = MACAddress.valueOf("00:AA:11:BB:33:CC");
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700449 SwitchPort sp = new SwitchPort(DPID_1, portNumber);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700450 List<SwitchPort> spLists = new ArrayList<SwitchPort>();
451 spLists.add(sp);
452 HostEvent hostEvent = new HostEvent(hostMac);
453 hostEvent.setAttachmentPoints(spLists);
454
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700455 // Call the topologyManager function for removing a Host
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700456 theTopologyManager.removeHostDiscoveryEvent(hostEvent);
457
458 // Verify the function calls
459 verify(eventChannel);
460 }
461
462 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700463 * Tests adding of a Switch Mastership event and the topology replica
464 * transformation.
465 */
466 @Test
467 public void testAddMastershipEvent() {
468 setupTopologyManager();
469
470 // Prepare the event
471 Role role = Role.MASTER;
472 MastershipEvent mastershipEvent =
473 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
474 // Add the event
475 TestUtils.callMethod(theTopologyManager, "addMastershipEvent",
476 MastershipEvent.class, mastershipEvent);
477
478 //
479 // NOTE: The topology itself doesn't contain the Mastership Events,
480 // hence we don't check the topology.
481 //
482
483 // Check the events to be fired
484 List<MastershipEvent> apiAddedMastershipEvents
485 = TestUtils.getField(theTopologyManager,
486 "apiAddedMastershipEvents");
487 assertThat(apiAddedMastershipEvents, hasItem(mastershipEvent));
488 }
489
490 /**
491 * Tests removing of a Switch Mastership event and the topology replica
492 * transformation.
493 */
494 @Test
495 public void testRemoveMastershipEvent() {
496 setupTopologyManager();
497
498 // Prepare the event
499 Role role = Role.MASTER;
500 MastershipEvent mastershipEvent =
501 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
502 // Add the event
503 TestUtils.callMethod(theTopologyManager, "addMastershipEvent",
504 MastershipEvent.class, mastershipEvent);
505
506 // Check the events to be fired
507 List<MastershipEvent> apiAddedMastershipEvents
508 = TestUtils.getField(theTopologyManager,
509 "apiAddedMastershipEvents");
510 assertThat(apiAddedMastershipEvents, hasItem(mastershipEvent));
511
512 // Remove the event
513 TestUtils.callMethod(theTopologyManager, "removeMastershipEvent",
514 MastershipEvent.class,
515 new MastershipEvent(mastershipEvent));
516
517 // Check the events to be fired
518 List<MastershipEvent> apiRemovedMastershipEvents
519 = TestUtils.getField(theTopologyManager,
520 "apiRemovedMastershipEvents");
521 assertThat(apiRemovedMastershipEvents, hasItem(mastershipEvent));
522 }
523
524 /**
525 * Tests adding of a Switch and the topology replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700526 */
527 @Test
528 public void testAddSwitch() {
529 setupTopologyManager();
530
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700531 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700532 sw.createStringAttribute("foo", "bar");
533
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700534 TestUtils.callMethod(theTopologyManager, "addSwitch",
535 SwitchEvent.class, sw);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700536
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700537 // Check the topology structure
538 TopologyInternal topology =
539 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700540 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700541 assertEquals(sw, swInTopo);
542 assertTrue(swInTopo.isFrozen());
543 assertEquals("bar", swInTopo.getStringAttribute("foo"));
544
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700545 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700546 List<SwitchEvent> apiAddedSwitchEvents
547 = TestUtils.getField(theTopologyManager, "apiAddedSwitchEvents");
548 assertThat(apiAddedSwitchEvents, hasItem(sw));
549 }
550
551 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700552 * Tests adding of a Port and the topology replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700553 */
554 @Test
555 public void testAddPort() {
556 setupTopologyManager();
557
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700558 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700559 sw.createStringAttribute("foo", "bar");
560
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700561 final PortNumber portNumber = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700562 PortEvent port = new PortEvent(DPID_1, portNumber);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700563 port.createStringAttribute("fuzz", "buzz");
564
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700565 TestUtils.callMethod(theTopologyManager, "addSwitch",
566 SwitchEvent.class, sw);
567 TestUtils.callMethod(theTopologyManager, "addPort",
568 PortEvent.class, port);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700569
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700570 // Check the topology structure
571 TopologyInternal topology =
572 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700573 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700574 assertEquals(sw, swInTopo);
575 assertTrue(swInTopo.isFrozen());
576 assertEquals("bar", swInTopo.getStringAttribute("foo"));
577
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700578 final SwitchPort switchPort = new SwitchPort(DPID_1, portNumber);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700579 PortEvent portInTopo = topology.getPortEvent(switchPort);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700580 assertEquals(port, portInTopo);
581 assertTrue(portInTopo.isFrozen());
582 assertEquals("buzz", portInTopo.getStringAttribute("fuzz"));
583
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700584 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700585 List<PortEvent> apiAddedPortEvents
586 = TestUtils.getField(theTopologyManager, "apiAddedPortEvents");
587 assertThat(apiAddedPortEvents, hasItem(port));
588 }
589
590 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700591 * Tests removing of a Port followed by removing of a Switch,
592 * and the topology replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700593 */
594 @Test
595 public void testRemovePortThenSwitch() {
596 setupTopologyManager();
597
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700598 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700599 sw.createStringAttribute("foo", "bar");
600
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700601 final PortNumber portNumber = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700602 PortEvent port = new PortEvent(DPID_1, portNumber);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700603 port.createStringAttribute("fuzz", "buzz");
604
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700605 TestUtils.callMethod(theTopologyManager, "addSwitch",
606 SwitchEvent.class, sw);
607 TestUtils.callMethod(theTopologyManager, "addPort",
608 PortEvent.class, port);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700609
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700610 // Check the topology structure
611 TopologyInternal topology =
612 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700613 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700614 assertEquals(sw, swInTopo);
615 assertTrue(swInTopo.isFrozen());
616 assertEquals("bar", swInTopo.getStringAttribute("foo"));
617
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700618 final SwitchPort switchPort = new SwitchPort(DPID_1, portNumber);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700619 PortEvent portInTopo = topology.getPortEvent(switchPort);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700620 assertEquals(port, portInTopo);
621 assertTrue(portInTopo.isFrozen());
622 assertEquals("buzz", portInTopo.getStringAttribute("fuzz"));
623
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700624 // Remove in proper order
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700625 TestUtils.callMethod(theTopologyManager, "removePort",
626 PortEvent.class, new PortEvent(port));
627 TestUtils.callMethod(theTopologyManager, "removeSwitch",
628 SwitchEvent.class, new SwitchEvent(sw));
629
630
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700631 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700632 List<PortEvent> apiRemovedPortEvents
633 = TestUtils.getField(theTopologyManager, "apiRemovedPortEvents");
634 assertThat(apiRemovedPortEvents, hasItem(port));
635 List<SwitchEvent> apiRemovedSwitchEvents
636 = TestUtils.getField(theTopologyManager, "apiRemovedSwitchEvents");
637 assertThat(apiRemovedSwitchEvents, hasItem(sw));
638 }
639
640 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700641 * Tests removing of a Switch without removing of a Port,
642 * and the topology replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700643 */
644 @Test
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700645 public void testRemoveSwitchWithoutPortRemoval() {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700646 setupTopologyManager();
647
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700648 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700649 sw.createStringAttribute("foo", "bar");
650
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700651 final PortNumber portNumber = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700652 PortEvent port = new PortEvent(DPID_1, portNumber);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700653 port.createStringAttribute("fuzz", "buzz");
654
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700655 TestUtils.callMethod(theTopologyManager, "addSwitch",
656 SwitchEvent.class, sw);
657 TestUtils.callMethod(theTopologyManager, "addPort",
658 PortEvent.class, port);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700659
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700660 // Check the topology structure
661 TopologyInternal topology =
662 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700663 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700664 assertEquals(sw, swInTopo);
665 assertTrue(swInTopo.isFrozen());
666 assertEquals("bar", swInTopo.getStringAttribute("foo"));
667
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700668 final SwitchPort switchPort = new SwitchPort(DPID_1, portNumber);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700669 PortEvent portInTopo = topology.getPortEvent(switchPort);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700670 assertEquals(port, portInTopo);
671 assertTrue(portInTopo.isFrozen());
672 assertEquals("buzz", portInTopo.getStringAttribute("fuzz"));
673
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700674 // Remove in in-proper order
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700675// TestUtils.callMethod(theTopologyManager, "removePort",
676// PortEvent.class, new PortEvent(port));
677 TestUtils.callMethod(theTopologyManager, "removeSwitch",
678 SwitchEvent.class, new SwitchEvent(sw));
679
680
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700681 // Check the events to be fired
682 // The outcome should be the same as #testRemovePortThenSwitch
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700683 List<PortEvent> apiRemovedPortEvents
684 = TestUtils.getField(theTopologyManager, "apiRemovedPortEvents");
685 assertThat(apiRemovedPortEvents, hasItem(port));
686 List<SwitchEvent> apiRemovedSwitchEvents
687 = TestUtils.getField(theTopologyManager, "apiRemovedSwitchEvents");
688 assertThat(apiRemovedSwitchEvents, hasItem(sw));
689 }
690
691 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700692 * Tests adding of a Link and the topology replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700693 */
694 @Test
695 public void testAddLink() {
696 setupTopologyManager();
697
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700698 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700699 sw.createStringAttribute("foo", "bar");
700
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700701 final PortNumber portNumberA = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700702 PortEvent portA = new PortEvent(DPID_1, portNumberA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700703 portA.createStringAttribute("fuzz", "buzz");
704
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700705 final PortNumber portNumberB = PortNumber.uint32(3);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700706 PortEvent portB = new PortEvent(DPID_1, portNumberB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700707 portB.createStringAttribute("fizz", "buz");
708
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700709 LinkEvent linkA = new LinkEvent(portA.getSwitchPort(),
710 portB.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700711 linkA.createStringAttribute(TopologyElement.TYPE,
712 TopologyElement.TYPE_OPTICAL_LAYER);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700713 LinkEvent linkB = new LinkEvent(portB.getSwitchPort(),
714 portA.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700715 linkB.createStringAttribute(TopologyElement.TYPE,
716 TopologyElement.TYPE_OPTICAL_LAYER);
717
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700718 TestUtils.callMethod(theTopologyManager, "addSwitch",
719 SwitchEvent.class, sw);
720 TestUtils.callMethod(theTopologyManager, "addPort",
721 PortEvent.class, portA);
722 TestUtils.callMethod(theTopologyManager, "addPort",
723 PortEvent.class, portB);
724 TestUtils.callMethod(theTopologyManager, "addLink",
725 LinkEvent.class, linkA);
726 TestUtils.callMethod(theTopologyManager, "addLink",
727 LinkEvent.class, linkB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700728
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700729 // Check the topology structure
730 TopologyInternal topology =
731 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700732 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700733 assertEquals(sw, swInTopo);
734 assertTrue(swInTopo.isFrozen());
735 assertEquals("bar", swInTopo.getStringAttribute("foo"));
736
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700737 final SwitchPort switchPortA = new SwitchPort(DPID_1, portNumberA);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700738 PortEvent portAInTopo = topology.getPortEvent(switchPortA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700739 assertEquals(portA, portAInTopo);
740 assertTrue(portAInTopo.isFrozen());
741 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
742
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700743 final SwitchPort switchPortB = new SwitchPort(DPID_1, portNumberB);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700744 PortEvent portBInTopo = topology.getPortEvent(switchPortB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700745 assertEquals(portB, portBInTopo);
746 assertTrue(portBInTopo.isFrozen());
747 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
748
749 LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple());
750 assertEquals(linkA, linkAInTopo);
751 assertTrue(linkAInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700752 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
753 linkAInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700754
755 LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple());
756 assertEquals(linkB, linkBInTopo);
757 assertTrue(linkBInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700758 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
759 linkBInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700760
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700761 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700762 List<LinkEvent> apiAddedLinkEvents
763 = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents");
764 assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB));
765 }
766
767 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700768 * Tests removing of a Link without removing of a Host, and the topology
769 * replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700770 */
771 @Test
772 public void testAddLinkKickingOffHost() {
773 setupTopologyManager();
774
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700775 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700776 sw.createStringAttribute("foo", "bar");
777
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700778 final PortNumber portNumberA = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700779 PortEvent portA = new PortEvent(DPID_1, portNumberA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700780 portA.createStringAttribute("fuzz", "buzz");
781
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700782 final PortNumber portNumberB = PortNumber.uint32(3);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700783 PortEvent portB = new PortEvent(DPID_1, portNumberB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700784 portB.createStringAttribute("fizz", "buz");
785
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700786 final PortNumber portNumberC = PortNumber.uint32(4);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700787 PortEvent portC = new PortEvent(DPID_1, portNumberC);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700788 portC.createStringAttribute("fizz", "buz");
789
790 final MACAddress macA = MACAddress.valueOf(666L);
791 HostEvent hostA = new HostEvent(macA);
792 hostA.addAttachmentPoint(portA.getSwitchPort());
793 final long timestampA = 392893200L;
794 hostA.setLastSeenTime(timestampA);
795
796 final MACAddress macB = MACAddress.valueOf(999L);
797 HostEvent hostB = new HostEvent(macB);
798 hostB.addAttachmentPoint(portB.getSwitchPort());
799 hostB.addAttachmentPoint(portC.getSwitchPort());
800 final long timestampB = 392893201L;
801 hostB.setLastSeenTime(timestampB);
802
803
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700804 LinkEvent linkA = new LinkEvent(portA.getSwitchPort(),
805 portB.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700806 linkA.createStringAttribute(TopologyElement.TYPE,
807 TopologyElement.TYPE_OPTICAL_LAYER);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700808 LinkEvent linkB = new LinkEvent(portB.getSwitchPort(),
809 portA.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700810 linkB.createStringAttribute(TopologyElement.TYPE,
811 TopologyElement.TYPE_OPTICAL_LAYER);
812
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700813 TestUtils.callMethod(theTopologyManager, "addSwitch",
814 SwitchEvent.class, sw);
815 TestUtils.callMethod(theTopologyManager, "addPort",
816 PortEvent.class, portA);
817 TestUtils.callMethod(theTopologyManager, "addPort",
818 PortEvent.class, portB);
819 TestUtils.callMethod(theTopologyManager, "addPort",
820 PortEvent.class, portC);
821 TestUtils.callMethod(theTopologyManager, "addHost",
822 HostEvent.class, hostA);
823 TestUtils.callMethod(theTopologyManager, "addHost",
824 HostEvent.class, hostB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700825
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700826 TestUtils.callMethod(theTopologyManager, "addLink",
827 LinkEvent.class, linkA);
828 TestUtils.callMethod(theTopologyManager, "addLink",
829 LinkEvent.class, linkB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700830
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700831 // Check the topology structure
832 TopologyInternal topology =
833 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700834 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700835 assertEquals(sw, swInTopo);
836 assertTrue(swInTopo.isFrozen());
837 assertEquals("bar", swInTopo.getStringAttribute("foo"));
838
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700839 final SwitchPort switchPortA = new SwitchPort(DPID_1, portNumberA);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700840 PortEvent portAInTopo = topology.getPortEvent(switchPortA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700841 assertEquals(portA, portAInTopo);
842 assertTrue(portAInTopo.isFrozen());
843 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
844
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700845 final SwitchPort switchPortB = new SwitchPort(DPID_1, portNumberB);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700846 PortEvent portBInTopo = topology.getPortEvent(switchPortB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700847 assertEquals(portB, portBInTopo);
848 assertTrue(portBInTopo.isFrozen());
849 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
850
851 // hostA expected to be removed
852 assertNull(topology.getHostEvent(macA));
853 // hostB expected to be there with reduced attachment point
854 HostEvent hostBrev = new HostEvent(macB);
855 hostBrev.addAttachmentPoint(portC.getSwitchPort());
856 hostBrev.setLastSeenTime(timestampB);
857 hostBrev.freeze();
858 assertEquals(hostBrev, topology.getHostEvent(macB));
859
860
861 LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple());
862 assertEquals(linkA, linkAInTopo);
863 assertTrue(linkAInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700864 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
865 linkAInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700866
867 LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple());
868 assertEquals(linkB, linkBInTopo);
869 assertTrue(linkBInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700870 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
871 linkBInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700872
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700873 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700874 List<HostEvent> apiAddedHostEvents
875 = TestUtils.getField(theTopologyManager, "apiAddedHostEvents");
876 assertThat(apiAddedHostEvents, hasItem(hostBrev));
877
878 List<HostEvent> apiRemovedHostEvents
879 = TestUtils.getField(theTopologyManager, "apiRemovedHostEvents");
880 assertThat(apiRemovedHostEvents, hasItem(hostA));
881 List<LinkEvent> apiAddedLinkEvents
882 = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents");
883 assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB));
884 }
885
886 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700887 * Tests removing of a Link and the topology replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700888 */
889 @Test
890 public void testRemoveLink() {
891 setupTopologyManager();
892
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700893 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700894 sw.createStringAttribute("foo", "bar");
895
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700896 final PortNumber portNumberA = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700897 PortEvent portA = new PortEvent(DPID_1, portNumberA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700898 portA.createStringAttribute("fuzz", "buzz");
899
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700900 final PortNumber portNumberB = PortNumber.uint32(3);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700901 PortEvent portB = new PortEvent(DPID_1, portNumberB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700902 portB.createStringAttribute("fizz", "buz");
903
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700904 LinkEvent linkA = new LinkEvent(portA.getSwitchPort(),
905 portB.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700906 linkA.createStringAttribute(TopologyElement.TYPE,
907 TopologyElement.TYPE_OPTICAL_LAYER);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700908 LinkEvent linkB = new LinkEvent(portB.getSwitchPort(),
909 portA.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700910 linkB.createStringAttribute(TopologyElement.TYPE,
911 TopologyElement.TYPE_OPTICAL_LAYER);
912
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700913 TestUtils.callMethod(theTopologyManager, "addSwitch",
914 SwitchEvent.class, sw);
915 TestUtils.callMethod(theTopologyManager, "addPort",
916 PortEvent.class, portA);
917 TestUtils.callMethod(theTopologyManager, "addPort",
918 PortEvent.class, portB);
919 TestUtils.callMethod(theTopologyManager, "addLink",
920 LinkEvent.class, linkA);
921 TestUtils.callMethod(theTopologyManager, "addLink",
922 LinkEvent.class, linkB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700923
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700924 // Check the topology structure
925 TopologyInternal topology =
926 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700927 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700928 assertEquals(sw, swInTopo);
929 assertTrue(swInTopo.isFrozen());
930 assertEquals("bar", swInTopo.getStringAttribute("foo"));
931
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700932 final SwitchPort switchPortA = new SwitchPort(DPID_1, portNumberA);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700933 PortEvent portAInTopo = topology.getPortEvent(switchPortA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700934 assertEquals(portA, portAInTopo);
935 assertTrue(portAInTopo.isFrozen());
936 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
937
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700938 final SwitchPort switchPortB = new SwitchPort(DPID_1, portNumberB);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700939 PortEvent portBInTopo = topology.getPortEvent(switchPortB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700940 assertEquals(portB, portBInTopo);
941 assertTrue(portBInTopo.isFrozen());
942 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
943
944 LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple());
945 assertEquals(linkA, linkAInTopo);
946 assertTrue(linkAInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700947 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
948 linkAInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700949
950
951 LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple());
952 assertEquals(linkB, linkBInTopo);
953 assertTrue(linkBInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700954 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
955 linkBInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700956
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700957 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700958 // FIXME if link flapped (linkA in this scenario),
959 // linkA appears in both removed and added is this expected behavior?
960 List<LinkEvent> apiAddedLinkEvents
961 = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents");
962 assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB));
963
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700964 // Clear the events before removing the link
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700965 apiAddedLinkEvents.clear();
966
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700967 // Remove the link
968 TestUtils.callMethod(theTopologyManager, "removeLink",
969 LinkEvent.class, new LinkEvent(linkA));
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700970
971 LinkEvent linkANotInTopo = topology.getLinkEvent(linkA.getLinkTuple());
972 assertNull(linkANotInTopo);
973
974 List<LinkEvent> apiRemovedLinkEvents
975 = TestUtils.getField(theTopologyManager, "apiRemovedLinkEvents");
976 assertThat(apiRemovedLinkEvents, hasItem(linkA));
977 }
978
979 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700980 * Tests adding of a Host without adding of a Link, and the topology
981 * replica transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700982 */
983 @Test
984 public void testAddHostIgnoredByLink() {
985 setupTopologyManager();
986
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700987 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700988 sw.createStringAttribute("foo", "bar");
989
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700990 final PortNumber portNumberA = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700991 PortEvent portA = new PortEvent(DPID_1, portNumberA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700992 portA.createStringAttribute("fuzz", "buzz");
993
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700994 final PortNumber portNumberB = PortNumber.uint32(3);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700995 PortEvent portB = new PortEvent(DPID_1, portNumberB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700996 portB.createStringAttribute("fizz", "buz");
997
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -0700998 final PortNumber portNumberC = PortNumber.uint32(4);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -0700999 PortEvent portC = new PortEvent(DPID_1, portNumberC);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001000 portC.createStringAttribute("fizz", "buz");
1001
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001002 LinkEvent linkA = new LinkEvent(portA.getSwitchPort(),
1003 portB.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001004 linkA.createStringAttribute(TopologyElement.TYPE,
1005 TopologyElement.TYPE_OPTICAL_LAYER);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001006 LinkEvent linkB = new LinkEvent(portB.getSwitchPort(),
1007 portA.getSwitchPort());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001008 linkB.createStringAttribute(TopologyElement.TYPE,
1009 TopologyElement.TYPE_OPTICAL_LAYER);
1010
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001011 TestUtils.callMethod(theTopologyManager, "addSwitch",
1012 SwitchEvent.class, sw);
1013 TestUtils.callMethod(theTopologyManager, "addPort",
1014 PortEvent.class, portA);
1015 TestUtils.callMethod(theTopologyManager, "addPort",
1016 PortEvent.class, portB);
1017 TestUtils.callMethod(theTopologyManager, "addPort",
1018 PortEvent.class, portC);
1019 TestUtils.callMethod(theTopologyManager, "addLink",
1020 LinkEvent.class, linkA);
1021 TestUtils.callMethod(theTopologyManager, "addLink",
1022 LinkEvent.class, linkB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001023
1024 // Add hostA attached to a port which already has a link
1025 final MACAddress macA = MACAddress.valueOf(666L);
1026 HostEvent hostA = new HostEvent(macA);
1027 hostA.addAttachmentPoint(portA.getSwitchPort());
1028 final long timestampA = 392893200L;
1029 hostA.setLastSeenTime(timestampA);
1030
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001031 TestUtils.callMethod(theTopologyManager, "addHost",
1032 HostEvent.class, hostA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001033
1034 // Add hostB attached to multiple ports,
1035 // some of them which already has a link
1036 final MACAddress macB = MACAddress.valueOf(999L);
1037 HostEvent hostB = new HostEvent(macB);
1038 hostB.addAttachmentPoint(portB.getSwitchPort());
1039 hostB.addAttachmentPoint(portC.getSwitchPort());
1040 final long timestampB = 392893201L;
1041 hostB.setLastSeenTime(timestampB);
1042
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001043 TestUtils.callMethod(theTopologyManager, "addHost",
1044 HostEvent.class, hostB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001045
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001046 // Check the topology structure
1047 TopologyInternal topology =
1048 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001049 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001050 assertEquals(sw, swInTopo);
1051 assertTrue(swInTopo.isFrozen());
1052 assertEquals("bar", swInTopo.getStringAttribute("foo"));
1053
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001054 final SwitchPort switchPortA = new SwitchPort(DPID_1, portNumberA);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001055 PortEvent portAInTopo = topology.getPortEvent(switchPortA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001056 assertEquals(portA, portAInTopo);
1057 assertTrue(portAInTopo.isFrozen());
1058 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
1059
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001060 final SwitchPort switchPortB = new SwitchPort(DPID_1, portNumberB);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001061 PortEvent portBInTopo = topology.getPortEvent(switchPortB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001062 assertEquals(portB, portBInTopo);
1063 assertTrue(portBInTopo.isFrozen());
1064 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
1065
1066 // hostA expected to be completely ignored
1067 assertNull(topology.getHostEvent(macA));
1068 // hostB expected to be there with reduced attachment point
1069 HostEvent hostBrev = new HostEvent(macB);
1070 hostBrev.addAttachmentPoint(portC.getSwitchPort());
1071 hostBrev.setLastSeenTime(timestampB);
1072 hostBrev.freeze();
1073 assertEquals(hostBrev, topology.getHostEvent(macB));
1074
1075
1076 LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple());
1077 assertEquals(linkA, linkAInTopo);
1078 assertTrue(linkAInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001079 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
1080 linkAInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001081
1082 LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple());
1083 assertEquals(linkB, linkBInTopo);
1084 assertTrue(linkBInTopo.isFrozen());
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001085 assertEquals(TopologyElement.TYPE_OPTICAL_LAYER,
1086 linkBInTopo.getType());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001087
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001088 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001089 // hostB should be added with reduced attachment points
1090 List<HostEvent> apiAddedHostEvents
1091 = TestUtils.getField(theTopologyManager, "apiAddedHostEvents");
1092 assertThat(apiAddedHostEvents, hasItem(hostBrev));
1093
1094 // hostA should not be ignored
1095 List<HostEvent> apiRemovedHostEvents
1096 = TestUtils.getField(theTopologyManager, "apiRemovedHostEvents");
1097 assertThat(apiRemovedHostEvents, not(hasItem(hostA)));
1098
1099 List<LinkEvent> apiAddedLinkEvents
1100 = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents");
1101 assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB));
1102 }
1103
1104 /**
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001105 * Tests adding and moving of a Host, and the topology replica
1106 * transformation.
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001107 */
1108 @Test
1109 public void testAddHostMove() {
1110 setupTopologyManager();
1111
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001112 SwitchEvent sw = new SwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001113 sw.createStringAttribute("foo", "bar");
1114
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001115 final PortNumber portNumberA = PortNumber.uint32(2);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001116 PortEvent portA = new PortEvent(DPID_1, portNumberA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001117 portA.createStringAttribute("fuzz", "buzz");
1118
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001119 final PortNumber portNumberB = PortNumber.uint32(3);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001120 PortEvent portB = new PortEvent(DPID_1, portNumberB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001121 portB.createStringAttribute("fizz", "buz");
1122
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001123 final PortNumber portNumberC = PortNumber.uint32(4);
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001124 PortEvent portC = new PortEvent(DPID_1, portNumberC);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001125 portC.createStringAttribute("fizz", "buz");
1126
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001127 TestUtils.callMethod(theTopologyManager, "addSwitch",
1128 SwitchEvent.class, sw);
1129 TestUtils.callMethod(theTopologyManager, "addPort",
1130 PortEvent.class, portA);
1131 TestUtils.callMethod(theTopologyManager, "addPort",
1132 PortEvent.class, portB);
1133 TestUtils.callMethod(theTopologyManager, "addPort",
1134 PortEvent.class, portC);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001135
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001136 // Add hostA attached to a Port which already has a Link
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001137 final MACAddress macA = MACAddress.valueOf(666L);
1138 HostEvent hostA = new HostEvent(macA);
1139 hostA.addAttachmentPoint(portA.getSwitchPort());
1140 final long timestampA = 392893200L;
1141 hostA.setLastSeenTime(timestampA);
1142
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001143 TestUtils.callMethod(theTopologyManager, "addHost",
1144 HostEvent.class, hostA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001145
1146
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001147 // Check the topology structure
1148 TopologyInternal topology =
1149 (TopologyInternal) theTopologyManager.getTopology();
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001150 SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001151 assertEquals(sw, swInTopo);
1152 assertTrue(swInTopo.isFrozen());
1153 assertEquals("bar", swInTopo.getStringAttribute("foo"));
1154
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001155 final SwitchPort switchPortA = new SwitchPort(DPID_1, portNumberA);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001156 PortEvent portAInTopo = topology.getPortEvent(switchPortA);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001157 assertEquals(portA, portAInTopo);
1158 assertTrue(portAInTopo.isFrozen());
1159 assertEquals("buzz", portAInTopo.getStringAttribute("fuzz"));
1160
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001161 final SwitchPort switchPortB = new SwitchPort(DPID_1, portNumberB);
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001162 PortEvent portBInTopo = topology.getPortEvent(switchPortB);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001163 assertEquals(portB, portBInTopo);
1164 assertTrue(portBInTopo.isFrozen());
1165 assertEquals("buz", portBInTopo.getStringAttribute("fizz"));
1166
1167 // hostA expected to be there
1168 assertEquals(hostA, topology.getHostEvent(macA));
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001169 assertEquals(timestampA,
1170 topology.getHostEvent(macA).getLastSeenTime());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001171
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001172 // Check the events to be fired
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001173 // hostA should be added
1174 List<HostEvent> apiAddedHostEvents
1175 = TestUtils.getField(theTopologyManager, "apiAddedHostEvents");
1176 assertThat(apiAddedHostEvents, hasItem(hostA));
1177
1178
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001179 // Clear the events before moving the Host
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001180 apiAddedHostEvents.clear();
1181
1182 HostEvent hostAmoved = new HostEvent(macA);
1183 hostAmoved.addAttachmentPoint(portB.getSwitchPort());
1184 final long timestampAmoved = 392893201L;
1185 hostAmoved.setLastSeenTime(timestampAmoved);
1186
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001187 TestUtils.callMethod(theTopologyManager, "addHost",
1188 HostEvent.class, hostAmoved);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001189
1190 assertEquals(hostAmoved, topology.getHostEvent(macA));
Pavlin Radoslavovefa17b22014-08-01 16:57:56 -07001191 assertEquals(timestampAmoved,
1192 topology.getHostEvent(macA).getLastSeenTime());
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -07001193
1194 // hostA expected to be there with new attachment point
1195 apiAddedHostEvents
1196 = TestUtils.getField(theTopologyManager, "apiAddedHostEvents");
1197 assertThat(apiAddedHostEvents, hasItem(hostAmoved));
1198
1199 // hostA is updated not removed
1200 List<HostEvent> apiRemovedHostEvents
1201 = TestUtils.getField(theTopologyManager, "apiRemovedHostEvents");
1202 assertThat(apiRemovedHostEvents, not(hasItem(hostA)));
1203 }
Pavlin Radoslavovc6236412014-08-01 20:37:46 -07001204
1205 /**
1206 * Tests processing of a Switch Mastership Event and the delivery of the
1207 * topology events.
1208 */
1209 @Test
1210 public void testProcessMastershipEvent() {
1211 List<EventEntry<TopologyEvent>> events = new LinkedList<>();
1212 EventEntry<TopologyEvent> eventEntry;
1213 TopologyEvent topologyEvent;
1214
1215 setupTopologyManagerWithEventHandler();
1216
1217 // Prepare the Mastership Event
1218 Role role = Role.MASTER;
1219 MastershipEvent mastershipEvent =
1220 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
1221 topologyEvent = new TopologyEvent(mastershipEvent, ONOS_INSTANCE_ID_1);
1222
1223 // Add the Mastership Event
1224 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1225 topologyEvent);
1226 events.add(eventEntry);
1227
1228 // Process the events
1229 TestUtils.callMethod(theEventHandler, "processEvents",
1230 List.class, events);
1231
1232 // Check the fired events
1233 TopologyEvents topologyEvents = theTopologyListener.topologyEvents;
1234 assertNotNull(topologyEvents);
1235 assertThat(topologyEvents.getAddedMastershipEvents(),
1236 hasItem(mastershipEvent));
1237 theTopologyListener.clear();
1238 }
1239
1240 /**
1241 * Tests processing of a Switch Event, and the delivery of the topology
1242 * events.
1243 *
1244 * We test the following scenario:
1245 * - Switch Mastership Event is processed along with a Switch Event - both
1246 * events should be delivered.
1247 */
1248 @Test
1249 public void testProcessSwitchEvent() {
1250 TopologyEvents topologyEvents;
1251 List<EventEntry<TopologyEvent>> events = new LinkedList<>();
1252 EventEntry<TopologyEvent> eventEntry;
1253 TopologyEvent topologyMastershipEvent;
1254 TopologyEvent topologySwitchEvent;
1255
1256 setupTopologyManagerWithEventHandler();
1257
1258 // Prepare the Mastership Event
1259 Role role = Role.MASTER;
1260 MastershipEvent mastershipEvent =
1261 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
1262 topologyMastershipEvent = new TopologyEvent(mastershipEvent,
1263 ONOS_INSTANCE_ID_1);
1264
1265 // Prepare the Switch Event
1266 SwitchEvent switchEvent = new SwitchEvent(DPID_1);
1267 topologySwitchEvent = new TopologyEvent(switchEvent,
1268 ONOS_INSTANCE_ID_1);
1269
1270 // Add the Mastership Event
1271 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1272 topologyMastershipEvent);
1273 events.add(eventEntry);
1274
1275 // Add the Switch Event
1276 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1277 topologySwitchEvent);
1278 events.add(eventEntry);
1279
1280 // Process the events
1281 TestUtils.callMethod(theEventHandler, "processEvents",
1282 List.class, events);
1283
1284 // Check the fired events
1285 topologyEvents = theTopologyListener.topologyEvents;
1286 assertNotNull(topologyEvents);
1287 assertThat(topologyEvents.getAddedMastershipEvents(),
1288 hasItem(mastershipEvent));
1289 assertThat(topologyEvents.getAddedSwitchEvents(),
1290 hasItem(switchEvent));
1291 theTopologyListener.clear();
1292 }
1293
1294 /**
1295 * Tests processing of a misordered Switch Event, and the delivery of the
1296 * topology events.
1297 *
1298 * We test the following scenario:
1299 * - Only a Switch Event is processed first, later followed by a Switch
1300 * Mastership Event - the Switch Event should be delivered after the
1301 * Switch Mastership Event is processed.
1302 */
1303 @Test
1304 public void testProcessMisorderedSwitchEvent() {
1305 TopologyEvents topologyEvents;
1306 List<EventEntry<TopologyEvent>> events = new LinkedList<>();
1307 EventEntry<TopologyEvent> eventEntry;
1308 TopologyEvent topologyMastershipEvent;
1309 TopologyEvent topologySwitchEvent;
1310
1311 setupTopologyManagerWithEventHandler();
1312
1313 // Prepare the Mastership Event
1314 Role role = Role.MASTER;
1315 MastershipEvent mastershipEvent =
1316 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
1317 topologyMastershipEvent = new TopologyEvent(mastershipEvent,
1318 ONOS_INSTANCE_ID_1);
1319
1320 // Prepare the Switch Event
1321 SwitchEvent switchEvent = new SwitchEvent(DPID_1);
1322 topologySwitchEvent = new TopologyEvent(switchEvent,
1323 ONOS_INSTANCE_ID_1);
1324
1325 // Add the Switch Event
1326 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1327 topologySwitchEvent);
1328 events.add(eventEntry);
1329
1330 // Process the events
1331 TestUtils.callMethod(theEventHandler, "processEvents",
1332 List.class, events);
1333
1334 // Check the fired events: no events should be fired
1335 topologyEvents = theTopologyListener.topologyEvents;
1336 assertNull(topologyEvents);
1337 theTopologyListener.clear();
1338 events.clear();
1339
1340 // Add the Mastership Event
1341 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1342 topologyMastershipEvent);
1343 events.add(eventEntry);
1344
1345 // Process the events
1346 TestUtils.callMethod(theEventHandler, "processEvents",
1347 List.class, events);
1348
1349 // Check the fired events: both events should be fired
1350 topologyEvents = theTopologyListener.topologyEvents;
1351 assertNotNull(topologyEvents);
1352 assertThat(topologyEvents.getAddedMastershipEvents(),
1353 hasItem(mastershipEvent));
1354 assertThat(topologyEvents.getAddedSwitchEvents(),
1355 hasItem(switchEvent));
1356 theTopologyListener.clear();
1357 }
1358
1359 /**
1360 * Tests processing of a Switch Event with Mastership Event from
1361 * another ONOS instance, and the delivery of the topology events.
1362 *
1363 * We test the following scenario:
1364 * - Only a Switch Event is processed first, later followed by a Switch
1365 * Mastership Event from another ONOS instance - only the Switch
1366 * Mastership Event should be delivered.
1367 */
1368 @Test
1369 public void testProcessSwitchEventNoMastership() {
1370 TopologyEvents topologyEvents;
1371 List<EventEntry<TopologyEvent>> events = new LinkedList<>();
1372 EventEntry<TopologyEvent> eventEntry;
1373 TopologyEvent topologyMastershipEvent;
1374 TopologyEvent topologySwitchEvent;
1375
1376 setupTopologyManagerWithEventHandler();
1377
1378 // Prepare the Mastership Event
1379 Role role = Role.MASTER;
1380 MastershipEvent mastershipEvent =
1381 new MastershipEvent(DPID_2, ONOS_INSTANCE_ID_2, role);
1382 topologyMastershipEvent = new TopologyEvent(mastershipEvent,
1383 ONOS_INSTANCE_ID_2);
1384
1385 // Prepare the Switch Event
1386 // NOTE: The originator (ONOS_INSTANCE_ID_1) is NOT the Master
1387 SwitchEvent switchEvent = new SwitchEvent(DPID_2);
1388 topologySwitchEvent = new TopologyEvent(switchEvent,
1389 ONOS_INSTANCE_ID_1);
1390
1391 // Add the Switch Event
1392 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1393 topologySwitchEvent);
1394 events.add(eventEntry);
1395
1396 // Process the events
1397 TestUtils.callMethod(theEventHandler, "processEvents",
1398 List.class, events);
1399
1400 // Check the fired events: no events should be fired
1401 topologyEvents = theTopologyListener.topologyEvents;
1402 assertNull(topologyEvents);
1403 theTopologyListener.clear();
1404 events.clear();
1405
1406 // Add the Mastership Event
1407 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1408 topologyMastershipEvent);
1409 events.add(eventEntry);
1410
1411 // Process the events
1412 TestUtils.callMethod(theEventHandler, "processEvents",
1413 List.class, events);
1414
1415 // Check the fired events: only the Mastership event should be fired
1416 topologyEvents = theTopologyListener.topologyEvents;
1417 assertNotNull(topologyEvents);
1418 assertThat(topologyEvents.getAddedMastershipEvents(),
1419 hasItem(mastershipEvent));
1420 assertThat(topologyEvents.getAddedSwitchEvents(), is(empty()));
1421 theTopologyListener.clear();
1422 }
1423
1424 /**
1425 * Tests processing of Switch Events with Mastership switchover between
1426 * two ONOS instance, and the delivery of the topology events.
1427 *
1428 * We test the following scenario:
1429 * - Initially, a Mastership Event and a Switch Event from one ONOS
1430 * instance are processed - both events should be delivered.
1431 * - Later, a Mastership Event and a Switch event from another ONOS
1432 * instances are processed - both events should be delivered.
1433 * - Finally, a REMOVE Switch Event is received from the first ONOS
1434 * instance - no event should be delivered.
1435 */
1436 @Test
1437 public void testProcessSwitchMastershipSwitchover() {
1438 TopologyEvents topologyEvents;
1439 List<EventEntry<TopologyEvent>> events = new LinkedList<>();
1440 EventEntry<TopologyEvent> eventEntry;
1441 TopologyEvent topologyMastershipEvent;
1442 TopologyEvent topologySwitchEvent;
1443
1444 setupTopologyManagerWithEventHandler();
1445
1446 // Prepare the Mastership Event from the first ONOS instance
1447 Role role = Role.MASTER;
1448 MastershipEvent mastershipEvent =
1449 new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role);
1450 topologyMastershipEvent = new TopologyEvent(mastershipEvent,
1451 ONOS_INSTANCE_ID_1);
1452
1453 // Prepare the Switch Event from the first ONOS instance
1454 SwitchEvent switchEvent = new SwitchEvent(DPID_1);
1455 topologySwitchEvent = new TopologyEvent(switchEvent,
1456 ONOS_INSTANCE_ID_1);
1457
1458 // Add the Mastership Event
1459 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1460 topologyMastershipEvent);
1461 events.add(eventEntry);
1462
1463 // Add the Switch Event
1464 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1465 topologySwitchEvent);
1466 events.add(eventEntry);
1467
1468 // Process the events
1469 TestUtils.callMethod(theEventHandler, "processEvents",
1470 List.class, events);
1471
1472 // Check the fired events: both events should be fired
1473 topologyEvents = theTopologyListener.topologyEvents;
1474 assertNotNull(topologyEvents);
1475 assertThat(topologyEvents.getAddedMastershipEvents(),
1476 hasItem(mastershipEvent));
1477 assertThat(topologyEvents.getAddedSwitchEvents(),
1478 hasItem(switchEvent));
1479 theTopologyListener.clear();
1480 events.clear();
1481
1482 //
1483 // Update the Registry Service, so the second ONOS instance is the
1484 // Master.
1485 //
1486 reset(registryService);
1487 try {
1488 expect(registryService.getControllerForSwitch(DPID_1.value())).
1489 andReturn(ONOS_INSTANCE_ID_2.toString()).anyTimes();
1490 } catch (RegistryException ex) {
1491 throw new IllegalStateException(ex);
1492 }
1493 replay(registryService);
1494
1495 // Prepare the Mastership Event from the second ONOS instance
1496 role = Role.MASTER;
1497 mastershipEvent = new MastershipEvent(DPID_1,
1498 ONOS_INSTANCE_ID_2, role);
1499 topologyMastershipEvent = new TopologyEvent(mastershipEvent,
1500 ONOS_INSTANCE_ID_2);
1501
1502 // Prepare the Switch Event from second ONOS instance
1503 switchEvent = new SwitchEvent(DPID_1);
1504 topologySwitchEvent = new TopologyEvent(switchEvent,
1505 ONOS_INSTANCE_ID_2);
1506
1507 // Add the Mastership Event
1508 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1509 topologyMastershipEvent);
1510 events.add(eventEntry);
1511
1512 // Add the Switch Event
1513 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD,
1514 topologySwitchEvent);
1515 events.add(eventEntry);
1516
1517 // Process the events
1518 TestUtils.callMethod(theEventHandler, "processEvents",
1519 List.class, events);
1520
1521 // Check the fired events: both events should be fired
1522 topologyEvents = theTopologyListener.topologyEvents;
1523 assertNotNull(topologyEvents);
1524 assertThat(topologyEvents.getAddedMastershipEvents(),
1525 hasItem(mastershipEvent));
1526 assertThat(topologyEvents.getAddedSwitchEvents(),
1527 hasItem(switchEvent));
1528 theTopologyListener.clear();
1529 events.clear();
1530
1531 // Prepare the REMOVE Switch Event from first ONOS instance
1532 switchEvent = new SwitchEvent(DPID_1);
1533 topologySwitchEvent = new TopologyEvent(switchEvent,
1534 ONOS_INSTANCE_ID_1);
1535 // Add the Switch Event
1536 eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_REMOVE,
1537 topologySwitchEvent);
1538 events.add(eventEntry);
1539
1540 // Process the events
1541 TestUtils.callMethod(theEventHandler, "processEvents",
1542 List.class, events);
1543
1544 // Check the fired events: no events should be fired
1545 topologyEvents = theTopologyListener.topologyEvents;
1546 assertNull(topologyEvents);
1547 theTopologyListener.clear();
1548 events.clear();
1549 }
weibitf7c31a42014-06-23 16:51:01 -07001550}