diff options
author | Ashlee Young <ashlee@wildernessvoice.com> | 2015-11-03 14:08:10 -0800 |
---|---|---|
committer | Ashlee Young <ashlee@wildernessvoice.com> | 2015-11-03 14:08:10 -0800 |
commit | 643ee33289bd2cb9e6afbfb09b4ed72d467ba1c2 (patch) | |
tree | c2c376a44a359544fe3d4c45eb0cc0e2ec4a7080 /framework/src/onos/apps/dhcp/api | |
parent | 46eeb79b54345bdafb6055b8ee4bad4ce8b01274 (diff) |
This updates ONOS src tree to commit id
03fa5e571cabbd001ddb1598847e1150b11c7333
Change-Id: I13b554026d6f902933e35887d29bd5fdb669c0bd
Signed-off-by: Ashlee Young <ashlee@wildernessvoice.com>
Diffstat (limited to 'framework/src/onos/apps/dhcp/api')
6 files changed, 589 insertions, 0 deletions
diff --git a/framework/src/onos/apps/dhcp/api/pom.xml b/framework/src/onos/apps/dhcp/api/pom.xml new file mode 100644 index 00000000..fb5246f7 --- /dev/null +++ b/framework/src/onos/apps/dhcp/api/pom.xml @@ -0,0 +1,64 @@ +<?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-dhcp</artifactId> + <groupId>org.onosproject</groupId> + <version>1.4.0-SNAPSHOT</version> + <relativePath>../pom.xml</relativePath> + </parent> + + <artifactId>onos-app-dhcp-api</artifactId> + <packaging>bundle</packaging> + + <url>http://onosproject.org</url> + + <description>DHCP Server application API</description> + + <dependencies> + <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> + + </dependencies> + + +</project> diff --git a/framework/src/onos/apps/dhcp/api/src/main/java/org/onosproject/dhcp/DhcpService.java b/framework/src/onos/apps/dhcp/api/src/main/java/org/onosproject/dhcp/DhcpService.java new file mode 100644 index 00000000..7c2127f9 --- /dev/null +++ b/framework/src/onos/apps/dhcp/api/src/main/java/org/onosproject/dhcp/DhcpService.java @@ -0,0 +1,81 @@ +/* + * 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; + +import org.onlab.packet.Ip4Address; +import org.onlab.packet.MacAddress; +import org.onosproject.net.HostId; + +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<HostId, IpAssignment> 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(); + +} diff --git a/framework/src/onos/apps/dhcp/api/src/main/java/org/onosproject/dhcp/DhcpStore.java b/framework/src/onos/apps/dhcp/api/src/main/java/org/onosproject/dhcp/DhcpStore.java new file mode 100644 index 00000000..e263b3a2 --- /dev/null +++ b/framework/src/onos/apps/dhcp/api/src/main/java/org/onosproject/dhcp/DhcpStore.java @@ -0,0 +1,109 @@ +/* + * 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; + +import org.onlab.packet.Ip4Address; +import org.onlab.packet.MacAddress; +import org.onosproject.net.HostId; + +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 hostId Host ID of the client requesting an IP + * @param requestedIP requested IP address + * @return IP address assigned to the Mac ID + */ + Ip4Address suggestIP(HostId hostId, Ip4Address requestedIP); + + /** + * Assigns the requested IP to the Mac ID, in response to a DHCP REQUEST message. + * + * @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(HostId hostId, 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); + + /** + * Releases the IP assigned to a Mac ID into the free pool. + * + * @param hostId the host ID for which the mapping needs to be changed + * @return released ip + */ + Ip4Address releaseIP(HostId hostId); + + /** + * Returns a collection of all the MacAddress to IPAddress mapping assigned to the hosts. + * + * @return the collection of the mappings + */ + Map<HostId, IpAssignment> listAssignedMapping(); + + /** + * Returns a collection of all the MacAddress to IPAddress mapping. + * + * @return the collection of the mappings + */ + Map<HostId, IpAssignment> listAllMapping(); + + /** + * 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/framework/src/onos/apps/dhcp/api/src/main/java/org/onosproject/dhcp/IpAssignment.java b/framework/src/onos/apps/dhcp/api/src/main/java/org/onosproject/dhcp/IpAssignment.java new file mode 100644 index 00000000..9b3aa686 --- /dev/null +++ b/framework/src/onos/apps/dhcp/api/src/main/java/org/onosproject/dhcp/IpAssignment.java @@ -0,0 +1,215 @@ +/* + * 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 in seconds + */ + public int leasePeriod() { + 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 + 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. + * + * @param assignment ip address assignment + * @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(); + 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; + 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/framework/src/onos/apps/dhcp/api/src/main/java/org/onosproject/dhcp/package-info.java b/framework/src/onos/apps/dhcp/api/src/main/java/org/onosproject/dhcp/package-info.java new file mode 100644 index 00000000..56778a35 --- /dev/null +++ b/framework/src/onos/apps/dhcp/api/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/api/src/test/java/org/onosproject/dhcp/IpAssignmentTest.java b/framework/src/onos/apps/dhcp/api/src/test/java/org/onosproject/dhcp/IpAssignmentTest.java new file mode 100644 index 00000000..3ecc5cfa --- /dev/null +++ b/framework/src/onos/apps/dhcp/api/src/test/java/org/onosproject/dhcp/IpAssignmentTest.java @@ -0,0 +1,100 @@ +/* + * 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 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 { + + 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")); + } + } +} |