Token ring counter parser

Change-Id: I9f4f3f6ff5e62501fe56336e0262b962027dad20
diff --git a/apps/ipflow-monitor/sflow/api/src/main/java/org/onosproject/sflow/TokenRingCounter.java b/apps/ipflow-monitor/sflow/api/src/main/java/org/onosproject/sflow/TokenRingCounter.java
index 1a275b1..5e09ec6 100644
--- a/apps/ipflow-monitor/sflow/api/src/main/java/org/onosproject/sflow/TokenRingCounter.java
+++ b/apps/ipflow-monitor/sflow/api/src/main/java/org/onosproject/sflow/TokenRingCounter.java
@@ -16,11 +16,18 @@
 package org.onosproject.sflow;
 
 import com.google.common.base.MoreObjects;
+import java.nio.ByteBuffer;
+import org.onlab.packet.BasePacket;
+import org.onlab.packet.Deserializer;
+
+import java.util.function.BiPredicate;
 
 /**
  * Represents Token Ring counters for network interfaces.
  */
-public final class TokenRingCounter {
+public final class TokenRingCounter extends BasePacket {
+
+    public static final int TOKENRING_COUNTER_LENGTH = 78;
 
     private InterfaceCounter generic;
     private int dot5StatsLineErrors;
@@ -261,6 +268,61 @@
     }
 
     /**
+     * Deserializer function for sFlow token ring counter.
+     *
+     * @return deserializer function
+     */
+    public static Deserializer<TokenRingCounter> deserializer() {
+        return (data, offset, length) -> {
+
+            BiPredicate<ByteBuffer, Integer> isValidBuffer = (b, l)
+                    -> b.hasRemaining() && b.remaining() >= l;
+
+            ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
+            byte[] ifCounterBytes;
+            if (!isValidBuffer.test(bb, InterfaceCounter.INTERFACE_COUNTER_LENGTH)) {
+                throw new IllegalStateException("Invalid token ring counter data.");
+            }
+
+            ifCounterBytes = new byte[InterfaceCounter.INTERFACE_COUNTER_LENGTH];
+            bb.get(ifCounterBytes);
+
+            InterfaceCounter interfaceCounter = InterfaceCounter.deserializer().deserialize(ifCounterBytes,
+                    0, InterfaceCounter.INTERFACE_COUNTER_LENGTH);
+            if (!isValidBuffer.test(bb, TOKENRING_COUNTER_LENGTH)) {
+                throw new IllegalStateException("Invalid token ring counter buffer size.");
+            }
+
+            Builder builder = new Builder();
+            return builder.generic(interfaceCounter)
+                    .dot5StatsLineErrors(bb.getInt())
+                    .dot5StatsBurstErrors(bb.getInt())
+                    .dot5StatsAcErrors(bb.getInt())
+                    .dot5StatsAbortTransErrors(bb.getInt())
+                    .dot5StatsInternalErrors(bb.getInt())
+                    .dot5StatsLostFrameErrors(bb.getInt())
+                    .dot5StatsReceiveCongestions(bb.getInt())
+                    .dot5StatsFrameCopiedErrors(bb.getInt())
+                    .dot5StatsTokenErrors(bb.getInt())
+                    .dot5StatsSoftErrors(bb.getInt())
+                    .dot5StatsHardErrors(bb.getInt())
+                    .dot5StatsSignalLoss(bb.getInt())
+                    .dot5StatsTransmitBeacons(bb.getInt())
+                    .dot5StatsRecoverys(bb.getInt())
+                    .dot5StatsLobeWires(bb.getInt())
+                    .dot5StatsRemoves(bb.getInt())
+                    .dot5StatsSingles(bb.getInt())
+                    .dot5StatsFreqErrors(bb.getInt())
+                    .build();
+        };
+    }
+
+    @Override
+    public byte[] serialize() {
+        throw new UnsupportedOperationException("Not supported yet.");
+    }
+
+    /**
      * Builder pattern to create an instance of InterfaceCounter.
      */
     private static class Builder {