Diego Garcia | aab9947 | 2019-01-10 13:53:31 +0100 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 18 | package org.onosproject.drivers.odtn.tapi; |
| 19 | |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import org.onosproject.net.Port; |
| 22 | import org.onosproject.net.Device; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.ChannelSpacing; |
| 25 | import org.onosproject.net.GridType; |
| 26 | import org.onosproject.net.OchSignal; |
| 27 | import org.onosproject.net.PortNumber; |
| 28 | import org.onosproject.net.behaviour.LambdaQuery; |
| 29 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 30 | import org.onosproject.protocol.rest.RestSBController; |
| 31 | |
| 32 | import com.fasterxml.jackson.databind.JsonNode; |
| 33 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 34 | import org.slf4j.Logger; |
| 35 | |
| 36 | import java.io.IOException; |
| 37 | import java.io.InputStream; |
| 38 | import java.util.Iterator; |
| 39 | import java.util.LinkedHashSet; |
| 40 | import java.util.Set; |
| 41 | |
| 42 | import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.MEDIA_CHANNEL_SERVICE_INTERFACE_POINT_SPEC; |
| 43 | import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.AVAILABLE_SPECTRUM; |
| 44 | import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.ADJUSTMENT_GRANULARITY; |
| 45 | import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.BASE_FREQUENCY; |
| 46 | import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.FREQUENCY_CONSTRAINT; |
| 47 | import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.GRID_TYPE; |
| 48 | import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.LOWER_FREQUENCY; |
| 49 | import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.MC_POOL; |
| 50 | import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.UPPER_FREQUENCY; |
| 51 | import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.toMbpsFromHz; |
| 52 | import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.getChannelSpacing; |
| 53 | import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.getSlotGranularity; |
| 54 | |
| 55 | import static org.slf4j.LoggerFactory.getLogger; |
| 56 | import static com.google.common.base.Preconditions.checkNotNull; |
| 57 | import org.onosproject.net.device.DeviceService; |
| 58 | import 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 | */ |
| 65 | public 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 Milkey | 0a9e0aa9 | 2019-01-23 14:37:46 -0800 | [diff] [blame^] | 88 | return ImmutableSet.of(); |
Diego Garcia | aab9947 | 2019-01-10 13:53:31 +0100 | [diff] [blame] | 89 | } |
| 90 | Port p = deviceService.getPort(dev.id(), port); |
| 91 | if (p == null) { |
| 92 | log.error("Port {} does not exist", port); |
Ray Milkey | 0a9e0aa9 | 2019-01-23 14:37:46 -0800 | [diff] [blame^] | 93 | return ImmutableSet.of(); |
Diego Garcia | aab9947 | 2019-01-10 13:53:31 +0100 | [diff] [blame] | 94 | } |
| 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 | } |