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