blob: 2c885e05819f2682b78d3a83579fc4afcca01a94 [file] [log] [blame]
andreaeb70a942015-10-16 21:34:46 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
Andreas Papazois4752cfa2016-04-25 14:52:12 +030022import org.apache.felix.scr.annotations.Modified;
23import org.apache.felix.scr.annotations.Property;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070024import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
andreaeb70a942015-10-16 21:34:46 -070026import org.apache.felix.scr.annotations.Service;
Holger Schulz092cbbf2017-08-31 17:52:30 +020027import org.bouncycastle.jce.provider.BouncyCastleProvider;
andreaeb70a942015-10-16 21:34:46 -070028import org.onlab.packet.IpAddress;
Andreas Papazois4752cfa2016-04-25 14:52:12 +030029import org.onosproject.cfg.ComponentConfigService;
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -060030import org.onosproject.net.AnnotationKeys;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070031import org.onosproject.net.Device;
andreaeb70a942015-10-16 21:34:46 -070032import org.onosproject.net.DeviceId;
Sean Condon54d82432017-07-26 22:27:25 +010033import org.onosproject.net.config.NetworkConfigRegistry;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070034import org.onosproject.net.device.DeviceService;
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -060035import org.onosproject.net.key.DeviceKey;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070036import org.onosproject.net.key.DeviceKeyId;
37import org.onosproject.net.key.DeviceKeyService;
38import org.onosproject.net.key.UsernamePassword;
andreaeb70a942015-10-16 21:34:46 -070039import org.onosproject.netconf.NetconfController;
40import org.onosproject.netconf.NetconfDevice;
Andrea Campanella950310c2016-02-12 18:14:38 -080041import org.onosproject.netconf.NetconfDeviceFactory;
andreaeb70a942015-10-16 21:34:46 -070042import org.onosproject.netconf.NetconfDeviceInfo;
43import org.onosproject.netconf.NetconfDeviceListener;
Andrea Campanella2464dc32016-02-17 17:54:53 -080044import org.onosproject.netconf.NetconfDeviceOutputEvent;
45import org.onosproject.netconf.NetconfDeviceOutputEventListener;
Andrea Campanella101417d2015-12-11 17:58:07 -080046import org.onosproject.netconf.NetconfException;
Sean Condon54d82432017-07-26 22:27:25 +010047import org.onosproject.netconf.config.NetconfDeviceConfig;
48import org.onosproject.netconf.config.NetconfSshClientLib;
andreaeb70a942015-10-16 21:34:46 -070049import org.osgi.service.component.ComponentContext;
50import org.slf4j.Logger;
51import org.slf4j.LoggerFactory;
52
Holger Schulz092cbbf2017-08-31 17:52:30 +020053import java.security.Security;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070054import java.util.Arrays;
Andreas Papazois4752cfa2016-04-25 14:52:12 +030055import java.util.Dictionary;
andreaeb70a942015-10-16 21:34:46 -070056import java.util.Map;
57import java.util.Set;
58import java.util.concurrent.ConcurrentHashMap;
59import java.util.concurrent.CopyOnWriteArraySet;
Andrea Campanellac3627842017-04-04 18:06:54 +020060import java.util.concurrent.ExecutorService;
61import java.util.concurrent.Executors;
andreaeb70a942015-10-16 21:34:46 -070062
Andreas Papazois4752cfa2016-04-25 14:52:12 +030063import static org.onlab.util.Tools.get;
Sean Condon54d82432017-07-26 22:27:25 +010064import static org.onlab.util.Tools.getIntegerProperty;
Andrea Campanellac3627842017-04-04 18:06:54 +020065import static org.onlab.util.Tools.groupedThreads;
Andreas Papazois4752cfa2016-04-25 14:52:12 +030066
andreaeb70a942015-10-16 21:34:46 -070067/**
68 * The implementation of NetconfController.
69 */
70@Component(immediate = true)
71@Service
72public class NetconfControllerImpl implements NetconfController {
Andrea Campanella7bbe7b12017-05-03 16:03:38 -070073
74 private static final String ETHZ_SSH2 = "ethz-ssh2";
75
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -070076 protected static final int DEFAULT_CONNECT_TIMEOUT_SECONDS = 5;
Sean Condon334ad692016-12-13 17:56:56 +000077 private static final String PROP_NETCONF_CONNECT_TIMEOUT = "netconfConnectTimeout";
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -070078 // FIXME @Property should not be static
Sean Condon334ad692016-12-13 17:56:56 +000079 @Property(name = PROP_NETCONF_CONNECT_TIMEOUT, intValue = DEFAULT_CONNECT_TIMEOUT_SECONDS,
80 label = "Time (in seconds) to wait for a NETCONF connect.")
81 protected static int netconfConnectTimeout = DEFAULT_CONNECT_TIMEOUT_SECONDS;
82
Andreas Papazois4752cfa2016-04-25 14:52:12 +030083 private static final String PROP_NETCONF_REPLY_TIMEOUT = "netconfReplyTimeout";
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -070084 protected static final int DEFAULT_REPLY_TIMEOUT_SECONDS = 5;
85 // FIXME @Property should not be static
Andreas Papazois4752cfa2016-04-25 14:52:12 +030086 @Property(name = PROP_NETCONF_REPLY_TIMEOUT, intValue = DEFAULT_REPLY_TIMEOUT_SECONDS,
87 label = "Time (in seconds) waiting for a NetConf reply")
88 protected static int netconfReplyTimeout = DEFAULT_REPLY_TIMEOUT_SECONDS;
89
Sean Condon7347de92017-07-21 12:17:25 +010090 private static final String PROP_NETCONF_IDLE_TIMEOUT = "netconfIdleTimeout";
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -070091 protected static final int DEFAULT_IDLE_TIMEOUT_SECONDS = 300;
92 // FIXME @Property should not be static
Sean Condon7347de92017-07-21 12:17:25 +010093 @Property(name = PROP_NETCONF_IDLE_TIMEOUT, intValue = DEFAULT_IDLE_TIMEOUT_SECONDS,
94 label = "Time (in seconds) SSH session will close if no traffic seen")
95 protected static int netconfIdleTimeout = DEFAULT_IDLE_TIMEOUT_SECONDS;
96
Andrea Campanella7bbe7b12017-05-03 16:03:38 -070097 private static final String SSH_LIBRARY = "sshLibrary";
Sean Condon54d82432017-07-26 22:27:25 +010098 private static final String APACHE_MINA_STR = "apache-mina";
99 @Property(name = SSH_LIBRARY, value = APACHE_MINA_STR,
Sean Condon7347de92017-07-21 12:17:25 +0100100 label = "Ssh Library instead of apache_mina (i.e. ethz-ssh2")
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -0700101 protected NetconfSshClientLib sshLibrary = NetconfSshClientLib.APACHE_MINA;
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700102
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300103 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
104 protected ComponentConfigService cfgService;
105
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700106 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
107 protected DeviceService deviceService;
108
109 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
110 protected DeviceKeyService deviceKeyService;
andreaeb70a942015-10-16 21:34:46 -0700111
Sean Condon54d82432017-07-26 22:27:25 +0100112 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
113 protected NetworkConfigRegistry netCfgService;
114
andreaeb70a942015-10-16 21:34:46 -0700115 public static final Logger log = LoggerFactory
116 .getLogger(NetconfControllerImpl.class);
117
Andrea Campanella8b1cb672016-01-25 13:58:58 -0800118 private Map<DeviceId, NetconfDevice> netconfDeviceMap = new ConcurrentHashMap<>();
andreaeb70a942015-10-16 21:34:46 -0700119
Andrea Campanella2464dc32016-02-17 17:54:53 -0800120 private final NetconfDeviceOutputEventListener downListener = new DeviceDownEventListener();
121
andreaeb70a942015-10-16 21:34:46 -0700122 protected Set<NetconfDeviceListener> netconfDeviceListeners = new CopyOnWriteArraySet<>();
Andrea Campanella950310c2016-02-12 18:14:38 -0800123 protected NetconfDeviceFactory deviceFactory = new DefaultNetconfDeviceFactory();
andreaeb70a942015-10-16 21:34:46 -0700124
Andrea Campanellac3627842017-04-04 18:06:54 +0200125 protected final ExecutorService executor =
126 Executors.newCachedThreadPool(groupedThreads("onos/netconfdevicecontroller",
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700127 "connection-reopen-%d", log));
Andrea Campanellac3627842017-04-04 18:06:54 +0200128
andreaeb70a942015-10-16 21:34:46 -0700129 @Activate
130 public void activate(ComponentContext context) {
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300131 cfgService.registerProperties(getClass());
132 modified(context);
Yuta HIGUCHIe9761742017-09-10 15:10:19 -0700133 Security.addProvider(new BouncyCastleProvider());
andreaeb70a942015-10-16 21:34:46 -0700134 log.info("Started");
135 }
136
137 @Deactivate
138 public void deactivate() {
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700139 netconfDeviceMap.values().forEach(device -> {
140 device.getSession().removeDeviceOutputListener(downListener);
141 device.disconnect();
142 });
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300143 cfgService.unregisterProperties(getClass(), false);
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700144 netconfDeviceListeners.clear();
andreaeb70a942015-10-16 21:34:46 -0700145 netconfDeviceMap.clear();
Holger Schulz092cbbf2017-08-31 17:52:30 +0200146 Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME);
andreaeb70a942015-10-16 21:34:46 -0700147 log.info("Stopped");
148 }
149
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300150 @Modified
151 public void modified(ComponentContext context) {
152 if (context == null) {
153 netconfReplyTimeout = DEFAULT_REPLY_TIMEOUT_SECONDS;
Sean Condon334ad692016-12-13 17:56:56 +0000154 netconfConnectTimeout = DEFAULT_CONNECT_TIMEOUT_SECONDS;
Sean Condon54d82432017-07-26 22:27:25 +0100155 netconfIdleTimeout = DEFAULT_IDLE_TIMEOUT_SECONDS;
156 sshLibrary = NetconfSshClientLib.APACHE_MINA;
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300157 log.info("No component configuration");
158 return;
159 }
160
161 Dictionary<?, ?> properties = context.getProperties();
162
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700163 String newSshLibrary;
Sean Condon334ad692016-12-13 17:56:56 +0000164
Sean Condon54d82432017-07-26 22:27:25 +0100165 int newNetconfReplyTimeout = getIntegerProperty(
166 properties, PROP_NETCONF_REPLY_TIMEOUT, netconfReplyTimeout);
167 int newNetconfConnectTimeout = getIntegerProperty(
168 properties, PROP_NETCONF_CONNECT_TIMEOUT, netconfConnectTimeout);
169 int newNetconfIdleTimeout = getIntegerProperty(
170 properties, PROP_NETCONF_IDLE_TIMEOUT, netconfIdleTimeout);
Sean Condon334ad692016-12-13 17:56:56 +0000171
Sean Condon54d82432017-07-26 22:27:25 +0100172 newSshLibrary = get(properties, SSH_LIBRARY);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300173
Sean Condon334ad692016-12-13 17:56:56 +0000174 if (newNetconfConnectTimeout < 0) {
175 log.warn("netconfConnectTimeout is invalid - less than 0");
176 return;
177 } else if (newNetconfReplyTimeout <= 0) {
178 log.warn("netconfReplyTimeout is invalid - 0 or less.");
179 return;
Sean Condon7347de92017-07-21 12:17:25 +0100180 } else if (newNetconfIdleTimeout <= 0) {
181 log.warn("netconfIdleTimeout is invalid - 0 or less.");
182 return;
Sean Condon334ad692016-12-13 17:56:56 +0000183 }
184
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300185 netconfReplyTimeout = newNetconfReplyTimeout;
Sean Condon334ad692016-12-13 17:56:56 +0000186 netconfConnectTimeout = newNetconfConnectTimeout;
Sean Condon7347de92017-07-21 12:17:25 +0100187 netconfIdleTimeout = newNetconfIdleTimeout;
Sean Condon54d82432017-07-26 22:27:25 +0100188 if (newSshLibrary != null) {
189 sshLibrary = NetconfSshClientLib.getEnum(newSshLibrary);
190 }
191 log.info("Settings: {} = {}, {} = {}, {} = {}, {} = {}",
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700192 PROP_NETCONF_REPLY_TIMEOUT, netconfReplyTimeout,
193 PROP_NETCONF_CONNECT_TIMEOUT, netconfConnectTimeout,
Sean Condon7347de92017-07-21 12:17:25 +0100194 PROP_NETCONF_IDLE_TIMEOUT, netconfIdleTimeout,
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700195 SSH_LIBRARY, sshLibrary);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300196 }
197
andreaeb70a942015-10-16 21:34:46 -0700198 @Override
199 public void addDeviceListener(NetconfDeviceListener listener) {
200 if (!netconfDeviceListeners.contains(listener)) {
201 netconfDeviceListeners.add(listener);
202 }
203 }
204
205 @Override
206 public void removeDeviceListener(NetconfDeviceListener listener) {
207 netconfDeviceListeners.remove(listener);
208 }
209
210 @Override
211 public NetconfDevice getNetconfDevice(DeviceId deviceInfo) {
212 return netconfDeviceMap.get(deviceInfo);
213 }
214
215 @Override
216 public NetconfDevice getNetconfDevice(IpAddress ip, int port) {
andreaeb70a942015-10-16 21:34:46 -0700217 for (DeviceId info : netconfDeviceMap.keySet()) {
Andrea Campanella57efbb22016-02-11 14:21:41 -0800218 if (info.uri().getSchemeSpecificPart().equals(ip.toString() + ":" + port)) {
andreaeb70a942015-10-16 21:34:46 -0700219 return netconfDeviceMap.get(info);
220 }
221 }
Andrea Campanella1cd641b2015-12-07 17:28:34 -0800222 return null;
andreaeb70a942015-10-16 21:34:46 -0700223 }
224
225 @Override
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700226 public NetconfDevice connectDevice(DeviceId deviceId) throws NetconfException {
Sean Condon54d82432017-07-26 22:27:25 +0100227 NetconfDeviceConfig netCfg = netCfgService.getConfig(
228 deviceId, NetconfDeviceConfig.class);
229 NetconfDeviceInfo deviceInfo = null;
230
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700231 if (netconfDeviceMap.containsKey(deviceId)) {
232 log.debug("Device {} is already present", deviceId);
233 return netconfDeviceMap.get(deviceId);
Sean Condon54d82432017-07-26 22:27:25 +0100234 } else if (netCfg != null) {
235 log.debug("Device {} is present in NetworkConfig", deviceId);
236 deviceInfo = new NetconfDeviceInfo(netCfg);
237
andreaeb70a942015-10-16 21:34:46 -0700238 } else {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700239 log.debug("Creating NETCONF device {}", deviceId);
240 Device device = deviceService.getDevice(deviceId);
241 String ip;
242 int port;
243 if (device != null) {
244 ip = device.annotations().value("ipaddress");
245 port = Integer.parseInt(device.annotations().value("port"));
246 } else {
247 String[] info = deviceId.toString().split(":");
248 if (info.length == 3) {
249 ip = info[1];
250 port = Integer.parseInt(info[2]);
251 } else {
252 ip = Arrays.asList(info).stream().filter(el -> !el.equals(info[0])
Andrea Campanellac3627842017-04-04 18:06:54 +0200253 && !el.equals(info[info.length - 1]))
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700254 .reduce((t, u) -> t + ":" + u)
255 .get();
256 log.debug("ip v6 {}", ip);
257 port = Integer.parseInt(info[info.length - 1]);
258 }
259 }
260 try {
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600261 DeviceKey deviceKey = deviceKeyService.getDeviceKey(
Andrea Campanellac3627842017-04-04 18:06:54 +0200262 DeviceKeyId.deviceKeyId(deviceId.toString()));
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600263 if (deviceKey.type() == DeviceKey.Type.USERNAME_PASSWORD) {
264 UsernamePassword usernamepasswd = deviceKey.asUsernamePassword();
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700265
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600266 deviceInfo = new NetconfDeviceInfo(usernamepasswd.username(),
Andrea Campanellac3627842017-04-04 18:06:54 +0200267 usernamepasswd.password(),
268 IpAddress.valueOf(ip),
269 port);
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600270
271 } else if (deviceKey.type() == DeviceKey.Type.SSL_KEY) {
272 String username = deviceKey.annotations().value(AnnotationKeys.USERNAME);
273 String password = deviceKey.annotations().value(AnnotationKeys.PASSWORD);
Andrea Campanellac3627842017-04-04 18:06:54 +0200274 String sshkey = deviceKey.annotations().value(AnnotationKeys.SSHKEY);
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600275
276 deviceInfo = new NetconfDeviceInfo(username,
Andrea Campanellac3627842017-04-04 18:06:54 +0200277 password,
278 IpAddress.valueOf(ip),
279 port,
280 sshkey);
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600281 } else {
282 log.error("Unknown device key for device {}", deviceId);
283 }
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700284 } catch (NullPointerException e) {
285 throw new NetconfException("No Device Key for device " + deviceId, e);
286 }
andreaeb70a942015-10-16 21:34:46 -0700287 }
Sean Condon54d82432017-07-26 22:27:25 +0100288 NetconfDevice netconfDevicedevice = createDevice(deviceInfo);
289 netconfDevicedevice.getSession().addDeviceOutputListener(downListener);
290 return netconfDevicedevice;
andreaeb70a942015-10-16 21:34:46 -0700291 }
292
293 @Override
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700294 public void disconnectDevice(DeviceId deviceId, boolean remove) {
295 if (!netconfDeviceMap.containsKey(deviceId)) {
296 log.warn("Device {} is not present", deviceId);
andreaeb70a942015-10-16 21:34:46 -0700297 } else {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700298 stopDevice(deviceId, remove);
andreaeb70a942015-10-16 21:34:46 -0700299 }
300 }
301
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700302 private void stopDevice(DeviceId deviceId, boolean remove) {
303 netconfDeviceMap.get(deviceId).disconnect();
304 netconfDeviceMap.remove(deviceId);
305 if (remove) {
Andrea Campanella86294db2016-03-07 11:42:49 -0800306 for (NetconfDeviceListener l : netconfDeviceListeners) {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700307 l.deviceRemoved(deviceId);
308 }
309 }
310 }
311
312 @Override
313 public void removeDevice(DeviceId deviceId) {
314 if (!netconfDeviceMap.containsKey(deviceId)) {
315 log.warn("Device {} is not present", deviceId);
316 for (NetconfDeviceListener l : netconfDeviceListeners) {
317 l.deviceRemoved(deviceId);
318 }
319 } else {
320 netconfDeviceMap.remove(deviceId);
321 for (NetconfDeviceListener l : netconfDeviceListeners) {
322 l.deviceRemoved(deviceId);
Andrea Campanella86294db2016-03-07 11:42:49 -0800323 }
324 }
325 }
326
Andrea Campanella101417d2015-12-11 17:58:07 -0800327 private NetconfDevice createDevice(NetconfDeviceInfo deviceInfo) throws NetconfException {
Andrea Campanella950310c2016-02-12 18:14:38 -0800328 NetconfDevice netconfDevice = deviceFactory.createNetconfDevice(deviceInfo);
Andrea Campanella087ceb92015-12-07 09:58:34 -0800329 netconfDeviceMap.put(deviceInfo.getDeviceId(), netconfDevice);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700330 for (NetconfDeviceListener l : netconfDeviceListeners) {
331 l.deviceAdded(deviceInfo.getDeviceId());
332 }
andreaeb70a942015-10-16 21:34:46 -0700333 return netconfDevice;
334 }
335
andreaeb70a942015-10-16 21:34:46 -0700336
337 @Override
338 public Map<DeviceId, NetconfDevice> getDevicesMap() {
339 return netconfDeviceMap;
340 }
Andrea Campanella950310c2016-02-12 18:14:38 -0800341
Andrea Campanella86294db2016-03-07 11:42:49 -0800342 @Override
343 public Set<DeviceId> getNetconfDevices() {
344 return netconfDeviceMap.keySet();
345 }
346
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700347
Andrea Campanella950310c2016-02-12 18:14:38 -0800348 //Device factory for the specific NetconfDeviceImpl
349 private class DefaultNetconfDeviceFactory implements NetconfDeviceFactory {
350
351 @Override
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700352 public NetconfDevice createNetconfDevice(NetconfDeviceInfo netconfDeviceInfo)
353 throws NetconfException {
Yuta HIGUCHI6800ced2017-11-20 11:15:37 -0800354 if (NetconfSshClientLib.ETHZ_SSH2.equals(netconfDeviceInfo.sshClientLib().orElse(null)) ||
Sean Condon54d82432017-07-26 22:27:25 +0100355 NetconfSshClientLib.ETHZ_SSH2.equals(sshLibrary)) {
356 log.info("Creating NETCONF session to {} with {}",
357 netconfDeviceInfo.name(), NetconfSshClientLib.ETHZ_SSH2);
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700358 return new DefaultNetconfDevice(netconfDeviceInfo,
Sean Condon54d82432017-07-26 22:27:25 +0100359 new NetconfSessionImpl.SshNetconfSessionFactory());
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700360 }
Sean Condon54d82432017-07-26 22:27:25 +0100361 log.info("Creating NETCONF session to {} with {}",
362 netconfDeviceInfo.getDeviceId(), NetconfSshClientLib.APACHE_MINA);
Andrea Campanella950310c2016-02-12 18:14:38 -0800363 return new DefaultNetconfDevice(netconfDeviceInfo);
364 }
365 }
Andrea Campanella2464dc32016-02-17 17:54:53 -0800366
Yuta HIGUCHIf7089102017-05-03 10:31:46 -0700367 //Listener for closed session with devices, gets triggered when devices goes down
368 // or sends the end pattern ]]>]]>
Andrea Campanella2464dc32016-02-17 17:54:53 -0800369 private class DeviceDownEventListener implements NetconfDeviceOutputEventListener {
370
371 @Override
372 public void event(NetconfDeviceOutputEvent event) {
Andrea Campanellac3627842017-04-04 18:06:54 +0200373 DeviceId did = event.getDeviceInfo().getDeviceId();
Andrea Campanella2464dc32016-02-17 17:54:53 -0800374 if (event.type().equals(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED)) {
Andrea Campanellac3627842017-04-04 18:06:54 +0200375 removeDevice(did);
376 } else if (event.type().equals(NetconfDeviceOutputEvent.Type.SESSION_CLOSED)) {
377 log.info("Trying to reestablish connection with device {}", did);
378 executor.execute(() -> {
379 try {
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700380 NetconfDevice device = netconfDeviceMap.get(did);
381 if (device != null) {
382 device.getSession().checkAndReestablish();
383 log.info("Connection with device {} was reestablished", did);
384 } else {
385 log.warn("The device {} is not in the system", did);
386 }
387
Andrea Campanellac3627842017-04-04 18:06:54 +0200388 } catch (NetconfException e) {
389 log.error("The SSH connection with device {} couldn't be " +
Sean Condon54d82432017-07-26 22:27:25 +0100390 "reestablished due to {}. " +
391 "Marking the device as unreachable", e.getMessage());
Andrea Campanellac3627842017-04-04 18:06:54 +0200392 log.debug("Complete exception: ", e);
393 removeDevice(did);
394 }
395 });
Andrea Campanella2464dc32016-02-17 17:54:53 -0800396 }
397 }
398
399 @Override
400 public boolean isRelevant(NetconfDeviceOutputEvent event) {
401 return getDevicesMap().containsKey(event.getDeviceInfo().getDeviceId());
402 }
403 }
andreaeb70a942015-10-16 21:34:46 -0700404}