blob: 8ff65bf4b3cbb7d19ce83ff6353a0f2970c31e2d [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
Yuta HIGUCHIe3ae8212017-04-20 10:18:41 -070044 @Deprecated
Andrea Campanellae7006dc2017-02-15 16:04:09 -080045 private File keyFile;
Andrea Campanella57efbb22016-02-11 14:21:41 -080046 private DeviceId deviceId;
andreaeb70a942015-10-16 21:34:46 -070047
48
49 /**
50 * Information for contacting the controller.
51 *
52 * @param name the connection type
53 * @param password the password for the device
54 * @param ipAddress the ip address
55 * @param port the tcp port
56 */
57 public NetconfDeviceInfo(String name, String password, IpAddress ipAddress,
58 int port) {
Andrea Campanella34cf65c2017-04-12 13:51:32 +020059 Preconditions.checkArgument(!name.equals(""), "Empty device username");
andreaeb70a942015-10-16 21:34:46 -070060 Preconditions.checkNotNull(port > 0, "Negative port");
61 Preconditions.checkNotNull(ipAddress, "Null ip address");
62 this.name = name;
63 this.password = password;
64 this.ipAddress = ipAddress;
65 this.port = port;
66 }
67
68 /**
69 * Information for contacting the controller.
70 *
71 * @param name the connection type
72 * @param password the password for the device
73 * @param ipAddress the ip address
74 * @param port the tcp port
Andrea Campanellae7006dc2017-02-15 16:04:09 -080075 * @param keyString the string containing a DSA or RSA private key
76 * of the user in OpenSSH key format
77 * <br>
78 * (Pre 1.9.0 behaviour: {@code keyString} can be file path
79 * to a file containing DSA or RSA private key of the user
80 * in OpenSSH key format)
andreaeb70a942015-10-16 21:34:46 -070081 */
82 public NetconfDeviceInfo(String name, String password, IpAddress ipAddress,
83 int port, String keyString) {
84 Preconditions.checkArgument(!name.equals(""), "Empty device name");
85 Preconditions.checkNotNull(port > 0, "Negative port");
86 Preconditions.checkNotNull(ipAddress, "Null ip address");
87 this.name = name;
88 this.password = password;
89 this.ipAddress = ipAddress;
90 this.port = port;
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -060091 this.key = keyString.toCharArray();
Andrea Campanellae7006dc2017-02-15 16:04:09 -080092 this.keyFile = new File(keyString);
andreaeb70a942015-10-16 21:34:46 -070093 }
94
95 /**
96 * Exposes the name of the controller.
97 *
98 * @return String name
99 */
100 public String name() {
101 return name;
102 }
103
104 /**
105 * Exposes the password of the controller.
106 *
107 * @return String password
108 */
109 public String password() {
110 return password;
111 }
112
113 /**
114 * Exposes the ip address of the controller.
115 *
116 * @return IpAddress ip address
117 */
118 public IpAddress ip() {
119 return ipAddress;
120 }
121
122 /**
123 * Exposes the port of the controller.
124 *
Yuta HIGUCHIe3ae8212017-04-20 10:18:41 -0700125 * @return port number
andreaeb70a942015-10-16 21:34:46 -0700126 */
127 public int port() {
128 return port;
129 }
130
131 /**
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600132 * Exposes the key of the controller.
andreaeb70a942015-10-16 21:34:46 -0700133 *
Andrea Campanellae7006dc2017-02-15 16:04:09 -0800134 * @return {@code char[]} containing a DSA or RSA private key of the user
135 * in OpenSSH key format
136 * or null if device is not configured to use public key authentication
andreaeb70a942015-10-16 21:34:46 -0700137 */
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600138 public char[] getKey() {
139 return key;
andreaeb70a942015-10-16 21:34:46 -0700140 }
141
142 /**
Andrea Campanellae7006dc2017-02-15 16:04:09 -0800143 * Exposes the keyFile of the controller.
144 *
145 * @return File object pointing to a file containing a DSA or RSA
146 * private key of the user in OpenSSH key format,
147 * or null if device is not configured to use public key authentication
148 * @deprecated 1.9.0
149 */
150 @Deprecated
151 public File getKeyFile() {
152 return keyFile;
153 }
154
155 /**
andreaeb70a942015-10-16 21:34:46 -0700156 * Return the info about the device in a string.
157 * String format: "netconf:name@ip:port"
158 *
159 * @return String device info
160 */
Andrea Campanellae7006dc2017-02-15 16:04:09 -0800161 @Override
andreaeb70a942015-10-16 21:34:46 -0700162 public String toString() {
163 return "netconf:" + name + "@" + ipAddress + ":" + port;
164 }
165
166 /**
167 * Return the DeviceId about the device containing the URI.
168 *
169 * @return DeviceId
170 */
171 public DeviceId getDeviceId() {
Andrea Campanella57efbb22016-02-11 14:21:41 -0800172 if (deviceId == null) {
173 try {
174 deviceId = DeviceId.deviceId(new URI("netconf", ipAddress.toString() + ":" + port, null));
175 } catch (URISyntaxException e) {
176 throw new IllegalArgumentException("Unable to build deviceID for device " + toString(), e);
177 }
andreaeb70a942015-10-16 21:34:46 -0700178 }
Andrea Campanella57efbb22016-02-11 14:21:41 -0800179 return deviceId;
andreaeb70a942015-10-16 21:34:46 -0700180 }
181
182 @Override
183 public int hashCode() {
184 return Objects.hash(ipAddress, port, name);
185 }
186
187 @Override
188 public boolean equals(Object toBeCompared) {
189 if (toBeCompared instanceof NetconfDeviceInfo) {
190 NetconfDeviceInfo netconfDeviceInfo = (NetconfDeviceInfo) toBeCompared;
191 if (netconfDeviceInfo.name().equals(name)
192 && netconfDeviceInfo.ip().equals(ipAddress)
193 && netconfDeviceInfo.port() == port
194 && netconfDeviceInfo.password().equals(password)) {
195 return true;
196 }
197 }
198 return false;
199 }
200}