In the last few posts, we have focused quite heavily on methods & technologies that have brought a completely radical change in the way, the field of network management is viewed across the industry. It is becoming more & more apparent with each passing day that the traditional way of managing a medium to large sized network using CLIs and treating each function as a standalone entity is not going to cut it anymore. Even if you are not a full time Network Engineer (I am a UC guy, for instance) but work with network devices in some capacity then it is imperative that you have a sound understanding of components like YANG Modules that form the backbone of a modern programming based network management architecture. This brief tutorial on Pyang aims to do just that.
Challenges
It is quite common to get overwhelmed when one actually starts going through different YANG models. It requires quite a decent amount of time to get familiar with different entities like Containers, Modules, Leafs etc that make up a YANG model to a point where you feel comfortable to work with them in production scenarios. But what if you don’t have that much time or simply don’t want to spend all that time understanding the nitty gritties of a Model & instead want to focus on building applications to resolve real world use cases ? Well, there is a good news! And that good news comes in the form of Pyang.

What is Pyang
To put it simply, Pyang is a program written in Python that can be used to convert YANG modules into a Tree like structure. This makes understanding a YANG module’s syntax insanely easy. It can be used to convert YANG modules into other formats as well like YIN (XML), jstree (HTML) but from a visualization perspective, I’ve found “Tree” to be the best format. I’d suggest you to try all available formats & pick one that works the best for you. Let’s now work with an example as a part of this Pyang tutorial.
Sample YANG Model
I am using a YANG model that provides real time operational data for Voice traffic. Examples include information on active calls, registrations status, call statistics etc. The model is linked below.
https://github.com/YangModels/yang/blob/main/vendor/cisco/xe/1761/Cisco-IOS-XE-voice-oper.yang
I am also producing a snippet of the same model here. As can be seen, it takes a significant amount of time to understand how the nodes are grouped and being referenced throughout the model. The actual model is more than 2000 lines long. So, it doesn’t take much for one to get frustrated before they find the information they are looking for.
grouping active-voip-call-summary {
description
"Total active VOIP call summary";
leaf telephony-call-legs {
type uint32;
description
"Total number of telephony call legs";
}
leaf sip-call-legs {
type uint32;
description
"Total number of SIP call legs";
}
leaf total-call-legs {
type uint32;
description
"Total number of SIP and telephony legs";
}
list active-voip-call-details {
key "call-index call-setuptime";
description
"A list of active VOIP call leg details";
uses voice-ios-xe-oper:active-voip-call-key;
uses voice-ios-xe-oper:active-voip-call-details;
}
}
Now compare the above snippet with the following tree like structure which was obtained with the help of Pyang and the benefits of using Pyang to demystify YANG modules become instantly apparent.
module: Cisco-IOS-XE-voice-oper
+--ro voice-oper-data
+--ro active-voip-call-summary!
| +--ro telephony-call-legs? uint32
| +--ro sip-call-legs? uint32
| +--ro total-call-legs? uint32
| +--ro active-voip-call-details* [call-index call-setuptime]
| +--ro call-index uint32
| +--ro call-setuptime yang:date-and-time
| +--ro call-id? uint32
| +--ro call-type? voice-ios-xe-oper:voice-call-type-enum
| +--ro bridge-id? uint32
| +--ro peer-id? int32
| +--ro direction? voice-ios-xe-oper:voice-call-direction-enum
| +--ro directory-number? string
| +--ro call-state? voice-ios-xe-oper:voice-call-state-enum
| +--ro call-duration? uint32
| +--ro transmit-packets? uint32
| +--ro transmit-bytes? uint32
| +--ro receive-packets? uint32
| +--ro receive-bytes? uint32
| +--ro remote-ip-address? inet:ip-address
| +--ro remote-media-port? uint16
| +--ro codec? voice-ios-xe-oper:voip-codec-type
The above snippet makes it easier to identify different components. For example, if we wanted to get details on the number of active SIP call legs then we can just follow the path starting from the root. This is akin to navigating to a file/folder in your computer. So, if we are using this YANG model with NETCONF, then the GET operation will need to be performed at the following branch to get details of active SIP call legs
voice-oper-data --> active-voip-call-summary --> sip-call-legs
Installation
The process of installing Pyang is pretty straightforward. You will use Python’s native package manager PIP. The command is given below.
pip install pyang
In Action
A YANG module can be converted into a tree like structure using the below command. You can replace the argument “tree” with “yin” if you wish to get an XML based output or “jstree” for an HTML equivalent.
pyang -f tree <path-to-the-yang-module>
pyang -f tree yang/vendor/cisco/xe/1761/Cisco-IOS-XE-voice-oper.yang
I hope this brief tutorial on Pyang was good enough to explain its benefits and how it can be used to easily simplify YANG models. Having a good grasp on YANG models is essential if we want to exploit protocols like NETCONF & RESTCONF to their full extent.
Please feel free to provide your feedback/suggestions, if any.
