blob: 46fa997d8d76d46f0d019b00163021cb8a2b45d9 [file] [log] [blame]
andreaeb70a942015-10-16 21:34:46 -07001/*
2 * Copyright 2015 Open Networking Laboratory
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
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
25import java.io.File;
26import 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;
42 private File keyFile;
Andrea Campanella57efbb22016-02-11 14:21:41 -080043 private DeviceId deviceId;
andreaeb70a942015-10-16 21:34:46 -070044
45
46 /**
47 * Information for contacting the controller.
48 *
49 * @param name the connection type
50 * @param password the password for the device
51 * @param ipAddress the ip address
52 * @param port the tcp port
53 */
54 public NetconfDeviceInfo(String name, String password, IpAddress ipAddress,
55 int port) {
56 Preconditions.checkArgument(!name.equals(""), "Empty device name");
57 Preconditions.checkNotNull(port > 0, "Negative port");
58 Preconditions.checkNotNull(ipAddress, "Null ip address");
59 this.name = name;
60 this.password = password;
61 this.ipAddress = ipAddress;
62 this.port = port;
63 }
64
65 /**
66 * Information for contacting the controller.
67 *
68 * @param name the connection type
69 * @param password the password for the device
70 * @param ipAddress the ip address
71 * @param port the tcp port
Andrea Campanella1cd641b2015-12-07 17:28:34 -080072 * @param keyString the string containing the key.
andreaeb70a942015-10-16 21:34:46 -070073 */
74 public NetconfDeviceInfo(String name, String password, IpAddress ipAddress,
75 int port, String keyString) {
76 Preconditions.checkArgument(!name.equals(""), "Empty device name");
77 Preconditions.checkNotNull(port > 0, "Negative port");
78 Preconditions.checkNotNull(ipAddress, "Null ip address");
79 this.name = name;
80 this.password = password;
81 this.ipAddress = ipAddress;
82 this.port = port;
83 this.keyFile = new File(keyString);
84 }
85
86 /**
87 * Exposes the name of the controller.
88 *
89 * @return String name
90 */
91 public String name() {
92 return name;
93 }
94
95 /**
96 * Exposes the password of the controller.
97 *
98 * @return String password
99 */
100 public String password() {
101 return password;
102 }
103
104 /**
105 * Exposes the ip address of the controller.
106 *
107 * @return IpAddress ip address
108 */
109 public IpAddress ip() {
110 return ipAddress;
111 }
112
113 /**
114 * Exposes the port of the controller.
115 *
116 * @return int port address
117 */
118 public int port() {
119 return port;
120 }
121
122 /**
123 * Exposes the keyFile of the controller.
124 *
125 * @return int port address
126 */
127 public File getKeyFile() {
128 return keyFile;
129 }
130
131 /**
132 * Return the info about the device in a string.
133 * String format: "netconf:name@ip:port"
134 *
135 * @return String device info
136 */
137 public String toString() {
138 return "netconf:" + name + "@" + ipAddress + ":" + port;
139 }
140
141 /**
142 * Return the DeviceId about the device containing the URI.
143 *
144 * @return DeviceId
145 */
146 public DeviceId getDeviceId() {
Andrea Campanella57efbb22016-02-11 14:21:41 -0800147 if (deviceId == null) {
148 try {
149 deviceId = DeviceId.deviceId(new URI("netconf", ipAddress.toString() + ":" + port, null));
150 } catch (URISyntaxException e) {
151 throw new IllegalArgumentException("Unable to build deviceID for device " + toString(), e);
152 }
andreaeb70a942015-10-16 21:34:46 -0700153 }
Andrea Campanella57efbb22016-02-11 14:21:41 -0800154 return deviceId;
andreaeb70a942015-10-16 21:34:46 -0700155 }
156
157 @Override
158 public int hashCode() {
159 return Objects.hash(ipAddress, port, name);
160 }
161
162 @Override
163 public boolean equals(Object toBeCompared) {
164 if (toBeCompared instanceof NetconfDeviceInfo) {
165 NetconfDeviceInfo netconfDeviceInfo = (NetconfDeviceInfo) toBeCompared;
166 if (netconfDeviceInfo.name().equals(name)
167 && netconfDeviceInfo.ip().equals(ipAddress)
168 && netconfDeviceInfo.port() == port
169 && netconfDeviceInfo.password().equals(password)) {
170 return true;
171 }
172 }
173 return false;
174 }
175}