blob: 029c2fc535ae320c1cd21e3194117251b6b6896c [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;
44
45 private Ip4Address ip4Address;
46 private DHCP.MsgType ip4Status;
47
48 private Ip6Address ip6Address;
49 private DHCP6.MsgType ip6Status;
50
51 private long lastSeen;
52 private boolean directlyConnected;
53
54 /**
55 * Creates a DHCP record for a host (mac + vlan).
56 *
57 * @param hostId the host id for the host
58 */
59 public DhcpRecord(HostId hostId) {
60 checkNotNull(hostId, "Host id can't be null");
61
62 this.locations = Sets.newHashSet();
63 this.macAddress = hostId.mac();
64 this.vlanId = hostId.vlanId();
65 this.lastSeen = System.currentTimeMillis();
66 this.directlyConnected = false;
67 }
68
69 /**
70 * Gets host locations.
71 *
72 * @return the locations of host
73 */
74 public Set<HostLocation> locations() {
75 return locations;
76 }
77
78 /**
79 * Adds a location to record.
80 *
81 * @param location the location
82 * @return the DHCP record
83 */
84 public DhcpRecord addLocation(HostLocation location) {
85 locations.add(location);
86 return this;
87 }
88
89 /**
90 * Removes a location from record.
91 *
92 * @param location the location
93 * @return the DHCP record
94 */
95 public DhcpRecord removeLocation(HostLocation location) {
96 locations.remove(location);
97 return this;
98 }
99
100 /**
101 * Gets host mac address of this record.
102 *
103 * @return the host mac address
104 */
105 public MacAddress macAddress() {
106 return macAddress;
107 }
108
109 /**
110 * Gets host vlan id of this record.
111 *
112 * @return the host id.
113 */
114 public VlanId vlanId() {
115 return vlanId;
116 }
117
118 /**
119 * Gets IPv4 address which assigned to the host.
120 *
121 * @return the IP address assigned to the host
122 */
123 public Optional<Ip4Address> ip4Address() {
124 return Optional.ofNullable(ip4Address);
125 }
126
127 /**
128 * Sets IPv4 address.
129 *
130 * @param ip4Address the IPv4 address
131 * @return the DHCP record
132 */
133 public DhcpRecord ip4Address(Ip4Address ip4Address) {
134 this.ip4Address = ip4Address;
135 return this;
136 }
137
138 /**
139 * Gets IPv6 address which assigned to the host.
140 *
141 * @return the IP address assigned to the host
142 */
143 public Optional<Ip6Address> ip6Address() {
144 return Optional.ofNullable(ip6Address);
145 }
146
147 /**
148 * Sets IPv6 address.
149 *
150 * @param ip6Address the IPv6 address
151 * @return the DHCP record
152 */
153 public DhcpRecord ip6Address(Ip6Address ip6Address) {
154 this.ip6Address = ip6Address;
155 return this;
156 }
157
158 /**
159 * Gets the latest time this record updated.
160 *
161 * @return the last time host send or receive DHCP packet
162 */
163 public long lastSeen() {
164 return lastSeen;
165 }
166
167 /**
168 * Updates the update time of this record.
169 *
170 * @return the DHCP record
171 */
172 public DhcpRecord updateLastSeen() {
173 lastSeen = System.currentTimeMillis();
174 return this;
175 }
176
177 /**
178 * Indicated that the host is direct connected to the network or not.
179 *
180 * @return true if the host is directly connected to the network; false otherwise
181 */
182 public boolean directlyConnected() {
183 return directlyConnected;
184 }
185
186 /**
187 * Sets the flag which indicated that the host is directly connected to the
188 * network.
189 *
190 * @param directlyConnected the flag to set
191 * @return the DHCP record
192 */
193 public DhcpRecord setDirectlyConnected(boolean directlyConnected) {
194 this.directlyConnected = directlyConnected;
195 return this;
196 }
197
198 /**
199 * Gets the DHCPv4 status of this record.
200 *
201 * @return the DHCPv4 status; empty if not exists
202 */
203 public Optional<DHCP.MsgType> ip4Status() {
204 return Optional.ofNullable(ip4Status);
205 }
206
207 /**
208 * Sets status of DHCPv4.
209 *
210 * @param ip4Status the status
211 * @return the DHCP record
212 */
213 public DhcpRecord ip4Status(DHCP.MsgType ip4Status) {
214 this.ip4Status = ip4Status;
215 return this;
216 }
217
218 /**
219 * Gets the DHCPv6 status of this record.
220 *
221 * @return the DHCPv6 status; empty if not exists
222 */
223 public Optional<DHCP6.MsgType> ip6Status() {
224 return Optional.ofNullable(ip6Status);
225 }
226
227 /**
228 * Sets status of DHCPv6.
229 *
230 * @param ip6Status the DHCPv6 status
231 * @return the DHCP record
232 */
233 public DhcpRecord ip6Status(DHCP6.MsgType ip6Status) {
234 this.ip6Status = ip6Status;
235 return this;
236 }
237
238 /**
239 * Gets nextHop mac address.
240 *
241 * @return the IPv4 nextHop mac address; empty if not exists
242 */
243 public Optional<MacAddress> nextHop() {
244 return Optional.ofNullable(nextHop);
245 }
246
247 /**
248 * Sets nextHop mac address.
249 *
250 * @param nextHop the IPv4 nextHop mac address
251 * @return the DHCP record
252 */
253 public DhcpRecord nextHop(MacAddress nextHop) {
254 this.nextHop = nextHop;
255 return this;
256 }
257
258 /**
259 * Clone this DHCP record.
260 *
261 * @return the DHCP record which cloned
262 */
263 public DhcpRecord clone() {
264 DhcpRecord newRecord = new DhcpRecord(HostId.hostId(macAddress, vlanId));
265 locations.forEach(newRecord::addLocation);
266 newRecord.directlyConnected = directlyConnected;
267 newRecord.nextHop = nextHop;
268 newRecord.ip4Address = ip4Address;
269 newRecord.ip4Status = ip4Status;
270 newRecord.ip6Address = ip6Address;
271 newRecord.ip6Status = ip6Status;
272 newRecord.lastSeen = lastSeen;
273 return newRecord;
274 }
275
276 @Override
277 public int hashCode() {
278 return Objects.hash(locations, macAddress, vlanId, ip4Address, ip4Status,
279 nextHop, ip6Address, ip6Status, lastSeen);
280 }
281
282 @Override
283 public boolean equals(Object obj) {
284 if (this == obj) {
285 return true;
286 }
287 if (!(obj instanceof DhcpRecord)) {
288 return false;
289 }
290 DhcpRecord that = (DhcpRecord) obj;
291 return Objects.equals(locations, that.locations) &&
292 Objects.equals(macAddress, that.macAddress) &&
293 Objects.equals(vlanId, that.vlanId) &&
294 Objects.equals(ip4Address, that.ip4Address) &&
295 Objects.equals(ip4Status, that.ip4Status) &&
296 Objects.equals(nextHop, that.nextHop) &&
297 Objects.equals(ip6Address, that.ip6Address) &&
298 Objects.equals(ip6Status, that.ip6Status) &&
299 Objects.equals(lastSeen, that.lastSeen) &&
300 Objects.equals(directlyConnected, that.directlyConnected);
301 }
302
303 @Override
304 public String toString() {
305 return MoreObjects.toStringHelper(getClass())
306 .add("locations", locations)
307 .add("macAddress", macAddress)
308 .add("vlanId", vlanId)
309 .add("ip4Address", ip4Address)
310 .add("ip4State", ip4Status)
311 .add("nextHop", nextHop)
312 .add("ip6Address", ip6Address)
313 .add("ip6State", ip6Status)
314 .add("lastSeen", lastSeen)
315 .add("directlyConnected", directlyConnected)
316 .toString();
317 }
318}