Network automation protocols such as NETCONF, RESTCONF, gNMI, and Ansible use XML, JSON,, YAML or Protobuf data structures as a common language for data exchange between network devices and automation controller.

In this section we will have a practical overview and comparison between XML, JSON and YAML data formats or structures. Protobuf will be discussed in gNMI section.

XML, JSON and YAML in Network Automation

The first question that probably arises is why we use XML, JSON and YAML standard data formats in YANG based automation protocols?

XML, JSON and YAML are data formats that automation protocols use as a common language between network devices and automation controller.

Otherwise, automation controller must transfer data with each device with a different format since the configuration and also the output format of each device is different from other devices.

And which data format is the best and the most common option? Actually depending on the automation protocol you’re using, you don’t have many options.

Data Formats in YANG based Automation Protocols
Data Formats in YANG based Automation Protocols

In NETCONF the only option is XML.

There are two options in RESTCONF, XML and JSON, but usually JSON is preferred as it is easier to read and also faster to transmit.

In ANSIBLE, the only option is YAML, the most readable and faster format.

Common Types of Data transferred in Automation protocols

And what are the type of data that are usually sent in network automation? In other words, which types of data should be transferable through standard data formats?

Common Types of Data transferred in Automation protocols
Common Types of Data transferred in Automation protocols

Usually data are in the format of a dictionary, a list or any combination of list and dictionary, for example a list of dictionary or a dictionary with a list of values or any other combination.

This figure shows five examples of different data types that are usually transferred through automation protocols.

A dictionary with a format of “key:value”. Here, “IP”, “MASK” and “Description” are the keys, each with a value.

A List of data is the second example. Here a list of interfaces are exchanged through automation protocol.

In the third example, a list of dictionary will be exchanged through automation control. Here a list of interfaces are displayed. In each element of the List, there is a dictionary with “Interface”, “IP” and “MASK” as the Keys.

In the fourth example, a dictionary with a list of values are displayed. “Interface” is the key but with a list of values, “Ethernet1”, “Ethernet2” and “Ethernet3”.

In the last example, a more complex example is displayed. A “dictionary-list-dictionary” format.

In the first dictionary, “Interface” is a key with a list of value. In each element of list, again we have a dictionary, with “IP” and “MASK” as the keys.

Therefore it is important that standard data formats are capable to store and send list, dictionary and any combination of list and dictionary.

Of course, XML, JSON and YAML are standard data formats, which are capable to send these  types of data.

Compare XML, JSON and YAML Data Formats

This is a Figure showing the difference between XML, JSON and YAML data formats using examples.

All examples are for specific data and is taken from the developer.cisco.com documents stored in the github source community.

Compare XML JSON and YAML Data Formats_
Compare XML JSON and YAML Data Formats_

The first question regarding the difference between XML, JSON and YAML is how dictionary and list are represented in these data structures.

As you can see in the figure, in XML, dictionary is represented as “<key>value</key>„ like HTML tags. In XML whitespace is not important but make it easier to read.

XML list uses repeated instances of “<key>value</key>”.

<?xml version="1.0" encoding="UTF-8" ?>
<interface xmlns="ietf-interfaces">
  <name>GigabitEthernet2</name>
  <description>Wide Area Network</description>
  <enabled>true</enabled>
  <ipv4>
    <address>
      <ip>172.16.0.2</ip>
      <netmask>255.255.255.0</netmask>
    </address>
  </ipv4>
</interface>
<?xml version="1.0" encoding="UTF-8" ?>
<addresses>
  <ip>172.16.0.2</ip>
  <netmask>255.255.255.0</netmask>
</addresses>
<addresses>
  <ip>172.16.0.3</ip>
  <netmask>255.255.255.0</netmask>
</addresses>
<addresses>
  <ip>172.16.0.4</ip>
  <netmask>255.255.255.0</netmask>
</addresses>

In JSON, dictionary represented as “Key”: value surrounded by { } and different “Key”: value are separated by comma but no comma at the end. In JSON like XML, whitespace is not important but it helps to be more readable.

JSON list is repressed inside [ ] and list elements are separated by comma. Again no comma for the last element.

{
  "ietf-interfaces:interface": {
    "name": "GigabitEthernet2",
    "description": "Wide Area Network",
    "enabled": true,
    "ietf-ip:ipv4": {
      "address": [
        {
          "ip": "172.16.0.2",
		  "netmask": "255.255.255.0"
		}
	  ]
	}
  }
}
{
  "addresses": [
    {
	  "ip": "172.16.0.2",
	  "netmask": "255.255.255.0"
	},
	{
	  "ip": "172.16.0.3",
	  "netmask": "255.255.255.0"
	},
	{
	  "ip": "172.16.0.4",
	  "netmask": "255.255.255.0"
	}
  ]
}

YAML is the minimalist format which is commonly used for configuration files.

In YAML, dictionary is represented as a normal key: value. No comma is used in YAML format. But Whitespace is very important in YAML and related set of data are configured at the common indentation level.

YAML list uses “” character to indicate a list element.

---
ietf-interfaces:interface:
  name: GigabitEthernet2
  description: Wide Area Network
  enabled: true
  ietf-ip:ipv4:
    address:
      - ip: 172.16.0.2
        netmask: 255.255.255.0
---
addresses:
  - ip: 172.16.0.2
    netmask: 255.255.255.0
  - ip: 172.16.0.3
    netmask: 255.255.255.0
  - ip: 172.16.0.4
    netmask: 255.255.255.0
Back to: YANG based Network Automation using NETCONF RESTCONF gNMI > NETCONF, RESTCONF and gNMI Introduction

Leave a Reply

Your email address will not be published. Required fields are marked *


Post comment