Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 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 | |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
| 20 | import com.google.common.collect.Maps; |
| 21 | import com.google.common.collect.Sets; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 22 | import org.junit.After; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
| 25 | import org.onosproject.common.event.impl.TestEventDispatcher; |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 26 | import org.onosproject.event.Event; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 27 | import org.onosproject.net.ConnectPoint; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 28 | import org.onosproject.net.DefaultPort; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 29 | import org.onosproject.net.Device; |
| 30 | import org.onosproject.net.DeviceId; |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame^] | 31 | import org.onosproject.net.Link; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 32 | import org.onosproject.net.NetTestTools; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 33 | import org.onosproject.net.Port; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 34 | import org.onosproject.net.PortNumber; |
| 35 | import org.onosproject.net.device.DeviceEvent; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 36 | import org.onosproject.net.device.DeviceListener; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 37 | import org.onosproject.net.device.DeviceServiceAdapter; |
| 38 | import org.onosproject.net.edge.EdgePortEvent; |
| 39 | import org.onosproject.net.edge.EdgePortListener; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 40 | import org.onosproject.net.link.LinkEvent; |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame^] | 41 | import org.onosproject.net.link.LinkListener; |
| 42 | import org.onosproject.net.link.LinkServiceAdapter; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 43 | import org.onosproject.net.packet.OutboundPacket; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 44 | import org.onosproject.net.packet.PacketServiceAdapter; |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 45 | import org.onosproject.net.topology.TopologyEvent; |
| 46 | import org.onosproject.net.topology.TopologyEvent.Type; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 47 | |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 48 | import java.nio.ByteBuffer; |
| 49 | import java.util.ArrayList; |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame^] | 50 | import java.util.Collections; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 51 | import java.util.Iterator; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 52 | import java.util.List; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 53 | import java.util.Map; |
| 54 | import java.util.Optional; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 55 | import java.util.Set; |
| 56 | |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame^] | 57 | import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
| 58 | import static org.hamcrest.Matchers.is; |
| 59 | import static org.junit.Assert.assertEquals; |
| 60 | import static org.junit.Assert.assertFalse; |
| 61 | import static org.junit.Assert.assertThat; |
| 62 | import static org.junit.Assert.assertTrue; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 63 | import static org.onosproject.net.NetTestTools.injectEventDispatcher; |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame^] | 64 | import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED; |
| 65 | import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED; |
| 66 | import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_REMOVED; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 67 | import static org.onosproject.net.edge.EdgePortEvent.Type.EDGE_PORT_ADDED; |
| 68 | import static org.onosproject.net.edge.EdgePortEvent.Type.EDGE_PORT_REMOVED; |
| 69 | import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED; |
| 70 | import static org.onosproject.net.link.LinkEvent.Type.LINK_REMOVED; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 71 | |
| 72 | /** |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 73 | * Test of the edge port manager. Each device has ports '0' through 'numPorts - 1' |
| 74 | * as specified by the variable 'numPorts'. |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 75 | */ |
| 76 | public class EdgeManagerTest { |
| 77 | |
| 78 | private EdgeManager mgr; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 79 | 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); |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 86 | private TestDeviceManager testDeviceManager; |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame^] | 87 | private TestLinkService testLinkService; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 88 | |
| 89 | @Before |
| 90 | public void setUp() { |
| 91 | mgr = new EdgeManager(); |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 92 | injectEventDispatcher(mgr, new TestEventDispatcher()); |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 93 | testDeviceManager = new TestDeviceManager(devices); |
| 94 | mgr.deviceService = testDeviceManager; |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame^] | 95 | |
| 96 | testLinkService = new TestLinkService(); |
| 97 | mgr.linkService = testLinkService; |
| 98 | |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 99 | 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 Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 111 | 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 Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 119 | assertFalse("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 | removeInfraPort(NetTestTools.connectPoint(Integer.toString(1), 1)); |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame^] | 123 | postTopologyEvent(new LinkEvent(LINK_REMOVED, NetTestTools.link(Integer.toString(1), 1, "b", 2))); |
| 124 | assertTrue("Expected isEdge to return true", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 125 | mgr.isEdgePoint(NetTestTools.connectPoint(Integer.toString(1), 1))); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Test |
| 129 | public void testLinkUpdates() { |
| 130 | //Setup |
| 131 | ConnectPoint testPoint, referencePoint; |
| 132 | |
| 133 | //Testing link removal |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 134 | postTopologyEvent(new LinkEvent(LINK_REMOVED, NetTestTools.link("a", 1, "b", 2))); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 135 | |
| 136 | assertTrue("The list contained an unexpected number of events", events.size() == 2); |
| 137 | assertTrue("The first element is of the wrong type.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 138 | events.get(0).type() == EDGE_PORT_ADDED); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 139 | |
| 140 | testPoint = events.get(0).subject(); |
| 141 | referencePoint = NetTestTools.connectPoint("a", 1); |
| 142 | assertTrue("The port numbers of the first element are incorrect", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 143 | testPoint.port().toLong() == referencePoint.port().toLong()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 144 | assertTrue("The device id of the first element is incorrect.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 145 | testPoint.deviceId().equals(referencePoint.deviceId())); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 146 | |
| 147 | testPoint = events.get(1).subject(); |
| 148 | referencePoint = NetTestTools.connectPoint("b", 2); |
| 149 | assertTrue("The port numbers of the second element are incorrect", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 150 | testPoint.port().toLong() == referencePoint.port().toLong()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 151 | assertTrue("The device id of the second element is incorrect.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 152 | testPoint.deviceId().equals(referencePoint.deviceId())); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 153 | |
| 154 | //Rebroadcast event to ensure it results in no additional events |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 155 | postTopologyEvent(new LinkEvent(LINK_REMOVED, NetTestTools.link("a", 1, "b", 2))); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 156 | assertTrue("The list contained an unexpected number of events", events.size() == 2); |
| 157 | |
| 158 | //Testing link adding when links to remove exist |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 159 | events.clear(); |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 160 | postTopologyEvent(new LinkEvent(LINK_ADDED, NetTestTools.link("a", 1, "b", 2))); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 161 | |
| 162 | assertTrue("The list contained an unexpected number of events", events.size() == 2); |
| 163 | assertTrue("The first element is of the wrong type.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 164 | events.get(0).type() == EDGE_PORT_REMOVED); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 165 | assertTrue("The second element is of the wrong type.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 166 | events.get(1).type() == EDGE_PORT_REMOVED); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 167 | |
| 168 | testPoint = events.get(0).subject(); |
| 169 | referencePoint = NetTestTools.connectPoint("a", 1); |
| 170 | assertTrue("The port numbers of the first element are incorrect", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 171 | testPoint.port().toLong() == referencePoint.port().toLong()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 172 | assertTrue("The device id of the first element is incorrect.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 173 | testPoint.deviceId().equals(referencePoint.deviceId())); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 174 | |
| 175 | testPoint = events.get(1).subject(); |
| 176 | referencePoint = NetTestTools.connectPoint("b", 2); |
| 177 | assertTrue("The port numbers of the second element are incorrect", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 178 | testPoint.port().toLong() == referencePoint.port().toLong()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 179 | assertTrue("The device id of the second element is incorrect.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 180 | testPoint.deviceId().equals(referencePoint.deviceId())); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 181 | |
| 182 | //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] | 183 | events.clear(); |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 184 | postTopologyEvent(new LinkEvent(LINK_ADDED, NetTestTools.link("a", 1, "b", 2))); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 185 | assertTrue("The list should contain no events, the removed elements don't exist.", events.size() == 0); |
| 186 | } |
| 187 | |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 188 | |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 189 | @Test |
| 190 | public void testDeviceUpdates() { |
| 191 | //Setup |
| 192 | |
| 193 | Device referenceDevice; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 194 | DeviceEvent event; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 195 | int numDevices = 10; |
| 196 | int numInfraPorts = 5; |
| 197 | totalPorts = 10; |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame^] | 198 | |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 199 | defaultPopulator(numDevices, numInfraPorts); |
| 200 | |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame^] | 201 | events.clear(); |
| 202 | |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 203 | //Test response to device added events |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame^] | 204 | |
| 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 | } |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 210 | event = new DeviceEvent(DEVICE_ADDED, referenceDevice, |
| 211 | new DefaultPort(referenceDevice, PortNumber.portNumber(1), true)); |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 212 | postTopologyEvent(event); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 213 | |
| 214 | //Check that ports were populated correctly |
| 215 | assertTrue("Unexpected number of new ports added", |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame^] | 216 | mgr.deviceService.getPorts(NetTestTools.did("11")).size() == 10); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 217 | |
| 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.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 227 | events.get(index).subject().port(), |
| 228 | NetTestTools.connectPoint("a", index + numInfraPorts + 1).port()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 229 | } |
| 230 | events.clear(); |
| 231 | |
| 232 | //Repost the event to test repeated posts |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 233 | postTopologyEvent(event); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 234 | assertEquals("The redundant notification should not have created additional notifications.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 235 | 0, events.size()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 236 | //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 Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame^] | 243 | assertEquals("Unexpected number of edge points", (numDevices + 1) * numInfraPorts, count); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 244 | //Testing device removal |
| 245 | events.clear(); |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 246 | event = (new DeviceEvent(DEVICE_REMOVED, referenceDevice, |
| 247 | new DefaultPort(referenceDevice, PortNumber.portNumber(1), true))); |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 248 | postTopologyEvent(event); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 249 | |
| 250 | assertEquals("There should be five new events from removal of edge points", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 251 | totalPorts - numInfraPorts, events.size()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 252 | for (int index = 0; index < events.size(); index++) { |
| 253 | //Assert that the correct port numbers were removed in the correct order |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 254 | assertThat("Port removed had unexpected port number.", |
| 255 | events.get(index).subject().port().toLong(), |
| 256 | is(greaterThanOrEqualTo((long) numInfraPorts))); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 257 | //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 HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 262 | postTopologyEvent(event); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 263 | assertEquals("Rebroadcast of removal event should not produce additional events", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 264 | 0, events.size()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 265 | |
| 266 | //Testing device status change, changed from unavailable to available |
| 267 | events.clear(); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 268 | //Make sure that the devicemanager shows the device as available. |
| 269 | addDevice(referenceDevice, "1", 5); |
| 270 | devices.put(referenceDevice.id(), referenceDevice); |
| 271 | |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 272 | event = new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, referenceDevice); |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 273 | postTopologyEvent(event); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 274 | //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 HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 280 | postTopologyEvent(event); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 281 | 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 Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame^] | 287 | // 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 Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 292 | alwaysReturnPorts = true; |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 293 | postTopologyEvent(event); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 294 | 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.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 299 | EdgePortEvent.Type.EDGE_PORT_REMOVED, edgeEvent.type()); |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 300 | assertThat("The event pertains to an unexpected port", |
| 301 | edgeEvent.subject().port().toLong(), |
| 302 | is(greaterThanOrEqualTo((long) numInfraPorts))); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 306 | |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 307 | @Test |
| 308 | public void testInternalCache() { |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 309 | 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 HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 318 | postTopologyEvent(new DeviceEvent(DEVICE_ADDED, newDevice)); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 319 | } |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 320 | //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.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 337 | (totalPorts - numPorts) * numDevices, count); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 338 | 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 Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 347 | @Test |
| 348 | public void testEmit() { |
| 349 | byte[] arr = new byte[10]; |
| 350 | Device referenceDevice; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 351 | DeviceEvent event; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 352 | 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 Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame^] | 361 | testDeviceManager.listener.event(new DeviceEvent(DEVICE_ADDED, referenceDevice, |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 362 | new DefaultPort(referenceDevice, |
| 363 | PortNumber.portNumber(1), |
| 364 | true))); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 365 | } |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 366 | |
Sho SHIMIZU | 21d0069 | 2016-08-15 11:15:28 -0700 | [diff] [blame] | 367 | mgr.emitPacket(ByteBuffer.wrap(arr), Optional.empty()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 368 | |
| 369 | assertEquals("There were an unexpected number of emitted packets", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 370 | (totalPorts - numInfraPorts) * numDevices, packets.size()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 371 | 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 SHIMIZU | 21d0069 | 2016-08-15 11:15:28 -0700 | [diff] [blame] | 379 | mgr.emitPacket(NetTestTools.did(Integer.toString(1)), ByteBuffer.wrap(arr), Optional.empty()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 380 | |
| 381 | assertEquals("Unexpected number of outbound packets were emitted.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 382 | totalPorts - numInfraPorts, packets.size()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 383 | 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 HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 392 | * 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 Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame^] | 408 | 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 HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | |
| 418 | /** |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 419 | * @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 Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame^] | 428 | testDeviceManager.listener.event(new DeviceEvent(DEVICE_ADDED, deviceToAdd)); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 429 | for (int port = 1; port <= numInfraPorts; port++) { |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame^] | 430 | testLinkService.listener.event(new LinkEvent(LINK_ADDED, NetTestTools.link(str, port, "other", 1))); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 431 | 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 Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 460 | private class TestDeviceManager extends DeviceServiceAdapter { |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 461 | private DeviceListener listener; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 462 | |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 463 | private Map<DeviceId, Device> devices; |
| 464 | |
| 465 | public TestDeviceManager(Map<DeviceId, Device> devices) { |
| 466 | this.devices = devices; |
| 467 | } |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 468 | |
| 469 | @Override |
| 470 | public boolean isAvailable(DeviceId deviceId) { |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 471 | for (DeviceId id : devices.keySet()) { |
| 472 | if (id.equals(deviceId)) { |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 473 | return true; |
| 474 | } |
| 475 | } |
| 476 | return false; |
| 477 | } |
| 478 | |
| 479 | @Override |
| 480 | public List<Port> getPorts(DeviceId deviceId) { |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 481 | 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 Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | @Override |
| 494 | public Iterable<Device> getAvailableDevices() { |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 495 | return devices.values(); |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 496 | } |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 497 | |
| 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 Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame^] | 510 | 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 Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 529 | private class TestPacketManager extends PacketServiceAdapter { |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 530 | @Override |
| 531 | public void emit(OutboundPacket packet) { |
| 532 | packets.add(packet); |
| 533 | } |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 536 | private class TestListener implements EdgePortListener { |
| 537 | private List<EdgePortEvent> events; |
| 538 | |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 539 | public TestListener(List<EdgePortEvent> events) { |
| 540 | this.events = events; |
| 541 | } |
| 542 | |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 543 | @Override |
| 544 | public void event(EdgePortEvent event) { |
| 545 | events.add(event); |
| 546 | } |
| 547 | } |
Sho SHIMIZU | 21d0069 | 2016-08-15 11:15:28 -0700 | [diff] [blame] | 548 | } |