ONOS-2197 Adding service interface and CLI for DHCP Server
Change-Id: I387b86181e78bed666032c69dd80c71cec85c9fc
diff --git a/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/DHCPService.java b/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/DHCPService.java
new file mode 100644
index 0000000..2bc7724
--- /dev/null
+++ b/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/DHCPService.java
@@ -0,0 +1,80 @@
+/*
+ * 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.dhcpserver;
+
+import org.onlab.packet.Ip4Address;
+import org.onlab.packet.MacAddress;
+
+import java.util.Map;
+
+/**
+ * DHCP Service Interface.
+ */
+public interface DHCPService {
+
+ /**
+ * Returns a collection of all the MacAddress to IPAddress mapping.
+ *
+ * @return collection of mappings.
+ */
+ public Map<MacAddress, Ip4Address> listMapping();
+
+ /**
+ * Returns the default lease time granted by the DHCP Server.
+ *
+ * @return lease time
+ */
+ public int getLeaseTime();
+
+ /**
+ * Returns the default renewal time granted by the DHCP Server.
+ *
+ * @return renewal time
+ */
+ public int getRenewalTime();
+
+ /**
+ * Returns the default rebinding time granted by the DHCP Server.
+ *
+ * @return rebinding time
+ */
+ public int getRebindingTime();
+
+ /**
+ * Registers a static IP mapping with the DHCP Server.
+ *
+ * @param macID macID of the client
+ * @param ipAddress IP Address requested for the client
+ * @return true if the mapping was successfully registered, false otherwise
+ */
+ public boolean setStaticMapping(MacAddress macID, Ip4Address ipAddress);
+
+ /**
+ * Removes a static IP mapping with the DHCP Server.
+ *
+ * @param macID macID of the client
+ * @return true if the mapping was successfully removed, false otherwise
+ */
+ public boolean removeStaticMapping(MacAddress macID);
+
+ /**
+ * Returns the list of all the available IPs with the server.
+ *
+ * @return list of available IPs
+ */
+ public Iterable<Ip4Address> getAvailableIPs();
+
+}
diff --git a/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/DHCPStore.java b/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/DHCPStore.java
index 0a3d973..b6c60a2 100644
--- a/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/DHCPStore.java
+++ b/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/DHCPStore.java
@@ -18,6 +18,8 @@
import org.onlab.packet.Ip4Address;
import org.onlab.packet.MacAddress;
+import java.util.Map;
+
/**
* DHCPStore Interface.
*/
@@ -39,14 +41,14 @@
* @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, long leaseTime);
+ boolean assignIP(MacAddress macID, Ip4Address ipAddr, int leaseTime);
/**
* Sets the default time for which suggested IP mappings are valid.
*
* @param timeInSeconds default time for IP mappings to be valid
*/
- void setDefaultTimeoutForPurge(long timeInSeconds);
+ void setDefaultTimeoutForPurge(int timeInSeconds);
/**
* Releases the IP assigned to a Mac ID into the free pool.
@@ -55,4 +57,35 @@
*/
void releaseIP(MacAddress macID);
+ /**
+ * Returns a collection of all the MacAddress to IPAddress mapping.
+ *
+ * @return the collection of the mappings
+ */
+ Map<MacAddress, Ip4Address> listMapping();
+
+ /**
+ * Assigns the requested IP to the MAC ID (if available) for an indefinite period of time.
+ *
+ * @param macID macID of the client
+ * @param ipAddr IP Address requested for the client
+ * @return true if the mapping was successfully registered, false otherwise
+ */
+ boolean assignStaticIP(MacAddress macID, Ip4Address ipAddr);
+
+ /**
+ * Removes a static IP mapping associated with the given MAC ID from the DHCP Server.
+ *
+ * @param macID macID of the client
+ * @return true if the mapping was successfully registered, false otherwise
+ */
+ boolean removeStaticIP(MacAddress macID);
+
+ /**
+ * Returns the list of all the available IPs with the server.
+ *
+ * @return list of available IPs
+ */
+ Iterable<Ip4Address> getAvailableIPs();
+
}
diff --git a/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/IPAssignment.java b/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/IPAssignment.java
index 97cdd87..56cf3da 100644
--- a/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/IPAssignment.java
+++ b/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/IPAssignment.java
@@ -15,105 +15,67 @@
*/
package org.onosproject.dhcpserver;
+import com.google.common.base.MoreObjects;
import org.onlab.packet.Ip4Address;
import java.util.Date;
+import static com.google.common.base.Preconditions.checkNotNull;
+
/**
* Stores the MAC ID to IP Address mapping details.
*/
+public final class IPAssignment {
-// TODO Convert this into an immutable class using the builder pattern.
-public class IPAssignment {
+ private final Ip4Address ipAddress;
- private Ip4Address ipAddress;
+ private final Date timestamp;
- private Date timestamp;
+ private final long leasePeriod;
- private long leasePeriod;
-
- private AssignmentStatus assignmentStatus;
+ private final AssignmentStatus assignmentStatus;
public enum AssignmentStatus {
+ /**
+ * IP has been requested by a host, but not assigned to it yet.
+ */
Option_Requested,
+
+ /**
+ * IP has been assigned to a host.
+ */
Option_Assigned,
+
+ /**
+ * IP mapping is no longer active.
+ */
Option_Expired;
}
/**
- * Default constructor for IPAssignment, where the timestamp is set to the current time.
- */
- IPAssignment() {
- timestamp = new Date();
- }
-
- /**
- * Constructor for IPAssignment, where the ipAddress, the lease period
- * and assignment status is supplied. The timestamp is set to the current time.
+ * Constructor for IPAssignment, where the ipAddress, the lease period, the timestamp
+ * and assignment status is supplied.
*
* @param ipAddress
* @param leasePeriod
* @param assignmentStatus
*/
- IPAssignment(Ip4Address ipAddress, long leasePeriod, AssignmentStatus assignmentStatus) {
+ private IPAssignment(Ip4Address ipAddress,
+ long leasePeriod,
+ Date timestamp,
+ AssignmentStatus assignmentStatus) {
this.ipAddress = ipAddress;
this.leasePeriod = leasePeriod;
- this.assignmentStatus = assignmentStatus;
-
- this.timestamp = new Date();
- }
-
- /**
- * Sets the IP address for the IP assignment.
- *
- * @param ipaddress the assigned IP address
- */
- public void setIpAddress(Ip4Address ipaddress) {
- this.ipAddress = ipaddress;
- }
-
- /**
- * Sets the Timestamp for the IP assignment when the assignment was made.
- *
- * @param timestamp timestamp when the assignment was made
- */
- public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
- }
-
- /**
- * Sets the assignment status for the IP assignment.
- *
- * @param assignmentStatus the assignment status
- */
- public void setAssignmentStatus(AssignmentStatus assignmentStatus) {
this.assignmentStatus = assignmentStatus;
}
/**
- * Sets the Lease period value when the argument supplied is in seconds.
- *
- * @param leasePeriod lease time in seconds
- */
- public void setLeasePeriodinSeconds(long leasePeriod) {
- this.leasePeriod = leasePeriod * 1000;
- }
-
- /**
- * Sets the Lease period value when the argument supplied is in milliseconds.
- *
- * @param leasePeriod lease time in milliseconds
- */
- public void setLeasePeriodinMilliseconds(long leasePeriod) {
- this.leasePeriod = leasePeriod;
- }
-
- /**
* Returns the IP Address of the IP assignment.
*
* @return the IP address
*/
- public Ip4Address getIpAddress() {
+ public Ip4Address ipAddress() {
return this.ipAddress;
}
@@ -122,7 +84,7 @@
*
* @return the timestamp
*/
- public Date getTimestamp() {
+ public Date timestamp() {
return this.timestamp;
}
@@ -131,7 +93,7 @@
*
* @return the assignment status
*/
- public AssignmentStatus getAssignmentStatus() {
+ public AssignmentStatus assignmentStatus() {
return this.assignmentStatus;
}
@@ -140,7 +102,95 @@
*
* @return the lease period
*/
- public long getLeasePeriod() {
+ public long leasePeriod() {
return this.leasePeriod;
}
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .add("ip", ipAddress)
+ .add("timestamp", timestamp)
+ .add("lease", leasePeriod)
+ .add("assignmentStatus", assignmentStatus)
+ .toString();
+ }
+
+ /**
+ * Creates and returns a new builder instance.
+ *
+ * @return new builder
+ */
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ /**
+ * IPAssignment Builder.
+ */
+ public static final class Builder {
+
+ private Ip4Address ipAddress;
+
+ private Date timeStamp;
+
+ private long leasePeriod;
+
+ private AssignmentStatus assignmentStatus;
+
+ private Builder() {
+
+ }
+
+ private Builder(IPAssignment ipAssignment) {
+ ipAddress = ipAssignment.ipAddress();
+ timeStamp = ipAssignment.timestamp();
+ leasePeriod = ipAssignment.leasePeriod();
+ assignmentStatus = ipAssignment.assignmentStatus();
+ }
+
+ public IPAssignment build() {
+ validateInputs();
+ return new IPAssignment(ipAddress,
+ leasePeriod,
+ timeStamp,
+ assignmentStatus);
+ }
+
+ public Builder ipAddress(Ip4Address addr) {
+ ipAddress = addr;
+ return this;
+ }
+
+ public Builder timestamp(Date timestamp) {
+ timeStamp = timestamp;
+ return this;
+ }
+
+ public Builder leasePeriod(int leasePeriodinSeconds) {
+ leasePeriod = leasePeriodinSeconds * 1000;
+ return this;
+ }
+
+ public Builder assignmentStatus(AssignmentStatus status) {
+ assignmentStatus = status;
+ return this;
+ }
+
+ private void validateInputs() {
+ checkNotNull(ipAddress, "IP Address must be specified");
+ checkNotNull(assignmentStatus, "Assignment Status must be specified");
+ checkNotNull(leasePeriod, "Lease Period must be specified");
+ checkNotNull(timeStamp, "Timestamp must be specified");
+
+ switch (assignmentStatus) {
+ case Option_Requested:
+ case Option_Assigned:
+ case Option_Expired:
+ break;
+ default:
+ throw new IllegalStateException("Unknown assignment status");
+ }
+ }
+ }
}
diff --git a/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/cli/DHCPLeaseDetails.java b/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/cli/DHCPLeaseDetails.java
new file mode 100644
index 0000000..027c7c2
--- /dev/null
+++ b/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/cli/DHCPLeaseDetails.java
@@ -0,0 +1,41 @@
+/*
+ * 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.dhcpserver.cli;
+
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.dhcpserver.DHCPService;
+
+/**
+ * Lists all the default lease parameters offered by the DHCP Server.
+ */
+@Command(scope = "onos", name = "dhcp-lease",
+ description = "Lists all the default lease parameters offered by the DHCP Server")
+public class DHCPLeaseDetails extends AbstractShellCommand {
+
+ private static final String DHCP_LEASE_FORMAT = "Lease Time: %ds\nRenewal Time: %ds\nRebinding Time: %ds";
+
+ @Override
+ protected void execute() {
+
+ DHCPService dhcpService = AbstractShellCommand.get(DHCPService.class);
+ int leaseTime = dhcpService.getLeaseTime();
+ int renewTime = dhcpService.getRenewalTime();
+ int rebindTime = dhcpService.getRebindingTime();
+
+ print(DHCP_LEASE_FORMAT, leaseTime, renewTime, rebindTime);
+ }
+}
diff --git a/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/cli/DHCPListAllMappings.java b/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/cli/DHCPListAllMappings.java
new file mode 100644
index 0000000..b8d6287
--- /dev/null
+++ b/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/cli/DHCPListAllMappings.java
@@ -0,0 +1,44 @@
+/*
+ * 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.dhcpserver.cli;
+
+import org.apache.karaf.shell.commands.Command;
+import org.onlab.packet.Ip4Address;
+import org.onlab.packet.MacAddress;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.dhcpserver.DHCPService;
+
+import java.util.Map;
+
+/**
+ * Lists all the MacAddress to IP Address mappings held by the DHCP Server.
+ */
+@Command(scope = "onos", name = "dhcp-list",
+ description = "Lists all the MAC to IP mappings held by the DHCP Server")
+public class DHCPListAllMappings extends AbstractShellCommand {
+
+ private static final String DHCP_MAPPING_FORMAT = "MAC ID: %s -> IP ASSIGNED %s";
+ @Override
+ protected void execute() {
+
+ DHCPService dhcpService = AbstractShellCommand.get(DHCPService.class);
+ Map<MacAddress, Ip4Address> allocationMap = dhcpService.listMapping();
+
+ for (Map.Entry<MacAddress, Ip4Address> entry : allocationMap.entrySet()) {
+ print(DHCP_MAPPING_FORMAT, entry.getKey().toString(), entry.getValue().toString());
+ }
+ }
+}
diff --git a/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/cli/DHCPRemoveStaticMapping.java b/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/cli/DHCPRemoveStaticMapping.java
new file mode 100644
index 0000000..c238d12
--- /dev/null
+++ b/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/cli/DHCPRemoveStaticMapping.java
@@ -0,0 +1,56 @@
+/*
+ * 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.dhcpserver.cli;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onlab.packet.MacAddress;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.dhcpserver.DHCPService;
+
+/**
+ * Removes a static MAC Address to IP Mapping from the DHCP Server.
+ */
+@Command(scope = "onos", name = "dhcp-remove-static-mapping",
+ description = "Removes a static MAC Address to IP Mapping from the DHCP Server")
+public class DHCPRemoveStaticMapping extends AbstractShellCommand {
+
+ @Argument(index = 0, name = "macAddr",
+ description = "MAC Address of the client",
+ required = true, multiValued = false)
+ String macAddr = null;
+
+ private static final String DHCP_SUCCESS = "Static Mapping Successfully Removed.";
+ private static final String DHCP_FAILURE = "Static Mapping Failed. " +
+ "Either the mapping does not exist or it is not static.";
+
+ @Override
+ protected void execute() {
+ DHCPService dhcpService = AbstractShellCommand.get(DHCPService.class);
+
+ try {
+ MacAddress macID = MacAddress.valueOf(macAddr);
+ if (dhcpService.removeStaticMapping(macID)) {
+ print(DHCP_SUCCESS);
+ } else {
+ print(DHCP_FAILURE);
+ }
+
+ } catch (IllegalArgumentException e) {
+ print(e.getMessage());
+ }
+ }
+}
diff --git a/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/cli/DHCPSetStaticMapping.java b/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/cli/DHCPSetStaticMapping.java
new file mode 100644
index 0000000..bb4fa97
--- /dev/null
+++ b/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/cli/DHCPSetStaticMapping.java
@@ -0,0 +1,61 @@
+/*
+ * 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.dhcpserver.cli;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onlab.packet.Ip4Address;
+import org.onlab.packet.MacAddress;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.dhcpserver.DHCPService;
+
+/**
+ * Registers a static MAC Address to IP Mapping with the DHCP Server.
+ */
+@Command(scope = "onos", name = "dhcp-set-static-mapping",
+ description = "Registers a static MAC Address to IP Mapping with the DHCP Server")
+public class DHCPSetStaticMapping extends AbstractShellCommand {
+
+ @Argument(index = 0, name = "macAddr",
+ description = "MAC Address of the client",
+ required = true, multiValued = false)
+ String macAddr = null;
+
+ @Argument(index = 1, name = "ipAddr",
+ description = "IP Address requested for static mapping",
+ required = true, multiValued = false)
+ String ipAddr = null;
+
+ private static final String DHCP_SUCCESS = "Static Mapping Successfully Added.";
+ private static final String DHCP_FAILURE = "Static Mapping Failed. The IP maybe unavailable.";
+ @Override
+ protected void execute() {
+ DHCPService dhcpService = AbstractShellCommand.get(DHCPService.class);
+
+ try {
+ MacAddress macID = MacAddress.valueOf(macAddr);
+ Ip4Address ipAddress = Ip4Address.valueOf(ipAddr);
+ if (dhcpService.setStaticMapping(macID, ipAddress)) {
+ print(DHCP_SUCCESS);
+ } else {
+ print(DHCP_FAILURE);
+ }
+
+ } catch (IllegalArgumentException e) {
+ print(e.getMessage());
+ }
+ }
+}
diff --git a/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/cli/FreeIPCompleter.java b/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/cli/FreeIPCompleter.java
new file mode 100644
index 0000000..0989c2e
--- /dev/null
+++ b/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/cli/FreeIPCompleter.java
@@ -0,0 +1,48 @@
+/*
+ * 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.dhcpserver.cli;
+
+import org.apache.karaf.shell.console.Completer;
+import org.apache.karaf.shell.console.completer.StringsCompleter;
+import org.onlab.packet.Ip4Address;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.dhcpserver.DHCPService;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.SortedSet;
+
+/**
+ * Free IP Completer.
+ */
+public class FreeIPCompleter implements Completer {
+
+ @Override
+ public int complete(String buffer, int cursor, List<String> candidates) {
+ // Delegate string completer
+ StringsCompleter delegate = new StringsCompleter();
+ DHCPService dhcpService = AbstractShellCommand.get(DHCPService.class);
+ Iterator<Ip4Address> it = dhcpService.getAvailableIPs().iterator();
+ SortedSet<String> strings = delegate.getStrings();
+
+ while (it.hasNext()) {
+ strings.add(it.next().toString());
+ }
+
+ // Now let the completer do the work for figuring out what to offer.
+ return delegate.complete(buffer, cursor, candidates);
+ }
+}
diff --git a/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/cli/MacIdCompleter.java b/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/cli/MacIdCompleter.java
new file mode 100644
index 0000000..99942fb
--- /dev/null
+++ b/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/cli/MacIdCompleter.java
@@ -0,0 +1,48 @@
+/*
+ * 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.dhcpserver.cli;
+
+import org.apache.karaf.shell.console.Completer;
+import org.apache.karaf.shell.console.completer.StringsCompleter;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.net.Host;
+import org.onosproject.net.host.HostService;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.SortedSet;
+
+/**
+ * MAC ID Completer.
+ */
+public class MacIdCompleter implements Completer {
+
+ @Override
+ public int complete(String buffer, int cursor, List<String> candidates) {
+ // Delegate string completer
+ StringsCompleter delegate = new StringsCompleter();
+ HostService service = AbstractShellCommand.get(HostService.class);
+ Iterator<Host> it = service.getHosts().iterator();
+ SortedSet<String> strings = delegate.getStrings();
+
+ while (it.hasNext()) {
+ strings.add(it.next().mac().toString());
+ }
+
+ // Now let the completer do the work for figuring out what to offer.
+ return delegate.complete(buffer, cursor, candidates);
+ }
+}
diff --git a/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/DHCPServer.java b/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/impl/DHCPManager.java
similarity index 93%
rename from onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/DHCPServer.java
rename to onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/impl/DHCPManager.java
index db9d129..3190a54 100644
--- a/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/DHCPServer.java
+++ b/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/impl/DHCPManager.java
@@ -13,13 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.onosproject.dhcpserver;
+package org.onosproject.dhcpserver.impl;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
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.onlab.packet.DHCP;
import org.onlab.packet.DHCPOption;
import org.onlab.packet.Ethernet;
@@ -29,6 +30,8 @@
import org.onlab.packet.UDP;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService;
+import org.onosproject.dhcpserver.DHCPService;
+import org.onosproject.dhcpserver.DHCPStore;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.flow.DefaultTrafficSelector;
import org.onosproject.net.flow.DefaultTrafficTreatment;
@@ -45,6 +48,7 @@
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
+import java.util.Map;
import static org.onlab.packet.MacAddress.valueOf;
@@ -52,7 +56,8 @@
* Skeletal ONOS DHCP Server application.
*/
@Component(immediate = true)
-public class DHCPServer {
+@Service
+public class DHCPManager implements DHCPService {
private final Logger log = LoggerFactory.getLogger(getClass());
@@ -132,6 +137,42 @@
PacketPriority.CONTROL, appId);
}
+ @Override
+ public Map<MacAddress, Ip4Address> listMapping() {
+
+ return dhcpStore.listMapping();
+ }
+
+ @Override
+ public int getLeaseTime() {
+ return leaseTime;
+ }
+
+ @Override
+ public int getRenewalTime() {
+ return renewalTime;
+ }
+
+ @Override
+ public int getRebindingTime() {
+ return rebindingTime;
+ }
+
+ @Override
+ public boolean setStaticMapping(MacAddress macID, Ip4Address ipAddress) {
+ return dhcpStore.assignStaticIP(macID, ipAddress);
+ }
+
+ @Override
+ public boolean removeStaticMapping(MacAddress macID) {
+ return dhcpStore.removeStaticIP(macID);
+ }
+
+ @Override
+ public Iterable<Ip4Address> getAvailableIPs() {
+ return dhcpStore.getAvailableIPs();
+ }
+
private class DHCPPacketProcessor implements PacketProcessor {
/**
diff --git a/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/DistributedDHCPStore.java b/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/impl/DistributedDHCPStore.java
similarity index 67%
rename from onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/DistributedDHCPStore.java
rename to onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/impl/DistributedDHCPStore.java
index 5671fe6..1ad4bc5 100644
--- a/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/DistributedDHCPStore.java
+++ b/onos-app-dhcpserver/src/main/java/org/onosproject/dhcpserver/impl/DistributedDHCPStore.java
@@ -13,8 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.onosproject.dhcpserver;
+package org.onosproject.dhcpserver.impl;
+import com.google.common.collect.ImmutableSet;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
@@ -27,6 +28,8 @@
import org.onlab.packet.MacAddress;
import org.onlab.util.KryoNamespace;
import org.onlab.util.Timer;
+import org.onosproject.dhcpserver.DHCPStore;
+import org.onosproject.dhcpserver.IPAssignment;
import org.onosproject.store.serializers.KryoNamespaces;
import org.onosproject.store.service.ConsistentMap;
import org.onosproject.store.service.DistributedSet;
@@ -37,6 +40,7 @@
import org.slf4j.LoggerFactory;
import java.util.Date;
+import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@@ -64,11 +68,11 @@
private static final int INITIAL_DELAY = 2;
- private static long timeoutForPendingAssignments = 60;
+ private static int timeoutForPendingAssignments = 60;
- private static final Ip4Address START_IP = Ip4Address.valueOf("10.1.0.100");
+ private static final Ip4Address START_IP = Ip4Address.valueOf("10.1.0.140");
- private static final Ip4Address END_IP = Ip4Address.valueOf("10.1.0.200");
+ private static final Ip4Address END_IP = Ip4Address.valueOf("10.1.0.160");
@Activate
protected void activate() {
@@ -108,13 +112,17 @@
IPAssignment assignmentInfo;
if (allocationMap.containsKey(macID)) {
assignmentInfo = allocationMap.get(macID).value();
- return assignmentInfo.getIpAddress();
+ return assignmentInfo.ipAddress();
} else {
Ip4Address nextIPAddr = fetchNextIP();
- assignmentInfo = new IPAssignment(nextIPAddr,
- timeoutForPendingAssignments,
- IPAssignment.AssignmentStatus.Option_Requested);
+
+ assignmentInfo = IPAssignment.builder()
+ .ipAddress(nextIPAddr)
+ .timestamp(new Date())
+ .leasePeriod(timeoutForPendingAssignments)
+ .assignmentStatus(IPAssignment.AssignmentStatus.Option_Requested)
+ .build();
allocationMap.put(macID, assignmentInfo);
return nextIPAddr;
@@ -122,20 +130,29 @@
}
@Override
- public boolean assignIP(MacAddress macID, Ip4Address ipAddr, long leaseTime) {
+ public boolean assignIP(MacAddress macID, Ip4Address ipAddr, int leaseTime) {
IPAssignment assignmentInfo;
if (allocationMap.containsKey(macID)) {
assignmentInfo = allocationMap.get(macID).value();
- if (assignmentInfo.getIpAddress().toInt() == ipAddr.toInt()) {
- assignmentInfo.setAssignmentStatus(IPAssignment.AssignmentStatus.Option_Assigned);
- assignmentInfo.setTimestamp(new Date());
- assignmentInfo.setLeasePeriodinSeconds(leaseTime);
+ if (assignmentInfo.ipAddress().toInt() == ipAddr.toInt()) {
+
+ assignmentInfo = IPAssignment.builder()
+ .ipAddress(ipAddr)
+ .timestamp(new Date())
+ .leasePeriod(leaseTime)
+ .assignmentStatus(IPAssignment.AssignmentStatus.Option_Assigned)
+ .build();
allocationMap.put(macID, assignmentInfo);
return true;
}
} else if (freeIPPool.contains(ipAddr)) {
- assignmentInfo = new IPAssignment(ipAddr, leaseTime, IPAssignment.AssignmentStatus.Option_Assigned);
+ assignmentInfo = IPAssignment.builder()
+ .ipAddress(ipAddr)
+ .timestamp(new Date())
+ .leasePeriod(leaseTime)
+ .assignmentStatus(IPAssignment.AssignmentStatus.Option_Assigned)
+ .build();
if (freeIPPool.remove(ipAddr)) {
allocationMap.put(macID, assignmentInfo);
return true;
@@ -147,17 +164,55 @@
@Override
public void releaseIP(MacAddress macID) {
if (allocationMap.containsKey(macID)) {
- Ip4Address freeIP = allocationMap.get(macID).value().getIpAddress();
+ Ip4Address freeIP = allocationMap.get(macID).value().ipAddress();
allocationMap.remove(macID);
freeIPPool.add(freeIP);
}
}
@Override
- public void setDefaultTimeoutForPurge(long timeInSeconds) {
+ public void setDefaultTimeoutForPurge(int timeInSeconds) {
timeoutForPendingAssignments = timeInSeconds;
}
+ @Override
+ public Map<MacAddress, Ip4Address> listMapping() {
+
+ Map<MacAddress, Ip4Address> allMapping = new HashMap<>();
+ for (Map.Entry<MacAddress, Versioned<IPAssignment>> entry: allocationMap.entrySet()) {
+ IPAssignment assignment = entry.getValue().value();
+ if (assignment.assignmentStatus() == IPAssignment.AssignmentStatus.Option_Assigned) {
+ allMapping.put(entry.getKey(), assignment.ipAddress());
+ }
+ }
+
+ return allMapping;
+ }
+
+ @Override
+ public boolean assignStaticIP(MacAddress macID, Ip4Address ipAddr) {
+ return assignIP(macID, ipAddr, -1);
+ }
+
+ @Override
+ public boolean removeStaticIP(MacAddress macID) {
+ if (allocationMap.containsKey(macID)) {
+ IPAssignment assignment = allocationMap.get(macID).value();
+ Ip4Address freeIP = assignment.ipAddress();
+ if (assignment.leasePeriod() < 0) {
+ allocationMap.remove(macID);
+ freeIPPool.add(freeIP);
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public Iterable<Ip4Address> getAvailableIPs() {
+ return ImmutableSet.<Ip4Address>copyOf(freeIPPool);
+ }
+
/**
* Appends all the IPs in a given range to the free pool of IPs.
*
@@ -198,9 +253,9 @@
Date dateNow = new Date();
for (Map.Entry<MacAddress, Versioned<IPAssignment>> entry: allocationMap.entrySet()) {
ipAssignment = entry.getValue().value();
- long timeLapsed = dateNow.getTime() - ipAssignment.getTimestamp().getTime();
- if (timeLapsed > (ipAssignment.getLeasePeriod())) {
- Ip4Address freeIP = ipAssignment.getIpAddress();
+ long timeLapsed = dateNow.getTime() - ipAssignment.timestamp().getTime();
+ if ((ipAssignment.leasePeriod() > 0) && (timeLapsed > (ipAssignment.leasePeriod()))) {
+ Ip4Address freeIP = ipAssignment.ipAddress();
allocationMap.remove(entry.getKey());
freeIPPool.add(freeIP);
}
diff --git a/onos-app-dhcpserver/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/onos-app-dhcpserver/src/main/resources/OSGI-INF/blueprint/shell-config.xml
new file mode 100644
index 0000000..954af27
--- /dev/null
+++ b/onos-app-dhcpserver/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -0,0 +1,43 @@
+<!--
+ ~ 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.
+ -->
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
+
+ <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0">
+ <command>
+ <action class="org.onosproject.dhcpserver.cli.DHCPListAllMappings"/>
+ </command>
+ <command>
+ <action class="org.onosproject.dhcpserver.cli.DHCPLeaseDetails"/>
+ </command>
+ <command>
+ <action class="org.onosproject.dhcpserver.cli.DHCPSetStaticMapping"/>
+ <completers>
+ <ref component-id="macIDCompleter"/>
+ <ref component-id="freeIPCompleter"/>
+ </completers>
+ </command>
+ <command>
+ <action class="org.onosproject.dhcpserver.cli.DHCPRemoveStaticMapping"/>
+ <completers>
+ <ref component-id="macIDCompleter"/>
+ </completers>
+ </command>
+ </command-bundle>
+
+ <bean id="macIDCompleter" class="org.onosproject.dhcpserver.cli.MacIdCompleter"/>
+ <bean id="freeIPCompleter" class="org.onosproject.dhcpserver.cli.FreeIPCompleter"/>
+
+</blueprint>
\ No newline at end of file
diff --git a/onos-app-dhcpserver/src/test/java/org/onosproject/dhcpserver/DHCPServerTest.java b/onos-app-dhcpserver/src/test/java/org/onosproject/dhcpserver/impl/DHCPManagerTest.java
similarity index 86%
rename from onos-app-dhcpserver/src/test/java/org/onosproject/dhcpserver/DHCPServerTest.java
rename to onos-app-dhcpserver/src/test/java/org/onosproject/dhcpserver/impl/DHCPManagerTest.java
index b91cb81..a1ffa78 100644
--- a/onos-app-dhcpserver/src/test/java/org/onosproject/dhcpserver/DHCPServerTest.java
+++ b/onos-app-dhcpserver/src/test/java/org/onosproject/dhcpserver/impl/DHCPManagerTest.java
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.onosproject.dhcpserver;
+package org.onosproject.dhcpserver.impl;
import org.junit.After;
import org.junit.Before;
@@ -26,13 +26,13 @@
// TODO Add tests later.
@Ignore
-public class DHCPServerTest {
+public class DHCPManagerTest {
- private DHCPServer component;
+ private DHCPManager component;
@Before
public void setUp() {
- component = new DHCPServer();
+ component = new DHCPManager();
component.activate();
}