blob: 0711137b11c249c03c3c4c52d59d4d75789dd8db [file] [log] [blame]
andreaeb70a942015-10-16 21:34:46 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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
Yuta HIGUCHI5ac34432018-02-13 16:29:15 -080023import static com.google.common.base.Preconditions.checkNotNull;
24
Aaron Kruglikov72db6422017-02-13 12:16:51 -080025import java.util.Set;
Andrea Campanella101417d2015-12-11 17:58:07 -080026import java.util.concurrent.CompletableFuture;
andreaeb70a942015-10-16 21:34:46 -070027
28/**
29 * NETCONF session object that allows NETCONF operations on top with the physical
30 * device on top of an SSH connection.
31 */
Sean Condond2c8d472017-02-17 17:09:39 +000032// TODO change return type of methods to <Capability, XMLdoc, string or yang obj>
andreaeb70a942015-10-16 21:34:46 -070033public interface NetconfSession {
34
35 /**
Andrea Campanella101417d2015-12-11 17:58:07 -080036 * Executes an asynchronous RPC to the server and obtains a future to be completed.
37 *
Sean Condond2c8d472017-02-17 17:09:39 +000038 * The caller must ensure that the message-id in any request is unique
39 * for the session
40 *
41 * @deprecated - 1.10.0 do not remove needs reworking
Andrea Campanella101417d2015-12-11 17:58:07 -080042 * @param request the XML containing the RPC for the server.
43 * @return Server response or ERROR
44 * @throws NetconfException when there is a problem in the communication process on
45 * the underlying connection
46 */
Sean Condond2c8d472017-02-17 17:09:39 +000047 @Deprecated
Andrea Campanella101417d2015-12-11 17:58:07 -080048 CompletableFuture<String> request(String request) throws NetconfException;
49
Yuta HIGUCHI6e6c26e2017-09-06 14:25:57 -070050 /**
51 * Executes an asynchronous RPC request to the server and obtains a future
52 * for it's response.
53 *
54 * @param request the XML containing the RPC request for the server.
55 * @return Server response or ERROR
56 * @throws NetconfException when there is a problem in the communication process on
57 * the underlying connection
58 * @throws NetconfTransportException on secure transport-layer error
59 */
60 default CompletableFuture<String> rpc(String request) throws NetconfException {
61 return request(request);
62 }
63
Yuta HIGUCHI5ac34432018-02-13 16:29:15 -080064 /**
65 * Retrieves the specified configuration.
66 *
67 * @param datastore to retrieve configuration from
68 * @return specified configuration
69 *
70 * @throws NetconfException when there is a problem in the communication process on
71 * the underlying connection
72 */
73 default CompletableFuture<CharSequence> asyncGetConfig(DatastoreId datastore) throws NetconfException {
74 StringBuilder rpc = new StringBuilder();
75 rpc.append("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n");
76 rpc.append("<get-config>\n");
77 rpc.append("<source>\n");
78 rpc.append('<').append(checkNotNull(datastore)).append("/>");
79 rpc.append("</source>");
80 // filter here
81 rpc.append("</get-config>\n");
82 rpc.append("</rpc>");
83
84 return rpc(rpc.toString())
85 .thenApply(msg -> {
86 // crude way of removing rpc-reply envelope
87 int begin = msg.indexOf("<data>");
88 int end = msg.lastIndexOf("</data>");
89 if (begin != -1 && end != -1) {
Yuta HIGUCHIf7333732018-05-14 16:07:37 -070090 return msg.subSequence(begin, end + "</data>".length());
Yuta HIGUCHI5ac34432018-02-13 16:29:15 -080091 } else {
92 // FIXME probably should exceptionally fail here.
93 return msg;
94 }
95 });
96 }
97
98 /**
99 * Retrieves running configuration and device state.
100 *
101 * @return running configuration and device state
102 *
103 * @throws NetconfException when there is a problem in the communication process on
104 * the underlying connection
105 */
106 default CompletableFuture<CharSequence> asyncGet() throws NetconfException {
107 StringBuilder rpc = new StringBuilder();
108 rpc.append("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n");
109 rpc.append("<get>\n");
110 // filter here
111 rpc.append("</get>\n");
112 rpc.append("</rpc>");
113 return rpc(rpc.toString())
114 .thenApply(msg -> {
115 // crude way of removing rpc-reply envelope
116 int begin = msg.indexOf("<data>");
117 int end = msg.lastIndexOf("</data>");
118 if (begin != -1 && end != -1) {
Yuta HIGUCHIf7333732018-05-14 16:07:37 -0700119 return msg.subSequence(begin, end + "</data>".length());
Yuta HIGUCHI5ac34432018-02-13 16:29:15 -0800120 } else {
121 // FIXME probably should exceptionally fail here.
122 return msg;
123 }
124 });
125
126 }
127
Andrea Campanella101417d2015-12-11 17:58:07 -0800128
129 /**
Sean Condond2c8d472017-02-17 17:09:39 +0000130 * Retrieves the requested configuration, different from get-config.
Andrea Campanellaf4fd0352015-12-14 17:03:05 -0800131 *
andreaeb70a942015-10-16 21:34:46 -0700132 * @param request the XML containing the request to the server.
133 * @return device running configuration
Andrea Campanella101417d2015-12-11 17:58:07 -0800134 * @throws NetconfException when there is a problem in the communication process on
135 * the underlying connection
andreaeb70a942015-10-16 21:34:46 -0700136 */
Andrea Campanella101417d2015-12-11 17:58:07 -0800137 String get(String request) throws NetconfException;
andreaeb70a942015-10-16 21:34:46 -0700138
139 /**
Sean Condond2c8d472017-02-17 17:09:39 +0000140 * Retrieves the requested data.
Akihiro Yamanouchi5e5d4df2016-06-08 17:06:33 +0900141 *
142 * @param filterSchema XML subtrees to include in the reply
143 * @param withDefaultsMode with-defaults mode
144 * @return Server response
145 * @throws NetconfException when there is a problem in the communication process on
146 * the underlying connection
147 */
148 String get(String filterSchema, String withDefaultsMode)
149 throws NetconfException;
150
151 /**
Akihiro Yamanouchi45122222016-07-15 13:13:11 +0900152 * Executes an synchronous RPC to the server and wrap the request in RPC header.
Akihiro Yamanouchi8d3a9d32016-07-12 11:41:44 +0900153 *
154 * @param request the XML containing the request to the server.
155 * @return Server response or ERROR
156 * @throws NetconfException when there is a problem in the communication process on
157 * the underlying connection
158 */
159 String doWrappedRpc(String request) throws NetconfException;
160
161 /**
Andrea Campanella101417d2015-12-11 17:58:07 -0800162 * Executes an synchronous RPC to the server.
Andrea Campanellaf4fd0352015-12-14 17:03:05 -0800163 *
andreaeb70a942015-10-16 21:34:46 -0700164 * @param request the XML containing the RPC for the server.
165 * @return Server response or ERROR
Andrea Campanella101417d2015-12-11 17:58:07 -0800166 * @throws NetconfException when there is a problem in the communication process on
167 * the underlying connection
andreaeb70a942015-10-16 21:34:46 -0700168 */
Andrea Campanella101417d2015-12-11 17:58:07 -0800169 String requestSync(String request) throws NetconfException;
andreaeb70a942015-10-16 21:34:46 -0700170
171 /**
Yuta HIGUCHI89111d92017-05-04 11:29:17 -0700172 * Retrieves the specified configuration.
andreaeb70a942015-10-16 21:34:46 -0700173 *
Andrei Mihaescuac542ca2017-03-26 21:36:25 +0300174 * @param netconfTargetConfig the type of configuration to retrieve.
andreaeb70a942015-10-16 21:34:46 -0700175 * @return specified configuration.
Andrea Campanella101417d2015-12-11 17:58:07 -0800176 * @throws NetconfException when there is a problem in the communication process on
177 * the underlying connection
Yuta HIGUCHI5ac34432018-02-13 16:29:15 -0800178 *
179 * @deprecated in 1.13.0 use async version instead.
andreaeb70a942015-10-16 21:34:46 -0700180 */
Yuta HIGUCHI5ac34432018-02-13 16:29:15 -0800181 @Deprecated
Yuta HIGUCHIdd7c3f82017-09-03 14:18:01 -0700182 String getConfig(DatastoreId netconfTargetConfig) throws NetconfException;
andreaeb70a942015-10-16 21:34:46 -0700183
184 /**
Yuta HIGUCHI89111d92017-05-04 11:29:17 -0700185 * Retrieves part of the specified configuration based on the filterSchema.
186 *
187 * @param netconfTargetConfig the type of configuration to retrieve.
188 * @param configurationFilterSchema XML schema to filter the configuration
189 * elements we are interested in
190 * @return device running configuration.
191 * @throws NetconfException when there is a problem in the communication process on
192 * the underlying connection
193 */
Yuta HIGUCHIdd7c3f82017-09-03 14:18:01 -0700194 String getConfig(DatastoreId netconfTargetConfig,
Yuta HIGUCHI89111d92017-05-04 11:29:17 -0700195 String configurationFilterSchema)
Yuta HIGUCHIdd7c3f82017-09-03 14:18:01 -0700196 throws NetconfException;
Andrei Mihaescuac542ca2017-03-26 21:36:25 +0300197
andreaeb70a942015-10-16 21:34:46 -0700198 /**
Yuta HIGUCHI89111d92017-05-04 11:29:17 -0700199 * Retrieves part of the specified configuration based on the filterSchema.
andreaeb70a942015-10-16 21:34:46 -0700200 *
201 * @param newConfiguration configuration to set
202 * @return true if the configuration was edited correctly
Andrea Campanella101417d2015-12-11 17:58:07 -0800203 * @throws NetconfException when there is a problem in the communication process on
204 * the underlying connection
andreaeb70a942015-10-16 21:34:46 -0700205 */
Andrea Campanella101417d2015-12-11 17:58:07 -0800206 boolean editConfig(String newConfiguration) throws NetconfException;
andreaeb70a942015-10-16 21:34:46 -0700207
208 /**
Yuta HIGUCHI89111d92017-05-04 11:29:17 -0700209 * Retrieves part of the specified configuration based on the filterSchema.
210 *
211 * @param netconfTargetConfig the targetConfiguration to change
Yuta HIGUCHIe34c9c22017-12-04 17:47:14 -0800212 * @param mode default-operation mode
Yuta HIGUCHI89111d92017-05-04 11:29:17 -0700213 * @param newConfiguration configuration to set
214 * @return true if the configuration was edited correctly
215 * @throws NetconfException when there is a problem in the communication process on
216 * the underlying connection
217 */
Yuta HIGUCHIdd7c3f82017-09-03 14:18:01 -0700218 boolean editConfig(DatastoreId netconfTargetConfig, String mode, String newConfiguration)
219 throws NetconfException;
Yuta HIGUCHI89111d92017-05-04 11:29:17 -0700220
221 /**
222 * Copies the configuration between configuration datastores.
223 * <p>
224 * The target configuration can't be the running one
225 *
226 * @param destination configuration datastore
227 * @param source configuration datastore
228 * @return true if the configuration was copied correctly
229 * @throws NetconfException when there is a problem in the communication process on
230 * the underlying connection
231 */
Yuta HIGUCHIdd7c3f82017-09-03 14:18:01 -0700232 boolean copyConfig(DatastoreId destination, DatastoreId source)
233 throws NetconfException;
Yuta HIGUCHI89111d92017-05-04 11:29:17 -0700234
235 /**
236 * Copies the new configuration, an Url or a complete configuration xml tree
237 * to the target configuration.
238 * The target configuration can't be the running one
239 *
240 * @param netconfTargetConfig the type of configuration to retrieve.
241 * @param newConfiguration configuration XML to set or URL tag to the configuration
242 * @return true if the configuration was copied correctly
243 * @throws NetconfException when there is a problem in the communication process on
244 * the underlying connection
245 */
Yuta HIGUCHIdd7c3f82017-09-03 14:18:01 -0700246 boolean copyConfig(DatastoreId netconfTargetConfig, String newConfiguration)
247 throws NetconfException;
Andrea Campanellaf4fd0352015-12-14 17:03:05 -0800248
249 /**
andreaeb70a942015-10-16 21:34:46 -0700250 * Copies the new configuration, an Url or a complete configuration xml tree
251 * to the target configuration.
252 * The target configuration can't be the running one
253 *
Andrei Mihaescuac542ca2017-03-26 21:36:25 +0300254 * @param netconfTargetConfig the type of configuration to retrieve.
255 * @param newConfiguration configuration to set
256 * @return true if the configuration was copied correctly
257 * @throws NetconfException when there is a problem in the communication process on
258 * the underlying connection
Andrei Mihaescuac542ca2017-03-26 21:36:25 +0300259 */
Andrei Mihaescuac542ca2017-03-26 21:36:25 +0300260 boolean copyConfig(String netconfTargetConfig, String newConfiguration)
261 throws NetconfException;
262
263 /**
Yuta HIGUCHI89111d92017-05-04 11:29:17 -0700264 * Deletes part of the specified configuration based on the filterSchema.
265 *
266 * @param netconfTargetConfig the name of the configuration to delete
267 * @return true if the configuration was copied correctly
268 * @throws NetconfException when there is a problem in the communication process on
269 * the underlying connection
270 */
Yuta HIGUCHIdd7c3f82017-09-03 14:18:01 -0700271 boolean deleteConfig(DatastoreId netconfTargetConfig) throws NetconfException;
andreaeb70a942015-10-16 21:34:46 -0700272
273 /**
helenyrwu0407c642016-06-09 12:01:30 -0700274 * Starts subscription to the device's notifications.
275 *
276 * @throws NetconfException when there is a problem starting the subscription
277 */
278 void startSubscription() throws NetconfException;
279
280 /**
Akihiro Yamanouchi45122222016-07-15 13:13:11 +0900281 * Starts subscription to the device's notifications.
282 *
283 * @param filterSchema XML subtrees to indicate specific notification
284 * @throws NetconfException when there is a problem starting the subscription
285 */
286 @Beta
287 void startSubscription(String filterSchema) throws NetconfException;
288
289 /**
helenyrwu0407c642016-06-09 12:01:30 -0700290 * Ends subscription to the device's notifications.
291 *
292 * @throws NetconfException when there is a problem ending the subscription
293 */
294 void endSubscription() throws NetconfException;
295
296 /**
297 * Locks the specified configuration.
298 *
Yuta HIGUCHI89111d92017-05-04 11:29:17 -0700299 * @param datastore configuration datastore to be locked
helenyrwu0407c642016-06-09 12:01:30 -0700300 * @return true if successful.
301 * @throws NetconfException when there is a problem in the communication process on
302 * the underlying connection
303 */
Yuta HIGUCHIdd7c3f82017-09-03 14:18:01 -0700304 boolean lock(DatastoreId datastore) throws NetconfException;
Yuta HIGUCHI89111d92017-05-04 11:29:17 -0700305
306 /**
307 * Unlocks the specified configuration.
308 *
309 * @param datastore configuration datastore to be unlocked
310 * @return true if successful.
311 * @throws NetconfException when there is a problem in the communication process on
312 * the underlying connection
313 */
Yuta HIGUCHIdd7c3f82017-09-03 14:18:01 -0700314 boolean unlock(DatastoreId datastore) throws NetconfException;
helenyrwu0407c642016-06-09 12:01:30 -0700315
316 /**
317 * Locks the running configuration.
andreaeb70a942015-10-16 21:34:46 -0700318 *
319 * @return true if successful.
Andrea Campanella101417d2015-12-11 17:58:07 -0800320 * @throws NetconfException when there is a problem in the communication process on
321 * the underlying connection
andreaeb70a942015-10-16 21:34:46 -0700322 */
Yuta HIGUCHI89111d92017-05-04 11:29:17 -0700323 default boolean lock() throws NetconfException {
324 return lock(DatastoreId.RUNNING);
325 }
andreaeb70a942015-10-16 21:34:46 -0700326
327 /**
helenyrwu0407c642016-06-09 12:01:30 -0700328 * Unlocks the running configuration.
andreaeb70a942015-10-16 21:34:46 -0700329 *
330 * @return true if successful.
Andrea Campanella101417d2015-12-11 17:58:07 -0800331 * @throws NetconfException when there is a problem in the communication process on
332 * the underlying connection
andreaeb70a942015-10-16 21:34:46 -0700333 */
Yuta HIGUCHI89111d92017-05-04 11:29:17 -0700334 default boolean unlock() throws NetconfException {
335 return unlock(DatastoreId.RUNNING);
336 }
andreaeb70a942015-10-16 21:34:46 -0700337
338 /**
339 * Closes the Netconf session with the device.
340 * the first time it tries gracefully, then kills it forcefully
Andrea Campanellaf4fd0352015-12-14 17:03:05 -0800341 *
andreaeb70a942015-10-16 21:34:46 -0700342 * @return true if closed
Andrea Campanella101417d2015-12-11 17:58:07 -0800343 * @throws NetconfException when there is a problem in the communication process on
344 * the underlying connection
andreaeb70a942015-10-16 21:34:46 -0700345 */
Andrea Campanella101417d2015-12-11 17:58:07 -0800346 boolean close() throws NetconfException;
andreaeb70a942015-10-16 21:34:46 -0700347
348 /**
349 * Gets the session ID of the Netconf session.
350 *
351 * @return Session ID as a string.
352 */
353 String getSessionId();
354
355 /**
Aaron Kruglikov72db6422017-02-13 12:16:51 -0800356 * Gets the capabilities of the remote Netconf device associated to this
357 * session.
358 *
359 * @return Network capabilities as strings in a Set.
360 *
361 * @since 1.10.0
Aaron Kruglikov72db6422017-02-13 12:16:51 -0800362 */
Yuta HIGUCHIdd7c3f82017-09-03 14:18:01 -0700363 Set<String> getDeviceCapabilitiesSet();
andreaeb70a942015-10-16 21:34:46 -0700364
Andrea Campanella101417d2015-12-11 17:58:07 -0800365 /**
Andrea Campanellac3627842017-04-04 18:06:54 +0200366 * Checks the state of the underlying SSH session and connection
367 * and if necessary it reestablishes it.
368 * Should be implemented, providing a default here for retrocompatibility.
369 * @throws NetconfException when there is a problem in reestablishing
370 * the connection or the session to the device.
371 */
Andrea Campanellac3627842017-04-04 18:06:54 +0200372 default void checkAndReestablish() throws NetconfException {
373 Logger log = LoggerFactory.getLogger(NetconfSession.class);
374 log.error("Not implemented/exposed by the underlying session implementation");
375 }
Yuta HIGUCHI89111d92017-05-04 11:29:17 -0700376
Aaron Kruglikov72db6422017-02-13 12:16:51 -0800377 /**
378 * Sets the ONOS side capabilities.
379 *
380 * @param capabilities list of capabilities ONOS has.
381 *
382 * @since 1.10.0
383 */
384 default void setOnosCapabilities(Iterable<String> capabilities) {
385 // default implementation should be removed in the future
386 // no-op
387 }
Andrea Campanellac3627842017-04-04 18:06:54 +0200388
389 /**
Andrea Campanella101417d2015-12-11 17:58:07 -0800390 * Remove a listener from the underlying stream handler implementation.
391 *
392 * @param listener event listener.
393 */
394 void addDeviceOutputListener(NetconfDeviceOutputEventListener listener);
395
396 /**
397 * Remove a listener from the underlying stream handler implementation.
398 *
399 * @param listener event listener.
400 */
401 void removeDeviceOutputListener(NetconfDeviceOutputEventListener listener);
402
Sean Condon54d82432017-07-26 22:27:25 +0100403 /**
404 * Read the connect timeout that this session was created with.
405 * @return timeout in seconds
406 */
407 default int timeoutConnectSec() {
408 return 0;
409 };
410
411 /**
412 * Read the reply timeout that this session was created with.
413 * @return timeout in seconds
414 */
415 default int timeoutReplySec() {
416 return 0;
417 };
418
419 /**
420 * Read the idle timeout that this session was created with.
421 * @return timeout in seconds
422 */
423 default int timeoutIdleSec() {
424 return 0;
425 };
426
andreaeb70a942015-10-16 21:34:46 -0700427}