[VOL-3500] Moving DHCP packet processing in separated thread

Change-Id: Ibb9ca631548d9cabb8e3c76daa8c55094a18304a
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 a765548..8e11002 100755
--- a/app/src/main/java/org/opencord/dhcpl2relay/impl/DhcpL2Relay.java
+++ b/app/src/main/java/org/opencord/dhcpl2relay/impl/DhcpL2Relay.java
@@ -15,31 +15,11 @@
  */
 package org.opencord.dhcpl2relay.impl;
 
-import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_MessageType;
-import static org.onlab.packet.MacAddress.valueOf;
-import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY;
-import static org.opencord.dhcpl2relay.impl.OsgiPropertyConstants.ENABLE_DHCP_BROADCAST_REPLIES;
-import static org.opencord.dhcpl2relay.impl.OsgiPropertyConstants.ENABLE_DHCP_BROADCAST_REPLIES_DEFAULT;
-import static org.opencord.dhcpl2relay.impl.OsgiPropertyConstants.OPTION_82;
-import static org.opencord.dhcpl2relay.impl.OsgiPropertyConstants.OPTION_82_DEFAULT;
-
-import java.io.ByteArrayOutputStream;
-import java.nio.ByteBuffer;
-import java.time.Instant;
-import java.util.ArrayList;
-import java.util.Dictionary;
-import java.util.List;
-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;
-import java.util.concurrent.atomic.AtomicReference;
-import java.util.function.Predicate;
-import java.util.stream.Collectors;
-
+import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
 import org.apache.commons.io.HexDump;
 import org.onlab.packet.DHCP;
 import org.onlab.packet.Ethernet;
@@ -113,10 +93,30 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.Lists;
-import com.google.common.collect.Sets;
+import java.io.ByteArrayOutputStream;
+import java.nio.ByteBuffer;
+import java.time.Instant;
+import java.util.ArrayList;
+import java.util.Dictionary;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.UUID;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+
+import static java.util.concurrent.Executors.newFixedThreadPool;
+import static org.onlab.packet.DHCP.DHCPOptionCode.OptionCode_MessageType;
+import static org.onlab.packet.MacAddress.valueOf;
+import static org.onlab.util.Tools.groupedThreads;
+import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY;
+import static org.opencord.dhcpl2relay.impl.OsgiPropertyConstants.*;
 
 /**
  * DHCP Relay Agent Application Component.
@@ -125,6 +125,7 @@
         property = {
                 OPTION_82 + ":Boolean=" + OPTION_82_DEFAULT,
                 ENABLE_DHCP_BROADCAST_REPLIES + ":Boolean=" + ENABLE_DHCP_BROADCAST_REPLIES_DEFAULT,
+                PACKET_PROCESSOR_THREADS + ":Integer=" + PACKET_PROCESSOR_THREADS_DEFAULT,
         })
 public class DhcpL2Relay
         extends AbstractListenerManager<DhcpL2RelayEvent, DhcpL2RelayListener>
@@ -197,6 +198,11 @@
      */
     protected boolean enableDhcpBroadcastReplies = ENABLE_DHCP_BROADCAST_REPLIES_DEFAULT;
 
+    /**
+     * Number of threads used to process the packet.
+     */
+    protected int packetProcessorThreads = PACKET_PROCESSOR_THREADS_DEFAULT;
+
     ScheduledFuture<?> refreshTask;
     ScheduledExecutorService refreshService = Executors.newSingleThreadScheduledExecutor();
 
@@ -221,8 +227,11 @@
 
     private DhcpL2RelayStoreDelegate delegate = new InnerDhcpL2RelayStoreDelegate();
 
+    protected ExecutorService packetProcessorExecutor;
+
     @Activate
     protected void activate(ComponentContext context) {
+
         //start the dhcp relay agent
         appId = coreService.registerApplication(DHCP_L2RELAY_APP);
         // ensure that host-learning via dhcp includes IP addresses
@@ -255,13 +264,15 @@
         factories.forEach(cfgService::registerConfigFactory);
         //update the dhcp server configuration.
         updateConfig();
-        //add the packet services.
-        packetService.addProcessor(dhcpRelayPacketProcessor,
-                                   PacketProcessor.director(0));
+
         if (context != null) {
             modified(context);
         }
 
+        //add the packet services.
+        packetService.addProcessor(dhcpRelayPacketProcessor,
+                                   PacketProcessor.director(0));
+
         log.info("DHCP-L2-RELAY Started");
     }
 
