blob: 93234d3015ebfd2f1621d585df70e24cfe6c084b [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 HIGUCHI37dca332016-11-14 18:43:22 -0800134 devices.put(NetTestTools.did("a"), NetTestTools.device("a"));
135 devices.put(NetTestTools.did("b"), NetTestTools.device("b"));
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700136 postTopologyEvent(new LinkEvent(LINK_REMOVED, NetTestTools.link("a", 1, "b", 2)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700137
Yuta HIGUCHI37dca332016-11-14 18:43:22 -0800138 assertEquals("The list contained an unexpected number of events",
139 2, events.size());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700140 assertTrue("The first element is of the wrong type.",
alshabib8a4a6002015-11-25 14:31:16 -0800141 events.get(0).type() == EDGE_PORT_ADDED);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700142
143 testPoint = events.get(0).subject();
144 referencePoint = NetTestTools.connectPoint("a", 1);
145 assertTrue("The port numbers of the first element are incorrect",
alshabib8a4a6002015-11-25 14:31:16 -0800146 testPoint.port().toLong() == referencePoint.port().toLong());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700147 assertTrue("The device id of the first element is incorrect.",
alshabib8a4a6002015-11-25 14:31:16 -0800148 testPoint.deviceId().equals(referencePoint.deviceId()));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700149
150 testPoint = events.get(1).subject();
151 referencePoint = NetTestTools.connectPoint("b", 2);
152 assertTrue("The port numbers of the second element are incorrect",
alshabib8a4a6002015-11-25 14:31:16 -0800153 testPoint.port().toLong() == referencePoint.port().toLong());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700154 assertTrue("The device id of the second element is incorrect.",
alshabib8a4a6002015-11-25 14:31:16 -0800155 testPoint.deviceId().equals(referencePoint.deviceId()));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700156
157 //Rebroadcast event to ensure it results in no additional events
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700158 postTopologyEvent(new LinkEvent(LINK_REMOVED, NetTestTools.link("a", 1, "b", 2)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700159 assertTrue("The list contained an unexpected number of events", events.size() == 2);
160
161 //Testing link adding when links to remove exist
Aaron Kruglikove1200592015-06-29 16:31:23 -0700162 events.clear();
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700163 postTopologyEvent(new LinkEvent(LINK_ADDED, NetTestTools.link("a", 1, "b", 2)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700164
165 assertTrue("The list contained an unexpected number of events", events.size() == 2);
166 assertTrue("The first element is of the wrong type.",
alshabib8a4a6002015-11-25 14:31:16 -0800167 events.get(0).type() == EDGE_PORT_REMOVED);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700168 assertTrue("The second element is of the wrong type.",
alshabib8a4a6002015-11-25 14:31:16 -0800169 events.get(1).type() == EDGE_PORT_REMOVED);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700170
171 testPoint = events.get(0).subject();
172 referencePoint = NetTestTools.connectPoint("a", 1);
173 assertTrue("The port numbers of the first element are incorrect",
alshabib8a4a6002015-11-25 14:31:16 -0800174 testPoint.port().toLong() == referencePoint.port().toLong());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700175 assertTrue("The device id of the first element is incorrect.",
alshabib8a4a6002015-11-25 14:31:16 -0800176 testPoint.deviceId().equals(referencePoint.deviceId()));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700177
178 testPoint = events.get(1).subject();
179 referencePoint = NetTestTools.connectPoint("b", 2);
180 assertTrue("The port numbers of the second element are incorrect",
alshabib8a4a6002015-11-25 14:31:16 -0800181 testPoint.port().toLong() == referencePoint.port().toLong());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700182 assertTrue("The device id of the second element is incorrect.",
alshabib8a4a6002015-11-25 14:31:16 -0800183 testPoint.deviceId().equals(referencePoint.deviceId()));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700184
185 //Apparent duplicate of previous method tests removal when the elements have already been removed
Aaron Kruglikove1200592015-06-29 16:31:23 -0700186 events.clear();
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700187 postTopologyEvent(new LinkEvent(LINK_ADDED, NetTestTools.link("a", 1, "b", 2)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700188 assertTrue("The list should contain no events, the removed elements don't exist.", events.size() == 0);
189 }
190
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700191
Aaron Kruglikove1200592015-06-29 16:31:23 -0700192 @Test
193 public void testDeviceUpdates() {
194 //Setup
195
196 Device referenceDevice;
alshabib8a4a6002015-11-25 14:31:16 -0800197 DeviceEvent event;
Aaron Kruglikove1200592015-06-29 16:31:23 -0700198 int numDevices = 10;
199 int numInfraPorts = 5;
200 totalPorts = 10;
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700201
Aaron Kruglikove1200592015-06-29 16:31:23 -0700202 defaultPopulator(numDevices, numInfraPorts);
203
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700204 events.clear();
205
Aaron Kruglikove1200592015-06-29 16:31:23 -0700206 //Test response to device added events
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700207
208 referenceDevice = NetTestTools.device("11");
209 devices.put(referenceDevice.id(), referenceDevice);
210 for (int port = 1; port <= numInfraPorts; port++) {
211 infrastructurePorts.add(NetTestTools.connectPoint("11", port));
212 }
alshabib8a4a6002015-11-25 14:31:16 -0800213 event = new DeviceEvent(DEVICE_ADDED, referenceDevice,
214 new DefaultPort(referenceDevice, PortNumber.portNumber(1), true));
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700215 postTopologyEvent(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700216
217 //Check that ports were populated correctly
218 assertTrue("Unexpected number of new ports added",
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700219 mgr.deviceService.getPorts(NetTestTools.did("11")).size() == 10);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700220
221 //Check that of the ten ports the half that are infrastructure ports aren't added
222 assertEquals("Unexpected number of new edge ports added", (totalPorts - numInfraPorts), events.size());
223
224 for (int index = 0; index < numInfraPorts; index++) {
225 assertTrue("Unexpected type of event", events.get(index).type() == EDGE_PORT_ADDED);
226 }
227 //Names here are irrelevant, the first 5 ports are populated as infrastructure, 6-10 are edge
228 for (int index = 0; index < events.size(); index++) {
229 assertEquals("Port added had unexpected port number.",
alshabib8a4a6002015-11-25 14:31:16 -0800230 events.get(index).subject().port(),
231 NetTestTools.connectPoint("a", index + numInfraPorts + 1).port());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700232 }
233 events.clear();
234
235 //Repost the event to test repeated posts
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700236 postTopologyEvent(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700237 assertEquals("The redundant notification should not have created additional notifications.",
alshabib8a4a6002015-11-25 14:31:16 -0800238 0, events.size());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700239 //Calculate the size of the returned iterable of edge points.
240 Iterable<ConnectPoint> pts = mgr.getEdgePoints();
241 Iterator pointIterator = pts.iterator();
242 int count = 0;
243 for (; pointIterator.hasNext(); count++) {
244 pointIterator.next();
245 }
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700246 assertEquals("Unexpected number of edge points", (numDevices + 1) * numInfraPorts, count);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700247 //Testing device removal
248 events.clear();
alshabib8a4a6002015-11-25 14:31:16 -0800249 event = (new DeviceEvent(DEVICE_REMOVED, referenceDevice,
250 new DefaultPort(referenceDevice, PortNumber.portNumber(1), true)));
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700251 postTopologyEvent(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700252
253 assertEquals("There should be five new events from removal of edge points",
alshabib8a4a6002015-11-25 14:31:16 -0800254 totalPorts - numInfraPorts, events.size());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700255 for (int index = 0; index < events.size(); index++) {
256 //Assert that the correct port numbers were removed in the correct order
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700257 assertThat("Port removed had unexpected port number.",
258 events.get(index).subject().port().toLong(),
259 is(greaterThanOrEqualTo((long) numInfraPorts)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700260 //Assert that the events are of the correct type
261 assertEquals("Unexpected type of event", events.get(index).type(), EDGE_PORT_REMOVED);
262 }
263 events.clear();
264 //Rebroadcast event to check that it triggers no new behavior
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700265 postTopologyEvent(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700266 assertEquals("Rebroadcast of removal event should not produce additional events",
alshabib8a4a6002015-11-25 14:31:16 -0800267 0, events.size());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700268
269 //Testing device status change, changed from unavailable to available
270 events.clear();
Aaron Kruglikove1200592015-06-29 16:31:23 -0700271 //Make sure that the devicemanager shows the device as available.
272 addDevice(referenceDevice, "1", 5);
273 devices.put(referenceDevice.id(), referenceDevice);
274
alshabib8a4a6002015-11-25 14:31:16 -0800275 event = new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, referenceDevice);
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700276 postTopologyEvent(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700277 //An earlier setup set half of the reference device ports to infrastructure
278 assertEquals("An unexpected number of events were generated.", totalPorts - numInfraPorts, events.size());
279 for (int i = 0; i < 5; i++) {
280 assertEquals("The event was not of the right type", events.get(i).type(), EDGE_PORT_ADDED);
281 }
282 events.clear();
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700283 postTopologyEvent(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700284 assertEquals("No events should have been generated for a set of existing ports.", 0, events.size());
285
286 //Test removal when state changes when the device becomes unavailable
287
288 //Ensure that the deviceManager shows the device as unavailable
289 removeDevice(referenceDevice);
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700290 // This variable copies the behavior of the topology by returning ports
291 // attached to an unavailable device this behavior is necessary for the
292 // following event to execute properly, if these statements are removed
293 // no events will be generated since no ports will be provided in
294 // getPorts() to EdgeManager.
Aaron Kruglikove1200592015-06-29 16:31:23 -0700295 alwaysReturnPorts = true;
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700296 postTopologyEvent(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700297 alwaysReturnPorts = false;
298 assertEquals("An unexpected number of events were created.", totalPorts - numInfraPorts, events.size());
299 for (int i = 0; i < 5; i++) {
300 EdgePortEvent edgeEvent = events.get(i);
301 assertEquals("The event is of an unexpected type.",
alshabib8a4a6002015-11-25 14:31:16 -0800302 EdgePortEvent.Type.EDGE_PORT_REMOVED, edgeEvent.type());
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700303 assertThat("The event pertains to an unexpected port",
304 edgeEvent.subject().port().toLong(),
305 is(greaterThanOrEqualTo((long) numInfraPorts)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700306 }
307 }
308
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700309
Aaron Kruglikove1200592015-06-29 16:31:23 -0700310 @Test
311 public void testInternalCache() {
Aaron Kruglikove1200592015-06-29 16:31:23 -0700312 int numDevices = 10;
313 //Number of infrastructure ports per device
314 int numPorts = 5;
315 //Total ports per device when requesting all devices
316 totalPorts = 10;
317 defaultPopulator(numDevices, numPorts);
318 for (int i = 0; i < numDevices; i++) {
319 Device newDevice = NetTestTools.device(Integer.toString(i));
320 devices.put(newDevice.id(), newDevice);
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700321 postTopologyEvent(new DeviceEvent(DEVICE_ADDED, newDevice));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700322 }
Aaron Kruglikove1200592015-06-29 16:31:23 -0700323 //Check all ports have correct designations
324 ConnectPoint testPoint;
325 for (int deviceNum = 0; deviceNum < numDevices; deviceNum++) {
326 for (int portNum = 1; portNum <= totalPorts; portNum++) {
327 testPoint = NetTestTools.connectPoint(Integer.toString(deviceNum), portNum);
328 if (portNum <= numPorts) {
329 assertFalse("This should not be an edge point", mgr.isEdgePoint(testPoint));
330 } else {
331 assertTrue("This should be an edge point", mgr.isEdgePoint(testPoint));
332 }
333 }
334 }
335 int count = 0;
336 for (ConnectPoint ignored : mgr.getEdgePoints()) {
337 count++;
338 }
339 assertEquals("There are an unexpeceted number of edge points returned.",
alshabib8a4a6002015-11-25 14:31:16 -0800340 (totalPorts - numPorts) * numDevices, count);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700341 for (int deviceNumber = 0; deviceNumber < numDevices; deviceNumber++) {
342 count = 0;
343 for (ConnectPoint ignored : mgr.getEdgePoints(NetTestTools.did("1"))) {
344 count++;
345 }
346 assertEquals("This element has an unexpected number of edge points.", (totalPorts - numPorts), count);
347 }
348 }
349
Aaron Kruglikove1200592015-06-29 16:31:23 -0700350 @Test
351 public void testEmit() {
352 byte[] arr = new byte[10];
353 Device referenceDevice;
alshabib8a4a6002015-11-25 14:31:16 -0800354 DeviceEvent event;
Aaron Kruglikove1200592015-06-29 16:31:23 -0700355 int numDevices = 10;
356 int numInfraPorts = 5;
357 totalPorts = 10;
358 defaultPopulator(numDevices, numInfraPorts);
359 for (byte byteIndex = 0; byteIndex < arr.length; byteIndex++) {
360 arr[byteIndex] = byteIndex;
361 }
362 for (int i = 0; i < numDevices; i++) {
363 referenceDevice = NetTestTools.device(Integer.toString(i));
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700364 testDeviceManager.listener.event(new DeviceEvent(DEVICE_ADDED, referenceDevice,
alshabib8a4a6002015-11-25 14:31:16 -0800365 new DefaultPort(referenceDevice,
366 PortNumber.portNumber(1),
367 true)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700368 }
Aaron Kruglikove1200592015-06-29 16:31:23 -0700369
Sho SHIMIZU21d00692016-08-15 11:15:28 -0700370 mgr.emitPacket(ByteBuffer.wrap(arr), Optional.empty());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700371
372 assertEquals("There were an unexpected number of emitted packets",
alshabib8a4a6002015-11-25 14:31:16 -0800373 (totalPorts - numInfraPorts) * numDevices, packets.size());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700374 Iterator<OutboundPacket> packetIter = packets.iterator();
375 OutboundPacket packet;
376 while (packetIter.hasNext()) {
377 packet = packetIter.next();
378 assertEquals("The packet had an incorrect payload.", arr, packet.data().array());
379 }
380 //Start testing emission to a specific device
381 packets.clear();
Sho SHIMIZU21d00692016-08-15 11:15:28 -0700382 mgr.emitPacket(NetTestTools.did(Integer.toString(1)), ByteBuffer.wrap(arr), Optional.empty());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700383
384 assertEquals("Unexpected number of outbound packets were emitted.",
alshabib8a4a6002015-11-25 14:31:16 -0800385 totalPorts - numInfraPorts, packets.size());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700386 packetIter = packets.iterator();
387 while (packetIter.hasNext()) {
388 packet = packetIter.next();
389 assertEquals("The packet had an incorrect payload", arr, packet.data().array());
390 }
391 }
392
393
394 /**
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700395 * Creates TopologyEvent triggered by {@code event}.
396 *
397 * @param event reason of the TopologyEvent
398 * @return TopologyEvent
399 */
400 private TopologyEvent topologyEventOf(Event event) {
401 return new TopologyEvent(Type.TOPOLOGY_CHANGED, null, ImmutableList.of(event));
402 }
403
404
405 /**
406 * Post Event dispatched from TopologyManager.
407 *
408 * @param event Event
409 */
410 private void postTopologyEvent(Event event) {
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700411 if (event instanceof DeviceEvent) {
412 testDeviceManager.listener.event((DeviceEvent) event);
413 }
414 if (event instanceof LinkEvent) {
415 testLinkService.listener.event((LinkEvent) event);
416 }
417 //testTopologyManager.listener.event(topologyEventOf(event));
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700418 }
419
420
421 /**
Aaron Kruglikove1200592015-06-29 16:31:23 -0700422 * @param numDevices the number of devices to populate.
423 * @param numInfraPorts the number of ports to be set as infrastructure on each device, numbered base 0, ports 0
424 * through numInfraPorts - 1
425 */
426 private void defaultPopulator(int numDevices, int numInfraPorts) {
427 for (int device = 0; device < numDevices; device++) {
428 String str = Integer.toString(device);
429 Device deviceToAdd = NetTestTools.device(str);
430 devices.put(deviceToAdd.id(), deviceToAdd);
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700431 testDeviceManager.listener.event(new DeviceEvent(DEVICE_ADDED, deviceToAdd));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700432 for (int port = 1; port <= numInfraPorts; port++) {
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700433 testLinkService.listener.event(new LinkEvent(LINK_ADDED, NetTestTools.link(str, port, "other", 1)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700434 infrastructurePorts.add(NetTestTools.connectPoint(str, port));
435 }
436 }
437 }
438
439 /**
440 * Adds the specified device with the specified number of edge ports so long as it is less than the total ports.
441 *
442 * @param device The device to be added
443 * @param deviceName The name given to generate the devices DID
444 * @param numInfraPorts The number of ports to be added numbered 1 ... numInfraPorts
445 */
446 private void addDevice(Device device, String deviceName, int numInfraPorts) {
447 if (!devices.keySet().contains(device.id())) {
448 devices.put(device.id(), device);
449 for (int i = 1; i <= numInfraPorts && i <= totalPorts; i++) {
450 infrastructurePorts.add(NetTestTools.connectPoint(deviceName, i));
451 }
452 }
453 }
454
455 private void removeDevice(Device device) {
456 devices.remove(device.id());
457 }
458
459 private void removeInfraPort(ConnectPoint port) {
460 infrastructurePorts.remove(port);
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700461 }
462
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700463 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
Jonathan Hart6ff6ffe2016-09-09 11:55:50 -0700513 private class TestLinkService extends LinkServiceAdapter {
514
515 private LinkListener listener;
516
517 @Override
518 public Set<Link> getLinks(ConnectPoint connectPoint) {
519 if (infrastructurePorts.contains(connectPoint)) {
520 return Collections.singleton(NetTestTools.link("1", 1, "2", 1));
521 } else {
522 return Collections.emptySet();
523 }
524 }
525
526 @Override
527 public void addListener(LinkListener listener) {
528 this.listener = listener;
529 }
530 }
531
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700532 private class TestPacketManager extends PacketServiceAdapter {
Aaron Kruglikove1200592015-06-29 16:31:23 -0700533 @Override
534 public void emit(OutboundPacket packet) {
535 packets.add(packet);
536 }
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700537 }
538
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700539 private class TestListener implements EdgePortListener {
540 private List<EdgePortEvent> events;
541
Aaron Kruglikove1200592015-06-29 16:31:23 -0700542 public TestListener(List<EdgePortEvent> events) {
543 this.events = events;
544 }
545
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700546 @Override
547 public void event(EdgePortEvent event) {
548 events.add(event);
549 }
550 }
Sho SHIMIZU21d00692016-08-15 11:15:28 -0700551}