Rohit Singh | 16d4df9 | 2019-07-15 19:33:05 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 19 | package org.onosproject.drivers.odtn.util; |
| 20 | |
Sudeep Desai | 5013f49 | 2020-01-08 16:01:47 +0530 | [diff] [blame] | 21 | import org.apache.commons.configuration.ConfigurationException; |
Rohit Singh | 16d4df9 | 2019-07-15 19:33:05 +0530 | [diff] [blame] | 22 | import org.apache.commons.configuration.XMLConfiguration; |
| 23 | import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine; |
| 24 | import org.onosproject.drivers.utilities.XmlConfigParser; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.netconf.NetconfController; |
| 27 | import org.onosproject.netconf.NetconfDevice; |
| 28 | import org.onosproject.netconf.NetconfException; |
| 29 | import org.onosproject.netconf.NetconfSession; |
| 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
Sudeep Desai | 5013f49 | 2020-01-08 16:01:47 +0530 | [diff] [blame] | 33 | import java.io.StringWriter; |
Rohit Singh | 16d4df9 | 2019-07-15 19:33:05 +0530 | [diff] [blame] | 34 | import java.util.concurrent.CompletableFuture; |
| 35 | import java.util.concurrent.ExecutionException; |
| 36 | |
| 37 | public 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 Campanella | bdeeda1 | 2019-08-02 16:12:05 +0200 | [diff] [blame] | 53 | log.debug("Inside getNetconfSession () method for device : {}", deviceId); |
Rohit Singh | 16d4df9 | 2019-07-15 19:33:05 +0530 | [diff] [blame] | 54 | 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 Desai | 5013f49 | 2020-01-08 16:01:47 +0530 | [diff] [blame] | 72 | 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 Singh | 16d4df9 | 2019-07-15 19:33:05 +0530 | [diff] [blame] | 83 | CompletableFuture<String> fut = session.rpc(message); |
| 84 | String rpcReply = fut.get(); |
| 85 | XMLConfiguration xconf = (XMLConfiguration) XmlConfigParser.loadXmlString(rpcReply); |
| 86 | xconf.setExpressionEngine(new XPathExpressionEngine()); |
Sudeep Desai | 5013f49 | 2020-01-08 16:01:47 +0530 | [diff] [blame] | 87 | 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 Singh | 16d4df9 | 2019-07-15 19:33:05 +0530 | [diff] [blame] | 96 | 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 Desai | 5013f49 | 2020-01-08 16:01:47 +0530 | [diff] [blame] | 102 | log.error("Concurrent Exception while executing Netconf operation: {}.", ee); |
Rohit Singh | 16d4df9 | 2019-07-15 19:33:05 +0530 | [diff] [blame] | 103 | } |
| 104 | return null; |
| 105 | } |
| 106 | |
| 107 | } |