Interface counter record

hierarchy of the flow counter
1) sFlow packet
2) sFlow samples
3) counter samples
4) counter record
5) counter packet

Change-Id: Ic6b8464e2769452c6392ce2cef3e5a6ae8822e85
diff --git a/apps/ipflow-monitor/sflow/api/src/main/java/org/onosproject/sflow/CounterPacket.java b/apps/ipflow-monitor/sflow/api/src/main/java/org/onosproject/sflow/CounterPacket.java
new file mode 100644
index 0000000..d76fa30
--- /dev/null
+++ b/apps/ipflow-monitor/sflow/api/src/main/java/org/onosproject/sflow/CounterPacket.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2024-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.sflow;
+
+import org.onlab.packet.BasePacket;
+
+/**
+ * The sFlow interface counter packet.
+ */
+public abstract class CounterPacket extends BasePacket {
+
+    @Override
+    public byte[] serialize() {
+        throw new UnsupportedOperationException("Not supported yet.");
+    }
+
+}
\ No newline at end of file
diff --git a/apps/ipflow-monitor/sflow/api/src/main/java/org/onosproject/sflow/CounterRecord.java b/apps/ipflow-monitor/sflow/api/src/main/java/org/onosproject/sflow/CounterRecord.java
new file mode 100644
index 0000000..8b4091e
--- /dev/null
+++ b/apps/ipflow-monitor/sflow/api/src/main/java/org/onosproject/sflow/CounterRecord.java
@@ -0,0 +1,209 @@
+/*
+ * Copyright 2024-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.sflow;
+
+import java.util.Arrays;
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.onlab.packet.BasePacket;
+import org.onlab.packet.DeserializationException;
+import org.onlab.packet.Deserializer;
+
+import com.google.common.base.MoreObjects;
+
+import static com.google.common.base.Preconditions.checkState;
+
+/**
+ * The sFlow interface counter record.
+ */
+public final class CounterRecord extends BasePacket {
+
+    private Type type;
+
+    private int length;
+
+    private CounterPacket counterPacket;
+
+
+    private CounterRecord(Builder builder) {
+        this.type = builder.type;
+        this.length = builder.length;
+        this.counterPacket = builder.counterPacket;
+    }
+
+    /**
+     * Get sFlow counter type.
+     *
+     * @return counter type.
+     */
+    public Type getType() {
+        return type;
+    }
+
+    /**
+     * Get sFlow counter record lenght.
+     *
+     * @return counter record length.
+     */
+    public int getLength() {
+        return length;
+    }
+
+    /**
+     * Get sFlow counter packet.
+     *
+     * @return counter packet.
+     */
+    public CounterPacket getCounterPacket() {
+        return counterPacket;
+    }
+
+
+    /**
+     * Interface counter type.
+     * Counter type : Generic, Ethernet, Token ring, Fddi, Vg, Wan, Vlan.
+     */
+    public enum Type {
+
+        GENERIC(1, InterfaceCounter.deserializer()),
+        ETHERNET(2, EthernetCounter.deserializer()),
+        TOKENRING(3, TokenRingCounter.deserializer()),
+        FDDI(4, InterfaceCounter.deserializer()),
+        VG(5, VgCounter.deserializer()),
+        WAN(6, InterfaceCounter.deserializer()),
+        VLAN(7, VlanCounter.deserializer());
+
+
+        private final int counterType;
+        private final Deserializer deserializer;
+
+        Type(int counterType, Deserializer deserializer) {
+            this.counterType = counterType;
+            this.deserializer = deserializer;
+        }
+
+        private static Map<Integer, Type> parser = new ConcurrentHashMap<>();
+
+        static {
+            Arrays.stream(Type.values()).forEach(type -> parser.put(type.counterType, type));
+        }
+
+        public static Type getType(int ctype) throws DeserializationException {
+            if ((ctype < 1) || (ctype > 7)) {
+                throw new DeserializationException("Invalid counter type");
+            }
+            return Optional.of(ctype)
+                    .filter(id -> parser.containsKey(id))
+                    .map(id -> parser.get(id))
+                    .orElse(GENERIC);
+        }
+
+        public Deserializer getDecoder() {
+            return this.deserializer;
+        }
+
+    }
+
+    /**
+     * Data deserializer function for flow interface counter record.
+     *
+     * @return data deserializer function
+     */
+    public static Deserializer<CounterSample> deserializer() {
+        return (data, offset, length) -> {
+            return null;
+        };
+    }
+
+    @Override
+    public byte[] serialize() {
+        throw new UnsupportedOperationException("Not supported yet.");
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(getClass())
+                .add("type", type)
+                .add("length", length)
+                .add("counterPacket", counterPacket)
+                .toString();
+    }
+
+    /**
+     * Builder for sFlow interface counter record.
+     */
+    private static class Builder {
+
+        private Type type;
+
+        private int length;
+
+        private CounterPacket counterPacket;
+
+        /**
+         * Setter sFlow counter record length.
+         *
+         * @param length of the counter record.
+         * @return this class builder.
+         */
+        public Builder length(int length) {
+            this.length = length;
+            return this;
+        }
+
+        /**
+         * Setter sFlow counter type.
+         *
+         * @param type sFlow counter type.
+         * @return this class builder.
+         */
+        public Builder type(Type type) {
+            this.type = type;
+            return this;
+        }
+
+        /**
+         * Setter sFlow flow counter packet.
+         *
+         * @param sequenceNumber flow counter packet.
+         * @return this class builder.
+         */
+        public Builder counterPacket(CounterPacket counterPacket) {
+            this.counterPacket = counterPacket;
+            return this;
+        }
+
+        /**
+         * Checks arguments for sFlow sample interface counter.
+         */
+        private void checkArguments() {
+            checkState(type != null, "Invalid counter type.");
+            checkState(length != 0, "Invalid counter record length.");
+        }
+
+        /**
+         * Builds sFlow interface counter record.
+         *
+         * @return interface counter record.
+         */
+        public CounterRecord build() {
+            checkArguments();
+            return new CounterRecord(this);
+        }
+    }
+}
\ No newline at end of file
diff --git a/apps/ipflow-monitor/sflow/api/src/main/java/org/onosproject/sflow/EthernetCounter.java b/apps/ipflow-monitor/sflow/api/src/main/java/org/onosproject/sflow/EthernetCounter.java
index 553963a..1aef808 100644
--- a/apps/ipflow-monitor/sflow/api/src/main/java/org/onosproject/sflow/EthernetCounter.java
+++ b/apps/ipflow-monitor/sflow/api/src/main/java/org/onosproject/sflow/EthernetCounter.java
@@ -17,7 +17,6 @@
 
 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;
