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; |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame^] | 20 | import org.onosproject.incubator.net.virtual.NetworkId; |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 21 | import org.onosproject.incubator.net.virtual.VirtualDevice; |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 22 | import org.onosproject.incubator.net.virtual.VirtualNetworkService; |
| 23 | import org.onosproject.incubator.net.virtual.VirtualPort; |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame^] | 24 | import org.onosproject.incubator.net.virtual.event.AbstractVirtualListenerManager; |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 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 | */ |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 44 | public class VirtualNetworkDeviceManager |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame^] | 45 | extends AbstractVirtualListenerManager<DeviceEvent, DeviceListener> |
| 46 | implements DeviceService { |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 47 | |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 48 | private static final String TYPE_NULL = "Type cannot be null"; |
| 49 | private static final String DEVICE_NULL = "Device cannot be null"; |
Viswanath KSP | 22774cd | 2016-08-20 20:06:30 +0530 | [diff] [blame] | 50 | private static final String PORT_NUMBER_NULL = "PortNumber cannot be null"; |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 51 | |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 52 | /** |
| 53 | * Creates a new VirtualNetworkDeviceService object. |
| 54 | * |
| 55 | * @param virtualNetworkManager virtual network manager service |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame^] | 56 | * @param networkId a virtual network identifier |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 57 | */ |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 58 | public VirtualNetworkDeviceManager(VirtualNetworkService virtualNetworkManager, |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame^] | 59 | NetworkId networkId) { |
| 60 | super(virtualNetworkManager, networkId); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public int getDeviceCount() { |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame^] | 65 | return manager.getVirtualDevices(this.networkId).size(); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public Iterable<Device> getDevices() { |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 70 | return manager.getVirtualDevices( |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame^] | 71 | this.networkId).stream().collect(Collectors.toSet()); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public Iterable<Device> getDevices(Device.Type type) { |
| 76 | checkNotNull(type, TYPE_NULL); |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame^] | 77 | return manager.getVirtualDevices(this.networkId) |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 78 | .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); |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 96 | Optional<VirtualDevice> foundDevice = |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame^] | 97 | manager.getVirtualDevices(this.networkId) |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 98 | .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); |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame^] | 117 | return manager.getVirtualPorts(this.networkId, deviceId) |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 118 | .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 |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 137 | public PortStatistics getStatisticsForPort(DeviceId deviceId, |
| 138 | PortNumber portNumber) { |
Viswanath KSP | 22774cd | 2016-08-20 20:06:30 +0530 | [diff] [blame] | 139 | checkNotNull(deviceId, DEVICE_NULL); |
| 140 | checkNotNull(deviceId, PORT_NUMBER_NULL); |
| 141 | // TODO not supported at the moment. |
| 142 | return null; |
| 143 | } |
| 144 | |
| 145 | @Override |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 146 | public PortStatistics getDeltaStatisticsForPort(DeviceId deviceId, |
| 147 | PortNumber portNumber) { |
Viswanath KSP | 22774cd | 2016-08-20 20:06:30 +0530 | [diff] [blame] | 148 | 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 Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 155 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
| 156 | checkNotNull(deviceId, DEVICE_NULL); |
| 157 | |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 158 | Optional<VirtualPort> foundPort = |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame^] | 159 | manager.getVirtualPorts(this.networkId, deviceId) |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 160 | .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 Das | d5ec9e9 | 2017-01-17 10:40:18 -0800 | [diff] [blame] | 175 | public String localStatus(DeviceId deviceId) { |
| 176 | // TODO not supported at this time |
| 177 | return null; |
| 178 | } |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 179 | } |