samanwita pal
Committed by Gerrit Code Review

Moving DHCP to onos branch. REST API included, GUI to be added later.

Change-Id: Id52781ba93d48ad1d56097a9ceff7613a190c48e
Showing 21 changed files with 1556 additions and 0 deletions
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
--><project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>onos-apps</artifactId>
<groupId>org.onosproject</groupId>
<version>1.3.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>onos-app-dhcp</artifactId>
<packaging>bundle</packaging>
<url>http://onosproject.org</url>
<description>DHCP Server application</description>
<properties>
<onos.app.name>org.onosproject.dhcp</onos.app.name>
<web.context>/onos/dhcp</web.context>
<api.version>1.0.0</api.version>
<api.title>DHCP Server REST API</api.title>
<api.description>
APIs for interacting with the DHCP Server application.
</api.description>
<api.package>org.onosproject.dhcp.rest</api.package>
</properties>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.compendium</artifactId>
</dependency>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onos-cli</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.karaf.shell</groupId>
<artifactId>org.apache.karaf.shell.console</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onlab-junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onos-core-serializers</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onos-incubator-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onos-api</artifactId>
<version>${project.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onos-rest</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onlab-rest</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<_wab>src/main/webapp/</_wab>
<Include-Resource>
WEB-INF/classes/apidoc/swagger.json=target/swagger.json,
{maven-resources}
</Include-Resource>
<Bundle-SymbolicName>
${project.groupId}.${project.artifactId}
</Bundle-SymbolicName>
<Import-Package>
org.slf4j,
org.osgi.framework,
javax.ws.rs,
javax.ws.rs.core,
com.sun.jersey.api.core,
com.sun.jersey.spi.container.servlet,
com.sun.jersey.server.impl.container.servlet,
com.fasterxml.jackson.databind,
com.fasterxml.jackson.databind.node,
com.fasterxml.jackson.core,
org.apache.karaf.shell.commands,
org.apache.karaf.shell.console,
com.google.common.*,
org.onlab.packet.*,
org.onlab.rest.*,
org.onosproject.*,
org.onlab.util.*,
org.jboss.netty.util.*
</Import-Package>
<Web-ContextPath>${web.context}</Web-ContextPath>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
/*
* 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;
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.
*/
Map<MacAddress, Ip4Address> listMapping();
/**
* Returns the default lease time granted by the DHCP Server.
*
* @return lease time
*/
int getLeaseTime();
/**
* Returns the default renewal time granted by the DHCP Server.
*
* @return renewal time
*/
int getRenewalTime();
/**
* Returns the default rebinding time granted by the DHCP Server.
*
* @return rebinding time
*/
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
*/
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
*/
boolean removeStaticMapping(MacAddress macID);
/**
* Returns the list of all the available IPs with the server.
*
* @return list of available IPs
*/
Iterable<Ip4Address> getAvailableIPs();
}
/*
* 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;
import org.onlab.packet.Ip4Address;
import org.onlab.packet.MacAddress;
import java.util.Map;
/**
* DHCPStore Interface.
*/
public interface DHCPStore {
/**
* Appends all the IPs in a given range to the free pool of IPs.
*
* @param startIP Start IP for the range
* @param endIP End IP for the range
*/
void populateIPPoolfromRange(Ip4Address startIP, Ip4Address endIP);
/**
* 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
* @return IP address assigned to the Mac ID
*/
Ip4Address suggestIP(MacAddress macID, 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 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);
/**
* Sets the default time for which suggested IP mappings are valid.
*
* @param timeInSeconds default time for IP mappings to be valid
*/
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
*/
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();
}
/*
* 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;
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 {
private final Ip4Address ipAddress;
private final Date timestamp;
private final long leasePeriod;
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;
}
/**
* Constructor for IPAssignment, where the ipAddress, the lease period, the timestamp
* and assignment status is supplied.
*
* @param ipAddress
* @param leasePeriod
* @param assignmentStatus
*/
private IPAssignment(Ip4Address ipAddress,
long leasePeriod,
Date timestamp,
AssignmentStatus assignmentStatus) {
this.ipAddress = ipAddress;
this.leasePeriod = leasePeriod;
this.timestamp = timestamp;
this.assignmentStatus = assignmentStatus;
}
/**
* Returns the IP Address of the IP assignment.
*
* @return the IP address
*/
public Ip4Address ipAddress() {
return this.ipAddress;
}
/**
* Returns the timestamp of the IP assignment.
*
* @return the timestamp
*/
public Date timestamp() {
return this.timestamp;
}
/**
* Returns the assignment status of the IP assignment.
*
* @return the assignment status
*/
public AssignmentStatus assignmentStatus() {
return this.assignmentStatus;
}
/**
* Returns the lease period of the IP assignment.
*
* @return the lease period
*/
public int leasePeriod() {
return (int) this.leasePeriod / 1000;
}
@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();
}
/**
* Creates and returns a new builder instance that clones an existing IPAssignment.
*
* @return new builder
*/
public static Builder builder(IPAssignment assignment) {
return new Builder(assignment);
}
/**
* 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() * 1000;
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");
}
}
}
}
/*
* 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.cli;
import org.apache.karaf.shell.commands.Command;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.dhcp.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);
}
}
/*
* 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.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.dhcp.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());
}
}
}
/*
* 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.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.dhcp.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 Removal 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());
}
}
}
/*
* 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.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.dhcp.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());
}
}
}
/*
* 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.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.dhcp.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);
}
}
/*
* 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.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);
}
}
/*
* 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 Config class.
*/
public class DHCPConfig extends Config<ApplicationId> {
public static final String MY_IP = "ip";
public static final String MY_MAC = "mac";
public static final String SUBNET_MASK = "subnet";
public static final String BROADCAST_ADDRESS = "broadcast";
public static final String ROUTER_ADDRESS = "router";
public static final String DOMAIN_SERVER = "domain";
public static final String TTL = "ttl";
public static final String LEASE_TIME = "lease";
public static final String RENEW_TIME = "renew";
public static final String REBIND_TIME = "rebind";
/**
* Returns the dhcp server ip.
*
* @return ip address or null if not set
*/
public String ip() {
return get(MY_IP, null);
}
/**
* Sets the dhcp server ip.
*
* @param ip new ip address; null to clear
* @return self
*/
public BasicElementConfig ip(String ip) {
return (BasicElementConfig) setOrClear(MY_IP, ip);
}
/**
* Returns the dhcp server mac.
*
* @return server mac or null if not set
*/
public String mac() {
return get(MY_MAC, null);
}
/**
* Sets the dhcp server mac.
*
* @param mac new mac address; null to clear
* @return self
*/
public BasicElementConfig mac(String mac) {
return (BasicElementConfig) setOrClear(MY_MAC, mac);
}
/**
* Returns the subnet mask.
*
* @return subnet mask or null if not set
*/
public String subnetMask() {
return get(SUBNET_MASK, null);
}
/**
* Sets the subnet mask.
*
* @param subnet new subnet mask; null to clear
* @return self
*/
public BasicElementConfig subnetMask(String subnet) {
return (BasicElementConfig) setOrClear(SUBNET_MASK, subnet);
}
/**
* Returns the broadcast address.
*
* @return broadcast address or null if not set
*/
public String broadcastAddress() {
return get(BROADCAST_ADDRESS, null);
}
/**
* Sets the broadcast address.
*
* @param broadcast new broadcast address; null to clear
* @return self
*/
public BasicElementConfig broadcastAddress(String broadcast) {
return (BasicElementConfig) setOrClear(BROADCAST_ADDRESS, broadcast);
}
/**
* Returns the Time To Live for the reply packets.
*
* @return ttl or null if not set
*/
public String ttl() {
return get(TTL, null);
}
/**
* Sets the Time To Live for the reply packets.
*
* @param ttl new ttl; null to clear
* @return self
*/
public BasicElementConfig ttl(String ttl) {
return (BasicElementConfig) setOrClear(TTL, ttl);
}
/**
* Returns the Lease Time offered by the DHCP Server.
*
* @return lease time or null if not set
*/
public String leaseTime() {
return get(LEASE_TIME, null);
}
/**
* Sets the Lease Time offered by the DHCP Server.
*
* @param lease new lease time; null to clear
* @return self
*/
public BasicElementConfig leaseTime(String lease) {
return (BasicElementConfig) setOrClear(LEASE_TIME, lease);
}
/**
* Returns the Renew Time offered by the DHCP Server.
*
* @return renew time or null if not set
*/
public String renewTime() {
return get(RENEW_TIME, null);
}
/**
* Sets the Renew Time offered by the DHCP Server.
*
* @param renew new renew time; null to clear
* @return self
*/
public BasicElementConfig renewTime(String renew) {
return (BasicElementConfig) setOrClear(RENEW_TIME, renew);
}
/**
* Returns the Rebind Time offered by the DHCP Server.
*
* @return rebind time or null if not set
*/
public String rebindTime() {
return get(REBIND_TIME, null);
}
/**
* Sets the Rebind Time offered by the DHCP Server.
*
* @param rebind new rebind time; null to clear
* @return self
*/
public BasicElementConfig rebindTime(String rebind) {
return (BasicElementConfig) setOrClear(REBIND_TIME, rebind);
}
/**
* Returns the Router Address.
*
* @return router address or null if not set
*/
public String routerAddress() {
return get(ROUTER_ADDRESS, null);
}
/**
* Sets the Router Address.
*
* @param router new router address; null to clear
* @return self
*/
public BasicElementConfig routerAddress(String router) {
return (BasicElementConfig) setOrClear(ROUTER_ADDRESS, router);
}
/**
* Returns the Domain Server Address.
*
* @return domain server address or null if not set
*/
public String domainServer() {
return get(DOMAIN_SERVER, null);
}
/**
* Sets the Domain Server Address.
*
* @param domain new domain server address; null to clear
* @return self
*/
public BasicElementConfig domainServer(String domain) {
return (BasicElementConfig) setOrClear(DOMAIN_SERVER, domain);
}
}
/*
* 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> {
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);
}
}
/*
* 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.
*/
package org.onosproject.dhcp.rest;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.onlab.packet.Ip4Address;
import org.onlab.packet.MacAddress;
import org.onosproject.dhcp.DHCPService;
import org.onosproject.rest.AbstractWebResource;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
/**
* Manage DHCP address assignments.
*/
@Path("dhcp")
public class DHCPWebResource extends AbstractWebResource {
final DHCPService service = get(DHCPService.class);
/**
* Get DHCP server configuration data.
* Shows lease, renewal and rebinding times in seconds.
*
* @return 200 OK
*/
@GET
@Path("config")
public Response getConfigs() {
DHCPService service = get(DHCPService.class);
ObjectNode node = mapper().createObjectNode()
.put("leaseTime", service.getLeaseTime())
.put("renewalTime", service.getRenewalTime())
.put("rebindingTime", service.getRebindingTime());
return ok(node.toString()).build();
}
/**
* Get all MAC/IP mappings.
* Shows all MAC/IP mappings held by the DHCP server.
*
* @return 200 OK
*/
@GET
@Path("mappings")
public Response listMappings() {
ObjectNode root = mapper().createObjectNode();
final Map<MacAddress, Ip4Address> intents = service.listMapping();
ArrayNode arrayNode = root.putArray("mappings");
intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode()
.put("mac", i.getKey().toString())
.put("ip", i.getValue().toString())));
return ok(root.toString()).build();
}
/**
* Get all available IPs.
* Shows all the IPs in the free pool of the DHCP Server.
*
* @return 200 OK
*/
@GET
@Path("available")
public Response listAvailableIPs() {
final Iterable<Ip4Address> availableIPList = service.getAvailableIPs();
final ObjectNode root = mapper().createObjectNode();
ArrayNode arrayNode = root.putArray("availableIP");
availableIPList.forEach(i -> arrayNode.add(i.toString()));
return ok(root.toString()).build();
}
/**
* Post a new static MAC/IP binding.
* Registers a static binding to the DHCP server, and displays the current set of bindings.
*
* @return 200 OK
*/
@POST
@Path("mappings")
@Consumes(MediaType.APPLICATION_JSON)
public Response setMapping(InputStream stream) {
ObjectNode root = mapper().createObjectNode();
try {
ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
JsonNode macID = jsonTree.get("mac");
JsonNode ip = jsonTree.get("ip");
if (macID != null && ip != null) {
if (!service.setStaticMapping(MacAddress.valueOf(macID.asText()),
Ip4Address.valueOf(ip.asText()))) {
throw new IllegalArgumentException("Static Mapping Failed. The IP maybe unavailable.");
}
}
final Map<MacAddress, Ip4Address> intents = service.listMapping();
ArrayNode arrayNode = root.putArray("mappings");
intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode()
.put("mac", i.getKey().toString())
.put("ip", i.getValue().toString())));
} catch (IOException e) {
throw new IllegalArgumentException(e.getMessage());
}
return ok(root.toString()).build();
}
/**
* Delete a static MAC/IP binding.
* Removes a static binding from the DHCP Server, and displays the current set of bindings.
*
* @return 200 OK
*/
@DELETE
@Path("mappings/{macID}")
public Response deleteMapping(@PathParam("macID") String macID) {
ObjectNode root = mapper().createObjectNode();
if (!service.removeStaticMapping(MacAddress.valueOf(macID))) {
throw new IllegalArgumentException("Static Mapping Removal Failed.");
}
final Map<MacAddress, Ip4Address> intents = service.listMapping();
ArrayNode arrayNode = root.putArray("mappings");
intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode()
.put("mac", i.getKey().toString())
.put("ip", i.getValue().toString())));
return ok(root.toString()).build();
}
}
<!--
~ 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.dhcp.cli.DHCPListAllMappings"/>
</command>
<command>
<action class="org.onosproject.dhcp.cli.DHCPLeaseDetails"/>
</command>
<command>
<action class="org.onosproject.dhcp.cli.DHCPSetStaticMapping"/>
<completers>
<ref component-id="macIDCompleter"/>
<ref component-id="freeIPCompleter"/>
</completers>
</command>
<command>
<action class="org.onosproject.dhcp.cli.DHCPRemoveStaticMapping"/>
<completers>
<ref component-id="macIDCompleter"/>
</completers>
</command>
</command-bundle>
<bean id="macIDCompleter" class="org.onosproject.dhcp.cli.MacIdCompleter"/>
<bean id="freeIPCompleter" class="org.onosproject.dhcp.cli.FreeIPCompleter"/>
</blueprint>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="ONOS" version="2.5">
<display-name>DHCP Server REST API v1.0</display-name>
<servlet>
<servlet-name>JAX-RS Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.resourceConfigClass</param-name>
<param-value>com.sun.jersey.api.core.ClassNamesResourceConfig</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.classnames</param-name>
<param-value>
org.onosproject.dhcp.rest.DHCPWebResource
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAX-RS Service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
/*
* 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;
import com.google.common.testing.EqualsTester;
import junit.framework.TestCase;
import org.junit.Assert;
import org.junit.Test;
import org.onlab.packet.Ip4Address;
import java.util.Date;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.fail;
/**
* Unit Tests for IPAssignment class.
*/
public class IPAssignmentTest extends TestCase {
private final Date dateNow = new Date();
private final IPAssignment stats1 = IPAssignment.builder()
.ipAddress(Ip4Address.valueOf("10.10.10.10"))
.leasePeriod(300)
.assignmentStatus(IPAssignment.AssignmentStatus.Option_Expired)
.timestamp(dateNow)
.build();
private final IPAssignment stats2 = IPAssignment.builder()
.ipAddress(Ip4Address.valueOf("10.10.10.10"))
.leasePeriod(300)
.assignmentStatus(IPAssignment.AssignmentStatus.Option_Assigned)
.timestamp(dateNow)
.build();
private final IPAssignment stats3 = IPAssignment.builder(stats1)
.build();
/**
* Tests the constructor for the class.
*/
@Test
public void testConstruction() {
assertThat(stats3.ipAddress(), is(Ip4Address.valueOf("10.10.10.10")));
assertThat(stats3.timestamp(), is(dateNow));
assertThat(stats3.leasePeriod(), is(300));
assertThat(stats3.assignmentStatus(), is(IPAssignment.AssignmentStatus.Option_Expired));
}
/**
* Tests the equality and inequality of objects using Guava EqualsTester.
*/
@Test
public void testEquals() {
new EqualsTester()
.addEqualityGroup(stats1, stats1)
.addEqualityGroup(stats2)
.testEquals();
}
/**
* Tests if the toString method returns a consistent value for hashing.
*/
@Test
public void testToString() {
assertThat(stats1.toString(), is(stats1.toString()));
}
/**
* Tests if the validateInputs method returns an exception for malformed object.
*/
@Test
public void testValidateInputs() {
try {
IPAssignment stats4 = IPAssignment.builder()
.ipAddress(Ip4Address.valueOf("10.10.10.10"))
.leasePeriod(300)
.build();
fail("Construction of a malformed IPAssignment did not throw an exception");
} catch (NullPointerException e) {
Assert.assertThat(e.getMessage(), containsString("must be specified"));
}
}
}
\ No newline at end of file
{
"apps": {
"org.onosproject.dhcp" : {
"dhcp" : {
"ip": "10.0.0.1",
"mac": "1a:2b:3c:4e:5e:6f",
"subnet": "255.0.0.0",
"broadcast": "10.255.255.255",
"router": "10.0.0.1",
"domain": "10.0.0.1",
"ttl": "63",
"lease": "300",
"renew": "150",
"rebind": "200"
},
"dhcpstore" : {
"delay": "3",
"timeout": "150",
"startip": "10.0.0.110",
"endip": "10.0.0.130"
}
}
}
}
\ No newline at end of file
......@@ -57,6 +57,7 @@
<module>vtnrsc</module>
<module>vtn</module>
<module>vtnweb</module>
<module>onos-app-dhcp</module>
</modules>
<properties>
......