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 | |
| 17 | package org.onosproject.netconf; |
| 18 | |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 19 | import org.apache.commons.lang3.tuple.Triple; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 20 | import org.onlab.packet.IpAddress; |
| 21 | import org.onosproject.net.DeviceId; |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 22 | import org.onosproject.netconf.config.NetconfDeviceConfig; |
| 23 | import org.onosproject.netconf.config.NetconfSshClientLib; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
| 26 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 27 | import java.net.URI; |
| 28 | import java.net.URISyntaxException; |
| 29 | import java.util.Objects; |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 30 | import java.util.Optional; |
| 31 | import java.util.OptionalInt; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 32 | |
Ray Milkey | d4b51c2 | 2018-02-08 11:36:43 -0800 | [diff] [blame] | 33 | import static com.google.common.base.Preconditions.checkArgument; |
| 34 | import static com.google.common.base.Preconditions.checkNotNull; |
| 35 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Represents a Netconf device information. |
| 38 | */ |
| 39 | public class NetconfDeviceInfo { |
| 40 | |
| 41 | public static final Logger log = LoggerFactory |
| 42 | .getLogger(NetconfDeviceInfo.class); |
| 43 | |
| 44 | private String name; |
| 45 | private String password; |
| 46 | private IpAddress ipAddress; |
| 47 | private int port; |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 48 | private Optional<String> path; |
Himanshu Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 49 | private char[] key; |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 50 | private Optional<NetconfSshClientLib> sshClientLib; |
| 51 | private OptionalInt connectTimeoutSec; |
| 52 | private OptionalInt replyTimeoutSec; |
| 53 | private OptionalInt idleTimeoutSec; |
Andrea Campanella | 57efbb2 | 2016-02-11 14:21:41 -0800 | [diff] [blame] | 54 | private DeviceId deviceId; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 55 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 56 | /** |
| 57 | * Information for contacting the controller. |
| 58 | * |
| 59 | * @param name the connection type |
| 60 | * @param password the password for the device |
| 61 | * @param ipAddress the ip address |
| 62 | * @param port the tcp port |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 63 | * @param path the path part |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 64 | */ |
| 65 | public NetconfDeviceInfo(String name, String password, IpAddress ipAddress, |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 66 | int port, String path) { |
Yuta HIGUCHI | 09ae368 | 2017-08-14 18:56:54 -0700 | [diff] [blame] | 67 | checkArgument(!name.equals(""), "Empty device username"); |
Yuta HIGUCHI | fbd9ae9 | 2018-01-24 23:39:06 -0800 | [diff] [blame] | 68 | checkArgument(port > 0, "Negative port"); |
Yuta HIGUCHI | 09ae368 | 2017-08-14 18:56:54 -0700 | [diff] [blame] | 69 | checkNotNull(ipAddress, "Null ip address"); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 70 | this.name = name; |
| 71 | this.password = password; |
| 72 | this.ipAddress = ipAddress; |
| 73 | this.port = port; |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 74 | if (path == null || path.isEmpty()) { |
| 75 | this.path = Optional.empty(); |
| 76 | } else { |
| 77 | this.path = Optional.of(path); |
| 78 | } |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 79 | this.sshClientLib = Optional.empty(); |
| 80 | this.connectTimeoutSec = OptionalInt.empty(); |
| 81 | this.replyTimeoutSec = OptionalInt.empty(); |
| 82 | this.idleTimeoutSec = OptionalInt.empty(); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Information for contacting the controller. |
| 87 | * |
| 88 | * @param name the connection type |
| 89 | * @param password the password for the device |
| 90 | * @param ipAddress the ip address |
| 91 | * @param port the tcp port |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 92 | */ |
| 93 | public NetconfDeviceInfo(String name, String password, IpAddress ipAddress, |
| 94 | int port) { |
| 95 | this(name, password, ipAddress, port, null); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Information for contacting the controller. |
| 100 | * |
| 101 | * @param name the connection type |
| 102 | * @param password the password for the device |
| 103 | * @param ipAddress the ip address |
| 104 | * @param port the tcp port |
| 105 | * @param path the path part |
Andrea Campanella | e7006dc | 2017-02-15 16:04:09 -0800 | [diff] [blame] | 106 | * @param keyString the string containing a DSA or RSA private key |
| 107 | * of the user in OpenSSH key format |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 108 | */ |
| 109 | public NetconfDeviceInfo(String name, String password, IpAddress ipAddress, |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 110 | int port, String path, String keyString) { |
Yuta HIGUCHI | 09ae368 | 2017-08-14 18:56:54 -0700 | [diff] [blame] | 111 | checkArgument(!name.equals(""), "Empty device name"); |
Yuta HIGUCHI | fbd9ae9 | 2018-01-24 23:39:06 -0800 | [diff] [blame] | 112 | checkArgument(port > 0, "Negative port"); |
Yuta HIGUCHI | 09ae368 | 2017-08-14 18:56:54 -0700 | [diff] [blame] | 113 | checkNotNull(ipAddress, "Null ip address"); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 114 | this.name = name; |
| 115 | this.password = password; |
| 116 | this.ipAddress = ipAddress; |
| 117 | this.port = port; |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 118 | this.path = Optional.ofNullable(path); |
Himanshu Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 119 | this.key = keyString.toCharArray(); |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 120 | this.sshClientLib = Optional.empty(); |
| 121 | this.connectTimeoutSec = OptionalInt.empty(); |
| 122 | this.replyTimeoutSec = OptionalInt.empty(); |
| 123 | this.idleTimeoutSec = OptionalInt.empty(); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Convenieince constructor that converts all known fields from NetCfg data. |
| 128 | * @param netconfConfig NetCf configuration |
| 129 | */ |
| 130 | public NetconfDeviceInfo(NetconfDeviceConfig netconfConfig) { |
Yuta HIGUCHI | 09ae368 | 2017-08-14 18:56:54 -0700 | [diff] [blame] | 131 | checkArgument(!netconfConfig.username().isEmpty(), "Empty device name"); |
Yuta HIGUCHI | fbd9ae9 | 2018-01-24 23:39:06 -0800 | [diff] [blame] | 132 | checkArgument(netconfConfig.port() > 0, "Negative port"); |
Yuta HIGUCHI | 09ae368 | 2017-08-14 18:56:54 -0700 | [diff] [blame] | 133 | checkNotNull(netconfConfig.ip(), "Null ip address"); |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 134 | |
| 135 | this.name = netconfConfig.username(); |
| 136 | this.password = netconfConfig.password(); |
| 137 | this.ipAddress = netconfConfig.ip(); |
| 138 | this.port = netconfConfig.port(); |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 139 | this.path = netconfConfig.path(); |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 140 | if (netconfConfig.sshKey() != null && !netconfConfig.sshKey().isEmpty()) { |
| 141 | this.key = netconfConfig.sshKey().toCharArray(); |
| 142 | } |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 143 | if (netconfConfig.sshClient().isPresent()) { |
| 144 | this.sshClientLib = Optional.of(NetconfSshClientLib.getEnum(netconfConfig.sshClient().get())); |
| 145 | } else { |
| 146 | this.sshClientLib = Optional.empty(); |
| 147 | } |
| 148 | this.connectTimeoutSec = netconfConfig.connectTimeout(); |
| 149 | this.replyTimeoutSec = netconfConfig.replyTimeout(); |
| 150 | this.idleTimeoutSec = netconfConfig.idleTimeout(); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Allows the NETCONF SSH Client library to be set. |
| 155 | * |
| 156 | * @param sshClientLib An enumerated value |
| 157 | */ |
| 158 | public void setSshClientLib(Optional<NetconfSshClientLib> sshClientLib) { |
| 159 | this.sshClientLib = sshClientLib; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Allows the NETCONF SSH session initial connect timeout to be set. |
| 164 | * |
| 165 | * @param connectTimeoutSec value in seconds |
| 166 | */ |
| 167 | public void setConnectTimeoutSec(OptionalInt connectTimeoutSec) { |
| 168 | this.connectTimeoutSec = connectTimeoutSec; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Allows the NETCONF SSH session replies timeout to be set. |
| 173 | * |
| 174 | * @param replyTimeoutSec value in seconds |
| 175 | */ |
| 176 | public void setReplyTimeoutSec(OptionalInt replyTimeoutSec) { |
| 177 | this.replyTimeoutSec = replyTimeoutSec; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Allows the NETCONF SSH session idle timeout to be set. |
| 182 | * |
| 183 | * @param idleTimeoutSec value in seconds |
| 184 | */ |
| 185 | public void setIdleTimeoutSec(OptionalInt idleTimeoutSec) { |
| 186 | this.idleTimeoutSec = idleTimeoutSec; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /** |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 190 | * Allows the path aspect of the device URI to be set. |
| 191 | * |
| 192 | * @param path path aspect value |
| 193 | */ |
| 194 | public void setPath(Optional<String> path) { |
| 195 | this.path = path; |
| 196 | } |
| 197 | |
| 198 | /** |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 199 | * Exposes the name of the controller. |
| 200 | * |
| 201 | * @return String name |
| 202 | */ |
| 203 | public String name() { |
| 204 | return name; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Exposes the password of the controller. |
| 209 | * |
| 210 | * @return String password |
| 211 | */ |
| 212 | public String password() { |
| 213 | return password; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Exposes the ip address of the controller. |
| 218 | * |
| 219 | * @return IpAddress ip address |
| 220 | */ |
| 221 | public IpAddress ip() { |
| 222 | return ipAddress; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Exposes the port of the controller. |
| 227 | * |
Yuta HIGUCHI | e3ae821 | 2017-04-20 10:18:41 -0700 | [diff] [blame] | 228 | * @return port number |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 229 | */ |
| 230 | public int port() { |
| 231 | return port; |
| 232 | } |
| 233 | |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 234 | /* |
| 235 | * Exposes the path of the aspect. |
| 236 | * |
| 237 | * @return path aspect |
| 238 | */ |
| 239 | public Optional<String> path() { |
| 240 | return path; |
| 241 | } |
| 242 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 243 | /** |
Himanshu Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 244 | * Exposes the key of the controller. |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 245 | * |
Andrea Campanella | e7006dc | 2017-02-15 16:04:09 -0800 | [diff] [blame] | 246 | * @return {@code char[]} containing a DSA or RSA private key of the user |
| 247 | * in OpenSSH key format |
| 248 | * or null if device is not configured to use public key authentication |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 249 | */ |
Himanshu Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 250 | public char[] getKey() { |
| 251 | return key; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /** |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 255 | * Exposes the Client library implementation. |
| 256 | * |
| 257 | * @return Enumerated value |
| 258 | */ |
| 259 | public Optional<NetconfSshClientLib> sshClientLib() { |
| 260 | return sshClientLib; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Exposes the device specific connect timeout. |
| 265 | * |
| 266 | * @return The timeout value in seconds |
| 267 | */ |
| 268 | public OptionalInt getConnectTimeoutSec() { |
| 269 | return connectTimeoutSec; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Exposes the device specific reply timeout. |
| 274 | * |
| 275 | * @return The timeout value in seconds |
| 276 | */ |
| 277 | public OptionalInt getReplyTimeoutSec() { |
| 278 | return replyTimeoutSec; |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Exposes the device specific idle timeout. |
| 283 | * |
| 284 | * @return The timeout value in seconds |
| 285 | */ |
| 286 | public OptionalInt getIdleTimeoutSec() { |
| 287 | return idleTimeoutSec; |
| 288 | } |
| 289 | |
| 290 | /** |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 291 | * Return the info about the device in a string. |
| 292 | * String format: "netconf:name@ip:port" |
| 293 | * |
| 294 | * @return String device info |
| 295 | */ |
Andrea Campanella | e7006dc | 2017-02-15 16:04:09 -0800 | [diff] [blame] | 296 | @Override |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 297 | public String toString() { |
David K. Bainbridge | 9b582b0 | 2019-02-01 16:04:05 -0800 | [diff] [blame] | 298 | return "netconf:" + name + "@" + ipAddress + ":" + port + |
| 299 | (path.isPresent() ? '/' + path.get() : ""); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Return the DeviceId about the device containing the URI. |
| 304 | * |
| 305 | * @return DeviceId |
| 306 | */ |
| 307 | public DeviceId getDeviceId() { |
Andrea Campanella | 57efbb2 | 2016-02-11 14:21:41 -0800 | [diff] [blame] | 308 | if (deviceId == null) { |
| 309 | try { |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 310 | deviceId = DeviceId.deviceId(new URI("netconf", ipAddress.toString() + ":" + port + |
| 311 | (path.isPresent() ? "/" + path.get() : ""), null)); |
Andrea Campanella | 57efbb2 | 2016-02-11 14:21:41 -0800 | [diff] [blame] | 312 | } catch (URISyntaxException e) { |
| 313 | throw new IllegalArgumentException("Unable to build deviceID for device " + toString(), e); |
| 314 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 315 | } |
Andrea Campanella | 57efbb2 | 2016-02-11 14:21:41 -0800 | [diff] [blame] | 316 | return deviceId; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | @Override |
| 320 | public int hashCode() { |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 321 | if (path.isPresent()) { |
| 322 | return Objects.hash(ipAddress, port, path.get(), name); |
| 323 | } else { |
| 324 | return Objects.hash(ipAddress, port, name); |
| 325 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | @Override |
| 329 | public boolean equals(Object toBeCompared) { |
| 330 | if (toBeCompared instanceof NetconfDeviceInfo) { |
| 331 | NetconfDeviceInfo netconfDeviceInfo = (NetconfDeviceInfo) toBeCompared; |
| 332 | if (netconfDeviceInfo.name().equals(name) |
| 333 | && netconfDeviceInfo.ip().equals(ipAddress) |
| 334 | && netconfDeviceInfo.port() == port |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 335 | && netconfDeviceInfo.path().equals(path) |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 336 | && netconfDeviceInfo.password().equals(password)) { |
| 337 | return true; |
| 338 | } |
| 339 | } |
| 340 | return false; |
| 341 | } |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 342 | |
| 343 | public static Triple<String, Integer, Optional<String>> extractIpPortPath(DeviceId deviceId) { |
| 344 | /* |
| 345 | * We can expect the following formats: |
| 346 | * |
| 347 | * netconf:ip:port/path |
| 348 | * netconf:ip:port |
| 349 | */ |
| 350 | String string = deviceId.toString(); |
| 351 | |
| 352 | /* |
| 353 | * The first ':' is the separation between the scheme and the IP. |
| 354 | * |
| 355 | * The last ':' will represent the separator between the IP and the port. |
| 356 | */ |
| 357 | int first = string.indexOf(':'); |
| 358 | int last = string.lastIndexOf(':'); |
| 359 | String ip = string.substring(first + 1, last); |
| 360 | String port = string.substring(last + 1); |
| 361 | String path = null; |
| 362 | int pathSep = port.indexOf('/'); |
| 363 | if (pathSep != -1) { |
| 364 | path = port.substring(pathSep + 1); |
| 365 | port = port.substring(0, pathSep); |
| 366 | } |
| 367 | |
| 368 | return Triple.of(ip, new Integer(port), |
| 369 | (path == null || path.isEmpty() ? Optional.empty() : Optional.of(path))); |
| 370 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 371 | } |