CESNET / libnetconf2

C NETCONF library
BSD 3-Clause "New" or "Revised" License
203 stars 147 forks source link

Distinguish rpc-reply success from error #422

Closed Kendaii closed 1 year ago

Kendaii commented 1 year ago

I'm working on a client and server NETCONF both using libnetconf2 & libyang. Everything is working fine but on the client side, I find difficult to distinguish success rpc-reply :

<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="37">
  <ok/>
</rpc-reply>

from error :

<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="36">
  <rpc-error>
    <error-type>application</error-type>
    <error-tag>operation-failed</error-tag>
    <error-severity>error</error-severity>
    <error-message xml:lang="en">Internal error</error-message>
  </rpc-error>
  <rpc-error>
    <error-type>application</error-type>
    <error-tag>operation-failed</error-tag>
    <error-severity>error</error-severity>
    <error-message xml:lang="en">User callback failed.</error-message>
  </rpc-error>
</rpc-reply>

RPC methods are custom and do not send any output data (op is empty). I can't use libyang XPath methods to validate message as I understand that rpc-reply definition is NETCONF specific and do not appears in any yang file.

For now I iterate over envelope data to find an ok tag as follow, but maybe there is a better way than that :

nc_recv_reply(nc_session, rpc, msgid, timeout, &envelope, &op);

LYD_TREE_DFS_BEGIN(envelope, elem)
    {
        if (strncmp(LYD_NAME(elem), "ok", 2) == 0)
            printf("RESPONSE IS OK\n");
        LYD_TREE_DFS_END(envelope, elem)
    }

I apologize in advance if my question is dumb but I couldn't find an answer in documentation or github issues.

michalvasko commented 1 year ago

Not a dumb question and you can look at how the CLI checks the reply type, similarly. I agree it is a bit unexpected that you have to "manually" check the reply type but it seemed redundant to add some special function or something like that. You can be certain that the returned reply is valid so the DFS loop is not needed, the ok reply can only have that single element I believe.

Kendaii commented 1 year ago

Thank you for your quick response, if anybody has the same question here is the answer (I was not too far) :

if (!strncmp(LYD_NAME(lyd_child(envelope)), "ok", 2))
        printf("RESPONSE IS OK\n");