[ONOS-7596] Support reading table entries with counter data in P4Runtime

Change-Id: I85bacb1697a6c881dd69ba74a2162c73ec0b8aee
diff --git a/core/api/src/main/java/org/onosproject/net/pi/runtime/PiCounterCell.java b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiCounterCell.java
new file mode 100644
index 0000000..9508cbc
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiCounterCell.java
@@ -0,0 +1,98 @@
+/*
+ * Copyright 2017-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.net.pi.runtime;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Objects;
+
+/**
+ * Counter cell of a protocol-independent pipeline.
+ */
+@Beta
+public final class PiCounterCell {
+
+    private final PiCounterCellId cellId;
+    private final PiCounterCellData counterData;
+
+    /**
+     * Creates a new counter cell for the given cell identifier and counter cell data.
+     *
+     * @param cellId  counter cell identifier
+     * @param piCounterCellData  counter cell data
+     */
+    public PiCounterCell(PiCounterCellId cellId, PiCounterCellData piCounterCellData) {
+        this.cellId = cellId;
+        this.counterData = piCounterCellData;
+    }
+
+    /**
+     * Creates a new counter cell for the given cell identifier, number of packets and bytes.
+     *
+     * @param cellId  counter cell identifier
+     * @param packets  number of packets
+     * @param bytes  number of bytes
+     */
+    public PiCounterCell(PiCounterCellId cellId, long packets, long bytes) {
+        this.cellId = cellId;
+        this.counterData = new PiCounterCellData(packets, bytes);
+    }
+
+    /**
+     * Returns the cell identifier.
+     *
+     * @return cell identifier
+     */
+    public PiCounterCellId cellId() {
+        return cellId;
+    }
+
+    /**
+     * Returns the data contained by this cell.
+     *
+     * @return counter cell data
+     */
+    public PiCounterCellData data() {
+        return counterData;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (!(o instanceof PiCounterCell)) {
+            return false;
+        }
+        PiCounterCell that = (PiCounterCell) o;
+        return Objects.equal(cellId, that.cellId) &&
+                Objects.equal(counterData, that.counterData);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hashCode(cellId, counterData);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this)
+                .add("cellId", cellId)
+                .add("counterData", counterData)
+                .toString();
+    }
+}
diff --git a/core/api/src/main/java/org/onosproject/net/pi/runtime/PiCounterCellData.java b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiCounterCellData.java
index f0aada5..b640970 100644
--- a/core/api/src/main/java/org/onosproject/net/pi/runtime/PiCounterCellData.java
+++ b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiCounterCellData.java
@@ -1,5 +1,5 @@
 /*
- * Copyright 2017-present Open Networking Foundation
+ * Copyright 2018-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.
@@ -23,37 +23,26 @@
 /**
  * Data of a counter cell of a protocol-independent pipeline.
  */
+
 @Beta
 public final class PiCounterCellData {
 
-    private final PiCounterCellId cellId;
     private final long packets;
     private final long bytes;
 
     /**
-     * Creates a new counter cell data for the given cell identifier, number of packets and bytes.
+     * Creates a new counter cell data for the given number of packets and bytes.
      *
-     * @param cellId  counter cell identifier
-     * @param packets number of packets
-     * @param bytes   number of bytes
+     * @param packets  number of packets
+     * @param bytes  number of bytes
      */
-    public PiCounterCellData(PiCounterCellId cellId, long packets, long bytes) {
-        this.cellId = cellId;
+    public PiCounterCellData(long packets, long bytes) {
         this.packets = packets;
         this.bytes = bytes;
     }
 
     /**
-     * Returns the cell identifier.
-     *
-     * @return cell identifier
-     */
-    public PiCounterCellId cellId() {
-        return cellId;
-    }
-
-    /**
-     * Returns the packet count value contained by this cell.
+     * Returns the packet count value contained by this counter data.
      *
      * @return number of packets
      */
@@ -62,7 +51,7 @@
     }
 
     /**
-     * Returns the byte count value contained by this cell.
+     * Returns the byte count value contained by this counter data.
      *
      * @return number of bytes
      */
@@ -80,19 +69,17 @@
         }
         PiCounterCellData that = (PiCounterCellData) o;
         return packets == that.packets &&
