extern "C" cps_api_return_code_t cps_api_commit_one(cps_api_operation_types_t type, cps_api_object_t obj, size_t retry_count, size_t ms_delay_between) { cps_api_transaction_params_t tr; if (cps_api_transaction_init(&tr)!=cps_api_ret_code_OK) { return cps_api_ret_code_ERR; } cps_api_transaction_guard tr_g(&tr); cps_api_key_set_attr(cps_api_object_key(obj),type); if (!cps_api_object_list_append(tr.change_list,obj)) { return cps_api_ret_code_ERR; } bool inf = retry_count==0; if (ms_delay_between==0) ms_delay_between = BASIC_WAIT_TIME; ms_delay_between = MILLI_TO_MICRO(ms_delay_between); cps_api_return_code_t rc = cps_api_ret_code_ERR; while (inf || (retry_count >0) ) { rc = cps_api_commit(&tr); if (rc==cps_api_ret_code_OK) { //since this will be erased by the caller remove it from the transaction list break; } if (!inf) --retry_count; std_usleep(ms_delay_between); } cps_api_object_list_remove(tr.change_list,0); return rc; }
static bool cps_api_clean_db_instance(const char *group) { cps_api_transaction_params_t tr; if (cps_api_transaction_init(&tr)!=cps_api_ret_code_OK) { return false; } cps_api_transaction_guard tg(&tr); cps_api_object_t db_obj = cps_api_object_create(); if(db_obj == nullptr ) return false; cps_api_key_from_attr_with_qual(cps_api_object_key(db_obj),CPS_DB_INSTANCE_OBJ, cps_api_qualifier_TARGET); cps_api_object_attr_add(db_obj,CPS_DB_INSTANCE_GROUP,group,strlen(group)+1); if(cps_api_delete(&tr,db_obj) != cps_api_ret_code_OK ) { return false; } if(cps_api_commit(&tr) != cps_api_ret_code_OK ) { EV_LOGGING(DSAPI,ERR,"CPS-DB","Failed to delete db instance for group %s",group); return false; } return true; }
TEST(std_nas_route_test, nas_route_mp_delete) { cps_api_object_t obj = cps_api_object_create(); //cps_api_key_init(cps_api_object_key(obj),cps_api_qualifier_TARGET, // cps_api_obj_CAT_BASE_ROUTE, BASE_ROUTE_OBJ_OBJ,0 ); cps_api_key_from_attr_with_qual(cps_api_object_key(obj), BASE_ROUTE_OBJ_OBJ,cps_api_qualifier_TARGET); cps_api_object_attr_add_u32(obj,BASE_ROUTE_OBJ_ENTRY_AF,AF_INET); cps_api_object_attr_add_u32(obj,BASE_ROUTE_OBJ_ENTRY_PREFIX_LEN,32); cps_api_object_attr_add_u32(obj,BASE_ROUTE_OBJ_ENTRY_VRF_ID,0); uint32_t ip; struct in_addr a; inet_aton("6.6.6.6",&a); ip=a.s_addr; cps_api_object_attr_add(obj,BASE_ROUTE_OBJ_ENTRY_ROUTE_PREFIX,&ip,sizeof(ip)); /* * CPS transaction */ cps_api_transaction_params_t tr; ASSERT_TRUE(cps_api_transaction_init(&tr)==cps_api_ret_code_OK); cps_api_delete(&tr,obj); ASSERT_TRUE(cps_api_commit(&tr)==cps_api_ret_code_OK); cps_api_transaction_close(&tr); printf("___________________________________________\n"); system("ip route show 6.6.6.6"); printf("___________________________________________\n"); }
TEST(std_nas_route_test, nas_neighbor_delete_static) { cps_api_object_t obj = cps_api_object_create(); //cps_api_key_init(cps_api_object_key(obj),cps_api_qualifier_TARGET, // cps_api_obj_CAT_BASE_ROUTE, BASE_ROUTE_OBJ_OBJ,0 ); cps_api_key_from_attr_with_qual(cps_api_object_key(obj), BASE_ROUTE_OBJ_OBJ,cps_api_qualifier_TARGET); cps_api_object_attr_add_u32(obj,BASE_ROUTE_OBJ_NBR_AF,AF_INET); cps_api_object_attr_add_u32(obj,BASE_ROUTE_OBJ_NBR_TYPE,BASE_ROUTE_RT_TYPE_STATIC); uint32_t ip; struct in_addr a; inet_aton("6.6.6.7",&a); ip=a.s_addr; cps_api_object_attr_add(obj,BASE_ROUTE_OBJ_NBR_ADDRESS,&ip,sizeof(ip)); int port_index = if_nametoindex("e101-001-0"); cps_api_object_attr_add_u32(obj,BASE_ROUTE_OBJ_NBR_IFINDEX, port_index); /* * CPS transaction */ cps_api_transaction_params_t tr; ASSERT_TRUE(cps_api_transaction_init(&tr)==cps_api_ret_code_OK); cps_api_delete(&tr,obj); ASSERT_TRUE(cps_api_commit(&tr)==cps_api_ret_code_OK); cps_api_transaction_close(&tr); }
TEST(std_nas_route_test, nas_neighbor_set) { cps_api_object_t obj = cps_api_object_create(); //cps_api_key_init(cps_api_object_key(obj),cps_api_qualifier_TARGET, // cps_api_obj_CAT_BASE_ROUTE, BASE_ROUTE_OBJ_OBJ,0 ); cps_api_key_from_attr_with_qual(cps_api_object_key(obj), BASE_ROUTE_OBJ_OBJ,cps_api_qualifier_TARGET); cps_api_object_attr_add_u32(obj,BASE_ROUTE_OBJ_NBR_AF,AF_INET); uint32_t ip; struct in_addr a; inet_aton("6.6.6.6",&a); ip=a.s_addr; cps_api_object_attr_add(obj,BASE_ROUTE_OBJ_NBR_ADDRESS,&ip,sizeof(ip)); int port_index = if_nametoindex("e101-001-0"); cps_api_object_attr_add_u32(obj,BASE_ROUTE_OBJ_NBR_IFINDEX, port_index); hal_mac_addr_t mac_addr = {0x00, 0x00, 0x00, 0xaa, 0xbb, 0xcc}; cps_api_object_attr_add(obj, BASE_ROUTE_OBJ_NBR_MAC_ADDR, &mac_addr, HAL_MAC_ADDR_LEN); /* * CPS transaction */ cps_api_transaction_params_t tr; ASSERT_TRUE(cps_api_transaction_init(&tr)==cps_api_ret_code_OK); cps_api_set(&tr,obj); ASSERT_TRUE(cps_api_commit(&tr)==cps_api_ret_code_OK); cps_api_transaction_close(&tr); }
TEST(std_nas_route_test, nas_route_add) { cps_api_object_t obj = cps_api_object_create(); cps_api_key_from_attr_with_qual(cps_api_object_key(obj), BASE_ROUTE_OBJ_OBJ,cps_api_qualifier_TARGET); //cps_api_key_init(cps_api_object_key(obj),cps_api_qualifier_TARGET, // cps_api_obj_CAT_BASE_ROUTE, BASE_ROUTE_OBJ_OBJ,0 ); /* * Check mandatory route attributes * BASE_ROUTE_OBJ_ENTRY_AF, BASE_ROUTE_OBJ_ENTRY_VRF_ID); * BASE_ROUTE_OBJ_ENTRY_ROUTE_PREFIX, BASE_ROUTE_OBJ_ENTRY_PREFIX_LEN; */ cps_api_object_attr_add_u32(obj,BASE_ROUTE_OBJ_ENTRY_AF,AF_INET); cps_api_object_attr_add_u32(obj,BASE_ROUTE_OBJ_ENTRY_VRF_ID,0); cps_api_object_attr_add_u32(obj,BASE_ROUTE_OBJ_ENTRY_PREFIX_LEN,32); uint32_t ip; struct in_addr a; inet_aton("6.6.6.6",&a); ip=a.s_addr; cps_api_object_attr_add(obj,BASE_ROUTE_OBJ_ENTRY_ROUTE_PREFIX,&ip,sizeof(ip)); cps_api_object_attr_add_u32(obj,BASE_ROUTE_OBJ_ENTRY_PREFIX_LEN,32); cps_api_attr_id_t ids[3]; const int ids_len = sizeof(ids)/sizeof(*ids); ids[0] = BASE_ROUTE_OBJ_ENTRY_NH_LIST; ids[1] = 0; ids[2] = BASE_ROUTE_OBJ_ENTRY_NH_LIST_NH_ADDR; /* * Set Loopback0 NH */ inet_aton("127.0.0.1",&a); ip=a.s_addr; cps_api_object_e_add(obj,ids,ids_len,cps_api_object_ATTR_T_BIN, &ip,sizeof(ip)); cps_api_object_attr_add_u32(obj,BASE_ROUTE_OBJ_ENTRY_NH_COUNT,1); /* * CPS transaction */ cps_api_transaction_params_t tr; ASSERT_TRUE(cps_api_transaction_init(&tr)==cps_api_ret_code_OK); cps_api_create(&tr,obj); ASSERT_TRUE(cps_api_commit(&tr)==cps_api_ret_code_OK); cps_api_transaction_close(&tr); }
TEST(std_nas_route_test, nas_route_delete_scale) { char ip_addr[256]; int i=1, j=1; uint32_t ip; struct in_addr a; for(i=1; i<65; i++) { for (j=1; j<251; j++) { cps_api_object_t obj = cps_api_object_create(); //cps_api_key_init(cps_api_object_key(obj),cps_api_qualifier_TARGET, // cps_api_obj_CAT_BASE_ROUTE, BASE_ROUTE_OBJ_OBJ,0 ); cps_api_key_from_attr_with_qual(cps_api_object_key(obj), BASE_ROUTE_OBJ_OBJ,cps_api_qualifier_TARGET); cps_api_object_attr_add_u32(obj,BASE_ROUTE_OBJ_ENTRY_AF,AF_INET); cps_api_object_attr_add_u32(obj,BASE_ROUTE_OBJ_ENTRY_PREFIX_LEN,32); cps_api_object_attr_add_u32(obj,BASE_ROUTE_OBJ_ENTRY_VRF_ID,0); snprintf(ip_addr,256, "11.10.%d.%d",i,j); //printf ("Delete Route:%s \n", ip_addr); inet_aton(ip_addr,&a); //inet_aton("6.6.6.6",&a); ip=a.s_addr; cps_api_object_attr_add(obj,BASE_ROUTE_OBJ_ENTRY_ROUTE_PREFIX,&ip,sizeof(ip)); /* * CPS transaction */ cps_api_transaction_params_t tr; ASSERT_TRUE(cps_api_transaction_init(&tr)==cps_api_ret_code_OK); cps_api_delete(&tr,obj); ASSERT_TRUE(cps_api_commit(&tr)==cps_api_ret_code_OK); cps_api_transaction_close(&tr); } } }
TEST(std_nas_route_test, nas_peer_routing_config_set) { cps_api_object_t obj = cps_api_object_create(); cps_api_key_from_attr_with_qual(cps_api_object_key(obj), BASE_ROUTE_PEER_ROUTING_CONFIG_OBJ,cps_api_qualifier_TARGET); cps_api_object_attr_add_u32(obj,BASE_ROUTE_PEER_ROUTING_CONFIG_VRF_ID,0); hal_mac_addr_t mac_addr = {0x01, 0x22, 0x23, 0x24, 0x25, 0x26}; cps_api_object_attr_add(obj, BASE_ROUTE_PEER_ROUTING_CONFIG_PEER_MAC_ADDR, &mac_addr, HAL_MAC_ADDR_LEN); /* * CPS transaction */ cps_api_transaction_params_t tr; ASSERT_TRUE(cps_api_transaction_init(&tr)==cps_api_ret_code_OK); cps_api_set(&tr,obj); ASSERT_TRUE(cps_api_commit(&tr)==cps_api_ret_code_OK); cps_api_transaction_close(&tr); }
PyObject * py_cps_trans(PyObject *self, PyObject *args) { PyObject *list; if (! PyArg_ParseTuple( args, "O!", &PyList_Type, &list)) return NULL; cps_api_transaction_params_t tr; if (cps_api_transaction_init(&tr)!=cps_api_ret_code_OK) { py_set_error_string("Memory allocation error."); return nullptr; } cps_api_transaction_guard pg(&tr); size_t ix = 0; size_t mx = PyList_Size(list); for ( ; ix < mx ; ++ix ) { PyObject *dict = PyList_GetItem(list,ix); if (!py_add_object_to_trans(&tr,dict)) { py_set_error_string("Could not translate the request to a transaction"); return nullptr; } } cps_api_return_code_t rc; { NonBlockingPythonContext l; rc = cps_api_commit(&tr); } if (rc!=cps_api_ret_code_OK) { Py_RETURN_FALSE; } ix = 0; mx = PyList_Size(list); for ( ; ix < mx ; ++ix ) { PyObject *dict = PyList_GetItem(list,ix); if (dict==NULL || !PyDict_Check(dict)) { py_set_error_string("Failed to convert the transaction response."); return nullptr; } PyObject *_req = PyDict_GetItemString(dict,"change"); if (_req==NULL || !PyDict_Check(_req)) { py_set_error_string("Failed to convert the transaction response."); return nullptr; } cps_api_object_t obj = cps_api_object_list_get(tr.change_list,ix); if (obj==NULL) { py_set_error_string("Failed to convert the transaction response."); return nullptr; } py_cps_util_set_item_to_dict(_req,"key", PyString_FromString(cps_key_to_string(cps_api_object_key(obj)).c_str())); if (PyDict_GetItemString(dict,"change")!=NULL) PyDict_DelItemString(dict,"change"); py_cps_util_set_item_to_dict(dict,"change",cps_obj_to_dict(obj)); } cps_api_transaction_close(&tr); Py_RETURN_TRUE; }
cps_api_return_code_t cps_api_create_global_instance(cps_api_node_group_t *group) { if(group == nullptr) return cps_api_ret_code_ERR; size_t local_node_ix; if(!cps_api_find_local_node(group,local_node_ix)) { EV_LOGGING(DSAPI,ERR,"SET-GLOBAL","Failed to find local node in group %s",group); return cps_api_ret_code_ERR; } if(local_node_ix >= group->addr_len) { return cps_api_ret_code_ERR; } cps_api_transaction_params_t tr; if (cps_api_transaction_init(&tr)!=cps_api_ret_code_OK) { EV_LOGGING(DSAPI,ERR,"SET-GLOBAL","Failed to init transaction"); return cps_api_ret_code_ERR; } cps_api_transaction_guard tg(&tr); cps_api_object_t db_obj = cps_api_object_create(); if(db_obj == nullptr ) return cps_api_ret_code_ERR; cps_api_key_from_attr_with_qual(cps_api_object_key(db_obj),CPS_DB_INSTANCE_OBJ, cps_api_qualifier_TARGET); cps_api_object_attr_add(db_obj,CPS_DB_INSTANCE_GROUP,group->id,strlen(group->id)+1); if(cps_api_create(&tr,db_obj) != cps_api_ret_code_OK ) { return cps_api_ret_code_ERR; } if(cps_api_commit(&tr) != cps_api_ret_code_OK ) { EV_LOGGING(DSAPI,ERR,"SET-GLOBAL","Failed to create new global instance for %s",group); return cps_api_ret_code_ERR; } cps_api_object_t ret_obj = cps_api_object_list_get(tr.change_list,0); if (ret_obj == nullptr ) { EV_LOGGING(DSAPI,ERR,"SET-GLOBAL","Failed to find db instance port info for %s",group); return cps_api_ret_code_ERR; } // Get the port where new db instance was started const char * db_port = (const char*) cps_api_object_get_data(ret_obj,CPS_DB_INSTANCE_PORT); std::string _db_port = db_port; /* * Push the group name local node id and its port to local/remote DB. */ cps_api_node_ident & node = group->addrs[local_node_ix]; cps_api_object_guard db_node(cps_api_object_create()); if (db_node.get() == nullptr) { return cps_api_ret_code_ERR; } cps_api_key_from_attr_with_qual(cps_api_object_key(db_node.get()),CPS_DB_INSTANCE_OBJ, cps_api_qualifier_TARGET); cps_api_set_key_data(db_node.get(),CPS_DB_INSTANCE_GROUP,cps_api_object_ATTR_T_BIN,group->id,strlen(group->id)+1); cps_api_set_key_data(db_node.get(),CPS_DB_INSTANCE_NODE_ID,cps_api_object_ATTR_T_BIN,node.node_name,strlen(node.node_name)+1); cps_api_object_attr_add(db_node.get(),CPS_DB_INSTANCE_PORT,_db_port.c_str(),strlen(_db_port.c_str())+1); if (!cps_api_node_set_iterate(group->id,[&db_node](const std::string &name,void *c) { cps_db::connection_request r(cps_db::ProcessDBCache(),name.c_str()); cps_db::store_object(r.get(),db_node.get()); return; },nullptr)) { return cps_api_ret_code_ERR; } return cps_api_ret_code_OK; }
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; }
//Scale tests for route add/delete TEST(std_nas_route_test, nas_route_add_scale) { char ip_addr[256]; int i=1, j=1 ,count=0; uint32_t ip; struct in_addr a; for(i=1; i<101; i++) { for (j=1; j<251; j++) { cps_api_object_t obj = cps_api_object_create(); cps_api_key_from_attr_with_qual(cps_api_object_key(obj), BASE_ROUTE_OBJ_OBJ,cps_api_qualifier_TARGET); //cps_api_key_init(cps_api_object_key(obj),cps_api_qualifier_TARGET, // cps_api_obj_CAT_BASE_ROUTE, BASE_ROUTE_OBJ_OBJ,0 ); /* * Check mandatory route attributes * BASE_ROUTE_OBJ_ENTRY_AF, BASE_ROUTE_OBJ_ENTRY_VRF_ID); * BASE_ROUTE_OBJ_ENTRY_ROUTE_PREFIX, BASE_ROUTE_OBJ_ENTRY_PREFIX_LEN; */ cps_api_object_attr_add_u32(obj,BASE_ROUTE_OBJ_ENTRY_AF,AF_INET); cps_api_object_attr_add_u32(obj,BASE_ROUTE_OBJ_ENTRY_VRF_ID,0); cps_api_object_attr_add_u32(obj,BASE_ROUTE_OBJ_ENTRY_PREFIX_LEN,32); snprintf(ip_addr,256, "11.10.%d.%d",i,j); // printf ("Add Route:%s, NH:1.1.1.4\n", ip_addr); inet_aton(ip_addr,&a); //inet_aton("6.6.6.6",&a); ip=a.s_addr; //printf ("Route:%d\n", ip); cps_api_object_attr_add(obj,BASE_ROUTE_OBJ_ENTRY_ROUTE_PREFIX,&ip,sizeof(ip)); cps_api_object_attr_add_u32(obj,BASE_ROUTE_OBJ_ENTRY_PREFIX_LEN,32); cps_api_attr_id_t ids[3]; const int ids_len = sizeof(ids)/sizeof(*ids); ids[0] = BASE_ROUTE_OBJ_ENTRY_NH_LIST; ids[1] = 0; ids[2] = BASE_ROUTE_OBJ_ENTRY_NH_LIST_NH_ADDR; /* * Set NH */ inet_aton("1.1.1.4",&a); ip=a.s_addr; cps_api_object_e_add(obj,ids,ids_len,cps_api_object_ATTR_T_BIN, &ip,sizeof(ip)); cps_api_object_attr_add_u32(obj,BASE_ROUTE_OBJ_ENTRY_NH_COUNT,1); /* * CPS transaction */ cps_api_transaction_params_t tr; ASSERT_TRUE(cps_api_transaction_init(&tr)==cps_api_ret_code_OK); cps_api_create(&tr,obj); ASSERT_TRUE(cps_api_commit(&tr)==cps_api_ret_code_OK); cps_api_transaction_close(&tr); count++; } } printf("Sent %d Routes\n", count); }