blob: 5e6d7f917b1b1c1cea335d1d371735a33304979b [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
Sean Condon334ad692016-12-13 17:56:56 +000074 private static final int DEFAULT_CONNECT_TIMEOUT_SECONDS = 5;
75 private static final String PROP_NETCONF_CONNECT_TIMEOUT = "netconfConnectTimeout";
76 @Property(name = PROP_NETCONF_CONNECT_TIMEOUT, intValue = DEFAULT_CONNECT_TIMEOUT_SECONDS,
77 label = "Time (in seconds) to wait for a NETCONF connect.")
78 protected static int netconfConnectTimeout = DEFAULT_CONNECT_TIMEOUT_SECONDS;
79
Andreas Papazois4752cfa2016-04-25 14:52:12 +030080 private static final String PROP_NETCONF_REPLY_TIMEOUT = "netconfReplyTimeout";
81 private static final int DEFAULT_REPLY_TIMEOUT_SECONDS = 5;
82 @Property(name = PROP_NETCONF_REPLY_TIMEOUT, intValue = DEFAULT_REPLY_TIMEOUT_SECONDS,
83 label = "Time (in seconds) waiting for a NetConf reply")
84 protected static int netconfReplyTimeout = DEFAULT_REPLY_TIMEOUT_SECONDS;
85
Sean Condon7347de92017-07-21 12:17:25 +010086 private static final String PROP_NETCONF_IDLE_TIMEOUT = "netconfIdleTimeout";
87 private static final int DEFAULT_IDLE_TIMEOUT_SECONDS = 300;
88 @Property(name = PROP_NETCONF_IDLE_TIMEOUT, intValue = DEFAULT_IDLE_TIMEOUT_SECONDS,
89 label = "Time (in seconds) SSH session will close if no traffic seen")
90 protected static int netconfIdleTimeout = DEFAULT_IDLE_TIMEOUT_SECONDS;
91
Andrea Campanella7bbe7b12017-05-03 16:03:38 -070092 private static final String SSH_LIBRARY = "sshLibrary";
Sean Condon54d82432017-07-26 22:27:25 +010093 private static final String APACHE_MINA_STR = "apache-mina";
94 @Property(name = SSH_LIBRARY, value = APACHE_MINA_STR,
Sean Condon7347de92017-07-21 12:17:25 +010095 label = "Ssh Library instead of apache_mina (i.e. ethz-ssh2")
Sean Condon54d82432017-07-26 22:27:25 +010096 protected static NetconfSshClientLib sshLibrary = NetconfSshClientLib.APACHE_MINA;
Andrea Campanella7bbe7b12017-05-03 16:03:38 -070097
Andreas Papazois4752cfa2016-04-25 14:52:12 +030098 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
99 protected ComponentConfigService cfgService;
100
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700101 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
102 protected DeviceService deviceService;
103
104 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
105 protected DeviceKeyService deviceKeyService;
andreaeb70a942015-10-16 21:34:46 -0700106
Sean Condon54d82432017-07-26 22:27:25 +0100107 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
108 protected NetworkConfigRegistry netCfgService;
109
andreaeb70a942015-10-16 21:34:46 -0700110 public static final Logger log = LoggerFactory
111 .getLogger(NetconfControllerImpl.class);
112
Andrea Campanella8b1cb672016-01-25 13:58:58 -0800113 private Map<DeviceId, NetconfDevice> netconfDeviceMap = new ConcurrentHashMap<>();
andreaeb70a942015-10-16 21:34:46 -0700114
Andrea Campanella2464dc32016-02-17 17:54:53 -0800115 private final NetconfDeviceOutputEventListener downListener = new DeviceDownEventListener();
116
andreaeb70a942015-10-16 21:34:46 -0700117 protected Set<NetconfDeviceListener> netconfDeviceListeners = new CopyOnWriteArraySet<>();
Andrea Campanella950310c2016-02-12 18:14:38 -0800118 protected NetconfDeviceFactory deviceFactory = new DefaultNetconfDeviceFactory();
andreaeb70a942015-10-16 21:34:46 -0700119
Andrea Campanellac3627842017-04-04 18:06:54 +0200120 protected final ExecutorService executor =
121 Executors.newCachedThreadPool(groupedThreads("onos/netconfdevicecontroller",
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700122 "connection-reopen-%d", log));
Andrea Campanellac3627842017-04-04 18:06:54 +0200123
andreaeb70a942015-10-16 21:34:46 -0700124 @Activate
125 public void activate(ComponentContext context) {
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300126 cfgService.registerProperties(getClass());
127 modified(context);
andreaeb70a942015-10-16 21:34:46 -0700128 log.info("Started");
129 }
130
131 @Deactivate
132 public void deactivate() {
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700133 netconfDeviceMap.values().forEach(device -> {
134 device.getSession().removeDeviceOutputListener(downListener);
135 device.disconnect();
136 });
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300137 cfgService.unregisterProperties(getClass(), false);
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700138 netconfDeviceListeners.clear();
andreaeb70a942015-10-16 21:34:46 -0700139 netconfDeviceMap.clear();
140 log.info("Stopped");
141 }
142
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300143 @Modified
144 public void modified(ComponentContext context) {
145 if (context == null) {
146 netconfReplyTimeout = DEFAULT_REPLY_TIMEOUT_SECONDS;
Sean Condon334ad692016-12-13 17:56:56 +0000147 netconfConnectTimeout = DEFAULT_CONNECT_TIMEOUT_SECONDS;
Sean Condon54d82432017-07-26 22:27:25 +0100148 netconfIdleTimeout = DEFAULT_IDLE_TIMEOUT_SECONDS;
149 sshLibrary = NetconfSshClientLib.APACHE_MINA;
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300150 log.info("No component configuration");
151 return;
152 }
153
154 Dictionary<?, ?> properties = context.getProperties();
155
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700156 String newSshLibrary;
Sean Condon334ad692016-12-13 17:56:56 +0000157
Sean Condon54d82432017-07-26 22:27:25 +0100158 int newNetconfReplyTimeout = getIntegerProperty(
159 properties, PROP_NETCONF_REPLY_TIMEOUT, netconfReplyTimeout);
160 int newNetconfConnectTimeout = getIntegerProperty(
161 properties, PROP_NETCONF_CONNECT_TIMEOUT, netconfConnectTimeout);
162 int newNetconfIdleTimeout = getIntegerProperty(
163 properties, PROP_NETCONF_IDLE_TIMEOUT, netconfIdleTimeout);
Sean Condon334ad692016-12-13 17:56:56 +0000164
Sean Condon54d82432017-07-26 22:27:25 +0100165 newSshLibrary = get(properties, SSH_LIBRARY);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300166
Sean Condon334ad692016-12-13 17:56:56 +0000167 if (newNetconfConnectTimeout < 0) {
168 log.warn("netconfConnectTimeout is invalid - less than 0");
169 return;
170 } else if (newNetconfReplyTimeout <= 0) {
171 log.warn("netconfReplyTimeout is invalid - 0 or less.");
172 return;
Sean Condon7347de92017-07-21 12:17:25 +0100173 } else if (newNetconfIdleTimeout <= 0) {
174 log.warn("netconfIdleTimeout is invalid - 0 or less.");
175 return;
Sean Condon334ad692016-12-13 17:56:56 +0000176 }
177
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300178 netconfReplyTimeout = newNetconfReplyTimeout;
Sean Condon334ad692016-12-13 17:56:56 +0000179 netconfConnectTimeout = newNetconfConnectTimeout;
Sean Condon7347de92017-07-21 12:17:25 +0100180 netconfIdleTimeout = newNetconfIdleTimeout;
Sean Condon54d82432017-07-26 22:27:25 +0100181 if (newSshLibrary != null) {
182 sshLibrary = NetconfSshClientLib.getEnum(newSshLibrary);
183 }
184 log.info("Settings: {} = {}, {} = {}, {} = {}, {} = {}",
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700185 PROP_NETCONF_REPLY_TIMEOUT, netconfReplyTimeout,
186 PROP_NETCONF_CONNECT_TIMEOUT, netconfConnectTimeout,
Sean Condon7347de92017-07-21 12:17:25 +0100187 PROP_NETCONF_IDLE_TIMEOUT, netconfIdleTimeout,
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700188 SSH_LIBRARY, sshLibrary);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300189 }
190
andreaeb70a942015-10-16 21:34:46 -0700191 @Override
192 public void addDeviceListener(NetconfDeviceListener listener) {
193 if (!netconfDeviceListeners.contains(listener)) {
194 netconfDeviceListeners.add(listener);
195 }
196 }
197
198 @Override
199 public void removeDeviceListener(NetconfDeviceListener listener) {
200 netconfDeviceListeners.remove(listener);
201 }
202
203 @Override
204 public NetconfDevice getNetconfDevice(DeviceId deviceInfo) {
205 return netconfDeviceMap.get(deviceInfo);
206 }
207
208 @Override
209 public NetconfDevice getNetconfDevice(IpAddress ip, int port) {
andreaeb70a942015-10-16 21:34:46 -0700210 for (DeviceId info : netconfDeviceMap.keySet()) {
Andrea Campanella57efbb22016-02-11 14:21:41 -0800211 if (info.uri().getSchemeSpecificPart().equals(ip.toString() + ":" + port)) {
andreaeb70a942015-10-16 21:34:46 -0700212 return netconfDeviceMap.get(info);
213 }
214 }
Andrea Campanella1cd641b2015-12-07 17:28:34 -0800215 return null;
andreaeb70a942015-10-16 21:34:46 -0700216 }
217
218 @Override
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700219 public NetconfDevice connectDevice(DeviceId deviceId) throws NetconfException {
Sean Condon54d82432017-07-26 22:27:25 +0100220 NetconfDeviceConfig netCfg = netCfgService.getConfig(
221 deviceId, NetconfDeviceConfig.class);
222 NetconfDeviceInfo deviceInfo = null;
223
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700224 if (netconfDeviceMap.containsKey(deviceId)) {
225 log.debug("Device {} is already present", deviceId);
226 return netconfDeviceMap.get(deviceId);
Sean Condon54d82432017-07-26 22:27:25 +0100227 } else if (netCfg != null) {
228 log.debug("Device {} is present in NetworkConfig", deviceId);
229 deviceInfo = new NetconfDeviceInfo(netCfg);
230
andreaeb70a942015-10-16 21:34:46 -0700231 } else {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700232 log.debug("Creating NETCONF device {}", deviceId);
233 Device device = deviceService.getDevice(deviceId);
234 String ip;
235 int port;
236 if (device != null) {
237 ip = device.annotations().value("ipaddress");
238 port = Integer.parseInt(device.annotations().value("port"));
239 } else {
240 String[] info = deviceId.toString().split(":");
241 if (info.length == 3) {
242 ip = info[1];
243 port = Integer.parseInt(info[2]);
244 } else {
245 ip = Arrays.asList(info).stream().filter(el -> !el.equals(info[0])
Andrea Campanellac3627842017-04-04 18:06:54 +0200246 && !el.equals(info[info.length - 1]))
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700247 .reduce((t, u) -> t + ":" + u)
248 .get();
249 log.debug("ip v6 {}", ip);
250 port = Integer.parseInt(info[info.length - 1]);
251 }
252 }
253 try {
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600254 DeviceKey deviceKey = deviceKeyService.getDeviceKey(
Andrea Campanellac3627842017-04-04 18:06:54 +0200255 DeviceKeyId.deviceKeyId(deviceId.toString()));
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600256 if (deviceKey.type() == DeviceKey.Type.USERNAME_PASSWORD) {
257 UsernamePassword usernamepasswd = deviceKey.asUsernamePassword();
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700258
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600259 deviceInfo = new NetconfDeviceInfo(usernamepasswd.username(),
Andrea Campanellac3627842017-04-04 18:06:54 +0200260 usernamepasswd.password(),
261 IpAddress.valueOf(ip),
262 port);
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600263
264 } else if (deviceKey.type() == DeviceKey.Type.SSL_KEY) {
265 String username = deviceKey.annotations().value(AnnotationKeys.USERNAME);
266 String password = deviceKey.annotations().value(AnnotationKeys.PASSWORD);
Andrea Campanellac3627842017-04-04 18:06:54 +0200267 String sshkey = deviceKey.annotations().value(AnnotationKeys.SSHKEY);
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600268
269 deviceInfo = new NetconfDeviceInfo(username,
Andrea Campanellac3627842017-04-04 18:06:54 +0200270 password,
271 IpAddress.valueOf(ip),
272 port,
273 sshkey);
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600274 } else {
275 log.error("Unknown device key for device {}", deviceId);
276 }
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700277 } catch (NullPointerException e) {
278 throw new NetconfException("No Device Key for device " + deviceId, e);
279 }
andreaeb70a942015-10-16 21:34:46 -0700280 }
Sean Condon54d82432017-07-26 22:27:25 +0100281 NetconfDevice netconfDevicedevice = createDevice(deviceInfo);
282 netconfDevicedevice.getSession().addDeviceOutputListener(downListener);
283 return netconfDevicedevice;
andreaeb70a942015-10-16 21:34:46 -0700284 }
285
286 @Override
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700287 public void disconnectDevice(DeviceId deviceId, boolean remove) {
288 if (!netconfDeviceMap.containsKey(deviceId)) {
289 log.warn("Device {} is not present", deviceId);
andreaeb70a942015-10-16 21:34:46 -0700290 } else {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700291 stopDevice(deviceId, remove);
andreaeb70a942015-10-16 21:34:46 -0700292 }
293 }
294
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700295 private void stopDevice(DeviceId deviceId, boolean remove) {
296 netconfDeviceMap.get(deviceId).disconnect();
297 netconfDeviceMap.remove(deviceId);
298 if (remove) {
Andrea Campanella86294db2016-03-07 11:42:49 -0800299 for (NetconfDeviceListener l : netconfDeviceListeners) {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700300 l.deviceRemoved(deviceId);
301 }
302 }
303 }
304
305 @Override
306 public void removeDevice(DeviceId deviceId) {
307 if (!netconfDeviceMap.containsKey(deviceId)) {
308 log.warn("Device {} is not present", deviceId);
309 for (NetconfDeviceListener l : netconfDeviceListeners) {
310 l.deviceRemoved(deviceId);
311 }
312 } else {
313 netconfDeviceMap.remove(deviceId);
314 for (NetconfDeviceListener l : netconfDeviceListeners) {
315 l.deviceRemoved(deviceId);
Andrea Campanella86294db2016-03-07 11:42:49 -0800316 }
317 }
318 }
319
Andrea Campanella101417d2015-12-11 17:58:07 -0800320 private NetconfDevice createDevice(NetconfDeviceInfo deviceInfo) throws NetconfException {
Andrea Campanella950310c2016-02-12 18:14:38 -0800321 NetconfDevice netconfDevice = deviceFactory.createNetconfDevice(deviceInfo);
Andrea Campanella087ceb92015-12-07 09:58:34 -0800322 netconfDeviceMap.put(deviceInfo.getDeviceId(), netconfDevice);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700323 for (NetconfDeviceListener l : netconfDeviceListeners) {
324 l.deviceAdded(deviceInfo.getDeviceId());
325 }
andreaeb70a942015-10-16 21:34:46 -0700326 return netconfDevice;
327 }
328
andreaeb70a942015-10-16 21:34:46 -0700329
330 @Override
331 public Map<DeviceId, NetconfDevice> getDevicesMap() {
332 return netconfDeviceMap;
333 }
Andrea Campanella950310c2016-02-12 18:14:38 -0800334
Andrea Campanella86294db2016-03-07 11:42:49 -0800335 @Override
336 public Set<DeviceId> getNetconfDevices() {
337 return netconfDeviceMap.keySet();
338 }
339
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700340
Andrea Campanella950310c2016-02-12 18:14:38 -0800341 //Device factory for the specific NetconfDeviceImpl
342 private class DefaultNetconfDeviceFactory implements NetconfDeviceFactory {
343
344 @Override
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700345 public NetconfDevice createNetconfDevice(NetconfDeviceInfo netconfDeviceInfo)
346 throws NetconfException {
Sean Condon54d82432017-07-26 22:27:25 +0100347 if (NetconfSshClientLib.ETHZ_SSH2.equals(netconfDeviceInfo.sshClientLib()) ||
348 NetconfSshClientLib.ETHZ_SSH2.equals(sshLibrary)) {
349 log.info("Creating NETCONF session to {} with {}",
350 netconfDeviceInfo.name(), NetconfSshClientLib.ETHZ_SSH2);
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700351 return new DefaultNetconfDevice(netconfDeviceInfo,
Sean Condon54d82432017-07-26 22:27:25 +0100352 new NetconfSessionImpl.SshNetconfSessionFactory());
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700353 }
Sean Condon54d82432017-07-26 22:27:25 +0100354 log.info("Creating NETCONF session to {} with {}",
355 netconfDeviceInfo.getDeviceId(), NetconfSshClientLib.APACHE_MINA);
Andrea Campanella950310c2016-02-12 18:14:38 -0800356 return new DefaultNetconfDevice(netconfDeviceInfo);
357 }
358 }
Andrea Campanella2464dc32016-02-17 17:54:53 -0800359
Yuta HIGUCHIf7089102017-05-03 10:31:46 -0700360 //Listener for closed session with devices, gets triggered when devices goes down
361 // or sends the end pattern ]]>]]>
Andrea Campanella2464dc32016-02-17 17:54:53 -0800362 private class DeviceDownEventListener implements NetconfDeviceOutputEventListener {
363
364 @Override
365 public void event(NetconfDeviceOutputEvent event) {
Andrea Campanellac3627842017-04-04 18:06:54 +0200366 DeviceId did = event.getDeviceInfo().getDeviceId();
Andrea Campanella2464dc32016-02-17 17:54:53 -0800367 if (event.type().equals(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED)) {
Andrea Campanellac3627842017-04-04 18:06:54 +0200368 removeDevice(did);
369 } else if (event.type().equals(NetconfDeviceOutputEvent.Type.SESSION_CLOSED)) {
370 log.info("Trying to reestablish connection with device {}", did);
371 executor.execute(() -> {
372 try {
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700373 NetconfDevice device = netconfDeviceMap.get(did);
374 if (device != null) {
375 device.getSession().checkAndReestablish();
376 log.info("Connection with device {} was reestablished", did);
377 } else {
378 log.warn("The device {} is not in the system", did);
379 }
380
Andrea Campanellac3627842017-04-04 18:06:54 +0200381 } catch (NetconfException e) {
382 log.error("The SSH connection with device {} couldn't be " +
Sean Condon54d82432017-07-26 22:27:25 +0100383 "reestablished due to {}. " +
384 "Marking the device as unreachable", e.getMessage());
Andrea Campanellac3627842017-04-04 18:06:54 +0200385 log.debug("Complete exception: ", e);
386 removeDevice(did);
387 }
388 });
Andrea Campanella2464dc32016-02-17 17:54:53 -0800389 }
390 }
391
392 @Override
393 public boolean isRelevant(NetconfDeviceOutputEvent event) {
394 return getDevicesMap().containsKey(event.getDeviceInfo().getDeviceId());
395 }
396 }
andreaeb70a942015-10-16 21:34:46 -0700397}