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