blob: 4fffde64bc8b4ba80954525ac5d89c8a0d9fb382 [file] [log] [blame]
andreaeb70a942015-10-16 21:34:46 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
andreaeb70a942015-10-16 21:34:46 -07003 *
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.netconf;
18
Akihiro Yamanouchi45122222016-07-15 13:13:11 +090019import com.google.common.annotations.Beta;
Andrea Campanellac3627842017-04-04 18:06:54 +020020import org.slf4j.Logger;
21import org.slf4j.LoggerFactory;
Andrei Mihaescuac542ca2017-03-26 21:36:25 +030022
Aaron Kruglikov72db6422017-02-13 12:16:51 -080023import java.util.LinkedHashSet;
andreaeb70a942015-10-16 21:34:46 -070024import java.util.List;
Aaron Kruglikov72db6422017-02-13 12:16:51 -080025import java.util.Set;
Andrea Campanella101417d2015-12-11 17:58:07 -080026import java.util.concurrent.CompletableFuture;
Aaron Kruglikov72db6422017-02-13 12:16:51 -080027import java.util.regex.Matcher;
28import java.util.regex.Pattern;
andreaeb70a942015-10-16 21:34:46 -070029
30/**
31 * NETCONF session object that allows NETCONF operations on top with the physical
32 * device on top of an SSH connection.
33 */
Sean Condond2c8d472017-02-17 17:09:39 +000034// TODO change return type of methods to <Capability, XMLdoc, string or yang obj>
andreaeb70a942015-10-16 21:34:46 -070035public interface NetconfSession {
36
37 /**
Andrea Campanella101417d2015-12-11 17:58:07 -080038 * Executes an asynchronous RPC to the server and obtains a future to be completed.
39 *
Sean Condond2c8d472017-02-17 17:09:39 +000040 * The caller must ensure that the message-id in any request is unique
41 * for the session
42 *
43 * @deprecated - 1.10.0 do not remove needs reworking
Andrea Campanella101417d2015-12-11 17:58:07 -080044 * @param request the XML containing the RPC for the server.
45 * @return Server response or ERROR
46 * @throws NetconfException when there is a problem in the communication process on
47 * the underlying connection
48 */
Sean Condond2c8d472017-02-17 17:09:39 +000049 @Deprecated
Andrea Campanella101417d2015-12-11 17:58:07 -080050 CompletableFuture<String> request(String request) throws NetconfException;
51
52
53 /**
Sean Condond2c8d472017-02-17 17:09:39 +000054 * Retrieves the requested configuration, different from get-config.
Andrea Campanellaf4fd0352015-12-14 17:03:05 -080055 *
andreaeb70a942015-10-16 21:34:46 -070056 * @param request the XML containing the request to the server.
57 * @return device running configuration
Andrea Campanella101417d2015-12-11 17:58:07 -080058 * @throws NetconfException when there is a problem in the communication process on
59 * the underlying connection
andreaeb70a942015-10-16 21:34:46 -070060 */
Andrea Campanella101417d2015-12-11 17:58:07 -080061 String get(String request) throws NetconfException;
andreaeb70a942015-10-16 21:34:46 -070062
63 /**
Sean Condond2c8d472017-02-17 17:09:39 +000064 * Retrieves the requested data.
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +090065 *
66 * @param filterSchema XML subtrees to include in the reply
67 * @param withDefaultsMode with-defaults mode
68 * @return Server response
69 * @throws NetconfException when there is a problem in the communication process on
70 * the underlying connection
71 */
72 String get(String filterSchema, String withDefaultsMode)
73 throws NetconfException;
74
75 /**
Akihiro Yamanouchi45122222016-07-15 13:13:11 +090076 * Executes an synchronous RPC to the server and wrap the request in RPC header.
Akihiro Yamanouchi8d3a9d32016-07-12 11:41:44 +090077 *
78 * @param request the XML containing the request to the server.
79 * @return Server response or ERROR
80 * @throws NetconfException when there is a problem in the communication process on
81 * the underlying connection
82 */
83 String doWrappedRpc(String request) throws NetconfException;
84
85 /**
Andrea Campanella101417d2015-12-11 17:58:07 -080086 * Executes an synchronous RPC to the server.
Andrea Campanellaf4fd0352015-12-14 17:03:05 -080087 *
andreaeb70a942015-10-16 21:34:46 -070088 * @param request the XML containing the RPC for the server.
89 * @return Server response or ERROR
Andrea Campanella101417d2015-12-11 17:58:07 -080090 * @throws NetconfException when there is a problem in the communication process on
91 * the underlying connection
andreaeb70a942015-10-16 21:34:46 -070092 */
Andrea Campanella101417d2015-12-11 17:58:07 -080093 String requestSync(String request) throws NetconfException;
andreaeb70a942015-10-16 21:34:46 -070094
95 /**
96 * Retrives the specified configuration.
97 *
Andrei Mihaescuac542ca2017-03-26 21:36:25 +030098 * @param netconfTargetConfig the type of configuration to retrieve.
andreaeb70a942015-10-16 21:34:46 -070099 * @return specified configuration.
Andrea Campanella101417d2015-12-11 17:58:07 -0800100 * @throws NetconfException when there is a problem in the communication process on
101 * the underlying connection
andreaeb70a942015-10-16 21:34:46 -0700102 */
Andrei Mihaescuac542ca2017-03-26 21:36:25 +0300103 String getConfig(TargetConfig netconfTargetConfig) throws NetconfException;
104
105 /**
106 * Retrives the specified configuration.
107 *
108 * @param netconfTargetConfig the type of configuration to retrieve.
109 * @return specified configuration.
110 * @throws NetconfException when there is a problem in the communication process on
111 * the underlying connection
112 * @deprecated - 1.10.0 Kingfisher use method overload that accepts
113 * org.onosproject.netconf.TargetConfig enum parameter instead
114 */
115 @Deprecated
116 String getConfig(String netconfTargetConfig) throws NetconfException;
117
andreaeb70a942015-10-16 21:34:46 -0700118
119 /**
120 * Retrives part of the specivied configuration based on the filterSchema.
121 *
Andrei Mihaescuac542ca2017-03-26 21:36:25 +0300122 * @param netconfTargetConfig the type of configuration to retrieve.
123 * @param configurationFilterSchema XML schema to filter the configuration
124 * elements we are interested in
125 * @return device running configuration.
126 * @throws NetconfException when there is a problem in the communication process on
127 * the underlying connection
128 * @deprecated - 1.10.0 Kingfisher use method overload that accepts
129 * org.onosproject.netconf.TargetConfig enum parameter instead
130 */
131 @Deprecated
132 String getConfig(String netconfTargetConfig, String configurationFilterSchema)
133 throws NetconfException;
134
135 /**
136 * Retrives part of the specivied configuration based on the filterSchema.
137 *
138 * @param netconfTargetConfig the type of configuration to retrieve.
andreaeb70a942015-10-16 21:34:46 -0700139 * @param configurationFilterSchema XML schema to filter the configuration
140 * elements we are interested in
141 * @return device running configuration.
Andrea Campanella101417d2015-12-11 17:58:07 -0800142 * @throws NetconfException when there is a problem in the communication process on
143 * the underlying connection
andreaeb70a942015-10-16 21:34:46 -0700144 */
Andrei Mihaescuac542ca2017-03-26 21:36:25 +0300145 String getConfig(TargetConfig netconfTargetConfig, String configurationFilterSchema)
Andrea Campanella101417d2015-12-11 17:58:07 -0800146 throws NetconfException;
andreaeb70a942015-10-16 21:34:46 -0700147
Andrei Mihaescuac542ca2017-03-26 21:36:25 +0300148
andreaeb70a942015-10-16 21:34:46 -0700149 /**
150 * Retrives part of the specified configuration based on the filterSchema.
151 *
152 * @param newConfiguration configuration to set
153 * @return true if the configuration was edited correctly
Andrea Campanella101417d2015-12-11 17:58:07 -0800154 * @throws NetconfException when there is a problem in the communication process on
155 * the underlying connection
andreaeb70a942015-10-16 21:34:46 -0700156 */
157
Andrea Campanella101417d2015-12-11 17:58:07 -0800158 boolean editConfig(String newConfiguration) throws NetconfException;
andreaeb70a942015-10-16 21:34:46 -0700159
160 /**
Andrea Campanellaf4fd0352015-12-14 17:03:05 -0800161 * Retrives part of the specified configuration based on the filterSchema.
Andrea Campanella101417d2015-12-11 17:58:07 -0800162 *
Andrei Mihaescuac542ca2017-03-26 21:36:25 +0300163 * @param netconfTargetConfig the targetConfiguration to change
164 * @param mode selected mode to change the configuration
165 * @param newConfiguration configuration to set
166 * @return true if the configuration was edited correctly
167 * @throws NetconfException when there is a problem in the communication process on
168 * the underlying connection
169 * @deprecated - 1.10.0 Kingfisher use method overload that accepts
170 * org.onosproject.netconf.TargetConfig enum parameter instead
171 */
172 @Deprecated
173 boolean editConfig(String netconfTargetConfig, String mode, String newConfiguration)
174 throws NetconfException;
175
176 /**
177 * Retrives part of the specified configuration based on the filterSchema.
178 *
179 * @param netconfTargetConfig the targetConfiguration to change
Andrea Campanella101417d2015-12-11 17:58:07 -0800180 * @param mode selected mode to change the configuration
181 * @param newConfiguration configuration to set
Andrea Campanellaf4fd0352015-12-14 17:03:05 -0800182 * @return true if the configuration was edited correctly
Andrea Campanella101417d2015-12-11 17:58:07 -0800183 * @throws NetconfException when there is a problem in the communication process on
184 * the underlying connection
Andrea Campanellaf4fd0352015-12-14 17:03:05 -0800185 */
Andrei Mihaescuac542ca2017-03-26 21:36:25 +0300186 boolean editConfig(TargetConfig netconfTargetConfig, String mode, String newConfiguration)
Andrea Campanella101417d2015-12-11 17:58:07 -0800187 throws NetconfException;
Andrea Campanellaf4fd0352015-12-14 17:03:05 -0800188
189 /**
andreaeb70a942015-10-16 21:34:46 -0700190 * Copies the new configuration, an Url or a complete configuration xml tree
191 * to the target configuration.
192 * The target configuration can't be the running one
193 *
Andrei Mihaescuac542ca2017-03-26 21:36:25 +0300194 * @param netconfTargetConfig the type of configuration to retrieve.
195 * @param newConfiguration configuration to set
196 * @return true if the configuration was copied correctly
197 * @throws NetconfException when there is a problem in the communication process on
198 * the underlying connection
199 * @deprecated - 1.10.0 Kingfisher use method overload that accepts
200 * org.onosproject.netconf.TargetConfig enum parameter instead
201 */
202 @Deprecated
203 boolean copyConfig(String netconfTargetConfig, String newConfiguration)
204 throws NetconfException;
205
206 /**
207 * Copies the new configuration, an Url or a complete configuration xml tree
208 * to the target configuration.
209 * The target configuration can't be the running one
210 *
211 * @param netconfTargetConfig the type of configuration to retrieve.
andreaeb70a942015-10-16 21:34:46 -0700212 * @param newConfiguration configuration to set
213 * @return true if the configuration was copied correctly
Andrea Campanella101417d2015-12-11 17:58:07 -0800214 * @throws NetconfException when there is a problem in the communication process on
215 * the underlying connection
andreaeb70a942015-10-16 21:34:46 -0700216 */
Andrei Mihaescuac542ca2017-03-26 21:36:25 +0300217 boolean copyConfig(TargetConfig netconfTargetConfig, String newConfiguration)
Andrea Campanella101417d2015-12-11 17:58:07 -0800218 throws NetconfException;
andreaeb70a942015-10-16 21:34:46 -0700219
220 /**
221 * Deletes part of the specified configuration based on the filterSchema.
222 *
Andrei Mihaescuac542ca2017-03-26 21:36:25 +0300223 * @param netconfTargetConfig the name of the configuration to delete
224 * @return true if the configuration was copied correctly
225 * @throws NetconfException when there is a problem in the communication process on
226 * the underlying connection
227 * @deprecated - 1.10.0 Kingfisher use method overload that accepts
228 * org.onosproject.netconf.TargetConfig enum parameter instead
229 */
230 @Deprecated
231 boolean deleteConfig(String netconfTargetConfig) throws NetconfException;
232
233 /**
234 * Deletes part of the specified configuration based on the filterSchema.
235 *
236 * @param netconfTargetConfig the name of the configuration to delete
andreaeb70a942015-10-16 21:34:46 -0700237 * @return true if the configuration was copied correctly
Andrea Campanella101417d2015-12-11 17:58:07 -0800238 * @throws NetconfException when there is a problem in the communication process on
239 * the underlying connection
andreaeb70a942015-10-16 21:34:46 -0700240 */
Andrei Mihaescuac542ca2017-03-26 21:36:25 +0300241 boolean deleteConfig(TargetConfig netconfTargetConfig) throws NetconfException;
andreaeb70a942015-10-16 21:34:46 -0700242
243 /**
helenyrwu0407c642016-06-09 12:01:30 -0700244 * Starts subscription to the device's notifications.
245 *
246 * @throws NetconfException when there is a problem starting the subscription
247 */
248 void startSubscription() throws NetconfException;
249
250 /**
Akihiro Yamanouchi45122222016-07-15 13:13:11 +0900251 * Starts subscription to the device's notifications.
252 *
253 * @param filterSchema XML subtrees to indicate specific notification
254 * @throws NetconfException when there is a problem starting the subscription
255 */
256 @Beta
257 void startSubscription(String filterSchema) throws NetconfException;
258
259 /**
helenyrwu0407c642016-06-09 12:01:30 -0700260 * Ends subscription to the device's notifications.
261 *
262 * @throws NetconfException when there is a problem ending the subscription
263 */
264 void endSubscription() throws NetconfException;
265
266 /**
267 * Locks the specified configuration.
268 *
269 * @param configType type of configuration to be locked
270 * @return true if successful.
271 * @throws NetconfException when there is a problem in the communication process on
272 * the underlying connection
273 */
274 boolean lock(String configType) throws NetconfException;
275
276 /**
277 * Unlocks the specified configuration.
278 *
279 * @param configType type of configuration to be locked
280 * @return true if successful.
281 * @throws NetconfException when there is a problem in the communication process on
282 * the underlying connection
283 */
284 boolean unlock(String configType) throws NetconfException;
285
286 /**
287 * Locks the running configuration.
andreaeb70a942015-10-16 21:34:46 -0700288 *
289 * @return true if successful.
Andrea Campanella101417d2015-12-11 17:58:07 -0800290 * @throws NetconfException when there is a problem in the communication process on
291 * the underlying connection
andreaeb70a942015-10-16 21:34:46 -0700292 */
Andrea Campanella101417d2015-12-11 17:58:07 -0800293 boolean lock() throws NetconfException;
andreaeb70a942015-10-16 21:34:46 -0700294
295 /**
helenyrwu0407c642016-06-09 12:01:30 -0700296 * Unlocks the running configuration.
andreaeb70a942015-10-16 21:34:46 -0700297 *
298 * @return true if successful.
Andrea Campanella101417d2015-12-11 17:58:07 -0800299 * @throws NetconfException when there is a problem in the communication process on
300 * the underlying connection
andreaeb70a942015-10-16 21:34:46 -0700301 */
Andrea Campanella101417d2015-12-11 17:58:07 -0800302 boolean unlock() throws NetconfException;
andreaeb70a942015-10-16 21:34:46 -0700303
304 /**
305 * Closes the Netconf session with the device.
306 * the first time it tries gracefully, then kills it forcefully
Andrea Campanellaf4fd0352015-12-14 17:03:05 -0800307 *
andreaeb70a942015-10-16 21:34:46 -0700308 * @return true if closed
Andrea Campanella101417d2015-12-11 17:58:07 -0800309 * @throws NetconfException when there is a problem in the communication process on
310 * the underlying connection
andreaeb70a942015-10-16 21:34:46 -0700311 */
Andrea Campanella101417d2015-12-11 17:58:07 -0800312 boolean close() throws NetconfException;
andreaeb70a942015-10-16 21:34:46 -0700313
314 /**
315 * Gets the session ID of the Netconf session.
316 *
317 * @return Session ID as a string.
318 */
319 String getSessionId();
320
321 /**
Aaron Kruglikov72db6422017-02-13 12:16:51 -0800322 * Gets the capabilities of the remote Netconf device associated to this
323 * session.
324 *
325 * @return Network capabilities as strings in a Set.
326 *
327 * @since 1.10.0
328 * Note: default implementation provided with the interface
329 * will be removed when {@code getServerCapabilities()} reaches
330 * deprecation grace period.
331 */
332 default Set<String> getDeviceCapabilitiesSet() {
333 // default implementation should be removed in the future
334 Set<String> capabilities = new LinkedHashSet<>();
335 Matcher capabilityMatcher =
336 Pattern.compile("<capability>\\s*(.*?)\\s*</capability>")
337 .matcher(getServerCapabilities());
338 while (capabilityMatcher.find()) {
339 capabilities.add(capabilityMatcher.group(1));
340 }
341 return capabilities;
342 }
343
344 /**
345 * Gets the capabilities of the Netconf server (remote device) associated
346 * to this session.
andreaeb70a942015-10-16 21:34:46 -0700347 *
348 * @return Network capabilities as a string.
Aaron Kruglikov72db6422017-02-13 12:16:51 -0800349 * @deprecated 1.10.0 use {@link #getDeviceCapabilitiesSet()} instead
andreaeb70a942015-10-16 21:34:46 -0700350 */
Aaron Kruglikov72db6422017-02-13 12:16:51 -0800351 @Deprecated
andreaeb70a942015-10-16 21:34:46 -0700352 String getServerCapabilities();
353
354 /**
Andrea Campanella101417d2015-12-11 17:58:07 -0800355 * Sets the ONOS side capabilities.
Andrea Campanellaf4fd0352015-12-14 17:03:05 -0800356 *
andreaeb70a942015-10-16 21:34:46 -0700357 * @param capabilities list of capabilities the device has.
Aaron Kruglikov72db6422017-02-13 12:16:51 -0800358 * @deprecated 1.10.0 use {@link #setOnosCapabilities(Iterable)} instead
andreaeb70a942015-10-16 21:34:46 -0700359 */
Aaron Kruglikov72db6422017-02-13 12:16:51 -0800360 @Deprecated
andreaeb70a942015-10-16 21:34:46 -0700361 void setDeviceCapabilities(List<String> capabilities);
362
Andrea Campanella101417d2015-12-11 17:58:07 -0800363 /**
Andrea Campanellac3627842017-04-04 18:06:54 +0200364 * Checks the state of the underlying SSH session and connection
365 * and if necessary it reestablishes it.
366 * Should be implemented, providing a default here for retrocompatibility.
367 * @throws NetconfException when there is a problem in reestablishing
368 * the connection or the session to the device.
369 */
370
371 default void checkAndReestablish() throws NetconfException {
372 Logger log = LoggerFactory.getLogger(NetconfSession.class);
373 log.error("Not implemented/exposed by the underlying session implementation");
374 }
Aaron Kruglikov72db6422017-02-13 12:16:51 -0800375 /**
376 * Sets the ONOS side capabilities.
377 *
378 * @param capabilities list of capabilities ONOS has.
379 *
380 * @since 1.10.0
381 */
382 default void setOnosCapabilities(Iterable<String> capabilities) {
383 // default implementation should be removed in the future
384 // no-op
385 }
Andrea Campanellac3627842017-04-04 18:06:54 +0200386
387 /**
Andrea Campanella101417d2015-12-11 17:58:07 -0800388 * Remove a listener from the underlying stream handler implementation.
389 *
390 * @param listener event listener.
391 */
392 void addDeviceOutputListener(NetconfDeviceOutputEventListener listener);
393
394 /**
395 * Remove a listener from the underlying stream handler implementation.
396 *
397 * @param listener event listener.
398 */
399 void removeDeviceOutputListener(NetconfDeviceOutputEventListener listener);
400
andreaeb70a942015-10-16 21:34:46 -0700401}