blob: 1129d2a987b6d560f6ac8ec0f236498148a022e3 [file] [log] [blame]
samanwita pal18696f62015-07-17 13:15:43 -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.dhcpserver;
17
samanwita palf66ed8a2015-07-21 15:45:33 -070018import com.google.common.base.MoreObjects;
samanwita pal18696f62015-07-17 13:15:43 -070019import org.onlab.packet.Ip4Address;
20
21import java.util.Date;
22
samanwita palf66ed8a2015-07-21 15:45:33 -070023import static com.google.common.base.Preconditions.checkNotNull;
24
samanwita pal18696f62015-07-17 13:15:43 -070025/**
26 * Stores the MAC ID to IP Address mapping details.
27 */
samanwita palf66ed8a2015-07-21 15:45:33 -070028public final class IPAssignment {
samanwita pal18696f62015-07-17 13:15:43 -070029
samanwita palf66ed8a2015-07-21 15:45:33 -070030 private final Ip4Address ipAddress;
samanwita pal18696f62015-07-17 13:15:43 -070031
samanwita palf66ed8a2015-07-21 15:45:33 -070032 private final Date timestamp;
samanwita pal18696f62015-07-17 13:15:43 -070033
samanwita palf66ed8a2015-07-21 15:45:33 -070034 private final long leasePeriod;
samanwita pal18696f62015-07-17 13:15:43 -070035
samanwita palf66ed8a2015-07-21 15:45:33 -070036 private final AssignmentStatus assignmentStatus;
samanwita pal18696f62015-07-17 13:15:43 -070037
38 public enum AssignmentStatus {
samanwita palf66ed8a2015-07-21 15:45:33 -070039 /**
40 * IP has been requested by a host, but not assigned to it yet.
41 */
samanwita pal18696f62015-07-17 13:15:43 -070042 Option_Requested,
samanwita palf66ed8a2015-07-21 15:45:33 -070043
44 /**
45 * IP has been assigned to a host.
46 */
samanwita pal18696f62015-07-17 13:15:43 -070047 Option_Assigned,
samanwita palf66ed8a2015-07-21 15:45:33 -070048
49 /**
50 * IP mapping is no longer active.
51 */
samanwita pal18696f62015-07-17 13:15:43 -070052 Option_Expired;
53 }
54
55 /**
samanwita palf66ed8a2015-07-21 15:45:33 -070056 * Constructor for IPAssignment, where the ipAddress, the lease period, the timestamp
57 * and assignment status is supplied.
samanwita pal18696f62015-07-17 13:15:43 -070058 *
59 * @param ipAddress
60 * @param leasePeriod
61 * @param assignmentStatus
62 */
samanwita palf66ed8a2015-07-21 15:45:33 -070063 private IPAssignment(Ip4Address ipAddress,
64 long leasePeriod,
65 Date timestamp,
66 AssignmentStatus assignmentStatus) {
samanwita pal18696f62015-07-17 13:15:43 -070067 this.ipAddress = ipAddress;
68 this.leasePeriod = leasePeriod;
samanwita pal18696f62015-07-17 13:15:43 -070069 this.timestamp = timestamp;
samanwita pal18696f62015-07-17 13:15:43 -070070 this.assignmentStatus = assignmentStatus;
71 }
72
73 /**
samanwita pal18696f62015-07-17 13:15:43 -070074 * Returns the IP Address of the IP assignment.
75 *
76 * @return the IP address
77 */
samanwita palf66ed8a2015-07-21 15:45:33 -070078 public Ip4Address ipAddress() {
samanwita pal18696f62015-07-17 13:15:43 -070079 return this.ipAddress;
80 }
81
82 /**
83 * Returns the timestamp of the IP assignment.
84 *
85 * @return the timestamp
86 */
samanwita palf66ed8a2015-07-21 15:45:33 -070087 public Date timestamp() {
samanwita pal18696f62015-07-17 13:15:43 -070088 return this.timestamp;
89 }
90
91 /**
92 * Returns the assignment status of the IP assignment.
93 *
94 * @return the assignment status
95 */
samanwita palf66ed8a2015-07-21 15:45:33 -070096 public AssignmentStatus assignmentStatus() {
samanwita pal18696f62015-07-17 13:15:43 -070097 return this.assignmentStatus;
98 }
99
100 /**
101 * Returns the lease period of the IP assignment.
102 *
103 * @return the lease period
104 */
samanwita palf66ed8a2015-07-21 15:45:33 -0700105 public long leasePeriod() {
samanwita pal18696f62015-07-17 13:15:43 -0700106 return this.leasePeriod;
107 }
samanwita palf66ed8a2015-07-21 15:45:33 -0700108
109 @Override
110 public String toString() {
111 return MoreObjects.toStringHelper(getClass())
112 .add("ip", ipAddress)
113 .add("timestamp", timestamp)
114 .add("lease", leasePeriod)
115 .add("assignmentStatus", assignmentStatus)
116 .toString();
117 }
118
119 /**
120 * Creates and returns a new builder instance.
121 *
122 * @return new builder
123 */
124 public static Builder builder() {
125 return new Builder();
126 }
127
128 /**
samanwita palf09c09e2015-07-22 16:06:42 -0700129 * Creates and returns a new builder instance that clones an existing IPAssignment.
130 *
131 * @return new builder
132 */
133 public static Builder builder(IPAssignment assignment) {
134 return new Builder(assignment);
135 }
136
137 /**
samanwita palf66ed8a2015-07-21 15:45:33 -0700138 * IPAssignment Builder.
139 */
140 public static final class Builder {
141
142 private Ip4Address ipAddress;
143
144 private Date timeStamp;
145
146 private long leasePeriod;
147
148 private AssignmentStatus assignmentStatus;
149
150 private Builder() {
151
152 }
153
154 private Builder(IPAssignment ipAssignment) {
155 ipAddress = ipAssignment.ipAddress();
156 timeStamp = ipAssignment.timestamp();
157 leasePeriod = ipAssignment.leasePeriod();
158 assignmentStatus = ipAssignment.assignmentStatus();
159 }
160
161 public IPAssignment build() {
162 validateInputs();
163 return new IPAssignment(ipAddress,
164 leasePeriod,
165 timeStamp,
166 assignmentStatus);
167 }
168
169 public Builder ipAddress(Ip4Address addr) {
170 ipAddress = addr;
171 return this;
172 }
173
174 public Builder timestamp(Date timestamp) {
175 timeStamp = timestamp;
176 return this;
177 }
178
179 public Builder leasePeriod(int leasePeriodinSeconds) {
180 leasePeriod = leasePeriodinSeconds * 1000;
181 return this;
182 }
183
184 public Builder assignmentStatus(AssignmentStatus status) {
185 assignmentStatus = status;
186 return this;
187 }
188
189 private void validateInputs() {
190 checkNotNull(ipAddress, "IP Address must be specified");
191 checkNotNull(assignmentStatus, "Assignment Status must be specified");
192 checkNotNull(leasePeriod, "Lease Period must be specified");
193 checkNotNull(timeStamp, "Timestamp must be specified");
194
195 switch (assignmentStatus) {
196 case Option_Requested:
197 case Option_Assigned:
198 case Option_Expired:
199 break;
200 default:
201 throw new IllegalStateException("Unknown assignment status");
202 }
203 }
204 }
samanwita pal18696f62015-07-17 13:15:43 -0700205}