blob: 0127f43ab8b75bf5758686747488e3f8f54c6cab [file] [log] [blame]
Diego Garciaaab99472019-01-10 13:53:31 +01001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * This work was partially supported by EC H2020 project METRO-HAUL (761727).
17 */
18package org.onosproject.drivers.odtn.tapi;
19
20import com.google.common.collect.ImmutableSet;
21import org.onosproject.net.Port;
22import org.onosproject.net.Device;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.ChannelSpacing;
25import org.onosproject.net.GridType;
26import org.onosproject.net.OchSignal;
27import org.onosproject.net.PortNumber;
28import org.onosproject.net.behaviour.LambdaQuery;
29import org.onosproject.net.driver.AbstractHandlerBehaviour;
30import org.onosproject.protocol.rest.RestSBController;
31
32import com.fasterxml.jackson.databind.JsonNode;
33import com.fasterxml.jackson.databind.ObjectMapper;
34import org.slf4j.Logger;
35
36import java.io.IOException;
37import java.io.InputStream;
38import java.util.Iterator;
39import java.util.LinkedHashSet;
40import java.util.Set;
41
42import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.MEDIA_CHANNEL_SERVICE_INTERFACE_POINT_SPEC;
43import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.AVAILABLE_SPECTRUM;
44import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.ADJUSTMENT_GRANULARITY;
45import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.BASE_FREQUENCY;
46import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.FREQUENCY_CONSTRAINT;
47import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.GRID_TYPE;
48import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.LOWER_FREQUENCY;
49import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.MC_POOL;
50import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.UPPER_FREQUENCY;
51import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.toMbpsFromHz;
52import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.getChannelSpacing;
53import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.getSlotGranularity;
54
55import static org.slf4j.LoggerFactory.getLogger;
56import static com.google.common.base.Preconditions.checkNotNull;
57import org.onosproject.net.device.DeviceService;
58import javax.ws.rs.core.MediaType;
59
60
61
62/**
63 * Driver behavior of TAPI devices to discover the map of available lambdas of the ports.
64 */
65public class TapiDeviceLambdaQuery extends AbstractHandlerBehaviour
66 implements LambdaQuery {
67
68 private static final Logger log = getLogger(TapiDeviceLambdaQuery.class);
69 private static final String SIP_REQUEST_DATA_API = "/data/context/service-interface-point=";
70
71 /**
72 * Get the deviceId for which the methods apply.
73 *
74 * @return The deviceId as contained in the handler data
75 */
76 private DeviceId did() {
77 return handler().data().deviceId();
78 }
79
80 @Override
81 public Set<OchSignal> queryLambdas(PortNumber port) {
82 RestSBController controller = checkNotNull(handler().get(RestSBController.class));
83 DeviceService deviceService = checkNotNull(handler().get(DeviceService.class));
84 DeviceId deviceId = did();
85 Device dev = deviceService.getDevice(deviceId);
86 if (dev == null) {
87 log.error("Device {} does not exist", deviceId);
Ray Milkey0a9e0aa92019-01-23 14:37:46 -080088 return ImmutableSet.of();
Diego Garciaaab99472019-01-10 13:53:31 +010089 }
90 Port p = deviceService.getPort(dev.id(), port);
91 if (p == null) {
92 log.error("Port {} does not exist", port);
Ray Milkey0a9e0aa92019-01-23 14:37:46 -080093 return ImmutableSet.of();
Diego Garciaaab99472019-01-10 13:53:31 +010094 }
95 String uuid = p.annotations().value(port.toString());
96
97 try {
98 InputStream inputStream = controller.get(deviceId, SIP_REQUEST_DATA_API + uuid,
99 MediaType.APPLICATION_JSON_TYPE);
100 log.debug("Service interface point UUID: {}", uuid);
101 JsonNode sipAttributes = new ObjectMapper().readTree(inputStream);
102 JsonNode mcPool = sipAttributes.get(MEDIA_CHANNEL_SERVICE_INTERFACE_POINT_SPEC).get(MC_POOL);
103
104 //This creates a hashset of OChSignals representing the spectrum availability at the target port.
105 Set<OchSignal> lambdas = getOchSignal(mcPool);
106 log.debug("Lambdas: {}", lambdas.toString());
107 return lambdas;
108
109 } catch (IOException e) {
110 log.error("Exception discoverPortDetails() {}", did(), e);
111 return ImmutableSet.of();
112 }
113 }
114
115 /**
116 * If SIP info match our criteria, SIP component shall includes mc-pool information which must be obtained in order
117 * to complete the OchSignal info. with the TAPI SIP information included in spectrum-supported, spectrum-available
118 * and spectrum-occupied tapi objects.
119 **/
120 private Set<OchSignal> getOchSignal(JsonNode mcPool) {
121
122 Set<OchSignal> lambdas = new LinkedHashSet<>();
123 long availableUpperFrec = 0;
124 long availableLowerFrec = 0;
125 String availableAdjustmentGranularity = "";
126 String availableGridType = "";
127 JsonNode availableSpectrum = mcPool.get(AVAILABLE_SPECTRUM);
128
129 /**At this time only the latest availableSpectrum is used**/
130 Iterator<JsonNode> iterAvailable = availableSpectrum.iterator();
131 while (iterAvailable.hasNext()) {
132 JsonNode availableSpec = iterAvailable.next();
133 availableUpperFrec = availableSpec.get(UPPER_FREQUENCY).asLong();
134 availableLowerFrec = availableSpec.get(LOWER_FREQUENCY).asLong();
135 availableAdjustmentGranularity = availableSpec.get(FREQUENCY_CONSTRAINT)
136 .get(ADJUSTMENT_GRANULARITY).textValue();
137 availableGridType = availableSpec.get(FREQUENCY_CONSTRAINT).get(GRID_TYPE).textValue();
138
139 int spacingMult = 0;
140 int slotGranularity = 1;
141 ChannelSpacing chSpacing = getChannelSpacing(availableAdjustmentGranularity);
142 long spacingFrequency = chSpacing.frequency().asHz();
143 long centralFrequency = (availableUpperFrec - (availableUpperFrec - availableLowerFrec) / 2);
144
145 GridType gridType = GridType.valueOf(availableGridType);
146 if (gridType == GridType.DWDM) {
147 spacingMult = (int) ((centralFrequency - BASE_FREQUENCY) / toMbpsFromHz(spacingFrequency));
148 OchSignal ochSignal = new OchSignal(gridType, chSpacing, spacingMult, slotGranularity);
149 lambdas.add(ochSignal);
150 } else if (gridType == GridType.CWDM) {
151 log.warn("GridType CWDM. Not implemented");
152 } else if (gridType == GridType.FLEX) {
153 log.warn("GridType FLEX. Not implemented");
154 slotGranularity = getSlotGranularity(chSpacing);
155 } else {
156 log.warn("Unknown GridType");
157 }
158 }
159 return lambdas;
160 }
161
162}