diff options
Diffstat (limited to 'framework/src/onos/apps/dhcp')
11 files changed, 260 insertions, 222 deletions
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 index 7c2127f9..ae610239 100644 --- 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 @@ -19,8 +19,10 @@ import org.onlab.packet.Ip4Address; import org.onlab.packet.MacAddress; import org.onosproject.net.HostId; +import java.util.List; import java.util.Map; + /** * DHCP Service Interface. */ @@ -56,12 +58,16 @@ public interface DhcpService { /** * Registers a static IP mapping with the DHCP Server. + * Supports rangeNotEnforced option * - * @param macID macID of the client + * @param macID macID of the client * @param ipAddress IP Address requested for the client - * @return true if the mapping was successfully registered, false otherwise + * @param rangeNotEnforced true if rangeNotEnforced was set and the mapping will be eternal + * @param addressList subnetMask, DHCP/Router/DNS IP Addresses if rangeNotEnforced was set + * @return true if the mapping was successfully added, false otherwise */ - boolean setStaticMapping(MacAddress macID, Ip4Address ipAddress); + boolean setStaticMapping(MacAddress macID, Ip4Address ipAddress, boolean rangeNotEnforced, + List<Ip4Address> addressList); /** * Removes a static IP mapping with the DHCP Server. @@ -77,5 +83,4 @@ public interface DhcpService { * @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 index e263b3a2..cdfadf7b 100644 --- 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 @@ -19,8 +19,10 @@ import org.onlab.packet.Ip4Address; import org.onlab.packet.MacAddress; import org.onosproject.net.HostId; +import java.util.List; import java.util.Map; + /** * DHCPStore Interface. */ @@ -43,15 +45,20 @@ public interface DhcpStore { */ 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 + * @param rangeNotEnforced true if rangeNotEnforced was set + * @param addressList subnetMask, DHCP/Router/DNS IP Addresses if rangeNotEnforced was set * @return returns true if the assignment was successful, false otherwise */ - boolean assignIP(HostId hostId, Ip4Address ipAddr, int leaseTime); + boolean assignIP(HostId hostId, Ip4Address ipAddr, int leaseTime, boolean rangeNotEnforced, + List<Ip4Address> addressList); + /** * Sets the default time for which suggested IP mappings are valid. @@ -87,9 +94,11 @@ public interface DhcpStore { * * @param macID macID of the client * @param ipAddr IP Address requested for the client + * @param rangeNotEnforced true if rangeNotEnforced was set + * @param addressList subnetMask, DHCP/Router/DNS IP Addresses rangeNotEnforced was set * @return true if the mapping was successfully registered, false otherwise */ - boolean assignStaticIP(MacAddress macID, Ip4Address ipAddr); + boolean assignStaticIP(MacAddress macID, Ip4Address ipAddr, boolean rangeNotEnforced, List<Ip4Address> addressList); /** * Removes a static IP mapping associated with the given MAC ID from the DHCP Server. @@ -106,4 +115,11 @@ public interface DhcpStore { */ Iterable<Ip4Address> getAvailableIPs(); + /** + * + * + * @param hostId + * @return + */ + IpAssignment getIpAssignmentFromAllocationMap(HostId hostId); } 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 index 9b3aa686..5610fec8 100644 --- 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 @@ -33,6 +33,16 @@ public final class IpAssignment { private final long leasePeriod; + private final Ip4Address subnetMask; + + private final Ip4Address dhcpServer; + + private final Ip4Address routerAddress; + + private final Ip4Address domainServer; + + private final boolean rangeNotEnforced; + private final AssignmentStatus assignmentStatus; public enum AssignmentStatus { @@ -42,6 +52,10 @@ public final class IpAssignment { Option_Requested, /** + * IP Assignment has been requested by a OpenStack. + */ + Option_RangeNotEnforced, + /** * IP has been assigned to a host. */ Option_Assigned, @@ -58,16 +72,28 @@ public final class IpAssignment { * * @param ipAddress * @param leasePeriod + * @param timestamp * @param assignmentStatus + * @param subnetMask + * @param dhcpServer + * @param routerAddress + * @param domainServer + * @param rangeNotEnforced */ private IpAssignment(Ip4Address ipAddress, long leasePeriod, Date timestamp, - AssignmentStatus assignmentStatus) { + AssignmentStatus assignmentStatus, Ip4Address subnetMask, Ip4Address dhcpServer, + Ip4Address routerAddress, Ip4Address domainServer, boolean rangeNotEnforced) { this.ipAddress = ipAddress; this.leasePeriod = leasePeriod; this.timestamp = timestamp; this.assignmentStatus = assignmentStatus; + this.subnetMask = subnetMask; + this.dhcpServer = dhcpServer; + this.routerAddress = routerAddress; + this.domainServer = domainServer; + this.rangeNotEnforced = rangeNotEnforced; } /** @@ -115,6 +141,26 @@ public final class IpAssignment { return (int) this.leasePeriod * 1000; } + public Ip4Address subnetMask() { + return subnetMask; + } + + public Ip4Address dhcpServer() { + return dhcpServer; + } + + public Ip4Address routerAddress() { + return routerAddress; + } + + public Ip4Address domainServer() { + return domainServer; + } + + public boolean rangeNotEnforced() { + return rangeNotEnforced; + } + @Override public String toString() { return MoreObjects.toStringHelper(getClass()) @@ -122,6 +168,11 @@ public final class IpAssignment { .add("timestamp", timestamp) .add("lease", leasePeriod) .add("assignmentStatus", assignmentStatus) + .add("subnetMask", subnetMask) + .add("dhcpServer", dhcpServer) + .add("routerAddress", routerAddress) + .add("domainServer", domainServer) + .add("rangeNotEnforced", rangeNotEnforced) .toString(); } @@ -157,6 +208,16 @@ public final class IpAssignment { private AssignmentStatus assignmentStatus; + private Ip4Address subnetMask; + + private Ip4Address dhcpServer; + + private Ip4Address domainServer; + + private Ip4Address routerAddress; + + private boolean rangeNotEnforced = false; + private Builder() { } @@ -170,10 +231,8 @@ public final class IpAssignment { public IpAssignment build() { validateInputs(); - return new IpAssignment(ipAddress, - leasePeriod, - timeStamp, - assignmentStatus); + return new IpAssignment(ipAddress, leasePeriod, timeStamp, assignmentStatus, subnetMask, + dhcpServer, domainServer, routerAddress, rangeNotEnforced); } public Builder ipAddress(Ip4Address addr) { @@ -196,14 +255,48 @@ public final class IpAssignment { return this; } + public Builder subnetMask(Ip4Address subnetMask) { + this.subnetMask = subnetMask; + return this; + } + + public Builder dhcpServer(Ip4Address dhcpServer) { + this.dhcpServer = dhcpServer; + return this; + } + + public Builder domainServer(Ip4Address domainServer) { + this.domainServer = domainServer; + return this; + } + + public Builder routerAddress(Ip4Address routerAddress) { + this.routerAddress = routerAddress; + return this; + } + + public Builder rangeNotEnforced(boolean rangeNotEnforced) { + this.rangeNotEnforced = rangeNotEnforced; + 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"); + if (rangeNotEnforced) { + checkNotNull(subnetMask, "subnetMask must be specified in case of rangeNotEnforced"); + checkNotNull(dhcpServer, "dhcpServer must be specified in case of rangeNotEnforced"); + checkNotNull(domainServer, "domainServer must be specified in case of rangeNotEnforced"); + checkNotNull(routerAddress, "routerAddress must be specified in case of rangeNotEnforced"); + } + switch (assignmentStatus) { case Option_Requested: + case Option_RangeNotEnforced: case Option_Assigned: case Option_Expired: break; diff --git a/framework/src/onos/apps/dhcp/app/src/main/java/org/onosproject/dhcp/cli/DhcpSetStaticMapping.java b/framework/src/onos/apps/dhcp/app/src/main/java/org/onosproject/dhcp/cli/DhcpSetStaticMapping.java index 9f4f6580..e1ce8904 100644 --- a/framework/src/onos/apps/dhcp/app/src/main/java/org/onosproject/dhcp/cli/DhcpSetStaticMapping.java +++ b/framework/src/onos/apps/dhcp/app/src/main/java/org/onosproject/dhcp/cli/DhcpSetStaticMapping.java @@ -15,6 +15,7 @@ */ package org.onosproject.dhcp.cli; +import com.google.common.collect.Lists; import org.apache.karaf.shell.commands.Argument; import org.apache.karaf.shell.commands.Command; import org.onlab.packet.Ip4Address; @@ -48,7 +49,7 @@ public class DhcpSetStaticMapping extends AbstractShellCommand { try { MacAddress macID = MacAddress.valueOf(macAddr); Ip4Address ipAddress = Ip4Address.valueOf(ipAddr); - if (dhcpService.setStaticMapping(macID, ipAddress)) { + if (dhcpService.setStaticMapping(macID, ipAddress, false, Lists.newArrayList())) { print(DHCP_SUCCESS); } else { print(DHCP_FAILURE); 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 96d94a2b..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 @@ -16,6 +16,7 @@ package org.onosproject.dhcp.impl; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Lists; import org.apache.felix.scr.annotations.Activate; import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.Deactivate; @@ -77,7 +78,6 @@ import java.util.Map; import java.util.Objects; import java.util.Set; import java.util.concurrent.TimeUnit; - import static org.onlab.packet.MacAddress.valueOf; import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; @@ -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; @@ -168,7 +168,6 @@ public class DhcpManager implements DhcpService { cfgService.addListener(cfgListener); factories.forEach(cfgService::registerConfigFactory); cfgListener.reconfigureNetwork(cfgService.getConfig(appId, DhcpConfig.class)); - hostProviderService = hostProviderRegistry.register(hostProvider); packetService.addProcessor(processor, PacketProcessor.director(0)); requestPackets(); @@ -242,8 +241,12 @@ public class DhcpManager implements DhcpService { } @Override - public boolean setStaticMapping(MacAddress macID, Ip4Address ipAddress) { - return dhcpStore.assignStaticIP(macID, ipAddress); + 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, rangeNotEnforced, addressList); } @Override @@ -256,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. @@ -268,6 +271,26 @@ public class DhcpManager implements DhcpService { */ private Ethernet buildReply(Ethernet packet, Ip4Address ipOffered, byte outgoingMessageType) { + Ip4Address subnetMaskReply; + Ip4Address dhcpServerReply; + Ip4Address routerAddressReply; + Ip4Address domainServerReply; + IpAssignment ipAssignment; + + ipAssignment = dhcpStore.getIpAssignmentFromAllocationMap(HostId.hostId(packet.getSourceMAC())); + + if (ipAssignment != null && ipAssignment.rangeNotEnforced()) { + subnetMaskReply = ipAssignment.subnetMask(); + dhcpServerReply = ipAssignment.dhcpServer(); + domainServerReply = ipAssignment.domainServer(); + routerAddressReply = ipAssignment.routerAddress(); + } else { + subnetMaskReply = subnetMask; + dhcpServerReply = myIP; + routerAddressReply = routerAddress; + domainServerReply = domainServer; + } + // Ethernet Frame. Ethernet ethReply = new Ethernet(); ethReply.setSourceMACAddress(myMAC); @@ -278,7 +301,7 @@ public class DhcpManager implements DhcpService { // IP Packet IPv4 ipv4Packet = (IPv4) packet.getPayload(); IPv4 ipv4Reply = new IPv4(); - ipv4Reply.setSourceAddress(myIP.toInt()); + ipv4Reply.setSourceAddress(dhcpServerReply.toInt()); ipv4Reply.setDestinationAddress(ipOffered.toInt()); ipv4Reply.setTtl(packetTTL); @@ -299,7 +322,7 @@ public class DhcpManager implements DhcpService { if (outgoingMessageType != DHCPPacketType.DHCPNAK.getValue()) { dhcpReply.setYourIPAddress(ipOffered.toInt()); - dhcpReply.setServerIPAddress(myIP.toInt()); + dhcpReply.setServerIPAddress(dhcpServerReply.toInt()); if (dhcpPacket.getGatewayIPAddress() == 0) { ipv4Reply.setDestinationAddress(IP_BROADCAST.toInt()); } @@ -322,7 +345,7 @@ public class DhcpManager implements DhcpService { option = new DHCPOption(); option.setCode(DHCP.DHCPOptionCode.OptionCode_DHCPServerIp.getValue()); option.setLength((byte) 4); - option.setData(myIP.toOctets()); + option.setData(dhcpServerReply.toOctets()); optionList.add(option); if (outgoingMessageType != DHCPPacketType.DHCPNAK.getValue()) { @@ -352,7 +375,7 @@ public class DhcpManager implements DhcpService { option = new DHCPOption(); option.setCode(DHCP.DHCPOptionCode.OptionCode_SubnetMask.getValue()); option.setLength((byte) 4); - option.setData(subnetMask.toOctets()); + option.setData(subnetMaskReply.toOctets()); optionList.add(option); // Broadcast Address. @@ -366,14 +389,14 @@ public class DhcpManager implements DhcpService { option = new DHCPOption(); option.setCode(DHCP.DHCPOptionCode.OptionCode_RouterAddress.getValue()); option.setLength((byte) 4); - option.setData(routerAddress.toOctets()); + option.setData(routerAddressReply.toOctets()); optionList.add(option); // DNS Server Address. option = new DHCPOption(); option.setCode(DHCP.DHCPOptionCode.OptionCode_DomainServer.getValue()); option.setLength((byte) 4); - option.setData(domainServer.toOctets()); + option.setData(domainServerReply.toOctets()); optionList.add(option); } @@ -384,7 +407,6 @@ public class DhcpManager implements DhcpService { optionList.add(option); dhcpReply.setOptions(optionList); - udpReply.setPayload(dhcpReply); ipv4Reply.setPayload(udpReply); ethReply.setPayload(ipv4Reply); @@ -415,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; @@ -442,38 +464,48 @@ 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()) { outgoingPacketType = DHCPPacketType.DHCPOFFER; - Ip4Address ipOffered = dhcpStore.suggestIP(hostId, requestedIP); + Ip4Address ipOffered = null; + ipOffered = dhcpStore.suggestIP(hostId, requestedIP); + if (ipOffered != null) { Ethernet ethReply = buildReply(packet, ipOffered, (byte) outgoingPacketType.getValue()); sendReply(context, ethReply); } - } else if (incomingPacketType.getValue() == DHCPPacketType.DHCPREQUEST.getValue()) { if (flagIfServerIP && flagIfRequestedIP) { // SELECTING state - if (myIP.equals(serverIP)) { - if (dhcpStore.assignIP(hostId, requestedIP, leaseTime)) { - outgoingPacketType = DHCPPacketType.DHCPACK; - discoverHost(context, requestedIP); - } else { - outgoingPacketType = DHCPPacketType.DHCPNAK; - } + + if (dhcpStore.getIpAssignmentFromAllocationMap(HostId.hostId(clientMac)) + .rangeNotEnforced()) { + outgoingPacketType = DHCPPacketType.DHCPACK; Ethernet ethReply = buildReply(packet, requestedIP, (byte) outgoingPacketType.getValue()); sendReply(context, ethReply); + } else { + if (myIP.equals(serverIP)) { + if (dhcpStore.assignIP(hostId, requestedIP, leaseTime, false, Lists.newArrayList())) { + outgoingPacketType = DHCPPacketType.DHCPACK; + discoverHost(context, requestedIP); + } else { + outgoingPacketType = DHCPPacketType.DHCPNAK; + } + Ethernet ethReply = buildReply(packet, requestedIP, + (byte) outgoingPacketType.getValue()); + sendReply(context, ethReply); + } } } else if (flagIfRequestedIP) { // INIT-REBOOT state - if (dhcpStore.assignIP(hostId, requestedIP, leaseTime)) { + if (dhcpStore.assignIP(hostId, requestedIP, leaseTime, false, Lists.newArrayList())) { outgoingPacketType = DHCPPacketType.DHCPACK; Ethernet ethReply = buildReply(packet, requestedIP, (byte) outgoingPacketType.getValue()); sendReply(context, ethReply); @@ -485,7 +517,7 @@ public class DhcpManager implements DhcpService { int ciaadr = dhcpPayload.getClientIPAddress(); if (ciaadr != 0) { Ip4Address clientIaddr = Ip4Address.valueOf(ciaadr); - if (dhcpStore.assignIP(hostId, clientIaddr, leaseTime)) { + if (dhcpStore.assignIP(hostId, clientIaddr, leaseTime, false, Lists.newArrayList())) { outgoingPacketType = DHCPPacketType.DHCPACK; discoverHost(context, clientIaddr); } else if (packet.getEtherType() == Ethernet.TYPE_IPV4 && @@ -513,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(); @@ -574,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) { @@ -583,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); } } @@ -696,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 63f69d40..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 @@ -38,8 +38,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Date; -import java.util.HashMap; import java.util.Map; +import java.util.List; +import java.util.HashMap; import java.util.Objects; /** @@ -105,7 +106,9 @@ public class DistributedDhcpStore implements DhcpStore { IpAssignment.AssignmentStatus status = assignmentInfo.assignmentStatus(); Ip4Address ipAddr = assignmentInfo.ipAddress(); - if (status == IpAssignment.AssignmentStatus.Option_Assigned || + if (assignmentInfo.rangeNotEnforced()) { + return assignmentInfo.ipAddress(); + } else if (status == IpAssignment.AssignmentStatus.Option_Assigned || status == IpAssignment.AssignmentStatus.Option_Requested) { // Client has a currently Active Binding. if (ipWithinRange(ipAddr)) { @@ -160,10 +163,15 @@ public class DistributedDhcpStore implements DhcpStore { } @Override - public boolean assignIP(HostId hostId, Ip4Address ipAddr, int leaseTime) { + 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(); @@ -207,6 +215,20 @@ public class DistributedDhcpStore implements DhcpStore { allocationMap.put(hostId, assignmentInfo); return true; } + } else if (rangeNotEnforced) { + assignmentInfo = IpAssignment.builder() + .ipAddress(ipAddr) + .timestamp(new Date()) + .leasePeriod(leaseTime) + .rangeNotEnforced(true) + .assignmentStatus(IpAssignment.AssignmentStatus.Option_RangeNotEnforced) + .subnetMask((Ip4Address) addressList.toArray()[0]) + .dhcpServer((Ip4Address) addressList.toArray()[1]) + .routerAddress((Ip4Address) addressList.toArray()[2]) + .domainServer((Ip4Address) addressList.toArray()[3]) + .build(); + allocationMap.put(hostId, assignmentInfo); + return true; } return false; } @@ -239,7 +261,8 @@ public class DistributedDhcpStore implements DhcpStore { IpAssignment assignment; for (Map.Entry<HostId, Versioned<IpAssignment>> entry: allocationMap.entrySet()) { assignment = entry.getValue().value(); - if (assignment.assignmentStatus() == IpAssignment.AssignmentStatus.Option_Assigned) { + if (assignment.assignmentStatus() == IpAssignment.AssignmentStatus.Option_Assigned + || assignment.assignmentStatus() == IpAssignment.AssignmentStatus.Option_RangeNotEnforced) { validMapping.put(entry.getKey(), assignment); } } @@ -256,9 +279,10 @@ public class DistributedDhcpStore implements DhcpStore { } @Override - public boolean assignStaticIP(MacAddress macID, Ip4Address ipAddr) { + public boolean assignStaticIP(MacAddress macID, Ip4Address ipAddr, boolean rangeNotEnforced, + List<Ip4Address> addressList) { HostId host = HostId.hostId(macID); - return assignIP(host, ipAddr, -1); + return assignIP(host, ipAddr, -1, rangeNotEnforced, addressList); } @Override @@ -266,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); @@ -299,6 +329,11 @@ public class DistributedDhcpStore implements DhcpStore { } } + @Override + public IpAssignment getIpAssignmentFromAllocationMap(HostId hostId) { + return allocationMap.get(hostId).value(); + } + /** * Fetches the next available IP from the free pool pf IPs. * @@ -326,3 +361,4 @@ public class DistributedDhcpStore implements DhcpStore { return false; } } + 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 646ab7ea..00000000 --- a/framework/src/onos/apps/dhcp/app/src/main/java/org/onosproject/dhcp/rest/DHCPWebResource.java +++ /dev/null @@ -1,164 +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 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()))) { - 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 fd4701c6..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(); @@ -228,7 +228,8 @@ public class DhcpManagerTest { return Ip4Address.valueOf(EXPECTED_IP); } - public boolean assignIP(HostId hostId, Ip4Address ipAddr, int leaseTime) { + public boolean assignIP(HostId hostId, Ip4Address ipAddr, int leaseTime, boolean fromOpenStack, + List<Ip4Address> addressList) { return true; } @@ -255,7 +256,8 @@ public class DhcpManagerTest { return map; } - public boolean assignStaticIP(MacAddress macID, Ip4Address ipAddr) { + public boolean assignStaticIP(MacAddress macID, Ip4Address ipAddr, boolean fromOpenStack, + List<Ip4Address> addressList) { return true; } @@ -268,6 +270,9 @@ public class DhcpManagerTest { ipList.add(Ip4Address.valueOf(EXPECTED_IP)); return ImmutableSet.copyOf(ipList); } + public IpAssignment getIpAssignmentFromAllocationMap(HostId hostId) { + return null; + } } /** diff --git a/framework/src/onos/apps/dhcp/pom.xml b/framework/src/onos/apps/dhcp/pom.xml index 7a10776e..473caea6 100644 --- a/framework/src/onos/apps/dhcp/pom.xml +++ b/framework/src/onos/apps/dhcp/pom.xml @@ -36,7 +36,4 @@ <module>app</module> </modules> - <dependencies> - </dependencies> - </project> |