Table of Contents

netconf xpath filter example for get command is the topic of this section.

In the previous section, we used xpath filter for the netconf get-config command, which is used to get the configuration of network devices.

However, the netconf get command is mainly used to get the statistics of network devices.

With xpath filter it is also possible to filter the statistics in order to show only a certain section in the output.

NETCONF Xpath Filter for get Command

If you remember, it was easy to write XPath filters with netconf get-config command, because when we get the entire running configuration, it shows all supported YANG files and the structure of each YANG file.

Then we find the interesting part of the configuration and the associated TAGs. Then the TAG is used directly in the xpath filter to limit the output of the running configuration to the interesting section.

use xpath filter with netconf get command
use xpath filter with netconf get command

However, with the netconf get command, you already need to know which YANG file is displaying your interesting statistics.

Then we can find the associated TAG in the structure of that YANG file.

When we find the TAG, Then it can be used in the xpath filter to restrict the statistics output to a specific section.

How YANG models keep statistics data?

The question is, how do I find the YANG file for the statistics that I’m looking for?

To get the answer to this question, you need to know how Cisco, Openconfig, and IETF, model the statistics data and how it differs from the modeling of the running configuration.

The answer of this question is displayed in this figure.

config and statistics data modeling in cisco ietf and openconfig
config and statistics data modeling in cisco ietf and openconfig

In Cisco, YANG files are different for configuration and statistics. The name of the YANG file related to statistics is the same as that of the YANG file related to configuration, but with a “-oper” at the end of the file name.

For example, if “Cisco-IOS-XE-interfaces.yang” is used for interface configuration, then “Cisco-IOS-XE-interfaces-oper.yang” is used to keep interface statistics.

If “Cisco-IOS-XE-ospf.yang” is used for OSPF configuration, then “Cisco-IOS-XE-ospf-oper.yang” is used to keep OSPF statistics.

But in IETF and Openconfig YANG data models, configuration and statistics are kept in a single file but with different xml TAG.

For example, in IETF, the XML tag “interface” is used for interface configuration and the XML tag “interface-state” is used for maintaining interface statistics and both in a single file “ietf-interfaces.yang”.

Openconfig always uses the XML tag “config” to store the configuration and the XML tag “state” to store the statistics, both in a single YANG file.

With this knowledge in mind, it is not difficult to find the right YANG model for any type of statistic.

For Cisco, we find the YANG model for the configuration and then there is another YANG model with the same name and a “-oper” at the end.

For IETF and Openconfig we use the same YANG file used for configuration. Different XML tags are used in the same file, as we have explained earlier, to store configuration and statistics.

Create Sample XM Skeleton from YANG Model

Now that we know which YANG file to use to get the statistics, how do we find the right XML tag to use in the xpath filter?

The easiest way is to create a sample XML skeleton structure from a YANG file that contains all the XML tags needed to get statistics.

A sample XML skeleton can be created using the “pyang” tool with the “-f sample-xml-skeleton” parameter and the name of YANG file.

pyang -f sample-xml-skeleton Cisco-IOS-XE-interfaces-oper.yang

To see some examples, let’s see sample xml skeleton of some famous YANG data model.

As we know, for Cisco there is a different YANG data model with the name including “-oper” at the end like “Cisco-IOS-XE-interfaces-oper.yang” and “Cisco-IOS-XE-ospf-oper.yang”.

majid@devnet:~/yang/vendor/cisco/xe/1791$ pyang -f sample-xml-skeleton Cisco-IOS-XE-interfaces-oper.yang
<?xml version='1.0' encoding='UTF-8'?>
<data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
  <interfaces xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-interfaces-oper">
    <interface>
      <name/>
      <interface-type/>
      <admin-status/>
      <oper-status/>
      <last-change/>
      <if-index/>
      <phys-address/>
	  ...
      <speed/>
      <statistics>
        <discontinuity-time/>
        <in-octets/>
...
majid@devnet:~/yang/vendor/cisco/xe/1791$ pyang -f sample-xml-skeleton Cisco-IOS-XE-ospf-oper.yang
<?xml version='1.0' encoding='UTF-8'?>
<data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
  <ospf-oper-data xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-ospf-oper">
    <ospf-state>
      <op-mode/>
      <ospf-instance>
        <af/>
        <router-id/>
        <ospf-area>
          <area-id/>
          <ospf-interface>
            <name/>
            <network-type/>
            <passive/>
            <demand-circuit/>
