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 | |
| 19 | import org.onosproject.netconf.NetconfDevice; |
| 20 | import org.onosproject.netconf.NetconfDeviceInfo; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 21 | import org.onosproject.netconf.NetconfException; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 22 | import org.onosproject.netconf.NetconfSession; |
Andrea Campanella | 8b1cb67 | 2016-01-25 13:58:58 -0800 | [diff] [blame] | 23 | import org.onosproject.netconf.NetconfSessionFactory; |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 26 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 27 | /** |
Ryan Goulding | d57dea4 | 2016-12-03 13:01:15 -0500 | [diff] [blame] | 28 | * Default implementation of a NETCONF device. |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 29 | */ |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 30 | public class DefaultNetconfDevice implements NetconfDevice { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 31 | |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 32 | public static final Logger log = LoggerFactory |
Andrea Campanella | 50d2521 | 2016-02-26 13:06:23 -0800 | [diff] [blame] | 33 | .getLogger(DefaultNetconfDevice.class); |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 34 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 35 | private NetconfDeviceInfo netconfDeviceInfo; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 36 | private boolean deviceState = true; |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 37 | private final NetconfSessionFactory sessionFactory; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 38 | private NetconfSession netconfSession; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 39 | |
Yuta HIGUCHI | e3ae821 | 2017-04-20 10:18:41 -0700 | [diff] [blame] | 40 | // will block until hello RPC handshake completes |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 41 | /** |
| 42 | * Creates a new default NETCONF device with the information provided. |
Yuta HIGUCHI | 0454d70 | 2017-03-17 10:08:38 -0700 | [diff] [blame] | 43 | * 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] | 44 | * it and establishing the NETCONF session. |
| 45 | * @param deviceInfo information about the device to be created. |
| 46 | * @throws NetconfException if there are problems in creating or establishing |
| 47 | * the underlying NETCONF connection and session. |
| 48 | */ |
| 49 | public DefaultNetconfDevice(NetconfDeviceInfo deviceInfo) |
Andrea Campanella | 8b1cb67 | 2016-01-25 13:58:58 -0800 | [diff] [blame] | 50 | throws NetconfException { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 51 | netconfDeviceInfo = deviceInfo; |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 52 | sessionFactory = new NetconfSessionMinaImpl.MinaSshNetconfSessionFactory(); |
| 53 | try { |
| 54 | netconfSession = sessionFactory.createNetconfSession(deviceInfo); |
Yuta HIGUCHI | 234eaf3 | 2017-09-06 13:45:05 -0700 | [diff] [blame^] | 55 | } catch (NetconfException e) { |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 56 | deviceState = false; |
| 57 | throw new NetconfException("Cannot create connection and session for device " + |
| 58 | deviceInfo, e); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // will block until hello RPC handshake completes |
| 63 | /** |
| 64 | * Creates a new default NETCONF device with the information provided. |
| 65 | * The device gets created only if no exception is thrown while connecting to |
| 66 | * it and establishing the NETCONF session. |
| 67 | * |
| 68 | * @param deviceInfo information about the device to be created. |
| 69 | * @param factory the factory used to create the session |
| 70 | * @throws NetconfException if there are problems in creating or establishing |
| 71 | * the underlying NETCONF connection and session. |
| 72 | */ |
| 73 | public DefaultNetconfDevice(NetconfDeviceInfo deviceInfo, NetconfSessionFactory factory) |
| 74 | throws NetconfException { |
| 75 | netconfDeviceInfo = deviceInfo; |
| 76 | sessionFactory = factory; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 77 | try { |
Andrea Campanella | 8b1cb67 | 2016-01-25 13:58:58 -0800 | [diff] [blame] | 78 | netconfSession = sessionFactory.createNetconfSession(deviceInfo); |
Yuta HIGUCHI | 234eaf3 | 2017-09-06 13:45:05 -0700 | [diff] [blame^] | 79 | } catch (NetconfException e) { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 80 | deviceState = false; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 81 | throw new NetconfException("Cannot create connection and session for device " + |
| 82 | deviceInfo, e); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 83 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public boolean isActive() { |
| 88 | return deviceState; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public NetconfSession getSession() { |
| 93 | return netconfSession; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void disconnect() { |
| 98 | deviceState = false; |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 99 | try { |
| 100 | netconfSession.close(); |
Yuta HIGUCHI | 234eaf3 | 2017-09-06 13:45:05 -0700 | [diff] [blame^] | 101 | } catch (NetconfException e) { |
Andrea Campanella | 50d2521 | 2016-02-26 13:06:23 -0800 | [diff] [blame] | 102 | log.warn("Cannot communicate with the device {} session already closed", netconfDeviceInfo); |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 103 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public NetconfDeviceInfo getDeviceInfo() { |
| 108 | return netconfDeviceInfo; |
| 109 | } |
Andrea Campanella | 8b1cb67 | 2016-01-25 13:58:58 -0800 | [diff] [blame] | 110 | |
Andrea Campanella | 8b1cb67 | 2016-01-25 13:58:58 -0800 | [diff] [blame] | 111 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 112 | } |