@@ -25,7 +24,7 @@
 /**
  * Represents ethernet counters for network interfaces.
  */
-public final class EthernetCounter extends BasePacket {
+public final class EthernetCounter extends CounterPacket {
 
     public static final int ETHERNET_COUNTER_LENGTH = 52;
 
@@ -252,11 +251,6 @@
         };
     }
 
-    @Override
-    public byte[] serialize() {
-        throw new UnsupportedOperationException("Not supported yet.");
-    }
-
     /**
      * Builder pattern to create an instance of InterfaceCounter.
      */
diff --git a/apps/ipflow-monitor/sflow/api/src/main/java/org/onosproject/sflow/InterfaceCounter.java b/apps/ipflow-monitor/sflow/api/src/main/java/org/onosproject/sflow/InterfaceCounter.java
index 3b03a65..f0b7d72 100644
--- a/apps/ipflow-monitor/sflow/api/src/main/java/org/onosproject/sflow/InterfaceCounter.java
+++ b/apps/ipflow-monitor/sflow/api/src/main/java/org/onosproject/sflow/InterfaceCounter.java
@@ -18,7 +18,6 @@
 import com.google.common.base.MoreObjects;
 import static com.google.common.base.Preconditions.checkState;
 import java.nio.ByteBuffer;
-import org.onlab.packet.BasePacket;
 import org.onlab.packet.Deserializer;
 
 import java.util.function.BiPredicate;
@@ -26,7 +25,7 @@
 /**
  * Represents interface counters for network interfaces.
  */
-public final class InterfaceCounter extends BasePacket {
+public final class InterfaceCounter extends CounterPacket {
 
     public static final int INTERFACE_COUNTER_LENGTH = 88;
 
@@ -372,11 +371,6 @@
         };
     }
 
-    @Override
-    public byte[] serialize() {
-        throw new UnsupportedOperationException("Not supported yet.");
-    }
-
     /**
      * Builder pattern to create an instance of InterfaceCounter.
      */
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 5e09ec6..6af79e3 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
@@ -17,7 +17,6 @@
 
 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;
@@ -25,7 +24,7 @@
 /**
  * Represents Token Ring counters for network interfaces.
  */
-public final class TokenRingCounter extends BasePacket {
+public final class TokenRingCounter extends CounterPacket {
 
     public static final int TOKENRING_COUNTER_LENGTH = 78;
 
@@ -317,11 +316,6 @@
         };
     }
 
-    @Override
-    public byte[] serialize() {
-        throw new UnsupportedOperationException("Not supported yet.");
-    }
-
     /**
      * Builder pattern to create an instance of InterfaceCounter.
      */
diff --git a/apps/ipflow-monitor/sflow/api/src/main/java/org/onosproject/sflow/VgCounter.java b/apps/ipflow-monitor/sflow/api/src/main/java/org/onosproject/sflow/VgCounter.java
index aa85d9b..2ba6ed7 100644
--- a/apps/ipflow-monitor/sflow/api/src/main/java/org/onosproject/sflow/VgCounter.java
+++ b/apps/ipflow-monitor/sflow/api/src/main/java/org/onosproject/sflow/VgCounter.java
@@ -17,7 +17,6 @@
 
 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;
@@ -25,7 +24,7 @@
 /**
  * Represents VG counters for network interfaces.
  */
-public final class VgCounter extends BasePacket {
+public final class VgCounter extends CounterPacket {
 
     public static final int VG_COUNTER_LENGTH = 80;
 
@@ -267,11 +266,6 @@
         };
     }
 
-    @Override
-    public byte[] serialize() {
-        throw new UnsupportedOperationException("Not supported yet.");
-    }
-
     /**
      * Builder pattern to create an instance of InterfaceCounter.
      */
diff --git a/apps/ipflow-monitor/sflow/api/src/main/java/org/onosproject/sflow/VlanCounter.java b/apps/ipflow-monitor/sflow/api/src/main/java/org/onosproject/sflow/VlanCounter.java
index deb699a..479b6ab 100644
--- a/apps/ipflow-monitor/sflow/api/src/main/java/org/onosproject/sflow/VlanCounter.java
+++ b/apps/ipflow-monitor/sflow/api/src/main/java/org/onosproject/sflow/VlanCounter.java
@@ -17,7 +17,6 @@
 
 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;
@@ -25,7 +24,7 @@
 /**
  * Represents VLAN counters for network interfaces.
  */
-public final class VlanCounter extends BasePacket {
+public final class VlanCounter extends CounterPacket {
 
     public static final int VLAN_COUNTER_LENGTH = 28;
 
@@ -139,11 +138,6 @@
         };
     }
 
-    @Override
-    public byte[] serialize() {
-        throw new UnsupportedOperationException("Not supported yet.");
-    }
-
     /**
      * Builder pattern to create an instance of VlanCounter.
      */