@@ -279,6 +290,7 @@
         packetService.removeProcessor(dhcpRelayPacketProcessor);
         cancelDhcpPktsFromServer();
 
+        packetProcessorExecutor.shutdown();
         componentConfigService.unregisterProperties(getClass(), false);
         deviceService.removeListener(deviceListener);
         mastershipService.removeListener(changeListener);
@@ -300,6 +312,19 @@
         if (o != null) {
             enableDhcpBroadcastReplies = o;
         }
+
+        String s = Tools.get(properties, PACKET_PROCESSOR_THREADS);
+        int oldpacketProcessorThreads = packetProcessorThreads;
+        packetProcessorThreads = Strings.isNullOrEmpty(s) ? oldpacketProcessorThreads
+                : Integer.parseInt(s.trim());
+        if (packetProcessorExecutor == null || oldpacketProcessorThreads != packetProcessorThreads) {
+            if (packetProcessorExecutor != null) {
+                packetProcessorExecutor.shutdown();
+            }
+            packetProcessorExecutor = newFixedThreadPool(packetProcessorThreads,
+                                                         groupedThreads("onos/dhcp",
+                                                                        "dhcp-packet-%d", log));
+        }
     }
 
     @Override
@@ -591,6 +616,12 @@
 
         @Override
         public void process(PacketContext context) {
+            packetProcessorExecutor.execute(() -> {
+                processInternal(context);
+            });
+        }
+
+        private void processInternal(PacketContext context) {
             if (!configured()) {
                 log.warn("Missing DHCP relay config. Abort packet processing");
                 return;
@@ -612,6 +643,11 @@
                     if (udpPacket.getSourcePort() == UDP.DHCP_CLIENT_PORT ||
                             udpPacket.getSourcePort() == UDP.DHCP_SERVER_PORT) {
                         DHCP dhcpPayload = (DHCP) udpPacket.getPayload();
+                        if (log.isTraceEnabled()) {
+                            log.trace("Processing packet with type {} from MAC {}",
+                                      getDhcpPacketType(dhcpPayload),
+                                      MacAddress.valueOf(dhcpPayload.getClientHardwareAddress()));
+                        }
                         //This packet is dhcp.
                         processDhcpPacket(context, packet, dhcpPayload);
                     }
@@ -621,8 +657,15 @@
 
         //forward the packet to ConnectPoint where the DHCP server is attached.
         private void forwardPacket(Ethernet packet, PacketContext context) {
+            if (log.isTraceEnabled()) {
+                IPv4 ipv4Packet = (IPv4) packet.getPayload();
+                UDP udpPacket = (UDP) ipv4Packet.getPayload();
+                DHCP dhcpPayload = (DHCP) udpPacket.getPayload();
+                log.trace("Emitting packet to server: packet {}, with MAC {}",
+                          getDhcpPacketType(dhcpPayload),
+                          MacAddress.valueOf(dhcpPayload.getClientHardwareAddress()));
+            }
             ConnectPoint toSendTo = null;
-
             if (!useOltUplink) {
                 toSendTo = dhcpServerConnectPoint.get();
             } else {
@@ -895,7 +938,7 @@
             // If we can't find the subscriber, can't process further
             if (subsCp == null) {
                 log.warn("Couldn't find subscriber, service or host info for mac"
-                        + " address {} .. DHCP packet won't be delivered", dstMac);
+                        + " address {} and vlan {} .. DHCP packet won't be delivered", dstMac, innerVlan);
                 return null;
             }
 
@@ -984,17 +1027,15 @@
         private void sendReply(Ethernet ethPacket, DHCP dhcpPayload, PacketContext context) {
             MacAddress descMac = valueOf(dhcpPayload.getClientHardwareAddress());
             ConnectPoint subCp = getConnectPointOfClient(descMac, context);
-
             // Send packet out to requester if the host information is available
             if (subCp != null) {
-                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,
-                              ethPacket);
+                    log.trace("Relaying packet to DHCP client at {} with MacAddress {}, {}", subCp,
+                              descMac, ethPacket);
                 }
                 packetService.emit(o);
             } else {
diff --git a/app/src/main/java/org/opencord/dhcpl2relay/impl/OsgiPropertyConstants.java b/app/src/main/java/org/opencord/dhcpl2relay/impl/OsgiPropertyConstants.java
index 3c47467..bcd4672 100644
--- a/app/src/main/java/org/opencord/dhcpl2relay/impl/OsgiPropertyConstants.java
+++ b/app/src/main/java/org/opencord/dhcpl2relay/impl/OsgiPropertyConstants.java
@@ -33,6 +33,9 @@
     public static final String PUBLISH_COUNTERS_RATE = "publishCountersRate";
     public static final int PUBLISH_COUNTERS_RATE_DEFAULT = 10;
 
+    public static final String PACKET_PROCESSOR_THREADS = "packetProcessorThreads";
+    public static final int PACKET_PROCESSOR_THREADS_DEFAULT = 10;
+
     public static final String SYNC_COUNTERS_RATE = "syncCountersRate";
     public static final int SYNC_COUNTERS_RATE_DEFAULT = 5;
 }
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 e37470b..c19b2eb 100755
--- a/app/src/test/java/org/opencord/dhcpl2relay/impl/DhcpL2RelayTest.java
+++ b/app/src/test/java/org/opencord/dhcpl2relay/impl/DhcpL2RelayTest.java
@@ -16,13 +16,14 @@
 package org.opencord.dhcpl2relay.impl;
 
 import static org.easymock.EasyMock.createMock;
-import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.*;
 import static org.slf4j.LoggerFactory.getLogger;
 
 import java.nio.ByteBuffer;
 import java.util.List;
 import java.util.Map;
 
+import com.google.common.util.concurrent.MoreExecutors;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -87,6 +88,7 @@
         store.clusterCommunicationService = new ClusterCommunicationServiceAdapter();
         store.componentConfigService = mockConfigService;
         TestUtils.setField(store, "eventDispatcher", new TestEventDispatcher());
+        TestUtils.setField(dhcpL2Relay, "packetProcessorExecutor", MoreExecutors.newDirectExecutorService());
         store.activate(new MockComponentContext());
         dhcpL2Relay.dhcpL2RelayCounters = this.store;
     }
@@ -101,9 +103,9 @@
 
     private void checkAllocation(DHCP.MsgType messageType) {
         ConnectPoint clientCp = ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/"
-                + String.valueOf(CLIENT_PORT));
+                                                                        + String.valueOf(CLIENT_PORT));
         allocs = dhcpL2Relay.getAllocationInfo();
