What is YANG, and what are containers, lists and leafs?
Show the answer
Say this first
YANG is a data modeling language (RFC 6020, version 1.1 in RFC 7950). It defines the structure, types and rules of configuration and state data, so a client knows exactly what a device accepts. It is not a protocol and not an encoding: NETCONF, RESTCONF and gNMI carry YANG-modeled data as XML, JSON or protobuf. A container groups nodes, a list holds entries identified by a key, a leaf holds one typed value and a leaf-list holds several values.
Go deeper
A small model
module example-vlans {
namespace "urn:example:vlans";
prefix exv;
container vlans {
list vlan {
key "id";
leaf id {
type uint16 {
range "1..4094";
}
}
leaf name {
type string;
}
leaf-list tagged-ports {
type string;
}
}
}
}The building blocks
| Statement | What it is |
|---|---|
module | The file-level unit, with a namespace and prefix |
container | A grouping node with no value of its own (interfaces) |
list | Repeating entries, each identified by key (interface keyed by name) |
leaf | One value with a type (description, mtu) |
leaf-list | Many values of one type (DNS servers) |
typedef, grouping, uses | Reusable types and blocks |
augment | Adds nodes into another module's tree (how vendors extend IETF models) |
config false | Marks operational state (counters, oper-status), read-only |
rpc, action, notification | Operations and events |
Why engineers care
The model tells you the path and type before you send anything. The device validates input against it,
so id: 5000 fails cleanly instead of half applying. And the same model works over NETCONF, RESTCONF and gNMI.
They will ask next
What is the difference between config and state data?
Config data is what you set (
config true, the default): description, IP address. State data is what the device reports (config false): counters, oper-status. You read both with NETCONFget, but only config withget-config, and you can only write config.Container or list, how do you tell from a path?
A list needs a key to pick one entry:
interfaces/interface=GigabitEthernet1in RESTCONF,interface[name=GigabitEthernet1]in a gNMI path,interface[name='GigabitEthernet1']in XPath. A container is just a name in the path.
Traps
- Saying YANG is XML. YANG has its own syntax; XML and JSON are encodings of data that follows the model.
- Confusing YANG with SNMP MIBs. Both are models, but MIBs (SMIv2) are mostly read-only and tied to SNMP.