Brian Stanke
Committed by Thomas Vachuska

ONOS-2184 - Implemented Virtual Network DeviceService and LinkService to use

VirtualNetwork service.

Change-Id: I695af440bc2fc5d688f8b9cf5201375bacd02e8a
......@@ -145,6 +145,14 @@ public interface VirtualNetworkStore
Set<VirtualNetwork> getNetworks(TenantId tenantId);
/**
* Returns the virtual network for the given network identifier.
*
* @param networkId network identifier
* @return the virtual network
*/
VirtualNetwork getNetwork(NetworkId networkId);
/**
* Returns the list of devices in the specified virtual network.
*
* @param networkId network identifier
......
/*
* 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 com.google.common.collect.ImmutableList;
import org.onosproject.event.AbstractListenerManager;
import org.onosproject.incubator.net.virtual.VirtualDevice;
import org.onosproject.incubator.net.virtual.VirtualNetwork;
import org.onosproject.incubator.net.virtual.VirtualNetworkService;
import org.onosproject.incubator.net.virtual.VirtualPort;
import org.onosproject.net.Device;
import org.onosproject.net.DeviceId;
import org.onosproject.net.MastershipRole;
import org.onosproject.net.Port;
import org.onosproject.net.PortNumber;
import org.onosproject.net.device.DeviceEvent;
import org.onosproject.net.device.DeviceListener;
import org.onosproject.net.device.DeviceService;
import org.onosproject.net.device.PortStatistics;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Device service implementation built on the virtual network service.
*/
public class VirtualNetworkDeviceService extends AbstractListenerManager<DeviceEvent, DeviceListener>
implements DeviceService, VnetService {
private static final String NETWORK_NULL = "Network ID cannot be null";
private static final String TYPE_NULL = "Type cannot be null";
private static final String DEVICE_NULL = "Device cannot be null";
private final VirtualNetwork network;
private final VirtualNetworkService manager;
/**
* Creates a new VirtualNetworkDeviceService object.
*
* @param virtualNetworkManager virtual network manager service
* @param network virtual network
*/
public VirtualNetworkDeviceService(VirtualNetworkService virtualNetworkManager, VirtualNetwork network) {
checkNotNull(network, NETWORK_NULL);
this.network = network;
this.manager = virtualNetworkManager;
}
@Override
public int getDeviceCount() {
return manager.getVirtualDevices(this.network.id()).size();
}
@Override
public Iterable<Device> getDevices() {
return manager.getVirtualDevices(this.network.id()).stream().collect(Collectors.toSet());
}
@Override
public Iterable<Device> getDevices(Device.Type type) {
checkNotNull(type, TYPE_NULL);
return manager.getVirtualDevices(this.network.id())
.stream()
.filter(device -> type.equals(device.type()))
.collect(Collectors.toSet());
}
@Override
public Iterable<Device> getAvailableDevices() {
return getDevices();
}
@Override
public Iterable<Device> getAvailableDevices(Device.Type type) {
return getDevices(type);
}
@Override
public Device getDevice(DeviceId deviceId) {
checkNotNull(deviceId, DEVICE_NULL);
Optional<VirtualDevice> foundDevice = manager.getVirtualDevices(this.network.id())
.stream()
.filter(device -> deviceId.equals(device.id()))
.findFirst();
if (foundDevice.isPresent()) {
return foundDevice.get();
}
return null;
}
@Override
public MastershipRole getRole(DeviceId deviceId) {
checkNotNull(deviceId, DEVICE_NULL);
// TODO hard coded to master for now.
return MastershipRole.MASTER;
}
@Override
public List<Port> getPorts(DeviceId deviceId) {
checkNotNull(deviceId, DEVICE_NULL);
return manager.getVirtualPorts(this.network.id(), deviceId)
.stream()
.collect(Collectors.toList());
}
@Override
public List<PortStatistics> getPortStatistics(DeviceId deviceId) {
checkNotNull(deviceId, DEVICE_NULL);
// TODO not supported at the moment.
return ImmutableList.of();
}
@Override
public List<PortStatistics> getPortDeltaStatistics(DeviceId deviceId) {
checkNotNull(deviceId, DEVICE_NULL);
// TODO not supported at the moment.
return ImmutableList.of();
}
@Override
public Port getPort(DeviceId deviceId, PortNumber portNumber) {
checkNotNull(deviceId, DEVICE_NULL);
Optional<VirtualPort> foundPort = manager.getVirtualPorts(this.network.id(), deviceId)
.stream()
.filter(port -> port.number().equals(portNumber))
.findFirst();
if (foundPort.isPresent()) {
return foundPort.get();
}
return null;
}
@Override
public boolean isAvailable(DeviceId deviceId) {
return getDevice(deviceId) != null;
}
@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.onosproject.event.AbstractListenerManager;
import org.onosproject.incubator.net.virtual.VirtualLink;
import org.onosproject.incubator.net.virtual.VirtualNetwork;
import org.onosproject.incubator.net.virtual.VirtualNetworkService;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.DeviceId;
import org.onosproject.net.Link;
import org.onosproject.net.link.LinkEvent;
import org.onosproject.net.link.LinkListener;
import org.onosproject.net.link.LinkService;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Link service implementation built on the virtual network service.
*/
public class VirtualNetworkLinkService extends AbstractListenerManager<LinkEvent, LinkListener>
implements LinkService, VnetService {
private static final String NETWORK_NULL = "Network ID cannot be null";
private static final String DEVICE_NULL = "Device cannot be null";
private static final String CONNECT_POINT_NULL = "Connect point cannot be null";
private final VirtualNetwork network;
private final VirtualNetworkService manager;
/**
* Creates a new VirtualNetworkLinkService object.
*
* @param virtualNetworkManager virtual network manager service
* @param network virtual network
*/
public VirtualNetworkLinkService(VirtualNetworkService virtualNetworkManager, VirtualNetwork network) {
checkNotNull(network, NETWORK_NULL);
this.network = network;
this.manager = virtualNetworkManager;
}
@Override
public VirtualNetwork network() {
return network;
}
@Override
public int getLinkCount() {
return manager.getVirtualLinks(this.network.id()).size();
}
@Override
public Iterable<Link> getLinks() {
return manager.getVirtualLinks(this.network.id()).stream().collect(Collectors.toSet());
}
@Override
public Iterable<Link> getActiveLinks() {
return manager.getVirtualLinks(this.network.id())
.stream()
.filter(link -> (link.state().equals(Link.State.ACTIVE)))
.collect(Collectors.toSet());
}
@Override
public Set<Link> getDeviceLinks(DeviceId deviceId) {
checkNotNull(deviceId, DEVICE_NULL);
return manager.getVirtualLinks(this.network.id())
.stream()
.filter(link -> (deviceId.equals(link.src().elementId()) ||
deviceId.equals(link.dst().elementId())))
.collect(Collectors.toSet());
}
@Override
public Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
checkNotNull(deviceId, DEVICE_NULL);
return manager.getVirtualLinks(this.network.id())
.stream()
.filter(link -> (deviceId.equals(link.dst().elementId())))
.collect(Collectors.toSet());
}
@Override
public Set<Link> getDeviceIngressLinks(DeviceId deviceId) {
checkNotNull(deviceId, DEVICE_NULL);
return manager.getVirtualLinks(this.network.id())
.stream()
.filter(link -> (deviceId.equals(link.src().elementId())))
.collect(Collectors.toSet());
}
@Override
public Set<Link> getLinks(ConnectPoint connectPoint) {
checkNotNull(connectPoint, CONNECT_POINT_NULL);
return manager.getVirtualLinks(this.network.id())
.stream()
.filter(link -> (connectPoint.equals(link.src()) ||
connectPoint.equals(link.dst())))
.collect(Collectors.toSet());
}
@Override
public Set<Link> getEgressLinks(ConnectPoint connectPoint) {
checkNotNull(connectPoint, CONNECT_POINT_NULL);
return manager.getVirtualLinks(this.network.id())
.stream()
.filter(link -> (connectPoint.equals(link.dst())))
.collect(Collectors.toSet());
}
@Override
public Set<Link> getIngressLinks(ConnectPoint connectPoint) {
checkNotNull(connectPoint, CONNECT_POINT_NULL);
return manager.getVirtualLinks(this.network.id())
.stream()
.filter(link -> (connectPoint.equals(link.src())))
.collect(Collectors.toSet());
}
@Override
public Link getLink(ConnectPoint src, ConnectPoint dst) {
checkNotNull(src, CONNECT_POINT_NULL);
checkNotNull(dst, CONNECT_POINT_NULL);
Optional<VirtualLink> foundLink = manager.getVirtualLinks(this.network.id())
.stream()
.filter(link -> (src.equals(link.src()) &&
dst.equals(link.dst())))
.findFirst();
if (foundLink.isPresent()) {
return foundLink.get();
}
return null;
}
}
......@@ -15,6 +15,7 @@
*/
package org.onosproject.incubator.net.virtual.impl;
import com.google.common.collect.Maps;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
......@@ -42,11 +43,14 @@ import org.onosproject.net.DeviceId;
import org.onosproject.net.Link;
import org.onosproject.net.Port;
import org.onosproject.net.PortNumber;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
......@@ -231,6 +235,11 @@ public class VirtualNetworkManager
return store.getNetworks(tenantId);
}
private VirtualNetwork getVirtualNetwork(NetworkId networkId) {
checkNotNull(networkId, NETWORK_NULL);
return store.getNetwork(networkId);
}
@Override
public Set<VirtualDevice> getVirtualDevices(NetworkId networkId) {
checkNotNull(networkId, NETWORK_NULL);
......@@ -249,11 +258,60 @@ public class VirtualNetworkManager
return store.getPorts(networkId, deviceId);
}
private final Map<ServiceKey, VnetService> networkServices = Maps.newConcurrentMap();
@Override
public <T> T get(NetworkId networkId, Class<T> serviceClass) {
checkNotNull(networkId, NETWORK_NULL);
ServiceKey serviceKey = networkServiceKey(networkId, serviceClass);
VnetService service = lookup(serviceKey);
if (service == null) {
service = create(serviceKey);
}
return (T) service;
}
private VnetService lookup(ServiceKey serviceKey) {
return networkServices.get(serviceKey);
}
private <T> ServiceKey networkServiceKey(NetworkId networkId, Class<T> serviceClass) {
return new ServiceKey(networkId, serviceClass);
}
private VnetService create(ServiceKey serviceKey) {
VirtualNetwork network = getVirtualNetwork(serviceKey.networkId());
VnetService service;
if (serviceKey.serviceClass.equals(DeviceService.class)) {
service = new VirtualNetworkDeviceService(this, network);
} else if (serviceKey.serviceClass.equals(LinkService.class)) {
service = new VirtualNetworkLinkService(this, network);
} else {
return null;
}
networkServices.put(serviceKey, service);
return service;
}
private class ServiceKey {
final NetworkId networkId;
final Class serviceClass;
public ServiceKey(NetworkId networkId, Class serviceClass) {
checkNotNull(networkId, NETWORK_NULL);
this.networkId = networkId;
this.serviceClass = serviceClass;
}
public NetworkId networkId() {
return networkId;
}
public Class serviceClass() {
return serviceClass;
}
}
@Override
protected VirtualNetworkProviderService createProviderService(VirtualNetworkProvider provider) {
......
/*
* 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;
/**
* Virtual network service interface.
*/
interface VnetService {
VirtualNetwork network();
}
......@@ -57,10 +57,10 @@ import static org.junit.Assert.*;
* Junit tests for VirtualNetworkManager.
*/
public class VirtualNetworkManagerTest {
final String tenantIdValue1 = "TENANT_ID1";
final String tenantIdValue2 = "TENANT_ID2";
final String deviceIdValue1 = "DEVICE_ID1";
final String deviceIdValue2 = "DEVICE_ID2";
private final String tenantIdValue1 = "TENANT_ID1";
private final String tenantIdValue2 = "TENANT_ID2";
private final String deviceIdValue1 = "DEVICE_ID1";
private final String deviceIdValue2 = "DEVICE_ID2";
private VirtualNetworkManager manager;
private VirtualNetworkService virtualNetworkManagerService;
......
......@@ -460,6 +460,11 @@ public class DistributedVirtualNetworkStore
}
@Override
public VirtualNetwork getNetwork(NetworkId networkId) {
return networkIdVirtualNetworkMap.get(networkId);
}
@Override
public Set<VirtualDevice> getDevices(NetworkId networkId) {
checkState(networkExists(networkId), "The network has not been added.");
Set<DeviceId> deviceIdSet = networkIdDeviceIdSetMap.get(networkId);
......