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