libyang icon indicating copy to clipboard operation
libyang copied to clipboard

Question: Parsing <rpc-reply> and traversing the resulting lyd_node

Open rabhat31 opened this issue 2 years ago • 2 comments

Hi, I have a question about how to parse an and traverse the resulting lyd_node.

I have an rpc:

<get xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
  <filter>
    <native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
      <interface>
        <GigabitEthernet>
          <name>1/0/5</name>
        </GigabitEthernet>
      </interface>
    </native>
  </filter>
</get>

And the resulting

<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="urn:uuid:9015b7e6-b478-4215-9814-448f64b77d0f">
  <data>
    <native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
      <interface>
        <GigabitEthernet>
          <name>1/0/5</name>
          <logging>
            <event>
              <link-status/>
            </event>
          </logging>
          <access-session>
            <host-mode>multi-auth</host-mode>
          </access-session>
          <cdp xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-cdp">
            <tlv>
              <app-config>
                <app>true</app>
              </app-config>
              <app/>
              <server-location/>
              <location/>
            </tlv>
          </cdp>
          <cts xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-cts">
            <role-based>
              <enforcement/>
            </role-based>
          </cts>
        </GigabitEthernet>
      </interface>
    </native>
  </data>
</rpc-reply>

I am parsing this rpc-reply into an lyd_node using

lyd_parse_op () the output of which is an lyd_node

<get xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
  <data>
    <native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
      <interface>
        <GigabitEthernet>
          <name>1/0/5</name>
          <logging>
            <event>
              <link-status/>
            </event>
          </logging>
          <access-session>
            <host-mode>multi-auth</host-mode>
          </access-session>
          <cdp xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-cdp">
            <tlv>
              <app-config>
                <app>true</app>
              </app-config>
              <app/>
              <server-location/>
              <location/>
            </tlv>
          </cdp>
          <cts xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-cts">
            <role-based>
              <enforcement/>
            </role-based>
          </cts>
        </GigabitEthernet>
      </interface>
    </native>
  </data>
  <filter>
    <native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
      <interface>
        <GigabitEthernet>
          <name>1/0/5</name>
        </GigabitEthernet>
      </interface>
    </native>
  </filter>
</get>

When I try to run a DFS traversal of this tree, I get only 3 nodes:

<get>
.....
</get>
<data>
.....
</data>
<filter>
.....
</filter>

I'm unsure of why this is the case. Ideally, I'd like to be able to traverse the entire output tree and be able to access nodes like:

<role-based>
    <enforcement/>
</role-based>

How can I go about getting a complete DFS traversal of the resulting data tree?

Thanks !

rabhat31 avatar Jun 07 '23 23:06 rabhat31

If you look at the YANG schema structure of the RPC output, those are the nodes that DFS traverses. To access anydata or anyxml data tree, you need to manually cast those nodes into struct lyd_node_any and access their nested tree, which then can again be traversed using DFS.

michalvasko avatar Jun 08 '23 06:06 michalvasko

If @rabhat31 is who I think he is (my coworker), then the approach you described here helped us properly parse the underlying yang data from the RPC wrapper. If that's the case - can we close this issue?

8bitDesigner avatar Jul 11 '23 17:07 8bitDesigner