blob: 8f59093f7489e2bdb0aa85cc6c9fc4ff3a9b6e59 [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;
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 Campanella945ded22016-01-07 13:17:43 -080048
49 public DefaultRestSBDevice(IpAddress ip, int port, String name, String password,
Andrea Campanella2947e622016-01-27 09:23:46 -080050 String protocol, String url, boolean isActive) {
Michele Santuaric372c222017-01-12 09:41:25 +010051 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 Campanella945ded22016-01-07 13:17:43 -080058 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 Campanella784ee0f2016-02-17 15:50:59 -080063 this.username = name;
Andrea Campanella2947e622016-01-27 09:23:46 -080064 this.password = StringUtils.isEmpty(password) ? null : password;
Andrea Campanella945ded22016-01-07 13:17:43 -080065 this.isActive = isActive;
66 this.protocol = protocol;
Andrea Campanella2947e622016-01-27 09:23:46 -080067 this.url = StringUtils.isEmpty(url) ? null : url;
Michele Santuaric372c222017-01-12 09:41:25 +010068 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 Campanella945ded22016-01-07 13:17:43 -080083 }
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 Campanella784ee0f2016-02-17 15:50:59 -080096 public String username() {
97 return username;
Andrea Campanella945ded22016-01-07 13:17:43 -080098 }
99
100 @Override
101 public String password() {
102 return password;
103 }
104
105 @Override
106 public DeviceId deviceId() {
Andrea Campanella57efbb22016-02-11 14:21:41 -0800107 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 Campanella945ded22016-01-07 13:17:43 -0800114 }
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 Campanella2947e622016-01-27 09:23:46 -0800132 public String url() {
133 return url;
134 }
135
136 @Override
Michele Santuaric372c222017-01-12 09:41:25 +0100137 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 Campanella2947e622016-01-27 09:23:46 -0800162 public String toString() {
163 return MoreObjects.toStringHelper(this)
Michele Santuaric372c222017-01-12 09:41:25 +0100164 .omitNullValues()
Andrea Campanella2947e622016-01-27 09:23:46 -0800165 .add("url", url)
Michele Santuaric372c222017-01-12 09:41:25 +0100166 .add("testUrl", testUrl)
Andrea Campanella2947e622016-01-27 09:23:46 -0800167 .add("protocol", protocol)
Andrea Campanella784ee0f2016-02-17 15:50:59 -0800168 .add("username", username)
Andrea Campanella2947e622016-01-27 09:23:46 -0800169 .add("port", port)
170 .add("ip", ip)
Michele Santuaric372c222017-01-12 09:41:25 +0100171 .add("manufacturer", manufacturer.orElse(null))
172 .add("hwVersion", hwVersion.orElse(null))
173 .add("swVersion", swVersion.orElse(null))
Andrea Campanella2947e622016-01-27 09:23:46 -0800174 .toString();
Michele Santuaric372c222017-01-12 09:41:25 +0100175
Andrea Campanella2947e622016-01-27 09:23:46 -0800176 }
177
178 @Override
Andrea Campanella945ded22016-01-07 13:17:43 -0800179 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 Campanella784ee0f2016-02-17 15:50:59 -0800187 return this.username.equals(device.username()) && this.ip.equals(device.ip()) &&
Andrea Campanella945ded22016-01-07 13:17:43 -0800188 this.port == device.port();
189
190 }
191
192 @Override
193 public int hashCode() {
194 return Objects.hash(ip, port);
195 }
196
197}