Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 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; |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 25 | import org.onosproject.incubator.net.virtual.VnetService; |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 26 | import org.onosproject.net.Device; |
| 27 | import org.onosproject.net.DeviceId; |
| 28 | import org.onosproject.net.MastershipRole; |
| 29 | import org.onosproject.net.Port; |
| 30 | import org.onosproject.net.PortNumber; |
| 31 | import org.onosproject.net.device.DeviceEvent; |
| 32 | import org.onosproject.net.device.DeviceListener; |
| 33 | import org.onosproject.net.device.DeviceService; |
| 34 | import org.onosproject.net.device.PortStatistics; |
| 35 | |
| 36 | import java.util.List; |
| 37 | import java.util.Optional; |
| 38 | import java.util.stream.Collectors; |
| 39 | |
| 40 | import static com.google.common.base.Preconditions.checkNotNull; |
| 41 | |
| 42 | /** |
| 43 | * Device service implementation built on the virtual network service. |
| 44 | */ |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 45 | public class VirtualNetworkDeviceManager |
| 46 | extends AbstractListenerManager<DeviceEvent, DeviceListener> |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 47 | 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 KSP | 22774cd | 2016-08-20 20:06:30 +0530 | [diff] [blame] | 52 | private static final String PORT_NUMBER_NULL = "PortNumber cannot be null"; |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 53 | |
| 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 | */ |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 63 | public VirtualNetworkDeviceManager(VirtualNetworkService virtualNetworkManager, |
| 64 | VirtualNetwork network) { |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 65 | 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() { |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 77 | return manager.getVirtualDevices( |
| 78 | this.network.id()).stream().collect(Collectors.toSet()); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 79 | } |
| 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); |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 103 | Optional<VirtualDevice> foundDevice = |
| 104 | manager.getVirtualDevices(this.network.id()) |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 105 | .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 |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 144 | public PortStatistics getStatisticsForPort(DeviceId deviceId, |
| 145 | PortNumber portNumber) { |
Viswanath KSP | 22774cd | 2016-08-20 20:06:30 +0530 | [diff] [blame] | 146 | checkNotNull(deviceId, DEVICE_NULL); |
| 147 | checkNotNull(deviceId, PORT_NUMBER_NULL); |
| 148 | // TODO not supported at the moment. |
| 149 | return null; |
| 150 | } |
| 151 | |
| 152 | @Override |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 153 | public PortStatistics getDeltaStatisticsForPort(DeviceId deviceId, |
| 154 | PortNumber portNumber) { |
Viswanath KSP | 22774cd | 2016-08-20 20:06:30 +0530 | [diff] [blame] | 155 | 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 Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 162 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
| 163 | checkNotNull(deviceId, DEVICE_NULL); |
| 164 | |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 165 | Optional<VirtualPort> foundPort = |
| 166 | manager.getVirtualPorts(this.network.id(), deviceId) |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 167 | .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 Das | d5ec9e9 | 2017-01-17 10:40:18 -0800 | [diff] [blame^] | 185 | |
| 186 | @Override |
| 187 | public String localStatus(DeviceId deviceId) { |
| 188 | // TODO not supported at this time |
| 189 | return null; |
| 190 | } |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 191 | } |