blob: 4f759233a9c51cc7d68df8c860463371a5524d83 [file] [log] [blame]
Laszlo Papp8b3a5f62017-10-05 13:32:00 +01001/*
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
17package org.onosproject.drivers.polatis.netconf;
18
19import com.google.common.collect.ImmutableList;
20import org.apache.commons.configuration.HierarchicalConfiguration;
21import org.onosproject.drivers.utilities.XmlConfigParser;
22import org.onosproject.net.driver.DriverHandler;
23import org.onosproject.netconf.DatastoreId;
24import org.onosproject.netconf.NetconfController;
25import org.onosproject.netconf.NetconfException;
26import org.onosproject.netconf.NetconfSession;
27
28import org.slf4j.Logger;
29
30import java.util.List;
31import java.util.Set;
32
33import static com.google.common.base.Preconditions.checkNotNull;
34
35import static org.slf4j.LoggerFactory.getLogger;
36
37/**
38 * Netconf utility for polatis netconf drivers.
39 */
40public final class PolatisNetconfUtility {
41
42 public static final String KEY_XMLNS = "xmlns=\"http://www.polatis.com/yang/optical-switch\"";
Laszlo Papp4ffd6ae2018-03-28 18:09:55 +010043 public static final String KEY_POLATIS_XMLNS = "xmlns=\"http://www.polatis.com/yang/polatis-switch\"";
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010044 public static final String KEY_DATA = "data";
45 public static final String KEY_PORT = "port";
Michael Enrico584eebd2021-07-22 08:04:13 +000046 public static final String KEY_PAIR = "pair";
47 public static final String KEY_PAIRS = "pairs";
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010048 public static final String KEY_PORTID = "port-id";
Michael Enrico584eebd2021-07-22 08:04:13 +000049 public static final String KEY_PORTPEER = "portPeer";
50 public static final String KEY_PORTDIR = "portDir";
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010051 public static final String KEY_PORTCONFIG = "port-config";
Laszlo Papp4ffd6ae2018-03-28 18:09:55 +010052 public static final String KEY_SYSTEMALARMS = "system-alarms";
53 public static final String KEY_ALARM = "alarm";
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010054 public static final String KEY_CONNS = "cross-connects";
55 public static final String KEY_PRODINF = "product-information";
Michael Enrico584eebd2021-07-22 08:04:13 +000056 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 Papp8b3a5f62017-10-05 13:32:00 +010059 public static final String KEY_PORTCONFIG_XMLNS = String.format("%s %s", KEY_PORTCONFIG, KEY_XMLNS);
Laszlo Papp4ffd6ae2018-03-28 18:09:55 +010060 public static final String KEY_SYSTEMALARMS_XMLNS = String.format("%s %s", KEY_SYSTEMALARMS, KEY_POLATIS_XMLNS);
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010061 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 Enrico584eebd2021-07-22 08:04:13 +000063 public static final String KEY_PORTSETSTATE_XMLNS = String.format("%s %s", KEY_PORTSETSTATE, KEY_XMLNS);
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010064 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 Papp4ffd6ae2018-03-28 18:09:55 +010067 public static final String KEY_DATA_SYSTEMALARMS = String.format("%s.%s.%s", KEY_DATA, KEY_SYSTEMALARMS, KEY_ALARM);
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010068 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 Papp8cd61fb2017-10-13 16:45:00 +010070 public static final String KEY_DATA_OPM = String.format("%s.%s.%s", KEY_DATA, KEY_OPM, KEY_PORT);
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010071 public static final String KEY_DATA_OPM_PORT = String.format("%s.%s.%s", KEY_DATA, KEY_OPM, KEY_PORT);
Laszlo Papp8b87a192017-10-19 18:47:12 +010072 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 Papp8b3a5f62017-10-05 13:32:00 +010075 public static final String OPTICAL_CAPABILITY_PREFIX
76 = "http://www.polatis.com/yang/optical-switch?module=optical-switch&amp;revision=";
77
Laszlo Papp8b87a192017-10-19 18:47:12 +010078 public static final String CFG_MODE_MERGE = "merge";
79
Michael Enrico584eebd2021-07-22 08:04:13 +000080 private static final Logger log = getLogger(PolatisDeviceDescription.class);
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010081
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 Milkey986a47a2018-01-25 11:38:51 -080098 throw new IllegalStateException(new NetconfException("Failed to retrieve configuration.", e));
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010099 }
100 return reply;
101 }
102
103 /**
Michael Enrico584eebd2021-07-22 08:04:13 +0000104 * 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 Papp8b3a5f62017-10-05 13:32:00 +0100121 * 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 Milkey986a47a2018-01-25 11:38:51 -0800133 throw new IllegalStateException(new NetconfException("Failed to retrieve configuration.", e));
Laszlo Papp8b3a5f62017-10-05 13:32:00 +0100134 }
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 Milkey986a47a2018-01-25 11:38:51 -0800152 throw new IllegalStateException(new NetconfException("Failed to edit configuration.", e));
Laszlo Papp8b3a5f62017-10-05 13:32:00 +0100153 }
154 return reply;
155 }
156
157 /**
Michael Enrico584eebd2021-07-22 08:04:13 +0000158 * 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 Papp8b3a5f62017-10-05 13:32:00 +0100176 * 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 Enrico584eebd2021-07-22 08:04:13 +0000185 HierarchicalConfiguration cfg = XmlConfigParser.loadXmlString(content, false);
Laszlo Papp8b3a5f62017-10-05 13:32:00 +0100186 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 Enrico584eebd2021-07-22 08:04:13 +0000204 HierarchicalConfiguration cfg = XmlConfigParser.loadXmlString(content, false);
Laszlo Papp8b3a5f62017-10-05 13:32:00 +0100205 info = cfg.configurationsAt(key);
Michael Enrico584eebd2021-07-22 08:04:13 +0000206
Laszlo Papp8b3a5f62017-10-05 13:32:00 +0100207 } 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 Pappbdb64082018-09-11 12:21:29 +0100267 * 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 Papp8b3a5f62017-10-05 13:32:00 +0100284 * 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 Milkey986a47a2018-01-25 11:38:51 -0800292 throw new IllegalStateException(new NetconfException("Failed to retrieve the netconf device."));
Laszlo Papp8b3a5f62017-10-05 13:32:00 +0100293 }
294 return session;
295 }
296}