aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/apps/dhcp/src/main/java/org
diff options
context:
space:
mode:
Diffstat (limited to 'framework/src/onos/apps/dhcp/src/main/java/org')
-rw-r--r--framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/DhcpService.java3
-rw-r--r--framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/DhcpStore.java23
-rw-r--r--framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/IpAssignment.java19
-rw-r--r--framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/cli/DhcpListAllMappings.java6
-rw-r--r--framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/cli/package-info.java20
-rw-r--r--framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DhcpConfig.java148
-rw-r--r--framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DhcpManager.java214
-rw-r--r--framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DhcpViewMessageHandler.java19
-rw-r--r--framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DistributedDhcpStore.java186
-rw-r--r--framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/package-info.java20
-rw-r--r--framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/package-info.java20
-rw-r--r--framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/rest/DHCPWebResource.java13
-rw-r--r--framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/rest/package-info.java20
13 files changed, 453 insertions, 258 deletions
diff --git a/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/DhcpService.java b/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/DhcpService.java
index 20e1c614..7c2127f9 100644
--- a/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/DhcpService.java
+++ b/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/DhcpService.java
@@ -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.
diff --git a/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/DhcpStore.java b/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/DhcpStore.java
index 4e2d67d6..c9fade9e 100644
--- a/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/DhcpStore.java
+++ b/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/DhcpStore.java
@@ -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,25 @@ public interface DhcpStore {
void setDefaultTimeoutForPurge(int timeInSeconds);
/**
- * Sets the delay after which the dhcp server will purge expired entries.
+ * Releases the IP assigned to a Mac ID into the free pool.
*
- * @param timeInSeconds default time
+ * @param hostId the host ID for which the mapping needs to be changed
*/
- void setTimerDelay(int timeInSeconds);
+ void releaseIP(HostId hostId);
/**
- * Releases the IP assigned to a Mac ID into the free pool.
+ * Returns a collection of all the MacAddress to IPAddress mapping assigned to the hosts.
*
- * @param macID the macID for which the mapping needs to be changed
+ * @return the collection of the mappings
*/
- void releaseIP(MacAddress macID);
+ Map<HostId, IpAssignment> listAssignedMapping();
/**
* Returns a collection of all the MacAddress to IPAddress mapping.
*
* @return the collection of the mappings
*/
- Map<MacAddress, IpAssignment> listMapping();
+ Map<HostId, IpAssignment> listAllMapping();
/**
* Assigns the requested IP to the MAC ID (if available) for an indefinite period of time.
diff --git a/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/IpAssignment.java b/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/IpAssignment.java
index c8bd1906..9b3aa686 100644
--- a/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/IpAssignment.java
+++ b/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/IpAssignment.java
@@ -49,7 +49,7 @@ public final class IpAssignment {
/**
* IP mapping is no longer active.
*/
- Option_Expired;
+ Option_Expired
}
/**
@@ -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;
}
diff --git a/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/cli/DhcpListAllMappings.java b/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/cli/DhcpListAllMappings.java
index fc470ce3..209ba683 100644
--- a/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/cli/DhcpListAllMappings.java
+++ b/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/cli/DhcpListAllMappings.java
@@ -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());
}
}
diff --git a/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/cli/package-info.java b/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/cli/package-info.java
new file mode 100644
index 00000000..f8780195
--- /dev/null
+++ b/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/cli/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2015 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.
+ */
+
+/**
+ * CLI implementation for sample application that assigns and manages DHCP leases.
+ */
+package org.onosproject.dhcp.cli; \ No newline at end of file
diff --git a/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DhcpConfig.java b/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DhcpConfig.java
index f8d5e63b..4353d623 100644
--- a/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DhcpConfig.java
+++ b/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DhcpConfig.java
@@ -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,21 @@ 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";
+
+ public static final int DEFAULT = -1;
/**
* 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 +68,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 +88,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 +108,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;
}
/**
@@ -114,10 +126,10 @@ public class DhcpConfig extends Config<ApplicationId> {
/**
* Returns the Time To Live for the reply packets.
*
- * @return ttl or null if not set
+ * @return ttl or -1 if not set
*/
- public String ttl() {
- return get(TTL, null);
+ public int ttl() {
+ return get(TTL, DEFAULT);
}
/**
@@ -126,17 +138,17 @@ 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);
}
/**
* Returns the Lease Time offered by the DHCP Server.
*
- * @return lease time or null if not set
+ * @return lease time or -1 if not set
*/
- public String leaseTime() {
- return get(LEASE_TIME, null);
+ public int leaseTime() {
+ return get(LEASE_TIME, DEFAULT);
}
/**
@@ -145,17 +157,17 @@ 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);
}
/**
* Returns the Renew Time offered by the DHCP Server.
*
- * @return renew time or null if not set
+ * @return renew time or -1 if not set
*/
- public String renewTime() {
- return get(RENEW_TIME, null);
+ public int renewTime() {
+ return get(RENEW_TIME, DEFAULT);
}
/**
@@ -164,17 +176,17 @@ 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);
}
/**
* Returns the Rebind Time offered by the DHCP Server.
*
- * @return rebind time or null if not set
+ * @return rebind time or -1 if not set
*/
- public String rebindTime() {
- return get(REBIND_TIME, null);
+ public int rebindTime() {
+ return get(REBIND_TIME, DEFAULT);
}
/**
@@ -183,7 +195,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 +204,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 +224,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 +238,82 @@ public class DhcpConfig extends Config<ApplicationId> {
public BasicElementConfig domainServer(String domain) {
return (BasicElementConfig) setOrClear(DOMAIN_SERVER, domain);
}
+
+ /**
+ * Returns the delay in minutes after which the dhcp server will purge expired entries.
+ *
+ * @return time delay or -1 if not set
+ */
+ public int timerDelay() {
+ return get(TIMER_DELAY, DEFAULT);
+ }
+
+ /**
+ * 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 -1 if not set
+ */
+ public int defaultTimeout() {
+ return get(DEFAULT_TIMEOUT, DEFAULT);
+ }
+
+ /**
+ * 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);
+ }
}
diff --git a/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DhcpManager.java b/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DhcpManager.java
index 24cb0878..345d5ad0 100644
--- a/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DhcpManager.java
+++ b/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DhcpManager.java
@@ -22,6 +22,8 @@ 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.ARP;
import org.onlab.packet.DHCP;
import org.onlab.packet.DHCPOption;
@@ -34,20 +36,20 @@ import org.onlab.packet.MacAddress;
import org.onlab.packet.TpPort;
import org.onlab.packet.UDP;
import org.onlab.packet.VlanId;
+import org.onlab.util.Timer;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService;
import org.onosproject.dhcp.DhcpService;
import org.onosproject.dhcp.DhcpStore;
import org.onosproject.dhcp.IpAssignment;
-import org.onosproject.net.config.ConfigFactory;
-import org.onosproject.net.config.NetworkConfigEvent;
-import org.onosproject.net.config.NetworkConfigListener;
-import org.onosproject.net.config.NetworkConfigRegistry;
-
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.Host;
import org.onosproject.net.HostId;
import org.onosproject.net.HostLocation;
+import org.onosproject.net.config.ConfigFactory;
+import org.onosproject.net.config.NetworkConfigEvent;
+import org.onosproject.net.config.NetworkConfigListener;
+import org.onosproject.net.config.NetworkConfigRegistry;
import org.onosproject.net.flow.DefaultTrafficSelector;
import org.onosproject.net.flow.DefaultTrafficTreatment;
import org.onosproject.net.flow.TrafficSelector;
@@ -68,10 +70,13 @@ import org.slf4j.LoggerFactory;
import java.nio.ByteBuffer;
import java.util.ArrayList;
+import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
import java.util.Set;
+import java.util.concurrent.TimeUnit;
import static org.onlab.packet.MacAddress.valueOf;
import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY;
@@ -86,7 +91,7 @@ public class DhcpManager implements DhcpService {
private static final ProviderId PID = new ProviderId("of", "org.onosproject.dhcp", true);
private final Logger log = LoggerFactory.getLogger(getClass());
- private final NetworkConfigListener cfgListener = new InternalConfigListener();
+ private final InternalConfigListener cfgListener = new InternalConfigListener();
private final Set<ConfigFactory> factories = ImmutableSet.of(
new ConfigFactory<ApplicationId, DhcpConfig>(APP_SUBJECT_FACTORY,
@@ -96,14 +101,6 @@ public class DhcpManager implements DhcpService {
public DhcpConfig createConfig() {
return new DhcpConfig();
}
- },
- new ConfigFactory<ApplicationId, DhcpStoreConfig>(APP_SUBJECT_FACTORY,
- DhcpStoreConfig.class,
- "dhcpstore") {
- @Override
- public DhcpStoreConfig createConfig() {
- return new DhcpStoreConfig();
- }
}
);
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
@@ -125,11 +122,13 @@ public class DhcpManager implements DhcpService {
protected HostProviderService hostProviderService;
+ private final HostProvider hostProvider = new InternalHostProvider();
+
private ApplicationId appId;
// Hardcoded values are default values.
- private static String myIP = "10.0.0.2";
+ private static Ip4Address myIP = Ip4Address.valueOf("10.0.0.2");
private static MacAddress myMAC = valueOf("4f:4f:4f:4f:4f:4f");
@@ -147,14 +146,17 @@ public class DhcpManager implements DhcpService {
private static byte packetTTL = (byte) 127;
- private static String subnetMask = "255.0.0.0";
+ private static Ip4Address subnetMask = Ip4Address.valueOf("255.0.0.0");
- private static String broadcastAddress = "10.255.255.255";
+ private static Ip4Address broadcastAddress = Ip4Address.valueOf("10.255.255.255");
- private static String routerAddress = "10.0.0.2";
+ private static Ip4Address routerAddress = Ip4Address.valueOf("10.0.0.2");
- private static String domainServer = "10.0.0.2";
- private final HostProvider hostProvider = new InternalHostProvider();
+ private static Ip4Address domainServer = Ip4Address.valueOf("10.0.0.2");
+
+ protected Timeout timeout;
+
+ protected static int timerDelay = 2;
@Activate
protected void activate() {
@@ -163,9 +165,12 @@ public class DhcpManager implements DhcpService {
cfgService.addListener(cfgListener);
factories.forEach(cfgService::registerConfigFactory);
+ cfgListener.reconfigureNetwork(cfgService.getConfig(appId, DhcpConfig.class));
+
hostProviderService = hostProviderRegistry.register(hostProvider);
- packetService.addProcessor(processor, PacketProcessor.observer(1));
+ packetService.addProcessor(processor, PacketProcessor.director(0));
requestPackets();
+ timeout = Timer.getTimer().newTimeout(new PurgeListTask(), timerDelay, TimeUnit.MINUTES);
log.info("Started");
}
@@ -177,6 +182,7 @@ public class DhcpManager implements DhcpService {
hostProviderRegistry.unregister(hostProvider);
hostProviderService = null;
cancelPackets();
+ timeout.cancel();
log.info("Stopped");
}
@@ -214,8 +220,8 @@ public class DhcpManager implements DhcpService {
}
@Override
- public Map<MacAddress, IpAssignment> listMapping() {
- return dhcpStore.listMapping();
+ public Map<HostId, IpAssignment> listMapping() {
+ return dhcpStore.listAssignedMapping();
}
@Override
@@ -258,9 +264,7 @@ public class DhcpManager implements DhcpService {
* @param outgoingMessageType the message type of the outgoing packet
* @return the Ethernet reply frame
*/
- private Ethernet buildReply(Ethernet packet, String ipOffered, byte outgoingMessageType) {
- Ip4Address myIPAddress = Ip4Address.valueOf(myIP);
- Ip4Address ipAddress;
+ private Ethernet buildReply(Ethernet packet, Ip4Address ipOffered, byte outgoingMessageType) {
// Ethernet Frame.
Ethernet ethReply = new Ethernet();
@@ -272,9 +276,8 @@ public class DhcpManager implements DhcpService {
// IP Packet
IPv4 ipv4Packet = (IPv4) packet.getPayload();
IPv4 ipv4Reply = new IPv4();
- ipv4Reply.setSourceAddress(myIPAddress.toInt());
- ipAddress = Ip4Address.valueOf(ipOffered);
- ipv4Reply.setDestinationAddress(ipAddress.toInt());
+ ipv4Reply.setSourceAddress(myIP.toInt());
+ ipv4Reply.setDestinationAddress(ipOffered.toInt());
ipv4Reply.setTtl(packetTTL);
// UDP Datagram.
@@ -288,9 +291,8 @@ public class DhcpManager implements DhcpService {
DHCP dhcpReply = new DHCP();
dhcpReply.setOpCode(DHCP.OPCODE_REPLY);
- ipAddress = Ip4Address.valueOf(ipOffered);
- dhcpReply.setYourIPAddress(ipAddress.toInt());
- dhcpReply.setServerIPAddress(myIPAddress.toInt());
+ dhcpReply.setYourIPAddress(ipOffered.toInt());
+ dhcpReply.setServerIPAddress(myIP.toInt());
dhcpReply.setTransactionId(dhcpPacket.getTransactionId());
dhcpReply.setClientHardwareAddress(dhcpPacket.getClientHardwareAddress());
@@ -312,7 +314,7 @@ public class DhcpManager implements DhcpService {
option = new DHCPOption();
option.setCode(DHCP.DHCPOptionCode.OptionCode_DHCPServerIp.getValue());
option.setLength((byte) 4);
- option.setData(myIPAddress.toOctets());
+ option.setData(myIP.toOctets());
optionList.add(option);
// IP Address Lease Time.
@@ -340,32 +342,28 @@ public class DhcpManager implements DhcpService {
option = new DHCPOption();
option.setCode(DHCP.DHCPOptionCode.OptionCode_SubnetMask.getValue());
option.setLength((byte) 4);
- ipAddress = Ip4Address.valueOf(subnetMask);
- option.setData(ipAddress.toOctets());
+ option.setData(subnetMask.toOctets());
optionList.add(option);
// Broadcast Address.
option = new DHCPOption();
option.setCode(DHCP.DHCPOptionCode.OptionCode_BroadcastAddress.getValue());
option.setLength((byte) 4);
- ipAddress = Ip4Address.valueOf(broadcastAddress);
- option.setData(ipAddress.toOctets());
+ option.setData(broadcastAddress.toOctets());
optionList.add(option);
// Router Address.
option = new DHCPOption();
option.setCode(DHCP.DHCPOptionCode.OptionCode_RouterAddress.getValue());
option.setLength((byte) 4);
- ipAddress = Ip4Address.valueOf(routerAddress);
- option.setData(ipAddress.toOctets());
+ option.setData(routerAddress.toOctets());
optionList.add(option);
// DNS Server Address.
option = new DHCPOption();
option.setCode(DHCP.DHCPOptionCode.OptionCode_DomainServer.getValue());
option.setLength((byte) 4);
- ipAddress = Ip4Address.valueOf(domainServer);
- option.setData(ipAddress.toOctets());
+ option.setData(domainServer.toOctets());
optionList.add(option);
// End Option.
@@ -394,7 +392,7 @@ public class DhcpManager implements DhcpService {
TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
ConnectPoint sourcePoint = context.inPacket().receivedFrom();
builder.setOutput(sourcePoint.port());
-
+ context.block();
packetService.emit(new DefaultOutboundPacket(sourcePoint.deviceId(),
builder.build(), ByteBuffer.wrap(reply.serialize())));
}
@@ -407,7 +405,6 @@ public class DhcpManager implements DhcpService {
* @param dhcpPayload the extracted DHCP payload
*/
private void processDHCPPacket(PacketContext context, DHCP dhcpPayload) {
-
Ethernet packet = context.inPacket().parsed();
boolean flagIfRequestedIP = false;
boolean flagIfServerIP = false;
@@ -416,12 +413,11 @@ public class DhcpManager implements DhcpService {
if (dhcpPayload != null) {
- // TODO Convert this to enum value.
- byte incomingPacketType = 0;
+ DHCPPacketType incomingPacketType = DHCPPacketType.getType(0);
for (DHCPOption option : dhcpPayload.getOptions()) {
if (option.getCode() == DHCP.DHCPOptionCode.OptionCode_MessageType.getValue()) {
byte[] data = option.getData();
- incomingPacketType = data[0];
+ incomingPacketType = DHCPPacketType.getType(data[0]);
}
if (option.getCode() == DHCP.DHCPOptionCode.OptionCode_RequestedIP.getValue()) {
byte[] data = option.getData();
@@ -434,37 +430,39 @@ public class DhcpManager implements DhcpService {
flagIfServerIP = true;
}
}
-
- String ipOffered = "";
DHCPPacketType outgoingPacketType;
MacAddress clientMAC = new MacAddress(dhcpPayload.getClientHardwareAddress());
+ VlanId vlanId = VlanId.vlanId(packet.getVlanID());
+ HostId hostId = HostId.hostId(clientMAC, vlanId);
- if (incomingPacketType == DHCPPacketType.DHCPDISCOVER.getValue()) {
+ if (incomingPacketType.getValue() == DHCPPacketType.DHCPDISCOVER.getValue()) {
outgoingPacketType = DHCPPacketType.DHCPOFFER;
- ipOffered = dhcpStore.suggestIP(clientMAC, requestedIP).toString();
-
- Ethernet ethReply = buildReply(packet, ipOffered, (byte) outgoingPacketType.getValue());
- sendReply(context, ethReply);
+ Ip4Address ipOffered = dhcpStore.suggestIP(hostId, requestedIP);
+ if (ipOffered != null) {
+ Ethernet ethReply = buildReply(packet, ipOffered,
+ (byte) outgoingPacketType.getValue());
+ sendReply(context, ethReply);
+ }
- } else if (incomingPacketType == DHCPPacketType.DHCPREQUEST.getValue()) {
+ } else if (incomingPacketType.getValue() == DHCPPacketType.DHCPREQUEST.getValue()) {
outgoingPacketType = DHCPPacketType.DHCPACK;
if (flagIfServerIP && flagIfRequestedIP) {
// SELECTING state
- if (myIP.equals(serverIP.toString()) &&
- dhcpStore.assignIP(clientMAC, requestedIP, leaseTime)) {
+ if (myIP.equals(serverIP) &&
+ dhcpStore.assignIP(hostId, requestedIP, leaseTime)) {
- Ethernet ethReply = buildReply(packet, requestedIP.toString(),
+ Ethernet ethReply = buildReply(packet, requestedIP,
(byte) outgoingPacketType.getValue());
sendReply(context, ethReply);
discoverHost(context, requestedIP);
}
} else if (flagIfRequestedIP) {
// INIT-REBOOT state
- if (dhcpStore.assignIP(clientMAC, requestedIP, leaseTime)) {
- Ethernet ethReply = buildReply(packet, requestedIP.toString(),
+ if (dhcpStore.assignIP(hostId, requestedIP, leaseTime)) {
+ Ethernet ethReply = buildReply(packet, requestedIP,
(byte) outgoingPacketType.getValue());
sendReply(context, ethReply);
discoverHost(context, requestedIP);
@@ -474,17 +472,16 @@ public class DhcpManager implements DhcpService {
int ciaadr = dhcpPayload.getClientIPAddress();
if (ciaadr != 0) {
Ip4Address clientIaddr = Ip4Address.valueOf(ciaadr);
- if (dhcpStore.assignIP(clientMAC, clientIaddr, leaseTime)) {
- Ethernet ethReply = buildReply(packet, clientIaddr.toString(),
+ if (dhcpStore.assignIP(hostId, clientIaddr, leaseTime)) {
+ Ethernet ethReply = buildReply(packet, clientIaddr,
(byte) outgoingPacketType.getValue());
sendReply(context, ethReply);
discoverHost(context, clientIaddr);
}
}
}
- } else if (incomingPacketType == DHCPPacketType.DHCPRELEASE.getValue()) {
-
- dhcpStore.releaseIP(clientMAC);
+ } else if (incomingPacketType.getValue() == DHCPPacketType.DHCPRELEASE.getValue()) {
+ dhcpStore.releaseIP(hostId);
}
}
}
@@ -540,7 +537,6 @@ public class DhcpManager implements DhcpService {
@Override
public void process(PacketContext context) {
-
Ethernet packet = context.inPacket().parsed();
if (packet == null) {
return;
@@ -564,7 +560,7 @@ public class DhcpManager implements DhcpService {
ARP arpPacket = (ARP) packet.getPayload();
if ((arpPacket.getOpCode() == ARP.OP_REQUEST) &&
- (Ip4Address.valueOf(arpPacket.getTargetProtocolAddress()).toString().equals(myIP))) {
+ Objects.equals(myIP, Ip4Address.valueOf(arpPacket.getTargetProtocolAddress()))) {
processARPPacket(context, packet);
@@ -581,12 +577,14 @@ public class DhcpManager implements DhcpService {
* @param cfg configuration object
*/
private void reconfigureNetwork(DhcpConfig cfg) {
-
+ if (cfg == null) {
+ return;
+ }
if (cfg.ip() != null) {
myIP = cfg.ip();
}
if (cfg.mac() != null) {
- myMAC = MacAddress.valueOf(cfg.mac());
+ myMAC = cfg.mac();
}
if (cfg.subnetMask() != null) {
subnetMask = cfg.subnetMask();
@@ -600,54 +598,40 @@ public class DhcpManager implements DhcpService {
if (cfg.domainServer() != null) {
domainServer = cfg.domainServer();
}
- if (cfg.ttl() != null) {
- packetTTL = Byte.valueOf(cfg.ttl());
+ if (cfg.ttl() != -1) {
+ packetTTL = (byte) cfg.ttl();
}
- if (cfg.leaseTime() != null) {
- leaseTime = Integer.valueOf(cfg.leaseTime());
+ if (cfg.leaseTime() != -1) {
+ leaseTime = cfg.leaseTime();
}
- if (cfg.renewTime() != null) {
- renewalTime = Integer.valueOf(cfg.renewTime());
+ if (cfg.renewTime() != -1) {
+ renewalTime = cfg.renewTime();
}
- if (cfg.rebindTime() != null) {
- rebindingTime = Integer.valueOf(cfg.rebindTime());
+ if (cfg.rebindTime() != -1) {
+ rebindingTime = cfg.rebindTime();
}
- }
-
- /**
- * Reconfigures the DHCP Store according to the configuration parameters passed.
- *
- * @param cfg configuration object
- */
- private void reconfigureStore(DhcpStoreConfig cfg) {
-
- if (cfg.defaultTimeout() != null) {
- dhcpStore.setDefaultTimeoutForPurge(Integer.valueOf(cfg.defaultTimeout()));
+ if (cfg.defaultTimeout() != -1) {
+ dhcpStore.setDefaultTimeoutForPurge(cfg.defaultTimeout());
}
- if (cfg.timerDelay() != null) {
- dhcpStore.setTimerDelay(Integer.valueOf(cfg.defaultTimeout()));
+ if (cfg.timerDelay() != -1) {
+ timerDelay = cfg.timerDelay();
}
- if ((cfg.startIP() != null) && (cfg.endIP() != null)) {
- dhcpStore.populateIPPoolfromRange(Ip4Address.valueOf(cfg.startIP()),
- Ip4Address.valueOf(cfg.endIP()));
+ if ((cfg.startIp() != null) && (cfg.endIp() != null)) {
+ dhcpStore.populateIPPoolfromRange(cfg.startIp(), cfg.endIp());
}
}
+
@Override
public void event(NetworkConfigEvent event) {
if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
- event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED)) {
- if (event.configClass().equals(DhcpConfig.class)) {
- DhcpConfig cfg = cfgService.getConfig(appId, DhcpConfig.class);
- reconfigureNetwork(cfg);
- log.info("Reconfigured Manager");
- }
- if (event.configClass().equals(DhcpStoreConfig.class)) {
- DhcpStoreConfig cfg = cfgService.getConfig(appId, DhcpStoreConfig.class);
- reconfigureStore(cfg);
- log.info("Reconfigured Store");
- }
+ event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) &&
+ event.configClass().equals(DhcpConfig.class)) {
+
+ DhcpConfig cfg = cfgService.getConfig(appId, DhcpConfig.class);
+ reconfigureNetwork(cfg);
+ log.info("Reconfigured");
}
}
}
@@ -666,4 +650,28 @@ public class DhcpManager implements DhcpService {
// nothing to do
}
}
+
+ private class PurgeListTask implements TimerTask {
+
+ @Override
+ public void run(Timeout to) {
+ IpAssignment ipAssignment;
+ Date dateNow = new Date();
+
+ Map<HostId, IpAssignment> ipAssignmentMap = dhcpStore.listAllMapping();
+ for (Map.Entry<HostId, IpAssignment> entry: ipAssignmentMap.entrySet()) {
+ ipAssignment = entry.getValue();
+
+ long timeLapsed = dateNow.getTime() - ipAssignment.timestamp().getTime();
+ if ((ipAssignment.assignmentStatus() != IpAssignment.AssignmentStatus.Option_Expired) &&
+ (ipAssignment.leasePeriod() > 0) && (timeLapsed > (ipAssignment.leasePeriodMs()))) {
+
+ dhcpStore.releaseIP(entry.getKey());
+ // TODO remove only the IP from the host entry when the API is in place.
+ hostProviderService.hostVanished(entry.getKey());
+ }
+ }
+ timeout = Timer.getTimer().newTimeout(new PurgeListTask(), timerDelay, TimeUnit.MINUTES);
+ }
+ }
} \ No newline at end of file
diff --git a/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DhcpViewMessageHandler.java b/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DhcpViewMessageHandler.java
index 89ed5720..9ce65d5e 100644
--- a/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DhcpViewMessageHandler.java
+++ b/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DhcpViewMessageHandler.java
@@ -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
@@ -54,6 +54,7 @@ public class DhcpViewMessageHandler extends UiMessageHandler {
);
}
+ // handler for dhcp table requests
private final class DataRequestHandler extends TableRequestHandler {
private DataRequestHandler() {
@@ -62,7 +63,7 @@ public class DhcpViewMessageHandler extends UiMessageHandler {
@Override
protected String defaultColumnId() {
- return MAC;
+ return HOST;
}
@Override
@@ -73,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");
}
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;
}
-
}
diff --git a/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/package-info.java b/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/package-info.java
new file mode 100644
index 00000000..12e14e48
--- /dev/null
+++ b/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2015 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.
+ */
+
+/**
+ * Implementation classes for sample application that assigns and manages DHCP leases.
+ */
+package org.onosproject.dhcp.impl; \ No newline at end of file
diff --git a/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/package-info.java b/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/package-info.java
new file mode 100644
index 00000000..56778a35
--- /dev/null
+++ b/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2015 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.
+ */
+
+/**
+ * Sample application that assigns and manages DHCP leases.
+ */
+package org.onosproject.dhcp; \ No newline at end of file
diff --git a/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/rest/DHCPWebResource.java b/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/rest/DHCPWebResource.java
index bfa2767d..646ab7ea 100644
--- a/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/rest/DHCPWebResource.java
+++ b/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/rest/DHCPWebResource.java
@@ -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();
diff --git a/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/rest/package-info.java b/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/rest/package-info.java
new file mode 100644
index 00000000..73173c55
--- /dev/null
+++ b/framework/src/onos/apps/dhcp/src/main/java/org/onosproject/dhcp/rest/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2015 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.
+ */
+
+/**
+ * REST APIs for sample application that assigns and manages DHCP leases.
+ */
+package org.onosproject.dhcp.rest; \ No newline at end of file