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 | |
| 170 | @Override |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 171 | public MacAddress hwAddress() { |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 172 | return mac; |
| 173 | } |
| 174 | |
| 175 | @Override |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 176 | public VlanId vlan() { |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 177 | return vlan; |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public HostLocation location() { |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 182 | return locations.stream() |
| 183 | .sorted(Comparator.comparingLong(HostLocation::time).reversed()) |
| 184 | .findFirst().orElse(null); |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public Set<HostLocation> locations() { |
| 189 | return locations; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | @Override |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 193 | public Set<IpAddress> ipAddress() { |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 194 | return ip; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | @Override |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 198 | public boolean configured() { |
| 199 | return configured; |
| 200 | } |
| 201 | |
| 202 | @Override |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 203 | public VlanId innerVlan() { |
| 204 | return innerVlan; |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public EthType tpid() { |
| 209 | return tpid; |
| 210 | } |
| 211 | |
| 212 | @Override |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 213 | public String toString() { |
| 214 | return toStringHelper(this) |
| 215 | .add("mac", mac) |
| 216 | .add("vlan", vlan) |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 217 | .add("locations", locations) |
tom | 093340b | 2014-10-10 00:15:36 -0700 | [diff] [blame] | 218 | .add("ipAddress", ip) |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 219 | .add("configured", configured) |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 220 | .add("innerVlanId", innerVlan) |
| 221 | .add("outerTPID", tpid) |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 222 | .toString(); |
| 223 | } |
| 224 | |
HIGUCHI Yuta | 703a5af | 2015-11-18 23:43:50 -0800 | [diff] [blame] | 225 | @Override |
| 226 | public int hashCode() { |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 227 | return Objects.hashCode(super.hashCode(), mac, vlan, locations, ip); |
HIGUCHI Yuta | 703a5af | 2015-11-18 23:43:50 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public boolean equals(Object object) { |
| 232 | if (object != null && getClass() == object.getClass()) { |
| 233 | if (!super.equals(object)) { |
| 234 | return false; |
| 235 | } |
| 236 | DefaultHostDescription that = (DefaultHostDescription) object; |
| 237 | return Objects.equal(this.mac, that.mac) |
| 238 | && Objects.equal(this.vlan, that.vlan) |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 239 | && Objects.equal(this.locations, that.locations) |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 240 | && Objects.equal(this.ip, that.ip) |
| 241 | && Objects.equal(this.innerVlan, that.innerVlan) |
| 242 | && Objects.equal(this.tpid, that.tpid); |
HIGUCHI Yuta | 703a5af | 2015-11-18 23:43:50 -0800 | [diff] [blame] | 243 | } |
| 244 | return false; |
| 245 | } |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 246 | } |