Cisco Platforms and Development · Topic 6 of 8

Model Driven Programmability: YANG, NETCONF and RESTCONF

Learn how YANG models, NETCONF and RESTCONF work together for model driven programmability on Cisco devices, with datastores, operations, paths and Python.

  • 5 min
  • Includes a diagram
In a nutshell
  • YANG models the data. NETCONF and RESTCONF are protocols that carry it.
  • NETCONF uses XML over SSH, on port 830 by default.
  • RESTCONF uses HTTPS and supports JSON or XML.

What it is

Model driven programmability manages a device through a formal data model instead of through CLI text.

A YANG model describes what can be configured and read. NETCONF and RESTCONF are the protocols that carry requests that follow that model.

The result is structured, predictable data that scripts can read and change safely, whatever the device's CLI looks like.

YANG modeldescribes the dataused byNETCONFXML over SSHYANG modelused byRESTCONFJSON or XML over HTTPSNETCONFconfiguresDeviceIOS XE

How it works

The model defines the data, and the protocol decides how to send it.

YANG

YANG is a data modelling language. A YANG module defines containers, lists, leaves and their types. It also says which parts are configuration and which are read only status.

Models come from three sources:

SourceExample
IETF standardietf-interfaces
OpenConfig, vendor neutralopenconfig-interfaces
Cisco nativeCisco-IOS-XE-native

A Cisco native model reflects the features of that platform. A standard model works across vendors but may offer fewer features.

NETCONF

NETCONF is a network management protocol defined by the IETF. It uses XML and normally runs over SSH on port 830.

Client and server first exchange a hello message that lists their capabilities. The client then sends remote procedure calls, called RPCs, and the server replies.

OperationPurpose
getRead configuration and state data
get-configRead configuration from a datastore
edit-configChange configuration
copy-configCopy a datastore to another
delete-configDelete a datastore
lock and unlockStop other sessions changing a datastore
commitApply the candidate datastore
close-sessionEnd the session

NETCONF works on datastores. The running datastore holds the active configuration. Some devices also have a candidate datastore, where you stage changes before a commit, and a startup datastore.

NETCONF is transactional. A change is applied as a whole or not at all, and it can be rolled back.

NETCONF from Python

The ncclient library is the usual Python client.

from ncclient import manager

with manager.connect(
    host=HOST,
    port=830,
    username="admin",
    password=PASSWORD,
    hostkey_verify=False,
) as m:
    for cap in m.server_capabilities:
        print(cap)
    reply = m.get_config(source="running")
    print(reply.xml)

The device replies with an XML document. You usually pass a filter so that you receive only the part that you need.

RESTCONF

RESTCONF gives HTTP access to the same YANG data. It maps the standard HTTP methods onto the model, and it uses HTTPS.

HTTP methodRESTCONF action
GETRead data
POSTCreate a new child resource
PUTReplace a resource
PATCHMerge changes into a resource
DELETERemove a resource

Data lives under /restconf/data/, and operations that run an action live under /restconf/operations/. The path names the module and the node, and a list entry is selected with its key after an equals sign.

GET /restconf/data/ietf-interfaces:interfaces/interface=GigabitEthernet1
Accept: application/yang-data+json

Special characters in a key must be percent encoded, so a slash in an interface name becomes %2F.

The two media types are application/yang-data+json and application/yang-data+xml.

A RESTCONF change

This request changes the description of an interface.

PATCH /restconf/data/ietf-interfaces:interfaces/interface=GigabitEthernet1
Content-Type: application/yang-data+json
{
  "ietf-interfaces:interface": {
    "name": "GigabitEthernet1",
    "description": "Uplink to core"
  }
}

A successful PATCH normally returns 204 with no body. The JSON key carries the module name, a colon, and the node name.

NETCONF compared with RESTCONF

A script reaches a device through NETCONF (XML over SSH, port 830) or RESTCONF (HTTPS), and both follow the same YANG data model that defines what the device exposes.

PointNETCONFRESTCONF
TransportSSH, port 830HTTPS
EncodingXMLJSON or XML
StyleRPC operationsREST methods
DatastoresRunning, candidate and startupRunning datastore
TransactionsYes, with candidate and commitEach request is a transaction

Why use models

  • The same data model works with every tool and language.
  • Data types are checked, so mistakes are found early.
  • Output is structured, so there is no screen scraping.
  • Tools such as pyang can read the model and show its tree.
Exam tip

A fast way to recall the pairing: NETCONF means XML over SSH, and RESTCONF means REST over HTTPS, and YANG is the model behind both.

What to remember for the exam

Seven facts cover this topic.

  • YANG models the data. NETCONF and RESTCONF are protocols that carry it.
  • NETCONF uses XML over SSH, on port 830 by default.
  • RESTCONF uses HTTPS and supports JSON or XML.
  • NETCONF operations include get, get-config, edit-config and commit.
  • RESTCONF uses GET, POST, PUT, PATCH and DELETE under /restconf/data/.
  • RESTCONF media types are application/yang-data+json and application/yang-data+xml.
  • Models come from the IETF, from OpenConfig and from Cisco.

Common mistakes

Four mistakes are typical.

Calling YANG a protocol. It is a modelling language. The protocols are NETCONF and RESTCONF.

Using the wrong media type. A plain application/json header may be rejected by RESTCONF.

Forgetting to enable the service. The device must have NETCONF or RESTCONF turned on.

Forgetting to encode a slash in a key. An interface called GigabitEthernet0/0 must have its slash encoded in the path.

Watch out

Check the running configuration and the datastore before you push an edit. A wrong edit sent to a live device takes effect at once.

Check yourself

Work through these three questions.

  • What is the relationship between YANG, NETCONF and RESTCONF?

    YANG models the data, and NETCONF and RESTCONF are the two protocols that carry requests that follow the model.

  • What transport and port does NETCONF use by default?

    It uses SSH on port 830.

  • Which RESTCONF method merges changes into an existing resource?

    PATCH.

Topic 6 of 8 in Cisco Platforms and Development

Test yourself on Cisco Platforms and Development

The full Cisco CCNA 200-901 DEVASC question bank has 808 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.