Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.net.edgeservice.impl; |
| 17 | |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Lists; |
| 19 | import com.google.common.collect.Maps; |
| 20 | import com.google.common.collect.Sets; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 21 | import org.junit.After; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onosproject.common.event.impl.TestEventDispatcher; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 25 | import org.onosproject.event.Event; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 26 | import org.onosproject.net.ConnectPoint; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 27 | import org.onosproject.net.DefaultPort; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 28 | import org.onosproject.net.Device; |
| 29 | import org.onosproject.net.DeviceId; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 30 | import org.onosproject.net.NetTestTools; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 31 | import org.onosproject.net.Port; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 32 | import org.onosproject.net.PortNumber; |
| 33 | import org.onosproject.net.device.DeviceEvent; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 34 | import org.onosproject.net.device.DeviceServiceAdapter; |
| 35 | import org.onosproject.net.edge.EdgePortEvent; |
| 36 | import org.onosproject.net.edge.EdgePortListener; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 37 | import org.onosproject.net.flow.TrafficTreatment; |
| 38 | import org.onosproject.net.link.LinkEvent; |
| 39 | import org.onosproject.net.packet.OutboundPacket; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 40 | import org.onosproject.net.packet.PacketServiceAdapter; |
| 41 | import org.onosproject.net.topology.Topology; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 42 | import org.onosproject.net.topology.TopologyEvent; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 43 | import org.onosproject.net.topology.TopologyListener; |
| 44 | import org.onosproject.net.topology.TopologyServiceAdapter; |
| 45 | |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 46 | import java.nio.ByteBuffer; |
| 47 | import java.util.ArrayList; |
| 48 | import java.util.Iterator; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 49 | import java.util.List; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 50 | import java.util.Map; |
| 51 | import java.util.Optional; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 52 | import java.util.Set; |
| 53 | |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 54 | import static org.junit.Assert.*; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame^] | 55 | import static org.onosproject.net.NetTestTools.injectEventDispatcher; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 56 | import static org.onosproject.net.device.DeviceEvent.Type.*; |
| 57 | import static org.onosproject.net.edge.EdgePortEvent.Type.EDGE_PORT_ADDED; |
| 58 | import static org.onosproject.net.edge.EdgePortEvent.Type.EDGE_PORT_REMOVED; |
| 59 | import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED; |
| 60 | import static org.onosproject.net.link.LinkEvent.Type.LINK_REMOVED; |
| 61 | import static org.onosproject.net.topology.TopologyEvent.Type.TOPOLOGY_CHANGED; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 62 | |
| 63 | /** |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 64 | * Test of the edge port manager. Each device has ports '0' through 'numPorts - 1' |
| 65 | * as specified by the variable 'numPorts'. |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 66 | */ |
| 67 | public class EdgeManagerTest { |
| 68 | |
| 69 | private EdgeManager mgr; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 70 | private int totalPorts = 10; |
| 71 | private boolean alwaysReturnPorts = false; |
| 72 | private final Set<ConnectPoint> infrastructurePorts = Sets.newConcurrentHashSet(); |
| 73 | private List<EdgePortEvent> events = Lists.newArrayList(); |
| 74 | private final Map<DeviceId, Device> devices = Maps.newConcurrentMap(); |
| 75 | private Set<OutboundPacket> packets = Sets.newConcurrentHashSet(); |
| 76 | private final EdgePortListener testListener = new TestListener(events); |
| 77 | private TestTopologyManager testTopologyManager; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 78 | |
| 79 | @Before |
| 80 | public void setUp() { |
| 81 | mgr = new EdgeManager(); |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame^] | 82 | injectEventDispatcher(mgr, new TestEventDispatcher()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 83 | testTopologyManager = new TestTopologyManager(infrastructurePorts); |
| 84 | mgr.topologyService = testTopologyManager; |
| 85 | mgr.deviceService = new TestDeviceManager(devices); |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 86 | mgr.packetService = new TestPacketManager(); |
| 87 | mgr.activate(); |
| 88 | mgr.addListener(testListener); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 89 | |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 92 | |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 93 | @After |
| 94 | public void tearDown() { |
| 95 | mgr.removeListener(testListener); |
| 96 | mgr.deactivate(); |
| 97 | } |
| 98 | |
| 99 | @Test |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 100 | public void testBasics() { |
| 101 | //Setup |
| 102 | int numDevices = 20; |
| 103 | int numPorts = 4; |
| 104 | defaultPopulator(numDevices, numPorts); |
| 105 | |
| 106 | assertEquals("Unexpected number of ports", numDevices * numPorts, infrastructurePorts.size()); |
| 107 | |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 108 | assertFalse("no ports expected", mgr.getEdgePoints().iterator().hasNext()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 109 | |
| 110 | assertFalse("Expected isEdge to return false", |
| 111 | mgr.isEdgePoint(NetTestTools.connectPoint(Integer.toString(1), 1))); |
| 112 | |
| 113 | removeInfraPort(NetTestTools.connectPoint(Integer.toString(1), 1)); |
| 114 | assertTrue("Expected isEdge to return false", |
| 115 | mgr.isEdgePoint(NetTestTools.connectPoint(Integer.toString(1), 1))); |
| 116 | } |
| 117 | |
| 118 | @Test |
| 119 | public void testLinkUpdates() { |
| 120 | //Setup |
| 121 | ConnectPoint testPoint, referencePoint; |
| 122 | |
| 123 | //Testing link removal |
| 124 | List<Event> eventsToAdd = Lists.newArrayList(); |
| 125 | eventsToAdd.add(new LinkEvent(LINK_REMOVED, NetTestTools.link("a", 1, "b", 2))); |
| 126 | TopologyEvent event = new TopologyEvent(TOPOLOGY_CHANGED, null, eventsToAdd); |
| 127 | testTopologyManager.listener.event(event); |
| 128 | |
| 129 | assertTrue("The list contained an unexpected number of events", events.size() == 2); |
| 130 | assertTrue("The first element is of the wrong type.", |
| 131 | events.get(0).type() == EDGE_PORT_ADDED); |
| 132 | assertTrue("The second element is of the wrong type.", |
| 133 | events.get(1).type() == EDGE_PORT_ADDED); |
| 134 | |
| 135 | testPoint = events.get(0).subject(); |
| 136 | referencePoint = NetTestTools.connectPoint("a", 1); |
| 137 | assertTrue("The port numbers of the first element are incorrect", |
| 138 | testPoint.port().toLong() == referencePoint.port().toLong()); |
| 139 | assertTrue("The device id of the first element is incorrect.", |
| 140 | testPoint.deviceId().equals(referencePoint.deviceId())); |
| 141 | |
| 142 | testPoint = events.get(1).subject(); |
| 143 | referencePoint = NetTestTools.connectPoint("b", 2); |
| 144 | assertTrue("The port numbers of the second element are incorrect", |
| 145 | testPoint.port().toLong() == referencePoint.port().toLong()); |
| 146 | assertTrue("The device id of the second element is incorrect.", |
| 147 | testPoint.deviceId().equals(referencePoint.deviceId())); |
| 148 | |
| 149 | //Rebroadcast event to ensure it results in no additional events |
| 150 | testTopologyManager.listener.event(event); |
| 151 | assertTrue("The list contained an unexpected number of events", events.size() == 2); |
| 152 | |
| 153 | //Testing link adding when links to remove exist |
| 154 | eventsToAdd.clear(); |
| 155 | events.clear(); |
| 156 | eventsToAdd.add(new LinkEvent(LINK_ADDED, NetTestTools.link("a", 1, "b", 2))); |
| 157 | event = new TopologyEvent(TOPOLOGY_CHANGED, null, eventsToAdd); |
| 158 | testTopologyManager.listener.event(event); |
| 159 | |
| 160 | assertTrue("The list contained an unexpected number of events", events.size() == 2); |
| 161 | assertTrue("The first element is of the wrong type.", |
| 162 | events.get(0).type() == EDGE_PORT_REMOVED); |
| 163 | assertTrue("The second element is of the wrong type.", |
| 164 | events.get(1).type() == EDGE_PORT_REMOVED); |
| 165 | |
| 166 | testPoint = events.get(0).subject(); |
| 167 | referencePoint = NetTestTools.connectPoint("a", 1); |
| 168 | assertTrue("The port numbers of the first element are incorrect", |
| 169 | testPoint.port().toLong() == referencePoint.port().toLong()); |
| 170 | assertTrue("The device id of the first element is incorrect.", |
| 171 | testPoint.deviceId().equals(referencePoint.deviceId())); |
| 172 | |
| 173 | testPoint = events.get(1).subject(); |
| 174 | referencePoint = NetTestTools.connectPoint("b", 2); |
| 175 | assertTrue("The port numbers of the second element are incorrect", |
| 176 | testPoint.port().toLong() == referencePoint.port().toLong()); |
| 177 | assertTrue("The device id of the second element is incorrect.", |
| 178 | testPoint.deviceId().equals(referencePoint.deviceId())); |
| 179 | |
| 180 | //Apparent duplicate of previous method tests removal when the elements have already been removed |
| 181 | eventsToAdd.clear(); |
| 182 | events.clear(); |
| 183 | eventsToAdd.add(new LinkEvent(LINK_ADDED, NetTestTools.link("a", 1, "b", 2))); |
| 184 | event = new TopologyEvent(TOPOLOGY_CHANGED, null, eventsToAdd); |
| 185 | testTopologyManager.listener.event(event); |
| 186 | |
| 187 | assertTrue("The list should contain no events, the removed elements don't exist.", events.size() == 0); |
| 188 | } |
| 189 | |
| 190 | @Test |
| 191 | public void testDeviceUpdates() { |
| 192 | //Setup |
| 193 | |
| 194 | Device referenceDevice; |
| 195 | TopologyEvent event; |
| 196 | List<Event> eventsToAdd = Lists.newArrayList(); |
| 197 | int numDevices = 10; |
| 198 | int numInfraPorts = 5; |
| 199 | totalPorts = 10; |
| 200 | defaultPopulator(numDevices, numInfraPorts); |
| 201 | |
| 202 | //Test response to device added events |
| 203 | referenceDevice = NetTestTools.device("1"); |
| 204 | eventsToAdd.add(new DeviceEvent(DEVICE_ADDED, referenceDevice, |
| 205 | new DefaultPort(referenceDevice, PortNumber.portNumber(1), true))); |
| 206 | event = new TopologyEvent(TOPOLOGY_CHANGED, null, eventsToAdd); |
| 207 | testTopologyManager.listener.event(event); |
| 208 | |
| 209 | //Check that ports were populated correctly |
| 210 | assertTrue("Unexpected number of new ports added", |
| 211 | mgr.deviceService.getPorts(NetTestTools.did("1")).size() == 10); |
| 212 | |
| 213 | //Check that of the ten ports the half that are infrastructure ports aren't added |
| 214 | assertEquals("Unexpected number of new edge ports added", (totalPorts - numInfraPorts), events.size()); |
| 215 | |
| 216 | for (int index = 0; index < numInfraPorts; index++) { |
| 217 | assertTrue("Unexpected type of event", events.get(index).type() == EDGE_PORT_ADDED); |
| 218 | } |
| 219 | //Names here are irrelevant, the first 5 ports are populated as infrastructure, 6-10 are edge |
| 220 | for (int index = 0; index < events.size(); index++) { |
| 221 | assertEquals("Port added had unexpected port number.", |
| 222 | events.get(index).subject().port(), |
| 223 | NetTestTools.connectPoint("a", index + numInfraPorts + 1).port()); |
| 224 | } |
| 225 | events.clear(); |
| 226 | |
| 227 | //Repost the event to test repeated posts |
| 228 | testTopologyManager.listener.event(event); |
| 229 | assertEquals("The redundant notification should not have created additional notifications.", |
| 230 | 0, events.size()); |
| 231 | //Calculate the size of the returned iterable of edge points. |
| 232 | Iterable<ConnectPoint> pts = mgr.getEdgePoints(); |
| 233 | Iterator pointIterator = pts.iterator(); |
| 234 | int count = 0; |
| 235 | for (; pointIterator.hasNext(); count++) { |
| 236 | pointIterator.next(); |
| 237 | } |
| 238 | assertEquals("Unexpected number of edge points", totalPorts - numInfraPorts, count); |
| 239 | //Testing device removal |
| 240 | events.clear(); |
| 241 | eventsToAdd.clear(); |
| 242 | eventsToAdd.add(new DeviceEvent(DEVICE_REMOVED, referenceDevice, |
| 243 | new DefaultPort(referenceDevice, PortNumber.portNumber(1), true))); |
| 244 | event = new TopologyEvent(TOPOLOGY_CHANGED, null, eventsToAdd); |
| 245 | testTopologyManager.listener.event(event); |
| 246 | |
| 247 | assertEquals("There should be five new events from removal of edge points", |
| 248 | totalPorts - numInfraPorts, events.size()); |
| 249 | for (int index = 0; index < events.size(); index++) { |
| 250 | //Assert that the correct port numbers were removed in the correct order |
| 251 | assertEquals("Port removed had unexpected port number.", |
| 252 | events.get(index).subject().port(), |
| 253 | (NetTestTools.connectPoint("a", index + numInfraPorts + 1).port())); |
| 254 | //Assert that the events are of the correct type |
| 255 | assertEquals("Unexpected type of event", events.get(index).type(), EDGE_PORT_REMOVED); |
| 256 | } |
| 257 | events.clear(); |
| 258 | //Rebroadcast event to check that it triggers no new behavior |
| 259 | testTopologyManager.listener.event(event); |
| 260 | assertEquals("Rebroadcast of removal event should not produce additional events", |
| 261 | 0, events.size()); |
| 262 | |
| 263 | //Testing device status change, changed from unavailable to available |
| 264 | events.clear(); |
| 265 | eventsToAdd.clear(); |
| 266 | //Make sure that the devicemanager shows the device as available. |
| 267 | addDevice(referenceDevice, "1", 5); |
| 268 | devices.put(referenceDevice.id(), referenceDevice); |
| 269 | |
| 270 | eventsToAdd.add(new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, referenceDevice)); |
| 271 | event = new TopologyEvent(TOPOLOGY_CHANGED, null, eventsToAdd); |
| 272 | testTopologyManager.listener.event(event); |
| 273 | //An earlier setup set half of the reference device ports to infrastructure |
| 274 | assertEquals("An unexpected number of events were generated.", totalPorts - numInfraPorts, events.size()); |
| 275 | for (int i = 0; i < 5; i++) { |
| 276 | assertEquals("The event was not of the right type", events.get(i).type(), EDGE_PORT_ADDED); |
| 277 | } |
| 278 | events.clear(); |
| 279 | testTopologyManager.listener.event(event); |
| 280 | assertEquals("No events should have been generated for a set of existing ports.", 0, events.size()); |
| 281 | |
| 282 | //Test removal when state changes when the device becomes unavailable |
| 283 | |
| 284 | //Ensure that the deviceManager shows the device as unavailable |
| 285 | removeDevice(referenceDevice); |
| 286 | /*This variable copies the behavior of the topology by returning ports attached to an unavailable device |
| 287 | //this behavior is necessary for the following event to execute properly, if these statements are removed |
| 288 | no events will be generated since no ports will be provided in getPorts() to EdgeManager. |
| 289 | */ |
| 290 | alwaysReturnPorts = true; |
| 291 | testTopologyManager.listener.event(event); |
| 292 | alwaysReturnPorts = false; |
| 293 | assertEquals("An unexpected number of events were created.", totalPorts - numInfraPorts, events.size()); |
| 294 | for (int i = 0; i < 5; i++) { |
| 295 | EdgePortEvent edgeEvent = events.get(i); |
| 296 | assertEquals("The event is of an unexpected type.", |
| 297 | EdgePortEvent.Type.EDGE_PORT_REMOVED, edgeEvent.type()); |
| 298 | assertEquals("The event pertains to an unexpected port", PortNumber.portNumber(i + numInfraPorts + 1), |
| 299 | edgeEvent.subject().port()); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | @Test |
| 304 | public void testInternalCache() { |
| 305 | List<Event> eventsToAdd = Lists.newArrayList(); |
| 306 | int numDevices = 10; |
| 307 | //Number of infrastructure ports per device |
| 308 | int numPorts = 5; |
| 309 | //Total ports per device when requesting all devices |
| 310 | totalPorts = 10; |
| 311 | defaultPopulator(numDevices, numPorts); |
| 312 | for (int i = 0; i < numDevices; i++) { |
| 313 | Device newDevice = NetTestTools.device(Integer.toString(i)); |
| 314 | devices.put(newDevice.id(), newDevice); |
| 315 | eventsToAdd.add(new DeviceEvent(DEVICE_ADDED, newDevice)); |
| 316 | } |
| 317 | TopologyEvent event = new TopologyEvent(TOPOLOGY_CHANGED, null, eventsToAdd); |
| 318 | testTopologyManager.listener.event(event); |
| 319 | |
| 320 | //Check all ports have correct designations |
| 321 | ConnectPoint testPoint; |
| 322 | for (int deviceNum = 0; deviceNum < numDevices; deviceNum++) { |
| 323 | for (int portNum = 1; portNum <= totalPorts; portNum++) { |
| 324 | testPoint = NetTestTools.connectPoint(Integer.toString(deviceNum), portNum); |
| 325 | if (portNum <= numPorts) { |
| 326 | assertFalse("This should not be an edge point", mgr.isEdgePoint(testPoint)); |
| 327 | } else { |
| 328 | assertTrue("This should be an edge point", mgr.isEdgePoint(testPoint)); |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | int count = 0; |
| 333 | for (ConnectPoint ignored : mgr.getEdgePoints()) { |
| 334 | count++; |
| 335 | } |
| 336 | assertEquals("There are an unexpeceted number of edge points returned.", |
| 337 | (totalPorts - numPorts) * numDevices, count); |
| 338 | for (int deviceNumber = 0; deviceNumber < numDevices; deviceNumber++) { |
| 339 | count = 0; |
| 340 | for (ConnectPoint ignored : mgr.getEdgePoints(NetTestTools.did("1"))) { |
| 341 | count++; |
| 342 | } |
| 343 | assertEquals("This element has an unexpected number of edge points.", (totalPorts - numPorts), count); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | |
| 348 | @Test |
| 349 | public void testEmit() { |
| 350 | byte[] arr = new byte[10]; |
| 351 | Device referenceDevice; |
| 352 | TopologyEvent event; |
| 353 | List<Event> eventsToAdd = Lists.newArrayList(); |
| 354 | int numDevices = 10; |
| 355 | int numInfraPorts = 5; |
| 356 | totalPorts = 10; |
| 357 | defaultPopulator(numDevices, numInfraPorts); |
| 358 | for (byte byteIndex = 0; byteIndex < arr.length; byteIndex++) { |
| 359 | arr[byteIndex] = byteIndex; |
| 360 | } |
| 361 | for (int i = 0; i < numDevices; i++) { |
| 362 | referenceDevice = NetTestTools.device(Integer.toString(i)); |
| 363 | eventsToAdd.add(new DeviceEvent(DEVICE_ADDED, referenceDevice, |
| 364 | new DefaultPort(referenceDevice, PortNumber.portNumber(1), true))); |
| 365 | } |
| 366 | event = new TopologyEvent(TOPOLOGY_CHANGED, null, eventsToAdd); |
| 367 | testTopologyManager.listener.event(event); |
| 368 | |
| 369 | mgr.emitPacket(ByteBuffer.wrap(arr), Optional.<TrafficTreatment>empty()); |
| 370 | |
| 371 | assertEquals("There were an unexpected number of emitted packets", |
| 372 | (totalPorts - numInfraPorts) * numDevices, packets.size()); |
| 373 | Iterator<OutboundPacket> packetIter = packets.iterator(); |
| 374 | OutboundPacket packet; |
| 375 | while (packetIter.hasNext()) { |
| 376 | packet = packetIter.next(); |
| 377 | assertEquals("The packet had an incorrect payload.", arr, packet.data().array()); |
| 378 | } |
| 379 | //Start testing emission to a specific device |
| 380 | packets.clear(); |
| 381 | mgr.emitPacket(NetTestTools.did(Integer.toString(1)), ByteBuffer.wrap(arr), Optional.<TrafficTreatment>empty()); |
| 382 | |
| 383 | assertEquals("Unexpected number of outbound packets were emitted.", |
| 384 | totalPorts - numInfraPorts, packets.size()); |
| 385 | packetIter = packets.iterator(); |
| 386 | while (packetIter.hasNext()) { |
| 387 | packet = packetIter.next(); |
| 388 | assertEquals("The packet had an incorrect payload", arr, packet.data().array()); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | |
| 393 | /** |
| 394 | * @param numDevices the number of devices to populate. |
| 395 | * @param numInfraPorts the number of ports to be set as infrastructure on each device, numbered base 0, ports 0 |
| 396 | * through numInfraPorts - 1 |
| 397 | */ |
| 398 | private void defaultPopulator(int numDevices, int numInfraPorts) { |
| 399 | for (int device = 0; device < numDevices; device++) { |
| 400 | String str = Integer.toString(device); |
| 401 | Device deviceToAdd = NetTestTools.device(str); |
| 402 | devices.put(deviceToAdd.id(), deviceToAdd); |
| 403 | for (int port = 1; port <= numInfraPorts; port++) { |
| 404 | infrastructurePorts.add(NetTestTools.connectPoint(str, port)); |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * Adds the specified device with the specified number of edge ports so long as it is less than the total ports. |
| 411 | * |
| 412 | * @param device The device to be added |
| 413 | * @param deviceName The name given to generate the devices DID |
| 414 | * @param numInfraPorts The number of ports to be added numbered 1 ... numInfraPorts |
| 415 | */ |
| 416 | private void addDevice(Device device, String deviceName, int numInfraPorts) { |
| 417 | if (!devices.keySet().contains(device.id())) { |
| 418 | devices.put(device.id(), device); |
| 419 | for (int i = 1; i <= numInfraPorts && i <= totalPorts; i++) { |
| 420 | infrastructurePorts.add(NetTestTools.connectPoint(deviceName, i)); |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | private void removeDevice(Device device) { |
| 426 | devices.remove(device.id()); |
| 427 | } |
| 428 | |
| 429 | private void removeInfraPort(ConnectPoint port) { |
| 430 | infrastructurePorts.remove(port); |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | private class TestTopologyManager extends TopologyServiceAdapter { |
| 434 | private TopologyListener listener; |
| 435 | private Set<ConnectPoint> infrastructurePorts; |
| 436 | |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 437 | public TestTopologyManager(Set<ConnectPoint> infrastructurePorts) { |
| 438 | this.infrastructurePorts = infrastructurePorts; |
| 439 | } |
| 440 | |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 441 | @Override |
| 442 | public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) { |
| 443 | return infrastructurePorts.contains(connectPoint); |
| 444 | } |
| 445 | |
| 446 | @Override |
| 447 | public void addListener(TopologyListener listener) { |
| 448 | this.listener = listener; |
| 449 | } |
| 450 | |
| 451 | @Override |
| 452 | public void removeListener(TopologyListener listener) { |
| 453 | this.listener = null; |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | private class TestDeviceManager extends DeviceServiceAdapter { |
| 458 | |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 459 | private Map<DeviceId, Device> devices; |
| 460 | |
| 461 | public TestDeviceManager(Map<DeviceId, Device> devices) { |
| 462 | this.devices = devices; |
| 463 | } |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 464 | |
| 465 | @Override |
| 466 | public boolean isAvailable(DeviceId deviceId) { |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 467 | for (DeviceId id : devices.keySet()) { |
| 468 | if (id.equals(deviceId)) { |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 469 | return true; |
| 470 | } |
| 471 | } |
| 472 | return false; |
| 473 | } |
| 474 | |
| 475 | @Override |
| 476 | public List<Port> getPorts(DeviceId deviceId) { |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 477 | List<Port> ports = new ArrayList<>(); |
| 478 | Device device = devices.get(deviceId); |
| 479 | if (device == null && !alwaysReturnPorts) { |
| 480 | return ports; |
| 481 | } |
| 482 | for (int portNum = 1; portNum <= totalPorts; portNum++) { |
| 483 | //String is generated using 'of:' + the passed name, this creates a |
| 484 | ports.add(new DefaultPort(device, PortNumber.portNumber(portNum), true)); |
| 485 | } |
| 486 | return ports; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | @Override |
| 490 | public Iterable<Device> getAvailableDevices() { |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 491 | return devices.values(); |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
| 495 | private class TestPacketManager extends PacketServiceAdapter { |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 496 | @Override |
| 497 | public void emit(OutboundPacket packet) { |
| 498 | packets.add(packet); |
| 499 | } |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 502 | private class TestListener implements EdgePortListener { |
| 503 | private List<EdgePortEvent> events; |
| 504 | |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 505 | public TestListener(List<EdgePortEvent> events) { |
| 506 | this.events = events; |
| 507 | } |
| 508 | |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 509 | @Override |
| 510 | public void event(EdgePortEvent event) { |
| 511 | events.add(event); |
| 512 | } |
| 513 | } |
| 514 | } |