blob: baf26cb40dc53e57b3f59aec2206c25fab191740 [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
19import com.google.common.base.Preconditions;
20import org.onlab.packet.IpAddress;
21import org.onosproject.net.DeviceId;
22
23import java.util.Objects;
24
25/**
26 * Default implementation for Rest devices.
27 */
28public class DefaultRestSBDevice implements RestSBDevice {
29 private static final String REST = "rest";
30 private static final String COLON = ":";
31 private final IpAddress ip;
32 private final int port;
33 private final String name;
34 private final String password;
35 private boolean isActive;
36 private String protocol;
37
38 public DefaultRestSBDevice(IpAddress ip, int port, String name, String password,
39 String protocol, boolean isActive) {
40 Preconditions.checkNotNull(ip, "IP address cannot be null");
41 Preconditions.checkArgument(port > 0, "Port address cannot be negative");
42 Preconditions.checkNotNull(protocol, "protocol address cannot be null");
43 this.ip = ip;
44 this.port = port;
45 this.name = name;
46 this.password = password;
47 this.isActive = isActive;
48 this.protocol = protocol;
49 }
50
51 @Override
52 public IpAddress ip() {
53 return ip;
54 }
55
56 @Override
57 public int port() {
58 return port;
59 }
60
61 @Override
62 public String name() {
63 return name;
64 }
65
66 @Override
67 public String password() {
68 return password;
69 }
70
71 @Override
72 public DeviceId deviceId() {
73 return DeviceId.deviceId(REST + COLON + ip + COLON + port);
74 }
75
76 @Override
77 public void setActive(boolean active) {
78 isActive = active;
79 }
80
81 @Override
82 public boolean isActive() {
83 return isActive;
84 }
85
86 @Override
87 public String protocol() {
88 return protocol;
89 }
90
91 @Override
92 public boolean equals(Object obj) {
93 if (obj == this) {
94 return true;
95 }
96 if (!(obj instanceof RestSBDevice)) {
97 return false;
98 }
99 RestSBDevice device = (RestSBDevice) obj;
100 return this.name.equals(device.name()) && this.ip.equals(device.ip()) &&
101 this.port == device.port();
102
103 }
104
105 @Override
106 public int hashCode() {
107 return Objects.hash(ip, port);
108 }
109
110}