sFlow interface counter sample decoder
Change-Id: I87dea27c00baaf5fe43670d2b49e41eb9498bf01
diff --git a/apps/ipflow-monitor/sflow/api/src/main/java/org/onosproject/sflow/CounterSample.java b/apps/ipflow-monitor/sflow/api/src/main/java/org/onosproject/sflow/CounterSample.java
index 502646f..5c54c30 100644
--- a/apps/ipflow-monitor/sflow/api/src/main/java/org/onosproject/sflow/CounterSample.java
+++ b/apps/ipflow-monitor/sflow/api/src/main/java/org/onosproject/sflow/CounterSample.java
@@ -20,6 +20,8 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
+import java.util.function.BiPredicate;
+import java.nio.ByteBuffer;
import com.google.common.base.MoreObjects;
@@ -59,8 +61,10 @@
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
+ public static final int COUNTER_HEADER_LENGTH = 20;
+ public static final int RECORD_MIN_HEADER_LENGTH = 8;
- private List<Object> records;
+ private List<CounterRecord> records;
private CounterSample(Builder builder) {
this.enterprise = builder.enterprise;
@@ -78,7 +82,7 @@
*
* @return counter records.
*/
- public List<Object> getRecords() {
+ public List<CounterRecord> getRecords() {
return records;
}
@@ -89,7 +93,35 @@
*/
public static Deserializer<CounterSample> deserializer() {
return (data, offset, length) -> {
- return null;
+ BiPredicate<ByteBuffer, Integer> isValidBuffer = (b, l)
+ -> b.hasRemaining() && b.remaining() >= l;
+
+ ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
+ if (!isValidBuffer.test(bb, (COUNTER_HEADER_LENGTH + RECORD_MIN_HEADER_LENGTH))) {
+ throw new IllegalStateException("Invalid interface sample counter buffer size.");
+ }
+ Builder builder = new Builder();
+ builder.type(SflowSample.Type.getType(bb.getInt()))
+ .length(bb.getInt())
+ .sequenceNumber(bb.getInt())
+ .sourceIndex(bb.getInt())
+ .numberOfRecords(bb.getInt());
+
+ while (bb.hasRemaining()) {
+ int counterType = bb.getInt();
+ int counterLength = bb.getInt();
+ bb.position(bb.position() - RECORD_MIN_HEADER_LENGTH);
+ int recordLenght = counterLength + RECORD_MIN_HEADER_LENGTH;
+ if (bb.remaining() < recordLenght) {
+ break;
+ }
+ byte[] recordBytes = new byte[recordLenght];
+ bb.get(recordBytes);
+ builder.record(CounterRecord.deserializer()
+ .deserialize(recordBytes, 0, recordLenght));
+ }
+ return builder.build();
+
};
}
@@ -171,7 +203,7 @@
private int numberOfRecords;
- private List<Object> records = new LinkedList<>();
+ private List<CounterRecord> records = new LinkedList<>();
/**
* Setter sFlow enterprise id.
@@ -256,7 +288,7 @@
* @param records interface counter records.
* @return this class builder.
*/
- public Builder records(List<Object> records) {
+ public Builder records(List<CounterRecord> records) {
this.records = records;
return this;
}
@@ -267,7 +299,7 @@
* @param record sample interface counter record.
* @return this class builder.
*/
- public Builder record(Object record) {
+ public Builder record(CounterRecord record) {
this.records.add(record);
return this;
}