blob: 9b3aa68641077f94ead52ece9f8ef320a6fc9569 [file] [log] [blame]
samanwita palf28207b2015-09-04 10:41:56 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
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
36 private final AssignmentStatus assignmentStatus;
37
38 public enum AssignmentStatus {
39 /**
40 * IP has been requested by a host, but not assigned to it yet.
41 */
42 Option_Requested,
43
44 /**
45 * IP has been assigned to a host.
46 */
47 Option_Assigned,
48
49 /**
50 * IP mapping is no longer active.
51 */
Sho SHIMIZU9f614a42015-09-11 15:32:46 -070052 Option_Expired
samanwita palf28207b2015-09-04 10:41:56 -070053 }
54
55 /**
56 * Constructor for IPAssignment, where the ipAddress, the lease period, the timestamp
57 * and assignment status is supplied.
58 *
59 * @param ipAddress
60 * @param leasePeriod
61 * @param assignmentStatus
62 */
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -070063 private IpAssignment(Ip4Address ipAddress,
samanwita palf28207b2015-09-04 10:41:56 -070064 long leasePeriod,
65 Date timestamp,
66 AssignmentStatus assignmentStatus) {
67 this.ipAddress = ipAddress;
68 this.leasePeriod = leasePeriod;
69 this.timestamp = timestamp;
70 this.assignmentStatus = assignmentStatus;
71 }
72
73 /**
74 * Returns the IP Address of the IP assignment.
75 *
76 * @return the IP address
77 */
78 public Ip4Address ipAddress() {
79 return this.ipAddress;
80 }
81
82 /**
83 * Returns the timestamp of the IP assignment.
84 *
85 * @return the timestamp
86 */
87 public Date timestamp() {
88 return this.timestamp;
89 }
90
91 /**
92 * Returns the assignment status of the IP assignment.
93 *
94 * @return the assignment status
95 */
96 public AssignmentStatus assignmentStatus() {
97 return this.assignmentStatus;
98 }
99
100 /**
101 * Returns the lease period of the IP assignment.
102 *
samanwita pal2a313402015-09-14 16:03:22 -0700103 * @return the lease period in seconds
samanwita palf28207b2015-09-04 10:41:56 -0700104 */
105 public int leasePeriod() {
samanwita pal2a313402015-09-14 16:03:22 -0700106 return (int) this.leasePeriod;
107 }
108
109 /**
110 * Returns the lease period of the IP assignment.
111 *
112 * @return the lease period in milliseconds
113 */
114 public int leasePeriodMs() {
115 return (int) this.leasePeriod * 1000;
samanwita palf28207b2015-09-04 10:41:56 -0700116 }
117
118 @Override
119 public String toString() {
120 return MoreObjects.toStringHelper(getClass())
121 .add("ip", ipAddress)
122 .add("timestamp", timestamp)
123 .add("lease", leasePeriod)
124 .add("assignmentStatus", assignmentStatus)
125 .toString();
126 }
127
128 /**
129 * Creates and returns a new builder instance.
130 *
131 * @return new builder
132 */
133 public static Builder builder() {
134 return new Builder();
135 }
136
137 /**
138 * Creates and returns a new builder instance that clones an existing IPAssignment.
139 *
Ray Milkey9b36d812015-09-09 15:24:54 -0700140 * @param assignment ip address assignment
samanwita palf28207b2015-09-04 10:41:56 -0700141 * @return new builder
142 */
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -0700143 public static Builder builder(IpAssignment assignment) {
samanwita palf28207b2015-09-04 10:41:56 -0700144 return new Builder(assignment);
145 }
146
147 /**
148 * IPAssignment Builder.
149 */
150 public static final class Builder {
151
152 private Ip4Address ipAddress;
153
154 private Date timeStamp;
155
156 private long leasePeriod;
157
158 private AssignmentStatus assignmentStatus;
159
160 private Builder() {
161
162 }
163
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -0700164 private Builder(IpAssignment ipAssignment) {
samanwita palf28207b2015-09-04 10:41:56 -0700165 ipAddress = ipAssignment.ipAddress();
166 timeStamp = ipAssignment.timestamp();
samanwita pal2a313402015-09-14 16:03:22 -0700167 leasePeriod = ipAssignment.leasePeriod();
samanwita palf28207b2015-09-04 10:41:56 -0700168 assignmentStatus = ipAssignment.assignmentStatus();
169 }
170
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -0700171 public IpAssignment build() {
samanwita palf28207b2015-09-04 10:41:56 -0700172 validateInputs();
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -0700173 return new IpAssignment(ipAddress,
samanwita palf28207b2015-09-04 10:41:56 -0700174 leasePeriod,
175 timeStamp,
176 assignmentStatus);
177 }
178
179 public Builder ipAddress(Ip4Address addr) {
180 ipAddress = addr;
181 return this;
182 }
183
184 public Builder timestamp(Date timestamp) {
185 timeStamp = timestamp;
186 return this;
187 }
188
189 public Builder leasePeriod(int leasePeriodinSeconds) {
samanwita pal2a313402015-09-14 16:03:22 -0700190 leasePeriod = leasePeriodinSeconds;
samanwita palf28207b2015-09-04 10:41:56 -0700191 return this;
192 }
193
194 public Builder assignmentStatus(AssignmentStatus status) {
195 assignmentStatus = status;
196 return this;
197 }
198
199 private void validateInputs() {
200 checkNotNull(ipAddress, "IP Address must be specified");
201 checkNotNull(assignmentStatus, "Assignment Status must be specified");
202 checkNotNull(leasePeriod, "Lease Period must be specified");
203 checkNotNull(timeStamp, "Timestamp must be specified");
204
205 switch (assignmentStatus) {
206 case Option_Requested:
207 case Option_Assigned:
208 case Option_Expired:
209 break;
210 default:
211 throw new IllegalStateException("Unknown assignment status");
212 }
213 }
214 }
215}