blob: 40816a46be1f359ddf86020334bf40956a3adbfb [file] [log] [blame]
Andrea Campanella945ded22016-01-07 13:17:43 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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
Hesam Rahimi4a409b42016-08-12 18:37:33 -040019import java.net.URI;
20import java.net.URISyntaxException;
21import java.util.Objects;
22
Andrea Campanella2947e622016-01-27 09:23:46 -080023import org.apache.commons.lang3.StringUtils;
Andrea Campanella945ded22016-01-07 13:17:43 -080024import org.onlab.packet.IpAddress;
25import org.onosproject.net.DeviceId;
26
Hesam Rahimi4a409b42016-08-12 18:37:33 -040027import com.google.common.base.MoreObjects;
28import com.google.common.base.Preconditions;
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;
Andrea Campanella945ded22016-01-07 13:17:43 -080043
44 public DefaultRestSBDevice(IpAddress ip, int port, String name, String password,
Andrea Campanella2947e622016-01-27 09:23:46 -080045 String protocol, String url, boolean isActive) {
Andrea Campanella945ded22016-01-07 13:17:43 -080046 Preconditions.checkNotNull(ip, "IP address cannot be null");
47 Preconditions.checkArgument(port > 0, "Port address cannot be negative");
48 Preconditions.checkNotNull(protocol, "protocol address cannot be null");
49 this.ip = ip;
50 this.port = port;
Andrea Campanella784ee0f2016-02-17 15:50:59 -080051 this.username = name;
Andrea Campanella2947e622016-01-27 09:23:46 -080052 this.password = StringUtils.isEmpty(password) ? null : password;
Andrea Campanella945ded22016-01-07 13:17:43 -080053 this.isActive = isActive;
54 this.protocol = protocol;
Andrea Campanella2947e622016-01-27 09:23:46 -080055 this.url = StringUtils.isEmpty(url) ? null : url;
Andrea Campanella945ded22016-01-07 13:17:43 -080056 }
57
58 @Override
59 public IpAddress ip() {
60 return ip;
61 }
62
63 @Override
64 public int port() {
65 return port;
66 }
67
68 @Override
Andrea Campanella784ee0f2016-02-17 15:50:59 -080069 public String username() {
70 return username;
Andrea Campanella945ded22016-01-07 13:17:43 -080071 }
72
73 @Override
74 public String password() {
75 return password;
76 }
77
78 @Override
79 public DeviceId deviceId() {
Andrea Campanella57efbb22016-02-11 14:21:41 -080080 try {
81 return DeviceId.deviceId(new URI(REST, ip + COLON + port, null));
82 } catch (URISyntaxException e) {
83 throw new IllegalArgumentException("Cannot create deviceID " +
84 REST + COLON + ip +
85 COLON + port, e);
86 }
Andrea Campanella945ded22016-01-07 13:17:43 -080087 }
88
89 @Override
90 public void setActive(boolean active) {
91 isActive = active;
92 }
93
94 @Override
95 public boolean isActive() {
96 return isActive;
97 }
98
99 @Override
100 public String protocol() {
101 return protocol;
102 }
103
104 @Override
Andrea Campanella2947e622016-01-27 09:23:46 -0800105 public String url() {
106 return url;
107 }
108
109 @Override
110 public String toString() {
111 return MoreObjects.toStringHelper(this)
112 .add("url", url)
113 .add("protocol", protocol)
Andrea Campanella784ee0f2016-02-17 15:50:59 -0800114 .add("username", username)
Andrea Campanella2947e622016-01-27 09:23:46 -0800115 .add("port", port)
116 .add("ip", ip)
117 .toString();
118 }
119
120 @Override
Andrea Campanella945ded22016-01-07 13:17:43 -0800121 public boolean equals(Object obj) {
122 if (obj == this) {
123 return true;
124 }
125 if (!(obj instanceof RestSBDevice)) {
126 return false;
127 }
128 RestSBDevice device = (RestSBDevice) obj;
Andrea Campanella784ee0f2016-02-17 15:50:59 -0800129 return this.username.equals(device.username()) && this.ip.equals(device.ip()) &&
Andrea Campanella945ded22016-01-07 13:17:43 -0800130 this.port == device.port();
131
132 }
133
134 @Override
135 public int hashCode() {
136 return Objects.hash(ip, port);
137 }
138
139}