Committed by
Gerrit Code Review
Remove deprecated ConfigProvider.
Change-Id: Ibf927671b0729212e9d1d2bbfd19d4aaab48b9dd
Showing
3 changed files
with
0 additions
and
691 deletions
| 1 | -/* | ||
| 2 | - * Copyright 2015-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 | -package org.onosproject.rest.resources; | ||
| 17 | - | ||
| 18 | -import com.fasterxml.jackson.databind.JsonNode; | ||
| 19 | -import com.google.common.base.Strings; | ||
| 20 | -import com.google.common.collect.Lists; | ||
| 21 | -import com.google.common.collect.Maps; | ||
| 22 | - | ||
| 23 | -import org.onlab.packet.ChassisId; | ||
| 24 | -import org.onlab.packet.IpAddress; | ||
| 25 | -import org.onlab.packet.MacAddress; | ||
| 26 | -import org.onlab.packet.VlanId; | ||
| 27 | -import org.onlab.util.Frequency; | ||
| 28 | -import org.onlab.util.Spectrum; | ||
| 29 | -import org.onosproject.net.AnnotationKeys; | ||
| 30 | -import org.onosproject.net.ChannelSpacing; | ||
| 31 | -import org.onosproject.net.ConnectPoint; | ||
| 32 | -import org.onosproject.net.DefaultAnnotations; | ||
| 33 | -import org.onosproject.net.Device; | ||
| 34 | -import org.onosproject.net.DeviceId; | ||
| 35 | -import org.onosproject.net.GridType; | ||
| 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; | ||
| 41 | -import org.onosproject.net.OchSignal; | ||
| 42 | -import org.onosproject.net.OduSignalType; | ||
| 43 | -import org.onosproject.net.Port; | ||
| 44 | -import org.onosproject.net.PortNumber; | ||
| 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; | ||
| 49 | -import org.onosproject.net.device.DeviceEvent; | ||
| 50 | -import org.onosproject.net.device.DeviceListener; | ||
| 51 | -import org.onosproject.net.device.DeviceProvider; | ||
| 52 | -import org.onosproject.net.device.DeviceProviderRegistry; | ||
| 53 | -import org.onosproject.net.device.DeviceProviderService; | ||
| 54 | -import org.onosproject.net.device.DeviceService; | ||
| 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; | ||
| 64 | -import org.onosproject.net.optical.OchPort; | ||
| 65 | -import org.onosproject.net.optical.OduCltPort; | ||
| 66 | -import org.onosproject.net.optical.OmsPort; | ||
| 67 | -import org.onosproject.net.provider.ProviderId; | ||
| 68 | -import org.slf4j.Logger; | ||
| 69 | -import org.slf4j.LoggerFactory; | ||
| 70 | - | ||
| 71 | -import java.net.URI; | ||
| 72 | -import java.util.ArrayList; | ||
| 73 | -import java.util.HashSet; | ||
| 74 | -import java.util.Iterator; | ||
| 75 | -import java.util.List; | ||
| 76 | -import java.util.Map; | ||
| 77 | -import java.util.Set; | ||
| 78 | -import java.util.concurrent.CountDownLatch; | ||
| 79 | -import java.util.concurrent.TimeUnit; | ||
| 80 | -import java.util.stream.Collectors; | ||
| 81 | -import java.util.stream.Stream; | ||
| 82 | - | ||
| 83 | -import static com.google.common.base.Preconditions.checkNotNull; | ||
| 84 | -import static org.onosproject.net.DeviceId.deviceId; | ||
| 85 | -import static org.onosproject.net.PortNumber.portNumber; | ||
| 86 | -import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED; | ||
| 87 | -import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED; | ||
| 88 | -import static org.onosproject.net.optical.device.OchPortHelper.ochPortDescription; | ||
| 89 | -import static org.onosproject.net.optical.device.OduCltPortHelper.oduCltPortDescription; | ||
| 90 | -import static org.onosproject.net.optical.device.OmsPortHelper.omsPortDescription; | ||
| 91 | -import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView; | ||
| 92 | - | ||
| 93 | -/** | ||
| 94 | - * Provider of devices and links parsed from a JSON configuration structure. | ||
| 95 | - * | ||
| 96 | - * @deprecated in 1.5.0 (Falcon) | ||
| 97 | - */ | ||
| 98 | -@Deprecated | ||
| 99 | -class ConfigProvider implements DeviceProvider, LinkProvider, HostProvider { | ||
| 100 | - | ||
| 101 | - private final Logger log = LoggerFactory.getLogger(getClass()); | ||
| 102 | - | ||
| 103 | - private static final ProviderId PID = | ||
| 104 | - new ProviderId("cfg", "org.onosproject.rest", true); | ||
| 105 | - | ||
| 106 | - private static final String UNKNOWN = "unknown"; | ||
| 107 | - | ||
| 108 | - // C-band has 4.4 THz (4,400 GHz) total bandwidth | ||
| 109 | - private static final Frequency TOTAL = Frequency.ofGHz(4_400); | ||
| 110 | - | ||
| 111 | - private CountDownLatch deviceLatch; | ||
| 112 | - | ||
| 113 | - private final JsonNode cfg; | ||
| 114 | - private final DeviceService deviceService; | ||
| 115 | - | ||
| 116 | - private final DeviceProviderRegistry deviceProviderRegistry; | ||
| 117 | - private final LinkProviderRegistry linkProviderRegistry; | ||
| 118 | - private final HostProviderRegistry hostProviderRegistry; | ||
| 119 | - | ||
| 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(); | ||
| 126 | - private Map<ConnectPoint, PortDescription> descriptions = Maps.newHashMap(); | ||
| 127 | - | ||
| 128 | - /** | ||
| 129 | - * Creates a new configuration provider. | ||
| 130 | - * | ||
| 131 | - * @param cfg JSON configuration | ||
| 132 | - * @param deviceService device service | ||
| 133 | - * @param deviceProviderRegistry device provider registry | ||
| 134 | - * @param linkProviderRegistry link provider registry | ||
| 135 | - * @param hostProviderRegistry host provider registry | ||
| 136 | - */ | ||
| 137 | - ConfigProvider(JsonNode cfg, | ||
| 138 | - DeviceService deviceService, | ||
| 139 | - DeviceProviderRegistry deviceProviderRegistry, | ||
| 140 | - LinkProviderRegistry linkProviderRegistry, | ||
| 141 | - HostProviderRegistry hostProviderRegistry) { | ||
| 142 | - this.cfg = checkNotNull(cfg, "Configuration cannot be null"); | ||
| 143 | - this.deviceService = opticalView(checkNotNull(deviceService, "Device service cannot be null")); | ||
| 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() { | ||
| 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); | ||
| 174 | - } | ||
| 175 | - | ||
| 176 | - // Parses the given JSON and provides devices. | ||
| 177 | - private void parseDevices() { | ||
| 178 | - try { | ||
| 179 | - JsonNode nodes = cfg.get("devices"); | ||
| 180 | - if (nodes != null) { | ||
| 181 | - prepareForDeviceEvents(nodes.size()); | ||
| 182 | - for (JsonNode node : nodes) { | ||
| 183 | - parseDevice(node); | ||
| 184 | - | ||
| 185 | - // FIXME: hack to make sure device attributes take | ||
| 186 | - // This will be fixed when GossipDeviceStore uses ECM | ||
| 187 | - parseDevice(node); | ||
| 188 | - } | ||
| 189 | - } | ||
| 190 | - } finally { | ||
| 191 | - waitForDeviceEvents(); | ||
| 192 | - } | ||
| 193 | - } | ||
| 194 | - | ||
| 195 | - // Parses the given node with device data and supplies the device. | ||
| 196 | - private void parseDevice(JsonNode node) { | ||
| 197 | - URI uri = URI.create(get(node, "uri")); | ||
| 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")); | ||
| 204 | - SparseAnnotations annotations = annotations(node.get("annotations")); | ||
| 205 | - | ||
| 206 | - DeviceDescription desc = | ||
| 207 | - new DefaultDeviceDescription(uri, type, mfr, hw, sw, serial, | ||
| 208 | - cid, annotations); | ||
| 209 | - DeviceId deviceId = deviceId(uri); | ||
| 210 | - deviceProviderService.deviceConnected(deviceId, desc); | ||
| 211 | - | ||
| 212 | - JsonNode ports = node.get("ports"); | ||
| 213 | - if (ports != null) { | ||
| 214 | - parsePorts(deviceId, ports); | ||
| 215 | - } | ||
| 216 | - } | ||
| 217 | - | ||
| 218 | - // Parses the given node with list of device ports. | ||
| 219 | - private void parsePorts(DeviceId deviceId, JsonNode nodes) { | ||
| 220 | - List<PortDescription> ports = new ArrayList<>(); | ||
| 221 | - for (JsonNode node : nodes) { | ||
| 222 | - ports.add(parsePort(deviceId, node)); | ||
| 223 | - } | ||
| 224 | - deviceProviderService.updatePorts(deviceId, ports); | ||
| 225 | - } | ||
| 226 | - | ||
| 227 | - // Parses the given node with port information. | ||
| 228 | - private PortDescription parsePort(DeviceId deviceId, JsonNode node) { | ||
| 229 | - Port.Type type = Port.Type.valueOf(node.path("type").asText("COPPER")); | ||
| 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 | - | ||
| 243 | - if (port == null) { | ||
| 244 | - log.error("Cannot find port given in node {}", node); | ||
| 245 | - return null; | ||
| 246 | - } | ||
| 247 | - | ||
| 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 | - } | ||
| 254 | - switch (type) { | ||
| 255 | - case COPPER: | ||
| 256 | - return new DefaultPortDescription(port, node.path("enabled").asBoolean(true), | ||
| 257 | - type, node.path("speed").asLong(1_000), | ||
| 258 | - annotations); | ||
| 259 | - case FIBER: | ||
| 260 | - // Currently, assume OMS when FIBER. Provide sane defaults. | ||
| 261 | - annotations = annotations(node.get("annotations")); | ||
| 262 | - return omsPortDescription(port, node.path("enabled").asBoolean(true), | ||
| 263 | - Spectrum.CENTER_FREQUENCY, Spectrum.CENTER_FREQUENCY.add(TOTAL), | ||
| 264 | - Frequency.ofGHz(100), annotations); | ||
| 265 | - case ODUCLT: | ||
| 266 | - annotations = annotations(node.get("annotations")); | ||
| 267 | - OduCltPort oduCltPort = (OduCltPort) deviceService.getPort(deviceId, port); | ||
| 268 | - return oduCltPortDescription(port, node.path("enabled").asBoolean(true), | ||
| 269 | - oduCltPort.signalType(), annotations); | ||
| 270 | - case OCH: | ||
| 271 | - annotations = annotations(node.get("annotations")); | ||
| 272 | - OchPort ochPort = (OchPort) deviceService.getPort(deviceId, port); | ||
| 273 | - return ochPortDescription(port, node.path("enabled").asBoolean(true), | ||
| 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); | ||
| 279 | - return omsPortDescription(port, node.path("enabled").asBoolean(true), | ||
| 280 | - omsPort.minFrequency(), omsPort.maxFrequency(), omsPort.grid(), annotations); | ||
| 281 | - default: | ||
| 282 | - log.warn("{}: Unsupported Port Type"); | ||
| 283 | - } | ||
| 284 | - return new DefaultPortDescription(port, node.path("enabled").asBoolean(true), | ||
| 285 | - type, node.path("speed").asLong(1_000), | ||
| 286 | - annotations); | ||
| 287 | - } | ||
| 288 | - | ||
| 289 | - // Parses the given JSON and provides links as configured. | ||
| 290 | - private void parseLinks() { | ||
| 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); | ||
| 297 | - } | ||
| 298 | - } | ||
| 299 | - } | ||
| 300 | - } | ||
| 301 | - | ||
| 302 | - // Parses the given node with link data and supplies the link. | ||
| 303 | - private void parseLink(JsonNode node, boolean reverse) { | ||
| 304 | - ConnectPoint src = connectPoint(get(node, "src")); | ||
| 305 | - ConnectPoint dst = connectPoint(get(node, "dst")); | ||
| 306 | - Link.Type type = Link.Type.valueOf(get(node, "type", "DIRECT")); | ||
| 307 | - SparseAnnotations annotations = annotations(node.get("annotations")); | ||
| 308 | - // take annotations to update optical ports with correct attributes. | ||
| 309 | - updatePorts(src, dst, annotations); | ||
| 310 | - DefaultLinkDescription desc = reverse ? | ||
| 311 | - new DefaultLinkDescription(dst, src, type, annotations) : | ||
| 312 | - new DefaultLinkDescription(src, dst, type, annotations); | ||
| 313 | - linkProviderService.linkDetected(desc); | ||
| 314 | - | ||
| 315 | - connectPoints.add(src); | ||
| 316 | - connectPoints.add(dst); | ||
| 317 | - } | ||
| 318 | - | ||
| 319 | - private void updatePorts(ConnectPoint src, ConnectPoint dst, SparseAnnotations annotations) { | ||
| 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); | ||
| 325 | - updateOchPort(bw, src, dst); | ||
| 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); | ||
| 334 | - updateOmsPorts(numChls, src, dst); | ||
| 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. | ||
| 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). | ||
| 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) { | ||
| 367 | - PortDescription portDesc = ochPortDescription(srcCp.port(), true, | ||
| 368 | - OduSignalType.ODU4, true, signal); | ||
| 369 | - descriptions.put(srcCp, portDesc); | ||
| 370 | - deviceProviderService.portStatusChanged(srcCp.deviceId(), portDesc); | ||
| 371 | - } | ||
| 372 | - if (dst.type() == Device.Type.ROADM) { | ||
| 373 | - PortDescription portDesc = ochPortDescription(dstCp.port(), true, | ||
| 374 | - OduSignalType.ODU4, true, signal); | ||
| 375 | - descriptions.put(dstCp, portDesc); | ||
| 376 | - deviceProviderService.portStatusChanged(dstCp.deviceId(), portDesc); | ||
| 377 | - } | ||
| 378 | - } | ||
| 379 | - | ||
| 380 | - private void updateOmsPorts(int numChls, ConnectPoint srcCp, ConnectPoint dstCp) { | ||
| 381 | - // round down to largest slot that allows numChl channels to fit into C band range | ||
| 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. | ||
| 396 | - Frequency grid = chl.frequency(); | ||
| 397 | - // say Linc's 1st slot starts at CENTER and goes up from there. | ||
| 398 | - Frequency min = Spectrum.CENTER_FREQUENCY.add(grid); | ||
| 399 | - Frequency max = Spectrum.CENTER_FREQUENCY.add(grid.multiply(numChls)); | ||
| 400 | - | ||
| 401 | - PortDescription srcPortDesc = omsPortDescription(srcCp.port(), true, min, max, grid); | ||
| 402 | - PortDescription dstPortDesc = omsPortDescription(dstCp.port(), true, min, max, grid); | ||
| 403 | - descriptions.put(srcCp, srcPortDesc); | ||
| 404 | - descriptions.put(dstCp, dstPortDesc); | ||
| 405 | - deviceProviderService.portStatusChanged(srcCp.deviceId(), srcPortDesc); | ||
| 406 | - deviceProviderService.portStatusChanged(dstCp.deviceId(), dstPortDesc); | ||
| 407 | - } | ||
| 408 | - | ||
| 409 | - // Parses the given JSON and provides hosts as configured. | ||
| 410 | - private void parseHosts() { | ||
| 411 | - try { | ||
| 412 | - JsonNode nodes = cfg.get("hosts"); | ||
| 413 | - if (nodes != null) { | ||
| 414 | - for (JsonNode node : nodes) { | ||
| 415 | - parseHost(node); | ||
| 416 | - | ||
| 417 | - // FIXME: hack to make sure host attributes take | ||
| 418 | - // This will be fixed when GossipHostStore uses ECM | ||
| 419 | - parseHost(node); | ||
| 420 | - } | ||
| 421 | - } | ||
| 422 | - } finally { | ||
| 423 | - hostProviderRegistry.unregister(this); | ||
| 424 | - } | ||
| 425 | - } | ||
| 426 | - | ||
| 427 | - // Parses the given node with host data and supplies the host. | ||
| 428 | - private void parseHost(JsonNode node) { | ||
| 429 | - MacAddress mac = MacAddress.valueOf(get(node, "mac")); | ||
| 430 | - VlanId vlanId = VlanId.vlanId((short) node.get("vlan").asInt(VlanId.UNTAGGED)); | ||
| 431 | - HostId hostId = HostId.hostId(mac, vlanId); | ||
| 432 | - SparseAnnotations annotations = annotations(node.get("annotations")); | ||
| 433 | - HostLocation location = new HostLocation(connectPoint(get(node, "location")), 0); | ||
| 434 | - | ||
| 435 | - String[] ipStrings = get(node, "ip", "").split(","); | ||
| 436 | - Set<IpAddress> ips = new HashSet<>(); | ||
| 437 | - for (String ip : ipStrings) { | ||
| 438 | - ips.add(IpAddress.valueOf(ip.trim())); | ||
| 439 | - } | ||
| 440 | - | ||
| 441 | - DefaultHostDescription desc = | ||
| 442 | - new DefaultHostDescription(mac, vlanId, location, ips, annotations); | ||
| 443 | - hostProviderService.hostDetected(hostId, desc, false); | ||
| 444 | - | ||
| 445 | - connectPoints.add(location); | ||
| 446 | - } | ||
| 447 | - | ||
| 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) { | ||
| 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()); | ||
| 464 | - | ||
| 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 | - } | ||
| 475 | - } | ||
| 476 | - | ||
| 477 | - // Creates a port description from the specified port. | ||
| 478 | - private PortDescription description(Port p) { | ||
| 479 | - switch (p.type()) { | ||
| 480 | - case OMS: | ||
| 481 | - OmsPort op = (OmsPort) p; | ||
| 482 | - return omsPortDescription( | ||
| 483 | - op.number(), op.isEnabled(), op.minFrequency(), op.maxFrequency(), op.grid()); | ||
| 484 | - case OCH: | ||
| 485 | - OchPort ochp = (OchPort) p; | ||
| 486 | - return ochPortDescription( | ||
| 487 | - ochp.number(), ochp.isEnabled(), ochp.signalType(), ochp.isTunable(), ochp.lambda()); | ||
| 488 | - case ODUCLT: | ||
| 489 | - OduCltPort odup = (OduCltPort) p; | ||
| 490 | - return oduCltPortDescription( | ||
| 491 | - odup.number(), odup.isEnabled(), odup.signalType()); | ||
| 492 | - default: | ||
| 493 | - return new DefaultPortDescription(p.number(), p.isEnabled(), p.type(), p.portSpeed()); | ||
| 494 | - } | ||
| 495 | - } | ||
| 496 | - | ||
| 497 | - // Creates a port description from the specified connection point if none created earlier. | ||
| 498 | - private PortDescription description(ConnectPoint cp) { | ||
| 499 | - PortDescription saved = descriptions.get(cp); | ||
| 500 | - if (saved != null) { | ||
| 501 | - return saved; | ||
| 502 | - } | ||
| 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); | ||
| 508 | - } | ||
| 509 | - | ||
| 510 | - // Produces set of annotations from the given JSON node. | ||
| 511 | - private SparseAnnotations annotations(JsonNode node) { | ||
| 512 | - if (node == null) { | ||
| 513 | - return DefaultAnnotations.EMPTY; | ||
| 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("/"); | ||
| 528 | - String portName = text.substring(i + 1); | ||
| 529 | - DeviceId deviceId = deviceId(text.substring(0, i)); | ||
| 530 | - | ||
| 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 | - } | ||
| 537 | - | ||
| 538 | - long portNum; | ||
| 539 | - try { | ||
| 540 | - portNum = Long.parseLong(portName); | ||
| 541 | - } catch (NumberFormatException e) { | ||
| 542 | - portNum = 0; | ||
| 543 | - } | ||
| 544 | - | ||
| 545 | - return new ConnectPoint(deviceId, portNumber(portNum, portName)); | ||
| 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 | - | ||
| 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); | ||
| 556 | - } | ||
| 557 | - | ||
| 558 | - @Override | ||
| 559 | - public void roleChanged(DeviceId device, MastershipRole newRole) { | ||
| 560 | - deviceProviderService.receivedRoleReply(device, newRole, newRole); | ||
| 561 | - } | ||
| 562 | - | ||
| 563 | - @Override | ||
| 564 | - public void triggerProbe(DeviceId deviceId) { | ||
| 565 | - } | ||
| 566 | - | ||
| 567 | - @Override | ||
| 568 | - public void triggerProbe(Host host) { | ||
| 569 | - } | ||
| 570 | - | ||
| 571 | - @Override | ||
| 572 | - public void changePortState(DeviceId deviceId, PortNumber portNumber, | ||
| 573 | - boolean enable) { | ||
| 574 | - } | ||
| 575 | - | ||
| 576 | - @Override | ||
| 577 | - public ProviderId id() { | ||
| 578 | - return PID; | ||
| 579 | - } | ||
| 580 | - | ||
| 581 | - @Override | ||
| 582 | - public boolean isReachable(DeviceId device) { | ||
| 583 | - return true; | ||
| 584 | - } | ||
| 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 | - | ||
| 619 | - | ||
| 620 | - | ||
| 621 | -} |
| 1 | -/* | ||
| 2 | - * Copyright 2015-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 | -package org.onosproject.rest.resources; | ||
| 17 | - | ||
| 18 | -import com.fasterxml.jackson.databind.JsonNode; | ||
| 19 | -import com.fasterxml.jackson.databind.ObjectMapper; | ||
| 20 | -import org.onlab.rest.BaseResource; | ||
| 21 | -import org.onosproject.net.device.DeviceProviderRegistry; | ||
| 22 | -import org.onosproject.net.device.DeviceService; | ||
| 23 | -import org.onosproject.net.host.HostProviderRegistry; | ||
| 24 | -import org.onosproject.net.link.LinkProviderRegistry; | ||
| 25 | -import org.slf4j.Logger; | ||
| 26 | -import org.slf4j.LoggerFactory; | ||
| 27 | - | ||
| 28 | -import javax.ws.rs.Consumes; | ||
| 29 | -import javax.ws.rs.POST; | ||
| 30 | -import javax.ws.rs.Path; | ||
| 31 | -import javax.ws.rs.core.MediaType; | ||
| 32 | -import javax.ws.rs.core.Response; | ||
| 33 | -import java.io.InputStream; | ||
| 34 | - | ||
| 35 | -import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; | ||
| 36 | - | ||
| 37 | -/** | ||
| 38 | - * Inject devices, ports, links and end-station hosts. | ||
| 39 | - */ | ||
| 40 | -@Path("config") | ||
| 41 | -public class ConfigWebResource extends BaseResource { | ||
| 42 | - | ||
| 43 | - private static Logger log = LoggerFactory.getLogger(ConfigWebResource.class); | ||
| 44 | - | ||
| 45 | - /** | ||
| 46 | - * Upload device, port, link and host data. | ||
| 47 | - * | ||
| 48 | - * @param input JSON blob | ||
| 49 | - * @return 200 OK | ||
| 50 | - */ | ||
| 51 | - @POST | ||
| 52 | - @Path("topology") | ||
| 53 | - @Consumes(MediaType.APPLICATION_JSON) | ||
| 54 | - public Response topology(InputStream input) { | ||
| 55 | - try { | ||
| 56 | - ObjectMapper mapper = new ObjectMapper(); | ||
| 57 | - JsonNode cfg = mapper.readTree(input); | ||
| 58 | - new ConfigProvider(cfg, get(DeviceService.class), | ||
| 59 | - get(DeviceProviderRegistry.class), | ||
| 60 | - get(LinkProviderRegistry.class), | ||
| 61 | - get(HostProviderRegistry.class)).parse(); | ||
| 62 | - return Response.ok().build(); | ||
| 63 | - } catch (Exception e) { | ||
| 64 | - log.error("Unable to parse topology configuration", e); | ||
| 65 | - return Response.status(INTERNAL_SERVER_ERROR).entity(e.toString()).build(); | ||
| 66 | - } | ||
| 67 | - } | ||
| 68 | - | ||
| 69 | -} |
| ... | @@ -40,7 +40,6 @@ public class CoreWebApplication extends AbstractWebApplication { | ... | @@ -40,7 +40,6 @@ public class CoreWebApplication extends AbstractWebApplication { |
| 40 | GroupsWebResource.class, | 40 | GroupsWebResource.class, |
| 41 | MetersWebResource.class, | 41 | MetersWebResource.class, |
| 42 | TopologyWebResource.class, | 42 | TopologyWebResource.class, |
| 43 | - ConfigWebResource.class, | ||
| 44 | PathsWebResource.class, | 43 | PathsWebResource.class, |
| 45 | StatisticsWebResource.class, | 44 | StatisticsWebResource.class, |
| 46 | MetricsWebResource.class, | 45 | MetricsWebResource.class, | ... | ... |
-
Please register or login to post a comment