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