Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.host; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 17 | |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 18 | import java.util.Collections; |
| 19 | import java.util.Set; |
| 20 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 21 | import org.onosproject.net.AbstractDescription; |
| 22 | import org.onosproject.net.HostLocation; |
| 23 | import org.onosproject.net.SparseAnnotations; |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 24 | import org.onlab.packet.IpAddress; |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 25 | import org.onlab.packet.MacAddress; |
| 26 | import org.onlab.packet.VlanId; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 27 | |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 28 | import com.google.common.collect.ImmutableSet; |
| 29 | |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 30 | import static com.google.common.base.MoreObjects.toStringHelper; |
HIGUCHI Yuta | 703a5af | 2015-11-18 23:43:50 -0800 | [diff] [blame] | 31 | import com.google.common.base.Objects; |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Default implementation of an immutable host description. |
| 35 | */ |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 36 | public class DefaultHostDescription extends AbstractDescription |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 37 | implements HostDescription { |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 38 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 39 | private final MacAddress mac; |
| 40 | private final VlanId vlan; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 41 | private final HostLocation location; |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 42 | private final Set<IpAddress> ip; |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 43 | private final boolean configured; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 44 | |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 45 | /** |
| 46 | * Creates a host description using the supplied information. |
| 47 | * |
| 48 | * @param mac host MAC address |
| 49 | * @param vlan host VLAN identifier |
| 50 | * @param location host location |
| 51 | * @param annotations optional key/value annotations map |
| 52 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 53 | public DefaultHostDescription(MacAddress mac, VlanId vlan, |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 54 | HostLocation location, |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 55 | SparseAnnotations... annotations) { |
Sho SHIMIZU | 21d0069 | 2016-08-15 11:15:28 -0700 | [diff] [blame] | 56 | this(mac, vlan, location, Collections.emptySet(), |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 57 | annotations); |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 58 | } |
| 59 | |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 60 | /** |
| 61 | * Creates a host description using the supplied information. |
| 62 | * |
| 63 | * @param mac host MAC address |
| 64 | * @param vlan host VLAN identifier |
| 65 | * @param location host location |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 66 | * @param ip host IP address |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 67 | * @param annotations optional key/value annotations map |
| 68 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 69 | public DefaultHostDescription(MacAddress mac, VlanId vlan, |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 70 | HostLocation location, IpAddress ip, |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 71 | SparseAnnotations... annotations) { |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 72 | this(mac, vlan, location, ImmutableSet.of(ip), annotations); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Creates a host description using the supplied information. |
| 77 | * |
| 78 | * @param mac host MAC address |
| 79 | * @param vlan host VLAN identifier |
| 80 | * @param location host location |
| 81 | * @param ip host IP addresses |
| 82 | * @param annotations optional key/value annotations map |
| 83 | */ |
| 84 | public DefaultHostDescription(MacAddress mac, VlanId vlan, |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 85 | HostLocation location, Set<IpAddress> ip, |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 86 | SparseAnnotations... annotations) { |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 87 | this(mac, vlan, location, ip, false, annotations); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Creates a host description using the supplied information. |
| 92 | * |
| 93 | * @param mac host MAC address |
| 94 | * @param vlan host VLAN identifier |
| 95 | * @param location host location |
| 96 | * @param configured true if configured via NetworkConfiguration |
| 97 | * @param annotations optional key/value annotations map |
| 98 | */ |
| 99 | public DefaultHostDescription(MacAddress mac, VlanId vlan, |
| 100 | HostLocation location, |
| 101 | boolean configured, |
| 102 | SparseAnnotations... annotations) { |
| 103 | this(mac, vlan, location, Collections.<IpAddress>emptySet(), |
| 104 | configured, annotations); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Creates a host description using the supplied information. |
| 109 | * |
| 110 | * @param mac host MAC address |
| 111 | * @param vlan host VLAN identifier |
| 112 | * @param location host location |
| 113 | * @param ip host IP address |
| 114 | * @param configured true if configured via NetworkConfiguration |
| 115 | * @param annotations optional key/value annotations map |
| 116 | */ |
| 117 | public DefaultHostDescription(MacAddress mac, VlanId vlan, |
| 118 | HostLocation location, Set<IpAddress> ip, |
| 119 | boolean configured, |
| 120 | SparseAnnotations... annotations) { |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 121 | super(annotations); |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 122 | this.mac = mac; |
| 123 | this.vlan = vlan; |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 124 | this.location = location; |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 125 | this.ip = ImmutableSet.copyOf(ip); |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 126 | this.configured = configured; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 130 | public MacAddress hwAddress() { |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 131 | return mac; |
| 132 | } |
| 133 | |
| 134 | @Override |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 135 | public VlanId vlan() { |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 136 | return vlan; |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public HostLocation location() { |
| 141 | return location; |
| 142 | } |
| 143 | |
| 144 | @Override |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 145 | public Set<IpAddress> ipAddress() { |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 146 | return ip; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Override |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 150 | public boolean configured() { |
| 151 | return configured; |
| 152 | } |
| 153 | |
| 154 | @Override |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 155 | public String toString() { |
| 156 | return toStringHelper(this) |
| 157 | .add("mac", mac) |
| 158 | .add("vlan", vlan) |
| 159 | .add("location", location) |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 160 | .add("ipAddress", ip) |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 161 | .add("configured", configured) |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 162 | .toString(); |
| 163 | } |
| 164 | |
HIGUCHI Yuta | 703a5af | 2015-11-18 23:43:50 -0800 | [diff] [blame] | 165 | @Override |
| 166 | public int hashCode() { |
| 167 | return Objects.hashCode(super.hashCode(), mac, vlan, location, ip); |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public boolean equals(Object object) { |
| 172 | if (object != null && getClass() == object.getClass()) { |
| 173 | if (!super.equals(object)) { |
| 174 | return false; |
| 175 | } |
| 176 | DefaultHostDescription that = (DefaultHostDescription) object; |
| 177 | return Objects.equal(this.mac, that.mac) |
| 178 | && Objects.equal(this.vlan, that.vlan) |
| 179 | && Objects.equal(this.location, that.location) |
| 180 | && Objects.equal(this.ip, that.ip); |
| 181 | } |
| 182 | return false; |
| 183 | } |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 184 | } |