blob: e18714c39d07ddf443a50c66e20ff505202780b4 [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
Holger Schulz092cbbf2017-08-31 17:52:30 +020019import org.bouncycastle.jce.provider.BouncyCastleProvider;
andreaeb70a942015-10-16 21:34:46 -070020import org.onlab.packet.IpAddress;
Andreas Papazois4752cfa2016-04-25 14:52:12 +030021import org.onosproject.cfg.ComponentConfigService;
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -060022import org.onosproject.net.AnnotationKeys;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070023import org.onosproject.net.Device;
andreaeb70a942015-10-16 21:34:46 -070024import org.onosproject.net.DeviceId;
Sean Condon54d82432017-07-26 22:27:25 +010025import org.onosproject.net.config.NetworkConfigRegistry;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070026import org.onosproject.net.device.DeviceService;
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -060027import org.onosproject.net.key.DeviceKey;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070028import org.onosproject.net.key.DeviceKeyId;
29import org.onosproject.net.key.DeviceKeyService;
30import org.onosproject.net.key.UsernamePassword;
andreaeb70a942015-10-16 21:34:46 -070031import org.onosproject.netconf.NetconfController;
32import org.onosproject.netconf.NetconfDevice;
Andrea Campanella950310c2016-02-12 18:14:38 -080033import org.onosproject.netconf.NetconfDeviceFactory;
andreaeb70a942015-10-16 21:34:46 -070034import org.onosproject.netconf.NetconfDeviceInfo;
35import org.onosproject.netconf.NetconfDeviceListener;
Andrea Campanella2464dc32016-02-17 17:54:53 -080036import org.onosproject.netconf.NetconfDeviceOutputEvent;
37import org.onosproject.netconf.NetconfDeviceOutputEventListener;
Andrea Campanella101417d2015-12-11 17:58:07 -080038import org.onosproject.netconf.NetconfException;
Sean Condon54d82432017-07-26 22:27:25 +010039import org.onosproject.netconf.config.NetconfDeviceConfig;
40import org.onosproject.netconf.config.NetconfSshClientLib;
andreaeb70a942015-10-16 21:34:46 -070041import org.osgi.service.component.ComponentContext;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070042import org.osgi.service.component.annotations.Activate;
43import org.osgi.service.component.annotations.Component;
44import org.osgi.service.component.annotations.Deactivate;
45import org.osgi.service.component.annotations.Modified;
46import org.osgi.service.component.annotations.Reference;
47import org.osgi.service.component.annotations.ReferenceCardinality;
andreaeb70a942015-10-16 21:34:46 -070048import org.slf4j.Logger;
49import org.slf4j.LoggerFactory;
50
Holger Schulz092cbbf2017-08-31 17:52:30 +020051import java.security.Security;
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 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070068@Component(immediate = true, service = NetconfController.class)
andreaeb70a942015-10-16 21:34:46 -070069public class NetconfControllerImpl implements NetconfController {
Andrea Campanella7bbe7b12017-05-03 16:03:38 -070070
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -070071 protected static final int DEFAULT_CONNECT_TIMEOUT_SECONDS = 5;
Sean Condon334ad692016-12-13 17:56:56 +000072 private static final String PROP_NETCONF_CONNECT_TIMEOUT = "netconfConnectTimeout";
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -070073 // FIXME @Property should not be static
Ray Milkeyd84f89b2018-08-17 14:54:17 -070074 //@Property(name = PROP_NETCONF_CONNECT_TIMEOUT, intValue = DEFAULT_CONNECT_TIMEOUT_SECONDS,
75 // label = "Time (in seconds) to wait for a NETCONF connect.")
Sean Condon334ad692016-12-13 17:56:56 +000076 protected static int netconfConnectTimeout = DEFAULT_CONNECT_TIMEOUT_SECONDS;
77
Andreas Papazois4752cfa2016-04-25 14:52:12 +030078 private static final String PROP_NETCONF_REPLY_TIMEOUT = "netconfReplyTimeout";
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -070079 protected static final int DEFAULT_REPLY_TIMEOUT_SECONDS = 5;
80 // FIXME @Property should not be static
Ray Milkeyd84f89b2018-08-17 14:54:17 -070081 //@Property(name = PROP_NETCONF_REPLY_TIMEOUT, intValue = DEFAULT_REPLY_TIMEOUT_SECONDS,
82 // label = "Time (in seconds) waiting for a NetConf reply")
Andreas Papazois4752cfa2016-04-25 14:52:12 +030083 protected static int netconfReplyTimeout = DEFAULT_REPLY_TIMEOUT_SECONDS;
84
Sean Condon7347de92017-07-21 12:17:25 +010085 private static final String PROP_NETCONF_IDLE_TIMEOUT = "netconfIdleTimeout";
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -070086 protected static final int DEFAULT_IDLE_TIMEOUT_SECONDS = 300;
87 // FIXME @Property should not be static
Ray Milkeyd84f89b2018-08-17 14:54:17 -070088 //@Property(name = PROP_NETCONF_IDLE_TIMEOUT, intValue = DEFAULT_IDLE_TIMEOUT_SECONDS,
89 // label = "Time (in seconds) SSH session will close if no traffic seen")
Sean Condon7347de92017-07-21 12:17:25 +010090 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";
Ray Milkeyd84f89b2018-08-17 14:54:17 -070094 //@Property(name = SSH_LIBRARY, value = APACHE_MINA_STR,
95 // label = "Ssh client library to use")
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -070096 protected NetconfSshClientLib sshLibrary = NetconfSshClientLib.APACHE_MINA;
Andrea Campanella7bbe7b12017-05-03 16:03:38 -070097
Ray Milkeyd84f89b2018-08-17 14:54:17 -070098 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andreas Papazois4752cfa2016-04-25 14:52:12 +030099 protected ComponentConfigService cfgService;
100
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700101 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700102 protected DeviceService deviceService;
103
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700104 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700105 protected DeviceKeyService deviceKeyService;
andreaeb70a942015-10-16 21:34:46 -0700106
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700107 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Sean Condon54d82432017-07-26 22:27:25 +0100108 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<>();
Yuta HIGUCHI2ee4fba2018-06-12 16:21:06 -0700118 protected NetconfDeviceFactory deviceFactory = (deviceInfo) -> new DefaultNetconfDevice(deviceInfo);
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);
Yuta HIGUCHIe9761742017-09-10 15:10:19 -0700128 Security.addProvider(new BouncyCastleProvider());
andreaeb70a942015-10-16 21:34:46 -0700129 log.info("Started");
130 }
131
132 @Deactivate
133 public void deactivate() {
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700134 netconfDeviceMap.values().forEach(device -> {
135 device.getSession().removeDeviceOutputListener(downListener);
136 device.disconnect();
137 });
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300138 cfgService.unregisterProperties(getClass(), false);
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700139 netconfDeviceListeners.clear();
andreaeb70a942015-10-16 21:34:46 -0700140 netconfDeviceMap.clear();
Holger Schulz092cbbf2017-08-31 17:52:30 +0200141 Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME);
andreaeb70a942015-10-16 21:34:46 -0700142 log.info("Stopped");
143 }
144
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300145 @Modified
146 public void modified(ComponentContext context) {
147 if (context == null) {
148 netconfReplyTimeout = DEFAULT_REPLY_TIMEOUT_SECONDS;
Sean Condon334ad692016-12-13 17:56:56 +0000149 netconfConnectTimeout = DEFAULT_CONNECT_TIMEOUT_SECONDS;
Sean Condon54d82432017-07-26 22:27:25 +0100150 netconfIdleTimeout = DEFAULT_IDLE_TIMEOUT_SECONDS;
151 sshLibrary = NetconfSshClientLib.APACHE_MINA;
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300152 log.info("No component configuration");
153 return;
154 }
155
156 Dictionary<?, ?> properties = context.getProperties();
157
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700158 String newSshLibrary;
Sean Condon334ad692016-12-13 17:56:56 +0000159
Sean Condon54d82432017-07-26 22:27:25 +0100160 int newNetconfReplyTimeout = getIntegerProperty(
161 properties, PROP_NETCONF_REPLY_TIMEOUT, netconfReplyTimeout);
162 int newNetconfConnectTimeout = getIntegerProperty(
163 properties, PROP_NETCONF_CONNECT_TIMEOUT, netconfConnectTimeout);
164 int newNetconfIdleTimeout = getIntegerProperty(
165 properties, PROP_NETCONF_IDLE_TIMEOUT, netconfIdleTimeout);
Sean Condon334ad692016-12-13 17:56:56 +0000166
Sean Condon54d82432017-07-26 22:27:25 +0100167 newSshLibrary = get(properties, SSH_LIBRARY);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300168
Sean Condon334ad692016-12-13 17:56:56 +0000169 if (newNetconfConnectTimeout < 0) {
170 log.warn("netconfConnectTimeout is invalid - less than 0");
171 return;
172 } else if (newNetconfReplyTimeout <= 0) {
173 log.warn("netconfReplyTimeout is invalid - 0 or less.");
174 return;
Sean Condon7347de92017-07-21 12:17:25 +0100175 } else if (newNetconfIdleTimeout <= 0) {
176 log.warn("netconfIdleTimeout is invalid - 0 or less.");
177 return;
Sean Condon334ad692016-12-13 17:56:56 +0000178 }
179
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300180 netconfReplyTimeout = newNetconfReplyTimeout;
Sean Condon334ad692016-12-13 17:56:56 +0000181 netconfConnectTimeout = newNetconfConnectTimeout;
Sean Condon7347de92017-07-21 12:17:25 +0100182 netconfIdleTimeout = newNetconfIdleTimeout;
Sean Condon54d82432017-07-26 22:27:25 +0100183 if (newSshLibrary != null) {
184 sshLibrary = NetconfSshClientLib.getEnum(newSshLibrary);
185 }
186 log.info("Settings: {} = {}, {} = {}, {} = {}, {} = {}",
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700187 PROP_NETCONF_REPLY_TIMEOUT, netconfReplyTimeout,
188 PROP_NETCONF_CONNECT_TIMEOUT, netconfConnectTimeout,
Sean Condon7347de92017-07-21 12:17:25 +0100189 PROP_NETCONF_IDLE_TIMEOUT, netconfIdleTimeout,
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700190 SSH_LIBRARY, sshLibrary);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300191 }
192
andreaeb70a942015-10-16 21:34:46 -0700193 @Override
194 public void addDeviceListener(NetconfDeviceListener listener) {
195 if (!netconfDeviceListeners.contains(listener)) {
196 netconfDeviceListeners.add(listener);
197 }
198 }
199
200 @Override
201 public void removeDeviceListener(NetconfDeviceListener listener) {
202 netconfDeviceListeners.remove(listener);
203 }
204
205 @Override
206 public NetconfDevice getNetconfDevice(DeviceId deviceInfo) {
207 return netconfDeviceMap.get(deviceInfo);
208 }
209
210 @Override
211 public NetconfDevice getNetconfDevice(IpAddress ip, int port) {
andreaeb70a942015-10-16 21:34:46 -0700212 for (DeviceId info : netconfDeviceMap.keySet()) {
Andrea Campanella57efbb22016-02-11 14:21:41 -0800213 if (info.uri().getSchemeSpecificPart().equals(ip.toString() + ":" + port)) {
andreaeb70a942015-10-16 21:34:46 -0700214 return netconfDeviceMap.get(info);
215 }
216 }
Andrea Campanella1cd641b2015-12-07 17:28:34 -0800217 return null;
andreaeb70a942015-10-16 21:34:46 -0700218 }
219
220 @Override
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700221 public NetconfDevice connectDevice(DeviceId deviceId) throws NetconfException {
Sean Condon54d82432017-07-26 22:27:25 +0100222 NetconfDeviceConfig netCfg = netCfgService.getConfig(
223 deviceId, NetconfDeviceConfig.class);
224 NetconfDeviceInfo deviceInfo = null;
225
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700226 if (netconfDeviceMap.containsKey(deviceId)) {
227 log.debug("Device {} is already present", deviceId);
228 return netconfDeviceMap.get(deviceId);
Sean Condon54d82432017-07-26 22:27:25 +0100229 } else if (netCfg != null) {
230 log.debug("Device {} is present in NetworkConfig", deviceId);
231 deviceInfo = new NetconfDeviceInfo(netCfg);
232
andreaeb70a942015-10-16 21:34:46 -0700233 } else {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700234 log.debug("Creating NETCONF device {}", deviceId);
235 Device device = deviceService.getDevice(deviceId);
236 String ip;
237 int port;
238 if (device != null) {
239 ip = device.annotations().value("ipaddress");
240 port = Integer.parseInt(device.annotations().value("port"));
241 } else {
242 String[] info = deviceId.toString().split(":");
243 if (info.length == 3) {
244 ip = info[1];
245 port = Integer.parseInt(info[2]);
246 } else {
247 ip = Arrays.asList(info).stream().filter(el -> !el.equals(info[0])
Andrea Campanellac3627842017-04-04 18:06:54 +0200248 && !el.equals(info[info.length - 1]))
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700249 .reduce((t, u) -> t + ":" + u)
250 .get();
251 log.debug("ip v6 {}", ip);
252 port = Integer.parseInt(info[info.length - 1]);
253 }
254 }
255 try {
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600256 DeviceKey deviceKey = deviceKeyService.getDeviceKey(
Andrea Campanellac3627842017-04-04 18:06:54 +0200257 DeviceKeyId.deviceKeyId(deviceId.toString()));
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600258 if (deviceKey.type() == DeviceKey.Type.USERNAME_PASSWORD) {
259 UsernamePassword usernamepasswd = deviceKey.asUsernamePassword();
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700260
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600261 deviceInfo = new NetconfDeviceInfo(usernamepasswd.username(),
Andrea Campanellac3627842017-04-04 18:06:54 +0200262 usernamepasswd.password(),
263 IpAddress.valueOf(ip),
264 port);
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600265
266 } else if (deviceKey.type() == DeviceKey.Type.SSL_KEY) {
267 String username = deviceKey.annotations().value(AnnotationKeys.USERNAME);
268 String password = deviceKey.annotations().value(AnnotationKeys.PASSWORD);
Andrea Campanellac3627842017-04-04 18:06:54 +0200269 String sshkey = deviceKey.annotations().value(AnnotationKeys.SSHKEY);
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600270
271 deviceInfo = new NetconfDeviceInfo(username,
Andrea Campanellac3627842017-04-04 18:06:54 +0200272 password,
273 IpAddress.valueOf(ip),
274 port,
275 sshkey);
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600276 } else {
277 log.error("Unknown device key for device {}", deviceId);
278 }
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700279 } catch (NullPointerException e) {
280 throw new NetconfException("No Device Key for device " + deviceId, e);
281 }
andreaeb70a942015-10-16 21:34:46 -0700282 }
Sean Condon54d82432017-07-26 22:27:25 +0100283 NetconfDevice netconfDevicedevice = createDevice(deviceInfo);
284 netconfDevicedevice.getSession().addDeviceOutputListener(downListener);
285 return netconfDevicedevice;
andreaeb70a942015-10-16 21:34:46 -0700286 }
287
288 @Override
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700289 public void disconnectDevice(DeviceId deviceId, boolean remove) {
290 if (!netconfDeviceMap.containsKey(deviceId)) {
291 log.warn("Device {} is not present", deviceId);
andreaeb70a942015-10-16 21:34:46 -0700292 } else {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700293 stopDevice(deviceId, remove);
andreaeb70a942015-10-16 21:34:46 -0700294 }
295 }
296
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700297 private void stopDevice(DeviceId deviceId, boolean remove) {
298 netconfDeviceMap.get(deviceId).disconnect();
299 netconfDeviceMap.remove(deviceId);
300 if (remove) {
Andrea Campanella86294db2016-03-07 11:42:49 -0800301 for (NetconfDeviceListener l : netconfDeviceListeners) {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700302 l.deviceRemoved(deviceId);
303 }
304 }
305 }
306
307 @Override
308 public void removeDevice(DeviceId deviceId) {
309 if (!netconfDeviceMap.containsKey(deviceId)) {
310 log.warn("Device {} is not present", deviceId);
311 for (NetconfDeviceListener l : netconfDeviceListeners) {
312 l.deviceRemoved(deviceId);
313 }
314 } else {
zhongguo zhao78eab372018-08-27 16:22:39 +0800315 stopDevice(deviceId, true);
Andrea Campanella86294db2016-03-07 11:42:49 -0800316 }
317 }
318
Andrea Campanella101417d2015-12-11 17:58:07 -0800319 private NetconfDevice createDevice(NetconfDeviceInfo deviceInfo) throws NetconfException {
Andrea Campanella950310c2016-02-12 18:14:38 -0800320 NetconfDevice netconfDevice = deviceFactory.createNetconfDevice(deviceInfo);
Andrea Campanella087ceb92015-12-07 09:58:34 -0800321 netconfDeviceMap.put(deviceInfo.getDeviceId(), netconfDevice);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700322 for (NetconfDeviceListener l : netconfDeviceListeners) {
323 l.deviceAdded(deviceInfo.getDeviceId());
324 }
andreaeb70a942015-10-16 21:34:46 -0700325 return netconfDevice;
326 }
327
andreaeb70a942015-10-16 21:34:46 -0700328
329 @Override
330 public Map<DeviceId, NetconfDevice> getDevicesMap() {
331 return netconfDeviceMap;
332 }
Andrea Campanella950310c2016-02-12 18:14:38 -0800333
Andrea Campanella86294db2016-03-07 11:42:49 -0800334 @Override
335 public Set<DeviceId> getNetconfDevices() {
336 return netconfDeviceMap.keySet();
337 }
338
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700339
Yuta HIGUCHI2ee4fba2018-06-12 16:21:06 -0700340 /**
341 * Device factory for the specific NetconfDeviceImpl.
342 *
343 * @deprecated in 1.14.0
344 */
345 @Deprecated
Andrea Campanella950310c2016-02-12 18:14:38 -0800346 private class DefaultNetconfDeviceFactory implements NetconfDeviceFactory {
347
348 @Override
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700349 public NetconfDevice createNetconfDevice(NetconfDeviceInfo netconfDeviceInfo)
350 throws NetconfException {
Andrea Campanella950310c2016-02-12 18:14:38 -0800351 return new DefaultNetconfDevice(netconfDeviceInfo);
352 }
353 }
Andrea Campanella2464dc32016-02-17 17:54:53 -0800354
Yuta HIGUCHIf7089102017-05-03 10:31:46 -0700355 //Listener for closed session with devices, gets triggered when devices goes down
356 // or sends the end pattern ]]>]]>
Andrea Campanella2464dc32016-02-17 17:54:53 -0800357 private class DeviceDownEventListener implements NetconfDeviceOutputEventListener {
358
359 @Override
360 public void event(NetconfDeviceOutputEvent event) {
Andrea Campanellac3627842017-04-04 18:06:54 +0200361 DeviceId did = event.getDeviceInfo().getDeviceId();
Andrea Campanella2464dc32016-02-17 17:54:53 -0800362 if (event.type().equals(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED)) {
Andrea Campanellac3627842017-04-04 18:06:54 +0200363 removeDevice(did);
364 } else if (event.type().equals(NetconfDeviceOutputEvent.Type.SESSION_CLOSED)) {
365 log.info("Trying to reestablish connection with device {}", did);
366 executor.execute(() -> {
367 try {
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700368 NetconfDevice device = netconfDeviceMap.get(did);
369 if (device != null) {
370 device.getSession().checkAndReestablish();
371 log.info("Connection with device {} was reestablished", did);
372 } else {
373 log.warn("The device {} is not in the system", did);
374 }
375
Andrea Campanellac3627842017-04-04 18:06:54 +0200376 } catch (NetconfException e) {
377 log.error("The SSH connection with device {} couldn't be " +
Sean Condon54d82432017-07-26 22:27:25 +0100378 "reestablished due to {}. " +
379 "Marking the device as unreachable", e.getMessage());
Andrea Campanellac3627842017-04-04 18:06:54 +0200380 log.debug("Complete exception: ", e);
381 removeDevice(did);
382 }
383 });
Andrea Campanella2464dc32016-02-17 17:54:53 -0800384 }
385 }
386
387 @Override
388 public boolean isRelevant(NetconfDeviceOutputEvent event) {
389 return getDevicesMap().containsKey(event.getDeviceInfo().getDeviceId());
390 }
391 }
andreaeb70a942015-10-16 21:34:46 -0700392}