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,10 +258,59 @@ 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);
return 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
......
/*
* 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();
}
/*
* 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.Iterators;
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.TenantId;
import org.onosproject.incubator.net.virtual.VirtualDevice;
import org.onosproject.incubator.net.virtual.VirtualNetwork;
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.device.DeviceService;
import org.onosproject.store.service.TestStorageService;
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicLong;
import static org.junit.Assert.*;
/**
* Junit tests for VirtualNetworkDeviceService.
*/
public class VirtualNetworkDeviceServiceTest {
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;
private CoreService coreService;
@Before
public void setUp() throws Exception {
virtualNetworkManagerStore = new DistributedVirtualNetworkStore();
coreService = new VirtualNetworkDeviceServiceTest.TestCoreService();
virtualNetworkManagerStore.setCoreService(coreService);
TestUtils.setField(coreService, "coreService", new VirtualNetworkDeviceServiceTest.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);
}
/**
* Tests the getDevices(), getAvailableDevices(), getDeviceCount(), getDevice(), and isAvailable() methods.
*/
@Test
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));
DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class);
// test the getDevices() method
Iterator<Device> it = deviceService.getDevices().iterator();
assertEquals("The device set size did not match.", 2, Iterators.size(it));
// test the getAvailableDevices() method
Iterator<Device> it2 = deviceService.getAvailableDevices().iterator();
assertEquals("The device set size did not match.", 2, Iterators.size(it2));
// test the getDeviceCount() method
assertEquals("The device set size did not match.", 2, deviceService.getDeviceCount());
// test the getDevice() method
assertEquals("The expect device did not match.", device1,
deviceService.getDevice(DeviceId.deviceId(deviceIdValue1)));
assertNotEquals("The expect device should not have matched.", device1,
deviceService.getDevice(DeviceId.deviceId(deviceIdValue2)));
// test the isAvailable() method
assertTrue("The expect device availability did not match.",
deviceService.isAvailable(DeviceId.deviceId(deviceIdValue1)));
assertFalse("The expect device availability did not match.",
deviceService.isAvailable(DeviceId.deviceId(deviceIdValue3)));
}
/**
* Tests querying for a device using a null device identifier.
*/
@Test(expected = NullPointerException.class)
public void testGetDeviceByNullId() {
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class);
// test the getDevice() method with null device id value.
deviceService.getDevice(null);
}
/**
* Tests querying for a device using a null device type.
*/
@Test(expected = NullPointerException.class)
public void testGetDeviceByNullType() {
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class);
// test the getDevices() method with null type value.
deviceService.getDevices(null);
}
/**
* Tests the isAvailable method using a null device identifier.
*/
@Test(expected = NullPointerException.class)
public void testIsAvailableByNullId() {
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class);
// test the isAvailable() method with null device id value.
deviceService.isAvailable(null);
}
/**
* Tests querying for a device and available devices by device type.
*/
@Test
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));
DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class);
// test the getDevices(Type) method.
Iterator<Device> it = deviceService.getDevices(Device.Type.VIRTUAL).iterator();
assertEquals("The device set size did not match.", 2, Iterators.size(it));
Iterator<Device> it2 = deviceService.getDevices(Device.Type.SWITCH).iterator();
assertEquals("The device set size did not match.", 0, Iterators.size(it2));
// test the getAvailableDevices(Type) method.
Iterator<Device> it3 = deviceService.getAvailableDevices(Device.Type.VIRTUAL).iterator();
assertEquals("The device set size did not match.", 2, Iterators.size(it3));
}
/**
* Tests querying the role of a device by null device identifier.
*/
@Test(expected = NullPointerException.class)
public void testGetRoleByNullId() {
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class);
// test the getRole() method using a null device identifier
deviceService.getRole(null);
}
/**
* Tests querying the role of a device by device identifier.
*/
@Test
public void testGetRole() {
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class);
// test the getRole() method
assertEquals("The expect device role did not match.", MastershipRole.MASTER,
deviceService.getRole(DeviceId.deviceId(deviceIdValue1)));
}
/**
* Tests querying the ports of a device by null device identifier.
*/
@Test(expected = NullPointerException.class)
public void testGetPortsByNullId() {
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class);
// test the getPorts() method using a null device identifier
deviceService.getPorts(null);
}
/**
* Tests querying the ports of a device by device identifier.
*/
@Test
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));
DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class);
Port port = new DefaultPort(virtualDevice, PortNumber.portNumber(1), true);
manager.createVirtualPort(virtualNetwork.id(), virtualDevice.id(), PortNumber.portNumber(1), port);
manager.createVirtualPort(virtualNetwork.id(), virtualDevice.id(), PortNumber.portNumber(2), port);
// test the getPorts() method
assertEquals("The port set size did not match.", 2,
deviceService.getPorts(DeviceId.deviceId(deviceIdValue1)).size());
assertEquals("The port set size did not match.", 0,
deviceService.getPorts(DeviceId.deviceId(deviceIdValue2)).size());
}
/**
* Tests querying the port of a device by device identifier and port number.
*/
@Test
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));
DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class);
Port port = new DefaultPort(virtualDevice, PortNumber.portNumber(1), true);
VirtualPort virtualPort1 = manager.createVirtualPort(virtualNetwork.id(), virtualDevice.id(),
PortNumber.portNumber(1), port);
manager.createVirtualPort(virtualNetwork.id(), virtualDevice.id(), PortNumber.portNumber(2), port);
// test the getPort() method
assertEquals("The port did not match as expected.", virtualPort1,
deviceService.getPort(DeviceId.deviceId(deviceIdValue1), PortNumber.portNumber(1)));
assertNotEquals("The port did not match as expected.", virtualPort1,
deviceService.getPort(DeviceId.deviceId(deviceIdValue1), PortNumber.portNumber(3)));
}
/**
* Tests querying the port statistics of a device by null device identifier.
*/
@Test(expected = NullPointerException.class)
public void testGetPortsStatisticsByNullId() {
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class);
// test the getPortStatistics() method using a null device identifier
deviceService.getPortStatistics(null);
}
/**
* Tests querying the port statistics of a device by device identifier.
*/
@Test
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));
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());
}
/**
* Tests querying the port delta statistics of a device by null device identifier.
*/
@Test(expected = NullPointerException.class)
public void testGetPortsDeltaStatisticsByNullId() {
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class);
// test the getPortDeltaStatistics() method using a null device identifier
deviceService.getPortDeltaStatistics(null);
}
/**
* Tests querying the port delta statistics of a device by device identifier.
*/
@Test
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));
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());
}
/**
* 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();
}
};
}
}
}
/*
* 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.Iterators;
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.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.Link;
import org.onosproject.net.NetTestTools;
import org.onosproject.net.PortNumber;
import org.onosproject.net.link.LinkService;
import org.onosproject.store.service.TestStorageService;
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicLong;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
/**
* Junit tests for VirtualNetworkLinkService.
*/
public class VirtualNetworkLinkServiceTest {
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;
private CoreService coreService;
@Before
public void setUp() throws Exception {
virtualNetworkManagerStore = new DistributedVirtualNetworkStore();
coreService = new VirtualNetworkLinkServiceTest.TestCoreService();
virtualNetworkManagerStore.setCoreService(coreService);
TestUtils.setField(coreService, "coreService", new VirtualNetworkLinkServiceTest.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);
}
/**
* Tests the getLinks(), getActiveLinks(), getLinkCount(), getLink(),
* getLinks(ConnectPoint), getDeviceLinks(), getDeviceEgressLinks(), getDeviceIngressLinks(),
* getEgressLinks(), getIngressLinks() methods.
*/
@Test
public void testGetLinks() {
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
VirtualDevice srcVirtualDevice =
manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue1));
VirtualDevice dstVirtualDevice =
manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue2));
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);
VirtualLink link2 = manager.createVirtualLink(virtualNetwork.id(), dst, src);
LinkService linkService = manager.get(virtualNetwork.id(), LinkService.class);
// test the getLinks() method
Iterator<Link> it = linkService.getLinks().iterator();
assertEquals("The link set size did not match.", 2, Iterators.size(it));
// test the getActiveLinks() method where no links are ACTIVE
Iterator<Link> it2 = linkService.getActiveLinks().iterator();
assertEquals("The link set size did not match.", 0, Iterators.size(it2));
// test the getActiveLinks() method where one link is ACTIVE
virtualNetworkManagerStore.updateLink(link1, link1.tunnelId(), Link.State.ACTIVE);
Iterator<Link> it3 = linkService.getActiveLinks().iterator();
assertEquals("The link set size did not match.", 1, Iterators.size(it3));
// test the getLinkCount() method
assertEquals("The link set size did not match.", 2, linkService.getLinkCount());
// test the getLink() method
assertEquals("The expect link did not match.", link1,
linkService.getLink(src, dst));
assertEquals("The expect link did not match.", link2,
linkService.getLink(dst, src));
assertNotEquals("The expect link should not have matched.", link1,
linkService.getLink(dst, src));
// test the getLinks(ConnectPoint) method
assertEquals("The link set size did not match.", 2, linkService.getLinks(src).size());
assertEquals("The link set size did not match.", 2, linkService.getLinks(dst).size());
ConnectPoint connectPoint = new ConnectPoint(dstVirtualDevice.id(), PortNumber.portNumber(3));
assertEquals("The link set size did not match.", 0, linkService.getLinks(connectPoint).size());
// test the getDeviceLinks() method
assertEquals("The link set size did not match.", 2,
linkService.getDeviceLinks(DeviceId.deviceId(deviceIdValue1)).size());
assertEquals("The link set size did not match.", 2,
linkService.getDeviceLinks(DeviceId.deviceId(deviceIdValue2)).size());
assertEquals("The link set size did not match.", 0,
linkService.getDeviceLinks(DeviceId.deviceId(deviceIdValue3)).size());
// test the getDeviceEgressLinks() method
assertEquals("The link set size did not match.", 1,
linkService.getDeviceEgressLinks(DeviceId.deviceId(deviceIdValue1)).size());
assertEquals("The link set size did not match.", 1,
linkService.getDeviceEgressLinks(DeviceId.deviceId(deviceIdValue2)).size());
assertEquals("The link set size did not match.", 0,
linkService.getDeviceEgressLinks(DeviceId.deviceId(deviceIdValue3)).size());
// test the getDeviceIngressLinks() method
assertEquals("The link set size did not match.", 1,
linkService.getDeviceIngressLinks(DeviceId.deviceId(deviceIdValue1)).size());
assertEquals("The link set size did not match.", 1,
linkService.getDeviceIngressLinks(DeviceId.deviceId(deviceIdValue2)).size());
assertEquals("The link set size did not match.", 0,
linkService.getDeviceIngressLinks(DeviceId.deviceId(deviceIdValue3)).size());
// test the getEgressLinks() method
assertEquals("The link set size did not match.", 1,
linkService.getEgressLinks(src).size());
assertEquals("The link set size did not match.", 1,
linkService.getEgressLinks(dst).size());
assertEquals("The link set size did not match.", 0,
linkService.getEgressLinks(connectPoint).size());
// test the getIngressLinks() method
assertEquals("The link set size did not match.", 1,
linkService.getIngressLinks(src).size());
assertEquals("The link set size did not match.", 1,
linkService.getIngressLinks(dst).size());
assertEquals("The link set size did not match.", 0,
linkService.getIngressLinks(connectPoint).size());
}
/**
* Tests the getLink() method using a null src connect point.
*/
@Test(expected = NullPointerException.class)
public void testGetLinkByNullSrc() {
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
VirtualDevice srcVirtualDevice =
manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue1));
VirtualDevice dstVirtualDevice =
manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue2));
ConnectPoint src = new ConnectPoint(srcVirtualDevice.id(), PortNumber.portNumber(1));
ConnectPoint dst = new ConnectPoint(dstVirtualDevice.id(), PortNumber.portNumber(2));
manager.createVirtualLink(virtualNetwork.id(), src, dst);
manager.createVirtualLink(virtualNetwork.id(), dst, src);
LinkService linkService = manager.get(virtualNetwork.id(), LinkService.class);
// test the getLink() method with a null src connect point.
linkService.getLink(null, dst);
}
/**
* Tests the getLink() method using a null dst connect point.
*/
@Test(expected = NullPointerException.class)
public void testGetLinkByNullDst() {
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
VirtualDevice srcVirtualDevice =
manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue1));
VirtualDevice dstVirtualDevice =
manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue2));
ConnectPoint src = new ConnectPoint(srcVirtualDevice.id(), PortNumber.portNumber(1));
ConnectPoint dst = new ConnectPoint(dstVirtualDevice.id(), PortNumber.portNumber(2));
manager.createVirtualLink(virtualNetwork.id(), src, dst);
manager.createVirtualLink(virtualNetwork.id(), dst, src);
LinkService linkService = manager.get(virtualNetwork.id(), LinkService.class);
// test the getLink() method with a null dst connect point.
linkService.getLink(src, null);
}
/**
* Tests querying for links using a null device identifier.
*/
@Test(expected = NullPointerException.class)
public void testGetDeviceLinksByNullId() {
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
LinkService linkService = manager.get(virtualNetwork.id(), LinkService.class);
// test the getDeviceLinks() method with a null device identifier.
linkService.getDeviceLinks(null);
}
/**
* Tests querying for links using a null connect point.
*/
@Test(expected = NullPointerException.class)
public void testGetLinksByNullId() {
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
LinkService linkService = manager.get(virtualNetwork.id(), LinkService.class);
// test the getLinks() method with a null connect point.
linkService.getLinks(null);
}
/**
* Tests querying for device egress links using a null device identifier.
*/
@Test(expected = NullPointerException.class)
public void testGetDeviceEgressLinksByNullId() {
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
LinkService linkService = manager.get(virtualNetwork.id(), LinkService.class);
// test the getDeviceEgressLinks() method with a null device identifier.
linkService.getDeviceEgressLinks(null);
}
/**
* Tests querying for device ingress links using a null device identifier.
*/
@Test(expected = NullPointerException.class)
public void testGetDeviceIngressLinksByNullId() {
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
LinkService linkService = manager.get(virtualNetwork.id(), LinkService.class);
// test the getDeviceIngressLinks() method with a null device identifier.
linkService.getDeviceIngressLinks(null);
}
/**
* Tests querying for egress links using a null connect point.
*/
@Test(expected = NullPointerException.class)
public void testGetEgressLinksByNullId() {
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
LinkService linkService = manager.get(virtualNetwork.id(), LinkService.class);
// test the getEgressLinks() method with a null connect point.
linkService.getEgressLinks(null);
}
/**
* Tests querying for ingress links using a null connect point.
*/
@Test(expected = NullPointerException.class)
public void testGetIngressLinksByNullId() {
manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
LinkService linkService = manager.get(virtualNetwork.id(), LinkService.class);
// test the getIngressLinks() method with a null connect point.
linkService.getIngressLinks(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();
}
};
}
}
}
......@@ -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);
......