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