Post

Handling YANG with Python

Handling YANG with Python

What is YANG and Why Should You Care?

As networks become programmable, automated, and API-driven, we need a way to define what data is available, how it’s structured, and how to configure or retrieve it.

That’s where YANG (Yet Another Next Generation) comes in.

YANG is a data modeling language used to model configuration and state data manipulated by NETCONF, RESTCONF

Why YANG Matters:

  • Standardizes device configurations and telemetry
  • Defines data schema for network protocols (BGP, OSPF, Interfaces, QoS, etc.)
  • Powers model-driven telemetry and programmable networks
  • Is vendor-agnostic, but supports vendor-specific extensions

YANG allows vendors and standards bodies (like IETF) to define data models, which network automation tools (like Ansible, Python scripts, Cisco NSO, Juniper PyEZ) can use.


Anatomy of a YANG Model

Here’s a basic example of a YANG model that describes a network interface:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
module my-interfaces {
  namespace "http://example.com/my-interfaces";
  prefix "if";

  container interfaces {
    list interface {
      key "name";
      leaf name {
        type string;
      }
      leaf enabled {
        type boolean;
        default "true";
      }
      leaf mtu {
        type uint16;
      }
    }
  }
}

Key Building Blocks:

  • module: Defines the model and its namespace
  • container: Groups related nodes
  • list: Defines a list of items (like multiple interfaces)
  • leaf: Defines a single value
  • type: Defines the datatype (string, int, boolean, etc.)
  • default: Provides default values

This model says: “Each device has interfaces. Each interface has a name, an enabled state, and an MTU.”


YANG + NETCONF in Action

Let’s say a device supports the IETF interfaces YANG model (ietf-interfaces). You can query, configure, and modify interfaces using NETCONF.


Hands-On: Use Python + NETCONF + YANG

Step 1: Install Required Python Libraries

1
pip install ncclient xmltodict

Step 2: Get Interface Config via NETCONF

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from ncclient import manager
import xmltodict
import json

device = {
    "host": "10.10.10.1",
    "port": 830,
    "username": "admin",
    "password": "admin",
    "hostkey_verify": False
}

filter = """
<filter>
  <interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces"/>
</filter>
"""

with manager.connect(**device) as m:
    response = m.get(filter)
    data = xmltodict.parse(response.xml)
    print(json.dumps(data, indent=2))

Sample Output (from a real router):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
  "rpc-reply": {
    "data": {
      "interfaces": {
        "interface": [
          {
            "name": "GigabitEthernet0/0",
            "enabled": "true",
            "ipv4": {
              "address": {
                "ip": "192.0.2.1",
                "netmask": "255.255.255.0"
              }
            }
          }
        ]
      }
    }
  }
}

Step 3: Configure Interface via YANG Model (NETCONF Edit)

Let’s disable an interface by sending an <edit-config> RPC:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
config_payload = """
<config>
  <interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
    <interface>
      <name>GigabitEthernet0/0</name>
      <enabled>false</enabled>
    </interface>
  </interfaces>
</config>
"""

with manager.connect(**device) as m:
    response = m.edit_config(config=config_payload, target="running")
    print(response)

✅ This config complies with the YANG model. NETCONF checks your payload against the model and rejects anything invalid.


Where Do You Find YANG Models?

  • IETF YANG Catalog: https://yangcatalog.org
  • OpenConfig (vendor-neutral): https://github.com/openconfig/public
  • Vendor Docs: Cisco, Juniper, Nokia, Arista, etc.

Download .yang files and explore them. They’re readable, structured, and often come with schema trees to visualize relationships.


Tools to Explore YANG Models

  • pyang: CLI tool to lint and visualize YANG
    1
    2
    
      pip install pyang
      pyang -f tree ietf-interfaces.yang
    

    Output:

    1
    2
    3
    4
    5
    6
    
      module: ietf-interfaces
        +--rw interfaces
           +--rw interface* [name]
              +--rw name       string
              +--rw enabled?   boolean
              +--rw mtu?       uint16
    
  • YangSuite (by Cisco): GUI explorer for YANG models
  • YANG Explorer: Interactive GUI, deprecated but useful
This post is licensed under CC BY 4.0 by the author.