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 59310b5..31c278a 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
 @Property(name = "version", value = "4")
@@ -1183,19 +1182,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 {
@@ -1520,7 +1540,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
@@ -1531,7 +1550,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)
@@ -1592,8 +1610,6 @@
 
     }
 
-
-
     /**
      * Send the response DHCP to the requester host.
      *
@@ -1866,7 +1882,6 @@
         return validServerInfo;
     }
 
-
     private boolean checkDhcpServerConnPt(boolean directConnFlag,
                                           DhcpServerInfo serverInfo) {
         if (serverInfo.getDhcpServerConnectPoint() == null) {
@@ -1968,7 +1983,6 @@
         }
     }
 
-
     private DhcpServerInfo findServerInfoFromServer(boolean directConnFlag, ConnectPoint inPort) {
         List<DhcpServerInfo> validServerInfoList = findValidServerInfo(directConnFlag);
         DhcpServerInfo  foundServerInfo = null;