Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.protocol.rest; |
| 18 | |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame^] | 19 | import com.google.common.base.MoreObjects; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 20 | import com.google.common.base.Preconditions; |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame^] | 21 | import org.apache.commons.lang3.StringUtils; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | |
| 25 | import java.util.Objects; |
| 26 | |
| 27 | /** |
| 28 | * Default implementation for Rest devices. |
| 29 | */ |
| 30 | public 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 Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame^] | 39 | private String url; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 40 | |
| 41 | public DefaultRestSBDevice(IpAddress ip, int port, String name, String password, |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame^] | 42 | String protocol, String url, boolean isActive) { |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 43 | 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 Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame^] | 49 | this.password = StringUtils.isEmpty(password) ? null : password; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 50 | this.isActive = isActive; |
| 51 | this.protocol = protocol; |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame^] | 52 | this.url = StringUtils.isEmpty(url) ? null : url; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 53 | } |
| 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 Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame^] | 96 | 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 Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 112 | 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 | } |