[VOL-2672] dhcpl2relay should relay DHCPNAK and DHCPDECLINE messages
Change-Id: I545f8c18766c08c00ae6fedc40ebf950e6f4c78f
diff --git a/app/src/main/java/org/opencord/dhcpl2relay/impl/DhcpL2Relay.java b/app/src/main/java/org/opencord/dhcpl2relay/impl/DhcpL2Relay.java
index 0e5b593..2c1081b 100755
--- a/app/src/main/java/org/opencord/dhcpl2relay/impl/DhcpL2Relay.java
+++ b/app/src/main/java/org/opencord/dhcpl2relay/impl/DhcpL2Relay.java
@@ -784,10 +784,21 @@
updateDhcpRelayCountersStore(entry, DhcpL2RelayCounters.valueOf("DHCPACK"));
break;
case DHCPDECLINE:
+ Ethernet ethernetPacketDecline =
+ processDhcpPacketFromClient(context, packet);
+ if (ethernetPacketDecline != null) {
+ forwardPacket(ethernetPacketDecline, context);
+ }
entry = getSubscriberInfoFromClient(context);
updateDhcpRelayCountersStore(entry, DhcpL2RelayCounters.valueOf("DHCPDECLINE"));
break;
case DHCPNAK:
+ //reply to dhcp client.
+ Ethernet ethernetPacketNak =
+ processDhcpPacketFromServer(context, packet);
+ if (ethernetPacketNak != null) {
+ sendReply(ethernetPacketNak, dhcpPayload, context);
+ }
entry = getSubscriberInfoFromServer(dhcpPayload, context);
updateDhcpRelayCountersStore(entry, DhcpL2RelayCounters.valueOf("DHCPNACK"));
break;
diff --git a/app/src/test/java/org/opencord/dhcpl2relay/impl/DhcpL2RelayTest.java b/app/src/test/java/org/opencord/dhcpl2relay/impl/DhcpL2RelayTest.java
index a0cab4a..6d0524e 100755
--- a/app/src/test/java/org/opencord/dhcpl2relay/impl/DhcpL2RelayTest.java
+++ b/app/src/test/java/org/opencord/dhcpl2relay/impl/DhcpL2RelayTest.java
@@ -151,6 +151,39 @@
}
/**
+ * Tests the DHCP relay app by sending DHCP Nak Packet.
+ *
+ * @throws Exception when an unhandled error occurs
+ */
+ @Test
+ public void testDhcpNak() {
+
+ Ethernet nakPacket = constructDhcpNakPacket(SERVER_MAC,
+ CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
+
+ sendPacket(nakPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
+
+ Ethernet nakRelayed = (Ethernet) getPacket();
+ compareServerPackets(nakPacket, nakRelayed);
+ }
+
+ /**
+ * Tests the DHCP relay app by sending DHCP Decline Packet.
+ *
+ * @throws Exception when an unhandled error occurs
+ */
+ @Test
+ public void testDhcpDecline() {
+
+ Ethernet declinePacket = constructDhcpDeclinePacket(CLIENT_MAC);
+
+ sendPacket(declinePacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
+
+ Ethernet declineRelayed = (Ethernet) getPacket();
+ compareClientPackets(declinePacket, declineRelayed);
+ }
+
+ /**
* Tests the DHCP global counters.
*/
@Test
diff --git a/app/src/test/java/org/opencord/dhcpl2relay/impl/DhcpL2RelayTestBase.java b/app/src/test/java/org/opencord/dhcpl2relay/impl/DhcpL2RelayTestBase.java
index 85854ab..3d88b38 100755
--- a/app/src/test/java/org/opencord/dhcpl2relay/impl/DhcpL2RelayTestBase.java
+++ b/app/src/test/java/org/opencord/dhcpl2relay/impl/DhcpL2RelayTestBase.java
@@ -729,6 +729,46 @@
}
/**
+ * Constructs DHCP Nak Packet.
+ *
+ * @return Ethernet packet
+ */
+ Ethernet constructDhcpNakPacket(MacAddress servMac, MacAddress clientMac,
+ String ipAddress, String dhcpClientIpAddress) {
+
+ Ethernet pkt = construcEthernetPacket(servMac, clientMac, ipAddress, DHCP.OPCODE_REPLY,
+ clientMac, Ip4Address.valueOf(dhcpClientIpAddress));
+
+ IPv4 ipv4Packet = (IPv4) pkt.getPayload();
+ UDP udpPacket = (UDP) ipv4Packet.getPayload();
+ DHCP dhcpPacket = (DHCP) udpPacket.getPayload();
+
+ dhcpPacket.setOptions(constructDhcpOptions(DHCP.MsgType.DHCPNAK));
+
+ return pkt;
+ }
+
+ /**
+ * Constructs DHCP Decline Packet.
+ *
+ * @return Ethernet packet
+ */
+ Ethernet constructDhcpDeclinePacket(MacAddress clientMac) {
+
+ Ethernet pkt = construcEthernetPacket(clientMac, MacAddress.BROADCAST,
+ "255.255.255.255", DHCP.OPCODE_REQUEST, MacAddress.NONE,
+ Ip4Address.valueOf("0.0.0.0"));
+
+ IPv4 ipv4Packet = (IPv4) pkt.getPayload();
+ UDP udpPacket = (UDP) ipv4Packet.getPayload();
+ DHCP dhcpPacket = (DHCP) udpPacket.getPayload();
+
+ dhcpPacket.setOptions(constructDhcpOptions(DHCP.MsgType.DHCPDECLINE));
+
+ return pkt;
+ }
+
+ /**
* Constructs DHCP Discover Options.
*
* @return Ethernet packet