Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2020-present Open Networking Foundation |
| 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.t3.api; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableMap; |
| 21 | import org.onosproject.net.ConnectPoint; |
| 22 | import org.onosproject.net.Device; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.Port; |
| 25 | |
| 26 | import java.util.List; |
| 27 | import java.util.Map; |
| 28 | import java.util.Set; |
| 29 | |
| 30 | /** |
| 31 | * Represents Network Information Base (NIB) for devices |
| 32 | * and supports alternative functions to |
| 33 | * {@link org.onosproject.net.device.DeviceService} for offline data. |
| 34 | */ |
| 35 | public class DeviceNib { |
| 36 | |
| 37 | private Map<Device, Set<Port>> devicePortMap; |
| 38 | |
| 39 | // use the singleton helper to create the instance |
| 40 | protected DeviceNib() { |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Sets a map of device : ports of the device. |
| 45 | * |
| 46 | * @param devicePortMap device-ports map |
| 47 | */ |
| 48 | public void setDevicePortMap(Map<Device, Set<Port>> devicePortMap) { |
| 49 | this.devicePortMap = devicePortMap; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Returns the device-ports map. |
| 54 | * |
| 55 | * @return device-ports map |
| 56 | */ |
| 57 | public Map<Device, Set<Port>> getDevicePortMap() { |
| 58 | return ImmutableMap.copyOf(devicePortMap); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Returns the device with the specified identifier. |
| 63 | * |
| 64 | * @param deviceId device identifier |
| 65 | * @return device or null if one with the given identifier is not known |
| 66 | */ |
| 67 | public Device getDevice(DeviceId deviceId) { |
| 68 | return devicePortMap.keySet().stream() |
| 69 | .filter(device -> device.id().equals(deviceId)) |
| 70 | .findFirst().orElse(null); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Returns the port with the specified connect point. |
| 75 | * |
| 76 | * @param cp connect point |
| 77 | * @return device port |
| 78 | */ |
| 79 | public Port getPort(ConnectPoint cp) { |
| 80 | return devicePortMap.get(getDevice(cp.deviceId())).stream() |
| 81 | .filter(port -> port.number().equals(cp.port())) |
| 82 | .findFirst().orElse(null); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns the list of ports associated with the device. |
| 87 | * |
| 88 | * @param deviceId device identifier |
| 89 | * @return list of ports |
| 90 | */ |
| 91 | public List<Port> getPorts(DeviceId deviceId) { |
| 92 | return ImmutableList.copyOf(devicePortMap.get(getDevice(deviceId))); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Indicates whether or not the device is presently online and available. |
| 97 | * Availability, unlike reachability, denotes whether ANY node in the |
| 98 | * cluster can discover that this device is in an operational state, |
| 99 | * this does not necessarily mean that there exists a node that can |
| 100 | * control this device. |
| 101 | * |
| 102 | * @param deviceId device identifier |
| 103 | * @return true if the device is available |
| 104 | */ |
| 105 | public boolean isAvailable(DeviceId deviceId) { |
| 106 | Device device = getDevice(deviceId); |
| 107 | // TODO: may need an extra REST API to get availableDevices from DeviceService, not from device annotations |
| 108 | return device.annotations().value("available").equals("true") ? true : false; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Returns the singleton instance of devices NIB. |
| 113 | * |
| 114 | * @return instance of devices NIB |
| 115 | */ |
| 116 | public static DeviceNib getInstance() { |
| 117 | return DeviceNib.SingletonHelper.INSTANCE; |
| 118 | } |
| 119 | |
| 120 | private static class SingletonHelper { |
| 121 | private static final DeviceNib INSTANCE = new DeviceNib(); |
| 122 | } |
| 123 | |
| 124 | } |