blob: 5a8f499f8f743350dfe89c80ca1f561eae585ee2 [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 Campanella1cd641b2015-12-07 17:28:34 -080023import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
andreaeb70a942015-10-16 21:34:46 -070025
26import java.io.IOException;
27
28/**
29 * Implementation of a NETCONF device.
30 */
31public class NetconfDeviceImpl implements NetconfDevice {
32
Andrea Campanella1cd641b2015-12-07 17:28:34 -080033 public static final Logger log = LoggerFactory
34 .getLogger(NetconfSessionImpl.class);
35
andreaeb70a942015-10-16 21:34:46 -070036 private NetconfDeviceInfo netconfDeviceInfo;
37 private boolean deviceState = false;
38 private NetconfSession netconfSession;
andreaeb70a942015-10-16 21:34:46 -070039
Andrea Campanella101417d2015-12-11 17:58:07 -080040 public NetconfDeviceImpl(NetconfDeviceInfo deviceInfo) throws NetconfException {
andreaeb70a942015-10-16 21:34:46 -070041 netconfDeviceInfo = deviceInfo;
42 try {
43 netconfSession = new NetconfSessionImpl(netconfDeviceInfo);
44 } catch (IOException e) {
Andrea Campanella101417d2015-12-11 17:58:07 -080045 throw new NetconfException("Cannot create connection and session for device " +
46 deviceInfo, e);
andreaeb70a942015-10-16 21:34:46 -070047 }
48 deviceState = true;
andreaeb70a942015-10-16 21:34:46 -070049 }
50
51 @Override
52 public boolean isActive() {
53 return deviceState;
54 }
55
56 @Override
57 public NetconfSession getSession() {
58 return netconfSession;
59 }
60
61 @Override
62 public void disconnect() {
63 deviceState = false;
Andrea Campanella1cd641b2015-12-07 17:28:34 -080064 try {
65 netconfSession.close();
66 } catch (IOException e) {
67 log.warn("Cannot communicate with the device {} ", netconfDeviceInfo);
68 }
andreaeb70a942015-10-16 21:34:46 -070069 }
70
71 @Override
72 public NetconfDeviceInfo getDeviceInfo() {
73 return netconfDeviceInfo;
74 }
75}