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