CORD-13:Table Statistics support along with CLI and REST

Change-Id: Ic7facc73754c4b1e7c9c5a9eecd217f89a67e135
diff --git a/core/api/src/main/java/org/onosproject/net/flow/DefaultTableStatisticsEntry.java b/core/api/src/main/java/org/onosproject/net/flow/DefaultTableStatisticsEntry.java
new file mode 100644
index 0000000..929b285
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/flow/DefaultTableStatisticsEntry.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * 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.flow;
+
+import org.onosproject.net.DeviceId;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Default implementation of table statistics entry interface.
+ */
+public final class DefaultTableStatisticsEntry implements TableStatisticsEntry {
+
+    private final DeviceId deviceId;
+    private final int tableId;
+    private final long activeFlowEntries;
+    private final long packetsLookedupCount;
+    private final long packetsMatchedCount;
+
+    /**
+     * Default table statistics constructor.
+     *
+     * @param deviceId device identifier
+     * @param tableId table identifier
+     * @param activeFlowEntries number of active flow entries in the table
+     * @param packetsLookedupCount number of packets looked up in table
+     * @param packetsMatchedCount number of packets that hit table
+     */
+    public DefaultTableStatisticsEntry(DeviceId deviceId,
+                                  int  tableId,
+                                  long activeFlowEntries,
+                                  long packetsLookedupCount,
+                                  long packetsMatchedCount) {
+        this.deviceId = checkNotNull(deviceId);
+        this.tableId = tableId;
+        this.activeFlowEntries = activeFlowEntries;
+        this.packetsLookedupCount = packetsLookedupCount;
+        this.packetsMatchedCount = packetsMatchedCount;
+    }
+
+    @Override
+    public String toString() {
+        StringBuilder sb = new StringBuilder("device: " + deviceId + ", ");
+
+        sb.append("tableId: " + this.tableId + ", ");
+        sb.append("activeEntries: " + this.activeFlowEntries + ", ");
+        sb.append("packetsLookedUp: " + this.packetsLookedupCount + ", ");
+        sb.append("packetsMatched: " + this.packetsMatchedCount);
+
+        return sb.toString();
+    }
+
+    @Override
+    public int tableId() {
+        return tableId;
+    }
+
+    @Override
+    public long activeFlowEntries() {
+        return activeFlowEntries;
+    }
+
+    @Override
+    public long packetsLookedup() {
+        return packetsLookedupCount;
+    }
+
+    @Override
+    public long packetsMatched() {
+        return packetsMatchedCount;
+    }
+
+    @Override
+    public DeviceId deviceId() {
+        return deviceId;
+    }
+}
diff --git a/core/api/src/main/java/org/onosproject/net/flow/FlowRuleProviderService.java b/core/api/src/main/java/org/onosproject/net/flow/FlowRuleProviderService.java
index 48aa504..aefa96b 100644
--- a/core/api/src/main/java/org/onosproject/net/flow/FlowRuleProviderService.java
+++ b/core/api/src/main/java/org/onosproject/net/flow/FlowRuleProviderService.java
@@ -15,6 +15,8 @@
  */
 package org.onosproject.net.flow;
 
+import java.util.List;
+
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.provider.ProviderService;
 
@@ -50,6 +52,15 @@
     void pushFlowMetricsWithoutFlowMissing(DeviceId deviceId, Iterable<FlowEntry> flowEntries);
 
     /**
+     * Pushes the collection of table statistics entries currently extracted
+     * from the given device.
+     *
+     * @param deviceId device identifier
+     * @param tableStatsEntries collection of flow table statistics entries
+     */
+    void pushTableStatistics(DeviceId deviceId, List<TableStatisticsEntry> tableStatsEntries);
+
+    /**
      * Indicates to the core that the requested batch operation has
      * been completed.
      *
@@ -57,5 +68,4 @@
      * @param operation the resulting outcome of the operation
      */
     void batchOperationCompleted(long batchId, CompletedBatchOperation operation);
-
 }
diff --git a/core/api/src/main/java/org/onosproject/net/flow/FlowRuleService.java b/core/api/src/main/java/org/onosproject/net/flow/FlowRuleService.java
index d4f959c..ee8d5a9 100644
--- a/core/api/src/main/java/org/onosproject/net/flow/FlowRuleService.java
+++ b/core/api/src/main/java/org/onosproject/net/flow/FlowRuleService.java
@@ -104,4 +104,11 @@
      */
     void apply(FlowRuleOperations ops);
 
+    /**
+     * Returns the collection of flow table statistics of the specified device.
+     *
+     * @param deviceId device identifier
+     * @return collection of flow table statistics
+     */
+    Iterable<TableStatisticsEntry> getFlowTableStatistics(DeviceId deviceId);
 }
diff --git a/core/api/src/main/java/org/onosproject/net/flow/FlowRuleStore.java b/core/api/src/main/java/org/onosproject/net/flow/FlowRuleStore.java
index cece989..d81c73c 100644
--- a/core/api/src/main/java/org/onosproject/net/flow/FlowRuleStore.java
+++ b/core/api/src/main/java/org/onosproject/net/flow/FlowRuleStore.java
@@ -15,6 +15,8 @@
  */
 package org.onosproject.net.flow;
 
+import java.util.List;
+
 import org.onosproject.net.DeviceId;
 import org.onosproject.store.Store;
 
@@ -93,4 +95,23 @@
      * @return flow_removed event, or null if nothing removed
      */
     FlowRuleEvent removeFlowRule(FlowEntry rule);
+
+    /**
+     * Updates the flow table statistics of the specified device using
+     * the given statistics.
+     *
+     * @param deviceId    device identifier
+     * @param tableStats   list of table statistics
+     * @return ready to send event describing what occurred;
+     */
+    FlowRuleEvent updateTableStatistics(DeviceId deviceId,
+                                        List<TableStatisticsEntry> tableStats);
+
+    /**
+     * Returns the flow table statistics associated with a device.
+     *
+     * @param deviceId the device ID
+     * @return the flow table statistics
+     */
+    Iterable<TableStatisticsEntry> getTableStatistics(DeviceId deviceId);
 }
diff --git a/core/api/src/main/java/org/onosproject/net/flow/TableStatisticsEntry.java b/core/api/src/main/java/org/onosproject/net/flow/TableStatisticsEntry.java
new file mode 100644
index 0000000..563f31c
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/flow/TableStatisticsEntry.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * 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.flow;
+
+import org.onosproject.net.DeviceId;
+
+/**
+ * Interface for flow table statistics of a device.
+ */
+public interface TableStatisticsEntry {
+
+    /**
+     * Returns the device Id.
+     *
+     * @return device id
+     */
+    DeviceId  deviceId();
+
+    /**
+     * Returns the table number.
+     *
+     * @return table number
+     */
+    int  tableId();
+
+    /**
+     * Returns the number of active flow entries in this table.
+     *
+     * @return the number of active flow entries
+     */
+    long activeFlowEntries();
+
+    /**
+     * Returns the number of packets looked up in the table.
+     *
+     * @return the number of packets looked up in the table
+     */
+    long packetsLookedup();
+
+    /**
+     * Returns the number of packets that successfully matched in the table.
+     *
+     * @return the number of packets that successfully matched in the table
+     */
+    long packetsMatched();
+}