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; |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 9 | import net.onrc.onos.core.registry.RegistryException; |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 10 | import net.onrc.onos.core.util.Dpid; |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 11 | import net.onrc.onos.core.util.EventEntry; |
Pavlin Radoslavov | 53b208a | 2014-07-28 13:16:11 -0700 | [diff] [blame] | 12 | import net.onrc.onos.core.util.OnosInstanceId; |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 13 | import net.onrc.onos.core.util.PortNumber; |
| 14 | import net.onrc.onos.core.util.SwitchPort; |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 15 | import net.onrc.onos.core.util.TestUtils; |
Ray Milkey | 3830135 | 2014-07-28 08:51:54 -0700 | [diff] [blame] | 16 | import net.onrc.onos.core.util.UnitTest; |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 17 | import org.easymock.EasyMock; |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 18 | import org.junit.Before; |
| 19 | import org.junit.Test; |
| 20 | |
Ray Milkey | 3830135 | 2014-07-28 08:51:54 -0700 | [diff] [blame] | 21 | import java.util.ArrayList; |
| 22 | import java.util.Collection; |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 23 | import java.util.LinkedList; |
Ray Milkey | 3830135 | 2014-07-28 08:51:54 -0700 | [diff] [blame] | 24 | import java.util.List; |
| 25 | import java.util.concurrent.CopyOnWriteArrayList; |
| 26 | |
| 27 | import static org.easymock.EasyMock.anyObject; |
| 28 | import static org.easymock.EasyMock.createMock; |
| 29 | import static org.easymock.EasyMock.eq; |
| 30 | import static org.easymock.EasyMock.expect; |
| 31 | import static org.easymock.EasyMock.replay; |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 32 | import static org.easymock.EasyMock.reset; |
Ray Milkey | 3830135 | 2014-07-28 08:51:54 -0700 | [diff] [blame] | 33 | import static org.easymock.EasyMock.verify; |
| 34 | import static org.hamcrest.Matchers.containsInAnyOrder; |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 35 | import static org.hamcrest.Matchers.empty; |
Ray Milkey | 3830135 | 2014-07-28 08:51:54 -0700 | [diff] [blame] | 36 | import static org.hamcrest.Matchers.hasItem; |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 37 | import static org.hamcrest.Matchers.is; |
Ray Milkey | 3830135 | 2014-07-28 08:51:54 -0700 | [diff] [blame] | 38 | import static org.hamcrest.Matchers.not; |
| 39 | import static org.junit.Assert.assertEquals; |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 40 | import static org.junit.Assert.assertNotNull; |
Ray Milkey | 3830135 | 2014-07-28 08:51:54 -0700 | [diff] [blame] | 41 | import static org.junit.Assert.assertNull; |
| 42 | import static org.junit.Assert.assertThat; |
| 43 | import static org.junit.Assert.assertTrue; |
| 44 | |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 45 | /** |
| 46 | * Unit tests for the TopologyManager class in the Topology module. |
| 47 | * These test cases only check the sanity of functions in the TopologyManager. |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 48 | * Note that we do not test the eventHandler functions in the TopologyManager |
| 49 | * class. |
| 50 | * DatagridService, DataStoreService, eventChannel, and |
| 51 | * controllerRegistryService are mocked out. |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 52 | */ |
Ray Milkey | 3830135 | 2014-07-28 08:51:54 -0700 | [diff] [blame] | 53 | public class TopologyManagerTest extends UnitTest { |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 54 | private TopologyManager theTopologyManager; |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 55 | private TopologyManager.EventHandler theEventHandler; |
| 56 | private TopologyListenerTest theTopologyListener = |
| 57 | new TopologyListenerTest(); |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 58 | private final String eventChannelName = "onos.topology"; |
| 59 | private IEventChannel<byte[], TopologyEvent> eventChannel; |
| 60 | private IDatagridService datagridService; |
| 61 | private TopologyDatastore dataStoreService; |
| 62 | private IControllerRegistryService registryService; |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 63 | private Collection<TopologyEvent> allTopologyEvents; |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 64 | private static final OnosInstanceId ONOS_INSTANCE_ID_1 = |
| 65 | new OnosInstanceId("ONOS-Instance-ID-1"); |
| 66 | private static final OnosInstanceId ONOS_INSTANCE_ID_2 = |
| 67 | new OnosInstanceId("ONOS-Instance-ID-2"); |
| 68 | private static final Dpid DPID_1 = new Dpid(1); |
| 69 | private static final Dpid DPID_2 = new Dpid(2); |
| 70 | |
| 71 | /** |
| 72 | * Topology events listener. |
| 73 | */ |
| 74 | private class TopologyListenerTest implements ITopologyListener { |
| 75 | private TopologyEvents topologyEvents; |
| 76 | |
| 77 | @Override |
| 78 | public void topologyEvents(TopologyEvents events) { |
| 79 | this.topologyEvents = events; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Clears the Topology Listener state. |
| 84 | */ |
| 85 | public void clear() { |
| 86 | this.topologyEvents = null; |
| 87 | } |
| 88 | } |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 89 | |
| 90 | @SuppressWarnings("unchecked") |
| 91 | @Before |
| 92 | public void setUp() throws Exception { |
| 93 | // Mock objects for testing |
| 94 | datagridService = EasyMock.createNiceMock(IDatagridService.class); |
| 95 | dataStoreService = EasyMock.createNiceMock(TopologyDatastore.class); |
| 96 | registryService = createMock(IControllerRegistryService.class); |
| 97 | eventChannel = EasyMock.createNiceMock(IEventChannel.class); |
| 98 | |
| 99 | expect(datagridService.createChannel( |
| 100 | eq(eventChannelName), |
| 101 | eq(byte[].class), |
| 102 | eq(TopologyEvent.class))) |
| 103 | .andReturn(eventChannel).once(); |
| 104 | |
| 105 | expect(datagridService.addListener( |
| 106 | eq(eventChannelName), |
| 107 | anyObject(IEventChannelListener.class), |
| 108 | eq(byte[].class), |
| 109 | eq(TopologyEvent.class))) |
| 110 | .andReturn(eventChannel).once(); |
| 111 | |
| 112 | expect(dataStoreService.addSwitch( |
| 113 | anyObject(SwitchEvent.class), |
| 114 | anyObject(Collection.class))) |
| 115 | .andReturn(true).anyTimes(); |
| 116 | |
| 117 | expect(dataStoreService.deactivateSwitch( |
| 118 | anyObject(SwitchEvent.class), |
| 119 | anyObject(Collection.class))) |
| 120 | .andReturn(true).anyTimes(); |
| 121 | |
| 122 | expect(dataStoreService.addPort( |
| 123 | anyObject(PortEvent.class))) |
| 124 | .andReturn(true).anyTimes(); |
| 125 | |
| 126 | expect(dataStoreService.deactivatePort( |
| 127 | anyObject(PortEvent.class))) |
| 128 | .andReturn(true).anyTimes(); |
| 129 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 130 | expect(dataStoreService.addHost( |
| 131 | anyObject(HostEvent.class))) |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 132 | .andReturn(true).anyTimes(); |
| 133 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 134 | expect(dataStoreService.removeHost( |
| 135 | anyObject(HostEvent.class))) |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 136 | .andReturn(true).anyTimes(); |
| 137 | |
| 138 | expect(dataStoreService.addLink( |
| 139 | anyObject(LinkEvent.class))) |
| 140 | .andReturn(true).anyTimes(); |
| 141 | |
| 142 | expect(dataStoreService.removeLink( |
| 143 | anyObject(LinkEvent.class))) |
| 144 | .andReturn(true).anyTimes(); |
| 145 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 146 | // Setup the Registry Service |
| 147 | expect(registryService.getOnosInstanceId()).andReturn(ONOS_INSTANCE_ID_1).anyTimes(); |
| 148 | try { |
| 149 | expect(registryService.getControllerForSwitch(DPID_1.value())). |
| 150 | andReturn(ONOS_INSTANCE_ID_1.toString()).anyTimes(); |
| 151 | expect(registryService.getControllerForSwitch(DPID_2.value())). |
| 152 | andReturn(ONOS_INSTANCE_ID_2.toString()).anyTimes(); |
| 153 | } catch (RegistryException ex) { |
| 154 | throw new IllegalStateException(ex); |
| 155 | } |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 156 | |
| 157 | allTopologyEvents = new CopyOnWriteArrayList<>(); |
Yuta HIGUCHI | 81d8b9e | 2014-07-14 23:56:34 -0700 | [diff] [blame] | 158 | expect(eventChannel.getAllEntries()) |
| 159 | .andReturn(allTopologyEvents).anyTimes(); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 160 | |
| 161 | replay(datagridService); |
| 162 | replay(registryService); |
| 163 | replay(dataStoreService); |
| 164 | // replay(eventChannel); |
Yuta HIGUCHI | 81d8b9e | 2014-07-14 23:56:34 -0700 | [diff] [blame] | 165 | } |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 166 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 167 | /** |
| 168 | * Setup the Topology Manager. |
| 169 | */ |
Yuta HIGUCHI | 81d8b9e | 2014-07-14 23:56:34 -0700 | [diff] [blame] | 170 | private void setupTopologyManager() { |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 171 | // Create a TopologyManager object for testing |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame^] | 172 | theTopologyManager = new TopologyManager(registryService); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 173 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 174 | // Replace the eventHandler to prevent the thread from starting |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 175 | TestUtils.setField(theTopologyManager, "eventHandler", |
| 176 | EasyMock.createNiceMock(TopologyManager.EventHandler.class)); |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 177 | theTopologyManager.startup(datagridService); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 178 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 179 | // Replace the data store with a mocked object |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 180 | TestUtils.setField(theTopologyManager, "datastore", dataStoreService); |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 181 | } |
| 182 | |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 183 | /** |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 184 | * Setup the Topology Manager with the Event Handler. |
| 185 | */ |
| 186 | private void setupTopologyManagerWithEventHandler() { |
| 187 | // Create a TopologyManager object for testing |
Pavlin Radoslavov | 8a44b78 | 2014-08-07 12:53:27 -0700 | [diff] [blame^] | 188 | theTopologyManager = new TopologyManager(registryService); |
| 189 | theTopologyManager.registerTopologyListener(theTopologyListener); |
| 190 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 191 | // Allocate the Event Handler, so we can have direct access to it |
| 192 | theEventHandler = theTopologyManager.new EventHandler(); |
| 193 | TestUtils.setField(theTopologyManager, "eventHandler", |
| 194 | theEventHandler); |
| 195 | |
| 196 | // Replace the data store with a mocked object |
| 197 | TestUtils.setField(theTopologyManager, "datastore", dataStoreService); |
| 198 | |
| 199 | replay(eventChannel); |
| 200 | // |
| 201 | // NOTE: Uncomment-out the line below if the startup() method needs |
| 202 | // to be called for some of the unit tests. For now it is commented-out |
| 203 | // to avoid any side effects of starting the eventHandler thread. |
| 204 | // |
| 205 | // theTopologyManager.startup(datagridService); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Test the Switch Mastership Updated Event. |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 210 | */ |
| 211 | @Test |
| 212 | public void testPutSwitchMastershipEvent() { |
| 213 | // Mock the eventChannel functions first |
| 214 | eventChannel.addEntry(anyObject(byte[].class), |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 215 | anyObject(TopologyEvent.class)); |
| 216 | EasyMock.expectLastCall().times(1, 1); // 1 event |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 217 | replay(eventChannel); |
| 218 | |
| 219 | setupTopologyManager(); |
| 220 | |
| 221 | // Generate a new Switch Mastership event |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 222 | Role role = Role.MASTER; |
| 223 | MastershipEvent mastershipEvent = |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 224 | new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role); |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 225 | |
| 226 | // Call the topologyManager function for adding the event |
| 227 | theTopologyManager.putSwitchMastershipEvent(mastershipEvent); |
| 228 | |
| 229 | // Verify the function calls |
| 230 | verify(eventChannel); |
| 231 | } |
| 232 | |
| 233 | /** |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 234 | * Test the Switch Mastership Removed Event. |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 235 | */ |
| 236 | @Test |
| 237 | public void testRemoveSwitchMastershipEvent() { |
| 238 | // Mock the eventChannel functions first |
| 239 | eventChannel.removeEntry(anyObject(byte[].class)); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 240 | EasyMock.expectLastCall().times(1, 1); // 1 event |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 241 | replay(eventChannel); |
| 242 | |
| 243 | setupTopologyManager(); |
| 244 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 245 | // Generate a new Switch Mastership Event |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 246 | Role role = Role.MASTER; |
| 247 | MastershipEvent mastershipEvent = |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 248 | new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role); |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 249 | |
| 250 | // Call the topologyManager function for removing the event |
| 251 | theTopologyManager.removeSwitchMastershipEvent(mastershipEvent); |
| 252 | |
| 253 | // Verify the function calls |
| 254 | verify(eventChannel); |
| 255 | } |
| 256 | |
| 257 | /** |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 258 | * Test the Switch discovered and Port discovered functions. |
| 259 | */ |
| 260 | @Test |
| 261 | public void testPutSwitchAndPortDiscoveryEvent() { |
| 262 | // Mock the eventChannel functions first |
| 263 | eventChannel.addEntry(anyObject(byte[].class), |
| 264 | anyObject(TopologyEvent.class)); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 265 | EasyMock.expectLastCall().times(3, 3); // (1 Switch + 1 Port), 1 Port |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 266 | replay(eventChannel); |
| 267 | |
| 268 | setupTopologyManager(); |
| 269 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 270 | // Mock Switch has one Port |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 271 | PortNumber portNumber = PortNumber.uint32(1); |
| 272 | |
| 273 | // Generate a new Switch Event along with a Port Event |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 274 | SwitchEvent switchEvent = new SwitchEvent(DPID_1); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 275 | |
| 276 | Collection<PortEvent> portEvents = new ArrayList<PortEvent>(); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 277 | portEvents.add(new PortEvent(DPID_1, portNumber)); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 278 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 279 | // Call the topologyManager function for adding a Switch |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 280 | theTopologyManager.putSwitchDiscoveryEvent(switchEvent, portEvents); |
| 281 | |
| 282 | for (PortEvent portEvent : portEvents) { |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 283 | // Call the topologyManager function for adding a Port |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 284 | theTopologyManager.putPortDiscoveryEvent(portEvent); |
| 285 | } |
| 286 | |
| 287 | // Verify the function calls |
| 288 | verify(eventChannel); |
| 289 | |
| 290 | } |
| 291 | |
| 292 | /** |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 293 | * Test the Switch and Port removed functions. |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 294 | */ |
| 295 | @Test |
| 296 | public void testRemoveSwitchAndPortDiscoveryEvent() { |
| 297 | // Mock the eventChannel functions first |
| 298 | eventChannel.removeEntry(anyObject(byte[].class)); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 299 | EasyMock.expectLastCall().times(2, 2); // 1 Switch, 1 Port |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 300 | replay(eventChannel); |
| 301 | |
| 302 | setupTopologyManager(); |
| 303 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 304 | PortNumber portNumber = PortNumber.uint32(1); |
| 305 | |
| 306 | // Generate a Port Event |
| 307 | Collection<PortEvent> portEvents = new ArrayList<PortEvent>(); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 308 | portEvents.add(new PortEvent(DPID_1, portNumber)); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 309 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 310 | // Call the topologyManager function for removing a Port |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 311 | for (PortEvent portEvent : portEvents) { |
| 312 | theTopologyManager.removePortDiscoveryEvent(portEvent); |
| 313 | } |
| 314 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 315 | // Call the topologyManager function for removing a Switch |
| 316 | SwitchEvent switchEvent = new SwitchEvent(DPID_1); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 317 | theTopologyManager.removeSwitchDiscoveryEvent(switchEvent); |
| 318 | |
| 319 | // Verify the function calls |
| 320 | verify(eventChannel); |
| 321 | |
| 322 | } |
| 323 | |
| 324 | /** |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 325 | * Test the Link discovered function. |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 326 | */ |
| 327 | @Test |
| 328 | public void testPutLinkDiscoveryEvent() { |
| 329 | // Mock the eventChannel functions first |
| 330 | eventChannel.addEntry(anyObject(byte[].class), |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 331 | anyObject(TopologyEvent.class)); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 332 | EasyMock.expectLastCall().times(5, 5); // (2 Switch + 2 Port + 1 Link) |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 333 | replay(eventChannel); |
| 334 | |
Yuta HIGUCHI | 81d8b9e | 2014-07-14 23:56:34 -0700 | [diff] [blame] | 335 | setupTopologyManager(); |
| 336 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 337 | // Generate the Switch and Port Events |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 338 | PortNumber portNumber1 = PortNumber.uint32(1); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 339 | SwitchEvent switchEvent1 = new SwitchEvent(DPID_1); |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 340 | Collection<PortEvent> portEvents1 = new ArrayList<PortEvent>(); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 341 | portEvents1.add(new PortEvent(DPID_1, portNumber1)); |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 342 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 343 | // Call the topologyManager function for adding a Switch |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 344 | theTopologyManager.putSwitchDiscoveryEvent(switchEvent1, portEvents1); |
| 345 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 346 | // Generate the Switch and Port Events |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 347 | PortNumber portNumber2 = PortNumber.uint32(2); |
| 348 | SwitchEvent switchEvent2 = new SwitchEvent(DPID_2); |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 349 | Collection<PortEvent> portEvents2 = new ArrayList<PortEvent>(); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 350 | portEvents2.add(new PortEvent(DPID_2, portNumber2)); |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 351 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 352 | // Call the topologyManager function for adding a Switch |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 353 | theTopologyManager.putSwitchDiscoveryEvent(switchEvent2, portEvents2); |
| 354 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 355 | // Create the Link Event |
| 356 | LinkEvent linkEvent = |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 357 | new LinkEvent(new SwitchPort(DPID_1, portNumber1), |
| 358 | new SwitchPort(DPID_2, portNumber2)); |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 359 | theTopologyManager.putLinkDiscoveryEvent(linkEvent); |
| 360 | |
| 361 | // Verify the function calls |
| 362 | verify(eventChannel); |
| 363 | } |
| 364 | |
| 365 | /** |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 366 | * Test the Link removed function. |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 367 | */ |
| 368 | @Test |
| 369 | public void testRemoveLinkDiscoveryEvent() { |
| 370 | // Mock the eventChannel functions first |
| 371 | eventChannel.removeEntry(anyObject(byte[].class)); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 372 | EasyMock.expectLastCall().times(1, 1); // (1 Link) |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 373 | replay(eventChannel); |
| 374 | |
Yuta HIGUCHI | 81d8b9e | 2014-07-14 23:56:34 -0700 | [diff] [blame] | 375 | setupTopologyManager(); |
| 376 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 377 | // Generate the Switch and Port Events |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 378 | PortNumber portNumber1 = PortNumber.uint32(1); |
| 379 | SwitchEvent switchEvent1 = new SwitchEvent(DPID_1); |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 380 | Collection<PortEvent> portEvents1 = new ArrayList<PortEvent>(); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 381 | portEvents1.add(new PortEvent(DPID_1, portNumber1)); |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 382 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 383 | // Call the topologyManager function for adding a Switch |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 384 | theTopologyManager.putSwitchDiscoveryEvent(switchEvent1, portEvents1); |
| 385 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 386 | // Generate the Switch and Port Events |
| 387 | PortNumber portNumber2 = PortNumber.uint32(2); |
| 388 | SwitchEvent switchEvent2 = new SwitchEvent(DPID_2); |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 389 | Collection<PortEvent> portEvents2 = new ArrayList<PortEvent>(); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 390 | portEvents2.add(new PortEvent(DPID_2, portNumber2)); |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 391 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 392 | // Call the topologyManager function for adding a Switch |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 393 | theTopologyManager.putSwitchDiscoveryEvent(switchEvent2, portEvents2); |
| 394 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 395 | // Remove the Link |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 396 | LinkEvent linkEventRemove = |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 397 | new LinkEvent(new SwitchPort(DPID_1, portNumber1), |
| 398 | new SwitchPort(DPID_2, portNumber2)); |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 399 | theTopologyManager.removeLinkDiscoveryEvent(linkEventRemove); |
| 400 | |
| 401 | // Verify the function calls |
| 402 | verify(eventChannel); |
| 403 | } |
| 404 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 405 | /** |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 406 | * Test the Host discovered function. |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 407 | */ |
| 408 | @Test |
| 409 | public void testPutHostDiscoveryEvent() { |
| 410 | // Mock the eventChannel functions first |
| 411 | eventChannel.addEntry(anyObject(byte[].class), |
| 412 | anyObject(TopologyEvent.class)); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 413 | EasyMock.expectLastCall().times(1, 1); // 1 Host |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 414 | replay(eventChannel); |
| 415 | |
| 416 | setupTopologyManager(); |
| 417 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 418 | // Generate a new Host Event |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 419 | PortNumber portNumber = PortNumber.uint32(1); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 420 | MACAddress hostMac = MACAddress.valueOf("00:AA:11:BB:33:CC"); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 421 | SwitchPort sp = new SwitchPort(DPID_1, portNumber); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 422 | List<SwitchPort> spLists = new ArrayList<SwitchPort>(); |
| 423 | spLists.add(sp); |
| 424 | HostEvent hostEvent = new HostEvent(hostMac); |
| 425 | hostEvent.setAttachmentPoints(spLists); |
| 426 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 427 | // Call the topologyManager function for adding a Host |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 428 | theTopologyManager.putHostDiscoveryEvent(hostEvent); |
| 429 | |
| 430 | // Verify the function calls |
| 431 | verify(eventChannel); |
| 432 | } |
| 433 | |
| 434 | /** |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 435 | * Test the Host removed function. |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 436 | */ |
| 437 | @Test |
| 438 | public void testRemoveHostDiscoveryEvent() { |
| 439 | // Mock the eventChannel functions first |
| 440 | eventChannel.removeEntry(anyObject(byte[].class)); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 441 | EasyMock.expectLastCall().times(1, 1); // 1 Host |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 442 | replay(eventChannel); |
| 443 | |
| 444 | setupTopologyManager(); |
| 445 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 446 | // Generate a new Host Event |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 447 | PortNumber portNumber = PortNumber.uint32(1); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 448 | MACAddress hostMac = MACAddress.valueOf("00:AA:11:BB:33:CC"); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 449 | SwitchPort sp = new SwitchPort(DPID_1, portNumber); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 450 | List<SwitchPort> spLists = new ArrayList<SwitchPort>(); |
| 451 | spLists.add(sp); |
| 452 | HostEvent hostEvent = new HostEvent(hostMac); |
| 453 | hostEvent.setAttachmentPoints(spLists); |
| 454 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 455 | // Call the topologyManager function for removing a Host |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 456 | theTopologyManager.removeHostDiscoveryEvent(hostEvent); |
| 457 | |
| 458 | // Verify the function calls |
| 459 | verify(eventChannel); |
| 460 | } |
| 461 | |
| 462 | /** |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 463 | * Tests adding of a Switch Mastership event and the topology replica |
| 464 | * transformation. |
| 465 | */ |
| 466 | @Test |
| 467 | public void testAddMastershipEvent() { |
| 468 | setupTopologyManager(); |
| 469 | |
| 470 | // Prepare the event |
| 471 | Role role = Role.MASTER; |
| 472 | MastershipEvent mastershipEvent = |
| 473 | new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role); |
| 474 | // Add the event |
| 475 | TestUtils.callMethod(theTopologyManager, "addMastershipEvent", |
| 476 | MastershipEvent.class, mastershipEvent); |
| 477 | |
| 478 | // |
| 479 | // NOTE: The topology itself doesn't contain the Mastership Events, |
| 480 | // hence we don't check the topology. |
| 481 | // |
| 482 | |
| 483 | // Check the events to be fired |
| 484 | List<MastershipEvent> apiAddedMastershipEvents |
| 485 | = TestUtils.getField(theTopologyManager, |
| 486 | "apiAddedMastershipEvents"); |
| 487 | assertThat(apiAddedMastershipEvents, hasItem(mastershipEvent)); |
| 488 | } |
| 489 | |
| 490 | /** |
| 491 | * Tests removing of a Switch Mastership event and the topology replica |
| 492 | * transformation. |
| 493 | */ |
| 494 | @Test |
| 495 | public void testRemoveMastershipEvent() { |
| 496 | setupTopologyManager(); |
| 497 | |
| 498 | // Prepare the event |
| 499 | Role role = Role.MASTER; |
| 500 | MastershipEvent mastershipEvent = |
| 501 | new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role); |
| 502 | // Add the event |
| 503 | TestUtils.callMethod(theTopologyManager, "addMastershipEvent", |
| 504 | MastershipEvent.class, mastershipEvent); |
| 505 | |
| 506 | // Check the events to be fired |
| 507 | List<MastershipEvent> apiAddedMastershipEvents |
| 508 | = TestUtils.getField(theTopologyManager, |
| 509 | "apiAddedMastershipEvents"); |
| 510 | assertThat(apiAddedMastershipEvents, hasItem(mastershipEvent)); |
| 511 | |
| 512 | // Remove the event |
| 513 | TestUtils.callMethod(theTopologyManager, "removeMastershipEvent", |
| 514 | MastershipEvent.class, |
| 515 | new MastershipEvent(mastershipEvent)); |
| 516 | |
| 517 | // Check the events to be fired |
| 518 | List<MastershipEvent> apiRemovedMastershipEvents |
| 519 | = TestUtils.getField(theTopologyManager, |
| 520 | "apiRemovedMastershipEvents"); |
| 521 | assertThat(apiRemovedMastershipEvents, hasItem(mastershipEvent)); |
| 522 | } |
| 523 | |
| 524 | /** |
| 525 | * Tests adding of a Switch and the topology replica transformation. |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 526 | */ |
| 527 | @Test |
| 528 | public void testAddSwitch() { |
| 529 | setupTopologyManager(); |
| 530 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 531 | SwitchEvent sw = new SwitchEvent(DPID_1); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 532 | sw.createStringAttribute("foo", "bar"); |
| 533 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 534 | TestUtils.callMethod(theTopologyManager, "addSwitch", |
| 535 | SwitchEvent.class, sw); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 536 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 537 | // Check the topology structure |
| 538 | TopologyInternal topology = |
| 539 | (TopologyInternal) theTopologyManager.getTopology(); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 540 | SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 541 | assertEquals(sw, swInTopo); |
| 542 | assertTrue(swInTopo.isFrozen()); |
| 543 | assertEquals("bar", swInTopo.getStringAttribute("foo")); |
| 544 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 545 | // Check the events to be fired |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 546 | List<SwitchEvent> apiAddedSwitchEvents |
| 547 | = TestUtils.getField(theTopologyManager, "apiAddedSwitchEvents"); |
| 548 | assertThat(apiAddedSwitchEvents, hasItem(sw)); |
| 549 | } |
| 550 | |
| 551 | /** |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 552 | * Tests adding of a Port and the topology replica transformation. |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 553 | */ |
| 554 | @Test |
| 555 | public void testAddPort() { |
| 556 | setupTopologyManager(); |
| 557 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 558 | SwitchEvent sw = new SwitchEvent(DPID_1); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 559 | sw.createStringAttribute("foo", "bar"); |
| 560 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 561 | final PortNumber portNumber = PortNumber.uint32(2); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 562 | PortEvent port = new PortEvent(DPID_1, portNumber); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 563 | port.createStringAttribute("fuzz", "buzz"); |
| 564 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 565 | TestUtils.callMethod(theTopologyManager, "addSwitch", |
| 566 | SwitchEvent.class, sw); |
| 567 | TestUtils.callMethod(theTopologyManager, "addPort", |
| 568 | PortEvent.class, port); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 569 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 570 | // Check the topology structure |
| 571 | TopologyInternal topology = |
| 572 | (TopologyInternal) theTopologyManager.getTopology(); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 573 | SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 574 | assertEquals(sw, swInTopo); |
| 575 | assertTrue(swInTopo.isFrozen()); |
| 576 | assertEquals("bar", swInTopo.getStringAttribute("foo")); |
| 577 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 578 | final SwitchPort switchPort = new SwitchPort(DPID_1, portNumber); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 579 | PortEvent portInTopo = topology.getPortEvent(switchPort); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 580 | assertEquals(port, portInTopo); |
| 581 | assertTrue(portInTopo.isFrozen()); |
| 582 | assertEquals("buzz", portInTopo.getStringAttribute("fuzz")); |
| 583 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 584 | // Check the events to be fired |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 585 | List<PortEvent> apiAddedPortEvents |
| 586 | = TestUtils.getField(theTopologyManager, "apiAddedPortEvents"); |
| 587 | assertThat(apiAddedPortEvents, hasItem(port)); |
| 588 | } |
| 589 | |
| 590 | /** |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 591 | * Tests removing of a Port followed by removing of a Switch, |
| 592 | * and the topology replica transformation. |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 593 | */ |
| 594 | @Test |
| 595 | public void testRemovePortThenSwitch() { |
| 596 | setupTopologyManager(); |
| 597 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 598 | SwitchEvent sw = new SwitchEvent(DPID_1); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 599 | sw.createStringAttribute("foo", "bar"); |
| 600 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 601 | final PortNumber portNumber = PortNumber.uint32(2); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 602 | PortEvent port = new PortEvent(DPID_1, portNumber); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 603 | port.createStringAttribute("fuzz", "buzz"); |
| 604 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 605 | TestUtils.callMethod(theTopologyManager, "addSwitch", |
| 606 | SwitchEvent.class, sw); |
| 607 | TestUtils.callMethod(theTopologyManager, "addPort", |
| 608 | PortEvent.class, port); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 609 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 610 | // Check the topology structure |
| 611 | TopologyInternal topology = |
| 612 | (TopologyInternal) theTopologyManager.getTopology(); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 613 | SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 614 | assertEquals(sw, swInTopo); |
| 615 | assertTrue(swInTopo.isFrozen()); |
| 616 | assertEquals("bar", swInTopo.getStringAttribute("foo")); |
| 617 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 618 | final SwitchPort switchPort = new SwitchPort(DPID_1, portNumber); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 619 | PortEvent portInTopo = topology.getPortEvent(switchPort); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 620 | assertEquals(port, portInTopo); |
| 621 | assertTrue(portInTopo.isFrozen()); |
| 622 | assertEquals("buzz", portInTopo.getStringAttribute("fuzz")); |
| 623 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 624 | // Remove in proper order |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 625 | TestUtils.callMethod(theTopologyManager, "removePort", |
| 626 | PortEvent.class, new PortEvent(port)); |
| 627 | TestUtils.callMethod(theTopologyManager, "removeSwitch", |
| 628 | SwitchEvent.class, new SwitchEvent(sw)); |
| 629 | |
| 630 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 631 | // Check the events to be fired |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 632 | List<PortEvent> apiRemovedPortEvents |
| 633 | = TestUtils.getField(theTopologyManager, "apiRemovedPortEvents"); |
| 634 | assertThat(apiRemovedPortEvents, hasItem(port)); |
| 635 | List<SwitchEvent> apiRemovedSwitchEvents |
| 636 | = TestUtils.getField(theTopologyManager, "apiRemovedSwitchEvents"); |
| 637 | assertThat(apiRemovedSwitchEvents, hasItem(sw)); |
| 638 | } |
| 639 | |
| 640 | /** |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 641 | * Tests removing of a Switch without removing of a Port, |
| 642 | * and the topology replica transformation. |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 643 | */ |
| 644 | @Test |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 645 | public void testRemoveSwitchWithoutPortRemoval() { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 646 | setupTopologyManager(); |
| 647 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 648 | SwitchEvent sw = new SwitchEvent(DPID_1); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 649 | sw.createStringAttribute("foo", "bar"); |
| 650 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 651 | final PortNumber portNumber = PortNumber.uint32(2); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 652 | PortEvent port = new PortEvent(DPID_1, portNumber); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 653 | port.createStringAttribute("fuzz", "buzz"); |
| 654 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 655 | TestUtils.callMethod(theTopologyManager, "addSwitch", |
| 656 | SwitchEvent.class, sw); |
| 657 | TestUtils.callMethod(theTopologyManager, "addPort", |
| 658 | PortEvent.class, port); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 659 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 660 | // Check the topology structure |
| 661 | TopologyInternal topology = |
| 662 | (TopologyInternal) theTopologyManager.getTopology(); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 663 | SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 664 | assertEquals(sw, swInTopo); |
| 665 | assertTrue(swInTopo.isFrozen()); |
| 666 | assertEquals("bar", swInTopo.getStringAttribute("foo")); |
| 667 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 668 | final SwitchPort switchPort = new SwitchPort(DPID_1, portNumber); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 669 | PortEvent portInTopo = topology.getPortEvent(switchPort); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 670 | assertEquals(port, portInTopo); |
| 671 | assertTrue(portInTopo.isFrozen()); |
| 672 | assertEquals("buzz", portInTopo.getStringAttribute("fuzz")); |
| 673 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 674 | // Remove in in-proper order |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 675 | // TestUtils.callMethod(theTopologyManager, "removePort", |
| 676 | // PortEvent.class, new PortEvent(port)); |
| 677 | TestUtils.callMethod(theTopologyManager, "removeSwitch", |
| 678 | SwitchEvent.class, new SwitchEvent(sw)); |
| 679 | |
| 680 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 681 | // Check the events to be fired |
| 682 | // The outcome should be the same as #testRemovePortThenSwitch |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 683 | List<PortEvent> apiRemovedPortEvents |
| 684 | = TestUtils.getField(theTopologyManager, "apiRemovedPortEvents"); |
| 685 | assertThat(apiRemovedPortEvents, hasItem(port)); |
| 686 | List<SwitchEvent> apiRemovedSwitchEvents |
| 687 | = TestUtils.getField(theTopologyManager, "apiRemovedSwitchEvents"); |
| 688 | assertThat(apiRemovedSwitchEvents, hasItem(sw)); |
| 689 | } |
| 690 | |
| 691 | /** |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 692 | * Tests adding of a Link and the topology replica transformation. |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 693 | */ |
| 694 | @Test |
| 695 | public void testAddLink() { |
| 696 | setupTopologyManager(); |
| 697 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 698 | SwitchEvent sw = new SwitchEvent(DPID_1); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 699 | sw.createStringAttribute("foo", "bar"); |
| 700 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 701 | final PortNumber portNumberA = PortNumber.uint32(2); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 702 | PortEvent portA = new PortEvent(DPID_1, portNumberA); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 703 | portA.createStringAttribute("fuzz", "buzz"); |
| 704 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 705 | final PortNumber portNumberB = PortNumber.uint32(3); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 706 | PortEvent portB = new PortEvent(DPID_1, portNumberB); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 707 | portB.createStringAttribute("fizz", "buz"); |
| 708 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 709 | LinkEvent linkA = new LinkEvent(portA.getSwitchPort(), |
| 710 | portB.getSwitchPort()); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 711 | linkA.createStringAttribute(TopologyElement.TYPE, |
| 712 | TopologyElement.TYPE_OPTICAL_LAYER); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 713 | LinkEvent linkB = new LinkEvent(portB.getSwitchPort(), |
| 714 | portA.getSwitchPort()); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 715 | linkB.createStringAttribute(TopologyElement.TYPE, |
| 716 | TopologyElement.TYPE_OPTICAL_LAYER); |
| 717 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 718 | TestUtils.callMethod(theTopologyManager, "addSwitch", |
| 719 | SwitchEvent.class, sw); |
| 720 | TestUtils.callMethod(theTopologyManager, "addPort", |
| 721 | PortEvent.class, portA); |
| 722 | TestUtils.callMethod(theTopologyManager, "addPort", |
| 723 | PortEvent.class, portB); |
| 724 | TestUtils.callMethod(theTopologyManager, "addLink", |
| 725 | LinkEvent.class, linkA); |
| 726 | TestUtils.callMethod(theTopologyManager, "addLink", |
| 727 | LinkEvent.class, linkB); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 728 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 729 | // Check the topology structure |
| 730 | TopologyInternal topology = |
| 731 | (TopologyInternal) theTopologyManager.getTopology(); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 732 | SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 733 | assertEquals(sw, swInTopo); |
| 734 | assertTrue(swInTopo.isFrozen()); |
| 735 | assertEquals("bar", swInTopo.getStringAttribute("foo")); |
| 736 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 737 | final SwitchPort switchPortA = new SwitchPort(DPID_1, portNumberA); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 738 | PortEvent portAInTopo = topology.getPortEvent(switchPortA); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 739 | assertEquals(portA, portAInTopo); |
| 740 | assertTrue(portAInTopo.isFrozen()); |
| 741 | assertEquals("buzz", portAInTopo.getStringAttribute("fuzz")); |
| 742 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 743 | final SwitchPort switchPortB = new SwitchPort(DPID_1, portNumberB); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 744 | PortEvent portBInTopo = topology.getPortEvent(switchPortB); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 745 | assertEquals(portB, portBInTopo); |
| 746 | assertTrue(portBInTopo.isFrozen()); |
| 747 | assertEquals("buz", portBInTopo.getStringAttribute("fizz")); |
| 748 | |
| 749 | LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple()); |
| 750 | assertEquals(linkA, linkAInTopo); |
| 751 | assertTrue(linkAInTopo.isFrozen()); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 752 | assertEquals(TopologyElement.TYPE_OPTICAL_LAYER, |
| 753 | linkAInTopo.getType()); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 754 | |
| 755 | LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple()); |
| 756 | assertEquals(linkB, linkBInTopo); |
| 757 | assertTrue(linkBInTopo.isFrozen()); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 758 | assertEquals(TopologyElement.TYPE_OPTICAL_LAYER, |
| 759 | linkBInTopo.getType()); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 760 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 761 | // Check the events to be fired |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 762 | List<LinkEvent> apiAddedLinkEvents |
| 763 | = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents"); |
| 764 | assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB)); |
| 765 | } |
| 766 | |
| 767 | /** |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 768 | * Tests removing of a Link without removing of a Host, and the topology |
| 769 | * replica transformation. |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 770 | */ |
| 771 | @Test |
| 772 | public void testAddLinkKickingOffHost() { |
| 773 | setupTopologyManager(); |
| 774 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 775 | SwitchEvent sw = new SwitchEvent(DPID_1); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 776 | sw.createStringAttribute("foo", "bar"); |
| 777 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 778 | final PortNumber portNumberA = PortNumber.uint32(2); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 779 | PortEvent portA = new PortEvent(DPID_1, portNumberA); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 780 | portA.createStringAttribute("fuzz", "buzz"); |
| 781 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 782 | final PortNumber portNumberB = PortNumber.uint32(3); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 783 | PortEvent portB = new PortEvent(DPID_1, portNumberB); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 784 | portB.createStringAttribute("fizz", "buz"); |
| 785 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 786 | final PortNumber portNumberC = PortNumber.uint32(4); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 787 | PortEvent portC = new PortEvent(DPID_1, portNumberC); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 788 | portC.createStringAttribute("fizz", "buz"); |
| 789 | |
| 790 | final MACAddress macA = MACAddress.valueOf(666L); |
| 791 | HostEvent hostA = new HostEvent(macA); |
| 792 | hostA.addAttachmentPoint(portA.getSwitchPort()); |
| 793 | final long timestampA = 392893200L; |
| 794 | hostA.setLastSeenTime(timestampA); |
| 795 | |
| 796 | final MACAddress macB = MACAddress.valueOf(999L); |
| 797 | HostEvent hostB = new HostEvent(macB); |
| 798 | hostB.addAttachmentPoint(portB.getSwitchPort()); |
| 799 | hostB.addAttachmentPoint(portC.getSwitchPort()); |
| 800 | final long timestampB = 392893201L; |
| 801 | hostB.setLastSeenTime(timestampB); |
| 802 | |
| 803 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 804 | LinkEvent linkA = new LinkEvent(portA.getSwitchPort(), |
| 805 | portB.getSwitchPort()); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 806 | linkA.createStringAttribute(TopologyElement.TYPE, |
| 807 | TopologyElement.TYPE_OPTICAL_LAYER); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 808 | LinkEvent linkB = new LinkEvent(portB.getSwitchPort(), |
| 809 | portA.getSwitchPort()); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 810 | linkB.createStringAttribute(TopologyElement.TYPE, |
| 811 | TopologyElement.TYPE_OPTICAL_LAYER); |
| 812 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 813 | TestUtils.callMethod(theTopologyManager, "addSwitch", |
| 814 | SwitchEvent.class, sw); |
| 815 | TestUtils.callMethod(theTopologyManager, "addPort", |
| 816 | PortEvent.class, portA); |
| 817 | TestUtils.callMethod(theTopologyManager, "addPort", |
| 818 | PortEvent.class, portB); |
| 819 | TestUtils.callMethod(theTopologyManager, "addPort", |
| 820 | PortEvent.class, portC); |
| 821 | TestUtils.callMethod(theTopologyManager, "addHost", |
| 822 | HostEvent.class, hostA); |
| 823 | TestUtils.callMethod(theTopologyManager, "addHost", |
| 824 | HostEvent.class, hostB); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 825 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 826 | TestUtils.callMethod(theTopologyManager, "addLink", |
| 827 | LinkEvent.class, linkA); |
| 828 | TestUtils.callMethod(theTopologyManager, "addLink", |
| 829 | LinkEvent.class, linkB); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 830 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 831 | // Check the topology structure |
| 832 | TopologyInternal topology = |
| 833 | (TopologyInternal) theTopologyManager.getTopology(); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 834 | SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 835 | assertEquals(sw, swInTopo); |
| 836 | assertTrue(swInTopo.isFrozen()); |
| 837 | assertEquals("bar", swInTopo.getStringAttribute("foo")); |
| 838 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 839 | final SwitchPort switchPortA = new SwitchPort(DPID_1, portNumberA); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 840 | PortEvent portAInTopo = topology.getPortEvent(switchPortA); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 841 | assertEquals(portA, portAInTopo); |
| 842 | assertTrue(portAInTopo.isFrozen()); |
| 843 | assertEquals("buzz", portAInTopo.getStringAttribute("fuzz")); |
| 844 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 845 | final SwitchPort switchPortB = new SwitchPort(DPID_1, portNumberB); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 846 | PortEvent portBInTopo = topology.getPortEvent(switchPortB); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 847 | assertEquals(portB, portBInTopo); |
| 848 | assertTrue(portBInTopo.isFrozen()); |
| 849 | assertEquals("buz", portBInTopo.getStringAttribute("fizz")); |
| 850 | |
| 851 | // hostA expected to be removed |
| 852 | assertNull(topology.getHostEvent(macA)); |
| 853 | // hostB expected to be there with reduced attachment point |
| 854 | HostEvent hostBrev = new HostEvent(macB); |
| 855 | hostBrev.addAttachmentPoint(portC.getSwitchPort()); |
| 856 | hostBrev.setLastSeenTime(timestampB); |
| 857 | hostBrev.freeze(); |
| 858 | assertEquals(hostBrev, topology.getHostEvent(macB)); |
| 859 | |
| 860 | |
| 861 | LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple()); |
| 862 | assertEquals(linkA, linkAInTopo); |
| 863 | assertTrue(linkAInTopo.isFrozen()); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 864 | assertEquals(TopologyElement.TYPE_OPTICAL_LAYER, |
| 865 | linkAInTopo.getType()); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 866 | |
| 867 | LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple()); |
| 868 | assertEquals(linkB, linkBInTopo); |
| 869 | assertTrue(linkBInTopo.isFrozen()); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 870 | assertEquals(TopologyElement.TYPE_OPTICAL_LAYER, |
| 871 | linkBInTopo.getType()); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 872 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 873 | // Check the events to be fired |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 874 | List<HostEvent> apiAddedHostEvents |
| 875 | = TestUtils.getField(theTopologyManager, "apiAddedHostEvents"); |
| 876 | assertThat(apiAddedHostEvents, hasItem(hostBrev)); |
| 877 | |
| 878 | List<HostEvent> apiRemovedHostEvents |
| 879 | = TestUtils.getField(theTopologyManager, "apiRemovedHostEvents"); |
| 880 | assertThat(apiRemovedHostEvents, hasItem(hostA)); |
| 881 | List<LinkEvent> apiAddedLinkEvents |
| 882 | = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents"); |
| 883 | assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB)); |
| 884 | } |
| 885 | |
| 886 | /** |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 887 | * Tests removing of a Link and the topology replica transformation. |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 888 | */ |
| 889 | @Test |
| 890 | public void testRemoveLink() { |
| 891 | setupTopologyManager(); |
| 892 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 893 | SwitchEvent sw = new SwitchEvent(DPID_1); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 894 | sw.createStringAttribute("foo", "bar"); |
| 895 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 896 | final PortNumber portNumberA = PortNumber.uint32(2); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 897 | PortEvent portA = new PortEvent(DPID_1, portNumberA); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 898 | portA.createStringAttribute("fuzz", "buzz"); |
| 899 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 900 | final PortNumber portNumberB = PortNumber.uint32(3); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 901 | PortEvent portB = new PortEvent(DPID_1, portNumberB); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 902 | portB.createStringAttribute("fizz", "buz"); |
| 903 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 904 | LinkEvent linkA = new LinkEvent(portA.getSwitchPort(), |
| 905 | portB.getSwitchPort()); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 906 | linkA.createStringAttribute(TopologyElement.TYPE, |
| 907 | TopologyElement.TYPE_OPTICAL_LAYER); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 908 | LinkEvent linkB = new LinkEvent(portB.getSwitchPort(), |
| 909 | portA.getSwitchPort()); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 910 | linkB.createStringAttribute(TopologyElement.TYPE, |
| 911 | TopologyElement.TYPE_OPTICAL_LAYER); |
| 912 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 913 | TestUtils.callMethod(theTopologyManager, "addSwitch", |
| 914 | SwitchEvent.class, sw); |
| 915 | TestUtils.callMethod(theTopologyManager, "addPort", |
| 916 | PortEvent.class, portA); |
| 917 | TestUtils.callMethod(theTopologyManager, "addPort", |
| 918 | PortEvent.class, portB); |
| 919 | TestUtils.callMethod(theTopologyManager, "addLink", |
| 920 | LinkEvent.class, linkA); |
| 921 | TestUtils.callMethod(theTopologyManager, "addLink", |
| 922 | LinkEvent.class, linkB); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 923 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 924 | // Check the topology structure |
| 925 | TopologyInternal topology = |
| 926 | (TopologyInternal) theTopologyManager.getTopology(); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 927 | SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 928 | assertEquals(sw, swInTopo); |
| 929 | assertTrue(swInTopo.isFrozen()); |
| 930 | assertEquals("bar", swInTopo.getStringAttribute("foo")); |
| 931 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 932 | final SwitchPort switchPortA = new SwitchPort(DPID_1, portNumberA); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 933 | PortEvent portAInTopo = topology.getPortEvent(switchPortA); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 934 | assertEquals(portA, portAInTopo); |
| 935 | assertTrue(portAInTopo.isFrozen()); |
| 936 | assertEquals("buzz", portAInTopo.getStringAttribute("fuzz")); |
| 937 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 938 | final SwitchPort switchPortB = new SwitchPort(DPID_1, portNumberB); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 939 | PortEvent portBInTopo = topology.getPortEvent(switchPortB); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 940 | assertEquals(portB, portBInTopo); |
| 941 | assertTrue(portBInTopo.isFrozen()); |
| 942 | assertEquals("buz", portBInTopo.getStringAttribute("fizz")); |
| 943 | |
| 944 | LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple()); |
| 945 | assertEquals(linkA, linkAInTopo); |
| 946 | assertTrue(linkAInTopo.isFrozen()); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 947 | assertEquals(TopologyElement.TYPE_OPTICAL_LAYER, |
| 948 | linkAInTopo.getType()); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 949 | |
| 950 | |
| 951 | LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple()); |
| 952 | assertEquals(linkB, linkBInTopo); |
| 953 | assertTrue(linkBInTopo.isFrozen()); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 954 | assertEquals(TopologyElement.TYPE_OPTICAL_LAYER, |
| 955 | linkBInTopo.getType()); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 956 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 957 | // Check the events to be fired |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 958 | // FIXME if link flapped (linkA in this scenario), |
| 959 | // linkA appears in both removed and added is this expected behavior? |
| 960 | List<LinkEvent> apiAddedLinkEvents |
| 961 | = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents"); |
| 962 | assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB)); |
| 963 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 964 | // Clear the events before removing the link |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 965 | apiAddedLinkEvents.clear(); |
| 966 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 967 | // Remove the link |
| 968 | TestUtils.callMethod(theTopologyManager, "removeLink", |
| 969 | LinkEvent.class, new LinkEvent(linkA)); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 970 | |
| 971 | LinkEvent linkANotInTopo = topology.getLinkEvent(linkA.getLinkTuple()); |
| 972 | assertNull(linkANotInTopo); |
| 973 | |
| 974 | List<LinkEvent> apiRemovedLinkEvents |
| 975 | = TestUtils.getField(theTopologyManager, "apiRemovedLinkEvents"); |
| 976 | assertThat(apiRemovedLinkEvents, hasItem(linkA)); |
| 977 | } |
| 978 | |
| 979 | /** |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 980 | * Tests adding of a Host without adding of a Link, and the topology |
| 981 | * replica transformation. |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 982 | */ |
| 983 | @Test |
| 984 | public void testAddHostIgnoredByLink() { |
| 985 | setupTopologyManager(); |
| 986 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 987 | SwitchEvent sw = new SwitchEvent(DPID_1); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 988 | sw.createStringAttribute("foo", "bar"); |
| 989 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 990 | final PortNumber portNumberA = PortNumber.uint32(2); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 991 | PortEvent portA = new PortEvent(DPID_1, portNumberA); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 992 | portA.createStringAttribute("fuzz", "buzz"); |
| 993 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 994 | final PortNumber portNumberB = PortNumber.uint32(3); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 995 | PortEvent portB = new PortEvent(DPID_1, portNumberB); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 996 | portB.createStringAttribute("fizz", "buz"); |
| 997 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 998 | final PortNumber portNumberC = PortNumber.uint32(4); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 999 | PortEvent portC = new PortEvent(DPID_1, portNumberC); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1000 | portC.createStringAttribute("fizz", "buz"); |
| 1001 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 1002 | LinkEvent linkA = new LinkEvent(portA.getSwitchPort(), |
| 1003 | portB.getSwitchPort()); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1004 | linkA.createStringAttribute(TopologyElement.TYPE, |
| 1005 | TopologyElement.TYPE_OPTICAL_LAYER); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 1006 | LinkEvent linkB = new LinkEvent(portB.getSwitchPort(), |
| 1007 | portA.getSwitchPort()); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1008 | linkB.createStringAttribute(TopologyElement.TYPE, |
| 1009 | TopologyElement.TYPE_OPTICAL_LAYER); |
| 1010 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 1011 | TestUtils.callMethod(theTopologyManager, "addSwitch", |
| 1012 | SwitchEvent.class, sw); |
| 1013 | TestUtils.callMethod(theTopologyManager, "addPort", |
| 1014 | PortEvent.class, portA); |
| 1015 | TestUtils.callMethod(theTopologyManager, "addPort", |
| 1016 | PortEvent.class, portB); |
| 1017 | TestUtils.callMethod(theTopologyManager, "addPort", |
| 1018 | PortEvent.class, portC); |
| 1019 | TestUtils.callMethod(theTopologyManager, "addLink", |
| 1020 | LinkEvent.class, linkA); |
| 1021 | TestUtils.callMethod(theTopologyManager, "addLink", |
| 1022 | LinkEvent.class, linkB); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1023 | |
| 1024 | // Add hostA attached to a port which already has a link |
| 1025 | final MACAddress macA = MACAddress.valueOf(666L); |
| 1026 | HostEvent hostA = new HostEvent(macA); |
| 1027 | hostA.addAttachmentPoint(portA.getSwitchPort()); |
| 1028 | final long timestampA = 392893200L; |
| 1029 | hostA.setLastSeenTime(timestampA); |
| 1030 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 1031 | TestUtils.callMethod(theTopologyManager, "addHost", |
| 1032 | HostEvent.class, hostA); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1033 | |
| 1034 | // Add hostB attached to multiple ports, |
| 1035 | // some of them which already has a link |
| 1036 | final MACAddress macB = MACAddress.valueOf(999L); |
| 1037 | HostEvent hostB = new HostEvent(macB); |
| 1038 | hostB.addAttachmentPoint(portB.getSwitchPort()); |
| 1039 | hostB.addAttachmentPoint(portC.getSwitchPort()); |
| 1040 | final long timestampB = 392893201L; |
| 1041 | hostB.setLastSeenTime(timestampB); |
| 1042 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 1043 | TestUtils.callMethod(theTopologyManager, "addHost", |
| 1044 | HostEvent.class, hostB); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1045 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 1046 | // Check the topology structure |
| 1047 | TopologyInternal topology = |
| 1048 | (TopologyInternal) theTopologyManager.getTopology(); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 1049 | SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1050 | assertEquals(sw, swInTopo); |
| 1051 | assertTrue(swInTopo.isFrozen()); |
| 1052 | assertEquals("bar", swInTopo.getStringAttribute("foo")); |
| 1053 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 1054 | final SwitchPort switchPortA = new SwitchPort(DPID_1, portNumberA); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 1055 | PortEvent portAInTopo = topology.getPortEvent(switchPortA); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1056 | assertEquals(portA, portAInTopo); |
| 1057 | assertTrue(portAInTopo.isFrozen()); |
| 1058 | assertEquals("buzz", portAInTopo.getStringAttribute("fuzz")); |
| 1059 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 1060 | final SwitchPort switchPortB = new SwitchPort(DPID_1, portNumberB); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 1061 | PortEvent portBInTopo = topology.getPortEvent(switchPortB); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1062 | assertEquals(portB, portBInTopo); |
| 1063 | assertTrue(portBInTopo.isFrozen()); |
| 1064 | assertEquals("buz", portBInTopo.getStringAttribute("fizz")); |
| 1065 | |
| 1066 | // hostA expected to be completely ignored |
| 1067 | assertNull(topology.getHostEvent(macA)); |
| 1068 | // hostB expected to be there with reduced attachment point |
| 1069 | HostEvent hostBrev = new HostEvent(macB); |
| 1070 | hostBrev.addAttachmentPoint(portC.getSwitchPort()); |
| 1071 | hostBrev.setLastSeenTime(timestampB); |
| 1072 | hostBrev.freeze(); |
| 1073 | assertEquals(hostBrev, topology.getHostEvent(macB)); |
| 1074 | |
| 1075 | |
| 1076 | LinkEvent linkAInTopo = topology.getLinkEvent(linkA.getLinkTuple()); |
| 1077 | assertEquals(linkA, linkAInTopo); |
| 1078 | assertTrue(linkAInTopo.isFrozen()); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 1079 | assertEquals(TopologyElement.TYPE_OPTICAL_LAYER, |
| 1080 | linkAInTopo.getType()); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1081 | |
| 1082 | LinkEvent linkBInTopo = topology.getLinkEvent(linkB.getLinkTuple()); |
| 1083 | assertEquals(linkB, linkBInTopo); |
| 1084 | assertTrue(linkBInTopo.isFrozen()); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 1085 | assertEquals(TopologyElement.TYPE_OPTICAL_LAYER, |
| 1086 | linkBInTopo.getType()); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1087 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 1088 | // Check the events to be fired |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1089 | // hostB should be added with reduced attachment points |
| 1090 | List<HostEvent> apiAddedHostEvents |
| 1091 | = TestUtils.getField(theTopologyManager, "apiAddedHostEvents"); |
| 1092 | assertThat(apiAddedHostEvents, hasItem(hostBrev)); |
| 1093 | |
| 1094 | // hostA should not be ignored |
| 1095 | List<HostEvent> apiRemovedHostEvents |
| 1096 | = TestUtils.getField(theTopologyManager, "apiRemovedHostEvents"); |
| 1097 | assertThat(apiRemovedHostEvents, not(hasItem(hostA))); |
| 1098 | |
| 1099 | List<LinkEvent> apiAddedLinkEvents |
| 1100 | = TestUtils.getField(theTopologyManager, "apiAddedLinkEvents"); |
| 1101 | assertThat(apiAddedLinkEvents, containsInAnyOrder(linkA, linkB)); |
| 1102 | } |
| 1103 | |
| 1104 | /** |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 1105 | * Tests adding and moving of a Host, and the topology replica |
| 1106 | * transformation. |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1107 | */ |
| 1108 | @Test |
| 1109 | public void testAddHostMove() { |
| 1110 | setupTopologyManager(); |
| 1111 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 1112 | SwitchEvent sw = new SwitchEvent(DPID_1); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1113 | sw.createStringAttribute("foo", "bar"); |
| 1114 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 1115 | final PortNumber portNumberA = PortNumber.uint32(2); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 1116 | PortEvent portA = new PortEvent(DPID_1, portNumberA); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1117 | portA.createStringAttribute("fuzz", "buzz"); |
| 1118 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 1119 | final PortNumber portNumberB = PortNumber.uint32(3); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 1120 | PortEvent portB = new PortEvent(DPID_1, portNumberB); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1121 | portB.createStringAttribute("fizz", "buz"); |
| 1122 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 1123 | final PortNumber portNumberC = PortNumber.uint32(4); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 1124 | PortEvent portC = new PortEvent(DPID_1, portNumberC); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1125 | portC.createStringAttribute("fizz", "buz"); |
| 1126 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 1127 | TestUtils.callMethod(theTopologyManager, "addSwitch", |
| 1128 | SwitchEvent.class, sw); |
| 1129 | TestUtils.callMethod(theTopologyManager, "addPort", |
| 1130 | PortEvent.class, portA); |
| 1131 | TestUtils.callMethod(theTopologyManager, "addPort", |
| 1132 | PortEvent.class, portB); |
| 1133 | TestUtils.callMethod(theTopologyManager, "addPort", |
| 1134 | PortEvent.class, portC); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1135 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 1136 | // Add hostA attached to a Port which already has a Link |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1137 | final MACAddress macA = MACAddress.valueOf(666L); |
| 1138 | HostEvent hostA = new HostEvent(macA); |
| 1139 | hostA.addAttachmentPoint(portA.getSwitchPort()); |
| 1140 | final long timestampA = 392893200L; |
| 1141 | hostA.setLastSeenTime(timestampA); |
| 1142 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 1143 | TestUtils.callMethod(theTopologyManager, "addHost", |
| 1144 | HostEvent.class, hostA); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1145 | |
| 1146 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 1147 | // Check the topology structure |
| 1148 | TopologyInternal topology = |
| 1149 | (TopologyInternal) theTopologyManager.getTopology(); |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 1150 | SwitchEvent swInTopo = topology.getSwitchEvent(DPID_1); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1151 | assertEquals(sw, swInTopo); |
| 1152 | assertTrue(swInTopo.isFrozen()); |
| 1153 | assertEquals("bar", swInTopo.getStringAttribute("foo")); |
| 1154 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 1155 | final SwitchPort switchPortA = new SwitchPort(DPID_1, portNumberA); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 1156 | PortEvent portAInTopo = topology.getPortEvent(switchPortA); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1157 | assertEquals(portA, portAInTopo); |
| 1158 | assertTrue(portAInTopo.isFrozen()); |
| 1159 | assertEquals("buzz", portAInTopo.getStringAttribute("fuzz")); |
| 1160 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 1161 | final SwitchPort switchPortB = new SwitchPort(DPID_1, portNumberB); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 1162 | PortEvent portBInTopo = topology.getPortEvent(switchPortB); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1163 | assertEquals(portB, portBInTopo); |
| 1164 | assertTrue(portBInTopo.isFrozen()); |
| 1165 | assertEquals("buz", portBInTopo.getStringAttribute("fizz")); |
| 1166 | |
| 1167 | // hostA expected to be there |
| 1168 | assertEquals(hostA, topology.getHostEvent(macA)); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 1169 | assertEquals(timestampA, |
| 1170 | topology.getHostEvent(macA).getLastSeenTime()); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1171 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 1172 | // Check the events to be fired |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1173 | // hostA should be added |
| 1174 | List<HostEvent> apiAddedHostEvents |
| 1175 | = TestUtils.getField(theTopologyManager, "apiAddedHostEvents"); |
| 1176 | assertThat(apiAddedHostEvents, hasItem(hostA)); |
| 1177 | |
| 1178 | |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 1179 | // Clear the events before moving the Host |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1180 | apiAddedHostEvents.clear(); |
| 1181 | |
| 1182 | HostEvent hostAmoved = new HostEvent(macA); |
| 1183 | hostAmoved.addAttachmentPoint(portB.getSwitchPort()); |
| 1184 | final long timestampAmoved = 392893201L; |
| 1185 | hostAmoved.setLastSeenTime(timestampAmoved); |
| 1186 | |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 1187 | TestUtils.callMethod(theTopologyManager, "addHost", |
| 1188 | HostEvent.class, hostAmoved); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1189 | |
| 1190 | assertEquals(hostAmoved, topology.getHostEvent(macA)); |
Pavlin Radoslavov | efa17b2 | 2014-08-01 16:57:56 -0700 | [diff] [blame] | 1191 | assertEquals(timestampAmoved, |
| 1192 | topology.getHostEvent(macA).getLastSeenTime()); |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 1193 | |
| 1194 | // hostA expected to be there with new attachment point |
| 1195 | apiAddedHostEvents |
| 1196 | = TestUtils.getField(theTopologyManager, "apiAddedHostEvents"); |
| 1197 | assertThat(apiAddedHostEvents, hasItem(hostAmoved)); |
| 1198 | |
| 1199 | // hostA is updated not removed |
| 1200 | List<HostEvent> apiRemovedHostEvents |
| 1201 | = TestUtils.getField(theTopologyManager, "apiRemovedHostEvents"); |
| 1202 | assertThat(apiRemovedHostEvents, not(hasItem(hostA))); |
| 1203 | } |
Pavlin Radoslavov | c623641 | 2014-08-01 20:37:46 -0700 | [diff] [blame] | 1204 | |
| 1205 | /** |
| 1206 | * Tests processing of a Switch Mastership Event and the delivery of the |
| 1207 | * topology events. |
| 1208 | */ |
| 1209 | @Test |
| 1210 | public void testProcessMastershipEvent() { |
| 1211 | List<EventEntry<TopologyEvent>> events = new LinkedList<>(); |
| 1212 | EventEntry<TopologyEvent> eventEntry; |
| 1213 | TopologyEvent topologyEvent; |
| 1214 | |
| 1215 | setupTopologyManagerWithEventHandler(); |
| 1216 | |
| 1217 | // Prepare the Mastership Event |
| 1218 | Role role = Role.MASTER; |
| 1219 | MastershipEvent mastershipEvent = |
| 1220 | new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role); |
| 1221 | topologyEvent = new TopologyEvent(mastershipEvent, ONOS_INSTANCE_ID_1); |
| 1222 | |
| 1223 | // Add the Mastership Event |
| 1224 | eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 1225 | topologyEvent); |
| 1226 | events.add(eventEntry); |
| 1227 | |
| 1228 | // Process the events |
| 1229 | TestUtils.callMethod(theEventHandler, "processEvents", |
| 1230 | List.class, events); |
| 1231 | |
| 1232 | // Check the fired events |
| 1233 | TopologyEvents topologyEvents = theTopologyListener.topologyEvents; |
| 1234 | assertNotNull(topologyEvents); |
| 1235 | assertThat(topologyEvents.getAddedMastershipEvents(), |
| 1236 | hasItem(mastershipEvent)); |
| 1237 | theTopologyListener.clear(); |
| 1238 | } |
| 1239 | |
| 1240 | /** |
| 1241 | * Tests processing of a Switch Event, and the delivery of the topology |
| 1242 | * events. |
| 1243 | * |
| 1244 | * We test the following scenario: |
| 1245 | * - Switch Mastership Event is processed along with a Switch Event - both |
| 1246 | * events should be delivered. |
| 1247 | */ |
| 1248 | @Test |
| 1249 | public void testProcessSwitchEvent() { |
| 1250 | TopologyEvents topologyEvents; |
| 1251 | List<EventEntry<TopologyEvent>> events = new LinkedList<>(); |
| 1252 | EventEntry<TopologyEvent> eventEntry; |
| 1253 | TopologyEvent topologyMastershipEvent; |
| 1254 | TopologyEvent topologySwitchEvent; |
| 1255 | |
| 1256 | setupTopologyManagerWithEventHandler(); |
| 1257 | |
| 1258 | // Prepare the Mastership Event |
| 1259 | Role role = Role.MASTER; |
| 1260 | MastershipEvent mastershipEvent = |
| 1261 | new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role); |
| 1262 | topologyMastershipEvent = new TopologyEvent(mastershipEvent, |
| 1263 | ONOS_INSTANCE_ID_1); |
| 1264 | |
| 1265 | // Prepare the Switch Event |
| 1266 | SwitchEvent switchEvent = new SwitchEvent(DPID_1); |
| 1267 | topologySwitchEvent = new TopologyEvent(switchEvent, |
| 1268 | ONOS_INSTANCE_ID_1); |
| 1269 | |
| 1270 | // Add the Mastership Event |
| 1271 | eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 1272 | topologyMastershipEvent); |
| 1273 | events.add(eventEntry); |
| 1274 | |
| 1275 | // Add the Switch Event |
| 1276 | eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 1277 | topologySwitchEvent); |
| 1278 | events.add(eventEntry); |
| 1279 | |
| 1280 | // Process the events |
| 1281 | TestUtils.callMethod(theEventHandler, "processEvents", |
| 1282 | List.class, events); |
| 1283 | |
| 1284 | // Check the fired events |
| 1285 | topologyEvents = theTopologyListener.topologyEvents; |
| 1286 | assertNotNull(topologyEvents); |
| 1287 | assertThat(topologyEvents.getAddedMastershipEvents(), |
| 1288 | hasItem(mastershipEvent)); |
| 1289 | assertThat(topologyEvents.getAddedSwitchEvents(), |
| 1290 | hasItem(switchEvent)); |
| 1291 | theTopologyListener.clear(); |
| 1292 | } |
| 1293 | |
| 1294 | /** |
| 1295 | * Tests processing of a misordered Switch Event, and the delivery of the |
| 1296 | * topology events. |
| 1297 | * |
| 1298 | * We test the following scenario: |
| 1299 | * - Only a Switch Event is processed first, later followed by a Switch |
| 1300 | * Mastership Event - the Switch Event should be delivered after the |
| 1301 | * Switch Mastership Event is processed. |
| 1302 | */ |
| 1303 | @Test |
| 1304 | public void testProcessMisorderedSwitchEvent() { |
| 1305 | TopologyEvents topologyEvents; |
| 1306 | List<EventEntry<TopologyEvent>> events = new LinkedList<>(); |
| 1307 | EventEntry<TopologyEvent> eventEntry; |
| 1308 | TopologyEvent topologyMastershipEvent; |
| 1309 | TopologyEvent topologySwitchEvent; |
| 1310 | |
| 1311 | setupTopologyManagerWithEventHandler(); |
| 1312 | |
| 1313 | // Prepare the Mastership Event |
| 1314 | Role role = Role.MASTER; |
| 1315 | MastershipEvent mastershipEvent = |
| 1316 | new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role); |
| 1317 | topologyMastershipEvent = new TopologyEvent(mastershipEvent, |
| 1318 | ONOS_INSTANCE_ID_1); |
| 1319 | |
| 1320 | // Prepare the Switch Event |
| 1321 | SwitchEvent switchEvent = new SwitchEvent(DPID_1); |
| 1322 | topologySwitchEvent = new TopologyEvent(switchEvent, |
| 1323 | ONOS_INSTANCE_ID_1); |
| 1324 | |
| 1325 | // Add the Switch Event |
| 1326 | eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 1327 | topologySwitchEvent); |
| 1328 | events.add(eventEntry); |
| 1329 | |
| 1330 | // Process the events |
| 1331 | TestUtils.callMethod(theEventHandler, "processEvents", |
| 1332 | List.class, events); |
| 1333 | |
| 1334 | // Check the fired events: no events should be fired |
| 1335 | topologyEvents = theTopologyListener.topologyEvents; |
| 1336 | assertNull(topologyEvents); |
| 1337 | theTopologyListener.clear(); |
| 1338 | events.clear(); |
| 1339 | |
| 1340 | // Add the Mastership Event |
| 1341 | eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 1342 | topologyMastershipEvent); |
| 1343 | events.add(eventEntry); |
| 1344 | |
| 1345 | // Process the events |
| 1346 | TestUtils.callMethod(theEventHandler, "processEvents", |
| 1347 | List.class, events); |
| 1348 | |
| 1349 | // Check the fired events: both events should be fired |
| 1350 | topologyEvents = theTopologyListener.topologyEvents; |
| 1351 | assertNotNull(topologyEvents); |
| 1352 | assertThat(topologyEvents.getAddedMastershipEvents(), |
| 1353 | hasItem(mastershipEvent)); |
| 1354 | assertThat(topologyEvents.getAddedSwitchEvents(), |
| 1355 | hasItem(switchEvent)); |
| 1356 | theTopologyListener.clear(); |
| 1357 | } |
| 1358 | |
| 1359 | /** |
| 1360 | * Tests processing of a Switch Event with Mastership Event from |
| 1361 | * another ONOS instance, and the delivery of the topology events. |
| 1362 | * |
| 1363 | * We test the following scenario: |
| 1364 | * - Only a Switch Event is processed first, later followed by a Switch |
| 1365 | * Mastership Event from another ONOS instance - only the Switch |
| 1366 | * Mastership Event should be delivered. |
| 1367 | */ |
| 1368 | @Test |
| 1369 | public void testProcessSwitchEventNoMastership() { |
| 1370 | TopologyEvents topologyEvents; |
| 1371 | List<EventEntry<TopologyEvent>> events = new LinkedList<>(); |
| 1372 | EventEntry<TopologyEvent> eventEntry; |
| 1373 | TopologyEvent topologyMastershipEvent; |
| 1374 | TopologyEvent topologySwitchEvent; |
| 1375 | |
| 1376 | setupTopologyManagerWithEventHandler(); |
| 1377 | |
| 1378 | // Prepare the Mastership Event |
| 1379 | Role role = Role.MASTER; |
| 1380 | MastershipEvent mastershipEvent = |
| 1381 | new MastershipEvent(DPID_2, ONOS_INSTANCE_ID_2, role); |
| 1382 | topologyMastershipEvent = new TopologyEvent(mastershipEvent, |
| 1383 | ONOS_INSTANCE_ID_2); |
| 1384 | |
| 1385 | // Prepare the Switch Event |
| 1386 | // NOTE: The originator (ONOS_INSTANCE_ID_1) is NOT the Master |
| 1387 | SwitchEvent switchEvent = new SwitchEvent(DPID_2); |
| 1388 | topologySwitchEvent = new TopologyEvent(switchEvent, |
| 1389 | ONOS_INSTANCE_ID_1); |
| 1390 | |
| 1391 | // Add the Switch Event |
| 1392 | eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 1393 | topologySwitchEvent); |
| 1394 | events.add(eventEntry); |
| 1395 | |
| 1396 | // Process the events |
| 1397 | TestUtils.callMethod(theEventHandler, "processEvents", |
| 1398 | List.class, events); |
| 1399 | |
| 1400 | // Check the fired events: no events should be fired |
| 1401 | topologyEvents = theTopologyListener.topologyEvents; |
| 1402 | assertNull(topologyEvents); |
| 1403 | theTopologyListener.clear(); |
| 1404 | events.clear(); |
| 1405 | |
| 1406 | // Add the Mastership Event |
| 1407 | eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 1408 | topologyMastershipEvent); |
| 1409 | events.add(eventEntry); |
| 1410 | |
| 1411 | // Process the events |
| 1412 | TestUtils.callMethod(theEventHandler, "processEvents", |
| 1413 | List.class, events); |
| 1414 | |
| 1415 | // Check the fired events: only the Mastership event should be fired |
| 1416 | topologyEvents = theTopologyListener.topologyEvents; |
| 1417 | assertNotNull(topologyEvents); |
| 1418 | assertThat(topologyEvents.getAddedMastershipEvents(), |
| 1419 | hasItem(mastershipEvent)); |
| 1420 | assertThat(topologyEvents.getAddedSwitchEvents(), is(empty())); |
| 1421 | theTopologyListener.clear(); |
| 1422 | } |
| 1423 | |
| 1424 | /** |
| 1425 | * Tests processing of Switch Events with Mastership switchover between |
| 1426 | * two ONOS instance, and the delivery of the topology events. |
| 1427 | * |
| 1428 | * We test the following scenario: |
| 1429 | * - Initially, a Mastership Event and a Switch Event from one ONOS |
| 1430 | * instance are processed - both events should be delivered. |
| 1431 | * - Later, a Mastership Event and a Switch event from another ONOS |
| 1432 | * instances are processed - both events should be delivered. |
| 1433 | * - Finally, a REMOVE Switch Event is received from the first ONOS |
| 1434 | * instance - no event should be delivered. |
| 1435 | */ |
| 1436 | @Test |
| 1437 | public void testProcessSwitchMastershipSwitchover() { |
| 1438 | TopologyEvents topologyEvents; |
| 1439 | List<EventEntry<TopologyEvent>> events = new LinkedList<>(); |
| 1440 | EventEntry<TopologyEvent> eventEntry; |
| 1441 | TopologyEvent topologyMastershipEvent; |
| 1442 | TopologyEvent topologySwitchEvent; |
| 1443 | |
| 1444 | setupTopologyManagerWithEventHandler(); |
| 1445 | |
| 1446 | // Prepare the Mastership Event from the first ONOS instance |
| 1447 | Role role = Role.MASTER; |
| 1448 | MastershipEvent mastershipEvent = |
| 1449 | new MastershipEvent(DPID_1, ONOS_INSTANCE_ID_1, role); |
| 1450 | topologyMastershipEvent = new TopologyEvent(mastershipEvent, |
| 1451 | ONOS_INSTANCE_ID_1); |
| 1452 | |
| 1453 | // Prepare the Switch Event from the first ONOS instance |
| 1454 | SwitchEvent switchEvent = new SwitchEvent(DPID_1); |
| 1455 | topologySwitchEvent = new TopologyEvent(switchEvent, |
| 1456 | ONOS_INSTANCE_ID_1); |
| 1457 | |
| 1458 | // Add the Mastership Event |
| 1459 | eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 1460 | topologyMastershipEvent); |
| 1461 | events.add(eventEntry); |
| 1462 | |
| 1463 | // Add the Switch Event |
| 1464 | eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 1465 | topologySwitchEvent); |
| 1466 | events.add(eventEntry); |
| 1467 | |
| 1468 | // Process the events |
| 1469 | TestUtils.callMethod(theEventHandler, "processEvents", |
| 1470 | List.class, events); |
| 1471 | |
| 1472 | // Check the fired events: both events should be fired |
| 1473 | topologyEvents = theTopologyListener.topologyEvents; |
| 1474 | assertNotNull(topologyEvents); |
| 1475 | assertThat(topologyEvents.getAddedMastershipEvents(), |
| 1476 | hasItem(mastershipEvent)); |
| 1477 | assertThat(topologyEvents.getAddedSwitchEvents(), |
| 1478 | hasItem(switchEvent)); |
| 1479 | theTopologyListener.clear(); |
| 1480 | events.clear(); |
| 1481 | |
| 1482 | // |
| 1483 | // Update the Registry Service, so the second ONOS instance is the |
| 1484 | // Master. |
| 1485 | // |
| 1486 | reset(registryService); |
| 1487 | try { |
| 1488 | expect(registryService.getControllerForSwitch(DPID_1.value())). |
| 1489 | andReturn(ONOS_INSTANCE_ID_2.toString()).anyTimes(); |
| 1490 | } catch (RegistryException ex) { |
| 1491 | throw new IllegalStateException(ex); |
| 1492 | } |
| 1493 | replay(registryService); |
| 1494 | |
| 1495 | // Prepare the Mastership Event from the second ONOS instance |
| 1496 | role = Role.MASTER; |
| 1497 | mastershipEvent = new MastershipEvent(DPID_1, |
| 1498 | ONOS_INSTANCE_ID_2, role); |
| 1499 | topologyMastershipEvent = new TopologyEvent(mastershipEvent, |
| 1500 | ONOS_INSTANCE_ID_2); |
| 1501 | |
| 1502 | // Prepare the Switch Event from second ONOS instance |
| 1503 | switchEvent = new SwitchEvent(DPID_1); |
| 1504 | topologySwitchEvent = new TopologyEvent(switchEvent, |
| 1505 | ONOS_INSTANCE_ID_2); |
| 1506 | |
| 1507 | // Add the Mastership Event |
| 1508 | eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 1509 | topologyMastershipEvent); |
| 1510 | events.add(eventEntry); |
| 1511 | |
| 1512 | // Add the Switch Event |
| 1513 | eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_ADD, |
| 1514 | topologySwitchEvent); |
| 1515 | events.add(eventEntry); |
| 1516 | |
| 1517 | // Process the events |
| 1518 | TestUtils.callMethod(theEventHandler, "processEvents", |
| 1519 | List.class, events); |
| 1520 | |
| 1521 | // Check the fired events: both events should be fired |
| 1522 | topologyEvents = theTopologyListener.topologyEvents; |
| 1523 | assertNotNull(topologyEvents); |
| 1524 | assertThat(topologyEvents.getAddedMastershipEvents(), |
| 1525 | hasItem(mastershipEvent)); |
| 1526 | assertThat(topologyEvents.getAddedSwitchEvents(), |
| 1527 | hasItem(switchEvent)); |
| 1528 | theTopologyListener.clear(); |
| 1529 | events.clear(); |
| 1530 | |
| 1531 | // Prepare the REMOVE Switch Event from first ONOS instance |
| 1532 | switchEvent = new SwitchEvent(DPID_1); |
| 1533 | topologySwitchEvent = new TopologyEvent(switchEvent, |
| 1534 | ONOS_INSTANCE_ID_1); |
| 1535 | // Add the Switch Event |
| 1536 | eventEntry = new EventEntry<TopologyEvent>(EventEntry.Type.ENTRY_REMOVE, |
| 1537 | topologySwitchEvent); |
| 1538 | events.add(eventEntry); |
| 1539 | |
| 1540 | // Process the events |
| 1541 | TestUtils.callMethod(theEventHandler, "processEvents", |
| 1542 | List.class, events); |
| 1543 | |
| 1544 | // Check the fired events: no events should be fired |
| 1545 | topologyEvents = theTopologyListener.topologyEvents; |
| 1546 | assertNull(topologyEvents); |
| 1547 | theTopologyListener.clear(); |
| 1548 | events.clear(); |
| 1549 | } |
weibit | f7c31a4 | 2014-06-23 16:51:01 -0700 | [diff] [blame] | 1550 | } |