/* Parse the attributes */
static cps_api_return_code_t  nas_qos_cps_parse_attr(cps_api_object_t obj,
        nas_qos_map_entry  &entry )
{
    nas_qos_map_type_t type = entry.get_type();

    cps_api_object_it_t it;
    cps_api_object_it_begin(obj,&it);
    for ( ; cps_api_object_it_valid(&it) ; cps_api_object_it_next(&it) ) {
        cps_api_attr_id_t id = cps_api_object_attr_id(it.attr);
        _attr_parse_fn fn;

        if (id == qos_map_entry_attr_1_obj_id(type)) {
            fn = qos_map_entry_attr_1_parse_fn(type);
            fn(it.attr, entry);
        }

        if (id == qos_map_entry_attr_2_obj_id(type)) {
            fn = qos_map_entry_attr_2_parse_fn(type);
            fn(it.attr, entry);
        }
    }

    return cps_api_ret_code_OK;
}
void nas_acl_set_action_list (const cps_api_object_t     obj,
                              const cps_api_object_it_t& it,
                              nas_acl_entry&             entry)
{
    /*
     * Encoding of ACL Entry Action parameters, with the following
     * sample action parameters:
     *   1. Ingress Mirroring
     *   2. Redirect Port
     *   3. Forward
     *
     * ------------------------------------------------------------------------
     * | Type : BASE_ACL_ENTRY_ACTION  (ACTION-List-Attr)                     |
     * | Len  : Length of Val                                                 |
     * | Val  ----------------------------------------------------------------|
     * |      | Type : 'list_index' = 0                                       |
     * |      | Len  : Length of Val                                          |
     * |      | Val  ---------------------------------------------------------|
     * |      |      | Type : BASE_ACL_ENTRY_ACTION_TYPE                      |
     * |      |      | Len  : Length of Val                                   |
     * |      |      | Val  : BASE_ACL_ACTION_TYPE_SET_POLICER     <--        |
     * |      |      |                                               |        |
     * |      |      |                                  --------------------  |
     * |      |      |                                  |'action_type_val' |  |
     * |      |      |                                  --------------------  |
     * |      |      |--------------------------------------------------------|
     * |      |      | Type : BASE_ACL_ENTRY_ACTION_POLICER_VALUE  <--        |
     * |      |      |                                               |        |
     * |      |      |                             -------------------------- |
     * |      |      |                             |'action_map.val.attr_id'| |
     * |      |      |                             -------------------------- |
     * |      |      | Len  : Length of Val                                   |
     * |      |      | Val  --------------------------------------------------|
     * |      |      |      | Type : BASE_ACL_ENTRY_ACTION_POLICER_VALUE_INDEX|
     * |      |      |      | Len  : Length of Val                            |
     * |      |      |      | Val  : <mirror-id>                              |
     * |      |      |      |                                                 |
     * |      |      |      | Type : BASE_ACL_ENTRY_ACTION_POLICER_VALUE_DATA |
     * |      |      |      | Len  : Length of Val                            |
     * |      |      |      | Val  : <mirror-blob-data>                       |
     * |      |      |      --------------------------------------------------|
     * |      |      |                                                        |
     * |      |      ---------------------------------------------------------|
     * |      |                                                               |
     * |      ----------------------------------------------------------------|
     * |      | Type : 'list_index' = 1                                       |
     * |      | Len  : Length of Val                                          |
     * |      | Val  ---------------------------------------------------------|
     * |      |      | Type : BASE_ACL_ENTRY_ACTION_TYPE                      |
     * |      |      | Len  : Length of Val                                   |
     * |      |      | Val  : BASE_ACL_ACTION_TYPE_FORWARD   <----            |
     * |      |      |                                           |            |
     * |      |      |                                  -------------------   |
     * |      |      |                                 |'action_type_val' |   |
     * |      |      |                                  -------------------   |
     * |      |      |     NOTE: There is no further data for this action.    |
     * |      |      ---------------------------------------------------------|
     * |      |                                                               |
     * |      ----------------------------------------------------------------|
     * |      |                                                               |
     * |      |                           ...                                 |
     * |      |                                                               |
     * |      ----------------------------------------------------------------|
     * |      | Type : 'list_index' = N                                       |
     * |      | Len  : Length of Val                                          |
     * |      | Val  ---------------------------------------------------------|
     * |      |      | Type : BASE_ACL_ENTRY_ACTION_TYPE                      |
     * |      |      | Len  : Length of Val                                   |
     * |      |      | Val  : BASE_ACL_ACTION_TYPE_REDIRECT_PORT      <--     |
     * |      |      |                                                  |     |
     * |      |      |                                 --------------------   |
     * |      |      |                                 |'action_type_val' |   |
     * |      |      |                                 --------------------   |
     * |      |      |--------------------------------------------------------|
     * |      |      | Type : BASE_ACL_ENTRY_ACTION_REDIRECT_PORT_VALUE       |
     * |      |      |                                              ^         |
     * |      |      |                                              |         |
     * |      |      |                             -------------------------- |
     * |      |      |                             |'action_map.val.attr_id'| |
     * |      |      |                             -------------------------- |
     * |      |      | Len  : Length of Val                                   |
     * |      |      | Val  : <if-index>                                      |
     * |      |      ---------------------------------------------------------|
     * |      |                                                               |
     * |      ----------------------------------------------------------------|
     * |                                                                      |
     * ------------------------------------------------------------------------
     */

    BASE_ACL_ACTION_TYPE_t     action_type_val;
    cps_api_object_it_t        it_action_list = it;
    cps_api_attr_id_t          list_index = 0;
    nas::attr_list_t           parent_attr_id_list;
    nas_acl_common_data_list_t common_data_list;

    // Parent attr list to build attr hierarchy
    //  - ACTION-List-Attr . Action-ListIndex . Action-Value-Attr . Action-Value-Child-Attr
    parent_attr_id_list.reserve(NAS_ACL_MAX_ATTR_DEPTH);

    //  Of this the following hierarchy is filled in this function
    //  - ACTION-List-Attr . Action-ListIndex

    for (cps_api_object_it_inside (&it_action_list);
         cps_api_object_it_valid (&it_action_list);
         cps_api_object_it_next (&it_action_list)) {

        parent_attr_id_list.clear ();
        parent_attr_id_list.push_back (BASE_ACL_ENTRY_ACTION);

        list_index = cps_api_object_attr_id (it_action_list.attr);

        parent_attr_id_list.push_back (list_index);

        cps_api_object_it_t it_action_attr = it_action_list;
        cps_api_object_it_inside (&it_action_attr);
        if (!cps_api_object_it_valid (&it_action_attr)) {
            throw nas::base_exception {NAS_ACL_E_MISSING_ATTR, __PRETTY_FUNCTION__,
                                       "Missing list container for ACTION in object"};
        }

        bool is_dupl;
        auto attr_action_type = nas_acl_get_attr (it_action_attr,
                                                 BASE_ACL_ENTRY_ACTION_TYPE, &is_dupl);

        if (attr_action_type == NULL) {
            throw nas::base_exception {NAS_ACL_E_MISSING_ATTR, __PRETTY_FUNCTION__,
                                       "Missing ACTION_TYPE attribute"};
        }
        if (is_dupl) {
            throw nas::base_exception {NAS_ACL_E_DUPLICATE, __PRETTY_FUNCTION__,
                                       "Duplicate ACTION_TYPE attribute"};
        }

        action_type_val = (BASE_ACL_ACTION_TYPE_t)
            cps_api_object_attr_data_u32 (attr_action_type);

        NAS_ACL_LOG_DETAIL ("action_type_val: %d (%s)", action_type_val,
                            nas_acl_action_t::type_name (action_type_val));

        nas_acl_set_action_attr (obj, entry, action_type_val,
                                 parent_attr_id_list, true);
    }
}
static bool nas_mirror_fill_entry(cps_api_object_t obj,nas_mirror_entry *entry
                                 ,nas_mirror_src_intf_map_t  & new_intf_map,bool update){

    nas::attr_set_t attrs;
    cps_api_object_it_t it;

    cps_api_object_it_begin(obj,&it);

    for ( ; cps_api_object_it_valid(&it) ; cps_api_object_it_next(&it) ) {

        int id = (int) cps_api_object_attr_id(it.attr);
        switch (id) {

        case BASE_MIRROR_ENTRY_DST_INTF :
            if(attrs.contains(BASE_MIRROR_ENTRY_DST_INTF) ||
               attrs.contains(BASE_MIRROR_ENTRY_LAG_OPAQUE_DATA)){
                NAS_MIRROR_LOG(ERR,ev_log_s_MINOR,"Multiple Destination Interface Index passed "
                        "for creating/updating Mirroring session");
                return false;
            }

            entry->set_dst_intf(cps_api_object_attr_data_u32(it.attr));
            interface_ctrl_t intf_ctrl;
            if(!nas_mirror_intf_to_port(entry->get_dst_intf(),&intf_ctrl)){
                return false;
            }

            entry->set_dst_port(intf_ctrl.npu_id,intf_ctrl.port_id);
            attrs.add(BASE_MIRROR_ENTRY_DST_INTF);
            break;

        case BASE_MIRROR_ENTRY_LAG_OPAQUE_DATA :
            if(attrs.contains(BASE_MIRROR_ENTRY_LAG_OPAQUE_DATA) ||
               attrs.contains(BASE_MIRROR_ENTRY_DST_INTF)){
                NAS_MIRROR_LOG(ERR,0,"Multiple Destination Lag opaque data "
                               "passed for creating/updating Mirroring session");
                return false;
            }

            {
                nas::ndi_obj_id_table_t lag_opaque_data_table;
                cps_api_attr_id_t  attr_id_list[] = {BASE_MIRROR_ENTRY_LAG_OPAQUE_DATA};
                if(!nas::ndi_obj_id_table_cps_unserialize (lag_opaque_data_table, obj,attr_id_list,
                                                           sizeof(attr_id_list)/
                                                           sizeof(attr_id_list[0]))){
                    NAS_MIRROR_LOG(ERR,0,"Failed to unserialize lag opaque data");
                    return false;
                }

                auto lag_opaue_it = lag_opaque_data_table.begin();
                if(lag_opaue_it == lag_opaque_data_table.end()){
                    NAS_MIRROR_LOG(ERR,0,"No lag opaque data passed");
                    return false;
                }

                entry->set_ndi_lag_id(lag_opaue_it->first,lag_opaue_it->second);
            }
            attrs.add(BASE_MIRROR_ENTRY_LAG_OPAQUE_DATA);
            break;

        case BASE_MIRROR_ENTRY_INTF:
            if(!update) attrs.add(BASE_MIRROR_ENTRY_INTF);
            if(!get_src_intf_attr(obj, new_intf_map , it)) return false;
            break;

        case BASE_MIRROR_ENTRY_FLOW_ENABLED :
            entry->set_flow(cps_api_object_attr_data_u32(it.attr));
            break;

        case BASE_MIRROR_ENTRY_TYPE:
            if(attrs.contains(BASE_MIRROR_ENTRY_TYPE)){
                NAS_MIRROR_LOG(ERR,ev_log_s_MINOR,"Multiple Mirror types passed "
                        "for creating Mirroring session");
                return false;
            }
            entry->set_mode((BASE_MIRROR_MODE_t)cps_api_object_attr_data_u32(it.attr));
            attrs.add(BASE_MIRROR_ENTRY_TYPE);
            break;
        }
    }

    if(update){
        if(!nas_mirror_update_attrs(entry,attrs)) return false;
        if(new_intf_map.size() > 0){
            if(!entry->update_src_intf_map(new_intf_map)) return false;
        }
    }
    else{
        if(!nas_mirror_validate_attr(attrs)) return false;
    }

    if(entry->get_mode() == BASE_MIRROR_MODE_RSPAN ){
        if(!nas_mirror_fill_rspan_attr(obj,entry,update)){
            return false;
        }
    }

    if(entry->get_mode() == BASE_MIRROR_MODE_ERSPAN){
       if(!nas_mirror_fill_erspan_attr(obj,entry,update)){
           return false;
       }
    }

    return true;
}
static bool nas_mirror_fill_erspan_attr(cps_api_object_t obj, nas_mirror_entry *entry, bool update){

    nas::attr_set_t attrs;
    cps_api_object_it_t it;
    cps_api_object_it_begin(obj,&it);

    for ( ; cps_api_object_it_valid(&it) ; cps_api_object_it_next(&it) ) {

        int id = (int) cps_api_object_attr_id(it.attr);
        switch (id) {

        case BASE_MIRROR_ENTRY_ERSPAN_VLAN_ID:
            if(attrs.contains(BASE_MIRROR_ENTRY_ERSPAN_VLAN_ID)){
                NAS_MIRROR_LOG(ERR,ev_log_s_MINOR,"Multiple VLAN Ids passed for creating ERSPAN Mirroring session");
                   return false;
            }
            entry->set_vlan(cps_api_object_attr_data_u32(it.attr));
            attrs.add(BASE_MIRROR_ENTRY_ERSPAN_VLAN_ID);
            break;

        case BASE_MIRROR_ENTRY_DESTINATION_IP:
            if( attrs.contains(BASE_MIRROR_ENTRY_DESTINATION_IP)){
                NAS_MIRROR_LOG(ERR,ev_log_s_MINOR,"Multiple Dst IP Address passed for creating ERSPAN Mirroring session");
                return false;
            }
            entry->set_dst_ip(cps_api_object_attr_data_u32(it.attr));
            attrs.add(BASE_MIRROR_ENTRY_DESTINATION_IP);
            break;

        case BASE_MIRROR_ENTRY_SOURCE_IP:
            if( attrs.contains(BASE_MIRROR_ENTRY_SOURCE_IP)){
                NAS_MIRROR_LOG(ERR,ev_log_s_MINOR,"Multiple Source IP Address passed for creating ERSPAN Mirroring session");
                return false;
            }
            entry->set_src_ip(cps_api_object_attr_data_u32(it.attr));
            attrs.add(BASE_MIRROR_ENTRY_SOURCE_IP);
            break;


        case BASE_MIRROR_ENTRY_DEST_MAC:
            if( attrs.contains(BASE_MIRROR_ENTRY_DEST_MAC)){
                NAS_MIRROR_LOG(ERR,ev_log_s_MINOR,"Multiple Dst MAC Address passed for creating ERSPAN Mirroring session");
                return false;
            }

            entry->set_dst_mac(cps_api_object_attr_data_bin(it.attr));
            attrs.add(BASE_MIRROR_ENTRY_DEST_MAC);
            break;

        case BASE_MIRROR_ENTRY_SOURCE_MAC:
            if( attrs.contains(BASE_MIRROR_ENTRY_SOURCE_MAC)){
                NAS_MIRROR_LOG(ERR,ev_log_s_MINOR,"Multiple Source MAC Address passed for creating ERSPAN Mirroring session");
                return false;
            }
            entry->set_src_mac(cps_api_object_attr_data_bin(it.attr));
            attrs.add(BASE_MIRROR_ENTRY_SOURCE_MAC);
            break;
        }
    }

    if(update){
        return nas_mirror_update_attrs(entry,attrs);
    }
    return nas_mirror_validate_erspan_attr(attrs);
}
/**
  * This function provides NAS-QoS priority_group stats CPS API clear function
  * To clear the priority_group stats, set relevant counters to zero
  * @Param    Standard CPS API params
  * @Return   Standard Error Code
  */
