DHCP util refactor
Move options to packet.dhcp package
Deprecated DHCPPacketType, add MsgType to DHCP class
Change-Id: I85ce7fa5e6f3fdc916fbbeba9a4e10e75064a054
diff --git a/apps/dhcp/app/src/main/java/org/onosproject/dhcp/impl/DhcpManager.java b/apps/dhcp/app/src/main/java/org/onosproject/dhcp/impl/DhcpManager.java
index dd21838..7190aea 100644
--- a/apps/dhcp/app/src/main/java/org/onosproject/dhcp/impl/DhcpManager.java
+++ b/apps/dhcp/app/src/main/java/org/onosproject/dhcp/impl/DhcpManager.java
@@ -28,8 +28,7 @@
import org.apache.felix.scr.annotations.Service;
import org.onlab.packet.ARP;
import org.onlab.packet.DHCP;
-import org.onlab.packet.DHCPOption;
-import org.onlab.packet.DHCPPacketType;
+import org.onlab.packet.dhcp.DhcpOption;
import org.onlab.packet.Ethernet;
import org.onlab.packet.IPv4;
import org.onlab.packet.Ip4Address;
@@ -89,9 +88,6 @@
import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_DHCPServerIp;
import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_MessageType;
import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_RequestedIP;
-import static org.onlab.packet.DHCPPacketType.DHCPACK;
-import static org.onlab.packet.DHCPPacketType.DHCPNAK;
-import static org.onlab.packet.DHCPPacketType.DHCPOFFER;
import static org.onlab.packet.MacAddress.valueOf;
import static org.onosproject.dhcp.IpAssignment.AssignmentStatus.Option_RangeNotEnforced;
import static org.onosproject.dhcp.IpAssignment.AssignmentStatus.Option_Requested;
@@ -344,7 +340,7 @@
dhcpReply.setClientHardwareAddress(dhcpPacket.getClientHardwareAddress());
dhcpReply.setTransactionId(dhcpPacket.getTransactionId());
- if (outgoingMessageType != DHCPPacketType.DHCPNAK.getValue()) {
+ if (outgoingMessageType != DHCP.MsgType.DHCPNAK.getValue()) {
dhcpReply.setYourIPAddress(ipOffered.toInt());
dhcpReply.setServerIPAddress(dhcpServerReply.toInt());
if (dhcpPacket.getGatewayIPAddress() == 0) {
@@ -355,8 +351,8 @@
dhcpReply.setHardwareAddressLength((byte) 6);
// DHCP Options.
- DHCPOption option = new DHCPOption();
- List<DHCPOption> optionList = new ArrayList<>();
+ DhcpOption option = new DhcpOption();
+ List<DhcpOption> optionList = new ArrayList<>();
// DHCP Message Type.
option.setCode(OptionCode_MessageType.getValue());
@@ -366,15 +362,15 @@
optionList.add(option);
// DHCP Server Identifier.
- option = new DHCPOption();
+ option = new DhcpOption();
option.setCode(OptionCode_DHCPServerIp.getValue());
option.setLength((byte) 4);
option.setData(dhcpServerReply.toOctets());
optionList.add(option);
- if (outgoingMessageType != DHCPPacketType.DHCPNAK.getValue()) {
+ if (outgoingMessageType != DHCP.MsgType.DHCPNAK.getValue()) {
// IP Address Lease Time.
- option = new DHCPOption();
+ option = new DhcpOption();
option.setCode(DHCP.DHCPOptionCode.OptionCode_LeaseTime.getValue());
option.setLength((byte) 4);
option.setData(ByteBuffer.allocate(4)
@@ -382,28 +378,28 @@
optionList.add(option);
// IP Address Renewal Time.
- option = new DHCPOption();
+ option = new DhcpOption();
option.setCode(DHCP.DHCPOptionCode.OptionCode_RenewalTime.getValue());
option.setLength((byte) 4);
option.setData(ByteBuffer.allocate(4).putInt(renewalTime).array());
optionList.add(option);
// IP Address Rebinding Time.
- option = new DHCPOption();
+ option = new DhcpOption();
option.setCode(DHCP.DHCPOptionCode.OPtionCode_RebindingTime.getValue());
option.setLength((byte) 4);
option.setData(ByteBuffer.allocate(4).putInt(rebindingTime).array());
optionList.add(option);
// Subnet Mask.
- option = new DHCPOption();
+ option = new DhcpOption();
option.setCode(DHCP.DHCPOptionCode.OptionCode_SubnetMask.getValue());
option.setLength((byte) 4);
option.setData(subnetMaskReply.toOctets());
optionList.add(option);
// Broadcast Address.
- option = new DHCPOption();
+ option = new DhcpOption();
option.setCode(DHCP.DHCPOptionCode.OptionCode_BroadcastAddress.getValue());
option.setLength((byte) 4);
option.setData(broadcastReply.toOctets());
@@ -411,7 +407,7 @@
// Router Address.
if (routerAddressReply.isPresent()) {
- option = new DHCPOption();
+ option = new DhcpOption();
option.setCode(DHCP.DHCPOptionCode.OptionCode_RouterAddress.getValue());
option.setLength((byte) 4);
option.setData(routerAddressReply.get().toOctets());
@@ -420,7 +416,7 @@
// DNS Server Address.
if (domainServerReply.isPresent()) {
- option = new DHCPOption();
+ option = new DhcpOption();
option.setCode(DHCP.DHCPOptionCode.OptionCode_DomainServer.getValue());
option.setLength((byte) 4);
option.setData(domainServerReply.get().toOctets());
@@ -429,7 +425,7 @@
}
// End Option.
- option = new DHCPOption();
+ option = new DhcpOption();
option.setCode(DHCP.DHCPOptionCode.OptionCode_END.getValue());
option.setLength((byte) 1);
optionList.add(option);
@@ -472,16 +468,16 @@
}
Ethernet packet = context.inPacket().parsed();
- DHCPPacketType incomingPacketType = null;
+ DHCP.MsgType incomingPacketType = null;
boolean flagIfRequestedIP = false;
boolean flagIfServerIP = false;
Ip4Address requestedIP = Ip4Address.valueOf("0.0.0.0");
Ip4Address serverIP = Ip4Address.valueOf("0.0.0.0");
- for (DHCPOption option : dhcpPayload.getOptions()) {
+ for (DhcpOption option : dhcpPayload.getOptions()) {
if (option.getCode() == OptionCode_MessageType.getValue()) {
byte[] data = option.getData();
- incomingPacketType = DHCPPacketType.getType(data[0]);
+ incomingPacketType = DHCP.MsgType.getType(data[0]);
}
if (option.getCode() == OptionCode_RequestedIP.getValue()) {
byte[] data = option.getData();
@@ -500,7 +496,7 @@
return;
}
- DHCPPacketType outgoingPacketType;
+ DHCP.MsgType outgoingPacketType;
MacAddress clientMac = new MacAddress(dhcpPayload.getClientHardwareAddress());
VlanId vlanId = VlanId.vlanId(packet.getVlanID());
HostId hostId = HostId.hostId(clientMac, vlanId);
@@ -513,7 +509,7 @@
Ethernet ethReply = buildReply(
packet,
ipOffered,
- (byte) DHCPOFFER.getValue());
+ (byte) DHCP.MsgType.DHCPOFFER.getValue());
sendReply(context, ethReply);
}
break;
@@ -536,10 +532,10 @@
.assignmentStatus(Option_Requested).build();
if (dhcpStore.assignIP(hostId, ipAssignment)) {
- outgoingPacketType = DHCPACK;
+ outgoingPacketType = DHCP.MsgType.DHCPACK;
discoverHost(context, requestedIP);
} else {
- outgoingPacketType = DHCPNAK;
+ outgoingPacketType = DHCP.MsgType.DHCPNAK;
}
Ethernet ethReply = buildReply(packet, requestedIP, (byte) outgoingPacketType.getValue());
diff --git a/apps/dhcp/app/src/test/java/org/onosproject/dhcp/impl/DhcpManagerTest.java b/apps/dhcp/app/src/test/java/org/onosproject/dhcp/impl/DhcpManagerTest.java
index 934e601..104d931 100644
--- a/apps/dhcp/app/src/test/java/org/onosproject/dhcp/impl/DhcpManagerTest.java
+++ b/apps/dhcp/app/src/test/java/org/onosproject/dhcp/impl/DhcpManagerTest.java
@@ -20,8 +20,7 @@
import org.junit.Before;
import org.junit.Test;
import org.onlab.packet.DHCP;
-import org.onlab.packet.DHCPOption;
-import org.onlab.packet.DHCPPacketType;
+import org.onlab.packet.dhcp.DhcpOption;
import org.onlab.packet.Ethernet;
import org.onlab.packet.IPv4;
import org.onlab.packet.Ip4Address;
@@ -109,7 +108,7 @@
*/
@Test
public void testDiscover() {
- Ethernet reply = constructDhcpPacket(DHCPPacketType.DHCPDISCOVER);
+ Ethernet reply = constructDhcpPacket(DHCP.MsgType.DHCPDISCOVER);
sendPacket(reply);
}
@@ -118,7 +117,7 @@
*/
@Test
public void testRequest() {
- Ethernet reply = constructDhcpPacket(DHCPPacketType.DHCPREQUEST);
+ Ethernet reply = constructDhcpPacket(DHCP.MsgType.DHCPREQUEST);
sendPacket(reply);
}
@@ -141,7 +140,7 @@
* @param packetType DHCP Message Type
* @return Ethernet packet
*/
- private Ethernet constructDhcpPacket(DHCPPacketType packetType) {
+ private Ethernet constructDhcpPacket(DHCP.MsgType packetType) {
// Ethernet Frame.
Ethernet ethReply = new Ethernet();
@@ -174,8 +173,8 @@
dhcpReply.setHardwareAddressLength((byte) 6);
// DHCP Options.
- DHCPOption option = new DHCPOption();
- List<DHCPOption> optionList = new ArrayList<>();
+ DhcpOption option = new DhcpOption();
+ List<DhcpOption> optionList = new ArrayList<>();
// DHCP Message Type.
option.setCode(DHCP.DHCPOptionCode.OptionCode_MessageType.getValue());
@@ -185,7 +184,7 @@
optionList.add(option);
// DHCP Requested IP.
- option = new DHCPOption();
+ option = new DhcpOption();
option.setCode(DHCP.DHCPOptionCode.OptionCode_RequestedIP.getValue());
option.setLength((byte) 4);
optionData = Ip4Address.valueOf(EXPECTED_IP).toOctets();
@@ -193,7 +192,7 @@
optionList.add(option);
// End Option.
- option = new DHCPOption();
+ option = new DhcpOption();
option.setCode(DHCP.DHCPOptionCode.OptionCode_END.getValue());
option.setLength((byte) 1);
optionList.add(option);
diff --git a/apps/dhcprelay/src/main/java/org/onosproject/dhcprelay/DhcpRelay.java b/apps/dhcprelay/src/main/java/org/onosproject/dhcprelay/DhcpRelay.java
index 40003eb..778f778 100644
--- a/apps/dhcprelay/src/main/java/org/onosproject/dhcprelay/DhcpRelay.java
+++ b/apps/dhcprelay/src/main/java/org/onosproject/dhcprelay/DhcpRelay.java
@@ -28,8 +28,7 @@
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.onlab.packet.ARP;
import org.onlab.packet.DHCP;
-import org.onlab.packet.DHCPOption;
-import org.onlab.packet.DHCPPacketType;
+import org.onlab.packet.dhcp.DhcpOption;
import org.onlab.packet.Ethernet;
import org.onlab.packet.IPv4;
import org.onlab.packet.Ip4Address;
@@ -421,11 +420,11 @@
}
Ethernet packet = context.inPacket().parsed();
- DHCPPacketType incomingPacketType = null;
- for (DHCPOption option : dhcpPayload.getOptions()) {
+ DHCP.MsgType incomingPacketType = null;
+ for (DhcpOption option : dhcpPayload.getOptions()) {
if (option.getCode() == OptionCode_MessageType.getValue()) {
byte[] data = option.getData();
- incomingPacketType = DHCPPacketType.getType(data[0]);
+ incomingPacketType = DHCP.MsgType.getType(data[0]);
}
}
diff --git a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackSwitchingDhcpHandler.java b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackSwitchingDhcpHandler.java
index d87326d8..8271644 100644
--- a/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackSwitchingDhcpHandler.java
+++ b/apps/openstacknetworking/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackSwitchingDhcpHandler.java
@@ -25,8 +25,7 @@
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.onlab.packet.DHCP;
-import org.onlab.packet.DHCPOption;
-import org.onlab.packet.DHCPPacketType;
+import org.onlab.packet.dhcp.DhcpOption;
import org.onlab.packet.Ethernet;
import org.onlab.packet.IPv4;
import org.onlab.packet.Ip4Address;
@@ -63,8 +62,8 @@
import java.util.List;
import static org.onlab.packet.DHCP.DHCPOptionCode.*;
-import static org.onlab.packet.DHCPPacketType.DHCPACK;
-import static org.onlab.packet.DHCPPacketType.DHCPOFFER;
+import static org.onlab.packet.DHCP.MsgType.DHCPACK;
+import static org.onlab.packet.DHCP.MsgType.DHCPOFFER;
import static org.onosproject.openstacknetworking.api.Constants.DEFAULT_GATEWAY_MAC_STR;
import static org.slf4j.LoggerFactory.getLogger;
@@ -192,7 +191,7 @@
return;
}
- DHCPPacketType inPacketType = getPacketType(dhcpPacket);
+ DHCP.MsgType inPacketType = getPacketType(dhcpPacket);
if (inPacketType == null || dhcpPacket.getClientHardwareAddress() == null) {
log.trace("Malformed DHCP packet received, ignore it");
return;
@@ -235,14 +234,14 @@
}
}
- private DHCPPacketType getPacketType(DHCP dhcpPacket) {
- DHCPOption optType = dhcpPacket.getOption(OptionCode_MessageType);
+ private DHCP.MsgType getPacketType(DHCP dhcpPacket) {
+ DhcpOption optType = dhcpPacket.getOption(OptionCode_MessageType);
if (optType == null) {
log.trace("DHCP packet with no message type, ignore it");
return null;
}
- DHCPPacketType inPacketType = DHCPPacketType.getType(optType.getData()[0]);
+ DHCP.MsgType inPacketType = DHCP.MsgType.getType(optType.getData()[0]);
if (inPacketType == null) {
log.trace("DHCP packet with no packet type, ignore it");
}
@@ -318,9 +317,9 @@
dhcpReply.setServerIPAddress(gatewayIp.toInt());
dhcpReply.setClientHardwareAddress(request.getClientHardwareAddress());
- List<DHCPOption> options = Lists.newArrayList();
+ List<DhcpOption> options = Lists.newArrayList();
// message type
- DHCPOption option = new DHCPOption();
+ DhcpOption option = new DhcpOption();
option.setCode(OptionCode_MessageType.getValue());
option.setLength((byte) 1);
byte[] optionData = {msgType};
@@ -328,14 +327,14 @@
options.add(option);
// server identifier
- option = new DHCPOption();
+ option = new DhcpOption();
option.setCode(OptionCode_DHCPServerIp.getValue());
option.setLength((byte) 4);
option.setData(gatewayIp.toOctets());
options.add(option);
// lease time
- option = new DHCPOption();
+ option = new DhcpOption();
option.setCode(OptionCode_LeaseTime.getValue());
option.setLength((byte) 4);
option.setData(DHCP_DATA_LEASE_INFINITE);
@@ -343,7 +342,7 @@
// subnet mask
Ip4Address subnetMask = Ip4Address.makeMaskPrefix(subnetPrefixLen);
- option = new DHCPOption();
+ option = new DhcpOption();
option.setCode(OptionCode_SubnetMask.getValue());
option.setLength((byte) 4);
option.setData(subnetMask.toOctets());
@@ -351,35 +350,35 @@
// broadcast address
Ip4Address broadcast = Ip4Address.makeMaskedAddress(yourIp, subnetPrefixLen);
- option = new DHCPOption();
+ option = new DhcpOption();
option.setCode(OptionCode_BroadcastAddress.getValue());
option.setLength((byte) 4);
option.setData(broadcast.toOctets());
options.add(option);
// domain server
- option = new DHCPOption();
+ option = new DhcpOption();
option.setCode(OptionCode_DomainServer.getValue());
option.setLength((byte) 4);
option.setData(DEFAULT_DNS.toOctets());
options.add(option);
// TODO fix MTU value to be configurable
- option = new DHCPOption();
+ option = new DhcpOption();
option.setCode(DHCP_OPTION_MTU);
option.setLength((byte) 2);
option.setData(DHCP_DATA_MTU_DEFAULT);
options.add(option);
// router address
- option = new DHCPOption();
+ option = new DhcpOption();
option.setCode(OptionCode_RouterAddress.getValue());
option.setLength((byte) 4);
option.setData(gatewayIp.toOctets());
options.add(option);
// end option
- option = new DHCPOption();
+ option = new DhcpOption();
option.setCode(OptionCode_END.getValue());
option.setLength((byte) 1);
options.add(option);