hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [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 | |
| 19 | package org.onosproject.drivers.odtn; |
| 20 | |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame^] | 21 | import com.google.common.annotations.VisibleForTesting; |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableList; |
| 23 | import org.apache.commons.configuration.HierarchicalConfiguration; |
| 24 | import org.apache.commons.configuration.XMLConfiguration; |
| 25 | import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine; |
| 26 | import org.onlab.packet.ChassisId; |
| 27 | import org.onosproject.drivers.utilities.XmlConfigParser; |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame^] | 28 | import org.onosproject.net.ChannelSpacing; |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 29 | import org.onosproject.net.DefaultAnnotations; |
| 30 | import org.onosproject.net.Device; |
| 31 | import org.onosproject.net.DeviceId; |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame^] | 32 | import org.onosproject.net.OchSignal; |
| 33 | import org.onosproject.net.OduSignalType; |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 34 | import org.onosproject.net.PortNumber; |
| 35 | import org.onosproject.net.device.DefaultDeviceDescription; |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 36 | import org.onosproject.net.device.DeviceDescription; |
| 37 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
| 38 | import org.onosproject.net.device.PortDescription; |
| 39 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame^] | 40 | import org.onosproject.net.optical.device.OchPortHelper; |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 41 | import org.onosproject.netconf.NetconfController; |
| 42 | import org.onosproject.netconf.NetconfDevice; |
| 43 | import org.onosproject.netconf.NetconfSession; |
| 44 | import org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery; |
| 45 | import org.slf4j.Logger; |
| 46 | |
| 47 | import java.util.HashMap; |
| 48 | import java.util.List; |
| 49 | import java.util.Map; |
| 50 | import java.util.Objects; |
| 51 | import java.util.concurrent.CompletableFuture; |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 52 | import java.util.stream.Collectors; |
| 53 | |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame^] | 54 | import static com.google.common.base.Preconditions.checkNotNull; |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 55 | import static org.slf4j.LoggerFactory.getLogger; |
| 56 | |
| 57 | |
| 58 | /** |
| 59 | * Driver Implementation of the DeviceDescrption discovery for OpenConfig |
| 60 | * terminal devices. |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 61 | */ |
| 62 | public class CassiniTerminalDeviceDiscovery |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame^] | 63 | extends AbstractHandlerBehaviour |
| 64 | implements OdtnDeviceDescriptionDiscovery, DeviceDescriptionDiscovery { |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 65 | |
| 66 | private static final String RPC_TAG_NETCONF_BASE = |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame^] | 67 | "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"; |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 68 | |
| 69 | private static final String RPC_CLOSE_TAG = "</rpc>"; |
| 70 | |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 71 | private static final String OC_TRANSPORT_TYPES_OPTICAL_CHANNEL = |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame^] | 72 | "OPTICAL_CHANNEL"; |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 73 | |
| 74 | private static final Logger log = getLogger(CassiniTerminalDeviceDiscovery.class); |
| 75 | |
| 76 | |
| 77 | /** |
| 78 | * Returns the NetconfSession with the device for which the method was called. |
| 79 | * |
| 80 | * @param deviceId device indetifier |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 81 | * @return The netconf session or null |
| 82 | */ |
| 83 | private NetconfSession getNetconfSession(DeviceId deviceId) { |
| 84 | NetconfController controller = handler().get(NetconfController.class); |
| 85 | NetconfDevice ncdev = controller.getDevicesMap().get(deviceId); |
| 86 | if (ncdev == null) { |
| 87 | log.trace("No netconf device, returning null session"); |
| 88 | return null; |
| 89 | } |
| 90 | return ncdev.getSession(); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | /** |
| 95 | * Get the deviceId for which the methods apply. |
| 96 | * |
| 97 | * @return The deviceId as contained in the handler data |
| 98 | */ |
| 99 | private DeviceId did() { |
| 100 | return handler().data().deviceId(); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | /** |
| 105 | * Construct a String with a Netconf filtered get RPC Message. |
| 106 | * |
| 107 | * @param filter A valid XML tree with the filter to apply in the get |
| 108 | * @return a String containing the RPC XML Document |
| 109 | */ |
| 110 | private String filteredGetBuilder(String filter) { |
| 111 | StringBuilder rpc = new StringBuilder(RPC_TAG_NETCONF_BASE); |
| 112 | rpc.append("<get>"); |
| 113 | rpc.append("<filter type='subtree'>"); |
| 114 | rpc.append(filter); |
| 115 | rpc.append("</filter>"); |
| 116 | rpc.append("</get>"); |
| 117 | rpc.append(RPC_CLOSE_TAG); |
| 118 | return rpc.toString(); |
| 119 | } |
| 120 | |
| 121 | |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 122 | @Override |
| 123 | public DeviceDescription discoverDeviceDetails() { |
| 124 | return new DefaultDeviceDescription(handler().data().deviceId().uri(), |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame^] | 125 | Device.Type.TERMINAL_DEVICE, |
| 126 | "EDGECORE", |
| 127 | "Cassini", |
| 128 | "OcNOS", |
| 129 | "", |
| 130 | new ChassisId("1")); |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 134 | /** |
| 135 | * Returns a list of PortDescriptions for the device. |
| 136 | * |
| 137 | * @return a list of descriptions. |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame^] | 138 | * <p> |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 139 | * The RPC reply follows the following pattern: |
| 140 | * //CHECKSTYLE:OFF |
| 141 | * <pre>{@code |
| 142 | * <?xml version="1.0" encoding="UTF-8"?> |
| 143 | * <rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="7"> |
| 144 | * <data> |
| 145 | * <components xmlns="http://openconfig.net/yang/platform"> |
| 146 | * <component>.... |
| 147 | * </component> |
| 148 | * <component>.... |
| 149 | * </component> |
| 150 | * </components> |
| 151 | * </data> |
| 152 | * </rpc-reply> |
| 153 | * }</pre> |
| 154 | * //CHECKSTYLE:ON |
| 155 | */ |
| 156 | @Override |
| 157 | public List<PortDescription> discoverPortDetails() { |
| 158 | try { |
| 159 | NetconfSession session = getNetconfSession(did()); |
| 160 | if (session == null) { |
| 161 | log.error("discoverPortDetails called with null session for {}", did()); |
| 162 | return ImmutableList.of(); |
| 163 | } |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame^] | 164 | CompletableFuture<CharSequence> fut1 = session.asyncGet(); |
| 165 | String rpcReplyTest = fut1.get().toString(); |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 166 | |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame^] | 167 | XMLConfiguration xconf = (XMLConfiguration) XmlConfigParser.loadXmlString(rpcReplyTest); |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 168 | xconf.setExpressionEngine(new XPathExpressionEngine()); |
| 169 | |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame^] | 170 | HierarchicalConfiguration logicalChannels = xconf.configurationAt("components"); |
| 171 | return discoverPorts(logicalChannels); |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 172 | } catch (Exception e) { |
| 173 | log.error("Exception discoverPortDetails() {}", did(), e); |
| 174 | return ImmutableList.of(); |
| 175 | } |
| 176 | } |
| 177 | |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 178 | /** |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame^] | 179 | * Parses port information from OpenConfig XML configuration. |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 180 | * |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame^] | 181 | * @param cfg tree where the root node is {@literal <data>} |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 182 | * @return List of ports |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 183 | */ |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame^] | 184 | @VisibleForTesting |
| 185 | private List<PortDescription> discoverPorts(HierarchicalConfiguration cfg) { |
| 186 | // If we want to use XPath |
| 187 | cfg.setExpressionEngine(new XPathExpressionEngine()); |
| 188 | // converting components into PortDescription. |
| 189 | List<HierarchicalConfiguration> components = cfg.configurationsAt("component"); |
| 190 | return components.stream() |
| 191 | .map(this::toPortDescriptionInternal) |
| 192 | .filter(Objects::nonNull) |
| 193 | .collect(Collectors.toList()); |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame^] | 196 | /** |
| 197 | * Converts Component subtree to PortDescription. |
| 198 | * |
| 199 | * @param component subtree to parse |
| 200 | * @return PortDescription or null if component is not an ONOS Port |
| 201 | */ |
| 202 | private PortDescription toPortDescriptionInternal(HierarchicalConfiguration component) { |
| 203 | Map<String, String> annotations = new HashMap<>(); |
| 204 | /* |
| 205 | <components xmlns="http://openconfig.net/yang/platform"> |
| 206 | <component> |
| 207 | <name>oc1/0</name> |
| 208 | <config> |
| 209 | <name>oc1/0</name> |
| 210 | </config> |
| 211 | <state> |
| 212 | <name>oc1/0</name> |
| 213 | <type>OPTICAL_CHANNEL</type> |
| 214 | <id/> |
| 215 | <description/> |
| 216 | <mfg-name/> |
| 217 | <hardware-version/> |
| 218 | <firmware-version/> |
| 219 | <software-version/> |
| 220 | <serial-no/> |
| 221 | <part-no/> |
| 222 | <removable>true</removable> |
| 223 | <empty>false</empty> |
| 224 | <parent/> |
| 225 | <temperature> |
| 226 | <instant>0.0</instant> |
| 227 | <avg>0.0</avg> |
| 228 | <min>0.0</min> |
| 229 | <max>0.0</max> |
| 230 | <interval>0</interval> |
| 231 | <min-time>0</min-time> |
| 232 | <max-time>0</max-time> |
| 233 | <alarm-status>true</alarm-status> |
| 234 | <alarm-threshold>0</alarm-threshold> |
| 235 | </temperature> |
| 236 | <memory> |
| 237 | <available>0</available> |
| 238 | <utilized>0</utilized> |
| 239 | </memory> |
| 240 | <allocated-power>0</allocated-power> |
| 241 | <used-power>0</used-power> |
| 242 | </state> |
| 243 | <optical-channel xmlns="http://openconfig.net/yang/terminal-device"> |
| 244 | <config> |
| 245 | <line-port>port-10101</line-port> |
| 246 | </config> |
| 247 | <state> |
| 248 | <output-power/> |
| 249 | <input-power/> |
| 250 | </state> |
| 251 | </optical-channel> |
| 252 | </component> |
| 253 | */ |
| 254 | String name = component.getString("name"); |
| 255 | String type = component.getString("state/type"); |
| 256 | checkNotNull(name, "name not found"); |
| 257 | checkNotNull(type, "state/type not found"); |
| 258 | annotations.put(OdtnDeviceDescriptionDiscovery.OC_NAME, name); |
| 259 | annotations.put(OdtnDeviceDescriptionDiscovery.OC_TYPE, type); |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 260 | |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame^] | 261 | //TODO this currently support only line-side ports through parsing of optical channels. |
| 262 | if (type.equals(OC_TRANSPORT_TYPES_OPTICAL_CHANNEL)) { |
| 263 | String portName = component.getString("optical-channel/config/line-port"); |
| 264 | String portIndex = portName.split("-")[1]; |
| 265 | annotations.putIfAbsent("name", portName); |
| 266 | annotations.putIfAbsent(PORT_TYPE, OdtnPortType.LINE.value()); |
| 267 | annotations.putIfAbsent(ONOS_PORT_INDEX, portIndex); |
| 268 | annotations.putIfAbsent(CONNECTION_ID, "connection-" + portIndex); |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 269 | |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame^] | 270 | OchSignal signalId = OchSignal.newDwdmSlot(ChannelSpacing.CHL_50GHZ, 1); |
| 271 | return OchPortHelper.ochPortDescription( |
| 272 | PortNumber.portNumber(Long.parseLong(portIndex)), |
| 273 | true, |
| 274 | OduSignalType.ODU4, // TODO Client signal to be discovered |
| 275 | true, |
| 276 | signalId, |
| 277 | DefaultAnnotations.builder().putAll(annotations).build()); |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 278 | |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame^] | 279 | } else { |
| 280 | log.debug("Unknown port component type {}", type); |
| 281 | return null; |
| 282 | } |
| 283 | } |
hiroki | 9e1484d | 2018-12-07 09:36:49 -0800 | [diff] [blame] | 284 | } |