blob: 5f8f433cba5679d238c3917a34b0e7beeca9e5d5 [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;
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;
Brian Stanke86914282016-05-25 15:36:50 -040022import org.onosproject.incubator.net.virtual.VirtualNetworkService;
23import org.onosproject.incubator.net.virtual.VirtualPort;
yoonseonc6a69272017-01-12 18:22:20 -080024import org.onosproject.incubator.net.virtual.event.AbstractVirtualListenerManager;
Brian Stanke86914282016-05-25 15:36:50 -040025import org.onosproject.net.Device;
26import org.onosproject.net.DeviceId;
27import org.onosproject.net.MastershipRole;
28import org.onosproject.net.Port;
29import org.onosproject.net.PortNumber;
30import org.onosproject.net.device.DeviceEvent;
31import org.onosproject.net.device.DeviceListener;
32import org.onosproject.net.device.DeviceService;
33import org.onosproject.net.device.PortStatistics;
34
35import java.util.List;
36import java.util.Optional;
37import java.util.stream.Collectors;
38
39import static com.google.common.base.Preconditions.checkNotNull;
40
41/**
42 * Device service implementation built on the virtual network service.
43 */
yoonseon214963b2016-11-21 15:41:07 -080044public class VirtualNetworkDeviceManager
yoonseonc6a69272017-01-12 18:22:20 -080045 extends AbstractVirtualListenerManager<DeviceEvent, DeviceListener>
46 implements DeviceService {
Brian Stanke86914282016-05-25 15:36:50 -040047
Brian Stanke86914282016-05-25 15:36:50 -040048 private static final String TYPE_NULL = "Type cannot be null";
49 private static final String DEVICE_NULL = "Device cannot be null";
Viswanath KSP22774cd2016-08-20 20:06:30 +053050 private static final String PORT_NUMBER_NULL = "PortNumber cannot be null";
Brian Stanke86914282016-05-25 15:36:50 -040051
Brian Stanke86914282016-05-25 15:36:50 -040052 /**
53 * Creates a new VirtualNetworkDeviceService object.
54 *
55 * @param virtualNetworkManager virtual network manager service
yoonseonc6a69272017-01-12 18:22:20 -080056 * @param networkId a virtual network identifier
Brian Stanke86914282016-05-25 15:36:50 -040057 */
yoonseon214963b2016-11-21 15:41:07 -080058 public VirtualNetworkDeviceManager(VirtualNetworkService virtualNetworkManager,
yoonseonc6a69272017-01-12 18:22:20 -080059 NetworkId networkId) {
60 super(virtualNetworkManager, networkId);
Brian Stanke86914282016-05-25 15:36:50 -040061 }
62
63 @Override
64 public int getDeviceCount() {
yoonseonc6a69272017-01-12 18:22:20 -080065 return manager.getVirtualDevices(this.networkId).size();
Brian Stanke86914282016-05-25 15:36:50 -040066 }
67
68 @Override
69 public Iterable<Device> getDevices() {
yoonseon214963b2016-11-21 15:41:07 -080070 return manager.getVirtualDevices(
yoonseonc6a69272017-01-12 18:22:20 -080071 this.networkId).stream().collect(Collectors.toSet());
Brian Stanke86914282016-05-25 15:36:50 -040072 }
73
74 @Override
75 public Iterable<Device> getDevices(Device.Type type) {
76 checkNotNull(type, TYPE_NULL);
yoonseonc6a69272017-01-12 18:22:20 -080077 return manager.getVirtualDevices(this.networkId)
Brian Stanke86914282016-05-25 15:36:50 -040078 .stream()
79 .filter(device -> type.equals(device.type()))
80 .collect(Collectors.toSet());
81 }
82
83 @Override
84 public Iterable<Device> getAvailableDevices() {
85 return getDevices();
86 }
87
88 @Override
89 public Iterable<Device> getAvailableDevices(Device.Type type) {
90 return getDevices(type);
91 }
92
93 @Override
94 public Device getDevice(DeviceId deviceId) {
95 checkNotNull(deviceId, DEVICE_NULL);
yoonseon214963b2016-11-21 15:41:07 -080096 Optional<VirtualDevice> foundDevice =
yoonseonc6a69272017-01-12 18:22:20 -080097 manager.getVirtualDevices(this.networkId)
Brian Stanke86914282016-05-25 15:36:50 -040098 .stream()
99 .filter(device -> deviceId.equals(device.id()))
100 .findFirst();
101 if (foundDevice.isPresent()) {
102 return foundDevice.get();
103 }
104 return null;
105 }
106
107 @Override
108 public MastershipRole getRole(DeviceId deviceId) {
109 checkNotNull(deviceId, DEVICE_NULL);
110 // TODO hard coded to master for now.
111 return MastershipRole.MASTER;
112 }
113
114 @Override
115 public List<Port> getPorts(DeviceId deviceId) {
116 checkNotNull(deviceId, DEVICE_NULL);
yoonseonc6a69272017-01-12 18:22:20 -0800117 return manager.getVirtualPorts(this.networkId, deviceId)
Brian Stanke86914282016-05-25 15:36:50 -0400118 .stream()
119 .collect(Collectors.toList());
120 }
121
122 @Override
123 public List<PortStatistics> getPortStatistics(DeviceId deviceId) {
124 checkNotNull(deviceId, DEVICE_NULL);
125 // TODO not supported at the moment.
126 return ImmutableList.of();
127 }
128
129 @Override
130 public List<PortStatistics> getPortDeltaStatistics(DeviceId deviceId) {
131 checkNotNull(deviceId, DEVICE_NULL);
132 // TODO not supported at the moment.
133 return ImmutableList.of();
134 }
135
136 @Override
yoonseon214963b2016-11-21 15:41:07 -0800137 public PortStatistics getStatisticsForPort(DeviceId deviceId,
138 PortNumber portNumber) {
Viswanath KSP22774cd2016-08-20 20:06:30 +0530139 checkNotNull(deviceId, DEVICE_NULL);
140 checkNotNull(deviceId, PORT_NUMBER_NULL);
141 // TODO not supported at the moment.
142 return null;
143 }
144
145 @Override
yoonseon214963b2016-11-21 15:41:07 -0800146 public PortStatistics getDeltaStatisticsForPort(DeviceId deviceId,
147 PortNumber portNumber) {
Viswanath KSP22774cd2016-08-20 20:06:30 +0530148 checkNotNull(deviceId, DEVICE_NULL);
149 checkNotNull(deviceId, PORT_NUMBER_NULL);
150 // TODO not supported at the moment.
151 return null;
152 }
153
154 @Override
Brian Stanke86914282016-05-25 15:36:50 -0400155 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
156 checkNotNull(deviceId, DEVICE_NULL);
157
yoonseon214963b2016-11-21 15:41:07 -0800158 Optional<VirtualPort> foundPort =
yoonseonc6a69272017-01-12 18:22:20 -0800159 manager.getVirtualPorts(this.networkId, deviceId)
Brian Stanke86914282016-05-25 15:36:50 -0400160 .stream()
161 .filter(port -> port.number().equals(portNumber))
162 .findFirst();
163 if (foundPort.isPresent()) {
164 return foundPort.get();
165 }
166 return null;
167 }
168
169 @Override
170 public boolean isAvailable(DeviceId deviceId) {
171 return getDevice(deviceId) != null;
172 }
173
174 @Override
Saurav Dasd5ec9e92017-01-17 10:40:18 -0800175 public String localStatus(DeviceId deviceId) {
176 // TODO not supported at this time
177 return null;
178 }
Brian Stanke86914282016-05-25 15:36:50 -0400179}