blob: 74fa9f4f3a35b50ccc9a0aeb79cfbead14db6682 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.host;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070017
Yuta HIGUCHI5fa3dc02014-10-15 17:08:13 -070018import java.util.Collections;
Charles Chancd06c692017-04-27 20:46:06 -070019import java.util.Comparator;
20import java.util.HashSet;
Charles Chanf42939d2019-12-06 19:19:30 -080021import java.util.Objects;
Yuta HIGUCHI5fa3dc02014-10-15 17:08:13 -070022import java.util.Set;
23
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -070024import org.onlab.packet.EthType;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.net.AbstractDescription;
26import org.onosproject.net.HostLocation;
27import org.onosproject.net.SparseAnnotations;
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070028import org.onlab.packet.IpAddress;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070029import org.onlab.packet.MacAddress;
30import org.onlab.packet.VlanId;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070031
Yuta HIGUCHI5fa3dc02014-10-15 17:08:13 -070032import com.google.common.collect.ImmutableSet;
33
tom27ae0e62014-10-01 20:35:01 -070034import static com.google.common.base.MoreObjects.toStringHelper;
35
36/**
37 * Default implementation of an immutable host description.
38 */
tomf5d85d42014-10-02 05:27:56 -070039public class DefaultHostDescription extends AbstractDescription
tom27ae0e62014-10-01 20:35:01 -070040 implements HostDescription {
Ayaka Koshibe74a23922014-09-09 16:45:39 -070041
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070042 private final MacAddress mac;
43 private final VlanId vlan;
Charles Chancd06c692017-04-27 20:46:06 -070044 private final Set<HostLocation> locations;
Charles Chanf42939d2019-12-06 19:19:30 -080045 private final Set<HostLocation> auxLocations;
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070046 private final Set<IpAddress> ip;
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -070047 private final VlanId innerVlan;
48 private final EthType tpid;
sdn5e935452016-08-30 04:12:54 -070049 private final boolean configured;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070050
tom27ae0e62014-10-01 20:35:01 -070051 /**
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 Koshibea9c199f2014-09-16 16:21:40 -070059 public DefaultHostDescription(MacAddress mac, VlanId vlan,
tom27ae0e62014-10-01 20:35:01 -070060 HostLocation location,
tomf5d85d42014-10-02 05:27:56 -070061 SparseAnnotations... annotations) {
Charles Chancd06c692017-04-27 20:46:06 -070062 this(mac, vlan, location, Collections.emptySet(), annotations);
Ayaka Koshibe1a100982014-09-13 19:32:19 -070063 }
64
tom27ae0e62014-10-01 20:35:01 -070065 /**
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
tom093340b2014-10-10 00:15:36 -070071 * @param ip host IP address
tom27ae0e62014-10-01 20:35:01 -070072 * @param annotations optional key/value annotations map
73 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070074 public DefaultHostDescription(MacAddress mac, VlanId vlan,
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070075 HostLocation location, IpAddress ip,
tomf5d85d42014-10-02 05:27:56 -070076 SparseAnnotations... annotations) {
Yuta HIGUCHI5fa3dc02014-10-15 17:08:13 -070077 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 Radoslavov33f228a2014-10-27 19:33:16 -070090 HostLocation location, Set<IpAddress> ip,
Yuta HIGUCHI5fa3dc02014-10-15 17:08:13 -070091 SparseAnnotations... annotations) {
sdn5e935452016-08-30 04:12:54 -070092 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 Chancd06c692017-04-27 20:46:06 -0700108 this(mac, vlan, location, Collections.emptySet(), configured, annotations);
sdn5e935452016-08-30 04:12:54 -0700109 }
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 Chancd06c692017-04-27 20:46:06 -0700125 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 Hyun2c95acf2018-03-14 16:47:34 -0700142 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 Chanf42939d2019-12-06 19:19:30 -0800161 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) {
tom27ae0e62014-10-01 20:35:01 -0700181 super(annotations);
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700182 this.mac = mac;
183 this.vlan = vlan;
Charles Chancd06c692017-04-27 20:46:06 -0700184 this.locations = new HashSet<>(locations);
Charles Chanf42939d2019-12-06 19:19:30 -0800185 this.auxLocations = (auxLocations != null) ? new HashSet<>(auxLocations) : null;
Charles Chancd06c692017-04-27 20:46:06 -0700186 this.ip = new HashSet<>(ip);
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700187 this.innerVlan = innerVlan;
188 this.tpid = tpid;
sdn5e935452016-08-30 04:12:54 -0700189 this.configured = configured;
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700190 }
191
RafaƂ Szaleckide5cf842018-11-17 13:30:01 +0100192 /**
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 Koshibe74a23922014-09-09 16:45:39 -0700214 @Override
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700215 public MacAddress hwAddress() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700216 return mac;
217 }
218
219 @Override
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700220 public VlanId vlan() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700221 return vlan;
222 }
223
224 @Override
225 public HostLocation location() {
Charles Chancd06c692017-04-27 20:46:06 -0700226 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 Koshibe74a23922014-09-09 16:45:39 -0700234 }
235
236 @Override
Charles Chanf42939d2019-12-06 19:19:30 -0800237 public Set<HostLocation> auxLocations() {
238 return auxLocations;
239 }
240
241 @Override
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700242 public Set<IpAddress> ipAddress() {
tom093340b2014-10-10 00:15:36 -0700243 return ip;
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700244 }
245
246 @Override
sdn5e935452016-08-30 04:12:54 -0700247 public boolean configured() {
248 return configured;
249 }
250
251 @Override
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700252 public VlanId innerVlan() {
253 return innerVlan;
254 }
255
256 @Override
257 public EthType tpid() {
258 return tpid;
259 }
260
261 @Override
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700262 public String toString() {
263 return toStringHelper(this)
264 .add("mac", mac)
265 .add("vlan", vlan)
Charles Chancd06c692017-04-27 20:46:06 -0700266 .add("locations", locations)
Charles Chanf42939d2019-12-06 19:19:30 -0800267 .add("auxLocations", auxLocations)
tom093340b2014-10-10 00:15:36 -0700268 .add("ipAddress", ip)
sdn5e935452016-08-30 04:12:54 -0700269 .add("configured", configured)
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700270 .add("innerVlanId", innerVlan)
271 .add("outerTPID", tpid)
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700272 .toString();
273 }
274
HIGUCHI Yuta703a5af2015-11-18 23:43:50 -0800275 @Override
276 public int hashCode() {
Charles Chanf42939d2019-12-06 19:19:30 -0800277 return Objects.hash(super.hashCode(), mac, vlan, locations, auxLocations, ip);
HIGUCHI Yuta703a5af2015-11-18 23:43:50 -0800278 }
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 Chanf42939d2019-12-06 19:19:30 -0800287 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 Yuta703a5af2015-11-18 23:43:50 -0800294 }
295 return false;
296 }
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700297}