blob: 3ebb9822c49a79bc87b3569df74705ba91b794dd [file] [log] [blame]
andreaeb70a942015-10-16 21:34:46 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
andreaeb70a942015-10-16 21:34:46 -07003 *
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
17package org.onosproject.netconf;
18
andreaeb70a942015-10-16 21:34:46 -070019import org.onlab.packet.IpAddress;
20import org.onosproject.net.DeviceId;
Sean Condon54d82432017-07-26 22:27:25 +010021import org.onosproject.netconf.config.NetconfDeviceConfig;
22import org.onosproject.netconf.config.NetconfSshClientLib;
andreaeb70a942015-10-16 21:34:46 -070023import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -070026import static com.google.common.base.Preconditions.checkArgument;
27import static com.google.common.base.Preconditions.checkNotNull;
28
Andrea Campanellae7006dc2017-02-15 16:04:09 -080029import java.io.File;
andreaeb70a942015-10-16 21:34:46 -070030import java.net.URI;
31import java.net.URISyntaxException;
32import java.util.Objects;
Sean Condon54d82432017-07-26 22:27:25 +010033import java.util.Optional;
34import java.util.OptionalInt;
andreaeb70a942015-10-16 21:34:46 -070035
36/**
37 * Represents a Netconf device information.
38 */
39public 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 Ranjan7c2ee3c2017-02-13 05:10:08 -060048 private char[] key;
Andrea Campanellae7006dc2017-02-15 16:04:09 -080049 //File keyFile @deprecated 1.9.0
Yuta HIGUCHIe3ae8212017-04-20 10:18:41 -070050 @Deprecated
Andrea Campanellae7006dc2017-02-15 16:04:09 -080051 private File keyFile;
Sean Condon54d82432017-07-26 22:27:25 +010052 private Optional<NetconfSshClientLib> sshClientLib;
53 private OptionalInt connectTimeoutSec;
54 private OptionalInt replyTimeoutSec;
55 private OptionalInt idleTimeoutSec;
Andrea Campanella57efbb22016-02-11 14:21:41 -080056 private DeviceId deviceId;
andreaeb70a942015-10-16 21:34:46 -070057
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 HIGUCHI09ae3682017-08-14 18:56:54 -070069 checkArgument(!name.equals(""), "Empty device username");
Yuta HIGUCHIfbd9ae92018-01-24 23:39:06 -080070 checkArgument(port > 0, "Negative port");
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -070071 checkNotNull(ipAddress, "Null ip address");
andreaeb70a942015-10-16 21:34:46 -070072 this.name = name;
73 this.password = password;
74 this.ipAddress = ipAddress;
75 this.port = port;
Sean Condon54d82432017-07-26 22:27:25 +010076 this.sshClientLib = Optional.empty();
77 this.connectTimeoutSec = OptionalInt.empty();
78 this.replyTimeoutSec = OptionalInt.empty();
79 this.idleTimeoutSec = OptionalInt.empty();
andreaeb70a942015-10-16 21:34:46 -070080 }
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 Campanellae7006dc2017-02-15 16:04:09 -080089 * @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)
andreaeb70a942015-10-16 21:34:46 -070095 */
96 public NetconfDeviceInfo(String name, String password, IpAddress ipAddress,
97 int port, String keyString) {
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -070098 checkArgument(!name.equals(""), "Empty device name");
Yuta HIGUCHIfbd9ae92018-01-24 23:39:06 -080099 checkArgument(port > 0, "Negative port");
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -0700100 checkNotNull(ipAddress, "Null ip address");
andreaeb70a942015-10-16 21:34:46 -0700101 this.name = name;
102 this.password = password;
103 this.ipAddress = ipAddress;
104 this.port = port;
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600105 this.key = keyString.toCharArray();
Andrea Campanellae7006dc2017-02-15 16:04:09 -0800106 this.keyFile = new File(keyString);
Sean Condon54d82432017-07-26 22:27:25 +0100107 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 HIGUCHI09ae3682017-08-14 18:56:54 -0700118 checkArgument(!netconfConfig.username().isEmpty(), "Empty device name");
Yuta HIGUCHIfbd9ae92018-01-24 23:39:06 -0800119 checkArgument(netconfConfig.port() > 0, "Negative port");
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -0700120 checkNotNull(netconfConfig.ip(), "Null ip address");
Sean Condon54d82432017-07-26 22:27:25 +0100121
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;
andreaeb70a942015-10-16 21:34:46 -0700174 }
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 HIGUCHIe3ae8212017-04-20 10:18:41 -0700206 * @return port number
andreaeb70a942015-10-16 21:34:46 -0700207 */
208 public int port() {
209 return port;
210 }
211
212 /**
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600213 * Exposes the key of the controller.
andreaeb70a942015-10-16 21:34:46 -0700214 *
Andrea Campanellae7006dc2017-02-15 16:04:09 -0800215 * @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
andreaeb70a942015-10-16 21:34:46 -0700218 */
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600219 public char[] getKey() {
220 return key;
andreaeb70a942015-10-16 21:34:46 -0700221 }
222
223 /**
Andrea Campanellae7006dc2017-02-15 16:04:09 -0800224 * 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 Condon54d82432017-07-26 22:27:25 +0100237 * 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 /**
andreaeb70a942015-10-16 21:34:46 -0700273 * Return the info about the device in a string.
274 * String format: "netconf:name@ip:port"
275 *
276 * @return String device info
277 */
Andrea Campanellae7006dc2017-02-15 16:04:09 -0800278 @Override
andreaeb70a942015-10-16 21:34:46 -0700279 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 Campanella57efbb22016-02-11 14:21:41 -0800289 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 }
andreaeb70a942015-10-16 21:34:46 -0700295 }
Andrea Campanella57efbb22016-02-11 14:21:41 -0800296 return deviceId;
andreaeb70a942015-10-16 21:34:46 -0700297 }
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}