Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 15 | */ |
Jonathan Hart | 9bb32ab | 2015-05-05 18:17:31 -0700 | [diff] [blame] | 16 | package org.onosproject.rest.resources; |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 19 | import com.google.common.base.Strings; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Lists; |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Maps; |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 22 | |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 23 | import org.onlab.packet.ChassisId; |
| 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onlab.packet.MacAddress; |
| 26 | import org.onlab.packet.VlanId; |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 27 | import org.onlab.util.Frequency; |
Sho SHIMIZU | 70e76d8 | 2016-01-05 16:26:26 -0800 | [diff] [blame] | 28 | import org.onlab.util.Spectrum; |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 29 | import org.onosproject.net.AnnotationKeys; |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 30 | import org.onosproject.net.ChannelSpacing; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 31 | import org.onosproject.net.ConnectPoint; |
| 32 | import org.onosproject.net.DefaultAnnotations; |
| 33 | import org.onosproject.net.Device; |
| 34 | import org.onosproject.net.DeviceId; |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 35 | import org.onosproject.net.GridType; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.net.Host; |
| 37 | import org.onosproject.net.HostId; |
| 38 | import org.onosproject.net.HostLocation; |
| 39 | import org.onosproject.net.Link; |
| 40 | import org.onosproject.net.MastershipRole; |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 41 | import org.onosproject.net.OchSignal; |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 42 | import org.onosproject.net.OduSignalType; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 43 | import org.onosproject.net.Port; |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 44 | import org.onosproject.net.PortNumber; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 45 | import org.onosproject.net.SparseAnnotations; |
| 46 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 47 | import org.onosproject.net.device.DefaultPortDescription; |
| 48 | import org.onosproject.net.device.DeviceDescription; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 49 | import org.onosproject.net.device.DeviceEvent; |
| 50 | import org.onosproject.net.device.DeviceListener; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 51 | import org.onosproject.net.device.DeviceProvider; |
| 52 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 53 | import org.onosproject.net.device.DeviceProviderService; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 54 | import org.onosproject.net.device.DeviceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 55 | import org.onosproject.net.device.PortDescription; |
| 56 | import org.onosproject.net.host.DefaultHostDescription; |
| 57 | import org.onosproject.net.host.HostProvider; |
| 58 | import org.onosproject.net.host.HostProviderRegistry; |
| 59 | import org.onosproject.net.host.HostProviderService; |
| 60 | import org.onosproject.net.link.DefaultLinkDescription; |
| 61 | import org.onosproject.net.link.LinkProvider; |
| 62 | import org.onosproject.net.link.LinkProviderRegistry; |
| 63 | import org.onosproject.net.link.LinkProviderService; |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 64 | import org.onosproject.net.optical.OchPort; |
HIGUCHI Yuta | 4c0ef6b | 2016-05-02 19:45:41 -0700 | [diff] [blame^] | 65 | import org.onosproject.net.optical.OduCltPort; |
HIGUCHI Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 66 | import org.onosproject.net.optical.OmsPort; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 67 | import org.onosproject.net.provider.ProviderId; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 68 | import org.slf4j.Logger; |
| 69 | import org.slf4j.LoggerFactory; |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 70 | |
| 71 | import java.net.URI; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 72 | import java.util.ArrayList; |
Thomas Vachuska | ece59ee | 2014-11-19 19:06:11 -0800 | [diff] [blame] | 73 | import java.util.HashSet; |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 74 | import java.util.Iterator; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 75 | import java.util.List; |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 76 | import java.util.Map; |
Thomas Vachuska | ece59ee | 2014-11-19 19:06:11 -0800 | [diff] [blame] | 77 | import java.util.Set; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 78 | import java.util.concurrent.CountDownLatch; |
| 79 | import java.util.concurrent.TimeUnit; |
| 80 | import java.util.stream.Collectors; |
Sho SHIMIZU | bb825d7 | 2015-06-11 10:56:32 -0700 | [diff] [blame] | 81 | import java.util.stream.Stream; |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 82 | |
| 83 | import static com.google.common.base.Preconditions.checkNotNull; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 84 | import static org.onosproject.net.DeviceId.deviceId; |
| 85 | import static org.onosproject.net.PortNumber.portNumber; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 86 | import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED; |
| 87 | import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED; |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 88 | import static org.onosproject.net.optical.device.OchPortHelper.ochPortDescription; |
HIGUCHI Yuta | 4c0ef6b | 2016-05-02 19:45:41 -0700 | [diff] [blame^] | 89 | import static org.onosproject.net.optical.device.OduCltPortHelper.oduCltPortDescription; |
HIGUCHI Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 90 | import static org.onosproject.net.optical.device.OmsPortHelper.omsPortDescription; |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 91 | import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView; |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * Provider of devices and links parsed from a JSON configuration structure. |
Thomas Vachuska | f9879c5 | 2016-01-15 11:12:19 -0800 | [diff] [blame] | 95 | * |
| 96 | * @deprecated in 1.5.0 (Falcon) |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 97 | */ |
Thomas Vachuska | f9879c5 | 2016-01-15 11:12:19 -0800 | [diff] [blame] | 98 | @Deprecated |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 99 | class ConfigProvider implements DeviceProvider, LinkProvider, HostProvider { |
| 100 | |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 101 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 102 | |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 103 | private static final ProviderId PID = |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 104 | new ProviderId("cfg", "org.onosproject.rest", true); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 105 | |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 106 | private static final String UNKNOWN = "unknown"; |
| 107 | |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 108 | // C-band has 4.4 THz (4,400 GHz) total bandwidth |
Sho SHIMIZU | 00762ee | 2016-01-05 16:32:24 -0800 | [diff] [blame] | 109 | private static final Frequency TOTAL = Frequency.ofGHz(4_400); |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 110 | |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 111 | private CountDownLatch deviceLatch; |
| 112 | |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 113 | private final JsonNode cfg; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 114 | private final DeviceService deviceService; |
| 115 | |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 116 | private final DeviceProviderRegistry deviceProviderRegistry; |
| 117 | private final LinkProviderRegistry linkProviderRegistry; |
| 118 | private final HostProviderRegistry hostProviderRegistry; |
| 119 | |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 120 | private DeviceProviderService deviceProviderService; |
| 121 | private LinkProviderService linkProviderService; |
| 122 | private HostProviderService hostProviderService; |
| 123 | |
| 124 | private DeviceListener deviceEventCounter = new DeviceEventCounter(); |
| 125 | private List<ConnectPoint> connectPoints = Lists.newArrayList(); |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 126 | private Map<ConnectPoint, PortDescription> descriptions = Maps.newHashMap(); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 127 | |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 128 | /** |
| 129 | * Creates a new configuration provider. |
| 130 | * |
| 131 | * @param cfg JSON configuration |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 132 | * @param deviceService device service |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 133 | * @param deviceProviderRegistry device provider registry |
| 134 | * @param linkProviderRegistry link provider registry |
| 135 | * @param hostProviderRegistry host provider registry |
| 136 | */ |
| 137 | ConfigProvider(JsonNode cfg, |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 138 | DeviceService deviceService, |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 139 | DeviceProviderRegistry deviceProviderRegistry, |
| 140 | LinkProviderRegistry linkProviderRegistry, |
| 141 | HostProviderRegistry hostProviderRegistry) { |
| 142 | this.cfg = checkNotNull(cfg, "Configuration cannot be null"); |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 143 | this.deviceService = opticalView(checkNotNull(deviceService, "Device service cannot be null")); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 144 | this.deviceProviderRegistry = checkNotNull(deviceProviderRegistry, "Device provider registry cannot be null"); |
| 145 | this.linkProviderRegistry = checkNotNull(linkProviderRegistry, "Link provider registry cannot be null"); |
| 146 | this.hostProviderRegistry = checkNotNull(hostProviderRegistry, "Host provider registry cannot be null"); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Parses the given JSON and provides links as configured. |
| 151 | */ |
| 152 | void parse() { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 153 | try { |
| 154 | register(); |
| 155 | parseDevices(); |
| 156 | parseLinks(); |
| 157 | parseHosts(); |
| 158 | addMissingPorts(); |
| 159 | } finally { |
| 160 | unregister(); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | private void register() { |
| 165 | deviceProviderService = deviceProviderRegistry.register(this); |
| 166 | linkProviderService = linkProviderRegistry.register(this); |
| 167 | hostProviderService = hostProviderRegistry.register(this); |
| 168 | } |
| 169 | |
| 170 | private void unregister() { |
| 171 | deviceProviderRegistry.unregister(this); |
| 172 | linkProviderRegistry.unregister(this); |
| 173 | hostProviderRegistry.unregister(this); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | // Parses the given JSON and provides devices. |
| 177 | private void parseDevices() { |
| 178 | try { |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 179 | JsonNode nodes = cfg.get("devices"); |
| 180 | if (nodes != null) { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 181 | prepareForDeviceEvents(nodes.size()); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 182 | for (JsonNode node : nodes) { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 183 | parseDevice(node); |
Thomas Vachuska | 3b84c86 | 2015-04-28 12:09:48 -0700 | [diff] [blame] | 184 | |
| 185 | // FIXME: hack to make sure device attributes take |
| 186 | // This will be fixed when GossipDeviceStore uses ECM |
| 187 | parseDevice(node); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | } finally { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 191 | waitForDeviceEvents(); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| 195 | // Parses the given node with device data and supplies the device. |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 196 | private void parseDevice(JsonNode node) { |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 197 | URI uri = URI.create(get(node, "uri")); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 198 | Device.Type type = Device.Type.valueOf(get(node, "type", "SWITCH")); |
| 199 | String mfr = get(node, "mfr", UNKNOWN); |
| 200 | String hw = get(node, "hw", UNKNOWN); |
| 201 | String sw = get(node, "sw", UNKNOWN); |
| 202 | String serial = get(node, "serial", UNKNOWN); |
| 203 | ChassisId cid = new ChassisId(get(node, "mac", "000000000000")); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 204 | SparseAnnotations annotations = annotations(node.get("annotations")); |
| 205 | |
| 206 | DeviceDescription desc = |
| 207 | new DefaultDeviceDescription(uri, type, mfr, hw, sw, serial, |
| 208 | cid, annotations); |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 209 | DeviceId deviceId = deviceId(uri); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 210 | deviceProviderService.deviceConnected(deviceId, desc); |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 211 | |
| 212 | JsonNode ports = node.get("ports"); |
| 213 | if (ports != null) { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 214 | parsePorts(deviceId, ports); |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
| 218 | // Parses the given node with list of device ports. |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 219 | private void parsePorts(DeviceId deviceId, JsonNode nodes) { |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 220 | List<PortDescription> ports = new ArrayList<>(); |
| 221 | for (JsonNode node : nodes) { |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 222 | ports.add(parsePort(deviceId, node)); |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 223 | } |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 224 | deviceProviderService.updatePorts(deviceId, ports); |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | // Parses the given node with port information. |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 228 | private PortDescription parsePort(DeviceId deviceId, JsonNode node) { |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 229 | Port.Type type = Port.Type.valueOf(node.path("type").asText("COPPER")); |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 230 | // TL1-based ports have a name |
| 231 | PortNumber port = null; |
| 232 | if (node.has("name")) { |
| 233 | for (Port p : deviceService.getPorts(deviceId)) { |
| 234 | if (p.number().name().equals(node.get("name").asText())) { |
| 235 | port = p.number(); |
| 236 | break; |
| 237 | } |
| 238 | } |
| 239 | } else { |
| 240 | port = portNumber(node.path("port").asLong(0)); |
| 241 | } |
| 242 | |
Marc De Leenheer | c973308 | 2015-06-04 12:22:38 -0700 | [diff] [blame] | 243 | if (port == null) { |
| 244 | log.error("Cannot find port given in node {}", node); |
| 245 | return null; |
| 246 | } |
| 247 | |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 248 | String portName = Strings.emptyToNull(port.name()); |
| 249 | SparseAnnotations annotations = null; |
| 250 | if (portName != null) { |
| 251 | annotations = DefaultAnnotations.builder() |
| 252 | .set(AnnotationKeys.PORT_NAME, portName).build(); |
| 253 | } |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 254 | switch (type) { |
| 255 | case COPPER: |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 256 | return new DefaultPortDescription(port, node.path("enabled").asBoolean(true), |
| 257 | type, node.path("speed").asLong(1_000), |
| 258 | annotations); |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 259 | case FIBER: |
| 260 | // Currently, assume OMS when FIBER. Provide sane defaults. |
Marc De Leenheer | fc913dd | 2015-07-30 16:04:55 -0700 | [diff] [blame] | 261 | annotations = annotations(node.get("annotations")); |
HIGUCHI Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 262 | return omsPortDescription(port, node.path("enabled").asBoolean(true), |
Sho SHIMIZU | 70e76d8 | 2016-01-05 16:26:26 -0800 | [diff] [blame] | 263 | Spectrum.CENTER_FREQUENCY, Spectrum.CENTER_FREQUENCY.add(TOTAL), |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 264 | Frequency.ofGHz(100), annotations); |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 265 | case ODUCLT: |
| 266 | annotations = annotations(node.get("annotations")); |
| 267 | OduCltPort oduCltPort = (OduCltPort) deviceService.getPort(deviceId, port); |
HIGUCHI Yuta | 4c0ef6b | 2016-05-02 19:45:41 -0700 | [diff] [blame^] | 268 | return oduCltPortDescription(port, node.path("enabled").asBoolean(true), |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 269 | oduCltPort.signalType(), annotations); |
| 270 | case OCH: |
| 271 | annotations = annotations(node.get("annotations")); |
| 272 | OchPort ochPort = (OchPort) deviceService.getPort(deviceId, port); |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 273 | return ochPortDescription(port, node.path("enabled").asBoolean(true), |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 274 | ochPort.signalType(), ochPort.isTunable(), |
| 275 | ochPort.lambda(), annotations); |
| 276 | case OMS: |
| 277 | annotations = annotations(node.get("annotations")); |
| 278 | OmsPort omsPort = (OmsPort) deviceService.getPort(deviceId, port); |
HIGUCHI Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 279 | return omsPortDescription(port, node.path("enabled").asBoolean(true), |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 280 | omsPort.minFrequency(), omsPort.maxFrequency(), omsPort.grid(), annotations); |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 281 | default: |
| 282 | log.warn("{}: Unsupported Port Type"); |
| 283 | } |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 284 | return new DefaultPortDescription(port, node.path("enabled").asBoolean(true), |
| 285 | type, node.path("speed").asLong(1_000), |
| 286 | annotations); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | // Parses the given JSON and provides links as configured. |
| 290 | private void parseLinks() { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 291 | JsonNode nodes = cfg.get("links"); |
| 292 | if (nodes != null) { |
| 293 | for (JsonNode node : nodes) { |
| 294 | parseLink(node, false); |
| 295 | if (!node.has("halfplex")) { |
| 296 | parseLink(node, true); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 297 | } |
| 298 | } |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
| 302 | // Parses the given node with link data and supplies the link. |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 303 | private void parseLink(JsonNode node, boolean reverse) { |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 304 | ConnectPoint src = connectPoint(get(node, "src")); |
| 305 | ConnectPoint dst = connectPoint(get(node, "dst")); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 306 | Link.Type type = Link.Type.valueOf(get(node, "type", "DIRECT")); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 307 | SparseAnnotations annotations = annotations(node.get("annotations")); |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 308 | // take annotations to update optical ports with correct attributes. |
| 309 | updatePorts(src, dst, annotations); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 310 | DefaultLinkDescription desc = reverse ? |
| 311 | new DefaultLinkDescription(dst, src, type, annotations) : |
| 312 | new DefaultLinkDescription(src, dst, type, annotations); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 313 | linkProviderService.linkDetected(desc); |
| 314 | |
| 315 | connectPoints.add(src); |
| 316 | connectPoints.add(dst); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 319 | private void updatePorts(ConnectPoint src, ConnectPoint dst, SparseAnnotations annotations) { |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 320 | final String linkType = annotations.value("optical.type"); |
| 321 | if ("cross-connect".equals(linkType)) { |
| 322 | String value = annotations.value("bandwidth").trim(); |
| 323 | try { |
| 324 | double bw = Double.parseDouble(value); |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 325 | updateOchPort(bw, src, dst); |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 326 | } catch (NumberFormatException e) { |
| 327 | log.warn("Invalid bandwidth ({}), can't configure port(s)", value); |
| 328 | return; |
| 329 | } |
| 330 | } else if ("WDM".equals(linkType)) { |
| 331 | String value = annotations.value("optical.waves").trim(); |
| 332 | try { |
| 333 | int numChls = Integer.parseInt(value); |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 334 | updateOmsPorts(numChls, src, dst); |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 335 | } catch (NumberFormatException e) { |
| 336 | log.warn("Invalid channel ({}), can't configure port(s)", value); |
| 337 | return; |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | // uses 'bandwidth' annotation to determine the channel spacing. |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 343 | private void updateOchPort(double bw, ConnectPoint srcCp, ConnectPoint dstCp) { |
| 344 | Device src = deviceService.getDevice(srcCp.deviceId()); |
| 345 | Device dst = deviceService.getDevice(dstCp.deviceId()); |
| 346 | // bandwidth in MHz (assuming Hz - linc is not clear if that or Mb). |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 347 | Frequency spacing = Frequency.ofMHz(bw); |
| 348 | // channel bandwidth is smaller than smallest standard channel spacing. |
| 349 | ChannelSpacing chsp = null; |
| 350 | if (spacing.compareTo(ChannelSpacing.CHL_6P25GHZ.frequency()) <= 0) { |
| 351 | chsp = ChannelSpacing.CHL_6P25GHZ; |
| 352 | } |
| 353 | for (int i = 1; i < ChannelSpacing.values().length; i++) { |
| 354 | Frequency val = ChannelSpacing.values()[i].frequency(); |
| 355 | // pick the next highest or equal channel interval. |
| 356 | if (val.isLessThan(spacing)) { |
| 357 | chsp = ChannelSpacing.values()[i - 1]; |
| 358 | break; |
| 359 | } |
| 360 | } |
| 361 | if (chsp == null) { |
| 362 | log.warn("Invalid channel spacing ({}), can't configure port(s)", spacing); |
| 363 | return; |
| 364 | } |
| 365 | OchSignal signal = new OchSignal(GridType.DWDM, chsp, 1, 1); |
| 366 | if (src.type() == Device.Type.ROADM) { |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 367 | PortDescription portDesc = ochPortDescription(srcCp.port(), true, |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 368 | OduSignalType.ODU4, true, signal); |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 369 | descriptions.put(srcCp, portDesc); |
| 370 | deviceProviderService.portStatusChanged(srcCp.deviceId(), portDesc); |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 371 | } |
| 372 | if (dst.type() == Device.Type.ROADM) { |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 373 | PortDescription portDesc = ochPortDescription(dstCp.port(), true, |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 374 | OduSignalType.ODU4, true, signal); |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 375 | descriptions.put(dstCp, portDesc); |
| 376 | deviceProviderService.portStatusChanged(dstCp.deviceId(), portDesc); |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 380 | private void updateOmsPorts(int numChls, ConnectPoint srcCp, ConnectPoint dstCp) { |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 381 | // round down to largest slot that allows numChl channels to fit into C band range |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 382 | ChannelSpacing chl = null; |
| 383 | Frequency perChl = TOTAL.floorDivision(numChls); |
| 384 | for (int i = 0; i < ChannelSpacing.values().length; i++) { |
| 385 | Frequency val = ChannelSpacing.values()[i].frequency(); |
| 386 | if (val.isLessThan(perChl)) { |
| 387 | chl = ChannelSpacing.values()[i]; |
| 388 | break; |
| 389 | } |
| 390 | } |
| 391 | if (chl == null) { |
| 392 | chl = ChannelSpacing.CHL_6P25GHZ; |
| 393 | } |
| 394 | |
| 395 | // if true, there was less channels than can be tightly packed. |
Sho SHIMIZU | f6501c0 | 2015-06-10 19:30:24 -0700 | [diff] [blame] | 396 | Frequency grid = chl.frequency(); |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 397 | // say Linc's 1st slot starts at CENTER and goes up from there. |
Sho SHIMIZU | 70e76d8 | 2016-01-05 16:26:26 -0800 | [diff] [blame] | 398 | Frequency min = Spectrum.CENTER_FREQUENCY.add(grid); |
| 399 | Frequency max = Spectrum.CENTER_FREQUENCY.add(grid.multiply(numChls)); |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 400 | |
HIGUCHI Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 401 | PortDescription srcPortDesc = omsPortDescription(srcCp.port(), true, min, max, grid); |
| 402 | PortDescription dstPortDesc = omsPortDescription(dstCp.port(), true, min, max, grid); |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 403 | descriptions.put(srcCp, srcPortDesc); |
| 404 | descriptions.put(dstCp, dstPortDesc); |
| 405 | deviceProviderService.portStatusChanged(srcCp.deviceId(), srcPortDesc); |
| 406 | deviceProviderService.portStatusChanged(dstCp.deviceId(), dstPortDesc); |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 409 | // Parses the given JSON and provides hosts as configured. |
| 410 | private void parseHosts() { |
| 411 | try { |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 412 | JsonNode nodes = cfg.get("hosts"); |
| 413 | if (nodes != null) { |
| 414 | for (JsonNode node : nodes) { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 415 | parseHost(node); |
Thomas Vachuska | 3b84c86 | 2015-04-28 12:09:48 -0700 | [diff] [blame] | 416 | |
| 417 | // FIXME: hack to make sure host attributes take |
| 418 | // This will be fixed when GossipHostStore uses ECM |
| 419 | parseHost(node); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | } finally { |
| 423 | hostProviderRegistry.unregister(this); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | // Parses the given node with host data and supplies the host. |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 428 | private void parseHost(JsonNode node) { |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 429 | MacAddress mac = MacAddress.valueOf(get(node, "mac")); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 430 | VlanId vlanId = VlanId.vlanId((short) node.get("vlan").asInt(VlanId.UNTAGGED)); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 431 | HostId hostId = HostId.hostId(mac, vlanId); |
| 432 | SparseAnnotations annotations = annotations(node.get("annotations")); |
| 433 | HostLocation location = new HostLocation(connectPoint(get(node, "location")), 0); |
Thomas Vachuska | ece59ee | 2014-11-19 19:06:11 -0800 | [diff] [blame] | 434 | |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 435 | String[] ipStrings = get(node, "ip", "").split(","); |
Thomas Vachuska | ece59ee | 2014-11-19 19:06:11 -0800 | [diff] [blame] | 436 | Set<IpAddress> ips = new HashSet<>(); |
| 437 | for (String ip : ipStrings) { |
| 438 | ips.add(IpAddress.valueOf(ip.trim())); |
| 439 | } |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 440 | |
| 441 | DefaultHostDescription desc = |
Thomas Vachuska | ece59ee | 2014-11-19 19:06:11 -0800 | [diff] [blame] | 442 | new DefaultHostDescription(mac, vlanId, location, ips, annotations); |
Ray Milkey | dc08344 | 2016-02-22 11:27:57 -0800 | [diff] [blame] | 443 | hostProviderService.hostDetected(hostId, desc, false); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 444 | |
| 445 | connectPoints.add(location); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 448 | // Adds any missing device ports for configured links and host locations. |
| 449 | private void addMissingPorts() { |
| 450 | deviceService.getDevices().forEach(this::addMissingPorts); |
| 451 | } |
| 452 | |
| 453 | // Adds any missing device ports. |
| 454 | private void addMissingPorts(Device device) { |
Jonathan Hart | eb431c1 | 2015-07-27 10:43:50 -0700 | [diff] [blame] | 455 | try { |
| 456 | List<Port> ports = deviceService.getPorts(device.id()); |
| 457 | Set<ConnectPoint> existing = ports.stream() |
| 458 | .map(p -> new ConnectPoint(device.id(), p.number())) |
| 459 | .collect(Collectors.toSet()); |
| 460 | Set<ConnectPoint> missing = connectPoints.stream() |
| 461 | .filter(cp -> cp.deviceId().equals(device.id())) |
| 462 | .filter(cp -> !existing.contains(cp)) |
| 463 | .collect(Collectors.toSet()); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 464 | |
Jonathan Hart | eb431c1 | 2015-07-27 10:43:50 -0700 | [diff] [blame] | 465 | if (!missing.isEmpty()) { |
| 466 | List<PortDescription> newPorts = Stream.concat( |
| 467 | ports.stream().map(this::description), |
| 468 | missing.stream().map(this::description) |
| 469 | ).collect(Collectors.toList()); |
| 470 | deviceProviderService.updatePorts(device.id(), newPorts); |
| 471 | } |
| 472 | } catch (IllegalArgumentException e) { |
| 473 | log.warn("Error pushing ports: {}", e.getMessage()); |
| 474 | } |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | // Creates a port description from the specified port. |
| 478 | private PortDescription description(Port p) { |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 479 | switch (p.type()) { |
| 480 | case OMS: |
| 481 | OmsPort op = (OmsPort) p; |
HIGUCHI Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 482 | return omsPortDescription( |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 483 | op.number(), op.isEnabled(), op.minFrequency(), op.maxFrequency(), op.grid()); |
| 484 | case OCH: |
| 485 | OchPort ochp = (OchPort) p; |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 486 | return ochPortDescription( |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 487 | ochp.number(), ochp.isEnabled(), ochp.signalType(), ochp.isTunable(), ochp.lambda()); |
| 488 | case ODUCLT: |
| 489 | OduCltPort odup = (OduCltPort) p; |
HIGUCHI Yuta | 4c0ef6b | 2016-05-02 19:45:41 -0700 | [diff] [blame^] | 490 | return oduCltPortDescription( |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 491 | odup.number(), odup.isEnabled(), odup.signalType()); |
| 492 | default: |
| 493 | return new DefaultPortDescription(p.number(), p.isEnabled(), p.type(), p.portSpeed()); |
| 494 | } |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 497 | // Creates a port description from the specified connection point if none created earlier. |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 498 | private PortDescription description(ConnectPoint cp) { |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 499 | PortDescription saved = descriptions.get(cp); |
| 500 | if (saved != null) { |
| 501 | return saved; |
| 502 | } |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 503 | Port p = deviceService.getPort(cp.deviceId(), cp.port()); |
| 504 | if (p == null) { |
| 505 | return new DefaultPortDescription(cp.port(), true); |
| 506 | } |
| 507 | return description(p); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 510 | // Produces set of annotations from the given JSON node. |
| 511 | private SparseAnnotations annotations(JsonNode node) { |
| 512 | if (node == null) { |
Sho SHIMIZU | a3d67cd | 2015-06-04 11:46:48 -0700 | [diff] [blame] | 513 | return DefaultAnnotations.EMPTY; |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | DefaultAnnotations.Builder builder = DefaultAnnotations.builder(); |
| 517 | Iterator<String> it = node.fieldNames(); |
| 518 | while (it.hasNext()) { |
| 519 | String k = it.next(); |
| 520 | builder.set(k, node.get(k).asText()); |
| 521 | } |
| 522 | return builder.build(); |
| 523 | } |
| 524 | |
| 525 | // Produces a connection point from the specified uri/port text. |
| 526 | private ConnectPoint connectPoint(String text) { |
| 527 | int i = text.lastIndexOf("/"); |
Ayaka Koshibe | c10d151 | 2015-06-04 15:17:58 -0700 | [diff] [blame] | 528 | String portName = text.substring(i + 1); |
| 529 | DeviceId deviceId = deviceId(text.substring(0, i)); |
| 530 | |
Ayaka Koshibe | c10d151 | 2015-06-04 15:17:58 -0700 | [diff] [blame] | 531 | for (Port port : deviceService.getPorts(deviceId)) { |
| 532 | PortNumber pn = port.number(); |
| 533 | if (pn.name().equals(portName)) { |
| 534 | return new ConnectPoint(deviceId, pn); |
| 535 | } |
| 536 | } |
Sho SHIMIZU | ccf40c7 | 2015-06-11 16:55:20 -0700 | [diff] [blame] | 537 | |
| 538 | long portNum; |
| 539 | try { |
| 540 | portNum = Long.parseLong(portName); |
| 541 | } catch (NumberFormatException e) { |
| 542 | portNum = 0; |
| 543 | } |
| 544 | |
Ayaka Koshibe | c10d151 | 2015-06-04 15:17:58 -0700 | [diff] [blame] | 545 | return new ConnectPoint(deviceId, portNumber(portNum, portName)); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | // Returns string form of the named property in the given JSON object. |
| 549 | private String get(JsonNode node, String name) { |
| 550 | return node.path(name).asText(); |
| 551 | } |
| 552 | |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 553 | // Returns string form of the named property in the given JSON object. |
| 554 | private String get(JsonNode node, String name, String defaultValue) { |
| 555 | return node.path(name).asText(defaultValue); |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | @Override |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 559 | public void roleChanged(DeviceId device, MastershipRole newRole) { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 560 | deviceProviderService.receivedRoleReply(device, newRole, newRole); |
| 561 | } |
| 562 | |
| 563 | @Override |
| 564 | public void triggerProbe(DeviceId deviceId) { |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | @Override |
| 568 | public void triggerProbe(Host host) { |
| 569 | } |
| 570 | |
| 571 | @Override |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 572 | public void changePortState(DeviceId deviceId, PortNumber portNumber, |
| 573 | boolean enable) { |
| 574 | } |
| 575 | |
| 576 | @Override |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 577 | public ProviderId id() { |
| 578 | return PID; |
| 579 | } |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 580 | |
| 581 | @Override |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 582 | public boolean isReachable(DeviceId device) { |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 583 | return true; |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 584 | } |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 585 | |
| 586 | /** |
| 587 | * Prepares to count device added/available/removed events. |
| 588 | * |
| 589 | * @param count number of events to count |
| 590 | */ |
| 591 | protected void prepareForDeviceEvents(int count) { |
| 592 | deviceLatch = new CountDownLatch(count); |
| 593 | deviceService.addListener(deviceEventCounter); |
| 594 | } |
| 595 | |
| 596 | /** |
| 597 | * Waits for all expected device added/available/removed events. |
| 598 | */ |
| 599 | protected void waitForDeviceEvents() { |
| 600 | try { |
| 601 | deviceLatch.await(2, TimeUnit.SECONDS); |
| 602 | } catch (InterruptedException e) { |
| 603 | log.warn("Device events did not arrive in time"); |
| 604 | } |
| 605 | deviceService.removeListener(deviceEventCounter); |
| 606 | } |
| 607 | |
| 608 | // Counts down number of device added/available/removed events. |
| 609 | private class DeviceEventCounter implements DeviceListener { |
| 610 | @Override |
| 611 | public void event(DeviceEvent event) { |
| 612 | DeviceEvent.Type type = event.type(); |
| 613 | if (type == DEVICE_ADDED || type == DEVICE_AVAILABILITY_CHANGED) { |
| 614 | deviceLatch.countDown(); |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 619 | |
| 620 | |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 621 | } |