Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
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; |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 19 | import java.util.Comparator; |
| 20 | import java.util.HashSet; |
Charles Chan | f42939d | 2019-12-06 19:19:30 -0800 | [diff] [blame] | 21 | import java.util.Objects; |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 22 | import java.util.Set; |
| 23 | |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 24 | import org.onlab.packet.EthType; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.net.AbstractDescription; |
| 26 | import org.onosproject.net.HostLocation; |
| 27 | import org.onosproject.net.SparseAnnotations; |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 28 | import org.onlab.packet.IpAddress; |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 29 | import org.onlab.packet.MacAddress; |
| 30 | import org.onlab.packet.VlanId; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 31 | |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 32 | import com.google.common.collect.ImmutableSet; |
| 33 | |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 34 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 35 | |
| 36 | /** |
| 37 | * Default implementation of an immutable host description. |
| 38 | */ |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 39 | public class DefaultHostDescription extends AbstractDescription |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 40 | implements HostDescription { |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 41 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 42 | private final MacAddress mac; |
| 43 | private final VlanId vlan; |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 44 | private final Set<HostLocation> locations; |
Charles Chan | f42939d | 2019-12-06 19:19:30 -0800 | [diff] [blame] | 45 | private final Set<HostLocation> auxLocations; |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 46 | private final Set<IpAddress> ip; |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 47 | private final VlanId innerVlan; |
| 48 | private final EthType tpid; |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 49 | private final boolean configured; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 50 | |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 51 | /** |
| 52 | * Creates a host description using the supplied information. |
| 53 | * |
| 54 | * @param mac host MAC address |
| 55 | * @param vlan host VLAN identifier |
| 56 | * @param location host location |
| 57 | * @param annotations optional key/value annotations map |
| 58 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 59 | public DefaultHostDescription(MacAddress mac, VlanId vlan, |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 60 | HostLocation location, |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 61 | SparseAnnotations... annotations) { |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 62 | this(mac, vlan, location, Collections.emptySet(), annotations); |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 63 | } |
| 64 | |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 65 | /** |
| 66 | * Creates a host description using the supplied information. |
| 67 | * |
| 68 | * @param mac host MAC address |
| 69 | * @param vlan host VLAN identifier |
| 70 | * @param location host location |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 71 | * @param ip host IP address |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 72 | * @param annotations optional key/value annotations map |
| 73 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 74 | public DefaultHostDescription(MacAddress mac, VlanId vlan, |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 75 | HostLocation location, IpAddress ip, |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 76 | SparseAnnotations... annotations) { |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 77 | this(mac, vlan, location, ImmutableSet.of(ip), annotations); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Creates a host description using the supplied information. |
| 82 | * |
| 83 | * @param mac host MAC address |
| 84 | * @param vlan host VLAN identifier |
| 85 | * @param location host location |
| 86 | * @param ip host IP addresses |
| 87 | * @param annotations optional key/value annotations map |
| 88 | */ |
| 89 | public DefaultHostDescription(MacAddress mac, VlanId vlan, |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 90 | HostLocation location, Set<IpAddress> ip, |
Yuta HIGUCHI | 5fa3dc0 | 2014-10-15 17:08:13 -0700 | [diff] [blame] | 91 | SparseAnnotations... annotations) { |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 92 | this(mac, vlan, location, ip, false, annotations); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Creates a host description using the supplied information. |
| 97 | * |
| 98 | * @param mac host MAC address |
| 99 | * @param vlan host VLAN identifier |
| 100 | * @param location host location |
| 101 | * @param configured true if configured via NetworkConfiguration |
| 102 | * @param annotations optional key/value annotations map |
| 103 | */ |
| 104 | public DefaultHostDescription(MacAddress mac, VlanId vlan, |
| 105 | HostLocation location, |
| 106 | boolean configured, |
| 107 | SparseAnnotations... annotations) { |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 108 | this(mac, vlan, location, Collections.emptySet(), configured, annotations); |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Creates a host description using the supplied information. |
| 113 | * |
| 114 | * @param mac host MAC address |
| 115 | * @param vlan host VLAN identifier |
| 116 | * @param location host location |
| 117 | * @param ip host IP address |
| 118 | * @param configured true if configured via NetworkConfiguration |
| 119 | * @param annotations optional key/value annotations map |
| 120 | */ |
| 121 | public DefaultHostDescription(MacAddress mac, VlanId vlan, |
| 122 | HostLocation location, Set<IpAddress> ip, |
| 123 | boolean configured, |
| 124 | SparseAnnotations... annotations) { |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 125 | this(mac, vlan, Collections.singleton(location), ip, configured, annotations); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Creates a host description using the supplied information. |
| 130 | * |
| 131 | * @param mac host MAC address |
| 132 | * @param vlan host VLAN identifier |
| 133 | * @param locations host locations |
| 134 | * @param ip host IP address |
| 135 | * @param configured true if configured via NetworkConfiguration |
| 136 | * @param annotations optional key/value annotations map |
| 137 | */ |
| 138 | public DefaultHostDescription(MacAddress mac, VlanId vlan, |
| 139 | Set<HostLocation> locations, |
| 140 | Set<IpAddress> ip, boolean configured, |
| 141 | SparseAnnotations... annotations) { |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 142 | this(mac, vlan, locations, ip, VlanId.NONE, EthType.EtherType.UNKNOWN.ethType(), |
| 143 | configured, annotations); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Creates a host description using the supplied information. |
| 148 | * |
| 149 | * @param mac host MAC address |
| 150 | * @param vlan host VLAN identifier |
| 151 | * @param locations host locations |
| 152 | * @param ip host IP address |
| 153 | * @param innerVlan host inner VLAN identifier |
| 154 | * @param tpid outer TPID of a host |
| 155 | * @param configured true if configured via NetworkConfiguration |
| 156 | * @param annotations optional key/value annotations map |
| 157 | */ |
| 158 | public DefaultHostDescription(MacAddress mac, VlanId vlan, Set<HostLocation> locations, |
| 159 | Set<IpAddress> ip, VlanId innerVlan, EthType tpid, |
| 160 | boolean configured, SparseAnnotations... annotations) { |
Charles Chan | f42939d | 2019-12-06 19:19:30 -0800 | [diff] [blame] | 161 | this(mac, vlan, locations, null, ip, innerVlan, tpid, configured, annotations); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Creates a host description using the supplied information. |
| 166 | * |
| 167 | * @param mac host MAC address |
| 168 | * @param vlan host VLAN identifier |
| 169 | * @param locations host locations |
| 170 | * @param auxLocations set of auxiliary locations, or null if unspecified |
| 171 | * @param ip host IP address |
| 172 | * @param innerVlan host inner VLAN identifier |
| 173 | * @param tpid outer TPID of a host |
| 174 | * @param configured true if configured via NetworkConfiguration |
| 175 | * @param annotations optional key/value annotations map |
| 176 | */ |
| 177 | public DefaultHostDescription(MacAddress mac, VlanId vlan, |
| 178 | Set<HostLocation> locations, Set<HostLocation> auxLocations, |
| 179 | Set<IpAddress> ip, VlanId innerVlan, EthType tpid, |
| 180 | boolean configured, SparseAnnotations... annotations) { |
tom | 27ae0e6 | 2014-10-01 20:35:01 -0700 | [diff] [blame] | 181 | super(annotations); |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 182 | this.mac = mac; |
| 183 | this.vlan = vlan; |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 184 | this.locations = new HashSet<>(locations); |
Charles Chan | f42939d | 2019-12-06 19:19:30 -0800 | [diff] [blame] | 185 | this.auxLocations = (auxLocations != null) ? new HashSet<>(auxLocations) : null; |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 186 | this.ip = new HashSet<>(ip); |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 187 | this.innerVlan = innerVlan; |
| 188 | this.tpid = tpid; |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 189 | this.configured = configured; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 190 | } |
| 191 | |
RafaĆ Szalecki | de5cf84 | 2018-11-17 13:30:01 +0100 | [diff] [blame] | 192 | /** |
| 193 | * Creates a host description using the supplied information. |
| 194 | * @param base HostDescription to basic information |
| 195 | * @param annotations Annotations to use. |
| 196 | */ |
| 197 | public DefaultHostDescription(HostDescription base, SparseAnnotations annotations) { |
| 198 | this(base.hwAddress(), base.vlan(), base.locations(), base.ipAddress(), base.innerVlan(), base.tpid(), |
| 199 | base.configured(), annotations); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Creates a host description using the supplied information. |
| 204 | * |
| 205 | * @param base base |
| 206 | * @param annotations annotations |
| 207 | * @return host description |
| 208 | */ |
| 209 | public static DefaultHostDescription copyReplacingAnnotation(HostDescription base, |
| 210 | SparseAnnotations annotations) { |
| 211 | return new DefaultHostDescription(base, annotations); |
| 212 | } |
| 213 | |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 214 | @Override |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 215 | public MacAddress hwAddress() { |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 216 | return mac; |
| 217 | } |
| 218 | |
| 219 | @Override |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 220 | public VlanId vlan() { |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 221 | return vlan; |
| 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public HostLocation location() { |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 226 | return locations.stream() |
| 227 | .sorted(Comparator.comparingLong(HostLocation::time).reversed()) |
| 228 | .findFirst().orElse(null); |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public Set<HostLocation> locations() { |
| 233 | return locations; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | @Override |
Charles Chan | f42939d | 2019-12-06 19:19:30 -0800 | [diff] [blame] | 237 | public Set<HostLocation> auxLocations() { |
| 238 | return auxLocations; |
| 239 | } |
| 240 | |
| 241 | @Override |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 242 | public Set<IpAddress> ipAddress() { |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 243 | return ip; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | @Override |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 247 | public boolean configured() { |
| 248 | return configured; |
| 249 | } |
| 250 | |
| 251 | @Override |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 252 | public VlanId innerVlan() { |
| 253 | return innerVlan; |
| 254 | } |
| 255 | |
| 256 | @Override |
| 257 | public EthType tpid() { |
| 258 | return tpid; |
| 259 | } |
| 260 | |
| 261 | @Override |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 262 | public String toString() { |
| 263 | return toStringHelper(this) |
| 264 | .add("mac", mac) |
| 265 | .add("vlan", vlan) |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 266 | .add("locations", locations) |
Charles Chan | f42939d | 2019-12-06 19:19:30 -0800 | [diff] [blame] | 267 | .add("auxLocations", auxLocations) |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 268 | .add("ipAddress", ip) |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 269 | .add("configured", configured) |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 270 | .add("innerVlanId", innerVlan) |
| 271 | .add("outerTPID", tpid) |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 272 | .toString(); |
| 273 | } |
| 274 | |
HIGUCHI Yuta | 703a5af | 2015-11-18 23:43:50 -0800 | [diff] [blame] | 275 | @Override |
| 276 | public int hashCode() { |
Charles Chan | f42939d | 2019-12-06 19:19:30 -0800 | [diff] [blame] | 277 | return Objects.hash(super.hashCode(), mac, vlan, locations, auxLocations, ip); |
HIGUCHI Yuta | 703a5af | 2015-11-18 23:43:50 -0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | @Override |
| 281 | public boolean equals(Object object) { |
| 282 | if (object != null && getClass() == object.getClass()) { |
| 283 | if (!super.equals(object)) { |
| 284 | return false; |
| 285 | } |
| 286 | DefaultHostDescription that = (DefaultHostDescription) object; |
Charles Chan | f42939d | 2019-12-06 19:19:30 -0800 | [diff] [blame] | 287 | return Objects.equals(this.mac, that.mac) |
| 288 | && Objects.equals(this.vlan, that.vlan) |
| 289 | && Objects.equals(this.locations, that.locations) |
| 290 | && Objects.equals(this.auxLocations, that.auxLocations) |
| 291 | && Objects.equals(this.ip, that.ip) |
| 292 | && Objects.equals(this.innerVlan, that.innerVlan) |
| 293 | && Objects.equals(this.tpid, that.tpid); |
HIGUCHI Yuta | 703a5af | 2015-11-18 23:43:50 -0800 | [diff] [blame] | 294 | } |
| 295 | return false; |
| 296 | } |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 297 | } |