blob: 5c28a04aaf19eefd8813fa22a0d5a658740c669d [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;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070017
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -070018import org.onlab.packet.EthType;
Brian O'Connorabafb502014-12-02 22:26:20 -080019import org.onosproject.net.provider.ProviderId;
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070020import org.onlab.packet.IpAddress;
tomf5d85d42014-10-02 05:27:56 -070021import org.onlab.packet.MacAddress;
22import org.onlab.packet.VlanId;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070023
24import java.util.Collections;
Charles Chancd06c692017-04-27 20:46:06 -070025import java.util.Comparator;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070026import java.util.HashSet;
27import java.util.Objects;
28import java.util.Set;
29
tomf5d85d42014-10-02 05:27:56 -070030import static com.google.common.base.MoreObjects.toStringHelper;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070031
Ayaka Koshibe1c7b38e2014-09-11 13:09:51 -070032/**
33 * A basic implementation of a Host.
34 */
Ayaka Koshibe74a23922014-09-09 16:45:39 -070035public class DefaultHost extends AbstractElement implements Host {
36
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070037 private final MacAddress mac;
38 private final VlanId vlan;
Charles Chancd06c692017-04-27 20:46:06 -070039 private final Set<HostLocation> locations;
Charles Chan67a38552019-11-19 14:30:43 -080040 private final Set<HostLocation> auxLocations;
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070041 private final Set<IpAddress> ips;
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -070042 private final VlanId innerVlan;
43 private final EthType tpid;
sdn5e935452016-08-30 04:12:54 -070044 private final boolean configured;
jobind19b7142019-05-17 09:46:52 -040045 private final boolean suspended;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070046
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -070047 // TODO consider moving this constructor to a builder pattern.
tomf5d85d42014-10-02 05:27:56 -070048 /**
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 Koshibea9c199f2014-09-16 16:21:40 -070059 public DefaultHost(ProviderId providerId, HostId id, MacAddress mac,
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070060 VlanId vlan, HostLocation location, Set<IpAddress> ips,
tomf5d85d42014-10-02 05:27:56 -070061 Annotations... annotations) {
sdn5e935452016-08-30 04:12:54 -070062 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 Chancd06c692017-04-27 20:46:06 -070080 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 Hyun2c95acf2018-03-14 16:47:34 -070087 * @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 Chancd06c692017-04-27 20:46:06 -070093 * @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 Hyun2c95acf2018-03-14 16:47:34 -070099 this(providerId, id, mac, vlan, locations, ips, VlanId.NONE,
jobind19b7142019-05-17 09:46:52 -0400100 EthType.EtherType.UNKNOWN.ethType(), configured, annotations);
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700101 }
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 Chan67a38552019-11-19 14:30:43 -0800120 this(providerId, id, mac, vlan, locations, ips, innerVlan, tpid, configured, false, annotations);
jobind19b7142019-05-17 09:46:52 -0400121 }
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 Chan67a38552019-11-19 14:30:43 -0800141 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) {
jobind19b7142019-05-17 09:46:52 -0400164 super(providerId, id, annotations);
165 this.mac = mac;
166 this.vlan = vlan;
167 this.locations = new HashSet<>(locations);
Charles Chan67a38552019-11-19 14:30:43 -0800168 this.auxLocations = (auxLocations != null) ? new HashSet<>(auxLocations) : null;
jobind19b7142019-05-17 09:46:52 -0400169 this.ips = new HashSet<>(ips);
170 this.configured = configured;
171 this.innerVlan = innerVlan;
172 this.tpid = tpid;
173 this.suspended = suspended;
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700174 }
175
176 @Override
177 public HostId id() {
tom5a9383a2014-10-02 07:33:52 -0700178 return (HostId) id;
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700179 }
180
181 @Override
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700182 public MacAddress mac() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700183 return mac;
184 }
185
Saurav Das018605f2017-02-18 14:05:44 -0800186 /**
187 * Returns an unmodifiable set of IP addresses currently bound to the
188 * host MAC address.
189 */
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700190 @Override
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700191 public Set<IpAddress> ipAddresses() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700192 return Collections.unmodifiableSet(ips);
193 }
194
195 @Override
196 public HostLocation location() {
Charles Chancd06c692017-04-27 20:46:06 -0700197 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 Koshibe74a23922014-09-09 16:45:39 -0700205 }
206
207 @Override
Charles Chan67a38552019-11-19 14:30:43 -0800208 public Set<HostLocation> auxLocations() {
209 return auxLocations;
210 }
211
212 @Override
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700213 public VlanId vlan() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700214 return vlan;
215 }
216
217 @Override
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700218 public VlanId innerVlan() {
219 return innerVlan;
220 }
221
222 @Override
223 public EthType tpid() {
224 return tpid;
225 }
226
227 @Override
sdn5e935452016-08-30 04:12:54 -0700228 public boolean configured() {
229 return configured;
230 }
231
232 @Override
jobind19b7142019-05-17 09:46:52 -0400233 public boolean suspended() {
234 return this.suspended;
235 }
236
237
238 @Override
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700239 public int hashCode() {
Charles Chan67a38552019-11-19 14:30:43 -0800240 return Objects.hash(id, mac, vlan, locations, auxLocations, ips, innerVlan, tpid, suspended);
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700241 }
242
243 @Override
244 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -0700245 if (this == obj) {
246 return true;
247 }
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700248 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 Chancd06c692017-04-27 20:46:06 -0700253 Objects.equals(this.locations, other.locations) &&
Charles Chan67a38552019-11-19 14:30:43 -0800254 Objects.equals(this.auxLocations, other.auxLocations) &&
Charles Chanf4f1f4b2016-02-22 11:12:53 -0800255 Objects.equals(this.ipAddresses(), other.ipAddresses()) &&
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700256 Objects.equals(this.innerVlan, other.innerVlan) &&
257 Objects.equals(this.tpid, other.tpid) &&
jobind19b7142019-05-17 09:46:52 -0400258 Objects.equals(this.annotations(), other.annotations()) &&
259 Objects.equals(this.suspended, other.suspended);
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700260 }
261 return false;
262 }
263
264 @Override
265 public String toString() {
266 return toStringHelper(this)
Charles Chanf4f1f4b2016-02-22 11:12:53 -0800267 .add("id", id())
268 .add("mac", mac())
269 .add("vlan", vlan())
Charles Chancd06c692017-04-27 20:46:06 -0700270 .add("locations", locations())
Charles Chan67a38552019-11-19 14:30:43 -0800271 .add("auxLocations", auxLocations())
Charles Chanf4f1f4b2016-02-22 11:12:53 -0800272 .add("ipAddresses", ipAddresses())
273 .add("annotations", annotations())
sdn5e935452016-08-30 04:12:54 -0700274 .add("configured", configured())
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700275 .add("innerVlanId", innerVlan())
276 .add("outerTPID", tpid())
jobind19b7142019-05-17 09:46:52 -0400277 .add("suspended", suspended())
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700278 .toString();
279 }
280
281}