blob: e9c286cd013e657a825028292b425d7c03edd532 [file] [log] [blame]
samanwita palf28207b2015-09-04 10:41:56 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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
30 private final Ip4Address ipAddress;
31
32 private final Date timestamp;
33
34 private final long leasePeriod;
35
danielcd9deed2015-10-30 17:16:16 +090036 private final Ip4Address subnetMask;
37
38 private final Ip4Address dhcpServer;
39
40 private final Ip4Address routerAddress;
41
42 private final Ip4Address domainServer;
43
daniel877bb2f2015-11-12 21:33:05 +090044 private final boolean rangeNotEnforced;
danielcd9deed2015-10-30 17:16:16 +090045
samanwita palf28207b2015-09-04 10:41:56 -070046 private final AssignmentStatus assignmentStatus;
47
48 public enum AssignmentStatus {
49 /**
50 * IP has been requested by a host, but not assigned to it yet.
51 */
52 Option_Requested,
53
54 /**
danielcd9deed2015-10-30 17:16:16 +090055 * IP Assignment has been requested by a OpenStack.
56 */
daniel877bb2f2015-11-12 21:33:05 +090057 Option_RangeNotEnforced,
danielcd9deed2015-10-30 17:16:16 +090058 /**
samanwita palf28207b2015-09-04 10:41:56 -070059 * IP has been assigned to a host.
60 */
61 Option_Assigned,
62
63 /**
64 * IP mapping is no longer active.
65 */
Sho SHIMIZU9f614a42015-09-11 15:32:46 -070066 Option_Expired
samanwita palf28207b2015-09-04 10:41:56 -070067 }
68
69 /**
70 * Constructor for IPAssignment, where the ipAddress, the lease period, the timestamp
71 * and assignment status is supplied.
72 *
73 * @param ipAddress
74 * @param leasePeriod
danielcd9deed2015-10-30 17:16:16 +090075 * @param timestamp
samanwita palf28207b2015-09-04 10:41:56 -070076 * @param assignmentStatus
danielcd9deed2015-10-30 17:16:16 +090077 * @param subnetMask
78 * @param dhcpServer
79 * @param routerAddress
80 * @param domainServer
daniel877bb2f2015-11-12 21:33:05 +090081 * @param rangeNotEnforced
samanwita palf28207b2015-09-04 10:41:56 -070082 */
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -070083 private IpAssignment(Ip4Address ipAddress,
samanwita palf28207b2015-09-04 10:41:56 -070084 long leasePeriod,
85 Date timestamp,
danielcd9deed2015-10-30 17:16:16 +090086 AssignmentStatus assignmentStatus, Ip4Address subnetMask, Ip4Address dhcpServer,
daniel877bb2f2015-11-12 21:33:05 +090087 Ip4Address routerAddress, Ip4Address domainServer, boolean rangeNotEnforced) {
samanwita palf28207b2015-09-04 10:41:56 -070088 this.ipAddress = ipAddress;
89 this.leasePeriod = leasePeriod;
90 this.timestamp = timestamp;
91 this.assignmentStatus = assignmentStatus;
danielcd9deed2015-10-30 17:16:16 +090092 this.subnetMask = subnetMask;
93 this.dhcpServer = dhcpServer;
94 this.routerAddress = routerAddress;
95 this.domainServer = domainServer;
daniel877bb2f2015-11-12 21:33:05 +090096 this.rangeNotEnforced = rangeNotEnforced;
samanwita palf28207b2015-09-04 10:41:56 -070097 }
98
99 /**
100 * Returns the IP Address of the IP assignment.
101 *
102 * @return the IP address
103 */
104 public Ip4Address ipAddress() {
105 return this.ipAddress;
106 }
107
108 /**
109 * Returns the timestamp of the IP assignment.
110 *
111 * @return the timestamp
112 */
113 public Date timestamp() {
114 return this.timestamp;
115 }
116
117 /**
118 * Returns the assignment status of the IP assignment.
119 *
120 * @return the assignment status
121 */
122 public AssignmentStatus assignmentStatus() {
123 return this.assignmentStatus;
124 }
125
126 /**
127 * Returns the lease period of the IP assignment.
128 *
samanwita pal2a313402015-09-14 16:03:22 -0700129 * @return the lease period in seconds
samanwita palf28207b2015-09-04 10:41:56 -0700130 */
131 public int leasePeriod() {
samanwita pal2a313402015-09-14 16:03:22 -0700132 return (int) this.leasePeriod;
133 }
134
135 /**
136 * Returns the lease period of the IP assignment.
137 *
138 * @return the lease period in milliseconds
139 */
140 public int leasePeriodMs() {
141 return (int) this.leasePeriod * 1000;
samanwita palf28207b2015-09-04 10:41:56 -0700142 }
143
danielcd9deed2015-10-30 17:16:16 +0900144 public Ip4Address subnetMask() {
145 return subnetMask;
146 }
147
148 public Ip4Address dhcpServer() {
149 return dhcpServer;
150 }
151
152 public Ip4Address routerAddress() {
153 return routerAddress;
154 }
155
156 public Ip4Address domainServer() {
157 return domainServer;
158 }
159
daniel877bb2f2015-11-12 21:33:05 +0900160 public boolean rangeNotEnforced() {
161 return rangeNotEnforced;
danielcd9deed2015-10-30 17:16:16 +0900162 }
163
samanwita palf28207b2015-09-04 10:41:56 -0700164 @Override
165 public String toString() {
166 return MoreObjects.toStringHelper(getClass())
167 .add("ip", ipAddress)
168 .add("timestamp", timestamp)
169 .add("lease", leasePeriod)
170 .add("assignmentStatus", assignmentStatus)
danielcd9deed2015-10-30 17:16:16 +0900171 .add("subnetMask", subnetMask)
172 .add("dhcpServer", dhcpServer)
173 .add("routerAddress", routerAddress)
174 .add("domainServer", domainServer)
daniel877bb2f2015-11-12 21:33:05 +0900175 .add("rangeNotEnforced", rangeNotEnforced)
samanwita palf28207b2015-09-04 10:41:56 -0700176 .toString();
177 }
178
179 /**
180 * Creates and returns a new builder instance.
181 *
182 * @return new builder
183 */
184 public static Builder builder() {
185 return new Builder();
186 }
187
188 /**
189 * Creates and returns a new builder instance that clones an existing IPAssignment.
190 *
Ray Milkey9b36d812015-09-09 15:24:54 -0700191 * @param assignment ip address assignment
samanwita palf28207b2015-09-04 10:41:56 -0700192 * @return new builder
193 */
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -0700194 public static Builder builder(IpAssignment assignment) {
samanwita palf28207b2015-09-04 10:41:56 -0700195 return new Builder(assignment);
196 }
197
198 /**
199 * IPAssignment Builder.
200 */
201 public static final class Builder {
202
203 private Ip4Address ipAddress;
204
205 private Date timeStamp;
206
207 private long leasePeriod;
208
209 private AssignmentStatus assignmentStatus;
210
danielcd9deed2015-10-30 17:16:16 +0900211 private Ip4Address subnetMask;
212
213 private Ip4Address dhcpServer;
214
215 private Ip4Address domainServer;
216
217 private Ip4Address routerAddress;
218
daniel877bb2f2015-11-12 21:33:05 +0900219 private boolean rangeNotEnforced = false;
danielcd9deed2015-10-30 17:16:16 +0900220
samanwita palf28207b2015-09-04 10:41:56 -0700221 private Builder() {
222
223 }
224
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -0700225 private Builder(IpAssignment ipAssignment) {
samanwita palf28207b2015-09-04 10:41:56 -0700226 ipAddress = ipAssignment.ipAddress();
227 timeStamp = ipAssignment.timestamp();
samanwita pal2a313402015-09-14 16:03:22 -0700228 leasePeriod = ipAssignment.leasePeriod();
samanwita palf28207b2015-09-04 10:41:56 -0700229 assignmentStatus = ipAssignment.assignmentStatus();
230 }
231
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -0700232 public IpAssignment build() {
samanwita palf28207b2015-09-04 10:41:56 -0700233 validateInputs();
danielcd9deed2015-10-30 17:16:16 +0900234 return new IpAssignment(ipAddress, leasePeriod, timeStamp, assignmentStatus, subnetMask,
Hyunsun Moon58174e42015-12-10 23:01:42 -0800235 dhcpServer, routerAddress, domainServer, rangeNotEnforced);
samanwita palf28207b2015-09-04 10:41:56 -0700236 }
237
238 public Builder ipAddress(Ip4Address addr) {
239 ipAddress = addr;
240 return this;
241 }
242
243 public Builder timestamp(Date timestamp) {
244 timeStamp = timestamp;
245 return this;
246 }
247
248 public Builder leasePeriod(int leasePeriodinSeconds) {
samanwita pal2a313402015-09-14 16:03:22 -0700249 leasePeriod = leasePeriodinSeconds;
samanwita palf28207b2015-09-04 10:41:56 -0700250 return this;
251 }
252
253 public Builder assignmentStatus(AssignmentStatus status) {
254 assignmentStatus = status;
255 return this;
256 }
257
danielcd9deed2015-10-30 17:16:16 +0900258 public Builder subnetMask(Ip4Address subnetMask) {
259 this.subnetMask = subnetMask;
260 return this;
261 }
262
263 public Builder dhcpServer(Ip4Address dhcpServer) {
264 this.dhcpServer = dhcpServer;
265 return this;
266 }
267
268 public Builder domainServer(Ip4Address domainServer) {
269 this.domainServer = domainServer;
270 return this;
271 }
272
273 public Builder routerAddress(Ip4Address routerAddress) {
274 this.routerAddress = routerAddress;
275 return this;
276 }
277
daniel877bb2f2015-11-12 21:33:05 +0900278 public Builder rangeNotEnforced(boolean rangeNotEnforced) {
279 this.rangeNotEnforced = rangeNotEnforced;
danielcd9deed2015-10-30 17:16:16 +0900280 return this;
281 }
282
283
samanwita palf28207b2015-09-04 10:41:56 -0700284 private void validateInputs() {
285 checkNotNull(ipAddress, "IP Address must be specified");
286 checkNotNull(assignmentStatus, "Assignment Status must be specified");
287 checkNotNull(leasePeriod, "Lease Period must be specified");
288 checkNotNull(timeStamp, "Timestamp must be specified");
289
daniel877bb2f2015-11-12 21:33:05 +0900290 if (rangeNotEnforced) {
291 checkNotNull(subnetMask, "subnetMask must be specified in case of rangeNotEnforced");
292 checkNotNull(dhcpServer, "dhcpServer must be specified in case of rangeNotEnforced");
293 checkNotNull(domainServer, "domainServer must be specified in case of rangeNotEnforced");
294 checkNotNull(routerAddress, "routerAddress must be specified in case of rangeNotEnforced");
danielcd9deed2015-10-30 17:16:16 +0900295 }
296
samanwita palf28207b2015-09-04 10:41:56 -0700297 switch (assignmentStatus) {
298 case Option_Requested:
daniel877bb2f2015-11-12 21:33:05 +0900299 case Option_RangeNotEnforced:
samanwita palf28207b2015-09-04 10:41:56 -0700300 case Option_Assigned:
301 case Option_Expired:
302 break;
303 default:
304 throw new IllegalStateException("Unknown assignment status");
305 }
306 }
307 }
308}