cps_api_return_code_t nas_qos_cps_api_priority_group_stat_clear (void * context,
                                            cps_api_transaction_params_t * param,
                                            size_t ix)
{
    cps_api_object_t obj = cps_api_object_list_get(param->change_list,ix);
    cps_api_operation_types_t op = cps_api_object_type_operation(cps_api_object_key(obj));

    if (op != cps_api_oper_SET)
        return NAS_QOS_E_UNSUPPORTED;

    cps_api_object_attr_t port_id_attr = cps_api_get_key_data(obj, BASE_QOS_PRIORITY_GROUP_STAT_PORT_ID);
    cps_api_object_attr_t local_id_attr = cps_api_get_key_data(obj, BASE_QOS_PRIORITY_GROUP_STAT_LOCAL_ID);

    if (port_id_attr == NULL ||
        local_id_attr == NULL) {
        EV_LOG_TRACE(ev_log_t_QOS, 3, "NAS-QOS",
                "Incomplete key: port-id, priority_group local id must be specified\n");
        return NAS_QOS_E_MISSING_KEY;
    }
    uint32_t switch_id = 0;
    nas_qos_priority_group_key_t key;
    key.port_id = cps_api_object_attr_data_u32(port_id_attr);
    key.local_id = *(uint8_t *)cps_api_object_attr_data_bin(local_id_attr);


    EV_LOG_TRACE(ev_log_t_QOS, 3, "NAS-QOS",
            "Read switch id %u, port_id %u priority_group local_id %u stat\n",
            switch_id, key.port_id,  key.local_id);

    std_mutex_simple_lock_guard p_m(&priority_group_mutex);

    // If the port is not be initialized yet, initialize it in NAS
    nas_qos_port_priority_group_init(key.port_id);

    nas_qos_switch *switch_p = nas_qos_get_switch(switch_id);
    if (switch_p == NULL) {
        EV_LOG_TRACE(ev_log_t_QOS, 3, "NAS-QOS", "switch_id %u not found", switch_id);
        return NAS_QOS_E_FAIL;
    }

    nas_qos_priority_group * priority_group_p = switch_p->get_priority_group(key);
    if (priority_group_p == NULL) {
        EV_LOG_TRACE(ev_log_t_QOS, 3, "NAS-QOS", "Priority Group not found");
        return NAS_QOS_E_FAIL;
    }

    std::vector<BASE_QOS_PRIORITY_GROUP_STAT_t> counter_ids;

    cps_api_object_it_t it;
    cps_api_object_it_begin(obj,&it);
    for ( ; cps_api_object_it_valid(&it) ; cps_api_object_it_next(&it) ) {
        cps_api_attr_id_t id = cps_api_object_attr_id(it.attr);
        switch (id) {
        case BASE_QOS_PRIORITY_GROUP_STAT_PORT_ID:
        case BASE_QOS_PRIORITY_GROUP_STAT_LOCAL_ID:
            break;

        case BASE_QOS_PRIORITY_GROUP_STAT_PACKETS:
        case BASE_QOS_PRIORITY_GROUP_STAT_BYTES:
        case BASE_QOS_PRIORITY_GROUP_STAT_WATERMARK_BYTES:
            counter_ids.push_back((BASE_QOS_PRIORITY_GROUP_STAT_t)id);
            break;

        case BASE_QOS_PRIORITY_GROUP_STAT_CURRENT_OCCUPANCY_BYTES:
            // READ-only
            break;

        default:
            EV_LOG_TRACE(ev_log_t_QOS, 3, "NAS-QOS",
                    "Unknown priority_group STAT flag: %u, ignored", id);
            break;
        }
    }

    if (ndi_qos_clear_priority_group_stats(priority_group_p->get_ndi_port_id(),
                                priority_group_p->ndi_obj_id(),
                                &counter_ids[0],
                                counter_ids.size()) != STD_ERR_OK) {
        EV_LOG_TRACE(ev_log_t_QOS, 3, "NAS-QOS", "Priority Group stats clear failed");
        return NAS_QOS_E_FAIL;
    }


    return  cps_api_ret_code_OK;

}
/**
  * This function provides NAS-QoS priority_group stats CPS API read function
  * @Param    Standard CPS API params
  * @Return   Standard Error Code
  */
