blob: ec0d9b5602bc40505e1d7d8e316b20efe35dbf3d [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;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070044
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -070045 // TODO consider moving this constructor to a builder pattern.
tomf5d85d42014-10-02 05:27:56 -070046 /**
47 * Creates an end-station host using the supplied information.
48 *
49 * @param providerId provider identity
50 * @param id host identifier
51 * @param mac host MAC address
52 * @param vlan host VLAN identifier
53 * @param location host location
54 * @param ips host IP addresses
55 * @param annotations optional key/value annotations
56 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070057 public DefaultHost(ProviderId providerId, HostId id, MacAddress mac,
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070058 VlanId vlan, HostLocation location, Set<IpAddress> ips,
tomf5d85d42014-10-02 05:27:56 -070059 Annotations... annotations) {
sdn5e935452016-08-30 04:12:54 -070060 this(providerId, id, mac, vlan, location, ips, false, annotations);
61 }
62
63 /**
64 * Creates an end-station host using the supplied information.
65 *
66 * @param providerId provider identity
67 * @param id host identifier
68 * @param mac host MAC address
69 * @param vlan host VLAN identifier
70 * @param location host location
71 * @param ips host IP addresses
72 * @param configured true if configured via NetworkConfiguration
73 * @param annotations optional key/value annotations
74 */
75 public DefaultHost(ProviderId providerId, HostId id, MacAddress mac,
76 VlanId vlan, HostLocation location, Set<IpAddress> ips,
77 boolean configured, Annotations... annotations) {
Charles Chancd06c692017-04-27 20:46:06 -070078 this(providerId, id, mac, vlan, Collections.singleton(location), ips,
79 configured, annotations);
80 }
81
82 /**
83 * Creates an end-station host using the supplied information.
84 *
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -070085 * @param providerId provider identity
86 * @param id host identifier
87 * @param mac host MAC address
88 * @param vlan host VLAN identifier
89 * @param locations set of host locations
90 * @param ips host IP addresses
Charles Chancd06c692017-04-27 20:46:06 -070091 * @param configured true if configured via NetworkConfiguration
92 * @param annotations optional key/value annotations
93 */
94 public DefaultHost(ProviderId providerId, HostId id, MacAddress mac,
95 VlanId vlan, Set<HostLocation> locations, Set<IpAddress> ips,
96 boolean configured, Annotations... annotations) {
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -070097 this(providerId, id, mac, vlan, locations, ips, VlanId.NONE,
98 EthType.EtherType.UNKNOWN.ethType(), configured, annotations);
99 }
100
101 /**
102 * Creates an end-station host using the supplied information.
103 *
104 * @param providerId provider identity
105 * @param id host identifier
106 * @param mac host MAC address
107 * @param vlan host VLAN identifier
108 * @param locations set of host locations
109 * @param ips host IP addresses
110 * @param innerVlan host inner VLAN identifier
111 * @param tpid outer TPID of a host
112 * @param configured true if configured via NetworkConfiguration
113 * @param annotations optional key/value annotations
114 */
115 public DefaultHost(ProviderId providerId, HostId id, MacAddress mac, VlanId vlan,
116 Set<HostLocation> locations, Set<IpAddress> ips, VlanId innerVlan,
117 EthType tpid, boolean configured, Annotations... annotations) {
tomf5d85d42014-10-02 05:27:56 -0700118 super(providerId, id, annotations);
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700119 this.mac = mac;
120 this.vlan = vlan;
Charles Chancd06c692017-04-27 20:46:06 -0700121 this.locations = new HashSet<>(locations);
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700122 this.ips = new HashSet<>(ips);
sdn5e935452016-08-30 04:12:54 -0700123 this.configured = configured;
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700124 this.innerVlan = innerVlan;
125 this.tpid = tpid;
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700126 }
127
128 @Override
129 public HostId id() {
tom5a9383a2014-10-02 07:33:52 -0700130 return (HostId) id;
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700131 }
132
133 @Override
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700134 public MacAddress mac() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700135 return mac;
136 }
137
Saurav Das018605f2017-02-18 14:05:44 -0800138 /**
139 * Returns an unmodifiable set of IP addresses currently bound to the
140 * host MAC address.
141 */
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700142 @Override
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700143 public Set<IpAddress> ipAddresses() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700144 return Collections.unmodifiableSet(ips);
145 }
146
147 @Override
148 public HostLocation location() {
Charles Chancd06c692017-04-27 20:46:06 -0700149 return locations.stream()
150 .sorted(Comparator.comparingLong(HostLocation::time).reversed())
151 .findFirst().orElse(null);
152 }
153
154 @Override
155 public Set<HostLocation> locations() {
156 return locations;
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700157 }
158
159 @Override
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700160 public VlanId vlan() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700161 return vlan;
162 }
163
164 @Override
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700165 public VlanId innerVlan() {
166 return innerVlan;
167 }
168
169 @Override
170 public EthType tpid() {
171 return tpid;
172 }
173
174 @Override
sdn5e935452016-08-30 04:12:54 -0700175 public boolean configured() {
176 return configured;
177 }
178
179 @Override
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700180 public int hashCode() {
Charles Chancd06c692017-04-27 20:46:06 -0700181 return Objects.hash(id, mac, vlan, locations);
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700182 }
183
184 @Override
185 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -0700186 if (this == obj) {
187 return true;
188 }
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700189 if (obj instanceof DefaultHost) {
190 final DefaultHost other = (DefaultHost) obj;
191 return Objects.equals(this.id, other.id) &&
192 Objects.equals(this.mac, other.mac) &&
193 Objects.equals(this.vlan, other.vlan) &&
Charles Chancd06c692017-04-27 20:46:06 -0700194 Objects.equals(this.locations, other.locations) &&
Charles Chanf4f1f4b2016-02-22 11:12:53 -0800195 Objects.equals(this.ipAddresses(), other.ipAddresses()) &&
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700196 Objects.equals(this.innerVlan, other.innerVlan) &&
197 Objects.equals(this.tpid, other.tpid) &&
Charles Chanf4f1f4b2016-02-22 11:12:53 -0800198 Objects.equals(this.annotations(), other.annotations());
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700199 }
200 return false;
201 }
202
203 @Override
204 public String toString() {
205 return toStringHelper(this)
Charles Chanf4f1f4b2016-02-22 11:12:53 -0800206 .add("id", id())
207 .add("mac", mac())
208 .add("vlan", vlan())
Charles Chancd06c692017-04-27 20:46:06 -0700209 .add("locations", locations())
Charles Chanf4f1f4b2016-02-22 11:12:53 -0800210 .add("ipAddresses", ipAddresses())
211 .add("annotations", annotations())
sdn5e935452016-08-30 04:12:54 -0700212 .add("configured", configured())
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700213 .add("innerVlanId", innerVlan())
214 .add("outerTPID", tpid())
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700215 .toString();
216 }
217
218}