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