cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2062
Views
0
Helpful
4
Replies

reference Yang objects with Python code

mastedarq
Level 1
Level 1

I've got an issue with referencing Yang objects from Python.

I'm capable to reference leaf objects as long as they're defined within "global" service space, but as soon as I move them into the list object I've no clue how to "reach" them.

Please find scrap of my Yang model below:

module ACCESS-LISTS {
... omitted std cfg
list ACCESS-LISTS {
... omitted std cfg
list acl { key acl_name; leaf acl_name { type string; } leaf acl_id { type uint32; } leaf line { type string; } }
}
}

Now my piece of Python code:

        vars = ncs.template.Variables()
        vars.add('ACL_NAME', service.acl.acl_name)
        vars.add('ID', service.acl.acl_id)
        vars.add('LINE', service.acl.line)

The rest of the Python file is as generated by the ncs-maker, so the servicepoint and Callbacks, etc are in place.

When I have Yang objetcs (acl_name, acl_id, line) in global space (not inside acl list) I'm able to reference them by using below Python code:

        vars = ncs.template.Variables()
        vars.add('ACL_NAME', service.acl_name)
        vars.add('ID', service.acl_id)
        vars.add('LINE', service.line)

But as soon as I place them inside above mentioned list and modify Python code accordingly, I've got below response when commiting ACCESS-LISTS service on NSO:

Aborted: Python cb_create error. 'List' object has no attribute 'acl_name'

I'm doing something wrong, just please give me some directions so I can find out what.

I was also trying to check the type of those access-lists to figure out how to maybe reference them, but the only thing I was able to find was:

type(r13.config.cisco_ios_xr__ipv4.access_list.named_acl)
ncs.maagic.List

R13 is my test router.

I went through maagic.List description, but none of the available attributes was working when I was trying it in my Python code.

1 Accepted Solution

Accepted Solutions

tcragg1
Cisco Employee
Cisco Employee

To directly access the child nodes of a list, you need to either specify the list key to access the nodes within a specific list member, or iterate over the list using a for loop to apply some logic to all members of the list like the following example:

 

for acl_obj in service.acl:
    vars = ncs.template.Variables()
    vars.add('ACL_NAME', acl_obj.acl_name)
    vars.add('ID', acl_obj.acl_id)
    vars.add('LINE', acl_obj.line)

If you do use a for loop to iterate over multiple ACLs, you will need to include the template apply logic within the same for loop (otherwise I think the end result will be that only the last entry in the list actually gets passed to the template).

 

View solution in original post

4 Replies 4

I've read them both, but was unable to find out how to solve my issue.

tcragg1
Cisco Employee
Cisco Employee

To directly access the child nodes of a list, you need to either specify the list key to access the nodes within a specific list member, or iterate over the list using a for loop to apply some logic to all members of the list like the following example:

 

for acl_obj in service.acl:
    vars = ncs.template.Variables()
    vars.add('ACL_NAME', acl_obj.acl_name)
    vars.add('ID', acl_obj.acl_id)
    vars.add('LINE', acl_obj.line)

If you do use a for loop to iterate over multiple ACLs, you will need to include the template apply logic within the same for loop (otherwise I think the end result will be that only the last entry in the list actually gets passed to the template).

 

Yes, it works now. Thank You.
Polls
AI-powered tools for network troubleshooting are likely to be part of everyone’s workflow sooner or later. What is the single biggest challenge or concern you see with adopting these tools in your organization?