blob: 786c999ca88702e9f97506219cf3fe4504e1cb88 [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
21import org.apache.commons.configuration.XMLConfiguration;
22import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine;
23import org.onosproject.drivers.utilities.XmlConfigParser;
24import org.onosproject.net.DeviceId;
25import org.onosproject.netconf.NetconfController;
26import org.onosproject.netconf.NetconfDevice;
27import org.onosproject.netconf.NetconfException;
28import org.onosproject.netconf.NetconfSession;
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32import java.util.concurrent.CompletableFuture;
33import java.util.concurrent.ExecutionException;
34
35public final class NetconfSessionUtility {
36
37 private static final Logger log = LoggerFactory.getLogger(NetconfSessionUtility.class);
38
39 private NetconfSessionUtility() {
40 }
41
42 /**
43 * Returns the NetconfSession with the device for which the method was called.
44 *
45 * @param deviceId device identifier
46 * @param controller NetconfController
47 * @return The netconf session or null
48 */
49 public static NetconfSession getNetconfSession(DeviceId deviceId,
50 NetconfController controller) {
51 log.info("Inside getNetconfSession () method for device : {}", deviceId);
52 NetconfDevice ncdev = controller.getDevicesMap().get(deviceId);
53 if (ncdev == null) {
54 log.trace("No netconf device, returning null session");
55 return null;
56 }
57 return ncdev.getSession();
58 }
59
60 /**
61 * Execute RPC request.
62 *
63 * @param session Netconf session
64 * @param message Netconf message in XML format
65 * @return XMLConfiguration object
66 */
67
68 public static XMLConfiguration executeRpc(NetconfSession session, String message) {
69 try {
70 CompletableFuture<String> fut = session.rpc(message);
71 String rpcReply = fut.get();
72 XMLConfiguration xconf = (XMLConfiguration) XmlConfigParser.loadXmlString(rpcReply);
73 xconf.setExpressionEngine(new XPathExpressionEngine());
74 return xconf;
75 } catch (NetconfException ne) {
76 log.error("Exception on Netconf protocol: {}.", ne);
77 } catch (InterruptedException ie) {
78 log.error("Interrupted Exception: {}.", ie);
79 } catch (ExecutionException ee) {
80 log.error("Concurrent Exception while executing Netconf operation: {}.", ee);
81 }
82 return null;
83 }
84
85}