Infrastructure and Automation · Topic 7 of 8

Interpreting RESTCONF and NETCONF Results and YANG Models

Learn to read RESTCONF and NETCONF replies and basic YANG models, including containers, lists, leaves, keys, types, config false and the pyang tree view.

  • 6 min
  • Includes a diagram
In a nutshell
  • A RESTCONF reply is JSON or XML, and the top key holds the module name and container.
  • A NETCONF reply is an rpc-reply with a data element, or an rpc-error on failure.
  • A YANG container groups nodes, a list repeats and has a key, and a leaf holds one value.

What it is

A RESTCONF or NETCONF query returns structured data that follows a YANG model, and you must be able to read both the reply and the model.

The reply shows the real values on a device. The YANG model shows the shape of that data, with its names, types and which parts are configuration.

Once you can read the model, you can predict what a reply looks like, and the other way round.

YANG modeldefines the shapeguidesQueryRESTCONF GET or NETCONF getreturnsReplyJSON or XML dataread byScriptpicks the values

How it works

Start with the model, then match the reply against it.

A RESTCONF reply

A GET on the interfaces path returns JSON like this.

{
  "ietf-interfaces:interfaces": {
    "interface": [
      {
        "name": "GigabitEthernet1",
        "description": "WAN link",
        "type": "iana-if-type:ethernetCsmacd",
        "enabled": true,
        "ietf-ip:ipv4": {
          "address": [
            {"ip": "192.0.2.1", "netmask": "255.255.255.0"}
          ]
        }
      }
    ]
  }
}

Reading it:

  • The top key is the module name, a colon, and the container name.
  • interface is a JSON list, with one entry per interface.
  • Each entry has leaves such as name, enabled and description.
  • Keys with a prefix, such as ietf-ip:ipv4, come from another module that adds to the model.
  • In Python the value is data["ietf-interfaces:interfaces"]["interface"][0]["name"].

RESTCONF status codes

CodeMeaning
200The GET worked, and the body holds the data
201A resource was created, after a POST or PUT
204The change worked, with no body
400The request or data is invalid
401Authentication failed
404The path does not exist, or the data is not found
409A conflict, such as creating something that already exists

A NETCONF reply

A NETCONF reply is XML. Because it is a tree of named elements, it can be pictured as an outline.

rpc-reply
  data
    interfaces
      interface
        name: GigabitEthernet1
        description: WAN link
        enabled: true

The reply is an rpc-reply element, and its data element holds the answer to a get or get-config request. The element names match the YANG names. The XML namespace tells you which module each element belongs to.

If the request works but returns no data, the reply holds an empty data element. A successful change has an ok element, and a failure has an rpc-error element.

Part of an errorMeaning
error-typeThe layer where it failed, such as application or protocol
error-tagA short reason, such as invalid-value or data-exists
error-messageReadable text

In Python with ncclient, the reply object has an xml attribute with the text, and data_xml for the data part. Parse it with xmltodict or ElementTree.

A basic YANG module

module example-interfaces {
  namespace "urn:example:interfaces";
  prefix exif;

  container interfaces {
    list interface {
      key "name";

      leaf name {
        type string;
      }
      leaf description {
        type string;
      }
      leaf enabled {
        type boolean;
        default "true";
      }
      leaf mtu {
        type uint16 {
          range "68..9000";
        }
      }
      leaf oper-status {
        type string;
        config false;
      }
    }
  }
}

Reading the module

KeywordMeaning
moduleThe top level name of the model
namespace and prefixA unique identifier and a short label for the module
containerA grouping node with no value of its own
listA repeated set of entries, one per item
keyThe leaf that uniquely identifies each list entry
leafA single value with a type
leaf-listA list of simple values
typeThe kind of value, such as string, boolean, uint16 or enumeration
defaultThe value used if none is set
config falseA read only state node, not configuration

Anything that lacks config false can be configured. The range on mtu limits it to 68 to 9000, and the device refuses values outside it.

From this module you can predict the RESTCONF path to one entry, which is /restconf/data/example-interfaces:interfaces/interface=GigabitEthernet1, with the list key after an equals sign.

The pyang tree view

A YANG module tree: module, container, and a list keyed by name with read-write leaves and one read-only state leaf, plus the RESTCONF path that selects one list entry by its key. The pyang tool prints a compact tree of a module.

$ pyang -f tree example-interfaces.yang
module: example-interfaces
  +--rw interfaces
     +--rw interface* [name]
        +--rw name           string
        +--rw description?   string
        +--rw enabled?       boolean
        +--rw mtu?           uint16
        +--ro oper-status?   string

Here rw means read and write, and ro means read only. A star marks a list, and the key is in square brackets. A question mark marks an optional leaf.

Matching replies to models

If a reply lacks a leaf that the model marks optional, the value was not set. If a list key is missing from a request, the device cannot tell which entry you mean.

Exam tip

In the tree view, rw leaves are configuration and ro leaves are state. A star means a list, and the name in brackets is its key.

What to remember for the exam

Seven facts cover this topic.

  • A RESTCONF reply is JSON or XML, and the top key holds the module name and container.
  • A NETCONF reply is an rpc-reply with a data element, or an rpc-error on failure.
  • A YANG container groups nodes, a list repeats and has a key, and a leaf holds one value.
  • config false marks read only state data.
  • In pyang tree output, rw is configurable, ro is read only, and * marks a list.
  • A list entry in a RESTCONF path is selected with the key after an equals sign.
  • A 204 means that a change succeeded with no body, and a 409 means a conflict.

Common mistakes

Four mistakes are typical.

Confusing container and list. A container is one grouping, while a list has many entries with a key.

Treating read only data as configuration. Nodes with config false cannot be set.

Ignoring the module prefix. In JSON, ietf-ip:ipv4 comes from a different module than its parent.

Expecting a body after a successful PUT or PATCH. A 204 reply has none.

Watch out

Do not guess a key or value. Read the module, because a wrong value is rejected or, worse, accepted and misapplied.

Check yourself

Work through these three questions.

  • What does rw or ro mean in the pyang tree?

    rw is read and write configuration, and ro is read only state.

  • What is the role of key in a YANG list?

    It names the leaf that uniquely identifies each entry in the list.

  • What does a NETCONF rpc-error element tell you?

    The request failed, and it gives the error type, a tag with the reason and a message.

Topic 7 of 8 in Infrastructure and Automation

Test yourself on Infrastructure and Automation

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.