[VOL-2061] OpenOLT should wait till BAL is ready before it can configure
the PON schedulers
Fixed unit test cases per the new code.

Change-Id: I51e49ee57b64d62558b3c9f0faa8fe8653916a6b
diff --git a/.gitignore b/.gitignore
index 90f483f..1ccc2a4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -23,13 +23,13 @@
 **/bal-api-3.1.0
 
 # Test binary
-test_openolt
-
-# proto files
-protos/voltha_protos/*.proto
+**/test_openolt
 
 # Test report
 **/test_openolt_report.xml
 
+# Downloaded voltha-protos file
+voltha_protos
+
 # IntelliJ Files
 .idea
diff --git a/agent/common/core.h b/agent/common/core.h
index 1caf3cc..26f1dac 100644
--- a/agent/common/core.h
+++ b/agent/common/core.h
@@ -107,9 +107,11 @@
 Status RemoveTrafficQueues_(const tech_profile::TrafficQueues *traffic_queues);
 uint32_t GetPortNum_(uint32_t flow_id);
 int get_status_bcm_cli_quit(void);
-uint16_t get_dev_id(void); 
+uint16_t get_dev_id(void);
 Status pushOltOperInd(uint32_t intf_id, const char *type, const char *state);
 uint64_t get_flow_status(uint16_t flow_id, uint16_t flow_type, uint16_t data_id);
 
 void stats_collection();
+Status check_connection();
+Status check_bal_ready();
 #endif
diff --git a/agent/scripts/init.d/openolt b/agent/scripts/init.d/openolt
index 1db4fbb..70594b1 100755
--- a/agent/scripts/init.d/openolt
+++ b/agent/scripts/init.d/openolt
@@ -25,9 +25,6 @@
 
 start() {
   printf "Starting '$NAME'... "
-  # TODO: If openolt agent has a way to figure out progamatically
-  # if the BAL is ready, the below timer may not be needed.
-  sleep 120
   rm -f /var/log/$NAME.log
   touch /var/log/$NAME.log
   start-stop-daemon --verbose --start --chuid "$USER:$GROUP" --background --no-close --make-pidfile --pidfile /var/run/$NAME.pid --chdir "$APPDIR" --exec "$APPBIN" -- $APPARGS < /dev/tty1 >> /var/log/$NAME.log 2>&1 || true
diff --git a/agent/src/core.cc b/agent/src/core.cc
index ef92547..897d326 100644
--- a/agent/src/core.cc
+++ b/agent/src/core.cc
@@ -25,6 +25,7 @@
 #include <thread>
 #include <bitset>
 #include <inttypes.h>
+#include <unistd.h>
 
 #include "device.h"
 #include "core.h"
@@ -161,6 +162,9 @@
     return dev_id;
 }
 
