blob: 6b8080b4d4ed911197fa8dcedebfe4e43c855ccd [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\"";
43 public static final String KEY_DATA = "data";
44 public static final String KEY_PORT = "port";
45 public static final String KEY_PORTID = "port-id";
46 public static final String KEY_PORTCONFIG = "port-config";
47 public static final String KEY_CONNS = "cross-connects";
48 public static final String KEY_PRODINF = "product-information";
49 public static final String KEY_PORTCONFIG_XMLNS = String.format("%s %s", KEY_PORTCONFIG, KEY_XMLNS);
50 public static final String KEY_CONNS_XMLNS = String.format("%s %s", KEY_CONNS, KEY_XMLNS);
51 public static final String KEY_PRODINF_XMLNS = String.format("%s %s", KEY_PRODINF, KEY_XMLNS);
52 public static final String KEY_DATA_CONNS = String.format("%s.%s", KEY_DATA, KEY_CONNS);
53 public static final String KEY_DATA_PRODINF = String.format("%s.%s", KEY_DATA, KEY_PRODINF);
54 public static final String KEY_DATA_PORTCONFIG = String.format("%s.%s.%s", KEY_DATA, KEY_PORTCONFIG, KEY_PORT);
55 public static final String KEY_OPM = "opm-power";
56 public static final String KEY_OPM_XMLNS = String.format("%s %s", KEY_OPM, KEY_XMLNS);
Laszlo Papp8cd61fb2017-10-13 16:45:00 +010057 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 +010058 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 +010059 public static final String KEY_VOA = "voa";
60 public static final String KEY_VOA_XMLNS = String.format("%s %s", KEY_VOA, KEY_XMLNS);
61 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 +010062 public static final String OPTICAL_CAPABILITY_PREFIX
63 = "http://www.polatis.com/yang/optical-switch?module=optical-switch&revision=";
64
Laszlo Papp8b87a192017-10-19 18:47:12 +010065 public static final String CFG_MODE_MERGE = "merge";
66
Laszlo Papp8b3a5f62017-10-05 13:32:00 +010067 private static final Logger log = getLogger(PolatisFlowRuleProgrammable.class);
68
69 private PolatisNetconfUtility() {
70 }
71
72 /**
73 * Retrieves session reply information for get operation.
74 *
75 * @param handler parent driver handler
76 * @param filter the filter string of xml content
77 * @return the reply string
78 */
79 public static String netconfGet(DriverHandler handler, String filter) {
80 NetconfSession session = getNetconfSession(handler);
81 String reply;
82 try {
83 reply = session.get(filter, null);
84 } catch (NetconfException e) {
85 throw new RuntimeException(new NetconfException("Failed to retrieve configuration.", e));
86 }
87 return reply;
88 }
89
90 /**
91 * Retrieves session reply information for get config operation.
92 *
93 * @param handler parent driver handler
94 * @param filter the filter string of xml content
95 * @return the reply string
96 */
97 public static String netconfGetConfig(DriverHandler handler, String filter) {
98 NetconfSession session = getNetconfSession(handler);
99 String reply;
100 try {
101 reply = session.getConfig(DatastoreId.RUNNING, filter);
102 } catch (NetconfException e) {
103 throw new RuntimeException(new NetconfException("Failed to retrieve configuration.", e));
104 }
105 return reply;
106 }
107
108 /**
109 * Retrieves session reply information for edit config operation.
110 *
111 * @param handler parent driver handler
112 * @param mode selected mode to change the configuration
113 * @param cfg the new configuration to be set
114 * @return the reply string
115 */
116 public static boolean netconfEditConfig(DriverHandler handler, String mode, String cfg) {
117 NetconfSession session = getNetconfSession(handler);
118 boolean reply = false;
119 try {
120 reply = session.editConfig(DatastoreId.RUNNING, mode, cfg);
121 } catch (NetconfException e) {
122 throw new RuntimeException(new NetconfException("Failed to edit configuration.", e));
123 }
124 return reply;
125 }
126
127 /**
128 * Retrieves specified node hierarchical configuration from the xml information.
129 *
130 * @param content the xml information
131 * @param key the configuration key node
132 * @return the hierarchical configuration, null if exception happens
133 */
134 public static HierarchicalConfiguration configAt(String content, String key) {
135 HierarchicalConfiguration info;
136 try {
137 HierarchicalConfiguration cfg = XmlConfigParser.loadXmlString(content);
138 info = cfg.configurationAt(key);
139 } catch (IllegalArgumentException e) {
140 // Accept null for information polling
141 return null;
142 }
143 return info;
144 }
145
146 /**
147 * Retrieves specified node hierarchical configurations from the xml information.
148 *
149 * @param content the xml information
150 * @param key the configuration key node
151 * @return the hierarchical configurations, empty if exception happens
152 */
153 public static List<HierarchicalConfiguration> configsAt(String content, String key) {
154 List<HierarchicalConfiguration> info;
155 try {
156 HierarchicalConfiguration cfg = XmlConfigParser.loadXmlString(content);
157 info = cfg.configurationsAt(key);
158 } catch (IllegalArgumentException e) {
159 // Accept empty for information polling
160 return ImmutableList.of();
161 }
162 return info;
163 }
164
165 /**
166 * Makes a xml format sentence.
167 *
168 * @param node the node name
169 * @param content the node content
170 * @return the xml format sentence
171 */
172 public static String xml(String node, String content) {
173 return String.format("<%s>%s</%s>", node, content, node);
174 }
175
176 /**
177 * Makes a xml format open tag.
178 *
179 * @param node the node name
180 * @return the xml head format string
181 */
182 public static String xmlOpen(String node) {
183 return String.format("<%s>", node);
184 }
185
186 /**
187 * Makes a xml format close tag.
188 *
189 * @param node the node name
190 * @return the xml end format string
191 */
192 public static String xmlClose(String node) {
193 return String.format("</%s>", node);
194 }
195
196 /**
197 * Makes a xml format empty tag.
198 *
199 * @param node the node name
200 * @return the xml format of empty tag
201 */
202 public static String xmlEmpty(String node) {
203 return String.format("<%s/>", node);
204 }
205
206 public static String opticalRevision(DriverHandler handler) {
207 NetconfSession session = getNetconfSession(handler);
208 Set<String> capabilities = session.getDeviceCapabilitiesSet();
209 for (String c : capabilities) {
210 if (c.startsWith(OPTICAL_CAPABILITY_PREFIX)) {
211 return c.substring(OPTICAL_CAPABILITY_PREFIX.length());
212 }
213 }
214 return null;
215 }
216
217 /**
218 * Returns the NETCONF session of the device.
219 *
220 * @return session
221 */
222 private static NetconfSession getNetconfSession(DriverHandler handler) {
223 NetconfController controller = checkNotNull(handler.get(NetconfController.class));
224 NetconfSession session = controller.getNetconfDevice(handler.data().deviceId()).getSession();
225 if (session == null) {
226 throw new RuntimeException(new NetconfException("Failed to retrieve the netconf device."));
227 }
228 return session;
229 }
230}