Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.protocol.rest; |
| 18 | |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 19 | import com.google.common.base.MoreObjects; |
| 20 | import com.google.common.base.Preconditions; |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 21 | import org.apache.commons.lang3.StringUtils; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | |
Yuta HIGUCHI | fc66705 | 2017-12-05 18:17:22 -0800 | [diff] [blame] | 25 | import static com.google.common.base.Strings.nullToEmpty; |
| 26 | |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 27 | import java.net.URI; |
| 28 | import java.net.URISyntaxException; |
| 29 | import java.util.Objects; |
| 30 | import java.util.Optional; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Default implementation for Rest devices. |
| 34 | */ |
| 35 | public 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 Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 40 | private final String username; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 41 | private final String password; |
| 42 | private boolean isActive; |
| 43 | private String protocol; |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 44 | private String url; |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 45 | private boolean isProxy; |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame] | 46 | private AuthenticationScheme authenticationScheme; |
| 47 | private String token; |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 48 | private final Optional<String> testUrl; |
| 49 | private final Optional<String> manufacturer; |
| 50 | private final Optional<String> hwVersion; |
| 51 | private final Optional<String> swVersion; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 52 | |
| 53 | public DefaultRestSBDevice(IpAddress ip, int port, String name, String password, |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 54 | String protocol, String url, boolean isActive) { |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame] | 55 | this(ip, port, name, password, protocol, url, isActive, "", "", "", "", AuthenticationScheme.BASIC, ""); |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | public DefaultRestSBDevice(IpAddress ip, int port, String name, String password, |
| 59 | String protocol, String url, boolean isActive, String testUrl, String manufacturer, |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame] | 60 | String hwVersion, String swVersion, AuthenticationScheme authenticationScheme, |
| 61 | String token) { |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 62 | 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 Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 67 | this.username = name; |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 68 | this.password = StringUtils.isEmpty(password) ? null : password; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 69 | this.isActive = isActive; |
| 70 | this.protocol = protocol; |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 71 | this.url = StringUtils.isEmpty(url) ? null : url; |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame] | 72 | this.authenticationScheme = authenticationScheme; |
| 73 | this.token = token; |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 74 | 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 Katsikas | 15841e2 | 2018-07-28 14:27:28 +0200 | [diff] [blame^] | 82 | 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 Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 110 | } |
| 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 Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 123 | public String username() { |
| 124 | return username; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public String password() { |
| 129 | return password; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public DeviceId deviceId() { |
Andrea Campanella | 57efbb2 | 2016-02-11 14:21:41 -0800 | [diff] [blame] | 134 | 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 Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 141 | } |
| 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 Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 159 | public String url() { |
| 160 | return url; |
| 161 | } |
| 162 | |
| 163 | @Override |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 164 | 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 |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame] | 189 | public AuthenticationScheme authentication() { |
| 190 | return authenticationScheme; |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public String token() { |
| 195 | return token; |
| 196 | } |
| 197 | |
| 198 | @Override |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 199 | public String toString() { |
| 200 | return MoreObjects.toStringHelper(this) |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 201 | .omitNullValues() |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 202 | .add("url", url) |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 203 | .add("testUrl", testUrl) |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 204 | .add("protocol", protocol) |
Andrea Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 205 | .add("username", username) |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 206 | .add("port", port) |
| 207 | .add("ip", ip) |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame] | 208 | .add("authentication", authenticationScheme.name()) |
| 209 | .add("token", token) |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 210 | .add("manufacturer", manufacturer.orElse(null)) |
| 211 | .add("hwVersion", hwVersion.orElse(null)) |
| 212 | .add("swVersion", swVersion.orElse(null)) |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 213 | .toString(); |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 214 | |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Yuta HIGUCHI | fc66705 | 2017-12-05 18:17:22 -0800 | [diff] [blame] | 217 | // FIXME revisit equality condition. Why urls are not included? |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 218 | @Override |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 219 | public boolean equals(Object obj) { |
| 220 | if (obj == this) { |
| 221 | return true; |
| 222 | } |
| 223 | if (!(obj instanceof RestSBDevice)) { |
| 224 | return false; |
| 225 | } |
Yuta HIGUCHI | fc66705 | 2017-12-05 18:17:22 -0800 | [diff] [blame] | 226 | 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 Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 230 | |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public int hashCode() { |
Yuta HIGUCHI | fc66705 | 2017-12-05 18:17:22 -0800 | [diff] [blame] | 235 | return Objects.hash(ip, port, nullToEmpty(username)); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | } |