blob: c99176236a4f43e7802c1219ed7551eb033fafc2 [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
19import com.google.common.base.Preconditions;
20import 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
Andrea Campanellae7006dc2017-02-15 16:04:09 -080027import java.io.File;
andreaeb70a942015-10-16 21:34:46 -070028import java.net.URI;
29import java.net.URISyntaxException;
30import java.util.Objects;
Sean Condon54d82432017-07-26 22:27:25 +010031import java.util.Optional;
32import java.util.OptionalInt;
andreaeb70a942015-10-16 21:34:46 -070033
34/**
35 * Represents a Netconf device information.
36 */
37public class NetconfDeviceInfo {
38
39 public static final Logger log = LoggerFactory
40 .getLogger(NetconfDeviceInfo.class);
41
42 private String name;
43 private String password;
44 private IpAddress ipAddress;
45 private int port;
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -060046 private char[] key;
Andrea Campanellae7006dc2017-02-15 16:04:09 -080047 //File keyFile @deprecated 1.9.0
Yuta HIGUCHIe3ae8212017-04-20 10:18:41 -070048 @Deprecated
Andrea Campanellae7006dc2017-02-15 16:04:09 -080049 private File keyFile;
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
56
57 /**
58 * Information for contacting the controller.
59 *
60 * @param name the connection type
61 * @param password the password for the device
62 * @param ipAddress the ip address
63 * @param port the tcp port
64 */
65 public NetconfDeviceInfo(String name, String password, IpAddress ipAddress,
66 int port) {
Andrea Campanella34cf65c2017-04-12 13:51:32 +020067 Preconditions.checkArgument(!name.equals(""), "Empty device username");
andreaeb70a942015-10-16 21:34:46 -070068 Preconditions.checkNotNull(port > 0, "Negative port");
69 Preconditions.checkNotNull(ipAddress, "Null ip address");
70 this.name = name;
71 this.password = password;
72 this.ipAddress = ipAddress;
73 this.port = port;
Sean Condon54d82432017-07-26 22:27:25 +010074 this.sshClientLib = Optional.empty();
75 this.connectTimeoutSec = OptionalInt.empty();
76 this.replyTimeoutSec = OptionalInt.empty();
77 this.idleTimeoutSec = OptionalInt.empty();
andreaeb70a942015-10-16 21:34:46 -070078 }
79
80 /**
81 * Information for contacting the controller.
82 *
83 * @param name the connection type
84 * @param password the password for the device
85 * @param ipAddress the ip address
86 * @param port the tcp port
Andrea Campanellae7006dc2017-02-15 16:04:09 -080087 * @param keyString the string containing a DSA or RSA private key
88 * of the user in OpenSSH key format
89 * <br>
90 * (Pre 1.9.0 behaviour: {@code keyString} can be file path
91 * to a file containing DSA or RSA private key of the user
92 * in OpenSSH key format)
andreaeb70a942015-10-16 21:34:46 -070093 */
94 public NetconfDeviceInfo(String name, String password, IpAddress ipAddress,
95 int port, String keyString) {
96 Preconditions.checkArgument(!name.equals(""), "Empty device name");
97 Preconditions.checkNotNull(port > 0, "Negative port");
98 Preconditions.checkNotNull(ipAddress, "Null ip address");
99 this.name = name;
100 this.password = password;
101 this.ipAddress = ipAddress;
102 this.port = port;
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600103 this.key = keyString.toCharArray();
Andrea Campanellae7006dc2017-02-15 16:04:09 -0800104 this.keyFile = new File(keyString);
Sean Condon54d82432017-07-26 22:27:25 +0100105 this.sshClientLib = Optional.empty();
106 this.connectTimeoutSec = OptionalInt.empty();
107 this.replyTimeoutSec = OptionalInt.empty();
108 this.idleTimeoutSec = OptionalInt.empty();
109 }
110
111 /**
112 * Convenieince constructor that converts all known fields from NetCfg data.
113 * @param netconfConfig NetCf configuration
114 */
115 public NetconfDeviceInfo(NetconfDeviceConfig netconfConfig) {
116 Preconditions.checkArgument(!netconfConfig.username().isEmpty(), "Empty device name");
117 Preconditions.checkNotNull(netconfConfig.port() > 0, "Negative port");
118 Preconditions.checkNotNull(netconfConfig.ip(), "Null ip address");
119
120 this.name = netconfConfig.username();
121 this.password = netconfConfig.password();
122 this.ipAddress = netconfConfig.ip();
123 this.port = netconfConfig.port();
124 if (netconfConfig.sshKey() != null && !netconfConfig.sshKey().isEmpty()) {
125 this.key = netconfConfig.sshKey().toCharArray();
126 }
127 this.keyFile = new File(netconfConfig.sshKey());
128 if (netconfConfig.sshClient().isPresent()) {
129 this.sshClientLib = Optional.of(NetconfSshClientLib.getEnum(netconfConfig.sshClient().get()));
130 } else {
131 this.sshClientLib = Optional.empty();
132 }
133 this.connectTimeoutSec = netconfConfig.connectTimeout();
134 this.replyTimeoutSec = netconfConfig.replyTimeout();
135 this.idleTimeoutSec = netconfConfig.idleTimeout();
136 }
137
138 /**
139 * Allows the NETCONF SSH Client library to be set.
140 *
141 * @param sshClientLib An enumerated value
142 */
143 public void setSshClientLib(Optional<NetconfSshClientLib> sshClientLib) {
144 this.sshClientLib = sshClientLib;
145 }
146
147 /**
148 * Allows the NETCONF SSH session initial connect timeout to be set.
149 *
150 * @param connectTimeoutSec value in seconds
151 */
152 public void setConnectTimeoutSec(OptionalInt connectTimeoutSec) {
153 this.connectTimeoutSec = connectTimeoutSec;
154 }
155
156 /**
157 * Allows the NETCONF SSH session replies timeout to be set.
158 *
159 * @param replyTimeoutSec value in seconds
160 */
161 public void setReplyTimeoutSec(OptionalInt replyTimeoutSec) {
162 this.replyTimeoutSec = replyTimeoutSec;
163 }
164
165 /**
166 * Allows the NETCONF SSH session idle timeout to be set.
167 *
168 * @param idleTimeoutSec value in seconds
169 */
170 public void setIdleTimeoutSec(OptionalInt idleTimeoutSec) {
171 this.idleTimeoutSec = idleTimeoutSec;
andreaeb70a942015-10-16 21:34:46 -0700172 }
173
174 /**
175 * Exposes the name of the controller.
176 *
177 * @return String name
178 */
179 public String name() {
180 return name;
181 }
182
183 /**
184 * Exposes the password of the controller.
185 *
186 * @return String password
187 */
188 public String password() {
189 return password;
190 }
191
192 /**
193 * Exposes the ip address of the controller.
194 *
195 * @return IpAddress ip address
196 */
197 public IpAddress ip() {
198 return ipAddress;
199 }
200
201 /**
202 * Exposes the port of the controller.
203 *
Yuta HIGUCHIe3ae8212017-04-20 10:18:41 -0700204 * @return port number
andreaeb70a942015-10-16 21:34:46 -0700205 */
206 public int port() {
207 return port;
208 }
209
210 /**
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600211 * Exposes the key of the controller.
andreaeb70a942015-10-16 21:34:46 -0700212 *
Andrea Campanellae7006dc2017-02-15 16:04:09 -0800213 * @return {@code char[]} containing a DSA or RSA private key of the user
214 * in OpenSSH key format
215 * or null if device is not configured to use public key authentication
andreaeb70a942015-10-16 21:34:46 -0700216 */
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600217 public char[] getKey() {
218 return key;
andreaeb70a942015-10-16 21:34:46 -0700219 }
220
221 /**
Andrea Campanellae7006dc2017-02-15 16:04:09 -0800222 * Exposes the keyFile of the controller.
223 *
224 * @return File object pointing to a file containing a DSA or RSA
225 * private key of the user in OpenSSH key format,
226 * or null if device is not configured to use public key authentication
227 * @deprecated 1.9.0
228 */
229 @Deprecated
230 public File getKeyFile() {
231 return keyFile;
232 }
233
234 /**
Sean Condon54d82432017-07-26 22:27:25 +0100235 * Exposes the Client library implementation.
236 *
237 * @return Enumerated value
238 */
239 public Optional<NetconfSshClientLib> sshClientLib() {
240 return sshClientLib;
241 }
242
243 /**
244 * Exposes the device specific connect timeout.
245 *
246 * @return The timeout value in seconds
247 */
248 public OptionalInt getConnectTimeoutSec() {
249 return connectTimeoutSec;
250 }
251
252 /**
253 * Exposes the device specific reply timeout.
254 *
255 * @return The timeout value in seconds
256 */
257 public OptionalInt getReplyTimeoutSec() {
258 return replyTimeoutSec;
259 }
260
261 /**
262 * Exposes the device specific idle timeout.
263 *
264 * @return The timeout value in seconds
265 */
266 public OptionalInt getIdleTimeoutSec() {
267 return idleTimeoutSec;
268 }
269
270 /**
andreaeb70a942015-10-16 21:34:46 -0700271 * Return the info about the device in a string.
272 * String format: "netconf:name@ip:port"
273 *
274 * @return String device info
275 */
Andrea Campanellae7006dc2017-02-15 16:04:09 -0800276 @Override
andreaeb70a942015-10-16 21:34:46 -0700277 public String toString() {
278 return "netconf:" + name + "@" + ipAddress + ":" + port;
279 }
280
281 /**
282 * Return the DeviceId about the device containing the URI.
283 *
284 * @return DeviceId
285 */
286 public DeviceId getDeviceId() {
Andrea Campanella57efbb22016-02-11 14:21:41 -0800287 if (deviceId == null) {
288 try {
289 deviceId = DeviceId.deviceId(new URI("netconf", ipAddress.toString() + ":" + port, null));
290 } catch (URISyntaxException e) {
291 throw new IllegalArgumentException("Unable to build deviceID for device " + toString(), e);
292 }
andreaeb70a942015-10-16 21:34:46 -0700293 }
Andrea Campanella57efbb22016-02-11 14:21:41 -0800294 return deviceId;
andreaeb70a942015-10-16 21:34:46 -0700295 }
296
297 @Override
298 public int hashCode() {
299 return Objects.hash(ipAddress, port, name);
300 }
301
302 @Override
303 public boolean equals(Object toBeCompared) {
304 if (toBeCompared instanceof NetconfDeviceInfo) {
305 NetconfDeviceInfo netconfDeviceInfo = (NetconfDeviceInfo) toBeCompared;
306 if (netconfDeviceInfo.name().equals(name)
307 && netconfDeviceInfo.ip().equals(ipAddress)
308 && netconfDeviceInfo.port() == port
309 && netconfDeviceInfo.password().equals(password)) {
310 return true;
311 }
312 }
313 return false;
314 }
315}