VOL-2014: Rebase bal30-dev branch with master and re-verify sanity with SEBA2.0 and VOLTHA2.x before merge to master
          Squashed all commits from bal30-dev branch to master branch as a single commit.
          Bumped version to 2.0.0-dev .
          Update README.md with the required ONL version for BAL3.1 .

VOL-2130: Wrong queue id assigned for downstream queue on tm_sched
          for the subscriber when using single gem port.
          Use queue_id as 0 when using single gem port.

          Issues need to be taken care:
          VOL-1580: Support reporting ONU Registration ID as part of ONU Activation Indication
          - commented the code developed for VOL-1580 to fetch ONU registration ID as it uses BAL2.6 specific API's.
          - Need to check how the same can be achieved using BAL3.x API's to fetch ONU registration ID

Set .gitreview defaultbranch to bal30-dev
Rebase change - Removed .gitreview defaultbranch so that by default it will point to master

Change-Id: Ib57e52e98cf09f4fa3dbaa9156a4375b7dc6ee43

Rebase: [VOL-1635] [BAL3.0 Bridade] OLT Activation [VOL-1636] [BAL3.0 Bridade] ONU Discovery,
        This OpenOLT agent supports the BAL V3.0.3.3 SDK. OLT/ONU activation from voltha-1.7 is successful

Change-Id: I860b31317172139cd4c4244a93e79e0c3ce4fdf8

Rebase: [VOL-1685][BAL3.0 Bridade] generate OpenOLT debian package file

Change-Id: Ib0332d948602bf7892b6aae91a4147356237f7e2

Rebase: [VOL-1685][BAL3.0 Brigade] generate OpenOLT debian package file
missing copy shell files (dev_mgmt_daemon and openolt).

Change-Id: Id29dc24be4d1d5fc12a26884e5b5a69de91fbd69

Rebase: [VOL-1685][BAL3.0 Brigade] generate OpenOLT debian package file

Change-Id: Ic5453616c0dbdbc1fb1eb241feba1cbfca5e8e85

Rebase: [VOL-1852][BAL3.0 Brigade] Support Edge-core OLT ASFvOLT16 (XGS-PON) and
ASGvOLT64 (GPON) devices

Change-Id: I316d35bc76c38b5381e8896148b4ee5cebbd1099

VOL-1850: Fix packet duplication with TM Queue Mapping profile
          Fix for US and DS scheduler ID allocation irrespective of device type(asfvolt16/asfvolt64)
          README.md file update with BAL3.1.1.1 specifications

Change-Id: I255bf7a4ff35771e9c628bb822ba2190a1fcd5ed

Rebase: [VOL-1787] : This commit sets up a unit-test framework for openolt
                     agent based on gtest, gmock and c-mock utilities.
                     A sample unit-test case to test EnableOlt success case is also
                     added. More test cases will be added in future commit.

Change-Id: If020be489a04d97df5fcbc15e9207deeee2fcfac

Rebase: [VOL-2040]: Patch the macro required by core.cc into bcmos_system.h file

Change-Id: Ic841785b2ce451b1acaf007074b8c458687bc753

Rebase: [VOL-2043] : OLT Enable Test Case

        - Added more test cases for OLT Enable. Now covers all scenarios.
        - Fixed typos in unit test README.md

Change-Id: Ib931d89cedee78fe40ca67761a028365a047ebc3

Rebase: VOL-1941: Use protobufs from the voltha-protos repo, not a copy

        - Use protobufs from the voltha-protos repo, not a copy
        - Provided option to specify the openolt proto version via make option
        - Removed duplicate README.md

Change-Id: Id67714c8119521f8358aaa9245268c47b7e843cf
diff --git a/agent/src/stats_collection.cc b/agent/src/stats_collection.cc
index 7bdde4c..62213c2 100644
--- a/agent/src/stats_collection.cc
+++ b/agent/src/stats_collection.cc
@@ -19,7 +19,6 @@
 
 #include <unistd.h>
 
-#include <openolt.grpc.pb.h>
 #include "indications.h"
 #include "core.h"
 #include "translation.h"
