blob: aa1b9590c3866f2277b3d342d1d50ea9957f116b [file] [log] [blame]
Andrea Campanella945ded22016-01-07 13:17:43 -08001/*
2 * Copyright 2016 Open Networking Laboratory
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
17package org.onosproject.protocol.rest;
18
Andrea Campanella2947e622016-01-27 09:23:46 -080019import com.google.common.base.MoreObjects;
Andrea Campanella945ded22016-01-07 13:17:43 -080020import 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
25import java.util.Objects;
26
27/**
28 * Default implementation for Rest devices.
29 */
30public class DefaultRestSBDevice implements RestSBDevice {
31 private static final String REST = "rest";
32 private static final String COLON = ":";
33 private final IpAddress ip;
34 private final int port;
35 private final String name;
36 private final String password;
37 private boolean isActive;
38 private String protocol;
Andrea Campanella2947e622016-01-27 09:23:46 -080039 private String url;
Andrea Campanella945ded22016-01-07 13:17:43 -080040
41 public DefaultRestSBDevice(IpAddress ip, int port, String name, String password,
Andrea Campanella2947e622016-01-27 09:23:46 -080042 String protocol, String url, boolean isActive) {
Andrea Campanella945ded22016-01-07 13:17:43 -080043 Preconditions.checkNotNull(ip, "IP address cannot be null");
44 Preconditions.checkArgument(port > 0, "Port address cannot be negative");
45 Preconditions.checkNotNull(protocol, "protocol address cannot be null");
46 this.ip = ip;
47 this.port = port;
48 this.name = name;
Andrea Campanella2947e622016-01-27 09:23:46 -080049 this.password = StringUtils.isEmpty(password) ? null : password;
Andrea Campanella945ded22016-01-07 13:17:43 -080050 this.isActive = isActive;
51 this.protocol = protocol;
Andrea Campanella2947e622016-01-27 09:23:46 -080052 this.url = StringUtils.isEmpty(url) ? null : url;
Andrea Campanella945ded22016-01-07 13:17:43 -080053 }
54
55 @Override
56 public IpAddress ip() {
57 return ip;
58 }
59
60 @Override
61 public int port() {
62 return port;
63 }
64
65 @Override
66 public String name() {
67 return name;
68 }
69
70 @Override
71 public String password() {
72 return password;
73 }
74
75 @Override
76 public DeviceId deviceId() {
77 return DeviceId.deviceId(REST + COLON + ip + COLON + port);
78 }
79
80 @Override
81 public void setActive(boolean active) {
82 isActive = active;
83 }
84
85 @Override
86 public boolean isActive() {
87 return isActive;
88 }
89
90 @Override
91 public String protocol() {
92 return protocol;
93 }
94
95 @Override
Andrea Campanella2947e622016-01-27 09:23:46 -080096 public String url() {
97 return url;
98 }
99
100 @Override
101 public String toString() {
102 return MoreObjects.toStringHelper(this)
103 .add("url", url)
104 .add("protocol", protocol)
105 .add("name", name)
106 .add("port", port)
107 .add("ip", ip)
108 .toString();
109 }
110
111 @Override
Andrea Campanella945ded22016-01-07 13:17:43 -0800112 public boolean equals(Object obj) {
113 if (obj == this) {
114 return true;
115 }
116 if (!(obj instanceof RestSBDevice)) {
117 return false;
118 }
119 RestSBDevice device = (RestSBDevice) obj;
120 return this.name.equals(device.name()) && this.ip.equals(device.ip()) &&
121 this.port == device.port();
122
123 }
124
125 @Override
126 public int hashCode() {
127 return Objects.hash(ip, port);
128 }
129
130}