alessio | 111ea5d | 2021-12-01 12:08:14 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2022-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 B5G-OPEN (101016663). |
| 17 | */ |
| 18 | |
| 19 | package org.onosproject.drivers.odtn; |
| 20 | |
| 21 | import org.apache.commons.configuration.XMLConfiguration; |
| 22 | import org.onosproject.drivers.odtn.util.NetconfSessionUtility; |
| 23 | import org.onosproject.drivers.utilities.XmlConfigParser; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.Port; |
| 26 | import org.onosproject.net.PortNumber; |
| 27 | import org.onosproject.net.behaviour.BitErrorRateState; |
| 28 | import org.onosproject.net.device.DeviceService; |
| 29 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 30 | import org.onosproject.netconf.NetconfController; |
| 31 | import org.onosproject.netconf.NetconfException; |
| 32 | import org.onosproject.netconf.NetconfSession; |
| 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
| 35 | |
| 36 | import java.util.Optional; |
| 37 | |
| 38 | import static com.google.common.base.Preconditions.checkNotNull; |
| 39 | import static org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery.OC_OPTICAL_CHANNEL_NAME; |
| 40 | |
| 41 | /** |
| 42 | * Implementation of BitErrorRateState interface for Cassini device running Ocnos v5. |
| 43 | */ |
| 44 | public class CassiniOcnos5BitErrorRate |
| 45 | extends AbstractHandlerBehaviour implements BitErrorRateState { |
| 46 | |
| 47 | private static final Logger log = LoggerFactory.getLogger(CassiniOcnos5BitErrorRate.class); |
| 48 | |
| 49 | private static final String RPC_TAG_NETCONF_BASE = |
| 50 | "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"; |
| 51 | |
| 52 | private static final String RPC_CLOSE_TAG = "</rpc>"; |
| 53 | private static final String PRE_FEC_BER_TAG = "current-pre-fec-ber"; |
| 54 | private static final String POST_FEC_BER_TAG = "current-post-fec-ber"; |
| 55 | private static final String PRE_FEC_BER_FILTER = |
| 56 | "data.terminal-device.coherent-module.network-interfaces.interface.ber.state." + PRE_FEC_BER_TAG; |
| 57 | private static final String POST_FEC_BER_FILTER = |
| 58 | "data..terminal-device.coherent-module.network-interfaces.interface.ber.state." + POST_FEC_BER_TAG; |
| 59 | |
| 60 | /* |
| 61 | * This method returns the instance of NetconfController from DriverHandler. |
| 62 | */ |
| 63 | private NetconfController getController() { |
| 64 | return handler().get(NetconfController.class); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Get the BER value pre FEC. |
| 69 | * |
| 70 | * @param deviceId the device identifier |
| 71 | * @param port the port identifier |
| 72 | * @return the decimal value of BER |
| 73 | */ |
| 74 | @Override |
| 75 | public Optional<Double> getPreFecBer(DeviceId deviceId, PortNumber port) { |
| 76 | NetconfSession session = NetconfSessionUtility |
| 77 | .getNetconfSession(deviceId, getController()); |
| 78 | checkNotNull(session); |
| 79 | |
| 80 | String slotIndex = getOpticalChannel(port); |
| 81 | |
| 82 | if (slotIndex != null) { |
| 83 | |
| 84 | String reply; |
| 85 | try { |
| 86 | reply = session.get(getBerFilter(slotIndex, PRE_FEC_BER_TAG)); |
| 87 | } catch (Exception e) { |
| 88 | throw new IllegalStateException(new NetconfException("Failed to retrieve getPreFecBer info.", e)); |
| 89 | } |
| 90 | |
| 91 | log.debug("REPLY from device: {}", reply); |
| 92 | |
| 93 | XMLConfiguration xconf = (XMLConfiguration) XmlConfigParser.loadXmlString(reply); |
| 94 | if (xconf == null) { |
| 95 | log.error("Error in executing RPC"); |
| 96 | return Optional.empty(); |
| 97 | } |
| 98 | |
| 99 | String powerString = xconf.getString((PRE_FEC_BER_FILTER)); |
| 100 | |
| 101 | log.debug("currentPreFecBer from device: {}", powerString); |
| 102 | |
| 103 | if (powerString == null) { |
| 104 | return Optional.empty(); |
| 105 | } |
| 106 | |
| 107 | Double rational = 1e18; |
| 108 | return Optional.of(Double.valueOf(powerString) / rational); |
| 109 | } |
| 110 | |
| 111 | return Optional.empty(); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Get the OpenConfig component name for the OpticalChannel component. |
| 116 | * |
| 117 | * @param portNumber ONOS port number of the Line port (). |
| 118 | * @return the channel component name or null |
| 119 | */ |
| 120 | protected String getOpticalChannel(PortNumber portNumber) { |
| 121 | Port clientPort = handler().get(DeviceService.class).getPort(did(), portNumber); |
| 122 | return clientPort.annotations().value(OC_OPTICAL_CHANNEL_NAME); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Get the deviceId for which the methods apply. |
| 127 | * |
| 128 | * @return The deviceId as contained in the handler data |
| 129 | */ |
| 130 | private DeviceId did() { |
| 131 | return handler().data().deviceId(); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Get the BER value post FEC. |
| 136 | * |
| 137 | * @param deviceId the device identifier |
| 138 | * @param port the port identifier |
| 139 | * @return the decimal value of BER |
| 140 | */ |
| 141 | @Override |
| 142 | public Optional<Double> getPostFecBer(DeviceId deviceId, PortNumber port) { |
| 143 | NetconfSession session = NetconfSessionUtility |
| 144 | .getNetconfSession(deviceId, getController()); |
| 145 | checkNotNull(session); |
| 146 | |
| 147 | String slotIndex = getOpticalChannel(port); |
| 148 | |
| 149 | if (slotIndex != null) { |
| 150 | |
| 151 | String reply; |
| 152 | try { |
| 153 | reply = session.get(getBerFilter(slotIndex, POST_FEC_BER_TAG)); |
| 154 | } catch (Exception e) { |
| 155 | throw new IllegalStateException(new NetconfException("Failed to retrieve getPostFecBer info.", e)); |
| 156 | } |
| 157 | |
| 158 | log.debug("REPLY from device: {}", reply); |
| 159 | |
| 160 | XMLConfiguration xconf = (XMLConfiguration) XmlConfigParser.loadXmlString(reply); |
| 161 | if (xconf == null) { |
| 162 | log.error("Error in executing RPC"); |
| 163 | return Optional.empty(); |
| 164 | } |
| 165 | |
| 166 | String powerString = xconf.getString(POST_FEC_BER_FILTER); |
| 167 | |
| 168 | log.debug("currentPostFecBer from device: {}", powerString); |
| 169 | |
| 170 | if (powerString == null) { |
| 171 | return Optional.empty(); |
| 172 | } |
| 173 | |
| 174 | Double rational = 1e18; |
| 175 | return Optional.of(Double.valueOf(powerString) / rational); |
| 176 | } |
| 177 | |
| 178 | return Optional.empty(); |
| 179 | } |
| 180 | |
| 181 | private String getBerFilter(String slotNumber, String filterBer) { |
| 182 | StringBuilder filter = new StringBuilder(); |
| 183 | |
| 184 | filter.append("<terminal-device xmlns='http://www.ipinfusion.com/yang/ocnos/ipi-platform-terminal-device'>" |
| 185 | + "<coherent-module>" |
| 186 | + " <slot-index>" + slotNumber + "</slot-index>" |
| 187 | + "<network-interfaces>" |
| 188 | + "<interface>" |
| 189 | + " <net-index>0</net-index>" |
| 190 | + " <ber>" |
| 191 | + " <state>" |
| 192 | + " <" + filterBer + "/>" |
| 193 | + " </state>" |
| 194 | + " </ber>" |
| 195 | + "</interface>" |
| 196 | + "</network-interfaces>" |
| 197 | + "</coherent-module>" |
| 198 | + "</terminal-device>"); |
| 199 | |
| 200 | return filteredGetBuilder(filter.toString()); |
| 201 | } |
| 202 | |
| 203 | private String filteredGetBuilder(String filter) { |
| 204 | StringBuilder rpc = new StringBuilder(RPC_TAG_NETCONF_BASE); |
| 205 | rpc.append("<get>"); |
| 206 | rpc.append("<filter type='subtree'>"); |
| 207 | rpc.append(filter); |
| 208 | rpc.append("</filter>"); |
| 209 | rpc.append("</get>"); |
| 210 | rpc.append(RPC_CLOSE_TAG); |
| 211 | return rpc.toString(); |
| 212 | } |
| 213 | } |