blob: 7076583cd1dbbbafe716500fa35226482df1127f [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
Diego Garciaaab99472019-01-10 13:53:31 +010020import com.fasterxml.jackson.databind.JsonNode;
21import com.fasterxml.jackson.databind.ObjectMapper;
Andrea Campanellab9e491b2019-02-18 17:45:01 +010022import com.google.common.collect.ImmutableSet;
23import org.onosproject.net.Device;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.OchSignal;
26import org.onosproject.net.Port;
27import org.onosproject.net.PortNumber;
28import org.onosproject.net.behaviour.LambdaQuery;
29import org.onosproject.net.device.DeviceService;
30import org.onosproject.net.driver.AbstractHandlerBehaviour;
31import org.onosproject.protocol.rest.RestSBController;
Diego Garciaaab99472019-01-10 13:53:31 +010032import org.slf4j.Logger;
33
Andrea Campanellab9e491b2019-02-18 17:45:01 +010034import javax.ws.rs.core.MediaType;
Diego Garciaaab99472019-01-10 13:53:31 +010035import java.io.IOException;
36import java.io.InputStream;
Diego Garciaaab99472019-01-10 13:53:31 +010037import java.util.Set;
38
Diego Garciaaab99472019-01-10 13:53:31 +010039import static com.google.common.base.Preconditions.checkNotNull;
Andrea Campanellab9e491b2019-02-18 17:45:01 +010040import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.MC_POOL;
41import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.MEDIA_CHANNEL_SERVICE_INTERFACE_POINT_SPEC;
Andrea Campanella3d2ba462019-08-22 16:29:21 +020042import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.SERVICE_INTERFACE_POINT;
43import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.TAPI_COMMON;
Andrea Campanellab9e491b2019-02-18 17:45:01 +010044import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.UUID;
45import static org.slf4j.LoggerFactory.getLogger;
Diego Garciaaab99472019-01-10 13:53:31 +010046
47
48
49/**
50 * Driver behavior of TAPI devices to discover the map of available lambdas of the ports.
51 */
52public class TapiDeviceLambdaQuery extends AbstractHandlerBehaviour
53 implements LambdaQuery {
54
55 private static final Logger log = getLogger(TapiDeviceLambdaQuery.class);
Andrea Campanellab9e491b2019-02-18 17:45:01 +010056 private static final String SIP_REQUEST_DATA_API = "/restconf/data/tapi-common:context/service-interface-point=";
Diego Garciaaab99472019-01-10 13:53:31 +010057
58 /**
59 * Get the deviceId for which the methods apply.
60 *
61 * @return The deviceId as contained in the handler data
62 */
63 private DeviceId did() {
64 return handler().data().deviceId();
65 }
66
67 @Override
68 public Set<OchSignal> queryLambdas(PortNumber port) {
69 RestSBController controller = checkNotNull(handler().get(RestSBController.class));
70 DeviceService deviceService = checkNotNull(handler().get(DeviceService.class));
71 DeviceId deviceId = did();
72 Device dev = deviceService.getDevice(deviceId);
73 if (dev == null) {
74 log.error("Device {} does not exist", deviceId);
Ray Milkey0a9e0aa92019-01-23 14:37:46 -080075 return ImmutableSet.of();
Diego Garciaaab99472019-01-10 13:53:31 +010076 }
77 Port p = deviceService.getPort(dev.id(), port);
78 if (p == null) {
79 log.error("Port {} does not exist", port);
Ray Milkey0a9e0aa92019-01-23 14:37:46 -080080 return ImmutableSet.of();
Diego Garciaaab99472019-01-10 13:53:31 +010081 }
Andrea Campanella2bdf2042019-01-28 13:47:11 +010082 String uuid = p.annotations().value(UUID);
Diego Garciaaab99472019-01-10 13:53:31 +010083
84 try {
85 InputStream inputStream = controller.get(deviceId, SIP_REQUEST_DATA_API + uuid,
86 MediaType.APPLICATION_JSON_TYPE);
87 log.debug("Service interface point UUID: {}", uuid);
88 JsonNode sipAttributes = new ObjectMapper().readTree(inputStream);
Andrea Campanella3d2ba462019-08-22 16:29:21 +020089 JsonNode mcPool = sipAttributes.get(TAPI_COMMON + ":" + SERVICE_INTERFACE_POINT).get(0)
90 .get(MEDIA_CHANNEL_SERVICE_INTERFACE_POINT_SPEC).get(MC_POOL);
Diego Garciaaab99472019-01-10 13:53:31 +010091
92 //This creates a hashset of OChSignals representing the spectrum availability at the target port.
Andrea Campanellab9e491b2019-02-18 17:45:01 +010093 return TapiDeviceHelper.getOchSignal(mcPool);
Diego Garciaaab99472019-01-10 13:53:31 +010094
95 } catch (IOException e) {
96 log.error("Exception discoverPortDetails() {}", did(), e);
97 return ImmutableSet.of();
98 }
99 }
100
Diego Garciaaab99472019-01-10 13:53:31 +0100101
102}