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; |
| 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 | */ |
| 44 | public class VirtualNetworkDeviceService extends AbstractListenerManager<DeviceEvent, DeviceListener> |
| 45 | implements DeviceService, VnetService { |
| 46 | |
| 47 | private static final String NETWORK_NULL = "Network ID cannot be null"; |
| 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 | |
| 52 | private final VirtualNetwork network; |
| 53 | private final VirtualNetworkService manager; |
| 54 | |
| 55 | /** |
| 56 | * Creates a new VirtualNetworkDeviceService object. |
| 57 | * |
| 58 | * @param virtualNetworkManager virtual network manager service |
| 59 | * @param network virtual network |
| 60 | */ |
| 61 | public VirtualNetworkDeviceService(VirtualNetworkService virtualNetworkManager, VirtualNetwork network) { |
| 62 | checkNotNull(network, NETWORK_NULL); |
| 63 | this.network = network; |
| 64 | this.manager = virtualNetworkManager; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public int getDeviceCount() { |
| 69 | return manager.getVirtualDevices(this.network.id()).size(); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public Iterable<Device> getDevices() { |
| 74 | return manager.getVirtualDevices(this.network.id()).stream().collect(Collectors.toSet()); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public Iterable<Device> getDevices(Device.Type type) { |
| 79 | checkNotNull(type, TYPE_NULL); |
| 80 | return manager.getVirtualDevices(this.network.id()) |
| 81 | .stream() |
| 82 | .filter(device -> type.equals(device.type())) |
| 83 | .collect(Collectors.toSet()); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public Iterable<Device> getAvailableDevices() { |
| 88 | return getDevices(); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public Iterable<Device> getAvailableDevices(Device.Type type) { |
| 93 | return getDevices(type); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public Device getDevice(DeviceId deviceId) { |
| 98 | checkNotNull(deviceId, DEVICE_NULL); |
| 99 | Optional<VirtualDevice> foundDevice = manager.getVirtualDevices(this.network.id()) |
| 100 | .stream() |
| 101 | .filter(device -> deviceId.equals(device.id())) |
| 102 | .findFirst(); |
| 103 | if (foundDevice.isPresent()) { |
| 104 | return foundDevice.get(); |
| 105 | } |
| 106 | return null; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public MastershipRole getRole(DeviceId deviceId) { |
| 111 | checkNotNull(deviceId, DEVICE_NULL); |
| 112 | // TODO hard coded to master for now. |
| 113 | return MastershipRole.MASTER; |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public List<Port> getPorts(DeviceId deviceId) { |
| 118 | checkNotNull(deviceId, DEVICE_NULL); |
| 119 | return manager.getVirtualPorts(this.network.id(), deviceId) |
| 120 | .stream() |
| 121 | .collect(Collectors.toList()); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public List<PortStatistics> getPortStatistics(DeviceId deviceId) { |
| 126 | checkNotNull(deviceId, DEVICE_NULL); |
| 127 | // TODO not supported at the moment. |
| 128 | return ImmutableList.of(); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public List<PortStatistics> getPortDeltaStatistics(DeviceId deviceId) { |
| 133 | checkNotNull(deviceId, DEVICE_NULL); |
| 134 | // TODO not supported at the moment. |
| 135 | return ImmutableList.of(); |
| 136 | } |
| 137 | |
| 138 | @Override |
Viswanath KSP | 22774cd | 2016-08-20 20:06:30 +0530 | [diff] [blame^] | 139 | public PortStatistics getStatisticsForPort(DeviceId deviceId, PortNumber portNumber) { |
| 140 | checkNotNull(deviceId, DEVICE_NULL); |
| 141 | checkNotNull(deviceId, PORT_NUMBER_NULL); |
| 142 | // TODO not supported at the moment. |
| 143 | return null; |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public PortStatistics getDeltaStatisticsForPort(DeviceId deviceId, PortNumber portNumber) { |
| 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 | |
| 158 | Optional<VirtualPort> foundPort = manager.getVirtualPorts(this.network.id(), deviceId) |
| 159 | .stream() |
| 160 | .filter(port -> port.number().equals(portNumber)) |
| 161 | .findFirst(); |
| 162 | if (foundPort.isPresent()) { |
| 163 | return foundPort.get(); |
| 164 | } |
| 165 | return null; |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public boolean isAvailable(DeviceId deviceId) { |
| 170 | return getDevice(deviceId) != null; |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public VirtualNetwork network() { |
| 175 | return network; |
| 176 | } |
| 177 | } |