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