blob: 11bfdc83b4238d2ca7801f4dde879765c9b65182 [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;
Andrey Komarov2398d962016-09-26 15:11:23 +030045import org.onosproject.net.topology.LinkWeigher;
Brian Stanke8e9f8d12016-06-08 14:48:33 -040046import org.onosproject.net.topology.LinkWeight;
47import org.onosproject.net.topology.Topology;
48import org.onosproject.net.topology.TopologyCluster;
49import org.onosproject.net.topology.TopologyService;
50import org.onosproject.store.service.TestStorageService;
51
52import java.util.Map;
53import java.util.Optional;
54import java.util.Set;
55import java.util.concurrent.atomic.AtomicLong;
56
57import static junit.framework.TestCase.assertTrue;
58import static org.junit.Assert.*;
59
60/**
61 * Junit tests for VirtualNetworkTopologyService.
62 */
yoonseon214963b2016-11-21 15:41:07 -080063public class VirtualNetworkTopologyManagerTest extends TestDeviceParams {
Brian Stanke8e9f8d12016-06-08 14:48:33 -040064
65 private final String tenantIdValue1 = "TENANT_ID1";
66
67 private VirtualNetworkManager manager;
68 private DistributedVirtualNetworkStore virtualNetworkManagerStore;
69 private CoreService coreService;
Brian Stanke11f6d532016-07-05 16:17:59 -040070 private TestableIntentService intentService = new FakeIntentManager();
yoonseonc6a69272017-01-12 18:22:20 -080071 private TestServiceDirectory testDirectory;
Brian Stanke8e9f8d12016-06-08 14:48:33 -040072
73 @Before
74 public void setUp() throws Exception {
75 virtualNetworkManagerStore = new DistributedVirtualNetworkStore();
yoonseon214963b2016-11-21 15:41:07 -080076 coreService = new VirtualNetworkTopologyManagerTest.TestCoreService();
yoonseonc6a69272017-01-12 18:22:20 -080077 TestUtils.setField(virtualNetworkManagerStore, "coreService", coreService);
Brian Stanke8e9f8d12016-06-08 14:48:33 -040078 TestUtils.setField(virtualNetworkManagerStore, "storageService", new TestStorageService());
79 virtualNetworkManagerStore.activate();
80
yoonseonc6a69272017-01-12 18:22:20 -080081 BaseResource.setServiceDirectory(testDirectory);
Brian Stanke8e9f8d12016-06-08 14:48:33 -040082 manager = new VirtualNetworkManager();
83 manager.store = virtualNetworkManagerStore;
Brian Stanke11f6d532016-07-05 16:17:59 -040084 manager.intentService = intentService;
Brian Stanke8e9f8d12016-06-08 14:48:33 -040085 NetTestTools.injectEventDispatcher(manager, new TestEventDispatcher());
yoonseonc6a69272017-01-12 18:22:20 -080086
87 testDirectory = new TestServiceDirectory();
88 TestUtils.setField(manager, "serviceDirectory", testDirectory);
89
Brian Stanke8e9f8d12016-06-08 14:48:33 -040090 manager.activate();
91 }
92
93 @After
94 public void tearDown() {
95 virtualNetworkManagerStore.deactivate();
96 manager.deactivate();
97 NetTestTools.injectEventDispatcher(manager, null);
98 }
99
100 /**
101 * Method to create the virtual network for further testing.
102 *
103 * @return virtual network
104 */
105 private VirtualNetwork setupVirtualNetworkTopology() {
106 manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
107 VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
108 VirtualDevice virtualDevice1 =
109 manager.createVirtualDevice(virtualNetwork.id(), DID1);
110 VirtualDevice virtualDevice2 =
111 manager.createVirtualDevice(virtualNetwork.id(), DID2);
112 VirtualDevice virtualDevice3 =
113 manager.createVirtualDevice(virtualNetwork.id(), DID3);
114 VirtualDevice virtualDevice4 =
115 manager.createVirtualDevice(virtualNetwork.id(), DID4);
116
117 ConnectPoint cp1 = new ConnectPoint(virtualDevice1.id(), PortNumber.portNumber(1));
Yoonseon Han6c603892016-09-01 11:52:21 -0700118 manager.createVirtualPort(virtualNetwork.id(), cp1.deviceId(), cp1.port(), cp1);
Brian Stankefb61df42016-07-25 11:47:51 -0400119
Brian Stanke8e9f8d12016-06-08 14:48:33 -0400120 ConnectPoint cp2 = new ConnectPoint(virtualDevice1.id(), PortNumber.portNumber(2));
Yoonseon Han6c603892016-09-01 11:52:21 -0700121 manager.createVirtualPort(virtualNetwork.id(), cp2.deviceId(), cp2.port(), cp2);
Brian Stankefb61df42016-07-25 11:47:51 -0400122
Brian Stanke8e9f8d12016-06-08 14:48:33 -0400123 ConnectPoint cp3 = new ConnectPoint(virtualDevice2.id(), PortNumber.portNumber(3));
Yoonseon Han6c603892016-09-01 11:52:21 -0700124 manager.createVirtualPort(virtualNetwork.id(), cp3.deviceId(), cp3.port(), cp3);
Brian Stankefb61df42016-07-25 11:47:51 -0400125
Brian Stanke8e9f8d12016-06-08 14:48:33 -0400126 ConnectPoint cp4 = new ConnectPoint(virtualDevice2.id(), PortNumber.portNumber(4));
Yoonseon Han6c603892016-09-01 11:52:21 -0700127 manager.createVirtualPort(virtualNetwork.id(), cp4.deviceId(), cp4.port(), cp4);
Brian Stankefb61df42016-07-25 11:47:51 -0400128
Brian Stanke8e9f8d12016-06-08 14:48:33 -0400129 ConnectPoint cp5 = new ConnectPoint(virtualDevice3.id(), PortNumber.portNumber(5));
Yoonseon Han6c603892016-09-01 11:52:21 -0700130 manager.createVirtualPort(virtualNetwork.id(), cp5.deviceId(), cp5.port(), cp5);
Brian Stankefb61df42016-07-25 11:47:51 -0400131
Brian Stanke8e9f8d12016-06-08 14:48:33 -0400132 ConnectPoint cp6 = new ConnectPoint(virtualDevice3.id(), PortNumber.portNumber(6));
Yoonseon Han6c603892016-09-01 11:52:21 -0700133 manager.createVirtualPort(virtualNetwork.id(), cp6.deviceId(), cp6.port(), cp6);
Brian Stankefb61df42016-07-25 11:47:51 -0400134
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(),
Yoonseon Han6c603892016-09-01 11:52:21 -0700200 new ConnectPoint(srcVirtualDevice.id(), src.port()));
Brian Stankefb61df42016-07-25 11:47:51 -0400201
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(),
Yoonseon Han6c603892016-09-01 11:52:21 -0700204 new ConnectPoint(dstVirtualDevice.id(), dst.port()));
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.
Andrey Komarov2398d962016-09-26 15:11:23 +0300395 Set<Path> paths = topologyService.getPaths(topology, srcVirtualDevice.id(),
396 dstVirtualDevice.id(), (LinkWeigher) null);
Brian Stanke8e9f8d12016-06-08 14:48:33 -0400397 }
398
399 /**
400 * Test getPaths() and getPaths() by weight methods.
401 */
402 @Test
403 public void testGetPaths() {
404 VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
405
406 TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
407 Topology topology = topologyService.currentTopology();
408
409 VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1);
410 VirtualDevice dstVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID2);
411
412 // test the getPaths() method.
413 Set<Path> paths = topologyService.getPaths(topology, srcVirtualDevice.id(), dstVirtualDevice.id());
414 assertNotNull("The paths should not be null.", paths);
415 assertEquals("The paths size did not match.", 1, paths.size());
416
417 // test the getPaths() by weight method.
418 LinkWeight weight = edge -> 1.0;
419 Set<Path> paths1 = topologyService.getPaths(topology, srcVirtualDevice.id(), dstVirtualDevice.id(), weight);
420 assertNotNull("The paths should not be null.", paths1);
421 assertEquals("The paths size did not match.", 1, paths1.size());
422 Path path = paths1.iterator().next();
423 assertEquals("wrong path length", 1, path.links().size());
424 assertEquals("wrong path cost", 1.0, path.cost(), 0.01);
425 }
426
427 /**
428 * Test getDisjointPaths() methods using a null src device identifier.
429 */
430 @Test(expected = NullPointerException.class)
431 public void testGetDisjointPathsUsingNullSrcDeviceId() {
432 VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
433
434 TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
435 Topology topology = topologyService.currentTopology();
436
437 VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1);
438 VirtualDevice dstVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID2);
439
440 // test the getDisjointPaths() method using a null src device identifier.
441 Set<DisjointPath> paths = topologyService.getDisjointPaths(topology, null, dstVirtualDevice.id());
442 }
443
444 /**
445 * Test getDisjointPaths() methods using a null dst device identifier.
446 */
447 @Test(expected = NullPointerException.class)
448 public void testGetDisjointPathsUsingNullDstDeviceId() {
449 VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
450
451 TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
452 Topology topology = topologyService.currentTopology();
453
454 VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1);
455 VirtualDevice dstVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID2);
456
457 // test the getDisjointPaths() method using a null dst device identifier.
458 Set<DisjointPath> paths = topologyService.getDisjointPaths(topology, srcVirtualDevice.id(), null);
459 }
460
461 /**
462 * Test getDisjointPaths() methods using a null weight.
463 */
464 @Test(expected = NullPointerException.class)
465 public void testGetDisjointPathsUsingNullWeight() {
466 VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
467
468 TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
469 Topology topology = topologyService.currentTopology();
470
471 VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1);
472 VirtualDevice dstVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID2);
473
474 // test the getDisjointPaths() method using a null weight.
475 Set<DisjointPath> paths = topologyService.getDisjointPaths(topology, srcVirtualDevice.id(),
476 dstVirtualDevice.id(), (LinkWeight) null);
477 }
478
479 /**
480 * Test getDisjointPaths() methods using a null risk profile.
481 */
482 @Test(expected = NullPointerException.class)
483 public void testGetDisjointPathsUsingNullRiskProfile() {
484 VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
485
486 TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
487 Topology topology = topologyService.currentTopology();
488
489 VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1);
490 VirtualDevice dstVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID2);
491
492 // test the getDisjointPaths() method using a null risk profile.
493 Set<DisjointPath> paths = topologyService.getDisjointPaths(topology, srcVirtualDevice.id(),
494 dstVirtualDevice.id(), (Map<Link, Object>) null);
495 }
496
497 /**
498 * Test getDisjointPaths() methods.
499 */
500 @Test
501 public void testGetDisjointPaths() {
502 VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
503
504 TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
505 Topology topology = topologyService.currentTopology();
506
507 VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1);
508 VirtualDevice dstVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID2);
509
510 // test the getDisjointPaths() method.
511 Set<DisjointPath> paths = topologyService.getDisjointPaths(topology, srcVirtualDevice.id(),
512 dstVirtualDevice.id());
513 assertNotNull("The paths should not be null.", paths);
514 assertEquals("The paths size did not match.", 1, paths.size());
515
516 // test the getDisjointPaths() method using a weight.
517 LinkWeight weight = edge -> 1.0;
518 Set<DisjointPath> paths1 = topologyService.getDisjointPaths(topology, srcVirtualDevice.id(),
519 dstVirtualDevice.id(), weight);
520 assertNotNull("The paths should not be null.", paths1);
521 assertEquals("The paths size did not match.", 1, paths1.size());
522 }
523
524 /**
525 * Test isInfrastructure() method using a null connect point.
526 */
527 @Test(expected = NullPointerException.class)
528 public void testIsInfrastructureUsingNullConnectPoint() {
529 VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
530
531 TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
532 Topology topology = topologyService.currentTopology();
533
534 // test the isInfrastructure() method using a null connect point.
535 Boolean isInfrastructure = topologyService.isInfrastructure(topology, null);
536 }
537
538 /**
539 * Test isInfrastructure() method.
540 */
541 @Test
542 public void testIsInfrastructure() {
543 VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
544
545 TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
546 Topology topology = topologyService.currentTopology();
547
548 VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1);
549 VirtualDevice dstVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID4);
550 ConnectPoint cp1 = new ConnectPoint(srcVirtualDevice.id(), PortNumber.portNumber(1));
551 ConnectPoint cp2 = new ConnectPoint(dstVirtualDevice.id(), PortNumber.portNumber(2));
552
553 // test the isInfrastructure() method.
554 Boolean isInfrastructure = topologyService.isInfrastructure(topology, cp1);
555 assertTrue("The connect point should be infrastructure.", isInfrastructure);
556
557 isInfrastructure = topologyService.isInfrastructure(topology, cp2);
558 assertFalse("The connect point should not be infrastructure.", isInfrastructure);
559 }
560
561 /**
562 * Test isBroadcastPoint() method using a null connect point.
563 */
564 @Test(expected = NullPointerException.class)
565 public void testIsBroadcastUsingNullConnectPoint() {
566 VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
567
568 TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
569 Topology topology = topologyService.currentTopology();
570
571 // test the isInfrastructure() method using a null connect point.
572 Boolean isInfrastructure = topologyService.isBroadcastPoint(topology, null);
573 }
574
575 /**
576 * Test isBroadcastPoint() method.
577 */
578 @Test
579 public void testIsBroadcastPoint() {
580 VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
581
582 TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
583 Topology topology = topologyService.currentTopology();
584
585 VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1);
586 ConnectPoint cp = new ConnectPoint(srcVirtualDevice.id(), PortNumber.portNumber(1));
587
588 // test the isBroadcastPoint() method.
589 Boolean isBroadcastPoint = topologyService.isBroadcastPoint(topology, cp);
590 assertTrue("The connect point should be a broadcast point.", isBroadcastPoint);
591 }
592
593 /**
594 * Return the virtual device matching the device identifier.
595 *
596 * @param networkId virtual network identifier
597 * @param deviceId device identifier
598 * @return virtual device
599 */
600 private VirtualDevice getVirtualDevice(NetworkId networkId, DeviceId deviceId) {
601 Optional<VirtualDevice> foundDevice = manager.getVirtualDevices(networkId)
602 .stream()
603 .filter(device -> deviceId.equals(device.id()))
604 .findFirst();
605 if (foundDevice.isPresent()) {
606 return foundDevice.get();
607 }
608 return null;
609 }
610
611 /**
612 * Core service test class.
613 */
614 private class TestCoreService extends CoreServiceAdapter {
615
616 @Override
617 public IdGenerator getIdGenerator(String topic) {
618 return new IdGenerator() {
619 private AtomicLong counter = new AtomicLong(0);
620
621 @Override
622 public long getNewId() {
623 return counter.getAndIncrement();
624 }
625 };
626 }
627 }
628}