-        assert allocs.size() == 1;
+        assertEquals(1, allocs.size());
         allocs.forEach((k, v) -> {
             log.info("Allocation {} : {}", k, v);
             assertEquals(v.type(), messageType);
@@ -119,33 +121,36 @@
         MacAddress mac32 = MacAddress.valueOf("b4:96:91:0c:4f:e4");
         VlanId vlan32a = VlanId.vlanId((short) 801);
         Ethernet discover32a = constructDhcpDiscoverPacket(
-                                  mac32, vlan32a, (short) 0);
+                mac32, vlan32a, (short) 0);
         ConnectPoint client32 = ConnectPoint.deviceConnectPoint(
-                                                "of:0000b86a974385f7/32");
+                "of:0000b86a974385f7/32");
         sendPacket(discover32a, client32);
+
         allocs = dhcpL2Relay.getAllocationInfo();
-        assert allocs.size() == 1;
+        assertEquals(1, allocs.size());
 
         //Trigger a discover from another RG on port 4112
         MacAddress mac4112 = MacAddress.valueOf("b4:96:91:0c:4f:c9");
         VlanId vlan4112 = VlanId.vlanId((short) 101);
         Ethernet discover4112 = constructDhcpDiscoverPacket(
-                                                            mac4112, vlan4112,
-                                                            (short) 0);
+                mac4112, vlan4112,
+                (short) 0);
         ConnectPoint client4112 = ConnectPoint.deviceConnectPoint(
                 "of:0000b86a974385f7/4112");
         sendPacket(discover4112, client4112);
+
         allocs = dhcpL2Relay.getAllocationInfo();
-        assert allocs.size() == 2;
+        assertEquals(2, allocs.size());
 
         // Trigger a discover for another service with a different vlan
         // from the same UNI port 32
         VlanId vlan32b = VlanId.vlanId((short) 802);
         Ethernet discover32b = constructDhcpDiscoverPacket(
-                                  mac32, vlan32b, (short) 0);
+                mac32, vlan32b, (short) 0);
         sendPacket(discover32b, client32);
