andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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 | |
Holger Schulz | 092cbbf | 2017-08-31 17:52:30 +0200 | [diff] [blame] | 19 | import org.bouncycastle.jce.provider.BouncyCastleProvider; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 20 | import org.onlab.packet.IpAddress; |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 21 | import org.onosproject.cfg.ComponentConfigService; |
Himanshu Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 22 | import org.onosproject.net.AnnotationKeys; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 23 | import org.onosproject.net.Device; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 24 | import org.onosproject.net.DeviceId; |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 25 | import org.onosproject.net.config.NetworkConfigRegistry; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 26 | import org.onosproject.net.device.DeviceService; |
Himanshu Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 27 | import org.onosproject.net.key.DeviceKey; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 28 | import org.onosproject.net.key.DeviceKeyId; |
| 29 | import org.onosproject.net.key.DeviceKeyService; |
| 30 | import org.onosproject.net.key.UsernamePassword; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 31 | import org.onosproject.netconf.NetconfController; |
| 32 | import org.onosproject.netconf.NetconfDevice; |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 33 | import org.onosproject.netconf.NetconfDeviceFactory; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 34 | import org.onosproject.netconf.NetconfDeviceInfo; |
| 35 | import org.onosproject.netconf.NetconfDeviceListener; |
Andrea Campanella | 2464dc3 | 2016-02-17 17:54:53 -0800 | [diff] [blame] | 36 | import org.onosproject.netconf.NetconfDeviceOutputEvent; |
| 37 | import org.onosproject.netconf.NetconfDeviceOutputEventListener; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 38 | import org.onosproject.netconf.NetconfException; |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 39 | import org.onosproject.netconf.config.NetconfDeviceConfig; |
| 40 | import org.onosproject.netconf.config.NetconfSshClientLib; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 41 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 42 | import org.osgi.service.component.annotations.Activate; |
| 43 | import org.osgi.service.component.annotations.Component; |
| 44 | import org.osgi.service.component.annotations.Deactivate; |
| 45 | import org.osgi.service.component.annotations.Modified; |
| 46 | import org.osgi.service.component.annotations.Reference; |
| 47 | import org.osgi.service.component.annotations.ReferenceCardinality; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 48 | import org.slf4j.Logger; |
| 49 | import org.slf4j.LoggerFactory; |
| 50 | |
Holger Schulz | 092cbbf | 2017-08-31 17:52:30 +0200 | [diff] [blame] | 51 | import java.security.Security; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 52 | import java.util.Arrays; |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 53 | import java.util.Dictionary; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 54 | import java.util.Map; |
| 55 | import java.util.Set; |
| 56 | import java.util.concurrent.ConcurrentHashMap; |
| 57 | import java.util.concurrent.CopyOnWriteArraySet; |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame] | 58 | import java.util.concurrent.ExecutorService; |
| 59 | import java.util.concurrent.Executors; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 60 | |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 61 | import static org.onlab.util.Tools.get; |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 62 | import static org.onlab.util.Tools.getIntegerProperty; |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame] | 63 | import static org.onlab.util.Tools.groupedThreads; |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame^] | 64 | import static org.onosproject.netconf.ctl.impl.OsgiPropertyConstants.*; |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 65 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 66 | /** |
| 67 | * The implementation of NetconfController. |
| 68 | */ |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame^] | 69 | @Component(immediate = true, service = NetconfController.class, |
| 70 | property = { |
| 71 | NETCONF_CONNECT_TIMEOUT + ":Integer=" + NETCONF_CONNECT_TIMEOUT_DEFAULT, |
| 72 | NETCONF_REPLY_TIMEOUT + ":Integer=" + NETCONF_REPLY_TIMEOUT_DEFAULT, |
| 73 | NETCONF_IDLE_TIMEOUT + ":Integer=" + NETCONF_IDLE_TIMEOUT_DEFAULT, |
| 74 | SSH_LIBRARY + "=" + SSH_LIBRARY_DEFAULT, |
| 75 | }) |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 76 | public class NetconfControllerImpl implements NetconfController { |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 77 | |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame^] | 78 | /** Time (in seconds) to wait for a NETCONF connect. */ |
| 79 | protected static int netconfConnectTimeout = NETCONF_CONNECT_TIMEOUT_DEFAULT; |
Sean Condon | 334ad69 | 2016-12-13 17:56:56 +0000 | [diff] [blame] | 80 | |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame^] | 81 | /** Time (in seconds) waiting for a NetConf reply. */ |
| 82 | protected static int netconfReplyTimeout = NETCONF_REPLY_TIMEOUT_DEFAULT; |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 83 | |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame^] | 84 | /** Time (in seconds) SSH session will close if no traffic seen. */ |
| 85 | protected static int netconfIdleTimeout = NETCONF_IDLE_TIMEOUT_DEFAULT; |
Sean Condon | 7347de9 | 2017-07-21 12:17:25 +0100 | [diff] [blame] | 86 | |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame^] | 87 | /** SSH client library to use. */ |
| 88 | protected static String sshLibrary = SSH_LIBRARY_DEFAULT; |
| 89 | |
| 90 | protected NetconfSshClientLib sshClientLib = NetconfSshClientLib.APACHE_MINA; |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 91 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 92 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 93 | protected ComponentConfigService cfgService; |
| 94 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 95 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 96 | protected DeviceService deviceService; |
| 97 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 98 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 99 | protected DeviceKeyService deviceKeyService; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 100 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 101 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 102 | protected NetworkConfigRegistry netCfgService; |
| 103 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 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<>(); |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame^] | 112 | protected NetconfDeviceFactory deviceFactory = DefaultNetconfDevice::new; |
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); |
Yuta HIGUCHI | e976174 | 2017-09-10 15:10:19 -0700 | [diff] [blame] | 122 | Security.addProvider(new BouncyCastleProvider()); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 123 | log.info("Started"); |
| 124 | } |
| 125 | |
| 126 | @Deactivate |
| 127 | public void deactivate() { |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 128 | netconfDeviceMap.values().forEach(device -> { |
| 129 | device.getSession().removeDeviceOutputListener(downListener); |
| 130 | device.disconnect(); |
| 131 | }); |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 132 | cfgService.unregisterProperties(getClass(), false); |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 133 | netconfDeviceListeners.clear(); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 134 | netconfDeviceMap.clear(); |
Holger Schulz | 092cbbf | 2017-08-31 17:52:30 +0200 | [diff] [blame] | 135 | Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 136 | log.info("Stopped"); |
| 137 | } |
| 138 | |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 139 | @Modified |
| 140 | public void modified(ComponentContext context) { |
| 141 | if (context == null) { |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame^] | 142 | netconfReplyTimeout = NETCONF_REPLY_TIMEOUT_DEFAULT; |
| 143 | netconfConnectTimeout = NETCONF_CONNECT_TIMEOUT_DEFAULT; |
| 144 | netconfIdleTimeout = NETCONF_IDLE_TIMEOUT_DEFAULT; |
| 145 | sshLibrary = SSH_LIBRARY_DEFAULT; |
| 146 | sshClientLib = NetconfSshClientLib.APACHE_MINA; |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 147 | log.info("No component configuration"); |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | Dictionary<?, ?> properties = context.getProperties(); |
| 152 | |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 153 | String newSshLibrary; |
Sean Condon | 334ad69 | 2016-12-13 17:56:56 +0000 | [diff] [blame] | 154 | |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 155 | int newNetconfReplyTimeout = getIntegerProperty( |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame^] | 156 | properties, NETCONF_REPLY_TIMEOUT, netconfReplyTimeout); |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 157 | int newNetconfConnectTimeout = getIntegerProperty( |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame^] | 158 | properties, NETCONF_CONNECT_TIMEOUT, netconfConnectTimeout); |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 159 | int newNetconfIdleTimeout = getIntegerProperty( |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame^] | 160 | properties, NETCONF_IDLE_TIMEOUT, netconfIdleTimeout); |
Sean Condon | 334ad69 | 2016-12-13 17:56:56 +0000 | [diff] [blame] | 161 | |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 162 | newSshLibrary = get(properties, SSH_LIBRARY); |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 163 | |
Sean Condon | 334ad69 | 2016-12-13 17:56:56 +0000 | [diff] [blame] | 164 | if (newNetconfConnectTimeout < 0) { |
| 165 | log.warn("netconfConnectTimeout is invalid - less than 0"); |
| 166 | return; |
| 167 | } else if (newNetconfReplyTimeout <= 0) { |
| 168 | log.warn("netconfReplyTimeout is invalid - 0 or less."); |
| 169 | return; |
Sean Condon | 7347de9 | 2017-07-21 12:17:25 +0100 | [diff] [blame] | 170 | } else if (newNetconfIdleTimeout <= 0) { |
| 171 | log.warn("netconfIdleTimeout is invalid - 0 or less."); |
| 172 | return; |
Sean Condon | 334ad69 | 2016-12-13 17:56:56 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 175 | netconfReplyTimeout = newNetconfReplyTimeout; |
Sean Condon | 334ad69 | 2016-12-13 17:56:56 +0000 | [diff] [blame] | 176 | netconfConnectTimeout = newNetconfConnectTimeout; |
Sean Condon | 7347de9 | 2017-07-21 12:17:25 +0100 | [diff] [blame] | 177 | netconfIdleTimeout = newNetconfIdleTimeout; |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 178 | if (newSshLibrary != null) { |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame^] | 179 | sshLibrary = newSshLibrary; |
| 180 | sshClientLib = NetconfSshClientLib.getEnum(newSshLibrary); |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 181 | } |
| 182 | log.info("Settings: {} = {}, {} = {}, {} = {}, {} = {}", |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame^] | 183 | NETCONF_REPLY_TIMEOUT, netconfReplyTimeout, |
| 184 | NETCONF_CONNECT_TIMEOUT, netconfConnectTimeout, |
| 185 | NETCONF_IDLE_TIMEOUT, netconfIdleTimeout, |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 186 | SSH_LIBRARY, sshLibrary); |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 187 | } |
| 188 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 189 | @Override |
| 190 | public void addDeviceListener(NetconfDeviceListener listener) { |
| 191 | if (!netconfDeviceListeners.contains(listener)) { |
| 192 | netconfDeviceListeners.add(listener); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public void removeDeviceListener(NetconfDeviceListener listener) { |
| 198 | netconfDeviceListeners.remove(listener); |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public NetconfDevice getNetconfDevice(DeviceId deviceInfo) { |
| 203 | return netconfDeviceMap.get(deviceInfo); |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public NetconfDevice getNetconfDevice(IpAddress ip, int port) { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 208 | for (DeviceId info : netconfDeviceMap.keySet()) { |
Andrea Campanella | 57efbb2 | 2016-02-11 14:21:41 -0800 | [diff] [blame] | 209 | if (info.uri().getSchemeSpecificPart().equals(ip.toString() + ":" + port)) { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 210 | return netconfDeviceMap.get(info); |
| 211 | } |
| 212 | } |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 213 | return null; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | @Override |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 217 | public NetconfDevice connectDevice(DeviceId deviceId) throws NetconfException { |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 218 | NetconfDeviceConfig netCfg = netCfgService.getConfig( |
| 219 | deviceId, NetconfDeviceConfig.class); |
| 220 | NetconfDeviceInfo deviceInfo = null; |
| 221 | |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 222 | if (netconfDeviceMap.containsKey(deviceId)) { |
| 223 | log.debug("Device {} is already present", deviceId); |
| 224 | return netconfDeviceMap.get(deviceId); |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 225 | } else if (netCfg != null) { |
| 226 | log.debug("Device {} is present in NetworkConfig", deviceId); |
| 227 | deviceInfo = new NetconfDeviceInfo(netCfg); |
| 228 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 229 | } else { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 230 | log.debug("Creating NETCONF device {}", deviceId); |
| 231 | Device device = deviceService.getDevice(deviceId); |
| 232 | String ip; |
| 233 | int port; |
| 234 | if (device != null) { |
| 235 | ip = device.annotations().value("ipaddress"); |
| 236 | port = Integer.parseInt(device.annotations().value("port")); |
| 237 | } else { |
| 238 | String[] info = deviceId.toString().split(":"); |
| 239 | if (info.length == 3) { |
| 240 | ip = info[1]; |
| 241 | port = Integer.parseInt(info[2]); |
| 242 | } else { |
| 243 | ip = Arrays.asList(info).stream().filter(el -> !el.equals(info[0]) |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame] | 244 | && !el.equals(info[info.length - 1])) |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 245 | .reduce((t, u) -> t + ":" + u) |
| 246 | .get(); |
| 247 | log.debug("ip v6 {}", ip); |
| 248 | port = Integer.parseInt(info[info.length - 1]); |
| 249 | } |
| 250 | } |
| 251 | try { |
Himanshu Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 252 | DeviceKey deviceKey = deviceKeyService.getDeviceKey( |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame] | 253 | DeviceKeyId.deviceKeyId(deviceId.toString())); |
Himanshu Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 254 | if (deviceKey.type() == DeviceKey.Type.USERNAME_PASSWORD) { |
| 255 | UsernamePassword usernamepasswd = deviceKey.asUsernamePassword(); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 256 | |
Himanshu Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 257 | deviceInfo = new NetconfDeviceInfo(usernamepasswd.username(), |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame] | 258 | usernamepasswd.password(), |
| 259 | IpAddress.valueOf(ip), |
| 260 | port); |
Himanshu Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 261 | |
| 262 | } else if (deviceKey.type() == DeviceKey.Type.SSL_KEY) { |
| 263 | String username = deviceKey.annotations().value(AnnotationKeys.USERNAME); |
| 264 | String password = deviceKey.annotations().value(AnnotationKeys.PASSWORD); |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame] | 265 | String sshkey = deviceKey.annotations().value(AnnotationKeys.SSHKEY); |
Himanshu Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 266 | |
| 267 | deviceInfo = new NetconfDeviceInfo(username, |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame] | 268 | password, |
| 269 | IpAddress.valueOf(ip), |
| 270 | port, |
| 271 | sshkey); |
Himanshu Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 272 | } else { |
| 273 | log.error("Unknown device key for device {}", deviceId); |
| 274 | } |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 275 | } catch (NullPointerException e) { |
| 276 | throw new NetconfException("No Device Key for device " + deviceId, e); |
| 277 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 278 | } |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 279 | NetconfDevice netconfDevicedevice = createDevice(deviceInfo); |
| 280 | netconfDevicedevice.getSession().addDeviceOutputListener(downListener); |
| 281 | return netconfDevicedevice; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | @Override |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 285 | public void disconnectDevice(DeviceId deviceId, boolean remove) { |
| 286 | if (!netconfDeviceMap.containsKey(deviceId)) { |
| 287 | log.warn("Device {} is not present", deviceId); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 288 | } else { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 289 | stopDevice(deviceId, remove); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 293 | private void stopDevice(DeviceId deviceId, boolean remove) { |
| 294 | netconfDeviceMap.get(deviceId).disconnect(); |
| 295 | netconfDeviceMap.remove(deviceId); |
| 296 | if (remove) { |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 297 | for (NetconfDeviceListener l : netconfDeviceListeners) { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 298 | l.deviceRemoved(deviceId); |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public void removeDevice(DeviceId deviceId) { |
| 305 | if (!netconfDeviceMap.containsKey(deviceId)) { |
| 306 | log.warn("Device {} is not present", deviceId); |
| 307 | for (NetconfDeviceListener l : netconfDeviceListeners) { |
| 308 | l.deviceRemoved(deviceId); |
| 309 | } |
| 310 | } else { |
zhongguo zhao | 78eab37 | 2018-08-27 16:22:39 +0800 | [diff] [blame] | 311 | stopDevice(deviceId, true); |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 315 | private NetconfDevice createDevice(NetconfDeviceInfo deviceInfo) throws NetconfException { |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 316 | NetconfDevice netconfDevice = deviceFactory.createNetconfDevice(deviceInfo); |
Andrea Campanella | 087ceb9 | 2015-12-07 09:58:34 -0800 | [diff] [blame] | 317 | netconfDeviceMap.put(deviceInfo.getDeviceId(), netconfDevice); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 318 | for (NetconfDeviceListener l : netconfDeviceListeners) { |
| 319 | l.deviceAdded(deviceInfo.getDeviceId()); |
| 320 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 321 | return netconfDevice; |
| 322 | } |
| 323 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 324 | |
| 325 | @Override |
| 326 | public Map<DeviceId, NetconfDevice> getDevicesMap() { |
| 327 | return netconfDeviceMap; |
| 328 | } |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 329 | |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 330 | @Override |
| 331 | public Set<DeviceId> getNetconfDevices() { |
| 332 | return netconfDeviceMap.keySet(); |
| 333 | } |
| 334 | |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 335 | |
Yuta HIGUCHI | 2ee4fba | 2018-06-12 16:21:06 -0700 | [diff] [blame] | 336 | /** |
| 337 | * Device factory for the specific NetconfDeviceImpl. |
| 338 | * |
| 339 | * @deprecated in 1.14.0 |
| 340 | */ |
| 341 | @Deprecated |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 342 | private class DefaultNetconfDeviceFactory implements NetconfDeviceFactory { |
| 343 | |
| 344 | @Override |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 345 | public NetconfDevice createNetconfDevice(NetconfDeviceInfo netconfDeviceInfo) |
| 346 | throws NetconfException { |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 347 | return new DefaultNetconfDevice(netconfDeviceInfo); |
| 348 | } |
| 349 | } |
Andrea Campanella | 2464dc3 | 2016-02-17 17:54:53 -0800 | [diff] [blame] | 350 | |
Yuta HIGUCHI | f708910 | 2017-05-03 10:31:46 -0700 | [diff] [blame] | 351 | //Listener for closed session with devices, gets triggered when devices goes down |
| 352 | // or sends the end pattern ]]>]]> |
Andrea Campanella | 2464dc3 | 2016-02-17 17:54:53 -0800 | [diff] [blame] | 353 | private class DeviceDownEventListener implements NetconfDeviceOutputEventListener { |
| 354 | |
| 355 | @Override |
| 356 | public void event(NetconfDeviceOutputEvent event) { |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame] | 357 | DeviceId did = event.getDeviceInfo().getDeviceId(); |
Andrea Campanella | 2464dc3 | 2016-02-17 17:54:53 -0800 | [diff] [blame] | 358 | if (event.type().equals(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED)) { |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame] | 359 | removeDevice(did); |
| 360 | } else if (event.type().equals(NetconfDeviceOutputEvent.Type.SESSION_CLOSED)) { |
| 361 | log.info("Trying to reestablish connection with device {}", did); |
| 362 | executor.execute(() -> { |
| 363 | try { |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 364 | NetconfDevice device = netconfDeviceMap.get(did); |
| 365 | if (device != null) { |
| 366 | device.getSession().checkAndReestablish(); |
| 367 | log.info("Connection with device {} was reestablished", did); |
| 368 | } else { |
| 369 | log.warn("The device {} is not in the system", did); |
| 370 | } |
| 371 | |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame] | 372 | } catch (NetconfException e) { |
| 373 | log.error("The SSH connection with device {} couldn't be " + |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 374 | "reestablished due to {}. " + |
| 375 | "Marking the device as unreachable", e.getMessage()); |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame] | 376 | log.debug("Complete exception: ", e); |
| 377 | removeDevice(did); |
| 378 | } |
| 379 | }); |
Andrea Campanella | 2464dc3 | 2016-02-17 17:54:53 -0800 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
| 383 | @Override |
| 384 | public boolean isRelevant(NetconfDeviceOutputEvent event) { |
| 385 | return getDevicesMap().containsKey(event.getDeviceInfo().getDeviceId()); |
| 386 | } |
| 387 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 388 | } |