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