Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 52f2cd1 | 2018-11-08 21:20:04 -0800 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 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; |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 22 | import org.onosproject.incubator.net.virtual.VirtualNetworkEvent; |
| 23 | import org.onosproject.incubator.net.virtual.VirtualNetworkListener; |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 24 | import org.onosproject.incubator.net.virtual.VirtualNetworkService; |
| 25 | import org.onosproject.incubator.net.virtual.VirtualPort; |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 26 | import org.onosproject.incubator.net.virtual.event.AbstractVirtualListenerManager; |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 27 | import org.onosproject.net.Device; |
| 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.net.MastershipRole; |
| 30 | import org.onosproject.net.Port; |
| 31 | import org.onosproject.net.PortNumber; |
| 32 | import org.onosproject.net.device.DeviceEvent; |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 33 | import org.onosproject.net.device.DeviceEvent.Type; |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 34 | import org.onosproject.net.device.DeviceListener; |
| 35 | import org.onosproject.net.device.DeviceService; |
| 36 | import org.onosproject.net.device.PortStatistics; |
| 37 | |
| 38 | import java.util.List; |
| 39 | import java.util.Optional; |
| 40 | import java.util.stream.Collectors; |
| 41 | |
| 42 | import static com.google.common.base.Preconditions.checkNotNull; |
| 43 | |
| 44 | /** |
| 45 | * Device service implementation built on the virtual network service. |
| 46 | */ |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 47 | public class VirtualNetworkDeviceManager |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 48 | extends AbstractVirtualListenerManager<DeviceEvent, DeviceListener> |
| 49 | implements DeviceService { |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 50 | |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 51 | private static final String TYPE_NULL = "Type cannot be null"; |
| 52 | private static final String DEVICE_NULL = "Device cannot be null"; |
Viswanath KSP | 22774cd | 2016-08-20 20:06:30 +0530 | [diff] [blame] | 53 | private static final String PORT_NUMBER_NULL = "PortNumber cannot be null"; |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 54 | private VirtualNetworkListener virtualNetworkListener = new InternalVirtualNetworkListener(); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 55 | |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 56 | /** |
| 57 | * Creates a new VirtualNetworkDeviceService object. |
| 58 | * |
| 59 | * @param virtualNetworkManager virtual network manager service |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 60 | * @param networkId a virtual network identifier |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 61 | */ |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 62 | public VirtualNetworkDeviceManager(VirtualNetworkService virtualNetworkManager, |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 63 | NetworkId networkId) { |
Yoonseon Han | b14461b | 2017-03-07 14:08:01 +0900 | [diff] [blame] | 64 | super(virtualNetworkManager, networkId, DeviceEvent.class); |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 65 | manager.addListener(virtualNetworkListener); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public int getDeviceCount() { |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 70 | return manager.getVirtualDevices(this.networkId).size(); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public Iterable<Device> getDevices() { |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 75 | return manager.getVirtualDevices( |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 76 | this.networkId).stream().collect(Collectors.toSet()); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public Iterable<Device> getDevices(Device.Type type) { |
| 81 | checkNotNull(type, TYPE_NULL); |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 82 | return manager.getVirtualDevices(this.networkId) |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 83 | .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); |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 101 | Optional<VirtualDevice> foundDevice = |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 102 | manager.getVirtualDevices(this.networkId) |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 103 | .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); |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 122 | return manager.getVirtualPorts(this.networkId, deviceId) |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 123 | .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 |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 142 | public PortStatistics getStatisticsForPort(DeviceId deviceId, |
| 143 | PortNumber portNumber) { |
Viswanath KSP | 22774cd | 2016-08-20 20:06:30 +0530 | [diff] [blame] | 144 | checkNotNull(deviceId, DEVICE_NULL); |
Sean Condon | 0bd777c | 2021-01-01 14:23:29 +0000 | [diff] [blame^] | 145 | checkNotNull(portNumber, PORT_NUMBER_NULL); |
Viswanath KSP | 22774cd | 2016-08-20 20:06:30 +0530 | [diff] [blame] | 146 | // TODO not supported at the moment. |
| 147 | return null; |
| 148 | } |
| 149 | |
| 150 | @Override |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 151 | public PortStatistics getDeltaStatisticsForPort(DeviceId deviceId, |
| 152 | PortNumber portNumber) { |
Viswanath KSP | 22774cd | 2016-08-20 20:06:30 +0530 | [diff] [blame] | 153 | checkNotNull(deviceId, DEVICE_NULL); |
Sean Condon | 0bd777c | 2021-01-01 14:23:29 +0000 | [diff] [blame^] | 154 | checkNotNull(portNumber, PORT_NUMBER_NULL); |
Viswanath KSP | 22774cd | 2016-08-20 20:06:30 +0530 | [diff] [blame] | 155 | // TODO not supported at the moment. |
| 156 | return null; |
| 157 | } |
| 158 | |
| 159 | @Override |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 160 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
| 161 | checkNotNull(deviceId, DEVICE_NULL); |
| 162 | |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 163 | Optional<VirtualPort> foundPort = |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 164 | manager.getVirtualPorts(this.networkId, deviceId) |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 165 | .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 Das | d5ec9e9 | 2017-01-17 10:40:18 -0800 | [diff] [blame] | 180 | public String localStatus(DeviceId deviceId) { |
| 181 | // TODO not supported at this time |
| 182 | return null; |
| 183 | } |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 184 | |
Ray Milkey | 054e23d | 2018-03-22 13:37:11 -0700 | [diff] [blame] | 185 | @Override |
| 186 | public long getLastUpdatedInstant(DeviceId deviceId) { |
| 187 | // TODO not supported at this time |
| 188 | return 0; |
| 189 | } |
| 190 | |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 191 | /** |
| 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 Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 230 | } |