Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
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; |
| 44 | private final Optional<String> testUrl; |
| 45 | private final Optional<String> manufacturer; |
| 46 | private final Optional<String> hwVersion; |
| 47 | private final Optional<String> swVersion; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 48 | |
| 49 | public DefaultRestSBDevice(IpAddress ip, int port, String name, String password, |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 50 | String protocol, String url, boolean isActive) { |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame^] | 51 | this(ip, port, name, password, protocol, url, isActive, "", "", "", ""); |
| 52 | } |
| 53 | |
| 54 | public DefaultRestSBDevice(IpAddress ip, int port, String name, String password, |
| 55 | String protocol, String url, boolean isActive, String testUrl, String manufacturer, |
| 56 | String hwVersion, |
| 57 | String swVersion) { |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 58 | Preconditions.checkNotNull(ip, "IP address cannot be null"); |
| 59 | Preconditions.checkArgument(port > 0, "Port address cannot be negative"); |
| 60 | Preconditions.checkNotNull(protocol, "protocol address cannot be null"); |
| 61 | this.ip = ip; |
| 62 | this.port = port; |
Andrea Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 63 | this.username = name; |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 64 | this.password = StringUtils.isEmpty(password) ? null : password; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 65 | this.isActive = isActive; |
| 66 | this.protocol = protocol; |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 67 | this.url = StringUtils.isEmpty(url) ? null : url; |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame^] | 68 | this.manufacturer = StringUtils.isEmpty(manufacturer) ? |
| 69 | Optional.empty() : Optional.ofNullable(manufacturer); |
| 70 | this.hwVersion = StringUtils.isEmpty(hwVersion) ? |
| 71 | Optional.empty() : Optional.ofNullable(hwVersion); |
| 72 | this.swVersion = StringUtils.isEmpty(swVersion) ? |
| 73 | Optional.empty() : Optional.ofNullable(swVersion); |
| 74 | this.testUrl = StringUtils.isEmpty(testUrl) ? |
| 75 | Optional.empty() : Optional.ofNullable(testUrl); |
| 76 | if (this.manufacturer.isPresent() |
| 77 | && this.hwVersion.isPresent() |
| 78 | && this.swVersion.isPresent()) { |
| 79 | this.isProxy = true; |
| 80 | } else { |
| 81 | this.isProxy = false; |
| 82 | } |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public IpAddress ip() { |
| 87 | return ip; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public int port() { |
| 92 | return port; |
| 93 | } |
| 94 | |
| 95 | @Override |
Andrea Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 96 | public String username() { |
| 97 | return username; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public String password() { |
| 102 | return password; |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public DeviceId deviceId() { |
Andrea Campanella | 57efbb2 | 2016-02-11 14:21:41 -0800 | [diff] [blame] | 107 | try { |
| 108 | return DeviceId.deviceId(new URI(REST, ip + COLON + port, null)); |
| 109 | } catch (URISyntaxException e) { |
| 110 | throw new IllegalArgumentException("Cannot create deviceID " + |
| 111 | REST + COLON + ip + |
| 112 | COLON + port, e); |
| 113 | } |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public void setActive(boolean active) { |
| 118 | isActive = active; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public boolean isActive() { |
| 123 | return isActive; |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public String protocol() { |
| 128 | return protocol; |
| 129 | } |
| 130 | |
| 131 | @Override |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 132 | public String url() { |
| 133 | return url; |
| 134 | } |
| 135 | |
| 136 | @Override |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame^] | 137 | public boolean isProxy() { |
| 138 | return isProxy; |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public Optional<String> testUrl() { |
| 143 | return testUrl; |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public Optional<String> manufacturer() { |
| 148 | return manufacturer; |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public Optional<String> hwVersion() { |
| 153 | return hwVersion; |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public Optional<String> swVersion() { |
| 158 | return swVersion; |
| 159 | } |
| 160 | |
| 161 | @Override |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 162 | public String toString() { |
| 163 | return MoreObjects.toStringHelper(this) |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame^] | 164 | .omitNullValues() |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 165 | .add("url", url) |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame^] | 166 | .add("testUrl", testUrl) |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 167 | .add("protocol", protocol) |
Andrea Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 168 | .add("username", username) |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 169 | .add("port", port) |
| 170 | .add("ip", ip) |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame^] | 171 | .add("manufacturer", manufacturer.orElse(null)) |
| 172 | .add("hwVersion", hwVersion.orElse(null)) |
| 173 | .add("swVersion", swVersion.orElse(null)) |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 174 | .toString(); |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame^] | 175 | |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | @Override |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 179 | public boolean equals(Object obj) { |
| 180 | if (obj == this) { |
| 181 | return true; |
| 182 | } |
| 183 | if (!(obj instanceof RestSBDevice)) { |
| 184 | return false; |
| 185 | } |
| 186 | RestSBDevice device = (RestSBDevice) obj; |
Andrea Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 187 | return this.username.equals(device.username()) && this.ip.equals(device.ip()) && |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 188 | this.port == device.port(); |
| 189 | |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public int hashCode() { |
| 194 | return Objects.hash(ip, port); |
| 195 | } |
| 196 | |
| 197 | } |