blob: 971ce4b7575d784deb0d84c975eef00437a63d8f [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;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070022import org.junit.After;
23import org.junit.Before;
24import org.junit.Test;
25import org.onosproject.common.event.impl.TestEventDispatcher;
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -070026import org.onosproject.event.Event;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070027import org.onosproject.net.ConnectPoint;
Aaron Kruglikove1200592015-06-29 16:31:23 -070028import org.onosproject.net.DefaultPort;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070029import org.onosproject.net.Device;
30import org.onosproject.net.DeviceId;
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070031import org.onosproject.net.Link;
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;
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070041import org.onosproject.net.link.LinkListener;
42import org.onosproject.net.link.LinkServiceAdapter;
Aaron Kruglikove1200592015-06-29 16:31:23 -070043import org.onosproject.net.packet.OutboundPacket;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070044import org.onosproject.net.packet.PacketServiceAdapter;
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -070045import org.onosproject.net.topology.TopologyEvent;
46import org.onosproject.net.topology.TopologyEvent.Type;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070047
Aaron Kruglikove1200592015-06-29 16:31:23 -070048import java.nio.ByteBuffer;
49import java.util.ArrayList;
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070050import java.util.Collections;
Aaron Kruglikove1200592015-06-29 16:31:23 -070051import 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
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070057import static org.hamcrest.Matchers.greaterThanOrEqualTo;
58import static org.hamcrest.Matchers.is;
59import static org.junit.Assert.assertEquals;
60import static org.junit.Assert.assertFalse;
61import static org.junit.Assert.assertThat;
62import static org.junit.Assert.assertTrue;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070063import static org.onosproject.net.NetTestTools.injectEventDispatcher;
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070064import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED;
65import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED;
66import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_REMOVED;
Aaron Kruglikove1200592015-06-29 16:31:23 -070067import static org.onosproject.net.edge.EdgePortEvent.Type.EDGE_PORT_ADDED;
68import static org.onosproject.net.edge.EdgePortEvent.Type.EDGE_PORT_REMOVED;
69import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED;
70import static org.onosproject.net.link.LinkEvent.Type.LINK_REMOVED;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070071
72/**
Aaron Kruglikove1200592015-06-29 16:31:23 -070073 * Test of the edge port manager. Each device has ports '0' through 'numPorts - 1'
74 * as specified by the variable 'numPorts'.
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070075 */
76public class EdgeManagerTest {
77
78 private EdgeManager mgr;
Aaron Kruglikove1200592015-06-29 16:31:23 -070079 private int totalPorts = 10;
80 private boolean alwaysReturnPorts = false;
81 private final Set<ConnectPoint> infrastructurePorts = Sets.newConcurrentHashSet();
82 private List<EdgePortEvent> events = Lists.newArrayList();
83 private final Map<DeviceId, Device> devices = Maps.newConcurrentMap();
84 private Set<OutboundPacket> packets = Sets.newConcurrentHashSet();
85 private final EdgePortListener testListener = new TestListener(events);
alshabib8a4a6002015-11-25 14:31:16 -080086 private TestDeviceManager testDeviceManager;
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070087 private TestLinkService testLinkService;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070088
89 @Before
90 public void setUp() {
91 mgr = new EdgeManager();
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070092 injectEventDispatcher(mgr, new TestEventDispatcher());
alshabib8a4a6002015-11-25 14:31:16 -080093 testDeviceManager = new TestDeviceManager(devices);
94 mgr.deviceService = testDeviceManager;
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -070095
96 testLinkService = new TestLinkService();
97 mgr.linkService = testLinkService;
98
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070099 mgr.packetService = new TestPacketManager();
100 mgr.activate();
101 mgr.addListener(testListener);
102 }
103
104 @After
105 public void tearDown() {
106 mgr.removeListener(testListener);
107 mgr.deactivate();
108 }
109
110 @Test
Aaron Kruglikove1200592015-06-29 16:31:23 -0700111 public void testBasics() {
112 //Setup
113 int numDevices = 20;
114 int numPorts = 4;
115 defaultPopulator(numDevices, numPorts);
116
117 assertEquals("Unexpected number of ports", numDevices * numPorts, infrastructurePorts.size());
118
Aaron Kruglikove1200592015-06-29 16:31:23 -0700119 assertFalse("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 removeInfraPort(NetTestTools.connectPoint(Integer.toString(1), 1));
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700123 postTopologyEvent(new LinkEvent(LINK_REMOVED, NetTestTools.link(Integer.toString(1), 1, "b", 2)));
124 assertTrue("Expected isEdge to return true",
alshabib8a4a6002015-11-25 14:31:16 -0800125 mgr.isEdgePoint(NetTestTools.connectPoint(Integer.toString(1), 1)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700126 }
127
128 @Test
129 public void testLinkUpdates() {
130 //Setup
131 ConnectPoint testPoint, referencePoint;
132
133 //Testing link removal
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700134 postTopologyEvent(new LinkEvent(LINK_REMOVED, NetTestTools.link("a", 1, "b", 2)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700135
136 assertTrue("The list contained an unexpected number of events", events.size() == 2);
137 assertTrue("The first element is of the wrong type.",
alshabib8a4a6002015-11-25 14:31:16 -0800138 events.get(0).type() == EDGE_PORT_ADDED);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700139
140 testPoint = events.get(0).subject();
141 referencePoint = NetTestTools.connectPoint("a", 1);
142 assertTrue("The port numbers of the first element are incorrect",
alshabib8a4a6002015-11-25 14:31:16 -0800143 testPoint.port().toLong() == referencePoint.port().toLong());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700144 assertTrue("The device id of the first element is incorrect.",
alshabib8a4a6002015-11-25 14:31:16 -0800145 testPoint.deviceId().equals(referencePoint.deviceId()));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700146
147 testPoint = events.get(1).subject();
148 referencePoint = NetTestTools.connectPoint("b", 2);
149 assertTrue("The port numbers of the second element are incorrect",
alshabib8a4a6002015-11-25 14:31:16 -0800150 testPoint.port().toLong() == referencePoint.port().toLong());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700151 assertTrue("The device id of the second element is incorrect.",
alshabib8a4a6002015-11-25 14:31:16 -0800152 testPoint.deviceId().equals(referencePoint.deviceId()));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700153
154 //Rebroadcast event to ensure it results in no additional events
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700155 postTopologyEvent(new LinkEvent(LINK_REMOVED, NetTestTools.link("a", 1, "b", 2)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700156 assertTrue("The list contained an unexpected number of events", events.size() == 2);
157
158 //Testing link adding when links to remove exist
Aaron Kruglikove1200592015-06-29 16:31:23 -0700159 events.clear();
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700160 postTopologyEvent(new LinkEvent(LINK_ADDED, NetTestTools.link("a", 1, "b", 2)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700161
162 assertTrue("The list contained an unexpected number of events", events.size() == 2);
163 assertTrue("The first element is of the wrong type.",
alshabib8a4a6002015-11-25 14:31:16 -0800164 events.get(0).type() == EDGE_PORT_REMOVED);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700165 assertTrue("The second element is of the wrong type.",
alshabib8a4a6002015-11-25 14:31:16 -0800166 events.get(1).type() == EDGE_PORT_REMOVED);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700167
168 testPoint = events.get(0).subject();
169 referencePoint = NetTestTools.connectPoint("a", 1);
170 assertTrue("The port numbers of the first element are incorrect",
alshabib8a4a6002015-11-25 14:31:16 -0800171 testPoint.port().toLong() == referencePoint.port().toLong());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700172 assertTrue("The device id of the first element is incorrect.",
alshabib8a4a6002015-11-25 14:31:16 -0800173 testPoint.deviceId().equals(referencePoint.deviceId()));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700174
175 testPoint = events.get(1).subject();
176 referencePoint = NetTestTools.connectPoint("b", 2);
177 assertTrue("The port numbers of the second element are incorrect",
alshabib8a4a6002015-11-25 14:31:16 -0800178 testPoint.port().toLong() == referencePoint.port().toLong());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700179 assertTrue("The device id of the second element is incorrect.",
alshabib8a4a6002015-11-25 14:31:16 -0800180 testPoint.deviceId().equals(referencePoint.deviceId()));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700181
182 //Apparent duplicate of previous method tests removal when the elements have already been removed
Aaron Kruglikove1200592015-06-29 16:31:23 -0700183 events.clear();
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700184 postTopologyEvent(new LinkEvent(LINK_ADDED, NetTestTools.link("a", 1, "b", 2)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700185 assertTrue("The list should contain no events, the removed elements don't exist.", events.size() == 0);
186 }
187
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700188
Aaron Kruglikove1200592015-06-29 16:31:23 -0700189 @Test
190 public void testDeviceUpdates() {
191 //Setup
192
193 Device referenceDevice;
alshabib8a4a6002015-11-25 14:31:16 -0800194 DeviceEvent event;
Aaron Kruglikove1200592015-06-29 16:31:23 -0700195 int numDevices = 10;
196 int numInfraPorts = 5;
197 totalPorts = 10;
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700198
Aaron Kruglikove1200592015-06-29 16:31:23 -0700199 defaultPopulator(numDevices, numInfraPorts);
200
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700201 events.clear();
202
Aaron Kruglikove1200592015-06-29 16:31:23 -0700203 //Test response to device added events
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700204
205 referenceDevice = NetTestTools.device("11");
206 devices.put(referenceDevice.id(), referenceDevice);
207 for (int port = 1; port <= numInfraPorts; port++) {
208 infrastructurePorts.add(NetTestTools.connectPoint("11", port));
209 }
alshabib8a4a6002015-11-25 14:31:16 -0800210 event = new DeviceEvent(DEVICE_ADDED, referenceDevice,
211 new DefaultPort(referenceDevice, PortNumber.portNumber(1), true));
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700212 postTopologyEvent(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700213
214 //Check that ports were populated correctly
215 assertTrue("Unexpected number of new ports added",
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700216 mgr.deviceService.getPorts(NetTestTools.did("11")).size() == 10);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700217
218 //Check that of the ten ports the half that are infrastructure ports aren't added
219 assertEquals("Unexpected number of new edge ports added", (totalPorts - numInfraPorts), events.size());
220
221 for (int index = 0; index < numInfraPorts; index++) {
222 assertTrue("Unexpected type of event", events.get(index).type() == EDGE_PORT_ADDED);
223 }
224 //Names here are irrelevant, the first 5 ports are populated as infrastructure, 6-10 are edge
225 for (int index = 0; index < events.size(); index++) {
226 assertEquals("Port added had unexpected port number.",
alshabib8a4a6002015-11-25 14:31:16 -0800227 events.get(index).subject().port(),
228 NetTestTools.connectPoint("a", index + numInfraPorts + 1).port());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700229 }
230 events.clear();
231
232 //Repost the event to test repeated posts
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700233 postTopologyEvent(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700234 assertEquals("The redundant notification should not have created additional notifications.",
alshabib8a4a6002015-11-25 14:31:16 -0800235 0, events.size());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700236 //Calculate the size of the returned iterable of edge points.
237 Iterable<ConnectPoint> pts = mgr.getEdgePoints();
238 Iterator pointIterator = pts.iterator();
239 int count = 0;
240 for (; pointIterator.hasNext(); count++) {
241 pointIterator.next();
242 }
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700243 assertEquals("Unexpected number of edge points", (numDevices + 1) * numInfraPorts, count);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700244 //Testing device removal
245 events.clear();
alshabib8a4a6002015-11-25 14:31:16 -0800246 event = (new DeviceEvent(DEVICE_REMOVED, referenceDevice,
247 new DefaultPort(referenceDevice, PortNumber.portNumber(1), true)));
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700248 postTopologyEvent(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700249
250 assertEquals("There should be five new events from removal of edge points",
alshabib8a4a6002015-11-25 14:31:16 -0800251 totalPorts - numInfraPorts, events.size());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700252 for (int index = 0; index < events.size(); index++) {
253 //Assert that the correct port numbers were removed in the correct order
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700254 assertThat("Port removed had unexpected port number.",
255 events.get(index).subject().port().toLong(),
256 is(greaterThanOrEqualTo((long) numInfraPorts)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700257 //Assert that the events are of the correct type
258 assertEquals("Unexpected type of event", events.get(index).type(), EDGE_PORT_REMOVED);
259 }
260 events.clear();
261 //Rebroadcast event to check that it triggers no new behavior
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700262 postTopologyEvent(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700263 assertEquals("Rebroadcast of removal event should not produce additional events",
alshabib8a4a6002015-11-25 14:31:16 -0800264 0, events.size());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700265
266 //Testing device status change, changed from unavailable to available
267 events.clear();
Aaron Kruglikove1200592015-06-29 16:31:23 -0700268 //Make sure that the devicemanager shows the device as available.
269 addDevice(referenceDevice, "1", 5);
270 devices.put(referenceDevice.id(), referenceDevice);
271
alshabib8a4a6002015-11-25 14:31:16 -0800272 event = new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, referenceDevice);
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700273 postTopologyEvent(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700274 //An earlier setup set half of the reference device ports to infrastructure
275 assertEquals("An unexpected number of events were generated.", totalPorts - numInfraPorts, events.size());
276 for (int i = 0; i < 5; i++) {
277 assertEquals("The event was not of the right type", events.get(i).type(), EDGE_PORT_ADDED);
278 }
279 events.clear();
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700280 postTopologyEvent(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700281 assertEquals("No events should have been generated for a set of existing ports.", 0, events.size());
282
283 //Test removal when state changes when the device becomes unavailable
284
285 //Ensure that the deviceManager shows the device as unavailable
286 removeDevice(referenceDevice);
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700287 // This variable copies the behavior of the topology by returning ports
288 // attached to an unavailable device this behavior is necessary for the
289 // following event to execute properly, if these statements are removed
290 // no events will be generated since no ports will be provided in
291 // getPorts() to EdgeManager.
Aaron Kruglikove1200592015-06-29 16:31:23 -0700292 alwaysReturnPorts = true;
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700293 postTopologyEvent(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700294 alwaysReturnPorts = false;
295 assertEquals("An unexpected number of events were created.", totalPorts - numInfraPorts, events.size());
296 for (int i = 0; i < 5; i++) {
297 EdgePortEvent edgeEvent = events.get(i);
298 assertEquals("The event is of an unexpected type.",
alshabib8a4a6002015-11-25 14:31:16 -0800299 EdgePortEvent.Type.EDGE_PORT_REMOVED, edgeEvent.type());
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700300 assertThat("The event pertains to an unexpected port",
301 edgeEvent.subject().port().toLong(),
302 is(greaterThanOrEqualTo((long) numInfraPorts)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700303 }
304 }
305
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700306
Aaron Kruglikove1200592015-06-29 16:31:23 -0700307 @Test
308 public void testInternalCache() {
Aaron Kruglikove1200592015-06-29 16:31:23 -0700309 int numDevices = 10;
310 //Number of infrastructure ports per device
311 int numPorts = 5;
312 //Total ports per device when requesting all devices
313 totalPorts = 10;
314 defaultPopulator(numDevices, numPorts);
315 for (int i = 0; i < numDevices; i++) {
316 Device newDevice = NetTestTools.device(Integer.toString(i));
317 devices.put(newDevice.id(), newDevice);
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700318 postTopologyEvent(new DeviceEvent(DEVICE_ADDED, newDevice));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700319 }
Aaron Kruglikove1200592015-06-29 16:31:23 -0700320 //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.",
alshabib8a4a6002015-11-25 14:31:16 -0800337 (totalPorts - numPorts) * numDevices, count);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700338 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
Aaron Kruglikove1200592015-06-29 16:31:23 -0700347 @Test
348 public void testEmit() {
349 byte[] arr = new byte[10];
350 Device referenceDevice;
alshabib8a4a6002015-11-25 14:31:16 -0800351 DeviceEvent event;
Aaron Kruglikove1200592015-06-29 16:31:23 -0700352 int numDevices = 10;
353 int numInfraPorts = 5;
354 totalPorts = 10;
355 defaultPopulator(numDevices, numInfraPorts);
356 for (byte byteIndex = 0; byteIndex < arr.length; byteIndex++) {
357 arr[byteIndex] = byteIndex;
358 }
359 for (int i = 0; i < numDevices; i++) {
360 referenceDevice = NetTestTools.device(Integer.toString(i));
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700361 testDeviceManager.listener.event(new DeviceEvent(DEVICE_ADDED, referenceDevice,
alshabib8a4a6002015-11-25 14:31:16 -0800362 new DefaultPort(referenceDevice,
363 PortNumber.portNumber(1),
364 true)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700365 }
Aaron Kruglikove1200592015-06-29 16:31:23 -0700366
Sho SHIMIZU21d00692016-08-15 11:15:28 -0700367 mgr.emitPacket(ByteBuffer.wrap(arr), Optional.empty());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700368
369 assertEquals("There were an unexpected number of emitted packets",
alshabib8a4a6002015-11-25 14:31:16 -0800370 (totalPorts - numInfraPorts) * numDevices, packets.size());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700371 Iterator<OutboundPacket> packetIter = packets.iterator();
372 OutboundPacket packet;
373 while (packetIter.hasNext()) {
374 packet = packetIter.next();
375 assertEquals("The packet had an incorrect payload.", arr, packet.data().array());
376 }
377 //Start testing emission to a specific device
378 packets.clear();
Sho SHIMIZU21d00692016-08-15 11:15:28 -0700379 mgr.emitPacket(NetTestTools.did(Integer.toString(1)), ByteBuffer.wrap(arr), Optional.empty());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700380
381 assertEquals("Unexpected number of outbound packets were emitted.",
alshabib8a4a6002015-11-25 14:31:16 -0800382 totalPorts - numInfraPorts, packets.size());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700383 packetIter = packets.iterator();
384 while (packetIter.hasNext()) {
385 packet = packetIter.next();
386 assertEquals("The packet had an incorrect payload", arr, packet.data().array());
387 }
388 }
389
390
391 /**
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700392 * Creates TopologyEvent triggered by {@code event}.
393 *
394 * @param event reason of the TopologyEvent
395 * @return TopologyEvent
396 */
397 private TopologyEvent topologyEventOf(Event event) {
398 return new TopologyEvent(Type.TOPOLOGY_CHANGED, null, ImmutableList.of(event));
399 }
400
401
402 /**
403 * Post Event dispatched from TopologyManager.
404 *
405 * @param event Event
406 */
407 private void postTopologyEvent(Event event) {
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700408 if (event instanceof DeviceEvent) {
409 testDeviceManager.listener.event((DeviceEvent) event);
410 }
411 if (event instanceof LinkEvent) {
412 testLinkService.listener.event((LinkEvent) event);
413 }
414 //testTopologyManager.listener.event(topologyEventOf(event));
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700415 }
416
417
418 /**
Aaron Kruglikove1200592015-06-29 16:31:23 -0700419 * @param numDevices the number of devices to populate.
420 * @param numInfraPorts the number of ports to be set as infrastructure on each device, numbered base 0, ports 0
421 * through numInfraPorts - 1
422 */
423 private void defaultPopulator(int numDevices, int numInfraPorts) {
424 for (int device = 0; device < numDevices; device++) {
425 String str = Integer.toString(device);
426 Device deviceToAdd = NetTestTools.device(str);
427 devices.put(deviceToAdd.id(), deviceToAdd);
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700428 testDeviceManager.listener.event(new DeviceEvent(DEVICE_ADDED, deviceToAdd));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700429 for (int port = 1; port <= numInfraPorts; port++) {
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700430 testLinkService.listener.event(new LinkEvent(LINK_ADDED, NetTestTools.link(str, port, "other", 1)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700431 infrastructurePorts.add(NetTestTools.connectPoint(str, port));
432 }
433 }
434 }
435
436 /**
437 * Adds the specified device with the specified number of edge ports so long as it is less than the total ports.
438 *
439 * @param device The device to be added
440 * @param deviceName The name given to generate the devices DID
441 * @param numInfraPorts The number of ports to be added numbered 1 ... numInfraPorts
442 */
443 private void addDevice(Device device, String deviceName, int numInfraPorts) {
444 if (!devices.keySet().contains(device.id())) {
445 devices.put(device.id(), device);
446 for (int i = 1; i <= numInfraPorts && i <= totalPorts; i++) {
447 infrastructurePorts.add(NetTestTools.connectPoint(deviceName, i));
448 }
449 }
450 }
451
452 private void removeDevice(Device device) {
453 devices.remove(device.id());
454 }
455
456 private void removeInfraPort(ConnectPoint port) {
457 infrastructurePorts.remove(port);
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700458 }
459
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700460 private class TestDeviceManager extends DeviceServiceAdapter {
alshabib8a4a6002015-11-25 14:31:16 -0800461 private DeviceListener listener;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700462
Aaron Kruglikove1200592015-06-29 16:31:23 -0700463 private Map<DeviceId, Device> devices;
464
465 public TestDeviceManager(Map<DeviceId, Device> devices) {
466 this.devices = devices;
467 }
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700468
469 @Override
470 public boolean isAvailable(DeviceId deviceId) {
Aaron Kruglikove1200592015-06-29 16:31:23 -0700471 for (DeviceId id : devices.keySet()) {
472 if (id.equals(deviceId)) {
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700473 return true;
474 }
475 }
476 return false;
477 }
478
479 @Override
480 public List<Port> getPorts(DeviceId deviceId) {
Aaron Kruglikove1200592015-06-29 16:31:23 -0700481 List<Port> ports = new ArrayList<>();
482 Device device = devices.get(deviceId);
483 if (device == null && !alwaysReturnPorts) {
484 return ports;
485 }
486 for (int portNum = 1; portNum <= totalPorts; portNum++) {
487 //String is generated using 'of:' + the passed name, this creates a
488 ports.add(new DefaultPort(device, PortNumber.portNumber(portNum), true));
489 }
490 return ports;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700491 }
492
493 @Override
494 public Iterable<Device> getAvailableDevices() {
Aaron Kruglikove1200592015-06-29 16:31:23 -0700495 return devices.values();
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700496 }
alshabib8a4a6002015-11-25 14:31:16 -0800497
498
499 @Override
500 public void addListener(DeviceListener listener) {
501 this.listener = listener;
502 }
503
504 @Override
505 public void removeListener(DeviceListener listener) {
506 this.listener = null;
507 }
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700508 }
509
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700510 private class TestLinkService extends LinkServiceAdapter {
511
512 private LinkListener listener;
513
514 @Override
515 public Set<Link> getLinks(ConnectPoint connectPoint) {
516 if (infrastructurePorts.contains(connectPoint)) {
517 return Collections.singleton(NetTestTools.link("1", 1, "2", 1));
518 } else {
519 return Collections.emptySet();
520 }
521 }
522
523 @Override
524 public void addListener(LinkListener listener) {
525 this.listener = listener;
526 }
527 }
528
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700529 private class TestPacketManager extends PacketServiceAdapter {
Aaron Kruglikove1200592015-06-29 16:31:23 -0700530 @Override
531 public void emit(OutboundPacket packet) {
532 packets.add(packet);
533 }
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700534 }
535
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700536 private class TestListener implements EdgePortListener {
537 private List<EdgePortEvent> events;
538
Aaron Kruglikove1200592015-06-29 16:31:23 -0700539 public TestListener(List<EdgePortEvent> events) {
540 this.events = events;
541 }
542
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700543 @Override
544 public void event(EdgePortEvent event) {
545 events.add(event);
546 }
547 }
Sho SHIMIZU21d00692016-08-15 11:15:28 -0700548}