My Personal Notes on the Yang Data Model
See also: netconf-yang.org for details on using YANG, & NETCONF
1 YANG
Yet Another Next Generation, YANG
YANG is the data modelling language. It is a modern replacement of mibs
used
in snmp. Could think of it as SNMPv4
.
Do not confuse with YANG Data Models. (see YANG Data Models below)
Here is some YANG
module ietf-interfaces { import ietf-yang-types { prefix yang; } container interfaces { list interface { key "name"; leaf name { type string; } leaf enabled { type boolean; default "true"; } }
You will notice that the YANG model has these statements:
- model
- augment
- container
- list
- leaf
That follows this YANG data model
- Model - container - list - leaf - leaf - leaf - list - leaf - leaf - leaf - container - list - leaf - leaf
From Wikipedia, "YANG (Yet Another Next Generation)
[3] is a data modeling
language for the definition of data sent over network management protocols
such as the NETCONF and RESTCONF. The YANG data modeling language is
maintained by the NETMOD [6] working group in the Internet Engineering Task
Force (IETF) and was published as RFC 6020 in October 2010. The data modeling
language can be used to model both configuration data as well as state data
of network elements. Furthermore, YANG can be used to define the format of
event notifications emitted by network elements and it allows data modelers
to define the signature of remote procedure calls that can be invoked on
network elements via the NETCONF protocol. The language, being protocol
independent, can then be converted into any encoding format, e.g. XML or JSON,
that the network configuration protocol supports.
YANG is a modular language representing data structures in an XML tree format
.
The data modeling language comes with a number of built-in data types.
Additional application specific data types can be derived from the built-in
data types. More complex reusable data structures can be represented as
groupings. YANG data models can use XPATH expressions to define constraints
on the elements of a YANG data model.
YANG models were defined in 2010. RFC 6020 NETCONF defined in 2006, RFC 4741 RESTCONF defined in 2017, RFC 8040 gRPC defined in 2015, by Google.
2 Transport (Protocols) vs Data (Model)
Transport Protocol could be NETCONF, RESTCONF, or gRPC
, but all of these
carry the same data, represented in this case. YANG Data Model.
i.e. Payload is in the YANG format.
YANG is expressed in XML, so an instance of something modelled in YANG, is an XML document. As such, there is a mapping from a YANG model into a XML document.
YANG attributes are defined in leafs, which maps to an XML document element
with the same name. Note: in the XML document, the lists
remain un-named,
and un-ordered, and do nost contain any <list></list> elements. If you need
such elements, you would use a container in the YANG model. i.e. Lists are
used for multiple instances in an XML doc, but not attributes or leaf objects.
3 YANG Data, YANG Data Models, or "written in YANG".
They all basically mean the same thing, depending on the Context.
3.1 1. YANG as a Data Modelling Language
First and foremost it is a data modelling language.
- Self-contained
top-level hierarchy of nodes
- Uses "
containers
" to group related nodes Lists
to identify nodes that are stored in a sequence- Each individual attribute of a node is represented by a
Leaf
- Every leaf must have a
associated type
So we have
- Model - container - list - leaf - leaf - leaf - list - leaf - leaf - container - leaf - leaf - leaf - leaf - leaf - container - leaf - list - leaf - leaf
Note: "container" in the YANG context are simply hierarchical grouping of YANG data. NOT docker "containers".
Note: "leaf" in the YANG context is simply a lowest level object, with no sub-objects in the YANG data model. It has NOTHING to do with leaf switches in a spine-leaf architecture.
Here is an example of the ietf-interfaces standard YANG model, written in the YANG language":
module ietf-interfaces { namespace "urn:ietf:params:xml:ns:yang:ietf-interfaces"; prefix if; import ietf-yang-types { prefix yang; } container interfaces { list interface { key "name";g leaf name { type string; } leaf enabled { type boolean; default "true"; } }
Notice that the start is called a "module". That is why 'yang model' and 'yang module' are loosely the same thing.
3.1.1 Namespace
Inside of Netconf, the namespace is used to distinguish between different yang models that a device may support. Inside a device, it may support hundreds of yang data models. When specifying a node(leaf) from one model vs another model you first specify the namespace that the node is in. that is done with the
xmlns=
field, that identifies the urn of that namespace
<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="101"> <get-config> <source> <running/> </source> <filter> <interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces"> <interface> <name>GigabitEthernet 1/0</name> </interface> </interfaces> </filter> </get-config> </rpc>
And another example of using namespaces where leafs called "address" can refer to different things depending on the namaspace you are in. In this case address in ipv4 and address in ipv6:
<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="101"> <get-config> <source> <running/> </source> <filter> <interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces"> <interface> <ipv4 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip"> <address/> </ipv4> <ipv6 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip"> <address/> </ipv6> </interface> </interfaces> <routing xmlns="urn:ietf:params:xml:ns:yang:ietf-routing"> <routing-instance> <interfaces> <interface/> </interfaces> </routing-instance> </routing> </filter> </get-config> </rpc>
3.2 YANG device models and service models:
3.2.1 device data models
provide a descriptive, standardized description of networking entities.
- inteface
- vlan
- device acl
- tunnel
- osfp
- etc
3.2.2 service data models
this brings it up a layer and can describe larger, network-wide things.
- l3 mpls vpn
- mp-bgp
- vrf
- network acl
- system management
- network faults
- etc.
3.3 YANG structure: containers to leafs
3.4 yang description
module that is a self-contained top-level hierarchy of nodes uses containers to group related nodes, lists to identify nodes that are stored in squence
4 yang data models (a.k.a. yang data modules)
yang itself is a language. there are about 5 "standard" data models that use yang:
- ietf,
- itu,
- google's openconfig, &
- vendor specific.
4.1 YANG Data Models
A data model is simply a well understood and agreed upon method to describe
"something". It is a representation of what we are after. As an example consider
this simple "data model" for a person:
- Person
- Gender
- male, female, other
- Height
- feet/inches or meters
- Weight
- Pounds or Kilograms
- Hair Colour
- Brown, Black, Red, Blonde, other
- Eye Colour
- Brown, Blue, Green, Hazel, Black, other
In the network industry we lacked a common model. YANG made to fix that and provide a uniform, standardized model. Think of YANG as
SNMP v5
where in place of MIB and MIB trees with have aYANG datamodel
that can model not just configuration, but state of an object as well. Also note, that the configuration and state of an object in YANG is modelled separately, with typically the configuaration having r/w attributes where state fields are all r/o fields. This makes sense to me, as you should not be resetting or altering the packet counts on an interfaceConfiguration YANG model read-write State YANG model read-only - Gender
4.1.1 industry standard
ietf, itu, google's openconfig
ietf-diffserv-policy.yang
ietf-diffserv-classifier.yang
ietf-diffserv-target.yang
4.1.2 ietf models (only three listed here, but they are common ones)
- ietf-interfaces RFC 8343
- ietf-inet-types
- ietf-yang-types
4.1.3 openconfig.net
developed by google because they thought there were too many yang data models out there. ironically they created another standard. openconfig.net
From their website:
"OpenConfig is an informal working group of network operators sharing the goal of moving our networks toward a more dynamic, programmable infrastructure by adopting software-defined networking principles such as ~declarative configuration~ and model-driven management and operations."
4.1.4 Example Openconfig Model
module openconfig-interfaces { yang-version "1"; grouping interfaces-top { container interfaces { list interface { key "name"; leaf name { type leafref { path "../config/name"; } } container config { uses interface-phys-config; } ... } } } }
4.1.5 Vendor Specific
For example Cisco
cisco-memory-stats.yang
cisco-flow-monitoring
cisco=qos-action-qlimit-cfg
cisco-memory=stats.yang
cisco-qos-action-qlimit-cfg
4.1.6 Downloadable from :
Download these models to examine them from github < C-c C-o to open > I used: git clone https://github.com/YangModels/yang
OpenConfig models are avaiable from https://github.com/openconfig/public
4.2 Cisco XR Yang data models
XR devices ship
with the YANG files
that define the data models they
support
. Using a management protocol (e.g. NETCONF, gRPC, RESTCONF
), you
can programmatically query a device
for the list of models
it supports and
retrieve the model files. In addition, the XR models are made publicly
available
for download on GitHub
. You can use that repository to explore the
XR native models.
4.3 Displaying YANG Data Models
Depending on your needs and context, you can display YANG data modes,
YANG
Language # good if you are writing YANG models, but otherwise too detailed.Clear Text
XML
JSON
* often a good choice, easier to understand etc.HTML/JavaScript
* often a good choice, easier to understand etc.
4.4 Source of Truth for my specific platform?
Need to go to the device itself and download the schema. You can use
YANG Suite
from Cisco (free).
4.5 YANG ordered-by
The "ordered-by" statement defines whether the order of entries
within a list are determined by the user
or the system
. The argument
is one of the strings "system" or "user". If not present, order
defaults to "system".
This statement is ignored if the list represents state data, RPC output parameters, or notification content.
5 YANG Device models vs YANG Service models
A YANG model could describe a service such as an MPLS VPN service.
6 YANG terminology
6.1 Containers (top level objects)
- not to be confused with Docker containers
6.2 Leafs (Attributes of a container)
- not to be confused with a leaf switch in a spine-leaf fabric
6.3 RW are read-write attributes
6.4 RO are read-only attributes
6.5 Type Definitions
Every leaf has a type. The Yang model, or the language states that every leaf has a very specific type associated with it.
6.6 Identities
6.7 Features
6.8 Container interfaces *
These are typically what a developer/operator are interested in.
7 Notifications
From Cisco training:* The next listing shows a notification, with the YANG definition followed by an example in XML encoding.
YANG Definition:
notification link-failure { description "A link failure has been detected"; leaf if-name { type leafref { path "/interface/name"; } } leaf if-admin-status { type admin-status; } leaf if-oper-status { type oper-status; } }
The same definition, now in the XML encoding:
<notification xmlns="…"> <eventTime>2016-09-01T10:00:00Z</eventTime> <link-failure xmlns="…"> <if-name>so-1/2/3.0</if-name> <if-admin-status>up</if-admin-status> <if-oper-status>down</if-oper-status> </link-failure> </notification>
NETCONF notifications
are defined in RFC 5277
. This optional capability is an
addition to the base NETCONF definition. The notification mechanism is
conceptually one of subscribing to a stream of notifications with a filter,
and with start and stop times for notification replay.
The notification statement takes one argument, which is an identifier, followed by a block of substatements that holds detailed notification information.
The example has the YANG definition for a link failure notification, including the interface name and the administration and operational statuses. It is possible for an interface to be in the administration status of "up", meaning it is configured to be available. However, in the example, it is operationally down—perhaps because someone disconnected the cable from the port.