samanwita pal | f28207b | 2015-09-04 10:41:56 -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.dhcp; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import org.onlab.packet.Ip4Address; |
| 20 | |
| 21 | import java.util.Date; |
| 22 | |
| 23 | import static com.google.common.base.Preconditions.checkNotNull; |
| 24 | |
| 25 | /** |
| 26 | * Stores the MAC ID to IP Address mapping details. |
| 27 | */ |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 28 | public final class IpAssignment { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 29 | |
| 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 | */ |
| 52 | Option_Expired; |
| 53 | } |
| 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 Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 63 | private IpAssignment(Ip4Address ipAddress, |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 64 | 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 | * |
| 103 | * @return the lease period |
| 104 | */ |
| 105 | public int leasePeriod() { |
| 106 | return (int) this.leasePeriod / 1000; |
| 107 | } |
| 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 | * Creates and returns a new builder instance that clones an existing IPAssignment. |
| 130 | * |
Ray Milkey | 9b36d81 | 2015-09-09 15:24:54 -0700 | [diff] [blame^] | 131 | * @param assignment ip address assignment |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 132 | * @return new builder |
| 133 | */ |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 134 | public static Builder builder(IpAssignment assignment) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 135 | return new Builder(assignment); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * IPAssignment Builder. |
| 140 | */ |
| 141 | public static final class Builder { |
| 142 | |
| 143 | private Ip4Address ipAddress; |
| 144 | |
| 145 | private Date timeStamp; |
| 146 | |
| 147 | private long leasePeriod; |
| 148 | |
| 149 | private AssignmentStatus assignmentStatus; |
| 150 | |
| 151 | private Builder() { |
| 152 | |
| 153 | } |
| 154 | |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 155 | private Builder(IpAssignment ipAssignment) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 156 | ipAddress = ipAssignment.ipAddress(); |
| 157 | timeStamp = ipAssignment.timestamp(); |
| 158 | leasePeriod = ipAssignment.leasePeriod() * 1000; |
| 159 | assignmentStatus = ipAssignment.assignmentStatus(); |
| 160 | } |
| 161 | |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 162 | public IpAssignment build() { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 163 | validateInputs(); |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 164 | return new IpAssignment(ipAddress, |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 165 | leasePeriod, |
| 166 | timeStamp, |
| 167 | assignmentStatus); |
| 168 | } |
| 169 | |
| 170 | public Builder ipAddress(Ip4Address addr) { |
| 171 | ipAddress = addr; |
| 172 | return this; |
| 173 | } |
| 174 | |
| 175 | public Builder timestamp(Date timestamp) { |
| 176 | timeStamp = timestamp; |
| 177 | return this; |
| 178 | } |
| 179 | |
| 180 | public Builder leasePeriod(int leasePeriodinSeconds) { |
| 181 | leasePeriod = leasePeriodinSeconds * 1000; |
| 182 | return this; |
| 183 | } |
| 184 | |
| 185 | public Builder assignmentStatus(AssignmentStatus status) { |
| 186 | assignmentStatus = status; |
| 187 | return this; |
| 188 | } |
| 189 | |
| 190 | private void validateInputs() { |
| 191 | checkNotNull(ipAddress, "IP Address must be specified"); |
| 192 | checkNotNull(assignmentStatus, "Assignment Status must be specified"); |
| 193 | checkNotNull(leasePeriod, "Lease Period must be specified"); |
| 194 | checkNotNull(timeStamp, "Timestamp must be specified"); |
| 195 | |
| 196 | switch (assignmentStatus) { |
| 197 | case Option_Requested: |
| 198 | case Option_Assigned: |
| 199 | case Option_Expired: |
| 200 | break; |
| 201 | default: |
| 202 | throw new IllegalStateException("Unknown assignment status"); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } |