Committed by
Thomas Vachuska
ONOS-2184 - Implemented Virtual Network DeviceService and LinkService to use
VirtualNetwork service. Change-Id: I695af440bc2fc5d688f8b9cf5201375bacd02e8a
Showing
9 changed files
with
1087 additions
and
4 deletions
| ... | @@ -145,6 +145,14 @@ public interface VirtualNetworkStore | ... | @@ -145,6 +145,14 @@ public interface VirtualNetworkStore |
| 145 | Set<VirtualNetwork> getNetworks(TenantId tenantId); | 145 | Set<VirtualNetwork> getNetworks(TenantId tenantId); |
| 146 | 146 | ||
| 147 | /** | 147 | /** |
| 148 | + * Returns the virtual network for the given network identifier. | ||
| 149 | + * | ||
| 150 | + * @param networkId network identifier | ||
| 151 | + * @return the virtual network | ||
| 152 | + */ | ||
| 153 | + VirtualNetwork getNetwork(NetworkId networkId); | ||
| 154 | + | ||
| 155 | + /** | ||
| 148 | * Returns the list of devices in the specified virtual network. | 156 | * Returns the list of devices in the specified virtual network. |
| 149 | * | 157 | * |
| 150 | * @param networkId network identifier | 158 | * @param networkId network identifier | ... | ... |
| 1 | +/* | ||
| 2 | + * Copyright 2016-present Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | +package org.onosproject.incubator.net.virtual.impl; | ||
| 18 | + | ||
| 19 | +import com.google.common.collect.ImmutableList; | ||
| 20 | +import org.onosproject.event.AbstractListenerManager; | ||
| 21 | +import org.onosproject.incubator.net.virtual.VirtualDevice; | ||
| 22 | +import org.onosproject.incubator.net.virtual.VirtualNetwork; | ||
| 23 | +import org.onosproject.incubator.net.virtual.VirtualNetworkService; | ||
| 24 | +import org.onosproject.incubator.net.virtual.VirtualPort; | ||
| 25 | +import org.onosproject.net.Device; | ||
| 26 | +import org.onosproject.net.DeviceId; | ||
| 27 | +import org.onosproject.net.MastershipRole; | ||
| 28 | +import org.onosproject.net.Port; | ||
| 29 | +import org.onosproject.net.PortNumber; | ||
| 30 | +import org.onosproject.net.device.DeviceEvent; | ||
| 31 | +import org.onosproject.net.device.DeviceListener; | ||
| 32 | +import org.onosproject.net.device.DeviceService; | ||
| 33 | +import org.onosproject.net.device.PortStatistics; | ||
| 34 | + | ||
| 35 | +import java.util.List; | ||
| 36 | +import java.util.Optional; | ||
| 37 | +import java.util.stream.Collectors; | ||
| 38 | + | ||
| 39 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
| 40 | + | ||
| 41 | +/** | ||
| 42 | + * Device service implementation built on the virtual network service. | ||
| 43 | + */ | ||
| 44 | +public class VirtualNetworkDeviceService extends AbstractListenerManager<DeviceEvent, DeviceListener> | ||
| 45 | + implements DeviceService, VnetService { | ||
| 46 | + | ||
| 47 | + private static final String NETWORK_NULL = "Network ID cannot be null"; | ||
| 48 | + private static final String TYPE_NULL = "Type cannot be null"; | ||
| 49 | + private static final String DEVICE_NULL = "Device cannot be null"; | ||
| 50 | + | ||
| 51 | + private final VirtualNetwork network; | ||
| 52 | + private final VirtualNetworkService manager; | ||
| 53 | + | ||
| 54 | + /** | ||
| 55 | + * Creates a new VirtualNetworkDeviceService object. | ||
| 56 | + * | ||
| 57 | + * @param virtualNetworkManager virtual network manager service | ||
| 58 | + * @param network virtual network | ||
| 59 | + */ | ||
| 60 | + public VirtualNetworkDeviceService(VirtualNetworkService virtualNetworkManager, VirtualNetwork network) { | ||
| 61 | + checkNotNull(network, NETWORK_NULL); | ||
| 62 | + this.network = network; | ||
| 63 | + this.manager = virtualNetworkManager; | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + @Override | ||
| 67 | + public int getDeviceCount() { | ||
| 68 | + return manager.getVirtualDevices(this.network.id()).size(); | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + @Override | ||
| 72 | + public Iterable<Device> getDevices() { | ||
| 73 | + return manager.getVirtualDevices(this.network.id()).stream().collect(Collectors.toSet()); | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + @Override | ||
| 77 | + public Iterable<Device> getDevices(Device.Type type) { | ||
| 78 | + checkNotNull(type, TYPE_NULL); | ||
| 79 | + return manager.getVirtualDevices(this.network.id()) | ||
| 80 | + .stream() | ||
| 81 | + .filter(device -> type.equals(device.type())) | ||
| 82 | + .collect(Collectors.toSet()); | ||
| 83 | + } | ||
| 84 | + | ||
| 85 | + @Override | ||
| 86 | + public Iterable<Device> getAvailableDevices() { | ||
| 87 | + return getDevices(); | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + @Override | ||
| 91 | + public Iterable<Device> getAvailableDevices(Device.Type type) { | ||
| 92 | + return getDevices(type); | ||
| 93 | + } | ||
| 94 | + | ||
| 95 | + @Override | ||
| 96 | + public Device getDevice(DeviceId deviceId) { | ||
| 97 | + checkNotNull(deviceId, DEVICE_NULL); | ||
| 98 | + Optional<VirtualDevice> foundDevice = manager.getVirtualDevices(this.network.id()) | ||
| 99 | + .stream() | ||
| 100 | + .filter(device -> deviceId.equals(device.id())) | ||
| 101 | + .findFirst(); | ||
| 102 | + if (foundDevice.isPresent()) { | ||
| 103 | + return foundDevice.get(); | ||
| 104 | + } | ||
| 105 | + return null; | ||
| 106 | + } | ||
| 107 | + | ||
| 108 | + @Override | ||
| 109 | + public MastershipRole getRole(DeviceId deviceId) { | ||
| 110 | + checkNotNull(deviceId, DEVICE_NULL); | ||
| 111 | + // TODO hard coded to master for now. | ||
| 112 | + return MastershipRole.MASTER; | ||
| 113 | + } | ||
| 114 | + | ||
| 115 | + @Override | ||
| 116 | + public List<Port> getPorts(DeviceId deviceId) { | ||
| 117 | + checkNotNull(deviceId, DEVICE_NULL); | ||
| 118 | + return manager.getVirtualPorts(this.network.id(), deviceId) | ||
| 119 | + .stream() | ||
| 120 | + .collect(Collectors.toList()); | ||
| 121 | + } | ||
| 122 | + | ||
| 123 | + @Override | ||
| 124 | + public List<PortStatistics> getPortStatistics(DeviceId deviceId) { | ||
| 125 | + checkNotNull(deviceId, DEVICE_NULL); | ||
| 126 | + // TODO not supported at the moment. | ||
| 127 | + return ImmutableList.of(); | ||
| 128 | + } | ||
| 129 | + | ||
| 130 | + @Override | ||
| 131 | + public List<PortStatistics> getPortDeltaStatistics(DeviceId deviceId) { | ||
| 132 | + checkNotNull(deviceId, DEVICE_NULL); | ||
| 133 | + // TODO not supported at the moment. | ||
| 134 | + return ImmutableList.of(); | ||
| 135 | + } | ||
| 136 | + | ||
| 137 | + @Override | ||
| 138 | + public Port getPort(DeviceId deviceId, PortNumber portNumber) { | ||
| 139 | + checkNotNull(deviceId, DEVICE_NULL); | ||
| 140 | + | ||
| 141 | + Optional<VirtualPort> foundPort = manager.getVirtualPorts(this.network.id(), deviceId) | ||
| 142 | + .stream() | ||
| 143 | + .filter(port -> port.number().equals(portNumber)) | ||
| 144 | + .findFirst(); | ||
| 145 | + if (foundPort.isPresent()) { | ||
| 146 | + return foundPort.get(); | ||
| 147 | + } | ||
| 148 | + return null; | ||
| 149 | + } | ||
| 150 | + | ||
| 151 | + @Override | ||
| 152 | + public boolean isAvailable(DeviceId deviceId) { | ||
| 153 | + return getDevice(deviceId) != null; | ||
| 154 | + } | ||
| 155 | + | ||
| 156 | + @Override | ||
| 157 | + public VirtualNetwork network() { | ||
| 158 | + return network; | ||
| 159 | + } | ||
| 160 | +} |
| 1 | +/* | ||
| 2 | + * Copyright 2016-present Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | +package org.onosproject.incubator.net.virtual.impl; | ||
| 18 | + | ||
| 19 | +import org.onosproject.event.AbstractListenerManager; | ||
| 20 | +import org.onosproject.incubator.net.virtual.VirtualLink; | ||
| 21 | +import org.onosproject.incubator.net.virtual.VirtualNetwork; | ||
| 22 | +import org.onosproject.incubator.net.virtual.VirtualNetworkService; | ||
| 23 | +import org.onosproject.net.ConnectPoint; | ||
| 24 | +import org.onosproject.net.DeviceId; | ||
| 25 | +import org.onosproject.net.Link; | ||
| 26 | +import org.onosproject.net.link.LinkEvent; | ||
| 27 | +import org.onosproject.net.link.LinkListener; | ||
| 28 | +import org.onosproject.net.link.LinkService; | ||
| 29 | + | ||
| 30 | +import java.util.Optional; | ||
| 31 | +import java.util.Set; | ||
| 32 | +import java.util.stream.Collectors; | ||
| 33 | + | ||
| 34 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
| 35 | + | ||
| 36 | +/** | ||
| 37 | + * Link service implementation built on the virtual network service. | ||
| 38 | + */ | ||
| 39 | +public class VirtualNetworkLinkService extends AbstractListenerManager<LinkEvent, LinkListener> | ||
| 40 | + implements LinkService, VnetService { | ||
| 41 | + | ||
| 42 | + private static final String NETWORK_NULL = "Network ID cannot be null"; | ||
| 43 | + private static final String DEVICE_NULL = "Device cannot be null"; | ||
| 44 | + private static final String CONNECT_POINT_NULL = "Connect point cannot be null"; | ||
| 45 | + | ||
| 46 | + private final VirtualNetwork network; | ||
| 47 | + private final VirtualNetworkService manager; | ||
| 48 | + | ||
| 49 | + /** | ||
| 50 | + * Creates a new VirtualNetworkLinkService object. | ||
| 51 | + * | ||
| 52 | + * @param virtualNetworkManager virtual network manager service | ||
| 53 | + * @param network virtual network | ||
| 54 | + */ | ||
| 55 | + public VirtualNetworkLinkService(VirtualNetworkService virtualNetworkManager, VirtualNetwork network) { | ||
| 56 | + checkNotNull(network, NETWORK_NULL); | ||
| 57 | + this.network = network; | ||
| 58 | + this.manager = virtualNetworkManager; | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + @Override | ||
| 62 | + public VirtualNetwork network() { | ||
| 63 | + return network; | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + @Override | ||
| 67 | + public int getLinkCount() { | ||
| 68 | + return manager.getVirtualLinks(this.network.id()).size(); | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + @Override | ||
| 72 | + public Iterable<Link> getLinks() { | ||
| 73 | + return manager.getVirtualLinks(this.network.id()).stream().collect(Collectors.toSet()); | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + @Override | ||
| 77 | + public Iterable<Link> getActiveLinks() { | ||
| 78 | + | ||
| 79 | + return manager.getVirtualLinks(this.network.id()) | ||
| 80 | + .stream() | ||
| 81 | + .filter(link -> (link.state().equals(Link.State.ACTIVE))) | ||
| 82 | + .collect(Collectors.toSet()); | ||
| 83 | + } | ||
| 84 | + | ||
| 85 | + @Override | ||
| 86 | + public Set<Link> getDeviceLinks(DeviceId deviceId) { | ||
| 87 | + checkNotNull(deviceId, DEVICE_NULL); | ||
| 88 | + return manager.getVirtualLinks(this.network.id()) | ||
| 89 | + .stream() | ||
| 90 | + .filter(link -> (deviceId.equals(link.src().elementId()) || | ||
| 91 | + deviceId.equals(link.dst().elementId()))) | ||
| 92 | + .collect(Collectors.toSet()); | ||
| 93 | + } | ||
| 94 | + | ||
| 95 | + @Override | ||
| 96 | + public Set<Link> getDeviceEgressLinks(DeviceId deviceId) { | ||
| 97 | + checkNotNull(deviceId, DEVICE_NULL); | ||
| 98 | + return manager.getVirtualLinks(this.network.id()) | ||
| 99 | + .stream() | ||
| 100 | + .filter(link -> (deviceId.equals(link.dst().elementId()))) | ||
| 101 | + .collect(Collectors.toSet()); | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + @Override | ||
| 105 | + public Set<Link> getDeviceIngressLinks(DeviceId deviceId) { | ||
| 106 | + checkNotNull(deviceId, DEVICE_NULL); | ||
| 107 | + return manager.getVirtualLinks(this.network.id()) | ||
| 108 | + .stream() | ||
| 109 | + .filter(link -> (deviceId.equals(link.src().elementId()))) | ||
| 110 | + .collect(Collectors.toSet()); | ||
| 111 | + } | ||
| 112 | + | ||
| 113 | + @Override | ||
| 114 | + public Set<Link> getLinks(ConnectPoint connectPoint) { | ||
| 115 | + checkNotNull(connectPoint, CONNECT_POINT_NULL); | ||
| 116 | + return manager.getVirtualLinks(this.network.id()) | ||
| 117 | + .stream() | ||
| 118 | + .filter(link -> (connectPoint.equals(link.src()) || | ||
| 119 | + connectPoint.equals(link.dst()))) | ||
| 120 | + .collect(Collectors.toSet()); | ||
| 121 | + } | ||
| 122 | + | ||
| 123 | + @Override | ||
| 124 | + public Set<Link> getEgressLinks(ConnectPoint connectPoint) { | ||
| 125 | + checkNotNull(connectPoint, CONNECT_POINT_NULL); | ||
| 126 | + return manager.getVirtualLinks(this.network.id()) | ||
| 127 | + .stream() | ||
| 128 | + .filter(link -> (connectPoint.equals(link.dst()))) | ||
| 129 | + .collect(Collectors.toSet()); | ||
| 130 | + } | ||
| 131 | + | ||
| 132 | + @Override | ||
| 133 | + public Set<Link> getIngressLinks(ConnectPoint connectPoint) { | ||
| 134 | + checkNotNull(connectPoint, CONNECT_POINT_NULL); | ||
| 135 | + return manager.getVirtualLinks(this.network.id()) | ||
| 136 | + .stream() | ||
| 137 | + .filter(link -> (connectPoint.equals(link.src()))) | ||
| 138 | + .collect(Collectors.toSet()); | ||
| 139 | + } | ||
| 140 | + | ||
| 141 | + @Override | ||
| 142 | + public Link getLink(ConnectPoint src, ConnectPoint dst) { | ||
| 143 | + checkNotNull(src, CONNECT_POINT_NULL); | ||
| 144 | + checkNotNull(dst, CONNECT_POINT_NULL); | ||
| 145 | + Optional<VirtualLink> foundLink = manager.getVirtualLinks(this.network.id()) | ||
| 146 | + .stream() | ||
| 147 | + .filter(link -> (src.equals(link.src()) && | ||
| 148 | + dst.equals(link.dst()))) | ||
| 149 | + .findFirst(); | ||
| 150 | + | ||
| 151 | + if (foundLink.isPresent()) { | ||
| 152 | + return foundLink.get(); | ||
| 153 | + } | ||
| 154 | + return null; | ||
| 155 | + } | ||
| 156 | +} |
| ... | @@ -15,6 +15,7 @@ | ... | @@ -15,6 +15,7 @@ |
| 15 | */ | 15 | */ |
| 16 | package org.onosproject.incubator.net.virtual.impl; | 16 | package org.onosproject.incubator.net.virtual.impl; |
| 17 | 17 | ||
| 18 | +import com.google.common.collect.Maps; | ||
| 18 | import org.apache.felix.scr.annotations.Activate; | 19 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; | 20 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; | 21 | import org.apache.felix.scr.annotations.Deactivate; |
| ... | @@ -42,11 +43,14 @@ import org.onosproject.net.DeviceId; | ... | @@ -42,11 +43,14 @@ import org.onosproject.net.DeviceId; |
| 42 | import org.onosproject.net.Link; | 43 | import org.onosproject.net.Link; |
| 43 | import org.onosproject.net.Port; | 44 | import org.onosproject.net.Port; |
| 44 | import org.onosproject.net.PortNumber; | 45 | import org.onosproject.net.PortNumber; |
| 46 | +import org.onosproject.net.device.DeviceService; | ||
| 47 | +import org.onosproject.net.link.LinkService; | ||
| 45 | import org.onosproject.net.provider.AbstractListenerProviderRegistry; | 48 | import org.onosproject.net.provider.AbstractListenerProviderRegistry; |
| 46 | import org.onosproject.net.provider.AbstractProviderService; | 49 | import org.onosproject.net.provider.AbstractProviderService; |
| 47 | import org.slf4j.Logger; | 50 | import org.slf4j.Logger; |
| 48 | import org.slf4j.LoggerFactory; | 51 | import org.slf4j.LoggerFactory; |
| 49 | 52 | ||
| 53 | +import java.util.Map; | ||
| 50 | import java.util.Set; | 54 | import java.util.Set; |
| 51 | 55 | ||
| 52 | import static com.google.common.base.Preconditions.checkNotNull; | 56 | import static com.google.common.base.Preconditions.checkNotNull; |
| ... | @@ -231,6 +235,11 @@ public class VirtualNetworkManager | ... | @@ -231,6 +235,11 @@ public class VirtualNetworkManager |
| 231 | return store.getNetworks(tenantId); | 235 | return store.getNetworks(tenantId); |
| 232 | } | 236 | } |
| 233 | 237 | ||
| 238 | + private VirtualNetwork getVirtualNetwork(NetworkId networkId) { | ||
| 239 | + checkNotNull(networkId, NETWORK_NULL); | ||
| 240 | + return store.getNetwork(networkId); | ||
| 241 | + } | ||
| 242 | + | ||
| 234 | @Override | 243 | @Override |
| 235 | public Set<VirtualDevice> getVirtualDevices(NetworkId networkId) { | 244 | public Set<VirtualDevice> getVirtualDevices(NetworkId networkId) { |
| 236 | checkNotNull(networkId, NETWORK_NULL); | 245 | checkNotNull(networkId, NETWORK_NULL); |
| ... | @@ -249,11 +258,60 @@ public class VirtualNetworkManager | ... | @@ -249,11 +258,60 @@ public class VirtualNetworkManager |
| 249 | return store.getPorts(networkId, deviceId); | 258 | return store.getPorts(networkId, deviceId); |
| 250 | } | 259 | } |
| 251 | 260 | ||
| 261 | + private final Map<ServiceKey, VnetService> networkServices = Maps.newConcurrentMap(); | ||
| 262 | + | ||
| 252 | @Override | 263 | @Override |
| 253 | public <T> T get(NetworkId networkId, Class<T> serviceClass) { | 264 | public <T> T get(NetworkId networkId, Class<T> serviceClass) { |
| 254 | checkNotNull(networkId, NETWORK_NULL); | 265 | checkNotNull(networkId, NETWORK_NULL); |
| 266 | + ServiceKey serviceKey = networkServiceKey(networkId, serviceClass); | ||
| 267 | + VnetService service = lookup(serviceKey); | ||
| 268 | + if (service == null) { | ||
| 269 | + service = create(serviceKey); | ||
| 270 | + } | ||
| 271 | + return (T) service; | ||
| 272 | + } | ||
| 273 | + | ||
| 274 | + private VnetService lookup(ServiceKey serviceKey) { | ||
| 275 | + return networkServices.get(serviceKey); | ||
| 276 | + } | ||
| 277 | + | ||
| 278 | + private <T> ServiceKey networkServiceKey(NetworkId networkId, Class<T> serviceClass) { | ||
| 279 | + return new ServiceKey(networkId, serviceClass); | ||
| 280 | + } | ||
| 281 | + | ||
| 282 | + | ||
| 283 | + private VnetService create(ServiceKey serviceKey) { | ||
| 284 | + VirtualNetwork network = getVirtualNetwork(serviceKey.networkId()); | ||
| 285 | + VnetService service; | ||
| 286 | + if (serviceKey.serviceClass.equals(DeviceService.class)) { | ||
| 287 | + service = new VirtualNetworkDeviceService(this, network); | ||
| 288 | + } else if (serviceKey.serviceClass.equals(LinkService.class)) { | ||
| 289 | + service = new VirtualNetworkLinkService(this, network); | ||
| 290 | + } else { | ||
| 255 | return null; | 291 | return null; |
| 256 | } | 292 | } |
| 293 | + networkServices.put(serviceKey, service); | ||
| 294 | + return service; | ||
| 295 | + } | ||
| 296 | + | ||
| 297 | + private class ServiceKey { | ||
| 298 | + final NetworkId networkId; | ||
| 299 | + final Class serviceClass; | ||
| 300 | + | ||
| 301 | + public ServiceKey(NetworkId networkId, Class serviceClass) { | ||
| 302 | + checkNotNull(networkId, NETWORK_NULL); | ||
| 303 | + this.networkId = networkId; | ||
| 304 | + this.serviceClass = serviceClass; | ||
| 305 | + } | ||
| 306 | + | ||
| 307 | + public NetworkId networkId() { | ||
| 308 | + return networkId; | ||
| 309 | + } | ||
| 310 | + | ||
| 311 | + public Class serviceClass() { | ||
| 312 | + return serviceClass; | ||
| 313 | + } | ||
| 314 | + } | ||
| 257 | 315 | ||
| 258 | @Override | 316 | @Override |
| 259 | protected VirtualNetworkProviderService createProviderService(VirtualNetworkProvider provider) { | 317 | protected VirtualNetworkProviderService createProviderService(VirtualNetworkProvider provider) { | ... | ... |
| 1 | +/* | ||
| 2 | + * Copyright 2016-present Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | +package org.onosproject.incubator.net.virtual.impl; | ||
| 18 | + | ||
| 19 | +import org.onosproject.incubator.net.virtual.VirtualNetwork; | ||
| 20 | + | ||
| 21 | +/** | ||
| 22 | + * Virtual network service interface. | ||
| 23 | + */ | ||
| 24 | +interface VnetService { | ||
| 25 | + VirtualNetwork network(); | ||
| 26 | + | ||
| 27 | +} |
| 1 | +/* | ||
| 2 | + * Copyright 2016-present Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | +package org.onosproject.incubator.net.virtual.impl; | ||
| 18 | + | ||
| 19 | +import com.google.common.collect.Iterators; | ||
| 20 | +import org.junit.After; | ||
| 21 | +import org.junit.Before; | ||
| 22 | +import org.junit.Test; | ||
| 23 | +import org.onlab.junit.TestUtils; | ||
| 24 | +import org.onosproject.common.event.impl.TestEventDispatcher; | ||
| 25 | +import org.onosproject.core.CoreService; | ||
| 26 | +import org.onosproject.core.CoreServiceAdapter; | ||
| 27 | +import org.onosproject.core.IdGenerator; | ||
| 28 | +import org.onosproject.incubator.net.virtual.TenantId; | ||
| 29 | +import org.onosproject.incubator.net.virtual.VirtualDevice; | ||
| 30 | +import org.onosproject.incubator.net.virtual.VirtualNetwork; | ||
| 31 | +import org.onosproject.incubator.net.virtual.VirtualPort; | ||
| 32 | +import org.onosproject.incubator.store.virtual.impl.DistributedVirtualNetworkStore; | ||
| 33 | +import org.onosproject.net.DefaultPort; | ||
| 34 | +import org.onosproject.net.Device; | ||
| 35 | +import org.onosproject.net.DeviceId; | ||
| 36 | +import org.onosproject.net.MastershipRole; | ||
| 37 | +import org.onosproject.net.NetTestTools; | ||
| 38 | +import org.onosproject.net.Port; | ||
| 39 | +import org.onosproject.net.PortNumber; | ||
| 40 | +import org.onosproject.net.device.DeviceService; | ||
| 41 | +import org.onosproject.store.service.TestStorageService; | ||
| 42 | + | ||
| 43 | +import java.util.Iterator; | ||
| 44 | +import java.util.concurrent.atomic.AtomicLong; | ||
| 45 | + | ||
| 46 | +import static org.junit.Assert.*; | ||
| 47 | + | ||
| 48 | +/** | ||
| 49 | + * Junit tests for VirtualNetworkDeviceService. | ||
| 50 | + */ | ||
| 51 | +public class VirtualNetworkDeviceServiceTest { | ||
| 52 | + private final String tenantIdValue1 = "TENANT_ID1"; | ||
| 53 | + private final String deviceIdValue1 = "DEVICE_ID1"; | ||
| 54 | + private final String deviceIdValue2 = "DEVICE_ID2"; | ||
| 55 | + private final String deviceIdValue3 = "DEVICE_ID3"; | ||
| 56 | + | ||
| 57 | + private VirtualNetworkManager manager; | ||
| 58 | + private DistributedVirtualNetworkStore virtualNetworkManagerStore; | ||
| 59 | + private CoreService coreService; | ||
| 60 | + | ||
| 61 | + @Before | ||
| 62 | + public void setUp() throws Exception { | ||
| 63 | + virtualNetworkManagerStore = new DistributedVirtualNetworkStore(); | ||
| 64 | + | ||
| 65 | + coreService = new VirtualNetworkDeviceServiceTest.TestCoreService(); | ||
| 66 | + virtualNetworkManagerStore.setCoreService(coreService); | ||
| 67 | + TestUtils.setField(coreService, "coreService", new VirtualNetworkDeviceServiceTest.TestCoreService()); | ||
| 68 | + TestUtils.setField(virtualNetworkManagerStore, "storageService", new TestStorageService()); | ||
| 69 | + virtualNetworkManagerStore.activate(); | ||
| 70 | + | ||
| 71 | + manager = new VirtualNetworkManager(); | ||
| 72 | + manager.store = virtualNetworkManagerStore; | ||
| 73 | + NetTestTools.injectEventDispatcher(manager, new TestEventDispatcher()); | ||
| 74 | + manager.activate(); | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + @After | ||
| 78 | + public void tearDown() { | ||
| 79 | + virtualNetworkManagerStore.deactivate(); | ||
| 80 | + manager.deactivate(); | ||
| 81 | + NetTestTools.injectEventDispatcher(manager, null); | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + /** | ||
| 85 | + * Tests the getDevices(), getAvailableDevices(), getDeviceCount(), getDevice(), and isAvailable() methods. | ||
| 86 | + */ | ||
| 87 | + @Test | ||
| 88 | + public void testGetDevices() { | ||
| 89 | + manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); | ||
| 90 | + VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); | ||
| 91 | + VirtualDevice device1 = manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue1)); | ||
| 92 | + VirtualDevice device2 = manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue2)); | ||
| 93 | + | ||
| 94 | + DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); | ||
| 95 | + | ||
| 96 | + // test the getDevices() method | ||
| 97 | + Iterator<Device> it = deviceService.getDevices().iterator(); | ||
| 98 | + assertEquals("The device set size did not match.", 2, Iterators.size(it)); | ||
| 99 | + | ||
| 100 | + // test the getAvailableDevices() method | ||
| 101 | + Iterator<Device> it2 = deviceService.getAvailableDevices().iterator(); | ||
| 102 | + assertEquals("The device set size did not match.", 2, Iterators.size(it2)); | ||
| 103 | + | ||
| 104 | + // test the getDeviceCount() method | ||
| 105 | + assertEquals("The device set size did not match.", 2, deviceService.getDeviceCount()); | ||
| 106 | + | ||
| 107 | + // test the getDevice() method | ||
| 108 | + assertEquals("The expect device did not match.", device1, | ||
| 109 | + deviceService.getDevice(DeviceId.deviceId(deviceIdValue1))); | ||
| 110 | + assertNotEquals("The expect device should not have matched.", device1, | ||
| 111 | + deviceService.getDevice(DeviceId.deviceId(deviceIdValue2))); | ||
| 112 | + | ||
| 113 | + // test the isAvailable() method | ||
| 114 | + assertTrue("The expect device availability did not match.", | ||
| 115 | + deviceService.isAvailable(DeviceId.deviceId(deviceIdValue1))); | ||
| 116 | + assertFalse("The expect device availability did not match.", | ||
| 117 | + deviceService.isAvailable(DeviceId.deviceId(deviceIdValue3))); | ||
| 118 | + } | ||
| 119 | + | ||
| 120 | + /** | ||
| 121 | + * Tests querying for a device using a null device identifier. | ||
| 122 | + */ | ||
| 123 | + @Test(expected = NullPointerException.class) | ||
| 124 | + public void testGetDeviceByNullId() { | ||
| 125 | + manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); | ||
| 126 | + VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); | ||
| 127 | + DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); | ||
| 128 | + | ||
| 129 | + // test the getDevice() method with null device id value. | ||
| 130 | + deviceService.getDevice(null); | ||
| 131 | + } | ||
| 132 | + | ||
| 133 | + /** | ||
| 134 | + * Tests querying for a device using a null device type. | ||
| 135 | + */ | ||
| 136 | + @Test(expected = NullPointerException.class) | ||
| 137 | + public void testGetDeviceByNullType() { | ||
| 138 | + manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); | ||
| 139 | + VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); | ||
| 140 | + DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); | ||
| 141 | + | ||
| 142 | + // test the getDevices() method with null type value. | ||
| 143 | + deviceService.getDevices(null); | ||
| 144 | + } | ||
| 145 | + | ||
| 146 | + /** | ||
| 147 | + * Tests the isAvailable method using a null device identifier. | ||
| 148 | + */ | ||
| 149 | + @Test(expected = NullPointerException.class) | ||
| 150 | + public void testIsAvailableByNullId() { | ||
| 151 | + manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); | ||
| 152 | + VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); | ||
| 153 | + DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); | ||
| 154 | + | ||
| 155 | + // test the isAvailable() method with null device id value. | ||
| 156 | + deviceService.isAvailable(null); | ||
| 157 | + } | ||
| 158 | + | ||
| 159 | + /** | ||
| 160 | + * Tests querying for a device and available devices by device type. | ||
| 161 | + */ | ||
| 162 | + @Test | ||
| 163 | + public void testGetDeviceType() { | ||
| 164 | + manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); | ||
| 165 | + VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); | ||
| 166 | + manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue1)); | ||
| 167 | + manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue2)); | ||
| 168 | + | ||
| 169 | + DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); | ||
| 170 | + | ||
| 171 | + // test the getDevices(Type) method. | ||
| 172 | + Iterator<Device> it = deviceService.getDevices(Device.Type.VIRTUAL).iterator(); | ||
| 173 | + assertEquals("The device set size did not match.", 2, Iterators.size(it)); | ||
| 174 | + Iterator<Device> it2 = deviceService.getDevices(Device.Type.SWITCH).iterator(); | ||
| 175 | + assertEquals("The device set size did not match.", 0, Iterators.size(it2)); | ||
| 176 | + | ||
| 177 | + // test the getAvailableDevices(Type) method. | ||
| 178 | + Iterator<Device> it3 = deviceService.getAvailableDevices(Device.Type.VIRTUAL).iterator(); | ||
| 179 | + assertEquals("The device set size did not match.", 2, Iterators.size(it3)); | ||
| 180 | + } | ||
| 181 | + | ||
| 182 | + /** | ||
| 183 | + * Tests querying the role of a device by null device identifier. | ||
| 184 | + */ | ||
| 185 | + @Test(expected = NullPointerException.class) | ||
| 186 | + public void testGetRoleByNullId() { | ||
| 187 | + manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); | ||
| 188 | + VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); | ||
| 189 | + DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); | ||
| 190 | + | ||
| 191 | + // test the getRole() method using a null device identifier | ||
| 192 | + deviceService.getRole(null); | ||
| 193 | + } | ||
| 194 | + | ||
| 195 | + /** | ||
| 196 | + * Tests querying the role of a device by device identifier. | ||
| 197 | + */ | ||
| 198 | + @Test | ||
| 199 | + public void testGetRole() { | ||
| 200 | + manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); | ||
| 201 | + VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); | ||
| 202 | + DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); | ||
| 203 | + | ||
| 204 | + // test the getRole() method | ||
| 205 | + assertEquals("The expect device role did not match.", MastershipRole.MASTER, | ||
| 206 | + deviceService.getRole(DeviceId.deviceId(deviceIdValue1))); | ||
| 207 | + } | ||
| 208 | + | ||
| 209 | + /** | ||
| 210 | + * Tests querying the ports of a device by null device identifier. | ||
| 211 | + */ | ||
| 212 | + @Test(expected = NullPointerException.class) | ||
| 213 | + public void testGetPortsByNullId() { | ||
| 214 | + manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); | ||
| 215 | + VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); | ||
| 216 | + DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); | ||
| 217 | + | ||
| 218 | + // test the getPorts() method using a null device identifier | ||
| 219 | + deviceService.getPorts(null); | ||
| 220 | + } | ||
| 221 | + | ||
| 222 | + /** | ||
| 223 | + * Tests querying the ports of a device by device identifier. | ||
| 224 | + */ | ||
| 225 | + @Test | ||
| 226 | + public void testGetPorts() { | ||
| 227 | + manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); | ||
| 228 | + VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); | ||
| 229 | + VirtualDevice virtualDevice = manager.createVirtualDevice(virtualNetwork.id(), | ||
| 230 | + DeviceId.deviceId(deviceIdValue1)); | ||
| 231 | + manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue2)); | ||
| 232 | + | ||
| 233 | + DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); | ||
| 234 | + | ||
| 235 | + Port port = new DefaultPort(virtualDevice, PortNumber.portNumber(1), true); | ||
| 236 | + | ||
| 237 | + manager.createVirtualPort(virtualNetwork.id(), virtualDevice.id(), PortNumber.portNumber(1), port); | ||
| 238 | + manager.createVirtualPort(virtualNetwork.id(), virtualDevice.id(), PortNumber.portNumber(2), port); | ||
| 239 | + | ||
| 240 | + // test the getPorts() method | ||
| 241 | + assertEquals("The port set size did not match.", 2, | ||
| 242 | + deviceService.getPorts(DeviceId.deviceId(deviceIdValue1)).size()); | ||
| 243 | + assertEquals("The port set size did not match.", 0, | ||
| 244 | + deviceService.getPorts(DeviceId.deviceId(deviceIdValue2)).size()); | ||
| 245 | + } | ||
| 246 | + | ||
| 247 | + /** | ||
| 248 | + * Tests querying the port of a device by device identifier and port number. | ||
| 249 | + */ | ||
| 250 | + @Test | ||
| 251 | + public void testGetPort() { | ||
| 252 | + manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); | ||
| 253 | + VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); | ||
| 254 | + VirtualDevice virtualDevice = manager.createVirtualDevice(virtualNetwork.id(), | ||
| 255 | + DeviceId.deviceId(deviceIdValue1)); | ||
| 256 | + manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue2)); | ||
| 257 | + | ||
| 258 | + DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); | ||
| 259 | + | ||
| 260 | + Port port = new DefaultPort(virtualDevice, PortNumber.portNumber(1), true); | ||
| 261 | + | ||
| 262 | + VirtualPort virtualPort1 = manager.createVirtualPort(virtualNetwork.id(), virtualDevice.id(), | ||
| 263 | + PortNumber.portNumber(1), port); | ||
| 264 | + manager.createVirtualPort(virtualNetwork.id(), virtualDevice.id(), PortNumber.portNumber(2), port); | ||
| 265 | + | ||
| 266 | + // test the getPort() method | ||
| 267 | + assertEquals("The port did not match as expected.", virtualPort1, | ||
| 268 | + deviceService.getPort(DeviceId.deviceId(deviceIdValue1), PortNumber.portNumber(1))); | ||
| 269 | + assertNotEquals("The port did not match as expected.", virtualPort1, | ||
| 270 | + deviceService.getPort(DeviceId.deviceId(deviceIdValue1), PortNumber.portNumber(3))); | ||
| 271 | + } | ||
| 272 | + | ||
| 273 | + /** | ||
| 274 | + * Tests querying the port statistics of a device by null device identifier. | ||
| 275 | + */ | ||
| 276 | + @Test(expected = NullPointerException.class) | ||
| 277 | + public void testGetPortsStatisticsByNullId() { | ||
| 278 | + manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); | ||
| 279 | + VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); | ||
| 280 | + DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); | ||
| 281 | + | ||
| 282 | + // test the getPortStatistics() method using a null device identifier | ||
| 283 | + deviceService.getPortStatistics(null); | ||
| 284 | + } | ||
| 285 | + | ||
| 286 | + /** | ||
| 287 | + * Tests querying the port statistics of a device by device identifier. | ||
| 288 | + */ | ||
| 289 | + @Test | ||
| 290 | + public void testGetPortStatistics() { | ||
| 291 | + manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); | ||
| 292 | + VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); | ||
| 293 | + VirtualDevice virtualDevice = manager.createVirtualDevice(virtualNetwork.id(), | ||
| 294 | + DeviceId.deviceId(deviceIdValue1)); | ||
| 295 | + manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue2)); | ||
| 296 | + | ||
| 297 | + DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); | ||
| 298 | + | ||
| 299 | + // test the getPortStatistics() method | ||
| 300 | + assertEquals("The port statistics set size did not match.", 0, | ||
| 301 | + deviceService.getPortStatistics(DeviceId.deviceId(deviceIdValue1)).size()); | ||
| 302 | + } | ||
| 303 | + | ||
| 304 | + /** | ||
| 305 | + * Tests querying the port delta statistics of a device by null device identifier. | ||
| 306 | + */ | ||
| 307 | + @Test(expected = NullPointerException.class) | ||
| 308 | + public void testGetPortsDeltaStatisticsByNullId() { | ||
| 309 | + manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); | ||
| 310 | + VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); | ||
| 311 | + DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); | ||
| 312 | + | ||
| 313 | + // test the getPortDeltaStatistics() method using a null device identifier | ||
| 314 | + deviceService.getPortDeltaStatistics(null); | ||
| 315 | + } | ||
| 316 | + | ||
| 317 | + /** | ||
| 318 | + * Tests querying the port delta statistics of a device by device identifier. | ||
| 319 | + */ | ||
| 320 | + @Test | ||
| 321 | + public void testGetPortDeltaStatistics() { | ||
| 322 | + manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); | ||
| 323 | + VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); | ||
| 324 | + VirtualDevice virtualDevice = manager.createVirtualDevice(virtualNetwork.id(), | ||
| 325 | + DeviceId.deviceId(deviceIdValue1)); | ||
| 326 | + manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue2)); | ||
| 327 | + | ||
| 328 | + DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); | ||
| 329 | + | ||
| 330 | + // test the getPortDeltaStatistics() method | ||
| 331 | + assertEquals("The port delta statistics set size did not match.", 0, | ||
| 332 | + deviceService.getPortDeltaStatistics(DeviceId.deviceId(deviceIdValue1)).size()); | ||
| 333 | + } | ||
| 334 | + | ||
| 335 | + /** | ||
| 336 | + * Core service test class. | ||
| 337 | + */ | ||
| 338 | + private class TestCoreService extends CoreServiceAdapter { | ||
| 339 | + | ||
| 340 | + @Override | ||
| 341 | + public IdGenerator getIdGenerator(String topic) { | ||
| 342 | + return new IdGenerator() { | ||
| 343 | + private AtomicLong counter = new AtomicLong(0); | ||
| 344 | + | ||
| 345 | + @Override | ||
| 346 | + public long getNewId() { | ||
| 347 | + return counter.getAndIncrement(); | ||
| 348 | + } | ||
| 349 | + }; | ||
| 350 | + } | ||
| 351 | + } | ||
| 352 | +} |
| 1 | +/* | ||
| 2 | + * Copyright 2016-present Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | +package org.onosproject.incubator.net.virtual.impl; | ||
| 18 | + | ||
| 19 | +import com.google.common.collect.Iterators; | ||
| 20 | +import org.junit.After; | ||
| 21 | +import org.junit.Before; | ||
| 22 | +import org.junit.Test; | ||
| 23 | +import org.onlab.junit.TestUtils; | ||
| 24 | +import org.onosproject.common.event.impl.TestEventDispatcher; | ||
| 25 | +import org.onosproject.core.CoreService; | ||
| 26 | +import org.onosproject.core.CoreServiceAdapter; | ||
| 27 | +import org.onosproject.core.IdGenerator; | ||
| 28 | +import org.onosproject.incubator.net.virtual.TenantId; | ||
| 29 | +import org.onosproject.incubator.net.virtual.VirtualDevice; | ||
| 30 | +import org.onosproject.incubator.net.virtual.VirtualLink; | ||
| 31 | +import org.onosproject.incubator.net.virtual.VirtualNetwork; | ||
| 32 | +import org.onosproject.incubator.store.virtual.impl.DistributedVirtualNetworkStore; | ||
| 33 | +import org.onosproject.net.ConnectPoint; | ||
| 34 | +import org.onosproject.net.DeviceId; | ||
| 35 | +import org.onosproject.net.Link; | ||
| 36 | +import org.onosproject.net.NetTestTools; | ||
| 37 | +import org.onosproject.net.PortNumber; | ||
| 38 | +import org.onosproject.net.link.LinkService; | ||
| 39 | +import org.onosproject.store.service.TestStorageService; | ||
| 40 | + | ||
| 41 | +import java.util.Iterator; | ||
| 42 | +import java.util.concurrent.atomic.AtomicLong; | ||
| 43 | + | ||
| 44 | +import static org.junit.Assert.assertEquals; | ||
| 45 | +import static org.junit.Assert.assertNotEquals; | ||
| 46 | + | ||
| 47 | +/** | ||
| 48 | + * Junit tests for VirtualNetworkLinkService. | ||
| 49 | + */ | ||
| 50 | +public class VirtualNetworkLinkServiceTest { | ||
| 51 | + | ||
| 52 | + private final String tenantIdValue1 = "TENANT_ID1"; | ||
| 53 | + private final String deviceIdValue1 = "DEVICE_ID1"; | ||
| 54 | + private final String deviceIdValue2 = "DEVICE_ID2"; | ||
| 55 | + private final String deviceIdValue3 = "DEVICE_ID3"; | ||
| 56 | + | ||
| 57 | + private VirtualNetworkManager manager; | ||
| 58 | + private DistributedVirtualNetworkStore virtualNetworkManagerStore; | ||
| 59 | + private CoreService coreService; | ||
| 60 | + | ||
| 61 | + @Before | ||
| 62 | + public void setUp() throws Exception { | ||
| 63 | + virtualNetworkManagerStore = new DistributedVirtualNetworkStore(); | ||
| 64 | + | ||
| 65 | + coreService = new VirtualNetworkLinkServiceTest.TestCoreService(); | ||
| 66 | + virtualNetworkManagerStore.setCoreService(coreService); | ||
| 67 | + TestUtils.setField(coreService, "coreService", new VirtualNetworkLinkServiceTest.TestCoreService()); | ||
| 68 | + TestUtils.setField(virtualNetworkManagerStore, "storageService", new TestStorageService()); | ||
| 69 | + virtualNetworkManagerStore.activate(); | ||
| 70 | + | ||
| 71 | + manager = new VirtualNetworkManager(); | ||
| 72 | + manager.store = virtualNetworkManagerStore; | ||
| 73 | + NetTestTools.injectEventDispatcher(manager, new TestEventDispatcher()); | ||
| 74 | + manager.activate(); | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + @After | ||
| 78 | + public void tearDown() { | ||
| 79 | + virtualNetworkManagerStore.deactivate(); | ||
| 80 | + manager.deactivate(); | ||
| 81 | + NetTestTools.injectEventDispatcher(manager, null); | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + /** | ||
| 85 | + * Tests the getLinks(), getActiveLinks(), getLinkCount(), getLink(), | ||
| 86 | + * getLinks(ConnectPoint), getDeviceLinks(), getDeviceEgressLinks(), getDeviceIngressLinks(), | ||
| 87 | + * getEgressLinks(), getIngressLinks() methods. | ||
| 88 | + */ | ||
| 89 | + @Test | ||
| 90 | + public void testGetLinks() { | ||
| 91 | + | ||
| 92 | + manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); | ||
| 93 | + VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); | ||
| 94 | + VirtualDevice srcVirtualDevice = | ||
| 95 | + manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue1)); | ||
| 96 | + VirtualDevice dstVirtualDevice = | ||
| 97 | + manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue2)); | ||
| 98 | + ConnectPoint src = new ConnectPoint(srcVirtualDevice.id(), PortNumber.portNumber(1)); | ||
| 99 | + ConnectPoint dst = new ConnectPoint(dstVirtualDevice.id(), PortNumber.portNumber(2)); | ||
| 100 | + VirtualLink link1 = manager.createVirtualLink(virtualNetwork.id(), src, dst); | ||
| 101 | + VirtualLink link2 = manager.createVirtualLink(virtualNetwork.id(), dst, src); | ||
| 102 | + | ||
| 103 | + LinkService linkService = manager.get(virtualNetwork.id(), LinkService.class); | ||
| 104 | + | ||
| 105 | + // test the getLinks() method | ||
| 106 | + Iterator<Link> it = linkService.getLinks().iterator(); | ||
| 107 | + assertEquals("The link set size did not match.", 2, Iterators.size(it)); | ||
| 108 | + | ||
| 109 | + // test the getActiveLinks() method where no links are ACTIVE | ||
| 110 | + Iterator<Link> it2 = linkService.getActiveLinks().iterator(); | ||
| 111 | + assertEquals("The link set size did not match.", 0, Iterators.size(it2)); | ||
| 112 | + | ||
| 113 | + // test the getActiveLinks() method where one link is ACTIVE | ||
| 114 | + virtualNetworkManagerStore.updateLink(link1, link1.tunnelId(), Link.State.ACTIVE); | ||
| 115 | + Iterator<Link> it3 = linkService.getActiveLinks().iterator(); | ||
| 116 | + assertEquals("The link set size did not match.", 1, Iterators.size(it3)); | ||
| 117 | + | ||
| 118 | + // test the getLinkCount() method | ||
| 119 | + assertEquals("The link set size did not match.", 2, linkService.getLinkCount()); | ||
| 120 | + | ||
| 121 | + // test the getLink() method | ||
| 122 | + assertEquals("The expect link did not match.", link1, | ||
| 123 | + linkService.getLink(src, dst)); | ||
| 124 | + assertEquals("The expect link did not match.", link2, | ||
| 125 | + linkService.getLink(dst, src)); | ||
| 126 | + assertNotEquals("The expect link should not have matched.", link1, | ||
| 127 | + linkService.getLink(dst, src)); | ||
| 128 | + | ||
| 129 | + // test the getLinks(ConnectPoint) method | ||
| 130 | + assertEquals("The link set size did not match.", 2, linkService.getLinks(src).size()); | ||
| 131 | + assertEquals("The link set size did not match.", 2, linkService.getLinks(dst).size()); | ||
| 132 | + ConnectPoint connectPoint = new ConnectPoint(dstVirtualDevice.id(), PortNumber.portNumber(3)); | ||
| 133 | + assertEquals("The link set size did not match.", 0, linkService.getLinks(connectPoint).size()); | ||
| 134 | + | ||
| 135 | + // test the getDeviceLinks() method | ||
| 136 | + assertEquals("The link set size did not match.", 2, | ||
| 137 | + linkService.getDeviceLinks(DeviceId.deviceId(deviceIdValue1)).size()); | ||
| 138 | + assertEquals("The link set size did not match.", 2, | ||
| 139 | + linkService.getDeviceLinks(DeviceId.deviceId(deviceIdValue2)).size()); | ||
| 140 | + assertEquals("The link set size did not match.", 0, | ||
| 141 | + linkService.getDeviceLinks(DeviceId.deviceId(deviceIdValue3)).size()); | ||
| 142 | + | ||
| 143 | + // test the getDeviceEgressLinks() method | ||
| 144 | + assertEquals("The link set size did not match.", 1, | ||
| 145 | + linkService.getDeviceEgressLinks(DeviceId.deviceId(deviceIdValue1)).size()); | ||
| 146 | + assertEquals("The link set size did not match.", 1, | ||
| 147 | + linkService.getDeviceEgressLinks(DeviceId.deviceId(deviceIdValue2)).size()); | ||
| 148 | + assertEquals("The link set size did not match.", 0, | ||
| 149 | + linkService.getDeviceEgressLinks(DeviceId.deviceId(deviceIdValue3)).size()); | ||
| 150 | + | ||
| 151 | + // test the getDeviceIngressLinks() method | ||
| 152 | + assertEquals("The link set size did not match.", 1, | ||
| 153 | + linkService.getDeviceIngressLinks(DeviceId.deviceId(deviceIdValue1)).size()); | ||
| 154 | + assertEquals("The link set size did not match.", 1, | ||
| 155 | + linkService.getDeviceIngressLinks(DeviceId.deviceId(deviceIdValue2)).size()); | ||
| 156 | + assertEquals("The link set size did not match.", 0, | ||
| 157 | + linkService.getDeviceIngressLinks(DeviceId.deviceId(deviceIdValue3)).size()); | ||
| 158 | + | ||
| 159 | + // test the getEgressLinks() method | ||
| 160 | + assertEquals("The link set size did not match.", 1, | ||
| 161 | + linkService.getEgressLinks(src).size()); | ||
| 162 | + assertEquals("The link set size did not match.", 1, | ||
| 163 | + linkService.getEgressLinks(dst).size()); | ||
| 164 | + assertEquals("The link set size did not match.", 0, | ||
| 165 | + linkService.getEgressLinks(connectPoint).size()); | ||
| 166 | + | ||
| 167 | + // test the getIngressLinks() method | ||
| 168 | + assertEquals("The link set size did not match.", 1, | ||
| 169 | + linkService.getIngressLinks(src).size()); | ||
| 170 | + assertEquals("The link set size did not match.", 1, | ||
| 171 | + linkService.getIngressLinks(dst).size()); | ||
| 172 | + assertEquals("The link set size did not match.", 0, | ||
| 173 | + linkService.getIngressLinks(connectPoint).size()); | ||
| 174 | + } | ||
| 175 | + | ||
| 176 | + /** | ||
| 177 | + * Tests the getLink() method using a null src connect point. | ||
| 178 | + */ | ||
| 179 | + @Test(expected = NullPointerException.class) | ||
| 180 | + public void testGetLinkByNullSrc() { | ||
| 181 | + | ||
| 182 | + manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); | ||
| 183 | + VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); | ||
| 184 | + VirtualDevice srcVirtualDevice = | ||
| 185 | + manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue1)); | ||
| 186 | + VirtualDevice dstVirtualDevice = | ||
| 187 | + manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue2)); | ||
| 188 | + ConnectPoint src = new ConnectPoint(srcVirtualDevice.id(), PortNumber.portNumber(1)); | ||
| 189 | + ConnectPoint dst = new ConnectPoint(dstVirtualDevice.id(), PortNumber.portNumber(2)); | ||
| 190 | + manager.createVirtualLink(virtualNetwork.id(), src, dst); | ||
| 191 | + manager.createVirtualLink(virtualNetwork.id(), dst, src); | ||
| 192 | + | ||
| 193 | + LinkService linkService = manager.get(virtualNetwork.id(), LinkService.class); | ||
| 194 | + | ||
| 195 | + // test the getLink() method with a null src connect point. | ||
| 196 | + linkService.getLink(null, dst); | ||
| 197 | + } | ||
| 198 | + | ||
| 199 | + /** | ||
| 200 | + * Tests the getLink() method using a null dst connect point. | ||
| 201 | + */ | ||
| 202 | + @Test(expected = NullPointerException.class) | ||
| 203 | + public void testGetLinkByNullDst() { | ||
| 204 | + | ||
| 205 | + manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); | ||
| 206 | + VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); | ||
| 207 | + VirtualDevice srcVirtualDevice = | ||
| 208 | + manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue1)); | ||
| 209 | + VirtualDevice dstVirtualDevice = | ||
| 210 | + manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue2)); | ||
| 211 | + ConnectPoint src = new ConnectPoint(srcVirtualDevice.id(), PortNumber.portNumber(1)); | ||
| 212 | + ConnectPoint dst = new ConnectPoint(dstVirtualDevice.id(), PortNumber.portNumber(2)); | ||
| 213 | + manager.createVirtualLink(virtualNetwork.id(), src, dst); | ||
| 214 | + manager.createVirtualLink(virtualNetwork.id(), dst, src); | ||
| 215 | + | ||
| 216 | + LinkService linkService = manager.get(virtualNetwork.id(), LinkService.class); | ||
| 217 | + | ||
| 218 | + // test the getLink() method with a null dst connect point. | ||
| 219 | + linkService.getLink(src, null); | ||
| 220 | + } | ||
| 221 | + | ||
| 222 | + /** | ||
| 223 | + * Tests querying for links using a null device identifier. | ||
| 224 | + */ | ||
| 225 | + @Test(expected = NullPointerException.class) | ||
| 226 | + public void testGetDeviceLinksByNullId() { | ||
| 227 | + manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); | ||
| 228 | + VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); | ||
| 229 | + LinkService linkService = manager.get(virtualNetwork.id(), LinkService.class); | ||
| 230 | + | ||
| 231 | + // test the getDeviceLinks() method with a null device identifier. | ||
| 232 | + linkService.getDeviceLinks(null); | ||
| 233 | + } | ||
| 234 | + | ||
| 235 | + /** | ||
| 236 | + * Tests querying for links using a null connect point. | ||
| 237 | + */ | ||
| 238 | + @Test(expected = NullPointerException.class) | ||
| 239 | + public void testGetLinksByNullId() { | ||
| 240 | + manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); | ||
| 241 | + VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); | ||
| 242 | + LinkService linkService = manager.get(virtualNetwork.id(), LinkService.class); | ||
| 243 | + | ||
| 244 | + // test the getLinks() method with a null connect point. | ||
| 245 | + linkService.getLinks(null); | ||
| 246 | + } | ||
| 247 | + | ||
| 248 | + /** | ||
| 249 | + * Tests querying for device egress links using a null device identifier. | ||
| 250 | + */ | ||
| 251 | + @Test(expected = NullPointerException.class) | ||
| 252 | + public void testGetDeviceEgressLinksByNullId() { | ||
| 253 | + manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); | ||
| 254 | + VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); | ||
| 255 | + LinkService linkService = manager.get(virtualNetwork.id(), LinkService.class); | ||
| 256 | + | ||
| 257 | + // test the getDeviceEgressLinks() method with a null device identifier. | ||
| 258 | + linkService.getDeviceEgressLinks(null); | ||
| 259 | + } | ||
| 260 | + | ||
| 261 | + /** | ||
| 262 | + * Tests querying for device ingress links using a null device identifier. | ||
| 263 | + */ | ||
| 264 | + @Test(expected = NullPointerException.class) | ||
| 265 | + public void testGetDeviceIngressLinksByNullId() { | ||
| 266 | + manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); | ||
| 267 | + VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); | ||
| 268 | + LinkService linkService = manager.get(virtualNetwork.id(), LinkService.class); | ||
| 269 | + | ||
| 270 | + // test the getDeviceIngressLinks() method with a null device identifier. | ||
| 271 | + linkService.getDeviceIngressLinks(null); | ||
| 272 | + } | ||
| 273 | + | ||
| 274 | + /** | ||
| 275 | + * Tests querying for egress links using a null connect point. | ||
| 276 | + */ | ||
| 277 | + @Test(expected = NullPointerException.class) | ||
| 278 | + public void testGetEgressLinksByNullId() { | ||
| 279 | + manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); | ||
| 280 | + VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); | ||
| 281 | + LinkService linkService = manager.get(virtualNetwork.id(), LinkService.class); | ||
| 282 | + | ||
| 283 | + // test the getEgressLinks() method with a null connect point. | ||
| 284 | + linkService.getEgressLinks(null); | ||
| 285 | + } | ||
| 286 | + | ||
| 287 | + /** | ||
| 288 | + * Tests querying for ingress links using a null connect point. | ||
| 289 | + */ | ||
| 290 | + @Test(expected = NullPointerException.class) | ||
| 291 | + public void testGetIngressLinksByNullId() { | ||
| 292 | + manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); | ||
| 293 | + VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); | ||
| 294 | + LinkService linkService = manager.get(virtualNetwork.id(), LinkService.class); | ||
| 295 | + | ||
| 296 | + // test the getIngressLinks() method with a null connect point. | ||
| 297 | + linkService.getIngressLinks(null); | ||
| 298 | + } | ||
| 299 | + | ||
| 300 | + /** | ||
| 301 | + * Core service test class. | ||
| 302 | + */ | ||
| 303 | + private class TestCoreService extends CoreServiceAdapter { | ||
| 304 | + | ||
| 305 | + @Override | ||
| 306 | + public IdGenerator getIdGenerator(String topic) { | ||
| 307 | + return new IdGenerator() { | ||
| 308 | + private AtomicLong counter = new AtomicLong(0); | ||
| 309 | + | ||
| 310 | + @Override | ||
| 311 | + public long getNewId() { | ||
| 312 | + return counter.getAndIncrement(); | ||
| 313 | + } | ||
| 314 | + }; | ||
| 315 | + } | ||
| 316 | + } | ||
| 317 | +} |
| ... | @@ -57,10 +57,10 @@ import static org.junit.Assert.*; | ... | @@ -57,10 +57,10 @@ import static org.junit.Assert.*; |
| 57 | * Junit tests for VirtualNetworkManager. | 57 | * Junit tests for VirtualNetworkManager. |
| 58 | */ | 58 | */ |
| 59 | public class VirtualNetworkManagerTest { | 59 | public class VirtualNetworkManagerTest { |
| 60 | - final String tenantIdValue1 = "TENANT_ID1"; | 60 | + private final String tenantIdValue1 = "TENANT_ID1"; |
| 61 | - final String tenantIdValue2 = "TENANT_ID2"; | 61 | + private final String tenantIdValue2 = "TENANT_ID2"; |
| 62 | - final String deviceIdValue1 = "DEVICE_ID1"; | 62 | + private final String deviceIdValue1 = "DEVICE_ID1"; |
| 63 | - final String deviceIdValue2 = "DEVICE_ID2"; | 63 | + private final String deviceIdValue2 = "DEVICE_ID2"; |
| 64 | 64 | ||
| 65 | private VirtualNetworkManager manager; | 65 | private VirtualNetworkManager manager; |
| 66 | private VirtualNetworkService virtualNetworkManagerService; | 66 | private VirtualNetworkService virtualNetworkManagerService; | ... | ... |
| ... | @@ -460,6 +460,11 @@ public class DistributedVirtualNetworkStore | ... | @@ -460,6 +460,11 @@ public class DistributedVirtualNetworkStore |
| 460 | } | 460 | } |
| 461 | 461 | ||
| 462 | @Override | 462 | @Override |
| 463 | + public VirtualNetwork getNetwork(NetworkId networkId) { | ||
| 464 | + return networkIdVirtualNetworkMap.get(networkId); | ||
| 465 | + } | ||
| 466 | + | ||
| 467 | + @Override | ||
| 463 | public Set<VirtualDevice> getDevices(NetworkId networkId) { | 468 | public Set<VirtualDevice> getDevices(NetworkId networkId) { |
| 464 | checkState(networkExists(networkId), "The network has not been added."); | 469 | checkState(networkExists(networkId), "The network has not been added."); |
| 465 | Set<DeviceId> deviceIdSet = networkIdDeviceIdSetMap.get(networkId); | 470 | Set<DeviceId> deviceIdSet = networkIdDeviceIdSetMap.get(networkId); | ... | ... |
-
Please register or login to post a comment