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