[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
diff --git a/agent/common/main.cc b/agent/common/main.cc
index b211cfb..0454337 100644
--- a/agent/common/main.cc
+++ b/agent/common/main.cc
@@ -42,6 +42,26 @@
         }
     }
 
+    ProbeDeviceCapabilities_();
+    sleep(2);
+    ProbePonIfTechnology_();
+    sleep(2);
+    // Enable all PON interfaces. 
+    for (int i = 0; i < NumPonIf_(); i++) {
+        status = EnablePonIf_(i);
+        if (!status.ok()) {
+            // FIXME - raise alarm to report error in enabling PON
+        }
+    }
+    sleep(2);
+    // Enable all NNI interfaces.
+    for (int i = 0; i < NumNniIf_(); i++) {
+        status = EnableUplinkIf_(i);
+        if (!status.ok()) {
+            // FIXME - raise alarm to report error in enabling PON
+        }
+    }
+
     RunServer();
 
     return 0;
diff --git a/agent/src/core.cc b/agent/src/core.cc
index 4175a7f..37f8b17 100644
--- a/agent/src/core.cc
+++ b/agent/src/core.cc
@@ -354,7 +354,7 @@
 
     // Legacy, device-wide ranges. To be deprecated when adapter
     // is upgraded to support per-interface ranges
-    if (board_technology == "XGPON") {
+    if (board_technology == "XGS-PON") {
         device_info->set_onu_id_start(1);
         device_info->set_onu_id_end(255);
         device_info->set_alloc_id_start(MIN_ALLOC_ID_XGSPON);
@@ -604,17 +604,19 @@
                     bcmolt_device_key key = {.device_id = dev};
                     bcmolt_device_connect oper;
                     bcmolt_device_cfg cfg;
-                    BCM_LOG(INFO, openolt_log_id, "Enable Maple - %d/%d\n", dev, BCM_MAX_DEVS_PER_LINE_CARD);
+                    BCM_LOG(INFO, openolt_log_id, "Enable Maple - %d/%d\n", dev + 1, BCM_MAX_DEVS_PER_LINE_CARD);
                     BCMOLT_OPER_INIT(&oper, device, connect, key);
                     BCMOLT_MSG_FIELD_SET(&oper, inni_config.mode, BCMOLT_INNI_MODE_ALL_10_G_XFI);
-                    BCMOLT_MSG_FIELD_SET (&oper, system_mode, BCMOLT_SYSTEM_MODE_XGPON__2_X);
+                    BCMOLT_MSG_FIELD_SET (&oper, system_mode, BCMOLT_SYSTEM_MODE_XGS__2_X);
                     err = bcmolt_oper_submit(dev_id, &oper.hdr);
                     if (err) 
                         BCM_LOG(ERROR, openolt_log_id, "Enable Maple deivce %d failed\n", dev);
                     bcmos_usleep(200000);
                 }
-                else
+                else {
                     BCM_LOG(ERROR, openolt_log_id, "Maple deivce %d already connected\n", dev);
+                    state.activate();
+                }
             }
             init_stats();
         }
@@ -690,6 +692,7 @@
 
 Status EnablePonIf_(uint32_t intf_id) {
     bcmos_errno err = BCM_ERR_OK; 
+    bcmolt_pon_interface_cfg interface_obj;
     bcmolt_pon_interface_key intf_key = {.pon_ni = (bcmolt_interface)intf_id};
     bcmolt_pon_interface_set_pon_interface_state pon_interface_set_state;
     bcmolt_interface_state state;
@@ -701,9 +704,20 @@
             return Status::OK;
         }
     } 
+    BCMOLT_CFG_INIT(&interface_obj, pon_interface, intf_key);
     BCMOLT_OPER_INIT(&pon_interface_set_state, pon_interface, set_pon_interface_state, intf_key);
+    BCMOLT_MSG_FIELD_SET(&interface_obj, discovery.control, BCMOLT_CONTROL_STATE_ENABLE);
+    BCMOLT_MSG_FIELD_SET(&interface_obj, discovery.interval, 5000);
+    BCMOLT_MSG_FIELD_SET(&interface_obj, discovery.onu_post_discovery_mode,
+        BCMOLT_ONU_POST_DISCOVERY_MODE_ACTIVATE);
     BCMOLT_FIELD_SET(&pon_interface_set_state.data, pon_interface_set_pon_interface_state_data,
         operation, BCMOLT_INTERFACE_OPERATION_ACTIVE_WORKING);
