blob: b01ec39e1c4880e7c575740b3a05446f9d6ec80e [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.flow.TrafficTreatment;
41import org.onosproject.net.link.LinkEvent;
42import org.onosproject.net.packet.OutboundPacket;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070043import org.onosproject.net.packet.PacketServiceAdapter;
44import org.onosproject.net.topology.Topology;
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 -070047import org.onosproject.net.topology.TopologyListener;
48import org.onosproject.net.topology.TopologyServiceAdapter;
49
Aaron Kruglikove1200592015-06-29 16:31:23 -070050import java.nio.ByteBuffer;
51import java.util.ArrayList;
52import java.util.Iterator;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070053import java.util.List;
Aaron Kruglikove1200592015-06-29 16:31:23 -070054import java.util.Map;
55import java.util.Optional;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070056import java.util.Set;
57
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -070058import static org.hamcrest.Matchers.*;
Aaron Kruglikove1200592015-06-29 16:31:23 -070059import static org.junit.Assert.*;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070060import static org.onosproject.net.NetTestTools.injectEventDispatcher;
Aaron Kruglikove1200592015-06-29 16:31:23 -070061import static org.onosproject.net.device.DeviceEvent.Type.*;
62import static org.onosproject.net.edge.EdgePortEvent.Type.EDGE_PORT_ADDED;
63import static org.onosproject.net.edge.EdgePortEvent.Type.EDGE_PORT_REMOVED;
64import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED;
65import static org.onosproject.net.link.LinkEvent.Type.LINK_REMOVED;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070066
67/**
Aaron Kruglikove1200592015-06-29 16:31:23 -070068 * Test of the edge port manager. Each device has ports '0' through 'numPorts - 1'
69 * as specified by the variable 'numPorts'.
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070070 */
71public class EdgeManagerTest {
72
73 private EdgeManager mgr;
Aaron Kruglikove1200592015-06-29 16:31:23 -070074 private int totalPorts = 10;
75 private boolean alwaysReturnPorts = false;
76 private final Set<ConnectPoint> infrastructurePorts = Sets.newConcurrentHashSet();
77 private List<EdgePortEvent> events = Lists.newArrayList();
78 private final Map<DeviceId, Device> devices = Maps.newConcurrentMap();
79 private Set<OutboundPacket> packets = Sets.newConcurrentHashSet();
80 private final EdgePortListener testListener = new TestListener(events);
alshabib8a4a6002015-11-25 14:31:16 -080081 private TestDeviceManager testDeviceManager;
Aaron Kruglikove1200592015-06-29 16:31:23 -070082 private TestTopologyManager testTopologyManager;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070083
84 @Before
85 public void setUp() {
86 mgr = new EdgeManager();
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070087 injectEventDispatcher(mgr, new TestEventDispatcher());
Aaron Kruglikove1200592015-06-29 16:31:23 -070088 testTopologyManager = new TestTopologyManager(infrastructurePorts);
89 mgr.topologyService = testTopologyManager;
alshabib8a4a6002015-11-25 14:31:16 -080090 testDeviceManager = new TestDeviceManager(devices);
91 mgr.deviceService = testDeviceManager;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070092 mgr.packetService = new TestPacketManager();
93 mgr.activate();
94 mgr.addListener(testListener);
Aaron Kruglikove1200592015-06-29 16:31:23 -070095
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070096 }
97
Aaron Kruglikove1200592015-06-29 16:31:23 -070098
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -070099 @After
100 public void tearDown() {
101 mgr.removeListener(testListener);
102 mgr.deactivate();
103 }
104
105 @Test
Aaron Kruglikove1200592015-06-29 16:31:23 -0700106 public void testBasics() {
107 //Setup
108 int numDevices = 20;
109 int numPorts = 4;
110 defaultPopulator(numDevices, numPorts);
111
112 assertEquals("Unexpected number of ports", numDevices * numPorts, infrastructurePorts.size());
113
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700114 assertFalse("no ports expected", mgr.getEdgePoints().iterator().hasNext());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700115
116 assertFalse("Expected isEdge to return false",
alshabib8a4a6002015-11-25 14:31:16 -0800117 mgr.isEdgePoint(NetTestTools.connectPoint(Integer.toString(1), 1)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700118
119 removeInfraPort(NetTestTools.connectPoint(Integer.toString(1), 1));
120 assertTrue("Expected isEdge to return false",
alshabib8a4a6002015-11-25 14:31:16 -0800121 mgr.isEdgePoint(NetTestTools.connectPoint(Integer.toString(1), 1)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700122 }
123
124 @Test
125 public void testLinkUpdates() {
126 //Setup
127 ConnectPoint testPoint, referencePoint;
128
129 //Testing link removal
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700130 postTopologyEvent(new LinkEvent(LINK_REMOVED, NetTestTools.link("a", 1, "b", 2)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700131
132 assertTrue("The list contained an unexpected number of events", events.size() == 2);
133 assertTrue("The first element is of the wrong type.",
alshabib8a4a6002015-11-25 14:31:16 -0800134 events.get(0).type() == EDGE_PORT_ADDED);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700135
136 testPoint = events.get(0).subject();
137 referencePoint = NetTestTools.connectPoint("a", 1);
138 assertTrue("The port numbers of the first element are incorrect",
alshabib8a4a6002015-11-25 14:31:16 -0800139 testPoint.port().toLong() == referencePoint.port().toLong());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700140 assertTrue("The device id of the first element is incorrect.",
alshabib8a4a6002015-11-25 14:31:16 -0800141 testPoint.deviceId().equals(referencePoint.deviceId()));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700142
143 testPoint = events.get(1).subject();
144 referencePoint = NetTestTools.connectPoint("b", 2);
145 assertTrue("The port numbers of the second 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 second element is incorrect.",
alshabib8a4a6002015-11-25 14:31:16 -0800148 testPoint.deviceId().equals(referencePoint.deviceId()));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700149
150 //Rebroadcast event to ensure it results in no additional events
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700151 postTopologyEvent(new LinkEvent(LINK_REMOVED, NetTestTools.link("a", 1, "b", 2)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700152 assertTrue("The list contained an unexpected number of events", events.size() == 2);
153
154 //Testing link adding when links to remove exist
Aaron Kruglikove1200592015-06-29 16:31:23 -0700155 events.clear();
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700156 postTopologyEvent(new LinkEvent(LINK_ADDED, NetTestTools.link("a", 1, "b", 2)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700157
158 assertTrue("The list contained an unexpected number of events", events.size() == 2);
159 assertTrue("The first element is of the wrong type.",
alshabib8a4a6002015-11-25 14:31:16 -0800160 events.get(0).type() == EDGE_PORT_REMOVED);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700161 assertTrue("The second element is of the wrong type.",
alshabib8a4a6002015-11-25 14:31:16 -0800162 events.get(1).type() == EDGE_PORT_REMOVED);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700163
164 testPoint = events.get(0).subject();
165 referencePoint = NetTestTools.connectPoint("a", 1);
166 assertTrue("The port numbers of the first element are incorrect",
alshabib8a4a6002015-11-25 14:31:16 -0800167 testPoint.port().toLong() == referencePoint.port().toLong());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700168 assertTrue("The device id of the first element is incorrect.",
alshabib8a4a6002015-11-25 14:31:16 -0800169 testPoint.deviceId().equals(referencePoint.deviceId()));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700170
171 testPoint = events.get(1).subject();
172 referencePoint = NetTestTools.connectPoint("b", 2);
173 assertTrue("The port numbers of the second 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 second element is incorrect.",
alshabib8a4a6002015-11-25 14:31:16 -0800176 testPoint.deviceId().equals(referencePoint.deviceId()));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700177
178 //Apparent duplicate of previous method tests removal when the elements have already been removed
Aaron Kruglikove1200592015-06-29 16:31:23 -0700179 events.clear();
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700180 postTopologyEvent(new LinkEvent(LINK_ADDED, NetTestTools.link("a", 1, "b", 2)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700181 assertTrue("The list should contain no events, the removed elements don't exist.", events.size() == 0);
182 }
183
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700184
Aaron Kruglikove1200592015-06-29 16:31:23 -0700185 @Test
186 public void testDeviceUpdates() {
187 //Setup
188
189 Device referenceDevice;
alshabib8a4a6002015-11-25 14:31:16 -0800190 DeviceEvent event;
Aaron Kruglikove1200592015-06-29 16:31:23 -0700191 int numDevices = 10;
192 int numInfraPorts = 5;
193 totalPorts = 10;
194 defaultPopulator(numDevices, numInfraPorts);
195
196 //Test response to device added events
197 referenceDevice = NetTestTools.device("1");
alshabib8a4a6002015-11-25 14:31:16 -0800198 event = new DeviceEvent(DEVICE_ADDED, referenceDevice,
199 new DefaultPort(referenceDevice, PortNumber.portNumber(1), true));
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700200 postTopologyEvent(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700201
202 //Check that ports were populated correctly
203 assertTrue("Unexpected number of new ports added",
alshabib8a4a6002015-11-25 14:31:16 -0800204 mgr.deviceService.getPorts(NetTestTools.did("1")).size() == 10);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700205
206 //Check that of the ten ports the half that are infrastructure ports aren't added
207 assertEquals("Unexpected number of new edge ports added", (totalPorts - numInfraPorts), events.size());
208
209 for (int index = 0; index < numInfraPorts; index++) {
210 assertTrue("Unexpected type of event", events.get(index).type() == EDGE_PORT_ADDED);
211 }
212 //Names here are irrelevant, the first 5 ports are populated as infrastructure, 6-10 are edge
213 for (int index = 0; index < events.size(); index++) {
214 assertEquals("Port added had unexpected port number.",
alshabib8a4a6002015-11-25 14:31:16 -0800215 events.get(index).subject().port(),
216 NetTestTools.connectPoint("a", index + numInfraPorts + 1).port());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700217 }
218 events.clear();
219
220 //Repost the event to test repeated posts
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700221 postTopologyEvent(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700222 assertEquals("The redundant notification should not have created additional notifications.",
alshabib8a4a6002015-11-25 14:31:16 -0800223 0, events.size());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700224 //Calculate the size of the returned iterable of edge points.
225 Iterable<ConnectPoint> pts = mgr.getEdgePoints();
226 Iterator pointIterator = pts.iterator();
227 int count = 0;
228 for (; pointIterator.hasNext(); count++) {
229 pointIterator.next();
230 }
231 assertEquals("Unexpected number of edge points", totalPorts - numInfraPorts, count);
232 //Testing device removal
233 events.clear();
alshabib8a4a6002015-11-25 14:31:16 -0800234 event = (new DeviceEvent(DEVICE_REMOVED, referenceDevice,
235 new DefaultPort(referenceDevice, PortNumber.portNumber(1), true)));
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700236 postTopologyEvent(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700237
238 assertEquals("There should be five new events from removal of edge points",
alshabib8a4a6002015-11-25 14:31:16 -0800239 totalPorts - numInfraPorts, events.size());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700240 for (int index = 0; index < events.size(); index++) {
241 //Assert that the correct port numbers were removed in the correct order
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700242 assertThat("Port removed had unexpected port number.",
243 events.get(index).subject().port().toLong(),
244 is(greaterThanOrEqualTo((long) numInfraPorts)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700245 //Assert that the events are of the correct type
246 assertEquals("Unexpected type of event", events.get(index).type(), EDGE_PORT_REMOVED);
247 }
248 events.clear();
249 //Rebroadcast event to check that it triggers no new behavior
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700250 postTopologyEvent(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700251 assertEquals("Rebroadcast of removal event should not produce additional events",
alshabib8a4a6002015-11-25 14:31:16 -0800252 0, events.size());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700253
254 //Testing device status change, changed from unavailable to available
255 events.clear();
Aaron Kruglikove1200592015-06-29 16:31:23 -0700256 //Make sure that the devicemanager shows the device as available.
257 addDevice(referenceDevice, "1", 5);
258 devices.put(referenceDevice.id(), referenceDevice);
259
alshabib8a4a6002015-11-25 14:31:16 -0800260 event = new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, referenceDevice);
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700261 postTopologyEvent(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700262 //An earlier setup set half of the reference device ports to infrastructure
263 assertEquals("An unexpected number of events were generated.", totalPorts - numInfraPorts, events.size());
264 for (int i = 0; i < 5; i++) {
265 assertEquals("The event was not of the right type", events.get(i).type(), EDGE_PORT_ADDED);
266 }
267 events.clear();
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700268 postTopologyEvent(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700269 assertEquals("No events should have been generated for a set of existing ports.", 0, events.size());
270
271 //Test removal when state changes when the device becomes unavailable
272
273 //Ensure that the deviceManager shows the device as unavailable
274 removeDevice(referenceDevice);
275 /*This variable copies the behavior of the topology by returning ports attached to an unavailable device
276 //this behavior is necessary for the following event to execute properly, if these statements are removed
277 no events will be generated since no ports will be provided in getPorts() to EdgeManager.
278 */
279 alwaysReturnPorts = true;
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700280 postTopologyEvent(event);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700281 alwaysReturnPorts = false;
282 assertEquals("An unexpected number of events were created.", totalPorts - numInfraPorts, events.size());
283 for (int i = 0; i < 5; i++) {
284 EdgePortEvent edgeEvent = events.get(i);
285 assertEquals("The event is of an unexpected type.",
alshabib8a4a6002015-11-25 14:31:16 -0800286 EdgePortEvent.Type.EDGE_PORT_REMOVED, edgeEvent.type());
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700287 assertThat("The event pertains to an unexpected port",
288 edgeEvent.subject().port().toLong(),
289 is(greaterThanOrEqualTo((long) numInfraPorts)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700290 }
291 }
292
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700293
Aaron Kruglikove1200592015-06-29 16:31:23 -0700294 @Test
295 public void testInternalCache() {
Aaron Kruglikove1200592015-06-29 16:31:23 -0700296 int numDevices = 10;
297 //Number of infrastructure ports per device
298 int numPorts = 5;
299 //Total ports per device when requesting all devices
300 totalPorts = 10;
301 defaultPopulator(numDevices, numPorts);
302 for (int i = 0; i < numDevices; i++) {
303 Device newDevice = NetTestTools.device(Integer.toString(i));
304 devices.put(newDevice.id(), newDevice);
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700305 postTopologyEvent(new DeviceEvent(DEVICE_ADDED, newDevice));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700306 }
Aaron Kruglikove1200592015-06-29 16:31:23 -0700307 //Check all ports have correct designations
308 ConnectPoint testPoint;
309 for (int deviceNum = 0; deviceNum < numDevices; deviceNum++) {
310 for (int portNum = 1; portNum <= totalPorts; portNum++) {
311 testPoint = NetTestTools.connectPoint(Integer.toString(deviceNum), portNum);
312 if (portNum <= numPorts) {
313 assertFalse("This should not be an edge point", mgr.isEdgePoint(testPoint));
314 } else {
315 assertTrue("This should be an edge point", mgr.isEdgePoint(testPoint));
316 }
317 }
318 }
319 int count = 0;
320 for (ConnectPoint ignored : mgr.getEdgePoints()) {
321 count++;
322 }
323 assertEquals("There are an unexpeceted number of edge points returned.",
alshabib8a4a6002015-11-25 14:31:16 -0800324 (totalPorts - numPorts) * numDevices, count);
Aaron Kruglikove1200592015-06-29 16:31:23 -0700325 for (int deviceNumber = 0; deviceNumber < numDevices; deviceNumber++) {
326 count = 0;
327 for (ConnectPoint ignored : mgr.getEdgePoints(NetTestTools.did("1"))) {
328 count++;
329 }
330 assertEquals("This element has an unexpected number of edge points.", (totalPorts - numPorts), count);
331 }
332 }
333
334
335 @Test
336 public void testEmit() {
337 byte[] arr = new byte[10];
338 Device referenceDevice;
alshabib8a4a6002015-11-25 14:31:16 -0800339 DeviceEvent event;
Aaron Kruglikove1200592015-06-29 16:31:23 -0700340 int numDevices = 10;
341 int numInfraPorts = 5;
342 totalPorts = 10;
343 defaultPopulator(numDevices, numInfraPorts);
344 for (byte byteIndex = 0; byteIndex < arr.length; byteIndex++) {
345 arr[byteIndex] = byteIndex;
346 }
347 for (int i = 0; i < numDevices; i++) {
348 referenceDevice = NetTestTools.device(Integer.toString(i));
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700349 postTopologyEvent(new DeviceEvent(DEVICE_ADDED, referenceDevice,
alshabib8a4a6002015-11-25 14:31:16 -0800350 new DefaultPort(referenceDevice,
351 PortNumber.portNumber(1),
352 true)));
Aaron Kruglikove1200592015-06-29 16:31:23 -0700353 }
Aaron Kruglikove1200592015-06-29 16:31:23 -0700354
355 mgr.emitPacket(ByteBuffer.wrap(arr), Optional.<TrafficTreatment>empty());
356
357 assertEquals("There were an unexpected number of emitted packets",
alshabib8a4a6002015-11-25 14:31:16 -0800358 (totalPorts - numInfraPorts) * numDevices, packets.size());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700359 Iterator<OutboundPacket> packetIter = packets.iterator();
360 OutboundPacket packet;
361 while (packetIter.hasNext()) {
362 packet = packetIter.next();
363 assertEquals("The packet had an incorrect payload.", arr, packet.data().array());
364 }
365 //Start testing emission to a specific device
366 packets.clear();
367 mgr.emitPacket(NetTestTools.did(Integer.toString(1)), ByteBuffer.wrap(arr), Optional.<TrafficTreatment>empty());
368
369 assertEquals("Unexpected number of outbound packets were emitted.",
alshabib8a4a6002015-11-25 14:31:16 -0800370 totalPorts - numInfraPorts, packets.size());
Aaron Kruglikove1200592015-06-29 16:31:23 -0700371 packetIter = packets.iterator();
372 while (packetIter.hasNext()) {
373 packet = packetIter.next();
374 assertEquals("The packet had an incorrect payload", arr, packet.data().array());
375 }
376 }
377
378
379 /**
Yuta HIGUCHIb440ef42016-07-15 09:09:09 -0700380 * Creates TopologyEvent triggered by {@code event}.
381 *
382 * @param event reason of the TopologyEvent
383 * @return TopologyEvent
384 */
385 private TopologyEvent topologyEventOf(Event event) {
386 return new TopologyEvent(Type.TOPOLOGY_CHANGED, null, ImmutableList.of(event));
387 }
388
389
390 /**
391 * Post Event dispatched from TopologyManager.
392 *
393 * @param event Event
394 */
395 private void postTopologyEvent(Event event) {
396 testTopologyManager.listener.event(topologyEventOf(event));
397 }
398
399
400 /**
Aaron Kruglikove1200592015-06-29 16:31:23 -0700401 * @param numDevices the number of devices to populate.
402 * @param numInfraPorts the number of ports to be set as infrastructure on each device, numbered base 0, ports 0
403 * through numInfraPorts - 1
404 */
405 private void defaultPopulator(int numDevices, int numInfraPorts) {
406 for (int device = 0; device < numDevices; device++) {
407 String str = Integer.toString(device);
408 Device deviceToAdd = NetTestTools.device(str);
409 devices.put(deviceToAdd.id(), deviceToAdd);
410 for (int port = 1; port <= numInfraPorts; port++) {
411 infrastructurePorts.add(NetTestTools.connectPoint(str, port));
412 }
413 }
414 }
415
416 /**
417 * Adds the specified device with the specified number of edge ports so long as it is less than the total ports.
418 *
419 * @param device The device to be added
420 * @param deviceName The name given to generate the devices DID
421 * @param numInfraPorts The number of ports to be added numbered 1 ... numInfraPorts
422 */
423 private void addDevice(Device device, String deviceName, int numInfraPorts) {
424 if (!devices.keySet().contains(device.id())) {
425 devices.put(device.id(), device);
426 for (int i = 1; i <= numInfraPorts && i <= totalPorts; i++) {
427 infrastructurePorts.add(NetTestTools.connectPoint(deviceName, i));
428 }
429 }
430 }
431
432 private void removeDevice(Device device) {
433 devices.remove(device.id());
434 }
435
436 private void removeInfraPort(ConnectPoint port) {
437 infrastructurePorts.remove(port);
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700438 }
439
440 private class TestTopologyManager extends TopologyServiceAdapter {
441 private TopologyListener listener;
442 private Set<ConnectPoint> infrastructurePorts;
443
Aaron Kruglikove1200592015-06-29 16:31:23 -0700444 public TestTopologyManager(Set<ConnectPoint> infrastructurePorts) {
445 this.infrastructurePorts = infrastructurePorts;
446 }
447
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700448 @Override
449 public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) {
450 return infrastructurePorts.contains(connectPoint);
451 }
452
453 @Override
454 public void addListener(TopologyListener listener) {
455 this.listener = listener;
456 }
457
458 @Override
459 public void removeListener(TopologyListener listener) {
460 this.listener = null;
461 }
462 }
463
464 private class TestDeviceManager extends DeviceServiceAdapter {
alshabib8a4a6002015-11-25 14:31:16 -0800465 private DeviceListener listener;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700466
Aaron Kruglikove1200592015-06-29 16:31:23 -0700467 private Map<DeviceId, Device> devices;
468
469 public TestDeviceManager(Map<DeviceId, Device> devices) {
470 this.devices = devices;
471 }
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700472
473 @Override
474 public boolean isAvailable(DeviceId deviceId) {
Aaron Kruglikove1200592015-06-29 16:31:23 -0700475 for (DeviceId id : devices.keySet()) {
476 if (id.equals(deviceId)) {
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700477 return true;
478 }
479 }
480 return false;
481 }
482
483 @Override
484 public List<Port> getPorts(DeviceId deviceId) {
Aaron Kruglikove1200592015-06-29 16:31:23 -0700485 List<Port> ports = new ArrayList<>();
486 Device device = devices.get(deviceId);
487 if (device == null && !alwaysReturnPorts) {
488 return ports;
489 }
490 for (int portNum = 1; portNum <= totalPorts; portNum++) {
491 //String is generated using 'of:' + the passed name, this creates a
492 ports.add(new DefaultPort(device, PortNumber.portNumber(portNum), true));
493 }
494 return ports;
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700495 }
496
497 @Override
498 public Iterable<Device> getAvailableDevices() {
Aaron Kruglikove1200592015-06-29 16:31:23 -0700499 return devices.values();
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700500 }
alshabib8a4a6002015-11-25 14:31:16 -0800501
502
503 @Override
504 public void addListener(DeviceListener listener) {
505 this.listener = listener;
506 }
507
508 @Override
509 public void removeListener(DeviceListener listener) {
510 this.listener = null;
511 }
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700512 }
513
514 private class TestPacketManager extends PacketServiceAdapter {
Aaron Kruglikove1200592015-06-29 16:31:23 -0700515 @Override
516 public void emit(OutboundPacket packet) {
517 packets.add(packet);
518 }
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700519 }
520
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700521 private class TestListener implements EdgePortListener {
522 private List<EdgePortEvent> events;
523
Aaron Kruglikove1200592015-06-29 16:31:23 -0700524 public TestListener(List<EdgePortEvent> events) {
525 this.events = events;
526 }
527
Thomas Vachuskaf3ed6552015-06-29 13:56:03 -0700528 @Override
529 public void event(EdgePortEvent event) {
530 events.add(event);
531 }
532 }
533}