samanwita pal
Committed by Gerrit Code Review

DHCP Config changes + null pointer checks + ONOS-2881

Change-Id: Ice391f539ae816329fde7970d762380a36fd7661
......@@ -17,6 +17,7 @@ package org.onosproject.dhcp;
import org.onlab.packet.Ip4Address;
import org.onlab.packet.MacAddress;
import org.onosproject.net.HostId;
import java.util.Map;
......@@ -30,7 +31,7 @@ public interface DhcpService {
*
* @return collection of mappings.
*/
Map<MacAddress, IpAssignment> listMapping();
Map<HostId, IpAssignment> listMapping();
/**
* Returns the default lease time granted by the DHCP Server.
......
......@@ -17,6 +17,7 @@ package org.onosproject.dhcp;
import org.onlab.packet.Ip4Address;
import org.onlab.packet.MacAddress;
import org.onosproject.net.HostId;
import java.util.Map;
......@@ -36,21 +37,21 @@ public interface DhcpStore {
/**
* Returns an IP Address for a Mac ID, in response to a DHCP DISCOVER message.
*
* @param macID Mac ID of the client requesting an IP
* @param hostId Host ID of the client requesting an IP
* @param requestedIP requested IP address
* @return IP address assigned to the Mac ID
*/
Ip4Address suggestIP(MacAddress macID, Ip4Address requestedIP);
Ip4Address suggestIP(HostId hostId, Ip4Address requestedIP);
/**
* Assigns the requested IP to the Mac ID, in response to a DHCP REQUEST message.
*
* @param macID Mac Id of the client requesting an IP
* @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
* @return returns true if the assignment was successful, false otherwise
*/
boolean assignIP(MacAddress macID, Ip4Address ipAddr, int leaseTime);
boolean assignIP(HostId hostId, Ip4Address ipAddr, int leaseTime);
/**
* Sets the default time for which suggested IP mappings are valid.
......@@ -60,25 +61,18 @@ public interface DhcpStore {
void setDefaultTimeoutForPurge(int timeInSeconds);
/**
* Sets the delay after which the dhcp server will purge expired entries.
*
* @param timeInSeconds default time
*/
void setTimerDelay(int timeInSeconds);
/**
* Releases the IP assigned to a Mac ID into the free pool.
*
* @param macID the macID for which the mapping needs to be changed
* @param hostId the host ID for which the mapping needs to be changed
*/
void releaseIP(MacAddress macID);
void releaseIP(HostId hostId);
/**
* Returns a collection of all the MacAddress to IPAddress mapping.
*
* @return the collection of the mappings
*/
Map<MacAddress, IpAssignment> listMapping();
Map<HostId, IpAssignment> listMapping();
/**
* Assigns the requested IP to the MAC ID (if available) for an indefinite period of time.
......
......@@ -100,10 +100,19 @@ public final class IpAssignment {
/**
* Returns the lease period of the IP assignment.
*
* @return the lease period
* @return the lease period in seconds
*/
public int leasePeriod() {
return (int) this.leasePeriod / 1000;
return (int) this.leasePeriod;
}
/**
* Returns the lease period of the IP assignment.
*
* @return the lease period in milliseconds
*/
public int leasePeriodMs() {
return (int) this.leasePeriod * 1000;
}
@Override
......@@ -155,7 +164,7 @@ public final class IpAssignment {
private Builder(IpAssignment ipAssignment) {
ipAddress = ipAssignment.ipAddress();
timeStamp = ipAssignment.timestamp();
leasePeriod = ipAssignment.leasePeriod() * 1000;
leasePeriod = ipAssignment.leasePeriod();
assignmentStatus = ipAssignment.assignmentStatus();
}
......@@ -178,7 +187,7 @@ public final class IpAssignment {
}
public Builder leasePeriod(int leasePeriodinSeconds) {
leasePeriod = leasePeriodinSeconds * 1000;
leasePeriod = leasePeriodinSeconds;
return this;
}
......
......@@ -16,10 +16,10 @@
package org.onosproject.dhcp.cli;
import org.apache.karaf.shell.commands.Command;
import org.onlab.packet.MacAddress;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.dhcp.DhcpService;
import org.onosproject.dhcp.IpAssignment;
import org.onosproject.net.HostId;
import java.util.Map;
......@@ -35,9 +35,9 @@ public class DhcpListAllMappings extends AbstractShellCommand {
protected void execute() {
DhcpService dhcpService = AbstractShellCommand.get(DhcpService.class);
Map<MacAddress, IpAssignment> allocationMap = dhcpService.listMapping();
Map<HostId, IpAssignment> allocationMap = dhcpService.listMapping();
for (Map.Entry<MacAddress, IpAssignment> entry : allocationMap.entrySet()) {
for (Map.Entry<HostId, IpAssignment> entry : allocationMap.entrySet()) {
print(DHCP_MAPPING_FORMAT, entry.getKey().toString(), entry.getValue().ipAddress().toString());
}
}
......
......@@ -15,6 +15,8 @@
*/
package org.onosproject.dhcp.impl;
import org.onlab.packet.Ip4Address;
import org.onlab.packet.MacAddress;
import org.onosproject.core.ApplicationId;
import org.onosproject.net.config.Config;
import org.onosproject.net.config.basics.BasicElementConfig;
......@@ -34,14 +36,19 @@ public class DhcpConfig extends Config<ApplicationId> {
public static final String LEASE_TIME = "lease";
public static final String RENEW_TIME = "renew";
public static final String REBIND_TIME = "rebind";
public static final String TIMER_DELAY = "delay";
public static final String DEFAULT_TIMEOUT = "timeout";
public static final String START_IP = "startip";
public static final String END_IP = "endip";
/**
* Returns the dhcp server ip.
*
* @return ip address or null if not set
*/
public String ip() {
return get(MY_IP, null);
public Ip4Address ip() {
String ip = get(MY_IP, null);
return ip != null ? Ip4Address.valueOf(ip) : null;
}
/**
......@@ -59,8 +66,9 @@ public class DhcpConfig extends Config<ApplicationId> {
*
* @return server mac or null if not set
*/
public String mac() {
return get(MY_MAC, null);
public MacAddress mac() {
String mac = get(MY_MAC, null);
return mac != null ? MacAddress.valueOf(mac) : null;
}
/**
......@@ -78,8 +86,9 @@ public class DhcpConfig extends Config<ApplicationId> {
*
* @return subnet mask or null if not set
*/
public String subnetMask() {
return get(SUBNET_MASK, null);
public Ip4Address subnetMask() {
String ip = get(SUBNET_MASK, null);
return ip != null ? Ip4Address.valueOf(ip) : null;
}
/**
......@@ -97,8 +106,9 @@ public class DhcpConfig extends Config<ApplicationId> {
*
* @return broadcast address or null if not set
*/
public String broadcastAddress() {
return get(BROADCAST_ADDRESS, null);
public Ip4Address broadcastAddress() {
String ip = get(BROADCAST_ADDRESS, null);
return ip != null ? Ip4Address.valueOf(ip) : null;
}
/**
......@@ -116,8 +126,8 @@ public class DhcpConfig extends Config<ApplicationId> {
*
* @return ttl or null if not set
*/
public String ttl() {
return get(TTL, null);
public int ttl() {
return get(TTL, 0);
}
/**
......@@ -126,7 +136,7 @@ public class DhcpConfig extends Config<ApplicationId> {
* @param ttl new ttl; null to clear
* @return self
*/
public BasicElementConfig ttl(String ttl) {
public BasicElementConfig ttl(int ttl) {
return (BasicElementConfig) setOrClear(TTL, ttl);
}
......@@ -135,8 +145,8 @@ public class DhcpConfig extends Config<ApplicationId> {
*
* @return lease time or null if not set
*/
public String leaseTime() {
return get(LEASE_TIME, null);
public int leaseTime() {
return get(LEASE_TIME, 0);
}
/**
......@@ -145,7 +155,7 @@ public class DhcpConfig extends Config<ApplicationId> {
* @param lease new lease time; null to clear
* @return self
*/
public BasicElementConfig leaseTime(String lease) {
public BasicElementConfig leaseTime(int lease) {
return (BasicElementConfig) setOrClear(LEASE_TIME, lease);
}
......@@ -154,8 +164,8 @@ public class DhcpConfig extends Config<ApplicationId> {
*
* @return renew time or null if not set
*/
public String renewTime() {
return get(RENEW_TIME, null);
public int renewTime() {
return get(RENEW_TIME, 0);
}
/**
......@@ -164,7 +174,7 @@ public class DhcpConfig extends Config<ApplicationId> {
* @param renew new renew time; null to clear
* @return self
*/
public BasicElementConfig renewTime(String renew) {
public BasicElementConfig renewTime(int renew) {
return (BasicElementConfig) setOrClear(RENEW_TIME, renew);
}
......@@ -173,8 +183,8 @@ public class DhcpConfig extends Config<ApplicationId> {
*
* @return rebind time or null if not set
*/
public String rebindTime() {
return get(REBIND_TIME, null);
public int rebindTime() {
return get(REBIND_TIME, 0);
}
/**
......@@ -183,7 +193,7 @@ public class DhcpConfig extends Config<ApplicationId> {
* @param rebind new rebind time; null to clear
* @return self
*/
public BasicElementConfig rebindTime(String rebind) {
public BasicElementConfig rebindTime(int rebind) {
return (BasicElementConfig) setOrClear(REBIND_TIME, rebind);
}
......@@ -192,8 +202,9 @@ public class DhcpConfig extends Config<ApplicationId> {
*
* @return router address or null if not set
*/
public String routerAddress() {
return get(ROUTER_ADDRESS, null);
public Ip4Address routerAddress() {
String ip = get(ROUTER_ADDRESS, null);
return ip != null ? Ip4Address.valueOf(ip) : null;
}
/**
......@@ -211,8 +222,9 @@ public class DhcpConfig extends Config<ApplicationId> {
*
* @return domain server address or null if not set
*/
public String domainServer() {
return get(DOMAIN_SERVER, null);
public Ip4Address domainServer() {
String ip = get(DOMAIN_SERVER, null);
return ip != null ? Ip4Address.valueOf(ip) : null;
}
/**
......@@ -224,4 +236,82 @@ public class DhcpConfig extends Config<ApplicationId> {
public BasicElementConfig domainServer(String domain) {
return (BasicElementConfig) setOrClear(DOMAIN_SERVER, domain);
}
/**
* Returns the delay after which the dhcp server will purge expired entries.
*
* @return time delay or null if not set
*/
public int timerDelay() {
return get(TIMER_DELAY, 0);
}
/**
* Sets the delay after which the dhcp server will purge expired entries.
*
* @param delay new time delay; null to clear
* @return self
*/
public BasicElementConfig timerDelay(int delay) {
return (BasicElementConfig) setOrClear(TIMER_DELAY, delay);
}
/**
* Returns the default timeout for pending assignments.
*
* @return default timeout or null if not set
*/
public int defaultTimeout() {
return get(DEFAULT_TIMEOUT, 0);
}
/**
* Sets the default timeout for pending assignments.
*
* @param defaultTimeout new default timeout; null to clear
* @return self
*/
public BasicElementConfig defaultTimeout(int defaultTimeout) {
return (BasicElementConfig) setOrClear(DEFAULT_TIMEOUT, defaultTimeout);
}
/**
* Returns the start IP for the available IP Range.
*
* @return start IP or null if not set
*/
public Ip4Address startIp() {
String ip = get(START_IP, null);
return ip != null ? Ip4Address.valueOf(ip) : null;
}
/**
* Sets the start IP for the available IP Range.
*
* @param startIp new start IP; null to clear
* @return self
*/
public BasicElementConfig startIp(String startIp) {
return (BasicElementConfig) setOrClear(START_IP, startIp);
}
/**
* Returns the end IP for the available IP Range.
*
* @return end IP or null if not set
*/
public Ip4Address endIp() {
String ip = get(END_IP, null);
return ip != null ? Ip4Address.valueOf(ip) : null;
}
/**
* Sets the end IP for the available IP Range.
*
* @param endIp new end IP; null to clear
* @return self
*/
public BasicElementConfig endIp(String endIp) {
return (BasicElementConfig) setOrClear(END_IP, endIp);
}
}
......
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.dhcp.impl;
import org.onosproject.core.ApplicationId;
import org.onosproject.net.config.Config;
import org.onosproject.net.config.basics.BasicElementConfig;
/**
* DHCP Store Config class.
*/
public class DhcpStoreConfig extends Config<ApplicationId> {
// FIXME: combine with the other config and properly type the values
public static final String TIMER_DELAY = "delay";
public static final String DEFAULT_TIMEOUT = "timeout";
public static final String START_IP = "startip";
public static final String END_IP = "endip";
/**
* Returns the delay after which the dhcp server will purge expired entries.
*
* @return time delay or null if not set
*/
public String timerDelay() {
return get(TIMER_DELAY, null);
}
/**
* Sets the delay after which the dhcp server will purge expired entries.
*
* @param delay new time delay; null to clear
* @return self
*/
public BasicElementConfig timerDelay(String delay) {
return (BasicElementConfig) setOrClear(TIMER_DELAY, delay);
}
/**
* Returns the default timeout for pending assignments.
*
* @return default timeout or null if not set
*/
public String defaultTimeout() {
return get(DEFAULT_TIMEOUT, null);
}
/**
* Sets the default timeout for pending assignments.
*
* @param defaultTimeout new default timeout; null to clear
* @return self
*/
public BasicElementConfig defaultTimeout(String defaultTimeout) {
return (BasicElementConfig) setOrClear(DEFAULT_TIMEOUT, defaultTimeout);
}
/**
* Returns the start IP for the available IP Range.
*
* @return start IP or null if not set
*/
public String startIP() {
return get(START_IP, null);
}
/**
* Sets the start IP for the available IP Range.
*
* @param startIP new start IP; null to clear
* @return self
*/
public BasicElementConfig startIP(String startIP) {
return (BasicElementConfig) setOrClear(START_IP, startIP);
}
/**
* Returns the end IP for the available IP Range.
*
* @return end IP or null if not set
*/
public String endIP() {
return get(END_IP, null);
}
/**
* Sets the end IP for the available IP Range.
*
* @param endIP new end IP; null to clear
* @return self
*/
public BasicElementConfig endIP(String endIP) {
return (BasicElementConfig) setOrClear(END_IP, endIP);
}
}
......@@ -17,10 +17,10 @@ package org.onosproject.dhcp.impl;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.ImmutableSet;
import org.onlab.packet.MacAddress;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.dhcp.DhcpService;
import org.onosproject.dhcp.IpAssignment;
import org.onosproject.net.HostId;
import org.onosproject.ui.RequestHandler;
import org.onosproject.ui.UiMessageHandler;
import org.onosproject.ui.table.TableModel;
......@@ -39,12 +39,12 @@ public class DhcpViewMessageHandler extends UiMessageHandler {
private static final String DHCP_DATA_RESP = "dhcpDataResponse";
private static final String DHCP = "dhcps";
private static final String MAC = "mac";
private static final String HOST = "host";
private static final String IP = "ip";
private static final String LEASE = "lease";
private static final String[] COL_IDS = {
MAC, IP, LEASE
HOST, IP, LEASE
};
@Override
......@@ -63,7 +63,7 @@ public class DhcpViewMessageHandler extends UiMessageHandler {
@Override
protected String defaultColumnId() {
return MAC;
return HOST;
}
@Override
......@@ -74,21 +74,21 @@ public class DhcpViewMessageHandler extends UiMessageHandler {
@Override
protected void populateTable(TableModel tm, ObjectNode payload) {
DhcpService dhcpService = AbstractShellCommand.get(DhcpService.class);
Map<MacAddress, IpAssignment> allocationMap = dhcpService.listMapping();
Map<HostId, IpAssignment> allocationMap = dhcpService.listMapping();
for (Map.Entry<MacAddress, IpAssignment> entry : allocationMap.entrySet()) {
for (Map.Entry<HostId, IpAssignment> entry : allocationMap.entrySet()) {
populateRow(tm.addRow(), entry);
}
}
private void populateRow(TableModel.Row row, Map.Entry<MacAddress, IpAssignment> entry) {
private void populateRow(TableModel.Row row, Map.Entry<HostId, IpAssignment> entry) {
if (entry.getValue().leasePeriod() > 0) {
Date now = new Date(entry.getValue().timestamp().getTime() + entry.getValue().leasePeriod());
row.cell(MAC, entry.getKey())
row.cell(HOST, entry.getKey())
.cell(IP, entry.getValue().ipAddress())
.cell(LEASE, now.toString());
} else {
row.cell(MAC, entry.getKey())
row.cell(HOST, entry.getKey())
.cell(IP, entry.getValue().ipAddress())
.cell(LEASE, "Infinite Static Lease");
}
......
......@@ -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,6 @@ import org.slf4j.LoggerFactory;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* Manages the pool of available IP Addresses in the network and
......@@ -58,25 +55,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,23 +87,20 @@ 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();
......@@ -131,7 +121,7 @@ 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;
}
}
......@@ -148,7 +138,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,24 +146,26 @@ 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 (allocationMap.containsKey(hostId)) {
assignmentInfo = allocationMap.get(hostId).value();
if ((assignmentInfo.ipAddress().toInt() == ipAddr.toInt()) &&
(ipAddr.toInt() >= startIPRange.toInt()) && (ipAddr.toInt() <= endIPRange.toInt())) {
......@@ -183,7 +175,7 @@ public class DistributedDhcpStore implements DhcpStore {
.leasePeriod(leaseTime)
.assignmentStatus(IpAssignment.AssignmentStatus.Option_Assigned)
.build();
allocationMap.put(macID, assignmentInfo);
allocationMap.put(hostId, assignmentInfo);
return true;
}
} else if (freeIPPool.contains(ipAddr)) {
......@@ -194,7 +186,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 +194,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 ((freeIP.toInt() > startIPRange.toInt()) && (freeIP.toInt() < endIPRange.toInt())) {
freeIPPool.add(freeIP);
}
}
}
......@@ -219,15 +213,10 @@ public class DistributedDhcpStore implements DhcpStore {
}
@Override
public void setTimerDelay(int timeInSeconds) {
timerDelay = timeInSeconds;
}
@Override
public Map<MacAddress, IpAssignment> listMapping() {
public Map<HostId, IpAssignment> listMapping() {
Map<MacAddress, IpAssignment> allMapping = new HashMap<>();
for (Map.Entry<MacAddress, Versioned<IpAssignment>> entry: allocationMap.entrySet()) {
Map<HostId, IpAssignment> allMapping = new HashMap<>();
for (Map.Entry<HostId, Versioned<IpAssignment>> entry: allocationMap.entrySet()) {
IpAssignment assignment = entry.getValue().value();
if (assignment.assignmentStatus() == IpAssignment.AssignmentStatus.Option_Assigned) {
allMapping.put(entry.getKey(), assignment);
......@@ -239,17 +228,21 @@ public class DistributedDhcpStore implements DhcpStore {
@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 ((freeIP.toInt() > startIPRange.toInt()) && (freeIP.toInt() < endIPRange.toInt())) {
freeIPPool.add(freeIP);
}
return true;
}
}
......@@ -289,36 +282,4 @@ public class DistributedDhcpStore implements DhcpStore {
}
return null;
}
/**
* Purges the IP allocation map to remove expired entries and returns the freed IPs to the free pool.
*/
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);
}
}
}
......
......@@ -22,6 +22,7 @@ import org.onlab.packet.Ip4Address;
import org.onlab.packet.MacAddress;
import org.onosproject.dhcp.DhcpService;
import org.onosproject.dhcp.IpAssignment;
import org.onosproject.net.HostId;
import org.onosproject.rest.AbstractWebResource;
import javax.ws.rs.Consumes;
......@@ -72,10 +73,10 @@ public class DHCPWebResource extends AbstractWebResource {
public Response listMappings() {
ObjectNode root = mapper().createObjectNode();
final Map<MacAddress, IpAssignment> intents = service.listMapping();
final Map<HostId, IpAssignment> intents = service.listMapping();
ArrayNode arrayNode = root.putArray("mappings");
intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode()
.put("mac", i.getKey().toString())
.put("host", i.getKey().toString())
.put("ip", i.getValue().ipAddress().toString())));
return ok(root.toString()).build();
......@@ -125,10 +126,10 @@ public class DHCPWebResource extends AbstractWebResource {
}
}
final Map<MacAddress, IpAssignment> intents = service.listMapping();
final Map<HostId, IpAssignment> intents = service.listMapping();
ArrayNode arrayNode = root.putArray("mappings");
intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode()
.put("mac", i.getKey().toString())
.put("host", i.getKey().toString())
.put("ip", i.getValue().ipAddress().toString())));
} catch (IOException e) {
throw new IllegalArgumentException(e.getMessage());
......@@ -152,10 +153,10 @@ public class DHCPWebResource extends AbstractWebResource {
if (!service.removeStaticMapping(MacAddress.valueOf(macID))) {
throw new IllegalArgumentException("Static Mapping Removal Failed.");
}
final Map<MacAddress, IpAssignment> intents = service.listMapping();
final Map<HostId, IpAssignment> intents = service.listMapping();
ArrayNode arrayNode = root.putArray("mappings");
intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode()
.put("mac", i.getKey().toString())
.put("host", i.getKey().toString())
.put("ip", i.getValue().ipAddress().toString())));
return ok(root.toString()).build();
......
......@@ -17,7 +17,7 @@
<div class="table-header" onos-sortable-header>
<table>
<tr>
<td colId="mac" sortable>MAC Address</td>
<td colId="host" sortable>Host ID</td>
<td colId="ip" sortable>IP Address</td>
<td colId="lease" sortable>Lease Expiry</td>
</tr>
......@@ -25,7 +25,7 @@
</div>
<div class="table-body">
<table onos-flash-changes id-prop="mac">
<table onos-flash-changes id-prop="host">
<tr ng-if="!tableData.length" class="no-data">
<td colspan="2">
No mappings found
......@@ -34,8 +34,8 @@
<tr ng-repeat="dhcp in tableData track by $index"
ng-click="selectCallback($event, dhcp)"
ng-repeat-complete row-id="{{dhcp.mac}}">
<td>{{dhcp.mac}}</td>
ng-repeat-complete row-id="{{dhcp.host}}">
<td>{{dhcp.host}}</td>
<td>{{dhcp.ip}}</td>
<td>{{dhcp.lease}}</td>
</tr>
......
......@@ -72,7 +72,7 @@ public class DhcpManagerTest {
protected HostProviderService hostProviderService;
private static final MacAddress CLIENT1_MAC = MacAddress.valueOf("1a:1a:1a:1a:1a:1a");
private static final HostId CLIENT1_HOST = HostId.hostId(MacAddress.valueOf("1a:1a:1a:1a:1a:1a"));
private static final String EXPECTED_IP = "10.2.0.2";
......@@ -141,7 +141,7 @@ public class DhcpManagerTest {
// Ethernet Frame.
Ethernet ethReply = new Ethernet();
ethReply.setSourceMACAddress(CLIENT1_MAC);
ethReply.setSourceMACAddress(CLIENT1_HOST.mac());
ethReply.setDestinationMACAddress(MacAddress.BROADCAST);
ethReply.setEtherType(Ethernet.TYPE_IPV4);
ethReply.setVlanID((short) 2);
......@@ -165,7 +165,7 @@ public class DhcpManagerTest {
dhcpReply.setServerIPAddress(0);
dhcpReply.setTransactionId(TRANSACTION_ID);
dhcpReply.setClientHardwareAddress(CLIENT1_MAC.toBytes());
dhcpReply.setClientHardwareAddress(CLIENT1_HOST.mac().toBytes());
dhcpReply.setHardwareType(DHCP.HWTYPE_ETHERNET);
dhcpReply.setHardwareAddressLength((byte) 6);
......@@ -209,7 +209,7 @@ public class DhcpManagerTest {
*/
private void validatePacket(Ethernet packet) {
DHCP dhcpPacket = (DHCP) packet.getPayload().getPayload().getPayload();
assertEquals(MacAddress.valueOf(dhcpPacket.getClientHardwareAddress()), CLIENT1_MAC);
assertEquals(MacAddress.valueOf(dhcpPacket.getClientHardwareAddress()), CLIENT1_HOST.mac());
assertEquals(Ip4Address.valueOf(dhcpPacket.getYourIPAddress()), Ip4Address.valueOf(EXPECTED_IP));
assertEquals(dhcpPacket.getTransactionId(), TRANSACTION_ID);
}
......@@ -223,32 +223,29 @@ public class DhcpManagerTest {
public void populateIPPoolfromRange(Ip4Address startIP, Ip4Address endIP) {
}
public Ip4Address suggestIP(MacAddress macID, Ip4Address requestedIP) {
public Ip4Address suggestIP(HostId hostId, Ip4Address requestedIP) {
return Ip4Address.valueOf(EXPECTED_IP);
}
public boolean assignIP(MacAddress macID, Ip4Address ipAddr, int leaseTime) {
public boolean assignIP(HostId hostId, Ip4Address ipAddr, int leaseTime) {
return true;
}
public void setDefaultTimeoutForPurge(int timeInSeconds) {
}
public void setTimerDelay(int timeInSeconds) {
public void releaseIP(HostId hostId) {
}
public void releaseIP(MacAddress macID) {
}
public Map<MacAddress, IpAssignment> listMapping() {
Map<MacAddress, IpAssignment> map = new HashMap<>();
public Map<HostId, IpAssignment> listMapping() {
Map<HostId, IpAssignment> map = new HashMap<>();
IpAssignment assignment = IpAssignment.builder()
.ipAddress(Ip4Address.valueOf(EXPECTED_IP))
.assignmentStatus(IpAssignment.AssignmentStatus.Option_Assigned)
.leasePeriod(300)
.timestamp(new Date())
.build();
map.put(CLIENT1_MAC, assignment);
map.put(CLIENT1_HOST, assignment);
return map;
}
......
......@@ -11,9 +11,7 @@
"ttl": "63",
"lease": "300",
"renew": "150",
"rebind": "200"
},
"dhcpstore" : {
"rebind": "200",
"delay": "3",
"timeout": "150",
"startip": "10.0.0.110",
......