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