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