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