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; |
| 21 | import org.onosproject.netconf.NetconfSession; |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame^] | 22 | import org.slf4j.Logger; |
| 23 | import org.slf4j.LoggerFactory; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 24 | |
| 25 | import java.io.IOException; |
| 26 | |
| 27 | /** |
| 28 | * Implementation of a NETCONF device. |
| 29 | */ |
| 30 | public class NetconfDeviceImpl implements NetconfDevice { |
| 31 | |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame^] | 32 | public static final Logger log = LoggerFactory |
| 33 | .getLogger(NetconfSessionImpl.class); |
| 34 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 35 | private NetconfDeviceInfo netconfDeviceInfo; |
| 36 | private boolean deviceState = false; |
| 37 | private NetconfSession netconfSession; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 38 | |
| 39 | public NetconfDeviceImpl(NetconfDeviceInfo deviceInfo) throws IOException { |
| 40 | netconfDeviceInfo = deviceInfo; |
| 41 | try { |
| 42 | netconfSession = new NetconfSessionImpl(netconfDeviceInfo); |
| 43 | } catch (IOException e) { |
| 44 | throw new IOException("Cannot create connection and session", e); |
| 45 | } |
| 46 | deviceState = true; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public boolean isActive() { |
| 51 | return deviceState; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public NetconfSession getSession() { |
| 56 | return netconfSession; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public void disconnect() { |
| 61 | deviceState = false; |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame^] | 62 | try { |
| 63 | netconfSession.close(); |
| 64 | } catch (IOException e) { |
| 65 | log.warn("Cannot communicate with the device {} ", netconfDeviceInfo); |
| 66 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public NetconfDeviceInfo getDeviceInfo() { |
| 71 | return netconfDeviceInfo; |
| 72 | } |
| 73 | } |