Merge "[VOL-3353] Enabled Find Security Bugs"
diff --git a/api/src/main/java/org/opencord/dhcpl2relay/DhcpAllocationInfo.java b/api/src/main/java/org/opencord/dhcpl2relay/DhcpAllocationInfo.java
index f87c5b0..770b2f0 100755
--- a/api/src/main/java/org/opencord/dhcpl2relay/DhcpAllocationInfo.java
+++ b/api/src/main/java/org/opencord/dhcpl2relay/DhcpAllocationInfo.java
@@ -33,6 +33,7 @@
     private IpAddress ip;
     private Instant allocationTime;
     private DHCP.MsgType type;
+    private String subscriberId;
 
     /**
      * Creates a new DHCP allocation info record.
@@ -42,15 +43,17 @@
      * @param circuitId option 82 information
      * @param macAddress MAC address of client
      * @param ip IP of client if allocated
+     * @param subscriberId Sadis ID of a subscriber
      */
     public DhcpAllocationInfo(ConnectPoint location, DHCP.MsgType type,
-                              String circuitId, MacAddress macAddress, IpAddress ip) {
+                              String circuitId, MacAddress macAddress, IpAddress ip, String subscriberId) {
         this.location = location;
         this.type = type;
         this.circuitId = circuitId;
         this.macAddress = macAddress;
         this.ip = ip;
         this.allocationTime = Instant.now();
+        this.subscriberId = subscriberId;
     }
 
     /**
@@ -99,6 +102,15 @@
     }
 
     /**
+     * SubscriberId of client if it has one.
+     *
+     * @return SubscriberId
+     */
+    public String subscriberId() {
+        return subscriberId;
+    }
+
+    /**
      * Timestamp when the last DHCP message was seen.
      *
      * @return time
diff --git a/api/src/main/java/org/opencord/dhcpl2relay/DhcpL2RelayService.java b/api/src/main/java/org/opencord/dhcpl2relay/DhcpL2RelayService.java
index b8c18aa..73d0634 100644
--- a/api/src/main/java/org/opencord/dhcpl2relay/DhcpL2RelayService.java
+++ b/api/src/main/java/org/opencord/dhcpl2relay/DhcpL2RelayService.java
@@ -44,5 +44,5 @@
      * @param cp the ConnectPoint associated with this lease
      * @return boolean
      */
-    boolean removeAllocationByConnectPoint(ConnectPoint cp);
+    boolean removeAllocationsByConnectPoint(ConnectPoint cp);
 }
diff --git a/app/src/main/java/org/opencord/dhcpl2relay/cli/DhcpL2RelayAllocationsCommand.java b/app/src/main/java/org/opencord/dhcpl2relay/cli/DhcpL2RelayAllocationsCommand.java
index 7d08737..891e4fb 100644
--- a/app/src/main/java/org/opencord/dhcpl2relay/cli/DhcpL2RelayAllocationsCommand.java
+++ b/app/src/main/java/org/opencord/dhcpl2relay/cli/DhcpL2RelayAllocationsCommand.java
@@ -34,8 +34,8 @@
         service.getAllocationInfo().forEach((key, value) -> {
             print("SubscriberId=%s,ConnectPoint=%s,State=%s,MAC=%s,CircuitId=%s" +
                             ",IP Allocated=%s,Allocation Timestamp=%s",
-                    key, value.location(), value.type(), value.macAddress().toString(), value.circuitId(),
-                    value.ipAddress().getIp4Address().toString(), value.allocationTime().toString());
+                    value.subscriberId(), value.location(), value.type(), value.macAddress().toString(),
+                    value.circuitId(), value.ipAddress().getIp4Address().toString(), value.allocationTime().toString());
         });
     }
 }
diff --git a/app/src/main/java/org/opencord/dhcpl2relay/cli/DhcpL2RelayRemoveAllocationsCommand.java b/app/src/main/java/org/opencord/dhcpl2relay/cli/DhcpL2RelayRemoveAllocationsCommand.java
index 7120923..1f53909 100644
--- a/app/src/main/java/org/opencord/dhcpl2relay/cli/DhcpL2RelayRemoveAllocationsCommand.java
+++ b/app/src/main/java/org/opencord/dhcpl2relay/cli/DhcpL2RelayRemoveAllocationsCommand.java
@@ -53,12 +53,12 @@
         PortNumber port = PortNumber.portNumber(strPort);
         ConnectPoint cp = new ConnectPoint(deviceId, port);
 
