blob: c83c2c1408ff0e44da75e283acf730aae81c9656 [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;
Yuta HIGUCHI5fa3dc02014-10-15 17:08:13 -070021import java.util.Set;
22
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -070023import org.onlab.packet.EthType;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.net.AbstractDescription;
25import org.onosproject.net.HostLocation;
26import org.onosproject.net.SparseAnnotations;
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070027import org.onlab.packet.IpAddress;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070028import org.onlab.packet.MacAddress;
29import org.onlab.packet.VlanId;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070030
Yuta HIGUCHI5fa3dc02014-10-15 17:08:13 -070031import com.google.common.collect.ImmutableSet;
32
tom27ae0e62014-10-01 20:35:01 -070033import static com.google.common.base.MoreObjects.toStringHelper;
HIGUCHI Yuta703a5af2015-11-18 23:43:50 -080034import com.google.common.base.Objects;
tom27ae0e62014-10-01 20:35:01 -070035
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;
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070045 private final Set<IpAddress> ip;
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -070046 private final VlanId innerVlan;
47 private final EthType tpid;
sdn5e935452016-08-30 04:12:54 -070048 private final boolean configured;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070049
tom27ae0e62014-10-01 20:35:01 -070050 /**
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 Koshibea9c199f2014-09-16 16:21:40 -070058 public DefaultHostDescription(MacAddress mac, VlanId vlan,
tom27ae0e62014-10-01 20:35:01 -070059 HostLocation location,
tomf5d85d42014-10-02 05:27:56 -070060 SparseAnnotations... annotations) {
Charles Chancd06c692017-04-27 20:46:06 -070061 this(mac, vlan, location, Collections.emptySet(), annotations);
Ayaka Koshibe1a100982014-09-13 19:32:19 -070062 }
63
tom27ae0e62014-10-01 20:35:01 -070064 /**
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
tom093340b2014-10-10 00:15:36 -070070 * @param ip host IP address
tom27ae0e62014-10-01 20:35:01 -070071 * @param annotations optional key/value annotations map
72 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070073 public DefaultHostDescription(MacAddress mac, VlanId vlan,
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070074 HostLocation location, IpAddress ip,
tomf5d85d42014-10-02 05:27:56 -070075 SparseAnnotations... annotations) {
Yuta HIGUCHI5fa3dc02014-10-15 17:08:13 -070076 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 Radoslavov33f228a2014-10-27 19:33:16 -070089 HostLocation location, Set<IpAddress> ip,
Yuta HIGUCHI5fa3dc02014-10-15 17:08:13 -070090 SparseAnnotations... annotations) {
sdn5e935452016-08-30 04:12:54 -070091 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 Chancd06c692017-04-27 20:46:06 -0700107 this(mac, vlan, location, Collections.emptySet(), configured, annotations);
sdn5e935452016-08-30 04:12:54 -0700108 }
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 Chancd06c692017-04-27 20:46:06 -0700124 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 Hyun2c95acf2018-03-14 16:47:34 -0700141 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) {
tom27ae0e62014-10-01 20:35:01 -0700160 super(annotations);
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700161 this.mac = mac;
162 this.vlan = vlan;
Charles Chancd06c692017-04-27 20:46:06 -0700163 this.locations = new HashSet<>(locations);
164 this.ip = new HashSet<>(ip);
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700165 this.innerVlan = innerVlan;
166 this.tpid = tpid;
sdn5e935452016-08-30 04:12:54 -0700167 this.configured = configured;
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700168 }
169
RafaƂ Szaleckide5cf842018-11-17 13:30:01 +0100170 /**
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 Koshibe74a23922014-09-09 16:45:39 -0700192 @Override
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700193 public MacAddress hwAddress() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700194 return mac;
195 }
196
197 @Override
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700198 public VlanId vlan() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700199 return vlan;
200 }
201
202 @Override
203 public HostLocation location() {
Charles Chancd06c692017-04-27 20:46:06 -0700204 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 Koshibe74a23922014-09-09 16:45:39 -0700212 }
213
214 @Override
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700215 public Set<IpAddress> ipAddress() {
tom093340b2014-10-10 00:15:36 -0700216 return ip;
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700217 }
218
219 @Override
sdn5e935452016-08-30 04:12:54 -0700220 public boolean configured() {
221 return configured;
222 }
223
224 @Override
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700225 public VlanId innerVlan() {
226 return innerVlan;
227 }
228
229 @Override
230 public EthType tpid() {
231 return tpid;
232 }
233
234 @Override
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700235 public String toString() {
236 return toStringHelper(this)
237 .add("mac", mac)
238 .add("vlan", vlan)
Charles Chancd06c692017-04-27 20:46:06 -0700239 .add("locations", locations)
tom093340b2014-10-10 00:15:36 -0700240 .add("ipAddress", ip)
sdn5e935452016-08-30 04:12:54 -0700241 .add("configured", configured)
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700242 .add("innerVlanId", innerVlan)
243 .add("outerTPID", tpid)
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700244 .toString();
245 }
246
HIGUCHI Yuta703a5af2015-11-18 23:43:50 -0800247 @Override
248 public int hashCode() {
Charles Chancd06c692017-04-27 20:46:06 -0700249 return Objects.hashCode(super.hashCode(), mac, vlan, locations, ip);
HIGUCHI Yuta703a5af2015-11-18 23:43:50 -0800250 }
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 Chancd06c692017-04-27 20:46:06 -0700261 && Objects.equal(this.locations, that.locations)
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700262 && Objects.equal(this.ip, that.ip)
263 && Objects.equal(this.innerVlan, that.innerVlan)
264 && Objects.equal(this.tpid, that.tpid);
HIGUCHI Yuta703a5af2015-11-18 23:43:50 -0800265 }
266 return false;
267 }
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700268}