There is a Python tool called pyang that can read yang files. To install it on Linux machine:
apt install git python3-pip pip install pyang
Now, git clone yang data model to your linux machine:
cd ~;git clone https://github.com/YangModels/yang.git
Pyang by itself is capable of reading yang and present it as a tree. To get xpath, we need to install additional plugin from here: https://github.com/NSO-developer/pyang-xpath
Find your pyang installation, and put the the xpath plugin to the plugin folder:
find /usr/local/lib -iname pyang /usr/local/lib/python3.10/dist-packages/pyang
Download plugin:
wget -O /usr/local/lib/python3.10/dist-packages/pyang/plugins/xpath.py https://raw.githubusercontent.com/NSO-developer/pyang-xpath/master/xpath.py
Go to Yang model you cloned a few steps back (in this example, we’ll find xpath for Cisco IOS-XE cpu usage:
cd ~/yang/vendor/cisco/xe/1781/
Use xpath module on yang file to get xpath:
pyang -f xpath Cisco-IOS-XE-process-cpu-oper.yang >>> module: Cisco-IOS-XE-process-cpu-oper /cpu-usage /cpu-usage/cpu-utilization /cpu-usage/cpu-utilization/five-seconds /cpu-usage/cpu-utilization/five-seconds-intr /cpu-usage/cpu-utilization/one-minute /cpu-usage/cpu-utilization/five-minutes /cpu-usage/cpu-utilization/cpu-usage-processes /cpu-usage/cpu-utilization/cpu-usage-processes/cpu-usage-process /cpu-usage/cpu-utilization/cpu-usage-processes/cpu-usage-process/pid /cpu-usage/cpu-utilization/cpu-usage-processes/cpu-usage-process/name /cpu-usage/cpu-utilization/cpu-usage-processes/cpu-usage-process/tty /cpu-usage/cpu-utilization/cpu-usage-processes/cpu-usage-process/total-run-time /cpu-usage/cpu-utilization/cpu-usage-processes/cpu-usage-process/invocation-count /cpu-usage/cpu-utilization/cpu-usage-processes/cpu-usage-process/avg-run-time /cpu-usage/cpu-utilization/cpu-usage-processes/cpu-usage-process/five-seconds /cpu-usage/cpu-utilization/cpu-usage-processes/cpu-usage-process/one-minute /cpu-usage/cpu-utilization/cpu-usage-processes/cpu-usage-process/five-minutes
This are all xpaths, but we still need a prefix to construct a full path. It’s written at the start of the file:
grep prefix Cisco-IOS-XE-process-cpu-oper.yang prefix process-cpu-ios-xe-oper;
The whole xpath (level 1 depth) would look like this:
process-cpu-ios-xe-oper:cpu-usage/cpu-utilization
Depends on the depth and data needed, you can use this xpath with Cisco IOS-XE switch. Example:
telemetry ietf subscription 3305 encoding encode-kvgpb filter xpath /process-cpu-ios-xe-oper:cpu-usage/cpu-utilization source-address 10.10.1.1 stream yang-push update-policy periodic 3000 receiver ip address 10.20.20.250 57000 protocol grpc-tcp
References:
https://community.cisco.com/t5/service-providers-knowledge-base/how-to-derive-exr-telemetry-yang-path-using-pyang/ta-p/3713864
https://github.com/NSO-developer/pyang-xpath
https://github.com/jeremycohoe/cisco-ios-xe-mdt
https://www.youtube.com/watch?v=p94yetSTXdc
https://github.com/YangModels/yang