blob: a7b7717a35d7c566d84ec882deabd8a1ce2c831b [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/**
Andrea Campanella950310c2016-02-12 18:14:38 -080030 * Defautl 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
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 Campanella950310c2016-02-12 18:14:38 -080042 /**
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 Campanella8b1cb672016-01-25 13:58:58 -080051 throws NetconfException {
andreaeb70a942015-10-16 21:34:46 -070052 netconfDeviceInfo = deviceInfo;
53 try {
Andrea Campanella8b1cb672016-01-25 13:58:58 -080054 netconfSession = sessionFactory.createNetconfSession(deviceInfo);
andreaeb70a942015-10-16 21:34:46 -070055 } catch (IOException e) {
Andrea Campanella101417d2015-12-11 17:58:07 -080056 throw new NetconfException("Cannot create connection and session for device " +
57 deviceInfo, e);
andreaeb70a942015-10-16 21:34:46 -070058 }
59 deviceState = true;
andreaeb70a942015-10-16 21:34:46 -070060 }
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 Campanella1cd641b2015-12-07 17:28:34 -080075 try {
76 netconfSession.close();
77 } catch (IOException e) {
78 log.warn("Cannot communicate with the device {} ", netconfDeviceInfo);
79 }
andreaeb70a942015-10-16 21:34:46 -070080 }
81
82 @Override
83 public NetconfDeviceInfo getDeviceInfo() {
84 return netconfDeviceInfo;
85 }
Andrea Campanella8b1cb672016-01-25 13:58:58 -080086
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
andreaeb70a942015-10-16 21:34:46 -070095}