blob: 18ed5374225a2af389bd48cf618ae71497b9bb96 [file] [log] [blame]
Andrea Campanella945ded22016-01-07 13:17:43 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Andrea Campanella945ded22016-01-07 13:17:43 -08003 *
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.protocol.rest;
18
Michele Santuaric372c222017-01-12 09:41:25 +010019import com.google.common.base.MoreObjects;
20import com.google.common.base.Preconditions;
Andrea Campanella2947e622016-01-27 09:23:46 -080021import org.apache.commons.lang3.StringUtils;
Andrea Campanella945ded22016-01-07 13:17:43 -080022import org.onlab.packet.IpAddress;
23import org.onosproject.net.DeviceId;
24
Yuta HIGUCHIfc667052017-12-05 18:17:22 -080025import static com.google.common.base.Strings.nullToEmpty;
26
Michele Santuaric372c222017-01-12 09:41:25 +010027import java.net.URI;
28import java.net.URISyntaxException;
29import java.util.Objects;
30import java.util.Optional;
Andrea Campanella945ded22016-01-07 13:17:43 -080031
32/**
33 * Default implementation for Rest devices.
34 */
35public class DefaultRestSBDevice implements RestSBDevice {
36 private static final String REST = "rest";
37 private static final String COLON = ":";
38 private final IpAddress ip;
39 private final int port;
Andrea Campanella784ee0f2016-02-17 15:50:59 -080040 private final String username;
Andrea Campanella945ded22016-01-07 13:17:43 -080041 private final String password;
42 private boolean isActive;
43 private String protocol;
Andrea Campanella2947e622016-01-27 09:23:46 -080044 private String url;
Michele Santuaric372c222017-01-12 09:41:25 +010045 private boolean isProxy;
fahadnaeemkhan02ffa712017-12-01 19:49:45 -080046 private AuthenticationScheme authenticationScheme;
47 private String token;
Michele Santuaric372c222017-01-12 09:41:25 +010048 private final Optional<String> testUrl;
49 private final Optional<String> manufacturer;
50 private final Optional<String> hwVersion;
51 private final Optional<String> swVersion;
Andrea Campanella945ded22016-01-07 13:17:43 -080052
53 public DefaultRestSBDevice(IpAddress ip, int port, String name, String password,
Andrea Campanella2947e622016-01-27 09:23:46 -080054 String protocol, String url, boolean isActive) {
fahadnaeemkhan02ffa712017-12-01 19:49:45 -080055 this(ip, port, name, password, protocol, url, isActive, "", "", "", "", AuthenticationScheme.BASIC, "");
Michele Santuaric372c222017-01-12 09:41:25 +010056 }
57
58 public DefaultRestSBDevice(IpAddress ip, int port, String name, String password,
59 String protocol, String url, boolean isActive, String testUrl, String manufacturer,
fahadnaeemkhan02ffa712017-12-01 19:49:45 -080060 String hwVersion, String swVersion, AuthenticationScheme authenticationScheme,
61 String token) {
Andrea Campanella945ded22016-01-07 13:17:43 -080062 Preconditions.checkNotNull(ip, "IP address cannot be null");
63 Preconditions.checkArgument(port > 0, "Port address cannot be negative");
64 Preconditions.checkNotNull(protocol, "protocol address cannot be null");
65 this.ip = ip;
66 this.port = port;
Andrea Campanella784ee0f2016-02-17 15:50:59 -080067 this.username = name;
Andrea Campanella2947e622016-01-27 09:23:46 -080068 this.password = StringUtils.isEmpty(password) ? null : password;
Andrea Campanella945ded22016-01-07 13:17:43 -080069 this.isActive = isActive;
70 this.protocol = protocol;
Andrea Campanella2947e622016-01-27 09:23:46 -080071 this.url = StringUtils.isEmpty(url) ? null : url;
fahadnaeemkhan02ffa712017-12-01 19:49:45 -080072 this.authenticationScheme = authenticationScheme;
73 this.token = token;
Michele Santuaric372c222017-01-12 09:41:25 +010074 this.manufacturer = StringUtils.isEmpty(manufacturer) ?
75 Optional.empty() : Optional.ofNullable(manufacturer);
76 this.hwVersion = StringUtils.isEmpty(hwVersion) ?
77 Optional.empty() : Optional.ofNullable(hwVersion);
78 this.swVersion = StringUtils.isEmpty(swVersion) ?
79 Optional.empty() : Optional.ofNullable(swVersion);
80 this.testUrl = StringUtils.isEmpty(testUrl) ?
81 Optional.empty() : Optional.ofNullable(testUrl);
Georgios Katsikas15841e22018-07-28 14:27:28 +020082 this.isProxy = false;
83 }
84
85 public DefaultRestSBDevice(IpAddress ip, int port, String name, String password,
86 String protocol, String url, boolean isActive, String testUrl, String manufacturer,
87 String hwVersion, String swVersion, boolean isProxy,
88 AuthenticationScheme authenticationScheme, String token) {
89 Preconditions.checkNotNull(ip, "IP address cannot be null");
90 Preconditions.checkArgument(port > 0, "Port address cannot be negative");
91 Preconditions.checkNotNull(protocol, "protocol address cannot be null");
92 this.ip = ip;
93 this.port = port;
94 this.username = name;
95 this.password = StringUtils.isEmpty(password) ? null : password;
96 this.isActive = isActive;
97 this.protocol = protocol;
98 this.url = StringUtils.isEmpty(url) ? null : url;
99 this.authenticationScheme = authenticationScheme;
100 this.token = token;
101 this.manufacturer = StringUtils.isEmpty(manufacturer) ?
102 Optional.empty() : Optional.ofNullable(manufacturer);
103 this.hwVersion = StringUtils.isEmpty(hwVersion) ?
104 Optional.empty() : Optional.ofNullable(hwVersion);
105 this.swVersion = StringUtils.isEmpty(swVersion) ?
106 Optional.empty() : Optional.ofNullable(swVersion);
107 this.testUrl = StringUtils.isEmpty(testUrl) ?
108 Optional.empty() : Optional.ofNullable(testUrl);
109 this.isProxy = isProxy;
Andrea Campanella945ded22016-01-07 13:17:43 -0800110 }
111
112 @Override
113 public IpAddress ip() {
114 return ip;
115 }
116
117 @Override
118 public int port() {
119 return port;
120 }
121
122 @Override
Andrea Campanella784ee0f2016-02-17 15:50:59 -0800123 public String username() {
124 return username;
Andrea Campanella945ded22016-01-07 13:17:43 -0800125 }
126
127 @Override
128 public String password() {
129 return password;
130 }
131
132 @Override
133 public DeviceId deviceId() {
Andrea Campanella57efbb22016-02-11 14:21:41 -0800134 try {
135 return DeviceId.deviceId(new URI(REST, ip + COLON + port, null));
136 } catch (URISyntaxException e) {
137 throw new IllegalArgumentException("Cannot create deviceID " +
138 REST + COLON + ip +
139 COLON + port, e);
140 }
Andrea Campanella945ded22016-01-07 13:17:43 -0800141 }
142
143 @Override
144 public void setActive(boolean active) {
145 isActive = active;
146 }
147
148 @Override
149 public boolean isActive() {
150 return isActive;
151 }
152
153 @Override
154 public String protocol() {
155 return protocol;
156 }
157
158 @Override
Andrea Campanella2947e622016-01-27 09:23:46 -0800159 public String url() {
160 return url;
161 }
162
163 @Override
Michele Santuaric372c222017-01-12 09:41:25 +0100164 public boolean isProxy() {
165 return isProxy;
166 }
167
168 @Override
169 public Optional<String> testUrl() {
170 return testUrl;
171 }
172
173 @Override
174 public Optional<String> manufacturer() {
175 return manufacturer;
176 }
177
178 @Override
179 public Optional<String> hwVersion() {
180 return hwVersion;
181 }
182
183 @Override
184 public Optional<String> swVersion() {
185 return swVersion;
186 }
187
188 @Override
fahadnaeemkhan02ffa712017-12-01 19:49:45 -0800189 public AuthenticationScheme authentication() {
190 return authenticationScheme;
191 }
192
193 @Override
194 public String token() {
195 return token;
196 }
197
198 @Override
Andrea Campanella2947e622016-01-27 09:23:46 -0800199 public String toString() {
200 return MoreObjects.toStringHelper(this)
Michele Santuaric372c222017-01-12 09:41:25 +0100201 .omitNullValues()
Andrea Campanella2947e622016-01-27 09:23:46 -0800202 .add("url", url)
Michele Santuaric372c222017-01-12 09:41:25 +0100203 .add("testUrl", testUrl)
Andrea Campanella2947e622016-01-27 09:23:46 -0800204 .add("protocol", protocol)
Andrea Campanella784ee0f2016-02-17 15:50:59 -0800205 .add("username", username)
Andrea Campanella2947e622016-01-27 09:23:46 -0800206 .add("port", port)
207 .add("ip", ip)
fahadnaeemkhan02ffa712017-12-01 19:49:45 -0800208 .add("authentication", authenticationScheme.name())
209 .add("token", token)
Michele Santuaric372c222017-01-12 09:41:25 +0100210 .add("manufacturer", manufacturer.orElse(null))
211 .add("hwVersion", hwVersion.orElse(null))
212 .add("swVersion", swVersion.orElse(null))
Andrea Campanella2947e622016-01-27 09:23:46 -0800213 .toString();
Michele Santuaric372c222017-01-12 09:41:25 +0100214
Andrea Campanella2947e622016-01-27 09:23:46 -0800215 }
216
Yuta HIGUCHIfc667052017-12-05 18:17:22 -0800217 // FIXME revisit equality condition. Why urls are not included?
Andrea Campanella2947e622016-01-27 09:23:46 -0800218 @Override
Andrea Campanella945ded22016-01-07 13:17:43 -0800219 public boolean equals(Object obj) {
220 if (obj == this) {
221 return true;
222 }
223 if (!(obj instanceof RestSBDevice)) {
224 return false;
225 }
Yuta HIGUCHIfc667052017-12-05 18:17:22 -0800226 RestSBDevice that = (RestSBDevice) obj;
227 return Objects.equals(this.ip, that.ip()) &&
228 this.port == that.port() &&
229 nullToEmpty(this.username).equals(nullToEmpty(that.username()));
Andrea Campanella945ded22016-01-07 13:17:43 -0800230
231 }
232
233 @Override
234 public int hashCode() {
Yuta HIGUCHIfc667052017-12-05 18:17:22 -0800235 return Objects.hash(ip, port, nullToEmpty(username));
Andrea Campanella945ded22016-01-07 13:17:43 -0800236 }
237
238}