blob: 51d8f98a7d1c69f19b47731712f5259cfaf666c9 [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 Chan2f9aa2c2017-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 Chan2f9aa2c2017-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;
291 newRecord.ip4Address = ip4Address;
292 newRecord.ip4Status = ip4Status;
293 newRecord.ip6Address = ip6Address;
294 newRecord.ip6Status = ip6Status;
295 newRecord.lastSeen = lastSeen;
296 return newRecord;
297 }
298
299 @Override
300 public int hashCode() {
301 return Objects.hash(locations, macAddress, vlanId, ip4Address, ip4Status,
302 nextHop, ip6Address, ip6Status, lastSeen);
303 }
304
305 @Override
306 public boolean equals(Object obj) {
307 if (this == obj) {
308 return true;
309 }
310 if (!(obj instanceof DhcpRecord)) {
311 return false;
312 }
313 DhcpRecord that = (DhcpRecord) obj;
314 return Objects.equals(locations, that.locations) &&
315 Objects.equals(macAddress, that.macAddress) &&
316 Objects.equals(vlanId, that.vlanId) &&
317 Objects.equals(ip4Address, that.ip4Address) &&
318 Objects.equals(ip4Status, that.ip4Status) &&
319 Objects.equals(nextHop, that.nextHop) &&
320 Objects.equals(ip6Address, that.ip6Address) &&
321 Objects.equals(ip6Status, that.ip6Status) &&
322 Objects.equals(lastSeen, that.lastSeen) &&
323 Objects.equals(directlyConnected, that.directlyConnected);
324 }
325
326 @Override
327 public String toString() {
328 return MoreObjects.toStringHelper(getClass())
329 .add("locations", locations)
330 .add("macAddress", macAddress)
331 .add("vlanId", vlanId)
332 .add("ip4Address", ip4Address)
333 .add("ip4State", ip4Status)
334 .add("nextHop", nextHop)
335 .add("ip6Address", ip6Address)
336 .add("ip6State", ip6Status)
337 .add("lastSeen", lastSeen)
338 .add("directlyConnected", directlyConnected)
339 .toString();
340 }
341}