ietf-netconfnetconflibyang

How to fetch the Leaf Node value from lyd_node* received from server in Netopeer2 Client?


I'm using Netopeer2 library for Netconf client and server. Command line program source codes of client and server are available.
In client side, upon executing get-config for specific yang model, the response is printed in the command line.
However I want to fetch the value of the Leaf node from that response and use it for my application. The response is received in lyd_node*.

I got the hint online to use lyd_find_path() of libyang to do this. I called it like this:
LY_ERR ret = lyd_find_path(op, "/etsi-qkd-sdn-node:qkd_node/qkdn_version", 1, &match);

Upon debugging lyd_find_path(), I found that it is failing at the line
ret = ly_path_eval_partial(lypath, ctx_node, NULL, 0, NULL, match). The error value is: LY_ENOTFOUND


Solution

  • The return value of LY_ENOTFOUND is expected, because as the documentation of lyd_find_path() states:

    Search in given data for a node uniquely identified by a path. Always works in constant (O(1)) complexity. To be exact, it is O(n) where n is the depth of the path used. Opaque nodes are NEVER found/traversed.

    As the ietf-netconf YANG module specifies, the reply to get-config is enveloped in a node called data which is of type anyxml (= opaque).

    A possible solution to this is to obtain the tree you are interested in from this data node. An illustration of how this can be achieved:

    /* calling lyd_child, because op is the node <get-config>, but we are interested in <data> */
    struct lyd_node_any *data = (struct lyd_node_any *)lyd_child(op);
    struct lyd_node *tree = data->value.tree;
    struct lyd_node *node;
    LY_ERR ret = lyd_find_path(tree, "/etsi-qkd-sdn-node:qkd_node/qkdn_version", 0, &node);
    if (!ret) {
        printf("Value of the node %s is %s\n", LYD_NAME(node), lyd_get_value(node));
    }