How do I create, update, read and delete?

This document contains some examples of creating, reading and deleting yang data using YDK. To perform these operations, the CRUDService is used. Also, in these examples, YFilter is used to mark parts of the data for particular operations.

Creating a configuration with a list and a presence class

To configure a rule in the SNMP trap correlator, the below approach can be used.

Note that the Cisco_IOS_XR_snmp_agent_cfg.Snmp.Correlator.Rules.Rule class is a YList. So it needs to be instantiated and appended to its parent.

Also, the attribute non_stateful 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.

 1from ydk.models.cisco_ios_xr import Cisco_IOS_XR_snmp_agent_cfg
 2
 3# Create the top-level container
 4snmp = Cisco_IOS_XR_snmp_agent_cfg.Snmp()
 5
 6# Create the list instance
 7rule = Cisco_IOS_XR_snmp_agent_cfg.Snmp.Correlator.Rules.Rule()
 8rule.name = 'abc'
 9
10# Instantiate and assign the presence class
11rule.non_stateful = Cisco_IOS_XR_snmp_agent_cfg.Snmp.Correlator.Rules.Rule.NonStateful()
12
13rule.non_stateful.timeout = 3
14
15# Append the list instance to its parent
16snmp.correlator.rules.rule.append(rule)
17
18# Call the CRUD create on the top-level snmp object
19# (assuming you have already instantiated the service and provider)
20result = crud.create(provider, snmp)

Creating and replacing a configuration

First, let us create a configuration for the openconfig_bgp.Bgp.Global_.Config class. Here, we set the leaf as_, which represents the autonomous system number, to 65001 and the leaf router_id to '10.0.0.1'.

 1from ydk.types import YFilter
 2from ydk.models.openconfig import openconfig_bgp
 3
 4# First, create the top-level Bgp() object
 5bgp = openconfig_bgp.Bgp()
 6
 7# Populate the values for the global config object
 8bgp.global_.config.as_ = 65001
 9bgp.global_.config.router_id = '10.0.0.1'
10
11# Call the CRUD create on the top-level bgp object
12# (assuming you have already instantiated the service and provider)
13result = crud.create(provider, bgp)

Now, let us replace the above configuration with a new configuration for the openconfig_bgp.Bgp.Global_.Config class using the below code.

 1from ydk.types import YFilter
 2from ydk.models.openconfig import openconfig_bgp
 3
 4# First, create the top-level Bgp() object
 5bgp = openconfig_bgp.Bgp()
 6
 7# Set the yfilter attribute of the config object to YFilter.replace
 8bgp.global_.config.yfilter = YFilter.replace
 9
10# Populate the new values for the global config object
11bgp.global_.config.as_ = 65023
12bgp.global_.config.router_id = '25.3.55.12'
13
14# Call the CRUD update on the top-level bgp object
15# (assuming you have already instantiated the service and provider)
16result = crud.update(provider, bgp)

Creating and reading a list

For example, to read the instances of a deeply nested YList called Cisco_IOS_XR_ip_rib_ipv4_oper.Rib.Vrfs.Vrf.Afs.Af.Safs.Saf.IpRibRouteTableNames.IpRibRouteTableName.Routes.Route in the Cisco_IOS_XR_ip_rib_ipv4_oper module using YDK’s CRUDService, the below approach can be used.

 1from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ip_rib_ipv4_oper
 2from ydk.filters import YFilter
 3
 4# First create the top-level Rib() object
 5rib = Cisco_IOS_XR_ip_rib_ipv4_oper.Rib()
 6
 7# Then create the list instance Vrf()
 8vrf = Cisco_IOS_XR_ip_rib_ipv4_oper.Rib.Vrfs.Vrf()
 9vrf.vrf_name='default'