...

For IETF, a separate xml tag exists to keep statistical information. Let’s check “ietf-interfaces.yang”.

majid@devnet:~/yang/vendor/cisco/xe/1791$ pyang -f sample-xml-skeleton ietf-interfaces.yang
<?xml version='1.0' encoding='UTF-8'?>
<data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
  <interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
    <interface>
      <name/>
      <description/>
      <type/>
      <link-up-down-trap-enable/>
    </interface>
  </interfaces>
  <interfaces-state xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
    <interface>
      <name/>
      <type/>
      <admin-status/>
      <oper-status/>
      <last-change/>
      <if-index/>
      <phys-address/>
	  ...
      <speed/>
      <statistics>
        <discontinuity-time/>
        <in-octets/>
        <in-unicast-pkts/>
        <in-broadcast-pkts/>
		...
      </statistics>
    </interface>
  </interfaces-state>
</data>

The XML tag “Interface” is used to keep the configuration and “interfaces-state” XML tag is used to store statistical information.

For openconfig, a “config” xml tag is used to keep the configuration information and “state” xml tag is used to keep statistical information as we can see in “openconfig-interfaces.yang” and “openconfig-network-instance.yang”.

majid@devnet:~/yang/vendor/cisco/xe/1791$ pyang -f sample-xml-skeleton openconfig-interfaces.yang | egrep "config|state" -A 10 -B 10
<?xml version='1.0' encoding='UTF-8'?>
<data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
  <interfaces xmlns="http://openconfig.net/yang/interfaces">
    <interface>
      <name/>
      <config>
        <name/>
        <type/>
        <mtu/>
        <description/>
      </config>
      <state>
        <name/>
        <type/>
        <mtu/>
        <description/>
        <ifindex/>
        <admin-status/>
        <oper-status/>
        <last-change/>
        <counters>
          <in-octets/>
...
majid@devnet:~/yang/vendor/cisco/xe/1791$ pyang -f sample-xml-skeleton openconfig-network-instance.yang | egrep "config|state" -A 10 -B 10
<?xml version='1.0' encoding='UTF-8'?>
<data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
  <network-instances xmlns="http://openconfig.net/yang/network-instance">
    <network-instance>
      <name/>
      <fdb>
        <config>
          <mac-learning/>
          <mac-aging-time/>
          <maximum-entries/>
          <anycast-gateway-mac/>
        </config>
        <state>
          <mac-learning/>
          <mac-aging-time/>
          <maximum-entries/>
          <anycast-gateway-mac/>
        </state>
...

Find XML TAG from Sample Skeleton

Now suppose we want to check the number of bytes statistics entering the interfaces.

If we examine again the XML skeleton of the YANG data model “Cisco-IOS-XE-interfaces-oper.yang“, there is an XML tag “in-octets” that shows the number of bytes entering into interfaces.

majid@devnet:~/yang/vendor/cisco/xe/1791$ pyang -f sample-xml-skeleton Cisco-IOS-XE-interfaces-oper.yang
<?xml version='1.0' encoding='UTF-8'?>
<data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
  <interfaces xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-interfaces-oper">
    <interface>
      <name/>
      <interface-type/>
      <admin-status/>
      <oper-status/>
      <last-change/>
      <if-index/>
      <phys-address/>
	  ...
      <speed/>
      <statistics>
        <discontinuity-time/>
        <in-octets/>
...

So we use the XML tag “in-octets” as the first xpath filter to find the exact xpath filter in the next step.

NETCONF Xpath Filter Code Example

This is the script to get input byte statistics of the interfaces.

We use the task “netconf_get” from “nornir_scrapli” or “nornir_netconf” plugin to get the statistics.

In the previous section, we used “netconf_get_config” to get the configuration of network devices.

majid@ubuntu2204tls:~/devnet/pyhton_nornir/2023/12.netconf$ cat 12.3.netconf_xpath_filter_get.py
from nornir import InitNornir
from nornir_scrapli.tasks import netconf_get
#from nornir_netconf.plugins.tasks import netconf_get
from nornir_utils.plugins.functions import print_result

