blob: fef7f3708e8a874c48b3a4ef5ba769dc35f8296a [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
Michele Santuaric372c222017-01-12 09:41:25 +010025import java.net.URI;
26import java.net.URISyntaxException;
27import java.util.Objects;
28import java.util.Optional;
Andrea Campanella945ded22016-01-07 13:17:43 -080029
30/**
31 * Default implementation for Rest devices.
32 */
33public class DefaultRestSBDevice implements RestSBDevice {
34 private static final String REST = "rest";
35 private static final String COLON = ":";
36 private final IpAddress ip;
37 private final int port;
Andrea Campanella784ee0f2016-02-17 15:50:59 -080038 private final String username;
Andrea Campanella945ded22016-01-07 13:17:43 -080039 private final String password;
40 private boolean isActive;
41 private String protocol;
Andrea Campanella2947e622016-01-27 09:23:46 -080042 private String url;
Michele Santuaric372c222017-01-12 09:41:25 +010043 private boolean isProxy;
fahadnaeemkhan02ffa712017-12-01 19:49:45 -080044 private AuthenticationScheme authenticationScheme;
45 private String token;
Michele Santuaric372c222017-01-12 09:41:25 +010046 private final Optional<String> testUrl;
47 private final Optional<String> manufacturer;
48 private final Optional<String> hwVersion;
49 private final Optional<String> swVersion;
Andrea Campanella945ded22016-01-07 13:17:43 -080050
51 public DefaultRestSBDevice(IpAddress ip, int port, String name, String password,
Andrea Campanella2947e622016-01-27 09:23:46 -080052 String protocol, String url, boolean isActive) {
fahadnaeemkhan02ffa712017-12-01 19:49:45 -080053 this(ip, port, name, password, protocol, url, isActive, "", "", "", "", AuthenticationScheme.BASIC, "");
Michele Santuaric372c222017-01-12 09:41:25 +010054 }
55
56 public DefaultRestSBDevice(IpAddress ip, int port, String name, String password,
57 String protocol, String url, boolean isActive, String testUrl, String manufacturer,
fahadnaeemkhan02ffa712017-12-01 19:49:45 -080058 String hwVersion, String swVersion, AuthenticationScheme authenticationScheme,
59 String token) {
Andrea Campanella945ded22016-01-07 13:17:43 -080060 Preconditions.checkNotNull(ip, "IP address cannot be null");
61 Preconditions.checkArgument(port > 0, "Port address cannot be negative");
62 Preconditions.checkNotNull(protocol, "protocol address cannot be null");
63 this.ip = ip;
64 this.port = port;
Andrea Campanella784ee0f2016-02-17 15:50:59 -080065 this.username = name;
Andrea Campanella2947e622016-01-27 09:23:46 -080066 this.password = StringUtils.isEmpty(password) ? null : password;
Andrea Campanella945ded22016-01-07 13:17:43 -080067 this.isActive = isActive;
68 this.protocol = protocol;
Andrea Campanella2947e622016-01-27 09:23:46 -080069 this.url = StringUtils.isEmpty(url) ? null : url;
fahadnaeemkhan02ffa712017-12-01 19:49:45 -080070 this.authenticationScheme = authenticationScheme;
71 this.token = token;
Michele Santuaric372c222017-01-12 09:41:25 +010072 this.manufacturer = StringUtils.isEmpty(manufacturer) ?
73 Optional.empty() : Optional.ofNullable(manufacturer);
74 this.hwVersion = StringUtils.isEmpty(hwVersion) ?
75 Optional.empty() : Optional.ofNullable(hwVersion);
76 this.swVersion = StringUtils.isEmpty(swVersion) ?
77 Optional.empty() : Optional.ofNullable(swVersion);
78 this.testUrl = StringUtils.isEmpty(testUrl) ?
79 Optional.empty() : Optional.ofNullable(testUrl);
80 if (this.manufacturer.isPresent()
81 && this.hwVersion.isPresent()
82 && this.swVersion.isPresent()) {
83 this.isProxy = true;
84 } else {
85 this.isProxy = false;
86 }
Andrea Campanella945ded22016-01-07 13:17:43 -080087 }
88
89 @Override
90 public IpAddress ip() {
91 return ip;
92 }
93
94 @Override
95 public int port() {
96 return port;
97 }
98
99 @Override
Andrea Campanella784ee0f2016-02-17 15:50:59 -0800100 public String username() {
101 return username;
Andrea Campanella945ded22016-01-07 13:17:43 -0800102 }
103
104 @Override
105 public String password() {
106 return password;
107 }
108
109 @Override
110 public DeviceId deviceId() {
Andrea Campanella57efbb22016-02-11 14:21:41 -0800111 try {
112 return DeviceId.deviceId(new URI(REST, ip + COLON + port, null));
113 } catch (URISyntaxException e) {
114 throw new IllegalArgumentException("Cannot create deviceID " +
115 REST + COLON + ip +
116 COLON + port, e);
117 }
Andrea Campanella945ded22016-01-07 13:17:43 -0800118 }
119
120 @Override
121 public void setActive(boolean active) {
122 isActive = active;
123 }
124
125 @Override
126 public boolean isActive() {
127 return isActive;
128 }
129
130 @Override
131 public String protocol() {
132 return protocol;
133 }
134
135 @Override
Andrea Campanella2947e622016-01-27 09:23:46 -0800136 public String url() {
137 return url;
138 }
139
140 @Override
Michele Santuaric372c222017-01-12 09:41:25 +0100141 public boolean isProxy() {
142 return isProxy;
143 }
144
145 @Override
146 public Optional<String> testUrl() {
147 return testUrl;
148 }
149
150 @Override
151 public Optional<String> manufacturer() {
152 return manufacturer;
153 }
154
155 @Override
156 public Optional<String> hwVersion() {
157 return hwVersion;
158 }
159
160 @Override
161 public Optional<String> swVersion() {
162 return swVersion;
163 }
164
165 @Override
fahadnaeemkhan02ffa712017-12-01 19:49:45 -0800166 public AuthenticationScheme authentication() {
167 return authenticationScheme;
168 }
169
170 @Override
171 public String token() {
172 return token;
173 }
174
175 @Override
Andrea Campanella2947e622016-01-27 09:23:46 -0800176 public String toString() {
177 return MoreObjects.toStringHelper(this)
Michele Santuaric372c222017-01-12 09:41:25 +0100178 .omitNullValues()
Andrea Campanella2947e622016-01-27 09:23:46 -0800179 .add("url", url)
Michele Santuaric372c222017-01-12 09:41:25 +0100180 .add("testUrl", testUrl)
Andrea Campanella2947e622016-01-27 09:23:46 -0800181 .add("protocol", protocol)
Andrea Campanella784ee0f2016-02-17 15:50:59 -0800182 .add("username", username)
Andrea Campanella2947e622016-01-27 09:23:46 -0800183 .add("port", port)
184 .add("ip", ip)
fahadnaeemkhan02ffa712017-12-01 19:49:45 -0800185 .add("authentication", authenticationScheme.name())
186 .add("token", token)
Michele Santuaric372c222017-01-12 09:41:25 +0100187 .add("manufacturer", manufacturer.orElse(null))
188 .add("hwVersion", hwVersion.orElse(null))
189 .add("swVersion", swVersion.orElse(null))
Andrea Campanella2947e622016-01-27 09:23:46 -0800190 .toString();
Michele Santuaric372c222017-01-12 09:41:25 +0100191
Andrea Campanella2947e622016-01-27 09:23:46 -0800192 }
193
194 @Override
Andrea Campanella945ded22016-01-07 13:17:43 -0800195 public boolean equals(Object obj) {
196 if (obj == this) {
197 return true;
198 }
199 if (!(obj instanceof RestSBDevice)) {
200 return false;
201 }
202 RestSBDevice device = (RestSBDevice) obj;
Andrea Campanella784ee0f2016-02-17 15:50:59 -0800203 return this.username.equals(device.username()) && this.ip.equals(device.ip()) &&
Andrea Campanella945ded22016-01-07 13:17:43 -0800204 this.port == device.port();
205
206 }
207
208 @Override
209 public int hashCode() {
210 return Objects.hash(ip, port);
211 }
212
213}