+
+    err = bcmolt_cfg_set(dev_id, &interface_obj.hdr);
+    if (err != BCM_ERR_OK) {
+        BCM_LOG(ERROR, openolt_log_id, "Failed to enable discovery onu: %d\n", intf_id);
+        return bcm_to_grpc_err(err, "Failed to enable discovery onu");
+    }
     err = bcmolt_oper_submit(dev_id, &pon_interface_set_state.hdr);
     if (err != BCM_ERR_OK) {
         BCM_LOG(ERROR, openolt_log_id, "Failed to enable PON interface: %d\n", intf_id);
@@ -790,10 +804,10 @@
             olt_cfg.data.bal_state == BCMOLT_BAL_STATE_BAL_AND_SWITCH_READY 
             ? "up" : "down");
 
-    BCM_LOG(INFO, openolt_log_id, "--------------- version %s object model: %d\n", 
+    BCM_LOG(INFO, openolt_log_id, "version %s object model: %d\n", 
         bal_version.c_str(), BAL_API_VERSION);
 
-    BCM_LOG(INFO, openolt_log_id, "--------------- topology nni:%d pon:%d dev:%d ppd:%d family: %s\n",
+    BCM_LOG(INFO, openolt_log_id, "topology nni:%d pon:%d dev:%d ppd:%d family: %s\n",
             num_of_nni_ports,
             num_of_pon_ports,
             BCM_MAX_DEVS_PER_LINE_CARD,
@@ -825,7 +839,7 @@
             if(err != BCM_ERR_RANGE) BCM_LOG(ERROR, openolt_log_id, "Failed to get PON config: %d\n", intf_id);
         }
         else {
-            if (board_technology == "XGPON") {
+            if (board_technology == "XGS-PON") {
                 switch(interface_obj.data.xgpon_trx.transceiver_type) {
                     case BCMOLT_XGPON_TRX_TYPE_LTH_7222_PC:
                     case BCMOLT_XGPON_TRX_TYPE_WTD_RTXM266_702: 
@@ -834,7 +848,7 @@
                     case BCMOLT_XGPON_TRX_TYPE_LTH_5302_PC: 
                     case BCMOLT_XGPON_TRX_TYPE_LTH_7226_A_PC_PLUS: 
                     case BCMOLT_XGPON_TRX_TYPE_D272RR_SSCB_DM: 
-                        intf_technologies[intf_id] = "XGPON";
+                        intf_technologies[intf_id] = "XGS-PON";
                         break;
                 }
             } else if (board_technology == "GPON") {
@@ -921,30 +935,42 @@
 
 Status ActivateOnu_(uint32_t intf_id, uint32_t onu_id,
     const char *vendor_id, const char *vendor_specific, uint32_t pir) {
-
-    bcmolt_onu_cfg cfg_obj;
-    bcmolt_onu_key key;
+    bcmos_errno err = BCM_ERR_OK;
+    bcmolt_onu_cfg onu_cfg;
+    bcmolt_onu_key onu_key;
     bcmolt_serial_number serial_number; /**< ONU serial number */
     bcmolt_bin_str_36 registration_id; /**< ONU registration ID */
 
-    BCM_LOG(INFO, openolt_log_id,  "Enabling ONU %d on PON %d : vendor id %s, vendor specific %s, pir %d\n",
-        onu_id, intf_id, vendor_id, vendor_specific_to_str(vendor_specific).c_str(), pir);
+    onu_key.onu_id = onu_id;
+    onu_key.pon_ni = intf_id;
+    BCMOLT_CFG_INIT(&onu_cfg, onu, onu_key);
+    BCMOLT_FIELD_SET_PRESENT(&onu_cfg.data, onu_cfg_data, onu_state);
+    err = bcmolt_cfg_get(dev_id, &onu_cfg.hdr);
+    if (err == BCM_ERR_OK) { 
+        if ((onu_cfg.data.onu_state == BCMOLT_ONU_STATE_PROCESSING || 
+             onu_cfg.data.onu_state == BCMOLT_ONU_STATE_ACTIVE) ||
+           (onu_cfg.data.onu_state == BCMOLT_ONU_STATE_INACTIVE &&
+				onu_cfg.data.onu_old_state == BCMOLT_ONU_STATE_NOT_CONFIGURED))
+            return Status::OK;
+    }
 
-    key.onu_id = onu_id;
-    key.pon_ni = intf_id;
-    BCMOLT_CFG_INIT(&cfg_obj, onu, key);
+    BCM_LOG(INFO, openolt_log_id,  "Enabling ONU %d on PON %d : vendor id %s, \
+        vendor specific %s, pir %d\n", onu_id, intf_id, vendor_id, 
+        vendor_specific_to_str(vendor_specific).c_str(), pir);
+
     memcpy(serial_number.vendor_id.arr, vendor_id, 4);
     memcpy(serial_number.vendor_specific.arr, vendor_specific, 4);
-    BCMOLT_MSG_FIELD_SET(&cfg_obj, itu.serial_number, serial_number);
-    BCMOLT_MSG_FIELD_SET(&cfg_obj, itu.auto_learning, BCMOS_TRUE);
-	 /*set burst and data profiles to fec disabled*/
-	 BCMOLT_MSG_FIELD_SET(&cfg_obj, itu.xgpon.ranging_burst_profile, 0);
-	 BCMOLT_MSG_FIELD_SET(&cfg_obj, itu.xgpon.data_burst_profile, 1);
-
-    bcmos_errno err = bcmolt_cfg_set(dev_id, &cfg_obj.hdr);	
-    if (err) {
-        BCM_LOG(ERROR, openolt_log_id, "Failed to enable ONU %d on PON %d\n", onu_id, intf_id);
-        return bcm_to_grpc_err(err, "Failed to enable ONU");
+    BCMOLT_CFG_INIT(&onu_cfg, onu, onu_key);
+    BCMOLT_MSG_FIELD_SET(&onu_cfg, onu_rate, BCMOLT_ONU_RATE_RATE_10G_DS_10G_US);
+    BCMOLT_MSG_FIELD_SET(&onu_cfg, itu.serial_number, serial_number);
+    BCMOLT_MSG_FIELD_SET(&onu_cfg, itu.auto_learning, BCMOS_TRUE);
+    /*set burst and data profiles to fec disabled*/
+    BCMOLT_MSG_FIELD_SET(&onu_cfg, itu.xgpon.ranging_burst_profile, 0);
+    BCMOLT_MSG_FIELD_SET(&onu_cfg, itu.xgpon.data_burst_profile, 1);
+    err = bcmolt_cfg_set(dev_id, &onu_cfg.hdr);	
+    if (err != BCM_ERR_OK) {
+        BCM_LOG(ERROR, openolt_log_id, "Failed to set activate ONU %d on PON %d, err %d\n", onu_id, intf_id, err);
+        return bcm_to_grpc_err(err, "Failed to activate ONU");
     }
 
     return Status::OK;
@@ -952,24 +978,31 @@
 
 Status DeactivateOnu_(uint32_t intf_id, uint32_t onu_id,
     const char *vendor_id, const char *vendor_specific) {
+    bcmos_errno err = BCM_ERR_OK;
+    bcmolt_onu_set_onu_state onu_oper; /* declare main API struct */
+    bcmolt_onu_cfg onu_cfg;
+    bcmolt_onu_key onu_key; /**< Object key. */
+    bcmolt_onu_state onu_state;
 
-    bcmolt_onu_cfg cfg_obj;
-    bcmolt_onu_key key;
-    bcmolt_serial_number serial_number; /**< ONU serial number */
-    bcmolt_bin_str_36 registration_id; /**< ONU registration ID */
-
-    BCM_LOG(INFO, openolt_log_id,  "Deactivating ONU %d on PON %d : vendor id %s, vendor specific %s\n",
-        onu_id, intf_id, vendor_id, vendor_specific_to_str(vendor_specific).c_str());
-
-    key.onu_id= onu_id;
-    key.pon_ni = intf_id;
-    BCMOLT_CFG_INIT(&cfg_obj, onu, key);
-    BCMOLT_MSG_FIELD_SET(&cfg_obj, onu_state, BCMOLT_ONU_STATE_INACTIVE);
-
-    bcmos_errno err = bcmolt_cfg_set(dev_id, &cfg_obj.hdr);	
-    if (err) {
-        BCM_LOG(ERROR, openolt_log_id,  "Failed to deactivate ONU %d on PON %d\n", onu_id, intf_id);
-        return Status(grpc::StatusCode::INTERNAL, "Failed to deactivate ONU");
+    onu_key.onu_id = onu_id;
+    onu_key.pon_ni = intf_id;
+    BCMOLT_CFG_INIT(&onu_cfg, onu, onu_key);
+    BCMOLT_FIELD_SET_PRESENT(&onu_cfg.data, onu_cfg_data, onu_state);
+    err = bcmolt_cfg_get(dev_id, &onu_cfg.hdr);
+    if (err == BCM_ERR_OK) { 
+        switch (onu_state) {
+            case BCMOLT_ONU_OPERATION_ACTIVE:
+                BCMOLT_OPER_INIT(&onu_oper, onu, set_onu_state, onu_key);
+                BCMOLT_FIELD_SET(&onu_oper.data, onu_set_onu_state_data, 
+                    onu_state, BCMOLT_ONU_OPERATION_INACTIVE);
+                err = bcmolt_oper_submit(dev_id, &onu_oper.hdr);
+                if (err != BCM_ERR_OK) {
+                    BCM_LOG(ERROR, openolt_log_id, "Failed to \
+                        deactivate ONU %d on PON %d, err %d\n", onu_id, intf_id, err);
+                    return bcm_to_grpc_err(err, "Failed to deactivate ONU");
+                }
+                break;
+        }
     }
 
     return Status::OK;
diff --git a/agent/src/indications.cc b/agent/src/indications.cc
index 664c79a..ad71cee 100644
--- a/agent/src/indications.cc
+++ b/agent/src/indications.cc
@@ -55,6 +55,10 @@
          state == BCMOLT_ONU_OPERATION_ACTIVE_STANDBY) ? BCMOS_TRUE : BCMOS_FALSE)
 #define ONU_STATE_IF_UP(state) \
        ((state == BCMOLT_ONU_OPERATION_ACTIVE) ? BCMOS_TRUE : BCMOS_FALSE)
+#define ONU_RANGING_STATE_IF_UP(state) \
+       ((state == BCMOLT_RESULT_SUCCESS) ? BCMOS_TRUE : BCMOS_FALSE)
+#define ONU_RANGING_STATE_IF_DOWN(state) \
+       ((state != BCMOLT_RESULT_SUCCESS) ? BCMOS_TRUE : BCMOS_FALSE)
 
 std::string bcmbal_to_grpc_intf_type(bcmolt_interface_type intf_type)
 {
@@ -74,7 +78,6 @@
 
     switch (msg->subgroup) {
         case BCMOLT_DEVICE_AUTO_SUBGROUP_CONNECTION_COMPLETE:
-            ProbeDeviceCapabilities_();
             admin_state = "up";
             olt_ind->set_oper_state("up");
             break;
@@ -90,22 +93,6 @@
     ind.set_allocated_olt_ind(olt_ind);
 
     if (msg->subgroup == BCMOLT_DEVICE_AUTO_SUBGROUP_CONNECTION_COMPLETE) {
-        // Enable all PON interfaces. 
-        for (int i = 0; i < NumPonIf_(); i++) {
-            status = EnablePonIf_(i);
-            if (!status.ok()) {
-                // FIXME - raise alarm to report error in enabling PON
-            }
-        }
-
-        // Enable all NNI interfaces. 
-        for (int i = 0; i < NumNniIf_(); i++) {
-            status = EnableUplinkIf_(i);
-            if (!status.ok()) {
-                // FIXME - raise alarm to report error in enabling PON
-            }
-        }
-
         /* register for omci indication */
         {
             bcmolt_rx_cfg rx_cfg = {};
@@ -115,8 +102,6 @@
             rx_cfg.module = BCMOS_MODULE_ID_OMCI_TRANSPORT;
             bcmolt_ind_subscribe(current_device, &rx_cfg);
         }
-
-        ProbePonIfTechnology_();
         state.activate();
     }
     else {
@@ -168,8 +153,6 @@
             switch (msg->subgroup) {
                 case BCMOLT_PON_INTERFACE_AUTO_SUBGROUP_STATE_CHANGE_COMPLETED:
                 { 
-                    BCM_LOG(INFO, openolt_log_id, "intf indication, intf_id: %d\n",
-                        ((bcmolt_pon_interface_state_change_completed *)msg)->key.pon_ni);
                     bcmolt_pon_interface_key *key = 
                         &((bcmolt_pon_interface_state_change_completed*)msg)->key;
                     bcmolt_pon_interface_state_change_completed_data *data = 
@@ -373,18 +356,19 @@
     switch (msg->obj_type) {
         case BCMOLT_OBJ_ID_ONU:
             switch (msg->subgroup) {
-                case BCMOLT_ONU_AUTO_SUBGROUP_STATE_CHANGE:
+                case BCMOLT_ONU_AUTO_SUBGROUP_RANGING_COMPLETED:
                 {
-                    bcmolt_onu_key *key = &((bcmolt_onu_state_change*)msg)->key;
-                    bcmolt_onu_state_change_data *data = &((bcmolt_onu_state_change*)msg)->data;
+                    bcmolt_onu_key *key = &((bcmolt_onu_ranging_completed*)msg)->key;
+                    bcmolt_onu_ranging_completed_data *data = &((bcmolt_onu_ranging_completed*)msg)->data;
 
-                    BCM_LOG(INFO, openolt_log_id, "onu indication, pon_ni %d, onu_id %d, onu_state %d\n", key->pon_ni, key->onu_id, data->new_onu_state);
+                    BCM_LOG(INFO, openolt_log_id, "onu indication, pon_ni %d, onu_id %d, onu_state %s\n", 
+                        key->pon_ni, key->onu_id, (data->status==BCMOLT_RESULT_SUCCESS)?"up":"down");
 
                     onu_ind->set_intf_id(key->pon_ni);
                     onu_ind->set_onu_id(key->onu_id);
-                    if (ONU_STATE_IF_UP(data->new_onu_state))
+                    if (ONU_RANGING_STATE_IF_UP(data->status))
                         onu_ind->set_oper_state("up");
-                    if (ONU_STATE_IF_DOWN(data->new_onu_state))
+                    if (ONU_RANGING_STATE_IF_DOWN(data->status))
                         onu_ind->set_oper_state("down");
 
                     ind.set_allocated_onu_ind(onu_ind);
@@ -713,17 +697,18 @@
     switch (msg->obj_type) {
         case BCMOLT_OBJ_ID_ONU:
             switch (msg->subgroup) {
-                case BCMOLT_ONU_AUTO_SUBGROUP_ONU_ACTIVATION_COMPLETED:
+                case BCMOLT_ONU_AUTO_SUBGROUP_ONU_DEACTIVATION_COMPLETED:
                 {
                     bcmolt_onu_key *key = &((bcmolt_onu_onu_activation_completed*)msg)->key;
                     bcmolt_onu_onu_activation_completed_data *data = 
                         &((bcmolt_onu_onu_activation_completed*)msg)->data;
 
-                    BCM_LOG(WARNING, openolt_log_id, "onu activation failure indication, intf_id %d, onu_id %d, fail_reason %d\n",
+                    BCM_LOG(INFO, openolt_log_id, "Got onu deactivation, intf_id %d, onu_id %d, fail_reason %d\n",
                         key->pon_ni, key->onu_id, data->fail_reason);
 
                     activation_fail_ind->set_intf_id(key->pon_ni);
                     activation_fail_ind->set_onu_id(key->onu_id);
+                    activation_fail_ind->set_fail_reason(data->fail_reason);
                     alarm_ind->set_allocated_onu_activation_fail_ind(activation_fail_ind);
 
                     ind.set_allocated_alarm_ind(alarm_ind);
@@ -872,7 +857,8 @@
     rx_cfg.obj_type = BCMOLT_OBJ_ID_ONU;
     rx_cfg.rx_cb = OnuIndication;
     rx_cfg.flags = BCMOLT_AUTO_FLAGS_NONE;
-    rx_cfg.subgroup = bcmolt_onu_auto_subgroup_state_change;
+    rx_cfg.subgroup = bcmolt_onu_auto_subgroup_ranging_completed;
+
     rc = bcmolt_ind_subscribe(current_device, &rx_cfg);
     if(rc != BCM_ERR_OK)
         return Status(grpc::StatusCode::INTERNAL, "onu indication subscribe failed");
@@ -942,7 +928,7 @@
     rx_cfg.obj_type = BCMOLT_OBJ_ID_ONU;
     rx_cfg.rx_cb = OnuActivationFailureIndication;
     rx_cfg.flags = BCMOLT_AUTO_FLAGS_NONE;
-    rx_cfg.subgroup = bcmolt_onu_auto_subgroup_onu_activation_completed;
+    rx_cfg.subgroup = bcmolt_onu_auto_subgroup_onu_deactivation_completed;
     rc = bcmolt_ind_subscribe(current_device, &rx_cfg);
     if(rc != BCM_ERR_OK)
         return Status(grpc::StatusCode::INTERNAL, 
diff --git a/agent/src/stats_collection.cc b/agent/src/stats_collection.cc
index e2b3d09..515eb32 100644
--- a/agent/src/stats_collection.cc
+++ b/agent/src/stats_collection.cc
@@ -153,10 +153,10 @@
             {
                 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);
-                BCMOLT_STAT_INIT(&pon_stats, onu, itu_pon_stats, key);
 
                 /* call API */
                 err = bcmolt_stat_get((bcmolt_oltid)device_id, &pon_stats.hdr, clear_on_read);
diff --git a/protos/openolt.proto b/protos/openolt.proto
index 81ae2df..f87d8a4 100644
--- a/protos/openolt.proto
+++ b/protos/openolt.proto
@@ -474,6 +474,7 @@
 message OnuActivationFailureIndication {
     fixed32 intf_id = 1;
     fixed32 onu_id = 2;
+    fixed32 fail_reason = 3;
 }
 
 message OnuProcessingErrorIndication {