libyang icon indicating copy to clipboard operation
libyang copied to clipboard

howto get data from lyd_node?

Open dubhokku opened this issue 3 years ago • 1 comments

Hello, tried to get data from :

<rpc-reply message-id="26" xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
 <data>
  <lldp xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ethernet-lldp-oper">
   <nodes>
    <node>
     <node-name>0/0/CPU0</node-name>
     <neighbors>
      <details>
       <detail>
        <interface-name>GigabitEthernet0/0/0/0</interface-name>
...

earlier i'am use third library for retive data from xml, but i know, libyang make this better from lyd_node.

tried to make

ret_find_path = lyd_path( op, LYD_PATH_STD, buf_find_path, 127 );

return:

    ret_find_path /ietf-netconf:get
    buf_find_path /ietf-netconf:get

lyd_find_path( op, "data", true, &need_data ); return: LY_SUCCESS

lyd_child( need_data ); return: NULL

lyd_find_path( op, "lldp", true, &need_data ); return: LY_EVALID

lyd_find_path( op, "data/lldp", true, &need_data ); return: LY_EVALID


lyd_find_xpath( op, "data", &need_data ); return:

    need_data->count 1
    need_data->size 1

lyd_find_xpath( op, "lldp", &need_data ); return:

    need_data_cisco->count 0
    need_data_cisco->size 0

lyd_find_xpath( op, "data/lldp", &need_data ); return:

    need_data_cisco->count 0
    need_data_cisco->size 0

also i'am was reading some issues where was talking about lyd_node_any, any_xml and lyd_get_value(), but they not give undestand this.

Can you tell, howto may retrive data from any lyd_node uses xpath to him?

dubhokku avatar Jul 31 '22 20:07 dubhokku

Once you get the data node, you need to cast it to lyd_node_any and then get the rest of the content from its tree member.

michalvasko avatar Aug 02 '22 05:08 michalvasko

Once you get the data node, you need to cast it to lyd_node_any and then get the rest of the content from its tree member.

Hello, I'm a student who is studying yang & neconf.

I'm trying to extract my list data from the data node with the command,

I'm trying to get rid of node for for loop and get list data.

but continuously failed.

struct nc_rpc* rt_rpc = nc_rpc_getconfig(NC_DATASTORE_RUNNING, filter, NC_WD_UNKNOWN, NC_PARAMTYPE_CONST);


ret = nc_send_rpc(session, rt_rpc, 1000, &msgid);
if (ret != NC_MSG_RPC) {
    fprintf(stderr, "Failed to send get-config RPC\n");
    nc_rpc_free(rt_rpc);
}
msgtype = nc_recv_reply(session, rt_rpc, msgid, 10000, &envp, &op);
if (msgtype != NC_MSG_REPLY) {
    fprintf(stderr, "Failed to receive get-config reply\n");
    nc_rpc_free(rt_rpc);
}

lyd_print_file(stdout, op, LYD_XML, LYD_PRINT_SHRINK | LYD_PRINT_WITHSIBLINGS);
printf("\n");

struct lyd_node_any *any_node = (struct lyd_node_any *)op;
struct lyd_node *actual_data = any_node->value.tree;

fillSwitchInfo(actual_data, &switchInfo);

I'm trying to get data with lyd_node_any and using raw op variable both.

but both failed. there is no child after node.

`void fillSwitchInfo(struct lyd_node *node, SwitchInfo *switchInfo) { LY_ERR err; struct lyd_node *next; struct lyd_node_inner *inner; printf("fillSwitchInfo\n");

LYD_TREE_DFS_BEGIN(node, next) {
    printf("string : %s\n", next->schema->name);
    if (!strcmp(next->schema->name, "dpid")) {
        switchInfo->dpid = MAC_to_uint64(lyd_get_value(next));
        printf("dpid : %d \n", switchInfo->dpid);
    } else if (!strcmp(next->schema->name, "port")) {
        inner = (struct lyd_node_inner *)next;
        fillPortStatuses(inner, switchInfo->port_statuses);
    } else if (!strcmp(next->schema->name, "table_entry")) {
        inner = (struct lyd_node_inner *)next;
        //fillTableEntries(inner, switchInfo->l2_table_entries, switchInfo->cbs_table_entries);
    }
    LYD_TREE_DFS_END(node, next);
}

}

`

this is my fillswitchinfo function, but not working.

the data from command looks like this one. but there is no child after node.

<data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"><switch_info xmlns="urn:lgessu:params:xml:ns:yang:SmStubStd"><dpid>00:1d:7e:8a:74:d7</dpid><port_status><port><port_number>1</port_number><status>ALIVE</status></port><port><port_number>2</port_number><status>SLEEP</status></port></port_status><l2_table><table_entry><key>101</key><MAC_address>00:1d:7e:8a:74:d8</MAC_address><output_port>2</output_port></table_entry><table_entry><key>102</key><MAC_address>00:1d:7e:8a:74:d9</MAC_address><output_port>3</output_port></table_entry></l2_table><cbs_table><table_entry><key>201</key><bandwidth>100</bandwidth></table_entry><table_entry><key>202</key><bandwidth>150</bandwidth></table_entry></cbs_table></switch_info></data>

what should I do for extract data from get-config raw data.

ipseokri avatar May 17 '24 19:05 ipseokri