blob: c2531f61a167de7857a125cc2d40083137f01e14 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
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
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.net.AbstractDescription;
24import org.onosproject.net.HostLocation;
25import org.onosproject.net.SparseAnnotations;
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070026import org.onlab.packet.IpAddress;
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070027import org.onlab.packet.MacAddress;
28import org.onlab.packet.VlanId;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070029
Yuta HIGUCHI5fa3dc02014-10-15 17:08:13 -070030import com.google.common.collect.ImmutableSet;
31
tom27ae0e62014-10-01 20:35:01 -070032import static com.google.common.base.MoreObjects.toStringHelper;
HIGUCHI Yuta703a5af2015-11-18 23:43:50 -080033import com.google.common.base.Objects;
tom27ae0e62014-10-01 20:35:01 -070034
35/**
36 * Default implementation of an immutable host description.
37 */
tomf5d85d42014-10-02 05:27:56 -070038public class DefaultHostDescription extends AbstractDescription
tom27ae0e62014-10-01 20:35:01 -070039 implements HostDescription {
Ayaka Koshibe74a23922014-09-09 16:45:39 -070040
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070041 private final MacAddress mac;
42 private final VlanId vlan;
Charles Chancd06c692017-04-27 20:46:06 -070043 private final Set<HostLocation> locations;
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070044 private final Set<IpAddress> ip;
sdn5e935452016-08-30 04:12:54 -070045 private final boolean configured;
Ayaka Koshibe74a23922014-09-09 16:45:39 -070046
tom27ae0e62014-10-01 20:35:01 -070047 /**
48 * Creates a host description using the supplied information.
49 *
50 * @param mac host MAC address
51 * @param vlan host VLAN identifier
52 * @param location host location
53 * @param annotations optional key/value annotations map
54 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070055 public DefaultHostDescription(MacAddress mac, VlanId vlan,
tom27ae0e62014-10-01 20:35:01 -070056 HostLocation location,
tomf5d85d42014-10-02 05:27:56 -070057 SparseAnnotations... annotations) {
Charles Chancd06c692017-04-27 20:46:06 -070058 this(mac, vlan, location, Collections.emptySet(), annotations);
Ayaka Koshibe1a100982014-09-13 19:32:19 -070059 }
60
tom27ae0e62014-10-01 20:35:01 -070061 /**
62 * Creates a host description using the supplied information.
63 *
64 * @param mac host MAC address
65 * @param vlan host VLAN identifier
66 * @param location host location
tom093340b2014-10-10 00:15:36 -070067 * @param ip host IP address
tom27ae0e62014-10-01 20:35:01 -070068 * @param annotations optional key/value annotations map
69 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070070 public DefaultHostDescription(MacAddress mac, VlanId vlan,
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070071 HostLocation location, IpAddress ip,
tomf5d85d42014-10-02 05:27:56 -070072 SparseAnnotations... annotations) {
Yuta HIGUCHI5fa3dc02014-10-15 17:08:13 -070073 this(mac, vlan, location, ImmutableSet.of(ip), annotations);
74 }
75
76 /**
77 * Creates a host description using the supplied information.
78 *
79 * @param mac host MAC address
80 * @param vlan host VLAN identifier
81 * @param location host location
82 * @param ip host IP addresses
83 * @param annotations optional key/value annotations map
84 */
85 public DefaultHostDescription(MacAddress mac, VlanId vlan,
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070086 HostLocation location, Set<IpAddress> ip,
Yuta HIGUCHI5fa3dc02014-10-15 17:08:13 -070087 SparseAnnotations... annotations) {
sdn5e935452016-08-30 04:12:54 -070088 this(mac, vlan, location, ip, false, annotations);
89 }
90
91 /**
92 * Creates a host description using the supplied information.
93 *
94 * @param mac host MAC address
95 * @param vlan host VLAN identifier
96 * @param location host location
97 * @param configured true if configured via NetworkConfiguration
98 * @param annotations optional key/value annotations map
99 */
100 public DefaultHostDescription(MacAddress mac, VlanId vlan,
101 HostLocation location,
102 boolean configured,
103 SparseAnnotations... annotations) {
Charles Chancd06c692017-04-27 20:46:06 -0700104 this(mac, vlan, location, Collections.emptySet(), configured, annotations);
sdn5e935452016-08-30 04:12:54 -0700105 }
106
107 /**
108 * Creates a host description using the supplied information.
109 *
110 * @param mac host MAC address
111 * @param vlan host VLAN identifier
112 * @param location host location
113 * @param ip host IP address
114 * @param configured true if configured via NetworkConfiguration
115 * @param annotations optional key/value annotations map
116 */
117 public DefaultHostDescription(MacAddress mac, VlanId vlan,
118 HostLocation location, Set<IpAddress> ip,
119 boolean configured,
120 SparseAnnotations... annotations) {
Charles Chancd06c692017-04-27 20:46:06 -0700121 this(mac, vlan, Collections.singleton(location), ip, configured, annotations);
122 }
123
124 /**
125 * Creates a host description using the supplied information.
126 *
127 * @param mac host MAC address
128 * @param vlan host VLAN identifier
129 * @param locations host locations
130 * @param ip host IP address
131 * @param configured true if configured via NetworkConfiguration
132 * @param annotations optional key/value annotations map
133 */
134 public DefaultHostDescription(MacAddress mac, VlanId vlan,
135 Set<HostLocation> locations,
136 Set<IpAddress> ip, boolean configured,
137 SparseAnnotations... annotations) {
tom27ae0e62014-10-01 20:35:01 -0700138 super(annotations);
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700139 this.mac = mac;
140 this.vlan = vlan;
Charles Chancd06c692017-04-27 20:46:06 -0700141 this.locations = new HashSet<>(locations);
142 this.ip = new HashSet<>(ip);
sdn5e935452016-08-30 04:12:54 -0700143 this.configured = configured;
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700144 }
145
146 @Override
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700147 public MacAddress hwAddress() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700148 return mac;
149 }
150
151 @Override
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700152 public VlanId vlan() {
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700153 return vlan;
154 }
155
156 @Override
157 public HostLocation location() {
Charles Chancd06c692017-04-27 20:46:06 -0700158 return locations.stream()
159 .sorted(Comparator.comparingLong(HostLocation::time).reversed())
160 .findFirst().orElse(null);
161 }
162
163 @Override
164 public Set<HostLocation> locations() {
165 return locations;
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700166 }
167
168 @Override
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -0700169 public Set<IpAddress> ipAddress() {
tom093340b2014-10-10 00:15:36 -0700170 return ip;
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700171 }
172
173 @Override
sdn5e935452016-08-30 04:12:54 -0700174 public boolean configured() {
175 return configured;
176 }
177
178 @Override
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700179 public String toString() {
180 return toStringHelper(this)
181 .add("mac", mac)
182 .add("vlan", vlan)
Charles Chancd06c692017-04-27 20:46:06 -0700183 .add("locations", locations)
tom093340b2014-10-10 00:15:36 -0700184 .add("ipAddress", ip)
sdn5e935452016-08-30 04:12:54 -0700185 .add("configured", configured)
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700186 .toString();
187 }
188
HIGUCHI Yuta703a5af2015-11-18 23:43:50 -0800189 @Override
190 public int hashCode() {
Charles Chancd06c692017-04-27 20:46:06 -0700191 return Objects.hashCode(super.hashCode(), mac, vlan, locations, ip);
HIGUCHI Yuta703a5af2015-11-18 23:43:50 -0800192 }
193
194 @Override
195 public boolean equals(Object object) {
196 if (object != null && getClass() == object.getClass()) {
197 if (!super.equals(object)) {
198 return false;
199 }
200 DefaultHostDescription that = (DefaultHostDescription) object;
201 return Objects.equal(this.mac, that.mac)
202 && Objects.equal(this.vlan, that.vlan)
Charles Chancd06c692017-04-27 20:46:06 -0700203 && Objects.equal(this.locations, that.locations)
HIGUCHI Yuta703a5af2015-11-18 23:43:50 -0800204 && Objects.equal(this.ip, that.ip);
205 }
206 return false;
207 }
Ayaka Koshibe74a23922014-09-09 16:45:39 -0700208}