daniel
Committed by Gerrit Code Review

Fix DhcpService

 - boolean 'fromOpenstack' is changed to 'rangeNotEnforced'
 - rebased with master

Change-Id: Ida2828367339cdb0129f1555df9e1c90e6da6975
......@@ -58,15 +58,15 @@ public interface DhcpService {
/**
* Registers a static IP mapping with the DHCP Server.
* Supports the request from OpenStack
* Supports rangeNotEnforced option
*
* @param macID macID of the client
* @param ipAddress IP Address requested for the client
* @param fromOpenStack true if the request is from OpenStack
* @param addressList subnetMask, DHCP/Router/Domain Server IP Address if the request from OpenStack
* @param rangeNotEnforced true if rangeNotEnforced was set and the mapping will be eternal
* @param addressList subnetMask, DHCP/Router/DNS IP Addresses if rangeNotEnforced was set
* @return true if the mapping was successfully added, false otherwise
*/
boolean setStaticMapping(MacAddress macID, Ip4Address ipAddress, boolean fromOpenStack,
boolean setStaticMapping(MacAddress macID, Ip4Address ipAddress, boolean rangeNotEnforced,
List<Ip4Address> addressList);
/**
......
......@@ -52,12 +52,11 @@ public interface DhcpStore {
* @param hostId Host Id of the client requesting an IP
* @param ipAddr IP Address being requested
* @param leaseTime Lease time offered by the server for this mapping
* @param fromOpenStack true if the request is from Openstack
* @param addressList subnetMask, DHCP IP Address, Router IP Address, Domain Server IP Address if the request
* from OpenStack
* @param rangeNotEnforced true if rangeNotEnforced was set
* @param addressList subnetMask, DHCP/Router/DNS IP Addresses if rangeNotEnforced was set
* @return returns true if the assignment was successful, false otherwise
*/
boolean assignIP(HostId hostId, Ip4Address ipAddr, int leaseTime, boolean fromOpenStack,
boolean assignIP(HostId hostId, Ip4Address ipAddr, int leaseTime, boolean rangeNotEnforced,
List<Ip4Address> addressList);
......@@ -95,11 +94,11 @@ public interface DhcpStore {
*
* @param macID macID of the client
* @param ipAddr IP Address requested for the client
* @param fromOpenStack true if the request is from Openstack
* @param addressList subnetMask, DHCP/Router/Domain Server IP Address if the request from OpenStack
* @param rangeNotEnforced true if rangeNotEnforced was set
* @param addressList subnetMask, DHCP/Router/DNS IP Addresses rangeNotEnforced was set
* @return true if the mapping was successfully registered, false otherwise
*/
boolean assignStaticIP(MacAddress macID, Ip4Address ipAddr, boolean fromOpenStack, List<Ip4Address> addressList);
boolean assignStaticIP(MacAddress macID, Ip4Address ipAddr, boolean rangeNotEnforced, List<Ip4Address> addressList);
/**
* Removes a static IP mapping associated with the given MAC ID from the DHCP Server.
......
......@@ -41,7 +41,7 @@ public final class IpAssignment {
private final Ip4Address domainServer;
private final boolean fromOpenStack;
private final boolean rangeNotEnforced;
private final AssignmentStatus assignmentStatus;
......@@ -54,7 +54,7 @@ public final class IpAssignment {
/**
* IP Assignment has been requested by a OpenStack.
*/
Option_Requested_From_OpenStack,
Option_RangeNotEnforced,
/**
* IP has been assigned to a host.
*/
......@@ -78,13 +78,13 @@ public final class IpAssignment {
* @param dhcpServer
* @param routerAddress
* @param domainServer
* @param fromOpenStack
* @param rangeNotEnforced
*/
private IpAssignment(Ip4Address ipAddress,
long leasePeriod,
Date timestamp,
AssignmentStatus assignmentStatus, Ip4Address subnetMask, Ip4Address dhcpServer,
Ip4Address routerAddress, Ip4Address domainServer, boolean fromOpenStack) {
Ip4Address routerAddress, Ip4Address domainServer, boolean rangeNotEnforced) {
this.ipAddress = ipAddress;
this.leasePeriod = leasePeriod;
this.timestamp = timestamp;
......@@ -93,7 +93,7 @@ public final class IpAssignment {
this.dhcpServer = dhcpServer;
this.routerAddress = routerAddress;
this.domainServer = domainServer;
this.fromOpenStack = fromOpenStack;
this.rangeNotEnforced = rangeNotEnforced;
}
/**
......@@ -157,8 +157,8 @@ public final class IpAssignment {
return domainServer;
}
public boolean fromOpenStack() {
return fromOpenStack;
public boolean rangeNotEnforced() {
return rangeNotEnforced;
}
@Override
......@@ -172,7 +172,7 @@ public final class IpAssignment {
.add("dhcpServer", dhcpServer)
.add("routerAddress", routerAddress)
.add("domainServer", domainServer)
.add("fromOpenStack", fromOpenStack)
.add("rangeNotEnforced", rangeNotEnforced)
.toString();
}
......@@ -216,7 +216,7 @@ public final class IpAssignment {
private Ip4Address routerAddress;
private boolean fromOpenStack = false;
private boolean rangeNotEnforced = false;
private Builder() {
......@@ -232,7 +232,7 @@ public final class IpAssignment {
public IpAssignment build() {
validateInputs();
return new IpAssignment(ipAddress, leasePeriod, timeStamp, assignmentStatus, subnetMask,
dhcpServer, domainServer, routerAddress, fromOpenStack);
dhcpServer, domainServer, routerAddress, rangeNotEnforced);
}
public Builder ipAddress(Ip4Address addr) {
......@@ -275,8 +275,8 @@ public final class IpAssignment {
return this;
}
public Builder fromOpenStack(boolean fromOpenStack) {
this.fromOpenStack = fromOpenStack;
public Builder rangeNotEnforced(boolean rangeNotEnforced) {
this.rangeNotEnforced = rangeNotEnforced;
return this;
}
......@@ -287,16 +287,16 @@ public final class IpAssignment {
checkNotNull(leasePeriod, "Lease Period must be specified");
checkNotNull(timeStamp, "Timestamp must be specified");
if (fromOpenStack) {
checkNotNull(subnetMask, "subnetMask must be specified in case of OpenStack");
checkNotNull(dhcpServer, "dhcpServer must be specified in case of OpenStack");
checkNotNull(domainServer, "domainServer must be specified in case of OpenStack");
checkNotNull(routerAddress, "routerAddress must be specified in case of OpenStack");
if (rangeNotEnforced) {
checkNotNull(subnetMask, "subnetMask must be specified in case of rangeNotEnforced");
checkNotNull(dhcpServer, "dhcpServer must be specified in case of rangeNotEnforced");
checkNotNull(domainServer, "domainServer must be specified in case of rangeNotEnforced");
checkNotNull(routerAddress, "routerAddress must be specified in case of rangeNotEnforced");
}
switch (assignmentStatus) {
case Option_Requested:
case Option_Requested_From_OpenStack:
case Option_RangeNotEnforced:
case Option_Assigned:
case Option_Expired:
break;
......
......@@ -241,12 +241,12 @@ public class DhcpManager implements DhcpService {
}
@Override
public boolean setStaticMapping(MacAddress macID, Ip4Address ipAddress, boolean fromOpenStack,
public boolean setStaticMapping(MacAddress macID, Ip4Address ipAddress, boolean rangeNotEnforced,
List<Ip4Address> addressList) {
log.debug("setStaticMapping is called with Mac: {}, Ip: {} addressList: {}",
macID.toString(), ipAddress.toString(), addressList.toString());
return dhcpStore.assignStaticIP(macID, ipAddress, fromOpenStack, addressList);
return dhcpStore.assignStaticIP(macID, ipAddress, rangeNotEnforced, addressList);
}
@Override
......@@ -279,7 +279,7 @@ public class DhcpManager implements DhcpService {
ipAssignment = dhcpStore.getIpAssignmentFromAllocationMap(HostId.hostId(packet.getSourceMAC()));
if (ipAssignment != null && ipAssignment.fromOpenStack()) {
if (ipAssignment != null && ipAssignment.rangeNotEnforced()) {
subnetMaskReply = ipAssignment.subnetMask();
dhcpServerReply = ipAssignment.dhcpServer();
domainServerReply = ipAssignment.domainServer();
......@@ -484,8 +484,9 @@ public class DhcpManager implements DhcpService {
if (flagIfServerIP && flagIfRequestedIP) {
// SELECTING state
if (dhcpStore.getIpAssignmentFromAllocationMap(HostId.hostId(clientMac))
.fromOpenStack()) {
.rangeNotEnforced()) {
outgoingPacketType = DHCPPacketType.DHCPACK;
Ethernet ethReply = buildReply(packet, requestedIP, (byte) outgoingPacketType.getValue());
sendReply(context, ethReply);
......
......@@ -106,7 +106,7 @@ public class DistributedDhcpStore implements DhcpStore {
IpAssignment.AssignmentStatus status = assignmentInfo.assignmentStatus();
Ip4Address ipAddr = assignmentInfo.ipAddress();
if (assignmentInfo.fromOpenStack()) {
if (assignmentInfo.rangeNotEnforced()) {
return assignmentInfo.ipAddress();
} else if (status == IpAssignment.AssignmentStatus.Option_Assigned ||
status == IpAssignment.AssignmentStatus.Option_Requested) {
......@@ -163,12 +163,15 @@ public class DistributedDhcpStore implements DhcpStore {
}
@Override
public boolean assignIP(HostId hostId, Ip4Address ipAddr, int leaseTime, boolean fromOpenStack,
public boolean assignIP(HostId hostId, Ip4Address ipAddr, int leaseTime, boolean rangeNotEnforced,
List<Ip4Address> addressList) {
IpAssignment assignmentInfo;
log.debug("Assign IP Called w/ Ip4Address: {}, HostId: {}", ipAddr.toString(), hostId.mac().toString());
if (allocationMap.containsKey(hostId)) {
assignmentInfo = allocationMap.get(hostId).value();
IpAssignment.AssignmentStatus status = assignmentInfo.assignmentStatus();
......@@ -212,17 +215,17 @@ public class DistributedDhcpStore implements DhcpStore {
allocationMap.put(hostId, assignmentInfo);
return true;
}
} else if (fromOpenStack) {
} else if (rangeNotEnforced) {
assignmentInfo = IpAssignment.builder()
.ipAddress(ipAddr)
.timestamp(new Date())
.leasePeriod(leaseTime)
.fromOpenStack(true)
.assignmentStatus(IpAssignment.AssignmentStatus.Option_Requested_From_OpenStack)
.rangeNotEnforced(true)
.assignmentStatus(IpAssignment.AssignmentStatus.Option_RangeNotEnforced)
.subnetMask((Ip4Address) addressList.toArray()[0])
.dhcpServer((Ip4Address) addressList.toArray()[1])
.domainServer((Ip4Address) addressList.toArray()[2])
.routerAddress((Ip4Address) addressList.toArray()[3])
.routerAddress((Ip4Address) addressList.toArray()[2])
.domainServer((Ip4Address) addressList.toArray()[3])
.build();
allocationMap.put(hostId, assignmentInfo);
return true;
......@@ -259,7 +262,7 @@ public class DistributedDhcpStore implements DhcpStore {
for (Map.Entry<HostId, Versioned<IpAssignment>> entry: allocationMap.entrySet()) {
assignment = entry.getValue().value();
if (assignment.assignmentStatus() == IpAssignment.AssignmentStatus.Option_Assigned
|| assignment.assignmentStatus() == IpAssignment.AssignmentStatus.Option_Requested_From_OpenStack) {
|| assignment.assignmentStatus() == IpAssignment.AssignmentStatus.Option_RangeNotEnforced) {
validMapping.put(entry.getKey(), assignment);
}
}
......@@ -276,10 +279,10 @@ public class DistributedDhcpStore implements DhcpStore {
}
@Override
public boolean assignStaticIP(MacAddress macID, Ip4Address ipAddr, boolean fromOpenStack,
public boolean assignStaticIP(MacAddress macID, Ip4Address ipAddr, boolean rangeNotEnforced,
List<Ip4Address> addressList) {
HostId host = HostId.hostId(macID);
return assignIP(host, ipAddr, -1, fromOpenStack, addressList);
return assignIP(host, ipAddr, -1, rangeNotEnforced, addressList);
}
@Override
......@@ -287,6 +290,12 @@ public class DistributedDhcpStore implements DhcpStore {
HostId host = HostId.hostId(macID);
if (allocationMap.containsKey(host)) {
IpAssignment assignment = allocationMap.get(host).value();
if (assignment.rangeNotEnforced()) {
allocationMap.remove(host);
return true;
}
Ip4Address freeIP = assignment.ipAddress();
if (assignment.leasePeriod() < 0) {
allocationMap.remove(host);
......