aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DistributedDhcpStore.java
diff options
context:
space:
mode:
authorAshlee Young <ashlee@onosfw.com>2015-09-22 12:49:09 -0700
committerAshlee Young <ashlee@onosfw.com>2015-09-22 12:49:09 -0700
commit81391595dca425ae58e2294898f09f11d9a32dbc (patch)
treef5d65c39a732150b2b29daa8de98a35d1236d3fb /framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DistributedDhcpStore.java
parent0aa37e73dcb3a55b8d889b0c32ff74055551b1f3 (diff)
bringing src to commit tag 65d551b50e782b0c1ea76c1a9ed1c5a801a5a7e4
Change-Id: Ib2da78962eaef856f418636c31b0f5c84286244f
Diffstat (limited to 'framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DistributedDhcpStore.java')
-rw-r--r--framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DistributedDhcpStore.java186
1 files changed, 94 insertions, 92 deletions
diff --git a/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DistributedDhcpStore.java b/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DistributedDhcpStore.java
index 6e29216a..dbdadb34 100644
--- a/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DistributedDhcpStore.java
+++ b/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DistributedDhcpStore.java
@@ -22,14 +22,12 @@ import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.Service;
-import org.jboss.netty.util.Timeout;
-import org.jboss.netty.util.TimerTask;
import org.onlab.packet.Ip4Address;
import org.onlab.packet.MacAddress;
import org.onlab.util.KryoNamespace;
-import org.onlab.util.Timer;
import org.onosproject.dhcp.DhcpStore;
import org.onosproject.dhcp.IpAssignment;
+import org.onosproject.net.HostId;
import org.onosproject.store.serializers.KryoNamespaces;
import org.onosproject.store.service.ConsistentMap;
import org.onosproject.store.service.DistributedSet;
@@ -42,7 +40,7 @@ import org.slf4j.LoggerFactory;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
-import java.util.concurrent.TimeUnit;
+import java.util.Objects;
/**
* Manages the pool of available IP Addresses in the network and
@@ -58,25 +56,21 @@ public class DistributedDhcpStore implements DhcpStore {
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected StorageService storageService;
- private ConsistentMap<MacAddress, IpAssignment> allocationMap;
+ private ConsistentMap<HostId, IpAssignment> allocationMap;
private DistributedSet<Ip4Address> freeIPPool;
- private Timeout timeout;
-
private static Ip4Address startIPRange;
private static Ip4Address endIPRange;
// Hardcoded values are default values.
- private static int timerDelay = 2;
-
private static int timeoutForPendingAssignments = 60;
@Activate
protected void activate() {
- allocationMap = storageService.<MacAddress, IpAssignment>consistentMapBuilder()
+ allocationMap = storageService.<HostId, IpAssignment>consistentMapBuilder()
.withName("onos-dhcp-assignedIP")
.withSerializer(Serializer.using(
new KryoNamespace.Builder()
@@ -94,30 +88,27 @@ public class DistributedDhcpStore implements DhcpStore {
.withSerializer(Serializer.using(KryoNamespaces.API))
.build();
- timeout = Timer.getTimer().newTimeout(new PurgeListTask(), timerDelay, TimeUnit.MINUTES);
-
log.info("Started");
}
@Deactivate
protected void deactivate() {
- timeout.cancel();
log.info("Stopped");
}
@Override
- public Ip4Address suggestIP(MacAddress macID, Ip4Address requestedIP) {
+ public Ip4Address suggestIP(HostId hostId, Ip4Address requestedIP) {
IpAssignment assignmentInfo;
- if (allocationMap.containsKey(macID)) {
- assignmentInfo = allocationMap.get(macID).value();
+ if (allocationMap.containsKey(hostId)) {
+ assignmentInfo = allocationMap.get(hostId).value();
IpAssignment.AssignmentStatus status = assignmentInfo.assignmentStatus();
Ip4Address ipAddr = assignmentInfo.ipAddress();
if (status == IpAssignment.AssignmentStatus.Option_Assigned ||
status == IpAssignment.AssignmentStatus.Option_Requested) {
// Client has a currently Active Binding.
- if ((ipAddr.toInt() > startIPRange.toInt()) && (ipAddr.toInt() < endIPRange.toInt())) {
+ if (ipWithinRange(ipAddr)) {
return ipAddr;
}
@@ -131,13 +122,11 @@ public class DistributedDhcpStore implements DhcpStore {
.assignmentStatus(IpAssignment.AssignmentStatus.Option_Requested)
.build();
if (freeIPPool.remove(ipAddr)) {
- allocationMap.put(macID, assignmentInfo);
+ allocationMap.put(hostId, assignmentInfo);
return ipAddr;
}
}
}
- return assignmentInfo.ipAddress();
-
} else if (requestedIP.toInt() != 0) {
// Client has requested an IP.
if (freeIPPool.contains(requestedIP)) {
@@ -148,7 +137,7 @@ public class DistributedDhcpStore implements DhcpStore {
.assignmentStatus(IpAssignment.AssignmentStatus.Option_Requested)
.build();
if (freeIPPool.remove(requestedIP)) {
- allocationMap.put(macID, assignmentInfo);
+ allocationMap.put(hostId, assignmentInfo);
return requestedIP;
}
}
@@ -156,35 +145,56 @@ public class DistributedDhcpStore implements DhcpStore {
// Allocate a new IP from the server's pool of available IP.
Ip4Address nextIPAddr = fetchNextIP();
- assignmentInfo = IpAssignment.builder()
- .ipAddress(nextIPAddr)
- .timestamp(new Date())
- .leasePeriod(timeoutForPendingAssignments)
- .assignmentStatus(IpAssignment.AssignmentStatus.Option_Requested)
- .build();
+ if (nextIPAddr != null) {
+ assignmentInfo = IpAssignment.builder()
+ .ipAddress(nextIPAddr)
+ .timestamp(new Date())
+ .leasePeriod(timeoutForPendingAssignments)
+ .assignmentStatus(IpAssignment.AssignmentStatus.Option_Requested)
+ .build();
- allocationMap.put(macID, assignmentInfo);
+ allocationMap.put(hostId, assignmentInfo);
+ }
return nextIPAddr;
}
@Override
- public boolean assignIP(MacAddress macID, Ip4Address ipAddr, int leaseTime) {
+ public boolean assignIP(HostId hostId, Ip4Address ipAddr, int leaseTime) {
IpAssignment assignmentInfo;
- if (allocationMap.containsKey(macID)) {
- assignmentInfo = allocationMap.get(macID).value();
- if ((assignmentInfo.ipAddress().toInt() == ipAddr.toInt()) &&
- (ipAddr.toInt() > startIPRange.toInt()) && (ipAddr.toInt() < endIPRange.toInt())) {
+ if (allocationMap.containsKey(hostId)) {
+ assignmentInfo = allocationMap.get(hostId).value();
+ IpAssignment.AssignmentStatus status = assignmentInfo.assignmentStatus();
- assignmentInfo = IpAssignment.builder()
- .ipAddress(ipAddr)
- .timestamp(new Date())
- .leasePeriod(leaseTime)
- .assignmentStatus(IpAssignment.AssignmentStatus.Option_Assigned)
- .build();
- allocationMap.put(macID, assignmentInfo);
- return true;
+ if (Objects.equals(assignmentInfo.ipAddress(), ipAddr) && ipWithinRange(ipAddr)) {
+
+ if (status == IpAssignment.AssignmentStatus.Option_Assigned ||
+ status == IpAssignment.AssignmentStatus.Option_Requested) {
+ // Client has a currently active binding with the server.
+ assignmentInfo = IpAssignment.builder()
+ .ipAddress(ipAddr)
+ .timestamp(new Date())
+ .leasePeriod(leaseTime)
+ .assignmentStatus(IpAssignment.AssignmentStatus.Option_Assigned)
+ .build();
+ allocationMap.put(hostId, assignmentInfo);
+ return true;
+ } else if (status == IpAssignment.AssignmentStatus.Option_Expired) {
+ // Client has an expired binding with the server.
+ if (freeIPPool.contains(ipAddr)) {
+ assignmentInfo = IpAssignment.builder()
+ .ipAddress(ipAddr)
+ .timestamp(new Date())
+ .leasePeriod(leaseTime)
+ .assignmentStatus(IpAssignment.AssignmentStatus.Option_Assigned)
+ .build();
+ if (freeIPPool.remove(ipAddr)) {
+ allocationMap.put(hostId, assignmentInfo);
+ return true;
+ }
+ }
+ }
}
} else if (freeIPPool.contains(ipAddr)) {
assignmentInfo = IpAssignment.builder()
@@ -194,7 +204,7 @@ public class DistributedDhcpStore implements DhcpStore {
.assignmentStatus(IpAssignment.AssignmentStatus.Option_Assigned)
.build();
if (freeIPPool.remove(ipAddr)) {
- allocationMap.put(macID, assignmentInfo);
+ allocationMap.put(hostId, assignmentInfo);
return true;
}
}
@@ -202,14 +212,16 @@ public class DistributedDhcpStore implements DhcpStore {
}
@Override
- public void releaseIP(MacAddress macID) {
- if (allocationMap.containsKey(macID)) {
- IpAssignment newAssignment = IpAssignment.builder(allocationMap.get(macID).value())
+ public void releaseIP(HostId hostId) {
+ if (allocationMap.containsKey(hostId)) {
+ IpAssignment newAssignment = IpAssignment.builder(allocationMap.get(hostId).value())
.assignmentStatus(IpAssignment.AssignmentStatus.Option_Expired)
.build();
Ip4Address freeIP = newAssignment.ipAddress();
- allocationMap.put(macID, newAssignment);
- freeIPPool.add(freeIP);
+ allocationMap.put(hostId, newAssignment);
+ if (ipWithinRange(freeIP)) {
+ freeIPPool.add(freeIP);
+ }
}
}
@@ -219,37 +231,45 @@ public class DistributedDhcpStore implements DhcpStore {
}
@Override
- public void setTimerDelay(int timeInSeconds) {
- timerDelay = timeInSeconds;
- }
+ public Map<HostId, IpAssignment> listAssignedMapping() {
- @Override
- public Map<MacAddress, IpAssignment> listMapping() {
-
- Map<MacAddress, IpAssignment> allMapping = new HashMap<>();
- for (Map.Entry<MacAddress, Versioned<IpAssignment>> entry: allocationMap.entrySet()) {
- IpAssignment assignment = entry.getValue().value();
+ Map<HostId, IpAssignment> validMapping = new HashMap<>();
+ IpAssignment assignment;
+ for (Map.Entry<HostId, Versioned<IpAssignment>> entry: allocationMap.entrySet()) {
+ assignment = entry.getValue().value();
if (assignment.assignmentStatus() == IpAssignment.AssignmentStatus.Option_Assigned) {
- allMapping.put(entry.getKey(), assignment);
+ validMapping.put(entry.getKey(), assignment);
}
}
- return allMapping;
+ return validMapping;
+ }
+ @Override
+ public Map<HostId, IpAssignment> listAllMapping() {
+ Map<HostId, IpAssignment> validMapping = new HashMap<>();
+ for (Map.Entry<HostId, Versioned<IpAssignment>> entry: allocationMap.entrySet()) {
+ validMapping.put(entry.getKey(), entry.getValue().value());
+ }
+ return validMapping;
}
@Override
public boolean assignStaticIP(MacAddress macID, Ip4Address ipAddr) {
- return assignIP(macID, ipAddr, -1);
+ HostId host = HostId.hostId(macID);
+ return assignIP(host, ipAddr, -1);
}
@Override
public boolean removeStaticIP(MacAddress macID) {
- if (allocationMap.containsKey(macID)) {
- IpAssignment assignment = allocationMap.get(macID).value();
+ HostId host = HostId.hostId(macID);
+ if (allocationMap.containsKey(host)) {
+ IpAssignment assignment = allocationMap.get(host).value();
Ip4Address freeIP = assignment.ipAddress();
if (assignment.leasePeriod() < 0) {
- allocationMap.remove(macID);
- freeIPPool.add(freeIP);
+ allocationMap.remove(host);
+ if (ipWithinRange(freeIP)) {
+ freeIPPool.add(freeIP);
+ }
return true;
}
}
@@ -258,15 +278,16 @@ public class DistributedDhcpStore implements DhcpStore {
@Override
public Iterable<Ip4Address> getAvailableIPs() {
- return ImmutableSet.<Ip4Address>copyOf(freeIPPool);
+ return ImmutableSet.copyOf(freeIPPool);
}
@Override
public void populateIPPoolfromRange(Ip4Address startIP, Ip4Address endIP) {
// Clear all entries from previous range.
+ allocationMap.clear();
+ freeIPPool.clear();
startIPRange = startIP;
endIPRange = endIP;
- freeIPPool.clear();
int lastIP = endIP.toInt();
Ip4Address nextIP;
@@ -291,34 +312,15 @@ public class DistributedDhcpStore implements DhcpStore {
}
/**
- * Purges the IP allocation map to remove expired entries and returns the freed IPs to the free pool.
+ * Returns true if the given ip is within the range of available IPs.
+ *
+ * @param ip given ip address
+ * @return true if within range, false otherwise
*/
- private class PurgeListTask implements TimerTask {
-
- @Override
- public void run(Timeout to) {
- IpAssignment ipAssignment, newAssignment;
- Date dateNow = new Date();
- for (Map.Entry<MacAddress, Versioned<IpAssignment>> entry: allocationMap.entrySet()) {
- ipAssignment = entry.getValue().value();
- long timeLapsed = dateNow.getTime() - ipAssignment.timestamp().getTime();
- if ((ipAssignment.assignmentStatus() != IpAssignment.AssignmentStatus.Option_Expired) &&
- (ipAssignment.leasePeriod() > 0) && (timeLapsed > (ipAssignment.leasePeriod()))) {
- Ip4Address freeIP = ipAssignment.ipAddress();
-
- newAssignment = IpAssignment.builder(ipAssignment)
- .assignmentStatus(IpAssignment.AssignmentStatus.Option_Expired)
- .build();
- allocationMap.put(entry.getKey(), newAssignment);
-
- if ((freeIP.toInt() > startIPRange.toInt()) && (freeIP.toInt() < endIPRange.toInt())) {
- freeIPPool.add(freeIP);
- }
- }
- }
- timeout = Timer.getTimer().newTimeout(new PurgeListTask(), timerDelay, TimeUnit.MINUTES);
+ private boolean ipWithinRange(Ip4Address ip) {
+ if ((ip.toInt() >= startIPRange.toInt()) && (ip.toInt() <= endIPRange.toInt())) {
+ return true;
}
-
+ return false;
}
-
}