blob: be76056db5362be5e1ad86e70568524ef39d620d [file] [log] [blame]
andreaeb70a942015-10-16 21:34:46 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
Andrea Campanellae7006dc2017-02-15 16:04:09 -080025import java.io.File;
andreaeb70a942015-10-16 21:34:46 -070026import java.net.URI;
27import java.net.URISyntaxException;
28import java.util.Objects;
29
30/**
31 * Represents a Netconf device information.
32 */
33public class NetconfDeviceInfo {
34
35 public static final Logger log = LoggerFactory
36 .getLogger(NetconfDeviceInfo.class);
37
38 private String name;
39 private String password;
40 private IpAddress ipAddress;
41 private int port;
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -060042 private char[] key;
Andrea Campanellae7006dc2017-02-15 16:04:09 -080043 //File keyFile @deprecated 1.9.0
44 private File keyFile;
Andrea Campanella57efbb22016-02-11 14:21:41 -080045 private DeviceId deviceId;
andreaeb70a942015-10-16 21:34:46 -070046
47
48 /**
49 * Information for contacting the controller.
50 *
51 * @param name the connection type
52 * @param password the password for the device
53 * @param ipAddress the ip address
54 * @param port the tcp port
55 */
56 public NetconfDeviceInfo(String name, String password, IpAddress ipAddress,
57 int port) {
Andrea Campanella34cf65c2017-04-12 13:51:32 +020058 Preconditions.checkArgument(!name.equals(""), "Empty device username");
andreaeb70a942015-10-16 21:34:46 -070059 Preconditions.checkNotNull(port > 0, "Negative port");
60 Preconditions.checkNotNull(ipAddress, "Null ip address");
61 this.name = name;
62 this.password = password;
63 this.ipAddress = ipAddress;
64 this.port = port;
65 }
66
67 /**
68 * Information for contacting the controller.
69 *
70 * @param name the connection type
71 * @param password the password for the device
72 * @param ipAddress the ip address
73 * @param port the tcp port
Andrea Campanellae7006dc2017-02-15 16:04:09 -080074 * @param keyString the string containing a DSA or RSA private key
75 * of the user in OpenSSH key format
76 * <br>
77 * (Pre 1.9.0 behaviour: {@code keyString} can be file path
78 * to a file containing DSA or RSA private key of the user
79 * in OpenSSH key format)
andreaeb70a942015-10-16 21:34:46 -070080 */
81 public NetconfDeviceInfo(String name, String password, IpAddress ipAddress,
82 int port, String keyString) {
83 Preconditions.checkArgument(!name.equals(""), "Empty device name");
84 Preconditions.checkNotNull(port > 0, "Negative port");
85 Preconditions.checkNotNull(ipAddress, "Null ip address");
86 this.name = name;
87 this.password = password;
88 this.ipAddress = ipAddress;
89 this.port = port;
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -060090 this.key = keyString.toCharArray();
Andrea Campanellae7006dc2017-02-15 16:04:09 -080091 this.keyFile = new File(keyString);
andreaeb70a942015-10-16 21:34:46 -070092 }
93
94 /**
95 * Exposes the name of the controller.
96 *
97 * @return String name
98 */
99 public String name() {
100 return name;
101 }
102
103 /**
104 * Exposes the password of the controller.
105 *
106 * @return String password
107 */
108 public String password() {
109 return password;
110 }
111
112 /**
113 * Exposes the ip address of the controller.
114 *
115 * @return IpAddress ip address
116 */
117 public IpAddress ip() {
118 return ipAddress;
119 }
120
121 /**
122 * Exposes the port of the controller.
123 *
124 * @return int port address
125 */
126 public int port() {
127 return port;
128 }
129
130 /**
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600131 * Exposes the key of the controller.
andreaeb70a942015-10-16 21:34:46 -0700132 *
Andrea Campanellae7006dc2017-02-15 16:04:09 -0800133 * @return {@code char[]} containing a DSA or RSA private key of the user
134 * in OpenSSH key format
135 * or null if device is not configured to use public key authentication
andreaeb70a942015-10-16 21:34:46 -0700136 */
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600137 public char[] getKey() {
138 return key;
andreaeb70a942015-10-16 21:34:46 -0700139 }
140
141 /**
Andrea Campanellae7006dc2017-02-15 16:04:09 -0800142 * Exposes the keyFile of the controller.
143 *
144 * @return File object pointing to a file containing a DSA or RSA
145 * private key of the user in OpenSSH key format,
146 * or null if device is not configured to use public key authentication
147 * @deprecated 1.9.0
148 */
149 @Deprecated
150 public File getKeyFile() {
151 return keyFile;
152 }
153
154 /**
andreaeb70a942015-10-16 21:34:46 -0700155 * Return the info about the device in a string.
156 * String format: "netconf:name@ip:port"
157 *
158 * @return String device info
159 */
Andrea Campanellae7006dc2017-02-15 16:04:09 -0800160 @Override
andreaeb70a942015-10-16 21:34:46 -0700161 public String toString() {
162 return "netconf:" + name + "@" + ipAddress + ":" + port;
163 }
164
165 /**
166 * Return the DeviceId about the device containing the URI.
167 *
168 * @return DeviceId
169 */
170 public DeviceId getDeviceId() {
Andrea Campanella57efbb22016-02-11 14:21:41 -0800171 if (deviceId == null) {
172 try {
173 deviceId = DeviceId.deviceId(new URI("netconf", ipAddress.toString() + ":" + port, null));
174 } catch (URISyntaxException e) {
175 throw new IllegalArgumentException("Unable to build deviceID for device " + toString(), e);
176 }
andreaeb70a942015-10-16 21:34:46 -0700177 }
Andrea Campanella57efbb22016-02-11 14:21:41 -0800178 return deviceId;
andreaeb70a942015-10-16 21:34:46 -0700179 }
180
181 @Override
182 public int hashCode() {
183 return Objects.hash(ipAddress, port, name);
184 }
185
186 @Override
187 public boolean equals(Object toBeCompared) {
188 if (toBeCompared instanceof NetconfDeviceInfo) {
189 NetconfDeviceInfo netconfDeviceInfo = (NetconfDeviceInfo) toBeCompared;
190 if (netconfDeviceInfo.name().equals(name)
191 && netconfDeviceInfo.ip().equals(ipAddress)
192 && netconfDeviceInfo.port() == port
193 && netconfDeviceInfo.password().equals(password)) {
194 return true;
195 }
196 }
197 return false;
198 }
199}