blob: 19922839ee147847225c86af173ceae1e61cfcbf [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
170 @Override
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700171 public MacAddress hwAddress() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700172 return mac;
173 }
174
175 @Override
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700176 public VlanId vlan() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700177 return vlan;
178 }
179
180 @Override
181 public HostLocation location() {
Charles Chancd06c692017-04-27 20:46:06 -0700182 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 Koshibe74a23922014-09-09 16:45:39 -0700190 }
191
192 @Override
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700193 public Set<IpAddress> ipAddress() {
tom093340b2014-10-10 00:15:36 -0700194 return ip;
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700195 }
196
197 @Override
sdn5e935452016-08-30 04:12:54 -0700198 public boolean configured() {
199 return configured;
200 }
201
202 @Override
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700203 public VlanId innerVlan() {
204 return innerVlan;
205 }
206
207 @Override
208 public EthType tpid() {
209 return tpid;
210 }
211
212 @Override
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700213 public String toString() {
214 return toStringHelper(this)
215 .add("mac", mac)
216 .add("vlan", vlan)
Charles Chancd06c692017-04-27 20:46:06 -0700217 .add("locations", locations)
tom093340b2014-10-10 00:15:36 -0700218 .add("ipAddress", ip)
sdn5e935452016-08-30 04:12:54 -0700219 .add("configured", configured)
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700220 .add("innerVlanId", innerVlan)
221 .add("outerTPID", tpid)
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700222 .toString();
223 }
224
HIGUCHI Yuta703a5af2015-11-18 23:43:50 -0800225 @Override
226 public int hashCode() {
Charles Chancd06c692017-04-27 20:46:06 -0700227 return Objects.hashCode(super.hashCode(), mac, vlan, locations, ip);
HIGUCHI Yuta703a5af2015-11-18 23:43:50 -0800228 }
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 Chancd06c692017-04-27 20:46:06 -0700239 && Objects.equal(this.locations, that.locations)
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700240 && Objects.equal(this.ip, that.ip)
241 && Objects.equal(this.innerVlan, that.innerVlan)
242 && Objects.equal(this.tpid, that.tpid);
HIGUCHI Yuta703a5af2015-11-18 23:43:50 -0800243 }
244 return false;
245 }
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700246}