bool   cps_delete_mac(){
// Create   and initialize   the   transaction   object cps_api_transaction_params_t   tran;
if (cps_api_transaction_init(&tran)   !=   cps_api_ret_code_OK   ){
return   false;
}


// Create   and initialize   the   key cps_api_key_t   key;
cps_api_key_from_attr_with_qual(&key,   BASE_MAC_TABLE_OBJ,   cps_api_qualifier_TARGET);


// Create   the   object
cps_api_object_t   obj   =  cps_api_object_create();

if(obj   ==   NULL ){ cps_api_transaction_close(&tran); return   false;
}


// Set   the   key for   the   object
cps_api_object_set_key(obj,&key);

// Add   attributes   mandatory   to   create   MAC   address   entry
uint8_t   mac_addr[6]   =  {0x0,0xa,0xb,0xc,0xd,0xe}; 



uint16_t   vlan_id   =  131;


cps_api_object_attr_add(obj,BASE_MAC_TABLE_MAC_ADDRESS,   mac_addr,   sizeof(hal_mac_addr_t)); cps_api_object_attr_add_u32(obj,BASE_MAC_TABLE_IFINDEX,if_nametoindex("e101-001-0")   ); cps_api_object_attr_add_u16(obj,BASE_MAC_TABLE_VLAN,vlan_id);

// Add   the   object   along   with   the   operation   to   transaction if(cps_api_delete(&tran,obj)   !=   cps_api_ret_code_OK   ){
cps_api_delete_object(obj);
return   false;
}
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;
}
Esempio n. 9
0
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);
}