+
         allocs = dhcpL2Relay.getAllocationInfo();
-        assert allocs.size() == 3;
+        assertEquals(3, allocs.size());
 
         allocs.forEach((k, v) -> {
             log.info("Allocation {} : {}", k, v);
@@ -175,12 +180,12 @@
      * @throws Exception when an unhandled error occurs
      */
     @Test
-    public void testDhcpDiscover()  throws Exception {
+    public void testDhcpDiscover() throws Exception {
         // Sending DHCP Discover packet
         dhcpL2Relay.clearAllocations();
         Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC);
         ConnectPoint clientCp = ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/"
-                + String.valueOf(CLIENT_PORT));
+                                                                        + String.valueOf(CLIENT_PORT));
         sendPacket(discoverPacket, clientCp);
 
         Ethernet discoverRelayed = (Ethernet) getPacket();
@@ -194,11 +199,11 @@
      * @throws Exception when an unhandled error occurs
      */
     @Test
-    public void testDhcpRequest()  throws Exception {
+    public void testDhcpRequest() throws Exception {
         // Sending DHCP Request packet
         Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC);
         ConnectPoint clientCp = ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/"
-                + String.valueOf(CLIENT_PORT));
+                                                                        + String.valueOf(CLIENT_PORT));
         sendPacket(requestPacket, clientCp);
 
         Ethernet requestRelayed = (Ethernet) getPacket();
@@ -212,12 +217,12 @@
      * @throws Exception when an unhandled error occurs
      */
     @Test
-    public void testDhcpOffer() {
+    public void testDhcpOffer() throws InterruptedException {
         // Sending DHCP Offer packet
         Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC,
-                CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
+                                                        CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
         sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/"
-                + String.valueOf(UPLINK_PORT)));
+                                                                        + String.valueOf(UPLINK_PORT)));
 
         Ethernet offerRelayed = (Ethernet) getPacket();
         compareServerPackets(offerPacket, offerRelayed);
@@ -230,13 +235,13 @@
      * @throws Exception when an unhandled error occurs
      */
     @Test
-    public void testDhcpAck() {
+    public void testDhcpAck() throws InterruptedException {
 
         Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC,
-                CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
+                                                    CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
 
         sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/"
-                + String.valueOf(UPLINK_PORT)));
+                                                                      + String.valueOf(UPLINK_PORT)));
 
         Ethernet ackRelayed = (Ethernet) getPacket();
         compareServerPackets(ackPacket, ackRelayed);
@@ -249,10 +254,10 @@
      * @throws Exception when an unhandled error occurs
      */
     @Test
-    public void testDhcpNak() {
+    public void testDhcpNak() throws InterruptedException {
 
         Ethernet nakPacket = constructDhcpNakPacket(SERVER_MAC,
-                CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
+                                                    CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
 
         sendPacket(nakPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
 
@@ -267,7 +272,7 @@
      * @throws Exception when an unhandled error occurs
      */
     @Test
-    public void testDhcpDecline() {
+    public void testDhcpDecline() throws InterruptedException {
 
         Ethernet declinePacket = constructDhcpDeclinePacket(CLIENT_MAC);
 
@@ -282,34 +287,30 @@
      * Tests the DHCP global counters.
      */
     @Test
-    public void testDhcpGlobalCounters() {
-        long discoveryValue = 0;
-        long offerValue = 0;
-        long requestValue = 0;
-        long ackValue = 0;
+    public void testDhcpGlobalCounters() throws InterruptedException {
 
         Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC);
         Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC,
-                CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
+                                                        CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
         Ethernet requestPacket = constructDhcpRequestPacket(CLIENT_MAC);
         Ethernet ackPacket = constructDhcpAckPacket(SERVER_MAC,
-                CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
+                                                    CLIENT_MAC, DESTINATION_ADDRESS_IP, DHCP_CLIENT_IP_ADDRESS);
 
         sendPacket(discoverPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
         sendPacket(offerPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
         sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
         sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
 
-        Map<DhcpL2RelayCountersIdentifier, Long> countersMap = store.getCountersMap();
-        discoveryValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
-                DhcpL2RelayCounterNames.valueOf("DHCPDISCOVER")));
-        offerValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
-                DhcpL2RelayCounterNames.valueOf("DHCPOFFER")));
-        requestValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
-                DhcpL2RelayCounterNames.valueOf("DHCPREQUEST")));
-        ackValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
-                DhcpL2RelayCounterNames.valueOf("DHCPACK")));
 
+        Map<DhcpL2RelayCountersIdentifier, Long> countersMap = store.getCountersMap();
+        long discoveryValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
+                                                  DhcpL2RelayCounterNames.valueOf("DHCPDISCOVER")));
+        long offerValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
+                                              DhcpL2RelayCounterNames.valueOf("DHCPOFFER")));
+        long requestValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
+                                                DhcpL2RelayCounterNames.valueOf("DHCPREQUEST")));
+        long ackValue = countersMap.get(new DhcpL2RelayCountersIdentifier(DhcpL2RelayEvent.GLOBAL_COUNTER,
+                                            DhcpL2RelayCounterNames.valueOf("DHCPACK")));
         assertEquals(1, discoveryValue);
         assertEquals(1, offerValue);
         assertEquals(1, requestValue);
