blob: 75dce05f09bd0f5747fd3e8f2edb015a0cf20d4e [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
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,
Yuta HIGUCHI5233dec2018-05-02 15:22:37 -070098 label = "Ssh client library to use")
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<>();
Yuta HIGUCHI2ee4fba2018-06-12 16:21:06 -0700121 protected NetconfDeviceFactory deviceFactory = (deviceInfo) -> new DefaultNetconfDevice(deviceInfo);
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);
Yuta HIGUCHIe9761742017-09-10 15:10:19 -0700131 Security.addProvider(new BouncyCastleProvider());
andreaeb70a942015-10-16 21:34:46 -0700132 log.info("Started");
133 }
134
135 @Deactivate
136 public void deactivate() {
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700137 netconfDeviceMap.values().forEach(device -> {
138 device.getSession().removeDeviceOutputListener(downListener);
139 device.disconnect();
140 });
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300141 cfgService.unregisterProperties(getClass(), false);
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700142 netconfDeviceListeners.clear();
andreaeb70a942015-10-16 21:34:46 -0700143 netconfDeviceMap.clear();
Holger Schulz092cbbf2017-08-31 17:52:30 +0200144 Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME);
andreaeb70a942015-10-16 21:34:46 -0700145 log.info("Stopped");
146 }
147
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300148 @Modified
149 public void modified(ComponentContext context) {
150 if (context == null) {
151 netconfReplyTimeout = DEFAULT_REPLY_TIMEOUT_SECONDS;
Sean Condon334ad692016-12-13 17:56:56 +0000152 netconfConnectTimeout = DEFAULT_CONNECT_TIMEOUT_SECONDS;
Sean Condon54d82432017-07-26 22:27:25 +0100153 netconfIdleTimeout = DEFAULT_IDLE_TIMEOUT_SECONDS;
154 sshLibrary = NetconfSshClientLib.APACHE_MINA;
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300155 log.info("No component configuration");
156 return;
157 }
158
159 Dictionary<?, ?> properties = context.getProperties();
160
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700161 String newSshLibrary;
Sean Condon334ad692016-12-13 17:56:56 +0000162
Sean Condon54d82432017-07-26 22:27:25 +0100163 int newNetconfReplyTimeout = getIntegerProperty(
164 properties, PROP_NETCONF_REPLY_TIMEOUT, netconfReplyTimeout);
165 int newNetconfConnectTimeout = getIntegerProperty(
166 properties, PROP_NETCONF_CONNECT_TIMEOUT, netconfConnectTimeout);
167 int newNetconfIdleTimeout = getIntegerProperty(
168 properties, PROP_NETCONF_IDLE_TIMEOUT, netconfIdleTimeout);
Sean Condon334ad692016-12-13 17:56:56 +0000169
Sean Condon54d82432017-07-26 22:27:25 +0100170 newSshLibrary = get(properties, SSH_LIBRARY);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300171
Sean Condon334ad692016-12-13 17:56:56 +0000172 if (newNetconfConnectTimeout < 0) {
173 log.warn("netconfConnectTimeout is invalid - less than 0");
174 return;
175 } else if (newNetconfReplyTimeout <= 0) {
176 log.warn("netconfReplyTimeout is invalid - 0 or less.");
177 return;
Sean Condon7347de92017-07-21 12:17:25 +0100178 } else if (newNetconfIdleTimeout <= 0) {
179 log.warn("netconfIdleTimeout is invalid - 0 or less.");
180 return;
Sean Condon334ad692016-12-13 17:56:56 +0000181 }
182
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300183 netconfReplyTimeout = newNetconfReplyTimeout;
Sean Condon334ad692016-12-13 17:56:56 +0000184 netconfConnectTimeout = newNetconfConnectTimeout;
Sean Condon7347de92017-07-21 12:17:25 +0100185 netconfIdleTimeout = newNetconfIdleTimeout;
Sean Condon54d82432017-07-26 22:27:25 +0100186 if (newSshLibrary != null) {
187 sshLibrary = NetconfSshClientLib.getEnum(newSshLibrary);
188 }
189 log.info("Settings: {} = {}, {} = {}, {} = {}, {} = {}",
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700190 PROP_NETCONF_REPLY_TIMEOUT, netconfReplyTimeout,
191 PROP_NETCONF_CONNECT_TIMEOUT, netconfConnectTimeout,
Sean Condon7347de92017-07-21 12:17:25 +0100192 PROP_NETCONF_IDLE_TIMEOUT, netconfIdleTimeout,
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700193 SSH_LIBRARY, sshLibrary);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300194 }
195
andreaeb70a942015-10-16 21:34:46 -0700196 @Override
197 public void addDeviceListener(NetconfDeviceListener listener) {
198 if (!netconfDeviceListeners.contains(listener)) {
199 netconfDeviceListeners.add(listener);
200 }
201 }
202
203 @Override
204 public void removeDeviceListener(NetconfDeviceListener listener) {
205 netconfDeviceListeners.remove(listener);
206 }
207
208 @Override
209 public NetconfDevice getNetconfDevice(DeviceId deviceInfo) {
210 return netconfDeviceMap.get(deviceInfo);
211 }
212
213 @Override
214 public NetconfDevice getNetconfDevice(IpAddress ip, int port) {
andreaeb70a942015-10-16 21:34:46 -0700215 for (DeviceId info : netconfDeviceMap.keySet()) {
Andrea Campanella57efbb22016-02-11 14:21:41 -0800216 if (info.uri().getSchemeSpecificPart().equals(ip.toString() + ":" + port)) {
andreaeb70a942015-10-16 21:34:46 -0700217 return netconfDeviceMap.get(info);
218 }
219 }
Andrea Campanella1cd641b2015-12-07 17:28:34 -0800220 return null;
andreaeb70a942015-10-16 21:34:46 -0700221 }
222
223 @Override
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700224 public NetconfDevice connectDevice(DeviceId deviceId) throws NetconfException {
Sean Condon54d82432017-07-26 22:27:25 +0100225 NetconfDeviceConfig netCfg = netCfgService.getConfig(
226 deviceId, NetconfDeviceConfig.class);
227 NetconfDeviceInfo deviceInfo = null;
228
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700229 if (netconfDeviceMap.containsKey(deviceId)) {
230 log.debug("Device {} is already present", deviceId);
231 return netconfDeviceMap.get(deviceId);
Sean Condon54d82432017-07-26 22:27:25 +0100232 } else if (netCfg != null) {
233 log.debug("Device {} is present in NetworkConfig", deviceId);
234 deviceInfo = new NetconfDeviceInfo(netCfg);
235
andreaeb70a942015-10-16 21:34:46 -0700236 } else {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700237 log.debug("Creating NETCONF device {}", deviceId);
238 Device device = deviceService.getDevice(deviceId);
239 String ip;
240 int port;
241 if (device != null) {
242 ip = device.annotations().value("ipaddress");
243 port = Integer.parseInt(device.annotations().value("port"));
244 } else {
245 String[] info = deviceId.toString().split(":");
246 if (info.length == 3) {
247 ip = info[1];
248 port = Integer.parseInt(info[2]);
249 } else {
250 ip = Arrays.asList(info).stream().filter(el -> !el.equals(info[0])
Andrea Campanellac3627842017-04-04 18:06:54 +0200251 && !el.equals(info[info.length - 1]))
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700252 .reduce((t, u) -> t + ":" + u)
253 .get();
254 log.debug("ip v6 {}", ip);
255 port = Integer.parseInt(info[info.length - 1]);
256 }
257 }
258 try {
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600259 DeviceKey deviceKey = deviceKeyService.getDeviceKey(
Andrea Campanellac3627842017-04-04 18:06:54 +0200260 DeviceKeyId.deviceKeyId(deviceId.toString()));
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600261 if (deviceKey.type() == DeviceKey.Type.USERNAME_PASSWORD) {
262 UsernamePassword usernamepasswd = deviceKey.asUsernamePassword();
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700263
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600264 deviceInfo = new NetconfDeviceInfo(usernamepasswd.username(),
Andrea Campanellac3627842017-04-04 18:06:54 +0200265 usernamepasswd.password(),
266 IpAddress.valueOf(ip),
267 port);
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600268
269 } else if (deviceKey.type() == DeviceKey.Type.SSL_KEY) {
270 String username = deviceKey.annotations().value(AnnotationKeys.USERNAME);
271 String password = deviceKey.annotations().value(AnnotationKeys.PASSWORD);
Andrea Campanellac3627842017-04-04 18:06:54 +0200272 String sshkey = deviceKey.annotations().value(AnnotationKeys.SSHKEY);
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600273
274 deviceInfo = new NetconfDeviceInfo(username,
Andrea Campanellac3627842017-04-04 18:06:54 +0200275 password,
276 IpAddress.valueOf(ip),
277 port,
278 sshkey);
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600279 } else {
280 log.error("Unknown device key for device {}", deviceId);
281 }
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700282 } catch (NullPointerException e) {
283 throw new NetconfException("No Device Key for device " + deviceId, e);
284 }
andreaeb70a942015-10-16 21:34:46 -0700285 }
Sean Condon54d82432017-07-26 22:27:25 +0100286 NetconfDevice netconfDevicedevice = createDevice(deviceInfo);
287 netconfDevicedevice.getSession().addDeviceOutputListener(downListener);
288 return netconfDevicedevice;
andreaeb70a942015-10-16 21:34:46 -0700289 }
290
291 @Override
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700292 public void disconnectDevice(DeviceId deviceId, boolean remove) {
293 if (!netconfDeviceMap.containsKey(deviceId)) {
294 log.warn("Device {} is not present", deviceId);
andreaeb70a942015-10-16 21:34:46 -0700295 } else {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700296 stopDevice(deviceId, remove);
andreaeb70a942015-10-16 21:34:46 -0700297 }
298 }
299
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700300 private void stopDevice(DeviceId deviceId, boolean remove) {
301 netconfDeviceMap.get(deviceId).disconnect();
302 netconfDeviceMap.remove(deviceId);
303 if (remove) {
Andrea Campanella86294db2016-03-07 11:42:49 -0800304 for (NetconfDeviceListener l : netconfDeviceListeners) {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700305 l.deviceRemoved(deviceId);
306 }
307 }
308 }
309
310 @Override
311 public void removeDevice(DeviceId deviceId) {
312 if (!netconfDeviceMap.containsKey(deviceId)) {
313 log.warn("Device {} is not present", deviceId);
314 for (NetconfDeviceListener l : netconfDeviceListeners) {
315 l.deviceRemoved(deviceId);
316 }
317 } else {
zhongguo zhao78eab372018-08-27 16:22:39 +0800318 stopDevice(deviceId, true);
Andrea Campanella86294db2016-03-07 11:42:49 -0800319 }
320 }
321
Andrea Campanella101417d2015-12-11 17:58:07 -0800322 private NetconfDevice createDevice(NetconfDeviceInfo deviceInfo) throws NetconfException {
Andrea Campanella950310c2016-02-12 18:14:38 -0800323 NetconfDevice netconfDevice = deviceFactory.createNetconfDevice(deviceInfo);
Andrea Campanella087ceb92015-12-07 09:58:34 -0800324 netconfDeviceMap.put(deviceInfo.getDeviceId(), netconfDevice);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700325 for (NetconfDeviceListener l : netconfDeviceListeners) {
326 l.deviceAdded(deviceInfo.getDeviceId());
327 }
andreaeb70a942015-10-16 21:34:46 -0700328 return netconfDevice;
329 }
330
andreaeb70a942015-10-16 21:34:46 -0700331
332 @Override
333 public Map<DeviceId, NetconfDevice> getDevicesMap() {
334 return netconfDeviceMap;
335 }
Andrea Campanella950310c2016-02-12 18:14:38 -0800336
Andrea Campanella86294db2016-03-07 11:42:49 -0800337 @Override
338 public Set<DeviceId> getNetconfDevices() {
339 return netconfDeviceMap.keySet();
340 }
341
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700342
Yuta HIGUCHI2ee4fba2018-06-12 16:21:06 -0700343 /**
344 * Device factory for the specific NetconfDeviceImpl.
345 *
346 * @deprecated in 1.14.0
347 */
348 @Deprecated
Andrea Campanella950310c2016-02-12 18:14:38 -0800349 private class DefaultNetconfDeviceFactory implements NetconfDeviceFactory {
350
351 @Override
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700352 public NetconfDevice createNetconfDevice(NetconfDeviceInfo netconfDeviceInfo)
353 throws NetconfException {
Andrea Campanella950310c2016-02-12 18:14:38 -0800354 return new DefaultNetconfDevice(netconfDeviceInfo);
355 }
356 }
Andrea Campanella2464dc32016-02-17 17:54:53 -0800357
Yuta HIGUCHIf7089102017-05-03 10:31:46 -0700358 //Listener for closed session with devices, gets triggered when devices goes down
359 // or sends the end pattern ]]>]]>
Andrea Campanella2464dc32016-02-17 17:54:53 -0800360 private class DeviceDownEventListener implements NetconfDeviceOutputEventListener {
361
362 @Override
363 public void event(NetconfDeviceOutputEvent event) {
Andrea Campanellac3627842017-04-04 18:06:54 +0200364 DeviceId did = event.getDeviceInfo().getDeviceId();
Andrea Campanella2464dc32016-02-17 17:54:53 -0800365 if (event.type().equals(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED)) {
Andrea Campanellac3627842017-04-04 18:06:54 +0200366 removeDevice(did);
367 } else if (event.type().equals(NetconfDeviceOutputEvent.Type.SESSION_CLOSED)) {
368 log.info("Trying to reestablish connection with device {}", did);
369 executor.execute(() -> {
370 try {
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700371 NetconfDevice device = netconfDeviceMap.get(did);
372 if (device != null) {
373 device.getSession().checkAndReestablish();
374 log.info("Connection with device {} was reestablished", did);
375 } else {
376 log.warn("The device {} is not in the system", did);
377 }
378
Andrea Campanellac3627842017-04-04 18:06:54 +0200379 } catch (NetconfException e) {
380 log.error("The SSH connection with device {} couldn't be " +
Sean Condon54d82432017-07-26 22:27:25 +0100381 "reestablished due to {}. " +
382 "Marking the device as unreachable", e.getMessage());
Andrea Campanellac3627842017-04-04 18:06:54 +0200383 log.debug("Complete exception: ", e);
384 removeDevice(did);
385 }
386 });
Andrea Campanella2464dc32016-02-17 17:54:53 -0800387 }
388 }
389
390 @Override
391 public boolean isRelevant(NetconfDeviceOutputEvent event) {
392 return getDevicesMap().containsKey(event.getDeviceInfo().getDeviceId());
393 }
394 }
andreaeb70a942015-10-16 21:34:46 -0700395}