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 | |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 25 | import java.net.URI; |
| 26 | import java.net.URISyntaxException; |
| 27 | import java.util.Objects; |
| 28 | import java.util.Optional; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Default implementation for Rest devices. |
| 32 | */ |
| 33 | public 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 Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 38 | private final String username; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 39 | private final String password; |
| 40 | private boolean isActive; |
| 41 | private String protocol; |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 42 | private String url; |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 43 | private boolean isProxy; |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame^] | 44 | private AuthenticationScheme authenticationScheme; |
| 45 | private String token; |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 46 | private final Optional<String> testUrl; |
| 47 | private final Optional<String> manufacturer; |
| 48 | private final Optional<String> hwVersion; |
| 49 | private final Optional<String> swVersion; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 50 | |
| 51 | public DefaultRestSBDevice(IpAddress ip, int port, String name, String password, |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 52 | String protocol, String url, boolean isActive) { |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame^] | 53 | this(ip, port, name, password, protocol, url, isActive, "", "", "", "", AuthenticationScheme.BASIC, ""); |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | public DefaultRestSBDevice(IpAddress ip, int port, String name, String password, |
| 57 | String protocol, String url, boolean isActive, String testUrl, String manufacturer, |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame^] | 58 | String hwVersion, String swVersion, AuthenticationScheme authenticationScheme, |
| 59 | String token) { |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 60 | 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 Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 65 | this.username = name; |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 66 | this.password = StringUtils.isEmpty(password) ? null : password; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 67 | this.isActive = isActive; |
| 68 | this.protocol = protocol; |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 69 | this.url = StringUtils.isEmpty(url) ? null : url; |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame^] | 70 | this.authenticationScheme = authenticationScheme; |
| 71 | this.token = token; |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 72 | 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 Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 87 | } |
| 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 Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 100 | public String username() { |
| 101 | return username; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public String password() { |
| 106 | return password; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public DeviceId deviceId() { |
Andrea Campanella | 57efbb2 | 2016-02-11 14:21:41 -0800 | [diff] [blame] | 111 | 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 Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 118 | } |
| 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 Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 136 | public String url() { |
| 137 | return url; |
| 138 | } |
| 139 | |
| 140 | @Override |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 141 | 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 |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame^] | 166 | public AuthenticationScheme authentication() { |
| 167 | return authenticationScheme; |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public String token() { |
| 172 | return token; |
| 173 | } |
| 174 | |
| 175 | @Override |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 176 | public String toString() { |
| 177 | return MoreObjects.toStringHelper(this) |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 178 | .omitNullValues() |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 179 | .add("url", url) |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 180 | .add("testUrl", testUrl) |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 181 | .add("protocol", protocol) |
Andrea Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 182 | .add("username", username) |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 183 | .add("port", port) |
| 184 | .add("ip", ip) |
fahadnaeemkhan | 02ffa71 | 2017-12-01 19:49:45 -0800 | [diff] [blame^] | 185 | .add("authentication", authenticationScheme.name()) |
| 186 | .add("token", token) |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 187 | .add("manufacturer", manufacturer.orElse(null)) |
| 188 | .add("hwVersion", hwVersion.orElse(null)) |
| 189 | .add("swVersion", swVersion.orElse(null)) |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 190 | .toString(); |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 191 | |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | @Override |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 195 | 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 Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 203 | return this.username.equals(device.username()) && this.ip.equals(device.ip()) && |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 204 | this.port == device.port(); |
| 205 | |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public int hashCode() { |
| 210 | return Objects.hash(ip, port); |
| 211 | } |
| 212 | |
| 213 | } |