Claudine Chiu
Committed by Gerrit Code Review

ONOS-2184 - initial impl. of virtual network path service; move path related me…

…thods from PathManager to AbstractPathService (since they are reused for VirtualNetworkPathService).

Change-Id: Ib4211d69a15c23a50de420b771877bb659539c22
/*
*
* * 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.net.topology;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.DefaultDisjointPath;
import org.onosproject.net.DefaultEdgeLink;
import org.onosproject.net.DefaultPath;
import org.onosproject.net.DeviceId;
import org.onosproject.net.DisjointPath;
import org.onosproject.net.EdgeLink;
import org.onosproject.net.ElementId;
import org.onosproject.net.Host;
import org.onosproject.net.HostId;
import org.onosproject.net.HostLocation;
import org.onosproject.net.Link;
import org.onosproject.net.Path;
import org.onosproject.net.PortNumber;
import org.onosproject.net.host.HostService;
import org.onosproject.net.provider.ProviderId;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Helper class for path service.
*/
public abstract class AbstractPathService {
private static final String ELEMENT_ID_NULL = "Element ID cannot be null";
private static final EdgeLink NOT_HOST = new NotHost();
private static final ProviderId PID = new ProviderId("core", "org.onosproject.core");
private static final PortNumber P0 = PortNumber.portNumber(0);
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected TopologyService topologyService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected HostService hostService;
public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) {
checkNotNull(src, ELEMENT_ID_NULL);
checkNotNull(dst, ELEMENT_ID_NULL);
// Get the source and destination edge locations
EdgeLink srcEdge = getEdgeLink(src, true);
EdgeLink dstEdge = getEdgeLink(dst, false);
// If either edge is null, bail with no paths.
if (srcEdge == null || dstEdge == null) {
return ImmutableSet.of();
}
DeviceId srcDevice = srcEdge != NOT_HOST ? srcEdge.dst().deviceId() : (DeviceId) src;
DeviceId dstDevice = dstEdge != NOT_HOST ? dstEdge.src().deviceId() : (DeviceId) dst;
// If the source and destination are on the same edge device, there
// is just one path, so build it and return it.
if (srcDevice.equals(dstDevice)) {
return edgeToEdgePaths(srcEdge, dstEdge);
}
// Otherwise get all paths between the source and destination edge
// devices.
Topology topology = topologyService.currentTopology();
Set<Path> paths = weight == null ?
topologyService.getPaths(topology, srcDevice, dstDevice) :
topologyService.getPaths(topology, srcDevice, dstDevice, weight);
return edgeToEdgePaths(srcEdge, dstEdge, paths);
}
public Set<DisjointPath> getDisjointPaths(ElementId src, ElementId dst, LinkWeight weight) {
checkNotNull(src, ELEMENT_ID_NULL);
checkNotNull(dst, ELEMENT_ID_NULL);
// Get the source and destination edge locations
EdgeLink srcEdge = getEdgeLink(src, true);
EdgeLink dstEdge = getEdgeLink(dst, false);
// If either edge is null, bail with no paths.
if (srcEdge == null || dstEdge == null) {
return ImmutableSet.of();
}
DeviceId srcDevice = srcEdge != NOT_HOST ? srcEdge.dst().deviceId() : (DeviceId) src;
DeviceId dstDevice = dstEdge != NOT_HOST ? dstEdge.src().deviceId() : (DeviceId) dst;
// If the source and destination are on the same edge device, there
// is just one path, so build it and return it.
if (srcDevice.equals(dstDevice)) {
return edgeToEdgePathsDisjoint(srcEdge, dstEdge);
}
// Otherwise get all paths between the source and destination edge
// devices.
Topology topology = topologyService.currentTopology();
Set<DisjointPath> paths = weight == null ?
topologyService.getDisjointPaths(topology, srcDevice, dstDevice) :
topologyService.getDisjointPaths(topology, srcDevice, dstDevice, weight);
return edgeToEdgePathsDisjoint(srcEdge, dstEdge, paths);
}
public Set<DisjointPath> getDisjointPaths(ElementId src, ElementId dst, LinkWeight weight,
Map<Link, Object> riskProfile) {
checkNotNull(src, ELEMENT_ID_NULL);
checkNotNull(dst, ELEMENT_ID_NULL);
// Get the source and destination edge locations
EdgeLink srcEdge = getEdgeLink(src, true);
EdgeLink dstEdge = getEdgeLink(dst, false);
// If either edge is null, bail with no paths.
if (srcEdge == null || dstEdge == null) {
return ImmutableSet.of();
}
DeviceId srcDevice = srcEdge != NOT_HOST ? srcEdge.dst().deviceId() : (DeviceId) src;
DeviceId dstDevice = dstEdge != NOT_HOST ? dstEdge.src().deviceId() : (DeviceId) dst;
// If the source and destination are on the same edge device, there
// is just one path, so build it and return it.
if (srcDevice.equals(dstDevice)) {
return edgeToEdgePathsDisjoint(srcEdge, dstEdge);
}
// Otherwise get all paths between the source and destination edge
// devices.
Topology topology = topologyService.currentTopology();
Set<DisjointPath> paths = weight == null ?
topologyService.getDisjointPaths(topology, srcDevice, dstDevice, riskProfile) :
topologyService.getDisjointPaths(topology, srcDevice, dstDevice, weight, riskProfile);
return edgeToEdgePathsDisjoint(srcEdge, dstEdge, paths);
}
// Finds the host edge link if the element ID is a host id of an existing
// host. Otherwise, if the host does not exist, it returns null and if
// the element ID is not a host ID, returns NOT_HOST edge link.
private EdgeLink getEdgeLink(ElementId elementId, boolean isIngress) {
if (elementId instanceof HostId) {
// Resolve the host, return null.
Host host = hostService.getHost((HostId) elementId);
if (host == null) {
return null;
}
return new DefaultEdgeLink(PID, new ConnectPoint(elementId, P0),
host.location(), isIngress);
}
return NOT_HOST;
}
// Produces a set of edge-to-edge paths using the set of infrastructure
// paths and the given edge links.
private Set<Path> edgeToEdgePaths(EdgeLink srcLink, EdgeLink dstLink) {
Set<Path> endToEndPaths = Sets.newHashSetWithExpectedSize(1);
endToEndPaths.add(edgeToEdgePath(srcLink, dstLink, null));
return endToEndPaths;
}
// Produces a set of edge-to-edge paths using the set of infrastructure
// paths and the given edge links.
private Set<Path> edgeToEdgePaths(EdgeLink srcLink, EdgeLink dstLink, Set<Path> paths) {
Set<Path> endToEndPaths = Sets.newHashSetWithExpectedSize(paths.size());
for (Path path : paths) {
endToEndPaths.add(edgeToEdgePath(srcLink, dstLink, path));
}
return endToEndPaths;
}
private Set<DisjointPath> edgeToEdgePathsDisjoint(EdgeLink srcLink, EdgeLink dstLink) {
Set<DisjointPath> endToEndPaths = Sets.newHashSetWithExpectedSize(1);
endToEndPaths.add(edgeToEdgePathD(srcLink, dstLink, null));
return endToEndPaths;
}
private Set<DisjointPath> edgeToEdgePathsDisjoint(EdgeLink srcLink, EdgeLink dstLink,
Set<DisjointPath> paths) {
Set<DisjointPath> endToEndPaths = Sets.newHashSetWithExpectedSize(paths.size());
for (DisjointPath path : paths) {
endToEndPaths.add(edgeToEdgePathD(srcLink, dstLink, path));
}
return endToEndPaths;
}
// Produces a direct edge-to-edge path.
private Path edgeToEdgePath(EdgeLink srcLink, EdgeLink dstLink, Path path) {
List<Link> links = Lists.newArrayListWithCapacity(2);
double cost = 0;
// Add source and destination edge links only if they are real and
// add the infrastructure path only if it is not null.
if (srcLink != NOT_HOST) {
links.add(srcLink);
cost++;
}
if (path != null) {
links.addAll(path.links());
cost += path.cost();
}
if (dstLink != NOT_HOST) {
links.add(dstLink);
cost++;
}
return new DefaultPath(PID, links, cost);
}
// Produces a direct edge-to-edge path.
private DisjointPath edgeToEdgePathD(EdgeLink srcLink, EdgeLink dstLink, DisjointPath path) {
Path primary = null;
Path backup = null;
if (path != null) {
primary = path.primary();
backup = path.backup();
}
return new DefaultDisjointPath(PID, (DefaultPath) edgeToEdgePath(srcLink, dstLink, primary),
(DefaultPath) edgeToEdgePath(srcLink, dstLink, backup));
}
// Special value for edge link to represent that this is really not an
// edge link since the src or dst are really an infrastructure device.
private static class NotHost extends DefaultEdgeLink implements EdgeLink {
NotHost() {
super(PID, new ConnectPoint(HostId.NONE, P0),
new HostLocation(DeviceId.NONE, P0, 0L), false);
}
}
}
......@@ -15,43 +15,23 @@
*/
package org.onosproject.net.topology.impl;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.Service;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.DefaultEdgeLink;
import org.onosproject.net.DefaultPath;
import org.onosproject.net.DisjointPath;
import org.onosproject.net.DefaultDisjointPath;
import org.onosproject.net.DeviceId;
import org.onosproject.net.EdgeLink;
import org.onosproject.net.ElementId;
import org.onosproject.net.Host;
import org.onosproject.net.HostId;
import org.onosproject.net.HostLocation;
import org.onosproject.net.Link;
import org.onosproject.net.Path;
import org.onosproject.net.PortNumber;
import org.onosproject.net.host.HostService;
import org.onosproject.net.provider.ProviderId;
import org.onosproject.net.topology.LinkWeight;
import org.onosproject.net.topology.PathService;
import org.onosproject.net.topology.Topology;
import org.onosproject.net.topology.TopologyService;
import org.onosproject.net.topology.AbstractPathService;
import org.slf4j.Logger;
import java.util.List;
import java.util.Set;
import java.util.Map;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.slf4j.LoggerFactory.getLogger;
import static org.onosproject.security.AppGuard.checkPermission;
import static org.onosproject.security.AppPermission.Type.*;
......@@ -63,23 +43,11 @@ import static org.onosproject.security.AppPermission.Type.*;
*/
@Component(immediate = true)
@Service
public class PathManager implements PathService {
public class PathManager extends AbstractPathService implements PathService {
private static final String ELEMENT_ID_NULL = "Element ID cannot be null";
private static final ProviderId PID = new ProviderId("core", "org.onosproject.core");
private static final PortNumber P0 = PortNumber.portNumber(0);
private static final EdgeLink NOT_HOST = new NotHost();
private final Logger log = getLogger(getClass());
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected TopologyService topologyService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected HostService hostService;
@Activate
public void activate() {
log.info("Started");
......@@ -100,37 +68,9 @@ public class PathManager implements PathService {
@Override
public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) {
checkPermission(TOPOLOGY_READ);
checkNotNull(src, ELEMENT_ID_NULL);
checkNotNull(dst, ELEMENT_ID_NULL);
// Get the source and destination edge locations
EdgeLink srcEdge = getEdgeLink(src, true);
EdgeLink dstEdge = getEdgeLink(dst, false);
// If either edge is null, bail with no paths.
if (srcEdge == null || dstEdge == null) {
return ImmutableSet.of();
return super.getPaths(src, dst, weight);
}
DeviceId srcDevice = srcEdge != NOT_HOST ? srcEdge.dst().deviceId() : (DeviceId) src;
DeviceId dstDevice = dstEdge != NOT_HOST ? dstEdge.src().deviceId() : (DeviceId) dst;
// If the source and destination are on the same edge device, there
// is just one path, so build it and return it.
if (srcDevice.equals(dstDevice)) {
return edgeToEdgePaths(srcEdge, dstEdge);
}
// Otherwise get all paths between the source and destination edge
// devices.
Topology topology = topologyService.currentTopology();
Set<Path> paths = weight == null ?
topologyService.getPaths(topology, srcDevice, dstDevice) :
topologyService.getPaths(topology, srcDevice, dstDevice, weight);
return edgeToEdgePaths(srcEdge, dstEdge, paths);
}
@Override
public Set<DisjointPath> getDisjointPaths(ElementId src, ElementId dst) {
......@@ -141,35 +81,7 @@ public class PathManager implements PathService {
@Override
public Set<DisjointPath> getDisjointPaths(ElementId src, ElementId dst, LinkWeight weight) {
checkPermission(TOPOLOGY_READ);
checkNotNull(src, ELEMENT_ID_NULL);
checkNotNull(dst, ELEMENT_ID_NULL);
// Get the source and destination edge locations
EdgeLink srcEdge = getEdgeLink(src, true);
EdgeLink dstEdge = getEdgeLink(dst, false);
// If either edge is null, bail with no paths.
if (srcEdge == null || dstEdge == null) {
return ImmutableSet.of();
}
DeviceId srcDevice = srcEdge != NOT_HOST ? srcEdge.dst().deviceId() : (DeviceId) src;
DeviceId dstDevice = dstEdge != NOT_HOST ? dstEdge.src().deviceId() : (DeviceId) dst;
// If the source and destination are on the same edge device, there
// is just one path, so build it and return it.
if (srcDevice.equals(dstDevice)) {
return edgeToEdgePathsDisjoint(srcEdge, dstEdge);
}
// Otherwise get all paths between the source and destination edge
// devices.
Topology topology = topologyService.currentTopology();
Set<DisjointPath> paths = weight == null ?
topologyService.getDisjointPaths(topology, srcDevice, dstDevice) :
topologyService.getDisjointPaths(topology, srcDevice, dstDevice, weight);
return edgeToEdgePathsDisjoint(srcEdge, dstEdge, paths);
return super.getDisjointPaths(src, dst, weight);
}
@Override
......@@ -183,126 +95,7 @@ public class PathManager implements PathService {
public Set<DisjointPath> getDisjointPaths(ElementId src, ElementId dst, LinkWeight weight,
Map<Link, Object> riskProfile) {
checkPermission(TOPOLOGY_READ);
checkNotNull(src, ELEMENT_ID_NULL);
checkNotNull(dst, ELEMENT_ID_NULL);
// Get the source and destination edge locations
EdgeLink srcEdge = getEdgeLink(src, true);
EdgeLink dstEdge = getEdgeLink(dst, false);
// If either edge is null, bail with no paths.
if (srcEdge == null || dstEdge == null) {
return ImmutableSet.of();
}
DeviceId srcDevice = srcEdge != NOT_HOST ? srcEdge.dst().deviceId() : (DeviceId) src;
DeviceId dstDevice = dstEdge != NOT_HOST ? dstEdge.src().deviceId() : (DeviceId) dst;
// If the source and destination are on the same edge device, there
// is just one path, so build it and return it.
if (srcDevice.equals(dstDevice)) {
return edgeToEdgePathsDisjoint(srcEdge, dstEdge);
}
// Otherwise get all paths between the source and destination edge
// devices.
Topology topology = topologyService.currentTopology();
Set<DisjointPath> paths = weight == null ?
topologyService.getDisjointPaths(topology, srcDevice, dstDevice, riskProfile) :
topologyService.getDisjointPaths(topology, srcDevice, dstDevice, weight, riskProfile);
return edgeToEdgePathsDisjoint(srcEdge, dstEdge, paths);
}
// Finds the host edge link if the element ID is a host id of an existing
// host. Otherwise, if the host does not exist, it returns null and if
// the element ID is not a host ID, returns NOT_HOST edge link.
private EdgeLink getEdgeLink(ElementId elementId, boolean isIngress) {
if (elementId instanceof HostId) {
// Resolve the host, return null.
Host host = hostService.getHost((HostId) elementId);
if (host == null) {
return null;
}
return new DefaultEdgeLink(PID, new ConnectPoint(elementId, P0),
host.location(), isIngress);
}
return NOT_HOST;
}
// Produces a set of edge-to-edge paths using the set of infrastructure
// paths and the given edge links.
private Set<Path> edgeToEdgePaths(EdgeLink srcLink, EdgeLink dstLink) {
Set<Path> endToEndPaths = Sets.newHashSetWithExpectedSize(1);
endToEndPaths.add(edgeToEdgePath(srcLink, dstLink, null));
return endToEndPaths;
return super.getDisjointPaths(src, dst, weight, riskProfile);
}
// Produces a set of edge-to-edge paths using the set of infrastructure
// paths and the given edge links.
private Set<Path> edgeToEdgePaths(EdgeLink srcLink, EdgeLink dstLink, Set<Path> paths) {
Set<Path> endToEndPaths = Sets.newHashSetWithExpectedSize(paths.size());
for (Path path : paths) {
endToEndPaths.add(edgeToEdgePath(srcLink, dstLink, path));
}
return endToEndPaths;
}
private Set<DisjointPath> edgeToEdgePathsDisjoint(EdgeLink srcLink, EdgeLink dstLink) {
Set<DisjointPath> endToEndPaths = Sets.newHashSetWithExpectedSize(1);
endToEndPaths.add(edgeToEdgePathD(srcLink, dstLink, null));
return endToEndPaths;
}
private Set<DisjointPath> edgeToEdgePathsDisjoint(EdgeLink srcLink, EdgeLink dstLink, Set<DisjointPath> paths) {
Set<DisjointPath> endToEndPaths = Sets.newHashSetWithExpectedSize(paths.size());
for (DisjointPath path : paths) {
endToEndPaths.add(edgeToEdgePathD(srcLink, dstLink, path));
}
return endToEndPaths;
}
// Produces a direct edge-to-edge path.
private Path edgeToEdgePath(EdgeLink srcLink, EdgeLink dstLink, Path path) {
List<Link> links = Lists.newArrayListWithCapacity(2);
double cost = 0;
// Add source and destination edge links only if they are real and
// add the infrastructure path only if it is not null.
if (srcLink != NOT_HOST) {
links.add(srcLink);
cost++;
}
if (path != null) {
links.addAll(path.links());
cost += path.cost();
}
if (dstLink != NOT_HOST) {
links.add(dstLink);
cost++;
}
return new DefaultPath(PID, links, cost);
}
// Produces a direct edge-to-edge path.
private DisjointPath edgeToEdgePathD(EdgeLink srcLink, EdgeLink dstLink, DisjointPath path) {
Path primary = null;
Path backup = null;
if (path != null) {
primary = path.primary();
backup = path.backup();
}
return new DefaultDisjointPath(PID, (DefaultPath) edgeToEdgePath(srcLink, dstLink, primary),
(DefaultPath) edgeToEdgePath(srcLink, dstLink, backup));
}
// Special value for edge link to represent that this is really not an
// edge link since the src or dst are really an infrastructure device.
private static class NotHost extends DefaultEdgeLink implements EdgeLink {
NotHost() {
super(PID, new ConnectPoint(HostId.NONE, P0),
new HostLocation(DeviceId.NONE, P0, 0L), false);
}
}
}
......
......@@ -18,6 +18,7 @@ package org.onosproject.net.topology.impl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onlab.junit.TestUtils;
import org.onosproject.net.DeviceId;
import org.onosproject.net.ElementId;
import org.onosproject.net.Host;
......@@ -53,11 +54,11 @@ public class PathManagerTest {
private FakeHostMgr fakeHostMgr = new FakeHostMgr();
@Before
public void setUp() {
public void setUp() throws Exception {
mgr = new PathManager();
service = mgr;
mgr.topologyService = fakeTopoMgr;
mgr.hostService = fakeHostMgr;
TestUtils.setField(mgr, "topologyService", fakeTopoMgr);
TestUtils.setField(mgr, "hostService", fakeHostMgr);
mgr.activate();
}
......
......@@ -61,6 +61,7 @@ import org.onosproject.net.intent.IntentState;
import org.onosproject.net.link.LinkService;
import org.onosproject.net.provider.AbstractListenerProviderRegistry;
import org.onosproject.net.provider.AbstractProviderService;
import org.onosproject.net.topology.PathService;
import org.onosproject.net.topology.TopologyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -371,6 +372,8 @@ public class VirtualNetworkManager
service = new VirtualNetworkIntentService(this, network, new DefaultServiceDirectory());
} else if (serviceKey.serviceClass.equals(HostService.class)) {
service = new VirtualNetworkHostService(this, network);
} else if (serviceKey.serviceClass.equals(PathService.class)) {
service = new VirtualNetworkPathService(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.incubator.net.virtual.VirtualNetwork;
import org.onosproject.incubator.net.virtual.VirtualNetworkService;
import org.onosproject.net.DisjointPath;
import org.onosproject.net.ElementId;
import org.onosproject.net.Link;
import org.onosproject.net.Path;
import org.onosproject.net.host.HostService;
import org.onosproject.net.topology.LinkWeight;
import org.onosproject.net.topology.PathService;
import org.onosproject.net.topology.AbstractPathService;
import org.onosproject.net.topology.TopologyService;
import java.util.Map;
import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Path service implementation built on the virtual network service.
*/
public class VirtualNetworkPathService extends AbstractPathService
implements PathService, VnetService {
private static final String NETWORK_NULL = "Network ID cannot be null";
private final VirtualNetwork network;
/**
* Creates a new virtual network path service object.
*
* @param virtualNetworkManager virtual network manager service
* @param network virtual network
*/
public VirtualNetworkPathService(VirtualNetworkService virtualNetworkManager, VirtualNetwork network) {
checkNotNull(network, NETWORK_NULL);
this.network = network;
topologyService = virtualNetworkManager.get(network.id(), TopologyService.class);
hostService = virtualNetworkManager.get(network.id(), HostService.class);
}
@Override
public Set<Path> getPaths(ElementId src, ElementId dst) {
return getPaths(src, dst, null);
}
@Override
public Set<DisjointPath> getDisjointPaths(ElementId src, ElementId dst) {
return getDisjointPaths(src, dst, (LinkWeight) null);
}
@Override
public Set<DisjointPath> getDisjointPaths(ElementId src, ElementId dst, Map<Link, Object> riskProfile) {
return getDisjointPaths(src, dst, null, riskProfile);
}
@Override
public VirtualNetwork network() {
return network;
}
}
/*
* 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.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.DefaultPort;
import org.onosproject.net.DeviceId;
import org.onosproject.net.DisjointPath;
import org.onosproject.net.ElementId;
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.intent.FakeIntentManager;
import org.onosproject.net.intent.TestableIntentService;
import org.onosproject.net.topology.LinkWeight;
import org.onosproject.net.topology.PathService;
import org.onosproject.store.service.TestStorageService;
import java.util.Set;
import static org.junit.Assert.assertEquals;
/**
* Junit tests for VirtualNetworkPathService.
*/
public class VirtualNetworkPathServiceTest extends TestDeviceParams {
private final String tenantIdValue1 = "TENANT_ID1";
private VirtualNetworkManager manager;
private DistributedVirtualNetworkStore virtualNetworkManagerStore;
private final TestableIntentService intentService = new FakeIntentManager();
@Before
public void setUp() throws Exception {
virtualNetworkManagerStore = new DistributedVirtualNetworkStore();
CoreService coreService = new TestCoreService();
virtualNetworkManagerStore.setCoreService(coreService);
TestUtils.setField(virtualNetworkManagerStore, "storageService", new TestStorageService());
virtualNetworkManagerStore.activate();
manager = new VirtualNetworkManager();
manager.store = virtualNetworkManagerStore;
manager.intentService = intentService;
NetTestTools.injectEventDispatcher(manager, new TestEventDispatcher());
manager.activate();
}
@After
public void tearDown() {
virtualNetworkManagerStore.deactivate();
manager.deactivate();
NetTestTools.injectEventDispatcher(manager, null);
}
/**
* Sets up an empty virtual network (no devices, links).
*
* @return virtual network
*/
private VirtualNetwork setupEmptyVnet() {
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
return manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
}
/**
* Creates a virtual network for further testing.
*
* @return virtual network
*/
private VirtualNetwork setupVnet() {
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 cp11 = createConnectPointAndVirtualPort(virtualNetwork, virtualDevice1, 1);
ConnectPoint cp12 = createConnectPointAndVirtualPort(virtualNetwork, virtualDevice1, 2);
ConnectPoint cp23 = createConnectPointAndVirtualPort(virtualNetwork, virtualDevice2, 3);
ConnectPoint cp24 = createConnectPointAndVirtualPort(virtualNetwork, virtualDevice2, 4);
ConnectPoint cp35 = createConnectPointAndVirtualPort(virtualNetwork, virtualDevice3, 5);
ConnectPoint cp36 = createConnectPointAndVirtualPort(virtualNetwork, virtualDevice3, 6);
VirtualLink link1 = manager.createVirtualLink(virtualNetwork.id(), cp11, cp23);
virtualNetworkManagerStore.updateLink(link1, link1.tunnelId(), Link.State.ACTIVE);
VirtualLink link2 = manager.createVirtualLink(virtualNetwork.id(), cp23, cp11);
virtualNetworkManagerStore.updateLink(link2, link2.tunnelId(), Link.State.ACTIVE);
VirtualLink link3 = manager.createVirtualLink(virtualNetwork.id(), cp24, cp35);
virtualNetworkManagerStore.updateLink(link3, link3.tunnelId(), Link.State.ACTIVE);
VirtualLink link4 = manager.createVirtualLink(virtualNetwork.id(), cp35, cp24);
virtualNetworkManagerStore.updateLink(link4, link4.tunnelId(), Link.State.ACTIVE);
VirtualLink link5 = manager.createVirtualLink(virtualNetwork.id(), cp12, cp36);
virtualNetworkManagerStore.updateLink(link5, link5.tunnelId(), Link.State.ACTIVE);
VirtualLink link6 = manager.createVirtualLink(virtualNetwork.id(), cp36, cp12);
virtualNetworkManagerStore.updateLink(link6, link6.tunnelId(), Link.State.ACTIVE);
return virtualNetwork;
}
/**
* Creates a connect point and related virtual port.
*
* @param vnet virtual network
* @param vDev virtual device
* @param portNumber port number
* @return connect point
*/
private ConnectPoint createConnectPointAndVirtualPort(
VirtualNetwork vnet, VirtualDevice vDev, long portNumber) {
ConnectPoint cp = new ConnectPoint(vDev.id(), PortNumber.portNumber(portNumber));
manager.createVirtualPort(vnet.id(), cp.deviceId(), cp.port(),
new DefaultPort(vDev, cp.port(), true));
return cp;
}
/**
* Tests getPaths(), getDisjointPaths()
* on a non-empty virtual network.
*/
@Test
public void testGetPathsOnNonEmptyVnet() {
VirtualNetwork vnet = setupVnet();
PathService pathService = manager.get(vnet.id(), PathService.class);
// src and dest are in vnet and are connected by a virtual link
Set<Path> paths = pathService.getPaths(DID1, DID3);
validatePaths(paths, 1, 1, DID1, DID3, 1.0);
LinkWeight linkWeight = edge -> 2.0;
paths = pathService.getPaths(DID1, DID3, linkWeight);
validatePaths(paths, 1, 1, DID1, DID3, 2.0);
Set<DisjointPath> disjointPaths = pathService.getDisjointPaths(DID1, DID3);
validatePaths(disjointPaths, 1, 1, DID1, DID3, 1.0);
disjointPaths = pathService.getDisjointPaths(DID1, DID3, linkWeight);
validatePaths(disjointPaths, 1, 1, DID1, DID3, 2.0);
// src and dest are in vnet but are not connected
paths = pathService.getPaths(DID4, DID3);
assertEquals("incorrect path count", 0, paths.size());
disjointPaths = pathService.getDisjointPaths(DID4, DID3);
assertEquals("incorrect path count", 0, disjointPaths.size());
// src is in vnet, but dest is not in vnet.
DeviceId nonExistentDeviceId = DeviceId.deviceId("nonExistentDevice");
paths = pathService.getPaths(DID2, nonExistentDeviceId);
assertEquals("incorrect path count", 0, paths.size());
disjointPaths = pathService.getDisjointPaths(DID2, nonExistentDeviceId);
assertEquals("incorrect path count", 0, disjointPaths.size());
}
/**
* Tests getPaths(), getDisjointPaths()
* on an empty virtual network.
*/
@Test
public void testGetPathsOnEmptyVnet() {
VirtualNetwork vnet = setupEmptyVnet();
PathService pathService = manager.get(vnet.id(), PathService.class);
Set<Path> paths = pathService.getPaths(DID1, DID3);
assertEquals("incorrect path count", 0, paths.size());
Set<DisjointPath> disjointPaths = pathService.getDisjointPaths(DID1, DID3);
assertEquals("incorrect path count", 0, disjointPaths.size());
}
/**
* Tests getPaths() using a null source device on an empty virtual network.
*/
@Test(expected = NullPointerException.class)
public void testGetPathsWithNullSrc() {
VirtualNetwork vnet = setupEmptyVnet();
PathService pathService = manager.get(vnet.id(), PathService.class);
pathService.getPaths(null, DID3);
}
/**
* Tests getPaths() using a null destination device on a non-empty virtual network.
*/
@Test(expected = NullPointerException.class)
public void testGetPathsWithNullDest() {
VirtualNetwork vnet = setupVnet();
PathService pathService = manager.get(vnet.id(), PathService.class);
pathService.getPaths(DID1, null);
}
// Makes sure the set of paths meets basic expectations.
private void validatePaths(Set<? extends Path> paths, int count, int length,
ElementId src, ElementId dst, double cost) {
assertEquals("incorrect path count", count, paths.size());
for (Path path : paths) {
assertEquals("incorrect length", length, path.links().size());
assertEquals("incorrect source", src, path.src().elementId());
assertEquals("incorrect destination", dst, path.dst().elementId());
assertEquals("incorrect cost", cost, path.cost(), 0);
}
}
}