blob: 8e6c1952d95ba5497dd7a646d7acaf67e071afbe [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;
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070040 private final Set<IpAddress> ips;
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -070041 private final VlanId innerVlan;
42 private final EthType tpid;
sdn5e935452016-08-30 04:12:54 -070043 private final boolean configured;
jobind19b7142019-05-17 09:46:52 -040044 private final boolean suspended;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070045
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -070046 // TODO consider moving this constructor to a builder pattern.
tomf5d85d42014-10-02 05:27:56 -070047 /**
48 * Creates an end-station host using the supplied information.
49 *
50 * @param providerId provider identity
51 * @param id host identifier
52 * @param mac host MAC address
53 * @param vlan host VLAN identifier
54 * @param location host location
55 * @param ips host IP addresses
56 * @param annotations optional key/value annotations
57 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070058 public DefaultHost(ProviderId providerId, HostId id, MacAddress mac,
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070059 VlanId vlan, HostLocation location, Set<IpAddress> ips,
tomf5d85d42014-10-02 05:27:56 -070060 Annotations... annotations) {
sdn5e935452016-08-30 04:12:54 -070061 this(providerId, id, mac, vlan, location, ips, false, annotations);
62 }
63
64 /**
65 * Creates an end-station host using the supplied information.
66 *
67 * @param providerId provider identity
68 * @param id host identifier
69 * @param mac host MAC address
70 * @param vlan host VLAN identifier
71 * @param location host location
72 * @param ips host IP addresses
73 * @param configured true if configured via NetworkConfiguration
74 * @param annotations optional key/value annotations
75 */
76 public DefaultHost(ProviderId providerId, HostId id, MacAddress mac,
77 VlanId vlan, HostLocation location, Set<IpAddress> ips,
78 boolean configured, Annotations... annotations) {
Charles Chancd06c692017-04-27 20:46:06 -070079 this(providerId, id, mac, vlan, Collections.singleton(location), ips,
80 configured, annotations);
81 }
82
83 /**
84 * Creates an end-station host using the supplied information.
85 *
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -070086 * @param providerId provider identity
87 * @param id host identifier
88 * @param mac host MAC address
89 * @param vlan host VLAN identifier
90 * @param locations set of host locations
91 * @param ips host IP addresses
Charles Chancd06c692017-04-27 20:46:06 -070092 * @param configured true if configured via NetworkConfiguration
93 * @param annotations optional key/value annotations
94 */
95 public DefaultHost(ProviderId providerId, HostId id, MacAddress mac,
96 VlanId vlan, Set<HostLocation> locations, Set<IpAddress> ips,
97 boolean configured, Annotations... annotations) {
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -070098 this(providerId, id, mac, vlan, locations, ips, VlanId.NONE,
jobind19b7142019-05-17 09:46:52 -040099 EthType.EtherType.UNKNOWN.ethType(), configured, annotations);
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700100 }
101
102 /**
103 * Creates an end-station host using the supplied information.
104 *
105 * @param providerId provider identity
106 * @param id host identifier
107 * @param mac host MAC address
108 * @param vlan host VLAN identifier
109 * @param locations set of host locations
110 * @param ips host IP addresses
111 * @param innerVlan host inner VLAN identifier
112 * @param tpid outer TPID of a host
113 * @param configured true if configured via NetworkConfiguration
114 * @param annotations optional key/value annotations
115 */
116 public DefaultHost(ProviderId providerId, HostId id, MacAddress mac, VlanId vlan,
117 Set<HostLocation> locations, Set<IpAddress> ips, VlanId innerVlan,
118 EthType tpid, boolean configured, Annotations... annotations) {
tomf5d85d42014-10-02 05:27:56 -0700119 super(providerId, id, annotations);
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700120 this.mac = mac;
121 this.vlan = vlan;
Charles Chancd06c692017-04-27 20:46:06 -0700122 this.locations = new HashSet<>(locations);
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700123 this.ips = new HashSet<>(ips);
sdn5e935452016-08-30 04:12:54 -0700124 this.configured = configured;
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700125 this.innerVlan = innerVlan;
126 this.tpid = tpid;
jobind19b7142019-05-17 09:46:52 -0400127 this.suspended = false;
128 }
129
130 /**
131 * Creates an end-station host using the supplied information.
132 *
133 * @param providerId provider identity
134 * @param id host identifier
135 * @param mac host MAC address
136 * @param vlan host VLAN identifier
137 * @param locations set of host locations
138 * @param ips host IP addresses
139 * @param configured true if configured via NetworkConfiguration
140 * @param innerVlan host inner VLAN identifier
141 * @param tpid outer TPID of a host
142 * @param suspended true if the host is suspended due to policy violation.
143 * @param annotations optional key/value annotations
144 */
145 public DefaultHost(ProviderId providerId, HostId id, MacAddress mac,
146 VlanId vlan, Set<HostLocation> locations, Set<IpAddress> ips, VlanId innerVlan,
147 EthType tpid, boolean configured, boolean suspended, Annotations... annotations) {
148 super(providerId, id, annotations);
149 this.mac = mac;
150 this.vlan = vlan;
151 this.locations = new HashSet<>(locations);
152 this.ips = new HashSet<>(ips);
153 this.configured = configured;
154 this.innerVlan = innerVlan;
155 this.tpid = tpid;
156 this.suspended = suspended;
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700157 }
158
159 @Override
160 public HostId id() {
tom5a9383a2014-10-02 07:33:52 -0700161 return (HostId) id;
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700162 }
163
164 @Override
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700165 public MacAddress mac() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700166 return mac;
167 }
168
Saurav Das018605f2017-02-18 14:05:44 -0800169 /**
170 * Returns an unmodifiable set of IP addresses currently bound to the
171 * host MAC address.
172 */
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700173 @Override
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700174 public Set<IpAddress> ipAddresses() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700175 return Collections.unmodifiableSet(ips);
176 }
177
178 @Override
179 public HostLocation location() {
Charles Chancd06c692017-04-27 20:46:06 -0700180 return locations.stream()
181 .sorted(Comparator.comparingLong(HostLocation::time).reversed())
182 .findFirst().orElse(null);
183 }
184
185 @Override
186 public Set<HostLocation> locations() {
187 return locations;
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700188 }
189
190 @Override
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700191 public VlanId vlan() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700192 return vlan;
193 }
194
195 @Override
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700196 public VlanId innerVlan() {
197 return innerVlan;
198 }
199
200 @Override
201 public EthType tpid() {
202 return tpid;
203 }
204
205 @Override
sdn5e935452016-08-30 04:12:54 -0700206 public boolean configured() {
207 return configured;
208 }
209
210 @Override
jobind19b7142019-05-17 09:46:52 -0400211 public boolean suspended() {
212 return this.suspended;
213 }
214
215
216 @Override
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700217 public int hashCode() {
jobind19b7142019-05-17 09:46:52 -0400218 return Objects.hash(id, mac, vlan, locations, suspended);
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700219 }
220
221 @Override
222 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -0700223 if (this == obj) {
224 return true;
225 }
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700226 if (obj instanceof DefaultHost) {
227 final DefaultHost other = (DefaultHost) obj;
228 return Objects.equals(this.id, other.id) &&
229 Objects.equals(this.mac, other.mac) &&
230 Objects.equals(this.vlan, other.vlan) &&
Charles Chancd06c692017-04-27 20:46:06 -0700231 Objects.equals(this.locations, other.locations) &&
Charles Chanf4f1f4b2016-02-22 11:12:53 -0800232 Objects.equals(this.ipAddresses(), other.ipAddresses()) &&
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700233 Objects.equals(this.innerVlan, other.innerVlan) &&
234 Objects.equals(this.tpid, other.tpid) &&
jobind19b7142019-05-17 09:46:52 -0400235 Objects.equals(this.annotations(), other.annotations()) &&
236 Objects.equals(this.suspended, other.suspended);
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700237 }
238 return false;
239 }
240
241 @Override
242 public String toString() {
243 return toStringHelper(this)
Charles Chanf4f1f4b2016-02-22 11:12:53 -0800244 .add("id", id())
245 .add("mac", mac())
246 .add("vlan", vlan())
Charles Chancd06c692017-04-27 20:46:06 -0700247 .add("locations", locations())
Charles Chanf4f1f4b2016-02-22 11:12:53 -0800248 .add("ipAddresses", ipAddresses())
249 .add("annotations", annotations())
sdn5e935452016-08-30 04:12:54 -0700250 .add("configured", configured())
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700251 .add("innerVlanId", innerVlan())
252 .add("outerTPID", tpid())
jobind19b7142019-05-17 09:46:52 -0400253 .add("suspended", suspended())
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700254 .toString();
255 }
256
257}