Diego Garcia | 09ab5e2 | 2018-12-18 11:47:01 +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.fasterxml.jackson.databind.JsonNode; |
| 21 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 22 | import com.google.common.collect.ImmutableList; |
| 23 | import com.google.common.collect.Lists; |
| 24 | import org.onlab.packet.ChassisId; |
Andrea Campanella | b9e491b | 2019-02-18 17:45:01 +0100 | [diff] [blame] | 25 | import org.onosproject.net.DefaultAnnotations; |
Diego Garcia | 09ab5e2 | 2018-12-18 11:47:01 +0100 | [diff] [blame] | 26 | import org.onosproject.net.Device; |
Diego Garcia | 09ab5e2 | 2018-12-18 11:47:01 +0100 | [diff] [blame] | 27 | import org.onosproject.net.DeviceId; |
Andrea Campanella | b9e491b | 2019-02-18 17:45:01 +0100 | [diff] [blame] | 28 | import org.onosproject.net.OchSignal; |
| 29 | import org.onosproject.net.OduSignalType; |
| 30 | import org.onosproject.net.PortNumber; |
Diego Garcia | 09ab5e2 | 2018-12-18 11:47:01 +0100 | [diff] [blame] | 31 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 32 | import org.onosproject.net.device.DeviceDescription; |
| 33 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
| 34 | import org.onosproject.net.device.DeviceService; |
Andrea Campanella | b9e491b | 2019-02-18 17:45:01 +0100 | [diff] [blame] | 35 | import org.onosproject.net.device.PortDescription; |
Diego Garcia | 09ab5e2 | 2018-12-18 11:47:01 +0100 | [diff] [blame] | 36 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
Diego Garcia | 09ab5e2 | 2018-12-18 11:47:01 +0100 | [diff] [blame] | 37 | import org.onosproject.protocol.rest.RestSBController; |
| 38 | import org.slf4j.Logger; |
| 39 | |
| 40 | import javax.ws.rs.core.MediaType; |
| 41 | import java.io.IOException; |
| 42 | import java.io.InputStream; |
| 43 | import java.util.Iterator; |
| 44 | import java.util.List; |
| 45 | |
| 46 | import static com.google.common.base.Preconditions.checkNotNull; |
Andrea Campanella | 2bdf204 | 2019-01-28 13:47:11 +0100 | [diff] [blame] | 47 | import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.LAYER_PROTOCOL_NAME; |
Andrea Campanella | 2bdf204 | 2019-01-28 13:47:11 +0100 | [diff] [blame] | 48 | import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.MC_POOL; |
Andrea Campanella | b9e491b | 2019-02-18 17:45:01 +0100 | [diff] [blame] | 49 | import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.MEDIA_CHANNEL_SERVICE_INTERFACE_POINT_SPEC; |
Andrea Campanella | 2bdf204 | 2019-01-28 13:47:11 +0100 | [diff] [blame] | 50 | import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.PHOTONIC_LAYER_QUALIFIER_NMC; |
| 51 | import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.PHOTONIC_MEDIA; |
| 52 | import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.SERVICE_INTERFACE_POINT; |
| 53 | import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.SUPPORTED_LAYER_PROTOCOL_QUALIFIER; |
Andrea Campanella | 2bdf204 | 2019-01-28 13:47:11 +0100 | [diff] [blame] | 54 | import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.UUID; |
Andrea Campanella | b9e491b | 2019-02-18 17:45:01 +0100 | [diff] [blame] | 55 | import static org.onosproject.drivers.odtn.tapi.TapiDeviceHelper.getOchSignal; |
Diego Garcia | 09ab5e2 | 2018-12-18 11:47:01 +0100 | [diff] [blame] | 56 | import static org.onosproject.net.optical.device.OchPortHelper.ochPortDescription; |
| 57 | import static org.slf4j.LoggerFactory.getLogger; |
| 58 | |
| 59 | /** |
| 60 | * Driver Implementation of the DeviceDescrption discovery for ONF Transport-API (TAPI) v2.1 based |
| 61 | * open line systems (OLS). |
| 62 | */ |
| 63 | |
| 64 | public class TapiDeviceDescriptionDiscovery |
| 65 | extends AbstractHandlerBehaviour |
| 66 | implements DeviceDescriptionDiscovery { |
| 67 | |
| 68 | private static final Logger log = getLogger(TapiDeviceDescriptionDiscovery.class); |
Andrea Campanella | b9e491b | 2019-02-18 17:45:01 +0100 | [diff] [blame] | 69 | private static final String SIP_REQUEST_DATA_API = "/restconf/data/tapi-common:context"; |
Diego Garcia | 09ab5e2 | 2018-12-18 11:47:01 +0100 | [diff] [blame] | 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 DeviceDescription discoverDeviceDetails() { |
| 82 | log.debug("Getting device description"); |
| 83 | DeviceService deviceService = checkNotNull(handler().get(DeviceService.class)); |
| 84 | DeviceId deviceId = handler().data().deviceId(); |
| 85 | Device device = deviceService.getDevice(deviceId); |
| 86 | |
| 87 | if (device == null) { |
Andrea Campanella | b9e491b | 2019-02-18 17:45:01 +0100 | [diff] [blame] | 88 | //TODO need to obtain from the device. |
Diego Garcia | 09ab5e2 | 2018-12-18 11:47:01 +0100 | [diff] [blame] | 89 | return new DefaultDeviceDescription(deviceId.uri(), |
| 90 | Device.Type.OLS, |
Andrea Campanella | b9e491b | 2019-02-18 17:45:01 +0100 | [diff] [blame] | 91 | "Tapi", |
Diego Garcia | 09ab5e2 | 2018-12-18 11:47:01 +0100 | [diff] [blame] | 92 | "0", |
| 93 | "2.1", |
| 94 | "Unknown", |
| 95 | new ChassisId()); |
| 96 | } else { |
| 97 | return new DefaultDeviceDescription(device.id().uri(), |
| 98 | Device.Type.OLS, |
| 99 | device.manufacturer(), |
| 100 | device.hwVersion(), |
| 101 | device.swVersion(), |
| 102 | device.serialNumber(), |
| 103 | device.chassisId()); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public List<PortDescription> discoverPortDetails() { |
| 109 | log.debug("Discovering port details."); |
| 110 | RestSBController controller = checkNotNull(handler().get(RestSBController.class)); |
| 111 | DeviceId deviceId = handler().data().deviceId(); |
| 112 | |
| 113 | try { |
| 114 | InputStream inputStream = controller.get(deviceId, SIP_REQUEST_DATA_API, MediaType.APPLICATION_JSON_TYPE); |
| 115 | JsonNode jsonNode = new ObjectMapper().readTree(inputStream); |
Andrea Campanella | b9e491b | 2019-02-18 17:45:01 +0100 | [diff] [blame] | 116 | if (jsonNode == null) { |
| 117 | log.error("Can't discover port details at {}", SIP_REQUEST_DATA_API); |
| 118 | return ImmutableList.of(); |
| 119 | } |
Diego Garcia | 09ab5e2 | 2018-12-18 11:47:01 +0100 | [diff] [blame] | 120 | return parseTapiPorts(jsonNode); |
| 121 | } catch (IOException e) { |
| 122 | log.error("Exception discoverPortDetails() {}", did(), e); |
| 123 | return ImmutableList.of(); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | protected List<PortDescription> parseTapiPorts(JsonNode tapiContext) { |
| 128 | List<PortDescription> ports = Lists.newArrayList(); |
Andrea Campanella | b9e491b | 2019-02-18 17:45:01 +0100 | [diff] [blame] | 129 | /* |
Diego Garcia | 09ab5e2 | 2018-12-18 11:47:01 +0100 | [diff] [blame] | 130 | This annotations are used to store persistent mapping information between TAPI SIP's uuid |
| 131 | and ONOS device portNumbers. This is needed to be publicly available at least within ODTN app |
| 132 | when connectivity services will be sent to OLS Controller. |
Andrea Campanella | b9e491b | 2019-02-18 17:45:01 +0100 | [diff] [blame] | 133 | */ |
Diego Garcia | 09ab5e2 | 2018-12-18 11:47:01 +0100 | [diff] [blame] | 134 | DefaultAnnotations.Builder annotations = DefaultAnnotations.builder(); |
Boyuan Yan | 4cebe24 | 2019-05-28 16:59:37 -0700 | [diff] [blame] | 135 | Iterator<JsonNode> iter = tapiContext.get(SERVICE_INTERFACE_POINT).iterator(); |
Diego Garcia | 09ab5e2 | 2018-12-18 11:47:01 +0100 | [diff] [blame] | 136 | while (iter.hasNext()) { |
| 137 | JsonNode sipAttributes = iter.next(); |
| 138 | if (checkValidEndpoint(sipAttributes)) { |
| 139 | String uuid = sipAttributes.get(UUID).textValue(); |
Andrea Campanella | b9e491b | 2019-02-18 17:45:01 +0100 | [diff] [blame] | 140 | PortNumber portNumber = PortNumber.portNumber(uuid.split("-")[0]); |
| 141 | annotations.set(UUID, uuid); |
| 142 | |
Diego Garcia | 09ab5e2 | 2018-12-18 11:47:01 +0100 | [diff] [blame] | 143 | JsonNode mcPool = sipAttributes.get(MEDIA_CHANNEL_SERVICE_INTERFACE_POINT_SPEC).get(MC_POOL); |
Andrea Campanella | b9e491b | 2019-02-18 17:45:01 +0100 | [diff] [blame] | 144 | // We get the first OCH signal as reported by the device. |
| 145 | OchSignal ochSignal = getOchSignal(mcPool).iterator().next(); |
| 146 | //add och port |
| 147 | ports.add(ochPortDescription(portNumber, true, OduSignalType.ODU4, |
| 148 | false, ochSignal, annotations.build())); |
Diego Garcia | 09ab5e2 | 2018-12-18 11:47:01 +0100 | [diff] [blame] | 149 | |
Diego Garcia | aab9947 | 2019-01-10 13:53:31 +0100 | [diff] [blame] | 150 | |
Diego Garcia | 09ab5e2 | 2018-12-18 11:47:01 +0100 | [diff] [blame] | 151 | } else { |
| 152 | log.error("SIP {} is not valid", sipAttributes); |
| 153 | } |
| 154 | } |
| 155 | log.debug("PortList: {}", ports); |
| 156 | return ImmutableList.copyOf(ports); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Create a filter method to identify just valid OLS SIPs.This method must check the |
| 161 | * tapi object: "layer-protocol-name" and matching only if equals to "PHOTONIC-MEDIA" SIPs. |
| 162 | * Moreover,the filtering could be enhanced by reading also: |
| 163 | * "supported-layer-protocol-qualifier", to identify valid protocol-qualifier values such: |
| 164 | * [PHOTONIC_LAYER_QUALIFIER_NMC, PHOTONIC_LAYER_QUALIFIER_NMCA, PHOTONIC_LAYER_QUALIFIER_OTSI...] |
| 165 | **/ |
| 166 | private boolean checkValidEndpoint(JsonNode sipAttributes) { |
Andrea Campanella | b9e491b | 2019-02-18 17:45:01 +0100 | [diff] [blame] | 167 | return sipAttributes.has(LAYER_PROTOCOL_NAME) && |
| 168 | sipAttributes.get(LAYER_PROTOCOL_NAME).toString().contains(PHOTONIC_MEDIA) && |
| 169 | sipAttributes.has(SUPPORTED_LAYER_PROTOCOL_QUALIFIER) && |
Diego Garcia | 09ab5e2 | 2018-12-18 11:47:01 +0100 | [diff] [blame] | 170 | sipAttributes.get(SUPPORTED_LAYER_PROTOCOL_QUALIFIER).toString() |
Andrea Campanella | b9e491b | 2019-02-18 17:45:01 +0100 | [diff] [blame] | 171 | .contains(PHOTONIC_LAYER_QUALIFIER_NMC); |
Diego Garcia | 09ab5e2 | 2018-12-18 11:47:01 +0100 | [diff] [blame] | 172 | } |
| 173 | |
Diego Garcia | 09ab5e2 | 2018-12-18 11:47:01 +0100 | [diff] [blame] | 174 | } |