blob: 8331a124e453692c3917d663ddabc103bc74999f [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;
43
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;
82 this.keyFile = new File(keyString);
83 }
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 /**
122 * Exposes the keyFile of the controller.
123 *
124 * @return int port address
125 */
126 public File getKeyFile() {
127 return keyFile;
128 }
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() {
146
147 try {
148 return DeviceId.deviceId(new URI(this.toString()));
149 } catch (URISyntaxException e) {
150 log.debug("Unable to build deviceID for device {} ", this, e);
151 }
152 return null;
153 }
154
155 @Override
156 public int hashCode() {
157 return Objects.hash(ipAddress, port, name);
158 }
159
160 @Override
161 public boolean equals(Object toBeCompared) {
162 if (toBeCompared instanceof NetconfDeviceInfo) {
163 NetconfDeviceInfo netconfDeviceInfo = (NetconfDeviceInfo) toBeCompared;
164 if (netconfDeviceInfo.name().equals(name)
165 && netconfDeviceInfo.ip().equals(ipAddress)
166 && netconfDeviceInfo.port() == port
167 && netconfDeviceInfo.password().equals(password)) {
168 return true;
169 }
170 }
171 return false;
172 }
173}