Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | |
| 17 | package org.onosproject.incubator.net.virtual.impl; |
| 18 | |
| 19 | import org.junit.After; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.junit.TestUtils; |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 23 | import org.onlab.osgi.TestServiceDirectory; |
| 24 | import org.onlab.rest.BaseResource; |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 25 | import org.onosproject.common.event.impl.TestEventDispatcher; |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame^] | 26 | import org.onosproject.core.ApplicationId; |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 27 | import org.onosproject.core.CoreService; |
| 28 | import org.onosproject.core.CoreServiceAdapter; |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame^] | 29 | import org.onosproject.core.DefaultApplicationId; |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 30 | import org.onosproject.core.IdGenerator; |
| 31 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 32 | import org.onosproject.incubator.net.virtual.TenantId; |
| 33 | import org.onosproject.incubator.net.virtual.VirtualDevice; |
| 34 | import org.onosproject.incubator.net.virtual.VirtualLink; |
| 35 | import org.onosproject.incubator.net.virtual.VirtualNetwork; |
| 36 | import org.onosproject.incubator.store.virtual.impl.DistributedVirtualNetworkStore; |
| 37 | import org.onosproject.net.ConnectPoint; |
| 38 | import org.onosproject.net.DeviceId; |
| 39 | import org.onosproject.net.DisjointPath; |
| 40 | import org.onosproject.net.Link; |
| 41 | import org.onosproject.net.NetTestTools; |
| 42 | import org.onosproject.net.Path; |
| 43 | import org.onosproject.net.PortNumber; |
| 44 | import org.onosproject.net.TestDeviceParams; |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 45 | import org.onosproject.net.intent.FakeIntentManager; |
| 46 | import org.onosproject.net.intent.TestableIntentService; |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 47 | import org.onosproject.net.topology.LinkWeigher; |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 48 | import org.onosproject.net.topology.LinkWeight; |
| 49 | import org.onosproject.net.topology.Topology; |
| 50 | import org.onosproject.net.topology.TopologyCluster; |
| 51 | import org.onosproject.net.topology.TopologyService; |
| 52 | import org.onosproject.store.service.TestStorageService; |
| 53 | |
| 54 | import java.util.Map; |
| 55 | import java.util.Optional; |
| 56 | import java.util.Set; |
| 57 | import java.util.concurrent.atomic.AtomicLong; |
| 58 | |
| 59 | import static junit.framework.TestCase.assertTrue; |
| 60 | import static org.junit.Assert.*; |
| 61 | |
| 62 | /** |
| 63 | * Junit tests for VirtualNetworkTopologyService. |
| 64 | */ |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 65 | public class VirtualNetworkTopologyManagerTest extends TestDeviceParams { |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 66 | |
| 67 | private final String tenantIdValue1 = "TENANT_ID1"; |
| 68 | |
| 69 | private VirtualNetworkManager manager; |
| 70 | private DistributedVirtualNetworkStore virtualNetworkManagerStore; |
| 71 | private CoreService coreService; |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 72 | private TestableIntentService intentService = new FakeIntentManager(); |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 73 | private TestServiceDirectory testDirectory; |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 74 | |
| 75 | @Before |
| 76 | public void setUp() throws Exception { |
| 77 | virtualNetworkManagerStore = new DistributedVirtualNetworkStore(); |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 78 | coreService = new VirtualNetworkTopologyManagerTest.TestCoreService(); |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 79 | TestUtils.setField(virtualNetworkManagerStore, "coreService", coreService); |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 80 | TestUtils.setField(virtualNetworkManagerStore, "storageService", new TestStorageService()); |
| 81 | virtualNetworkManagerStore.activate(); |
| 82 | |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 83 | BaseResource.setServiceDirectory(testDirectory); |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 84 | manager = new VirtualNetworkManager(); |
| 85 | manager.store = virtualNetworkManagerStore; |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 86 | manager.intentService = intentService; |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame^] | 87 | manager.coreService = coreService; |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 88 | NetTestTools.injectEventDispatcher(manager, new TestEventDispatcher()); |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 89 | |
| 90 | testDirectory = new TestServiceDirectory(); |
| 91 | TestUtils.setField(manager, "serviceDirectory", testDirectory); |
| 92 | |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 93 | manager.activate(); |
| 94 | } |
| 95 | |
| 96 | @After |
| 97 | public void tearDown() { |
| 98 | virtualNetworkManagerStore.deactivate(); |
| 99 | manager.deactivate(); |
| 100 | NetTestTools.injectEventDispatcher(manager, null); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Method to create the virtual network for further testing. |
| 105 | * |
| 106 | * @return virtual network |
| 107 | */ |
| 108 | private VirtualNetwork setupVirtualNetworkTopology() { |
| 109 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 110 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 111 | VirtualDevice virtualDevice1 = |
| 112 | manager.createVirtualDevice(virtualNetwork.id(), DID1); |
| 113 | VirtualDevice virtualDevice2 = |
| 114 | manager.createVirtualDevice(virtualNetwork.id(), DID2); |
| 115 | VirtualDevice virtualDevice3 = |
| 116 | manager.createVirtualDevice(virtualNetwork.id(), DID3); |
| 117 | VirtualDevice virtualDevice4 = |
| 118 | manager.createVirtualDevice(virtualNetwork.id(), DID4); |
| 119 | |
| 120 | ConnectPoint cp1 = new ConnectPoint(virtualDevice1.id(), PortNumber.portNumber(1)); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 121 | manager.createVirtualPort(virtualNetwork.id(), cp1.deviceId(), cp1.port(), cp1); |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 122 | |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 123 | ConnectPoint cp2 = new ConnectPoint(virtualDevice1.id(), PortNumber.portNumber(2)); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 124 | manager.createVirtualPort(virtualNetwork.id(), cp2.deviceId(), cp2.port(), cp2); |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 125 | |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 126 | ConnectPoint cp3 = new ConnectPoint(virtualDevice2.id(), PortNumber.portNumber(3)); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 127 | manager.createVirtualPort(virtualNetwork.id(), cp3.deviceId(), cp3.port(), cp3); |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 128 | |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 129 | ConnectPoint cp4 = new ConnectPoint(virtualDevice2.id(), PortNumber.portNumber(4)); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 130 | manager.createVirtualPort(virtualNetwork.id(), cp4.deviceId(), cp4.port(), cp4); |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 131 | |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 132 | ConnectPoint cp5 = new ConnectPoint(virtualDevice3.id(), PortNumber.portNumber(5)); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 133 | manager.createVirtualPort(virtualNetwork.id(), cp5.deviceId(), cp5.port(), cp5); |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 134 | |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 135 | ConnectPoint cp6 = new ConnectPoint(virtualDevice3.id(), PortNumber.portNumber(6)); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 136 | manager.createVirtualPort(virtualNetwork.id(), cp6.deviceId(), cp6.port(), cp6); |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 137 | |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 138 | VirtualLink link1 = manager.createVirtualLink(virtualNetwork.id(), cp1, cp3); |
| 139 | virtualNetworkManagerStore.updateLink(link1, link1.tunnelId(), Link.State.ACTIVE); |
| 140 | VirtualLink link2 = manager.createVirtualLink(virtualNetwork.id(), cp3, cp1); |
| 141 | virtualNetworkManagerStore.updateLink(link2, link2.tunnelId(), Link.State.ACTIVE); |
| 142 | VirtualLink link3 = manager.createVirtualLink(virtualNetwork.id(), cp4, cp5); |
| 143 | virtualNetworkManagerStore.updateLink(link3, link3.tunnelId(), Link.State.ACTIVE); |
| 144 | VirtualLink link4 = manager.createVirtualLink(virtualNetwork.id(), cp5, cp4); |
| 145 | virtualNetworkManagerStore.updateLink(link4, link4.tunnelId(), Link.State.ACTIVE); |
| 146 | VirtualLink link5 = manager.createVirtualLink(virtualNetwork.id(), cp2, cp6); |
| 147 | virtualNetworkManagerStore.updateLink(link5, link5.tunnelId(), Link.State.ACTIVE); |
| 148 | VirtualLink link6 = manager.createVirtualLink(virtualNetwork.id(), cp6, cp2); |
| 149 | virtualNetworkManagerStore.updateLink(link6, link6.tunnelId(), Link.State.ACTIVE); |
| 150 | |
| 151 | return virtualNetwork; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Tests the currentTopology() method. |
| 156 | */ |
| 157 | @Test |
| 158 | public void testCurrentTopology() { |
| 159 | VirtualNetwork virtualNetwork = setupVirtualNetworkTopology(); |
| 160 | |
| 161 | TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class); |
| 162 | Topology topology = topologyService.currentTopology(); |
| 163 | assertNotNull("The topology should not be null.", topology); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Test isLatest() method using a null topology. |
| 168 | */ |
| 169 | @Test(expected = NullPointerException.class) |
| 170 | public void testIsLatestByNullTopology() { |
| 171 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 172 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 173 | TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class); |
| 174 | |
| 175 | // test the isLatest() method with a null topology. |
| 176 | topologyService.isLatest(null); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Test isLatest() method. |
| 181 | */ |
| 182 | @Test |
| 183 | public void testIsLatest() { |
| 184 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 185 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 186 | TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class); |
| 187 | Topology topology = topologyService.currentTopology(); |
| 188 | |
| 189 | // test the isLatest() method. |
| 190 | assertTrue("This should be latest topology", topologyService.isLatest(topology)); |
| 191 | |
| 192 | VirtualDevice srcVirtualDevice = |
| 193 | manager.createVirtualDevice(virtualNetwork.id(), DID1); |
| 194 | VirtualDevice dstVirtualDevice = |
| 195 | manager.createVirtualDevice(virtualNetwork.id(), DID2); |
| 196 | |
| 197 | // test the isLatest() method where a new device has been added to the current topology. |
| 198 | assertFalse("This should not be latest topology", topologyService.isLatest(topology)); |
| 199 | |
| 200 | topology = topologyService.currentTopology(); |
| 201 | ConnectPoint src = new ConnectPoint(srcVirtualDevice.id(), PortNumber.portNumber(1)); |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 202 | manager.createVirtualPort(virtualNetwork.id(), src.deviceId(), src.port(), |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 203 | new ConnectPoint(srcVirtualDevice.id(), src.port())); |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 204 | |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 205 | ConnectPoint dst = new ConnectPoint(dstVirtualDevice.id(), PortNumber.portNumber(2)); |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 206 | manager.createVirtualPort(virtualNetwork.id(), dst.deviceId(), dst.port(), |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 207 | new ConnectPoint(dstVirtualDevice.id(), dst.port())); |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 208 | VirtualLink link1 = manager.createVirtualLink(virtualNetwork.id(), src, dst); |
| 209 | |
| 210 | // test the isLatest() method where a new link has been added to the current topology. |
| 211 | assertFalse("This should not be latest topology", topologyService.isLatest(topology)); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Test getGraph() method. |
| 216 | */ |
| 217 | @Test |
| 218 | public void testGetGraph() { |
| 219 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 220 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 221 | TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class); |
| 222 | Topology topology = topologyService.currentTopology(); |
| 223 | |
| 224 | // test the getGraph() method. |
| 225 | assertNotNull("The graph should not be null.", topologyService.getGraph(topology)); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Test getClusters() method. |
| 230 | */ |
| 231 | @Test |
| 232 | public void testGetClusters() { |
| 233 | VirtualNetwork virtualNetwork = setupVirtualNetworkTopology(); |
| 234 | |
| 235 | TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class); |
| 236 | |
| 237 | Topology topology = topologyService.currentTopology(); |
| 238 | |
| 239 | // test the getClusters() method. |
| 240 | assertNotNull("The clusters should not be null.", topologyService.getClusters(topology)); |
| 241 | assertEquals("The clusters size did not match.", 2, topologyService.getClusters(topology).size()); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Test getCluster() method using a null cluster identifier. |
| 246 | */ |
| 247 | @Test(expected = NullPointerException.class) |
| 248 | public void testGetClusterUsingNullClusterId() { |
| 249 | VirtualNetwork virtualNetwork = setupVirtualNetworkTopology(); |
| 250 | |
| 251 | TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class); |
| 252 | Topology topology = topologyService.currentTopology(); |
| 253 | |
| 254 | Set<TopologyCluster> clusters = topologyService.getClusters(topology); |
| 255 | TopologyCluster cluster = clusters.stream().findFirst().get(); |
| 256 | |
| 257 | // test the getCluster() method with a null cluster identifier |
| 258 | TopologyCluster cluster1 = topologyService.getCluster(topology, null); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Test getCluster() method. |
| 263 | */ |
| 264 | @Test |
| 265 | public void testGetCluster() { |
| 266 | VirtualNetwork virtualNetwork = setupVirtualNetworkTopology(); |
| 267 | |
| 268 | TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class); |
| 269 | Topology topology = topologyService.currentTopology(); |
| 270 | |
| 271 | Set<TopologyCluster> clusters = topologyService.getClusters(topology); |
| 272 | assertNotNull("The clusters should not be null.", clusters); |
| 273 | assertEquals("The clusters size did not match.", 2, clusters.size()); |
| 274 | |
| 275 | // test the getCluster() method. |
| 276 | TopologyCluster cluster = clusters.stream().findFirst().get(); |
| 277 | assertNotNull("The cluster should not be null.", cluster); |
| 278 | TopologyCluster cluster1 = topologyService.getCluster(topology, cluster.id()); |
| 279 | assertNotNull("The cluster should not be null.", cluster1); |
| 280 | assertEquals("The cluster ID did not match.", cluster.id(), cluster1.id()); |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Test getClusterDevices() methods with a null cluster. |
| 285 | */ |
| 286 | @Test(expected = NullPointerException.class) |
| 287 | public void testGetClusterDevicesUsingNullCluster() { |
| 288 | VirtualNetwork virtualNetwork = setupVirtualNetworkTopology(); |
| 289 | |
| 290 | TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class); |
| 291 | Topology topology = topologyService.currentTopology(); |
| 292 | Set<TopologyCluster> clusters = topologyService.getClusters(topology); |
| 293 | |
| 294 | // test the getClusterDevices() method using a null cluster. |
| 295 | Object[] objects = clusters.stream().toArray(); |
| 296 | assertNotNull("The cluster should not be null.", objects); |
| 297 | Set<DeviceId> clusterDevices = topologyService.getClusterDevices(topology, null); |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Test getClusterLinks() methods with a null cluster. |
| 302 | */ |
| 303 | @Test(expected = NullPointerException.class) |
| 304 | public void testGetClusterLinksUsingNullCluster() { |
| 305 | VirtualNetwork virtualNetwork = setupVirtualNetworkTopology(); |
| 306 | |
| 307 | TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class); |
| 308 | Topology topology = topologyService.currentTopology(); |
| 309 | Set<TopologyCluster> clusters = topologyService.getClusters(topology); |
| 310 | |
| 311 | // test the getClusterLinks() method using a null cluster. |
| 312 | Object[] objects = clusters.stream().toArray(); |
| 313 | assertNotNull("The cluster should not be null.", objects); |
| 314 | Set<Link> clusterLinks = topologyService.getClusterLinks(topology, null); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Test getClusterDevices() and getClusterLinks() methods. |
| 319 | */ |
| 320 | @Test |
| 321 | public void testGetClusterDevicesLinks() { |
| 322 | VirtualNetwork virtualNetwork = setupVirtualNetworkTopology(); |
| 323 | |
| 324 | TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class); |
| 325 | Topology topology = topologyService.currentTopology(); |
| 326 | |
| 327 | Set<TopologyCluster> clusters = topologyService.getClusters(topology); |
| 328 | assertNotNull("The clusters should not be null.", clusters); |
| 329 | assertEquals("The clusters size did not match.", 2, clusters.size()); |
| 330 | |
| 331 | // test the getClusterDevices() method. |
| 332 | Object[] objects = clusters.stream().toArray(); |
| 333 | assertNotNull("The cluster should not be null.", objects); |
| 334 | Set<DeviceId> clusterDevices = topologyService.getClusterDevices(topology, (TopologyCluster) objects[0]); |
| 335 | assertNotNull("The devices should not be null.", clusterDevices); |
| 336 | assertEquals("The devices size did not match.", 3, clusterDevices.size()); |
| 337 | Set<DeviceId> clusterDevices1 = topologyService.getClusterDevices(topology, (TopologyCluster) objects[1]); |
| 338 | assertNotNull("The devices should not be null.", clusterDevices1); |
| 339 | assertEquals("The devices size did not match.", 1, clusterDevices1.size()); |
| 340 | |
| 341 | // test the getClusterLinks() method. |
| 342 | Set<Link> clusterLinks = topologyService.getClusterLinks(topology, (TopologyCluster) objects[0]); |
| 343 | assertNotNull("The links should not be null.", clusterLinks); |
| 344 | assertEquals("The links size did not match.", 6, clusterLinks.size()); |
| 345 | Set<Link> clusterLinks1 = topologyService.getClusterLinks(topology, (TopologyCluster) objects[1]); |
| 346 | assertNotNull("The links should not be null.", clusterLinks1); |
| 347 | assertEquals("The links size did not match.", 0, clusterLinks1.size()); |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Test getPaths() method using a null src device identifier. |
| 352 | */ |
| 353 | @Test(expected = NullPointerException.class) |
| 354 | public void testGetPathsUsingNullSrcDeviceId() { |
| 355 | VirtualNetwork virtualNetwork = setupVirtualNetworkTopology(); |
| 356 | |
| 357 | TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class); |
| 358 | Topology topology = topologyService.currentTopology(); |
| 359 | |
| 360 | VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1); |
| 361 | VirtualDevice dstVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID2); |
| 362 | |
| 363 | // test the getPaths() method using a null src device identifier. |
| 364 | Set<Path> paths = topologyService.getPaths(topology, null, dstVirtualDevice.id()); |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Test getPaths() method using a null dst device identifier. |
| 369 | */ |
| 370 | @Test(expected = NullPointerException.class) |
| 371 | public void testGetPathsUsingNullDstDeviceId() { |
| 372 | VirtualNetwork virtualNetwork = setupVirtualNetworkTopology(); |
| 373 | |
| 374 | TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class); |
| 375 | Topology topology = topologyService.currentTopology(); |
| 376 | |
| 377 | VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1); |
| 378 | VirtualDevice dstVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID2); |
| 379 | |
| 380 | // test the getPaths() method using a null dst device identifier. |
| 381 | Set<Path> paths = topologyService.getPaths(topology, srcVirtualDevice.id(), null); |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Test getPaths() method using a null weight. |
| 386 | */ |
| 387 | @Test(expected = NullPointerException.class) |
| 388 | public void testGetPathsUsingNullWeight() { |
| 389 | VirtualNetwork virtualNetwork = setupVirtualNetworkTopology(); |
| 390 | |
| 391 | TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class); |
| 392 | Topology topology = topologyService.currentTopology(); |
| 393 | |
| 394 | VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1); |
| 395 | VirtualDevice dstVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID2); |
| 396 | |
| 397 | // test the getPaths() method using a null weight. |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 398 | Set<Path> paths = topologyService.getPaths(topology, srcVirtualDevice.id(), |
| 399 | dstVirtualDevice.id(), (LinkWeigher) null); |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Test getPaths() and getPaths() by weight methods. |
| 404 | */ |
| 405 | @Test |
| 406 | public void testGetPaths() { |
| 407 | VirtualNetwork virtualNetwork = setupVirtualNetworkTopology(); |
| 408 | |
| 409 | TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class); |
| 410 | Topology topology = topologyService.currentTopology(); |
| 411 | |
| 412 | VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1); |
| 413 | VirtualDevice dstVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID2); |
| 414 | |
| 415 | // test the getPaths() method. |
| 416 | Set<Path> paths = topologyService.getPaths(topology, srcVirtualDevice.id(), dstVirtualDevice.id()); |
| 417 | assertNotNull("The paths should not be null.", paths); |
| 418 | assertEquals("The paths size did not match.", 1, paths.size()); |
| 419 | |
| 420 | // test the getPaths() by weight method. |
| 421 | LinkWeight weight = edge -> 1.0; |
| 422 | Set<Path> paths1 = topologyService.getPaths(topology, srcVirtualDevice.id(), dstVirtualDevice.id(), weight); |
| 423 | assertNotNull("The paths should not be null.", paths1); |
| 424 | assertEquals("The paths size did not match.", 1, paths1.size()); |
| 425 | Path path = paths1.iterator().next(); |
| 426 | assertEquals("wrong path length", 1, path.links().size()); |
| 427 | assertEquals("wrong path cost", 1.0, path.cost(), 0.01); |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Test getDisjointPaths() methods using a null src device identifier. |
| 432 | */ |
| 433 | @Test(expected = NullPointerException.class) |
| 434 | public void testGetDisjointPathsUsingNullSrcDeviceId() { |
| 435 | VirtualNetwork virtualNetwork = setupVirtualNetworkTopology(); |
| 436 | |
| 437 | TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class); |
| 438 | Topology topology = topologyService.currentTopology(); |
| 439 | |
| 440 | VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1); |
| 441 | VirtualDevice dstVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID2); |
| 442 | |
| 443 | // test the getDisjointPaths() method using a null src device identifier. |
| 444 | Set<DisjointPath> paths = topologyService.getDisjointPaths(topology, null, dstVirtualDevice.id()); |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * Test getDisjointPaths() methods using a null dst device identifier. |
| 449 | */ |
| 450 | @Test(expected = NullPointerException.class) |
| 451 | public void testGetDisjointPathsUsingNullDstDeviceId() { |
| 452 | VirtualNetwork virtualNetwork = setupVirtualNetworkTopology(); |
| 453 | |
| 454 | TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class); |
| 455 | Topology topology = topologyService.currentTopology(); |
| 456 | |
| 457 | VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1); |
| 458 | VirtualDevice dstVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID2); |
| 459 | |
| 460 | // test the getDisjointPaths() method using a null dst device identifier. |
| 461 | Set<DisjointPath> paths = topologyService.getDisjointPaths(topology, srcVirtualDevice.id(), null); |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * Test getDisjointPaths() methods using a null weight. |
| 466 | */ |
| 467 | @Test(expected = NullPointerException.class) |
| 468 | public void testGetDisjointPathsUsingNullWeight() { |
| 469 | VirtualNetwork virtualNetwork = setupVirtualNetworkTopology(); |
| 470 | |
| 471 | TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class); |
| 472 | Topology topology = topologyService.currentTopology(); |
| 473 | |
| 474 | VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1); |
| 475 | VirtualDevice dstVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID2); |
| 476 | |
| 477 | // test the getDisjointPaths() method using a null weight. |
| 478 | Set<DisjointPath> paths = topologyService.getDisjointPaths(topology, srcVirtualDevice.id(), |
| 479 | dstVirtualDevice.id(), (LinkWeight) null); |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * Test getDisjointPaths() methods using a null risk profile. |
| 484 | */ |
| 485 | @Test(expected = NullPointerException.class) |
| 486 | public void testGetDisjointPathsUsingNullRiskProfile() { |
| 487 | VirtualNetwork virtualNetwork = setupVirtualNetworkTopology(); |
| 488 | |
| 489 | TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class); |
| 490 | Topology topology = topologyService.currentTopology(); |
| 491 | |
| 492 | VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1); |
| 493 | VirtualDevice dstVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID2); |
| 494 | |
| 495 | // test the getDisjointPaths() method using a null risk profile. |
| 496 | Set<DisjointPath> paths = topologyService.getDisjointPaths(topology, srcVirtualDevice.id(), |
| 497 | dstVirtualDevice.id(), (Map<Link, Object>) null); |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * Test getDisjointPaths() methods. |
| 502 | */ |
| 503 | @Test |
| 504 | public void testGetDisjointPaths() { |
| 505 | VirtualNetwork virtualNetwork = setupVirtualNetworkTopology(); |
| 506 | |
| 507 | TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class); |
| 508 | Topology topology = topologyService.currentTopology(); |
| 509 | |
| 510 | VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1); |
| 511 | VirtualDevice dstVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID2); |
| 512 | |
| 513 | // test the getDisjointPaths() method. |
| 514 | Set<DisjointPath> paths = topologyService.getDisjointPaths(topology, srcVirtualDevice.id(), |
| 515 | dstVirtualDevice.id()); |
| 516 | assertNotNull("The paths should not be null.", paths); |
| 517 | assertEquals("The paths size did not match.", 1, paths.size()); |
| 518 | |
| 519 | // test the getDisjointPaths() method using a weight. |
| 520 | LinkWeight weight = edge -> 1.0; |
| 521 | Set<DisjointPath> paths1 = topologyService.getDisjointPaths(topology, srcVirtualDevice.id(), |
| 522 | dstVirtualDevice.id(), weight); |
| 523 | assertNotNull("The paths should not be null.", paths1); |
| 524 | assertEquals("The paths size did not match.", 1, paths1.size()); |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * Test isInfrastructure() method using a null connect point. |
| 529 | */ |
| 530 | @Test(expected = NullPointerException.class) |
| 531 | public void testIsInfrastructureUsingNullConnectPoint() { |
| 532 | VirtualNetwork virtualNetwork = setupVirtualNetworkTopology(); |
| 533 | |
| 534 | TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class); |
| 535 | Topology topology = topologyService.currentTopology(); |
| 536 | |
| 537 | // test the isInfrastructure() method using a null connect point. |
| 538 | Boolean isInfrastructure = topologyService.isInfrastructure(topology, null); |
| 539 | } |
| 540 | |
| 541 | /** |
| 542 | * Test isInfrastructure() method. |
| 543 | */ |
| 544 | @Test |
| 545 | public void testIsInfrastructure() { |
| 546 | VirtualNetwork virtualNetwork = setupVirtualNetworkTopology(); |
| 547 | |
| 548 | TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class); |
| 549 | Topology topology = topologyService.currentTopology(); |
| 550 | |
| 551 | VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1); |
| 552 | VirtualDevice dstVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID4); |
| 553 | ConnectPoint cp1 = new ConnectPoint(srcVirtualDevice.id(), PortNumber.portNumber(1)); |
| 554 | ConnectPoint cp2 = new ConnectPoint(dstVirtualDevice.id(), PortNumber.portNumber(2)); |
| 555 | |
| 556 | // test the isInfrastructure() method. |
| 557 | Boolean isInfrastructure = topologyService.isInfrastructure(topology, cp1); |
| 558 | assertTrue("The connect point should be infrastructure.", isInfrastructure); |
| 559 | |
| 560 | isInfrastructure = topologyService.isInfrastructure(topology, cp2); |
| 561 | assertFalse("The connect point should not be infrastructure.", isInfrastructure); |
| 562 | } |
| 563 | |
| 564 | /** |
| 565 | * Test isBroadcastPoint() method using a null connect point. |
| 566 | */ |
| 567 | @Test(expected = NullPointerException.class) |
| 568 | public void testIsBroadcastUsingNullConnectPoint() { |
| 569 | VirtualNetwork virtualNetwork = setupVirtualNetworkTopology(); |
| 570 | |
| 571 | TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class); |
| 572 | Topology topology = topologyService.currentTopology(); |
| 573 | |
| 574 | // test the isInfrastructure() method using a null connect point. |
| 575 | Boolean isInfrastructure = topologyService.isBroadcastPoint(topology, null); |
| 576 | } |
| 577 | |
| 578 | /** |
| 579 | * Test isBroadcastPoint() method. |
| 580 | */ |
| 581 | @Test |
| 582 | public void testIsBroadcastPoint() { |
| 583 | VirtualNetwork virtualNetwork = setupVirtualNetworkTopology(); |
| 584 | |
| 585 | TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class); |
| 586 | Topology topology = topologyService.currentTopology(); |
| 587 | |
| 588 | VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1); |
| 589 | ConnectPoint cp = new ConnectPoint(srcVirtualDevice.id(), PortNumber.portNumber(1)); |
| 590 | |
| 591 | // test the isBroadcastPoint() method. |
| 592 | Boolean isBroadcastPoint = topologyService.isBroadcastPoint(topology, cp); |
| 593 | assertTrue("The connect point should be a broadcast point.", isBroadcastPoint); |
| 594 | } |
| 595 | |
| 596 | /** |
| 597 | * Return the virtual device matching the device identifier. |
| 598 | * |
| 599 | * @param networkId virtual network identifier |
| 600 | * @param deviceId device identifier |
| 601 | * @return virtual device |
| 602 | */ |
| 603 | private VirtualDevice getVirtualDevice(NetworkId networkId, DeviceId deviceId) { |
| 604 | Optional<VirtualDevice> foundDevice = manager.getVirtualDevices(networkId) |
| 605 | .stream() |
| 606 | .filter(device -> deviceId.equals(device.id())) |
| 607 | .findFirst(); |
| 608 | if (foundDevice.isPresent()) { |
| 609 | return foundDevice.get(); |
| 610 | } |
| 611 | return null; |
| 612 | } |
| 613 | |
| 614 | /** |
| 615 | * Core service test class. |
| 616 | */ |
| 617 | private class TestCoreService extends CoreServiceAdapter { |
| 618 | |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame^] | 619 | ApplicationId appId; |
| 620 | |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 621 | @Override |
| 622 | public IdGenerator getIdGenerator(String topic) { |
| 623 | return new IdGenerator() { |
| 624 | private AtomicLong counter = new AtomicLong(0); |
| 625 | |
| 626 | @Override |
| 627 | public long getNewId() { |
| 628 | return counter.getAndIncrement(); |
| 629 | } |
| 630 | }; |
| 631 | } |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame^] | 632 | |
| 633 | @Override |
| 634 | public ApplicationId registerApplication(String name) { |
| 635 | appId = new DefaultApplicationId(1, name); |
| 636 | return appId; |
| 637 | } |
| 638 | |
| 639 | @Override |
| 640 | public ApplicationId getAppId(String name) { |
| 641 | return appId; |
| 642 | } |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 643 | } |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame^] | 644 | |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 645 | } |