samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 54dc352 | 2015-09-09 00:11:45 -0700 | [diff] [blame] | 2 | * Copyright 2015 Open Networking Laboratory |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 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.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.apache.felix.scr.annotations.Service; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 25 | import org.onlab.packet.Ip4Address; |
| 26 | import org.onlab.packet.MacAddress; |
| 27 | import org.onlab.util.KryoNamespace; |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 28 | import org.onosproject.dhcp.DhcpStore; |
| 29 | import org.onosproject.dhcp.IpAssignment; |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 30 | import org.onosproject.net.HostId; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 31 | import org.onosproject.store.serializers.KryoNamespaces; |
| 32 | import org.onosproject.store.service.ConsistentMap; |
| 33 | import org.onosproject.store.service.DistributedSet; |
| 34 | import org.onosproject.store.service.Serializer; |
| 35 | import org.onosproject.store.service.StorageService; |
| 36 | import org.onosproject.store.service.Versioned; |
| 37 | import org.slf4j.Logger; |
| 38 | import org.slf4j.LoggerFactory; |
| 39 | |
| 40 | import java.util.Date; |
| 41 | import java.util.HashMap; |
| 42 | import java.util.Map; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Manages the pool of available IP Addresses in the network and |
| 46 | * Remembers the mapping between MAC ID and IP Addresses assigned. |
| 47 | */ |
| 48 | |
| 49 | @Component(immediate = true) |
| 50 | @Service |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 51 | public class DistributedDhcpStore implements DhcpStore { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 52 | |
| 53 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 54 | |
| 55 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 56 | protected StorageService storageService; |
| 57 | |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 58 | private ConsistentMap<HostId, IpAssignment> allocationMap; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 59 | |
| 60 | private DistributedSet<Ip4Address> freeIPPool; |
| 61 | |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 62 | private static Ip4Address startIPRange; |
| 63 | |
| 64 | private static Ip4Address endIPRange; |
| 65 | |
| 66 | // Hardcoded values are default values. |
| 67 | |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 68 | private static int timeoutForPendingAssignments = 60; |
| 69 | |
| 70 | @Activate |
| 71 | protected void activate() { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 72 | allocationMap = storageService.<HostId, IpAssignment>consistentMapBuilder() |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 73 | .withName("onos-dhcp-assignedIP") |
| 74 | .withSerializer(Serializer.using( |
| 75 | new KryoNamespace.Builder() |
| 76 | .register(KryoNamespaces.API) |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 77 | .register(IpAssignment.class, |
| 78 | IpAssignment.AssignmentStatus.class, |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 79 | Date.class, |
| 80 | long.class, |
| 81 | Ip4Address.class) |
| 82 | .build())) |
| 83 | .build(); |
| 84 | |
| 85 | freeIPPool = storageService.<Ip4Address>setBuilder() |
| 86 | .withName("onos-dhcp-freeIP") |
| 87 | .withSerializer(Serializer.using(KryoNamespaces.API)) |
| 88 | .build(); |
| 89 | |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 90 | log.info("Started"); |
| 91 | } |
| 92 | |
| 93 | @Deactivate |
| 94 | protected void deactivate() { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 95 | log.info("Stopped"); |
| 96 | } |
| 97 | |
| 98 | @Override |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 99 | public Ip4Address suggestIP(HostId hostId, Ip4Address requestedIP) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 100 | |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 101 | IpAssignment assignmentInfo; |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 102 | if (allocationMap.containsKey(hostId)) { |
| 103 | assignmentInfo = allocationMap.get(hostId).value(); |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 104 | IpAssignment.AssignmentStatus status = assignmentInfo.assignmentStatus(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 105 | Ip4Address ipAddr = assignmentInfo.ipAddress(); |
| 106 | |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 107 | if (status == IpAssignment.AssignmentStatus.Option_Assigned || |
| 108 | status == IpAssignment.AssignmentStatus.Option_Requested) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 109 | // Client has a currently Active Binding. |
| 110 | if ((ipAddr.toInt() > startIPRange.toInt()) && (ipAddr.toInt() < endIPRange.toInt())) { |
| 111 | return ipAddr; |
| 112 | } |
| 113 | |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 114 | } else if (status == IpAssignment.AssignmentStatus.Option_Expired) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 115 | // Client has a Released or Expired Binding. |
| 116 | if (freeIPPool.contains(ipAddr)) { |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 117 | assignmentInfo = IpAssignment.builder() |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 118 | .ipAddress(ipAddr) |
| 119 | .timestamp(new Date()) |
| 120 | .leasePeriod(timeoutForPendingAssignments) |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 121 | .assignmentStatus(IpAssignment.AssignmentStatus.Option_Requested) |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 122 | .build(); |
| 123 | if (freeIPPool.remove(ipAddr)) { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 124 | allocationMap.put(hostId, assignmentInfo); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 125 | return ipAddr; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | return assignmentInfo.ipAddress(); |
| 130 | |
| 131 | } else if (requestedIP.toInt() != 0) { |
| 132 | // Client has requested an IP. |
| 133 | if (freeIPPool.contains(requestedIP)) { |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 134 | assignmentInfo = IpAssignment.builder() |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 135 | .ipAddress(requestedIP) |
| 136 | .timestamp(new Date()) |
| 137 | .leasePeriod(timeoutForPendingAssignments) |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 138 | .assignmentStatus(IpAssignment.AssignmentStatus.Option_Requested) |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 139 | .build(); |
| 140 | if (freeIPPool.remove(requestedIP)) { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 141 | allocationMap.put(hostId, assignmentInfo); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 142 | return requestedIP; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // Allocate a new IP from the server's pool of available IP. |
| 148 | Ip4Address nextIPAddr = fetchNextIP(); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 149 | if (nextIPAddr != null) { |
| 150 | assignmentInfo = IpAssignment.builder() |
| 151 | .ipAddress(nextIPAddr) |
| 152 | .timestamp(new Date()) |
| 153 | .leasePeriod(timeoutForPendingAssignments) |
| 154 | .assignmentStatus(IpAssignment.AssignmentStatus.Option_Requested) |
| 155 | .build(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 156 | |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 157 | allocationMap.put(hostId, assignmentInfo); |
| 158 | } |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 159 | return nextIPAddr; |
| 160 | |
| 161 | } |
| 162 | |
| 163 | @Override |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 164 | public boolean assignIP(HostId hostId, Ip4Address ipAddr, int leaseTime) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 165 | |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 166 | IpAssignment assignmentInfo; |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 167 | if (allocationMap.containsKey(hostId)) { |
| 168 | assignmentInfo = allocationMap.get(hostId).value(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 169 | if ((assignmentInfo.ipAddress().toInt() == ipAddr.toInt()) && |
Thomas Vachuska | 0009044 | 2015-09-11 18:08:04 -0700 | [diff] [blame] | 170 | (ipAddr.toInt() >= startIPRange.toInt()) && (ipAddr.toInt() <= endIPRange.toInt())) { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 171 | |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 172 | assignmentInfo = IpAssignment.builder() |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 173 | .ipAddress(ipAddr) |
| 174 | .timestamp(new Date()) |
| 175 | .leasePeriod(leaseTime) |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 176 | .assignmentStatus(IpAssignment.AssignmentStatus.Option_Assigned) |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 177 | .build(); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 178 | allocationMap.put(hostId, assignmentInfo); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 179 | return true; |
| 180 | } |
| 181 | } else if (freeIPPool.contains(ipAddr)) { |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 182 | assignmentInfo = IpAssignment.builder() |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 183 | .ipAddress(ipAddr) |
| 184 | .timestamp(new Date()) |
| 185 | .leasePeriod(leaseTime) |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 186 | .assignmentStatus(IpAssignment.AssignmentStatus.Option_Assigned) |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 187 | .build(); |
| 188 | if (freeIPPool.remove(ipAddr)) { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 189 | allocationMap.put(hostId, assignmentInfo); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 190 | return true; |
| 191 | } |
| 192 | } |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | @Override |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 197 | public void releaseIP(HostId hostId) { |
| 198 | if (allocationMap.containsKey(hostId)) { |
| 199 | IpAssignment newAssignment = IpAssignment.builder(allocationMap.get(hostId).value()) |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 200 | .assignmentStatus(IpAssignment.AssignmentStatus.Option_Expired) |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 201 | .build(); |
| 202 | Ip4Address freeIP = newAssignment.ipAddress(); |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 203 | allocationMap.put(hostId, newAssignment); |
| 204 | if ((freeIP.toInt() > startIPRange.toInt()) && (freeIP.toInt() < endIPRange.toInt())) { |
| 205 | freeIPPool.add(freeIP); |
| 206 | } |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public void setDefaultTimeoutForPurge(int timeInSeconds) { |
| 212 | timeoutForPendingAssignments = timeInSeconds; |
| 213 | } |
| 214 | |
| 215 | @Override |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 216 | public Map<HostId, IpAssignment> listMapping() { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 217 | |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 218 | Map<HostId, IpAssignment> allMapping = new HashMap<>(); |
| 219 | for (Map.Entry<HostId, Versioned<IpAssignment>> entry: allocationMap.entrySet()) { |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 220 | IpAssignment assignment = entry.getValue().value(); |
| 221 | if (assignment.assignmentStatus() == IpAssignment.AssignmentStatus.Option_Assigned) { |
Thomas Vachuska | 54dc352 | 2015-09-09 00:11:45 -0700 | [diff] [blame] | 222 | allMapping.put(entry.getKey(), assignment); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 223 | } |
| 224 | } |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 225 | return allMapping; |
Thomas Vachuska | 54dc352 | 2015-09-09 00:11:45 -0700 | [diff] [blame] | 226 | |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public boolean assignStaticIP(MacAddress macID, Ip4Address ipAddr) { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 231 | HostId host = HostId.hostId(macID); |
| 232 | return assignIP(host, ipAddr, -1); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public boolean removeStaticIP(MacAddress macID) { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 237 | HostId host = HostId.hostId(macID); |
| 238 | if (allocationMap.containsKey(host)) { |
| 239 | IpAssignment assignment = allocationMap.get(host).value(); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 240 | Ip4Address freeIP = assignment.ipAddress(); |
| 241 | if (assignment.leasePeriod() < 0) { |
samanwita pal | 2a31340 | 2015-09-14 16:03:22 -0700 | [diff] [blame] | 242 | allocationMap.remove(host); |
| 243 | if ((freeIP.toInt() > startIPRange.toInt()) && (freeIP.toInt() < endIPRange.toInt())) { |
| 244 | freeIPPool.add(freeIP); |
| 245 | } |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 246 | return true; |
| 247 | } |
| 248 | } |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public Iterable<Ip4Address> getAvailableIPs() { |
Sho SHIMIZU | fd0933b | 2015-09-11 15:17:48 -0700 | [diff] [blame] | 254 | return ImmutableSet.copyOf(freeIPPool); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public void populateIPPoolfromRange(Ip4Address startIP, Ip4Address endIP) { |
| 259 | // Clear all entries from previous range. |
| 260 | startIPRange = startIP; |
| 261 | endIPRange = endIP; |
| 262 | freeIPPool.clear(); |
| 263 | |
| 264 | int lastIP = endIP.toInt(); |
| 265 | Ip4Address nextIP; |
| 266 | for (int loopCounter = startIP.toInt(); loopCounter <= lastIP; loopCounter++) { |
| 267 | nextIP = Ip4Address.valueOf(loopCounter); |
| 268 | freeIPPool.add(nextIP); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Fetches the next available IP from the free pool pf IPs. |
| 274 | * |
| 275 | * @return the next available IP address |
| 276 | */ |
| 277 | private Ip4Address fetchNextIP() { |
| 278 | for (Ip4Address freeIP : freeIPPool) { |
| 279 | if (freeIPPool.remove(freeIP)) { |
| 280 | return freeIP; |
| 281 | } |
| 282 | } |
| 283 | return null; |
| 284 | } |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 285 | } |