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