libyang icon indicating copy to clipboard operation
libyang copied to clipboard

element order after lyd_parse_op() differs from input for anyxml

Open pfeige opened this issue 3 years ago • 7 comments

Hi,

please consider the following schema:

module test-send-notification {
  namespace "urn:test:send:notification";
  prefix tsn;

  rpc send-notification {
    description
      "Triggers sending of a notification whose content is specified
       by this RPC.";
    input {
      anyxml content {
        description
          "The content of the notification.";
      }
    }
  }
}

When the following RPC is sent:

<send-notification xmlns="http://www.adtran.com/ns/yang/adtran-int-notification-simulator">
  <content>
    <notification xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">
      <eventTime>2022-07-19T12:00:00Z</eventTime>
      <notification-embed-container xmlns="http://www.adtran.com/ns/yang/adtran-int-unit-test-rfc7950">
        <container-notification-string-changed>
          <new-value>new</new-value>
          <old-value>old</old-value>
        </container-notification-string-changed>
      </notification-embed-container>
    </notification>
  </content>
</send-notification>

Then after lyd_parse_op() in libnetconf2 the resulting XML tree looks like this:

<send-notification xmlns="http://www.adtran.com/ns/yang/adtran-int-notification-simulator">
  <content>
    <notification xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">
      <notification-embed-container xmlns="http://www.adtran.com/ns/yang/adtran-int-unit-test-rfc7950">
        <container-notification-string-changed>
          <new-value>new</new-value>
          <old-value>old</old-value>
        </container-notification-string-changed>
      </notification-embed-container>
      <eventTime>2022-07-19T12:00:00Z</eventTime>   <!-- wrong order -->
    </notification>
  </content>
</send-notification>

As you can see the elements eventTime and notification-embed-container are swapped. I think this should not happen because the order of elements matters like in the case above when the anyxml content is used as real notification to be sent. To solve the problem I modified this statement in lydxml_subtree_r():

    lydctx->parse_opts |= LYD_PARSE_OPAQ | LYD_PARSE_ORDERED | (ext ? LYD_PARSE_ONLY : 0);
                                           ^----------------

What do you think?

BR, Peter

pfeige avatar Jul 22 '22 06:07 pfeige

The reason for this is that YANG instance data (struct lyd_node *) are always in schema order before any opaque nodes (struct lyd_node_opaq *), which have no definition in any YANG (such as eventTime). Also, I do not think it is required anywhere for the data in an anyxml to keep their order in an input.

michalvasko avatar Jul 22 '22 11:07 michalvasko

Hm, I do not really understand what the advantage of such reordering is. From my point of view the anyxml data are generic and should not be interpreted in any way, even if there are known namespaces.

pfeige avatar Jul 22 '22 11:07 pfeige

The advantage is that if you receive, let's say, the reply to a get RPC, you have the full running and state data parsed into their corresponding YANG schema nodes instead of having just an opaque string or something, which you would have to parse a second time to actually get the received YANG data.

michalvasko avatar Jul 22 '22 11:07 michalvasko

In this case here we have a correct notification within the leaf content but after parsing it is not correct any more and cannot be sent this way. The node eventTime is mandatory and must be the first child in the notification node.

pfeige avatar Jul 22 '22 11:07 pfeige

I understand all that but, strictly speaking, you are relying on the fact that the order of all the nested elements will be kept, which is not guaranteed anywhere. Also, your use-case is quite non-standard, sending a notification embedded in an RPC. I would like to help but I really cannot think of a solution.

michalvasko avatar Jul 22 '22 13:07 michalvasko

I see.

pfeige avatar Jul 22 '22 13:07 pfeige

Note that the whole ordering requirements are somewhat unfortunate as there are ambiguous cases such as this one and other problems (I remember something about it in the mailing list this year). Our tools should be fine with whatever order and once parsed, the order is fairly strict (opaque nodes are at the end but in no specific order).

michalvasko avatar Jul 22 '22 14:07 michalvasko