blob: 57da2e5cc4c43b8895a9fb51e10fcb43329a85e9 [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
Andrea Campanella57efbb22016-02-11 14:21:41 -080025import java.net.URI;
26import java.net.URISyntaxException;
Andrea Campanella945ded22016-01-07 13:17:43 -080027import java.util.Objects;
28
29/**
30 * Default implementation for Rest devices.
31 */
32public class DefaultRestSBDevice implements RestSBDevice {
33 private static final String REST = "rest";
34 private static final String COLON = ":";
35 private final IpAddress ip;
36 private final int port;
37 private final String name;
38 private final String password;
39 private boolean isActive;
40 private String protocol;
Andrea Campanella2947e622016-01-27 09:23:46 -080041 private String url;
Andrea Campanella945ded22016-01-07 13:17:43 -080042
43 public DefaultRestSBDevice(IpAddress ip, int port, String name, String password,
Andrea Campanella2947e622016-01-27 09:23:46 -080044 String protocol, String url, boolean isActive) {
Andrea Campanella945ded22016-01-07 13:17:43 -080045 Preconditions.checkNotNull(ip, "IP address cannot be null");
46 Preconditions.checkArgument(port > 0, "Port address cannot be negative");
47 Preconditions.checkNotNull(protocol, "protocol address cannot be null");
48 this.ip = ip;
49 this.port = port;
50 this.name = name;
Andrea Campanella2947e622016-01-27 09:23:46 -080051 this.password = StringUtils.isEmpty(password) ? null : password;
Andrea Campanella945ded22016-01-07 13:17:43 -080052 this.isActive = isActive;
53 this.protocol = protocol;
Andrea Campanella2947e622016-01-27 09:23:46 -080054 this.url = StringUtils.isEmpty(url) ? null : url;
Andrea Campanella945ded22016-01-07 13:17:43 -080055 }
56
57 @Override
58 public IpAddress ip() {
59 return ip;
60 }
61
62 @Override
63 public int port() {
64 return port;
65 }
66
67 @Override
68 public String name() {
69 return name;
70 }
71
72 @Override
73 public String password() {
74 return password;
75 }
76
77 @Override
78 public DeviceId deviceId() {
Andrea Campanella57efbb22016-02-11 14:21:41 -080079 try {
80 return DeviceId.deviceId(new URI(REST, ip + COLON + port, null));
81 } catch (URISyntaxException e) {
82 throw new IllegalArgumentException("Cannot create deviceID " +
83 REST + COLON + ip +
84 COLON + port, e);
85 }
Andrea Campanella945ded22016-01-07 13:17:43 -080086 }
87
88 @Override
89 public void setActive(boolean active) {
90 isActive = active;
91 }
92
93 @Override
94 public boolean isActive() {
95 return isActive;
96 }
97
98 @Override
99 public String protocol() {
100 return protocol;
101 }
102
103 @Override
Andrea Campanella2947e622016-01-27 09:23:46 -0800104 public String url() {
105 return url;
106 }
107
108 @Override
109 public String toString() {
110 return MoreObjects.toStringHelper(this)
111 .add("url", url)
112 .add("protocol", protocol)
113 .add("name", name)
114 .add("port", port)
115 .add("ip", ip)
116 .toString();
117 }
118
119 @Override
Andrea Campanella945ded22016-01-07 13:17:43 -0800120 public boolean equals(Object obj) {
121 if (obj == this) {
122 return true;
123 }
124 if (!(obj instanceof RestSBDevice)) {
125 return false;
126 }
127 RestSBDevice device = (RestSBDevice) obj;
128 return this.name.equals(device.name()) && this.ip.equals(device.ip()) &&
129 this.port == device.port();
130
131 }
132
133 @Override
134 public int hashCode() {
135 return Objects.hash(ip, port);
136 }
137
138}