blob: 7b83f0c4390b03977fb7f5c942a63b5d7e66c992 [file] [log] [blame]
samanwita palf28207b2015-09-04 10:41:56 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
samanwita palf28207b2015-09-04 10:41:56 -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 */
16package org.onosproject.dhcp;
17
18import com.google.common.base.MoreObjects;
19import org.onlab.packet.Ip4Address;
20
21import java.util.Date;
22
23import static com.google.common.base.Preconditions.checkNotNull;
24
25/**
26 * Stores the MAC ID to IP Address mapping details.
27 */
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -070028public final class IpAssignment {
samanwita palf28207b2015-09-04 10:41:56 -070029
Hyunsun Moon04b1fe92016-05-18 21:28:06 -070030 // TODO make some dhcp options optional
samanwita palf28207b2015-09-04 10:41:56 -070031 private final Ip4Address ipAddress;
samanwita palf28207b2015-09-04 10:41:56 -070032 private final Date timestamp;
samanwita palf28207b2015-09-04 10:41:56 -070033 private final long leasePeriod;
danielcd9deed2015-10-30 17:16:16 +090034 private final Ip4Address subnetMask;
Hyunsun Moon04b1fe92016-05-18 21:28:06 -070035 private final Ip4Address broadcast;
danielcd9deed2015-10-30 17:16:16 +090036 private final Ip4Address dhcpServer;
danielcd9deed2015-10-30 17:16:16 +090037 private final Ip4Address routerAddress;
danielcd9deed2015-10-30 17:16:16 +090038 private final Ip4Address domainServer;
samanwita palf28207b2015-09-04 10:41:56 -070039 private final AssignmentStatus assignmentStatus;
40
41 public enum AssignmentStatus {
42 /**
43 * IP has been requested by a host, but not assigned to it yet.
44 */
45 Option_Requested,
46
47 /**
Hyunsun Moon04b1fe92016-05-18 21:28:06 -070048 * Static IP Assignment with unregistered IP range.
49 * This assignment can only be added or removed by set or remove static mapping.
danielcd9deed2015-10-30 17:16:16 +090050 */
Hyunsun Moon04b1fe92016-05-18 21:28:06 -070051 // TODO allow multiple IP ranges and remove this option
daniel877bb2f2015-11-12 21:33:05 +090052 Option_RangeNotEnforced,
danielcd9deed2015-10-30 17:16:16 +090053 /**
samanwita palf28207b2015-09-04 10:41:56 -070054 * IP has been assigned to a host.
55 */
56 Option_Assigned,
57
58 /**
59 * IP mapping is no longer active.
60 */
Sho SHIMIZU9f614a42015-09-11 15:32:46 -070061 Option_Expired
samanwita palf28207b2015-09-04 10:41:56 -070062 }
63
64 /**
65 * Constructor for IPAssignment, where the ipAddress, the lease period, the timestamp
66 * and assignment status is supplied.
67 *
Hyunsun Moon04b1fe92016-05-18 21:28:06 -070068 * @param ipAddress ip address to assign
69 * @param leasePeriod lease period
70 * @param timestamp time stamp of the assignment
71 * @param assignmentStatus statue of the assignment
72 * @param subnetMask subnet mask of assigned ip range
73 * @param broadcast broadcast address
74 * @param dhcpServer dhcp server address
75 * @param routerAddress router address
76 * @param domainServer domain server address
samanwita palf28207b2015-09-04 10:41:56 -070077 */
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -070078 private IpAssignment(Ip4Address ipAddress,
samanwita palf28207b2015-09-04 10:41:56 -070079 long leasePeriod,
80 Date timestamp,
Hyunsun Moon04b1fe92016-05-18 21:28:06 -070081 AssignmentStatus assignmentStatus,
82 Ip4Address subnetMask,
83 Ip4Address broadcast,
84 Ip4Address dhcpServer,
85 Ip4Address routerAddress,
86 Ip4Address domainServer) {
samanwita palf28207b2015-09-04 10:41:56 -070087 this.ipAddress = ipAddress;
88 this.leasePeriod = leasePeriod;
89 this.timestamp = timestamp;
90 this.assignmentStatus = assignmentStatus;
danielcd9deed2015-10-30 17:16:16 +090091 this.subnetMask = subnetMask;
Hyunsun Moon04b1fe92016-05-18 21:28:06 -070092 this.broadcast = broadcast;
danielcd9deed2015-10-30 17:16:16 +090093 this.dhcpServer = dhcpServer;
94 this.routerAddress = routerAddress;
95 this.domainServer = domainServer;
samanwita palf28207b2015-09-04 10:41:56 -070096 }
97
98 /**
99 * Returns the IP Address of the IP assignment.
100 *
101 * @return the IP address
102 */
103 public Ip4Address ipAddress() {
104 return this.ipAddress;
105 }
106
107 /**
108 * Returns the timestamp of the IP assignment.
109 *
110 * @return the timestamp
111 */
112 public Date timestamp() {
113 return this.timestamp;
114 }
115
116 /**
117 * Returns the assignment status of the IP assignment.
118 *
119 * @return the assignment status
120 */
121 public AssignmentStatus assignmentStatus() {
122 return this.assignmentStatus;
123 }
124
125 /**
126 * Returns the lease period of the IP assignment.
127 *
samanwita pal2a313402015-09-14 16:03:22 -0700128 * @return the lease period in seconds
samanwita palf28207b2015-09-04 10:41:56 -0700129 */
130 public int leasePeriod() {
samanwita pal2a313402015-09-14 16:03:22 -0700131 return (int) this.leasePeriod;
132 }
133
134 /**
135 * Returns the lease period of the IP assignment.
136 *
137 * @return the lease period in milliseconds
138 */
139 public int leasePeriodMs() {
140 return (int) this.leasePeriod * 1000;
samanwita palf28207b2015-09-04 10:41:56 -0700141 }
142
Hyunsun Moon04b1fe92016-05-18 21:28:06 -0700143 /**
144 * Returns subnet mask of the IP assignment.
145 *
146 * @return subnet mask
147 */
danielcd9deed2015-10-30 17:16:16 +0900148 public Ip4Address subnetMask() {
149 return subnetMask;
150 }
151
Hyunsun Moon04b1fe92016-05-18 21:28:06 -0700152 /**
153 * Returns broadcast address of the IP assignment.
154 *
155 * @return broadcast address
156 */
157 public Ip4Address broadcast() {
158 return broadcast;
159 }
160
161 /**
162 * Returns dhcp server of the IP assignment.
163 *
164 * @return dhcp server ip address
165 */
danielcd9deed2015-10-30 17:16:16 +0900166 public Ip4Address dhcpServer() {
167 return dhcpServer;
168 }
169
Hyunsun Moon04b1fe92016-05-18 21:28:06 -0700170 /**
171 * Returns router address of the IP assignment.
172 *
173 * @return router ip address
174 */
danielcd9deed2015-10-30 17:16:16 +0900175 public Ip4Address routerAddress() {
176 return routerAddress;
177 }
178
Hyunsun Moon04b1fe92016-05-18 21:28:06 -0700179 /**
180 * Returns domain server address.
181 *
182 * @return domain server ip address
183 */
danielcd9deed2015-10-30 17:16:16 +0900184 public Ip4Address domainServer() {
185 return domainServer;
186 }
187
samanwita palf28207b2015-09-04 10:41:56 -0700188 @Override
189 public String toString() {
190 return MoreObjects.toStringHelper(getClass())
191 .add("ip", ipAddress)
192 .add("timestamp", timestamp)
193 .add("lease", leasePeriod)
194 .add("assignmentStatus", assignmentStatus)
danielcd9deed2015-10-30 17:16:16 +0900195 .add("subnetMask", subnetMask)
Hyunsun Moon04b1fe92016-05-18 21:28:06 -0700196 .add("broadcast", broadcast)
danielcd9deed2015-10-30 17:16:16 +0900197 .add("dhcpServer", dhcpServer)
198 .add("routerAddress", routerAddress)
199 .add("domainServer", domainServer)
samanwita palf28207b2015-09-04 10:41:56 -0700200 .toString();
201 }
202
203 /**
204 * Creates and returns a new builder instance.
205 *
206 * @return new builder
207 */
208 public static Builder builder() {
209 return new Builder();
210 }
211
212 /**
213 * Creates and returns a new builder instance that clones an existing IPAssignment.
214 *
Ray Milkey9b36d812015-09-09 15:24:54 -0700215 * @param assignment ip address assignment
samanwita palf28207b2015-09-04 10:41:56 -0700216 * @return new builder
217 */
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -0700218 public static Builder builder(IpAssignment assignment) {
samanwita palf28207b2015-09-04 10:41:56 -0700219 return new Builder(assignment);
220 }
221
222 /**
223 * IPAssignment Builder.
224 */
225 public static final class Builder {
226
227 private Ip4Address ipAddress;
samanwita palf28207b2015-09-04 10:41:56 -0700228 private Date timeStamp;
samanwita palf28207b2015-09-04 10:41:56 -0700229 private long leasePeriod;
samanwita palf28207b2015-09-04 10:41:56 -0700230 private AssignmentStatus assignmentStatus;
danielcd9deed2015-10-30 17:16:16 +0900231 private Ip4Address subnetMask;
Hyunsun Moon04b1fe92016-05-18 21:28:06 -0700232 private Ip4Address broadcast;
danielcd9deed2015-10-30 17:16:16 +0900233 private Ip4Address dhcpServer;
Hyunsun Moon04b1fe92016-05-18 21:28:06 -0700234 private Ip4Address routerAddress;
danielcd9deed2015-10-30 17:16:16 +0900235 private Ip4Address domainServer;
236
samanwita palf28207b2015-09-04 10:41:56 -0700237 private Builder() {
samanwita palf28207b2015-09-04 10:41:56 -0700238 }
239
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -0700240 private Builder(IpAssignment ipAssignment) {
samanwita palf28207b2015-09-04 10:41:56 -0700241 ipAddress = ipAssignment.ipAddress();
242 timeStamp = ipAssignment.timestamp();
samanwita pal2a313402015-09-14 16:03:22 -0700243 leasePeriod = ipAssignment.leasePeriod();
samanwita palf28207b2015-09-04 10:41:56 -0700244 assignmentStatus = ipAssignment.assignmentStatus();
Hyunsun Moon04b1fe92016-05-18 21:28:06 -0700245 subnetMask = ipAssignment.subnetMask();
246 broadcast = ipAssignment.broadcast();
247 dhcpServer = ipAssignment.dhcpServer();
248 routerAddress = ipAssignment.routerAddress();
249 domainServer = ipAssignment.domainServer();
samanwita palf28207b2015-09-04 10:41:56 -0700250 }
251
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -0700252 public IpAssignment build() {
samanwita palf28207b2015-09-04 10:41:56 -0700253 validateInputs();
Hyunsun Moon04b1fe92016-05-18 21:28:06 -0700254 return new IpAssignment(ipAddress,
255 leasePeriod,
256 timeStamp,
257 assignmentStatus,
258 subnetMask,
259 broadcast,
260 dhcpServer,
261 routerAddress,
262 domainServer);
samanwita palf28207b2015-09-04 10:41:56 -0700263 }
264
265 public Builder ipAddress(Ip4Address addr) {
266 ipAddress = addr;
267 return this;
268 }
269
270 public Builder timestamp(Date timestamp) {
271 timeStamp = timestamp;
272 return this;
273 }
274
275 public Builder leasePeriod(int leasePeriodinSeconds) {
samanwita pal2a313402015-09-14 16:03:22 -0700276 leasePeriod = leasePeriodinSeconds;
samanwita palf28207b2015-09-04 10:41:56 -0700277 return this;
278 }
279
280 public Builder assignmentStatus(AssignmentStatus status) {
281 assignmentStatus = status;
282 return this;
283 }
284
danielcd9deed2015-10-30 17:16:16 +0900285 public Builder subnetMask(Ip4Address subnetMask) {
286 this.subnetMask = subnetMask;
287 return this;
288 }
289
Hyunsun Moon04b1fe92016-05-18 21:28:06 -0700290 public Builder broadcast(Ip4Address broadcast) {
291 this.broadcast = broadcast;
292 return this;
293 }
294
danielcd9deed2015-10-30 17:16:16 +0900295 public Builder dhcpServer(Ip4Address dhcpServer) {
296 this.dhcpServer = dhcpServer;
297 return this;
298 }
299
300 public Builder domainServer(Ip4Address domainServer) {
301 this.domainServer = domainServer;
302 return this;
303 }
304
305 public Builder routerAddress(Ip4Address routerAddress) {
306 this.routerAddress = routerAddress;
307 return this;
308 }
309
samanwita palf28207b2015-09-04 10:41:56 -0700310 private void validateInputs() {
311 checkNotNull(ipAddress, "IP Address must be specified");
312 checkNotNull(assignmentStatus, "Assignment Status must be specified");
samanwita palf28207b2015-09-04 10:41:56 -0700313 checkNotNull(timeStamp, "Timestamp must be specified");
314
315 switch (assignmentStatus) {
316 case Option_Requested:
daniel877bb2f2015-11-12 21:33:05 +0900317 case Option_RangeNotEnforced:
samanwita palf28207b2015-09-04 10:41:56 -0700318 case Option_Assigned:
319 case Option_Expired:
320 break;
321 default:
322 throw new IllegalStateException("Unknown assignment status");
323 }
324 }
325 }
326}