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