andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 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 | |
Yuta HIGUCHI | e3ae821 | 2017-04-20 10:18:41 -0700 | [diff] [blame] | 17 | package org.onosproject.netconf.ctl.impl; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 18 | |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 19 | import org.onosproject.netconf.NetconfController; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 20 | import org.onosproject.netconf.NetconfDevice; |
| 21 | import org.onosproject.netconf.NetconfDeviceInfo; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 22 | import org.onosproject.netconf.NetconfException; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 23 | import org.onosproject.netconf.NetconfSession; |
Andrea Campanella | 8b1cb67 | 2016-01-25 13:58:58 -0800 | [diff] [blame] | 24 | import org.onosproject.netconf.NetconfSessionFactory; |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 27 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 28 | /** |
Ryan Goulding | d57dea4 | 2016-12-03 13:01:15 -0500 | [diff] [blame] | 29 | * Default implementation of a NETCONF device. |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 30 | */ |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 31 | public class DefaultNetconfDevice implements NetconfDevice { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 32 | |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 33 | public static final Logger log = LoggerFactory |
Andrea Campanella | 50d2521 | 2016-02-26 13:06:23 -0800 | [diff] [blame] | 34 | .getLogger(DefaultNetconfDevice.class); |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 35 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 36 | private NetconfDeviceInfo netconfDeviceInfo; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 37 | private boolean deviceState = true; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 38 | private NetconfSession netconfSession; |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 39 | private boolean isMasterSession = false; |
| 40 | private NetconfSession netconfProxySession; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 41 | |
Yuta HIGUCHI | e3ae821 | 2017-04-20 10:18:41 -0700 | [diff] [blame] | 42 | // will block until hello RPC handshake completes |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 43 | /** |
| 44 | * Creates a new default NETCONF device with the information provided. |
Yuta HIGUCHI | 0454d70 | 2017-03-17 10:08:38 -0700 | [diff] [blame] | 45 | * The device gets created only if no exception is thrown while connecting to |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 46 | * it and establishing the NETCONF session. |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 47 | * The secure transport session will only be created if isMaster is true. |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 48 | * @param deviceInfo information about the device to be created. |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 49 | * @param isMaster if true create secure transport session, otherwise create proxy session. |
| 50 | * @param netconfController netconf controller object |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 51 | * @throws NetconfException if there are problems in creating or establishing |
| 52 | * the underlying NETCONF connection and session. |
| 53 | */ |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 54 | public DefaultNetconfDevice(NetconfDeviceInfo deviceInfo, |
| 55 | boolean isMaster, |
| 56 | NetconfController netconfController) |
Andrea Campanella | 8b1cb67 | 2016-01-25 13:58:58 -0800 | [diff] [blame] | 57 | throws NetconfException { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 58 | netconfDeviceInfo = deviceInfo; |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 59 | try { |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 60 | if (isMaster) { |
| 61 | netconfSession = new NetconfSessionMinaImpl(deviceInfo); |
| 62 | isMasterSession = true; |
| 63 | netconfProxySession = netconfSession; |
| 64 | } else { |
| 65 | netconfProxySession = new NetconfSessionProxyImpl |
| 66 | .ProxyNetconfSessionFactory() |
| 67 | .createNetconfSession(deviceInfo, netconfController); |
| 68 | } |
Yuta HIGUCHI | 234eaf3 | 2017-09-06 13:45:05 -0700 | [diff] [blame] | 69 | } catch (NetconfException e) { |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 70 | deviceState = false; |
| 71 | throw new NetconfException("Cannot create connection and session for device " + |
| 72 | deviceInfo, e); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // will block until hello RPC handshake completes |
| 77 | /** |
| 78 | * Creates a new default NETCONF device with the information provided. |
| 79 | * The device gets created only if no exception is thrown while connecting to |
| 80 | * it and establishing the NETCONF session. |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 81 | * The secure transport session will only be created if isMaster is true. |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 82 | * @param deviceInfo information about the device to be created. |
| 83 | * @param factory the factory used to create the session |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 84 | * @param isMaster if true create secure transport session, otherwise create proxy session. |
| 85 | * @param netconfController netconf controller object |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 86 | * @throws NetconfException if there are problems in creating or establishing |
| 87 | * the underlying NETCONF connection and session. |
| 88 | */ |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 89 | public DefaultNetconfDevice(NetconfDeviceInfo deviceInfo, |
| 90 | NetconfSessionFactory factory, |
| 91 | boolean isMaster, |
| 92 | NetconfController netconfController) |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 93 | throws NetconfException { |
| 94 | netconfDeviceInfo = deviceInfo; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 95 | try { |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 96 | if (isMaster) { |
| 97 | netconfSession = factory.createNetconfSession(deviceInfo, netconfController); |
| 98 | isMasterSession = true; |
| 99 | netconfProxySession = netconfSession; |
| 100 | } else { |
| 101 | netconfProxySession = new NetconfSessionProxyImpl |
| 102 | .ProxyNetconfSessionFactory() |
| 103 | .createNetconfSession(deviceInfo, netconfController); |
| 104 | } |
Yuta HIGUCHI | 234eaf3 | 2017-09-06 13:45:05 -0700 | [diff] [blame] | 105 | } catch (NetconfException e) { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 106 | deviceState = false; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 107 | throw new NetconfException("Cannot create connection and session for device " + |
| 108 | deviceInfo, e); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 109 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public boolean isActive() { |
| 114 | return deviceState; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public NetconfSession getSession() { |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 119 | return netconfProxySession; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void disconnect() { |
| 124 | deviceState = false; |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 125 | try { |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 126 | if (isMasterSession) { |
| 127 | netconfSession.close(); |
| 128 | } |
| 129 | netconfProxySession.close(); |
Yuta HIGUCHI | 234eaf3 | 2017-09-06 13:45:05 -0700 | [diff] [blame] | 130 | } catch (NetconfException e) { |
Andrea Campanella | 50d2521 | 2016-02-26 13:06:23 -0800 | [diff] [blame] | 131 | log.warn("Cannot communicate with the device {} session already closed", netconfDeviceInfo); |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 132 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 136 | public boolean isMasterSession() { |
| 137 | return isMasterSession; |
| 138 | } |
| 139 | |
| 140 | @Override |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 141 | public NetconfDeviceInfo getDeviceInfo() { |
| 142 | return netconfDeviceInfo; |
| 143 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 144 | } |