Rohit Singh | 35fd94c | 2019-11-11 16:38:18 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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 is contributed by Sterlite Technologies |
| 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.onosproject.drivers.odtn.util.NetconfSessionUtility; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.PortNumber; |
| 25 | import org.onosproject.net.behaviour.BitErrorRateState; |
| 26 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 27 | import org.onosproject.netconf.NetconfController; |
| 28 | import org.onosproject.netconf.NetconfSession; |
| 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
| 32 | import java.util.Optional; |
| 33 | |
| 34 | import static com.google.common.base.Preconditions.checkNotNull; |
| 35 | |
| 36 | /* |
| 37 | * Driver Implementation of the BitErrorRateConfig for OpenConfig terminal devices. |
| 38 | */ |
| 39 | public abstract class OpenconfigBitErrorRateState extends AbstractHandlerBehaviour implements BitErrorRateState { |
| 40 | |
| 41 | private static final Logger log = LoggerFactory.getLogger(OpenconfigBitErrorRateState.class); |
| 42 | |
| 43 | private static final String RPC_TAG_NETCONF_BASE = |
| 44 | "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"; |
| 45 | |
| 46 | private static final String RPC_CLOSE_TAG = "</rpc>"; |
| 47 | private static final String PRE_FEC_BER_TAG = "pre-fec-ber"; |
| 48 | private static final String POST_FEC_BER_TAG = "post-fec-ber"; |
| 49 | |
| 50 | /* |
| 51 | * This method returns the instance of NetconfController from DriverHandler. |
| 52 | */ |
| 53 | private NetconfController getController() { |
| 54 | return handler().get(NetconfController.class); |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * This method is used for getting Openconfig Component name. |
| 59 | * from DeviceService port annotations by device specific key for component name |
| 60 | * |
| 61 | * @param deviceId the device identifier |
| 62 | * @param port the port identifier |
| 63 | * @return String value representing Openconfig component name |
| 64 | */ |
| 65 | protected abstract String ocName(DeviceId deviceId, PortNumber port); |
| 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 preFecBerFilter = generateBerFilter(deviceId, port, PRE_FEC_BER_TAG); |
| 81 | String rpcRequest = getConfigOperation(preFecBerFilter); |
| 82 | log.debug("RPC call for fetching Pre FEC BER : {}", rpcRequest); |
| 83 | |
| 84 | XMLConfiguration xconf = NetconfSessionUtility.executeRpc(session, rpcRequest); |
| 85 | |
| 86 | if (xconf == null) { |
| 87 | log.error("Error in executing Pre FEC BER RPC"); |
| 88 | return Optional.empty(); |
| 89 | } |
| 90 | |
| 91 | try { |
| 92 | HierarchicalConfiguration config = |
| 93 | xconf.configurationAt("data/components/component/transceiver/state/" + PRE_FEC_BER_TAG); |
| 94 | if (config == null || config.getString("instant") == null) { |
| 95 | return Optional.empty(); |
| 96 | } |
| 97 | double ber = Float.valueOf(config.getString("instant")).doubleValue(); |
| 98 | return Optional.of(ber); |
| 99 | |
| 100 | } catch (IllegalArgumentException e) { |
| 101 | log.error("Error in fetching configuration : {}", e.getMessage()); |
| 102 | return Optional.empty(); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Get the BER value post FEC. |
| 108 | * |
| 109 | * @param deviceId the device identifier |
| 110 | * @param port the port identifier |
| 111 | * @return the decimal value of BER |
| 112 | */ |
| 113 | @Override |
| 114 | public Optional<Double> getPostFecBer(DeviceId deviceId, PortNumber port) { |
| 115 | NetconfSession session = NetconfSessionUtility |
| 116 | .getNetconfSession(deviceId, getController()); |
| 117 | checkNotNull(session); |
| 118 | |
| 119 | String postFecBerFilter = generateBerFilter(deviceId, port, POST_FEC_BER_TAG); |
| 120 | String rpcRequest = getConfigOperation(postFecBerFilter); |
| 121 | log.debug("RPC call for fetching Post FEC BER : {}", rpcRequest); |
| 122 | |
| 123 | XMLConfiguration xconf = NetconfSessionUtility.executeRpc(session, rpcRequest); |
| 124 | |
| 125 | if (xconf == null) { |
| 126 | log.error("Error in executing Post FEC BER RPC"); |
| 127 | return Optional.empty(); |
| 128 | } |
| 129 | |
| 130 | try { |
| 131 | HierarchicalConfiguration config = |
| 132 | xconf.configurationAt("data/components/component/transceiver/state/" + POST_FEC_BER_TAG); |
| 133 | if (config == null || config.getString("instant") == null) { |
| 134 | return Optional.empty(); |
| 135 | } |
| 136 | double ber = Float.valueOf(config.getString("instant")).doubleValue(); |
| 137 | return Optional.of(ber); |
| 138 | |
| 139 | } catch (IllegalArgumentException e) { |
| 140 | log.error("Error in fetching configuration : {}", e.getMessage()); |
| 141 | return Optional.empty(); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * This method is used for generating RPC for Netconf getconfig operation. |
| 147 | * |
| 148 | * @param filter the String representing the <filter> tag in Netconf RPC |
| 149 | * @return the String value representing Netconf getConfig Operation |
| 150 | */ |
| 151 | private String getConfigOperation(String filter) { |
| 152 | StringBuilder rpcRequest = new StringBuilder(); |
| 153 | |
| 154 | rpcRequest.append(RPC_TAG_NETCONF_BASE) |
| 155 | .append("<get>") |
| 156 | .append("<filter>") |
| 157 | .append(filter) |
| 158 | .append("</filter>") |
| 159 | .append("</get>") |
| 160 | .append(RPC_CLOSE_TAG); |
| 161 | |
| 162 | return rpcRequest.toString(); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Generate the BER filter to be used in Netconf get-operation RPC. |
| 167 | * |
| 168 | * @param deviceId the device identifier |
| 169 | * @param port the port identifier |
| 170 | * @param tag the String value to identify the preFecBer / PostFecBer |
| 171 | * @return the String value representing the BER filter |
| 172 | */ |
| 173 | private String generateBerFilter(DeviceId deviceId, PortNumber port, String tag) { |
| 174 | StringBuilder filter = new StringBuilder("<components xmlns=\"http://openconfig.net/yang/platform\">"); |
| 175 | filter.append("<component>"); |
| 176 | filter.append("<name>").append(ocName(deviceId, port)).append("</name>"); |
| 177 | filter.append("<transceiver xmlns=\"http://openconfig.net/yang/platform/transceiver\">"); |
| 178 | filter.append("<state>"); |
| 179 | if (PRE_FEC_BER_TAG.equals(tag)) { |
| 180 | filter.append("<pre-fec-ber>").append("<instant/>").append("</pre-fec-ber>"); |
| 181 | } else if (POST_FEC_BER_TAG.equals(tag)) { |
| 182 | filter.append("<post-fec-ber>").append("<instant/>").append("</post-fec-ber>"); |
| 183 | } |
| 184 | filter.append("</state>"); |
| 185 | filter.append("</transceiver>"); |
| 186 | filter.append("</component>"); |
| 187 | filter.append("</components>"); |
| 188 | |
| 189 | return filter.toString(); |
| 190 | } |
| 191 | } |