TEST(cps_api_key_cache,test_key_basic) { size_t mx = 100; while (mx-- > 1) { size_t ix = rand() % lst.size(); entry val; if (!cache.find(lst[ix],val,true)) { if (_missing_cache.find(lst[ix],val,true)) { continue; } char b1[100]; printf("Can't find %s\n",cps_api_key_print(lst[ix],b1,sizeof(b1))); continue; } ASSERT_TRUE(ix == val.ix); } mx = 100; while (mx-- > 1) { size_t ix = rand() % lst.size(); entry val; if (!cache.find(lst[ix],val,false)) { if (_missing_cache.find(lst[ix],val,true)) { continue; } char b1[100]; printf("Can't find %s\n",cps_api_key_print(lst[ix],b1,sizeof(b1))); continue; } char b1[100],b2[100]; printf("Found key %s looking for key %s\n",cps_api_key_print(&val.key,b2,sizeof(b2)), cps_api_key_print(lst[ix],b1,sizeof(b1))); } }
t_std_error nas_int_cps_init(cps_api_operation_handle_t handle) { cps_api_registration_functions_t f; char buff[CPS_API_KEY_STR_MAX]; memset(&f,0,sizeof(f)); if (!cps_api_key_from_attr_with_qual(&f.key,BASE_IF_PHY_PHYSICAL_OBJ,cps_api_qualifier_TARGET)) { EV_LOG(ERR,INTERFACE,0,"NAS-IF-REG","Could not translate %d to key %s", (int)(DELL_BASE_IF_CMN_IF_INTERFACES_INTERFACE_OBJ), cps_api_key_print(&f.key,buff,sizeof(buff)-1)); return STD_ERR(INTERFACE,FAIL,0); } EV_LOG(INFO,INTERFACE,0,"NAS-IF-REG","Registering for %s", cps_api_key_print(&f.key,buff,sizeof(buff)-1)); f.handle = handle; f._read_function = _phy_int_get; f._write_function = _phy_int_set; if (cps_api_register(&f)!=cps_api_ret_code_OK) { return STD_ERR(INTERFACE,FAIL,0); } t_std_error rc = STD_ERR_OK; npu_id_t npu = 0; npu_id_t npu_max = (npu_id_t)nas_switch_get_max_npus(); for ( ; npu < npu_max ; ++npu ) { rc = ndi_port_event_cb_register(npu,_ndi_port_event_update_); if (rc!=STD_ERR_OK) return rc; } if ((rc=nas_int_breakout_init(handle))!=STD_ERR_OK) return rc; return nas_int_logical_init(handle); }
cps_api_return_code_t cps_api_process_get_request(cps_api_get_params_t *param, size_t ix) { cps_api_return_code_t rc = cps_api_ret_code_ERR; char buff[SCRATCH_LOG_BUFF]; cps_api_channel_t handle; cps_api_key_t *key = cps_api_object_key(cps_api_object_list_get(param->filters,ix)); if (!cps_api_get_handle(*key,handle)) { EV_LOG(ERR,DSAPI,0,"NS","Failed to find owner for %s", cps_api_key_print(key,buff,sizeof(buff)-1)); return cps_api_ret_code_NO_SERVICE; } do { cps_api_object_t o = cps_api_object_list_get(param->filters,ix); if (o==NULL) { EV_LOG(ERR,DSAPI,0,"NS","Missing filters... %s", cps_api_key_print(key,buff,sizeof(buff)-1)); break; } if (!cps_api_send_one_object(handle,cps_api_msg_o_GET,o)) { EV_LOG(ERR,DSAPI,0,"NS","Failed to send request %s", cps_api_key_print(key,buff,sizeof(buff)-1)); break; } uint32_t op; do { if (param->timeout > 0) { fd_set rset; FD_ZERO(&rset); FD_SET(handle,&rset); if ((rc=cps_api_timeout_wait(handle,&rset,param->timeout,"CPS-OP-GET"))!=cps_api_ret_code_OK) { EV_LOG(ERR,DSAPI,0,"CPS-OP-GET","Send request %s timed out", cps_api_key_print(key,buff,sizeof(buff)-1)); break; } } size_t len; if (!cps_api_receive_header(handle,op,len)) break; if (op!=cps_api_msg_o_GET_RESP) break; cps_api_object_guard og (cps_api_receive_object(handle,len)); if (og.valid() && cps_api_object_list_append(param->list,og.get())) { og.release(); } else break; } while (op == cps_api_msg_o_GET_RESP); if (op!=cps_api_msg_o_GET_DONE) break; //leave an error code rc = cps_api_ret_code_OK; } while (0); cps_api_disconnect_owner(handle); return rc; }
TEST(cps_api_key_cache,test_missing_basic) { size_t mx = 100; while (mx-- > 1) { size_t ix = rand() % missing.size(); entry val; if (!cache.find(missing[ix],val,false)) { char b1[100]; printf("Can't find %s\n",cps_api_key_print(missing[ix],b1,sizeof(b1))); continue; } char b1[100],b2[100]; printf("Found key %s looking for key %s\n",cps_api_key_print(&val.key,b2,sizeof(b2)), cps_api_key_print(missing[ix],b1,sizeof(b1))); } }
static cps_api_return_code_t _cps_api_event_service_client_register( cps_api_event_service_handle_t handle, cps_api_event_reg_t * req) { cps_api_to_std_event_map_t *p = handle_to_data(handle); std_mutex_simple_lock_guard lg(&p->lock); char _key_s[CPS_API_KEY_STR_MAX]; size_t ix = 0; size_t mx = req->number_of_objects; for ( ; ix < mx ; ++ix ) { _key_t _k; _make_key(_k,&(req->objects[ix])); if (p->keys.find(_k)!=p->keys.end()) { EV_LOG(INFO,DSAPI,0,"CPS-EV-REG","Skipping key %s - already registered", cps_api_key_print(&req->objects[ix],_key_s,sizeof(_key_s))); continue; } p->keys[_k].sync = false; } if (p->connected) { __resync_regs(p); } return cps_api_ret_code_OK; }
TEST(cps_api_key_cache,test_key_inserts) { size_t ix = 0; size_t mx = lst.size(); for ( ; ix < mx ; ++ix ) { if ((rand()%20)==1) { char b1[100]; missing.push_back(lst[ix]); entry e; _missing_cache.insert(lst[ix],e); printf("Skipping.. %s\n",cps_api_key_print(lst[ix],b1,sizeof(b1))); ASSERT_TRUE(_missing_cache.find(lst[ix],e,true)); continue; } entry e; cps_api_key_copy(&e.key,lst[ix]); e.ix = ix; ASSERT_FALSE(cache.find(&e.key,e,true)); cache.insert(&e.key,e); size_t klen = (rand() % 4) + 1; cps_api_key_set_len(&e.key,klen); if (cache.find(&e.key,e,true)) continue; cache.insert(&e.key,e); } }
void dn_led_res_key_name( char *buf, unsigned bufsize, uint_t entity_type, uint_t slot, char *led_name, unsigned led_name_len ) { cps_api_key_t key[1]; uint_t n; cps_api_key_init(key, cps_api_qualifier_OBSERVED, cps_api_obj_CAT_BASE_PAS, BASE_PAS_LED_OBJ, 2, entity_type, slot ); cps_api_key_print(key, buf, bufsize); n = strlen(buf); memcpy(buf + n, led_name, led_name_len); buf[n + led_name_len] = 0; }
bool dn_pas_res_insert (cps_api_key_t *key, void *p_res_obj) { char key_str [PAS_CPS_KEY_STR_LEN] = ""; cps_api_key_print(key, key_str, sizeof(key_str)); return (dn_pas_res_insertc(key_str + IGNORE_QUALIFIER_OFS, p_res_obj)); }
void *dn_pas_res_get (cps_api_key_t *key) { char key_str [PAS_CPS_KEY_STR_LEN] = ""; cps_api_key_print(key, key_str, sizeof(key_str)); return (dn_pas_res_getc(key_str + IGNORE_QUALIFIER_OFS)); }
bool get_netlink_data(int rt_msg_type, struct nlmsghdr *hdr, void *data) { cps_api_object_t obj=cps_api_object_create(); if (!nl_get_if_info(rt_msg_type,hdr,obj)) return false; cps_api_object_attr_t attr = cps_api_object_attr_get(obj,cps_api_if_STRUCT_A_NAME); if (attr==CPS_API_ATTR_NULL) return false; char buff[1024]; printf("OID = %s\n",cps_api_key_print(cps_api_object_key(obj),buff,sizeof(buff))); printf("Name:%s \n", (char*)cps_api_object_attr_data_bin(attr)); cps_api_object_delete(obj); return true; }
void key_add(size_t *_k_data, size_t pos, size_t mx) { if (pos >= 5) return; for (int a = 0, _a = 10; a < _a ; ++a ) { _k_data[pos] = a; key_add(_k_data,pos+1,mx); if ((pos+1) == 4) { cps_api_key_t *key = (cps_api_key_t*)calloc(1,sizeof(cps_api_key_t)); cps_api_key_init(key,(cps_api_qualifier_t)(_k_data[0]+1),_k_data[1]+1,_k_data[2]+1,2,_k_data[3],_k_data[4]); char b1[100]; printf("Adding for test.. %s\n",cps_api_key_print(key,b1,sizeof(b1))); lst.push_back(key); } } }
extern "C" cps_api_return_code_t cps_api_object_stats(cps_api_key_t *key, cps_api_object_t stats_obj) { cps_api_return_code_t rc = cps_api_ret_code_ERR; cps_api_channel_t handle = -1; if (key==nullptr || cps_api_key_get_len(key)==0) { std_socket_address_t addr; cps_api_ns_get_address(&addr); int sock; t_std_error _rc = std_sock_connect(&addr,&sock); if (_rc!=STD_ERR_OK) { return rc; } handle = sock; } else { if (!cps_api_get_handle(*key,handle)) { char buff[CPS_API_KEY_STR_MAX]; EV_LOG(ERR,DSAPI,0,"NS","Failed to find owner for %s", cps_api_key_print(key,buff,sizeof(buff)-1)); return cps_api_ret_code_NO_SERVICE; } } if (handle==-1) { EV_LOG(ERR,DSAPI,0,"NS-STATS","No connection to the NS for stats."); return rc; } do { if (!cps_api_send_header(handle,cps_api_msg_o_STATS,0)) { break; } size_t len; uint32_t op=0; if (!cps_api_receive_header(handle,op,len)) break; if (op!=cps_api_msg_o_STATS) break; cps_api_object_guard og (cps_api_receive_object(handle,len)); if(!og.valid()) return false; if (!cps_api_object_clone(stats_obj,og.get())) { break; } rc = cps_api_ret_code_OK; } while (0); cps_api_disconnect_owner(handle); return rc; }
static bool cache_connect(cps_api_key_t &key, cps_api_channel_t &handle) { std_mutex_simple_lock_guard lg(&cache_lock); cache_entry ce; if (_cache.find(&key,ce,true)) { const static int TM = MILLI_TO_MICRO(1000) * 20; //20 seconds bool expired = (std_get_uptime(nullptr) - ce.last_updated) > TM; if (!expired && (cps_api_connect_owner(&ce.owner, handle)==cps_api_ret_code_OK)) { return true; } else { char buff[SCRATCH_LOG_BUFF]; EV_LOG(TRACE,DSAPI,0,"CPS-NS-CACHE","cache expired for key:%s", cps_api_key_print(&key,buff,sizeof(buff))); _cache.erase(&key); } } return false; }
bool cps_api_get_handle(cps_api_key_t &key, cps_api_channel_t &handle) { if (cache_connect(key,handle)) { return true; } cps_api_object_owner_reg_t owner; if (!cps_api_find_owners(&key,owner)) return false; bool rc = (cps_api_connect_owner(&owner, handle))==cps_api_ret_code_OK; if (!rc) { char buff[SCRATCH_LOG_BUFF]; EV_LOG(ERR,DSAPI,0,"NS","Could not connect with owner for %s (%s)", cps_api_key_print(&key,buff,sizeof(buff)-1), owner.addr.addr_type== e_std_socket_a_t_STRING ? owner.addr.address.str: "unk"); } if (rc) { fill_cache(&key,owner); } return rc; }
t_std_error nas_switch_log_init(cps_api_operation_handle_t handle) { cps_api_return_code_t rc; cps_api_registration_functions_t f; memset(&f,0,sizeof(f)); f.handle = handle; f._write_function = cps_nas_switch_log_set_function; char buff[CPS_API_KEY_STR_MAX]; if (!cps_api_key_from_attr_with_qual(&f.key,BASE_SWITCH_SET_LOG_OBJ,cps_api_qualifier_TARGET)) { EV_LOG(ERR,SYSTEM,0,"NAS-DIAG","Could not translate %d to key %s", (int)(BASE_SWITCH_SET_LOG_OBJ),cps_api_key_print(&f.key,buff,sizeof(buff)-1)); return STD_ERR(DIAG,FAIL,0); } if ((rc = cps_api_register(&f)) != cps_api_ret_code_OK) return STD_ERR(DIAG,FAIL,rc); return STD_ERR_OK; }
TEST(cps_api_key,key_compare) { char buff1[1024]; char buff2[1024]; cps_api_key_t key; memset(&key,0,sizeof(key)); cps_api_key_set(&key,0,1); cps_api_key_set(&key,1,3); cps_api_key_set(&key,2,1); cps_api_key_set(&key,3,1); cps_api_key_set_len(&key,4); cps_api_key_t key2; memset(&key2,0,sizeof(key2)); cps_api_key_set(&key2,0,1); cps_api_key_set(&key2,1,3); cps_api_key_set(&key2,2,1); cps_api_key_set(&key2,3,1); cps_api_key_set_len(&key2,4); ASSERT_TRUE(cps_api_key_matches(&key,&key2,false)==0); cps_api_key_set(&key2,1,4); ASSERT_TRUE(cps_api_key_matches(&key,&key2,false)!=0); cps_api_key_set(&key2,1,3); cps_api_key_set(&key2,4,2); cps_api_key_set_len(&key2,3); ASSERT_TRUE(cps_api_key_matches(&key,&key2,false)==0); cps_api_key_set(&key2,1,2); cps_api_key_set_len(&key2,4); ASSERT_TRUE(cps_api_key_matches(&key,&key2,false)!=0); cps_api_key_t key3; memset(&key3,0,sizeof(key3)); cps_api_key_set(&key3,0,1); cps_api_key_set(&key3,1,3); cps_api_key_set(&key3,2,1); cps_api_key_set_len(&key3,3); ASSERT_TRUE(cps_api_key_matches(&key,&key3,true)!=0); cps_api_key_set_len(&key3,1); ASSERT_TRUE(cps_api_key_matches(&key,&key3,false)==0); cps_api_key_set_len(&key3,2); ASSERT_TRUE(cps_api_key_matches(&key,&key3,false)==0); cps_api_key_set(&key2,1,3); ASSERT_TRUE(cps_api_key_matches(&key,&key2,true)==0); printf("Key 1 %s, Key 2 %s\n",cps_api_key_print(&key,buff1,sizeof(buff1)), cps_api_key_print(&key2,buff2,sizeof(buff2))); cps_api_key_set_len(&key2,2); cps_api_key_set(&key2,2,4); ASSERT_TRUE(cps_api_key_matches(&key,&key2,false)==0); printf("Key 1 %s, Key 2 %s\n",cps_api_key_print(&key,buff1,sizeof(buff1)), cps_api_key_print(&key2,buff2,sizeof(buff2))); ASSERT_TRUE(cps_api_key_matches(&key2,&key,false)!=0); cps_api_key_set(&key2,2,2); cps_api_key_set(&key,2,0); ASSERT_TRUE(cps_api_key_matches(&key,&key2,false)==0); printf("Key 1 %s, Key 2 %s\n",cps_api_key_print(&key,buff1,sizeof(buff1)), cps_api_key_print(&key2,buff2,sizeof(buff2))); memset(&key2,0,sizeof(key2)); cps_api_key_copy(&key2,&key); ASSERT_TRUE(cps_api_key_matches(&key,&key2,true)==0); printf("Final\nKey 1 %s, Key 2 %s\n",cps_api_key_print(&key,buff1,sizeof(buff1)), cps_api_key_print(&key2,buff2,sizeof(buff2))); }