-        Boolean success = service.removeAllocationByConnectPoint(cp);
+        Boolean success = service.removeAllocationsByConnectPoint(cp);
 
         if (success) {
-            print("DHCP Allocation removed for port %s on device %s", strPort, strDeviceId);
+            print("DHCP Allocation(s) removed for port %s on device %s", strPort, strDeviceId);
         } else {
-            print("DHCP Allocation not found for port %s on device %s", strPort, strDeviceId);
+            print("DHCP Allocation(s) not found for port %s on device %s", strPort, strDeviceId);
         }
 
     }
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 0d98431..f32b675 100755
--- a/app/src/main/java/org/opencord/dhcpl2relay/impl/DhcpL2Relay.java
+++ b/app/src/main/java/org/opencord/dhcpl2relay/impl/DhcpL2Relay.java
@@ -101,6 +101,7 @@
 import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
+import java.util.UUID;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.ScheduledFuture;
@@ -120,10 +121,10 @@
  * DHCP Relay Agent Application Component.
  */
 @Component(immediate = true,
-property = {
-        OPTION_82 + ":Boolean=" + OPTION_82_DEFAULT,
-        ENABLE_DHCP_BROADCAST_REPLIES + ":Boolean=" + ENABLE_DHCP_BROADCAST_REPLIES_DEFAULT,
-})
+        property = {
+                OPTION_82 + ":Boolean=" + OPTION_82_DEFAULT,
+                ENABLE_DHCP_BROADCAST_REPLIES + ":Boolean=" + ENABLE_DHCP_BROADCAST_REPLIES_DEFAULT,
+        })
 public class DhcpL2Relay
         extends AbstractListenerManager<DhcpL2RelayEvent, DhcpL2RelayListener>
         implements DhcpL2RelayService {
@@ -137,8 +138,8 @@
 
     private final Set<ConfigFactory> factories = ImmutableSet.of(
             new ConfigFactory<ApplicationId, DhcpL2RelayConfig>(APP_SUBJECT_FACTORY,
-                    DhcpL2RelayConfig.class,
-                    "dhcpl2relay") {
+                                                                DhcpL2RelayConfig.class,
+                                                                "dhcpl2relay") {
                 @Override
                 public DhcpL2RelayConfig createConfig() {
                     return new DhcpL2RelayConfig();
@@ -186,9 +187,13 @@
     protected ClusterService clusterService;
 
     // OSGi Properties
-    /** Add option 82 to relayed packets. */
+    /**
+     * Add option 82 to relayed packets.
+     */
     protected boolean option82 = OPTION_82_DEFAULT;
-    /** Ask the DHCP Server to send back replies as L2 broadcast. */
+    /**
+     * Ask the DHCP Server to send back replies as L2 broadcast.
+     */
     protected boolean enableDhcpBroadcastReplies = ENABLE_DHCP_BROADCAST_REPLIES_DEFAULT;
 
     ScheduledFuture<?> refreshTask;
@@ -251,7 +256,7 @@
         updateConfig();
         //add the packet services.
         packetService.addProcessor(dhcpRelayPacketProcessor,
-                PacketProcessor.director(0));
+                                   PacketProcessor.director(0));
         if (context != null) {
             modified(context);
         }
@@ -302,6 +307,15 @@
     }
 
     /**
+     * Generates a unique UUID from a string.
+     *
+     * @return true if all information we need have been initialized
+     */
+    private static String getUniqueUuidFromString(String value) {
+        return UUID.nameUUIDFromBytes(value.getBytes()).toString();
+    }
+
+    /**
      * Checks if this app has been configured.
      *
      * @return true if all information we need have been initialized
@@ -321,7 +335,7 @@
             dhcpServerConnectPoint.set(null);
             if (dhcpConnectPoints != null) {
                 // find a connect point through a device for which we are master
-                for (ConnectPoint cp: dhcpConnectPoints) {
+                for (ConnectPoint cp : dhcpConnectPoints) {
                     if (isLocalLeader(cp.deviceId())) {
                         if (deviceService.isAvailable(cp.deviceId())) {
                             dhcpServerConnectPoint.set(cp);
@@ -403,12 +417,12 @@
         }
         if (add) {
             log.info("Adding trap to dhcp server connect point: "
-                    + dhcpServerConnectPoint);
+                             + dhcpServerConnectPoint);
             requestDhcpPacketsFromConnectPoint(dhcpServerConnectPoint.get(),
                                                Optional.of(PacketPriority.HIGH1));
         } else {
             log.info("Removing trap from dhcp server connect point: "
-                    + dhcpServerConnectPoint);
+                             + dhcpServerConnectPoint);
             cancelDhcpPacketsFromConnectPoint(dhcpServerConnectPoint.get(),
                                               Optional.of(PacketPriority.HIGH1));
         }
@@ -489,7 +503,7 @@
      * Optionally provide a priority for the trap flow. If no such priority is
      * provided, the default priority will be used.
      *
-     * @param cp the connect point to trap dhcp packets from
+     * @param cp       the connect point to trap dhcp packets from
      * @param priority of the trap flow, null to use default priority
      */
     private void requestDhcpPacketsFromConnectPoint(ConnectPoint cp,
@@ -500,8 +514,8 @@
                 .matchIPProtocol(IPv4.PROTOCOL_UDP)
                 .matchUdpSrc(TpPort.tpPort(UDP.DHCP_SERVER_PORT));
         packetService.requestPackets(selectorServer.build(),
-                priority.isPresent() ? priority.get() : PacketPriority.CONTROL,
-                appId, Optional.of(cp.deviceId()));
+                                     priority.isPresent() ? priority.get() : PacketPriority.CONTROL,
+                                     appId, Optional.of(cp.deviceId()));
     }
 
     /**
@@ -509,9 +523,9 @@
      * the request was made with a specific packet priority, then the same
      * priority should be used in this call.
      *
-     * @param cp the connect point for the trap flow
+     * @param cp       the connect point for the trap flow
      * @param priority with which the trap flow was requested; if request
-     *            priority was not specified, this param should also be null
+     *                 priority was not specified, this param should also be null
      */
     private void cancelDhcpPacketsFromConnectPoint(ConnectPoint cp,
                                                    Optional<PacketPriority> priority) {
@@ -521,8 +535,8 @@
                 .matchIPProtocol(IPv4.PROTOCOL_UDP)
                 .matchUdpSrc(TpPort.tpPort(UDP.DHCP_SERVER_PORT));
         packetService.cancelPackets(selectorServer.build(),
-                priority.isPresent() ? priority.get() : PacketPriority.CONTROL,
-                appId, Optional.of(cp.deviceId()));
+                                    priority.isPresent() ? priority.get() : PacketPriority.CONTROL,
+                                    appId, Optional.of(cp.deviceId()));
     }
 
     private SubscriberAndDeviceInformation getDevice(PacketContext context) {
@@ -612,7 +626,7 @@
                 toSendTo = dhcpServerConnectPoint.get();
             } else {
                 toSendTo = getUplinkConnectPointOfOlt(context.inPacket().
-                                                      receivedFrom().deviceId());
+                        receivedFrom().deviceId());
             }
 
             if (toSendTo != null) {
@@ -646,8 +660,8 @@
             return null;
         }
 
