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