@@ -318,14 +319,9 @@
 
     /**
      * Tests the DHCP per subscriber counters.
-     *
      */
     @Test
-    public void testDhcpPerSubscriberCounters() {
-        long discoveryValue;
-        long offerValue;
-        long requestValue;
-        long ackValue;
+    public void testDhcpPerSubscriberCounters() throws Exception {
 
         Ethernet discoverPacket = constructDhcpDiscoverPacket(CLIENT_MAC);
         Ethernet offerPacket = constructDhcpOfferPacket(SERVER_MAC,
@@ -339,16 +335,16 @@
         sendPacket(requestPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
         sendPacket(ackPacket, ConnectPoint.deviceConnectPoint(OLT_DEV_ID + "/" + 1));
 
-        Map<DhcpL2RelayCountersIdentifier, Long> countersMap = store.getCountersMap();
-        discoveryValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
-                DhcpL2RelayCounterNames.valueOf("DHCPDISCOVER")));
-        offerValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
-                DhcpL2RelayCounterNames.valueOf("DHCPOFFER")));
-        requestValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
-                DhcpL2RelayCounterNames.valueOf("DHCPREQUEST")));
-        ackValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
-                DhcpL2RelayCounterNames.valueOf("DHCPACK")));
 
+        Map<DhcpL2RelayCountersIdentifier, Long> countersMap = store.getCountersMap();
+        long discoveryValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
+                                                  DhcpL2RelayCounterNames.valueOf("DHCPDISCOVER")));
+        long offerValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
+                                              DhcpL2RelayCounterNames.valueOf("DHCPOFFER")));
+        long requestValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
+                                                DhcpL2RelayCounterNames.valueOf("DHCPREQUEST")));
+        long ackValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
+                                            DhcpL2RelayCounterNames.valueOf("DHCPACK")));
         assertEquals(1, discoveryValue);
         assertEquals(1, offerValue);
         assertEquals(1, requestValue);
@@ -381,19 +377,21 @@
     }
 
     public void compareServerPackets(Ethernet sent, Ethernet relayed) {
-        sent.setDestinationMACAddress(CLIENT_MAC);
-        sent.setQinQVID(NOT_PROVIDED);
-        sent.setQinQPriorityCode((byte) NOT_PROVIDED);
-        sent.setVlanID(CLIENT_C_TAG.toShort());
 
-        final ByteBuffer byteBuffer = ByteBuffer.wrap(sent.serialize());
-        Ethernet expectedPacket = null;
         try {
-            expectedPacket = Ethernet.deserializer().deserialize(byteBuffer.array(),
-                    0, byteBuffer.array().length);
+            sent.setDestinationMACAddress(CLIENT_MAC);
+            sent.setQinQVID(NOT_PROVIDED);
+            sent.setQinQPriorityCode((byte) NOT_PROVIDED);
+            sent.setVlanID(CLIENT_C_TAG.toShort());
+
+            final ByteBuffer byteBuffer = ByteBuffer.wrap(sent.serialize());
+            Ethernet expectedPacket = Ethernet.deserializer().deserialize(byteBuffer.array(),
+                                                                          0, byteBuffer.array().length);
+            assertEquals(expectedPacket, relayed);
         } catch (Exception e) {
+            log.error(e.getMessage());
+            fail();
         }
-        assertEquals(expectedPacket, relayed);
 
     }