blob: 601a05d7dbe1273524439f0c956f45c37da576bd [file] [log] [blame]
Brian Stanke86914282016-05-25 15:36:50 -04001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Brian Stanke86914282016-05-25 15:36:50 -04003 *
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
17package org.onosproject.incubator.net.virtual.impl;
18
19import com.google.common.collect.ImmutableList;
yoonseonc6a69272017-01-12 18:22:20 -080020import org.onosproject.incubator.net.virtual.NetworkId;
Brian Stanke86914282016-05-25 15:36:50 -040021import org.onosproject.incubator.net.virtual.VirtualDevice;
Claudine Chiu465a2602017-03-17 18:33:36 -040022import org.onosproject.incubator.net.virtual.VirtualNetworkEvent;
23import org.onosproject.incubator.net.virtual.VirtualNetworkListener;
Brian Stanke86914282016-05-25 15:36:50 -040024import org.onosproject.incubator.net.virtual.VirtualNetworkService;
25import org.onosproject.incubator.net.virtual.VirtualPort;
yoonseonc6a69272017-01-12 18:22:20 -080026import org.onosproject.incubator.net.virtual.event.AbstractVirtualListenerManager;
Brian Stanke86914282016-05-25 15:36:50 -040027import org.onosproject.net.Device;
28import org.onosproject.net.DeviceId;
29import org.onosproject.net.MastershipRole;
30import org.onosproject.net.Port;
31import org.onosproject.net.PortNumber;
32import org.onosproject.net.device.DeviceEvent;
Claudine Chiu465a2602017-03-17 18:33:36 -040033import org.onosproject.net.device.DeviceEvent.Type;
Brian Stanke86914282016-05-25 15:36:50 -040034import org.onosproject.net.device.DeviceListener;
35import org.onosproject.net.device.DeviceService;
36import org.onosproject.net.device.PortStatistics;
37
38import java.util.List;
39import java.util.Optional;
40import java.util.stream.Collectors;
41
42import static com.google.common.base.Preconditions.checkNotNull;
43
44/**
45 * Device service implementation built on the virtual network service.
46 */
yoonseon214963b2016-11-21 15:41:07 -080047public class VirtualNetworkDeviceManager
yoonseonc6a69272017-01-12 18:22:20 -080048 extends AbstractVirtualListenerManager<DeviceEvent, DeviceListener>
49 implements DeviceService {
Brian Stanke86914282016-05-25 15:36:50 -040050
Brian Stanke86914282016-05-25 15:36:50 -040051 private static final String TYPE_NULL = "Type cannot be null";
52 private static final String DEVICE_NULL = "Device cannot be null";
Viswanath KSP22774cd2016-08-20 20:06:30 +053053 private static final String PORT_NUMBER_NULL = "PortNumber cannot be null";
Claudine Chiu465a2602017-03-17 18:33:36 -040054 private VirtualNetworkListener virtualNetworkListener = new InternalVirtualNetworkListener();
Brian Stanke86914282016-05-25 15:36:50 -040055
Brian Stanke86914282016-05-25 15:36:50 -040056 /**
57 * Creates a new VirtualNetworkDeviceService object.
58 *
59 * @param virtualNetworkManager virtual network manager service
yoonseonc6a69272017-01-12 18:22:20 -080060 * @param networkId a virtual network identifier
Brian Stanke86914282016-05-25 15:36:50 -040061 */
yoonseon214963b2016-11-21 15:41:07 -080062 public VirtualNetworkDeviceManager(VirtualNetworkService virtualNetworkManager,
yoonseonc6a69272017-01-12 18:22:20 -080063 NetworkId networkId) {
Yoonseon Hanb14461b2017-03-07 14:08:01 +090064 super(virtualNetworkManager, networkId, DeviceEvent.class);
Claudine Chiu465a2602017-03-17 18:33:36 -040065 manager.addListener(virtualNetworkListener);
Brian Stanke86914282016-05-25 15:36:50 -040066 }
67
68 @Override
69 public int getDeviceCount() {
yoonseonc6a69272017-01-12 18:22:20 -080070 return manager.getVirtualDevices(this.networkId).size();
Brian Stanke86914282016-05-25 15:36:50 -040071 }
72
73 @Override
74 public Iterable<Device> getDevices() {
yoonseon214963b2016-11-21 15:41:07 -080075 return manager.getVirtualDevices(
yoonseonc6a69272017-01-12 18:22:20 -080076 this.networkId).stream().collect(Collectors.toSet());
Brian Stanke86914282016-05-25 15:36:50 -040077 }
78
79 @Override
80 public Iterable<Device> getDevices(Device.Type type) {
81 checkNotNull(type, TYPE_NULL);
yoonseonc6a69272017-01-12 18:22:20 -080082 return manager.getVirtualDevices(this.networkId)
Brian Stanke86914282016-05-25 15:36:50 -040083 .stream()
84 .filter(device -> type.equals(device.type()))
85 .collect(Collectors.toSet());
86 }
87
88 @Override
89 public Iterable<Device> getAvailableDevices() {
90 return getDevices();
91 }
92
93 @Override
94 public Iterable<Device> getAvailableDevices(Device.Type type) {
95 return getDevices(type);
96 }
97
98 @Override
99 public Device getDevice(DeviceId deviceId) {
100 checkNotNull(deviceId, DEVICE_NULL);
yoonseon214963b2016-11-21 15:41:07 -0800101 Optional<VirtualDevice> foundDevice =
yoonseonc6a69272017-01-12 18:22:20 -0800102 manager.getVirtualDevices(this.networkId)
Brian Stanke86914282016-05-25 15:36:50 -0400103 .stream()
104 .filter(device -> deviceId.equals(device.id()))
105 .findFirst();
106 if (foundDevice.isPresent()) {
107 return foundDevice.get();
108 }
109 return null;
110 }
111
112 @Override
113 public MastershipRole getRole(DeviceId deviceId) {
114 checkNotNull(deviceId, DEVICE_NULL);
115 // TODO hard coded to master for now.
116 return MastershipRole.MASTER;
117 }
118
119 @Override
120 public List<Port> getPorts(DeviceId deviceId) {
121 checkNotNull(deviceId, DEVICE_NULL);
yoonseonc6a69272017-01-12 18:22:20 -0800122 return manager.getVirtualPorts(this.networkId, deviceId)
Brian Stanke86914282016-05-25 15:36:50 -0400123 .stream()
124 .collect(Collectors.toList());
125 }
126
127 @Override
128 public List<PortStatistics> getPortStatistics(DeviceId deviceId) {
129 checkNotNull(deviceId, DEVICE_NULL);
130 // TODO not supported at the moment.
131 return ImmutableList.of();
132 }
133
134 @Override
135 public List<PortStatistics> getPortDeltaStatistics(DeviceId deviceId) {
136 checkNotNull(deviceId, DEVICE_NULL);
137 // TODO not supported at the moment.
138 return ImmutableList.of();
139 }
140
141 @Override
yoonseon214963b2016-11-21 15:41:07 -0800142 public PortStatistics getStatisticsForPort(DeviceId deviceId,
143 PortNumber portNumber) {
Viswanath KSP22774cd2016-08-20 20:06:30 +0530144 checkNotNull(deviceId, DEVICE_NULL);
145 checkNotNull(deviceId, PORT_NUMBER_NULL);
146 // TODO not supported at the moment.
147 return null;
148 }
149
150 @Override
yoonseon214963b2016-11-21 15:41:07 -0800151 public PortStatistics getDeltaStatisticsForPort(DeviceId deviceId,
152 PortNumber portNumber) {
Viswanath KSP22774cd2016-08-20 20:06:30 +0530153 checkNotNull(deviceId, DEVICE_NULL);
154 checkNotNull(deviceId, PORT_NUMBER_NULL);
155 // TODO not supported at the moment.
156 return null;
157 }
158
159 @Override
Brian Stanke86914282016-05-25 15:36:50 -0400160 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
161 checkNotNull(deviceId, DEVICE_NULL);
162
yoonseon214963b2016-11-21 15:41:07 -0800163 Optional<VirtualPort> foundPort =
yoonseonc6a69272017-01-12 18:22:20 -0800164 manager.getVirtualPorts(this.networkId, deviceId)
Brian Stanke86914282016-05-25 15:36:50 -0400165 .stream()
166 .filter(port -> port.number().equals(portNumber))
167 .findFirst();
168 if (foundPort.isPresent()) {
169 return foundPort.get();
170 }
171 return null;
172 }
173
174 @Override
175 public boolean isAvailable(DeviceId deviceId) {
176 return getDevice(deviceId) != null;
177 }
178
179 @Override
Saurav Dasd5ec9e92017-01-17 10:40:18 -0800180 public String localStatus(DeviceId deviceId) {
181 // TODO not supported at this time
182 return null;
183 }
Claudine Chiu465a2602017-03-17 18:33:36 -0400184
Ray Milkey054e23d2018-03-22 13:37:11 -0700185 @Override
186 public long getLastUpdatedInstant(DeviceId deviceId) {
187 // TODO not supported at this time
188 return 0;
189 }
190
Claudine Chiu465a2602017-03-17 18:33:36 -0400191 /**
192 * Translates VirtualNetworkEvent to DeviceEvent.
193 */
194 private class InternalVirtualNetworkListener implements VirtualNetworkListener {
195 @Override
196 public boolean isRelevant(VirtualNetworkEvent event) {
197 return networkId().equals(event.subject());
198 }
199
200 @Override
201 public void event(VirtualNetworkEvent event) {
202 switch (event.type()) {
203 case VIRTUAL_DEVICE_ADDED:
204 post(new DeviceEvent(Type.DEVICE_ADDED, event.virtualDevice()));
205 break;
206 case VIRTUAL_DEVICE_UPDATED:
207 post(new DeviceEvent(Type.DEVICE_UPDATED, event.virtualDevice()));
208 break;
209 case VIRTUAL_DEVICE_REMOVED:
210 post(new DeviceEvent(Type.DEVICE_REMOVED, event.virtualDevice()));
211 break;
212 case VIRTUAL_PORT_ADDED:
213 post(new DeviceEvent(Type.PORT_ADDED, event.virtualDevice(), event.virtualPort()));
214 break;
215 case VIRTUAL_PORT_UPDATED:
216 post(new DeviceEvent(Type.PORT_UPDATED, event.virtualDevice(), event.virtualPort()));
217 break;
218 case VIRTUAL_PORT_REMOVED:
219 post(new DeviceEvent(Type.PORT_REMOVED, event.virtualDevice(), event.virtualPort()));
220 break;
221 case NETWORK_UPDATED:
222 case NETWORK_REMOVED:
223 case NETWORK_ADDED:
224 default:
225 // do nothing
226 break;
227 }
228 }
229 }
Brian Stanke86914282016-05-25 15:36:50 -0400230}