How do I work with instances of YANG data?

This document contains some examples of encoding and decoding yang data. To perform these operations, the CodecService is used.

The below approaches can be used to perform encoding and decoding of an interface Ipv4 loopback configuration. For these examples, the Cisco_IOS_XR_ifmgr_cfg.InterfaceConfigurations class is used. Note that the ydk and ydk-models-cisco-ios-xr python packages need to be installed for this example.

Converting between JSON and XML

To parse a JSON string representing yang data into a YDK python object and then to an XML string, the below approach can be used.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from ydk.providers import CodecServiceProvider
from ydk.services import CodecService

# Instantiate the codec service
codec = CodecService()

# Instantiate codec providers with json and xml options
json_provider = CodecServiceProvider(type='json')
xml_provider = CodecServiceProvider(type='xml')

# Declare the JSON configuration
if_json = ''' {
  "Cisco-IOS-XR-ifmgr-cfg:interface-configurations": {
    "interface-configuration": [
      {
        "active": "act",
        "interface-name": "Loopback0",
        "description": "PRIMARY ROUTER LOOPBACK",
        "Cisco-IOS-XR-ipv4-io-cfg:ipv4-network": {
          "addresses": {
            "primary": {
              "address": "172.16.255.1",
              "netmask": "255.255.255.255"
            }
          }
        }
      }
    ]
  }
}
'''

# Invoke the decode method  to decode the JSON payload to a YDK python object
interface_configurations = codec.decode(json_provider, if_json)

# Invoke the encode method to encode the YDK python object to an XML string
if_xml = codec.encode(xml_provider, interface_configurations)
print(if_xml)

Converting to JSON

To convert a YDK python object into a JSON string, the below approach can be used. Note that the attribute primary is an instance of a presence class, which is set to None by default. So it needs to be assigned to a new instance of its class.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from ydk.providers import CodecServiceProvider
from ydk.services import CodecService
from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ifmgr_cfg

# Instantiate the codec service
codec = CodecService()

# Instantiate the provider with json option
json_provider = CodecServiceProvider(type='json')

# Instantiate the interface configuration class to configure the IPv4 loopback
interface_configurations =  Cisco_IOS_XR_ifmgr_cfg.InterfaceConfigurations()

# Instantiate the InterfaceConfiguration list instance
interface_configuration = interface_configurations.InterfaceConfiguration()
interface_configuration.active = "act"
interface_configuration.interface_name = "Loopback0"
interface_configuration.description = "PRIMARY ROUTER LOOPBACK"

# Instantiate the Primary presence node
interface_configuration.ipv4_network.addresses.primary = interface_configuration.ipv4_network.addresses.Primary()
interface_configuration.ipv4_network.addresses.primary.address = "172.16.255.1"
interface_configuration.ipv4_network.addresses.primary.netmask = "255.255.255.255"

# Append the list instance to the parent list
interface_configurations.interface_configuration.append(interface_configuration)

# Invoke the encode method to encode the YDK python object to a JSON payload
json = codec.encode(json_provider, interface_configurations)
print(json)