blob: 5dfdd574e0f2004e2cd52a3de84295c8e8484cba [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;
20import org.onosproject.event.AbstractListenerManager;
21import org.onosproject.incubator.net.virtual.VirtualDevice;
22import org.onosproject.incubator.net.virtual.VirtualNetwork;
23import org.onosproject.incubator.net.virtual.VirtualNetworkService;
24import org.onosproject.incubator.net.virtual.VirtualPort;
25import 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 */
44public 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";
50
51 private final VirtualNetwork network;
52 private final VirtualNetworkService manager;
53
54 /**
55 * Creates a new VirtualNetworkDeviceService object.
56 *
57 * @param virtualNetworkManager virtual network manager service
58 * @param network virtual network
59 */
60 public VirtualNetworkDeviceService(VirtualNetworkService virtualNetworkManager, VirtualNetwork network) {
61 checkNotNull(network, NETWORK_NULL);
62 this.network = network;
63 this.manager = virtualNetworkManager;
64 }
65
66 @Override
67 public int getDeviceCount() {
68 return manager.getVirtualDevices(this.network.id()).size();
69 }
70
71 @Override
72 public Iterable<Device> getDevices() {
73 return manager.getVirtualDevices(this.network.id()).stream().collect(Collectors.toSet());
74 }
75
76 @Override
77 public Iterable<Device> getDevices(Device.Type type) {
78 checkNotNull(type, TYPE_NULL);
79 return manager.getVirtualDevices(this.network.id())
80 .stream()
81 .filter(device -> type.equals(device.type()))
82 .collect(Collectors.toSet());
83 }
84
85 @Override
86 public Iterable<Device> getAvailableDevices() {
87 return getDevices();
88 }
89
90 @Override
91 public Iterable<Device> getAvailableDevices(Device.Type type) {
92 return getDevices(type);
93 }
94
95 @Override
96 public Device getDevice(DeviceId deviceId) {
97 checkNotNull(deviceId, DEVICE_NULL);
98 Optional<VirtualDevice> foundDevice = manager.getVirtualDevices(this.network.id())
99 .stream()
100 .filter(device -> deviceId.equals(device.id()))
101 .findFirst();
102 if (foundDevice.isPresent()) {
103 return foundDevice.get();
104 }
105 return null;
106 }
107
108 @Override
109 public MastershipRole getRole(DeviceId deviceId) {
110 checkNotNull(deviceId, DEVICE_NULL);
111 // TODO hard coded to master for now.
112 return MastershipRole.MASTER;
113 }
114
115 @Override
116 public List<Port> getPorts(DeviceId deviceId) {
117 checkNotNull(deviceId, DEVICE_NULL);
118 return manager.getVirtualPorts(this.network.id(), deviceId)
119 .stream()
120 .collect(Collectors.toList());
121 }
122
123 @Override
124 public List<PortStatistics> getPortStatistics(DeviceId deviceId) {
125 checkNotNull(deviceId, DEVICE_NULL);
126 // TODO not supported at the moment.
127 return ImmutableList.of();
128 }
129
130 @Override
131 public List<PortStatistics> getPortDeltaStatistics(DeviceId deviceId) {
132 checkNotNull(deviceId, DEVICE_NULL);
133 // TODO not supported at the moment.
134 return ImmutableList.of();
135 }
136
137 @Override
138 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
139 checkNotNull(deviceId, DEVICE_NULL);
140
141 Optional<VirtualPort> foundPort = manager.getVirtualPorts(this.network.id(), deviceId)
142 .stream()
143 .filter(port -> port.number().equals(portNumber))
144 .findFirst();
145 if (foundPort.isPresent()) {
146 return foundPort.get();
147 }
148 return null;
149 }
150
151 @Override
152 public boolean isAvailable(DeviceId deviceId) {
153 return getDevice(deviceId) != null;
154 }
155
156 @Override
157 public VirtualNetwork network() {
158 return network;
159 }
160}