blob: 363e90d64dd15119130640855835eef4e7046c54 [file] [log] [blame]
Rohit Singh16d4df92019-07-15 19:33:05 +05301/*
2 * Copyright 2019-present 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 * This Work is contributed by Sterlite Technologies
17 */
18
19package org.onosproject.drivers.odtn.util;
20
Sudeep Desai5013f492020-01-08 16:01:47 +053021import org.apache.commons.configuration.ConfigurationException;
Rohit Singh16d4df92019-07-15 19:33:05 +053022import org.apache.commons.configuration.XMLConfiguration;
23import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine;
24import org.onosproject.drivers.utilities.XmlConfigParser;
25import org.onosproject.net.DeviceId;
26import org.onosproject.netconf.NetconfController;
27import org.onosproject.netconf.NetconfDevice;
28import org.onosproject.netconf.NetconfException;
29import org.onosproject.netconf.NetconfSession;
30import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
Sudeep Desai5013f492020-01-08 16:01:47 +053033import java.io.StringWriter;
Rohit Singh16d4df92019-07-15 19:33:05 +053034import java.util.concurrent.CompletableFuture;
35import java.util.concurrent.ExecutionException;
36
37public final class NetconfSessionUtility {
38
39 private static final Logger log = LoggerFactory.getLogger(NetconfSessionUtility.class);
40
41 private NetconfSessionUtility() {
42 }
43
44 /**
45 * Returns the NetconfSession with the device for which the method was called.
46 *
47 * @param deviceId device identifier
48 * @param controller NetconfController
49 * @return The netconf session or null
50 */
51 public static NetconfSession getNetconfSession(DeviceId deviceId,
52 NetconfController controller) {
Andrea Campanellabdeeda12019-08-02 16:12:05 +020053 log.debug("Inside getNetconfSession () method for device : {}", deviceId);
Rohit Singh16d4df92019-07-15 19:33:05 +053054 NetconfDevice ncdev = controller.getDevicesMap().get(deviceId);
55 if (ncdev == null) {
56 log.trace("No netconf device, returning null session");
57 return null;
58 }
59 return ncdev.getSession();
60 }
61
62 /**
63 * Execute RPC request.
64 *
65 * @param session Netconf session
66 * @param message Netconf message in XML format
67 * @return XMLConfiguration object
68 */
69
70 public static XMLConfiguration executeRpc(NetconfSession session, String message) {
71 try {
Sudeep Desai5013f492020-01-08 16:01:47 +053072 if (log.isDebugEnabled()) {
73 try {
74 StringWriter stringWriter = new StringWriter();
75 XMLConfiguration xconf = (XMLConfiguration) XmlConfigParser.loadXmlString(message);
76 xconf.setExpressionEngine(new XPathExpressionEngine());
77 xconf.save(stringWriter);
78 log.debug("Request {}", stringWriter.toString());
79 } catch (ConfigurationException e) {
80 log.error("XML Config Exception ", e);
81 }
82 }
Rohit Singh16d4df92019-07-15 19:33:05 +053083 CompletableFuture<String> fut = session.rpc(message);
84 String rpcReply = fut.get();
85 XMLConfiguration xconf = (XMLConfiguration) XmlConfigParser.loadXmlString(rpcReply);
86 xconf.setExpressionEngine(new XPathExpressionEngine());
Sudeep Desai5013f492020-01-08 16:01:47 +053087 if (log.isDebugEnabled()) {
88 try {
89 StringWriter stringWriter = new StringWriter();
90 xconf.save(stringWriter);
91 log.debug("Response {}", stringWriter.toString());
92 } catch (ConfigurationException e) {
93 log.error("XML Config Exception ", e);
94 }
95 }
Rohit Singh16d4df92019-07-15 19:33:05 +053096 return xconf;
97 } catch (NetconfException ne) {
98 log.error("Exception on Netconf protocol: {}.", ne);
99 } catch (InterruptedException ie) {
100 log.error("Interrupted Exception: {}.", ie);
101 } catch (ExecutionException ee) {
Sudeep Desai5013f492020-01-08 16:01:47 +0530102 log.error("Concurrent Exception while executing Netconf operation: {}.", ee);
Rohit Singh16d4df92019-07-15 19:33:05 +0530103 }
104 return null;
105 }
106
107}