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; |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 40 | private final Set<IpAddress> ips; |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 41 | private final VlanId innerVlan; |
| 42 | private final EthType tpid; |
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 | |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 45 | // TODO consider moving this constructor to a builder pattern. |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 46 | /** |
| 47 | * Creates an end-station host using the supplied information. |
| 48 | * |
| 49 | * @param providerId provider identity |
| 50 | * @param id host identifier |
| 51 | * @param mac host MAC address |
| 52 | * @param vlan host VLAN identifier |
| 53 | * @param location host location |
| 54 | * @param ips host IP addresses |
| 55 | * @param annotations optional key/value annotations |
| 56 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 57 | public DefaultHost(ProviderId providerId, HostId id, MacAddress mac, |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 58 | VlanId vlan, HostLocation location, Set<IpAddress> ips, |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 59 | Annotations... annotations) { |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 60 | this(providerId, id, mac, vlan, location, ips, false, annotations); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Creates an end-station host using the supplied information. |
| 65 | * |
| 66 | * @param providerId provider identity |
| 67 | * @param id host identifier |
| 68 | * @param mac host MAC address |
| 69 | * @param vlan host VLAN identifier |
| 70 | * @param location host location |
| 71 | * @param ips host IP addresses |
| 72 | * @param configured true if configured via NetworkConfiguration |
| 73 | * @param annotations optional key/value annotations |
| 74 | */ |
| 75 | public DefaultHost(ProviderId providerId, HostId id, MacAddress mac, |
| 76 | VlanId vlan, HostLocation location, Set<IpAddress> ips, |
| 77 | boolean configured, Annotations... annotations) { |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 78 | this(providerId, id, mac, vlan, Collections.singleton(location), ips, |
| 79 | configured, annotations); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Creates an end-station host using the supplied information. |
| 84 | * |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 85 | * @param providerId provider identity |
| 86 | * @param id host identifier |
| 87 | * @param mac host MAC address |
| 88 | * @param vlan host VLAN identifier |
| 89 | * @param locations set of host locations |
| 90 | * @param ips host IP addresses |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 91 | * @param configured true if configured via NetworkConfiguration |
| 92 | * @param annotations optional key/value annotations |
| 93 | */ |
| 94 | public DefaultHost(ProviderId providerId, HostId id, MacAddress mac, |
| 95 | VlanId vlan, Set<HostLocation> locations, Set<IpAddress> ips, |
| 96 | boolean configured, Annotations... annotations) { |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 97 | this(providerId, id, mac, vlan, locations, ips, VlanId.NONE, |
| 98 | EthType.EtherType.UNKNOWN.ethType(), configured, annotations); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Creates an end-station host using the supplied information. |
| 103 | * |
| 104 | * @param providerId provider identity |
| 105 | * @param id host identifier |
| 106 | * @param mac host MAC address |
| 107 | * @param vlan host VLAN identifier |
| 108 | * @param locations set of host locations |
| 109 | * @param ips host IP addresses |
| 110 | * @param innerVlan host inner VLAN identifier |
| 111 | * @param tpid outer TPID of a host |
| 112 | * @param configured true if configured via NetworkConfiguration |
| 113 | * @param annotations optional key/value annotations |
| 114 | */ |
| 115 | public DefaultHost(ProviderId providerId, HostId id, MacAddress mac, VlanId vlan, |
| 116 | Set<HostLocation> locations, Set<IpAddress> ips, VlanId innerVlan, |
| 117 | EthType tpid, boolean configured, Annotations... annotations) { |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 118 | super(providerId, id, annotations); |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 119 | this.mac = mac; |
| 120 | this.vlan = vlan; |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 121 | this.locations = new HashSet<>(locations); |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 122 | this.ips = new HashSet<>(ips); |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 123 | this.configured = configured; |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 124 | this.innerVlan = innerVlan; |
| 125 | this.tpid = tpid; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public HostId id() { |
tom | 5a9383a | 2014-10-02 07:33:52 -0700 | [diff] [blame] | 130 | return (HostId) id; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Override |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 134 | public MacAddress mac() { |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 135 | return mac; |
| 136 | } |
| 137 | |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 138 | /** |
| 139 | * Returns an unmodifiable set of IP addresses currently bound to the |
| 140 | * host MAC address. |
| 141 | */ |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 142 | @Override |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 143 | public Set<IpAddress> ipAddresses() { |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 144 | return Collections.unmodifiableSet(ips); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public HostLocation location() { |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 149 | return locations.stream() |
| 150 | .sorted(Comparator.comparingLong(HostLocation::time).reversed()) |
| 151 | .findFirst().orElse(null); |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public Set<HostLocation> locations() { |
| 156 | return locations; |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | @Override |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 160 | public VlanId vlan() { |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 161 | return vlan; |
| 162 | } |
| 163 | |
| 164 | @Override |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 165 | public VlanId innerVlan() { |
| 166 | return innerVlan; |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public EthType tpid() { |
| 171 | return tpid; |
| 172 | } |
| 173 | |
| 174 | @Override |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 175 | public boolean configured() { |
| 176 | return configured; |
| 177 | } |
| 178 | |
| 179 | @Override |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 180 | public int hashCode() { |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 181 | return Objects.hash(id, mac, vlan, locations); |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public boolean equals(Object obj) { |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame] | 186 | if (this == obj) { |
| 187 | return true; |
| 188 | } |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 189 | if (obj instanceof DefaultHost) { |
| 190 | final DefaultHost other = (DefaultHost) obj; |
| 191 | return Objects.equals(this.id, other.id) && |
| 192 | Objects.equals(this.mac, other.mac) && |
| 193 | Objects.equals(this.vlan, other.vlan) && |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 194 | Objects.equals(this.locations, other.locations) && |
Charles Chan | f4f1f4b | 2016-02-22 11:12:53 -0800 | [diff] [blame] | 195 | Objects.equals(this.ipAddresses(), other.ipAddresses()) && |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 196 | Objects.equals(this.innerVlan, other.innerVlan) && |
| 197 | Objects.equals(this.tpid, other.tpid) && |
Charles Chan | f4f1f4b | 2016-02-22 11:12:53 -0800 | [diff] [blame] | 198 | Objects.equals(this.annotations(), other.annotations()); |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 199 | } |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public String toString() { |
| 205 | return toStringHelper(this) |
Charles Chan | f4f1f4b | 2016-02-22 11:12:53 -0800 | [diff] [blame] | 206 | .add("id", id()) |
| 207 | .add("mac", mac()) |
| 208 | .add("vlan", vlan()) |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 209 | .add("locations", locations()) |
Charles Chan | f4f1f4b | 2016-02-22 11:12:53 -0800 | [diff] [blame] | 210 | .add("ipAddresses", ipAddresses()) |
| 211 | .add("annotations", annotations()) |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 212 | .add("configured", configured()) |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 213 | .add("innerVlanId", innerVlan()) |
| 214 | .add("outerTPID", tpid()) |
Ayaka Koshibe | 74a2392 | 2014-09-09 16:45:39 -0700 | [diff] [blame] | 215 | .toString(); |
| 216 | } |
| 217 | |
| 218 | } |