andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package org.onosproject.netconf; |
| 18 | |
| 19 | import com.google.common.base.Preconditions; |
| 20 | import org.onlab.packet.IpAddress; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.slf4j.Logger; |
| 23 | import org.slf4j.LoggerFactory; |
| 24 | |
Andrea Campanella | e7006dc | 2017-02-15 16:04:09 -0800 | [diff] [blame] | 25 | import java.io.File; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 26 | import java.net.URI; |
| 27 | import java.net.URISyntaxException; |
| 28 | import java.util.Objects; |
| 29 | |
| 30 | /** |
| 31 | * Represents a Netconf device information. |
| 32 | */ |
| 33 | public 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 Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 42 | private char[] key; |
Andrea Campanella | e7006dc | 2017-02-15 16:04:09 -0800 | [diff] [blame] | 43 | //File keyFile @deprecated 1.9.0 |
| 44 | private File keyFile; |
Andrea Campanella | 57efbb2 | 2016-02-11 14:21:41 -0800 | [diff] [blame] | 45 | private DeviceId deviceId; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 46 | |
| 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 Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame^] | 58 | Preconditions.checkArgument(!name.equals(""), "Empty device username"); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 59 | 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 Campanella | e7006dc | 2017-02-15 16:04:09 -0800 | [diff] [blame] | 74 | * @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) |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 80 | */ |
| 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 Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 90 | this.key = keyString.toCharArray(); |
Andrea Campanella | e7006dc | 2017-02-15 16:04:09 -0800 | [diff] [blame] | 91 | this.keyFile = new File(keyString); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 92 | } |
| 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 Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 131 | * Exposes the key of the controller. |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 132 | * |
Andrea Campanella | e7006dc | 2017-02-15 16:04:09 -0800 | [diff] [blame] | 133 | * @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 |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 136 | */ |
Himanshu Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 137 | public char[] getKey() { |
| 138 | return key; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /** |
Andrea Campanella | e7006dc | 2017-02-15 16:04:09 -0800 | [diff] [blame] | 142 | * 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 | /** |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 155 | * Return the info about the device in a string. |
| 156 | * String format: "netconf:name@ip:port" |
| 157 | * |
| 158 | * @return String device info |
| 159 | */ |
Andrea Campanella | e7006dc | 2017-02-15 16:04:09 -0800 | [diff] [blame] | 160 | @Override |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 161 | 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 Campanella | 57efbb2 | 2016-02-11 14:21:41 -0800 | [diff] [blame] | 171 | 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 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 177 | } |
Andrea Campanella | 57efbb2 | 2016-02-11 14:21:41 -0800 | [diff] [blame] | 178 | return deviceId; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 179 | } |
| 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 | } |