blob: 802bc0fca7985e74ccc1f87693d18d2d58069ca0 [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;
34import org.onosproject.net.DeviceId;
35import org.onosproject.net.DisjointPath;
36import org.onosproject.net.Link;
37import org.onosproject.net.NetTestTools;
38import org.onosproject.net.Path;
39import org.onosproject.net.PortNumber;
40import org.onosproject.net.TestDeviceParams;
Brian Stanke11f6d532016-07-05 16:17:59 -040041import org.onosproject.net.intent.FakeIntentManager;
42import org.onosproject.net.intent.TestableIntentService;
Brian Stanke8e9f8d12016-06-08 14:48:33 -040043import org.onosproject.net.topology.LinkWeight;
44import org.onosproject.net.topology.Topology;
45import org.onosproject.net.topology.TopologyCluster;
46import org.onosproject.net.topology.TopologyService;
47import org.onosproject.store.service.TestStorageService;
48
49import java.util.Map;
50import java.util.Optional;
51import java.util.Set;
52import java.util.concurrent.atomic.AtomicLong;
53
54import static junit.framework.TestCase.assertTrue;
55import static org.junit.Assert.*;
56
57/**
58 * Junit tests for VirtualNetworkTopologyService.
59 */
60public 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 Stanke11f6d532016-07-05 16:17:59 -040067 private TestableIntentService intentService = new FakeIntentManager();
Brian Stanke8e9f8d12016-06-08 14:48:33 -040068
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 Stanke11f6d532016-07-05 16:17:59 -040081 manager.intentService = intentService;
Brian Stanke8e9f8d12016-06-08 14:48:33 -040082 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));
111 ConnectPoint cp2 = new ConnectPoint(virtualDevice1.id(), PortNumber.portNumber(2));
112 ConnectPoint cp3 = new ConnectPoint(virtualDevice2.id(), PortNumber.portNumber(3));
113 ConnectPoint cp4 = new ConnectPoint(virtualDevice2.id(), PortNumber.portNumber(4));
114 ConnectPoint cp5 = new ConnectPoint(virtualDevice3.id(), PortNumber.portNumber(5));
115 ConnectPoint cp6 = new ConnectPoint(virtualDevice3.id(), PortNumber.portNumber(6));
116 VirtualLink link1 = manager.createVirtualLink(virtualNetwork.id(), cp1, cp3);
117 virtualNetworkManagerStore.updateLink(link1, link1.tunnelId(), Link.State.ACTIVE);
118 VirtualLink link2 = manager.createVirtualLink(virtualNetwork.id(), cp3, cp1);
119 virtualNetworkManagerStore.updateLink(link2, link2.tunnelId(), Link.State.ACTIVE);
120 VirtualLink link3 = manager.createVirtualLink(virtualNetwork.id(), cp4, cp5);
121 virtualNetworkManagerStore.updateLink(link3, link3.tunnelId(), Link.State.ACTIVE);
122 VirtualLink link4 = manager.createVirtualLink(virtualNetwork.id(), cp5, cp4);
123 virtualNetworkManagerStore.updateLink(link4, link4.tunnelId(), Link.State.ACTIVE);
124 VirtualLink link5 = manager.createVirtualLink(virtualNetwork.id(), cp2, cp6);
125 virtualNetworkManagerStore.updateLink(link5, link5.tunnelId(), Link.State.ACTIVE);
126 VirtualLink link6 = manager.createVirtualLink(virtualNetwork.id(), cp6, cp2);
127 virtualNetworkManagerStore.updateLink(link6, link6.tunnelId(), Link.State.ACTIVE);
128
129 return virtualNetwork;
130 }
131
132 /**
133 * Tests the currentTopology() method.
134 */
135 @Test
136 public void testCurrentTopology() {
137 VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
138
139 TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
140 Topology topology = topologyService.currentTopology();
141 assertNotNull("The topology should not be null.", topology);
142 }
143
144 /**
145 * Test isLatest() method using a null topology.
146 */
147 @Test(expected = NullPointerException.class)
148 public void testIsLatestByNullTopology() {
149 manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
150 VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
151 TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
152
153 // test the isLatest() method with a null topology.
154 topologyService.isLatest(null);
155 }
156
157 /**
158 * Test isLatest() method.
159 */
160 @Test
161 public void testIsLatest() {
162 manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
163 VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
164 TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
165 Topology topology = topologyService.currentTopology();
166
167 // test the isLatest() method.
168 assertTrue("This should be latest topology", topologyService.isLatest(topology));
169
170 VirtualDevice srcVirtualDevice =
171 manager.createVirtualDevice(virtualNetwork.id(), DID1);
172 VirtualDevice dstVirtualDevice =
173 manager.createVirtualDevice(virtualNetwork.id(), DID2);
174
175 // test the isLatest() method where a new device has been added to the current topology.
176 assertFalse("This should not be latest topology", topologyService.isLatest(topology));
177
178 topology = topologyService.currentTopology();
179 ConnectPoint src = new ConnectPoint(srcVirtualDevice.id(), PortNumber.portNumber(1));
180 ConnectPoint dst = new ConnectPoint(dstVirtualDevice.id(), PortNumber.portNumber(2));
181 VirtualLink link1 = manager.createVirtualLink(virtualNetwork.id(), src, dst);
182
183 // test the isLatest() method where a new link has been added to the current topology.
184 assertFalse("This should not be latest topology", topologyService.isLatest(topology));
185 }
186
187 /**
188 * Test getGraph() method.
189 */
190 @Test
191 public void testGetGraph() {
192 manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
193 VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
194 TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
195 Topology topology = topologyService.currentTopology();
196
197 // test the getGraph() method.
198 assertNotNull("The graph should not be null.", topologyService.getGraph(topology));
199 }
200
201 /**
202 * Test getClusters() method.
203 */
204 @Test
205 public void testGetClusters() {
206 VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
207
208 TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
209
210 Topology topology = topologyService.currentTopology();
211
212 // test the getClusters() method.
213 assertNotNull("The clusters should not be null.", topologyService.getClusters(topology));
214 assertEquals("The clusters size did not match.", 2, topologyService.getClusters(topology).size());
215 }
216
217 /**
218 * Test getCluster() method using a null cluster identifier.
219 */
220 @Test(expected = NullPointerException.class)
221 public void testGetClusterUsingNullClusterId() {
222 VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
223
224 TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
225 Topology topology = topologyService.currentTopology();
226
227 Set<TopologyCluster> clusters = topologyService.getClusters(topology);
228 TopologyCluster cluster = clusters.stream().findFirst().get();
229
230 // test the getCluster() method with a null cluster identifier
231 TopologyCluster cluster1 = topologyService.getCluster(topology, null);
232 }
233
234 /**
235 * Test getCluster() method.
236 */
237 @Test
238 public void testGetCluster() {
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 assertNotNull("The clusters should not be null.", clusters);
246 assertEquals("The clusters size did not match.", 2, clusters.size());
247
248 // test the getCluster() method.
249 TopologyCluster cluster = clusters.stream().findFirst().get();
250 assertNotNull("The cluster should not be null.", cluster);
251 TopologyCluster cluster1 = topologyService.getCluster(topology, cluster.id());
252 assertNotNull("The cluster should not be null.", cluster1);
253 assertEquals("The cluster ID did not match.", cluster.id(), cluster1.id());
254 }
255
256 /**
257 * Test getClusterDevices() methods with a null cluster.
258 */
259 @Test(expected = NullPointerException.class)
260 public void testGetClusterDevicesUsingNullCluster() {
261 VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
262
263 TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
264 Topology topology = topologyService.currentTopology();
265 Set<TopologyCluster> clusters = topologyService.getClusters(topology);
266
267 // test the getClusterDevices() method using a null cluster.
268 Object[] objects = clusters.stream().toArray();
269 assertNotNull("The cluster should not be null.", objects);
270 Set<DeviceId> clusterDevices = topologyService.getClusterDevices(topology, null);
271 }
272
273 /**
274 * Test getClusterLinks() methods with a null cluster.
275 */
276 @Test(expected = NullPointerException.class)
277 public void testGetClusterLinksUsingNullCluster() {
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 getClusterLinks() method using a null cluster.
285 Object[] objects = clusters.stream().toArray();
286 assertNotNull("The cluster should not be null.", objects);
287 Set<Link> clusterLinks = topologyService.getClusterLinks(topology, null);
288 }
289
290 /**
291 * Test getClusterDevices() and getClusterLinks() methods.
292 */
293 @Test
294 public void testGetClusterDevicesLinks() {
295 VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
296
297 TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
298 Topology topology = topologyService.currentTopology();
299
300 Set<TopologyCluster> clusters = topologyService.getClusters(topology);
301 assertNotNull("The clusters should not be null.", clusters);
302 assertEquals("The clusters size did not match.", 2, clusters.size());
303
304 // test the getClusterDevices() method.
305 Object[] objects = clusters.stream().toArray();
306 assertNotNull("The cluster should not be null.", objects);
307 Set<DeviceId> clusterDevices = topologyService.getClusterDevices(topology, (TopologyCluster) objects[0]);
308 assertNotNull("The devices should not be null.", clusterDevices);
309 assertEquals("The devices size did not match.", 3, clusterDevices.size());
310 Set<DeviceId> clusterDevices1 = topologyService.getClusterDevices(topology, (TopologyCluster) objects[1]);
311 assertNotNull("The devices should not be null.", clusterDevices1);
312 assertEquals("The devices size did not match.", 1, clusterDevices1.size());
313
314 // test the getClusterLinks() method.
315 Set<Link> clusterLinks = topologyService.getClusterLinks(topology, (TopologyCluster) objects[0]);
316 assertNotNull("The links should not be null.", clusterLinks);
317 assertEquals("The links size did not match.", 6, clusterLinks.size());
318 Set<Link> clusterLinks1 = topologyService.getClusterLinks(topology, (TopologyCluster) objects[1]);
319 assertNotNull("The links should not be null.", clusterLinks1);
320 assertEquals("The links size did not match.", 0, clusterLinks1.size());
321 }
322
323 /**
324 * Test getPaths() method using a null src device identifier.
325 */
326 @Test(expected = NullPointerException.class)
327 public void testGetPathsUsingNullSrcDeviceId() {
328 VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
329
330 TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
331 Topology topology = topologyService.currentTopology();
332
333 VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1);
334 VirtualDevice dstVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID2);
335
336 // test the getPaths() method using a null src device identifier.
337 Set<Path> paths = topologyService.getPaths(topology, null, dstVirtualDevice.id());
338 }
339
340 /**
341 * Test getPaths() method using a null dst device identifier.
342 */
343 @Test(expected = NullPointerException.class)
344 public void testGetPathsUsingNullDstDeviceId() {
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 dst device identifier.
354 Set<Path> paths = topologyService.getPaths(topology, srcVirtualDevice.id(), null);
355 }
356
357 /**
358 * Test getPaths() method using a null weight.
359 */
360 @Test(expected = NullPointerException.class)
361 public void testGetPathsUsingNullWeight() {
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 weight.
371 Set<Path> paths = topologyService.getPaths(topology, srcVirtualDevice.id(), dstVirtualDevice.id(), null);
372 }
373
374 /**
375 * Test getPaths() and getPaths() by weight methods.
376 */
377 @Test
378 public void testGetPaths() {
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.
388 Set<Path> paths = topologyService.getPaths(topology, srcVirtualDevice.id(), dstVirtualDevice.id());
389 assertNotNull("The paths should not be null.", paths);
390 assertEquals("The paths size did not match.", 1, paths.size());
391
392 // test the getPaths() by weight method.
393 LinkWeight weight = edge -> 1.0;
394 Set<Path> paths1 = topologyService.getPaths(topology, srcVirtualDevice.id(), dstVirtualDevice.id(), weight);
395 assertNotNull("The paths should not be null.", paths1);
396 assertEquals("The paths size did not match.", 1, paths1.size());
397 Path path = paths1.iterator().next();
398 assertEquals("wrong path length", 1, path.links().size());
399 assertEquals("wrong path cost", 1.0, path.cost(), 0.01);
400 }
401
402 /**
403 * Test getDisjointPaths() methods using a null src device identifier.
404 */
405 @Test(expected = NullPointerException.class)
406 public void testGetDisjointPathsUsingNullSrcDeviceId() {
407 VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
408
409 TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
410 Topology topology = topologyService.currentTopology();
411
412 VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1);
413 VirtualDevice dstVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID2);
414
415 // test the getDisjointPaths() method using a null src device identifier.
416 Set<DisjointPath> paths = topologyService.getDisjointPaths(topology, null, dstVirtualDevice.id());
417 }
418
419 /**
420 * Test getDisjointPaths() methods using a null dst device identifier.
421 */
422 @Test(expected = NullPointerException.class)
423 public void testGetDisjointPathsUsingNullDstDeviceId() {
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 dst device identifier.
433 Set<DisjointPath> paths = topologyService.getDisjointPaths(topology, srcVirtualDevice.id(), null);
434 }
435
436 /**
437 * Test getDisjointPaths() methods using a null weight.
438 */
439 @Test(expected = NullPointerException.class)
440 public void testGetDisjointPathsUsingNullWeight() {
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 weight.
450 Set<DisjointPath> paths = topologyService.getDisjointPaths(topology, srcVirtualDevice.id(),
451 dstVirtualDevice.id(), (LinkWeight) null);
452 }
453
454 /**
455 * Test getDisjointPaths() methods using a null risk profile.
456 */
457 @Test(expected = NullPointerException.class)
458 public void testGetDisjointPathsUsingNullRiskProfile() {
459 VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
460
461 TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
462 Topology topology = topologyService.currentTopology();
463
464 VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1);
465 VirtualDevice dstVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID2);
466
467 // test the getDisjointPaths() method using a null risk profile.
468 Set<DisjointPath> paths = topologyService.getDisjointPaths(topology, srcVirtualDevice.id(),
469 dstVirtualDevice.id(), (Map<Link, Object>) null);
470 }
471
472 /**
473 * Test getDisjointPaths() methods.
474 */
475 @Test
476 public void testGetDisjointPaths() {
477 VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
478
479 TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
480 Topology topology = topologyService.currentTopology();
481
482 VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1);
483 VirtualDevice dstVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID2);
484
485 // test the getDisjointPaths() method.
486 Set<DisjointPath> paths = topologyService.getDisjointPaths(topology, srcVirtualDevice.id(),
487 dstVirtualDevice.id());
488 assertNotNull("The paths should not be null.", paths);
489 assertEquals("The paths size did not match.", 1, paths.size());
490
491 // test the getDisjointPaths() method using a weight.
492 LinkWeight weight = edge -> 1.0;
493 Set<DisjointPath> paths1 = topologyService.getDisjointPaths(topology, srcVirtualDevice.id(),
494 dstVirtualDevice.id(), weight);
495 assertNotNull("The paths should not be null.", paths1);
496 assertEquals("The paths size did not match.", 1, paths1.size());
497 }
498
499 /**
500 * Test isInfrastructure() method using a null connect point.
501 */
502 @Test(expected = NullPointerException.class)
503 public void testIsInfrastructureUsingNullConnectPoint() {
504 VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
505
506 TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
507 Topology topology = topologyService.currentTopology();
508
509 // test the isInfrastructure() method using a null connect point.
510 Boolean isInfrastructure = topologyService.isInfrastructure(topology, null);
511 }
512
513 /**
514 * Test isInfrastructure() method.
515 */
516 @Test
517 public void testIsInfrastructure() {
518 VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
519
520 TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
521 Topology topology = topologyService.currentTopology();
522
523 VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1);
524 VirtualDevice dstVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID4);
525 ConnectPoint cp1 = new ConnectPoint(srcVirtualDevice.id(), PortNumber.portNumber(1));
526 ConnectPoint cp2 = new ConnectPoint(dstVirtualDevice.id(), PortNumber.portNumber(2));
527
528 // test the isInfrastructure() method.
529 Boolean isInfrastructure = topologyService.isInfrastructure(topology, cp1);
530 assertTrue("The connect point should be infrastructure.", isInfrastructure);
531
532 isInfrastructure = topologyService.isInfrastructure(topology, cp2);
533 assertFalse("The connect point should not be infrastructure.", isInfrastructure);
534 }
535
536 /**
537 * Test isBroadcastPoint() method using a null connect point.
538 */
539 @Test(expected = NullPointerException.class)
540 public void testIsBroadcastUsingNullConnectPoint() {
541 VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
542
543 TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
544 Topology topology = topologyService.currentTopology();
545
546 // test the isInfrastructure() method using a null connect point.
547 Boolean isInfrastructure = topologyService.isBroadcastPoint(topology, null);
548 }
549
550 /**
551 * Test isBroadcastPoint() method.
552 */
553 @Test
554 public void testIsBroadcastPoint() {
555 VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
556
557 TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
558 Topology topology = topologyService.currentTopology();
559
560 VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1);
561 ConnectPoint cp = new ConnectPoint(srcVirtualDevice.id(), PortNumber.portNumber(1));
562
563 // test the isBroadcastPoint() method.
564 Boolean isBroadcastPoint = topologyService.isBroadcastPoint(topology, cp);
565 assertTrue("The connect point should be a broadcast point.", isBroadcastPoint);
566 }
567
568 /**
569 * Return the virtual device matching the device identifier.
570 *
571 * @param networkId virtual network identifier
572 * @param deviceId device identifier
573 * @return virtual device
574 */
575 private VirtualDevice getVirtualDevice(NetworkId networkId, DeviceId deviceId) {
576 Optional<VirtualDevice> foundDevice = manager.getVirtualDevices(networkId)
577 .stream()
578 .filter(device -> deviceId.equals(device.id()))
579 .findFirst();
580 if (foundDevice.isPresent()) {
581 return foundDevice.get();
582 }
583 return null;
584 }
585
586 /**
587 * Core service test class.
588 */
589 private class TestCoreService extends CoreServiceAdapter {
590
591 @Override
592 public IdGenerator getIdGenerator(String topic) {
593 return new IdGenerator() {
594 private AtomicLong counter = new AtomicLong(0);
595
596 @Override
597 public long getNewId() {
598 return counter.getAndIncrement();
599 }
600 };
601 }
602 }
603}