Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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 | 37dca33 | 2016-11-14 18:43:22 -0800 | [diff] [blame] | 134 | devices.put(NetTestTools.did("a"), NetTestTools.device("a")); |
| 135 | devices.put(NetTestTools.did("b"), NetTestTools.device("b")); |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 136 | postTopologyEvent(new LinkEvent(LINK_REMOVED, NetTestTools.link("a", 1, "b", 2))); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 137 | |
Yuta HIGUCHI | 37dca33 | 2016-11-14 18:43:22 -0800 | [diff] [blame] | 138 | assertEquals("The list contained an unexpected number of events", |
| 139 | 2, events.size()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 140 | assertTrue("The first element is of the wrong type.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 141 | events.get(0).type() == EDGE_PORT_ADDED); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 142 | |
| 143 | testPoint = events.get(0).subject(); |
| 144 | referencePoint = NetTestTools.connectPoint("a", 1); |
| 145 | assertTrue("The port numbers of the first element are incorrect", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 146 | testPoint.port().toLong() == referencePoint.port().toLong()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 147 | assertTrue("The device id of the first element is incorrect.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 148 | testPoint.deviceId().equals(referencePoint.deviceId())); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 149 | |
| 150 | testPoint = events.get(1).subject(); |
| 151 | referencePoint = NetTestTools.connectPoint("b", 2); |
| 152 | assertTrue("The port numbers of the second element are incorrect", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 153 | testPoint.port().toLong() == referencePoint.port().toLong()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 154 | assertTrue("The device id of the second element is incorrect.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 155 | testPoint.deviceId().equals(referencePoint.deviceId())); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 156 | |
| 157 | //Rebroadcast event to ensure it results in no additional events |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 158 | postTopologyEvent(new LinkEvent(LINK_REMOVED, NetTestTools.link("a", 1, "b", 2))); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 159 | assertTrue("The list contained an unexpected number of events", events.size() == 2); |
| 160 | |
| 161 | //Testing link adding when links to remove exist |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 162 | events.clear(); |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 163 | postTopologyEvent(new LinkEvent(LINK_ADDED, NetTestTools.link("a", 1, "b", 2))); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 164 | |
| 165 | assertTrue("The list contained an unexpected number of events", events.size() == 2); |
| 166 | assertTrue("The first element is of the wrong type.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 167 | events.get(0).type() == EDGE_PORT_REMOVED); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 168 | assertTrue("The second element is of the wrong type.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 169 | events.get(1).type() == EDGE_PORT_REMOVED); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 170 | |
| 171 | testPoint = events.get(0).subject(); |
| 172 | referencePoint = NetTestTools.connectPoint("a", 1); |
| 173 | assertTrue("The port numbers of the first element are incorrect", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 174 | testPoint.port().toLong() == referencePoint.port().toLong()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 175 | assertTrue("The device id of the first element is incorrect.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 176 | testPoint.deviceId().equals(referencePoint.deviceId())); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 177 | |
| 178 | testPoint = events.get(1).subject(); |
| 179 | referencePoint = NetTestTools.connectPoint("b", 2); |
| 180 | assertTrue("The port numbers of the second element are incorrect", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 181 | testPoint.port().toLong() == referencePoint.port().toLong()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 182 | assertTrue("The device id of the second element is incorrect.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 183 | testPoint.deviceId().equals(referencePoint.deviceId())); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 184 | |
| 185 | //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] | 186 | events.clear(); |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 187 | postTopologyEvent(new LinkEvent(LINK_ADDED, NetTestTools.link("a", 1, "b", 2))); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 188 | assertTrue("The list should contain no events, the removed elements don't exist.", events.size() == 0); |
| 189 | } |
| 190 | |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 191 | |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 192 | @Test |
| 193 | public void testDeviceUpdates() { |
| 194 | //Setup |
| 195 | |
| 196 | Device referenceDevice; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 197 | DeviceEvent event; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 198 | int numDevices = 10; |
| 199 | int numInfraPorts = 5; |
| 200 | totalPorts = 10; |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame] | 201 | |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 202 | defaultPopulator(numDevices, numInfraPorts); |
| 203 | |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame] | 204 | events.clear(); |
| 205 | |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 206 | //Test response to device added events |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame] | 207 | |
| 208 | referenceDevice = NetTestTools.device("11"); |
| 209 | devices.put(referenceDevice.id(), referenceDevice); |
| 210 | for (int port = 1; port <= numInfraPorts; port++) { |
| 211 | infrastructurePorts.add(NetTestTools.connectPoint("11", port)); |
| 212 | } |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 213 | event = new DeviceEvent(DEVICE_ADDED, referenceDevice, |
| 214 | new DefaultPort(referenceDevice, PortNumber.portNumber(1), true)); |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 215 | postTopologyEvent(event); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 216 | |
| 217 | //Check that ports were populated correctly |
| 218 | assertTrue("Unexpected number of new ports added", |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame] | 219 | mgr.deviceService.getPorts(NetTestTools.did("11")).size() == 10); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 220 | |
| 221 | //Check that of the ten ports the half that are infrastructure ports aren't added |
| 222 | assertEquals("Unexpected number of new edge ports added", (totalPorts - numInfraPorts), events.size()); |
| 223 | |
| 224 | for (int index = 0; index < numInfraPorts; index++) { |
| 225 | assertTrue("Unexpected type of event", events.get(index).type() == EDGE_PORT_ADDED); |
| 226 | } |
| 227 | //Names here are irrelevant, the first 5 ports are populated as infrastructure, 6-10 are edge |
| 228 | for (int index = 0; index < events.size(); index++) { |
| 229 | assertEquals("Port added had unexpected port number.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 230 | events.get(index).subject().port(), |
| 231 | NetTestTools.connectPoint("a", index + numInfraPorts + 1).port()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 232 | } |
| 233 | events.clear(); |
| 234 | |
| 235 | //Repost the event to test repeated posts |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 236 | postTopologyEvent(event); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 237 | assertEquals("The redundant notification should not have created additional notifications.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 238 | 0, events.size()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 239 | //Calculate the size of the returned iterable of edge points. |
| 240 | Iterable<ConnectPoint> pts = mgr.getEdgePoints(); |
| 241 | Iterator pointIterator = pts.iterator(); |
| 242 | int count = 0; |
| 243 | for (; pointIterator.hasNext(); count++) { |
| 244 | pointIterator.next(); |
| 245 | } |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame] | 246 | assertEquals("Unexpected number of edge points", (numDevices + 1) * numInfraPorts, count); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 247 | //Testing device removal |
| 248 | events.clear(); |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 249 | event = (new DeviceEvent(DEVICE_REMOVED, referenceDevice, |
| 250 | new DefaultPort(referenceDevice, PortNumber.portNumber(1), true))); |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 251 | postTopologyEvent(event); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 252 | |
| 253 | assertEquals("There should be five new events from removal of edge points", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 254 | totalPorts - numInfraPorts, events.size()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 255 | for (int index = 0; index < events.size(); index++) { |
| 256 | //Assert that the correct port numbers were removed in the correct order |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 257 | assertThat("Port removed had unexpected port number.", |
| 258 | events.get(index).subject().port().toLong(), |
| 259 | is(greaterThanOrEqualTo((long) numInfraPorts))); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 260 | //Assert that the events are of the correct type |
| 261 | assertEquals("Unexpected type of event", events.get(index).type(), EDGE_PORT_REMOVED); |
| 262 | } |
| 263 | events.clear(); |
| 264 | //Rebroadcast event to check that it triggers no new behavior |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 265 | postTopologyEvent(event); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 266 | assertEquals("Rebroadcast of removal event should not produce additional events", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 267 | 0, events.size()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 268 | |
| 269 | //Testing device status change, changed from unavailable to available |
| 270 | events.clear(); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 271 | //Make sure that the devicemanager shows the device as available. |
| 272 | addDevice(referenceDevice, "1", 5); |
| 273 | devices.put(referenceDevice.id(), referenceDevice); |
| 274 | |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 275 | event = new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, referenceDevice); |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 276 | postTopologyEvent(event); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 277 | //An earlier setup set half of the reference device ports to infrastructure |
| 278 | assertEquals("An unexpected number of events were generated.", totalPorts - numInfraPorts, events.size()); |
| 279 | for (int i = 0; i < 5; i++) { |
| 280 | assertEquals("The event was not of the right type", events.get(i).type(), EDGE_PORT_ADDED); |
| 281 | } |
| 282 | events.clear(); |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 283 | postTopologyEvent(event); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 284 | assertEquals("No events should have been generated for a set of existing ports.", 0, events.size()); |
| 285 | |
| 286 | //Test removal when state changes when the device becomes unavailable |
| 287 | |
| 288 | //Ensure that the deviceManager shows the device as unavailable |
| 289 | removeDevice(referenceDevice); |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame] | 290 | // This variable copies the behavior of the topology by returning ports |
| 291 | // attached to an unavailable device this behavior is necessary for the |
| 292 | // following event to execute properly, if these statements are removed |
| 293 | // no events will be generated since no ports will be provided in |
| 294 | // getPorts() to EdgeManager. |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 295 | alwaysReturnPorts = true; |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 296 | postTopologyEvent(event); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 297 | alwaysReturnPorts = false; |
| 298 | assertEquals("An unexpected number of events were created.", totalPorts - numInfraPorts, events.size()); |
| 299 | for (int i = 0; i < 5; i++) { |
| 300 | EdgePortEvent edgeEvent = events.get(i); |
| 301 | assertEquals("The event is of an unexpected type.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 302 | EdgePortEvent.Type.EDGE_PORT_REMOVED, edgeEvent.type()); |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 303 | assertThat("The event pertains to an unexpected port", |
| 304 | edgeEvent.subject().port().toLong(), |
| 305 | is(greaterThanOrEqualTo((long) numInfraPorts))); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 309 | |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 310 | @Test |
| 311 | public void testInternalCache() { |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 312 | int numDevices = 10; |
| 313 | //Number of infrastructure ports per device |
| 314 | int numPorts = 5; |
| 315 | //Total ports per device when requesting all devices |
| 316 | totalPorts = 10; |
| 317 | defaultPopulator(numDevices, numPorts); |
| 318 | for (int i = 0; i < numDevices; i++) { |
| 319 | Device newDevice = NetTestTools.device(Integer.toString(i)); |
| 320 | devices.put(newDevice.id(), newDevice); |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 321 | postTopologyEvent(new DeviceEvent(DEVICE_ADDED, newDevice)); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 322 | } |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 323 | //Check all ports have correct designations |
| 324 | ConnectPoint testPoint; |
| 325 | for (int deviceNum = 0; deviceNum < numDevices; deviceNum++) { |
| 326 | for (int portNum = 1; portNum <= totalPorts; portNum++) { |
| 327 | testPoint = NetTestTools.connectPoint(Integer.toString(deviceNum), portNum); |
| 328 | if (portNum <= numPorts) { |
| 329 | assertFalse("This should not be an edge point", mgr.isEdgePoint(testPoint)); |
| 330 | } else { |
| 331 | assertTrue("This should be an edge point", mgr.isEdgePoint(testPoint)); |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | int count = 0; |
| 336 | for (ConnectPoint ignored : mgr.getEdgePoints()) { |
| 337 | count++; |
| 338 | } |
| 339 | assertEquals("There are an unexpeceted number of edge points returned.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 340 | (totalPorts - numPorts) * numDevices, count); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 341 | for (int deviceNumber = 0; deviceNumber < numDevices; deviceNumber++) { |
| 342 | count = 0; |
| 343 | for (ConnectPoint ignored : mgr.getEdgePoints(NetTestTools.did("1"))) { |
| 344 | count++; |
| 345 | } |
| 346 | assertEquals("This element has an unexpected number of edge points.", (totalPorts - numPorts), count); |
| 347 | } |
| 348 | } |
| 349 | |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 350 | @Test |
| 351 | public void testEmit() { |
| 352 | byte[] arr = new byte[10]; |
| 353 | Device referenceDevice; |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 354 | DeviceEvent event; |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 355 | int numDevices = 10; |
| 356 | int numInfraPorts = 5; |
| 357 | totalPorts = 10; |
| 358 | defaultPopulator(numDevices, numInfraPorts); |
| 359 | for (byte byteIndex = 0; byteIndex < arr.length; byteIndex++) { |
| 360 | arr[byteIndex] = byteIndex; |
| 361 | } |
| 362 | for (int i = 0; i < numDevices; i++) { |
| 363 | referenceDevice = NetTestTools.device(Integer.toString(i)); |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame] | 364 | testDeviceManager.listener.event(new DeviceEvent(DEVICE_ADDED, referenceDevice, |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 365 | new DefaultPort(referenceDevice, |
| 366 | PortNumber.portNumber(1), |
| 367 | true))); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 368 | } |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 369 | |
Sho SHIMIZU | 21d0069 | 2016-08-15 11:15:28 -0700 | [diff] [blame] | 370 | mgr.emitPacket(ByteBuffer.wrap(arr), Optional.empty()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 371 | |
| 372 | assertEquals("There were an unexpected number of emitted packets", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 373 | (totalPorts - numInfraPorts) * numDevices, packets.size()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 374 | Iterator<OutboundPacket> packetIter = packets.iterator(); |
| 375 | OutboundPacket packet; |
| 376 | while (packetIter.hasNext()) { |
| 377 | packet = packetIter.next(); |
| 378 | assertEquals("The packet had an incorrect payload.", arr, packet.data().array()); |
| 379 | } |
| 380 | //Start testing emission to a specific device |
| 381 | packets.clear(); |
Sho SHIMIZU | 21d0069 | 2016-08-15 11:15:28 -0700 | [diff] [blame] | 382 | mgr.emitPacket(NetTestTools.did(Integer.toString(1)), ByteBuffer.wrap(arr), Optional.empty()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 383 | |
| 384 | assertEquals("Unexpected number of outbound packets were emitted.", |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 385 | totalPorts - numInfraPorts, packets.size()); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 386 | packetIter = packets.iterator(); |
| 387 | while (packetIter.hasNext()) { |
| 388 | packet = packetIter.next(); |
| 389 | assertEquals("The packet had an incorrect payload", arr, packet.data().array()); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | |
| 394 | /** |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 395 | * Creates TopologyEvent triggered by {@code event}. |
| 396 | * |
| 397 | * @param event reason of the TopologyEvent |
| 398 | * @return TopologyEvent |
| 399 | */ |
| 400 | private TopologyEvent topologyEventOf(Event event) { |
| 401 | return new TopologyEvent(Type.TOPOLOGY_CHANGED, null, ImmutableList.of(event)); |
| 402 | } |
| 403 | |
| 404 | |
| 405 | /** |
| 406 | * Post Event dispatched from TopologyManager. |
| 407 | * |
| 408 | * @param event Event |
| 409 | */ |
| 410 | private void postTopologyEvent(Event event) { |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame] | 411 | if (event instanceof DeviceEvent) { |
| 412 | testDeviceManager.listener.event((DeviceEvent) event); |
| 413 | } |
| 414 | if (event instanceof LinkEvent) { |
| 415 | testLinkService.listener.event((LinkEvent) event); |
| 416 | } |
| 417 | //testTopologyManager.listener.event(topologyEventOf(event)); |
Yuta HIGUCHI | b440ef4 | 2016-07-15 09:09:09 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | |
| 421 | /** |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 422 | * @param numDevices the number of devices to populate. |
| 423 | * @param numInfraPorts the number of ports to be set as infrastructure on each device, numbered base 0, ports 0 |
| 424 | * through numInfraPorts - 1 |
| 425 | */ |
| 426 | private void defaultPopulator(int numDevices, int numInfraPorts) { |
| 427 | for (int device = 0; device < numDevices; device++) { |
| 428 | String str = Integer.toString(device); |
| 429 | Device deviceToAdd = NetTestTools.device(str); |
| 430 | devices.put(deviceToAdd.id(), deviceToAdd); |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame] | 431 | testDeviceManager.listener.event(new DeviceEvent(DEVICE_ADDED, deviceToAdd)); |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 432 | for (int port = 1; port <= numInfraPorts; port++) { |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame] | 433 | 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] | 434 | infrastructurePorts.add(NetTestTools.connectPoint(str, port)); |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * Adds the specified device with the specified number of edge ports so long as it is less than the total ports. |
| 441 | * |
| 442 | * @param device The device to be added |
| 443 | * @param deviceName The name given to generate the devices DID |
| 444 | * @param numInfraPorts The number of ports to be added numbered 1 ... numInfraPorts |
| 445 | */ |
| 446 | private void addDevice(Device device, String deviceName, int numInfraPorts) { |
| 447 | if (!devices.keySet().contains(device.id())) { |
| 448 | devices.put(device.id(), device); |
| 449 | for (int i = 1; i <= numInfraPorts && i <= totalPorts; i++) { |
| 450 | infrastructurePorts.add(NetTestTools.connectPoint(deviceName, i)); |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | private void removeDevice(Device device) { |
| 456 | devices.remove(device.id()); |
| 457 | } |
| 458 | |
| 459 | private void removeInfraPort(ConnectPoint port) { |
| 460 | infrastructurePorts.remove(port); |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 463 | private class TestDeviceManager extends DeviceServiceAdapter { |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 464 | private DeviceListener listener; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 465 | |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 466 | private Map<DeviceId, Device> devices; |
| 467 | |
| 468 | public TestDeviceManager(Map<DeviceId, Device> devices) { |
| 469 | this.devices = devices; |
| 470 | } |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 471 | |
| 472 | @Override |
| 473 | public boolean isAvailable(DeviceId deviceId) { |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 474 | for (DeviceId id : devices.keySet()) { |
| 475 | if (id.equals(deviceId)) { |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 476 | return true; |
| 477 | } |
| 478 | } |
| 479 | return false; |
| 480 | } |
| 481 | |
| 482 | @Override |
| 483 | public List<Port> getPorts(DeviceId deviceId) { |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 484 | List<Port> ports = new ArrayList<>(); |
| 485 | Device device = devices.get(deviceId); |
| 486 | if (device == null && !alwaysReturnPorts) { |
| 487 | return ports; |
| 488 | } |
| 489 | for (int portNum = 1; portNum <= totalPorts; portNum++) { |
| 490 | //String is generated using 'of:' + the passed name, this creates a |
| 491 | ports.add(new DefaultPort(device, PortNumber.portNumber(portNum), true)); |
| 492 | } |
| 493 | return ports; |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | @Override |
| 497 | public Iterable<Device> getAvailableDevices() { |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 498 | return devices.values(); |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 499 | } |
alshabib | 8a4a600 | 2015-11-25 14:31:16 -0800 | [diff] [blame] | 500 | |
| 501 | |
| 502 | @Override |
| 503 | public void addListener(DeviceListener listener) { |
| 504 | this.listener = listener; |
| 505 | } |
| 506 | |
| 507 | @Override |
| 508 | public void removeListener(DeviceListener listener) { |
| 509 | this.listener = null; |
| 510 | } |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Jonathan Hart | 6ff6ffe | 2016-09-09 11:55:50 -0700 | [diff] [blame] | 513 | private class TestLinkService extends LinkServiceAdapter { |
| 514 | |
| 515 | private LinkListener listener; |
| 516 | |
| 517 | @Override |
| 518 | public Set<Link> getLinks(ConnectPoint connectPoint) { |
| 519 | if (infrastructurePorts.contains(connectPoint)) { |
| 520 | return Collections.singleton(NetTestTools.link("1", 1, "2", 1)); |
| 521 | } else { |
| 522 | return Collections.emptySet(); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | @Override |
| 527 | public void addListener(LinkListener listener) { |
| 528 | this.listener = listener; |
| 529 | } |
| 530 | } |
| 531 | |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 532 | private class TestPacketManager extends PacketServiceAdapter { |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 533 | @Override |
| 534 | public void emit(OutboundPacket packet) { |
| 535 | packets.add(packet); |
| 536 | } |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 539 | private class TestListener implements EdgePortListener { |
| 540 | private List<EdgePortEvent> events; |
| 541 | |
Aaron Kruglikov | e120059 | 2015-06-29 16:31:23 -0700 | [diff] [blame] | 542 | public TestListener(List<EdgePortEvent> events) { |
| 543 | this.events = events; |
| 544 | } |
| 545 | |
Thomas Vachuska | f3ed655 | 2015-06-29 13:56:03 -0700 | [diff] [blame] | 546 | @Override |
| 547 | public void event(EdgePortEvent event) { |
| 548 | events.add(event); |
| 549 | } |
| 550 | } |
Sho SHIMIZU | 21d0069 | 2016-08-15 11:15:28 -0700 | [diff] [blame] | 551 | } |