nr = InitNornir(config_file="config.yaml")

def netconf_xpath_get(task):
    task.run(task=netconf_get, filter_type="xpath", filter_="//in-octets")
#    task.run(task=netconf_get, filter_type="xpath", filter_="/interfaces/interface/statistics/in-octets")

#    task.run(task=netconf_get, xmldict="false", filter_type="xpath", path="//in-octets")
#    task.run(task=netconf_get, xmldict="false", filter_type="xpath", path="/interfaces/interface/statistics/in-octets")

results = nr.run(task=netconf_xpath_get)
print_result(results)

In the first step, we use “//in-octets” as the xpath filter to extract any statistics from any YANG model up to the keyword “in-octets” in YANG model hierarchical structure.

Let’s run the script and see the result.

task.run(task=netconf_get, filter_type="xpath", filter_="//in-octets")
majid@ubuntu2204tls:~/devnet/pyhton_nornir/2023/12.netconf$ python3 12.3.netconf_xpath_filter_get.py
netconf_xpath_get***************************************************************
* R1 ** changed : False ********************************************************
vvvv netconf_xpath_get ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
---- netconf_get ** changed : False -------------------------------------------- INFO
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="101">
  <data>
    <interfaces xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-interfaces-oper">
      <interface>
        <name>Control Plane</name>
        <statistics>
          <in-octets>0</in-octets>
        </statistics>
        <v4-protocol-stats>
          <in-octets>0</in-octets>
        </v4-protocol-stats>
        <v6-protocol-stats>
          <in-octets>0</in-octets>
        </v6-protocol-stats>
      </interface>
      <interface>
        <name>GigabitEthernet1</name>
        <statistics>
          <in-octets>18386193</in-octets>
        </statistics>
...
    <interfaces xmlns="http://openconfig.net/yang/interfaces">
      <interface>
        <name>GigabitEthernet1</name>
        <state>
          <counters>
            <in-octets>18386193</in-octets>
          </counters>
        </state>
        <subinterfaces>
          <subinterface>
            <index>0</index>
            <state>
              <counters>
                <in-octets>18386193</in-octets>
              </counters>
            </state>
...
    <interfaces-state xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
      <interface>
        <name>Control Plane</name>
        <statistics>
          <in-octets>0</in-octets>
        </statistics>
      </interface>
      <interface>
        <name>GigabitEthernet1</name>
        <statistics>
          <in-octets>18386193</in-octets>
        </statistics>
      </interface>
...

In the output of running script, we see that the interface statistics from three YANG models are displayed, “Cisco-IOS-XE-interfaces-oper”, “openconfig-interfaces” and “ietf-nterfaces”.

To limit the output to only Cisco YANG data model, we use the exact xpath filter “/interfaces/interface/statistics/in-octets” to reach input byte statistics of different interfaces.

task.run(task=netconf_get, filter_type="xpath", filter_="/interfaces/interface/statistics/in-octets")

Now we run the script and see the result.

majid@ubuntu2204tls:~/devnet/pyhton_nornir/2023/12.netconf$ python3 12.3.netconf_xpath_filter_get.py
netconf_xpath_get***************************************************************
* R1 ** changed : False ********************************************************
vvvv netconf_xpath_get ** changed : False vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv INFO
---- netconf_get ** changed : False -------------------------------------------- INFO
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="101">
  <data>
    <interfaces xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-interfaces-oper">
      <interface>
        <name>Control Plane</name>
        <statistics>
          <in-octets>0</in-octets>
        </statistics>
      </interface>
      <interface>
        <name>GigabitEthernet1</name>
        <statistics>
          <in-octets>18476403</in-octets>
        </statistics>
      </interface>
      <interface>
        <name>GigabitEthernet2</name>
        <statistics>
          <in-octets>0</in-octets>
        </statistics>
      </interface>
      <interface>
        <name>GigabitEthernet3</name>
        <statistics>
          <in-octets>0</in-octets>
        </statistics>
      </interface>
    </interfaces>
  </data>
</rpc-reply>

you can download netconf get command xpath filter from here.

Back to: YANG based Network Automation using NETCONF RESTCONF gNMI > Network Automation using NETCONF

Leave a Reply

Your email address will not be published. Required fields are marked *


Post comment