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 ...@@ -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,10 +258,59 @@ public class VirtualNetworkManager ...@@ -249,10 +258,59 @@ 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);
255 - return 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 {
291 + return null;
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 + }
256 } 314 }
257 315
258 @Override 316 @Override
......
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 +}
...@@ -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);
......