cps_api_return_code_t nas_qos_cps_api_priority_group_stat_read (void * context,
                                            cps_api_get_params_t * param,
                                            size_t ix)
{

    cps_api_object_t obj = cps_api_object_list_get(param->filters, ix);
    cps_api_object_attr_t port_id_attr = cps_api_get_key_data(obj, BASE_QOS_PRIORITY_GROUP_STAT_PORT_ID);
    cps_api_object_attr_t local_id_attr = cps_api_get_key_data(obj, BASE_QOS_PRIORITY_GROUP_STAT_LOCAL_ID);

    if (port_id_attr == NULL ||
        local_id_attr == NULL ) {
        EV_LOG_TRACE(ev_log_t_QOS, 3, "NAS-QOS",
                "Incomplete key: port-id, priority_group local id must be specified\n");
        return NAS_QOS_E_MISSING_KEY;
    }
    uint32_t switch_id = 0;
    nas_qos_priority_group_key_t key;
    key.port_id = cps_api_object_attr_data_u32(port_id_attr);
    key.local_id = *(uint8_t *)cps_api_object_attr_data_bin(local_id_attr);


    EV_LOG_TRACE(ev_log_t_QOS, 3, "NAS-QOS",
            "Read switch id %u, port_id %u priority_group local id %u stat\n",
            switch_id, key.port_id, key.local_id);

    std_mutex_simple_lock_guard p_m(&priority_group_mutex);

    // If the port is not be initialized yet, initialize it in NAS
    nas_qos_port_priority_group_init(key.port_id);

    nas_qos_switch *switch_p = nas_qos_get_switch(switch_id);
    if (switch_p == NULL) {
        EV_LOG_TRACE(ev_log_t_QOS, 3, "NAS-QOS", "switch_id %u not found", switch_id);
        return NAS_QOS_E_FAIL;
    }

    nas_qos_priority_group * priority_group_p = switch_p->get_priority_group(key);
    if (priority_group_p == NULL) {
        EV_LOG_TRACE(ev_log_t_QOS, 3, "NAS-QOS", "Priority Group not found");
        return NAS_QOS_E_FAIL;
    }

    nas_qos_priority_group_stat_counter_t stats = {0};
    std::vector<BASE_QOS_PRIORITY_GROUP_STAT_t> counter_ids;

    cps_api_object_it_t it;
    cps_api_object_it_begin(obj,&it);
    for ( ; cps_api_object_it_valid(&it) ; cps_api_object_it_next(&it) ) {
        cps_api_attr_id_t id = cps_api_object_attr_id(it.attr);
        switch (id) {
        case BASE_QOS_PRIORITY_GROUP_STAT_PORT_ID:
        case BASE_QOS_PRIORITY_GROUP_STAT_LOCAL_ID:
            break;

        case BASE_QOS_PRIORITY_GROUP_STAT_PACKETS:
        case BASE_QOS_PRIORITY_GROUP_STAT_BYTES:
        case BASE_QOS_PRIORITY_GROUP_STAT_CURRENT_OCCUPANCY_BYTES:
        case BASE_QOS_PRIORITY_GROUP_STAT_WATERMARK_BYTES:
            counter_ids.push_back((BASE_QOS_PRIORITY_GROUP_STAT_t)id);
            break;

        default:
            EV_LOG_TRACE(ev_log_t_QOS, 3, "NAS-QOS", "Unknown priority_group STAT flag: %u, ignored", id);
            break;
        }
    }

    if (ndi_qos_get_priority_group_stats(priority_group_p->get_ndi_port_id(),
                                priority_group_p->ndi_obj_id(),
                                &counter_ids[0],
                                counter_ids.size(),
                                &stats) != STD_ERR_OK) {
        EV_LOG_TRACE(ev_log_t_QOS, 3, "NAS-QOS", "Priority Group stats get failed");
        return NAS_QOS_E_FAIL;
    }

    // return stats objects to cps-app
    cps_api_object_t ret_obj = cps_api_object_list_create_obj_and_append(param->list);
    if (ret_obj == NULL) {
        return cps_api_ret_code_ERR;
    }

    cps_api_key_from_attr_with_qual(cps_api_object_key(ret_obj),
            BASE_QOS_PRIORITY_GROUP_STAT_OBJ,
            cps_api_qualifier_TARGET);
    cps_api_set_key_data(ret_obj, BASE_QOS_PRIORITY_GROUP_STAT_PORT_ID,
            cps_api_object_ATTR_T_U32,
            &(key.port_id), sizeof(uint32_t));
    cps_api_set_key_data(ret_obj, BASE_QOS_PRIORITY_GROUP_STAT_LOCAL_ID,
            cps_api_object_ATTR_T_BIN,
            &(key.local_id), sizeof(uint8_t));

    uint64_t val64;
    for (uint_t i=0; i< counter_ids.size(); i++) {
        BASE_QOS_PRIORITY_GROUP_STAT_t id = counter_ids[i];
        val64 = get_stats_by_type(&stats, id);
        cps_api_object_attr_add_u64(ret_obj, id, val64);
    }

    return  cps_api_ret_code_OK;

}
bool cps_api_nodes::load_groups() {
    cps_api_object_guard og(cps_api_object_create());
    if (!cps_api_key_from_attr_with_qual(cps_api_object_key(og.get()),CPS_NODE_GROUP, cps_api_qualifier_TARGET)) {
        EV_LOG(ERR,DSAPI,0,"CPS-CLS-MAP","Meta data for cluster set is not loaded.");
        return false;
    }
    cps_db::connection_request b(cps_db::ProcessDBCache(),DEFAULT_REDIS_ADDR);

    if (!b.valid()) {
        return false;
    }

    group_data_t cpy;

    cps_api_object_list_guard lg(cps_api_object_list_create());
    if (!cps_db::get_objects(b.get(),og.get(),lg.get())) return false;

    for (size_t ix = 0,mx = cps_api_object_list_size(lg.get()); ix < mx ; ++ix ) {
        cps_api_object_t o = cps_api_object_list_get(lg.get(),ix);
        std::vector<_db_node_data> v;
        const char *name = (const char*) cps_api_object_get_data(o,CPS_NODE_GROUP_NAME);
        _node_data nd;



        cps_api_node_data_type_t *_type = (cps_api_node_data_type_t*)cps_api_object_get_data(o,CPS_NODE_GROUP_TYPE);
        if (_type==nullptr) {
            EV_LOGGING(DSAPI,ERR,"NODE-LOAD","Could not get the group type for group %s",name);
            return false;
        }
        nd.type = *_type;
        cps_api_object_attr_t _node_group = cps_api_object_attr_get(o,CPS_NODE_GROUP_NODE);
        if (_node_group==nullptr) continue; ///TODO log

        cps_api_object_it_t it;
        cps_api_object_it_from_attr(_node_group,&it);
        cps_api_object_it_inside(&it);

        while (cps_api_object_it_valid(&it)) {
            cps_api_attr_id_t id = cps_api_object_attr_id(it.attr);
            (void)id;
            cps_api_object_it_t elem = it;

            cps_api_object_it_inside(&elem);

            if (!cps_api_object_it_valid(&elem)) continue;
            _db_node_data db_node;
            cps_api_object_attr_t _ip =cps_api_object_it_find(&elem,CPS_NODE_GROUP_NODE_IP);
            cps_api_object_attr_t _name =cps_api_object_it_find(&elem,CPS_NODE_GROUP_NODE_NAME);
            if (_ip==nullptr || _name==nullptr) continue;
            const char *__ip = (const char*)cps_api_object_attr_data_bin(_ip);
            const char *__name =(const char*)cps_api_object_attr_data_bin(_name);

            if(nd.type == cps_api_node_data_1_PLUS_1_REDUNDENCY) {
                db_node._name = __name;
                if(get_port_info(name,&db_node)) {
                    auto lst = cps_string::split(std::string(__ip),":");
                    if (lst.size()!=2) {
                        return false;
                    }
                    db_node._addr = lst[0] + ':'+ db_node._addr;
                    v.push_back(std::move(db_node));
                }
                else {
                    EV_LOGGING(DSAPI,ERR,"CPS-DB","Failed to get port info for group %s and node %s",
                               name,__name);
                }

            }

            if(v.size()>0) {
                _db_node_map[name] = v;
                update_slaves(name);
            }

            _alias_map[__name] = __ip;

            const char * _alias = this->addr(__ip);
            if (_alias!=nullptr) __ip = _alias;

            nd._addrs.push_back(__ip);
            cps_api_object_it_next(&it);
        }
        cpy[name] = nd;
    }

    size_t _new_hash = cps_api_nodes::gen_hash(cpy);

    if (_new_hash==_hash) return false;  ///TODO is this unique enough?

    std::swap(_groups,cpy);
    _hash = _new_hash;
    return true;

}
/**
  * This function provides NAS-QoS SCHEDULER_GROUP CPS API read function
  * @Param      Standard CPS API params
  * @Return   Standard Error Code
  */
