blob: dfbe7609989b4133f7d2f0851e99752b39d9e5b2 [file] [log] [blame]
andreaeb70a942015-10-16 21:34:46 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
andreaeb70a942015-10-16 21:34:46 -07003 *
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 HIGUCHIe3ae8212017-04-20 10:18:41 -070017package org.onosproject.netconf.ctl.impl;
andreaeb70a942015-10-16 21:34:46 -070018
gyewan.an91d7e7e2019-01-17 15:12:48 +090019import org.onosproject.netconf.NetconfController;
andreaeb70a942015-10-16 21:34:46 -070020import org.onosproject.netconf.NetconfDevice;
21import org.onosproject.netconf.NetconfDeviceInfo;
Andrea Campanella101417d2015-12-11 17:58:07 -080022import org.onosproject.netconf.NetconfException;
andreaeb70a942015-10-16 21:34:46 -070023import org.onosproject.netconf.NetconfSession;
Andrea Campanella8b1cb672016-01-25 13:58:58 -080024import org.onosproject.netconf.NetconfSessionFactory;
Andrea Campanella1cd641b2015-12-07 17:28:34 -080025import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
andreaeb70a942015-10-16 21:34:46 -070027
andreaeb70a942015-10-16 21:34:46 -070028/**
Ryan Gouldingd57dea42016-12-03 13:01:15 -050029 * Default implementation of a NETCONF device.
andreaeb70a942015-10-16 21:34:46 -070030 */
Andrea Campanella950310c2016-02-12 18:14:38 -080031public class DefaultNetconfDevice implements NetconfDevice {
andreaeb70a942015-10-16 21:34:46 -070032
Andrea Campanella1cd641b2015-12-07 17:28:34 -080033 public static final Logger log = LoggerFactory
Andrea Campanella50d25212016-02-26 13:06:23 -080034 .getLogger(DefaultNetconfDevice.class);
Andrea Campanella1cd641b2015-12-07 17:28:34 -080035
andreaeb70a942015-10-16 21:34:46 -070036 private NetconfDeviceInfo netconfDeviceInfo;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070037 private boolean deviceState = true;
andreaeb70a942015-10-16 21:34:46 -070038 private NetconfSession netconfSession;
gyewan.an91d7e7e2019-01-17 15:12:48 +090039 private boolean isMasterSession = false;
40 private NetconfSession netconfProxySession;
andreaeb70a942015-10-16 21:34:46 -070041
Yuta HIGUCHIe3ae8212017-04-20 10:18:41 -070042 // will block until hello RPC handshake completes
Andrea Campanella950310c2016-02-12 18:14:38 -080043 /**
44 * Creates a new default NETCONF device with the information provided.
Yuta HIGUCHI0454d702017-03-17 10:08:38 -070045 * The device gets created only if no exception is thrown while connecting to
Andrea Campanella950310c2016-02-12 18:14:38 -080046 * it and establishing the NETCONF session.
gyewan.an91d7e7e2019-01-17 15:12:48 +090047 * The secure transport session will only be created if isMaster is true.
Andrea Campanella950310c2016-02-12 18:14:38 -080048 * @param deviceInfo information about the device to be created.
gyewan.an91d7e7e2019-01-17 15:12:48 +090049 * @param isMaster if true create secure transport session, otherwise create proxy session.
50 * @param netconfController netconf controller object
Andrea Campanella950310c2016-02-12 18:14:38 -080051 * @throws NetconfException if there are problems in creating or establishing
52 * the underlying NETCONF connection and session.
53 */
gyewan.an91d7e7e2019-01-17 15:12:48 +090054 public DefaultNetconfDevice(NetconfDeviceInfo deviceInfo,
55 boolean isMaster,
56 NetconfController netconfController)
Andrea Campanella8b1cb672016-01-25 13:58:58 -080057 throws NetconfException {
andreaeb70a942015-10-16 21:34:46 -070058 netconfDeviceInfo = deviceInfo;
Andrea Campanella7bbe7b12017-05-03 16:03:38 -070059 try {
gyewan.an91d7e7e2019-01-17 15:12:48 +090060 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 HIGUCHI234eaf32017-09-06 13:45:05 -070069 } catch (NetconfException e) {
Andrea Campanella7bbe7b12017-05-03 16:03:38 -070070 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.an91d7e7e2019-01-17 15:12:48 +090081 * The secure transport session will only be created if isMaster is true.
Andrea Campanella7bbe7b12017-05-03 16:03:38 -070082 * @param deviceInfo information about the device to be created.
83 * @param factory the factory used to create the session
gyewan.an91d7e7e2019-01-17 15:12:48 +090084 * @param isMaster if true create secure transport session, otherwise create proxy session.
85 * @param netconfController netconf controller object
Andrea Campanella7bbe7b12017-05-03 16:03:38 -070086 * @throws NetconfException if there are problems in creating or establishing
87 * the underlying NETCONF connection and session.
88 */
gyewan.an91d7e7e2019-01-17 15:12:48 +090089 public DefaultNetconfDevice(NetconfDeviceInfo deviceInfo,
90 NetconfSessionFactory factory,
91 boolean isMaster,
92 NetconfController netconfController)
Andrea Campanella7bbe7b12017-05-03 16:03:38 -070093 throws NetconfException {
94 netconfDeviceInfo = deviceInfo;
andreaeb70a942015-10-16 21:34:46 -070095 try {
gyewan.an91d7e7e2019-01-17 15:12:48 +090096 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 HIGUCHI234eaf32017-09-06 13:45:05 -0700105 } catch (NetconfException e) {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700106 deviceState = false;
Andrea Campanella101417d2015-12-11 17:58:07 -0800107 throw new NetconfException("Cannot create connection and session for device " +
108 deviceInfo, e);
andreaeb70a942015-10-16 21:34:46 -0700109 }
andreaeb70a942015-10-16 21:34:46 -0700110 }
111
112 @Override
113 public boolean isActive() {
114 return deviceState;
115 }
116
117 @Override
118 public NetconfSession getSession() {
gyewan.an91d7e7e2019-01-17 15:12:48 +0900119 return netconfProxySession;
andreaeb70a942015-10-16 21:34:46 -0700120 }
121
122 @Override
123 public void disconnect() {
124 deviceState = false;
Andrea Campanella1cd641b2015-12-07 17:28:34 -0800125 try {
gyewan.an91d7e7e2019-01-17 15:12:48 +0900126 if (isMasterSession) {
127 netconfSession.close();
128 }
129 netconfProxySession.close();
Yuta HIGUCHI234eaf32017-09-06 13:45:05 -0700130 } catch (NetconfException e) {
Andrea Campanella50d25212016-02-26 13:06:23 -0800131 log.warn("Cannot communicate with the device {} session already closed", netconfDeviceInfo);
Andrea Campanella1cd641b2015-12-07 17:28:34 -0800132 }
andreaeb70a942015-10-16 21:34:46 -0700133 }
134
135 @Override
gyewan.an91d7e7e2019-01-17 15:12:48 +0900136 public boolean isMasterSession() {
137 return isMasterSession;
138 }
139
140 @Override
andreaeb70a942015-10-16 21:34:46 -0700141 public NetconfDeviceInfo getDeviceInfo() {
142 return netconfDeviceInfo;
143 }
andreaeb70a942015-10-16 21:34:46 -0700144}