blob: daba5be8b10b3b05279526a311a90d9c13fa1a3d [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;
27import org.onlab.packet.IpAddress;
Andreas Papazois4752cfa2016-04-25 14:52:12 +030028import org.onosproject.cfg.ComponentConfigService;
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -060029import org.onosproject.net.AnnotationKeys;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070030import org.onosproject.net.Device;
andreaeb70a942015-10-16 21:34:46 -070031import org.onosproject.net.DeviceId;
Sean Condon54d82432017-07-26 22:27:25 +010032import org.onosproject.net.config.NetworkConfigRegistry;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070033import org.onosproject.net.device.DeviceService;
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -060034import org.onosproject.net.key.DeviceKey;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070035import org.onosproject.net.key.DeviceKeyId;
36import org.onosproject.net.key.DeviceKeyService;
37import org.onosproject.net.key.UsernamePassword;
andreaeb70a942015-10-16 21:34:46 -070038import org.onosproject.netconf.NetconfController;
39import org.onosproject.netconf.NetconfDevice;
Andrea Campanella950310c2016-02-12 18:14:38 -080040import org.onosproject.netconf.NetconfDeviceFactory;
andreaeb70a942015-10-16 21:34:46 -070041import org.onosproject.netconf.NetconfDeviceInfo;
42import org.onosproject.netconf.NetconfDeviceListener;
Andrea Campanella2464dc32016-02-17 17:54:53 -080043import org.onosproject.netconf.NetconfDeviceOutputEvent;
44import org.onosproject.netconf.NetconfDeviceOutputEventListener;
Andrea Campanella101417d2015-12-11 17:58:07 -080045import org.onosproject.netconf.NetconfException;
Sean Condon54d82432017-07-26 22:27:25 +010046import org.onosproject.netconf.config.NetconfDeviceConfig;
47import org.onosproject.netconf.config.NetconfSshClientLib;
andreaeb70a942015-10-16 21:34:46 -070048import org.osgi.service.component.ComponentContext;
49import org.slf4j.Logger;
50import org.slf4j.LoggerFactory;
51
Andrea Campanella7e6200a2016-03-21 09:48:40 -070052import java.util.Arrays;
Andreas Papazois4752cfa2016-04-25 14:52:12 +030053import java.util.Dictionary;
andreaeb70a942015-10-16 21:34:46 -070054import java.util.Map;
55import java.util.Set;
56import java.util.concurrent.ConcurrentHashMap;
57import java.util.concurrent.CopyOnWriteArraySet;
Andrea Campanellac3627842017-04-04 18:06:54 +020058import java.util.concurrent.ExecutorService;
59import java.util.concurrent.Executors;
andreaeb70a942015-10-16 21:34:46 -070060
Andreas Papazois4752cfa2016-04-25 14:52:12 +030061import static org.onlab.util.Tools.get;
Sean Condon54d82432017-07-26 22:27:25 +010062import static org.onlab.util.Tools.getIntegerProperty;
Andrea Campanellac3627842017-04-04 18:06:54 +020063import static org.onlab.util.Tools.groupedThreads;
Andreas Papazois4752cfa2016-04-25 14:52:12 +030064
andreaeb70a942015-10-16 21:34:46 -070065/**
66 * The implementation of NetconfController.
67 */
68@Component(immediate = true)
69@Service
70public class NetconfControllerImpl implements NetconfController {
Andrea Campanella7bbe7b12017-05-03 16:03:38 -070071
72 private static final String ETHZ_SSH2 = "ethz-ssh2";
73
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -070074 protected static final int DEFAULT_CONNECT_TIMEOUT_SECONDS = 5;
Sean Condon334ad692016-12-13 17:56:56 +000075 private static final String PROP_NETCONF_CONNECT_TIMEOUT = "netconfConnectTimeout";
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -070076 // FIXME @Property should not be static
Sean Condon334ad692016-12-13 17:56:56 +000077 @Property(name = PROP_NETCONF_CONNECT_TIMEOUT, intValue = DEFAULT_CONNECT_TIMEOUT_SECONDS,
78 label = "Time (in seconds) to wait for a NETCONF connect.")
79 protected static int netconfConnectTimeout = DEFAULT_CONNECT_TIMEOUT_SECONDS;
80
Andreas Papazois4752cfa2016-04-25 14:52:12 +030081 private static final String PROP_NETCONF_REPLY_TIMEOUT = "netconfReplyTimeout";
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -070082 protected static final int DEFAULT_REPLY_TIMEOUT_SECONDS = 5;
83 // FIXME @Property should not be static
Andreas Papazois4752cfa2016-04-25 14:52:12 +030084 @Property(name = PROP_NETCONF_REPLY_TIMEOUT, intValue = DEFAULT_REPLY_TIMEOUT_SECONDS,
85 label = "Time (in seconds) waiting for a NetConf reply")
86 protected static int netconfReplyTimeout = DEFAULT_REPLY_TIMEOUT_SECONDS;
87
Sean Condon7347de92017-07-21 12:17:25 +010088 private static final String PROP_NETCONF_IDLE_TIMEOUT = "netconfIdleTimeout";
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -070089 protected static final int DEFAULT_IDLE_TIMEOUT_SECONDS = 300;
90 // FIXME @Property should not be static
Sean Condon7347de92017-07-21 12:17:25 +010091 @Property(name = PROP_NETCONF_IDLE_TIMEOUT, intValue = DEFAULT_IDLE_TIMEOUT_SECONDS,
92 label = "Time (in seconds) SSH session will close if no traffic seen")
93 protected static int netconfIdleTimeout = DEFAULT_IDLE_TIMEOUT_SECONDS;
94
Andrea Campanella7bbe7b12017-05-03 16:03:38 -070095 private static final String SSH_LIBRARY = "sshLibrary";
Sean Condon54d82432017-07-26 22:27:25 +010096 private static final String APACHE_MINA_STR = "apache-mina";
97 @Property(name = SSH_LIBRARY, value = APACHE_MINA_STR,
Sean Condon7347de92017-07-21 12:17:25 +010098 label = "Ssh Library instead of apache_mina (i.e. ethz-ssh2")
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -070099 protected NetconfSshClientLib sshLibrary = NetconfSshClientLib.APACHE_MINA;
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700100
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300101 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
102 protected ComponentConfigService cfgService;
103
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700104 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
105 protected DeviceService deviceService;
106
107 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
108 protected DeviceKeyService deviceKeyService;
andreaeb70a942015-10-16 21:34:46 -0700109
Sean Condon54d82432017-07-26 22:27:25 +0100110 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
111 protected NetworkConfigRegistry netCfgService;
112
andreaeb70a942015-10-16 21:34:46 -0700113 public static final Logger log = LoggerFactory
114 .getLogger(NetconfControllerImpl.class);
115
Andrea Campanella8b1cb672016-01-25 13:58:58 -0800116 private Map<DeviceId, NetconfDevice> netconfDeviceMap = new ConcurrentHashMap<>();
andreaeb70a942015-10-16 21:34:46 -0700117
Andrea Campanella2464dc32016-02-17 17:54:53 -0800118 private final NetconfDeviceOutputEventListener downListener = new DeviceDownEventListener();
119
andreaeb70a942015-10-16 21:34:46 -0700120 protected Set<NetconfDeviceListener> netconfDeviceListeners = new CopyOnWriteArraySet<>();
Andrea Campanella950310c2016-02-12 18:14:38 -0800121 protected NetconfDeviceFactory deviceFactory = new DefaultNetconfDeviceFactory();
andreaeb70a942015-10-16 21:34:46 -0700122
Andrea Campanellac3627842017-04-04 18:06:54 +0200123 protected final ExecutorService executor =
124 Executors.newCachedThreadPool(groupedThreads("onos/netconfdevicecontroller",
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700125 "connection-reopen-%d", log));
Andrea Campanellac3627842017-04-04 18:06:54 +0200126
andreaeb70a942015-10-16 21:34:46 -0700127 @Activate
128 public void activate(ComponentContext context) {
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300129 cfgService.registerProperties(getClass());
130 modified(context);
andreaeb70a942015-10-16 21:34:46 -0700131 log.info("Started");
132 }
133
134 @Deactivate
135 public void deactivate() {
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700136 netconfDeviceMap.values().forEach(device -> {
137 device.getSession().removeDeviceOutputListener(downListener);
138 device.disconnect();
139 });
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300140 cfgService.unregisterProperties(getClass(), false);
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700141 netconfDeviceListeners.clear();
andreaeb70a942015-10-16 21:34:46 -0700142 netconfDeviceMap.clear();
143 log.info("Stopped");
144 }
145
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300146 @Modified
147 public void modified(ComponentContext context) {
148 if (context == null) {
149 netconfReplyTimeout = DEFAULT_REPLY_TIMEOUT_SECONDS;
Sean Condon334ad692016-12-13 17:56:56 +0000150 netconfConnectTimeout = DEFAULT_CONNECT_TIMEOUT_SECONDS;
Sean Condon54d82432017-07-26 22:27:25 +0100151 netconfIdleTimeout = DEFAULT_IDLE_TIMEOUT_SECONDS;
152 sshLibrary = NetconfSshClientLib.APACHE_MINA;
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300153 log.info("No component configuration");
154 return;
155 }
156
157 Dictionary<?, ?> properties = context.getProperties();
158
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700159 String newSshLibrary;
Sean Condon334ad692016-12-13 17:56:56 +0000160
Sean Condon54d82432017-07-26 22:27:25 +0100161 int newNetconfReplyTimeout = getIntegerProperty(
162 properties, PROP_NETCONF_REPLY_TIMEOUT, netconfReplyTimeout);
163 int newNetconfConnectTimeout = getIntegerProperty(
164 properties, PROP_NETCONF_CONNECT_TIMEOUT, netconfConnectTimeout);
165 int newNetconfIdleTimeout = getIntegerProperty(
166 properties, PROP_NETCONF_IDLE_TIMEOUT, netconfIdleTimeout);
Sean Condon334ad692016-12-13 17:56:56 +0000167
Sean Condon54d82432017-07-26 22:27:25 +0100168 newSshLibrary = get(properties, SSH_LIBRARY);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300169
Sean Condon334ad692016-12-13 17:56:56 +0000170 if (newNetconfConnectTimeout < 0) {
171 log.warn("netconfConnectTimeout is invalid - less than 0");
172 return;
173 } else if (newNetconfReplyTimeout <= 0) {
174 log.warn("netconfReplyTimeout is invalid - 0 or less.");
175 return;
Sean Condon7347de92017-07-21 12:17:25 +0100176 } else if (newNetconfIdleTimeout <= 0) {
177 log.warn("netconfIdleTimeout is invalid - 0 or less.");
178 return;
Sean Condon334ad692016-12-13 17:56:56 +0000179 }
180
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300181 netconfReplyTimeout = newNetconfReplyTimeout;
Sean Condon334ad692016-12-13 17:56:56 +0000182 netconfConnectTimeout = newNetconfConnectTimeout;
Sean Condon7347de92017-07-21 12:17:25 +0100183 netconfIdleTimeout = newNetconfIdleTimeout;
Sean Condon54d82432017-07-26 22:27:25 +0100184 if (newSshLibrary != null) {
185 sshLibrary = NetconfSshClientLib.getEnum(newSshLibrary);
186 }
187 log.info("Settings: {} = {}, {} = {}, {} = {}, {} = {}",
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700188 PROP_NETCONF_REPLY_TIMEOUT, netconfReplyTimeout,
189 PROP_NETCONF_CONNECT_TIMEOUT, netconfConnectTimeout,
Sean Condon7347de92017-07-21 12:17:25 +0100190 PROP_NETCONF_IDLE_TIMEOUT, netconfIdleTimeout,
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700191 SSH_LIBRARY, sshLibrary);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300192 }
193
andreaeb70a942015-10-16 21:34:46 -0700194 @Override
195 public void addDeviceListener(NetconfDeviceListener listener) {
196 if (!netconfDeviceListeners.contains(listener)) {
197 netconfDeviceListeners.add(listener);
198 }
199 }
200
201 @Override
202 public void removeDeviceListener(NetconfDeviceListener listener) {
203 netconfDeviceListeners.remove(listener);
204 }
205
206 @Override
207 public NetconfDevice getNetconfDevice(DeviceId deviceInfo) {
208 return netconfDeviceMap.get(deviceInfo);
209 }
210
211 @Override
212 public NetconfDevice getNetconfDevice(IpAddress ip, int port) {
andreaeb70a942015-10-16 21:34:46 -0700213 for (DeviceId info : netconfDeviceMap.keySet()) {
Andrea Campanella57efbb22016-02-11 14:21:41 -0800214 if (info.uri().getSchemeSpecificPart().equals(ip.toString() + ":" + port)) {
andreaeb70a942015-10-16 21:34:46 -0700215 return netconfDeviceMap.get(info);
216 }
217 }
Andrea Campanella1cd641b2015-12-07 17:28:34 -0800218 return null;
andreaeb70a942015-10-16 21:34:46 -0700219 }
220
221 @Override
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700222 public NetconfDevice connectDevice(DeviceId deviceId) throws NetconfException {
Sean Condon54d82432017-07-26 22:27:25 +0100223 NetconfDeviceConfig netCfg = netCfgService.getConfig(
224 deviceId, NetconfDeviceConfig.class);
225 NetconfDeviceInfo deviceInfo = null;
226
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700227 if (netconfDeviceMap.containsKey(deviceId)) {
228 log.debug("Device {} is already present", deviceId);
229 return netconfDeviceMap.get(deviceId);
Sean Condon54d82432017-07-26 22:27:25 +0100230 } else if (netCfg != null) {
231 log.debug("Device {} is present in NetworkConfig", deviceId);
232 deviceInfo = new NetconfDeviceInfo(netCfg);
233
andreaeb70a942015-10-16 21:34:46 -0700234 } else {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700235 log.debug("Creating NETCONF device {}", deviceId);
236 Device device = deviceService.getDevice(deviceId);
237 String ip;
238 int port;
239 if (device != null) {
240 ip = device.annotations().value("ipaddress");
241 port = Integer.parseInt(device.annotations().value("port"));
242 } else {
243 String[] info = deviceId.toString().split(":");
244 if (info.length == 3) {
245 ip = info[1];
246 port = Integer.parseInt(info[2]);
247 } else {
248 ip = Arrays.asList(info).stream().filter(el -> !el.equals(info[0])
Andrea Campanellac3627842017-04-04 18:06:54 +0200249 && !el.equals(info[info.length - 1]))
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700250 .reduce((t, u) -> t + ":" + u)
251 .get();
252 log.debug("ip v6 {}", ip);
253 port = Integer.parseInt(info[info.length - 1]);
254 }
255 }
256 try {
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600257 DeviceKey deviceKey = deviceKeyService.getDeviceKey(
Andrea Campanellac3627842017-04-04 18:06:54 +0200258 DeviceKeyId.deviceKeyId(deviceId.toString()));
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600259 if (deviceKey.type() == DeviceKey.Type.USERNAME_PASSWORD) {
260 UsernamePassword usernamepasswd = deviceKey.asUsernamePassword();
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700261
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600262 deviceInfo = new NetconfDeviceInfo(usernamepasswd.username(),
Andrea Campanellac3627842017-04-04 18:06:54 +0200263 usernamepasswd.password(),
264 IpAddress.valueOf(ip),
265 port);
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600266
267 } else if (deviceKey.type() == DeviceKey.Type.SSL_KEY) {
268 String username = deviceKey.annotations().value(AnnotationKeys.USERNAME);
269 String password = deviceKey.annotations().value(AnnotationKeys.PASSWORD);
Andrea Campanellac3627842017-04-04 18:06:54 +0200270 String sshkey = deviceKey.annotations().value(AnnotationKeys.SSHKEY);
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600271
272 deviceInfo = new NetconfDeviceInfo(username,
Andrea Campanellac3627842017-04-04 18:06:54 +0200273 password,
274 IpAddress.valueOf(ip),
275 port,
276 sshkey);
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600277 } else {
278 log.error("Unknown device key for device {}", deviceId);
279 }
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700280 } catch (NullPointerException e) {
281 throw new NetconfException("No Device Key for device " + deviceId, e);
282 }
andreaeb70a942015-10-16 21:34:46 -0700283 }
Sean Condon54d82432017-07-26 22:27:25 +0100284 NetconfDevice netconfDevicedevice = createDevice(deviceInfo);
285 netconfDevicedevice.getSession().addDeviceOutputListener(downListener);
286 return netconfDevicedevice;
andreaeb70a942015-10-16 21:34:46 -0700287 }
288
289 @Override
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700290 public void disconnectDevice(DeviceId deviceId, boolean remove) {
291 if (!netconfDeviceMap.containsKey(deviceId)) {
292 log.warn("Device {} is not present", deviceId);
andreaeb70a942015-10-16 21:34:46 -0700293 } else {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700294 stopDevice(deviceId, remove);
andreaeb70a942015-10-16 21:34:46 -0700295 }
296 }
297
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700298 private void stopDevice(DeviceId deviceId, boolean remove) {
299 netconfDeviceMap.get(deviceId).disconnect();
300 netconfDeviceMap.remove(deviceId);
301 if (remove) {
Andrea Campanella86294db2016-03-07 11:42:49 -0800302 for (NetconfDeviceListener l : netconfDeviceListeners) {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700303 l.deviceRemoved(deviceId);
304 }
305 }
306 }
307
308 @Override
309 public void removeDevice(DeviceId deviceId) {
310 if (!netconfDeviceMap.containsKey(deviceId)) {
311 log.warn("Device {} is not present", deviceId);
312 for (NetconfDeviceListener l : netconfDeviceListeners) {
313 l.deviceRemoved(deviceId);
314 }
315 } else {
316 netconfDeviceMap.remove(deviceId);
317 for (NetconfDeviceListener l : netconfDeviceListeners) {
318 l.deviceRemoved(deviceId);
Andrea Campanella86294db2016-03-07 11:42:49 -0800319 }
320 }
321 }
322
Andrea Campanella101417d2015-12-11 17:58:07 -0800323 private NetconfDevice createDevice(NetconfDeviceInfo deviceInfo) throws NetconfException {
Andrea Campanella950310c2016-02-12 18:14:38 -0800324 NetconfDevice netconfDevice = deviceFactory.createNetconfDevice(deviceInfo);
Andrea Campanella087ceb92015-12-07 09:58:34 -0800325 netconfDeviceMap.put(deviceInfo.getDeviceId(), netconfDevice);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700326 for (NetconfDeviceListener l : netconfDeviceListeners) {
327 l.deviceAdded(deviceInfo.getDeviceId());
328 }
andreaeb70a942015-10-16 21:34:46 -0700329 return netconfDevice;
330 }
331
andreaeb70a942015-10-16 21:34:46 -0700332
333 @Override
334 public Map<DeviceId, NetconfDevice> getDevicesMap() {
335 return netconfDeviceMap;
336 }
Andrea Campanella950310c2016-02-12 18:14:38 -0800337
Andrea Campanella86294db2016-03-07 11:42:49 -0800338 @Override
339 public Set<DeviceId> getNetconfDevices() {
340 return netconfDeviceMap.keySet();
341 }
342
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700343
Andrea Campanella950310c2016-02-12 18:14:38 -0800344 //Device factory for the specific NetconfDeviceImpl
345 private class DefaultNetconfDeviceFactory implements NetconfDeviceFactory {
346
347 @Override
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700348 public NetconfDevice createNetconfDevice(NetconfDeviceInfo netconfDeviceInfo)
349 throws NetconfException {
Sean Condon54d82432017-07-26 22:27:25 +0100350 if (NetconfSshClientLib.ETHZ_SSH2.equals(netconfDeviceInfo.sshClientLib()) ||
351 NetconfSshClientLib.ETHZ_SSH2.equals(sshLibrary)) {
352 log.info("Creating NETCONF session to {} with {}",
353 netconfDeviceInfo.name(), NetconfSshClientLib.ETHZ_SSH2);
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700354 return new DefaultNetconfDevice(netconfDeviceInfo,
Sean Condon54d82432017-07-26 22:27:25 +0100355 new NetconfSessionImpl.SshNetconfSessionFactory());
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700356 }
Sean Condon54d82432017-07-26 22:27:25 +0100357 log.info("Creating NETCONF session to {} with {}",
358 netconfDeviceInfo.getDeviceId(), NetconfSshClientLib.APACHE_MINA);
Andrea Campanella950310c2016-02-12 18:14:38 -0800359 return new DefaultNetconfDevice(netconfDeviceInfo);
360 }
361 }
Andrea Campanella2464dc32016-02-17 17:54:53 -0800362
Yuta HIGUCHIf7089102017-05-03 10:31:46 -0700363 //Listener for closed session with devices, gets triggered when devices goes down
364 // or sends the end pattern ]]>]]>
Andrea Campanella2464dc32016-02-17 17:54:53 -0800365 private class DeviceDownEventListener implements NetconfDeviceOutputEventListener {
366
367 @Override
368 public void event(NetconfDeviceOutputEvent event) {
Andrea Campanellac3627842017-04-04 18:06:54 +0200369 DeviceId did = event.getDeviceInfo().getDeviceId();
Andrea Campanella2464dc32016-02-17 17:54:53 -0800370 if (event.type().equals(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED)) {
Andrea Campanellac3627842017-04-04 18:06:54 +0200371 removeDevice(did);
372 } else if (event.type().equals(NetconfDeviceOutputEvent.Type.SESSION_CLOSED)) {
373 log.info("Trying to reestablish connection with device {}", did);
374 executor.execute(() -> {
375 try {
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700376 NetconfDevice device = netconfDeviceMap.get(did);
377 if (device != null) {
378 device.getSession().checkAndReestablish();
379 log.info("Connection with device {} was reestablished", did);
380 } else {
381 log.warn("The device {} is not in the system", did);
382 }
383
Andrea Campanellac3627842017-04-04 18:06:54 +0200384 } catch (NetconfException e) {
385 log.error("The SSH connection with device {} couldn't be " +
Sean Condon54d82432017-07-26 22:27:25 +0100386 "reestablished due to {}. " +
387 "Marking the device as unreachable", e.getMessage());
Andrea Campanellac3627842017-04-04 18:06:54 +0200388 log.debug("Complete exception: ", e);
389 removeDevice(did);
390 }
391 });
Andrea Campanella2464dc32016-02-17 17:54:53 -0800392 }
393 }
394
395 @Override
396 public boolean isRelevant(NetconfDeviceOutputEvent event) {
397 return getDevicesMap().containsKey(event.getDeviceInfo().getDeviceId());
398 }
399 }
andreaeb70a942015-10-16 21:34:46 -0700400}