samanwita pal | 18696f6 | 2015-07-17 13:15:43 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.dhcpserver; |
| 17 | |
samanwita pal | f66ed8a | 2015-07-21 15:45:33 -0700 | [diff] [blame^] | 18 | import com.google.common.base.MoreObjects; |
samanwita pal | 18696f6 | 2015-07-17 13:15:43 -0700 | [diff] [blame] | 19 | import org.onlab.packet.Ip4Address; |
| 20 | |
| 21 | import java.util.Date; |
| 22 | |
samanwita pal | f66ed8a | 2015-07-21 15:45:33 -0700 | [diff] [blame^] | 23 | import static com.google.common.base.Preconditions.checkNotNull; |
| 24 | |
samanwita pal | 18696f6 | 2015-07-17 13:15:43 -0700 | [diff] [blame] | 25 | /** |
| 26 | * Stores the MAC ID to IP Address mapping details. |
| 27 | */ |
samanwita pal | f66ed8a | 2015-07-21 15:45:33 -0700 | [diff] [blame^] | 28 | public final class IPAssignment { |
samanwita pal | 18696f6 | 2015-07-17 13:15:43 -0700 | [diff] [blame] | 29 | |
samanwita pal | f66ed8a | 2015-07-21 15:45:33 -0700 | [diff] [blame^] | 30 | private final Ip4Address ipAddress; |
samanwita pal | 18696f6 | 2015-07-17 13:15:43 -0700 | [diff] [blame] | 31 | |
samanwita pal | f66ed8a | 2015-07-21 15:45:33 -0700 | [diff] [blame^] | 32 | private final Date timestamp; |
samanwita pal | 18696f6 | 2015-07-17 13:15:43 -0700 | [diff] [blame] | 33 | |
samanwita pal | f66ed8a | 2015-07-21 15:45:33 -0700 | [diff] [blame^] | 34 | private final long leasePeriod; |
samanwita pal | 18696f6 | 2015-07-17 13:15:43 -0700 | [diff] [blame] | 35 | |
samanwita pal | f66ed8a | 2015-07-21 15:45:33 -0700 | [diff] [blame^] | 36 | private final AssignmentStatus assignmentStatus; |
samanwita pal | 18696f6 | 2015-07-17 13:15:43 -0700 | [diff] [blame] | 37 | |
| 38 | public enum AssignmentStatus { |
samanwita pal | f66ed8a | 2015-07-21 15:45:33 -0700 | [diff] [blame^] | 39 | /** |
| 40 | * IP has been requested by a host, but not assigned to it yet. |
| 41 | */ |
samanwita pal | 18696f6 | 2015-07-17 13:15:43 -0700 | [diff] [blame] | 42 | Option_Requested, |
samanwita pal | f66ed8a | 2015-07-21 15:45:33 -0700 | [diff] [blame^] | 43 | |
| 44 | /** |
| 45 | * IP has been assigned to a host. |
| 46 | */ |
samanwita pal | 18696f6 | 2015-07-17 13:15:43 -0700 | [diff] [blame] | 47 | Option_Assigned, |
samanwita pal | f66ed8a | 2015-07-21 15:45:33 -0700 | [diff] [blame^] | 48 | |
| 49 | /** |
| 50 | * IP mapping is no longer active. |
| 51 | */ |
samanwita pal | 18696f6 | 2015-07-17 13:15:43 -0700 | [diff] [blame] | 52 | Option_Expired; |
| 53 | } |
| 54 | |
| 55 | /** |
samanwita pal | f66ed8a | 2015-07-21 15:45:33 -0700 | [diff] [blame^] | 56 | * Constructor for IPAssignment, where the ipAddress, the lease period, the timestamp |
| 57 | * and assignment status is supplied. |
samanwita pal | 18696f6 | 2015-07-17 13:15:43 -0700 | [diff] [blame] | 58 | * |
| 59 | * @param ipAddress |
| 60 | * @param leasePeriod |
| 61 | * @param assignmentStatus |
| 62 | */ |
samanwita pal | f66ed8a | 2015-07-21 15:45:33 -0700 | [diff] [blame^] | 63 | private IPAssignment(Ip4Address ipAddress, |
| 64 | long leasePeriod, |
| 65 | Date timestamp, |
| 66 | AssignmentStatus assignmentStatus) { |
samanwita pal | 18696f6 | 2015-07-17 13:15:43 -0700 | [diff] [blame] | 67 | this.ipAddress = ipAddress; |
| 68 | this.leasePeriod = leasePeriod; |
samanwita pal | 18696f6 | 2015-07-17 13:15:43 -0700 | [diff] [blame] | 69 | this.timestamp = timestamp; |
samanwita pal | 18696f6 | 2015-07-17 13:15:43 -0700 | [diff] [blame] | 70 | this.assignmentStatus = assignmentStatus; |
| 71 | } |
| 72 | |
| 73 | /** |
samanwita pal | 18696f6 | 2015-07-17 13:15:43 -0700 | [diff] [blame] | 74 | * Returns the IP Address of the IP assignment. |
| 75 | * |
| 76 | * @return the IP address |
| 77 | */ |
samanwita pal | f66ed8a | 2015-07-21 15:45:33 -0700 | [diff] [blame^] | 78 | public Ip4Address ipAddress() { |
samanwita pal | 18696f6 | 2015-07-17 13:15:43 -0700 | [diff] [blame] | 79 | return this.ipAddress; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns the timestamp of the IP assignment. |
| 84 | * |
| 85 | * @return the timestamp |
| 86 | */ |
samanwita pal | f66ed8a | 2015-07-21 15:45:33 -0700 | [diff] [blame^] | 87 | public Date timestamp() { |
samanwita pal | 18696f6 | 2015-07-17 13:15:43 -0700 | [diff] [blame] | 88 | return this.timestamp; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Returns the assignment status of the IP assignment. |
| 93 | * |
| 94 | * @return the assignment status |
| 95 | */ |
samanwita pal | f66ed8a | 2015-07-21 15:45:33 -0700 | [diff] [blame^] | 96 | public AssignmentStatus assignmentStatus() { |
samanwita pal | 18696f6 | 2015-07-17 13:15:43 -0700 | [diff] [blame] | 97 | return this.assignmentStatus; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Returns the lease period of the IP assignment. |
| 102 | * |
| 103 | * @return the lease period |
| 104 | */ |
samanwita pal | f66ed8a | 2015-07-21 15:45:33 -0700 | [diff] [blame^] | 105 | public long leasePeriod() { |
samanwita pal | 18696f6 | 2015-07-17 13:15:43 -0700 | [diff] [blame] | 106 | return this.leasePeriod; |
| 107 | } |
samanwita pal | f66ed8a | 2015-07-21 15:45:33 -0700 | [diff] [blame^] | 108 | |
| 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 | /** |
| 129 | * IPAssignment Builder. |
| 130 | */ |
| 131 | public static final class Builder { |
| 132 | |
| 133 | private Ip4Address ipAddress; |
| 134 | |
| 135 | private Date timeStamp; |
| 136 | |
| 137 | private long leasePeriod; |
| 138 | |
| 139 | private AssignmentStatus assignmentStatus; |
| 140 | |
| 141 | private Builder() { |
| 142 | |
| 143 | } |
| 144 | |
| 145 | private Builder(IPAssignment ipAssignment) { |
| 146 | ipAddress = ipAssignment.ipAddress(); |
| 147 | timeStamp = ipAssignment.timestamp(); |
| 148 | leasePeriod = ipAssignment.leasePeriod(); |
| 149 | assignmentStatus = ipAssignment.assignmentStatus(); |
| 150 | } |
| 151 | |
| 152 | public IPAssignment build() { |
| 153 | validateInputs(); |
| 154 | return new IPAssignment(ipAddress, |
| 155 | leasePeriod, |
| 156 | timeStamp, |
| 157 | assignmentStatus); |
| 158 | } |
| 159 | |
| 160 | public Builder ipAddress(Ip4Address addr) { |
| 161 | ipAddress = addr; |
| 162 | return this; |
| 163 | } |
| 164 | |
| 165 | public Builder timestamp(Date timestamp) { |
| 166 | timeStamp = timestamp; |
| 167 | return this; |
| 168 | } |
| 169 | |
| 170 | public Builder leasePeriod(int leasePeriodinSeconds) { |
| 171 | leasePeriod = leasePeriodinSeconds * 1000; |
| 172 | return this; |
| 173 | } |
| 174 | |
| 175 | public Builder assignmentStatus(AssignmentStatus status) { |
| 176 | assignmentStatus = status; |
| 177 | return this; |
| 178 | } |
| 179 | |
| 180 | private void validateInputs() { |
| 181 | checkNotNull(ipAddress, "IP Address must be specified"); |
| 182 | checkNotNull(assignmentStatus, "Assignment Status must be specified"); |
| 183 | checkNotNull(leasePeriod, "Lease Period must be specified"); |
| 184 | checkNotNull(timeStamp, "Timestamp must be specified"); |
| 185 | |
| 186 | switch (assignmentStatus) { |
| 187 | case Option_Requested: |
| 188 | case Option_Assigned: |
| 189 | case Option_Expired: |
| 190 | break; |
| 191 | default: |
| 192 | throw new IllegalStateException("Unknown assignment status"); |
| 193 | } |
| 194 | } |
| 195 | } |
samanwita pal | 18696f6 | 2015-07-17 13:15:43 -0700 | [diff] [blame] | 196 | } |