andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
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 | |
| 17 | package org.onosproject.netconf.ctl; |
| 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 | |
| 27 | import java.io.IOException; |
| 28 | |
| 29 | /** |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 30 | * Defautl implementation of a NETCONF device. |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 31 | */ |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 32 | public class DefaultNetconfDevice implements NetconfDevice { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 33 | |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 34 | public static final Logger log = LoggerFactory |
Andrea Campanella | 50d2521 | 2016-02-26 13:06:23 -0800 | [diff] [blame] | 35 | .getLogger(DefaultNetconfDevice.class); |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 36 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 37 | private NetconfDeviceInfo netconfDeviceInfo; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 38 | private boolean deviceState = true; |
Andrea Campanella | 8b1cb67 | 2016-01-25 13:58:58 -0800 | [diff] [blame] | 39 | protected NetconfSessionFactory sessionFactory = new SshNetconfSessionFactory(); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 40 | private NetconfSession netconfSession; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 41 | |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 42 | /** |
| 43 | * Creates a new default NETCONF device with the information provided. |
| 44 | * The device gets created only if no exception is thrwn while connecting to |
| 45 | * it and establishing the NETCONF session. |
| 46 | * @param deviceInfo information about the device to be created. |
| 47 | * @throws NetconfException if there are problems in creating or establishing |
| 48 | * the underlying NETCONF connection and session. |
| 49 | */ |
| 50 | public DefaultNetconfDevice(NetconfDeviceInfo deviceInfo) |
Andrea Campanella | 8b1cb67 | 2016-01-25 13:58:58 -0800 | [diff] [blame] | 51 | throws NetconfException { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 52 | netconfDeviceInfo = deviceInfo; |
| 53 | try { |
Andrea Campanella | 8b1cb67 | 2016-01-25 13:58:58 -0800 | [diff] [blame] | 54 | netconfSession = sessionFactory.createNetconfSession(deviceInfo); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 55 | } catch (IOException e) { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 56 | deviceState = false; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 57 | throw new NetconfException("Cannot create connection and session for device " + |
| 58 | deviceInfo, e); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 59 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public boolean isActive() { |
| 64 | return deviceState; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public NetconfSession getSession() { |
| 69 | return netconfSession; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void disconnect() { |
| 74 | deviceState = false; |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 75 | try { |
| 76 | netconfSession.close(); |
| 77 | } catch (IOException e) { |
Andrea Campanella | 50d2521 | 2016-02-26 13:06:23 -0800 | [diff] [blame] | 78 | log.warn("Cannot communicate with the device {} session already closed", netconfDeviceInfo); |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 79 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public NetconfDeviceInfo getDeviceInfo() { |
| 84 | return netconfDeviceInfo; |
| 85 | } |
Andrea Campanella | 8b1cb67 | 2016-01-25 13:58:58 -0800 | [diff] [blame] | 86 | |
| 87 | public class SshNetconfSessionFactory implements NetconfSessionFactory { |
| 88 | |
| 89 | @Override |
| 90 | public NetconfSession createNetconfSession(NetconfDeviceInfo netconfDeviceInfo) throws NetconfException { |
| 91 | return new NetconfSessionImpl(netconfDeviceInfo); |
| 92 | } |
| 93 | } |
| 94 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 95 | } |