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 | |
Diego Garcia | aab9947 | 2019-01-10 13:53:31 +0100 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.JsonNode; |
| 21 | import com.fasterxml.jackson.databind.ObjectMapper; |
Andrea Campanella | b9e491b | 2019-02-18 17:45:01 +0100 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableSet; |
| 23 | import org.onosproject.net.Device; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.OchSignal; |
| 26 | import org.onosproject.net.Port; |
| 27 | import org.onosproject.net.PortNumber; |
| 28 | import org.onosproject.net.behaviour.LambdaQuery; |
| 29 | import org.onosproject.net.device.DeviceService; |
| 30 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 31 | import org.onosproject.protocol.rest.RestSBController; |
Diego Garcia | aab9947 | 2019-01-10 13:53:31 +0100 | [diff] [blame] | 32 | import org.slf4j.Logger; |
| 33 | |
Andrea Campanella | b9e491b | 2019-02-18 17:45:01 +0100 | [diff] [blame] | 34 | import javax.ws.rs.core.MediaType; |
Diego Garcia | aab9947 | 2019-01-10 13:53:31 +0100 | [diff] [blame] | 35 | import java.io.IOException; |
| 36 | import java.io.InputStream; |
Diego Garcia | aab9947 | 2019-01-10 13:53:31 +0100 | [diff] [blame] | 37 | import java.util.Set; |
| 38 | |
Diego Garcia | aab9947 | 2019-01-10 13:53:31 +0100 | [diff] [blame] | 39 | import static com.google.common.base.Preconditions.checkNotNull; |
Andrea Campanella | b9e491b | 2019-02-18 17:45:01 +0100 | [diff] [blame] | 40 | import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.MC_POOL; |
| 41 | import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.MEDIA_CHANNEL_SERVICE_INTERFACE_POINT_SPEC; |
| 42 | import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.UUID; |
| 43 | import static org.slf4j.LoggerFactory.getLogger; |
Diego Garcia | aab9947 | 2019-01-10 13:53:31 +0100 | [diff] [blame] | 44 | |
| 45 | |
| 46 | |
| 47 | /** |
| 48 | * Driver behavior of TAPI devices to discover the map of available lambdas of the ports. |
| 49 | */ |
| 50 | public class TapiDeviceLambdaQuery extends AbstractHandlerBehaviour |
| 51 | implements LambdaQuery { |
| 52 | |
| 53 | private static final Logger log = getLogger(TapiDeviceLambdaQuery.class); |
Andrea Campanella | b9e491b | 2019-02-18 17:45:01 +0100 | [diff] [blame] | 54 | private static final String SIP_REQUEST_DATA_API = "/restconf/data/tapi-common:context/service-interface-point="; |
Diego Garcia | aab9947 | 2019-01-10 13:53:31 +0100 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * Get the deviceId for which the methods apply. |
| 58 | * |
| 59 | * @return The deviceId as contained in the handler data |
| 60 | */ |
| 61 | private DeviceId did() { |
| 62 | return handler().data().deviceId(); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public Set<OchSignal> queryLambdas(PortNumber port) { |
| 67 | RestSBController controller = checkNotNull(handler().get(RestSBController.class)); |
| 68 | DeviceService deviceService = checkNotNull(handler().get(DeviceService.class)); |
| 69 | DeviceId deviceId = did(); |
| 70 | Device dev = deviceService.getDevice(deviceId); |
| 71 | if (dev == null) { |
| 72 | log.error("Device {} does not exist", deviceId); |
Ray Milkey | 0a9e0aa9 | 2019-01-23 14:37:46 -0800 | [diff] [blame] | 73 | return ImmutableSet.of(); |
Diego Garcia | aab9947 | 2019-01-10 13:53:31 +0100 | [diff] [blame] | 74 | } |
| 75 | Port p = deviceService.getPort(dev.id(), port); |
| 76 | if (p == null) { |
| 77 | log.error("Port {} does not exist", port); |
Ray Milkey | 0a9e0aa9 | 2019-01-23 14:37:46 -0800 | [diff] [blame] | 78 | return ImmutableSet.of(); |
Diego Garcia | aab9947 | 2019-01-10 13:53:31 +0100 | [diff] [blame] | 79 | } |
Andrea Campanella | 2bdf204 | 2019-01-28 13:47:11 +0100 | [diff] [blame] | 80 | String uuid = p.annotations().value(UUID); |
Diego Garcia | aab9947 | 2019-01-10 13:53:31 +0100 | [diff] [blame] | 81 | |
| 82 | try { |
| 83 | InputStream inputStream = controller.get(deviceId, SIP_REQUEST_DATA_API + uuid, |
| 84 | MediaType.APPLICATION_JSON_TYPE); |
| 85 | log.debug("Service interface point UUID: {}", uuid); |
| 86 | JsonNode sipAttributes = new ObjectMapper().readTree(inputStream); |
| 87 | JsonNode mcPool = sipAttributes.get(MEDIA_CHANNEL_SERVICE_INTERFACE_POINT_SPEC).get(MC_POOL); |
| 88 | |
| 89 | //This creates a hashset of OChSignals representing the spectrum availability at the target port. |
Andrea Campanella | b9e491b | 2019-02-18 17:45:01 +0100 | [diff] [blame] | 90 | return TapiDeviceHelper.getOchSignal(mcPool); |
Diego Garcia | aab9947 | 2019-01-10 13:53:31 +0100 | [diff] [blame] | 91 | |
| 92 | } catch (IOException e) { |
| 93 | log.error("Exception discoverPortDetails() {}", did(), e); |
| 94 | return ImmutableSet.of(); |
| 95 | } |
| 96 | } |
| 97 | |
Diego Garcia | aab9947 | 2019-01-10 13:53:31 +0100 | [diff] [blame] | 98 | |
| 99 | } |