blob: 8a6b89a90d4a0a6d8e0052eee261bc05edf226b5 [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);
57 public static final String KEY_POWER = "power";
58 public static final String KEY_DATA_OPM_PORT = String.format("%s.%s.%s", KEY_DATA, KEY_OPM, KEY_PORT);
59 public static final String OPTICAL_CAPABILITY_PREFIX
60 = "http://www.polatis.com/yang/optical-switch?module=optical-switch&revision=";
61
62 private static final Logger log = getLogger(PolatisFlowRuleProgrammable.class);
63
64 private PolatisNetconfUtility() {
65 }
66
67 /**
68 * Retrieves session reply information for get operation.
69 *
70 * @param handler parent driver handler
71 * @param filter the filter string of xml content
72 * @return the reply string
73 */
74 public static String netconfGet(DriverHandler handler, String filter) {
75 NetconfSession session = getNetconfSession(handler);
76 String reply;
77 try {
78 reply = session.get(filter, null);
79 } catch (NetconfException e) {
80 throw new RuntimeException(new NetconfException("Failed to retrieve configuration.", e));
81 }
82 return reply;
83 }
84
85 /**
86 * Retrieves session reply information for get config 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 netconfGetConfig(DriverHandler handler, String filter) {
93 NetconfSession session = getNetconfSession(handler);
94 String reply;
95 try {
96 reply = session.getConfig(DatastoreId.RUNNING, filter);
97 } catch (NetconfException e) {
98 throw new RuntimeException(new NetconfException("Failed to retrieve configuration.", e));
99 }
100 return reply;
101 }
102
103 /**
104 * Retrieves session reply information for edit config operation.
105 *
106 * @param handler parent driver handler
107 * @param mode selected mode to change the configuration
108 * @param cfg the new configuration to be set
109 * @return the reply string
110 */
111 public static boolean netconfEditConfig(DriverHandler handler, String mode, String cfg) {
112 NetconfSession session = getNetconfSession(handler);
113 boolean reply = false;
114 try {
115 reply = session.editConfig(DatastoreId.RUNNING, mode, cfg);
116 } catch (NetconfException e) {
117 throw new RuntimeException(new NetconfException("Failed to edit configuration.", e));
118 }
119 return reply;
120 }
121
122 /**
123 * Retrieves specified node hierarchical configuration from the xml information.
124 *
125 * @param content the xml information
126 * @param key the configuration key node
127 * @return the hierarchical configuration, null if exception happens
128 */
129 public static HierarchicalConfiguration configAt(String content, String key) {
130 HierarchicalConfiguration info;
131 try {
132 HierarchicalConfiguration cfg = XmlConfigParser.loadXmlString(content);
133 info = cfg.configurationAt(key);
134 } catch (IllegalArgumentException e) {
135 // Accept null for information polling
136 return null;
137 }
138 return info;
139 }
140
141 /**
142 * Retrieves specified node hierarchical configurations from the xml information.
143 *
144 * @param content the xml information
145 * @param key the configuration key node
146 * @return the hierarchical configurations, empty if exception happens
147 */
148 public static List<HierarchicalConfiguration> configsAt(String content, String key) {
149 List<HierarchicalConfiguration> info;
150 try {
151 HierarchicalConfiguration cfg = XmlConfigParser.loadXmlString(content);
152 info = cfg.configurationsAt(key);
153 } catch (IllegalArgumentException e) {
154 // Accept empty for information polling
155 return ImmutableList.of();
156 }
157 return info;
158 }
159
160 /**
161 * Makes a xml format sentence.
162 *
163 * @param node the node name
164 * @param content the node content
165 * @return the xml format sentence
166 */
167 public static String xml(String node, String content) {
168 return String.format("<%s>%s</%s>", node, content, node);
169 }
170
171 /**
172 * Makes a xml format open tag.
173 *
174 * @param node the node name
175 * @return the xml head format string
176 */
177 public static String xmlOpen(String node) {
178 return String.format("<%s>", node);
179 }
180
181 /**
182 * Makes a xml format close tag.
183 *
184 * @param node the node name
185 * @return the xml end format string
186 */
187 public static String xmlClose(String node) {
188 return String.format("</%s>", node);
189 }
190
191 /**
192 * Makes a xml format empty tag.
193 *
194 * @param node the node name
195 * @return the xml format of empty tag
196 */
197 public static String xmlEmpty(String node) {
198 return String.format("<%s/>", node);
199 }
200
201 public static String opticalRevision(DriverHandler handler) {
202 NetconfSession session = getNetconfSession(handler);
203 Set<String> capabilities = session.getDeviceCapabilitiesSet();
204 for (String c : capabilities) {
205 if (c.startsWith(OPTICAL_CAPABILITY_PREFIX)) {
206 return c.substring(OPTICAL_CAPABILITY_PREFIX.length());
207 }
208 }
209 return null;
210 }
211
212 /**
213 * Returns the NETCONF session of the device.
214 *
215 * @return session
216 */
217 private static NetconfSession getNetconfSession(DriverHandler handler) {
218 NetconfController controller = checkNotNull(handler.get(NetconfController.class));
219 NetconfSession session = controller.getNetconfDevice(handler.data().deviceId()).getSession();
220 if (session == null) {
221 throw new RuntimeException(new NetconfException("Failed to retrieve the netconf device."));
222 }
223 return session;
224 }
225}