blob: bbc33200bb03752e10b621416dcf61b752e6d8b3 [file] [log] [blame]
andreaeb70a942015-10-16 21:34:46 -07001/*
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
17package org.onosproject.netconf.ctl;
18
19import org.onosproject.netconf.NetconfDevice;
20import org.onosproject.netconf.NetconfDeviceInfo;
Andrea Campanella101417d2015-12-11 17:58:07 -080021import org.onosproject.netconf.NetconfException;
andreaeb70a942015-10-16 21:34:46 -070022import org.onosproject.netconf.NetconfSession;
Andrea Campanella8b1cb672016-01-25 13:58:58 -080023import org.onosproject.netconf.NetconfSessionFactory;
Andrea Campanella1cd641b2015-12-07 17:28:34 -080024import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
andreaeb70a942015-10-16 21:34:46 -070026
27import java.io.IOException;
28
29/**
30 * Implementation of a NETCONF device.
31 */
32public class NetconfDeviceImpl implements NetconfDevice {
33
Andrea Campanella1cd641b2015-12-07 17:28:34 -080034 public static final Logger log = LoggerFactory
35 .getLogger(NetconfSessionImpl.class);
36
andreaeb70a942015-10-16 21:34:46 -070037 private NetconfDeviceInfo netconfDeviceInfo;
38 private boolean deviceState = false;
Andrea Campanella8b1cb672016-01-25 13:58:58 -080039 protected NetconfSessionFactory sessionFactory = new SshNetconfSessionFactory();
andreaeb70a942015-10-16 21:34:46 -070040 private NetconfSession netconfSession;
andreaeb70a942015-10-16 21:34:46 -070041
Andrea Campanella8b1cb672016-01-25 13:58:58 -080042
43 public NetconfDeviceImpl(NetconfDeviceInfo deviceInfo)
44 throws NetconfException {
andreaeb70a942015-10-16 21:34:46 -070045 netconfDeviceInfo = deviceInfo;
46 try {
Andrea Campanella8b1cb672016-01-25 13:58:58 -080047 netconfSession = sessionFactory.createNetconfSession(deviceInfo);
andreaeb70a942015-10-16 21:34:46 -070048 } catch (IOException e) {
Andrea Campanella101417d2015-12-11 17:58:07 -080049 throw new NetconfException("Cannot create connection and session for device " +
50 deviceInfo, e);
andreaeb70a942015-10-16 21:34:46 -070051 }
52 deviceState = true;
andreaeb70a942015-10-16 21:34:46 -070053 }
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 Campanella1cd641b2015-12-07 17:28:34 -080068 try {
69 netconfSession.close();
70 } catch (IOException e) {
71 log.warn("Cannot communicate with the device {} ", netconfDeviceInfo);
72 }
andreaeb70a942015-10-16 21:34:46 -070073 }
74
75 @Override
76 public NetconfDeviceInfo getDeviceInfo() {
77 return netconfDeviceInfo;
78 }
Andrea Campanella8b1cb672016-01-25 13:58:58 -080079
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
andreaeb70a942015-10-16 21:34:46 -070088}