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