blob: c29b8954cfecf26622c2dcaca4bb2a046d4a049e [file] [log] [blame]
andreaeb70a942015-10-16 21:34:46 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
andreaeb70a942015-10-16 21:34:46 -07003 *
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 HIGUCHIe3ae8212017-04-20 10:18:41 -070017package org.onosproject.netconf.ctl.impl;
andreaeb70a942015-10-16 21:34:46 -070018
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/**
Ryan Gouldingd57dea42016-12-03 13:01:15 -050030 * Default implementation of a NETCONF device.
andreaeb70a942015-10-16 21:34:46 -070031 */
Andrea Campanella950310c2016-02-12 18:14:38 -080032public class DefaultNetconfDevice implements NetconfDevice {
andreaeb70a942015-10-16 21:34:46 -070033
Andrea Campanella1cd641b2015-12-07 17:28:34 -080034 public static final Logger log = LoggerFactory
Andrea Campanella50d25212016-02-26 13:06:23 -080035 .getLogger(DefaultNetconfDevice.class);
Andrea Campanella1cd641b2015-12-07 17:28:34 -080036
andreaeb70a942015-10-16 21:34:46 -070037 private NetconfDeviceInfo netconfDeviceInfo;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070038 private boolean deviceState = true;
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
Yuta HIGUCHIe3ae8212017-04-20 10:18:41 -070042 // will block until hello RPC handshake completes
Andrea Campanella950310c2016-02-12 18:14:38 -080043 /**
44 * Creates a new default NETCONF device with the information provided.
Yuta HIGUCHI0454d702017-03-17 10:08:38 -070045 * The device gets created only if no exception is thrown while connecting to
Andrea Campanella950310c2016-02-12 18:14:38 -080046 * it and establishing the NETCONF session.
47 * @param deviceInfo information about the device to be created.
48 * @throws NetconfException if there are problems in creating or establishing
49 * the underlying NETCONF connection and session.
50 */
51 public DefaultNetconfDevice(NetconfDeviceInfo deviceInfo)
Andrea Campanella8b1cb672016-01-25 13:58:58 -080052 throws NetconfException {
andreaeb70a942015-10-16 21:34:46 -070053 netconfDeviceInfo = deviceInfo;
54 try {
Andrea Campanella8b1cb672016-01-25 13:58:58 -080055 netconfSession = sessionFactory.createNetconfSession(deviceInfo);
andreaeb70a942015-10-16 21:34:46 -070056 } catch (IOException e) {
Andrea Campanella7e6200a2016-03-21 09:48:40 -070057 deviceState = false;
Andrea Campanella101417d2015-12-11 17:58:07 -080058 throw new NetconfException("Cannot create connection and session for device " +
59 deviceInfo, e);
andreaeb70a942015-10-16 21:34:46 -070060 }
andreaeb70a942015-10-16 21:34:46 -070061 }
62
63 @Override
64 public boolean isActive() {
65 return deviceState;
66 }
67
68 @Override
69 public NetconfSession getSession() {
70 return netconfSession;
71 }
72
73 @Override
74 public void disconnect() {
75 deviceState = false;
Andrea Campanella1cd641b2015-12-07 17:28:34 -080076 try {
77 netconfSession.close();
78 } catch (IOException e) {
Andrea Campanella50d25212016-02-26 13:06:23 -080079 log.warn("Cannot communicate with the device {} session already closed", netconfDeviceInfo);
Andrea Campanella1cd641b2015-12-07 17:28:34 -080080 }
andreaeb70a942015-10-16 21:34:46 -070081 }
82
83 @Override
84 public NetconfDeviceInfo getDeviceInfo() {
85 return netconfDeviceInfo;
86 }
Andrea Campanella8b1cb672016-01-25 13:58:58 -080087
88 public class SshNetconfSessionFactory implements NetconfSessionFactory {
89
90 @Override
91 public NetconfSession createNetconfSession(NetconfDeviceInfo netconfDeviceInfo) throws NetconfException {
92 return new NetconfSessionImpl(netconfDeviceInfo);
93 }
94 }
95
andreaeb70a942015-10-16 21:34:46 -070096}