blob: 67c82e19d107c7d0fcac737fe056b31ff8c12dd4 [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
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -070018import com.google.common.collect.ImmutableList;
Aaron Kruglikove1200592015-06-29 16:31:23 -070019import com.google.common.collect.Lists;
20import com.google.common.collect.Maps;
21import com.google.common.collect.Sets;
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -070022
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070023import org.junit.After;
24import org.junit.Before;
25import org.junit.Test;
26import org.onosproject.common.event.impl.TestEventDispatcher;
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -070027import org.onosproject.event.Event;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070028import org.onosproject.net.ConnectPoint;
Aaron Kruglikove1200592015-06-29 16:31:23 -070029import org.onosproject.net.DefaultPort;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070030import org.onosproject.net.Device;
31import org.onosproject.net.DeviceId;
Aaron Kruglikove1200592015-06-29 16:31:23 -070032import org.onosproject.net.NetTestTools;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070033import org.onosproject.net.Port;
Aaron Kruglikove1200592015-06-29 16:31:23 -070034import org.onosproject.net.PortNumber;
35import org.onosproject.net.device.DeviceEvent;
alshabib8a4a6002015-11-25 14:31:16 -080036import org.onosproject.net.device.DeviceListener;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070037import org.onosproject.net.device.DeviceServiceAdapter;
38import org.onosproject.net.edge.EdgePortEvent;
39import org.onosproject.net.edge.EdgePortListener;
Aaron Kruglikove1200592015-06-29 16:31:23 -070040import org.onosproject.net.link.LinkEvent;
41import org.onosproject.net.packet.OutboundPacket;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070042import org.onosproject.net.packet.PacketServiceAdapter;
43import org.onosproject.net.topology.Topology;
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -070044import org.onosproject.net.topology.TopologyEvent;
45import org.onosproject.net.topology.TopologyEvent.Type;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070046import org.onosproject.net.topology.TopologyListener;
47import org.onosproject.net.topology.TopologyServiceAdapter;
48
Aaron Kruglikove1200592015-06-29 16:31:23 -070049import java.nio.ByteBuffer;
50import java.util.ArrayList;
51import java.util.Iterator;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070052import java.util.List;
Aaron Kruglikove1200592015-06-29 16:31:23 -070053import java.util.Map;
54import java.util.Optional;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070055import java.util.Set;
56
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -070057import static org.hamcrest.Matchers.*;
Aaron Kruglikove1200592015-06-29 16:31:23 -070058import static org.junit.Assert.*;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070059import static org.onosproject.net.NetTestTools.injectEventDispatcher;
Aaron Kruglikove1200592015-06-29 16:31:23 -070060import static org.onosproject.net.device.DeviceEvent.Type.*;
61import static org.onosproject.net.edge.EdgePortEvent.Type.EDGE_PORT_ADDED;
62import static org.onosproject.net.edge.EdgePortEvent.Type.EDGE_PORT_REMOVED;
63import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED;
64import static org.onosproject.net.link.LinkEvent.Type.LINK_REMOVED;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070065
66/**
Aaron Kruglikove1200592015-06-29 16:31:23 -070067 * Test of the edge port manager. Each device has ports '0' through 'numPorts - 1'
68 * as specified by the variable 'numPorts'.
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070069 */
70public class EdgeManagerTest {
71
72 private EdgeManager mgr;
Aaron Kruglikove1200592015-06-29 16:31:23 -070073 private int totalPorts = 10;
74 private boolean alwaysReturnPorts = false;
75 private final Set<ConnectPoint> infrastructurePorts = Sets.newConcurrentHashSet();
76 private List<EdgePortEvent> events = Lists.newArrayList();
77 private final Map<DeviceId, Device> devices = Maps.newConcurrentMap();
78 private Set<OutboundPacket> packets = Sets.newConcurrentHashSet();
79 private final EdgePortListener testListener = new TestListener(events);
alshabib8a4a6002015-11-25 14:31:16 -080080 private TestDeviceManager testDeviceManager;
Aaron Kruglikove1200592015-06-29 16:31:23 -070081 private TestTopologyManager testTopologyManager;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070082
83 @Before
84 public void setUp() {
85 mgr = new EdgeManager();
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070086 injectEventDispatcher(mgr, new TestEventDispatcher());
Aaron Kruglikove1200592015-06-29 16:31:23 -070087 testTopologyManager = new TestTopologyManager(infrastructurePorts);
88 mgr.topologyService = testTopologyManager;
alshabib8a4a6002015-11-25 14:31:16 -080089 testDeviceManager = new TestDeviceManager(devices);
90 mgr.deviceService = testDeviceManager;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070091 mgr.packetService = new TestPacketManager();
92 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
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700129 postTopologyEvent(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
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700150 postTopologyEvent(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();
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700155 postTopologyEvent(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();
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700179 postTopologyEvent(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
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700183
Aaron Kruglikove1200592015-06-29 16:31:23 -0700184 @Test
185 public void testDeviceUpdates() {
186 //Setup
187
188 Device referenceDevice;
alshabib8a4a6002015-11-25 14:31:16 -0800189 DeviceEvent event;
Aaron Kruglikove1200592015-06-29 16:31:23 -0700190 int numDevices = 10;
191 int numInfraPorts = 5;
192 totalPorts = 10;
193 defaultPopulator(numDevices, numInfraPorts);
194
195 //Test response to device added events
196 referenceDevice = NetTestTools.device("1");
alshabib8a4a6002015-11-25 14:31:16 -0800197 event = new DeviceEvent(DEVICE_ADDED, referenceDevice,
198 new DefaultPort(referenceDevice, PortNumber.portNumber(1), true));
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700199 postTopologyEvent(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700200
201 //Check that ports were populated correctly
202 assertTrue("Unexpected number of new ports added",
alshabib8a4a6002015-11-25 14:31:16 -0800203 mgr.deviceService.getPorts(NetTestTools.did("1")).size() == 10);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700204
205 //Check that of the ten ports the half that are infrastructure ports aren't added
206 assertEquals("Unexpected number of new edge ports added", (totalPorts - numInfraPorts), events.size());
207
208 for (int index = 0; index < numInfraPorts; index++) {
209 assertTrue("Unexpected type of event", events.get(index).type() == EDGE_PORT_ADDED);
210 }
211 //Names here are irrelevant, the first 5 ports are populated as infrastructure, 6-10 are edge
212 for (int index = 0; index < events.size(); index++) {
213 assertEquals("Port added had unexpected port number.",
alshabib8a4a6002015-11-25 14:31:16 -0800214 events.get(index).subject().port(),
215 NetTestTools.connectPoint("a", index + numInfraPorts + 1).port());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700216 }
217 events.clear();
218
219 //Repost the event to test repeated posts
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700220 postTopologyEvent(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700221 assertEquals("The redundant notification should not have created additional notifications.",
alshabib8a4a6002015-11-25 14:31:16 -0800222 0, events.size());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700223 //Calculate the size of the returned iterable of edge points.
224 Iterable<ConnectPoint> pts = mgr.getEdgePoints();
225 Iterator pointIterator = pts.iterator();
226 int count = 0;
227 for (; pointIterator.hasNext(); count++) {
228 pointIterator.next();
229 }
230 assertEquals("Unexpected number of edge points", totalPorts - numInfraPorts, count);
231 //Testing device removal
232 events.clear();
alshabib8a4a6002015-11-25 14:31:16 -0800233 event = (new DeviceEvent(DEVICE_REMOVED, referenceDevice,
234 new DefaultPort(referenceDevice, PortNumber.portNumber(1), true)));
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700235 postTopologyEvent(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700236
237 assertEquals("There should be five new events from removal of edge points",
alshabib8a4a6002015-11-25 14:31:16 -0800238 totalPorts - numInfraPorts, events.size());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700239 for (int index = 0; index < events.size(); index++) {
240 //Assert that the correct port numbers were removed in the correct order
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700241 assertThat("Port removed had unexpected port number.",
242 events.get(index).subject().port().toLong(),
243 is(greaterThanOrEqualTo((long) numInfraPorts)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700244 //Assert that the events are of the correct type
245 assertEquals("Unexpected type of event", events.get(index).type(), EDGE_PORT_REMOVED);
246 }
247 events.clear();
248 //Rebroadcast event to check that it triggers no new behavior
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700249 postTopologyEvent(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700250 assertEquals("Rebroadcast of removal event should not produce additional events",
alshabib8a4a6002015-11-25 14:31:16 -0800251 0, events.size());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700252
253 //Testing device status change, changed from unavailable to available
254 events.clear();
Aaron Kruglikove1200592015-06-29 16:31:23 -0700255 //Make sure that the devicemanager shows the device as available.
256 addDevice(referenceDevice, "1", 5);
257 devices.put(referenceDevice.id(), referenceDevice);
258
alshabib8a4a6002015-11-25 14:31:16 -0800259 event = new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, referenceDevice);
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700260 postTopologyEvent(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700261 //An earlier setup set half of the reference device ports to infrastructure
262 assertEquals("An unexpected number of events were generated.", totalPorts - numInfraPorts, events.size());
263 for (int i = 0; i < 5; i++) {
264 assertEquals("The event was not of the right type", events.get(i).type(), EDGE_PORT_ADDED);
265 }
266 events.clear();
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700267 postTopologyEvent(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700268 assertEquals("No events should have been generated for a set of existing ports.", 0, events.size());
269
270 //Test removal when state changes when the device becomes unavailable
271
272 //Ensure that the deviceManager shows the device as unavailable
273 removeDevice(referenceDevice);
274 /*This variable copies the behavior of the topology by returning ports attached to an unavailable device
275 //this behavior is necessary for the following event to execute properly, if these statements are removed
276 no events will be generated since no ports will be provided in getPorts() to EdgeManager.
277 */
278 alwaysReturnPorts = true;
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700279 postTopologyEvent(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700280 alwaysReturnPorts = false;
281 assertEquals("An unexpected number of events were created.", totalPorts - numInfraPorts, events.size());
282 for (int i = 0; i < 5; i++) {
283 EdgePortEvent edgeEvent = events.get(i);
284 assertEquals("The event is of an unexpected type.",
alshabib8a4a6002015-11-25 14:31:16 -0800285 EdgePortEvent.Type.EDGE_PORT_REMOVED, edgeEvent.type());
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700286 assertThat("The event pertains to an unexpected port",
287 edgeEvent.subject().port().toLong(),
288 is(greaterThanOrEqualTo((long) numInfraPorts)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700289 }
290 }
291
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700292
Aaron Kruglikove1200592015-06-29 16:31:23 -0700293 @Test
294 public void testInternalCache() {
Aaron Kruglikove1200592015-06-29 16:31:23 -0700295 int numDevices = 10;
296 //Number of infrastructure ports per device
297 int numPorts = 5;
298 //Total ports per device when requesting all devices
299 totalPorts = 10;
300 defaultPopulator(numDevices, numPorts);
301 for (int i = 0; i < numDevices; i++) {
302 Device newDevice = NetTestTools.device(Integer.toString(i));
303 devices.put(newDevice.id(), newDevice);
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700304 postTopologyEvent(new DeviceEvent(DEVICE_ADDED, newDevice));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700305 }
Aaron Kruglikove1200592015-06-29 16:31:23 -0700306 //Check all ports have correct designations
307 ConnectPoint testPoint;
308 for (int deviceNum = 0; deviceNum < numDevices; deviceNum++) {
309 for (int portNum = 1; portNum <= totalPorts; portNum++) {
310 testPoint = NetTestTools.connectPoint(Integer.toString(deviceNum), portNum);
311 if (portNum <= numPorts) {
312 assertFalse("This should not be an edge point", mgr.isEdgePoint(testPoint));
313 } else {
314 assertTrue("This should be an edge point", mgr.isEdgePoint(testPoint));
315 }
316 }
317 }
318 int count = 0;
319 for (ConnectPoint ignored : mgr.getEdgePoints()) {
320 count++;
321 }
322 assertEquals("There are an unexpeceted number of edge points returned.",
alshabib8a4a6002015-11-25 14:31:16 -0800323 (totalPorts - numPorts) * numDevices, count);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700324 for (int deviceNumber = 0; deviceNumber < numDevices; deviceNumber++) {
325 count = 0;
326 for (ConnectPoint ignored : mgr.getEdgePoints(NetTestTools.did("1"))) {
327 count++;
328 }
329 assertEquals("This element has an unexpected number of edge points.", (totalPorts - numPorts), count);
330 }
331 }
332
333
334 @Test
335 public void testEmit() {
336 byte[] arr = new byte[10];
337 Device referenceDevice;
alshabib8a4a6002015-11-25 14:31:16 -0800338 DeviceEvent event;
Aaron Kruglikove1200592015-06-29 16:31:23 -0700339 int numDevices = 10;
340 int numInfraPorts = 5;
341 totalPorts = 10;
342 defaultPopulator(numDevices, numInfraPorts);
343 for (byte byteIndex = 0; byteIndex < arr.length; byteIndex++) {
344 arr[byteIndex] = byteIndex;
345 }
346 for (int i = 0; i < numDevices; i++) {
347 referenceDevice = NetTestTools.device(Integer.toString(i));
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700348 postTopologyEvent(new DeviceEvent(DEVICE_ADDED, referenceDevice,
alshabib8a4a6002015-11-25 14:31:16 -0800349 new DefaultPort(referenceDevice,
350 PortNumber.portNumber(1),
351 true)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700352 }
Aaron Kruglikove1200592015-06-29 16:31:23 -0700353
Sho SHIMIZU21d00692016-08-15 11:15:28 -0700354 mgr.emitPacket(ByteBuffer.wrap(arr), Optional.empty());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700355
356 assertEquals("There were an unexpected number of emitted packets",
alshabib8a4a6002015-11-25 14:31:16 -0800357 (totalPorts - numInfraPorts) * numDevices, packets.size());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700358 Iterator<OutboundPacket> packetIter = packets.iterator();
359 OutboundPacket packet;
360 while (packetIter.hasNext()) {
361 packet = packetIter.next();
362 assertEquals("The packet had an incorrect payload.", arr, packet.data().array());
363 }
364 //Start testing emission to a specific device
365 packets.clear();
Sho SHIMIZU21d00692016-08-15 11:15:28 -0700366 mgr.emitPacket(NetTestTools.did(Integer.toString(1)), ByteBuffer.wrap(arr), Optional.empty());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700367
368 assertEquals("Unexpected number of outbound packets were emitted.",
alshabib8a4a6002015-11-25 14:31:16 -0800369 totalPorts - numInfraPorts, packets.size());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700370 packetIter = packets.iterator();
371 while (packetIter.hasNext()) {
372 packet = packetIter.next();
373 assertEquals("The packet had an incorrect payload", arr, packet.data().array());
374 }
375 }
376
377
378 /**
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700379 * Creates TopologyEvent triggered by {@code event}.
380 *
381 * @param event reason of the TopologyEvent
382 * @return TopologyEvent
383 */
384 private TopologyEvent topologyEventOf(Event event) {
385 return new TopologyEvent(Type.TOPOLOGY_CHANGED, null, ImmutableList.of(event));
386 }
387
388
389 /**
390 * Post Event dispatched from TopologyManager.
391 *
392 * @param event Event
393 */
394 private void postTopologyEvent(Event event) {
395 testTopologyManager.listener.event(topologyEventOf(event));
396 }
397
398
399 /**
Aaron Kruglikove1200592015-06-29 16:31:23 -0700400 * @param numDevices the number of devices to populate.
401 * @param numInfraPorts the number of ports to be set as infrastructure on each device, numbered base 0, ports 0
402 * through numInfraPorts - 1
403 */
404 private void defaultPopulator(int numDevices, int numInfraPorts) {
405 for (int device = 0; device < numDevices; device++) {
406 String str = Integer.toString(device);
407 Device deviceToAdd = NetTestTools.device(str);
408 devices.put(deviceToAdd.id(), deviceToAdd);
409 for (int port = 1; port <= numInfraPorts; port++) {
410 infrastructurePorts.add(NetTestTools.connectPoint(str, port));
411 }
412 }
413 }
414
415 /**
416 * Adds the specified device with the specified number of edge ports so long as it is less than the total ports.
417 *
418 * @param device The device to be added
419 * @param deviceName The name given to generate the devices DID
420 * @param numInfraPorts The number of ports to be added numbered 1 ... numInfraPorts
421 */
422 private void addDevice(Device device, String deviceName, int numInfraPorts) {
423 if (!devices.keySet().contains(device.id())) {
424 devices.put(device.id(), device);
425 for (int i = 1; i <= numInfraPorts && i <= totalPorts; i++) {
426 infrastructurePorts.add(NetTestTools.connectPoint(deviceName, i));
427 }
428 }
429 }
430
431 private void removeDevice(Device device) {
432 devices.remove(device.id());
433 }
434
435 private void removeInfraPort(ConnectPoint port) {
436 infrastructurePorts.remove(port);
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700437 }
438
439 private class TestTopologyManager extends TopologyServiceAdapter {
440 private TopologyListener listener;
441 private Set<ConnectPoint> infrastructurePorts;
442
Aaron Kruglikove1200592015-06-29 16:31:23 -0700443 public TestTopologyManager(Set<ConnectPoint> infrastructurePorts) {
444 this.infrastructurePorts = infrastructurePorts;
445 }
446
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700447 @Override
448 public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) {
449 return infrastructurePorts.contains(connectPoint);
450 }
451
452 @Override
453 public void addListener(TopologyListener listener) {
454 this.listener = listener;
455 }
456
457 @Override
458 public void removeListener(TopologyListener listener) {
459 this.listener = null;
460 }
461 }
462
463 private class TestDeviceManager extends DeviceServiceAdapter {
alshabib8a4a6002015-11-25 14:31:16 -0800464 private DeviceListener listener;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700465
Aaron Kruglikove1200592015-06-29 16:31:23 -0700466 private Map<DeviceId, Device> devices;
467
468 public TestDeviceManager(Map<DeviceId, Device> devices) {
469 this.devices = devices;
470 }
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700471
472 @Override
473 public boolean isAvailable(DeviceId deviceId) {
Aaron Kruglikove1200592015-06-29 16:31:23 -0700474 for (DeviceId id : devices.keySet()) {
475 if (id.equals(deviceId)) {
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700476 return true;
477 }
478 }
479 return false;
480 }
481
482 @Override
483 public List<Port> getPorts(DeviceId deviceId) {
Aaron Kruglikove1200592015-06-29 16:31:23 -0700484 List<Port> ports = new ArrayList<>();
485 Device device = devices.get(deviceId);
486 if (device == null && !alwaysReturnPorts) {
487 return ports;
488 }
489 for (int portNum = 1; portNum <= totalPorts; portNum++) {
490 //String is generated using 'of:' + the passed name, this creates a
491 ports.add(new DefaultPort(device, PortNumber.portNumber(portNum), true));
492 }
493 return ports;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700494 }
495
496 @Override
497 public Iterable<Device> getAvailableDevices() {
Aaron Kruglikove1200592015-06-29 16:31:23 -0700498 return devices.values();
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700499 }
alshabib8a4a6002015-11-25 14:31:16 -0800500
501
502 @Override
503 public void addListener(DeviceListener listener) {
504 this.listener = listener;
505 }
506
507 @Override
508 public void removeListener(DeviceListener listener) {
509 this.listener = null;
510 }
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700511 }
512
513 private class TestPacketManager extends PacketServiceAdapter {
Aaron Kruglikove1200592015-06-29 16:31:23 -0700514 @Override
515 public void emit(OutboundPacket packet) {
516 packets.add(packet);
517 }
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700518 }
519
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700520 private class TestListener implements EdgePortListener {
521 private List<EdgePortEvent> events;
522
Aaron Kruglikove1200592015-06-29 16:31:23 -0700523 public TestListener(List<EdgePortEvent> events) {
524 this.events = events;
525 }
526
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700527 @Override
528 public void event(EdgePortEvent event) {
529 events.add(event);
530 }
531 }
Sho SHIMIZU21d00692016-08-15 11:15:28 -0700532}