Fix relay agent ip for indirect DHCPv4 packets

When only default server config is supplied
    The indirect traffic should be relayed to default server
When both default and indirect config is supplied
    The indirect traffic should be relayed to indirect server

When relay agent IP is not specified in either direct or indirect config
    For direct clients, use client interface IP as relay agent IP
    For indirect clients, keep original relay agent IP in the DHCP packet

Change-Id: I065e1adf25c942e19240efd5192b50333a650ad1
diff --git a/apps/dhcprelay/src/main/java/org/onosproject/dhcprelay/Dhcp4HandlerImpl.java b/apps/dhcprelay/src/main/java/org/onosproject/dhcprelay/Dhcp4HandlerImpl.java
index f8ee711..804059b 100644
--- a/apps/dhcprelay/src/main/java/org/onosproject/dhcprelay/Dhcp4HandlerImpl.java
+++ b/apps/dhcprelay/src/main/java/org/onosproject/dhcprelay/Dhcp4HandlerImpl.java
@@ -521,12 +521,13 @@
         }
 
         etherReply.setSourceMACAddress(macFacingServer);
-        etherReply.setDestinationMACAddress(dhcpConnectMac);
-        etherReply.setVlanID(dhcpConnectVlan.toShort());
         ipv4Packet.setSourceAddress(ipFacingServer.toInt());
-        ipv4Packet.setDestinationAddress(dhcpServerIp.toInt());
 
         if (isDirectlyConnected) {
+            etherReply.setDestinationMACAddress(dhcpConnectMac);
+            etherReply.setVlanID(dhcpConnectVlan.toShort());
+            ipv4Packet.setDestinationAddress(dhcpServerIp.toInt());
+
             ConnectPoint inPort = context.inPacket().receivedFrom();
             VlanId vlanId = VlanId.vlanId(ethernetPacket.getVlanID());
             // add connected in port and vlan
@@ -542,7 +543,7 @@
             newRelayAgentOpt.setCode(OptionCode_CircuitID.getValue());
             newRelayAgentOpt.addSubOption(circuitIdSubOpt);
 
-            // Removes END option  first
+            // Removes END option first
             List<DhcpOption> options = dhcpPacket.getOptions().stream()
                     .filter(opt -> opt.getCode() != OptionCode_END.getValue())
                     .collect(Collectors.toList());
@@ -557,23 +558,31 @@
 
             dhcpPacket.setOptions(options);
 
-            // Sets giaddr to IP address from the Interface which facing to
-            // DHCP client
-            dhcpPacket.setGatewayIPAddress(clientInterfaceIp.toInt());
+            // Sets relay agent IP
+            int effectiveRelayAgentIp = relayAgentIp != null ?
+                    relayAgentIp.toInt() : clientInterfaceIp.toInt();
+            dhcpPacket.setGatewayIPAddress(effectiveRelayAgentIp);
+        } else {
+            if (indirectDhcpServerIp != null) {
+                // Use indirect server config for indirect packets if configured
+                etherReply.setDestinationMACAddress(indirectDhcpConnectMac);
+                etherReply.setVlanID(indirectDhcpConnectVlan.toShort());
+                ipv4Packet.setDestinationAddress(indirectDhcpServerIp.toInt());
 
-            // replace giaddr if relay agent IP is set
-            if (relayAgentIp != null) {
-                dhcpPacket.setGatewayIPAddress(relayAgentIp.toInt());
-            }
-        } else if (indirectDhcpServerIp != null) {
-            // Indirect case, replace destination to indirect dhcp server if exist
-            etherReply.setDestinationMACAddress(indirectDhcpConnectMac);
-            etherReply.setVlanID(indirectDhcpConnectVlan.toShort());
-            ipv4Packet.setDestinationAddress(indirectDhcpServerIp.toInt());
+                // Set giaddr if indirect relay agent IP is configured
+                if (indirectRelayAgentIp != null) {
+                    dhcpPacket.setGatewayIPAddress(indirectRelayAgentIp.toInt());
+                }
+            } else {
+                // Otherwise, use default server config for indirect packets
+                etherReply.setDestinationMACAddress(dhcpConnectMac);
+                etherReply.setVlanID(dhcpConnectVlan.toShort());
+                ipv4Packet.setDestinationAddress(dhcpServerIp.toInt());
 
-            // replace giaddr if relay agent IP is set
-            if (indirectRelayAgentIp != null) {
-                dhcpPacket.setGatewayIPAddress(relayAgentIp.toInt());
+                // Set giaddr if direct relay agent IP is configured
+                if (relayAgentIp != null) {
+                    dhcpPacket.setGatewayIPAddress(relayAgentIp.toInt());
+                }
             }
         }