-        private void  updateDhcpRelayCountersStore(SubscriberAndDeviceInformation entry,
-                                                   DhcpL2RelayCounterNames counterType) {
+        private void updateDhcpRelayCountersStore(SubscriberAndDeviceInformation entry,
+                                                  DhcpL2RelayCounterNames counterType) {
             // Update global counter stats
             dhcpL2RelayCounters.incrementCounter(DhcpL2RelayEvent.GLOBAL_COUNTER, counterType);
             if (entry == null) {
@@ -694,13 +708,14 @@
 
             DHCP.MsgType incomingPacketType = getDhcpPacketType(dhcpPayload);
             if (incomingPacketType == null) {
-                log.warn("DHCP packet type not found. Dump of ethernet pkt in hex format for troubleshooting.");
+                log.warn("DHCP Packet type not found. Dump of ethernet pkt in hex format for troubleshooting.");
                 byte[] array = packet.serialize();
                 ByteArrayOutputStream buf = new ByteArrayOutputStream();
                 try {
                     HexDump.dump(array, 0, buf, 0);
                     log.trace(buf.toString());
-                } catch (Exception e) { }
+                } catch (Exception e) {
+                }
                 return;
             }
 
@@ -784,7 +799,7 @@
         private Ethernet processDhcpPacketFromClient(PacketContext context,
                                                      Ethernet ethernetPacket) {
             if (log.isTraceEnabled()) {
-                log.trace("DHCP packet received from client at {} {}",
+                log.trace("DHCP Packet received from client at {} {}",
                           context.inPacket().receivedFrom(), ethernetPacket);
             }
 
@@ -817,15 +832,16 @@
             UniTagInformation uniTagInformation = getUnitagInformationFromPacketContext(context, entry);
             if (uniTagInformation == null) {
                 log.warn("Missing service information for connectPoint {} / cTag {}",
-                        context.inPacket().receivedFrom(),  context.inPacket().parsed().getVlanID());
+                         context.inPacket().receivedFrom(), context.inPacket().parsed().getVlanID());
                 return null;
             }
 
             DhcpAllocationInfo info = new DhcpAllocationInfo(
                     context.inPacket().receivedFrom(), dhcpPacket.getPacketType(),
-                    entry.circuitId(), clientMac, clientIp);
+                    entry.circuitId(), clientMac, clientIp, entry.id());
 
-            allocations.put(entry.id(), info);
+            String key = getUniqueUuidFromString(entry.id() + info.macAddress());
+            allocations.put(key, info);
 
             post(new DhcpL2RelayEvent(DhcpL2RelayEvent.Type.UPDATED, info,
                                       context.inPacket().receivedFrom()));
@@ -848,8 +864,8 @@
             if (uniTagInformation.getUsPonSTagPriority() != -1) {
                 etherReply.setQinQPriorityCode((byte) uniTagInformation.getUsPonSTagPriority());
             }
-            log.info("Finished processing DHCP packet of type {} from {} and relaying to dhcpServer",
-                    dhcpPacket.getPacketType(), entry.id());
+            log.info("Finished processing DHCP Packet of type {} from {} and relaying to dhcpServer",
+                     dhcpPacket.getPacketType(), entry.id());
             return etherReply;
         }
 
@@ -857,7 +873,7 @@
         private Ethernet processDhcpPacketFromServer(PacketContext context,
                                                      Ethernet ethernetPacket) {
             if (log.isTraceEnabled()) {
-                log.trace("DHCP packet received from server at {} {}",
+                log.trace("DHCP Packet received from server at {} {}",
                           context.inPacket().receivedFrom(), ethernetPacket);
             }
             // get dhcp header.
@@ -882,8 +898,10 @@
                 IpAddress ip = IpAddress.valueOf(dhcpPayload.getYourIPAddress());
                 //storeDHCPAllocationInfo
                 DhcpAllocationInfo info = new DhcpAllocationInfo(subsCp,
-                        dhcpPayload.getPacketType(), entry.circuitId(), dstMac, ip);
-                    allocations.put(entry.id(), info);
+                     dhcpPayload.getPacketType(), entry.circuitId(), dstMac, ip, entry.id());
+
+                String key = getUniqueUuidFromString(entry.id() + info.macAddress());
+                allocations.put(key, info);
 
                 post(new DhcpL2RelayEvent(DhcpL2RelayEvent.Type.UPDATED, info, subsCp));
             } // end storing of info
@@ -892,7 +910,7 @@
             UniTagInformation uniTagInformation = getUnitagInformationFromPacketContext(context, entry);
             if (uniTagInformation == null) {
                 log.warn("Missing service information for connectPoint {} / cTag {}",
-                        context.inPacket().receivedFrom(),  context.inPacket().parsed().getVlanID());
+                         context.inPacket().receivedFrom(), context.inPacket().parsed().getVlanID());
                 return null;
             }
 
