blob: e133f031f054adcf1d725c9e4fe806c082483a3b [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;
25import org.onlab.packet.MacAddress;
26import org.onlab.packet.VlanId;
27import org.onosproject.net.HostId;
28import org.onosproject.net.HostLocation;
29
30import java.util.Objects;
31import java.util.Optional;
32import java.util.Set;
33
34import static com.google.common.base.Preconditions.checkNotNull;
35
36/**
37 * A class to record DHCP from DHCP relay application.
38 */
39public class DhcpRecord {
40 private final Set<HostLocation> locations;
41 private final MacAddress macAddress;
42 private final VlanId vlanId;
43 private MacAddress nextHop;
Charles Chand0dd7002017-10-08 23:53:36 -040044 // this will hold the potential next hop change in case
45 // of successfull LQ from another NH for a certain IP
46 private MacAddress nextHopTemp;
Yi Tsengb8e19f12017-06-07 15:47:23 -070047
48 private Ip4Address ip4Address;
49 private DHCP.MsgType ip4Status;
50
51 private Ip6Address ip6Address;
52 private DHCP6.MsgType ip6Status;
53
54 private long lastSeen;
55 private boolean directlyConnected;
56
57 /**
58 * Creates a DHCP record for a host (mac + vlan).
59 *
60 * @param hostId the host id for the host
61 */
62 public DhcpRecord(HostId hostId) {
63 checkNotNull(hostId, "Host id can't be null");
64
65 this.locations = Sets.newHashSet();
66 this.macAddress = hostId.mac();
67 this.vlanId = hostId.vlanId();
68 this.lastSeen = System.currentTimeMillis();
69 this.directlyConnected = false;
70 }
71
72 /**
73 * Gets host locations.
74 *
75 * @return the locations of host
76 */
77 public Set<HostLocation> locations() {
78 return locations;
79 }
80
81 /**
82 * Adds a location to record.
83 *
84 * @param location the location
85 * @return the DHCP record
86 */
87 public DhcpRecord addLocation(HostLocation location) {
88 locations.add(location);
89 return this;
90 }
91
92 /**
93 * Removes a location from record.
94 *
95 * @param location the location
96 * @return the DHCP record
97 */
98 public DhcpRecord removeLocation(HostLocation location) {
99 locations.remove(location);
100 return this;
101 }
102
103 /**
104 * Gets host mac address of this record.
105 *
106 * @return the host mac address
107 */
108 public MacAddress macAddress() {
109 return macAddress;
110 }
111
112 /**
113 * Gets host vlan id of this record.
114 *
115 * @return the host id.
116 */
117 public VlanId vlanId() {
118 return vlanId;
119 }
120
121 /**
122 * Gets IPv4 address which assigned to the host.
123 *
124 * @return the IP address assigned to the host
125 */
126 public Optional<Ip4Address> ip4Address() {
127 return Optional.ofNullable(ip4Address);
128 }
129
130 /**
131 * Sets IPv4 address.
132 *
133 * @param ip4Address the IPv4 address
134 * @return the DHCP record
135 */
136 public DhcpRecord ip4Address(Ip4Address ip4Address) {
137 this.ip4Address = ip4Address;
138 return this;
139 }
140
141 /**
142 * Gets IPv6 address which assigned to the host.
143 *
144 * @return the IP address assigned to the host
145 */
146 public Optional<Ip6Address> ip6Address() {
147 return Optional.ofNullable(ip6Address);
148 }
149
150 /**
151 * Sets IPv6 address.
152 *
153 * @param ip6Address the IPv6 address
154 * @return the DHCP record
155 */
156 public DhcpRecord ip6Address(Ip6Address ip6Address) {
157 this.ip6Address = ip6Address;
158 return this;
159 }
160
161 /**
162 * Gets the latest time this record updated.
163 *
164 * @return the last time host send or receive DHCP packet
165 */
166 public long lastSeen() {
167 return lastSeen;
168 }
169
170 /**
171 * Updates the update time of this record.
172 *
173 * @return the DHCP record
174 */
175 public DhcpRecord updateLastSeen() {
176 lastSeen = System.currentTimeMillis();
177 return this;
178 }
179
180 /**
181 * Indicated that the host is direct connected to the network or not.
182 *
183 * @return true if the host is directly connected to the network; false otherwise
184 */
185 public boolean directlyConnected() {
186 return directlyConnected;
187 }
188
189 /**
190 * Sets the flag which indicated that the host is directly connected to the
191 * network.
192 *
193 * @param directlyConnected the flag to set
194 * @return the DHCP record
195 */
196 public DhcpRecord setDirectlyConnected(boolean directlyConnected) {
197 this.directlyConnected = directlyConnected;
198 return this;
199 }
200
201 /**
202 * Gets the DHCPv4 status of this record.
203 *
204 * @return the DHCPv4 status; empty if not exists
205 */
206 public Optional<DHCP.MsgType> ip4Status() {
207 return Optional.ofNullable(ip4Status);
208 }
209
210 /**
211 * Sets status of DHCPv4.
212 *
213 * @param ip4Status the status
214 * @return the DHCP record
215 */
216 public DhcpRecord ip4Status(DHCP.MsgType ip4Status) {
217 this.ip4Status = ip4Status;
218 return this;
219 }
220
221 /**
222 * Gets the DHCPv6 status of this record.
223 *
224 * @return the DHCPv6 status; empty if not exists
225 */
226 public Optional<DHCP6.MsgType> ip6Status() {
227 return Optional.ofNullable(ip6Status);
228 }
229
230 /**
231 * Sets status of DHCPv6.
232 *
233 * @param ip6Status the DHCPv6 status
234 * @return the DHCP record
235 */
236 public DhcpRecord ip6Status(DHCP6.MsgType ip6Status) {
237 this.ip6Status = ip6Status;
238 return this;
239 }
240
241 /**
242 * Gets nextHop mac address.
243 *
244 * @return the IPv4 nextHop mac address; empty if not exists
245 */
246 public Optional<MacAddress> nextHop() {
247 return Optional.ofNullable(nextHop);
248 }
249
250 /**
251 * Sets nextHop mac address.
252 *
253 * @param nextHop the IPv4 nextHop mac address
254 * @return the DHCP record
255 */
256 public DhcpRecord nextHop(MacAddress nextHop) {
257 this.nextHop = nextHop;
258 return this;
259 }
260
261 /**
Charles Chand0dd7002017-10-08 23:53:36 -0400262 * Gets temporary nextHop mac address.
263 *
264 * @return the IPv4 nextHop mac address; empty if not exists
265 */
266 public Optional<MacAddress> nextHopTemp() {
267 return Optional.ofNullable(nextHopTemp);
268 }
269
270 /**
271 * Sets temporary nextHop mac address.
272 *
273 * @param nextHop the IPv4 nextHop mac address
274 * @return the DHCP record
275 */
276 public DhcpRecord nextHopTemp(MacAddress nextHop) {
277 this.nextHopTemp = nextHop;
278 return this;
279 }
280
281 /**
Yi Tsengb8e19f12017-06-07 15:47:23 -0700282 * Clone this DHCP record.
283 *
284 * @return the DHCP record which cloned
285 */
286 public DhcpRecord clone() {
287 DhcpRecord newRecord = new DhcpRecord(HostId.hostId(macAddress, vlanId));
288 locations.forEach(newRecord::addLocation);
289 newRecord.directlyConnected = directlyConnected;
290 newRecord.nextHop = nextHop;
Charles Chan7f8d14d2017-11-01 13:18:47 -0700291 newRecord.nextHopTemp = nextHopTemp;
Yi Tsengb8e19f12017-06-07 15:47:23 -0700292 newRecord.ip4Address = ip4Address;
293 newRecord.ip4Status = ip4Status;
294 newRecord.ip6Address = ip6Address;
295 newRecord.ip6Status = ip6Status;
296 newRecord.lastSeen = lastSeen;
297 return newRecord;
298 }
299
300 @Override
301 public int hashCode() {
302 return Objects.hash(locations, macAddress, vlanId, ip4Address, ip4Status,
Charles Chan7f8d14d2017-11-01 13:18:47 -0700303 nextHop, nextHopTemp, ip6Address, ip6Status, lastSeen);
Yi Tsengb8e19f12017-06-07 15:47:23 -0700304 }
305
306 @Override
307 public boolean equals(Object obj) {
308 if (this == obj) {
309 return true;
310 }
311 if (!(obj instanceof DhcpRecord)) {
312 return false;
313 }
314 DhcpRecord that = (DhcpRecord) obj;
315 return Objects.equals(locations, that.locations) &&
316 Objects.equals(macAddress, that.macAddress) &&
317 Objects.equals(vlanId, that.vlanId) &&
318 Objects.equals(ip4Address, that.ip4Address) &&
319 Objects.equals(ip4Status, that.ip4Status) &&
320 Objects.equals(nextHop, that.nextHop) &&
Charles Chan7f8d14d2017-11-01 13:18:47 -0700321 Objects.equals(nextHopTemp, that.nextHopTemp) &&
Yi Tsengb8e19f12017-06-07 15:47:23 -0700322 Objects.equals(ip6Address, that.ip6Address) &&
323 Objects.equals(ip6Status, that.ip6Status) &&
324 Objects.equals(lastSeen, that.lastSeen) &&
325 Objects.equals(directlyConnected, that.directlyConnected);
326 }
327
328 @Override
329 public String toString() {
330 return MoreObjects.toStringHelper(getClass())
331 .add("locations", locations)
332 .add("macAddress", macAddress)
333 .add("vlanId", vlanId)
334 .add("ip4Address", ip4Address)
335 .add("ip4State", ip4Status)
336 .add("nextHop", nextHop)
Charles Chan7f8d14d2017-11-01 13:18:47 -0700337 .add("nextHopTemp", nextHopTemp)
Yi Tsengb8e19f12017-06-07 15:47:23 -0700338 .add("ip6Address", ip6Address)
339 .add("ip6State", ip6Status)
340 .add("lastSeen", lastSeen)
341 .add("directlyConnected", directlyConnected)
342 .toString();
343 }
344}