blob: 930e7a50480345c1dfcbce5a8877042912a8a3f7 [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
David K. Bainbridge56e90232018-12-18 23:25:08 -080019import org.apache.commons.lang3.tuple.Triple;
andreaeb70a942015-10-16 21:34:46 -070020import org.onlab.packet.IpAddress;
21import org.onosproject.net.DeviceId;
Sean Condon54d82432017-07-26 22:27:25 +010022import org.onosproject.netconf.config.NetconfDeviceConfig;
23import org.onosproject.netconf.config.NetconfSshClientLib;
andreaeb70a942015-10-16 21:34:46 -070024import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
26
andreaeb70a942015-10-16 21:34:46 -070027import java.net.URI;
28import java.net.URISyntaxException;
29import java.util.Objects;
Sean Condon54d82432017-07-26 22:27:25 +010030import java.util.Optional;
31import java.util.OptionalInt;
andreaeb70a942015-10-16 21:34:46 -070032
Ray Milkeyd4b51c22018-02-08 11:36:43 -080033import static com.google.common.base.Preconditions.checkArgument;
34import static com.google.common.base.Preconditions.checkNotNull;
35
andreaeb70a942015-10-16 21:34:46 -070036/**
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;
David K. Bainbridge56e90232018-12-18 23:25:08 -080048 private Optional<String> path;
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -060049 private char[] key;
Sean Condon54d82432017-07-26 22:27:25 +010050 private Optional<NetconfSshClientLib> sshClientLib;
51 private OptionalInt connectTimeoutSec;
52 private OptionalInt replyTimeoutSec;
53 private OptionalInt idleTimeoutSec;
Andrea Campanella57efbb22016-02-11 14:21:41 -080054 private DeviceId deviceId;
andreaeb70a942015-10-16 21:34:46 -070055
andreaeb70a942015-10-16 21:34:46 -070056 /**
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. Bainbridge56e90232018-12-18 23:25:08 -080063 * @param path the path part
andreaeb70a942015-10-16 21:34:46 -070064 */
65 public NetconfDeviceInfo(String name, String password, IpAddress ipAddress,
David K. Bainbridge56e90232018-12-18 23:25:08 -080066 int port, String path) {
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -070067 checkArgument(!name.equals(""), "Empty device username");
Yuta HIGUCHIfbd9ae92018-01-24 23:39:06 -080068 checkArgument(port > 0, "Negative port");
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -070069 checkNotNull(ipAddress, "Null ip address");
andreaeb70a942015-10-16 21:34:46 -070070 this.name = name;
71 this.password = password;
72 this.ipAddress = ipAddress;
73 this.port = port;
David K. Bainbridge56e90232018-12-18 23:25:08 -080074 if (path == null || path.isEmpty()) {
75 this.path = Optional.empty();
76 } else {
77 this.path = Optional.of(path);
78 }
Sean Condon54d82432017-07-26 22:27:25 +010079 this.sshClientLib = Optional.empty();
80 this.connectTimeoutSec = OptionalInt.empty();
81 this.replyTimeoutSec = OptionalInt.empty();
82 this.idleTimeoutSec = OptionalInt.empty();
andreaeb70a942015-10-16 21:34:46 -070083 }
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. Bainbridge56e90232018-12-18 23:25:08 -080092 */
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 Campanellae7006dc2017-02-15 16:04:09 -0800106 * @param keyString the string containing a DSA or RSA private key
107 * of the user in OpenSSH key format
andreaeb70a942015-10-16 21:34:46 -0700108 */
109 public NetconfDeviceInfo(String name, String password, IpAddress ipAddress,
David K. Bainbridge56e90232018-12-18 23:25:08 -0800110 int port, String path, String keyString) {
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -0700111 checkArgument(!name.equals(""), "Empty device name");
Yuta HIGUCHIfbd9ae92018-01-24 23:39:06 -0800112 checkArgument(port > 0, "Negative port");
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -0700113 checkNotNull(ipAddress, "Null ip address");
andreaeb70a942015-10-16 21:34:46 -0700114 this.name = name;
115 this.password = password;
116 this.ipAddress = ipAddress;
117 this.port = port;
David K. Bainbridge56e90232018-12-18 23:25:08 -0800118 this.path = Optional.ofNullable(path);
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600119 this.key = keyString.toCharArray();
Sean Condon54d82432017-07-26 22:27:25 +0100120 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 HIGUCHI09ae3682017-08-14 18:56:54 -0700131 checkArgument(!netconfConfig.username().isEmpty(), "Empty device name");
Yuta HIGUCHIfbd9ae92018-01-24 23:39:06 -0800132 checkArgument(netconfConfig.port() > 0, "Negative port");
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -0700133 checkNotNull(netconfConfig.ip(), "Null ip address");
Sean Condon54d82432017-07-26 22:27:25 +0100134
135 this.name = netconfConfig.username();
136 this.password = netconfConfig.password();
137 this.ipAddress = netconfConfig.ip();
138 this.port = netconfConfig.port();
David K. Bainbridge56e90232018-12-18 23:25:08 -0800139 this.path = netconfConfig.path();
Sean Condon54d82432017-07-26 22:27:25 +0100140 if (netconfConfig.sshKey() != null && !netconfConfig.sshKey().isEmpty()) {
141 this.key = netconfConfig.sshKey().toCharArray();
142 }
Sean Condon54d82432017-07-26 22:27:25 +0100143 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;
andreaeb70a942015-10-16 21:34:46 -0700187 }
188
189 /**
David K. Bainbridge56e90232018-12-18 23:25:08 -0800190 * 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 /**
andreaeb70a942015-10-16 21:34:46 -0700199 * 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 HIGUCHIe3ae8212017-04-20 10:18:41 -0700228 * @return port number
andreaeb70a942015-10-16 21:34:46 -0700229 */
230 public int port() {
231 return port;
232 }
233
David K. Bainbridge56e90232018-12-18 23:25:08 -0800234 /*
235 * Exposes the path of the aspect.
236 *
237 * @return path aspect
238 */
239 public Optional<String> path() {
240 return path;
241 }
242
andreaeb70a942015-10-16 21:34:46 -0700243 /**
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600244 * Exposes the key of the controller.
andreaeb70a942015-10-16 21:34:46 -0700245 *
Andrea Campanellae7006dc2017-02-15 16:04:09 -0800246 * @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
andreaeb70a942015-10-16 21:34:46 -0700249 */
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600250 public char[] getKey() {
251 return key;
andreaeb70a942015-10-16 21:34:46 -0700252 }
253
254 /**
Sean Condon54d82432017-07-26 22:27:25 +0100255 * 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 /**
andreaeb70a942015-10-16 21:34:46 -0700291 * Return the info about the device in a string.
292 * String format: "netconf:name@ip:port"
293 *
294 * @return String device info
295 */
Andrea Campanellae7006dc2017-02-15 16:04:09 -0800296 @Override
andreaeb70a942015-10-16 21:34:46 -0700297 public String toString() {
298 return "netconf:" + name + "@" + ipAddress + ":" + port;
299 }
300
301 /**
302 * Return the DeviceId about the device containing the URI.
303 *
304 * @return DeviceId
305 */
306 public DeviceId getDeviceId() {
Andrea Campanella57efbb22016-02-11 14:21:41 -0800307 if (deviceId == null) {
308 try {
David K. Bainbridge56e90232018-12-18 23:25:08 -0800309 deviceId = DeviceId.deviceId(new URI("netconf", ipAddress.toString() + ":" + port +
310 (path.isPresent() ? "/" + path.get() : ""), null));
Andrea Campanella57efbb22016-02-11 14:21:41 -0800311 } catch (URISyntaxException e) {
312 throw new IllegalArgumentException("Unable to build deviceID for device " + toString(), e);
313 }
andreaeb70a942015-10-16 21:34:46 -0700314 }
Andrea Campanella57efbb22016-02-11 14:21:41 -0800315 return deviceId;
andreaeb70a942015-10-16 21:34:46 -0700316 }
317
318 @Override
319 public int hashCode() {
David K. Bainbridge56e90232018-12-18 23:25:08 -0800320 if (path.isPresent()) {
321 return Objects.hash(ipAddress, port, path.get(), name);
322 } else {
323 return Objects.hash(ipAddress, port, name);
324 }
andreaeb70a942015-10-16 21:34:46 -0700325 }
326
327 @Override
328 public boolean equals(Object toBeCompared) {
329 if (toBeCompared instanceof NetconfDeviceInfo) {
330 NetconfDeviceInfo netconfDeviceInfo = (NetconfDeviceInfo) toBeCompared;
331 if (netconfDeviceInfo.name().equals(name)
332 && netconfDeviceInfo.ip().equals(ipAddress)
333 && netconfDeviceInfo.port() == port
David K. Bainbridge56e90232018-12-18 23:25:08 -0800334 && netconfDeviceInfo.path().equals(path)
andreaeb70a942015-10-16 21:34:46 -0700335 && netconfDeviceInfo.password().equals(password)) {
336 return true;
337 }
338 }
339 return false;
340 }
David K. Bainbridge56e90232018-12-18 23:25:08 -0800341
342 public static Triple<String, Integer, Optional<String>> extractIpPortPath(DeviceId deviceId) {
343 /*
344 * We can expect the following formats:
345 *
346 * netconf:ip:port/path
347 * netconf:ip:port
348 */
349 String string = deviceId.toString();
350
351 /*
352 * The first ':' is the separation between the scheme and the IP.
353 *
354 * The last ':' will represent the separator between the IP and the port.
355 */
356 int first = string.indexOf(':');
357 int last = string.lastIndexOf(':');
358 String ip = string.substring(first + 1, last);
359 String port = string.substring(last + 1);
360 String path = null;
361 int pathSep = port.indexOf('/');
362 if (pathSep != -1) {
363 path = port.substring(pathSep + 1);
364 port = port.substring(0, pathSep);
365 }
366
367 return Triple.of(ip, new Integer(port),
368 (path == null || path.isEmpty() ? Optional.empty() : Optional.of(path)));
369 }
andreaeb70a942015-10-16 21:34:46 -0700370}