Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | |
| 17 | package org.onosproject.drivers.polatis.netconf; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import org.apache.commons.configuration.HierarchicalConfiguration; |
| 21 | import org.onosproject.drivers.utilities.XmlConfigParser; |
| 22 | import org.onosproject.net.driver.DriverHandler; |
| 23 | import org.onosproject.netconf.DatastoreId; |
| 24 | import org.onosproject.netconf.NetconfController; |
| 25 | import org.onosproject.netconf.NetconfException; |
| 26 | import org.onosproject.netconf.NetconfSession; |
| 27 | |
| 28 | import org.slf4j.Logger; |
| 29 | |
| 30 | import java.util.List; |
| 31 | import java.util.Set; |
| 32 | |
| 33 | import static com.google.common.base.Preconditions.checkNotNull; |
| 34 | |
| 35 | import static org.slf4j.LoggerFactory.getLogger; |
| 36 | |
| 37 | /** |
| 38 | * Netconf utility for polatis netconf drivers. |
| 39 | */ |
| 40 | public final class PolatisNetconfUtility { |
| 41 | |
| 42 | public static final String KEY_XMLNS = "xmlns=\"http://www.polatis.com/yang/optical-switch\""; |
Laszlo Papp | 4ffd6ae | 2018-03-28 18:09:55 +0100 | [diff] [blame] | 43 | public static final String KEY_POLATIS_XMLNS = "xmlns=\"http://www.polatis.com/yang/polatis-switch\""; |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 44 | public static final String KEY_DATA = "data"; |
| 45 | public static final String KEY_PORT = "port"; |
Michael Enrico | 584eebd | 2021-07-22 08:04:13 +0000 | [diff] [blame^] | 46 | public static final String KEY_PAIR = "pair"; |
| 47 | public static final String KEY_PAIRS = "pairs"; |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 48 | public static final String KEY_PORTID = "port-id"; |
Michael Enrico | 584eebd | 2021-07-22 08:04:13 +0000 | [diff] [blame^] | 49 | public static final String KEY_PORTPEER = "portPeer"; |
| 50 | public static final String KEY_PORTDIR = "portDir"; |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 51 | public static final String KEY_PORTCONFIG = "port-config"; |
Laszlo Papp | 4ffd6ae | 2018-03-28 18:09:55 +0100 | [diff] [blame] | 52 | public static final String KEY_SYSTEMALARMS = "system-alarms"; |
| 53 | public static final String KEY_ALARM = "alarm"; |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 54 | public static final String KEY_CONNS = "cross-connects"; |
| 55 | public static final String KEY_PRODINF = "product-information"; |
Michael Enrico | 584eebd | 2021-07-22 08:04:13 +0000 | [diff] [blame^] | 56 | public static final String KEY_PORTSETSTATE = "port-set-state"; |
| 57 | public static final String KEY_RPCREPLY = "rpc-reply"; |
| 58 | public static final String KEY_OK = "<ok/>"; |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 59 | public static final String KEY_PORTCONFIG_XMLNS = String.format("%s %s", KEY_PORTCONFIG, KEY_XMLNS); |
Laszlo Papp | 4ffd6ae | 2018-03-28 18:09:55 +0100 | [diff] [blame] | 60 | public static final String KEY_SYSTEMALARMS_XMLNS = String.format("%s %s", KEY_SYSTEMALARMS, KEY_POLATIS_XMLNS); |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 61 | public static final String KEY_CONNS_XMLNS = String.format("%s %s", KEY_CONNS, KEY_XMLNS); |
| 62 | public static final String KEY_PRODINF_XMLNS = String.format("%s %s", KEY_PRODINF, KEY_XMLNS); |
Michael Enrico | 584eebd | 2021-07-22 08:04:13 +0000 | [diff] [blame^] | 63 | public static final String KEY_PORTSETSTATE_XMLNS = String.format("%s %s", KEY_PORTSETSTATE, KEY_XMLNS); |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 64 | public static final String KEY_DATA_CONNS = String.format("%s.%s", KEY_DATA, KEY_CONNS); |
| 65 | public static final String KEY_DATA_PRODINF = String.format("%s.%s", KEY_DATA, KEY_PRODINF); |
| 66 | public static final String KEY_DATA_PORTCONFIG = String.format("%s.%s.%s", KEY_DATA, KEY_PORTCONFIG, KEY_PORT); |
Laszlo Papp | 4ffd6ae | 2018-03-28 18:09:55 +0100 | [diff] [blame] | 67 | public static final String KEY_DATA_SYSTEMALARMS = String.format("%s.%s.%s", KEY_DATA, KEY_SYSTEMALARMS, KEY_ALARM); |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 68 | public static final String KEY_OPM = "opm-power"; |
| 69 | public static final String KEY_OPM_XMLNS = String.format("%s %s", KEY_OPM, KEY_XMLNS); |
Laszlo Papp | 8cd61fb | 2017-10-13 16:45:00 +0100 | [diff] [blame] | 70 | public static final String KEY_DATA_OPM = String.format("%s.%s.%s", KEY_DATA, KEY_OPM, KEY_PORT); |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 71 | public static final String KEY_DATA_OPM_PORT = String.format("%s.%s.%s", KEY_DATA, KEY_OPM, KEY_PORT); |
Laszlo Papp | 8b87a19 | 2017-10-19 18:47:12 +0100 | [diff] [blame] | 72 | public static final String KEY_VOA = "voa"; |
| 73 | public static final String KEY_VOA_XMLNS = String.format("%s %s", KEY_VOA, KEY_XMLNS); |
| 74 | public static final String KEY_DATA_VOA_PORT = String.format("%s.%s.%s", KEY_DATA, KEY_VOA, KEY_PORT); |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 75 | public static final String OPTICAL_CAPABILITY_PREFIX |
| 76 | = "http://www.polatis.com/yang/optical-switch?module=optical-switch&revision="; |
| 77 | |
Laszlo Papp | 8b87a19 | 2017-10-19 18:47:12 +0100 | [diff] [blame] | 78 | public static final String CFG_MODE_MERGE = "merge"; |
| 79 | |
Michael Enrico | 584eebd | 2021-07-22 08:04:13 +0000 | [diff] [blame^] | 80 | private static final Logger log = getLogger(PolatisDeviceDescription.class); |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 81 | |
| 82 | private PolatisNetconfUtility() { |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Retrieves session reply information for get operation. |
| 87 | * |
| 88 | * @param handler parent driver handler |
| 89 | * @param filter the filter string of xml content |
| 90 | * @return the reply string |
| 91 | */ |
| 92 | public static String netconfGet(DriverHandler handler, String filter) { |
| 93 | NetconfSession session = getNetconfSession(handler); |
| 94 | String reply; |
| 95 | try { |
| 96 | reply = session.get(filter, null); |
| 97 | } catch (NetconfException e) { |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 98 | throw new IllegalStateException(new NetconfException("Failed to retrieve configuration.", e)); |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 99 | } |
| 100 | return reply; |
| 101 | } |
| 102 | |
| 103 | /** |
Michael Enrico | 584eebd | 2021-07-22 08:04:13 +0000 | [diff] [blame^] | 104 | * Retrieves session reply information for get operation. |
| 105 | * |
| 106 | * @param session explicit NETCONF session |
| 107 | * @param filter the filter string of xml content |
| 108 | * @return the reply string |
| 109 | */ |
| 110 | public static String netconfGet(NetconfSession session, String filter) { |
| 111 | String reply; |
| 112 | try { |
| 113 | reply = session.get(filter, null); |
| 114 | } catch (NetconfException e) { |
| 115 | throw new IllegalStateException(new NetconfException("Failed to retrieve configuration.", e)); |
| 116 | } |
| 117 | return reply; |
| 118 | } |
| 119 | |
| 120 | /** |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 121 | * Retrieves session reply information for get config operation. |
| 122 | * |
| 123 | * @param handler parent driver handler |
| 124 | * @param filter the filter string of xml content |
| 125 | * @return the reply string |
| 126 | */ |
| 127 | public static String netconfGetConfig(DriverHandler handler, String filter) { |
| 128 | NetconfSession session = getNetconfSession(handler); |
| 129 | String reply; |
| 130 | try { |
| 131 | reply = session.getConfig(DatastoreId.RUNNING, filter); |
| 132 | } catch (NetconfException e) { |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 133 | throw new IllegalStateException(new NetconfException("Failed to retrieve configuration.", e)); |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 134 | } |
| 135 | return reply; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Retrieves session reply information for edit config operation. |
| 140 | * |
| 141 | * @param handler parent driver handler |
| 142 | * @param mode selected mode to change the configuration |
| 143 | * @param cfg the new configuration to be set |
| 144 | * @return the reply string |
| 145 | */ |
| 146 | public static boolean netconfEditConfig(DriverHandler handler, String mode, String cfg) { |
| 147 | NetconfSession session = getNetconfSession(handler); |
| 148 | boolean reply = false; |
| 149 | try { |
| 150 | reply = session.editConfig(DatastoreId.RUNNING, mode, cfg); |
| 151 | } catch (NetconfException e) { |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 152 | throw new IllegalStateException(new NetconfException("Failed to edit configuration.", e)); |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 153 | } |
| 154 | return reply; |
| 155 | } |
| 156 | |
| 157 | /** |
Michael Enrico | 584eebd | 2021-07-22 08:04:13 +0000 | [diff] [blame^] | 158 | * Makes a NETCONF RPC. |
| 159 | * |
| 160 | * @param handler parent driver handler |
| 161 | * @param body body of RPC |
| 162 | * @return the reply string |
| 163 | */ |
| 164 | public static String netconfRpc(DriverHandler handler, String body) { |
| 165 | NetconfSession session = getNetconfSession(handler); |
| 166 | String reply; |
| 167 | try { |
| 168 | reply = session.doWrappedRpc(body); |
| 169 | } catch (NetconfException e) { |
| 170 | throw new IllegalStateException(new NetconfException("Failed to make RPC..", e)); |
| 171 | } |
| 172 | return reply; |
| 173 | } |
| 174 | |
| 175 | /** |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 176 | * Retrieves specified node hierarchical configuration from the xml information. |
| 177 | * |
| 178 | * @param content the xml information |
| 179 | * @param key the configuration key node |
| 180 | * @return the hierarchical configuration, null if exception happens |
| 181 | */ |
| 182 | public static HierarchicalConfiguration configAt(String content, String key) { |
| 183 | HierarchicalConfiguration info; |
| 184 | try { |
Michael Enrico | 584eebd | 2021-07-22 08:04:13 +0000 | [diff] [blame^] | 185 | HierarchicalConfiguration cfg = XmlConfigParser.loadXmlString(content, false); |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 186 | info = cfg.configurationAt(key); |
| 187 | } catch (IllegalArgumentException e) { |
| 188 | // Accept null for information polling |
| 189 | return null; |
| 190 | } |
| 191 | return info; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Retrieves specified node hierarchical configurations from the xml information. |
| 196 | * |
| 197 | * @param content the xml information |
| 198 | * @param key the configuration key node |
| 199 | * @return the hierarchical configurations, empty if exception happens |
| 200 | */ |
| 201 | public static List<HierarchicalConfiguration> configsAt(String content, String key) { |
| 202 | List<HierarchicalConfiguration> info; |
| 203 | try { |
Michael Enrico | 584eebd | 2021-07-22 08:04:13 +0000 | [diff] [blame^] | 204 | HierarchicalConfiguration cfg = XmlConfigParser.loadXmlString(content, false); |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 205 | info = cfg.configurationsAt(key); |
Michael Enrico | 584eebd | 2021-07-22 08:04:13 +0000 | [diff] [blame^] | 206 | |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 207 | } catch (IllegalArgumentException e) { |
| 208 | // Accept empty for information polling |
| 209 | return ImmutableList.of(); |
| 210 | } |
| 211 | return info; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Makes a xml format sentence. |
| 216 | * |
| 217 | * @param node the node name |
| 218 | * @param content the node content |
| 219 | * @return the xml format sentence |
| 220 | */ |
| 221 | public static String xml(String node, String content) { |
| 222 | return String.format("<%s>%s</%s>", node, content, node); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Makes a xml format open tag. |
| 227 | * |
| 228 | * @param node the node name |
| 229 | * @return the xml head format string |
| 230 | */ |
| 231 | public static String xmlOpen(String node) { |
| 232 | return String.format("<%s>", node); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Makes a xml format close tag. |
| 237 | * |
| 238 | * @param node the node name |
| 239 | * @return the xml end format string |
| 240 | */ |
| 241 | public static String xmlClose(String node) { |
| 242 | return String.format("</%s>", node); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Makes a xml format empty tag. |
| 247 | * |
| 248 | * @param node the node name |
| 249 | * @return the xml format of empty tag |
| 250 | */ |
| 251 | public static String xmlEmpty(String node) { |
| 252 | return String.format("<%s/>", node); |
| 253 | } |
| 254 | |
| 255 | public static String opticalRevision(DriverHandler handler) { |
| 256 | NetconfSession session = getNetconfSession(handler); |
| 257 | Set<String> capabilities = session.getDeviceCapabilitiesSet(); |
| 258 | for (String c : capabilities) { |
| 259 | if (c.startsWith(OPTICAL_CAPABILITY_PREFIX)) { |
| 260 | return c.substring(OPTICAL_CAPABILITY_PREFIX.length()); |
| 261 | } |
| 262 | } |
| 263 | return null; |
| 264 | } |
| 265 | |
| 266 | /** |
Laszlo Papp | bdb6408 | 2018-09-11 12:21:29 +0100 | [diff] [blame] | 267 | * Subscribes for notifications. |
| 268 | * |
| 269 | * @param handler parent driver handler |
| 270 | * @return true on success, false otherwise |
| 271 | */ |
| 272 | public static boolean subscribe(DriverHandler handler) { |
| 273 | NetconfSession session = getNetconfSession(handler); |
| 274 | try { |
| 275 | session.startSubscription(); |
| 276 | } catch (NetconfException e) { |
| 277 | log.error("Failed to subscribe for notifications."); |
| 278 | return false; |
| 279 | } |
| 280 | return true; |
| 281 | } |
| 282 | |
| 283 | /** |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 284 | * Returns the NETCONF session of the device. |
| 285 | * |
| 286 | * @return session |
| 287 | */ |
| 288 | private static NetconfSession getNetconfSession(DriverHandler handler) { |
| 289 | NetconfController controller = checkNotNull(handler.get(NetconfController.class)); |
| 290 | NetconfSession session = controller.getNetconfDevice(handler.data().deviceId()).getSession(); |
| 291 | if (session == null) { |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 292 | throw new IllegalStateException(new NetconfException("Failed to retrieve the netconf device.")); |
Laszlo Papp | 8b3a5f6 | 2017-10-05 13:32:00 +0100 | [diff] [blame] | 293 | } |
| 294 | return session; |
| 295 | } |
| 296 | } |