@@ -27,18 +26,18 @@
 extern "C"
 {
 #include <bcmos_system.h>
-#include <bal_api.h>
-#include <bal_api_end.h>
-#include <flow_fsm.h>
+#include <bcmolt_api.h>
+#include <bcmolt_api_model_api_structs.h>
 }
 
 //FIXME
 #define FLOWS_COUNT 100
 
-bcmbal_flow_key* flows_keys = new bcmbal_flow_key[FLOWS_COUNT];
+bcmolt_flow_key* flows_keys = new bcmolt_flow_key[FLOWS_COUNT];
+bcmolt_odid device_id = 0;    
 
 void init_stats() {
-    memset(flows_keys, 0, FLOWS_COUNT * sizeof(bcmbal_flow_key));
+    memset(flows_keys, 0, FLOWS_COUNT * sizeof(bcmolt_flow_key));
 }
 
 openolt::PortStatistics* get_default_port_statistics() {
@@ -75,50 +74,110 @@
 }
 #endif
 
-openolt::PortStatistics* collectPortStatistics(bcmbal_interface_key key) {
-
-    bcmos_errno err;
-    bcmbal_interface_stat stat;     /**< declare main API struct */
-    bcmos_bool clear_on_read = false;
+openolt::PortStatistics* collectPortStatistics(bcmolt_intf_ref intf_ref) {
 
     openolt::PortStatistics* port_stats = get_default_port_statistics();
+#ifndef TEST_MODE
+    bcmos_errno err;
+    bcmolt_stat_flags clear_on_read = BCMOLT_STAT_FLAGS_NONE;
+    bcmolt_nni_interface_stats nni_stats;
+    bcmolt_onu_itu_pon_stats pon_stats;
+    bcmolt_pon_interface_itu_pon_stats itu_pon_stats;
 
-    /* init the API struct */
-    BCMBAL_STAT_INIT(&stat, interface, key);
-    BCMBAL_STAT_PROP_GET(&stat, interface, all_properties);
 
-    /* call API */
-    err = bcmbal_stat_get(DEFAULT_ATERM_ID, &stat.hdr, clear_on_read);
-    if (err == BCM_ERR_OK)
-    {
-        //std::cout << "Interface statistics retrieved"
-        //          << " intf_id:" << intf_id << std::endl;
+    switch (intf_ref.intf_type) {
+        case BCMOLT_INTERFACE_TYPE_NNI:
+        {
+            bcmolt_nni_interface_key nni_intf_key;
+            nni_intf_key.id = intf_ref.intf_id;
+            /* init the API struct */
+            BCMOLT_STAT_INIT(&nni_stats, nni_interface, stats, nni_intf_key);
+            BCMOLT_MSG_FIELD_GET(&nni_stats, rx_bytes);
+            BCMOLT_MSG_FIELD_GET(&nni_stats, rx_packets);
+            BCMOLT_MSG_FIELD_GET(&nni_stats, rx_ucast_packets);
+            BCMOLT_MSG_FIELD_GET(&nni_stats, rx_mcast_packets);
+            BCMOLT_MSG_FIELD_GET(&nni_stats, rx_bcast_packets);
+            BCMOLT_MSG_FIELD_GET(&nni_stats, rx_error_packets);
+            BCMOLT_MSG_FIELD_GET(&nni_stats, tx_bytes);
+            BCMOLT_MSG_FIELD_GET(&nni_stats, tx_packets);
+            BCMOLT_MSG_FIELD_GET(&nni_stats, tx_ucast_packets);
+            BCMOLT_MSG_FIELD_GET(&nni_stats, tx_mcast_packets);
+            BCMOLT_MSG_FIELD_GET(&nni_stats, tx_bcast_packets);
+            BCMOLT_MSG_FIELD_GET(&nni_stats, tx_error_packets);
 
-        port_stats->set_rx_bytes(stat.data.rx_bytes);
-        port_stats->set_rx_packets(stat.data.rx_packets);
-        port_stats->set_rx_ucast_packets(stat.data.rx_ucast_packets);
-        port_stats->set_rx_mcast_packets(stat.data.rx_mcast_packets);
-        port_stats->set_rx_bcast_packets(stat.data.rx_bcast_packets);
-        port_stats->set_rx_error_packets(stat.data.rx_error_packets);
-        port_stats->set_tx_bytes(stat.data.tx_bytes);
-        port_stats->set_tx_packets(stat.data.tx_packets);
-        port_stats->set_tx_ucast_packets(stat.data.tx_ucast_packets);
-        port_stats->set_tx_mcast_packets(stat.data.tx_mcast_packets);
-        port_stats->set_tx_bcast_packets(stat.data.tx_bcast_packets);
-        port_stats->set_tx_error_packets(stat.data.tx_error_packets);
-        port_stats->set_rx_crc_errors(stat.data.rx_crc_errors);
-        port_stats->set_bip_errors(stat.data.bip_errors);
+            /* call API */
+            err = bcmolt_stat_get((bcmolt_oltid)device_id, &nni_stats.hdr, clear_on_read);
+            if (err == BCM_ERR_OK)
+            {
+                //std::cout << "Interface statistics retrieved"
+                //          << " intf_id:" << intf_id << std::endl;
+            
+                port_stats->set_rx_bytes(nni_stats.data.rx_bytes);
+                port_stats->set_rx_packets(nni_stats.data.rx_packets);
+                port_stats->set_rx_ucast_packets(nni_stats.data.rx_ucast_packets);
+                port_stats->set_rx_mcast_packets(nni_stats.data.rx_mcast_packets);
+                port_stats->set_rx_bcast_packets(nni_stats.data.rx_bcast_packets);
+                port_stats->set_rx_error_packets(nni_stats.data.rx_error_packets);
+                port_stats->set_tx_bytes(nni_stats.data.tx_bytes);
+                port_stats->set_tx_packets(nni_stats.data.tx_packets);
+                port_stats->set_tx_ucast_packets(nni_stats.data.tx_ucast_packets);
+                port_stats->set_tx_mcast_packets(nni_stats.data.tx_mcast_packets);
+                port_stats->set_tx_bcast_packets(nni_stats.data.tx_bcast_packets);
+                port_stats->set_tx_error_packets(nni_stats.data.tx_error_packets);
+            
+            } else {
+                OPENOLT_LOG(ERROR, openolt_log_id,  "Failed to retrieve port statistics, intf_id %d, intf_type %d\n",
+                    (int)intf_ref.intf_id, (int)intf_ref.intf_type);
+            }
+            break;
+        }
+        case BCMOLT_INTERFACE_TYPE_PON:
+        {
+            bcmolt_pon_interface_key key;
+            key.pon_ni = (bcmolt_interface)intf_ref.intf_id;
+            BCMOLT_STAT_INIT(&itu_pon_stats, pon_interface, itu_pon_stats, key);
+            BCMOLT_MSG_FIELD_GET(&itu_pon_stats, tx_packets);
+            BCMOLT_MSG_FIELD_GET(&itu_pon_stats, bip_errors);
+            BCMOLT_MSG_FIELD_GET(&itu_pon_stats, rx_crc_error);
 
-    } else {
-        BCM_LOG(ERROR, openolt_log_id,  "Failed to retrieve port statistics, intf_id %d, intf_type %d\n",
-            (int)key.intf_id, (int)key.intf_type);
-    }
+            /* call API */
+            err = bcmolt_stat_get((bcmolt_oltid)device_id, &itu_pon_stats.hdr, clear_on_read);
+            if (err == BCM_ERR_OK) {
+                port_stats->set_tx_packets(itu_pon_stats.data.tx_packets);
+                port_stats->set_bip_errors(itu_pon_stats.data.bip_errors);
+                port_stats->set_rx_crc_errors(itu_pon_stats.data.rx_crc_error);
+            } else {
+                OPENOLT_LOG(ERROR, openolt_log_id,  "Failed to retrieve port statistics, intf_id %d, intf_type %d, err %d\n",
+                    (int)intf_ref.intf_id, (int)intf_ref.intf_type, err);
+            }
+            {
+                bcmolt_onu_key key;
+                key.pon_ni = (bcmolt_interface)intf_ref.intf_id;
+                BCMOLT_STAT_INIT(&pon_stats, onu, itu_pon_stats, key);
+                BCMOLT_MSG_FIELD_GET(&pon_stats, rx_bytes);
+                BCMOLT_MSG_FIELD_GET(&pon_stats, rx_packets);
+                BCMOLT_MSG_FIELD_GET(&pon_stats, tx_bytes);
 
-    port_stats->set_intf_id(interface_key_to_port_no(key));
+                /* call API */
+                err = bcmolt_stat_get((bcmolt_oltid)device_id, &pon_stats.hdr, clear_on_read);
+                if (err == BCM_ERR_OK) {
+                    port_stats->set_rx_bytes(pon_stats.data.rx_bytes);
+                    port_stats->set_rx_packets(pon_stats.data.rx_packets);
+                    port_stats->set_tx_bytes(pon_stats.data.tx_bytes);
+                } else {
+                    OPENOLT_LOG(ERROR, openolt_log_id,  "Failed to retrieve port statistics, intf_id %d, intf_type %d, err %d\n",
+                        (int)intf_ref.intf_id, (int)intf_ref.intf_type, err);
+                }
+            }
+            break;
+        }
+    } 
+
+    port_stats->set_intf_id(interface_key_to_port_no((bcmolt_interface_id)intf_ref.intf_id, (bcmolt_interface_type)intf_ref.intf_type));
     time_t now;
     time(&now);
     port_stats->set_timestamp((int)now);
-
+#endif
     return port_stats;
 
 }
@@ -167,26 +226,27 @@
 void stats_collection() {
 
     if (!state.is_connected()) {
-        BCM_LOG(INFO, openolt_log_id, "Voltha is not connected, do not collect stats\n");
+        OPENOLT_LOG(INFO, openolt_log_id, "Voltha is not connected, do not collect stats\n");
         return;
     }
     if (!state.is_activated()) {
-        BCM_LOG(INFO, openolt_log_id, "The OLT is not up, do not collect stats\n");
+        OPENOLT_LOG(INFO, openolt_log_id, "The OLT is not up, do not collect stats\n");
         return;
     }
 
 
-    BCM_LOG(DEBUG, openolt_log_id, "Collecting statistics\n");
+    OPENOLT_LOG(DEBUG, openolt_log_id, "Collecting statistics\n");
 
     //Ports statistics
 
     //Uplink ports
     for (int i = 0; i < NumNniIf_(); i++) {
-        bcmbal_interface_key nni_interface;
-        nni_interface.intf_type = BCMBAL_INTF_TYPE_NNI;
-        nni_interface.intf_id = i;
+        bcmolt_intf_ref intf_ref;
+        intf_ref.intf_type = BCMOLT_INTERFACE_TYPE_NNI;
+        intf_ref.intf_id = i;
 
-        openolt::PortStatistics* port_stats = collectPortStatistics(nni_interface);
+        openolt::PortStatistics* port_stats =
+            collectPortStatistics(intf_ref);
 
         openolt::Indication ind;
         ind.set_allocated_port_stats(port_stats);
@@ -194,11 +254,12 @@
     }
     //Pon ports
     for (int i = 0; i < NumPonIf_(); i++) {
-        bcmbal_interface_key pon_interface;
-        pon_interface.intf_type = BCMBAL_INTF_TYPE_PON;
-        pon_interface.intf_id = i;
+        bcmolt_intf_ref intf_ref;
+        intf_ref.intf_type = BCMOLT_INTERFACE_TYPE_PON;
+        intf_ref.intf_id = i;
 
-        openolt::PortStatistics* port_stats = collectPortStatistics(pon_interface);
+        openolt::PortStatistics* port_stats = 
+            collectPortStatistics(intf_ref);
 
         openolt::Indication ind;
         ind.set_allocated_port_stats(port_stats);