@@ -926,15 +944,14 @@
             Set<Host> hosts = hostService.getHostsByMac(dstMac);
             if (hosts == null || hosts.isEmpty()) {
                 log.warn("Cannot determine host for DHCP client: {}. Aborting "
-                                + "relay for dhcp packet from server",
-                        dstMac);
+                                 + "relay for DHCP Packet from server", dstMac);
                 return null;
             }
             for (Host h : hosts) {
                 // if more than one,
                 // find the connect point which has an valid entry in SADIS
                 ConnectPoint cp = new ConnectPoint(h.location().deviceId(),
-                        h.location().port());
+                                                   h.location().port());
 
                 String portId = nasPortId(cp);
                 SubscriberAndDeviceInformation sub = subsService.get(portId);
@@ -950,7 +967,7 @@
             }
             // no sadis config found for this connectPoint/vlan
             log.warn("Missing service information for connectPoint {} / cTag {}",
-                    context.inPacket().receivedFrom(),  context.inPacket().parsed().getVlanID());
+                     context.inPacket().receivedFrom(), context.inPacket().parsed().getVlanID());
 
             return null;
         }
@@ -962,24 +979,24 @@
 
             // Send packet out to requester if the host information is available
             if (subCp != null) {
-                log.info("Sending DHCP packet to client at {}", subCp);
+                log.info("Sending DHCP Packet to client at {}", subCp);
                 TrafficTreatment t = DefaultTrafficTreatment.builder()
                         .setOutput(subCp.port()).build();
                 OutboundPacket o = new DefaultOutboundPacket(
                         subCp.deviceId(), t, ByteBuffer.wrap(ethPacket.serialize()));
                 if (log.isTraceEnabled()) {
-                    log.trace("Relaying packet to dhcp client at {} {}", subCp,
+                    log.trace("Relaying packet to DHCP client at {} {}", subCp,
                               ethPacket);
                 }
                 packetService.emit(o);
             } else {
-                log.error("Dropping DHCP packet because can't find host for {}", descMac);
+                log.error("Dropping DHCP Packet because can't find host for {}", descMac);
             }
         }
     }
 
     private DHCP addOption82(DHCP dhcpPacket, SubscriberAndDeviceInformation entry) {
-        log.debug("option82data {} ", entry);
+        log.trace("option82data {} ", entry);
 
         List<DhcpOption> options = Lists.newArrayList(dhcpPacket.getOptions());
         DhcpOption82 option82 = new DhcpOption82();
@@ -1005,6 +1022,7 @@
 
         return dhcpPacket.setOptions(newoptions);
     }
