weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.topology; |
| 2 | |
| 3 | import static org.easymock.EasyMock.anyObject; |
| 4 | import static org.easymock.EasyMock.createMock; |
| 5 | import static org.easymock.EasyMock.eq; |
| 6 | import static org.easymock.EasyMock.expect; |
| 7 | import static org.easymock.EasyMock.replay; |
| 8 | import static org.easymock.EasyMock.verify; |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 9 | import static org.junit.Assert.*; |
| 10 | import static org.hamcrest.Matchers.*; |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 11 | |
| 12 | import java.util.ArrayList; |
| 13 | import java.util.Collection; |
| 14 | import java.util.List; |
| 15 | import java.util.concurrent.CopyOnWriteArrayList; |
| 16 | |
| 17 | import net.floodlightcontroller.util.MACAddress; |
| 18 | import net.onrc.onos.core.datagrid.IDatagridService; |
| 19 | import net.onrc.onos.core.datagrid.IEventChannel; |
| 20 | import net.onrc.onos.core.datagrid.IEventChannelListener; |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame^] | 21 | import net.onrc.onos.core.metrics.OnosMetrics; |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 22 | import net.onrc.onos.core.registry.IControllerRegistryService; |
| 23 | import net.onrc.onos.core.util.Dpid; |
| 24 | import net.onrc.onos.core.util.PortNumber; |
| 25 | import net.onrc.onos.core.util.SwitchPort; |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 26 | import net.onrc.onos.core.util.TestUtils; |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 27 | |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame^] | 28 | import com.codahale.metrics.MetricFilter; |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 29 | import org.easymock.EasyMock; |
| 30 | import org.junit.After; |
| 31 | import org.junit.Before; |
| 32 | import org.junit.Test; |
| 33 | |
| 34 | /** |
| 35 | * Unit tests for the TopologyManager class in the Topology module. |
| 36 | * These test cases only check the sanity of functions in the TopologyManager. |
| 37 | * Note that we do not test the eventHandler functions in the TopologyManager class. |
| 38 | * DatagridService, DataStoreService, eventChannel, and controllerRegistryService are mocked out. |
| 39 | */ |
| 40 | public class TopologyManagerTest { |
| 41 | private TopologyManager theTopologyManager; |
| 42 | private final String eventChannelName = "onos.topology"; |
| 43 | private IEventChannel<byte[], TopologyEvent> eventChannel; |
| 44 | private IDatagridService datagridService; |
| 45 | private TopologyDatastore dataStoreService; |
| 46 | private IControllerRegistryService registryService; |
| 47 | private CopyOnWriteArrayList<ITopologyListener> topologyListeners; |
| 48 | private Collection<TopologyEvent> allTopologyEvents; |
| 49 | |
| 50 | @SuppressWarnings("unchecked") |
| 51 | @Before |
| 52 | public void setUp() throws Exception { |
| 53 | // Mock objects for testing |
| 54 | datagridService = EasyMock.createNiceMock(IDatagridService.class); |
| 55 | dataStoreService = EasyMock.createNiceMock(TopologyDatastore.class); |
| 56 | registryService = createMock(IControllerRegistryService.class); |
| 57 | eventChannel = EasyMock.createNiceMock(IEventChannel.class); |
| 58 | |
| 59 | expect(datagridService.createChannel( |
| 60 | eq(eventChannelName), |
| 61 | eq(byte[].class), |
| 62 | eq(TopologyEvent.class))) |
| 63 | .andReturn(eventChannel).once(); |
| 64 | |
| 65 | expect(datagridService.addListener( |
| 66 | eq(eventChannelName), |
| 67 | anyObject(IEventChannelListener.class), |
| 68 | eq(byte[].class), |
| 69 | eq(TopologyEvent.class))) |
| 70 | .andReturn(eventChannel).once(); |
| 71 | |
| 72 | expect(dataStoreService.addSwitch( |
| 73 | anyObject(SwitchEvent.class), |
| 74 | anyObject(Collection.class))) |
| 75 | .andReturn(true).anyTimes(); |
| 76 | |
| 77 | expect(dataStoreService.deactivateSwitch( |
| 78 | anyObject(SwitchEvent.class), |
| 79 | anyObject(Collection.class))) |
| 80 | .andReturn(true).anyTimes(); |
| 81 | |
| 82 | expect(dataStoreService.addPort( |
| 83 | anyObject(PortEvent.class))) |
| 84 | .andReturn(true).anyTimes(); |
| 85 | |
| 86 | expect(dataStoreService.deactivatePort( |
| 87 | anyObject(PortEvent.class))) |
| 88 | .andReturn(true).anyTimes(); |
| 89 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 90 | expect(dataStoreService.addHost( |
| 91 | anyObject(HostEvent.class))) |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 92 | .andReturn(true).anyTimes(); |
| 93 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 94 | expect(dataStoreService.removeHost( |
| 95 | anyObject(HostEvent.class))) |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 96 | .andReturn(true).anyTimes(); |
| 97 | |
| 98 | expect(dataStoreService.addLink( |
| 99 | anyObject(LinkEvent.class))) |
| 100 | .andReturn(true).anyTimes(); |
| 101 | |
| 102 | expect(dataStoreService.removeLink( |
| 103 | anyObject(LinkEvent.class))) |
| 104 | .andReturn(true).anyTimes(); |
| 105 | |
| 106 | replay(datagridService); |
| 107 | replay(registryService); |
| 108 | replay(dataStoreService); |
| 109 | |
| 110 | allTopologyEvents = new CopyOnWriteArrayList<>(); |
Yuta HIGUCHI | 81d8b9e | 2014-07-14 23:56:34 -0700 | [diff] [blame] | 111 | expect(eventChannel.getAllEntries()) |
| 112 | .andReturn(allTopologyEvents).anyTimes(); |
| 113 | } |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 114 | |
Yuta HIGUCHI | 81d8b9e | 2014-07-14 23:56:34 -0700 | [diff] [blame] | 115 | private void setupTopologyManager() { |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 116 | // Create a topologyManager object for testing |
| 117 | topologyListeners = new CopyOnWriteArrayList<>(); |
| 118 | theTopologyManager = new TopologyManager(registryService, topologyListeners); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 119 | |
| 120 | // replace EventHandler to avoid thread from starting |
| 121 | TestUtils.setField(theTopologyManager, "eventHandler", |
| 122 | EasyMock.createNiceMock(TopologyManager.EventHandler.class)); |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 123 | theTopologyManager.startup(datagridService); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 124 | |
| 125 | // replace data store with Mocked object |
| 126 | TestUtils.setField(theTopologyManager, "datastore", dataStoreService); |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @After |
| 130 | public void tearDown() throws Exception { |
Pavlin Radoslavov | d4f4037 | 2014-07-18 16:58:40 -0700 | [diff] [blame^] | 131 | OnosMetrics.removeMatching(MetricFilter.ALL); |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Test the Switch discovered and Port discovered functions. |
| 136 | */ |
| 137 | @Test |
| 138 | public void testPutSwitchAndPortDiscoveryEvent() { |
| 139 | // Mock the eventChannel functions first |
| 140 | eventChannel.addEntry(anyObject(byte[].class), |
| 141 | anyObject(TopologyEvent.class)); |
| 142 | EasyMock.expectLastCall().times(3, 3); // (1 switch + 1 port), 1 port |
| 143 | replay(eventChannel); |
| 144 | |
Yuta HIGUCHI | 81d8b9e | 2014-07-14 23:56:34 -0700 | [diff] [blame] | 145 | setupTopologyManager(); |
| 146 | |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 147 | // mockSwitch has one port |
| 148 | Dpid swDPId = new Dpid(100L); |
| 149 | PortNumber portId = new PortNumber((short) 1); |
| 150 | |
| 151 | // Generate a new switch event along with a port event |
| 152 | SwitchEvent switchEvent = new SwitchEvent(swDPId); |
| 153 | |
| 154 | Collection<PortEvent> portEvents = new ArrayList<PortEvent>(); |
| 155 | portEvents.add(new PortEvent(swDPId, portId)); |
| 156 | |
| 157 | // Call the topologyManager function for adding a switch |
| 158 | theTopologyManager.putSwitchDiscoveryEvent(switchEvent, portEvents); |
| 159 | |
| 160 | for (PortEvent portEvent : portEvents) { |
| 161 | // Call the topologyManager function for adding a port |
| 162 | theTopologyManager.putPortDiscoveryEvent(portEvent); |
| 163 | } |
| 164 | |
| 165 | // Verify the function calls |
| 166 | verify(eventChannel); |
| 167 | |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Test the switch and port removed functions. |
| 172 | */ |
| 173 | @Test |
| 174 | public void testRemoveSwitchAndPortDiscoveryEvent() { |
| 175 | // Mock the eventChannel functions first |
| 176 | eventChannel.removeEntry(anyObject(byte[].class)); |
| 177 | EasyMock.expectLastCall().times(2, 2); //1 switch, 1 port |
| 178 | replay(eventChannel); |
| 179 | |
Yuta HIGUCHI | 81d8b9e | 2014-07-14 23:56:34 -0700 | [diff] [blame] | 180 | setupTopologyManager(); |
| 181 | |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 182 | Dpid swDPId = new Dpid(100L); |
| 183 | PortNumber portId = new PortNumber((short) 1); |
| 184 | |
| 185 | // Generate a port event |
| 186 | Collection<PortEvent> portEvents = new ArrayList<PortEvent>(); |
| 187 | portEvents.add(new PortEvent(swDPId, portId)); |
| 188 | |
| 189 | // Call the topologyManager function for removing a port |
| 190 | for (PortEvent portEvent : portEvents) { |
| 191 | theTopologyManager.removePortDiscoveryEvent(portEvent); |
| 192 | } |
| 193 | |
| 194 | // Call the topologyManager function for removing a switch |
| 195 | SwitchEvent switchEvent = new SwitchEvent(swDPId); |
| 196 | theTopologyManager.removeSwitchDiscoveryEvent(switchEvent); |
| 197 | |
| 198 | // Verify the function calls |
| 199 | verify(eventChannel); |
| 200 | |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Test the device discovered function. |
| 205 | */ |
| 206 | @Test |
| 207 | public void testPutDeviceDiscoveryEvent() { |
| 208 | // Mock the eventChannel functions first |
| 209 | eventChannel.addEntry(anyObject(byte[].class), |
| 210 | anyObject(TopologyEvent.class)); |
| 211 | EasyMock.expectLastCall().times(1, 1); // 1 device |
| 212 | replay(eventChannel); |
| 213 | |
Yuta HIGUCHI | 81d8b9e | 2014-07-14 23:56:34 -0700 | [diff] [blame] | 214 | setupTopologyManager(); |
| 215 | |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 216 | long swDPId = 100L; |
| 217 | long portId = 1L; |
| 218 | |
| 219 | // Generate a new device event |
| 220 | MACAddress devMac = MACAddress.valueOf("00:AA:11:BB:33:CC"); |
| 221 | SwitchPort sp = new SwitchPort(swDPId, portId); |
| 222 | List<SwitchPort> spLists = new ArrayList<SwitchPort>(); |
| 223 | spLists.add(sp); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 224 | HostEvent hostEvent = new HostEvent(devMac); |
| 225 | hostEvent.setAttachmentPoints(spLists); |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 226 | |
| 227 | // Call the topologyManager function for adding a device |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 228 | theTopologyManager.putHostDiscoveryEvent(hostEvent); |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 229 | |
| 230 | // Verify the function calls |
| 231 | verify(eventChannel); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Test the device removed function. |
| 236 | */ |
| 237 | @Test |
| 238 | public void testRemoveDeviceDiscoveryEvent() { |
| 239 | // Mock the eventChannel functions first |
| 240 | eventChannel.removeEntry(anyObject(byte[].class)); |
| 241 | EasyMock.expectLastCall().times(1, 1); // 1 device |
| 242 | replay(eventChannel); |
| 243 | |
Yuta HIGUCHI | 81d8b9e | 2014-07-14 23:56:34 -0700 | [diff] [blame] | 244 | setupTopologyManager(); |
| 245 | |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 246 | long swDPId = 100L; |
| 247 | long portId = 1L; |
| 248 | |
| 249 | // Generate a new device event |
| 250 | MACAddress devMac = MACAddress.valueOf("00:AA:11:BB:33:CC"); |
| 251 | SwitchPort sp = new SwitchPort(swDPId, portId); |
| 252 | List<SwitchPort> spLists = new ArrayList<SwitchPort>(); |
| 253 | spLists.add(sp); |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 254 | HostEvent hostEvent = new HostEvent(devMac); |
| 255 | hostEvent.setAttachmentPoints(spLists); |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 256 | |
| 257 | // Call the topologyManager function for removing a device |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 258 | theTopologyManager.removeHostDiscoveryEvent(hostEvent); |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 259 | |
| 260 | // Verify the function calls |
| 261 | verify(eventChannel); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Test the link discovered function. |
| 266 | */ |
| 267 | @Test |
| 268 | public void testPutLinkDiscoveryEvent() { |
| 269 | // Mock the eventChannel functions first |
| 270 | eventChannel.addEntry(anyObject(byte[].class), |
| 271 | anyObject(TopologyEvent.class)); |
| 272 | EasyMock.expectLastCall().times(5, 5); // (2 switch + 2 port + 1 link) |
| 273 | replay(eventChannel); |
| 274 | |
Yuta HIGUCHI | 81d8b9e | 2014-07-14 23:56:34 -0700 | [diff] [blame] | 275 | setupTopologyManager(); |
| 276 | |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 277 | // Assign the switch and port IDs |
| 278 | Dpid sw1DPId = new Dpid(100L); |
| 279 | PortNumber port1Id = new PortNumber((short) 1); |
| 280 | Dpid sw2DPId = new Dpid(200L); |
| 281 | PortNumber port2Id = new PortNumber((short) 2); |
| 282 | |
| 283 | // Generate the switch and port events |
| 284 | SwitchEvent switchEvent1 = new SwitchEvent(sw1DPId); |
| 285 | Collection<PortEvent> portEvents1 = new ArrayList<PortEvent>(); |
| 286 | portEvents1.add(new PortEvent(sw1DPId, port1Id)); |
| 287 | |
| 288 | // Call the topologyManager function for adding a switch |
| 289 | theTopologyManager.putSwitchDiscoveryEvent(switchEvent1, portEvents1); |
| 290 | |
| 291 | // Generate the switch and port events |
| 292 | SwitchEvent switchEvent2 = new SwitchEvent(sw2DPId); |
| 293 | Collection<PortEvent> portEvents2 = new ArrayList<PortEvent>(); |
| 294 | portEvents2.add(new PortEvent(sw2DPId, port2Id)); |
| 295 | |
| 296 | // Call the topologyManager function for adding a switch |
| 297 | theTopologyManager.putSwitchDiscoveryEvent(switchEvent2, portEvents2); |
| 298 | |
| 299 | // Create the link |
| 300 | LinkEvent linkEvent = new LinkEvent(sw1DPId, port1Id, sw2DPId, port2Id); |
| 301 | theTopologyManager.putLinkDiscoveryEvent(linkEvent); |
| 302 | |
| 303 | // Verify the function calls |
| 304 | verify(eventChannel); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Test the link removed function. |
| 309 | */ |
| 310 | @Test |
| 311 | public void testRemoveLinkDiscoveryEvent() { |
| 312 | // Mock the eventChannel functions first |
| 313 | eventChannel.removeEntry(anyObject(byte[].class)); |
| 314 | EasyMock.expectLastCall().times(1, 1); // (1 link) |
| 315 | replay(eventChannel); |
| 316 | |
Yuta HIGUCHI | 81d8b9e | 2014-07-14 23:56:34 -0700 | [diff] [blame] | 317 | setupTopologyManager(); |
| 318 | |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 319 | // Assign the switch and port IDs |
| 320 | Dpid sw1DPId = new Dpid(100L); |
| 321 | PortNumber port1Id = new PortNumber((short) 1); |
| 322 | Dpid sw2DPId = new Dpid(200L); |
| 323 | PortNumber port2Id = new PortNumber((short) 2); |
| 324 | |
| 325 | // Generate the switch and port events |
| 326 | SwitchEvent switchEvent1 = new SwitchEvent(sw1DPId); |
| 327 | Collection<PortEvent> portEvents1 = new ArrayList<PortEvent>(); |
| 328 | portEvents1.add(new PortEvent(sw1DPId, port1Id)); |
| 329 | |
| 330 | // Call the topologyManager function for adding a switch |
| 331 | theTopologyManager.putSwitchDiscoveryEvent(switchEvent1, portEvents1); |
| 332 | |
| 333 | // Generate the switch and port events |
| 334 | SwitchEvent switchEvent2 = new SwitchEvent(sw2DPId); |
| 335 | Collection<PortEvent> portEvents2 = new ArrayList<PortEvent>(); |
| 336 | portEvents2.add(new PortEvent(sw2DPId, port2Id)); |
| 337 | |
| 338 | // Call the topologyManager function for adding a switch |
| 339 | theTopologyManager.putSwitchDiscoveryEvent(switchEvent2, portEvents2); |
| 340 | |
| 341 | // Remove the link |
| 342 | LinkEvent linkEventRemove = new LinkEvent(sw1DPId, port1Id, sw2DPId, port2Id); |
| 343 | theTopologyManager.removeLinkDiscoveryEvent(linkEventRemove); |
| 344 | |
| 345 | // Verify the function calls |
| 346 | verify(eventChannel); |
| 347 | } |
| 348 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 349 | /** |
| 350 | * Test to confirm topology replica transformation. |
| 351 | */ |
| 352 | @Test |
| 353 | public void testAddSwitch() { |
| 354 | setupTopologyManager(); |
| 355 | |
| 356 | final Dpid dpid = new Dpid(1); |
| 357 | SwitchEvent sw = new SwitchEvent(dpid); |
| 358 | sw.createStringAttribute("foo", "bar"); |
| 359 | |
| 360 | TestUtils.callMethod(theTopologyManager, "addSwitch", SwitchEvent.class, sw); |
| 361 | |
| 362 | // check topology structure |
| 363 | TopologyInternal topology = (TopologyInternal) theTopologyManager.getTopology(); |
| 364 | SwitchEvent swInTopo = topology.getSwitchEvent(dpid); |
| 365 | assertEquals(sw, swInTopo); |
| 366 | assertTrue(swInTopo.isFrozen()); |
| 367 | assertEquals("bar", swInTopo.getStringAttribute("foo")); |
| 368 | |
| 369 | // check events to be fired |
| 370 | List<SwitchEvent> apiAddedSwitchEvents |
| 371 | = TestUtils.getField(theTopologyManager, "apiAddedSwitchEvents"); |
| 372 | assertThat(apiAddedSwitchEvents, hasItem(sw)); |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * Test to confirm topology replica transformation. |
| 377 | */ |
| 378 | @Test |
| 379 | public void testAddPort() { |
| 380 | setupTopologyManager(); |
| 381 | |
| 382 | final Dpid dpid = new Dpid(1); |
| 383 | SwitchEvent sw = new SwitchEvent(dpid); |
| 384 | sw.createStringAttribute("foo", "bar"); |
| 385 | |
| 386 | final PortNumber portNumber = new PortNumber((short) 2); |
| 387 | PortEvent port = new PortEvent(dpid, portNumber); |
| 388 | port.createStringAttribute("fuzz", "buzz"); |
| 389 | |
| 390 | TestUtils.callMethod(theTopologyManager, "addSwitch", SwitchEvent.class, sw); |
| 391 | TestUtils.callMethod(theTopologyManager, "addPort", PortEvent.class, port); |
| 392 | |
| 393 | // check topology structure |
| 394 | TopologyInternal topology = (TopologyInternal) theTopologyManager.getTopology(); |
| 395 | SwitchEvent swInTopo = topology.getSwitchEvent(dpid); |
| 396 | assertEquals(sw, swInTopo); |
| 397 | assertTrue(swInTopo.isFrozen()); |
| 398 | assertEquals("bar", swInTopo.getStringAttribute("foo")); |
| 399 | |
| 400 | final SwitchPort portId = new SwitchPort(dpid, portNumber); |
| 401 | PortEvent portInTopo = topology.getPortEvent(portId); |
| 402 | assertEquals(port, portInTopo); |
| 403 | assertTrue(portInTopo.isFrozen()); |
| 404 | assertEquals("buzz", portInTopo.getStringAttribute("fuzz")); |
| 405 | |
| 406 | // check events to be fired |
| 407 | List<PortEvent> apiAddedPortEvents |
| 408 | = TestUtils.getField(theTopologyManager, "apiAddedPortEvents"); |
| 409 | assertThat(apiAddedPortEvents, hasItem(port)); |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Test to confirm topology replica transformation. |
| 414 | */ |
| 415 | @Test |
| 416 | public void testRemovePortThenSwitch() { |
| 417 | setupTopologyManager(); |
| 418 | |
| 419 | final Dpid dpid = new Dpid(1); |
| 420 | SwitchEvent sw = new SwitchEvent(dpid); |
| 421 | sw.createStringAttribute("foo", "bar"); |
| 422 | |
| 423 | final PortNumber portNumber = new PortNumber((short) 2); |
| 424 | PortEvent port = new PortEvent(dpid, portNumber); |
| 425 | port.createStringAttribute("fuzz", "buzz"); |
| 426 | |
| 427 | TestUtils.callMethod(theTopologyManager, "addSwitch", SwitchEvent.class, sw); |
| 428 | TestUtils.callMethod(theTopologyManager, "addPort", PortEvent.class, port); |
| 429 | |
| 430 | // check topology structure |
| 431 | TopologyInternal topology = (TopologyInternal) theTopologyManager.getTopology(); |
| 432 | SwitchEvent swInTopo = topology.getSwitchEvent(dpid); |
| 433 | assertEquals(sw, swInTopo); |
| 434 | assertTrue(swInTopo.isFrozen()); |
| 435 | assertEquals("bar", swInTopo.getStringAttribute("foo")); |
| 436 | |
| 437 | final SwitchPort portId = new SwitchPort(dpid, portNumber); |
| 438 | PortEvent portInTopo = topology.getPortEvent(portId); |
| 439 | assertEquals(port, portInTopo); |
| 440 | assertTrue(portInTopo.isFrozen()); |
| 441 | assertEquals("buzz", portInTopo.getStringAttribute("fuzz")); |
| 442 | |
| 443 | // remove in proper order |
| 444 | TestUtils.callMethod(theTopologyManager, "removePort", |
| 445 | PortEvent.class, new PortEvent(port)); |
| 446 | TestUtils.callMethod(theTopologyManager, "removeSwitch", |
| 447 | SwitchEvent.class, new SwitchEvent(sw)); |
| 448 | |
| 449 | |
| 450 | // check events to be fired |
| 451 | List<PortEvent> apiRemovedPortEvents |
| 452 | = TestUtils.getField(theTopologyManager, "apiRemovedPortEvents"); |
| 453 | assertThat(apiRemovedPortEvents, hasItem(port)); |
| 454 | List<SwitchEvent> apiRemovedSwitchEvents |
| 455 | = TestUtils.getField(theTopologyManager, "apiRemovedSwitchEvents"); |
| 456 | assertThat(apiRemovedSwitchEvents, hasItem(sw)); |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * Test to confirm topology replica transformation. |
| 461 | */ |
| 462 | @Test |
| 463 | public void testRemoveSwitch() { |
| 464 | setupTopologyManager(); |
| 465 | |
| 466 | final Dpid dpid = new Dpid(1); |
| 467 | SwitchEvent sw = new SwitchEvent(dpid); |
| 468 | sw.createStringAttribute("foo", "bar"); |
| 469 | |
| 470 | final PortNumber portNumber = new PortNumber((short) 2); |
| 471 | PortEvent port = new PortEvent(dpid, portNumber); |
| 472 | port.createStringAttribute("fuzz", "buzz"); |
| 473 | |
| 474 | TestUtils.callMethod(theTopologyManager, "addSwitch", SwitchEvent.class, sw); |
| 475 | TestUtils.callMethod(theTopologyManager, "addPort", PortEvent.class, port); |
| 476 | |
| 477 | // check topology structure |
| 478 | TopologyInternal topology = (TopologyInternal) theTopologyManager.getTopology(); |
| 479 | SwitchEvent swInTopo = topology.getSwitchEvent(dpid); |
| 480 | assertEquals(sw, swInTopo); |
| 481 | assertTrue(swInTopo.isFrozen()); |
| 482 | assertEquals("bar", swInTopo.getStringAttribute("foo")); |
| 483 | |
| 484 | final SwitchPort portId = new SwitchPort(dpid, portNumber); |
| 485 | PortEvent portInTopo = topology.getPortEvent(portId); |
| 486 | assertEquals(port, portInTopo); |
| 487 | assertTrue(portInTopo.isFrozen()); |
| 488 | assertEquals("buzz", portInTopo.getStringAttribute("fuzz")); |
| 489 | |
| 490 | // remove in in-proper order |
| 491 | // TestUtils.callMethod(theTopologyManager, "removePort", |
| 492 | // PortEvent.class, new PortEvent(port)); |
| 493 | TestUtils.callMethod(theTopologyManager, "removeSwitch", |
| 494 | SwitchEvent.class, new SwitchEvent(sw)); |
| 495 | |
| 496 | |
| 497 | // check events to be fired |
| 498 | // outcome should be the same as #testRemovePortThenSwitch |
| 499 | List<PortEvent> apiRemovedPortEvents |
| 500 | = TestUtils.getField(theTopologyManager, "apiRemovedPortEvents"); |
| 501 | assertThat(apiRemovedPortEvents, hasItem(port)); |
| 502 | List<SwitchEvent> apiRemovedSwitchEvents |
| 503 | = TestUtils.getField(theTopologyManager, "apiRemovedSwitchEvents"); |
| 504 | assertThat(apiRemovedSwitchEvents, hasItem(sw)); |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * Test to confirm topology replica transformation. |
| 509 | */ |
| 510 | @Test |
| 511 | public void testAddLink() { |
| 512 | setupTopologyManager(); |
| 513 | |
| 514 | final Dpid dpid = new Dpid(1); |
| 515 | SwitchEvent sw = new SwitchEvent(dpid); |
| 516 | sw.createStringAttribute("foo", "bar"); |
| 517 | |
| 518 | final PortNumber portNumberA = new PortNumber((short) 2); |
| 519 | PortEvent portA = new PortEvent(dpid, portNumberA); |
| 520 | portA.createStringAttribute("fuzz", "buzz"); |
| 521 | |
| 522 | final PortNumber portNumberB = new PortNumber((short) 3); |
| 523 | PortEvent portB = new PortEvent(dpid, portNumberB); |
| 524 | portB.createStringAttribute("fizz", "buz"); |
| 525 | |
| 526 | LinkEvent linkA = new LinkEvent(portA.getSwitchPort(), portB.getSwitchPort()); |
| 527 | linkA.createStringAttribute(TopologyElement.TYPE, |
| 528 | TopologyElement.TYPE_OPTICAL_LAYER); |
| 529 | LinkEvent linkB = new LinkEvent(portB.getSwitchPort(), portA.getSwitchPort()); |
| 530 | linkB.createStringAttribute(TopologyElement.TYPE, |
| 531 | TopologyElement.TYPE_OPTICAL_LAYER); |
| 532 | |
| 533 | TestUtils.callMethod(theTopologyManager, "addSwitch", SwitchEvent.class, sw); |
| 534 | TestUtils.callMethod(theTopologyManager, "addPort", PortEvent.class, portA); |
| 535 | TestUtils.callMethod(theTopologyManager, "addPort", PortEvent.class, portB); |
| 536 | TestUtils.callMethod(theTopologyManager, "addLink", LinkEvent.class, linkA); |
| 537 | TestUtils.callMethod(theTopologyManager, "addLink", LinkEvent.class, linkB); |
| 538 | |
| 539 | // check topology structure |
| 540 | TopologyInternal topology = (TopologyInternal) theTopologyManager.getTopology(); |
| 541 | SwitchEvent swInTopo = topology.getSwitchEvent(dpid); |
| 542 | assertEquals(sw, swInTopo); |
| 543 | assertTrue(swInTopo.isFrozen()); |
| 544 | assertEquals("bar", swInTopo.getStringAttribute("foo")); |
| 545 | |
| 546 | final SwitchPort portIdA = new SwitchPort(dpid, portNumberA); |
| 547 | PortEvent portAInTopo = topology.getPortEvent(portIdA); |
| 548 | assertEquals(portA, portAInTopo); |
| 549 | assertTrue(portAInTopo.isFrozen()); |
| 550 | assertEquals("buzz", portAInTopo.getStringAttribute("fuzz")); |
| 551 | |
| 552 | final SwitchPort portIdB = new SwitchPort(dpid, portNumberB); |
| 553 | PortEvent portBInTopo = topology.getPortEvent(portIdB); |
| 554 | assertEquals(portB, portBInTopo); |
| 555 | assertTrue(portBInTopo.isFrozen()); |
| 556 | assertEquals("buz", portBInTopo.getStringAttribute("fizz")); |
| 557 | |
| 558 | LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple()); |
| 559 | assertEquals(linkA, linkAInTopo); |
| 560 | assertTrue(linkAInTopo.isFrozen()); |
| 561 | assertEquals(TopologyElement.TYPE_OPTICAL_LAYER, linkAInTopo.getType()); |
| 562 | |
| 563 | LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple()); |
| 564 | assertEquals(linkB, linkBInTopo); |
| 565 | assertTrue(linkBInTopo.isFrozen()); |
| 566 | assertEquals(TopologyElement.TYPE_OPTICAL_LAYER, linkBInTopo.getType()); |
| 567 | |
| 568 | // check events to be fired |
| 569 | List<LinkEvent> apiAddedLinkEvents |
| 570 | = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents"); |
| 571 | assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB)); |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * Test to confirm topology replica transformation. |
| 576 | */ |
| 577 | @Test |
| 578 | public void testAddLinkKickingOffHost() { |
| 579 | setupTopologyManager(); |
| 580 | |
| 581 | final Dpid dpid = new Dpid(1); |
| 582 | SwitchEvent sw = new SwitchEvent(dpid); |
| 583 | sw.createStringAttribute("foo", "bar"); |
| 584 | |
| 585 | final PortNumber portNumberA = new PortNumber((short) 2); |
| 586 | PortEvent portA = new PortEvent(dpid, portNumberA); |
| 587 | portA.createStringAttribute("fuzz", "buzz"); |
| 588 | |
| 589 | final PortNumber portNumberB = new PortNumber((short) 3); |
| 590 | PortEvent portB = new PortEvent(dpid, portNumberB); |
| 591 | portB.createStringAttribute("fizz", "buz"); |
| 592 | |
| 593 | final PortNumber portNumberC = new PortNumber((short) 4); |
| 594 | PortEvent portC = new PortEvent(dpid, portNumberC); |
| 595 | portC.createStringAttribute("fizz", "buz"); |
| 596 | |
| 597 | final MACAddress macA = MACAddress.valueOf(666L); |
| 598 | HostEvent hostA = new HostEvent(macA); |
| 599 | hostA.addAttachmentPoint(portA.getSwitchPort()); |
| 600 | final long timestampA = 392893200L; |
| 601 | hostA.setLastSeenTime(timestampA); |
| 602 | |
| 603 | final MACAddress macB = MACAddress.valueOf(999L); |
| 604 | HostEvent hostB = new HostEvent(macB); |
| 605 | hostB.addAttachmentPoint(portB.getSwitchPort()); |
| 606 | hostB.addAttachmentPoint(portC.getSwitchPort()); |
| 607 | final long timestampB = 392893201L; |
| 608 | hostB.setLastSeenTime(timestampB); |
| 609 | |
| 610 | |
| 611 | LinkEvent linkA = new LinkEvent(portA.getSwitchPort(), portB.getSwitchPort()); |
| 612 | linkA.createStringAttribute(TopologyElement.TYPE, |
| 613 | TopologyElement.TYPE_OPTICAL_LAYER); |
| 614 | LinkEvent linkB = new LinkEvent(portB.getSwitchPort(), portA.getSwitchPort()); |
| 615 | linkB.createStringAttribute(TopologyElement.TYPE, |
| 616 | TopologyElement.TYPE_OPTICAL_LAYER); |
| 617 | |
| 618 | TestUtils.callMethod(theTopologyManager, "addSwitch", SwitchEvent.class, sw); |
| 619 | TestUtils.callMethod(theTopologyManager, "addPort", PortEvent.class, portA); |
| 620 | TestUtils.callMethod(theTopologyManager, "addPort", PortEvent.class, portB); |
| 621 | TestUtils.callMethod(theTopologyManager, "addPort", PortEvent.class, portC); |
| 622 | TestUtils.callMethod(theTopologyManager, "addHost", HostEvent.class, hostA); |
| 623 | TestUtils.callMethod(theTopologyManager, "addHost", HostEvent.class, hostB); |
| 624 | |
| 625 | TestUtils.callMethod(theTopologyManager, "addLink", LinkEvent.class, linkA); |
| 626 | TestUtils.callMethod(theTopologyManager, "addLink", LinkEvent.class, linkB); |
| 627 | |
| 628 | // check topology structure |
| 629 | TopologyInternal topology = (TopologyInternal) theTopologyManager.getTopology(); |
| 630 | SwitchEvent swInTopo = topology.getSwitchEvent(dpid); |
| 631 | assertEquals(sw, swInTopo); |
| 632 | assertTrue(swInTopo.isFrozen()); |
| 633 | assertEquals("bar", swInTopo.getStringAttribute("foo")); |
| 634 | |
| 635 | final SwitchPort portIdA = new SwitchPort(dpid, portNumberA); |
| 636 | PortEvent portAInTopo = topology.getPortEvent(portIdA); |
| 637 | assertEquals(portA, portAInTopo); |
| 638 | assertTrue(portAInTopo.isFrozen()); |
| 639 | assertEquals("buzz", portAInTopo.getStringAttribute("fuzz")); |
| 640 | |
| 641 | final SwitchPort portIdB = new SwitchPort(dpid, portNumberB); |
| 642 | PortEvent portBInTopo = topology.getPortEvent(portIdB); |
| 643 | assertEquals(portB, portBInTopo); |
| 644 | assertTrue(portBInTopo.isFrozen()); |
| 645 | assertEquals("buz", portBInTopo.getStringAttribute("fizz")); |
| 646 | |
| 647 | // hostA expected to be removed |
| 648 | assertNull(topology.getHostEvent(macA)); |
| 649 | // hostB expected to be there with reduced attachment point |
| 650 | HostEvent hostBrev = new HostEvent(macB); |
| 651 | hostBrev.addAttachmentPoint(portC.getSwitchPort()); |
| 652 | hostBrev.setLastSeenTime(timestampB); |
| 653 | hostBrev.freeze(); |
| 654 | assertEquals(hostBrev, topology.getHostEvent(macB)); |
| 655 | |
| 656 | |
| 657 | LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple()); |
| 658 | assertEquals(linkA, linkAInTopo); |
| 659 | assertTrue(linkAInTopo.isFrozen()); |
| 660 | assertEquals(TopologyElement.TYPE_OPTICAL_LAYER, linkAInTopo.getType()); |
| 661 | |
| 662 | LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple()); |
| 663 | assertEquals(linkB, linkBInTopo); |
| 664 | assertTrue(linkBInTopo.isFrozen()); |
| 665 | assertEquals(TopologyElement.TYPE_OPTICAL_LAYER, linkBInTopo.getType()); |
| 666 | |
| 667 | // check events to be fired |
| 668 | List<HostEvent> apiAddedHostEvents |
| 669 | = TestUtils.getField(theTopologyManager, "apiAddedHostEvents"); |
| 670 | assertThat(apiAddedHostEvents, hasItem(hostBrev)); |
| 671 | |
| 672 | List<HostEvent> apiRemovedHostEvents |
| 673 | = TestUtils.getField(theTopologyManager, "apiRemovedHostEvents"); |
| 674 | assertThat(apiRemovedHostEvents, hasItem(hostA)); |
| 675 | List<LinkEvent> apiAddedLinkEvents |
| 676 | = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents"); |
| 677 | assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB)); |
| 678 | } |
| 679 | |
| 680 | /** |
| 681 | * Test to confirm topology replica transformation. |
| 682 | */ |
| 683 | @Test |
| 684 | public void testRemoveLink() { |
| 685 | setupTopologyManager(); |
| 686 | |
| 687 | final Dpid dpid = new Dpid(1); |
| 688 | SwitchEvent sw = new SwitchEvent(dpid); |
| 689 | sw.createStringAttribute("foo", "bar"); |
| 690 | |
| 691 | final PortNumber portNumberA = new PortNumber((short) 2); |
| 692 | PortEvent portA = new PortEvent(dpid, portNumberA); |
| 693 | portA.createStringAttribute("fuzz", "buzz"); |
| 694 | |
| 695 | final PortNumber portNumberB = new PortNumber((short) 3); |
| 696 | PortEvent portB = new PortEvent(dpid, portNumberB); |
| 697 | portB.createStringAttribute("fizz", "buz"); |
| 698 | |
| 699 | LinkEvent linkA = new LinkEvent(portA.getSwitchPort(), portB.getSwitchPort()); |
| 700 | linkA.createStringAttribute(TopologyElement.TYPE, |
| 701 | TopologyElement.TYPE_OPTICAL_LAYER); |
| 702 | LinkEvent linkB = new LinkEvent(portB.getSwitchPort(), portA.getSwitchPort()); |
| 703 | linkB.createStringAttribute(TopologyElement.TYPE, |
| 704 | TopologyElement.TYPE_OPTICAL_LAYER); |
| 705 | |
| 706 | TestUtils.callMethod(theTopologyManager, "addSwitch", SwitchEvent.class, sw); |
| 707 | TestUtils.callMethod(theTopologyManager, "addPort", PortEvent.class, portA); |
| 708 | TestUtils.callMethod(theTopologyManager, "addPort", PortEvent.class, portB); |
| 709 | TestUtils.callMethod(theTopologyManager, "addLink", LinkEvent.class, linkA); |
| 710 | TestUtils.callMethod(theTopologyManager, "addLink", LinkEvent.class, linkB); |
| 711 | |
| 712 | // check topology structure |
| 713 | TopologyInternal topology = (TopologyInternal) theTopologyManager.getTopology(); |
| 714 | SwitchEvent swInTopo = topology.getSwitchEvent(dpid); |
| 715 | assertEquals(sw, swInTopo); |
| 716 | assertTrue(swInTopo.isFrozen()); |
| 717 | assertEquals("bar", swInTopo.getStringAttribute("foo")); |
| 718 | |
| 719 | final SwitchPort portIdA = new SwitchPort(dpid, portNumberA); |
| 720 | PortEvent portAInTopo = topology.getPortEvent(portIdA); |
| 721 | assertEquals(portA, portAInTopo); |
| 722 | assertTrue(portAInTopo.isFrozen()); |
| 723 | assertEquals("buzz", portAInTopo.getStringAttribute("fuzz")); |
| 724 | |
| 725 | final SwitchPort portIdB = new SwitchPort(dpid, portNumberB); |
| 726 | PortEvent portBInTopo = topology.getPortEvent(portIdB); |
| 727 | assertEquals(portB, portBInTopo); |
| 728 | assertTrue(portBInTopo.isFrozen()); |
| 729 | assertEquals("buz", portBInTopo.getStringAttribute("fizz")); |
| 730 | |
| 731 | LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple()); |
| 732 | assertEquals(linkA, linkAInTopo); |
| 733 | assertTrue(linkAInTopo.isFrozen()); |
| 734 | assertEquals(TopologyElement.TYPE_OPTICAL_LAYER, linkAInTopo.getType()); |
| 735 | |
| 736 | |
| 737 | LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple()); |
| 738 | assertEquals(linkB, linkBInTopo); |
| 739 | assertTrue(linkBInTopo.isFrozen()); |
| 740 | assertEquals(TopologyElement.TYPE_OPTICAL_LAYER, linkBInTopo.getType()); |
| 741 | |
| 742 | // check events to be fired |
| 743 | // FIXME if link flapped (linkA in this scenario), |
| 744 | // linkA appears in both removed and added is this expected behavior? |
| 745 | List<LinkEvent> apiAddedLinkEvents |
| 746 | = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents"); |
| 747 | assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB)); |
| 748 | |
| 749 | // clear event before removing Link |
| 750 | apiAddedLinkEvents.clear(); |
| 751 | |
| 752 | // remove link |
| 753 | TestUtils.callMethod(theTopologyManager, "removeLink", LinkEvent.class, new LinkEvent(linkA)); |
| 754 | |
| 755 | LinkEvent linkANotInTopo = topology.getLinkEvent(linkA.getLinkTuple()); |
| 756 | assertNull(linkANotInTopo); |
| 757 | |
| 758 | List<LinkEvent> apiRemovedLinkEvents |
| 759 | = TestUtils.getField(theTopologyManager, "apiRemovedLinkEvents"); |
| 760 | assertThat(apiRemovedLinkEvents, hasItem(linkA)); |
| 761 | } |
| 762 | |
| 763 | /** |
| 764 | * Test to confirm topology replica transformation. |
| 765 | */ |
| 766 | @Test |
| 767 | public void testAddHostIgnoredByLink() { |
| 768 | setupTopologyManager(); |
| 769 | |
| 770 | final Dpid dpid = new Dpid(1); |
| 771 | SwitchEvent sw = new SwitchEvent(dpid); |
| 772 | sw.createStringAttribute("foo", "bar"); |
| 773 | |
| 774 | final PortNumber portNumberA = new PortNumber((short) 2); |
| 775 | PortEvent portA = new PortEvent(dpid, portNumberA); |
| 776 | portA.createStringAttribute("fuzz", "buzz"); |
| 777 | |
| 778 | final PortNumber portNumberB = new PortNumber((short) 3); |
| 779 | PortEvent portB = new PortEvent(dpid, portNumberB); |
| 780 | portB.createStringAttribute("fizz", "buz"); |
| 781 | |
| 782 | final PortNumber portNumberC = new PortNumber((short) 4); |
| 783 | PortEvent portC = new PortEvent(dpid, portNumberC); |
| 784 | portC.createStringAttribute("fizz", "buz"); |
| 785 | |
| 786 | LinkEvent linkA = new LinkEvent(portA.getSwitchPort(), portB.getSwitchPort()); |
| 787 | linkA.createStringAttribute(TopologyElement.TYPE, |
| 788 | TopologyElement.TYPE_OPTICAL_LAYER); |
| 789 | LinkEvent linkB = new LinkEvent(portB.getSwitchPort(), portA.getSwitchPort()); |
| 790 | linkB.createStringAttribute(TopologyElement.TYPE, |
| 791 | TopologyElement.TYPE_OPTICAL_LAYER); |
| 792 | |
| 793 | TestUtils.callMethod(theTopologyManager, "addSwitch", SwitchEvent.class, sw); |
| 794 | TestUtils.callMethod(theTopologyManager, "addPort", PortEvent.class, portA); |
| 795 | TestUtils.callMethod(theTopologyManager, "addPort", PortEvent.class, portB); |
| 796 | TestUtils.callMethod(theTopologyManager, "addPort", PortEvent.class, portC); |
| 797 | TestUtils.callMethod(theTopologyManager, "addLink", LinkEvent.class, linkA); |
| 798 | TestUtils.callMethod(theTopologyManager, "addLink", LinkEvent.class, linkB); |
| 799 | |
| 800 | // Add hostA attached to a port which already has a link |
| 801 | final MACAddress macA = MACAddress.valueOf(666L); |
| 802 | HostEvent hostA = new HostEvent(macA); |
| 803 | hostA.addAttachmentPoint(portA.getSwitchPort()); |
| 804 | final long timestampA = 392893200L; |
| 805 | hostA.setLastSeenTime(timestampA); |
| 806 | |
| 807 | TestUtils.callMethod(theTopologyManager, "addHost", HostEvent.class, hostA); |
| 808 | |
| 809 | // Add hostB attached to multiple ports, |
| 810 | // some of them which already has a link |
| 811 | final MACAddress macB = MACAddress.valueOf(999L); |
| 812 | HostEvent hostB = new HostEvent(macB); |
| 813 | hostB.addAttachmentPoint(portB.getSwitchPort()); |
| 814 | hostB.addAttachmentPoint(portC.getSwitchPort()); |
| 815 | final long timestampB = 392893201L; |
| 816 | hostB.setLastSeenTime(timestampB); |
| 817 | |
| 818 | TestUtils.callMethod(theTopologyManager, "addHost", HostEvent.class, hostB); |
| 819 | |
| 820 | // check topology structure |
| 821 | TopologyInternal topology = (TopologyInternal) theTopologyManager.getTopology(); |
| 822 | SwitchEvent swInTopo = topology.getSwitchEvent(dpid); |
| 823 | assertEquals(sw, swInTopo); |
| 824 | assertTrue(swInTopo.isFrozen()); |
| 825 | assertEquals("bar", swInTopo.getStringAttribute("foo")); |
| 826 | |
| 827 | final SwitchPort portIdA = new SwitchPort(dpid, portNumberA); |
| 828 | PortEvent portAInTopo = topology.getPortEvent(portIdA); |
| 829 | assertEquals(portA, portAInTopo); |
| 830 | assertTrue(portAInTopo.isFrozen()); |
| 831 | assertEquals("buzz", portAInTopo.getStringAttribute("fuzz")); |
| 832 | |
| 833 | final SwitchPort portIdB = new SwitchPort(dpid, portNumberB); |
| 834 | PortEvent portBInTopo = topology.getPortEvent(portIdB); |
| 835 | assertEquals(portB, portBInTopo); |
| 836 | assertTrue(portBInTopo.isFrozen()); |
| 837 | assertEquals("buz", portBInTopo.getStringAttribute("fizz")); |
| 838 | |
| 839 | // hostA expected to be completely ignored |
| 840 | assertNull(topology.getHostEvent(macA)); |
| 841 | // hostB expected to be there with reduced attachment point |
| 842 | HostEvent hostBrev = new HostEvent(macB); |
| 843 | hostBrev.addAttachmentPoint(portC.getSwitchPort()); |
| 844 | hostBrev.setLastSeenTime(timestampB); |
| 845 | hostBrev.freeze(); |
| 846 | assertEquals(hostBrev, topology.getHostEvent(macB)); |
| 847 | |
| 848 | |
| 849 | LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple()); |
| 850 | assertEquals(linkA, linkAInTopo); |
| 851 | assertTrue(linkAInTopo.isFrozen()); |
| 852 | assertEquals(TopologyElement.TYPE_OPTICAL_LAYER, linkAInTopo.getType()); |
| 853 | |
| 854 | LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple()); |
| 855 | assertEquals(linkB, linkBInTopo); |
| 856 | assertTrue(linkBInTopo.isFrozen()); |
| 857 | assertEquals(TopologyElement.TYPE_OPTICAL_LAYER, linkBInTopo.getType()); |
| 858 | |
| 859 | // check events to be fired |
| 860 | // hostB should be added with reduced attachment points |
| 861 | List<HostEvent> apiAddedHostEvents |
| 862 | = TestUtils.getField(theTopologyManager, "apiAddedHostEvents"); |
| 863 | assertThat(apiAddedHostEvents, hasItem(hostBrev)); |
| 864 | |
| 865 | // hostA should not be ignored |
| 866 | List<HostEvent> apiRemovedHostEvents |
| 867 | = TestUtils.getField(theTopologyManager, "apiRemovedHostEvents"); |
| 868 | assertThat(apiRemovedHostEvents, not(hasItem(hostA))); |
| 869 | |
| 870 | List<LinkEvent> apiAddedLinkEvents |
| 871 | = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents"); |
| 872 | assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB)); |
| 873 | } |
| 874 | |
| 875 | /** |
| 876 | * Test to confirm topology replica transformation. |
| 877 | */ |
| 878 | @Test |
| 879 | public void testAddHostMove() { |
| 880 | setupTopologyManager(); |
| 881 | |
| 882 | final Dpid dpid = new Dpid(1); |
| 883 | SwitchEvent sw = new SwitchEvent(dpid); |
| 884 | sw.createStringAttribute("foo", "bar"); |
| 885 | |
| 886 | final PortNumber portNumberA = new PortNumber((short) 2); |
| 887 | PortEvent portA = new PortEvent(dpid, portNumberA); |
| 888 | portA.createStringAttribute("fuzz", "buzz"); |
| 889 | |
| 890 | final PortNumber portNumberB = new PortNumber((short) 3); |
| 891 | PortEvent portB = new PortEvent(dpid, portNumberB); |
| 892 | portB.createStringAttribute("fizz", "buz"); |
| 893 | |
| 894 | final PortNumber portNumberC = new PortNumber((short) 4); |
| 895 | PortEvent portC = new PortEvent(dpid, portNumberC); |
| 896 | portC.createStringAttribute("fizz", "buz"); |
| 897 | |
| 898 | TestUtils.callMethod(theTopologyManager, "addSwitch", SwitchEvent.class, sw); |
| 899 | TestUtils.callMethod(theTopologyManager, "addPort", PortEvent.class, portA); |
| 900 | TestUtils.callMethod(theTopologyManager, "addPort", PortEvent.class, portB); |
| 901 | TestUtils.callMethod(theTopologyManager, "addPort", PortEvent.class, portC); |
| 902 | |
| 903 | // Add hostA attached to a port which already has a link |
| 904 | final MACAddress macA = MACAddress.valueOf(666L); |
| 905 | HostEvent hostA = new HostEvent(macA); |
| 906 | hostA.addAttachmentPoint(portA.getSwitchPort()); |
| 907 | final long timestampA = 392893200L; |
| 908 | hostA.setLastSeenTime(timestampA); |
| 909 | |
| 910 | TestUtils.callMethod(theTopologyManager, "addHost", HostEvent.class, hostA); |
| 911 | |
| 912 | |
| 913 | // check topology structure |
| 914 | TopologyInternal topology = (TopologyInternal) theTopologyManager.getTopology(); |
| 915 | SwitchEvent swInTopo = topology.getSwitchEvent(dpid); |
| 916 | assertEquals(sw, swInTopo); |
| 917 | assertTrue(swInTopo.isFrozen()); |
| 918 | assertEquals("bar", swInTopo.getStringAttribute("foo")); |
| 919 | |
| 920 | final SwitchPort portIdA = new SwitchPort(dpid, portNumberA); |
| 921 | PortEvent portAInTopo = topology.getPortEvent(portIdA); |
| 922 | assertEquals(portA, portAInTopo); |
| 923 | assertTrue(portAInTopo.isFrozen()); |
| 924 | assertEquals("buzz", portAInTopo.getStringAttribute("fuzz")); |
| 925 | |
| 926 | final SwitchPort portIdB = new SwitchPort(dpid, portNumberB); |
| 927 | PortEvent portBInTopo = topology.getPortEvent(portIdB); |
| 928 | assertEquals(portB, portBInTopo); |
| 929 | assertTrue(portBInTopo.isFrozen()); |
| 930 | assertEquals("buz", portBInTopo.getStringAttribute("fizz")); |
| 931 | |
| 932 | // hostA expected to be there |
| 933 | assertEquals(hostA, topology.getHostEvent(macA)); |
| 934 | assertEquals(timestampA, topology.getHostEvent(macA).getLastSeenTime()); |
| 935 | |
| 936 | // check events to be fired |
| 937 | // hostA should be added |
| 938 | List<HostEvent> apiAddedHostEvents |
| 939 | = TestUtils.getField(theTopologyManager, "apiAddedHostEvents"); |
| 940 | assertThat(apiAddedHostEvents, hasItem(hostA)); |
| 941 | |
| 942 | |
| 943 | // clear event before moving host |
| 944 | apiAddedHostEvents.clear(); |
| 945 | |
| 946 | HostEvent hostAmoved = new HostEvent(macA); |
| 947 | hostAmoved.addAttachmentPoint(portB.getSwitchPort()); |
| 948 | final long timestampAmoved = 392893201L; |
| 949 | hostAmoved.setLastSeenTime(timestampAmoved); |
| 950 | |
| 951 | TestUtils.callMethod(theTopologyManager, "addHost", HostEvent.class, hostAmoved); |
| 952 | |
| 953 | assertEquals(hostAmoved, topology.getHostEvent(macA)); |
| 954 | assertEquals(timestampAmoved, topology.getHostEvent(macA).getLastSeenTime()); |
| 955 | |
| 956 | // hostA expected to be there with new attachment point |
| 957 | apiAddedHostEvents |
| 958 | = TestUtils.getField(theTopologyManager, "apiAddedHostEvents"); |
| 959 | assertThat(apiAddedHostEvents, hasItem(hostAmoved)); |
| 960 | |
| 961 | // hostA is updated not removed |
| 962 | List<HostEvent> apiRemovedHostEvents |
| 963 | = TestUtils.getField(theTopologyManager, "apiRemovedHostEvents"); |
| 964 | assertThat(apiRemovedHostEvents, not(hasItem(hostA))); |
| 965 | } |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 966 | } |