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 | |
| 21 | import org.apache.commons.configuration.XMLConfiguration; |
| 22 | import org.apache.commons.configuration.tree.xpath.XPathExpressionEngine; |
| 23 | import org.onosproject.drivers.utilities.XmlConfigParser; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.netconf.NetconfController; |
| 26 | import org.onosproject.netconf.NetconfDevice; |
| 27 | import org.onosproject.netconf.NetconfException; |
| 28 | import org.onosproject.netconf.NetconfSession; |
| 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
| 32 | import java.util.concurrent.CompletableFuture; |
| 33 | import java.util.concurrent.ExecutionException; |
| 34 | |
| 35 | public 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 | } |