Andrea Campanella | 99ab710 | 2019-07-26 14:43:14 +0200 | [diff] [blame] | 1 | /* |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame] | 2 | * Copyright 2019-present Open Networking Foundation |
Andrea Campanella | 99ab710 | 2019-07-26 14:43:14 +0200 | [diff] [blame] | 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; |
| 19 | |
| 20 | import org.apache.commons.configuration.HierarchicalConfiguration; |
| 21 | import org.apache.commons.configuration.XMLConfiguration; |
| 22 | import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine; |
| 23 | import org.onlab.osgi.DefaultServiceDirectory; |
| 24 | import org.onosproject.drivers.utilities.XmlConfigParser; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.ModulationScheme; |
| 27 | import org.onosproject.net.PortNumber; |
| 28 | import org.onosproject.net.behaviour.ModulationConfig; |
| 29 | import org.onosproject.net.device.DeviceService; |
| 30 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame] | 31 | import org.onosproject.netconf.DatastoreId; |
Andrea Campanella | 99ab710 | 2019-07-26 14:43:14 +0200 | [diff] [blame] | 32 | import org.onosproject.netconf.NetconfController; |
| 33 | import org.onosproject.netconf.NetconfDevice; |
| 34 | import org.onosproject.netconf.NetconfException; |
| 35 | import org.onosproject.netconf.NetconfSession; |
| 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
| 38 | |
Andrea Campanella | 99ab710 | 2019-07-26 14:43:14 +0200 | [diff] [blame] | 39 | import java.util.Optional; |
| 40 | import java.util.concurrent.CompletableFuture; |
| 41 | import java.util.concurrent.ExecutionException; |
| 42 | |
| 43 | import static com.google.common.base.Preconditions.checkNotNull; |
| 44 | |
| 45 | /* |
| 46 | * Driver Implementation of the ModulationConfig for OcNos standard open config based terminal devices. |
| 47 | */ |
| 48 | public class CassiniModulationOpenConfig<T> extends AbstractHandlerBehaviour implements ModulationConfig<T> { |
| 49 | |
| 50 | |
| 51 | private static final String RPC_TAG_NETCONF_BASE = |
| 52 | "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"; |
| 53 | |
| 54 | private static final String RPC_CLOSE_TAG = "</rpc>"; |
| 55 | |
| 56 | private static final Logger log = LoggerFactory.getLogger(CassiniModulationOpenConfig.class); |
| 57 | |
| 58 | private ComponentType state = ComponentType.DIRECTION; |
| 59 | |
| 60 | |
| 61 | enum BitRate { |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame] | 62 | GBPS_200(200), // 200 Gbps |
| 63 | GBPS_100(100), // 100 Gbps |
| 64 | GBPS_40(40), // 40 Gbps |
| 65 | GBPS_10(10); // 10 Gbps |
Andrea Campanella | 99ab710 | 2019-07-26 14:43:14 +0200 | [diff] [blame] | 66 | |
| 67 | private final long value; |
| 68 | |
| 69 | public long getValue() { |
| 70 | return value; |
| 71 | } |
| 72 | |
| 73 | BitRate(long value) { |
| 74 | this.value = value; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Returns the NetconfSession with the device for which the method was called. |
| 80 | * |
| 81 | * @param deviceId device indetifier |
| 82 | * @return The netconf session or null |
| 83 | */ |
| 84 | |
| 85 | |
| 86 | private NetconfSession getNetconfSession(DeviceId deviceId) { |
| 87 | log.info("Inside getNetconfSession () method for device : {}", deviceId); |
| 88 | NetconfController controller = handler().get(NetconfController.class); |
| 89 | NetconfDevice ncdev = controller.getDevicesMap().get(deviceId); |
| 90 | if (ncdev == null) { |
| 91 | log.trace("No netconf device, returning null session"); |
| 92 | return null; |
| 93 | } |
| 94 | return ncdev.getSession(); |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * |
| 99 | * Get the deviceId for which the methods apply. |
| 100 | * |
| 101 | * @return The deviceId as contained in the handler data |
| 102 | */ |
| 103 | |
| 104 | |
| 105 | private DeviceId did() { |
| 106 | return handler().data().deviceId(); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Execute RPC request. |
| 111 | * |
| 112 | * @param session Netconf session |
| 113 | * @param message Netconf message in XML format |
| 114 | * @return XMLConfiguration object |
| 115 | */ |
| 116 | |
| 117 | private XMLConfiguration executeRpc(NetconfSession session, String message) { |
| 118 | try { |
| 119 | CompletableFuture<String> fut = session.rpc(message); |
| 120 | String rpcReply = fut.get(); |
| 121 | XMLConfiguration xconf = (XMLConfiguration) XmlConfigParser.loadXmlString(rpcReply); |
| 122 | xconf.setExpressionEngine(new XPathExpressionEngine()); |
| 123 | return xconf; |
| 124 | } catch (NetconfException ne) { |
| 125 | log.error("Exception on Netconf protocol: {}.", ne); |
| 126 | } catch (InterruptedException ie) { |
| 127 | log.error("Interrupted Exception: {}.", ie); |
| 128 | } catch (ExecutionException ee) { |
| 129 | log.error("Concurrent Exception while executing Netconf operation: {}.", ee); |
| 130 | } |
| 131 | return null; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Get the target Modulation Scheme on the component. |
| 136 | * |
| 137 | * @param port the port |
| 138 | * @param component the port component |
| 139 | * @return ModulationScheme as per bitRate value |
| 140 | **/ |
| 141 | @Override |
| 142 | public Optional<ModulationScheme> getModulationScheme(PortNumber port, T component) { |
| 143 | checkState(component); |
| 144 | return state.getModulationScheme(port, component); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Set the target Modulation Scheme on the component. |
| 149 | * |
| 150 | * @param port the port |
| 151 | * @param component the port component |
| 152 | * @param bitRate bit rate in bps |
| 153 | **/ |
| 154 | @Override |
| 155 | public void setModulationScheme(PortNumber port, T component, long bitRate) { |
| 156 | checkState(component); |
| 157 | state.setModulationScheme(port, component, bitRate); |
| 158 | } |
| 159 | |
| 160 | |
| 161 | /* |
| 162 | * |
| 163 | * Set the ComponentType to invoke proper methods for different template T. |
| 164 | * @param component the component. |
| 165 | */ |
| 166 | void checkState(Object component) { |
| 167 | String clsName = component.getClass().getName(); |
| 168 | switch (clsName) { |
| 169 | case "org.onosproject.net.Direction": |
| 170 | state = CassiniModulationOpenConfig.ComponentType.DIRECTION; |
| 171 | break; |
| 172 | case "org.onosproject.net.OchSignal": |
| 173 | state = CassiniModulationOpenConfig.ComponentType.OCHSIGNAL; |
| 174 | break; |
| 175 | default: |
| 176 | log.error("Cannot parse the component type {}.", clsName); |
| 177 | log.info("The component content is {}.", component.toString()); |
| 178 | } |
| 179 | |
| 180 | state.cassini = this; |
| 181 | log.info("Setting the state with clsName :{} ", clsName); |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * |
| 186 | * Component type. |
| 187 | */ |
| 188 | |
| 189 | |
| 190 | enum ComponentType { |
| 191 | |
| 192 | /* |
| 193 | * |
| 194 | * Direction. |
| 195 | */ |
| 196 | |
| 197 | |
| 198 | DIRECTION() { |
| 199 | @Override |
| 200 | Optional<ModulationScheme> getModulationScheme(PortNumber port, Object component) { |
| 201 | return super.getModulationScheme(port, component); |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | void setModulationScheme(PortNumber port, Object component, long bitRate) { |
| 206 | log.info("Inside the enum setModulationScheme()"); |
| 207 | super.setModulationScheme(port, component, bitRate); |
| 208 | } |
| 209 | }, |
| 210 | |
| 211 | /** |
| 212 | * OchSignal. |
| 213 | */ |
| 214 | |
| 215 | |
| 216 | OCHSIGNAL() { |
| 217 | @Override |
| 218 | Optional<ModulationScheme> getModulationScheme(PortNumber port, Object component) { |
| 219 | return super.getModulationScheme(port, component); |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | void setModulationScheme(PortNumber port, Object component, long bitRate) { |
| 224 | super.setModulationScheme(port, component, bitRate); |
| 225 | } |
| 226 | }; |
| 227 | |
| 228 | |
| 229 | CassiniModulationOpenConfig cassini; |
| 230 | |
Andrea Campanella | 99ab710 | 2019-07-26 14:43:14 +0200 | [diff] [blame] | 231 | /* |
| 232 | * mirror method in the internal class. |
| 233 | * @param port port |
| 234 | * @param component component |
| 235 | * @return target modulation |
| 236 | */ |
| 237 | Optional<ModulationScheme> getModulationScheme(PortNumber port, Object component) { |
| 238 | NetconfSession session = cassini.getNetconfSession(cassini.did()); |
| 239 | checkNotNull(session); |
| 240 | String filter = createModulationFilter(cassini, port); |
| 241 | StringBuilder rpcReq = new StringBuilder(); |
| 242 | rpcReq.append(RPC_TAG_NETCONF_BASE) |
Andrea Campanella | 3ccee48 | 2019-08-02 19:15:18 +0200 | [diff] [blame] | 243 | .append("<get-config>") |
| 244 | .append("<source>") |
| 245 | .append("<" + DatastoreId.RUNNING + "/>") |
| 246 | .append("</source>") |
Andrea Campanella | 99ab710 | 2019-07-26 14:43:14 +0200 | [diff] [blame] | 247 | .append("<filter type='subtree'>") |
| 248 | .append(filter) |
| 249 | .append("</filter>") |
Andrea Campanella | 3ccee48 | 2019-08-02 19:15:18 +0200 | [diff] [blame] | 250 | .append("</get-config>") |
Andrea Campanella | 99ab710 | 2019-07-26 14:43:14 +0200 | [diff] [blame] | 251 | .append(RPC_CLOSE_TAG); |
Andrea Campanella | 99ab710 | 2019-07-26 14:43:14 +0200 | [diff] [blame] | 252 | XMLConfiguration xconf = cassini.executeRpc(session, rpcReq.toString()); |
Andrea Campanella | 3ccee48 | 2019-08-02 19:15:18 +0200 | [diff] [blame] | 253 | if (xconf == null) { |
| 254 | log.error("Error in executingRpc"); |
| 255 | return Optional.empty(); |
| 256 | } |
Andrea Campanella | 99ab710 | 2019-07-26 14:43:14 +0200 | [diff] [blame] | 257 | try { |
| 258 | HierarchicalConfiguration config = |
Andrea Campanella | 3ccee48 | 2019-08-02 19:15:18 +0200 | [diff] [blame] | 259 | xconf.configurationAt("data/components/component/optical-channel/config"); |
Andrea Campanella | 99ab710 | 2019-07-26 14:43:14 +0200 | [diff] [blame] | 260 | |
Andrea Campanella | 3ccee48 | 2019-08-02 19:15:18 +0200 | [diff] [blame] | 261 | String modulationScheme = String.valueOf(config.getString("modulation-format")); |
Andrea Campanella | 99ab710 | 2019-07-26 14:43:14 +0200 | [diff] [blame] | 262 | /*Used for Internal Testing */ |
| 263 | //String modulationScheme="DP16QAM"; |
Andrea Campanella | 3ccee48 | 2019-08-02 19:15:18 +0200 | [diff] [blame] | 264 | ModulationScheme modulation; |
| 265 | if (modulationScheme.equalsIgnoreCase("dp-16-qam")) { |
| 266 | modulation = ModulationScheme.DP_16QAM; |
| 267 | } else if (modulationScheme.equalsIgnoreCase("dp-8-qam")) { |
| 268 | modulation = ModulationScheme.DP_8QAM; |
| 269 | } else { |
| 270 | modulation = ModulationScheme.DP_QPSK; |
| 271 | } |
Andrea Campanella | 99ab710 | 2019-07-26 14:43:14 +0200 | [diff] [blame] | 272 | return Optional.of(modulation); |
| 273 | } catch (IllegalArgumentException e) { |
Andrea Campanella | 3ccee48 | 2019-08-02 19:15:18 +0200 | [diff] [blame] | 274 | log.error("Error in parsing config", e); |
Andrea Campanella | 99ab710 | 2019-07-26 14:43:14 +0200 | [diff] [blame] | 275 | return Optional.empty(); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | * mirror method in the internal class. |
| 281 | * @param port port |
| 282 | * @param component component |
| 283 | * @param power target value |
| 284 | */ |
| 285 | void setModulationScheme(PortNumber port, Object component, long bitRate) { |
| 286 | |
| 287 | ModulationScheme modulation = null; |
| 288 | String editConfig = null; |
| 289 | |
| 290 | //check if bitrate is less than equal to 100 Gig |
| 291 | if (bitRate <= BitRate.GBPS_100.value) { |
| 292 | modulation = ModulationScheme.DP_QPSK; |
| 293 | editConfig = modulationEditConfig(cassini, port, component, bitRate, modulation.name()); |
| 294 | //setting the modulation by calling rpc |
| 295 | setModulationRpc(port, component, editConfig); |
| 296 | } else { // check if bitrate is greater than 100 Gig |
| 297 | modulation = ModulationScheme.DP_16QAM; |
| 298 | editConfig = modulationEditConfig(cassini, port, component, bitRate, modulation.name()); |
| 299 | //setting the modulation by calling rpc |
| 300 | setModulationRpc(port, component, editConfig); |
| 301 | } |
| 302 | |
| 303 | |
| 304 | } |
| 305 | |
| 306 | private static String createModulationFilter(CassiniModulationOpenConfig modulationConfig, |
| 307 | PortNumber portNumber) { |
| 308 | String name = ocName(modulationConfig, portNumber); |
| 309 | StringBuilder sb = new StringBuilder("<components xmlns=\"http://openconfig.net/yang/platform\">"); |
| 310 | sb.append("<component>").append("<name>").append(name).append("</name>"); |
| 311 | sb.append("</component>").append("</components>"); |
| 312 | return sb.toString(); |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Extract component name from portNumber's annotations. |
| 317 | * |
| 318 | * @param pc modulation config instance |
| 319 | * @param portNumber the port number |
| 320 | * @return the component name |
| 321 | */ |
| 322 | |
| 323 | |
| 324 | private static String ocName(CassiniModulationOpenConfig pc, PortNumber portNumber) { |
| 325 | DeviceService deviceService = DefaultServiceDirectory.getService(DeviceService.class); |
| 326 | DeviceId deviceId = pc.handler().data().deviceId(); |
| 327 | return deviceService.getPort(deviceId, portNumber).annotations().value("oc-name"); |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * |
| 332 | * Parse filtering string from port and component. |
| 333 | * @param portNumber Port Number |
| 334 | * @param component port component (optical-channel) |
| 335 | * @param bitRate bitRate in bps |
| 336 | * @return filtering string in xml format |
| 337 | |
| 338 | */ |
| 339 | private String modulationEditConfig(CassiniModulationOpenConfig modulationConfig, PortNumber portNumber, |
| 340 | Object component, long bitRate, String modulation) { |
| 341 | /* |
| 342 | <components xmlns="http://openconfig.net/yang/platform"> |
| 343 | <component> |
| 344 | <name>oc1/0</name> |
| 345 | <config> |
| 346 | <name>oc1/0</name> |
| 347 | </config> |
| 348 | <optical-channel xmlns="http://openconfig.net/yang/terminal-device"> |
| 349 | <config> |
| 350 | <modulation-format>dp-16-qam</modulation-format> |
| 351 | </config> |
| 352 | </optical-channel> |
| 353 | </component> |
| 354 | </components> |
| 355 | */ |
| 356 | String modulationOcNos = convertToOcNosModulation(modulation); |
| 357 | if (component != null) { |
| 358 | // This is an edit-config operation. |
| 359 | String portName = ocName(modulationConfig, portNumber); //oc1/0 |
| 360 | StringBuilder sb = new StringBuilder("<components xmlns=\"http://openconfig.net/yang/platform\">"); |
| 361 | sb.append("<component>"); |
| 362 | sb.append("<name>").append(portName).append("</name>"); |
| 363 | sb.append("<config>"); |
| 364 | sb.append("<name>").append(portName).append("</name>"); |
| 365 | sb.append("</config>"); |
| 366 | sb.append("<optical-channel xmlns=\"http://openconfig.net/yang/terminal-device\">") |
| 367 | .append("<config>") |
| 368 | .append("<modulation-format>") |
| 369 | .append(modulationOcNos) |
| 370 | .append("</modulation-format>") |
| 371 | .append("</config>") |
| 372 | .append("</optical-channel>"); |
| 373 | sb.append("</component>"); |
| 374 | sb.append("</components>"); |
| 375 | return sb.toString(); |
| 376 | } else { |
| 377 | log.error("Cannot process the component {}.", component.getClass()); |
| 378 | return null; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | private String convertToOcNosModulation(String modulation) { |
| 383 | if (modulation.equals(ModulationScheme.DP_QPSK.name())) { |
| 384 | return "dp-qpsk"; |
| 385 | } else { |
| 386 | return "dp-16-qam"; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | private boolean setModulationRpc(PortNumber port, Object component, String editConfig) { |
| 391 | NetconfSession session = cassini.getNetconfSession(cassini.did()); |
| 392 | checkNotNull(session); |
| 393 | boolean response = true; |
| 394 | StringBuilder rpcReq = new StringBuilder(); |
| 395 | rpcReq.append(RPC_TAG_NETCONF_BASE) |
| 396 | .append("<edit-config>") |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame] | 397 | .append("<target><" + DatastoreId.CANDIDATE + "/></target>") |
Andrea Campanella | 99ab710 | 2019-07-26 14:43:14 +0200 | [diff] [blame] | 398 | .append("<config>") |
| 399 | .append(editConfig) |
| 400 | .append("</config>") |
| 401 | .append("</edit-config>") |
| 402 | .append(RPC_CLOSE_TAG); |
| 403 | log.info("RPC call for Setting Modulation : {}", rpcReq.toString()); |
| 404 | XMLConfiguration xconf = cassini.executeRpc(session, rpcReq.toString()); |
Andrea Campanella | 3ccee48 | 2019-08-02 19:15:18 +0200 | [diff] [blame] | 405 | if (xconf == null) { |
| 406 | log.error("The <edit-config> operation to set target-modulation of Port({}:{}) is failed.", |
| 407 | port.toString(), component.toString()); |
| 408 | } else if (!xconf.getRoot().getChild(0).getName().equals("ok")) { |
| 409 | // The successful reply should be "<rpc-reply ...><ok /></rpc-reply>" |
Andrea Campanella | 99ab710 | 2019-07-26 14:43:14 +0200 | [diff] [blame] | 410 | response = false; |
| 411 | log.error("The <edit-config> operation to set target-modulation of Port({}:{}) is failed.", |
| 412 | port.toString(), component.toString()); |
| 413 | } |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame] | 414 | try { |
| 415 | session.commit(); |
| 416 | } catch (NetconfException e) { |
| 417 | response = false; |
| 418 | log.error("error committing modulation changes"); |
| 419 | } |
Andrea Campanella | 99ab710 | 2019-07-26 14:43:14 +0200 | [diff] [blame] | 420 | return response; |
| 421 | } |
| 422 | } |
Andrea Campanella | 99ab710 | 2019-07-26 14:43:14 +0200 | [diff] [blame] | 423 | } |