Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 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 | */ |
| 16 | package org.onosproject.net.edgeservice.impl; |
| 17 | |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Lists; |
| 19 | import com.google.common.collect.Maps; |
| 20 | import com.google.common.collect.Sets; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 21 | import org.junit.After; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onosproject.common.event.impl.TestEventDispatcher; |
| 25 | import org.onosproject.net.ConnectPoint; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 26 | import org.onosproject.net.DefaultPort; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 27 | import org.onosproject.net.Device; |
| 28 | import org.onosproject.net.DeviceId; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 29 | import org.onosproject.net.NetTestTools; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 30 | import org.onosproject.net.Port; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 31 | import org.onosproject.net.PortNumber; |
| 32 | import org.onosproject.net.device.DeviceEvent; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 33 | import org.onosproject.net.device.DeviceListener; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 34 | import org.onosproject.net.device.DeviceServiceAdapter; |
| 35 | import org.onosproject.net.edge.EdgePortEvent; |
| 36 | import org.onosproject.net.edge.EdgePortListener; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 37 | import org.onosproject.net.flow.TrafficTreatment; |
| 38 | import org.onosproject.net.link.LinkEvent; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 39 | import org.onosproject.net.link.LinkListener; |
| 40 | import org.onosproject.net.link.LinkServiceAdapter; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 41 | import org.onosproject.net.packet.OutboundPacket; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 42 | import org.onosproject.net.packet.PacketServiceAdapter; |
| 43 | import org.onosproject.net.topology.Topology; |
| 44 | import org.onosproject.net.topology.TopologyListener; |
| 45 | import org.onosproject.net.topology.TopologyServiceAdapter; |
| 46 | |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 47 | import java.nio.ByteBuffer; |
| 48 | import java.util.ArrayList; |
| 49 | import java.util.Iterator; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 50 | import java.util.List; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 51 | import java.util.Map; |
| 52 | import java.util.Optional; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 53 | import java.util.Set; |
| 54 | |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 55 | import static org.junit.Assert.*; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 56 | import static org.onosproject.net.NetTestTools.injectEventDispatcher; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 57 | import static org.onosproject.net.device.DeviceEvent.Type.*; |
| 58 | import static org.onosproject.net.edge.EdgePortEvent.Type.EDGE_PORT_ADDED; |
| 59 | import static org.onosproject.net.edge.EdgePortEvent.Type.EDGE_PORT_REMOVED; |
| 60 | import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED; |
| 61 | import static org.onosproject.net.link.LinkEvent.Type.LINK_REMOVED; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 62 | |
| 63 | /** |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 64 | * Test of the edge port manager. Each device has ports '0' through 'numPorts - 1' |
| 65 | * as specified by the variable 'numPorts'. |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 66 | */ |
| 67 | public class EdgeManagerTest { |
| 68 | |
| 69 | private EdgeManager mgr; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 70 | private int totalPorts = 10; |
| 71 | private boolean alwaysReturnPorts = false; |
| 72 | private final Set<ConnectPoint> infrastructurePorts = Sets.newConcurrentHashSet(); |
| 73 | private List<EdgePortEvent> events = Lists.newArrayList(); |
| 74 | private final Map<DeviceId, Device> devices = Maps.newConcurrentMap(); |
| 75 | private Set<OutboundPacket> packets = Sets.newConcurrentHashSet(); |
| 76 | private final EdgePortListener testListener = new TestListener(events); |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 77 | private TestLinkManager testLinkManager; |
| 78 | private TestDeviceManager testDeviceManager; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 79 | private TestTopologyManager testTopologyManager; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 80 | |
| 81 | @Before |
| 82 | public void setUp() { |
| 83 | mgr = new EdgeManager(); |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 84 | injectEventDispatcher(mgr, new TestEventDispatcher()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 85 | testTopologyManager = new TestTopologyManager(infrastructurePorts); |
| 86 | mgr.topologyService = testTopologyManager; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 87 | testDeviceManager = new TestDeviceManager(devices); |
| 88 | mgr.deviceService = testDeviceManager; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 89 | mgr.packetService = new TestPacketManager(); |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 90 | testLinkManager = new TestLinkManager(); |
| 91 | mgr.linkService = testLinkManager; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 92 | mgr.activate(); |
| 93 | mgr.addListener(testListener); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 94 | |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 97 | |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 98 | @After |
| 99 | public void tearDown() { |
| 100 | mgr.removeListener(testListener); |
| 101 | mgr.deactivate(); |
| 102 | } |
| 103 | |
| 104 | @Test |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 105 | 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 Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 113 | assertFalse("no ports expected", mgr.getEdgePoints().iterator().hasNext()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 114 | |
| 115 | assertFalse("Expected isEdge to return false", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 116 | mgr.isEdgePoint(NetTestTools.connectPoint(Integer.toString(1), 1))); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 117 | |
| 118 | removeInfraPort(NetTestTools.connectPoint(Integer.toString(1), 1)); |
| 119 | assertTrue("Expected isEdge to return false", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 120 | mgr.isEdgePoint(NetTestTools.connectPoint(Integer.toString(1), 1))); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Test |
| 124 | public void testLinkUpdates() { |
| 125 | //Setup |
| 126 | ConnectPoint testPoint, referencePoint; |
| 127 | |
| 128 | //Testing link removal |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 129 | testLinkManager.listener.event(new LinkEvent(LINK_REMOVED, NetTestTools.link("a", 1, "b", 2))); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 130 | |
| 131 | assertTrue("The list contained an unexpected number of events", events.size() == 2); |
| 132 | assertTrue("The first element is of the wrong type.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 133 | events.get(0).type() == EDGE_PORT_ADDED); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 134 | |
| 135 | testPoint = events.get(0).subject(); |
| 136 | referencePoint = NetTestTools.connectPoint("a", 1); |
| 137 | assertTrue("The port numbers of the first element are incorrect", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 138 | testPoint.port().toLong() == referencePoint.port().toLong()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 139 | assertTrue("The device id of the first element is incorrect.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 140 | testPoint.deviceId().equals(referencePoint.deviceId())); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 141 | |
| 142 | testPoint = events.get(1).subject(); |
| 143 | referencePoint = NetTestTools.connectPoint("b", 2); |
| 144 | assertTrue("The port numbers of the second element are incorrect", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 145 | testPoint.port().toLong() == referencePoint.port().toLong()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 146 | assertTrue("The device id of the second element is incorrect.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 147 | testPoint.deviceId().equals(referencePoint.deviceId())); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 148 | |
| 149 | //Rebroadcast event to ensure it results in no additional events |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 150 | testLinkManager.listener.event(new LinkEvent(LINK_REMOVED, NetTestTools.link("a", 1, "b", 2))); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 151 | assertTrue("The list contained an unexpected number of events", events.size() == 2); |
| 152 | |
| 153 | //Testing link adding when links to remove exist |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 154 | events.clear(); |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 155 | testLinkManager.listener.event(new LinkEvent(LINK_ADDED, NetTestTools.link("a", 1, "b", 2))); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 156 | |
| 157 | assertTrue("The list contained an unexpected number of events", events.size() == 2); |
| 158 | assertTrue("The first element is of the wrong type.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 159 | events.get(0).type() == EDGE_PORT_REMOVED); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 160 | assertTrue("The second element is of the wrong type.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 161 | events.get(1).type() == EDGE_PORT_REMOVED); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 162 | |
| 163 | testPoint = events.get(0).subject(); |
| 164 | referencePoint = NetTestTools.connectPoint("a", 1); |
| 165 | assertTrue("The port numbers of the first element are incorrect", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 166 | testPoint.port().toLong() == referencePoint.port().toLong()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 167 | assertTrue("The device id of the first element is incorrect.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 168 | testPoint.deviceId().equals(referencePoint.deviceId())); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 169 | |
| 170 | testPoint = events.get(1).subject(); |
| 171 | referencePoint = NetTestTools.connectPoint("b", 2); |
| 172 | assertTrue("The port numbers of the second element are incorrect", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 173 | testPoint.port().toLong() == referencePoint.port().toLong()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 174 | assertTrue("The device id of the second element is incorrect.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 175 | testPoint.deviceId().equals(referencePoint.deviceId())); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 176 | |
| 177 | //Apparent duplicate of previous method tests removal when the elements have already been removed |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 178 | events.clear(); |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 179 | testLinkManager.listener.event(new LinkEvent(LINK_ADDED, NetTestTools.link("a", 1, "b", 2))); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 180 | assertTrue("The list should contain no events, the removed elements don't exist.", events.size() == 0); |
| 181 | } |
| 182 | |
| 183 | @Test |
| 184 | public void testDeviceUpdates() { |
| 185 | //Setup |
| 186 | |
| 187 | Device referenceDevice; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 188 | DeviceEvent event; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 189 | int numDevices = 10; |
| 190 | int numInfraPorts = 5; |
| 191 | totalPorts = 10; |
| 192 | defaultPopulator(numDevices, numInfraPorts); |
| 193 | |
| 194 | //Test response to device added events |
| 195 | referenceDevice = NetTestTools.device("1"); |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 196 | event = new DeviceEvent(DEVICE_ADDED, referenceDevice, |
| 197 | new DefaultPort(referenceDevice, PortNumber.portNumber(1), true)); |
| 198 | testDeviceManager.listener.event(event); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 199 | |
| 200 | //Check that ports were populated correctly |
| 201 | assertTrue("Unexpected number of new ports added", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 202 | mgr.deviceService.getPorts(NetTestTools.did("1")).size() == 10); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 203 | |
| 204 | //Check that of the ten ports the half that are infrastructure ports aren't added |
| 205 | assertEquals("Unexpected number of new edge ports added", (totalPorts - numInfraPorts), events.size()); |
| 206 | |
| 207 | for (int index = 0; index < numInfraPorts; index++) { |
| 208 | assertTrue("Unexpected type of event", events.get(index).type() == EDGE_PORT_ADDED); |
| 209 | } |
| 210 | //Names here are irrelevant, the first 5 ports are populated as infrastructure, 6-10 are edge |
| 211 | for (int index = 0; index < events.size(); index++) { |
| 212 | assertEquals("Port added had unexpected port number.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 213 | events.get(index).subject().port(), |
| 214 | NetTestTools.connectPoint("a", index + numInfraPorts + 1).port()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 215 | } |
| 216 | events.clear(); |
| 217 | |
| 218 | //Repost the event to test repeated posts |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 219 | testDeviceManager.listener.event(event); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 220 | assertEquals("The redundant notification should not have created additional notifications.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 221 | 0, events.size()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 222 | //Calculate the size of the returned iterable of edge points. |
| 223 | Iterable<ConnectPoint> pts = mgr.getEdgePoints(); |
| 224 | Iterator pointIterator = pts.iterator(); |
| 225 | int count = 0; |
| 226 | for (; pointIterator.hasNext(); count++) { |
| 227 | pointIterator.next(); |
| 228 | } |
| 229 | assertEquals("Unexpected number of edge points", totalPorts - numInfraPorts, count); |
| 230 | //Testing device removal |
| 231 | events.clear(); |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 232 | event = (new DeviceEvent(DEVICE_REMOVED, referenceDevice, |
| 233 | new DefaultPort(referenceDevice, PortNumber.portNumber(1), true))); |
| 234 | testDeviceManager.listener.event(event); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 235 | |
| 236 | assertEquals("There should be five new events from removal of edge points", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 237 | totalPorts - numInfraPorts, events.size()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 238 | for (int index = 0; index < events.size(); index++) { |
| 239 | //Assert that the correct port numbers were removed in the correct order |
| 240 | assertEquals("Port removed had unexpected port number.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 241 | events.get(index).subject().port(), |
| 242 | (NetTestTools.connectPoint("a", index + numInfraPorts + 1).port())); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 243 | //Assert that the events are of the correct type |
| 244 | assertEquals("Unexpected type of event", events.get(index).type(), EDGE_PORT_REMOVED); |
| 245 | } |
| 246 | events.clear(); |
| 247 | //Rebroadcast event to check that it triggers no new behavior |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 248 | testDeviceManager.listener.event(event); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 249 | assertEquals("Rebroadcast of removal event should not produce additional events", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 250 | 0, events.size()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 251 | |
| 252 | //Testing device status change, changed from unavailable to available |
| 253 | events.clear(); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 254 | //Make sure that the devicemanager shows the device as available. |
| 255 | addDevice(referenceDevice, "1", 5); |
| 256 | devices.put(referenceDevice.id(), referenceDevice); |
| 257 | |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 258 | event = new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, referenceDevice); |
| 259 | testDeviceManager.listener.event(event); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 260 | //An earlier setup set half of the reference device ports to infrastructure |
| 261 | assertEquals("An unexpected number of events were generated.", totalPorts - numInfraPorts, events.size()); |
| 262 | for (int i = 0; i < 5; i++) { |
| 263 | assertEquals("The event was not of the right type", events.get(i).type(), EDGE_PORT_ADDED); |
| 264 | } |
| 265 | events.clear(); |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 266 | testDeviceManager.listener.event(event); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 267 | assertEquals("No events should have been generated for a set of existing ports.", 0, events.size()); |
| 268 | |
| 269 | //Test removal when state changes when the device becomes unavailable |
| 270 | |
| 271 | //Ensure that the deviceManager shows the device as unavailable |
| 272 | removeDevice(referenceDevice); |
| 273 | /*This variable copies the behavior of the topology by returning ports attached to an unavailable device |
| 274 | //this behavior is necessary for the following event to execute properly, if these statements are removed |
| 275 | no events will be generated since no ports will be provided in getPorts() to EdgeManager. |
| 276 | */ |
| 277 | alwaysReturnPorts = true; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 278 | testDeviceManager.listener.event(event); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 279 | alwaysReturnPorts = false; |
| 280 | assertEquals("An unexpected number of events were created.", totalPorts - numInfraPorts, events.size()); |
| 281 | for (int i = 0; i < 5; i++) { |
| 282 | EdgePortEvent edgeEvent = events.get(i); |
| 283 | assertEquals("The event is of an unexpected type.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 284 | EdgePortEvent.Type.EDGE_PORT_REMOVED, edgeEvent.type()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 285 | assertEquals("The event pertains to an unexpected port", PortNumber.portNumber(i + numInfraPorts + 1), |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 286 | edgeEvent.subject().port()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
| 290 | @Test |
| 291 | public void testInternalCache() { |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 292 | int numDevices = 10; |
| 293 | //Number of infrastructure ports per device |
| 294 | int numPorts = 5; |
| 295 | //Total ports per device when requesting all devices |
| 296 | totalPorts = 10; |
| 297 | defaultPopulator(numDevices, numPorts); |
| 298 | for (int i = 0; i < numDevices; i++) { |
| 299 | Device newDevice = NetTestTools.device(Integer.toString(i)); |
| 300 | devices.put(newDevice.id(), newDevice); |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 301 | testDeviceManager.listener.event(new DeviceEvent(DEVICE_ADDED, newDevice)); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 302 | } |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 303 | //Check all ports have correct designations |
| 304 | ConnectPoint testPoint; |
| 305 | for (int deviceNum = 0; deviceNum < numDevices; deviceNum++) { |
| 306 | for (int portNum = 1; portNum <= totalPorts; portNum++) { |
| 307 | testPoint = NetTestTools.connectPoint(Integer.toString(deviceNum), portNum); |
| 308 | if (portNum <= numPorts) { |
| 309 | assertFalse("This should not be an edge point", mgr.isEdgePoint(testPoint)); |
| 310 | } else { |
| 311 | assertTrue("This should be an edge point", mgr.isEdgePoint(testPoint)); |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | int count = 0; |
| 316 | for (ConnectPoint ignored : mgr.getEdgePoints()) { |
| 317 | count++; |
| 318 | } |
| 319 | assertEquals("There are an unexpeceted number of edge points returned.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 320 | (totalPorts - numPorts) * numDevices, count); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 321 | for (int deviceNumber = 0; deviceNumber < numDevices; deviceNumber++) { |
| 322 | count = 0; |
| 323 | for (ConnectPoint ignored : mgr.getEdgePoints(NetTestTools.did("1"))) { |
| 324 | count++; |
| 325 | } |
| 326 | assertEquals("This element has an unexpected number of edge points.", (totalPorts - numPorts), count); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | |
| 331 | @Test |
| 332 | public void testEmit() { |
| 333 | byte[] arr = new byte[10]; |
| 334 | Device referenceDevice; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 335 | DeviceEvent event; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 336 | int numDevices = 10; |
| 337 | int numInfraPorts = 5; |
| 338 | totalPorts = 10; |
| 339 | defaultPopulator(numDevices, numInfraPorts); |
| 340 | for (byte byteIndex = 0; byteIndex < arr.length; byteIndex++) { |
| 341 | arr[byteIndex] = byteIndex; |
| 342 | } |
| 343 | for (int i = 0; i < numDevices; i++) { |
| 344 | referenceDevice = NetTestTools.device(Integer.toString(i)); |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 345 | testDeviceManager.listener.event(new DeviceEvent(DEVICE_ADDED, referenceDevice, |
| 346 | new DefaultPort(referenceDevice, |
| 347 | PortNumber.portNumber(1), |
| 348 | true))); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 349 | } |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 350 | |
| 351 | mgr.emitPacket(ByteBuffer.wrap(arr), Optional.<TrafficTreatment>empty()); |
| 352 | |
| 353 | assertEquals("There were an unexpected number of emitted packets", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 354 | (totalPorts - numInfraPorts) * numDevices, packets.size()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 355 | Iterator<OutboundPacket> packetIter = packets.iterator(); |
| 356 | OutboundPacket packet; |
| 357 | while (packetIter.hasNext()) { |
| 358 | packet = packetIter.next(); |
| 359 | assertEquals("The packet had an incorrect payload.", arr, packet.data().array()); |
| 360 | } |
| 361 | //Start testing emission to a specific device |
| 362 | packets.clear(); |
| 363 | mgr.emitPacket(NetTestTools.did(Integer.toString(1)), ByteBuffer.wrap(arr), Optional.<TrafficTreatment>empty()); |
| 364 | |
| 365 | assertEquals("Unexpected number of outbound packets were emitted.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 366 | totalPorts - numInfraPorts, packets.size()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 367 | packetIter = packets.iterator(); |
| 368 | while (packetIter.hasNext()) { |
| 369 | packet = packetIter.next(); |
| 370 | assertEquals("The packet had an incorrect payload", arr, packet.data().array()); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | |
| 375 | /** |
| 376 | * @param numDevices the number of devices to populate. |
| 377 | * @param numInfraPorts the number of ports to be set as infrastructure on each device, numbered base 0, ports 0 |
| 378 | * through numInfraPorts - 1 |
| 379 | */ |
| 380 | private void defaultPopulator(int numDevices, int numInfraPorts) { |
| 381 | for (int device = 0; device < numDevices; device++) { |
| 382 | String str = Integer.toString(device); |
| 383 | Device deviceToAdd = NetTestTools.device(str); |
| 384 | devices.put(deviceToAdd.id(), deviceToAdd); |
| 385 | for (int port = 1; port <= numInfraPorts; port++) { |
| 386 | infrastructurePorts.add(NetTestTools.connectPoint(str, port)); |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * Adds the specified device with the specified number of edge ports so long as it is less than the total ports. |
| 393 | * |
| 394 | * @param device The device to be added |
| 395 | * @param deviceName The name given to generate the devices DID |
| 396 | * @param numInfraPorts The number of ports to be added numbered 1 ... numInfraPorts |
| 397 | */ |
| 398 | private void addDevice(Device device, String deviceName, int numInfraPorts) { |
| 399 | if (!devices.keySet().contains(device.id())) { |
| 400 | devices.put(device.id(), device); |
| 401 | for (int i = 1; i <= numInfraPorts && i <= totalPorts; i++) { |
| 402 | infrastructurePorts.add(NetTestTools.connectPoint(deviceName, i)); |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | private void removeDevice(Device device) { |
| 408 | devices.remove(device.id()); |
| 409 | } |
| 410 | |
| 411 | private void removeInfraPort(ConnectPoint port) { |
| 412 | infrastructurePorts.remove(port); |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | private class TestTopologyManager extends TopologyServiceAdapter { |
| 416 | private TopologyListener listener; |
| 417 | private Set<ConnectPoint> infrastructurePorts; |
| 418 | |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 419 | public TestTopologyManager(Set<ConnectPoint> infrastructurePorts) { |
| 420 | this.infrastructurePorts = infrastructurePorts; |
| 421 | } |
| 422 | |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 423 | @Override |
| 424 | public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) { |
| 425 | return infrastructurePorts.contains(connectPoint); |
| 426 | } |
| 427 | |
| 428 | @Override |
| 429 | public void addListener(TopologyListener listener) { |
| 430 | this.listener = listener; |
| 431 | } |
| 432 | |
| 433 | @Override |
| 434 | public void removeListener(TopologyListener listener) { |
| 435 | this.listener = null; |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | private class TestDeviceManager extends DeviceServiceAdapter { |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 440 | private DeviceListener listener; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 441 | |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 442 | private Map<DeviceId, Device> devices; |
| 443 | |
| 444 | public TestDeviceManager(Map<DeviceId, Device> devices) { |
| 445 | this.devices = devices; |
| 446 | } |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 447 | |
| 448 | @Override |
| 449 | public boolean isAvailable(DeviceId deviceId) { |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 450 | for (DeviceId id : devices.keySet()) { |
| 451 | if (id.equals(deviceId)) { |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 452 | return true; |
| 453 | } |
| 454 | } |
| 455 | return false; |
| 456 | } |
| 457 | |
| 458 | @Override |
| 459 | public List<Port> getPorts(DeviceId deviceId) { |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 460 | List<Port> ports = new ArrayList<>(); |
| 461 | Device device = devices.get(deviceId); |
| 462 | if (device == null && !alwaysReturnPorts) { |
| 463 | return ports; |
| 464 | } |
| 465 | for (int portNum = 1; portNum <= totalPorts; portNum++) { |
| 466 | //String is generated using 'of:' + the passed name, this creates a |
| 467 | ports.add(new DefaultPort(device, PortNumber.portNumber(portNum), true)); |
| 468 | } |
| 469 | return ports; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | @Override |
| 473 | public Iterable<Device> getAvailableDevices() { |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 474 | return devices.values(); |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 475 | } |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 476 | |
| 477 | |
| 478 | @Override |
| 479 | public void addListener(DeviceListener listener) { |
| 480 | this.listener = listener; |
| 481 | } |
| 482 | |
| 483 | @Override |
| 484 | public void removeListener(DeviceListener listener) { |
| 485 | this.listener = null; |
| 486 | } |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | private class TestPacketManager extends PacketServiceAdapter { |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 490 | @Override |
| 491 | public void emit(OutboundPacket packet) { |
| 492 | packets.add(packet); |
| 493 | } |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 494 | } |
| 495 | |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 496 | private class TestLinkManager extends LinkServiceAdapter { |
| 497 | private LinkListener listener; |
| 498 | |
| 499 | @Override |
| 500 | public void addListener(LinkListener listener) { |
| 501 | this.listener = listener; |
| 502 | } |
| 503 | } |
| 504 | |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 505 | private class TestListener implements EdgePortListener { |
| 506 | private List<EdgePortEvent> events; |
| 507 | |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 508 | public TestListener(List<EdgePortEvent> events) { |
| 509 | this.events = events; |
| 510 | } |
| 511 | |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 512 | @Override |
| 513 | public void event(EdgePortEvent event) { |
| 514 | events.add(event); |
| 515 | } |
| 516 | } |
| 517 | } |