diff options
author | Ashlee Young <ashlee@wildernessvoice.com> | 2015-11-22 10:02:05 -0800 |
---|---|---|
committer | Ashlee Young <ashlee@wildernessvoice.com> | 2015-11-22 10:02:05 -0800 |
commit | 77ce3be7567bd01c66d8ee88a93b485666723501 (patch) | |
tree | 283e7f39a501750bbd705fbb91645b20198900f1 /framework/src/onos/apps/dhcp/app/src | |
parent | 00e6500d0813dcbccaaa741ef38cc1eae6d11e07 (diff) |
Removed patch path since changes have been merged upstream to a different path. Updated README with directions.
Change-Id: Ie419abd2d3d3ef7315de9f607dcd757a78190995
Signed-off-by: Ashlee Young <ashlee@wildernessvoice.com>
Diffstat (limited to 'framework/src/onos/apps/dhcp/app/src')
6 files changed, 55 insertions, 193 deletions
diff --git a/framework/src/onos/apps/dhcp/app/src/main/java/org/onosproject/dhcp/impl/DhcpConfig.java b/framework/src/onos/apps/dhcp/app/src/main/java/org/onosproject/dhcp/impl/DhcpConfig.java index 4353d623..1efdd082 100644 --- a/framework/src/onos/apps/dhcp/app/src/main/java/org/onosproject/dhcp/impl/DhcpConfig.java +++ b/framework/src/onos/apps/dhcp/app/src/main/java/org/onosproject/dhcp/impl/DhcpConfig.java @@ -21,6 +21,9 @@ import org.onosproject.core.ApplicationId; import org.onosproject.net.config.Config; import org.onosproject.net.config.basics.BasicElementConfig; +import static org.onosproject.net.config.Config.FieldPresence.MANDATORY; +import static org.onosproject.net.config.Config.FieldPresence.OPTIONAL; + /** * DHCP Config class. */ @@ -43,6 +46,20 @@ public class DhcpConfig extends Config<ApplicationId> { public static final int DEFAULT = -1; + @Override + public boolean isValid() { + // FIXME: Sweep through and revisit the validation assertions + // For now, this is just a demonstration of potential uses + return hasOnlyFields(MY_IP, MY_MAC, SUBNET_MASK, BROADCAST_ADDRESS, + ROUTER_ADDRESS, DOMAIN_SERVER, TTL, LEASE_TIME, + RENEW_TIME, REBIND_TIME, TIMER_DELAY, DEFAULT_TIMEOUT, + START_IP, END_IP) && + isIpAddress(MY_IP, MANDATORY) && isMacAddress(MY_MAC, MANDATORY) && + isIpAddress(START_IP, MANDATORY) && isIpAddress(END_IP, MANDATORY) && + isNumber(LEASE_TIME, OPTIONAL, 1) && isNumber(REBIND_TIME, OPTIONAL, 1) && + isNumber(DEFAULT_TIMEOUT, OPTIONAL, 1, 3600); + } + /** * Returns the dhcp server ip. * diff --git a/framework/src/onos/apps/dhcp/app/src/main/java/org/onosproject/dhcp/impl/DhcpManager.java b/framework/src/onos/apps/dhcp/app/src/main/java/org/onosproject/dhcp/impl/DhcpManager.java index 4093f2d2..a1707e0b 100644 --- a/framework/src/onos/apps/dhcp/app/src/main/java/org/onosproject/dhcp/impl/DhcpManager.java +++ b/framework/src/onos/apps/dhcp/app/src/main/java/org/onosproject/dhcp/impl/DhcpManager.java @@ -109,7 +109,7 @@ public class DhcpManager implements DhcpService { @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) protected PacketService packetService; - private DHCPPacketProcessor processor = new DHCPPacketProcessor(); + private DhcpPacketProcessor processor = new DhcpPacketProcessor(); @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) protected CoreService coreService; @@ -241,12 +241,12 @@ public class DhcpManager implements DhcpService { } @Override - public boolean setStaticMapping(MacAddress macID, Ip4Address ipAddress, boolean fromOpenStack, + public boolean setStaticMapping(MacAddress macID, Ip4Address ipAddress, boolean rangeNotEnforced, List<Ip4Address> addressList) { log.debug("setStaticMapping is called with Mac: {}, Ip: {} addressList: {}", macID.toString(), ipAddress.toString(), addressList.toString()); - return dhcpStore.assignStaticIP(macID, ipAddress, fromOpenStack, addressList); + return dhcpStore.assignStaticIP(macID, ipAddress, rangeNotEnforced, addressList); } @Override @@ -259,7 +259,7 @@ public class DhcpManager implements DhcpService { return dhcpStore.getAvailableIPs(); } - private class DHCPPacketProcessor implements PacketProcessor { + private class DhcpPacketProcessor implements PacketProcessor { /** * Builds the DHCP Reply packet. @@ -279,7 +279,7 @@ public class DhcpManager implements DhcpService { ipAssignment = dhcpStore.getIpAssignmentFromAllocationMap(HostId.hostId(packet.getSourceMAC())); - if (ipAssignment != null && ipAssignment.fromOpenStack()) { + if (ipAssignment != null && ipAssignment.rangeNotEnforced()) { subnetMaskReply = ipAssignment.subnetMask(); dhcpServerReply = ipAssignment.dhcpServer(); domainServerReply = ipAssignment.domainServer(); @@ -437,7 +437,7 @@ public class DhcpManager implements DhcpService { * @param context context of the incoming message * @param dhcpPayload the extracted DHCP payload */ - private void processDHCPPacket(PacketContext context, DHCP dhcpPayload) { + private void processDhcpPacket(PacketContext context, DHCP dhcpPayload) { Ethernet packet = context.inPacket().parsed(); boolean flagIfRequestedIP = false; boolean flagIfServerIP = false; @@ -464,9 +464,9 @@ public class DhcpManager implements DhcpService { } } DHCPPacketType outgoingPacketType; - MacAddress clientMAC = new MacAddress(dhcpPayload.getClientHardwareAddress()); + MacAddress clientMac = new MacAddress(dhcpPayload.getClientHardwareAddress()); VlanId vlanId = VlanId.vlanId(packet.getVlanID()); - HostId hostId = HostId.hostId(clientMAC, vlanId); + HostId hostId = HostId.hostId(clientMac, vlanId); if (incomingPacketType.getValue() == DHCPPacketType.DHCPDISCOVER.getValue()) { @@ -484,8 +484,9 @@ public class DhcpManager implements DhcpService { if (flagIfServerIP && flagIfRequestedIP) { // SELECTING state - if (dhcpStore.getIpAssignmentFromAllocationMap(HostId.hostId(clientMAC)) - .fromOpenStack()) { + + if (dhcpStore.getIpAssignmentFromAllocationMap(HostId.hostId(clientMac)) + .rangeNotEnforced()) { outgoingPacketType = DHCPPacketType.DHCPACK; Ethernet ethReply = buildReply(packet, requestedIP, (byte) outgoingPacketType.getValue()); sendReply(context, ethReply); @@ -544,7 +545,7 @@ public class DhcpManager implements DhcpService { * @param context context of the incoming message * @param packet the ethernet payload */ - private void processARPPacket(PacketContext context, Ethernet packet) { + private void processArpPacket(PacketContext context, Ethernet packet) { ARP arpPacket = (ARP) packet.getPayload(); @@ -605,7 +606,7 @@ public class DhcpManager implements DhcpService { // This is meant for the dhcp server so process the packet here. DHCP dhcpPayload = (DHCP) udpPacket.getPayload(); - processDHCPPacket(context, dhcpPayload); + processDhcpPacket(context, dhcpPayload); } } } else if (packet.getEtherType() == Ethernet.TYPE_ARP) { @@ -614,7 +615,7 @@ public class DhcpManager implements DhcpService { if ((arpPacket.getOpCode() == ARP.OP_REQUEST) && Objects.equals(myIP, Ip4Address.valueOf(arpPacket.getTargetProtocolAddress()))) { - processARPPacket(context, packet); + processArpPacket(context, packet); } } @@ -727,4 +728,4 @@ public class DhcpManager implements DhcpService { timeout = Timer.getTimer().newTimeout(new PurgeListTask(), timerDelay, TimeUnit.MINUTES); } } -}
\ No newline at end of file +} diff --git a/framework/src/onos/apps/dhcp/app/src/main/java/org/onosproject/dhcp/impl/DistributedDhcpStore.java b/framework/src/onos/apps/dhcp/app/src/main/java/org/onosproject/dhcp/impl/DistributedDhcpStore.java index 0f25495e..ad4522cb 100644 --- a/framework/src/onos/apps/dhcp/app/src/main/java/org/onosproject/dhcp/impl/DistributedDhcpStore.java +++ b/framework/src/onos/apps/dhcp/app/src/main/java/org/onosproject/dhcp/impl/DistributedDhcpStore.java @@ -106,7 +106,7 @@ public class DistributedDhcpStore implements DhcpStore { IpAssignment.AssignmentStatus status = assignmentInfo.assignmentStatus(); Ip4Address ipAddr = assignmentInfo.ipAddress(); - if (assignmentInfo.fromOpenStack()) { + if (assignmentInfo.rangeNotEnforced()) { return assignmentInfo.ipAddress(); } else if (status == IpAssignment.AssignmentStatus.Option_Assigned || status == IpAssignment.AssignmentStatus.Option_Requested) { @@ -163,12 +163,15 @@ public class DistributedDhcpStore implements DhcpStore { } @Override - public boolean assignIP(HostId hostId, Ip4Address ipAddr, int leaseTime, boolean fromOpenStack, + public boolean assignIP(HostId hostId, Ip4Address ipAddr, int leaseTime, boolean rangeNotEnforced, List<Ip4Address> addressList) { IpAssignment assignmentInfo; + log.debug("Assign IP Called w/ Ip4Address: {}, HostId: {}", ipAddr.toString(), hostId.mac().toString()); + if (allocationMap.containsKey(hostId)) { + assignmentInfo = allocationMap.get(hostId).value(); IpAssignment.AssignmentStatus status = assignmentInfo.assignmentStatus(); @@ -212,17 +215,17 @@ public class DistributedDhcpStore implements DhcpStore { allocationMap.put(hostId, assignmentInfo); return true; } - } else if (fromOpenStack) { + } else if (rangeNotEnforced) { assignmentInfo = IpAssignment.builder() .ipAddress(ipAddr) .timestamp(new Date()) .leasePeriod(leaseTime) - .fromOpenStack(true) - .assignmentStatus(IpAssignment.AssignmentStatus.Option_Requested_From_OpenStack) + .rangeNotEnforced(true) + .assignmentStatus(IpAssignment.AssignmentStatus.Option_RangeNotEnforced) .subnetMask((Ip4Address) addressList.toArray()[0]) .dhcpServer((Ip4Address) addressList.toArray()[1]) - .domainServer((Ip4Address) addressList.toArray()[2]) - .routerAddress((Ip4Address) addressList.toArray()[3]) + .routerAddress((Ip4Address) addressList.toArray()[2]) + .domainServer((Ip4Address) addressList.toArray()[3]) .build(); allocationMap.put(hostId, assignmentInfo); return true; @@ -259,7 +262,7 @@ public class DistributedDhcpStore implements DhcpStore { for (Map.Entry<HostId, Versioned<IpAssignment>> entry: allocationMap.entrySet()) { assignment = entry.getValue().value(); if (assignment.assignmentStatus() == IpAssignment.AssignmentStatus.Option_Assigned - || assignment.assignmentStatus() == IpAssignment.AssignmentStatus.Option_Requested_From_OpenStack) { + || assignment.assignmentStatus() == IpAssignment.AssignmentStatus.Option_RangeNotEnforced) { validMapping.put(entry.getKey(), assignment); } } @@ -276,10 +279,10 @@ public class DistributedDhcpStore implements DhcpStore { } @Override - public boolean assignStaticIP(MacAddress macID, Ip4Address ipAddr, boolean fromOpenStack, + public boolean assignStaticIP(MacAddress macID, Ip4Address ipAddr, boolean rangeNotEnforced, List<Ip4Address> addressList) { HostId host = HostId.hostId(macID); - return assignIP(host, ipAddr, -1, fromOpenStack, addressList); + return assignIP(host, ipAddr, -1, rangeNotEnforced, addressList); } @Override @@ -287,6 +290,12 @@ public class DistributedDhcpStore implements DhcpStore { HostId host = HostId.hostId(macID); if (allocationMap.containsKey(host)) { IpAssignment assignment = allocationMap.get(host).value(); + + if (assignment.rangeNotEnforced()) { + allocationMap.remove(host); + return true; + } + Ip4Address freeIP = assignment.ipAddress(); if (assignment.leasePeriod() < 0) { allocationMap.remove(host); diff --git a/framework/src/onos/apps/dhcp/app/src/main/java/org/onosproject/dhcp/rest/DHCPWebResource.java b/framework/src/onos/apps/dhcp/app/src/main/java/org/onosproject/dhcp/rest/DHCPWebResource.java deleted file mode 100644 index 7a078df9..00000000 --- a/framework/src/onos/apps/dhcp/app/src/main/java/org/onosproject/dhcp/rest/DHCPWebResource.java +++ /dev/null @@ -1,165 +0,0 @@ -/* - * 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 com.google.common.collect.Lists; -import org.onlab.packet.Ip4Address; -import org.onlab.packet.MacAddress; -import org.onosproject.dhcp.DhcpService; -import org.onosproject.dhcp.IpAssignment; -import org.onosproject.net.HostId; -import org.onosproject.rest.AbstractWebResource; - -import javax.ws.rs.Consumes; -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<HostId, IpAssignment> intents = service.listMapping(); - ArrayNode arrayNode = root.putArray("mappings"); - intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode() - .put("host", i.getKey().toString()) - .put("ip", i.getValue().ipAddress().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. - * - * @param stream JSON stream - * @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()), false, Lists.newArrayList())) { - throw new IllegalArgumentException("Static Mapping Failed. The IP maybe unavailable."); - } - } - - final Map<HostId, IpAssignment> intents = service.listMapping(); - ArrayNode arrayNode = root.putArray("mappings"); - intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode() - .put("host", i.getKey().toString()) - .put("ip", i.getValue().ipAddress().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. - * - * @param macID mac address identifier - * @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<HostId, IpAssignment> intents = service.listMapping(); - ArrayNode arrayNode = root.putArray("mappings"); - intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode() - .put("host", i.getKey().toString()) - .put("ip", i.getValue().ipAddress().toString()))); - - return ok(root.toString()).build(); - } -} diff --git a/framework/src/onos/apps/dhcp/app/src/main/webapp/WEB-INF/web.xml b/framework/src/onos/apps/dhcp/app/src/main/webapp/WEB-INF/web.xml index 27504548..a53110ee 100644 --- a/framework/src/onos/apps/dhcp/app/src/main/webapp/WEB-INF/web.xml +++ b/framework/src/onos/apps/dhcp/app/src/main/webapp/WEB-INF/web.xml @@ -30,7 +30,7 @@ <init-param> <param-name>com.sun.jersey.config.property.classnames</param-name> <param-value> - org.onosproject.dhcp.rest.DHCPWebResource + org.onosproject.dhcp.rest.DhcpWebResource </param-value> </init-param> <load-on-startup>1</load-on-startup> diff --git a/framework/src/onos/apps/dhcp/app/src/test/java/org/onosproject/dhcp/impl/DhcpManagerTest.java b/framework/src/onos/apps/dhcp/app/src/test/java/org/onosproject/dhcp/impl/DhcpManagerTest.java index bb6b74cc..e9b02608 100644 --- a/framework/src/onos/apps/dhcp/app/src/test/java/org/onosproject/dhcp/impl/DhcpManagerTest.java +++ b/framework/src/onos/apps/dhcp/app/src/test/java/org/onosproject/dhcp/impl/DhcpManagerTest.java @@ -106,7 +106,7 @@ public class DhcpManagerTest { */ @Test public void testDiscover() { - Ethernet reply = constructDHCPPacket(DHCPPacketType.DHCPDISCOVER); + Ethernet reply = constructDhcpPacket(DHCPPacketType.DHCPDISCOVER); sendPacket(reply); } @@ -115,7 +115,7 @@ public class DhcpManagerTest { */ @Test public void testRequest() { - Ethernet reply = constructDHCPPacket(DHCPPacketType.DHCPREQUEST); + Ethernet reply = constructDhcpPacket(DHCPPacketType.DHCPREQUEST); sendPacket(reply); } @@ -138,7 +138,7 @@ public class DhcpManagerTest { * @param packetType DHCP Message Type * @return Ethernet packet */ - private Ethernet constructDHCPPacket(DHCPPacketType packetType) { + private Ethernet constructDhcpPacket(DHCPPacketType packetType) { // Ethernet Frame. Ethernet ethReply = new Ethernet(); |