10
11# Then create the child list element Af() and the rest of the nested list instances
12af = Cisco_IOS_XR_ip_rib_ipv4_oper.Rib.Vrfs.Vrf.Afs.Af()
13af.af_name = 'IPv4'
14
15saf = Cisco_IOS_XR_ip_rib_ipv4_oper.Rib.Vrfs.Vrf.Afs.Af.Safs.Saf()
16saf.saf_name='Unicast'
17
18table_name = Cisco_IOS_XR_ip_rib_ipv4_oper.Rib.Vrfs.Vrf.Afs.Af.Safs.Saf.IpRibRouteTableNames.IpRibRouteTableName()
19table_name.route_table_name = 'default'
20
21# Create the final list instance Route()
22route = Cisco_IOS_XR_ip_rib_ipv4_oper.Rib.Vrfs.Vrf.Afs.Af.Safs.Saf.IpRibRouteTableNames.IpRibRouteTableName.Routes.Route()
23route.yfilter = YFilter.read # set the yfilter attribute for route to YFilter.read
24
25# Append each of the list instances to their respective parents
26table_name.routes.route.append(route)
27saf.ip_rib_route_table_names.ip_rib_route_table_name.append(table_name)
28af.safs.saf.append(saf)
29vrf.afs.af.append(af)
30rib.vrfs.vrf.append(vrf)
31
32# Call the CRUD read on the top-level rib object
33# (assuming you have already instantiated the service and provider)
34rib_oper = crud.read(provider, rib)
35
36vrf_list = rib_oper.vrfs.vrf
37vrf_default = vrf_list["default"]  # or  vrf_default = vrf_list.get("default")

Read all VRF configuration:

 1from ydk.models.cisco_ios_xr import Cisco_IOS_XR_ip_rib_ipv4_oper
 2from ydk.filters import YFilter
 3
 4# First create the top-level Rib() object
 5rib = Cisco_IOS_XR_ip_rib_ipv4_oper.Rib()
 6
 7# Call the CRUD read on the top-level rib object
 8# (assuming you have already instantiated the service and provider)
 9rib_oper = crud.read(provider, rib)
10
11# Access all VRFs in the list
12for vrf in rib_oper.vrfs.vrf:
13    print(vrf.vrf_name)
14
15# Get list of VRF names
16all_vrf_names = rib_oper.vrfs.vrf.keys()
17
18# Iterate over VRF names
19for vrf_name in all_vrf_names:
20    vrf = rib_oper.vrfs.vrf[vrf_name]
21    for af in vrf.afs.af:
22        print("VRF: %s, AF: %s", vrf_name, af)
23
24# Access specific VRF, when name is known
25vrf = rib_oper.vrfs.vrf["default"]
26if vrf is not None:
27    for af in vrf.afs.af:
28        print("VRF: %s, AF: %s", "default", af)

Reading a leaf

For example, to read a YLeaf called running in the Cisco_IOS_XR_clns_isis_cfg.Isis.Instances.Instance class in the Cisco_IOS_XR_clns_isis_cfg module using YDK’s CRUDService, the below approach can be used.

 1from ydk.models.cisco_ios_xr import Cisco_IOS_XR_clns_isis_cfg
 2from ydk.types import Empty
 3from ydk.filters import YFilter
 4
 5# First create the top-level Isis() object
 6isis = Cisco_IOS_XR_clns_isis_cfg.Isis()
 7
 8# Create the list instance
 9ins = Cisco_IOS_XR_clns_isis_cfg.Isis.Instances.Instance()
10ins.instance_name = 'default'
11
12# Set the yfilter attribute of the leaf called 'running' to YFilter.read
13ins.running = YFilter.read
14
15# Append the instance to the parent
16isis.instances.instance.append(ins)
17
18# Call the CRUD read on the top-level isis object
19# (assuming you have already instantiated the service and provider)
20result = crud.read(provider, isis)

Deleting a list element

For example, to delete a YList called Instance in the Cisco_IOS_XR_clns_isis_cfg module using YDK’s CRUDService, the below approach can be used.

 1from ydk.models.cisco_ios_xr import Cisco_IOS_XR_clns_isis_cfg
 2from ydk.types import Empty
 3from ydk.filters import YFilter
 4
 5# First create the top-level Isis() object
 6isis = Cisco_IOS_XR_clns_isis_cfg.Isis()
 7
 8# Create the list instance
 9ins = Cisco_IOS_XR_clns_isis_cfg.Isis.Instances.Instance()