+// Stubbed defntion of bcmolt_cfg_get required for unit-test
+extern bcmos_errno bcmolt_cfg_get_stub(bcmolt_oltid olt_id, void* ptr);
+
 /**
 * Returns the default NNI (Upstream direction) or PON (Downstream direction) scheduler
 * Every NNI port and PON port have default scheduler.
@@ -782,8 +786,11 @@
         bcmos_fastlock_init(&data_lock, 0);
         OPENOLT_LOG(INFO, openolt_log_id, "Enable OLT - %s-%s\n", VENDOR_ID, MODEL_ID);
 
-        if (bcmolt_api_conn_mgr_is_connected(dev_id))
-        {
+        //check BCM daemon is connected or not
+        Status status = check_connection();
+        if (!status.ok())
+            return status;
+        else {
             Status status = SubscribeIndication();
             if (!status.ok()) {
                 OPENOLT_LOG(ERROR, openolt_log_id, "SubscribeIndication failed - %s : %s\n",
@@ -791,6 +798,14 @@
                     status.error_message().c_str());
                 return status;
             }
+
+            //check BAL state in initial stage
+            status = check_bal_ready();
+            if (!status.ok())
+                return status;
+        }
+
+        {
             bcmos_errno err;
             bcmolt_odid dev;
             OPENOLT_LOG(INFO, openolt_log_id, "Enabling PON %d Devices ... \n", BCM_MAX_DEVS_PER_LINE_CARD);
@@ -801,7 +816,7 @@
                 BCMOLT_CFG_INIT(&dev_cfg, device, dev_key);
                 BCMOLT_MSG_FIELD_GET(&dev_cfg, system_mode);
                 err = bcmolt_cfg_get(dev_id, &dev_cfg.hdr);
-                if (err == BCM_ERR_NOT_CONNECTED) { 
+                if (err == BCM_ERR_NOT_CONNECTED) {
                     bcmolt_device_key key = {.device_id = dev};
                     bcmolt_device_connect oper;
                     BCMOLT_OPER_INIT(&oper, device, connect, key);
@@ -1148,7 +1163,7 @@
     err = get_pon_interface_status((bcmolt_interface)intf_id, &state); 
     if (err == BCM_ERR_OK) {
         if (state == BCMOLT_INTERFACE_STATE_ACTIVE_WORKING) {
-            OPENOLT_LOG(INFO, openolt_log_id, "PON interface: %d already enabled\n", intf_id);
+            OPENOLT_LOG(WARNING, openolt_log_id, "PON interface: %d already enabled\n", intf_id);
             return Status::OK;
         }
     } 
@@ -1347,7 +1362,7 @@
     err = get_nni_interface_status((bcmolt_interface)intf_id, &state); 
     if (err == BCM_ERR_OK) {
         if (set_state && state == BCMOLT_INTERFACE_STATE_ACTIVE_WORKING) {
-            OPENOLT_LOG(INFO, openolt_log_id, "NNI interface: %d already enabled\n", intf_id);
+            OPENOLT_LOG(WARNING, openolt_log_id, "NNI interface: %d already enabled\n", intf_id);
             OPENOLT_LOG(INFO, openolt_log_id, "Initializing tm sched creation for NNI interface: %d\n", intf_id);
             CreateDefaultSched(intf_id, upstream);
             CreateDefaultQueue(intf_id, upstream);
@@ -2190,6 +2205,19 @@
     bcmolt_tm_sched_key tm_sched_key = {.id = 1};
     tm_sched_key.id = get_default_tm_sched_id(intf_id, direction);
 
+    //check TM scheduler has configured or not
+    BCMOLT_CFG_INIT(&tm_sched_cfg, tm_sched, tm_sched_key);
+    BCMOLT_MSG_FIELD_GET(&tm_sched_cfg, state);
+    err = bcmolt_cfg_get(dev_id, &tm_sched_cfg.hdr);
+    if (err) {
+        OPENOLT_LOG(ERROR, openolt_log_id, "cfg: Failed to query TM scheduler\n");
+        return err;
+    }
+    else if (tm_sched_cfg.data.state == BCMOLT_CONFIG_STATE_CONFIGURED) {
+        OPENOLT_LOG(WARNING, openolt_log_id, "tm scheduler default config has already with id %d\n", tm_sched_key.id);
+        return BCM_ERR_OK;
+    }
+
     // bcmbal_tm_sched_owner
     BCMOLT_CFG_INIT(&tm_sched_cfg, tm_sched, tm_sched_key);
 
@@ -2799,3 +2827,49 @@
     }
     return Status::OK;
 }
+
+Status check_connection() {
+    int maxTrials = 60;
+    while (!bcmolt_api_conn_mgr_is_connected(dev_id)) {
+        sleep(1);
+        if (--maxTrials == 0)
+            return grpc::Status(grpc::StatusCode::UNAVAILABLE, "check connection failed");
+        else
+            OPENOLT_LOG(INFO, openolt_log_id, "waiting for daemon connection ...\n");
+    }
+    OPENOLT_LOG(INFO, openolt_log_id, "daemon is connected\n");
+    return Status::OK;
+}
+
+Status check_bal_ready() {
+    bcmos_errno err;
+    int maxTrials = 30;
+    bcmolt_olt_cfg olt_cfg = { };
+    bcmolt_olt_key olt_key = { };
+
+    BCMOLT_CFG_INIT(&olt_cfg, olt, olt_key);
+    BCMOLT_MSG_FIELD_GET(&olt_cfg, bal_state);
+
+    while (olt_cfg.data.bal_state != BCMOLT_BAL_STATE_BAL_AND_SWITCH_READY) {
+        if (--maxTrials == 0)
+            return grpc::Status(grpc::StatusCode::UNAVAILABLE, "check bal ready failed");
+        sleep(5);
+        #ifdef TEST_MODE
+        // It is impossible to mock the setting of olt_cfg.data.bal_state because
+        // the actual bcmolt_cfg_get passes the address of olt_cfg.hdr and we cannot
+        // set the olt_cfg.data.bal_state. So a new stub function is created and address
+        // of olt_cfg is passed. This is one-of case where we need to add specific
+        // code in product code.
+        if (bcmolt_cfg_get_stub(dev_id, &olt_cfg)) {
+        #else
+        if (bcmolt_cfg_get(dev_id, &olt_cfg.hdr)) {
+        #endif
+            continue;
+        }
+        else
+            OPENOLT_LOG(INFO, openolt_log_id, "waiting for BAL ready ...\n");
+    }
+
+    OPENOLT_LOG(INFO, openolt_log_id, "BAL is ready\n");
+    return Status::OK;
+}
diff --git a/agent/src/indications.cc b/agent/src/indications.cc
index 1826b77..cc42adc 100644
--- a/agent/src/indications.cc
+++ b/agent/src/indications.cc
@@ -145,7 +145,6 @@
 static void OltOperIndication(bcmolt_devid olt, bcmolt_msg *msg) {
     openolt::Indication ind;
     openolt::OltIndication* olt_ind = new openolt::OltIndication;
-    Status status;
     std::string admin_state;
 
     switch (msg->subgroup) {
diff --git a/agent/test/Makefile.in b/agent/test/Makefile.in
index 2122266..3f8eaf9 100644
--- a/agent/test/Makefile.in
+++ b/agent/test/Makefile.in
@@ -79,7 +79,7 @@
 	sudo make -C $(GRPC_DST) install
 	sudo ldconfig
 
-prereq-mock-lib: gtest cmock
+prereq-mock-lib: gtest cmock gmock-global
 
 gtest:
 	# Install gtest and gmock
@@ -96,6 +96,11 @@
 	make -C /tmp/C-Mock
 	sudo make -C /tmp/C-Mock install
 
+gmock-global:
+	# Install gmock-global
+	rm -rf /tmp/gmock-global && cd /tmp && git clone https://github.com/apriorit/gmock-global.git
+	cd /tmp/gmock-global && git checkout 1.0.2
+	sudo cp -rf /tmp/gmock-global/include/gmock-global /usr/local/include/
 ########################################################################
 ##
 ##
diff --git a/agent/test/inc/bal_mocker.h b/agent/test/inc/bal_mocker.h
index 85bdb22..72cbbc4 100644
--- a/agent/test/inc/bal_mocker.h
+++ b/agent/test/inc/bal_mocker.h
@@ -18,7 +18,10 @@
 #ifndef __BAL_MOCKER_H__
 #define __BAL_MOCKER_H__
 #include <cmock/cmock.h>
+#include <gmock-global/gmock-global.h>
 #include <cstdlib>
+#include <grpc++/grpc++.h>
+using grpc::Status;
 extern "C" {
 #include <bcmos_system.h>
 #include <bcmolt_api.h>
@@ -41,5 +44,7 @@
     MOCK_METHOD1(bcmolt_host_init, bcmos_errno(bcmolt_host_init_parms*));
     MOCK_METHOD2(bcmolt_cfg_get, bcmos_errno(bcmolt_oltid, bcmolt_cfg*));
     MOCK_METHOD2(bcmolt_oper_submit, bcmos_errno(bcmolt_oltid, bcmolt_oper*));
+  // Add more here
 };
+
 #endif
diff --git a/agent/test/src/bal_mocker.cc b/agent/test/src/bal_mocker.cc
index b38906f..1f9d4b0 100644
--- a/agent/test/src/bal_mocker.cc
+++ b/agent/test/src/bal_mocker.cc
@@ -23,3 +23,4 @@
 CMOCK_MOCK_FUNCTION2(BalMocker, bcmolt_cfg_get, bcmos_errno(bcmolt_oltid, bcmolt_cfg*));
 CMOCK_MOCK_FUNCTION2(BalMocker, bcmolt_oper_submit, bcmos_errno(bcmolt_oltid, bcmolt_oper*));
 }
+
diff --git a/agent/test/src/main_test.cc b/agent/test/src/main_test.cc
index 7f04e9c..8e3a903 100644
--- a/agent/test/src/main_test.cc
+++ b/agent/test/src/main_test.cc
@@ -17,7 +17,9 @@
 
 #include "gtest/gtest.h"
 
+using namespace testing;
+
 int main(int argc, char **argv) {
-  ::testing::InitGoogleTest(&argc, argv);
+  InitGoogleTest(&argc, argv);
   return RUN_ALL_TESTS();
 }
diff --git a/agent/test/src/test_enable_olt.cc b/agent/test/src/test_enable_olt.cc
index 0cdb965..e7757ca 100644
--- a/agent/test/src/test_enable_olt.cc
+++ b/agent/test/src/test_enable_olt.cc
@@ -18,7 +18,9 @@
 #include "bal_mocker.h"
 #include "core.h"
 
-class TestOltEnable : public ::testing::Test {
+using namespace testing;
+
+class TestOltEnable : public Test {
  protected:
   virtual void SetUp() {
   }
@@ -29,28 +31,41 @@
   }
 };
 
+// This is used to set custom bcmolt_cfg value to bcmolt_cfg pointer coming in
+// bcmolt_cfg_get_stub.
+ACTION_P(SetArg1ToBcmOltCfg, value) { *static_cast<bcmolt_olt_cfg*>(arg1) = value; };
+
+
+// Create a mock function for bcmolt_cfg_get_stub C++ function
+MOCK_GLOBAL_FUNC2(bcmolt_cfg_get_stub, bcmos_errno(bcmolt_oltid, void*));
+
 
 // Test Fixture for OltEnable
 
 // Test 1: OltEnableSuccess case
 TEST_F(TestOltEnable, OltEnableSuccess){
-    // NiceMock is used to suppress 'WillByDefault' return errors on using 'NiceMock'.
+    // NiceMock is used to suppress 'WillByDefault' return errors.
     // This is described in https://github.com/arangodb-helper/gtest/blob/master/googlemock/docs/CookBook.md
-    ::testing::NiceMock<BalMocker> balMock;
+    NiceMock<BalMocker> balMock;
     bcmos_errno host_init_res = BCM_ERR_OK;
+    bcmos_errno bal_cfg_get_stub_res = BCM_ERR_OK;
     bcmos_errno bal_cfg_get_res = BCM_ERR_NOT_CONNECTED;
     bcmos_errno olt_oper_res = BCM_ERR_OK;
 
+    bcmolt_olt_cfg olt_cfg = { };
+    bcmolt_olt_key olt_key = { };
+    BCMOLT_CFG_INIT(&olt_cfg, olt, olt_key);
+    olt_cfg.data.bal_state = BCMOLT_BAL_STATE_BAL_AND_SWITCH_READY;
+
     Status olt_enable_res;
 
-    // The 'EXPECT_CALL' will do strict validation of input parameters. This may not be relevant for
-    // the current test case. Use 'ON_CALL' instead.
-    // The ON_CALL results in WARNINGs when running tests. Use NickMock instead of directly using BalMocker.
-    // https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md#the-nice-the-strict-and-the-naggy-nicestrictnaggy
-    // In below tests '::testing::_' does no validation on argument.
-    ON_CALL(balMock, bcmolt_host_init(::testing::_)).WillByDefault(::testing::Return(host_init_res));
-    ON_CALL(balMock, bcmolt_cfg_get(::testing::_,::testing::_)).WillByDefault(::testing::Return(bal_cfg_get_res));
-    ON_CALL(balMock, bcmolt_oper_submit(::testing::_, ::testing::_)).WillByDefault(::testing::Return(olt_oper_res));
+    ON_CALL(balMock, bcmolt_host_init(_)).WillByDefault(Return(host_init_res));
+    EXPECT_GLOBAL_CALL(bcmolt_cfg_get_stub, bcmolt_cfg_get_stub(_, _))
+                     .WillOnce(DoAll(SetArg1ToBcmOltCfg(olt_cfg), Return(bal_cfg_get_stub_res)));
+    EXPECT_CALL(balMock, bcmolt_cfg_get(_, _))
+                         .Times(BCM_MAX_DEVS_PER_LINE_CARD)
+                         .WillRepeatedly(Return(bal_cfg_get_res));
+    ON_CALL(balMock, bcmolt_oper_submit(_, _)).WillByDefault(Return(olt_oper_res));
 
     olt_enable_res = Enable_(1, NULL);
     ASSERT_TRUE( olt_enable_res.error_message() == Status::OK.error_message() );
@@ -58,23 +73,17 @@
 
 // Test 2: OltEnableFail_host_init_fail
 TEST_F(TestOltEnable, OltEnableFail_host_init_fail) {
-    // NiceMock is used to suppress 'WillByDefault' return errors on using 'NiceMock'.
+    // NiceMock is used to suppress 'WillByDefault' return errors.
     // This is described in https://github.com/arangodb-helper/gtest/blob/master/googlemock/docs/CookBook.md
-    ::testing::NiceMock<BalMocker> balMock;
+    NiceMock<BalMocker> balMock;
     bcmos_errno host_init_res = BCM_ERR_INTERNAL;
-    bcmos_errno bal_cfg_get_res = BCM_ERR_NOT_CONNECTED;
-    bcmos_errno olt_oper_res = BCM_ERR_OK;
 
     Status olt_enable_res;
 
-    // The 'EXPECT_CALL' will do strict validation of input parameters. This may not be relevant for
-    // the current test case. Use 'ON_CALL' instead.
-    // The ON_CALL results in WARNINGs when running tests. Use NickMock instead of directly using BalMocker.
-    // https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md#the-nice-the-strict-and-the-naggy-nicestrictnaggy
-    // In below tests '::testing::_' does no validation on argument.
-    ON_CALL(balMock, bcmolt_host_init(::testing::_)).WillByDefault(::testing::Return(host_init_res));
-    ON_CALL(balMock, bcmolt_cfg_get(::testing::_,::testing::_)).WillByDefault(::testing::Return(bal_cfg_get_res));
-    ON_CALL(balMock, bcmolt_oper_submit(::testing::_, ::testing::_)).WillByDefault(::testing::Return(olt_oper_res));
+    // Ensure that the state of the OLT is in deactivated to start with..
+    state.deactivate();
+
+    ON_CALL(balMock, bcmolt_host_init(_)).WillByDefault(Return(host_init_res));
 
     olt_enable_res = Enable_(1, NULL);
     ASSERT_TRUE( olt_enable_res.error_message() != Status::OK.error_message() );
@@ -82,81 +91,104 @@
 
 // Test 3: OltEnableSuccess_PON_Device_Connected
 TEST_F(TestOltEnable, OltEnableSuccess_PON_Device_Connected) {
-    // NiceMock is used to suppress 'WillByDefault' return errors on using 'NiceMock'.
+
+    // NiceMock is used to suppress 'WillByDefault' return errors.
     // This is described in https://github.com/arangodb-helper/gtest/blob/master/googlemock/docs/CookBook.md
-    ::testing::NiceMock<BalMocker> balMock;
+    NiceMock<BalMocker> balMock;
     bcmos_errno host_init_res = BCM_ERR_OK;
+    bcmos_errno bal_cfg_get_stub_res = BCM_ERR_OK;
     bcmos_errno bal_cfg_get_res = BCM_ERR_OK;
+    bcmos_errno olt_oper_res = BCM_ERR_OK;
 
-    Status olt_enable_res;
-
-    // The 'EXPECT_CALL' will do strict validation of input parameters. This may not be relevant for
-    // the current test case. Use 'ON_CALL' instead.
-    // The ON_CALL results in WARNINGs when running tests. Use NickMock instead of directly using BalMocker.
-    // https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md#the-nice-the-strict-and-the-naggy-nicestrictnaggy
-    // In below tests '::testing::_' does no validation on argument.
-    ON_CALL(balMock, bcmolt_host_init(::testing::_)).WillByDefault(::testing::Return(host_init_res));
-    ON_CALL(balMock, bcmolt_cfg_get(::testing::_,::testing::_)).WillByDefault(::testing::Return(bal_cfg_get_res));
-
-    olt_enable_res = Enable_(1, NULL);
-    ASSERT_TRUE( olt_enable_res.error_message() == Status::OK.error_message() );
-}
-
-// Test 4: OltEnableFail_All_PON_Enable_Fail
-TEST_F(TestOltEnable, OltEnableFail_All_PON_Enable_Fail) {
-    // NiceMock is used to suppress 'WillByDefault' return errors on using 'NiceMock'.
-    // This is described in https://github.com/arangodb-helper/gtest/blob/master/googlemock/docs/CookBook.md
-    ::testing::NiceMock<BalMocker> balMock;
-    bcmos_errno host_init_res = BCM_ERR_OK;
-    bcmos_errno bal_cfg_get_res = BCM_ERR_NOT_CONNECTED;
-    bcmos_errno olt_oper_res = BCM_ERR_INTERNAL;
+    bcmolt_olt_cfg olt_cfg = { };
+    bcmolt_olt_key olt_key = { };
+    BCMOLT_CFG_INIT(&olt_cfg, olt, olt_key);
+    olt_cfg.data.bal_state = BCMOLT_BAL_STATE_BAL_AND_SWITCH_READY;
 
     Status olt_enable_res;
 
     // Ensure that the state of the OLT is in deactivated to start with..
     state.deactivate();
 
-    // The 'EXPECT_CALL' will do strict validation of input parameters. This may not be relevant for
-    // the current test case. Use 'ON_CALL' instead.
-    // The ON_CALL results in WARNINGs when running tests. Use NickMock instead of directly using BalMocker.
-    // https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md#the-nice-the-strict-and-the-naggy-nicestrictnaggy
-    // In below tests '::testing::_' does no validation on argument.
-    ON_CALL(balMock, bcmolt_host_init(::testing::_)).WillByDefault(::testing::Return(host_init_res));
-    ON_CALL(balMock, bcmolt_cfg_get(::testing::_,::testing::_)).WillByDefault(::testing::Return(bal_cfg_get_res));
-    ON_CALL(balMock, bcmolt_oper_submit(::testing::_, ::testing::_)).WillByDefault(::testing::Return(olt_oper_res));
+    ON_CALL(balMock, bcmolt_host_init(_)).WillByDefault(Return(host_init_res));
+    EXPECT_GLOBAL_CALL(bcmolt_cfg_get_stub, bcmolt_cfg_get_stub(_, _))
+                     .WillOnce(DoAll(SetArg1ToBcmOltCfg(olt_cfg), Return(bal_cfg_get_stub_res)));
+    EXPECT_CALL(balMock, bcmolt_cfg_get(_, _))
+                         .Times(BCM_MAX_DEVS_PER_LINE_CARD)
+                         .WillRepeatedly(Return(bal_cfg_get_res));
 
     olt_enable_res = Enable_(1, NULL);
+    ASSERT_TRUE( olt_enable_res.error_message() == Status::OK.error_message() );
+
+}
+
+// Test 4: OltEnableFail_All_PON_Enable_Fail
+TEST_F(TestOltEnable, OltEnableFail_All_PON_Enable_Fail) {
+
+    // NiceMock is used to suppress 'WillByDefault' return errors.
+    // This is described in https://github.com/arangodb-helper/gtest/blob/master/googlemock/docs/CookBook.md
+    NiceMock<BalMocker> balMock;
+    bcmos_errno host_init_res = BCM_ERR_OK;
+    bcmos_errno bal_cfg_get_stub_res = BCM_ERR_OK;
+    bcmos_errno bal_cfg_get_res = BCM_ERR_NOT_CONNECTED;
+    bcmos_errno olt_oper_res = BCM_ERR_INTERNAL;
+
+    bcmolt_olt_cfg olt_cfg = { };
+    bcmolt_olt_key olt_key = { };
+    BCMOLT_CFG_INIT(&olt_cfg, olt, olt_key);
+    olt_cfg.data.bal_state = BCMOLT_BAL_STATE_BAL_AND_SWITCH_READY;
+
+    Status olt_enable_res;
+
+    // Ensure that the state of the OLT is in deactivated to start with..
+    state.deactivate();
+
+    ON_CALL(balMock, bcmolt_host_init(_)).WillByDefault(Return(host_init_res));
+    EXPECT_GLOBAL_CALL(bcmolt_cfg_get_stub, bcmolt_cfg_get_stub(_, _))
+                     .WillOnce(DoAll(SetArg1ToBcmOltCfg(olt_cfg), Return(bal_cfg_get_stub_res)));
+    EXPECT_CALL(balMock, bcmolt_cfg_get(_, _))
+                         .Times(BCM_MAX_DEVS_PER_LINE_CARD)
+                         .WillRepeatedly(Return(bal_cfg_get_res));
+    ON_CALL(balMock, bcmolt_oper_submit(_, _)).WillByDefault(Return(olt_oper_res));
+
+    olt_enable_res = Enable_(1, NULL);
+
     ASSERT_TRUE( olt_enable_res.error_message() != Status::OK.error_message() );
 }
 
 // Test 5 OltEnableSuccess_One_PON_Enable_Fail : One PON device enable fails, but all others succeed.
 TEST_F(TestOltEnable, OltEnableSuccess_One_PON_Enable_Fail) {
-    // NiceMock is used to suppress 'WillByDefault' return errors on using 'NiceMock'.
+
+    // NiceMock is used to suppress 'WillByDefault' return errors.
     // This is described in https://github.com/arangodb-helper/gtest/blob/master/googlemock/docs/CookBook.md
-    ::testing::NiceMock<BalMocker> balMock;
+    NiceMock<BalMocker> balMock;
     bcmos_errno host_init_res = BCM_ERR_OK;
+    bcmos_errno bal_cfg_get_stub_res = BCM_ERR_OK;
     bcmos_errno bal_cfg_get_res = BCM_ERR_NOT_CONNECTED;
     bcmos_errno olt_oper_res_fail = BCM_ERR_INTERNAL;
     bcmos_errno olt_oper_res_success = BCM_ERR_OK;
 
+    bcmolt_olt_cfg olt_cfg = { };
+    bcmolt_olt_key olt_key = { };
+    BCMOLT_CFG_INIT(&olt_cfg, olt, olt_key);
+    olt_cfg.data.bal_state = BCMOLT_BAL_STATE_BAL_AND_SWITCH_READY;
+
     Status olt_enable_res;
 
     // Ensure that the state of the OLT is in deactivated to start with..
     state.deactivate();
 
-    // The 'EXPECT_CALL' will do strict validation of input parameters. This may not be relevant for
-    // the current test case. Use 'ON_CALL' instead.
-    // The ON_CALL results in WARNINGs when running tests. Use NickMock instead of directly using BalMocker.
-    // https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md#the-nice-the-strict-and-the-naggy-nicestrictnaggy
-    // In below tests '::testing::_' does no validation on argument.
-    ON_CALL(balMock, bcmolt_host_init(::testing::_)).WillByDefault(::testing::Return(host_init_res));
-    ON_CALL(balMock, bcmolt_cfg_get(::testing::_,::testing::_)).WillByDefault(::testing::Return(bal_cfg_get_res));
+    ON_CALL(balMock, bcmolt_host_init(_)).WillByDefault(Return(host_init_res));
+    EXPECT_GLOBAL_CALL(bcmolt_cfg_get_stub, bcmolt_cfg_get_stub(_, _))
+                     .WillOnce(DoAll(SetArg1ToBcmOltCfg(olt_cfg), Return(bal_cfg_get_stub_res)));
+    EXPECT_CALL(balMock, bcmolt_cfg_get(_, _))
+                         .Times(BCM_MAX_DEVS_PER_LINE_CARD)
+                         .WillRepeatedly(Return(bal_cfg_get_res));
     // For the the first PON mac device, the activation result will fail, and will succeed for all other PON mac devices.
-    EXPECT_CALL(balMock, bcmolt_oper_submit(::testing::_, ::testing::_))
-                         .WillOnce(::testing::Return(olt_oper_res_fail))
-                         .WillRepeatedly(::testing::Return(olt_oper_res_success));
-
+    EXPECT_CALL(balMock, bcmolt_oper_submit(_, _))
+                         .WillOnce(Return(olt_oper_res_fail))
+                         .WillRepeatedly(Return(olt_oper_res_success));
     olt_enable_res = Enable_(1, NULL);
-    // The OLT activation should succeed if at least one PON mac device activation succeeds.
+
     ASSERT_TRUE( olt_enable_res.error_message() == Status::OK.error_message() );
 }