-                bytes == that.bytes &&
-                Objects.equal(cellId, that.cellId);
+                bytes == that.bytes;
     }
 
     @Override
     public int hashCode() {
-        return Objects.hashCode(cellId, packets, bytes);
+        return Objects.hashCode(packets, bytes);
     }
 
     @Override
     public String toString() {
         return MoreObjects.toStringHelper(this)
-                .add("cellId", cellId)
                 .add("packets", packets)
                 .add("bytes", bytes)
                 .toString();
diff --git a/core/api/src/main/java/org/onosproject/net/pi/runtime/PiTableEntry.java b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiTableEntry.java
index f8a1460..ac73bbe 100644
--- a/core/api/src/main/java/org/onosproject/net/pi/runtime/PiTableEntry.java
+++ b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiTableEntry.java
@@ -42,10 +42,11 @@
     private final long cookie;
     private final int priority;
     private final double timeout;
+    private final PiCounterCellData counterData;
 
     private PiTableEntry(PiTableId tableId, PiMatchKey matchKey,
                          PiTableAction tableAction, boolean isDefaultAction,
-                         long cookie, int priority, double timeout) {
+                         long cookie, int priority, double timeout, PiCounterCellData data) {
         this.tableId = tableId;
         this.matchKey = matchKey;
         this.tableAction = tableAction;
@@ -53,6 +54,7 @@
         this.cookie = cookie;
         this.priority = priority;
         this.timeout = timeout;
+        this.counterData = data;
     }
 
     /**
@@ -125,6 +127,18 @@
         return timeout == NO_TIMEOUT ? Optional.empty() : Optional.of(timeout);
     }
 
+    /**
+     * Returns the data of the counter cell associated with this table entry.
+     * This method is meaningful only if the table entry was read from the
+     * infrastructure device and the table has direct counters, otherwise
+     * returns null.
+     *
+     * @return counter cell data
+     */
+    public PiCounterCellData counter() {
+        return counterData;
+    }
+
     @Override
     public boolean equals(Object o) {
         if (this == o) {
@@ -197,6 +211,7 @@
         private long cookie = 0;
         private int priority = NO_PRIORITY;
         private double timeout = NO_TIMEOUT;
+        private PiCounterCellData counterData;
 
         private Builder() {
             // Hides constructor.
@@ -272,6 +287,17 @@
         }
 
         /**
+         * Sets the counter cell data of this table entry.
+         *
+         * @param data counter cell data
+         * @return this
+         */
+        public Builder withCounterCellData(PiCounterCellData data) {
+            this.counterData = checkNotNull(data, "Counter cell data cannot be null");
+            return this;
+        }
+
+        /**
          * Builds the table entry.
          *
          * @return a new table entry
@@ -281,7 +307,7 @@
             checkNotNull(matchKey);
             final boolean isDefaultAction = matchKey.equals(PiMatchKey.EMPTY);
             return new PiTableEntry(tableId, matchKey, tableAction,
-                                    isDefaultAction, cookie, priority, timeout);
+                                    isDefaultAction, cookie, priority, timeout, counterData);
         }
     }
 }
diff --git a/core/api/src/test/java/org/onosproject/net/pi/runtime/PiCounterCellDataTest.java b/core/api/src/test/java/org/onosproject/net/pi/runtime/PiCounterCellDataTest.java
index 6676470..bfb8782 100644
--- a/core/api/src/test/java/org/onosproject/net/pi/runtime/PiCounterCellDataTest.java
+++ b/core/api/src/test/java/org/onosproject/net/pi/runtime/PiCounterCellDataTest.java
@@ -1,5 +1,5 @@
 /*
- * Copyright 2017-present Open Networking Foundation
+ * Copyright 2018-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.
@@ -18,48 +18,25 @@
 
 import com.google.common.testing.EqualsTester;
 import org.junit.Test;
-import org.onosproject.net.pi.model.PiActionId;
-import org.onosproject.net.pi.model.PiTableId;
 
 import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
-import static org.onosproject.net.pi.runtime.PiConstantsTest.DROP;
 
 /**
- * Unit tests for PiCounterCellData class.
+ * Unit tests for PiCounterData class.
  */
 public class PiCounterCellDataTest {
 
-    private static final PiTableEntry PI_TABLE_ENTRY_1 = PiTableEntry.builder()
-            .forTable(PiTableId.of("T10"))
-            .withCookie(0xac)
-            .withPriority(10)
-            .withAction(PiAction.builder().withId(PiActionId.of(DROP)).build())
-            .withTimeout(100)
-            .build();
-    private static final PiTableEntry PI_TABLE_ENTRY_2 = PiTableEntry.builder()
-            .forTable(PiTableId.of("T20"))
-            .withCookie(0xac)
-            .withPriority(10)
-            .withAction(PiAction.builder().withId(PiActionId.of(DROP)).build())
-            .withTimeout(1000)
-            .build();
-
-    private static final PiCounterCellId PI_COUNTER_CELL_ID_1 =
-            PiCounterCellId.ofDirect(PI_TABLE_ENTRY_1);
-    private static final PiCounterCellId PI_COUNTER_CELL_ID_2 =
-            PiCounterCellId.ofDirect(PI_TABLE_ENTRY_2);
-
     private static final long PACKETS_1 = 10;
     private static final long PACKETS_2 = 20;
     private static final long BYTES_1 = 100;
     private static final long BYTES_2 = 200;
 
-    private static final PiCounterCellData PI_COUNTER_CELL_DATA_1 =
-            new PiCounterCellData(PI_COUNTER_CELL_ID_1, PACKETS_1, BYTES_1);
-    private static final PiCounterCellData SAME_AS_PI_COUNTER_CELL_DATA_1 =
-            new PiCounterCellData(PI_COUNTER_CELL_ID_1, PACKETS_1, BYTES_1);
-    private static final PiCounterCellData PI_COUNTER_CELL_DATA_2 =
-            new PiCounterCellData(PI_COUNTER_CELL_ID_2, PACKETS_2, BYTES_2);
+    private static final PiCounterCellData PI_COUNTER_DATA_1 =
+            new PiCounterCellData(PACKETS_1, BYTES_1);
+    private static final PiCounterCellData SAME_AS_PI_COUNTER_DATA_1 =
+            new PiCounterCellData(PACKETS_1, BYTES_1);
+    private static final PiCounterCellData PI_COUNTER_DATA_2 =
+            new PiCounterCellData(PACKETS_2, BYTES_2);
 
     /**
      * Checks that the PiCounterCellData class is immutable.
@@ -75,8 +52,8 @@
     @Test
     public void testEquals() {
         new EqualsTester()
-                .addEqualityGroup(PI_COUNTER_CELL_DATA_1, SAME_AS_PI_COUNTER_CELL_DATA_1)
-                .addEqualityGroup(PI_COUNTER_CELL_DATA_2)
+                .addEqualityGroup(PI_COUNTER_DATA_1, SAME_AS_PI_COUNTER_DATA_1)
+                .addEqualityGroup(PI_COUNTER_DATA_2)
                 .testEquals();
     }
 }
diff --git a/core/api/src/test/java/org/onosproject/net/pi/runtime/PiCounterCellTest.java b/core/api/src/test/java/org/onosproject/net/pi/runtime/PiCounterCellTest.java
new file mode 100644
index 0000000..ab65efe
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/net/pi/runtime/PiCounterCellTest.java
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2017-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.net.pi.runtime;
+
+import com.google.common.testing.EqualsTester;
+import org.junit.Test;
+import org.onosproject.net.pi.model.PiActionId;
+import org.onosproject.net.pi.model.PiTableId;
+
+import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
+import static org.onosproject.net.pi.runtime.PiConstantsTest.DROP;
+
+/**
+ * Unit tests for PiCounterCell class.
+ */
+public class PiCounterCellTest {
+
+    private static final PiTableEntry PI_TABLE_ENTRY_1 = PiTableEntry.builder()
+            .forTable(PiTableId.of("T10"))
+            .withCookie(0xac)
+            .withPriority(10)
+            .withAction(PiAction.builder().withId(PiActionId.of(DROP)).build())
+            .withTimeout(100)
+            .build();
+    private static final PiTableEntry PI_TABLE_ENTRY_2 = PiTableEntry.builder()
+            .forTable(PiTableId.of("T20"))
+            .withCookie(0xac)
+            .withPriority(10)
+            .withAction(PiAction.builder().withId(PiActionId.of(DROP)).build())
+            .withTimeout(1000)
+            .build();
+
+    private static final PiCounterCellId PI_COUNTER_CELL_ID_1 =
+            PiCounterCellId.ofDirect(PI_TABLE_ENTRY_1);
+    private static final PiCounterCellId PI_COUNTER_CELL_ID_2 =
+            PiCounterCellId.ofDirect(PI_TABLE_ENTRY_2);
+
+    private static final long PACKETS_1 = 10;
+    private static final long PACKETS_2 = 20;
+    private static final long BYTES_1 = 100;
+    private static final long BYTES_2 = 200;
+
+    private static final PiCounterCell PI_COUNTER_CELL_1 =
+            new PiCounterCell(PI_COUNTER_CELL_ID_1, PACKETS_1, BYTES_1);
+    private static final PiCounterCell SAME_AS_PI_COUNTER_CELL_1 =
+            new PiCounterCell(PI_COUNTER_CELL_ID_1, PACKETS_1, BYTES_1);
+    private static final PiCounterCell PI_COUNTER_CELL_2 =
+            new PiCounterCell(PI_COUNTER_CELL_ID_2, PACKETS_2, BYTES_2);
+
+    /**
+     * Checks that the PiCounterCell class is immutable.
+     */
+    @Test
+    public void testImmutability() {
+        assertThatClassIsImmutable(PiCounterCell.class);
+    }
+
+    /**
+     * Checks the operation of equals(), hashCode() and toString() methods.
+     */
+    @Test
+    public void testEquals() {
+        new EqualsTester()
+                .addEqualityGroup(PI_COUNTER_CELL_1, SAME_AS_PI_COUNTER_CELL_1)
+                .addEqualityGroup(PI_COUNTER_CELL_2)
+                .testEquals();
+    }
+}