cps_api_return_code_t nas_qos_cps_api_scheduler_group_read (void * context,
                                            cps_api_get_params_t * param,
                                            size_t ix)
{
    cps_api_object_t obj = cps_api_object_list_get(param->filters, ix);
    cps_api_object_attr_t sg_attr = cps_api_get_key_data(obj, BASE_QOS_SCHEDULER_GROUP_ID);
    uint32_t port_id = 0, level;
    bool have_port = false, have_level = false;
    if (!sg_attr) {
        cps_api_object_it_t it;
        cps_api_object_it_begin(obj, &it);
        for ( ; cps_api_object_it_valid(&it); cps_api_object_it_next(&it)) {
            cps_api_attr_id_t id = cps_api_object_attr_id(it.attr);
            if (id == BASE_QOS_SCHEDULER_GROUP_PORT_ID) {
                port_id = cps_api_object_attr_data_u32(it.attr);
                have_port = true;
            } else if (id == BASE_QOS_SCHEDULER_GROUP_LEVEL) {
                level = cps_api_object_attr_data_u32(it.attr);
                have_level = true;
            }
        }
    }

    if (!sg_attr && !have_port) {
        EV_LOG_TRACE(ev_log_t_QOS, ev_log_s_MINOR, "NAS-QOS",
                     "Invalid input attributes for reading");
        return NAS_QOS_E_MISSING_KEY;
    }

    uint_t switch_id = 0;
    nas_obj_id_t scheduler_group_id = (sg_attr? cps_api_object_attr_data_u64(sg_attr): 0);

    EV_LOG_TRACE(ev_log_t_QOS, 3, "NAS-QOS", "Read switch id %u, scheduler_group id 0x%016lX\n",
                    switch_id, scheduler_group_id);

    std_mutex_simple_lock_guard p_m(&scheduler_group_mutex);

    if (have_port) {
        nas_qos_port_scheduler_group_init(port_id);
    }
    nas_qos_switch * p_switch = nas_qos_get_switch(switch_id);
    if (p_switch == NULL)
        return NAS_QOS_E_FAIL;

    std::vector<nas_qos_scheduler_group *> sg_list;
    if (sg_attr) {
        nas_qos_scheduler_group *scheduler_group = p_switch->get_scheduler_group(scheduler_group_id);
        if (scheduler_group == NULL) {
            EV_LOG_TRACE(ev_log_t_QOS, ev_log_s_MINOR, "NAS-QOS",
                         "Could not find scheduler group with ID %llu", scheduler_group_id);
            return NAS_QOS_E_FAIL;
        }
        sg_list.push_back(scheduler_group);
    } else {
        int match_level = have_level ? (int)level : -1;
        p_switch->get_port_scheduler_groups(port_id, match_level, sg_list);
    }

    /* fill in data */
    cps_api_object_t ret_obj;

    for (auto scheduler_group: sg_list) {
        ret_obj = cps_api_object_list_create_obj_and_append (param->list);
        if (ret_obj == NULL) {
            EV_LOG_TRACE(ev_log_t_QOS, ev_log_s_MINOR, "NAS-QOS",
                         "Failed to create cps object");
            return cps_api_ret_code_ERR;
        }

        cps_api_key_from_attr_with_qual(cps_api_object_key(ret_obj),BASE_QOS_SCHEDULER_GROUP_OBJ,
                cps_api_qualifier_TARGET);
        cps_api_set_key_data(ret_obj, BASE_QOS_SCHEDULER_GROUP_SWITCH_ID,
                cps_api_object_ATTR_T_U32,
                &switch_id, sizeof(uint32_t));
        scheduler_group_id = scheduler_group->get_scheduler_group_id();
        cps_api_set_key_data(ret_obj, BASE_QOS_SCHEDULER_GROUP_ID,
                cps_api_object_ATTR_T_U64,
                &scheduler_group_id, sizeof(uint64_t));

        cps_api_object_attr_add_u32(ret_obj,
                                    BASE_QOS_SCHEDULER_GROUP_CHILD_COUNT,
                                    scheduler_group->get_child_count());

        for (uint32_t idx = 0; idx < scheduler_group->get_child_count(); idx++) {
            cps_api_object_attr_add_u64(ret_obj,
                                        BASE_QOS_SCHEDULER_GROUP_CHILD_LIST,
                                        scheduler_group->get_child_id(idx));
        }

        cps_api_object_attr_add_u32(ret_obj, BASE_QOS_SCHEDULER_GROUP_PORT_ID,
                                    scheduler_group->get_port_id());
        cps_api_object_attr_add_u32(ret_obj, BASE_QOS_SCHEDULER_GROUP_LEVEL,
                                    scheduler_group->get_level());
        cps_api_object_attr_add_u64(ret_obj, BASE_QOS_SCHEDULER_GROUP_SCHEDULER_PROFILE_ID,
                                    scheduler_group->get_scheduler_profile_id());
    }

    return cps_api_ret_code_OK;
}