Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package org.onosproject.dhcprelay.store; |
| 18 | |
| 19 | import com.google.common.base.MoreObjects; |
| 20 | import com.google.common.collect.Sets; |
| 21 | import org.onlab.packet.DHCP; |
| 22 | import org.onlab.packet.DHCP6; |
| 23 | import org.onlab.packet.Ip4Address; |
| 24 | import org.onlab.packet.Ip6Address; |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 25 | import org.onlab.packet.IpPrefix; |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 26 | import org.onlab.packet.MacAddress; |
| 27 | import org.onlab.packet.VlanId; |
| 28 | import org.onosproject.net.HostId; |
| 29 | import org.onosproject.net.HostLocation; |
| 30 | |
| 31 | import java.util.Objects; |
| 32 | import java.util.Optional; |
| 33 | import java.util.Set; |
| 34 | |
| 35 | import static com.google.common.base.Preconditions.checkNotNull; |
| 36 | |
| 37 | /** |
| 38 | * A class to record DHCP from DHCP relay application. |
| 39 | */ |
| 40 | public class DhcpRecord { |
| 41 | private final Set<HostLocation> locations; |
| 42 | private final MacAddress macAddress; |
| 43 | private final VlanId vlanId; |
| 44 | private MacAddress nextHop; |
Charles Chan | d0dd700 | 2017-10-08 23:53:36 -0400 | [diff] [blame] | 45 | // 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 Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 48 | |
| 49 | private Ip4Address ip4Address; |
| 50 | private DHCP.MsgType ip4Status; |
| 51 | |
| 52 | private Ip6Address ip6Address; |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 53 | private IpPrefix pdPrefix; |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 54 | private DHCP6.MsgType ip6Status; |
| 55 | |
| 56 | private long lastSeen; |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 57 | private long lastIp6Update; |
| 58 | private long lastPdUpdate; |
| 59 | |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 60 | private boolean directlyConnected; |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 61 | private long addrPrefTime; |
| 62 | private long pdPrefTime; |
| 63 | private DhcpRelayCounters v6Counters; |
| 64 | |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 65 | |
| 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 Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 79 | this.v6Counters = new DhcpRelayCounters(); |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 80 | } |
| 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 Ginsburg | 431b6cb | 2018-05-16 08:02:06 +0300 | [diff] [blame^] | 98 | if (locations.contains(location)) { |
| 99 | locations.remove(location); |
| 100 | } |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 101 | 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 Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 175 | * 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 Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 195 | * 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 Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 214 | * 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 Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 292 | * 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 Chan | d0dd700 | 2017-10-08 23:53:36 -0400 | [diff] [blame] | 373 | * 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 Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 393 | * Gets dhcp relay counters. |
| 394 | * |
| 395 | * @return the counter object |
| 396 | */ |
| 397 | public DhcpRelayCounters getV6Counters() { |
| 398 | return v6Counters; |
| 399 | } |
| 400 | |
| 401 | /** |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 402 | * 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 Chan | 7f8d14d | 2017-11-01 13:18:47 -0700 | [diff] [blame] | 411 | newRecord.nextHopTemp = nextHopTemp; |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 412 | newRecord.ip4Address = ip4Address; |
| 413 | newRecord.ip4Status = ip4Status; |
| 414 | newRecord.ip6Address = ip6Address; |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 415 | newRecord.pdPrefix = pdPrefix; |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 416 | newRecord.ip6Status = ip6Status; |
| 417 | newRecord.lastSeen = lastSeen; |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 418 | newRecord.lastIp6Update = lastIp6Update; |
| 419 | newRecord.lastPdUpdate = lastPdUpdate; |
| 420 | newRecord.addrPrefTime = addrPrefTime; |
| 421 | newRecord.pdPrefTime = pdPrefTime; |
| 422 | newRecord.v6Counters = v6Counters; |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 423 | return newRecord; |
| 424 | } |
| 425 | |
| 426 | @Override |
| 427 | public int hashCode() { |
| 428 | return Objects.hash(locations, macAddress, vlanId, ip4Address, ip4Status, |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 429 | nextHop, nextHopTemp, ip6Address, pdPrefix, ip6Status, lastSeen, |
| 430 | lastIp6Update, lastPdUpdate, addrPrefTime, pdPrefTime, v6Counters); |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 431 | } |
| 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 Chan | 7f8d14d | 2017-11-01 13:18:47 -0700 | [diff] [blame] | 448 | Objects.equals(nextHopTemp, that.nextHopTemp) && |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 449 | Objects.equals(ip6Address, that.ip6Address) && |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 450 | Objects.equals(pdPrefix, that.pdPrefix) && |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 451 | Objects.equals(ip6Status, that.ip6Status) && |
| 452 | Objects.equals(lastSeen, that.lastSeen) && |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 453 | 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 Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 459 | } |
| 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 Chan | 7f8d14d | 2017-11-01 13:18:47 -0700 | [diff] [blame] | 470 | .add("nextHopTemp", nextHopTemp) |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 471 | .add("ip6Address", ip6Address) |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 472 | .add("pdPrefix", pdPrefix) |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 473 | .add("ip6State", ip6Status) |
| 474 | .add("lastSeen", lastSeen) |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 475 | .add("lastIp6Update", lastIp6Update) |
| 476 | .add("lastPdUpdate", lastPdUpdate) |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 477 | .add("directlyConnected", directlyConnected) |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 478 | .add("addPrefTime", addrPrefTime) |
| 479 | .add("pdPrefTime", pdPrefTime) |
| 480 | .add("v6Counters", v6Counters) |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 481 | .toString(); |
| 482 | } |
| 483 | } |