blob: 8d168734acd4a8116e30272a2d33f037fbd5a939 [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
andreaeb70a942015-10-16 21:34:46 -070026import java.net.URI;
27import java.net.URISyntaxException;
28import java.util.Objects;
Sean Condon54d82432017-07-26 22:27:25 +010029import java.util.Optional;
30import java.util.OptionalInt;
andreaeb70a942015-10-16 21:34:46 -070031
Ray Milkeyd4b51c22018-02-08 11:36:43 -080032import static com.google.common.base.Preconditions.checkArgument;
33import static com.google.common.base.Preconditions.checkNotNull;
34
andreaeb70a942015-10-16 21:34:46 -070035/**
36 * Represents a Netconf device information.
37 */
38public class NetconfDeviceInfo {
39
40 public static final Logger log = LoggerFactory
41 .getLogger(NetconfDeviceInfo.class);
42
43 private String name;
44 private String password;
45 private IpAddress ipAddress;
46 private int port;
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -060047 private char[] key;
Sean Condon54d82432017-07-26 22:27:25 +010048 private Optional<NetconfSshClientLib> sshClientLib;
49 private OptionalInt connectTimeoutSec;
50 private OptionalInt replyTimeoutSec;
51 private OptionalInt idleTimeoutSec;
Andrea Campanella57efbb22016-02-11 14:21:41 -080052 private DeviceId deviceId;
andreaeb70a942015-10-16 21:34:46 -070053
54
55 /**
56 * Information for contacting the controller.
57 *
58 * @param name the connection type
59 * @param password the password for the device
60 * @param ipAddress the ip address
61 * @param port the tcp port
62 */
63 public NetconfDeviceInfo(String name, String password, IpAddress ipAddress,
64 int port) {
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -070065 checkArgument(!name.equals(""), "Empty device username");
Yuta HIGUCHIfbd9ae92018-01-24 23:39:06 -080066 checkArgument(port > 0, "Negative port");
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -070067 checkNotNull(ipAddress, "Null ip address");
andreaeb70a942015-10-16 21:34:46 -070068 this.name = name;
69 this.password = password;
70 this.ipAddress = ipAddress;
71 this.port = port;
Sean Condon54d82432017-07-26 22:27:25 +010072 this.sshClientLib = Optional.empty();
73 this.connectTimeoutSec = OptionalInt.empty();
74 this.replyTimeoutSec = OptionalInt.empty();
75 this.idleTimeoutSec = OptionalInt.empty();
andreaeb70a942015-10-16 21:34:46 -070076 }
77
78 /**
79 * Information for contacting the controller.
80 *
81 * @param name the connection type
82 * @param password the password for the device
83 * @param ipAddress the ip address
84 * @param port the tcp port
Andrea Campanellae7006dc2017-02-15 16:04:09 -080085 * @param keyString the string containing a DSA or RSA private key
86 * of the user in OpenSSH key format
andreaeb70a942015-10-16 21:34:46 -070087 */
88 public NetconfDeviceInfo(String name, String password, IpAddress ipAddress,
89 int port, String keyString) {
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -070090 checkArgument(!name.equals(""), "Empty device name");
Yuta HIGUCHIfbd9ae92018-01-24 23:39:06 -080091 checkArgument(port > 0, "Negative port");
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -070092 checkNotNull(ipAddress, "Null ip address");
andreaeb70a942015-10-16 21:34:46 -070093 this.name = name;
94 this.password = password;
95 this.ipAddress = ipAddress;
96 this.port = port;
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -060097 this.key = keyString.toCharArray();
Sean Condon54d82432017-07-26 22:27:25 +010098 this.sshClientLib = Optional.empty();
99 this.connectTimeoutSec = OptionalInt.empty();
100 this.replyTimeoutSec = OptionalInt.empty();
101 this.idleTimeoutSec = OptionalInt.empty();
102 }
103
104 /**
105 * Convenieince constructor that converts all known fields from NetCfg data.
106 * @param netconfConfig NetCf configuration
107 */
108 public NetconfDeviceInfo(NetconfDeviceConfig netconfConfig) {
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -0700109 checkArgument(!netconfConfig.username().isEmpty(), "Empty device name");
Yuta HIGUCHIfbd9ae92018-01-24 23:39:06 -0800110 checkArgument(netconfConfig.port() > 0, "Negative port");
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -0700111 checkNotNull(netconfConfig.ip(), "Null ip address");
Sean Condon54d82432017-07-26 22:27:25 +0100112
113 this.name = netconfConfig.username();
114 this.password = netconfConfig.password();
115 this.ipAddress = netconfConfig.ip();
116 this.port = netconfConfig.port();
117 if (netconfConfig.sshKey() != null && !netconfConfig.sshKey().isEmpty()) {
118 this.key = netconfConfig.sshKey().toCharArray();
119 }
Sean Condon54d82432017-07-26 22:27:25 +0100120 if (netconfConfig.sshClient().isPresent()) {
121 this.sshClientLib = Optional.of(NetconfSshClientLib.getEnum(netconfConfig.sshClient().get()));
122 } else {
123 this.sshClientLib = Optional.empty();
124 }
125 this.connectTimeoutSec = netconfConfig.connectTimeout();
126 this.replyTimeoutSec = netconfConfig.replyTimeout();
127 this.idleTimeoutSec = netconfConfig.idleTimeout();
128 }
129
130 /**
131 * Allows the NETCONF SSH Client library to be set.
132 *
133 * @param sshClientLib An enumerated value
134 */
135 public void setSshClientLib(Optional<NetconfSshClientLib> sshClientLib) {
136 this.sshClientLib = sshClientLib;
137 }
138
139 /**
140 * Allows the NETCONF SSH session initial connect timeout to be set.
141 *
142 * @param connectTimeoutSec value in seconds
143 */
144 public void setConnectTimeoutSec(OptionalInt connectTimeoutSec) {
145 this.connectTimeoutSec = connectTimeoutSec;
146 }
147
148 /**
149 * Allows the NETCONF SSH session replies timeout to be set.
150 *
151 * @param replyTimeoutSec value in seconds
152 */
153 public void setReplyTimeoutSec(OptionalInt replyTimeoutSec) {
154 this.replyTimeoutSec = replyTimeoutSec;
155 }
156
157 /**
158 * Allows the NETCONF SSH session idle timeout to be set.
159 *
160 * @param idleTimeoutSec value in seconds
161 */
162 public void setIdleTimeoutSec(OptionalInt idleTimeoutSec) {
163 this.idleTimeoutSec = idleTimeoutSec;
andreaeb70a942015-10-16 21:34:46 -0700164 }
165
166 /**
167 * Exposes the name of the controller.
168 *
169 * @return String name
170 */
171 public String name() {
172 return name;
173 }
174
175 /**
176 * Exposes the password of the controller.
177 *
178 * @return String password
179 */
180 public String password() {
181 return password;
182 }
183
184 /**
185 * Exposes the ip address of the controller.
186 *
187 * @return IpAddress ip address
188 */
189 public IpAddress ip() {
190 return ipAddress;
191 }
192
193 /**
194 * Exposes the port of the controller.
195 *
Yuta HIGUCHIe3ae8212017-04-20 10:18:41 -0700196 * @return port number
andreaeb70a942015-10-16 21:34:46 -0700197 */
198 public int port() {
199 return port;
200 }
201
202 /**
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600203 * Exposes the key of the controller.
andreaeb70a942015-10-16 21:34:46 -0700204 *
Andrea Campanellae7006dc2017-02-15 16:04:09 -0800205 * @return {@code char[]} containing a DSA or RSA private key of the user
206 * in OpenSSH key format
207 * or null if device is not configured to use public key authentication
andreaeb70a942015-10-16 21:34:46 -0700208 */
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600209 public char[] getKey() {
210 return key;
andreaeb70a942015-10-16 21:34:46 -0700211 }
212
213 /**
Sean Condon54d82432017-07-26 22:27:25 +0100214 * Exposes the Client library implementation.
215 *
216 * @return Enumerated value
217 */
218 public Optional<NetconfSshClientLib> sshClientLib() {
219 return sshClientLib;
220 }
221
222 /**
223 * Exposes the device specific connect timeout.
224 *
225 * @return The timeout value in seconds
226 */
227 public OptionalInt getConnectTimeoutSec() {
228 return connectTimeoutSec;
229 }
230
231 /**
232 * Exposes the device specific reply timeout.
233 *
234 * @return The timeout value in seconds
235 */
236 public OptionalInt getReplyTimeoutSec() {
237 return replyTimeoutSec;
238 }
239
240 /**
241 * Exposes the device specific idle timeout.
242 *
243 * @return The timeout value in seconds
244 */
245 public OptionalInt getIdleTimeoutSec() {
246 return idleTimeoutSec;
247 }
248
249 /**
andreaeb70a942015-10-16 21:34:46 -0700250 * Return the info about the device in a string.
251 * String format: "netconf:name@ip:port"
252 *
253 * @return String device info
254 */
Andrea Campanellae7006dc2017-02-15 16:04:09 -0800255 @Override
andreaeb70a942015-10-16 21:34:46 -0700256 public String toString() {
257 return "netconf:" + name + "@" + ipAddress + ":" + port;
258 }
259
260 /**
261 * Return the DeviceId about the device containing the URI.
262 *
263 * @return DeviceId
264 */
265 public DeviceId getDeviceId() {
Andrea Campanella57efbb22016-02-11 14:21:41 -0800266 if (deviceId == null) {
267 try {
268 deviceId = DeviceId.deviceId(new URI("netconf", ipAddress.toString() + ":" + port, null));
269 } catch (URISyntaxException e) {
270 throw new IllegalArgumentException("Unable to build deviceID for device " + toString(), e);
271 }
andreaeb70a942015-10-16 21:34:46 -0700272 }
Andrea Campanella57efbb22016-02-11 14:21:41 -0800273 return deviceId;
andreaeb70a942015-10-16 21:34:46 -0700274 }
275
276 @Override
277 public int hashCode() {
278 return Objects.hash(ipAddress, port, name);
279 }
280
281 @Override
282 public boolean equals(Object toBeCompared) {
283 if (toBeCompared instanceof NetconfDeviceInfo) {
284 NetconfDeviceInfo netconfDeviceInfo = (NetconfDeviceInfo) toBeCompared;
285 if (netconfDeviceInfo.name().equals(name)
286 && netconfDeviceInfo.ip().equals(ipAddress)
287 && netconfDeviceInfo.port() == port
288 && netconfDeviceInfo.password().equals(password)) {
289 return true;
290 }
291 }
292 return false;
293 }
294}