blob: 24685622bd156100f811aa7ff12b2b87f474daf4 [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
andreaeb70a942015-10-16 21:34:46 -070025import java.net.URI;
26import java.net.URISyntaxException;
27import java.util.Objects;
28
29/**
30 * Represents a Netconf device information.
31 */
32public class NetconfDeviceInfo {
33
34 public static final Logger log = LoggerFactory
35 .getLogger(NetconfDeviceInfo.class);
36
37 private String name;
38 private String password;
39 private IpAddress ipAddress;
40 private int port;
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -060041 private char[] key;
Andrea Campanella57efbb22016-02-11 14:21:41 -080042 private DeviceId deviceId;
andreaeb70a942015-10-16 21:34:46 -070043
44
45 /**
46 * Information for contacting the controller.
47 *
48 * @param name the connection type
49 * @param password the password for the device
50 * @param ipAddress the ip address
51 * @param port the tcp port
52 */
53 public NetconfDeviceInfo(String name, String password, IpAddress ipAddress,
54 int port) {
55 Preconditions.checkArgument(!name.equals(""), "Empty device name");
56 Preconditions.checkNotNull(port > 0, "Negative port");
57 Preconditions.checkNotNull(ipAddress, "Null ip address");
58 this.name = name;
59 this.password = password;
60 this.ipAddress = ipAddress;
61 this.port = port;
62 }
63
64 /**
65 * Information for contacting the controller.
66 *
67 * @param name the connection type
68 * @param password the password for the device
69 * @param ipAddress the ip address
70 * @param port the tcp port
Andrea Campanella1cd641b2015-12-07 17:28:34 -080071 * @param keyString the string containing the key.
andreaeb70a942015-10-16 21:34:46 -070072 */
73 public NetconfDeviceInfo(String name, String password, IpAddress ipAddress,
74 int port, String keyString) {
75 Preconditions.checkArgument(!name.equals(""), "Empty device name");
76 Preconditions.checkNotNull(port > 0, "Negative port");
77 Preconditions.checkNotNull(ipAddress, "Null ip address");
78 this.name = name;
79 this.password = password;
80 this.ipAddress = ipAddress;
81 this.port = port;
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -060082 this.key = keyString.toCharArray();
andreaeb70a942015-10-16 21:34:46 -070083 }
84
85 /**
86 * Exposes the name of the controller.
87 *
88 * @return String name
89 */
90 public String name() {
91 return name;
92 }
93
94 /**
95 * Exposes the password of the controller.
96 *
97 * @return String password
98 */
99 public String password() {
100 return password;
101 }
102
103 /**
104 * Exposes the ip address of the controller.
105 *
106 * @return IpAddress ip address
107 */
108 public IpAddress ip() {
109 return ipAddress;
110 }
111
112 /**
113 * Exposes the port of the controller.
114 *
115 * @return int port address
116 */
117 public int port() {
118 return port;
119 }
120
121 /**
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600122 * Exposes the key of the controller.
andreaeb70a942015-10-16 21:34:46 -0700123 *
124 * @return int port address
125 */
Himanshu Ranjan7c2ee3c2017-02-13 05:10:08 -0600126 public char[] getKey() {
127 return key;
andreaeb70a942015-10-16 21:34:46 -0700128 }
129
130 /**
131 * Return the info about the device in a string.
132 * String format: "netconf:name@ip:port"
133 *
134 * @return String device info
135 */
136 public String toString() {
137 return "netconf:" + name + "@" + ipAddress + ":" + port;
138 }
139
140 /**
141 * Return the DeviceId about the device containing the URI.
142 *
143 * @return DeviceId
144 */
145 public DeviceId getDeviceId() {
Andrea Campanella57efbb22016-02-11 14:21:41 -0800146 if (deviceId == null) {
147 try {
148 deviceId = DeviceId.deviceId(new URI("netconf", ipAddress.toString() + ":" + port, null));
149 } catch (URISyntaxException e) {
150 throw new IllegalArgumentException("Unable to build deviceID for device " + toString(), e);
151 }
andreaeb70a942015-10-16 21:34:46 -0700152 }
Andrea Campanella57efbb22016-02-11 14:21:41 -0800153 return deviceId;
andreaeb70a942015-10-16 21:34:46 -0700154 }
155
156 @Override
157 public int hashCode() {
158 return Objects.hash(ipAddress, port, name);
159 }
160
161 @Override
162 public boolean equals(Object toBeCompared) {
163 if (toBeCompared instanceof NetconfDeviceInfo) {
164 NetconfDeviceInfo netconfDeviceInfo = (NetconfDeviceInfo) toBeCompared;
165 if (netconfDeviceInfo.name().equals(name)
166 && netconfDeviceInfo.ip().equals(ipAddress)
167 && netconfDeviceInfo.port() == port
168 && netconfDeviceInfo.password().equals(password)) {
169 return true;
170 }
171 }
172 return false;
173 }
174}