blob: 2f55de92792c80a29a760461bbf4a28f437c6e7e [file] [log] [blame]
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -07003 *
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 */
16package org.onosproject.net.edgeservice.impl;
17
Aaron Kruglikove1200592015-06-29 16:31:23 -070018import com.google.common.collect.Lists;
19import com.google.common.collect.Maps;
20import com.google.common.collect.Sets;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070021import org.junit.After;
22import org.junit.Before;
23import org.junit.Test;
24import org.onosproject.common.event.impl.TestEventDispatcher;
25import org.onosproject.net.ConnectPoint;
Aaron Kruglikove1200592015-06-29 16:31:23 -070026import org.onosproject.net.DefaultPort;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070027import org.onosproject.net.Device;
28import org.onosproject.net.DeviceId;
Aaron Kruglikove1200592015-06-29 16:31:23 -070029import org.onosproject.net.NetTestTools;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070030import org.onosproject.net.Port;
Aaron Kruglikove1200592015-06-29 16:31:23 -070031import org.onosproject.net.PortNumber;
32import org.onosproject.net.device.DeviceEvent;
alshabib8a4a6002015-11-25 14:31:16 -080033import org.onosproject.net.device.DeviceListener;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070034import org.onosproject.net.device.DeviceServiceAdapter;
35import org.onosproject.net.edge.EdgePortEvent;
36import org.onosproject.net.edge.EdgePortListener;
Aaron Kruglikove1200592015-06-29 16:31:23 -070037import org.onosproject.net.flow.TrafficTreatment;
38import org.onosproject.net.link.LinkEvent;
alshabib8a4a6002015-11-25 14:31:16 -080039import org.onosproject.net.link.LinkListener;
40import org.onosproject.net.link.LinkServiceAdapter;
Aaron Kruglikove1200592015-06-29 16:31:23 -070041import org.onosproject.net.packet.OutboundPacket;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070042import org.onosproject.net.packet.PacketServiceAdapter;
43import org.onosproject.net.topology.Topology;
44import org.onosproject.net.topology.TopologyListener;
45import org.onosproject.net.topology.TopologyServiceAdapter;
46
Aaron Kruglikove1200592015-06-29 16:31:23 -070047import java.nio.ByteBuffer;
48import java.util.ArrayList;
49import java.util.Iterator;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070050import java.util.List;
Aaron Kruglikove1200592015-06-29 16:31:23 -070051import java.util.Map;
52import java.util.Optional;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070053import java.util.Set;
54
Aaron Kruglikove1200592015-06-29 16:31:23 -070055import static org.junit.Assert.*;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070056import static org.onosproject.net.NetTestTools.injectEventDispatcher;
Aaron Kruglikove1200592015-06-29 16:31:23 -070057import static org.onosproject.net.device.DeviceEvent.Type.*;
58import static org.onosproject.net.edge.EdgePortEvent.Type.EDGE_PORT_ADDED;
59import static org.onosproject.net.edge.EdgePortEvent.Type.EDGE_PORT_REMOVED;
60import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED;
61import static org.onosproject.net.link.LinkEvent.Type.LINK_REMOVED;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070062
63/**
Aaron Kruglikove1200592015-06-29 16:31:23 -070064 * Test of the edge port manager. Each device has ports '0' through 'numPorts - 1'
65 * as specified by the variable 'numPorts'.
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070066 */
67public class EdgeManagerTest {
68
69 private EdgeManager mgr;
Aaron Kruglikove1200592015-06-29 16:31:23 -070070 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);
alshabib8a4a6002015-11-25 14:31:16 -080077 private TestLinkManager testLinkManager;
78 private TestDeviceManager testDeviceManager;
Aaron Kruglikove1200592015-06-29 16:31:23 -070079 private TestTopologyManager testTopologyManager;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070080
81 @Before
82 public void setUp() {
83 mgr = new EdgeManager();
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070084 injectEventDispatcher(mgr, new TestEventDispatcher());
Aaron Kruglikove1200592015-06-29 16:31:23 -070085 testTopologyManager = new TestTopologyManager(infrastructurePorts);
86 mgr.topologyService = testTopologyManager;
alshabib8a4a6002015-11-25 14:31:16 -080087 testDeviceManager = new TestDeviceManager(devices);
88 mgr.deviceService = testDeviceManager;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070089 mgr.packetService = new TestPacketManager();
alshabib8a4a6002015-11-25 14:31:16 -080090 testLinkManager = new TestLinkManager();
91 mgr.linkService = testLinkManager;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070092 mgr.activate();
93 mgr.addListener(testListener);
Aaron Kruglikove1200592015-06-29 16:31:23 -070094
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070095 }
96
Aaron Kruglikove1200592015-06-29 16:31:23 -070097
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070098 @After
99 public void tearDown() {
100 mgr.removeListener(testListener);
101 mgr.deactivate();
102 }
103
104 @Test
Aaron Kruglikove1200592015-06-29 16:31:23 -0700105 public void testBasics() {
106 //Setup
107 int numDevices = 20;
108 int numPorts = 4;
109 defaultPopulator(numDevices, numPorts);
110
111 assertEquals("Unexpected number of ports", numDevices * numPorts, infrastructurePorts.size());
112
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700113 assertFalse("no ports expected", mgr.getEdgePoints().iterator().hasNext());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700114
115 assertFalse("Expected isEdge to return false",
alshabib8a4a6002015-11-25 14:31:16 -0800116 mgr.isEdgePoint(NetTestTools.connectPoint(Integer.toString(1), 1)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700117
118 removeInfraPort(NetTestTools.connectPoint(Integer.toString(1), 1));
119 assertTrue("Expected isEdge to return false",
alshabib8a4a6002015-11-25 14:31:16 -0800120 mgr.isEdgePoint(NetTestTools.connectPoint(Integer.toString(1), 1)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700121 }
122
123 @Test
124 public void testLinkUpdates() {
125 //Setup
126 ConnectPoint testPoint, referencePoint;
127
128 //Testing link removal
alshabib8a4a6002015-11-25 14:31:16 -0800129 testLinkManager.listener.event(new LinkEvent(LINK_REMOVED, NetTestTools.link("a", 1, "b", 2)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700130
131 assertTrue("The list contained an unexpected number of events", events.size() == 2);
132 assertTrue("The first element is of the wrong type.",
alshabib8a4a6002015-11-25 14:31:16 -0800133 events.get(0).type() == EDGE_PORT_ADDED);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700134
135 testPoint = events.get(0).subject();
136 referencePoint = NetTestTools.connectPoint("a", 1);
137 assertTrue("The port numbers of the first element are incorrect",
alshabib8a4a6002015-11-25 14:31:16 -0800138 testPoint.port().toLong() == referencePoint.port().toLong());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700139 assertTrue("The device id of the first element is incorrect.",
alshabib8a4a6002015-11-25 14:31:16 -0800140 testPoint.deviceId().equals(referencePoint.deviceId()));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700141
142 testPoint = events.get(1).subject();
143 referencePoint = NetTestTools.connectPoint("b", 2);
144 assertTrue("The port numbers of the second element are incorrect",
alshabib8a4a6002015-11-25 14:31:16 -0800145 testPoint.port().toLong() == referencePoint.port().toLong());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700146 assertTrue("The device id of the second element is incorrect.",
alshabib8a4a6002015-11-25 14:31:16 -0800147 testPoint.deviceId().equals(referencePoint.deviceId()));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700148
149 //Rebroadcast event to ensure it results in no additional events
alshabib8a4a6002015-11-25 14:31:16 -0800150 testLinkManager.listener.event(new LinkEvent(LINK_REMOVED, NetTestTools.link("a", 1, "b", 2)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700151 assertTrue("The list contained an unexpected number of events", events.size() == 2);
152
153 //Testing link adding when links to remove exist
Aaron Kruglikove1200592015-06-29 16:31:23 -0700154 events.clear();
alshabib8a4a6002015-11-25 14:31:16 -0800155 testLinkManager.listener.event(new LinkEvent(LINK_ADDED, NetTestTools.link("a", 1, "b", 2)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700156
157 assertTrue("The list contained an unexpected number of events", events.size() == 2);
158 assertTrue("The first element is of the wrong type.",
alshabib8a4a6002015-11-25 14:31:16 -0800159 events.get(0).type() == EDGE_PORT_REMOVED);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700160 assertTrue("The second element is of the wrong type.",
alshabib8a4a6002015-11-25 14:31:16 -0800161 events.get(1).type() == EDGE_PORT_REMOVED);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700162
163 testPoint = events.get(0).subject();
164 referencePoint = NetTestTools.connectPoint("a", 1);
165 assertTrue("The port numbers of the first element are incorrect",
alshabib8a4a6002015-11-25 14:31:16 -0800166 testPoint.port().toLong() == referencePoint.port().toLong());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700167 assertTrue("The device id of the first element is incorrect.",
alshabib8a4a6002015-11-25 14:31:16 -0800168 testPoint.deviceId().equals(referencePoint.deviceId()));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700169
170 testPoint = events.get(1).subject();
171 referencePoint = NetTestTools.connectPoint("b", 2);
172 assertTrue("The port numbers of the second element are incorrect",
alshabib8a4a6002015-11-25 14:31:16 -0800173 testPoint.port().toLong() == referencePoint.port().toLong());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700174 assertTrue("The device id of the second element is incorrect.",
alshabib8a4a6002015-11-25 14:31:16 -0800175 testPoint.deviceId().equals(referencePoint.deviceId()));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700176
177 //Apparent duplicate of previous method tests removal when the elements have already been removed
Aaron Kruglikove1200592015-06-29 16:31:23 -0700178 events.clear();
alshabib8a4a6002015-11-25 14:31:16 -0800179 testLinkManager.listener.event(new LinkEvent(LINK_ADDED, NetTestTools.link("a", 1, "b", 2)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700180 assertTrue("The list should contain no events, the removed elements don't exist.", events.size() == 0);
181 }
182
183 @Test
184 public void testDeviceUpdates() {
185 //Setup
186
187 Device referenceDevice;
alshabib8a4a6002015-11-25 14:31:16 -0800188 DeviceEvent event;
Aaron Kruglikove1200592015-06-29 16:31:23 -0700189 int numDevices = 10;
190 int numInfraPorts = 5;
191 totalPorts = 10;
192 defaultPopulator(numDevices, numInfraPorts);
193
194 //Test response to device added events
195 referenceDevice = NetTestTools.device("1");
alshabib8a4a6002015-11-25 14:31:16 -0800196 event = new DeviceEvent(DEVICE_ADDED, referenceDevice,
197 new DefaultPort(referenceDevice, PortNumber.portNumber(1), true));
198 testDeviceManager.listener.event(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700199
200 //Check that ports were populated correctly
201 assertTrue("Unexpected number of new ports added",
alshabib8a4a6002015-11-25 14:31:16 -0800202 mgr.deviceService.getPorts(NetTestTools.did("1")).size() == 10);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700203
204 //Check that of the ten ports the half that are infrastructure ports aren't added
205 assertEquals("Unexpected number of new edge ports added", (totalPorts - numInfraPorts), events.size());
206
207 for (int index = 0; index < numInfraPorts; index++) {
208 assertTrue("Unexpected type of event", events.get(index).type() == EDGE_PORT_ADDED);
209 }
210 //Names here are irrelevant, the first 5 ports are populated as infrastructure, 6-10 are edge
211 for (int index = 0; index < events.size(); index++) {
212 assertEquals("Port added had unexpected port number.",
alshabib8a4a6002015-11-25 14:31:16 -0800213 events.get(index).subject().port(),
214 NetTestTools.connectPoint("a", index + numInfraPorts + 1).port());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700215 }
216 events.clear();
217
218 //Repost the event to test repeated posts
alshabib8a4a6002015-11-25 14:31:16 -0800219 testDeviceManager.listener.event(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700220 assertEquals("The redundant notification should not have created additional notifications.",
alshabib8a4a6002015-11-25 14:31:16 -0800221 0, events.size());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700222 //Calculate the size of the returned iterable of edge points.
223 Iterable<ConnectPoint> pts = mgr.getEdgePoints();
224 Iterator pointIterator = pts.iterator();
225 int count = 0;
226 for (; pointIterator.hasNext(); count++) {
227 pointIterator.next();
228 }
229 assertEquals("Unexpected number of edge points", totalPorts - numInfraPorts, count);
230 //Testing device removal
231 events.clear();
alshabib8a4a6002015-11-25 14:31:16 -0800232 event = (new DeviceEvent(DEVICE_REMOVED, referenceDevice,
233 new DefaultPort(referenceDevice, PortNumber.portNumber(1), true)));
234 testDeviceManager.listener.event(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700235
236 assertEquals("There should be five new events from removal of edge points",
alshabib8a4a6002015-11-25 14:31:16 -0800237 totalPorts - numInfraPorts, events.size());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700238 for (int index = 0; index < events.size(); index++) {
239 //Assert that the correct port numbers were removed in the correct order
240 assertEquals("Port removed had unexpected port number.",
alshabib8a4a6002015-11-25 14:31:16 -0800241 events.get(index).subject().port(),
242 (NetTestTools.connectPoint("a", index + numInfraPorts + 1).port()));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700243 //Assert that the events are of the correct type
244 assertEquals("Unexpected type of event", events.get(index).type(), EDGE_PORT_REMOVED);
245 }
246 events.clear();
247 //Rebroadcast event to check that it triggers no new behavior
alshabib8a4a6002015-11-25 14:31:16 -0800248 testDeviceManager.listener.event(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700249 assertEquals("Rebroadcast of removal event should not produce additional events",
alshabib8a4a6002015-11-25 14:31:16 -0800250 0, events.size());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700251
252 //Testing device status change, changed from unavailable to available
253 events.clear();
Aaron Kruglikove1200592015-06-29 16:31:23 -0700254 //Make sure that the devicemanager shows the device as available.
255 addDevice(referenceDevice, "1", 5);
256 devices.put(referenceDevice.id(), referenceDevice);
257
alshabib8a4a6002015-11-25 14:31:16 -0800258 event = new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, referenceDevice);
259 testDeviceManager.listener.event(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700260 //An earlier setup set half of the reference device ports to infrastructure
261 assertEquals("An unexpected number of events were generated.", totalPorts - numInfraPorts, events.size());
262 for (int i = 0; i < 5; i++) {
263 assertEquals("The event was not of the right type", events.get(i).type(), EDGE_PORT_ADDED);
264 }
265 events.clear();
alshabib8a4a6002015-11-25 14:31:16 -0800266 testDeviceManager.listener.event(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700267 assertEquals("No events should have been generated for a set of existing ports.", 0, events.size());
268
269 //Test removal when state changes when the device becomes unavailable
270
271 //Ensure that the deviceManager shows the device as unavailable
272 removeDevice(referenceDevice);
273 /*This variable copies the behavior of the topology by returning ports attached to an unavailable device
274 //this behavior is necessary for the following event to execute properly, if these statements are removed
275 no events will be generated since no ports will be provided in getPorts() to EdgeManager.
276 */
277 alwaysReturnPorts = true;
alshabib8a4a6002015-11-25 14:31:16 -0800278 testDeviceManager.listener.event(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700279 alwaysReturnPorts = false;
280 assertEquals("An unexpected number of events were created.", totalPorts - numInfraPorts, events.size());
281 for (int i = 0; i < 5; i++) {
282 EdgePortEvent edgeEvent = events.get(i);
283 assertEquals("The event is of an unexpected type.",
alshabib8a4a6002015-11-25 14:31:16 -0800284 EdgePortEvent.Type.EDGE_PORT_REMOVED, edgeEvent.type());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700285 assertEquals("The event pertains to an unexpected port", PortNumber.portNumber(i + numInfraPorts + 1),
alshabib8a4a6002015-11-25 14:31:16 -0800286 edgeEvent.subject().port());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700287 }
288 }
289
290 @Test
291 public void testInternalCache() {
Aaron Kruglikove1200592015-06-29 16:31:23 -0700292 int numDevices = 10;
293 //Number of infrastructure ports per device
294 int numPorts = 5;
295 //Total ports per device when requesting all devices
296 totalPorts = 10;
297 defaultPopulator(numDevices, numPorts);
298 for (int i = 0; i < numDevices; i++) {
299 Device newDevice = NetTestTools.device(Integer.toString(i));
300 devices.put(newDevice.id(), newDevice);
alshabib8a4a6002015-11-25 14:31:16 -0800301 testDeviceManager.listener.event(new DeviceEvent(DEVICE_ADDED, newDevice));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700302 }
Aaron Kruglikove1200592015-06-29 16:31:23 -0700303 //Check all ports have correct designations
304 ConnectPoint testPoint;
305 for (int deviceNum = 0; deviceNum < numDevices; deviceNum++) {
306 for (int portNum = 1; portNum <= totalPorts; portNum++) {
307 testPoint = NetTestTools.connectPoint(Integer.toString(deviceNum), portNum);
308 if (portNum <= numPorts) {
309 assertFalse("This should not be an edge point", mgr.isEdgePoint(testPoint));
310 } else {
311 assertTrue("This should be an edge point", mgr.isEdgePoint(testPoint));
312 }
313 }
314 }
315 int count = 0;
316 for (ConnectPoint ignored : mgr.getEdgePoints()) {
317 count++;
318 }
319 assertEquals("There are an unexpeceted number of edge points returned.",
alshabib8a4a6002015-11-25 14:31:16 -0800320 (totalPorts - numPorts) * numDevices, count);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700321 for (int deviceNumber = 0; deviceNumber < numDevices; deviceNumber++) {
322 count = 0;
323 for (ConnectPoint ignored : mgr.getEdgePoints(NetTestTools.did("1"))) {
324 count++;
325 }
326 assertEquals("This element has an unexpected number of edge points.", (totalPorts - numPorts), count);
327 }
328 }
329
330
331 @Test
332 public void testEmit() {
333 byte[] arr = new byte[10];
334 Device referenceDevice;
alshabib8a4a6002015-11-25 14:31:16 -0800335 DeviceEvent event;
Aaron Kruglikove1200592015-06-29 16:31:23 -0700336 int numDevices = 10;
337 int numInfraPorts = 5;
338 totalPorts = 10;
339 defaultPopulator(numDevices, numInfraPorts);
340 for (byte byteIndex = 0; byteIndex < arr.length; byteIndex++) {
341 arr[byteIndex] = byteIndex;
342 }
343 for (int i = 0; i < numDevices; i++) {
344 referenceDevice = NetTestTools.device(Integer.toString(i));
alshabib8a4a6002015-11-25 14:31:16 -0800345 testDeviceManager.listener.event(new DeviceEvent(DEVICE_ADDED, referenceDevice,
346 new DefaultPort(referenceDevice,
347 PortNumber.portNumber(1),
348 true)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700349 }
Aaron Kruglikove1200592015-06-29 16:31:23 -0700350
351 mgr.emitPacket(ByteBuffer.wrap(arr), Optional.<TrafficTreatment>empty());
352
353 assertEquals("There were an unexpected number of emitted packets",
alshabib8a4a6002015-11-25 14:31:16 -0800354 (totalPorts - numInfraPorts) * numDevices, packets.size());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700355 Iterator<OutboundPacket> packetIter = packets.iterator();
356 OutboundPacket packet;
357 while (packetIter.hasNext()) {
358 packet = packetIter.next();
359 assertEquals("The packet had an incorrect payload.", arr, packet.data().array());
360 }
361 //Start testing emission to a specific device
362 packets.clear();
363 mgr.emitPacket(NetTestTools.did(Integer.toString(1)), ByteBuffer.wrap(arr), Optional.<TrafficTreatment>empty());
364
365 assertEquals("Unexpected number of outbound packets were emitted.",
alshabib8a4a6002015-11-25 14:31:16 -0800366 totalPorts - numInfraPorts, packets.size());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700367 packetIter = packets.iterator();
368 while (packetIter.hasNext()) {
369 packet = packetIter.next();
370 assertEquals("The packet had an incorrect payload", arr, packet.data().array());
371 }
372 }
373
374
375 /**
376 * @param numDevices the number of devices to populate.
377 * @param numInfraPorts the number of ports to be set as infrastructure on each device, numbered base 0, ports 0
378 * through numInfraPorts - 1
379 */
380 private void defaultPopulator(int numDevices, int numInfraPorts) {
381 for (int device = 0; device < numDevices; device++) {
382 String str = Integer.toString(device);
383 Device deviceToAdd = NetTestTools.device(str);
384 devices.put(deviceToAdd.id(), deviceToAdd);
385 for (int port = 1; port <= numInfraPorts; port++) {
386 infrastructurePorts.add(NetTestTools.connectPoint(str, port));
387 }
388 }
389 }
390
391 /**
392 * Adds the specified device with the specified number of edge ports so long as it is less than the total ports.
393 *
394 * @param device The device to be added
395 * @param deviceName The name given to generate the devices DID
396 * @param numInfraPorts The number of ports to be added numbered 1 ... numInfraPorts
397 */
398 private void addDevice(Device device, String deviceName, int numInfraPorts) {
399 if (!devices.keySet().contains(device.id())) {
400 devices.put(device.id(), device);
401 for (int i = 1; i <= numInfraPorts && i <= totalPorts; i++) {
402 infrastructurePorts.add(NetTestTools.connectPoint(deviceName, i));
403 }
404 }
405 }
406
407 private void removeDevice(Device device) {
408 devices.remove(device.id());
409 }
410
411 private void removeInfraPort(ConnectPoint port) {
412 infrastructurePorts.remove(port);
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700413 }
414
415 private class TestTopologyManager extends TopologyServiceAdapter {
416 private TopologyListener listener;
417 private Set<ConnectPoint> infrastructurePorts;
418
Aaron Kruglikove1200592015-06-29 16:31:23 -0700419 public TestTopologyManager(Set<ConnectPoint> infrastructurePorts) {
420 this.infrastructurePorts = infrastructurePorts;
421 }
422
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700423 @Override
424 public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) {
425 return infrastructurePorts.contains(connectPoint);
426 }
427
428 @Override
429 public void addListener(TopologyListener listener) {
430 this.listener = listener;
431 }
432
433 @Override
434 public void removeListener(TopologyListener listener) {
435 this.listener = null;
436 }
437 }
438
439 private class TestDeviceManager extends DeviceServiceAdapter {
alshabib8a4a6002015-11-25 14:31:16 -0800440 private DeviceListener listener;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700441
Aaron Kruglikove1200592015-06-29 16:31:23 -0700442 private Map<DeviceId, Device> devices;
443
444 public TestDeviceManager(Map<DeviceId, Device> devices) {
445 this.devices = devices;
446 }
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700447
448 @Override
449 public boolean isAvailable(DeviceId deviceId) {
Aaron Kruglikove1200592015-06-29 16:31:23 -0700450 for (DeviceId id : devices.keySet()) {
451 if (id.equals(deviceId)) {
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700452 return true;
453 }
454 }
455 return false;
456 }
457
458 @Override
459 public List<Port> getPorts(DeviceId deviceId) {
Aaron Kruglikove1200592015-06-29 16:31:23 -0700460 List<Port> ports = new ArrayList<>();
461 Device device = devices.get(deviceId);
462 if (device == null && !alwaysReturnPorts) {
463 return ports;
464 }
465 for (int portNum = 1; portNum <= totalPorts; portNum++) {
466 //String is generated using 'of:' + the passed name, this creates a
467 ports.add(new DefaultPort(device, PortNumber.portNumber(portNum), true));
468 }
469 return ports;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700470 }
471
472 @Override
473 public Iterable<Device> getAvailableDevices() {
Aaron Kruglikove1200592015-06-29 16:31:23 -0700474 return devices.values();
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700475 }
alshabib8a4a6002015-11-25 14:31:16 -0800476
477
478 @Override
479 public void addListener(DeviceListener listener) {
480 this.listener = listener;
481 }
482
483 @Override
484 public void removeListener(DeviceListener listener) {
485 this.listener = null;
486 }
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700487 }
488
489 private class TestPacketManager extends PacketServiceAdapter {
Aaron Kruglikove1200592015-06-29 16:31:23 -0700490 @Override
491 public void emit(OutboundPacket packet) {
492 packets.add(packet);
493 }
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700494 }
495
alshabib8a4a6002015-11-25 14:31:16 -0800496 private class TestLinkManager extends LinkServiceAdapter {
497 private LinkListener listener;
498
499 @Override
500 public void addListener(LinkListener listener) {
501 this.listener = listener;
502 }
503 }
504
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700505 private class TestListener implements EdgePortListener {
506 private List<EdgePortEvent> events;
507
Aaron Kruglikove1200592015-06-29 16:31:23 -0700508 public TestListener(List<EdgePortEvent> events) {
509 this.events = events;
510 }
511
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700512 @Override
513 public void event(EdgePortEvent event) {
514 events.add(event);
515 }
516 }
517}