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