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