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