Brian Stanke
Committed by Gerrit Code Review

ONOS-2184 - Adding VirtualNetworkTopologyService using Virtual networks.

Change-Id: Ib7071314b49e23dc67459f70e7ca345f8a97db4e
......@@ -35,6 +35,8 @@ public abstract class TestDeviceParams {
protected static final ProviderId PID = new ProviderId("of", "foo");
protected static final DeviceId DID1 = deviceId("of:foo");
protected static final DeviceId DID2 = deviceId("of:bar");
protected static final DeviceId DID3 = deviceId("of:who");
protected static final DeviceId DID4 = deviceId("of:what");
protected static final MacAddress MAC1 = MacAddress.valueOf("00:11:00:00:00:01");
protected static final MacAddress MAC2 = MacAddress.valueOf("00:22:00:00:00:02");
protected static final VlanId VLAN1 = VlanId.vlanId((short) 11);
......
COMPILE_DEPS = [
'//lib:CORE_DEPS',
'//core/common:onos-core-common',
'//incubator/api:onos-incubator-api',
]
......
......@@ -47,6 +47,7 @@ import org.onosproject.net.device.DeviceService;
import org.onosproject.net.link.LinkService;
import org.onosproject.net.provider.AbstractListenerProviderRegistry;
import org.onosproject.net.provider.AbstractProviderService;
import org.onosproject.net.topology.TopologyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -287,6 +288,8 @@ public class VirtualNetworkManager
service = new VirtualNetworkDeviceService(this, network);
} else if (serviceKey.serviceClass.equals(LinkService.class)) {
service = new VirtualNetworkLinkService(this, network);
} else if (serviceKey.serviceClass.equals(TopologyService.class)) {
service = new VirtualNetworkTopologyService(this, network);
} else {
return null;
}
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.incubator.net.virtual.impl;
import org.onosproject.common.DefaultTopology;
import org.onosproject.event.AbstractListenerManager;
import org.onosproject.incubator.net.virtual.VirtualNetwork;
import org.onosproject.incubator.net.virtual.VirtualNetworkService;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.Device;
import org.onosproject.net.DeviceId;
import org.onosproject.net.DisjointPath;
import org.onosproject.net.Link;
import org.onosproject.net.Path;
import org.onosproject.net.topology.ClusterId;
import org.onosproject.net.topology.DefaultGraphDescription;
import org.onosproject.net.topology.LinkWeight;
import org.onosproject.net.topology.Topology;
import org.onosproject.net.topology.TopologyCluster;
import org.onosproject.net.topology.TopologyEvent;
import org.onosproject.net.topology.TopologyGraph;
import org.onosproject.net.topology.TopologyListener;
import org.onosproject.net.topology.TopologyService;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.onosproject.incubator.net.virtual.DefaultVirtualLink.PID;
/**
* Topology service implementation built on the virtual network service.
*/
public class VirtualNetworkTopologyService extends AbstractListenerManager<TopologyEvent, TopologyListener>
implements TopologyService, VnetService {
private static final String NETWORK_NULL = "Network ID cannot be null";
private static final String TOPOLOGY_NULL = "Topology cannot be null";
private static final String DEVICE_ID_NULL = "Device ID cannot be null";
private static final String CLUSTER_ID_NULL = "Cluster ID cannot be null";
private static final String CLUSTER_NULL = "Topology cluster cannot be null";
private static final String CONNECTION_POINT_NULL = "Connection point cannot be null";
private static final String LINK_WEIGHT_NULL = "Link weight cannot be null";
private final VirtualNetwork network;
private final VirtualNetworkService manager;
/**
* Creates a new VirtualNetworkTopologyService object.
*
* @param virtualNetworkManager virtual network manager service
* @param network virtual network
*/
public VirtualNetworkTopologyService(VirtualNetworkService virtualNetworkManager, VirtualNetwork network) {
checkNotNull(network, NETWORK_NULL);
this.network = network;
this.manager = virtualNetworkManager;
}
@Override
public Topology currentTopology() {
Iterable<Device> devices = manager.getVirtualDevices(network().id())
.stream()
.collect(Collectors.toSet());
Iterable<Link> links = manager.getVirtualLinks(network().id())
.stream()
.collect(Collectors.toSet());
DefaultGraphDescription graph = new DefaultGraphDescription(System.nanoTime(), System.currentTimeMillis(),
devices, links);
return new DefaultTopology(PID, graph);
}
@Override
public boolean isLatest(Topology topology) {
Topology currentTopology = currentTopology();
return defaultTopology(topology).getGraph().equals(defaultTopology(currentTopology).getGraph());
}
@Override
public TopologyGraph getGraph(Topology topology) {
return defaultTopology(topology).getGraph();
}
// Validates the specified topology and returns it as a default
private DefaultTopology defaultTopology(Topology topology) {
checkNotNull(topology, TOPOLOGY_NULL);
checkArgument(topology instanceof DefaultTopology,
"Topology class %s not supported", topology.getClass());
return (DefaultTopology) topology;
}
@Override
public Set<TopologyCluster> getClusters(Topology topology) {
return defaultTopology(topology).getClusters();
}
@Override
public TopologyCluster getCluster(Topology topology, ClusterId clusterId) {
checkNotNull(clusterId, CLUSTER_ID_NULL);
return defaultTopology(topology).getCluster(clusterId);
}
@Override
public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) {
checkNotNull(cluster, CLUSTER_NULL);
return defaultTopology(topology).getClusterDevices(cluster);
}
@Override
public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) {
checkNotNull(cluster, CLUSTER_NULL);
return defaultTopology(topology).getClusterLinks(cluster);
}
@Override
public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) {
checkNotNull(src, DEVICE_ID_NULL);
checkNotNull(dst, DEVICE_ID_NULL);
return defaultTopology(topology).getPaths(src, dst);
}
@Override
public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst, LinkWeight weight) {
checkNotNull(src, DEVICE_ID_NULL);
checkNotNull(dst, DEVICE_ID_NULL);
checkNotNull(weight, LINK_WEIGHT_NULL);
return defaultTopology(topology).getPaths(src, dst, weight);
}
@Override
public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst) {
checkNotNull(src, DEVICE_ID_NULL);
checkNotNull(dst, DEVICE_ID_NULL);
return defaultTopology(topology).getDisjointPaths(src, dst);
}
@Override
public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst, LinkWeight weight) {
checkNotNull(src, DEVICE_ID_NULL);
checkNotNull(dst, DEVICE_ID_NULL);
checkNotNull(weight, LINK_WEIGHT_NULL);
return defaultTopology(topology).getDisjointPaths(src, dst, weight);
}
@Override
public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst,
Map<Link, Object> riskProfile) {
checkNotNull(src, DEVICE_ID_NULL);
checkNotNull(dst, DEVICE_ID_NULL);
return defaultTopology(topology).getDisjointPaths(src, dst, riskProfile);
}
@Override
public Set<DisjointPath> getDisjointPaths(Topology topology, DeviceId src, DeviceId dst,
LinkWeight weight, Map<Link, Object> riskProfile) {
checkNotNull(src, DEVICE_ID_NULL);
checkNotNull(dst, DEVICE_ID_NULL);
checkNotNull(weight, LINK_WEIGHT_NULL);
return defaultTopology(topology).getDisjointPaths(src, dst, weight, riskProfile);
}
@Override
public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) {
checkNotNull(connectPoint, CONNECTION_POINT_NULL);
return defaultTopology(topology).isInfrastructure(connectPoint);
}
@Override
public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) {
checkNotNull(connectPoint, CONNECTION_POINT_NULL);
return defaultTopology(topology).isBroadcastPoint(connectPoint);
}
@Override
public VirtualNetwork network() {
return network;
}
}
......@@ -32,11 +32,11 @@ import org.onosproject.incubator.net.virtual.VirtualPort;
import org.onosproject.incubator.store.virtual.impl.DistributedVirtualNetworkStore;
import org.onosproject.net.DefaultPort;
import org.onosproject.net.Device;
import org.onosproject.net.DeviceId;
import org.onosproject.net.MastershipRole;
import org.onosproject.net.NetTestTools;
import org.onosproject.net.Port;
import org.onosproject.net.PortNumber;
import org.onosproject.net.TestDeviceParams;
import org.onosproject.net.device.DeviceService;
import org.onosproject.store.service.TestStorageService;
......@@ -48,11 +48,8 @@ import static org.junit.Assert.*;
/**
* Junit tests for VirtualNetworkDeviceService.
*/
public class VirtualNetworkDeviceServiceTest {
public class VirtualNetworkDeviceServiceTest extends TestDeviceParams {
private final String tenantIdValue1 = "TENANT_ID1";
private final String deviceIdValue1 = "DEVICE_ID1";
private final String deviceIdValue2 = "DEVICE_ID2";
private final String deviceIdValue3 = "DEVICE_ID3";
private VirtualNetworkManager manager;
private DistributedVirtualNetworkStore virtualNetworkManagerStore;
......@@ -88,8 +85,8 @@ public class VirtualNetworkDeviceServiceTest {
public void testGetDevices() {
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
VirtualDevice device1 = manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue1));
VirtualDevice device2 = manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue2));
VirtualDevice device1 = manager.createVirtualDevice(virtualNetwork.id(), DID1);
VirtualDevice device2 = manager.createVirtualDevice(virtualNetwork.id(), DID2);
DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class);
......@@ -106,15 +103,15 @@ public class VirtualNetworkDeviceServiceTest {
// test the getDevice() method
assertEquals("The expect device did not match.", device1,
deviceService.getDevice(DeviceId.deviceId(deviceIdValue1)));
deviceService.getDevice(DID1));
assertNotEquals("The expect device should not have matched.", device1,
deviceService.getDevice(DeviceId.deviceId(deviceIdValue2)));
deviceService.getDevice(DID2));
// test the isAvailable() method
assertTrue("The expect device availability did not match.",
deviceService.isAvailable(DeviceId.deviceId(deviceIdValue1)));
deviceService.isAvailable(DID1));
assertFalse("The expect device availability did not match.",
deviceService.isAvailable(DeviceId.deviceId(deviceIdValue3)));
deviceService.isAvailable(DID3));
}
/**
......@@ -163,8 +160,8 @@ public class VirtualNetworkDeviceServiceTest {
public void testGetDeviceType() {
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue1));
manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue2));
manager.createVirtualDevice(virtualNetwork.id(), DID1);
manager.createVirtualDevice(virtualNetwork.id(), DID2);
DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class);
......@@ -203,7 +200,7 @@ public class VirtualNetworkDeviceServiceTest {
// test the getRole() method
assertEquals("The expect device role did not match.", MastershipRole.MASTER,
deviceService.getRole(DeviceId.deviceId(deviceIdValue1)));
deviceService.getRole(DID1));
}
/**
......@@ -226,9 +223,8 @@ public class VirtualNetworkDeviceServiceTest {
public void testGetPorts() {
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
VirtualDevice virtualDevice = manager.createVirtualDevice(virtualNetwork.id(),
DeviceId.deviceId(deviceIdValue1));
manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue2));
VirtualDevice virtualDevice = manager.createVirtualDevice(virtualNetwork.id(), DID1);
manager.createVirtualDevice(virtualNetwork.id(), DID2);
DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class);
......@@ -239,9 +235,9 @@ public class VirtualNetworkDeviceServiceTest {
// test the getPorts() method
assertEquals("The port set size did not match.", 2,
deviceService.getPorts(DeviceId.deviceId(deviceIdValue1)).size());
deviceService.getPorts(DID1).size());
assertEquals("The port set size did not match.", 0,
deviceService.getPorts(DeviceId.deviceId(deviceIdValue2)).size());
deviceService.getPorts(DID2).size());
}
/**
......@@ -251,9 +247,8 @@ public class VirtualNetworkDeviceServiceTest {
public void testGetPort() {
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
VirtualDevice virtualDevice = manager.createVirtualDevice(virtualNetwork.id(),
DeviceId.deviceId(deviceIdValue1));
manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue2));
VirtualDevice virtualDevice = manager.createVirtualDevice(virtualNetwork.id(), DID1);
manager.createVirtualDevice(virtualNetwork.id(), DID2);
DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class);
......@@ -265,9 +260,9 @@ public class VirtualNetworkDeviceServiceTest {
// test the getPort() method
assertEquals("The port did not match as expected.", virtualPort1,
deviceService.getPort(DeviceId.deviceId(deviceIdValue1), PortNumber.portNumber(1)));
deviceService.getPort(DID1, PortNumber.portNumber(1)));
assertNotEquals("The port did not match as expected.", virtualPort1,
deviceService.getPort(DeviceId.deviceId(deviceIdValue1), PortNumber.portNumber(3)));
deviceService.getPort(DID1, PortNumber.portNumber(3)));
}
/**
......@@ -290,15 +285,14 @@ public class VirtualNetworkDeviceServiceTest {
public void testGetPortStatistics() {
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
VirtualDevice virtualDevice = manager.createVirtualDevice(virtualNetwork.id(),
DeviceId.deviceId(deviceIdValue1));
manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue2));
VirtualDevice virtualDevice = manager.createVirtualDevice(virtualNetwork.id(), DID1);
manager.createVirtualDevice(virtualNetwork.id(), DID2);
DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class);
// test the getPortStatistics() method
assertEquals("The port statistics set size did not match.", 0,
deviceService.getPortStatistics(DeviceId.deviceId(deviceIdValue1)).size());
deviceService.getPortStatistics(DID1).size());
}
/**
......@@ -321,15 +315,14 @@ public class VirtualNetworkDeviceServiceTest {
public void testGetPortDeltaStatistics() {
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
VirtualDevice virtualDevice = manager.createVirtualDevice(virtualNetwork.id(),
DeviceId.deviceId(deviceIdValue1));
manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue2));
VirtualDevice virtualDevice = manager.createVirtualDevice(virtualNetwork.id(), DID1);
manager.createVirtualDevice(virtualNetwork.id(), DID2);
DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class);
// test the getPortDeltaStatistics() method
assertEquals("The port delta statistics set size did not match.", 0,
deviceService.getPortDeltaStatistics(DeviceId.deviceId(deviceIdValue1)).size());
deviceService.getPortDeltaStatistics(DID1).size());
}
/**
......
......@@ -31,10 +31,10 @@ import org.onosproject.incubator.net.virtual.VirtualLink;
import org.onosproject.incubator.net.virtual.VirtualNetwork;
import org.onosproject.incubator.store.virtual.impl.DistributedVirtualNetworkStore;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.DeviceId;
import org.onosproject.net.Link;
import org.onosproject.net.NetTestTools;
import org.onosproject.net.PortNumber;
import org.onosproject.net.TestDeviceParams;
import org.onosproject.net.link.LinkService;
import org.onosproject.store.service.TestStorageService;
......@@ -47,12 +47,9 @@ import static org.junit.Assert.assertNotEquals;
/**
* Junit tests for VirtualNetworkLinkService.
*/
public class VirtualNetworkLinkServiceTest {
public class VirtualNetworkLinkServiceTest extends TestDeviceParams {
private final String tenantIdValue1 = "TENANT_ID1";
private final String deviceIdValue1 = "DEVICE_ID1";
private final String deviceIdValue2 = "DEVICE_ID2";
private final String deviceIdValue3 = "DEVICE_ID3";
private VirtualNetworkManager manager;
private DistributedVirtualNetworkStore virtualNetworkManagerStore;
......@@ -92,9 +89,9 @@ public class VirtualNetworkLinkServiceTest {
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
VirtualDevice srcVirtualDevice =
manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue1));
manager.createVirtualDevice(virtualNetwork.id(), DID1);
VirtualDevice dstVirtualDevice =
manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue2));
manager.createVirtualDevice(virtualNetwork.id(), DID2);
ConnectPoint src = new ConnectPoint(srcVirtualDevice.id(), PortNumber.portNumber(1));
ConnectPoint dst = new ConnectPoint(dstVirtualDevice.id(), PortNumber.portNumber(2));
VirtualLink link1 = manager.createVirtualLink(virtualNetwork.id(), src, dst);
......@@ -134,27 +131,27 @@ public class VirtualNetworkLinkServiceTest {
// test the getDeviceLinks() method
assertEquals("The link set size did not match.", 2,
linkService.getDeviceLinks(DeviceId.deviceId(deviceIdValue1)).size());
linkService.getDeviceLinks(DID1).size());
assertEquals("The link set size did not match.", 2,
linkService.getDeviceLinks(DeviceId.deviceId(deviceIdValue2)).size());
linkService.getDeviceLinks(DID2).size());
assertEquals("The link set size did not match.", 0,
linkService.getDeviceLinks(DeviceId.deviceId(deviceIdValue3)).size());
linkService.getDeviceLinks(DID3).size());
// test the getDeviceEgressLinks() method
assertEquals("The link set size did not match.", 1,
linkService.getDeviceEgressLinks(DeviceId.deviceId(deviceIdValue1)).size());
linkService.getDeviceEgressLinks(DID1).size());
assertEquals("The link set size did not match.", 1,
linkService.getDeviceEgressLinks(DeviceId.deviceId(deviceIdValue2)).size());
linkService.getDeviceEgressLinks(DID2).size());
assertEquals("The link set size did not match.", 0,
linkService.getDeviceEgressLinks(DeviceId.deviceId(deviceIdValue3)).size());
linkService.getDeviceEgressLinks(DID3).size());
// test the getDeviceIngressLinks() method
assertEquals("The link set size did not match.", 1,
linkService.getDeviceIngressLinks(DeviceId.deviceId(deviceIdValue1)).size());
linkService.getDeviceIngressLinks(DID1).size());
assertEquals("The link set size did not match.", 1,
linkService.getDeviceIngressLinks(DeviceId.deviceId(deviceIdValue2)).size());
linkService.getDeviceIngressLinks(DID2).size());
assertEquals("The link set size did not match.", 0,
linkService.getDeviceIngressLinks(DeviceId.deviceId(deviceIdValue3)).size());
linkService.getDeviceIngressLinks(DID3).size());
// test the getEgressLinks() method
assertEquals("The link set size did not match.", 1,
......@@ -182,9 +179,9 @@ public class VirtualNetworkLinkServiceTest {
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
VirtualDevice srcVirtualDevice =
manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue1));
manager.createVirtualDevice(virtualNetwork.id(), DID1);
VirtualDevice dstVirtualDevice =
manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue2));
manager.createVirtualDevice(virtualNetwork.id(), DID2);
ConnectPoint src = new ConnectPoint(srcVirtualDevice.id(), PortNumber.portNumber(1));
ConnectPoint dst = new ConnectPoint(dstVirtualDevice.id(), PortNumber.portNumber(2));
manager.createVirtualLink(virtualNetwork.id(), src, dst);
......@@ -205,9 +202,9 @@ public class VirtualNetworkLinkServiceTest {
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
VirtualDevice srcVirtualDevice =
manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue1));
manager.createVirtualDevice(virtualNetwork.id(), DID1);
VirtualDevice dstVirtualDevice =
manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue2));
manager.createVirtualDevice(virtualNetwork.id(), DID2);
ConnectPoint src = new ConnectPoint(srcVirtualDevice.id(), PortNumber.portNumber(1));
ConnectPoint dst = new ConnectPoint(dstVirtualDevice.id(), PortNumber.portNumber(2));
manager.createVirtualLink(virtualNetwork.id(), src, dst);
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.incubator.net.virtual.impl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onlab.junit.TestUtils;
import org.onosproject.common.event.impl.TestEventDispatcher;
import org.onosproject.core.CoreService;
import org.onosproject.core.CoreServiceAdapter;
import org.onosproject.core.IdGenerator;
import org.onosproject.incubator.net.virtual.NetworkId;
import org.onosproject.incubator.net.virtual.TenantId;
import org.onosproject.incubator.net.virtual.VirtualDevice;
import org.onosproject.incubator.net.virtual.VirtualLink;
import org.onosproject.incubator.net.virtual.VirtualNetwork;
import org.onosproject.incubator.store.virtual.impl.DistributedVirtualNetworkStore;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.DeviceId;
import org.onosproject.net.DisjointPath;
import org.onosproject.net.Link;
import org.onosproject.net.NetTestTools;
import org.onosproject.net.Path;
import org.onosproject.net.PortNumber;
import org.onosproject.net.TestDeviceParams;
import org.onosproject.net.topology.LinkWeight;
import org.onosproject.net.topology.Topology;
import org.onosproject.net.topology.TopologyCluster;
import org.onosproject.net.topology.TopologyService;
import org.onosproject.store.service.TestStorageService;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.*;
/**
* Junit tests for VirtualNetworkTopologyService.
*/
public class VirtualNetworkTopologyServiceTest extends TestDeviceParams {
private final String tenantIdValue1 = "TENANT_ID1";
private VirtualNetworkManager manager;
private DistributedVirtualNetworkStore virtualNetworkManagerStore;
private CoreService coreService;
@Before
public void setUp() throws Exception {
virtualNetworkManagerStore = new DistributedVirtualNetworkStore();
coreService = new VirtualNetworkTopologyServiceTest.TestCoreService();
virtualNetworkManagerStore.setCoreService(coreService);
TestUtils.setField(coreService, "coreService", new VirtualNetworkTopologyServiceTest.TestCoreService());
TestUtils.setField(virtualNetworkManagerStore, "storageService", new TestStorageService());
virtualNetworkManagerStore.activate();
manager = new VirtualNetworkManager();
manager.store = virtualNetworkManagerStore;
NetTestTools.injectEventDispatcher(manager, new TestEventDispatcher());
manager.activate();
}
@After
public void tearDown() {
virtualNetworkManagerStore.deactivate();
manager.deactivate();
NetTestTools.injectEventDispatcher(manager, null);
}
/**
* Method to create the virtual network for further testing.
*
* @return virtual network
*/
private VirtualNetwork setupVirtualNetworkTopology() {
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
VirtualDevice virtualDevice1 =
manager.createVirtualDevice(virtualNetwork.id(), DID1);
VirtualDevice virtualDevice2 =
manager.createVirtualDevice(virtualNetwork.id(), DID2);
VirtualDevice virtualDevice3 =
manager.createVirtualDevice(virtualNetwork.id(), DID3);
VirtualDevice virtualDevice4 =
manager.createVirtualDevice(virtualNetwork.id(), DID4);
ConnectPoint cp1 = new ConnectPoint(virtualDevice1.id(), PortNumber.portNumber(1));
ConnectPoint cp2 = new ConnectPoint(virtualDevice1.id(), PortNumber.portNumber(2));
ConnectPoint cp3 = new ConnectPoint(virtualDevice2.id(), PortNumber.portNumber(3));
ConnectPoint cp4 = new ConnectPoint(virtualDevice2.id(), PortNumber.portNumber(4));
ConnectPoint cp5 = new ConnectPoint(virtualDevice3.id(), PortNumber.portNumber(5));
ConnectPoint cp6 = new ConnectPoint(virtualDevice3.id(), PortNumber.portNumber(6));
VirtualLink link1 = manager.createVirtualLink(virtualNetwork.id(), cp1, cp3);
virtualNetworkManagerStore.updateLink(link1, link1.tunnelId(), Link.State.ACTIVE);
VirtualLink link2 = manager.createVirtualLink(virtualNetwork.id(), cp3, cp1);
virtualNetworkManagerStore.updateLink(link2, link2.tunnelId(), Link.State.ACTIVE);
VirtualLink link3 = manager.createVirtualLink(virtualNetwork.id(), cp4, cp5);
virtualNetworkManagerStore.updateLink(link3, link3.tunnelId(), Link.State.ACTIVE);
VirtualLink link4 = manager.createVirtualLink(virtualNetwork.id(), cp5, cp4);
virtualNetworkManagerStore.updateLink(link4, link4.tunnelId(), Link.State.ACTIVE);
VirtualLink link5 = manager.createVirtualLink(virtualNetwork.id(), cp2, cp6);
virtualNetworkManagerStore.updateLink(link5, link5.tunnelId(), Link.State.ACTIVE);
VirtualLink link6 = manager.createVirtualLink(virtualNetwork.id(), cp6, cp2);
virtualNetworkManagerStore.updateLink(link6, link6.tunnelId(), Link.State.ACTIVE);
return virtualNetwork;
}
/**
* Tests the currentTopology() method.
*/
@Test
public void testCurrentTopology() {
VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
Topology topology = topologyService.currentTopology();
assertNotNull("The topology should not be null.", topology);
}
/**
* Test isLatest() method using a null topology.
*/
@Test(expected = NullPointerException.class)
public void testIsLatestByNullTopology() {
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
// test the isLatest() method with a null topology.
topologyService.isLatest(null);
}
/**
* Test isLatest() method.
*/
@Test
public void testIsLatest() {
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
Topology topology = topologyService.currentTopology();
// test the isLatest() method.
assertTrue("This should be latest topology", topologyService.isLatest(topology));
VirtualDevice srcVirtualDevice =
manager.createVirtualDevice(virtualNetwork.id(), DID1);
VirtualDevice dstVirtualDevice =
manager.createVirtualDevice(virtualNetwork.id(), DID2);
// test the isLatest() method where a new device has been added to the current topology.
assertFalse("This should not be latest topology", topologyService.isLatest(topology));
topology = topologyService.currentTopology();
ConnectPoint src = new ConnectPoint(srcVirtualDevice.id(), PortNumber.portNumber(1));
ConnectPoint dst = new ConnectPoint(dstVirtualDevice.id(), PortNumber.portNumber(2));
VirtualLink link1 = manager.createVirtualLink(virtualNetwork.id(), src, dst);
// test the isLatest() method where a new link has been added to the current topology.
assertFalse("This should not be latest topology", topologyService.isLatest(topology));
}
/**
* Test getGraph() method.
*/
@Test
public void testGetGraph() {
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
Topology topology = topologyService.currentTopology();
// test the getGraph() method.
assertNotNull("The graph should not be null.", topologyService.getGraph(topology));
}
/**
* Test getClusters() method.
*/
@Test
public void testGetClusters() {
VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
Topology topology = topologyService.currentTopology();
// test the getClusters() method.
assertNotNull("The clusters should not be null.", topologyService.getClusters(topology));
assertEquals("The clusters size did not match.", 2, topologyService.getClusters(topology).size());
}
/**
* Test getCluster() method using a null cluster identifier.
*/
@Test(expected = NullPointerException.class)
public void testGetClusterUsingNullClusterId() {
VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
Topology topology = topologyService.currentTopology();
Set<TopologyCluster> clusters = topologyService.getClusters(topology);
TopologyCluster cluster = clusters.stream().findFirst().get();
// test the getCluster() method with a null cluster identifier
TopologyCluster cluster1 = topologyService.getCluster(topology, null);
}
/**
* Test getCluster() method.
*/
@Test
public void testGetCluster() {
VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
Topology topology = topologyService.currentTopology();
Set<TopologyCluster> clusters = topologyService.getClusters(topology);
assertNotNull("The clusters should not be null.", clusters);
assertEquals("The clusters size did not match.", 2, clusters.size());
// test the getCluster() method.
TopologyCluster cluster = clusters.stream().findFirst().get();
assertNotNull("The cluster should not be null.", cluster);
TopologyCluster cluster1 = topologyService.getCluster(topology, cluster.id());
assertNotNull("The cluster should not be null.", cluster1);
assertEquals("The cluster ID did not match.", cluster.id(), cluster1.id());
}
/**
* Test getClusterDevices() methods with a null cluster.
*/
@Test(expected = NullPointerException.class)
public void testGetClusterDevicesUsingNullCluster() {
VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
Topology topology = topologyService.currentTopology();
Set<TopologyCluster> clusters = topologyService.getClusters(topology);
// test the getClusterDevices() method using a null cluster.
Object[] objects = clusters.stream().toArray();
assertNotNull("The cluster should not be null.", objects);
Set<DeviceId> clusterDevices = topologyService.getClusterDevices(topology, null);
}
/**
* Test getClusterLinks() methods with a null cluster.
*/
@Test(expected = NullPointerException.class)
public void testGetClusterLinksUsingNullCluster() {
VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
Topology topology = topologyService.currentTopology();
Set<TopologyCluster> clusters = topologyService.getClusters(topology);
// test the getClusterLinks() method using a null cluster.
Object[] objects = clusters.stream().toArray();
assertNotNull("The cluster should not be null.", objects);
Set<Link> clusterLinks = topologyService.getClusterLinks(topology, null);
}
/**
* Test getClusterDevices() and getClusterLinks() methods.
*/
@Test
public void testGetClusterDevicesLinks() {
VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
Topology topology = topologyService.currentTopology();
Set<TopologyCluster> clusters = topologyService.getClusters(topology);
assertNotNull("The clusters should not be null.", clusters);
assertEquals("The clusters size did not match.", 2, clusters.size());
// test the getClusterDevices() method.
Object[] objects = clusters.stream().toArray();
assertNotNull("The cluster should not be null.", objects);
Set<DeviceId> clusterDevices = topologyService.getClusterDevices(topology, (TopologyCluster) objects[0]);
assertNotNull("The devices should not be null.", clusterDevices);
assertEquals("The devices size did not match.", 3, clusterDevices.size());
Set<DeviceId> clusterDevices1 = topologyService.getClusterDevices(topology, (TopologyCluster) objects[1]);
assertNotNull("The devices should not be null.", clusterDevices1);
assertEquals("The devices size did not match.", 1, clusterDevices1.size());
// test the getClusterLinks() method.
Set<Link> clusterLinks = topologyService.getClusterLinks(topology, (TopologyCluster) objects[0]);
assertNotNull("The links should not be null.", clusterLinks);
assertEquals("The links size did not match.", 6, clusterLinks.size());
Set<Link> clusterLinks1 = topologyService.getClusterLinks(topology, (TopologyCluster) objects[1]);
assertNotNull("The links should not be null.", clusterLinks1);
assertEquals("The links size did not match.", 0, clusterLinks1.size());
}
/**
* Test getPaths() method using a null src device identifier.
*/
@Test(expected = NullPointerException.class)
public void testGetPathsUsingNullSrcDeviceId() {
VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
Topology topology = topologyService.currentTopology();
VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1);
VirtualDevice dstVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID2);
// test the getPaths() method using a null src device identifier.
Set<Path> paths = topologyService.getPaths(topology, null, dstVirtualDevice.id());
}
/**
* Test getPaths() method using a null dst device identifier.
*/
@Test(expected = NullPointerException.class)
public void testGetPathsUsingNullDstDeviceId() {
VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
Topology topology = topologyService.currentTopology();
VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1);
VirtualDevice dstVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID2);
// test the getPaths() method using a null dst device identifier.
Set<Path> paths = topologyService.getPaths(topology, srcVirtualDevice.id(), null);
}
/**
* Test getPaths() method using a null weight.
*/
@Test(expected = NullPointerException.class)
public void testGetPathsUsingNullWeight() {
VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
Topology topology = topologyService.currentTopology();
VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1);
VirtualDevice dstVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID2);
// test the getPaths() method using a null weight.
Set<Path> paths = topologyService.getPaths(topology, srcVirtualDevice.id(), dstVirtualDevice.id(), null);
}
/**
* Test getPaths() and getPaths() by weight methods.
*/
@Test
public void testGetPaths() {
VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
Topology topology = topologyService.currentTopology();
VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1);
VirtualDevice dstVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID2);
// test the getPaths() method.
Set<Path> paths = topologyService.getPaths(topology, srcVirtualDevice.id(), dstVirtualDevice.id());
assertNotNull("The paths should not be null.", paths);
assertEquals("The paths size did not match.", 1, paths.size());
// test the getPaths() by weight method.
LinkWeight weight = edge -> 1.0;
Set<Path> paths1 = topologyService.getPaths(topology, srcVirtualDevice.id(), dstVirtualDevice.id(), weight);
assertNotNull("The paths should not be null.", paths1);
assertEquals("The paths size did not match.", 1, paths1.size());
Path path = paths1.iterator().next();
assertEquals("wrong path length", 1, path.links().size());
assertEquals("wrong path cost", 1.0, path.cost(), 0.01);
}
/**
* Test getDisjointPaths() methods using a null src device identifier.
*/
@Test(expected = NullPointerException.class)
public void testGetDisjointPathsUsingNullSrcDeviceId() {
VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
Topology topology = topologyService.currentTopology();
VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1);
VirtualDevice dstVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID2);
// test the getDisjointPaths() method using a null src device identifier.
Set<DisjointPath> paths = topologyService.getDisjointPaths(topology, null, dstVirtualDevice.id());
}
/**
* Test getDisjointPaths() methods using a null dst device identifier.
*/
@Test(expected = NullPointerException.class)
public void testGetDisjointPathsUsingNullDstDeviceId() {
VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
Topology topology = topologyService.currentTopology();
VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1);
VirtualDevice dstVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID2);
// test the getDisjointPaths() method using a null dst device identifier.
Set<DisjointPath> paths = topologyService.getDisjointPaths(topology, srcVirtualDevice.id(), null);
}
/**
* Test getDisjointPaths() methods using a null weight.
*/
@Test(expected = NullPointerException.class)
public void testGetDisjointPathsUsingNullWeight() {
VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
Topology topology = topologyService.currentTopology();
VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1);
VirtualDevice dstVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID2);
// test the getDisjointPaths() method using a null weight.
Set<DisjointPath> paths = topologyService.getDisjointPaths(topology, srcVirtualDevice.id(),
dstVirtualDevice.id(), (LinkWeight) null);
}
/**
* Test getDisjointPaths() methods using a null risk profile.
*/
@Test(expected = NullPointerException.class)
public void testGetDisjointPathsUsingNullRiskProfile() {
VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
Topology topology = topologyService.currentTopology();
VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1);
VirtualDevice dstVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID2);
// test the getDisjointPaths() method using a null risk profile.
Set<DisjointPath> paths = topologyService.getDisjointPaths(topology, srcVirtualDevice.id(),
dstVirtualDevice.id(), (Map<Link, Object>) null);
}
/**
* Test getDisjointPaths() methods.
*/
@Test
public void testGetDisjointPaths() {
VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
Topology topology = topologyService.currentTopology();
VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1);
VirtualDevice dstVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID2);
// test the getDisjointPaths() method.
Set<DisjointPath> paths = topologyService.getDisjointPaths(topology, srcVirtualDevice.id(),
dstVirtualDevice.id());
assertNotNull("The paths should not be null.", paths);
assertEquals("The paths size did not match.", 1, paths.size());
// test the getDisjointPaths() method using a weight.
LinkWeight weight = edge -> 1.0;
Set<DisjointPath> paths1 = topologyService.getDisjointPaths(topology, srcVirtualDevice.id(),
dstVirtualDevice.id(), weight);
assertNotNull("The paths should not be null.", paths1);
assertEquals("The paths size did not match.", 1, paths1.size());
}
/**
* Test isInfrastructure() method using a null connect point.
*/
@Test(expected = NullPointerException.class)
public void testIsInfrastructureUsingNullConnectPoint() {
VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
Topology topology = topologyService.currentTopology();
// test the isInfrastructure() method using a null connect point.
Boolean isInfrastructure = topologyService.isInfrastructure(topology, null);
}
/**
* Test isInfrastructure() method.
*/
@Test
public void testIsInfrastructure() {
VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
Topology topology = topologyService.currentTopology();
VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1);
VirtualDevice dstVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID4);
ConnectPoint cp1 = new ConnectPoint(srcVirtualDevice.id(), PortNumber.portNumber(1));
ConnectPoint cp2 = new ConnectPoint(dstVirtualDevice.id(), PortNumber.portNumber(2));
// test the isInfrastructure() method.
Boolean isInfrastructure = topologyService.isInfrastructure(topology, cp1);
assertTrue("The connect point should be infrastructure.", isInfrastructure);
isInfrastructure = topologyService.isInfrastructure(topology, cp2);
assertFalse("The connect point should not be infrastructure.", isInfrastructure);
}
/**
* Test isBroadcastPoint() method using a null connect point.
*/
@Test(expected = NullPointerException.class)
public void testIsBroadcastUsingNullConnectPoint() {
VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
Topology topology = topologyService.currentTopology();
// test the isInfrastructure() method using a null connect point.
Boolean isInfrastructure = topologyService.isBroadcastPoint(topology, null);
}
/**
* Test isBroadcastPoint() method.
*/
@Test
public void testIsBroadcastPoint() {
VirtualNetwork virtualNetwork = setupVirtualNetworkTopology();
TopologyService topologyService = manager.get(virtualNetwork.id(), TopologyService.class);
Topology topology = topologyService.currentTopology();
VirtualDevice srcVirtualDevice = getVirtualDevice(virtualNetwork.id(), DID1);
ConnectPoint cp = new ConnectPoint(srcVirtualDevice.id(), PortNumber.portNumber(1));
// test the isBroadcastPoint() method.
Boolean isBroadcastPoint = topologyService.isBroadcastPoint(topology, cp);
assertTrue("The connect point should be a broadcast point.", isBroadcastPoint);
}
/**
* Return the virtual device matching the device identifier.
*
* @param networkId virtual network identifier
* @param deviceId device identifier
* @return virtual device
*/
private VirtualDevice getVirtualDevice(NetworkId networkId, DeviceId deviceId) {
Optional<VirtualDevice> foundDevice = manager.getVirtualDevices(networkId)
.stream()
.filter(device -> deviceId.equals(device.id()))
.findFirst();
if (foundDevice.isPresent()) {
return foundDevice.get();
}
return null;
}
/**
* Core service test class.
*/
private class TestCoreService extends CoreServiceAdapter {
@Override
public IdGenerator getIdGenerator(String topic) {
return new IdGenerator() {
private AtomicLong counter = new AtomicLong(0);
@Override
public long getNewId() {
return counter.getAndIncrement();
}
};
}
}
}