andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package org.onosproject.netconf.ctl; |
| 18 | |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Modified; |
| 23 | import org.apache.felix.scr.annotations.Property; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Service; |
| 27 | import org.onlab.packet.IpAddress; |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 28 | import org.onosproject.cfg.ComponentConfigService; |
Himanshu Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 29 | import org.onosproject.net.AnnotationKeys; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 30 | import org.onosproject.net.Device; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 31 | import org.onosproject.net.DeviceId; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 32 | import org.onosproject.net.device.DeviceService; |
Himanshu Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 33 | import org.onosproject.net.key.DeviceKey; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 34 | import org.onosproject.net.key.DeviceKeyId; |
| 35 | import org.onosproject.net.key.DeviceKeyService; |
| 36 | import org.onosproject.net.key.UsernamePassword; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 37 | import org.onosproject.netconf.NetconfController; |
| 38 | import org.onosproject.netconf.NetconfDevice; |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 39 | import org.onosproject.netconf.NetconfDeviceFactory; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 40 | import org.onosproject.netconf.NetconfDeviceInfo; |
| 41 | import org.onosproject.netconf.NetconfDeviceListener; |
Andrea Campanella | 2464dc3 | 2016-02-17 17:54:53 -0800 | [diff] [blame] | 42 | import org.onosproject.netconf.NetconfDeviceOutputEvent; |
| 43 | import org.onosproject.netconf.NetconfDeviceOutputEventListener; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 44 | import org.onosproject.netconf.NetconfException; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 45 | import org.osgi.service.component.ComponentContext; |
| 46 | import org.slf4j.Logger; |
| 47 | import org.slf4j.LoggerFactory; |
| 48 | |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 49 | import java.util.Arrays; |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 50 | import java.util.Dictionary; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 51 | import java.util.Map; |
| 52 | import java.util.Set; |
| 53 | import java.util.concurrent.ConcurrentHashMap; |
| 54 | import java.util.concurrent.CopyOnWriteArraySet; |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame^] | 55 | import java.util.concurrent.ExecutorService; |
| 56 | import java.util.concurrent.Executors; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 57 | |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 58 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 59 | import static org.onlab.util.Tools.get; |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame^] | 60 | import static org.onlab.util.Tools.groupedThreads; |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 61 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 62 | /** |
| 63 | * The implementation of NetconfController. |
| 64 | */ |
| 65 | @Component(immediate = true) |
| 66 | @Service |
| 67 | public class NetconfControllerImpl implements NetconfController { |
Sean Condon | 334ad69 | 2016-12-13 17:56:56 +0000 | [diff] [blame] | 68 | private static final int DEFAULT_CONNECT_TIMEOUT_SECONDS = 5; |
| 69 | private static final String PROP_NETCONF_CONNECT_TIMEOUT = "netconfConnectTimeout"; |
| 70 | @Property(name = PROP_NETCONF_CONNECT_TIMEOUT, intValue = DEFAULT_CONNECT_TIMEOUT_SECONDS, |
| 71 | label = "Time (in seconds) to wait for a NETCONF connect.") |
| 72 | protected static int netconfConnectTimeout = DEFAULT_CONNECT_TIMEOUT_SECONDS; |
| 73 | |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 74 | private static final String PROP_NETCONF_REPLY_TIMEOUT = "netconfReplyTimeout"; |
| 75 | private static final int DEFAULT_REPLY_TIMEOUT_SECONDS = 5; |
| 76 | @Property(name = PROP_NETCONF_REPLY_TIMEOUT, intValue = DEFAULT_REPLY_TIMEOUT_SECONDS, |
| 77 | label = "Time (in seconds) waiting for a NetConf reply") |
| 78 | protected static int netconfReplyTimeout = DEFAULT_REPLY_TIMEOUT_SECONDS; |
| 79 | |
| 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 81 | protected ComponentConfigService cfgService; |
| 82 | |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 84 | protected DeviceService deviceService; |
| 85 | |
| 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 87 | protected DeviceKeyService deviceKeyService; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 88 | |
| 89 | public static final Logger log = LoggerFactory |
| 90 | .getLogger(NetconfControllerImpl.class); |
| 91 | |
Andrea Campanella | 8b1cb67 | 2016-01-25 13:58:58 -0800 | [diff] [blame] | 92 | private Map<DeviceId, NetconfDevice> netconfDeviceMap = new ConcurrentHashMap<>(); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 93 | |
Andrea Campanella | 2464dc3 | 2016-02-17 17:54:53 -0800 | [diff] [blame] | 94 | private final NetconfDeviceOutputEventListener downListener = new DeviceDownEventListener(); |
| 95 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 96 | protected Set<NetconfDeviceListener> netconfDeviceListeners = new CopyOnWriteArraySet<>(); |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 97 | protected NetconfDeviceFactory deviceFactory = new DefaultNetconfDeviceFactory(); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 98 | |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame^] | 99 | protected final ExecutorService executor = |
| 100 | Executors.newCachedThreadPool(groupedThreads("onos/netconfdevicecontroller", |
| 101 | "connection-reopen-%d", log)); |
| 102 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 103 | @Activate |
| 104 | public void activate(ComponentContext context) { |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 105 | cfgService.registerProperties(getClass()); |
| 106 | modified(context); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 107 | log.info("Started"); |
| 108 | } |
| 109 | |
| 110 | @Deactivate |
| 111 | public void deactivate() { |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 112 | cfgService.unregisterProperties(getClass(), false); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 113 | netconfDeviceMap.clear(); |
| 114 | log.info("Stopped"); |
| 115 | } |
| 116 | |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 117 | @Modified |
| 118 | public void modified(ComponentContext context) { |
| 119 | if (context == null) { |
| 120 | netconfReplyTimeout = DEFAULT_REPLY_TIMEOUT_SECONDS; |
Sean Condon | 334ad69 | 2016-12-13 17:56:56 +0000 | [diff] [blame] | 121 | netconfConnectTimeout = DEFAULT_CONNECT_TIMEOUT_SECONDS; |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 122 | log.info("No component configuration"); |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | Dictionary<?, ?> properties = context.getProperties(); |
| 127 | |
| 128 | int newNetconfReplyTimeout; |
Sean Condon | 334ad69 | 2016-12-13 17:56:56 +0000 | [diff] [blame] | 129 | int newNetconfConnectTimeout; |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 130 | try { |
| 131 | String s = get(properties, PROP_NETCONF_REPLY_TIMEOUT); |
| 132 | newNetconfReplyTimeout = isNullOrEmpty(s) ? |
| 133 | netconfReplyTimeout : Integer.parseInt(s.trim()); |
Sean Condon | 334ad69 | 2016-12-13 17:56:56 +0000 | [diff] [blame] | 134 | |
| 135 | s = get(properties, PROP_NETCONF_CONNECT_TIMEOUT); |
| 136 | newNetconfConnectTimeout = isNullOrEmpty(s) ? |
| 137 | netconfConnectTimeout : Integer.parseInt(s.trim()); |
| 138 | |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 139 | } catch (NumberFormatException e) { |
| 140 | log.warn("Component configuration had invalid value", e); |
| 141 | return; |
| 142 | } |
| 143 | |
Sean Condon | 334ad69 | 2016-12-13 17:56:56 +0000 | [diff] [blame] | 144 | if (newNetconfConnectTimeout < 0) { |
| 145 | log.warn("netconfConnectTimeout is invalid - less than 0"); |
| 146 | return; |
| 147 | } else if (newNetconfReplyTimeout <= 0) { |
| 148 | log.warn("netconfReplyTimeout is invalid - 0 or less."); |
| 149 | return; |
| 150 | } |
| 151 | |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 152 | netconfReplyTimeout = newNetconfReplyTimeout; |
Sean Condon | 334ad69 | 2016-12-13 17:56:56 +0000 | [diff] [blame] | 153 | netconfConnectTimeout = newNetconfConnectTimeout; |
| 154 | log.info("Settings: {} = {}, {} = {}", |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame^] | 155 | PROP_NETCONF_REPLY_TIMEOUT, netconfReplyTimeout, PROP_NETCONF_CONNECT_TIMEOUT, netconfConnectTimeout); |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 156 | } |
| 157 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 158 | @Override |
| 159 | public void addDeviceListener(NetconfDeviceListener listener) { |
| 160 | if (!netconfDeviceListeners.contains(listener)) { |
| 161 | netconfDeviceListeners.add(listener); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public void removeDeviceListener(NetconfDeviceListener listener) { |
| 167 | netconfDeviceListeners.remove(listener); |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public NetconfDevice getNetconfDevice(DeviceId deviceInfo) { |
| 172 | return netconfDeviceMap.get(deviceInfo); |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public NetconfDevice getNetconfDevice(IpAddress ip, int port) { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 177 | for (DeviceId info : netconfDeviceMap.keySet()) { |
Andrea Campanella | 57efbb2 | 2016-02-11 14:21:41 -0800 | [diff] [blame] | 178 | if (info.uri().getSchemeSpecificPart().equals(ip.toString() + ":" + port)) { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 179 | return netconfDeviceMap.get(info); |
| 180 | } |
| 181 | } |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 182 | return null; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | @Override |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 186 | public NetconfDevice connectDevice(DeviceId deviceId) throws NetconfException { |
| 187 | if (netconfDeviceMap.containsKey(deviceId)) { |
| 188 | log.debug("Device {} is already present", deviceId); |
| 189 | return netconfDeviceMap.get(deviceId); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 190 | } else { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 191 | log.debug("Creating NETCONF device {}", deviceId); |
| 192 | Device device = deviceService.getDevice(deviceId); |
| 193 | String ip; |
| 194 | int port; |
| 195 | if (device != null) { |
| 196 | ip = device.annotations().value("ipaddress"); |
| 197 | port = Integer.parseInt(device.annotations().value("port")); |
| 198 | } else { |
| 199 | String[] info = deviceId.toString().split(":"); |
| 200 | if (info.length == 3) { |
| 201 | ip = info[1]; |
| 202 | port = Integer.parseInt(info[2]); |
| 203 | } else { |
| 204 | ip = Arrays.asList(info).stream().filter(el -> !el.equals(info[0]) |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame^] | 205 | && !el.equals(info[info.length - 1])) |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 206 | .reduce((t, u) -> t + ":" + u) |
| 207 | .get(); |
| 208 | log.debug("ip v6 {}", ip); |
| 209 | port = Integer.parseInt(info[info.length - 1]); |
| 210 | } |
| 211 | } |
| 212 | try { |
Himanshu Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 213 | DeviceKey deviceKey = deviceKeyService.getDeviceKey( |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame^] | 214 | DeviceKeyId.deviceKeyId(deviceId.toString())); |
Himanshu Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 215 | NetconfDeviceInfo deviceInfo = null; |
| 216 | if (deviceKey.type() == DeviceKey.Type.USERNAME_PASSWORD) { |
| 217 | UsernamePassword usernamepasswd = deviceKey.asUsernamePassword(); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 218 | |
Himanshu Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 219 | deviceInfo = new NetconfDeviceInfo(usernamepasswd.username(), |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame^] | 220 | usernamepasswd.password(), |
| 221 | IpAddress.valueOf(ip), |
| 222 | port); |
Himanshu Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 223 | |
| 224 | } else if (deviceKey.type() == DeviceKey.Type.SSL_KEY) { |
| 225 | String username = deviceKey.annotations().value(AnnotationKeys.USERNAME); |
| 226 | String password = deviceKey.annotations().value(AnnotationKeys.PASSWORD); |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame^] | 227 | String sshkey = deviceKey.annotations().value(AnnotationKeys.SSHKEY); |
Himanshu Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 228 | |
| 229 | deviceInfo = new NetconfDeviceInfo(username, |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame^] | 230 | password, |
| 231 | IpAddress.valueOf(ip), |
| 232 | port, |
| 233 | sshkey); |
Himanshu Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 234 | } else { |
| 235 | log.error("Unknown device key for device {}", deviceId); |
| 236 | } |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 237 | NetconfDevice netconfDevicedevice = createDevice(deviceInfo); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 238 | netconfDevicedevice.getSession().addDeviceOutputListener(downListener); |
| 239 | return netconfDevicedevice; |
| 240 | } catch (NullPointerException e) { |
| 241 | throw new NetconfException("No Device Key for device " + deviceId, e); |
| 242 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
| 246 | @Override |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 247 | public void disconnectDevice(DeviceId deviceId, boolean remove) { |
| 248 | if (!netconfDeviceMap.containsKey(deviceId)) { |
| 249 | log.warn("Device {} is not present", deviceId); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 250 | } else { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 251 | stopDevice(deviceId, remove); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 255 | private void stopDevice(DeviceId deviceId, boolean remove) { |
| 256 | netconfDeviceMap.get(deviceId).disconnect(); |
| 257 | netconfDeviceMap.remove(deviceId); |
| 258 | if (remove) { |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 259 | for (NetconfDeviceListener l : netconfDeviceListeners) { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 260 | l.deviceRemoved(deviceId); |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public void removeDevice(DeviceId deviceId) { |
| 267 | if (!netconfDeviceMap.containsKey(deviceId)) { |
| 268 | log.warn("Device {} is not present", deviceId); |
| 269 | for (NetconfDeviceListener l : netconfDeviceListeners) { |
| 270 | l.deviceRemoved(deviceId); |
| 271 | } |
| 272 | } else { |
| 273 | netconfDeviceMap.remove(deviceId); |
| 274 | for (NetconfDeviceListener l : netconfDeviceListeners) { |
| 275 | l.deviceRemoved(deviceId); |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 280 | private NetconfDevice createDevice(NetconfDeviceInfo deviceInfo) throws NetconfException { |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 281 | NetconfDevice netconfDevice = deviceFactory.createNetconfDevice(deviceInfo); |
Andrea Campanella | 087ceb9 | 2015-12-07 09:58:34 -0800 | [diff] [blame] | 282 | netconfDeviceMap.put(deviceInfo.getDeviceId(), netconfDevice); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 283 | for (NetconfDeviceListener l : netconfDeviceListeners) { |
| 284 | l.deviceAdded(deviceInfo.getDeviceId()); |
| 285 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 286 | return netconfDevice; |
| 287 | } |
| 288 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 289 | |
| 290 | @Override |
| 291 | public Map<DeviceId, NetconfDevice> getDevicesMap() { |
| 292 | return netconfDeviceMap; |
| 293 | } |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 294 | |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 295 | @Override |
| 296 | public Set<DeviceId> getNetconfDevices() { |
| 297 | return netconfDeviceMap.keySet(); |
| 298 | } |
| 299 | |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 300 | |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 301 | //Device factory for the specific NetconfDeviceImpl |
| 302 | private class DefaultNetconfDeviceFactory implements NetconfDeviceFactory { |
| 303 | |
| 304 | @Override |
| 305 | public NetconfDevice createNetconfDevice(NetconfDeviceInfo netconfDeviceInfo) throws NetconfException { |
| 306 | return new DefaultNetconfDevice(netconfDeviceInfo); |
| 307 | } |
| 308 | } |
Andrea Campanella | 2464dc3 | 2016-02-17 17:54:53 -0800 | [diff] [blame] | 309 | |
| 310 | //Listener for closed session with devices, gets triggered whe devices goes down |
| 311 | // or sends the endpattern ]]>]]> |
| 312 | private class DeviceDownEventListener implements NetconfDeviceOutputEventListener { |
| 313 | |
| 314 | @Override |
| 315 | public void event(NetconfDeviceOutputEvent event) { |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame^] | 316 | DeviceId did = event.getDeviceInfo().getDeviceId(); |
Andrea Campanella | 2464dc3 | 2016-02-17 17:54:53 -0800 | [diff] [blame] | 317 | if (event.type().equals(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED)) { |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame^] | 318 | removeDevice(did); |
| 319 | } else if (event.type().equals(NetconfDeviceOutputEvent.Type.SESSION_CLOSED)) { |
| 320 | log.info("Trying to reestablish connection with device {}", did); |
| 321 | executor.execute(() -> { |
| 322 | try { |
| 323 | netconfDeviceMap.get(did).getSession().checkAndReestablish(); |
| 324 | log.info("Connection with device {} was reestablished", did); |
| 325 | } catch (NetconfException e) { |
| 326 | log.error("The SSH connection with device {} couldn't be " + |
| 327 | "reestablished due to {}. " + |
| 328 | "Marking the device as unreachable", e.getMessage()); |
| 329 | log.debug("Complete exception: ", e); |
| 330 | removeDevice(did); |
| 331 | } |
| 332 | }); |
Andrea Campanella | 2464dc3 | 2016-02-17 17:54:53 -0800 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
| 336 | @Override |
| 337 | public boolean isRelevant(NetconfDeviceOutputEvent event) { |
| 338 | return getDevicesMap().containsKey(event.getDeviceInfo().getDeviceId()); |
| 339 | } |
| 340 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 341 | } |