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) { |
| 98 | locations.add(location); |
| 99 | return this; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Removes a location from record. |
| 104 | * |
| 105 | * @param location the location |
| 106 | * @return the DHCP record |
| 107 | */ |
| 108 | public DhcpRecord removeLocation(HostLocation location) { |
| 109 | locations.remove(location); |
| 110 | return this; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Gets host mac address of this record. |
| 115 | * |
| 116 | * @return the host mac address |
| 117 | */ |
| 118 | public MacAddress macAddress() { |
| 119 | return macAddress; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Gets host vlan id of this record. |
| 124 | * |
| 125 | * @return the host id. |
| 126 | */ |
| 127 | public VlanId vlanId() { |
| 128 | return vlanId; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Gets IPv4 address which assigned to the host. |
| 133 | * |
| 134 | * @return the IP address assigned to the host |
| 135 | */ |
| 136 | public Optional<Ip4Address> ip4Address() { |
| 137 | return Optional.ofNullable(ip4Address); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Sets IPv4 address. |
| 142 | * |
| 143 | * @param ip4Address the IPv4 address |
| 144 | * @return the DHCP record |
| 145 | */ |
| 146 | public DhcpRecord ip4Address(Ip4Address ip4Address) { |
| 147 | this.ip4Address = ip4Address; |
| 148 | return this; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Gets IPv6 address which assigned to the host. |
| 153 | * |
| 154 | * @return the IP address assigned to the host |
| 155 | */ |
| 156 | public Optional<Ip6Address> ip6Address() { |
| 157 | return Optional.ofNullable(ip6Address); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Sets IPv6 address. |
| 162 | * |
| 163 | * @param ip6Address the IPv6 address |
| 164 | * @return the DHCP record |
| 165 | */ |
| 166 | public DhcpRecord ip6Address(Ip6Address ip6Address) { |
| 167 | this.ip6Address = ip6Address; |
| 168 | return this; |
| 169 | } |
| 170 | |
| 171 | /** |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 172 | * Gets IPv6 PD address which assigned to the host. |
| 173 | * |
| 174 | * @return the PD IP address assigned to the host |
| 175 | */ |
| 176 | public Optional<IpPrefix> pdPrefix() { |
| 177 | return Optional.ofNullable(pdPrefix); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Sets IPv6 PD address. |
| 182 | * |
| 183 | * @param pdPrefix the IPv6 PD address |
| 184 | * @return the DHCP record |
| 185 | */ |
| 186 | public DhcpRecord pdPrefix(IpPrefix pdPrefix) { |
| 187 | this.pdPrefix = pdPrefix; |
| 188 | return this; |
| 189 | } |
| 190 | |
| 191 | /** |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 192 | * Gets the latest time this record updated. |
| 193 | * |
| 194 | * @return the last time host send or receive DHCP packet |
| 195 | */ |
| 196 | public long lastSeen() { |
| 197 | return lastSeen; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Updates the update time of this record. |
| 202 | * |
| 203 | * @return the DHCP record |
| 204 | */ |
| 205 | public DhcpRecord updateLastSeen() { |
| 206 | lastSeen = System.currentTimeMillis(); |
| 207 | return this; |
| 208 | } |
| 209 | |
| 210 | /** |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 211 | * Gets the latest time this record updated with ip6 Address. |
| 212 | * |
| 213 | * @return the last time received DHCP packet provide ip6 Address |
| 214 | */ |
| 215 | public long getLastIp6Update() { |
| 216 | return lastIp6Update; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Updates the update time of this record is given ip6 Address. |
| 221 | * |
| 222 | * @return the DHCP record |
| 223 | */ |
| 224 | public DhcpRecord updateLastIp6Update() { |
| 225 | lastIp6Update = System.currentTimeMillis(); |
| 226 | return this; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Gets the latest time this record updated with pd Prefix. |
| 231 | * |
| 232 | * @return the last time received DHCP packet provide pd Prefix |
| 233 | */ |
| 234 | public long getLastPdUpdate() { |
| 235 | return lastPdUpdate; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Updates the update time of this record is given pd Prefix. |
| 240 | * |
| 241 | * @return the DHCP record |
| 242 | */ |
| 243 | public DhcpRecord updateLastPdUpdate() { |
| 244 | lastPdUpdate = System.currentTimeMillis(); |
| 245 | return this; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Gets the IP Address preferred time for this record. |
| 250 | * |
| 251 | * @return the preferred lease time for this ip address |
| 252 | */ |
| 253 | public long addrPrefTime() { |
| 254 | return addrPrefTime; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Updates the IP Address preferred time of this record. |
| 259 | * |
| 260 | * @param prefTime preferred liftme |
| 261 | * @return the DHCP record |
| 262 | */ |
| 263 | public DhcpRecord updateAddrPrefTime(long prefTime) { |
| 264 | addrPrefTime = prefTime; |
| 265 | return this; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Gets the PD Prefix preferred time for this record. |
| 270 | * |
| 271 | * @return the preferred lease time for this PD prefix |
| 272 | */ |
| 273 | public long pdPrefTime() { |
| 274 | return pdPrefTime; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Updates the PD Prefix preferred time of this record. |
| 279 | * |
| 280 | * @param prefTime preferred liftme |
| 281 | * @return the DHCP record |
| 282 | */ |
| 283 | public DhcpRecord updatePdPrefTime(long prefTime) { |
| 284 | pdPrefTime = prefTime; |
| 285 | return this; |
| 286 | } |
| 287 | |
| 288 | /** |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 289 | * Indicated that the host is direct connected to the network or not. |
| 290 | * |
| 291 | * @return true if the host is directly connected to the network; false otherwise |
| 292 | */ |
| 293 | public boolean directlyConnected() { |
| 294 | return directlyConnected; |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Sets the flag which indicated that the host is directly connected to the |
| 299 | * network. |
| 300 | * |
| 301 | * @param directlyConnected the flag to set |
| 302 | * @return the DHCP record |
| 303 | */ |
| 304 | public DhcpRecord setDirectlyConnected(boolean directlyConnected) { |
| 305 | this.directlyConnected = directlyConnected; |
| 306 | return this; |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Gets the DHCPv4 status of this record. |
| 311 | * |
| 312 | * @return the DHCPv4 status; empty if not exists |
| 313 | */ |
| 314 | public Optional<DHCP.MsgType> ip4Status() { |
| 315 | return Optional.ofNullable(ip4Status); |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Sets status of DHCPv4. |
| 320 | * |
| 321 | * @param ip4Status the status |
| 322 | * @return the DHCP record |
| 323 | */ |
| 324 | public DhcpRecord ip4Status(DHCP.MsgType ip4Status) { |
| 325 | this.ip4Status = ip4Status; |
| 326 | return this; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Gets the DHCPv6 status of this record. |
| 331 | * |
| 332 | * @return the DHCPv6 status; empty if not exists |
| 333 | */ |
| 334 | public Optional<DHCP6.MsgType> ip6Status() { |
| 335 | return Optional.ofNullable(ip6Status); |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Sets status of DHCPv6. |
| 340 | * |
| 341 | * @param ip6Status the DHCPv6 status |
| 342 | * @return the DHCP record |
| 343 | */ |
| 344 | public DhcpRecord ip6Status(DHCP6.MsgType ip6Status) { |
| 345 | this.ip6Status = ip6Status; |
| 346 | return this; |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Gets nextHop mac address. |
| 351 | * |
| 352 | * @return the IPv4 nextHop mac address; empty if not exists |
| 353 | */ |
| 354 | public Optional<MacAddress> nextHop() { |
| 355 | return Optional.ofNullable(nextHop); |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Sets nextHop mac address. |
| 360 | * |
| 361 | * @param nextHop the IPv4 nextHop mac address |
| 362 | * @return the DHCP record |
| 363 | */ |
| 364 | public DhcpRecord nextHop(MacAddress nextHop) { |
| 365 | this.nextHop = nextHop; |
| 366 | return this; |
| 367 | } |
| 368 | |
| 369 | /** |
Charles Chan | d0dd700 | 2017-10-08 23:53:36 -0400 | [diff] [blame] | 370 | * Gets temporary nextHop mac address. |
| 371 | * |
| 372 | * @return the IPv4 nextHop mac address; empty if not exists |
| 373 | */ |
| 374 | public Optional<MacAddress> nextHopTemp() { |
| 375 | return Optional.ofNullable(nextHopTemp); |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Sets temporary nextHop mac address. |
| 380 | * |
| 381 | * @param nextHop the IPv4 nextHop mac address |
| 382 | * @return the DHCP record |
| 383 | */ |
| 384 | public DhcpRecord nextHopTemp(MacAddress nextHop) { |
| 385 | this.nextHopTemp = nextHop; |
| 386 | return this; |
| 387 | } |
| 388 | |
| 389 | /** |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 390 | * Gets dhcp relay counters. |
| 391 | * |
| 392 | * @return the counter object |
| 393 | */ |
| 394 | public DhcpRelayCounters getV6Counters() { |
| 395 | return v6Counters; |
| 396 | } |
| 397 | |
| 398 | /** |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 399 | * Clone this DHCP record. |
| 400 | * |
| 401 | * @return the DHCP record which cloned |
| 402 | */ |
| 403 | public DhcpRecord clone() { |
| 404 | DhcpRecord newRecord = new DhcpRecord(HostId.hostId(macAddress, vlanId)); |
| 405 | locations.forEach(newRecord::addLocation); |
| 406 | newRecord.directlyConnected = directlyConnected; |
| 407 | newRecord.nextHop = nextHop; |
Charles Chan | 7f8d14d | 2017-11-01 13:18:47 -0700 | [diff] [blame] | 408 | newRecord.nextHopTemp = nextHopTemp; |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 409 | newRecord.ip4Address = ip4Address; |
| 410 | newRecord.ip4Status = ip4Status; |
| 411 | newRecord.ip6Address = ip6Address; |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 412 | newRecord.pdPrefix = pdPrefix; |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 413 | newRecord.ip6Status = ip6Status; |
| 414 | newRecord.lastSeen = lastSeen; |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 415 | newRecord.lastIp6Update = lastIp6Update; |
| 416 | newRecord.lastPdUpdate = lastPdUpdate; |
| 417 | newRecord.addrPrefTime = addrPrefTime; |
| 418 | newRecord.pdPrefTime = pdPrefTime; |
| 419 | newRecord.v6Counters = v6Counters; |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 420 | return newRecord; |
| 421 | } |
| 422 | |
| 423 | @Override |
| 424 | public int hashCode() { |
| 425 | return Objects.hash(locations, macAddress, vlanId, ip4Address, ip4Status, |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 426 | nextHop, nextHopTemp, ip6Address, pdPrefix, ip6Status, lastSeen, |
| 427 | lastIp6Update, lastPdUpdate, addrPrefTime, pdPrefTime, v6Counters); |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | @Override |
| 431 | public boolean equals(Object obj) { |
| 432 | if (this == obj) { |
| 433 | return true; |
| 434 | } |
| 435 | if (!(obj instanceof DhcpRecord)) { |
| 436 | return false; |
| 437 | } |
| 438 | DhcpRecord that = (DhcpRecord) obj; |
| 439 | return Objects.equals(locations, that.locations) && |
| 440 | Objects.equals(macAddress, that.macAddress) && |
| 441 | Objects.equals(vlanId, that.vlanId) && |
| 442 | Objects.equals(ip4Address, that.ip4Address) && |
| 443 | Objects.equals(ip4Status, that.ip4Status) && |
| 444 | Objects.equals(nextHop, that.nextHop) && |
Charles Chan | 7f8d14d | 2017-11-01 13:18:47 -0700 | [diff] [blame] | 445 | Objects.equals(nextHopTemp, that.nextHopTemp) && |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 446 | Objects.equals(ip6Address, that.ip6Address) && |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 447 | Objects.equals(pdPrefix, that.pdPrefix) && |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 448 | Objects.equals(ip6Status, that.ip6Status) && |
| 449 | Objects.equals(lastSeen, that.lastSeen) && |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 450 | Objects.equals(lastIp6Update, that.lastIp6Update) && |
| 451 | Objects.equals(lastPdUpdate, that.lastPdUpdate) && |
| 452 | Objects.equals(directlyConnected, that.directlyConnected) && |
| 453 | Objects.equals(addrPrefTime, that.addrPrefTime) && |
| 454 | Objects.equals(pdPrefTime, that.pdPrefTime) && |
| 455 | Objects.equals(v6Counters, that.v6Counters); |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | @Override |
| 459 | public String toString() { |
| 460 | return MoreObjects.toStringHelper(getClass()) |
| 461 | .add("locations", locations) |
| 462 | .add("macAddress", macAddress) |
| 463 | .add("vlanId", vlanId) |
| 464 | .add("ip4Address", ip4Address) |
| 465 | .add("ip4State", ip4Status) |
| 466 | .add("nextHop", nextHop) |
Charles Chan | 7f8d14d | 2017-11-01 13:18:47 -0700 | [diff] [blame] | 467 | .add("nextHopTemp", nextHopTemp) |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 468 | .add("ip6Address", ip6Address) |
Kalhee Kim | ea4b6c2 | 2017-11-09 14:38:37 +0000 | [diff] [blame] | 469 | .add("pdPrefix", pdPrefix) |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 470 | .add("ip6State", ip6Status) |
| 471 | .add("lastSeen", lastSeen) |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 472 | .add("lastIp6Update", lastIp6Update) |
| 473 | .add("lastPdUpdate", lastPdUpdate) |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 474 | .add("directlyConnected", directlyConnected) |
Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [diff] [blame] | 475 | .add("addPrefTime", addrPrefTime) |
| 476 | .add("pdPrefTime", pdPrefTime) |
| 477 | .add("v6Counters", v6Counters) |
Yi Tseng | b8e19f1 | 2017-06-07 15:47:23 -0700 | [diff] [blame] | 478 | .toString(); |
| 479 | } |
| 480 | } |