Indirect host DHCP replay from server set source IP as relay agent IP
Removed a line to make stylecheck happy
Added fixes identified in JUNIT
Corrected indentation
Fixed file lines count issue
Incorporated review comments

Change-Id: Iebfba49a940d4d1d388688dc8877765a0145d4af
diff --git a/apps/dhcprelay/app/src/main/java/org/onosproject/dhcprelay/Dhcp4HandlerImpl.java b/apps/dhcprelay/app/src/main/java/org/onosproject/dhcprelay/Dhcp4HandlerImpl.java
index 8decb7f..3868809 100644
--- a/apps/dhcprelay/app/src/main/java/org/onosproject/dhcprelay/Dhcp4HandlerImpl.java
+++ b/apps/dhcprelay/app/src/main/java/org/onosproject/dhcprelay/Dhcp4HandlerImpl.java
@@ -120,7 +120,6 @@
 import static org.onosproject.net.flowobjective.Objective.Operation.ADD;
 import static org.onosproject.net.flowobjective.Objective.Operation.REMOVE;
 
-
 @Component(
     service = { DhcpHandler.class, HostProvider.class },
     property = {
@@ -1184,19 +1183,40 @@
             etherReply.setDestinationMACAddress(dhcpPayload.getClientHardwareAddress());
         }
 
-        // we leave the srcMac from the original packet
-        // figure out the relay agent IP corresponding to the original request
         Ip4Address ipFacingClient = getFirstIpFromInterface(clientInterface);
-        if (ipFacingClient == null) {
-            log.warn("Cannot determine relay agent interface Ipv4 addr for host {}/{}. "
-                             + "Aborting relay for dhcp packet from server {}",
-                     etherReply.getDestinationMAC(), clientInterface.vlan(),
-                     ethernetPacket);
-            return null;
+        if (directlyConnected(dhcpPayload)) {
+            // we leave the srcMac from the original packet
+            // figure out the relay agent IP corresponding to the original request
+            if (ipFacingClient == null) {
+                log.warn("Cannot determine relay agent interface Ipv4 addr for host {}/{}. "
+                                + "Aborting relay for dhcp packet from server {}",
+                        etherReply.getDestinationMAC(), clientInterface.vlan(),
+                        ethernetPacket);
+                return null;
+            }
+            // SRC_IP: IP facing client
+            ipv4Packet.setSourceAddress(ipFacingClient.toInt());
+        } else {
+            // Get the IP address of the relay agent
+            Ip4Address relayAgentIp = foundServerInfo
+                .getRelayAgentIp4(clientInterface.connectPoint().deviceId()).orElse(null);
+            if (relayAgentIp == null) {
+                if (ipFacingClient == null) {
+                    log.warn("Cannot determine relay agent interface Ipv4 addr for host {}/{}. "
+                                    + "Aborting relay for dhcp packet from server for indirect host {}",
+                            etherReply.getDestinationMAC(), clientInterface.vlan(),
+                            ethernetPacket);
+                    return null;
+               } else {
+                    // SRC_IP: IP facing client
+                    ipv4Packet.setSourceAddress(ipFacingClient.toInt());
+                }
+            } else {
+                // SRC_IP: relay agent IP
+                ipv4Packet.setSourceAddress(relayAgentIp.toInt());
+            }
         }
-        // SRC_IP: relay agent IP
         // DST_IP: offered IP
-        ipv4Packet.setSourceAddress(ipFacingClient.toInt());
         if (((int) dhcpPayload.getFlags() & 0x8000) == 0x0000) {
             ipv4Packet.setDestinationAddress(dhcpPayload.getYourIPAddress());
         } else {
@@ -1521,7 +1541,6 @@
             log.debug("Invalid circuit {}, use information from dhcp payload",
                       circuitIdSubOption.getData());
         }
-
         // Use Vlan Id from DHCP server if DHCP relay circuit id was not
         // sent by ONOS or circuit Id can't be parsed
         // TODO: remove relay store from this method
@@ -1532,7 +1551,6 @@
             log.debug("not find the matching DHCP record for mac: {} and vlan: {}", dstMac, originalPacketVlanId);
             return Optional.empty();
         }
-
         Optional<DhcpRecord> dhcpRecord = dhcpRelayStore.getDhcpRecord(HostId.hostId(dstMac, filteredVlanId));
         ConnectPoint clientConnectPoint = dhcpRecord
                 .map(DhcpRecord::locations)
@@ -1593,8 +1611,6 @@
 
     }
 
-
-
     /**
      * Send the response DHCP to the requester host.
      *
@@ -1867,7 +1883,6 @@
         return validServerInfo;
     }
 
-
     private boolean checkDhcpServerConnPt(boolean directConnFlag,
                                           DhcpServerInfo serverInfo) {
         if (serverInfo.getDhcpServerConnectPoint() == null) {
@@ -1969,7 +1984,6 @@
         }
     }
 
-
     private DhcpServerInfo findServerInfoFromServer(boolean directConnFlag, ConnectPoint inPort) {
         List<DhcpServerInfo> validServerInfoList = findValidServerInfo(directConnFlag);
         DhcpServerInfo  foundServerInfo = null;