Automation and Artificial Intelligence · Topic 3 of 7
YANG Data Modeling
Learn the principles and benefits of YANG data models: modules, containers, lists and leaves, IETF, OpenConfig and native models, NETCONF and RESTCONF.
- 5 min
- YANG is a data modeling language. It is not a protocol and it does not carry data.
- NETCONF and RESTCONF carry data that follows YANG models.
- A container groups nodes, a list holds keyed entries, and a leaf holds one value.
What it is
YANG is a language that describes the structure of configuration and status data on a network device.
It does not carry data and it is not a protocol.
It is a blueprint. It says that a device has interfaces, that each interface has a name and a state, and what type each value has. Protocols such as NETCONF and RESTCONF then use the blueprint to read and write real data.
How it works
A model is built from a small set of node types, and many models are shared.
Why a data model is needed
Every vendor has its own command line, and every command line changes between releases. Scripts that parse text break easily. SNMP MIBs mostly describe monitoring data, and they are hard to use for configuration.
A data model gives one machine readable description of both configuration and status. A program can then check its data against the model before it sends it, and the device rejects data that does not fit.
YANG building blocks
| Node type | Meaning |
|---|---|
module | The top level file that holds a model |
container | A group of related nodes, with no value of its own |
list | A set of entries, each identified by a key |
leaf | A single value, with a type |
leaf-list | A list of simple values |
rpc | An operation that you can call |
notification | An event that the device can send |
Leaf values have types such as string, boolean, uint16 and enumeration. A model can also mark a node as configuration, or as read only status data.
A small example
module example-interfaces {
namespace "urn:example:interfaces";
prefix exif;
container interfaces {
list interface {
key "name";
leaf name { type string; }
leaf enabled { type boolean; default true; }
leaf mtu { type uint16; }
}
}
}
The module has a container called interfaces. Inside it is a list called interface, where name is the key, so each entry is identified by its name. Each entry has three leaves with their types.
A tool such as pyang can show the model as a tree, which is easier to read.
module: example-interfaces
+--rw interfaces
+--rw interface* [name]
+--rw name string
+--rw enabled? boolean
+--rw mtu? uint16
In the tree, rw means read and write configuration, and ro means read only status. The star marks a list, and the text in square brackets is its key. A question mark marks an optional leaf.
The same data in JSON
The data that follows the model can be written in JSON.
{
"example-interfaces:interfaces": {
"interface": [
{"name": "Gi1", "enabled": true, "mtu": 1500},
{"name": "Gi2", "enabled": false}
]
}
}
The container becomes an object, the list becomes an array of objects and each leaf becomes a key with a value. The module name is the prefix on the top key. The same data can also be written in XML for NETCONF.
Families of models
| Family | Source | Notes |
|---|---|---|
| IETF models | The IETF, such as ietf-interfaces | Vendor neutral, a basic common set |
| OpenConfig models | An industry working group, such as openconfig-interfaces | Aims at one model across vendors, used a lot for telemetry |
| Native models | The vendor, such as Cisco-IOS-XE-native | Cover every feature of that platform |
IETF and OpenConfig models are portable, but they cover a limited set of features. Native models cover the whole device, but they work only with that vendor.
How models are used
- NETCONF sends and receives XML that follows a model.
- RESTCONF uses the model names in its URL paths and in JSON.
- gNMI and streaming telemetry also use models to name the data that is sent.
A device lists the models that it supports. NETCONF does this in its capabilities exchange. You can then find the paths that you need and build your requests.
Benefits
- One structured description for configuration and status.
- Data can be checked before it is sent, so errors are found early.
- Tools can create code, documents and tests from the model.
- Models can be shared between vendors and tools.
- Scripts are less likely to break when a release changes the command line.
What to remember for the exam
Eight facts cover most questions on this topic.
- YANG is a data modeling language. It is not a protocol and it does not carry data.
- NETCONF and RESTCONF carry data that follows YANG models.
- A container groups nodes, a list holds keyed entries, and a leaf holds one value.
- A leaf has a type, such as string, boolean or uint16.
rwmarks configuration, andromarks read only status in a tree.- IETF and OpenConfig models are vendor neutral. Native models are vendor specific.
- YANG data can be encoded as XML or JSON.
- Models allow validation, tooling and consistent automation.
If a question asks what YANG does, the answer is that it describes the data structure. It is the model, not the transport.
Common mistakes
These four mistakes appear often.
Calling YANG a protocol. It is a language that describes data, and the protocols are NETCONF and RESTCONF.
Confusing a list with a leaf-list. A list has entries with keys, and a leaf-list holds simple values.
Assuming one model covers everything. Vendor neutral models cover less than native ones.
Treating the model as the data. The model is the structure, and the data is the actual values.
A native model works only on that vendor and often only on a range of releases, so check the version before relying on a path.
Check yourself
Work through these three questions.
Is YANG a protocol or a modeling language?
It is a modeling language that describes data, and it carries nothing by itself.
What does
rwmean in a YANG tree?It marks a node that is read and write configuration, while
romarks read only status.Why use a vendor neutral model rather than a native model?
It can be used across vendors, but it covers fewer features than a native model.
Topic 3 of 7 in Automation and Artificial Intelligence
Test yourself on Automation and Artificial Intelligence
The full Cisco CCNP 350-401 ENCOR question bank has 854 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by Cisco. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.