blob: f0beff9ac64822e20a256f1a7cc9c6a49ec8f131 [file] [log] [blame]
Yi Tsengb8e19f12017-06-07 15:47:23 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Yi Tsengb8e19f12017-06-07 15:47:23 -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 */
16
17package org.onosproject.dhcprelay.store;
18
19import com.google.common.base.MoreObjects;
20import com.google.common.collect.Sets;
21import org.onlab.packet.DHCP;
22import org.onlab.packet.DHCP6;
23import org.onlab.packet.Ip4Address;
24import org.onlab.packet.Ip6Address;
Kalhee Kimea4b6c22017-11-09 14:38:37 +000025import org.onlab.packet.IpPrefix;
Yi Tsengb8e19f12017-06-07 15:47:23 -070026import org.onlab.packet.MacAddress;
27import org.onlab.packet.VlanId;
28import org.onosproject.net.HostId;
29import org.onosproject.net.HostLocation;
30
31import java.util.Objects;
32import java.util.Optional;
33import java.util.Set;
34
35import static com.google.common.base.Preconditions.checkNotNull;
36
37/**
38 * A class to record DHCP from DHCP relay application.
39 */
40public class DhcpRecord {
41 private final Set<HostLocation> locations;
42 private final MacAddress macAddress;
43 private final VlanId vlanId;
44 private MacAddress nextHop;
Charles Chand0dd7002017-10-08 23:53:36 -040045 // this will hold the potential next hop change in case
46 // of successfull LQ from another NH for a certain IP
47 private MacAddress nextHopTemp;
Yi Tsengb8e19f12017-06-07 15:47:23 -070048
49 private Ip4Address ip4Address;
50 private DHCP.MsgType ip4Status;
51
52 private Ip6Address ip6Address;
Kalhee Kimea4b6c22017-11-09 14:38:37 +000053 private IpPrefix pdPrefix;
Yi Tsengb8e19f12017-06-07 15:47:23 -070054 private DHCP6.MsgType ip6Status;
55
56 private long lastSeen;
Kalhee Kimd94ceea2017-11-29 19:03:02 +000057 private long lastIp6Update;
58 private long lastPdUpdate;
59
Yi Tsengb8e19f12017-06-07 15:47:23 -070060 private boolean directlyConnected;
Kalhee Kimd94ceea2017-11-29 19:03:02 +000061 private long addrPrefTime;
62 private long pdPrefTime;
63 private DhcpRelayCounters v6Counters;
64
Yi Tsengb8e19f12017-06-07 15:47:23 -070065
66 /**
67 * Creates a DHCP record for a host (mac + vlan).
68 *
69 * @param hostId the host id for the host
70 */
71 public DhcpRecord(HostId hostId) {
72 checkNotNull(hostId, "Host id can't be null");
73
74 this.locations = Sets.newHashSet();
75 this.macAddress = hostId.mac();
76 this.vlanId = hostId.vlanId();
77 this.lastSeen = System.currentTimeMillis();
78 this.directlyConnected = false;
Kalhee Kimd94ceea2017-11-29 19:03:02 +000079 this.v6Counters = new DhcpRelayCounters();
Yi Tsengb8e19f12017-06-07 15:47:23 -070080 }
81
82 /**
83 * Gets host locations.
84 *
85 * @return the locations of host
86 */
87 public Set<HostLocation> locations() {
88 return locations;
89 }
90
91 /**
92 * Adds a location to record.
93 *
94 * @param location the location
95 * @return the DHCP record
96 */
97 public DhcpRecord addLocation(HostLocation location) {
Daniel Ginsburg431b6cb2018-05-16 08:02:06 +030098 if (locations.contains(location)) {
99 locations.remove(location);
100 }
Yi Tsengb8e19f12017-06-07 15:47:23 -0700101 locations.add(location);
102 return this;
103 }
104
105 /**
106 * Removes a location from record.
107 *
108 * @param location the location
109 * @return the DHCP record
110 */
111 public DhcpRecord removeLocation(HostLocation location) {
112 locations.remove(location);
113 return this;
114 }
115
116 /**
117 * Gets host mac address of this record.
118 *
119 * @return the host mac address
120 */
121 public MacAddress macAddress() {
122 return macAddress;
123 }
124
125 /**
126 * Gets host vlan id of this record.
127 *
128 * @return the host id.
129 */
130 public VlanId vlanId() {
131 return vlanId;
132 }
133
134 /**
135 * Gets IPv4 address which assigned to the host.
136 *
137 * @return the IP address assigned to the host
138 */
139 public Optional<Ip4Address> ip4Address() {
140 return Optional.ofNullable(ip4Address);
141 }
142
143 /**
144 * Sets IPv4 address.
145 *
146 * @param ip4Address the IPv4 address
147 * @return the DHCP record
148 */
149 public DhcpRecord ip4Address(Ip4Address ip4Address) {
150 this.ip4Address = ip4Address;
151 return this;
152 }
153
154 /**
155 * Gets IPv6 address which assigned to the host.
156 *
157 * @return the IP address assigned to the host
158 */
159 public Optional<Ip6Address> ip6Address() {
160 return Optional.ofNullable(ip6Address);
161 }
162
163 /**
164 * Sets IPv6 address.
165 *
166 * @param ip6Address the IPv6 address
167 * @return the DHCP record
168 */
169 public DhcpRecord ip6Address(Ip6Address ip6Address) {
170 this.ip6Address = ip6Address;
171 return this;
172 }
173
174 /**
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000175 * Gets IPv6 PD address which assigned to the host.
176 *
177 * @return the PD IP address assigned to the host
178 */
179 public Optional<IpPrefix> pdPrefix() {
180 return Optional.ofNullable(pdPrefix);
181 }
182
183 /**
184 * Sets IPv6 PD address.
185 *
186 * @param pdPrefix the IPv6 PD address
187 * @return the DHCP record
188 */
189 public DhcpRecord pdPrefix(IpPrefix pdPrefix) {
190 this.pdPrefix = pdPrefix;
191 return this;
192 }
193
194 /**
Yi Tsengb8e19f12017-06-07 15:47:23 -0700195 * Gets the latest time this record updated.
196 *
197 * @return the last time host send or receive DHCP packet
198 */
199 public long lastSeen() {
200 return lastSeen;
201 }
202
203 /**
204 * Updates the update time of this record.
205 *
206 * @return the DHCP record
207 */
208 public DhcpRecord updateLastSeen() {
209 lastSeen = System.currentTimeMillis();
210 return this;
211 }
212
213 /**
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000214 * Gets the latest time this record updated with ip6 Address.
215 *
216 * @return the last time received DHCP packet provide ip6 Address
217 */
218 public long getLastIp6Update() {
219 return lastIp6Update;
220 }
221
222 /**
223 * Updates the update time of this record is given ip6 Address.
224 *
225 * @return the DHCP record
226 */
227 public DhcpRecord updateLastIp6Update() {
228 lastIp6Update = System.currentTimeMillis();
229 return this;
230 }
231
232 /**
233 * Gets the latest time this record updated with pd Prefix.
234 *
235 * @return the last time received DHCP packet provide pd Prefix
236 */
237 public long getLastPdUpdate() {
238 return lastPdUpdate;
239 }
240
241 /**
242 * Updates the update time of this record is given pd Prefix.
243 *
244 * @return the DHCP record
245 */
246 public DhcpRecord updateLastPdUpdate() {
247 lastPdUpdate = System.currentTimeMillis();
248 return this;
249 }
250
251 /**
252 * Gets the IP Address preferred time for this record.
253 *
254 * @return the preferred lease time for this ip address
255 */
256 public long addrPrefTime() {
257 return addrPrefTime;
258 }
259
260 /**
261 * Updates the IP Address preferred time of this record.
262 *
263 * @param prefTime preferred liftme
264 * @return the DHCP record
265 */
266 public DhcpRecord updateAddrPrefTime(long prefTime) {
267 addrPrefTime = prefTime;
268 return this;
269 }
270
271 /**
272 * Gets the PD Prefix preferred time for this record.
273 *
274 * @return the preferred lease time for this PD prefix
275 */
276 public long pdPrefTime() {
277 return pdPrefTime;
278 }
279
280 /**
281 * Updates the PD Prefix preferred time of this record.
282 *
283 * @param prefTime preferred liftme
284 * @return the DHCP record
285 */
286 public DhcpRecord updatePdPrefTime(long prefTime) {
287 pdPrefTime = prefTime;
288 return this;
289 }
290
291 /**
Yi Tsengb8e19f12017-06-07 15:47:23 -0700292 * Indicated that the host is direct connected to the network or not.
293 *
294 * @return true if the host is directly connected to the network; false otherwise
295 */
296 public boolean directlyConnected() {
297 return directlyConnected;
298 }
299
300 /**
301 * Sets the flag which indicated that the host is directly connected to the
302 * network.
303 *
304 * @param directlyConnected the flag to set
305 * @return the DHCP record
306 */
307 public DhcpRecord setDirectlyConnected(boolean directlyConnected) {
308 this.directlyConnected = directlyConnected;
309 return this;
310 }
311
312 /**
313 * Gets the DHCPv4 status of this record.
314 *
315 * @return the DHCPv4 status; empty if not exists
316 */
317 public Optional<DHCP.MsgType> ip4Status() {
318 return Optional.ofNullable(ip4Status);
319 }
320
321 /**
322 * Sets status of DHCPv4.
323 *
324 * @param ip4Status the status
325 * @return the DHCP record
326 */
327 public DhcpRecord ip4Status(DHCP.MsgType ip4Status) {
328 this.ip4Status = ip4Status;
329 return this;
330 }
331
332 /**
333 * Gets the DHCPv6 status of this record.
334 *
335 * @return the DHCPv6 status; empty if not exists
336 */
337 public Optional<DHCP6.MsgType> ip6Status() {
338 return Optional.ofNullable(ip6Status);
339 }
340
341 /**
342 * Sets status of DHCPv6.
343 *
344 * @param ip6Status the DHCPv6 status
345 * @return the DHCP record
346 */
347 public DhcpRecord ip6Status(DHCP6.MsgType ip6Status) {
348 this.ip6Status = ip6Status;
349 return this;
350 }
351
352 /**
353 * Gets nextHop mac address.
354 *
355 * @return the IPv4 nextHop mac address; empty if not exists
356 */
357 public Optional<MacAddress> nextHop() {
358 return Optional.ofNullable(nextHop);
359 }
360
361 /**
362 * Sets nextHop mac address.
363 *
364 * @param nextHop the IPv4 nextHop mac address
365 * @return the DHCP record
366 */
367 public DhcpRecord nextHop(MacAddress nextHop) {
368 this.nextHop = nextHop;
369 return this;
370 }
371
372 /**
Charles Chand0dd7002017-10-08 23:53:36 -0400373 * Gets temporary nextHop mac address.
374 *
375 * @return the IPv4 nextHop mac address; empty if not exists
376 */
377 public Optional<MacAddress> nextHopTemp() {
378 return Optional.ofNullable(nextHopTemp);
379 }
380
381 /**
382 * Sets temporary nextHop mac address.
383 *
384 * @param nextHop the IPv4 nextHop mac address
385 * @return the DHCP record
386 */
387 public DhcpRecord nextHopTemp(MacAddress nextHop) {
388 this.nextHopTemp = nextHop;
389 return this;
390 }
391
392 /**
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000393 * Gets dhcp relay counters.
394 *
395 * @return the counter object
396 */
397 public DhcpRelayCounters getV6Counters() {
398 return v6Counters;
399 }
400
401 /**
Yi Tsengb8e19f12017-06-07 15:47:23 -0700402 * Clone this DHCP record.
403 *
404 * @return the DHCP record which cloned
405 */
406 public DhcpRecord clone() {
407 DhcpRecord newRecord = new DhcpRecord(HostId.hostId(macAddress, vlanId));
408 locations.forEach(newRecord::addLocation);
409 newRecord.directlyConnected = directlyConnected;
410 newRecord.nextHop = nextHop;
Charles Chan7f8d14d2017-11-01 13:18:47 -0700411 newRecord.nextHopTemp = nextHopTemp;
Yi Tsengb8e19f12017-06-07 15:47:23 -0700412 newRecord.ip4Address = ip4Address;
413 newRecord.ip4Status = ip4Status;
414 newRecord.ip6Address = ip6Address;
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000415 newRecord.pdPrefix = pdPrefix;
Yi Tsengb8e19f12017-06-07 15:47:23 -0700416 newRecord.ip6Status = ip6Status;
417 newRecord.lastSeen = lastSeen;
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000418 newRecord.lastIp6Update = lastIp6Update;
419 newRecord.lastPdUpdate = lastPdUpdate;
420 newRecord.addrPrefTime = addrPrefTime;
421 newRecord.pdPrefTime = pdPrefTime;
422 newRecord.v6Counters = v6Counters;
Yi Tsengb8e19f12017-06-07 15:47:23 -0700423 return newRecord;
424 }
425
426 @Override
427 public int hashCode() {
428 return Objects.hash(locations, macAddress, vlanId, ip4Address, ip4Status,
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000429 nextHop, nextHopTemp, ip6Address, pdPrefix, ip6Status, lastSeen,
430 lastIp6Update, lastPdUpdate, addrPrefTime, pdPrefTime, v6Counters);
Yi Tsengb8e19f12017-06-07 15:47:23 -0700431 }
432
433 @Override
434 public boolean equals(Object obj) {
435 if (this == obj) {
436 return true;
437 }
438 if (!(obj instanceof DhcpRecord)) {
439 return false;
440 }
441 DhcpRecord that = (DhcpRecord) obj;
442 return Objects.equals(locations, that.locations) &&
443 Objects.equals(macAddress, that.macAddress) &&
444 Objects.equals(vlanId, that.vlanId) &&
445 Objects.equals(ip4Address, that.ip4Address) &&
446 Objects.equals(ip4Status, that.ip4Status) &&
447 Objects.equals(nextHop, that.nextHop) &&
Charles Chan7f8d14d2017-11-01 13:18:47 -0700448 Objects.equals(nextHopTemp, that.nextHopTemp) &&
Yi Tsengb8e19f12017-06-07 15:47:23 -0700449 Objects.equals(ip6Address, that.ip6Address) &&
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000450 Objects.equals(pdPrefix, that.pdPrefix) &&
Yi Tsengb8e19f12017-06-07 15:47:23 -0700451 Objects.equals(ip6Status, that.ip6Status) &&
452 Objects.equals(lastSeen, that.lastSeen) &&
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000453 Objects.equals(lastIp6Update, that.lastIp6Update) &&
454 Objects.equals(lastPdUpdate, that.lastPdUpdate) &&
455 Objects.equals(directlyConnected, that.directlyConnected) &&
456 Objects.equals(addrPrefTime, that.addrPrefTime) &&
457 Objects.equals(pdPrefTime, that.pdPrefTime) &&
458 Objects.equals(v6Counters, that.v6Counters);
Yi Tsengb8e19f12017-06-07 15:47:23 -0700459 }
460
461 @Override
462 public String toString() {
463 return MoreObjects.toStringHelper(getClass())
464 .add("locations", locations)
465 .add("macAddress", macAddress)
466 .add("vlanId", vlanId)
467 .add("ip4Address", ip4Address)
468 .add("ip4State", ip4Status)
469 .add("nextHop", nextHop)
Charles Chan7f8d14d2017-11-01 13:18:47 -0700470 .add("nextHopTemp", nextHopTemp)
Yi Tsengb8e19f12017-06-07 15:47:23 -0700471 .add("ip6Address", ip6Address)
Kalhee Kimea4b6c22017-11-09 14:38:37 +0000472 .add("pdPrefix", pdPrefix)
Yi Tsengb8e19f12017-06-07 15:47:23 -0700473 .add("ip6State", ip6Status)
474 .add("lastSeen", lastSeen)
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000475 .add("lastIp6Update", lastIp6Update)
476 .add("lastPdUpdate", lastPdUpdate)
Yi Tsengb8e19f12017-06-07 15:47:23 -0700477 .add("directlyConnected", directlyConnected)
Kalhee Kimd94ceea2017-11-29 19:03:02 +0000478 .add("addPrefTime", addrPrefTime)
479 .add("pdPrefTime", pdPrefTime)
480 .add("v6Counters", v6Counters)
Yi Tsengb8e19f12017-06-07 15:47:23 -0700481 .toString();
482 }
483}