blob: ee1fe3947d36be7a39977cdf7353627a8e54e7c7 [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";
46 public static final String KEY_PORTID = "port-id";
47 public static final String KEY_PORTCONFIG = "port-config";
Laszlo Papp4ffd6ae2018-03-28 18:09:55 +010048 public static final String KEY_SYSTEMALARMS = "system-alarms";
49 public static final String KEY_ALARM = "alarm";
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010050 public static final String KEY_CONNS = "cross-connects";
51 public static final String KEY_PRODINF = "product-information";
52 public static final String KEY_PORTCONFIG_XMLNS = String.format("%s %s", KEY_PORTCONFIG, KEY_XMLNS);
Laszlo Papp4ffd6ae2018-03-28 18:09:55 +010053 public static final String KEY_SYSTEMALARMS_XMLNS = String.format("%s %s", KEY_SYSTEMALARMS, KEY_POLATIS_XMLNS);
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010054 public static final String KEY_CONNS_XMLNS = String.format("%s %s", KEY_CONNS, KEY_XMLNS);
55 public static final String KEY_PRODINF_XMLNS = String.format("%s %s", KEY_PRODINF, KEY_XMLNS);
56 public static final String KEY_DATA_CONNS = String.format("%s.%s", KEY_DATA, KEY_CONNS);
57 public static final String KEY_DATA_PRODINF = String.format("%s.%s", KEY_DATA, KEY_PRODINF);
58 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 +010059 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 +010060 public static final String KEY_OPM = "opm-power";
61 public static final String KEY_OPM_XMLNS = String.format("%s %s", KEY_OPM, KEY_XMLNS);
Laszlo Papp8cd61fb2017-10-13 16:45:00 +010062 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 +010063 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 +010064 public static final String KEY_VOA = "voa";
65 public static final String KEY_VOA_XMLNS = String.format("%s %s", KEY_VOA, KEY_XMLNS);
66 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 +010067 public static final String OPTICAL_CAPABILITY_PREFIX
68 = "http://www.polatis.com/yang/optical-switch?module=optical-switch&revision=";
69
Laszlo Papp8b87a192017-10-19 18:47:12 +010070 public static final String CFG_MODE_MERGE = "merge";
71
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010072 private static final Logger log = getLogger(PolatisFlowRuleProgrammable.class);
73
74 private PolatisNetconfUtility() {
75 }
76
77 /**
78 * Retrieves session reply information for get operation.
79 *
80 * @param handler parent driver handler
81 * @param filter the filter string of xml content
82 * @return the reply string
83 */
84 public static String netconfGet(DriverHandler handler, String filter) {
85 NetconfSession session = getNetconfSession(handler);
86 String reply;
87 try {
88 reply = session.get(filter, null);
89 } catch (NetconfException e) {
Ray Milkey986a47a2018-01-25 11:38:51 -080090 throw new IllegalStateException(new NetconfException("Failed to retrieve configuration.", e));
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010091 }
92 return reply;
93 }
94
95 /**
96 * Retrieves session reply information for get config operation.
97 *
98 * @param handler parent driver handler
99 * @param filter the filter string of xml content
100 * @return the reply string
101 */
102 public static String netconfGetConfig(DriverHandler handler, String filter) {
103 NetconfSession session = getNetconfSession(handler);
104 String reply;
105 try {
106 reply = session.getConfig(DatastoreId.RUNNING, filter);
107 } catch (NetconfException e) {
Ray Milkey986a47a2018-01-25 11:38:51 -0800108 throw new IllegalStateException(new NetconfException("Failed to retrieve configuration.", e));
Laszlo Papp8b3a5f62017-10-05 13:32:00 +0100109 }
110 return reply;
111 }
112
113 /**
114 * Retrieves session reply information for edit config operation.
115 *
116 * @param handler parent driver handler
117 * @param mode selected mode to change the configuration
118 * @param cfg the new configuration to be set
119 * @return the reply string
120 */
121 public static boolean netconfEditConfig(DriverHandler handler, String mode, String cfg) {
122 NetconfSession session = getNetconfSession(handler);
123 boolean reply = false;
124 try {
125 reply = session.editConfig(DatastoreId.RUNNING, mode, cfg);
126 } catch (NetconfException e) {
Ray Milkey986a47a2018-01-25 11:38:51 -0800127 throw new IllegalStateException(new NetconfException("Failed to edit configuration.", e));
Laszlo Papp8b3a5f62017-10-05 13:32:00 +0100128 }
129 return reply;
130 }
131
132 /**
133 * Retrieves specified node hierarchical configuration from the xml information.
134 *
135 * @param content the xml information
136 * @param key the configuration key node
137 * @return the hierarchical configuration, null if exception happens
138 */
139 public static HierarchicalConfiguration configAt(String content, String key) {
140 HierarchicalConfiguration info;
141 try {
142 HierarchicalConfiguration cfg = XmlConfigParser.loadXmlString(content);
143 info = cfg.configurationAt(key);
144 } catch (IllegalArgumentException e) {
145 // Accept null for information polling
146 return null;
147 }
148 return info;
149 }
150
151 /**
152 * Retrieves specified node hierarchical configurations from the xml information.
153 *
154 * @param content the xml information
155 * @param key the configuration key node
156 * @return the hierarchical configurations, empty if exception happens
157 */
158 public static List<HierarchicalConfiguration> configsAt(String content, String key) {
159 List<HierarchicalConfiguration> info;
160 try {
161 HierarchicalConfiguration cfg = XmlConfigParser.loadXmlString(content);
162 info = cfg.configurationsAt(key);
163 } catch (IllegalArgumentException e) {
164 // Accept empty for information polling
165 return ImmutableList.of();
166 }
167 return info;
168 }
169
170 /**
171 * Makes a xml format sentence.
172 *
173 * @param node the node name
174 * @param content the node content
175 * @return the xml format sentence
176 */
177 public static String xml(String node, String content) {
178 return String.format("<%s>%s</%s>", node, content, node);
179 }
180
181 /**
182 * Makes a xml format open tag.
183 *
184 * @param node the node name
185 * @return the xml head format string
186 */
187 public static String xmlOpen(String node) {
188 return String.format("<%s>", node);
189 }
190
191 /**
192 * Makes a xml format close tag.
193 *
194 * @param node the node name
195 * @return the xml end format string
196 */
197 public static String xmlClose(String node) {
198 return String.format("</%s>", node);
199 }
200
201 /**
202 * Makes a xml format empty tag.
203 *
204 * @param node the node name
205 * @return the xml format of empty tag
206 */
207 public static String xmlEmpty(String node) {
208 return String.format("<%s/>", node);
209 }
210
211 public static String opticalRevision(DriverHandler handler) {
212 NetconfSession session = getNetconfSession(handler);
213 Set<String> capabilities = session.getDeviceCapabilitiesSet();
214 for (String c : capabilities) {
215 if (c.startsWith(OPTICAL_CAPABILITY_PREFIX)) {
216 return c.substring(OPTICAL_CAPABILITY_PREFIX.length());
217 }
218 }
219 return null;
220 }
221
222 /**
Laszlo Pappbdb64082018-09-11 12:21:29 +0100223 * Subscribes for notifications.
224 *
225 * @param handler parent driver handler
226 * @return true on success, false otherwise
227 */
228 public static boolean subscribe(DriverHandler handler) {
229 NetconfSession session = getNetconfSession(handler);
230 try {
231 session.startSubscription();
232 } catch (NetconfException e) {
233 log.error("Failed to subscribe for notifications.");
234 return false;
235 }
236 return true;
237 }
238
239 /**
Laszlo Papp8b3a5f62017-10-05 13:32:00 +0100240 * Returns the NETCONF session of the device.
241 *
242 * @return session
243 */
244 private static NetconfSession getNetconfSession(DriverHandler handler) {
245 NetconfController controller = checkNotNull(handler.get(NetconfController.class));
246 NetconfSession session = controller.getNetconfDevice(handler.data().deviceId()).getSession();
247 if (session == null) {
Ray Milkey986a47a2018-01-25 11:38:51 -0800248 throw new IllegalStateException(new NetconfException("Failed to retrieve the netconf device."));
Laszlo Papp8b3a5f62017-10-05 13:32:00 +0100249 }
250 return session;
251 }
252}