netopeer2 icon indicating copy to clipboard operation
netopeer2 copied to clipboard

RPC Reply and Notification conversion to Xpath

Open masif-cs opened this issue 3 years ago • 5 comments

Hi,

I am trying to convert the RPC Reply and Notification XML received from netopeer-cli to XPATH.

I have converted the struct lyd_node *op, received in nc_recv_reply to XML using lyd_print_file. Which will dump the output XML in a file. Now I am trying to convert the XML into Xpath using libxml2. Normally the code works.

But for user-rpc response, we dont get the rpc name or you can say the root node. So the XML parsing is getting failed by libxml2. Could you please help in resolving the issue? Either tell me how can i have an RPC name in RPC response XML or how can we convert the XML into XPATH.

masif-cs avatar Apr 22 '22 06:04 masif-cs

My first question would be why are you using libxml2? One of the main reasons and selling points of libyang is that is does not use the XML library as it is too inefficient to be used for YANG.

convert the RPC Reply and Notification XML received from netopeer-cli to XPATH.

Not sure what you mean by this but you can generate XPath for data nodes with lyd_path(). If you do that, you should also have no problem with missing the RPC container in the reply, it is there in libyang data.

michalvasko avatar Apr 22 '22 09:04 michalvasko

I am aware of lyd_path which will give xpath of each node. But how we can get xpath with all keys populated and value of that xpath?

masif-cs avatar Apr 22 '22 11:04 masif-cs

lyd_path generates an XPath with all the key values by default. The value of the node can be retrieved by standard libyang API, you can use lyd_get_value(), for example.

michalvasko avatar Apr 25 '22 07:04 michalvasko

Thanks @michalvasko . I tried and it is working fine for generic RPC. But for get-config RPC, it is having a data node() on top of the yang nodes. So the for loop on lyd_node using LY_LIST_FOR(root, node) is not working.

Could you please let me know how can we iterate through the nodes to print all LEAF/LEAFLIST xpath?

masif-cs avatar Apr 25 '22 09:04 masif-cs

You are not being exact much but I will assume you are trying to get to the data nodes in a get-config reply. For that, you need to find the anyxml data node in the output. Once you cast it to struct lyd_node_any, you can access its value that should be of type LYD_ANYDATA_DATATREE. That means you can access its any->value.tree member, which is the actual data provided by the server that can be traversed as a standard data tree (LYD_TREE_DFS_BEGIN, for example), again.

michalvasko avatar Apr 25 '22 10:04 michalvasko