10ins.instance_name = 'xyz'
11
12# Set the yfilter attribute of the leaf called 'running' to YFilter.delete
13ins.yfilter = YFilter.delete
14
15# Append the instance to the parent
16isis.instances.instance.append(ins)
17
18# Call the CRUD update on the top-level isis object
19# (assuming you have already instantiated the service and provider)
20result = crud.update(provider, isis)

Deleting entire list

In order to delete entire list, all its elements must be deleted. It could be done like in example above. The issue here is that user have to know all the keys of the YList elements. To address this issue YDK offers a shortcut, which allows user to delete all list elements in one operation.

 1from ydk.models.cisco_ios_xr import Cisco_IOS_XR_clns_isis_cfg
 2from ydk.filters import YFilter
 3
 4# First read configuration of top level container
 5isis = crud.read_config(provider, Cisco_IOS_XR_clns_isis_cfg.Isis()
 6
 7# Then set YFilter.delete operation to the YList object
 8if len(isis.instances.instance) > 0:
 9    isis.instances.instance.yfilter = YFilter.delete
10
11    # Call the CRUD update on modified 'isis' object
12    result = crud.update(provider, isis)

Deleting a leaf

For example, to delete a YLeaf called timer of type int in the Cdp class in the Cisco_IOS_XR_cdp_cfg module using YDK’s CRUDService, the below approach can be used.

 1from ydk.models.cisco_ios_xr import Cisco_IOS_XR_cdp_cfg
 2from ydk.filters import YFilter
 3
 4# First create the top-level Cdp() object
 5cdp = Cisco_IOS_XR_cdp_cfg.Cdp()
 6
 7# Set a dummy value to the leaf
 8cdp.timer = 5
 9# Assign YFilter.delete to the 'timer' leaf
10cdp.timer = YFilter.delete
11
12# Call the CRUD update on the top-level cdp object
13# (assuming you have already instantiated the service and provider)
14result = crud.update(provider, cdp)

For example, to delete a YLeaf called running of type Empty in the Instance class in the Cisco_IOS_XR_clns_isis_cfg module using YDK’s CRUDService, the below approach can be used.

 1from ydk.models.cisco_ios_xr import Cisco_IOS_XR_clns_isis_cfg
 2from ydk.types import Empty
 3from ydk.filters import YFilter
 4
 5# First create the top-level Isis() object
 6isis = Cisco_IOS_XR_clns_isis_cfg.Isis()
 7
 8# Create the list instance
 9ins = Cisco_IOS_XR_clns_isis_cfg.Isis.Instances.Instance()
10ins.instance_name = 'default'
11
12# Assign YFilter.delete to the 'running' leaf
13ins.running = YFilter.delete
14
15# Append the instance to the parent
16isis.instances.instance.append(ins)
17
18# Call the CRUD update on the top-level isis object
19# (assuming you have already instantiated the service and provider)
20result = crud.update(provider, isis)

Applying CRUD to multiple entities

You can apply CRUD operations on multiple entities in one CRUD service call. For example, you want to ‘read’ BGP and Interfaces configuration together.

 1from ydk.types import Filter, Config
 2from ydk.models.openconfig import openconfig_bgp, openconfig_interfaces
 3
 4# First, create the top-level Bgp and Interface objects
 5int_filter = openconfig_interfaces.Interfaces()
 6bgp_filter = openconfig_bgp.Bgp()
 7
 8# Create read filter
 9read_filter = Filter(int_filter, bgp_filter)
10
11# Call the CRUD read-config to get configuration of entities
12result = crud.read_config(provider, read_filter)
13
14# Access read results from returned Config collection
15int_config = result[int_filter]
16bgp_config = result[bgp_filter]
17
18# Or print all configuration in XML format
19codec_service = CodecService()
20codec_provider = CodecServiceProvider()
21codec_provider.encoding = EncodingFormat.XML
22for entity in result:
23    xml_encode = codec_service.encode(codec_provider, entity)
24    print(xml_encode)