+
     /**
      * Listener for network config events.
      */
@@ -1055,15 +1073,16 @@
     }
 
 
-    public boolean removeAllocationByConnectPoint(ConnectPoint cp) {
+    public boolean removeAllocationsByConnectPoint(ConnectPoint cp) {
+        boolean removed = false;
         for (String key : allocations.keySet()) {
             DhcpAllocationInfo entry = allocations.asJavaMap().get(key);
             if (entry.location().equals(cp)) {
                 allocations.remove(key);
-                return true;
+                removed = true;
             }
         }
-        return false;
+        return removed;
     }
 
 
@@ -1171,8 +1190,8 @@
                     case PORT_ADDED:
                         if (useOltUplink && isUplinkPortOfOlt(event.subject().id(), event.port())) {
                             requestDhcpPacketsFromConnectPoint(
-                                new ConnectPoint(event.subject().id(), event.port().number()),
-                                        Optional.empty());
+                                    new ConnectPoint(event.subject().id(), event.port().number()),
+                                    Optional.empty());
                         }
                         break;
                     default:
@@ -1191,7 +1210,7 @@
                     // infuse the event with the allocation info before posting
                     DhcpAllocationInfo info = Versioned.valueOrNull(allocations.get(event.getSubscriberId()));
                     toPost = new DhcpL2RelayEvent(event.type(), info, event.connectPoint(),
-                            event.getCountersEntry(), event.getSubscriberId());
+                                                  event.getCountersEntry(), event.getSubscriberId());
                 }
                 post(toPost);
             }