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