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