blob: 4b9221e0cf9dd84da72e663467f5ae58ea555f00 [file] [log] [blame]
Brian Stanke86914282016-05-25 15:36:50 -04001/*
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
17package org.onosproject.incubator.net.virtual.impl;
18
19import com.google.common.collect.ImmutableList;
20import org.onosproject.event.AbstractListenerManager;
21import org.onosproject.incubator.net.virtual.VirtualDevice;
22import org.onosproject.incubator.net.virtual.VirtualNetwork;
23import org.onosproject.incubator.net.virtual.VirtualNetworkService;
24import org.onosproject.incubator.net.virtual.VirtualPort;
yoonseon214963b2016-11-21 15:41:07 -080025import org.onosproject.incubator.net.virtual.VnetService;
Brian Stanke86914282016-05-25 15:36:50 -040026import org.onosproject.net.Device;
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.MastershipRole;
29import org.onosproject.net.Port;
30import org.onosproject.net.PortNumber;
31import org.onosproject.net.device.DeviceEvent;
32import org.onosproject.net.device.DeviceListener;
33import org.onosproject.net.device.DeviceService;
34import org.onosproject.net.device.PortStatistics;
35
36import java.util.List;
37import java.util.Optional;
38import java.util.stream.Collectors;
39
40import static com.google.common.base.Preconditions.checkNotNull;
41
42/**
43 * Device service implementation built on the virtual network service.
44 */
yoonseon214963b2016-11-21 15:41:07 -080045public class VirtualNetworkDeviceManager
46 extends AbstractListenerManager<DeviceEvent, DeviceListener>
Brian Stanke86914282016-05-25 15:36:50 -040047 implements DeviceService, VnetService {
48
49 private static final String NETWORK_NULL = "Network ID cannot be null";
50 private static final String TYPE_NULL = "Type cannot be null";
51 private static final String DEVICE_NULL = "Device cannot be null";
Viswanath KSP22774cd2016-08-20 20:06:30 +053052 private static final String PORT_NUMBER_NULL = "PortNumber cannot be null";
Brian Stanke86914282016-05-25 15:36:50 -040053
54 private final VirtualNetwork network;
55 private final VirtualNetworkService manager;
56
57 /**
58 * Creates a new VirtualNetworkDeviceService object.
59 *
60 * @param virtualNetworkManager virtual network manager service
61 * @param network virtual network
62 */
yoonseon214963b2016-11-21 15:41:07 -080063 public VirtualNetworkDeviceManager(VirtualNetworkService virtualNetworkManager,
64 VirtualNetwork network) {
Brian Stanke86914282016-05-25 15:36:50 -040065 checkNotNull(network, NETWORK_NULL);
66 this.network = network;
67 this.manager = virtualNetworkManager;
68 }
69
70 @Override
71 public int getDeviceCount() {
72 return manager.getVirtualDevices(this.network.id()).size();
73 }
74
75 @Override
76 public Iterable<Device> getDevices() {
yoonseon214963b2016-11-21 15:41:07 -080077 return manager.getVirtualDevices(
78 this.network.id()).stream().collect(Collectors.toSet());
Brian Stanke86914282016-05-25 15:36:50 -040079 }
80
81 @Override
82 public Iterable<Device> getDevices(Device.Type type) {
83 checkNotNull(type, TYPE_NULL);
84 return manager.getVirtualDevices(this.network.id())
85 .stream()
86 .filter(device -> type.equals(device.type()))
87 .collect(Collectors.toSet());
88 }
89
90 @Override
91 public Iterable<Device> getAvailableDevices() {
92 return getDevices();
93 }
94
95 @Override
96 public Iterable<Device> getAvailableDevices(Device.Type type) {
97 return getDevices(type);
98 }
99
100 @Override
101 public Device getDevice(DeviceId deviceId) {
102 checkNotNull(deviceId, DEVICE_NULL);
yoonseon214963b2016-11-21 15:41:07 -0800103 Optional<VirtualDevice> foundDevice =
104 manager.getVirtualDevices(this.network.id())
Brian Stanke86914282016-05-25 15:36:50 -0400105 .stream()
106 .filter(device -> deviceId.equals(device.id()))
107 .findFirst();
108 if (foundDevice.isPresent()) {
109 return foundDevice.get();
110 }
111 return null;
112 }
113
114 @Override
115 public MastershipRole getRole(DeviceId deviceId) {
116 checkNotNull(deviceId, DEVICE_NULL);
117 // TODO hard coded to master for now.
118 return MastershipRole.MASTER;
119 }
120
121 @Override
122 public List<Port> getPorts(DeviceId deviceId) {
123 checkNotNull(deviceId, DEVICE_NULL);
124 return manager.getVirtualPorts(this.network.id(), deviceId)
125 .stream()
126 .collect(Collectors.toList());
127 }
128
129 @Override
130 public List<PortStatistics> getPortStatistics(DeviceId deviceId) {
131 checkNotNull(deviceId, DEVICE_NULL);
132 // TODO not supported at the moment.
133 return ImmutableList.of();
134 }
135
136 @Override
137 public List<PortStatistics> getPortDeltaStatistics(DeviceId deviceId) {
138 checkNotNull(deviceId, DEVICE_NULL);
139 // TODO not supported at the moment.
140 return ImmutableList.of();
141 }
142
143 @Override
yoonseon214963b2016-11-21 15:41:07 -0800144 public PortStatistics getStatisticsForPort(DeviceId deviceId,
145 PortNumber portNumber) {
Viswanath KSP22774cd2016-08-20 20:06:30 +0530146 checkNotNull(deviceId, DEVICE_NULL);
147 checkNotNull(deviceId, PORT_NUMBER_NULL);
148 // TODO not supported at the moment.
149 return null;
150 }
151
152 @Override
yoonseon214963b2016-11-21 15:41:07 -0800153 public PortStatistics getDeltaStatisticsForPort(DeviceId deviceId,
154 PortNumber portNumber) {
Viswanath KSP22774cd2016-08-20 20:06:30 +0530155 checkNotNull(deviceId, DEVICE_NULL);
156 checkNotNull(deviceId, PORT_NUMBER_NULL);
157 // TODO not supported at the moment.
158 return null;
159 }
160
161 @Override
Brian Stanke86914282016-05-25 15:36:50 -0400162 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
163 checkNotNull(deviceId, DEVICE_NULL);
164
yoonseon214963b2016-11-21 15:41:07 -0800165 Optional<VirtualPort> foundPort =
166 manager.getVirtualPorts(this.network.id(), deviceId)
Brian Stanke86914282016-05-25 15:36:50 -0400167 .stream()
168 .filter(port -> port.number().equals(portNumber))
169 .findFirst();
170 if (foundPort.isPresent()) {
171 return foundPort.get();
172 }
173 return null;
174 }
175
176 @Override
177 public boolean isAvailable(DeviceId deviceId) {
178 return getDevice(deviceId) != null;
179 }
180
181 @Override
182 public VirtualNetwork network() {
183 return network;
184 }
Saurav Dasd5ec9e92017-01-17 10:40:18 -0800185
186 @Override
187 public String localStatus(DeviceId deviceId) {
188 // TODO not supported at this time
189 return null;
190 }
Brian Stanke86914282016-05-25 15:36:50 -0400191}