static t_std_error dn_nas_get_phy_media_default_setting(PLATFORM_MEDIA_TYPE_t media_type, cps_api_object_t obj) { cps_api_get_params_t gp; cps_api_get_request_init(&gp); cps_api_get_request_guard rg(&gp); cps_api_object_t media_obj = cps_api_object_list_create_obj_and_append(gp.filters); t_std_error rc = STD_ERR_MK(e_std_err_INTERFACE, e_std_err_code_FAIL, 0); do { if (!cps_api_key_from_attr_with_qual(cps_api_object_key(media_obj), BASE_MEDIA_MEDIA_INFO_OBJ, cps_api_qualifier_OBSERVED)) { break; } cps_api_set_key_data_uint(media_obj, BASE_MEDIA_MEDIA_INFO_MEDIA_TYPE, &media_type, sizeof(media_type)); cps_api_object_attr_add_u32(media_obj, BASE_MEDIA_MEDIA_INFO_MEDIA_TYPE, media_type); if (cps_api_get(&gp) != cps_api_ret_code_OK) break; if (0 == cps_api_object_list_size(gp.list)) break; media_obj = cps_api_object_list_get(gp.list,0); if (!cps_api_object_clone(obj, media_obj)) { break; } rc = STD_ERR_OK; } while(0); return rc; }
extern "C" cps_api_return_code_t cps_api_get_objs(cps_api_object_t filt, cps_api_object_list_t obj_list, size_t retry_count, size_t ms_delay_between) { cps_api_return_code_t rc; cps_api_get_params_t get_req; if ((rc=cps_api_get_request_init(&get_req))!=cps_api_ret_code_OK) return rc; cps_api_get_request_guard rg(&get_req); cps_api_object_t obj = cps_api_object_list_create_obj_and_append(get_req.filters); if (obj==nullptr) return cps_api_ret_code_ERR; if (!cps_api_object_clone(obj,filt)) return cps_api_ret_code_ERR; if (ms_delay_between==0) ms_delay_between = BASIC_WAIT_TIME; ms_delay_between = MILLI_TO_MICRO(ms_delay_between); cps_api_object_list_t _tmp_lst = get_req.list; get_req.list = obj_list; bool inf = retry_count==0; while (inf || (retry_count >0) ) { rc = cps_api_get(&get_req); if (rc==cps_api_ret_code_OK) break; if (!inf) --retry_count; std_usleep(ms_delay_between); } get_req.list = _tmp_lst; return rc; }
t_std_error nas_get_lag_if_index (uint64_t ndi_port, hal_ifindex_t *lag_if_index) { cps_api_get_params_t gp; cps_api_get_request_init(&gp); cps_api_get_request_guard rg(&gp); EV_LOG_TRACE(ev_log_t_INTERFACE, 2, "INTF-C", "querying for ifindex of ndi lag id 0x%x ", ndi_port); cps_api_object_t obj = cps_api_object_list_create_obj_and_append(gp.filters); cps_api_key_from_attr_with_qual(cps_api_object_key(obj), DELL_BASE_IF_CMN_IF_INTERFACES_INTERFACE_OBJ, cps_api_qualifier_TARGET); cps_api_object_attr_add_u64(obj,BASE_IF_LAG_IF_INTERFACES_INTERFACE_LAG_OPAQUE_DATA, ndi_port); cps_api_object_attr_add(obj,IF_INTERFACES_INTERFACE_TYPE, (const char *)IF_INTERFACE_TYPE_IANAIFT_IANA_INTERFACE_TYPE_IANAIFT_IEEE8023ADLAG, sizeof(IF_INTERFACE_TYPE_IANAIFT_IANA_INTERFACE_TYPE_IANAIFT_IEEE8023ADLAG)); if (cps_api_get(&gp)==cps_api_ret_code_OK) { size_t mx = cps_api_object_list_size(gp.list); for (size_t ix = 0 ; ix < mx ; ++ix ) { cps_api_object_t obj = cps_api_object_list_get(gp.list,ix); cps_api_object_it_t it; cps_api_object_it_begin(obj,&it); cps_api_object_attr_t attr = cps_api_object_it_find(&it, DELL_BASE_IF_CMN_IF_INTERFACES_INTERFACE_IF_INDEX); if (!attr) { return STD_ERR(INTERFACE, FAIL, 0); } *lag_if_index = cps_api_object_attr_data_u32(attr); } } return(STD_ERR_OK); }
TEST(std_nas_route_test, nas_route_arp_get) { cps_api_get_params_t gp; cps_api_get_request_init(&gp); cps_api_object_t obj = cps_api_object_list_create_obj_and_append(gp.filters); cps_api_key_from_attr_with_qual(cps_api_object_key(obj),BASE_ROUTE_OBJ_NBR, cps_api_qualifier_TARGET); unsigned short af = 2; cps_api_set_key_data(obj,BASE_ROUTE_OBJ_NBR_AF,cps_api_object_ATTR_T_U16, &af,sizeof(af)); if (cps_api_get(&gp)==cps_api_ret_code_OK) { size_t mx = cps_api_object_list_size(gp.list); for ( size_t ix = 0 ; ix < mx ; ++ix ) { obj = cps_api_object_list_get(gp.list,ix); std::cout<<"ARP ENTRY "<<std::endl; std::cout<<"================================="<<std::endl; nas_route_dump_arp_object_content(obj); std::cout<<std::endl; } } cps_api_get_request_close(&gp); }
t_std_error dn_nas_lag_get_ndi_ids (hal_ifindex_t if_index, nas_ndi_obj_id_table_handle_t ndi_id_data) { if(ndi_id_data == NULL) { EV_LOG_ERR(ev_log_t_INTERFACE, 2, "INTF-C","Input ndi_data is NULL"); return(STD_ERR(INTERFACE, PARAM, 0)); } cps_api_get_params_t gp; cps_api_get_request_init(&gp); cps_api_object_t obj = cps_api_object_list_create_obj_and_append(gp.filters); t_std_error rc = STD_ERR(INTERFACE, FAIL, 0); do { if (!cps_api_key_from_attr_with_qual(cps_api_object_key(obj), DELL_BASE_IF_CMN_IF_INTERFACES_INTERFACE_OBJ, cps_api_qualifier_TARGET)) { break; } cps_api_object_attr_add_u32(obj,DELL_BASE_IF_CMN_IF_INTERFACES_INTERFACE_IF_INDEX, if_index); cps_api_object_attr_add(obj,IF_INTERFACES_INTERFACE_TYPE, (const char *)IF_INTERFACE_TYPE_IANAIFT_IANA_INTERFACE_TYPE_IANAIFT_IEEE8023ADLAG, sizeof(IF_INTERFACE_TYPE_IANAIFT_IANA_INTERFACE_TYPE_IANAIFT_IEEE8023ADLAG)); if (cps_api_get(&gp) != cps_api_ret_code_OK) break; if (0 == cps_api_object_list_size(gp.list)) break; obj = cps_api_object_list_get(gp.list,0); cps_api_attr_id_t attr_list[] = {BASE_IF_LAG_IF_INTERFACES_INTERFACE_LAG_OPAQUE_DATA}; if (!nas_ndi_obj_id_table_cps_unserialize (ndi_id_data, obj, attr_list, sizeof (attr_list)/sizeof (cps_api_attr_id_t))) { EV_LOG_ERR(ev_log_t_INTERFACE, 2, "INTF-C","Failed to get LAG opaque data"); break; } rc = STD_ERR_OK; } while (0); cps_api_get_request_close(&gp); return rc; }
extern "C" t_std_error dn_hal_get_interface_mac(hal_ifindex_t if_index, hal_mac_addr_t mac_addr) { if(mac_addr == NULL) { EV_LOG_TRACE(ev_log_t_INTERFACE, 2, "INTF-C","mac_addr_ptr NULL"); return(STD_ERR_MK(e_std_err_INTERFACE, e_std_err_code_PARAM, 0)); } cps_api_get_params_t gp; cps_api_get_request_init(&gp); cps_api_object_t obj = cps_api_object_list_create_obj_and_append(gp.filters); t_std_error rc = STD_ERR_MK(e_std_err_INTERFACE, e_std_err_code_FAIL, 0); do { if (!cps_api_key_from_attr_with_qual(cps_api_object_key(obj), DELL_BASE_IF_CMN_IF_INTERFACES_INTERFACE_OBJ, cps_api_qualifier_TARGET)) { break; } cps_api_object_attr_add_u32(obj,DELL_BASE_IF_CMN_IF_INTERFACES_INTERFACE_IF_INDEX, if_index); if (cps_api_get(&gp) != cps_api_ret_code_OK) break; if (0 == cps_api_object_list_size(gp.list)) break; obj = cps_api_object_list_get(gp.list,0); cps_api_object_attr_t attr = cps_api_object_attr_get(obj,DELL_IF_IF_INTERFACES_INTERFACE_PHYS_ADDRESS); if (attr != NULL) { void *_mac_str = cps_api_object_attr_data_bin(attr); size_t addr_len = strlen(static_cast<char *>(_mac_str)); EV_LOG_TRACE(ev_log_t_INTERFACE, 2, "INTF-C","mac_addr %s, len %d", _mac_str, addr_len); if (std_string_to_mac((hal_mac_addr_t *)mac_addr, static_cast<const char *>(_mac_str), addr_len)) { rc = STD_ERR_OK; } } } while (0); cps_api_get_request_close(&gp); return rc; }
t_std_error nas_switch_get_sys_mac_base(hal_mac_addr_t *mac_base) { if(mac_base == NULL) { EV_LOG(ERR,NAS_L2,0,"NAS-L2-SWITCH","invalid parameter"); return(STD_ERR_MK(e_std_err_BOARD, e_std_err_code_PARAM, 0)); } cps_api_get_params_t gp; cps_api_get_request_init(&gp); cps_api_object_t obj = cps_api_object_list_create_obj_and_append(gp.filters); t_std_error rc = STD_ERR_MK(e_std_err_BOARD, e_std_err_code_FAIL, 0); do { if (!cps_api_key_from_attr_with_qual(cps_api_object_key(obj), BASE_SWITCH_SWITCHING_ENTITIES_SWITCHING_ENTITY, cps_api_qualifier_TARGET)) { break; } uint32_t switch_id = 0; /* default switch ID. @TODO add switch id */ cps_api_set_key_data_uint(obj, BASE_SWITCH_SWITCHING_ENTITIES_SWITCHING_ENTITY_SWITCH_ID, &switch_id, sizeof(switch_id)); if (cps_api_get(&gp) != cps_api_ret_code_OK) break; if (0 == cps_api_object_list_size(gp.list)) break; obj = cps_api_object_list_get(gp.list,0); cps_api_object_attr_t attr = cps_api_object_attr_get(obj, BASE_SWITCH_SWITCHING_ENTITIES_SWITCHING_ENTITY_DEFAULT_MAC_ADDRESS); if (attr != NULL) { memcpy(*mac_base,cps_api_object_attr_data_bin(attr),sizeof(*mac_base)); rc = STD_ERR_OK; } } while (0); cps_api_get_request_close(&gp); return rc; }
TEST(std_nas_route_test, nas_peer_routing_config_get) { cps_api_get_params_t gp; cps_api_get_request_init(&gp); cps_api_object_t obj = cps_api_object_list_create_obj_and_append(gp.filters); cps_api_key_from_attr_with_qual(cps_api_object_key(obj),BASE_ROUTE_PEER_ROUTING_CONFIG_OBJ, cps_api_qualifier_TARGET); if (cps_api_get(&gp)==cps_api_ret_code_OK) { size_t mx = cps_api_object_list_size(gp.list); for ( size_t ix = 0 ; ix < mx ; ++ix ) { obj = cps_api_object_list_get(gp.list,ix); std::cout<<"Peer Routing Status "<<std::endl; std::cout<<"================================="<<std::endl; nas_route_dump_peer_routing_object_content(obj); std::cout<<std::endl; } } cps_api_get_request_close(&gp); }
TEST(std_nas_route_test, nas_route_ipv6_get) { cps_api_get_params_t gp; cps_api_get_request_init(&gp); cps_api_object_t obj = cps_api_object_list_create_obj_and_append(gp.filters); cps_api_key_from_attr_with_qual(cps_api_object_key(obj),BASE_ROUTE_OBJ_ENTRY, cps_api_qualifier_TARGET); unsigned int af = AF_INET6; cps_api_set_key_data(obj,BASE_ROUTE_OBJ_ENTRY_AF,cps_api_object_ATTR_T_U32, &af,sizeof(af)); if (cps_api_get(&gp)==cps_api_ret_code_OK) { size_t mx = cps_api_object_list_size(gp.list); std::cout<<"IPv6 FIB Entries"<<std::endl; std::cout<<"================================="<<std::endl; for ( size_t ix = 0 ; ix < mx ; ++ix ) { obj = cps_api_object_list_get(gp.list,ix); nas_route_dump_route_object_content(obj); std::cout<<std::endl; } } cps_api_get_request_close(&gp); }
PyObject * py_cps_get(PyObject *self, PyObject *args) { PyObject * param_list; cps_api_get_params_t gr; if (cps_api_get_request_init (&gr)==cps_api_ret_code_ERR) { py_set_error_string("Failed to initialize the get req"); return nullptr; } cps_api_get_request_guard rg(&gr); PyObject *res_obj; if (! PyArg_ParseTuple( args, "O!O", &PyList_Type, ¶m_list,&res_obj)) { py_set_error_string("Failed to parse input args."); return nullptr; } PyObject * lst = NULL; if (PyDict_Check(res_obj)) { PyObject *l = PyList_New(0); PyRef _l(l); if (l==NULL) { py_set_error_string("Can not create a list."); return nullptr; } PyObject *_prev = PyDict_GetItemString(res_obj,"list"); if (_prev!=NULL) { PyDict_DelItemString(res_obj,"list"); } if (!py_cps_util_set_item_to_dict(res_obj,"list",l)) { py_set_error_string("Can not create a list."); return nullptr; } lst = l; } if (PyList_Check(res_obj)) { lst = res_obj; } if (lst==NULL) { py_set_error_string("The return args are invalid."); return nullptr; } Py_ssize_t str_keys = PyList_Size(param_list); { Py_ssize_t ix = 0; for ( ;ix < str_keys ; ++ix ) { PyObject *strObj = PyList_GetItem(param_list, ix); if (PyString_Check(strObj)) { // cps_api_object_t o = cps_api_object_list_create_obj_and_append(gr.filters); if (o==NULL) { py_set_error_string("Memory allocation error."); return nullptr; } if (!cps_api_key_from_string(cps_api_object_key(o),PyString_AsString(strObj))) { py_set_error_string("Memory allocation error."); return nullptr; } } if (PyDict_Check(strObj)) { cps_api_object_t o = dict_to_cps_obj(strObj); if (o==NULL) { py_set_error_string("Can't convert from a python to internal object"); return nullptr; } if (!cps_api_object_list_append(gr.filters,o)) { cps_api_object_delete(o); py_set_error_string("Memory allocation error."); return nullptr; } } } } gr.keys = NULL; gr.key_count = 0; cps_api_return_code_t rc; { NonBlockingPythonContext l; rc = cps_api_get(&gr); } if (rc!=cps_api_ret_code_OK) { Py_RETURN_FALSE; } size_t ix = 0; size_t mx = cps_api_object_list_size(gr.list); for ( ; ix < mx ; ++ix) { cps_api_object_t obj = cps_api_object_list_get(gr.list,ix); PyObject *d = cps_obj_to_dict(obj); PyRef r(d); if (d==NULL) { py_set_error_string("Memory allocation error."); return nullptr; } if (PyList_Append(lst,d)) { py_set_error_string("Memory allocation error."); return nullptr; } } Py_RETURN_TRUE; }
int main() { if (!netlinl_test()) { exit(1); } cps_api_linux_init(); { cps_api_get_params_t gr; cps_api_get_request_init(&gr); cps_api_key_t if_key; cps_api_int_if_key_create(&if_key,false,0,0); gr.key_count = 1; gr.keys = &if_key; //ignore failures since all we want is to fill up the get request if (cps_api_get(&gr) != cps_api_ret_code_OK) { printf("cps_api_get() failed\n"); } cps_api_get_request_close(&gr); } cps_api_get_params_t gp; if (cps_api_get_request_init(&gp)!=cps_api_ret_code_OK) { exit(1); } cps_api_key_t key; cps_api_int_if_key_create(&key,false,0,0); gp.key_count = 1; gp.keys = &key; cps_api_transaction_params_t tr; if (cps_api_transaction_init(&tr)!=cps_api_ret_code_OK) { cps_api_get_request_close(&gp); return -1; } if (cps_api_get(&gp)==cps_api_ret_code_OK) { size_t ix = 0; size_t mx = cps_api_object_list_size(gp.list); char buff[1024]; for ( ; ix < mx ; ++ix ) { cps_api_object_t obj = cps_api_object_list_get(gp.list,ix); printf("Obj %d -> %s\n",(int)ix,cps_api_object_to_string(obj,buff,sizeof(buff))); cps_api_object_t clone = cps_api_object_create(); if (!cps_api_object_clone(clone,obj)) { printf("Clone Failed %d -> %s\n",(int)ix,cps_api_object_to_string(clone,buff,sizeof(buff))); } else { printf("Clone %d -> %s\n",(int)ix,cps_api_object_to_string(clone,buff,sizeof(buff))); } cps_api_set(&tr,clone); } } cps_api_get_request_close(&gp); if (cps_api_commit(&tr)!=cps_api_ret_code_OK) { return -1; } cps_api_transaction_close(&tr); return 0; }