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