andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | /** |
| 30 | * Implementation of a NETCONF device. |
| 31 | */ |
| 32 | public class NetconfDeviceImpl implements NetconfDevice { |
| 33 | |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 34 | public static final Logger log = LoggerFactory |
| 35 | .getLogger(NetconfSessionImpl.class); |
| 36 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 37 | private NetconfDeviceInfo netconfDeviceInfo; |
| 38 | private boolean deviceState = false; |
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 | 8b1cb67 | 2016-01-25 13:58:58 -0800 | [diff] [blame] | 42 | |
| 43 | public NetconfDeviceImpl(NetconfDeviceInfo deviceInfo) |
| 44 | throws NetconfException { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 45 | netconfDeviceInfo = deviceInfo; |
| 46 | try { |
Andrea Campanella | 8b1cb67 | 2016-01-25 13:58:58 -0800 | [diff] [blame] | 47 | netconfSession = sessionFactory.createNetconfSession(deviceInfo); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 48 | } catch (IOException e) { |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 49 | throw new NetconfException("Cannot create connection and session for device " + |
| 50 | deviceInfo, e); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 51 | } |
| 52 | deviceState = true; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public boolean isActive() { |
| 57 | return deviceState; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public NetconfSession getSession() { |
| 62 | return netconfSession; |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public void disconnect() { |
| 67 | deviceState = false; |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 68 | try { |
| 69 | netconfSession.close(); |
| 70 | } catch (IOException e) { |
| 71 | log.warn("Cannot communicate with the device {} ", netconfDeviceInfo); |
| 72 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public NetconfDeviceInfo getDeviceInfo() { |
| 77 | return netconfDeviceInfo; |
| 78 | } |
Andrea Campanella | 8b1cb67 | 2016-01-25 13:58:58 -0800 | [diff] [blame] | 79 | |
| 80 | public class SshNetconfSessionFactory implements NetconfSessionFactory { |
| 81 | |
| 82 | @Override |
| 83 | public NetconfSession createNetconfSession(NetconfDeviceInfo netconfDeviceInfo) throws NetconfException { |
| 84 | return new NetconfSessionImpl(netconfDeviceInfo); |
| 85 | } |
| 86 | } |
| 87 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 88 | } |