Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 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 | |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 19 | import java.net.URI; |
| 20 | import java.net.URISyntaxException; |
| 21 | import java.util.Objects; |
| 22 | |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 23 | import org.apache.commons.lang3.StringUtils; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 27 | import com.google.common.base.MoreObjects; |
| 28 | import com.google.common.base.Preconditions; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Default implementation for Rest devices. |
| 32 | */ |
| 33 | public 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 Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 38 | private final String username; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 39 | private final String password; |
| 40 | private boolean isActive; |
| 41 | private String protocol; |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 42 | private String url; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 43 | |
| 44 | public DefaultRestSBDevice(IpAddress ip, int port, String name, String password, |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 45 | String protocol, String url, boolean isActive) { |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 46 | 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 Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 51 | this.username = name; |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 52 | this.password = StringUtils.isEmpty(password) ? null : password; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 53 | this.isActive = isActive; |
| 54 | this.protocol = protocol; |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 55 | this.url = StringUtils.isEmpty(url) ? null : url; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 56 | } |
| 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 Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 69 | public String username() { |
| 70 | return username; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public String password() { |
| 75 | return password; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public DeviceId deviceId() { |
Andrea Campanella | 57efbb2 | 2016-02-11 14:21:41 -0800 | [diff] [blame] | 80 | 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 Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 87 | } |
| 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 Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 105 | 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 Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 114 | .add("username", username) |
Andrea Campanella | 2947e62 | 2016-01-27 09:23:46 -0800 | [diff] [blame] | 115 | .add("port", port) |
| 116 | .add("ip", ip) |
| 117 | .toString(); |
| 118 | } |
| 119 | |
| 120 | @Override |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 121 | 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 Campanella | 784ee0f | 2016-02-17 15:50:59 -0800 | [diff] [blame] | 129 | return this.username.equals(device.username()) && this.ip.equals(device.ip()) && |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 130 | this.port == device.port(); |
| 131 | |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public int hashCode() { |
| 136 | return Objects.hash(ip, port); |
| 137 | } |
| 138 | |
| 139 | } |