04-15-2020 04:11 AM - edited 04-15-2020 04:12 AM
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.
Solved! Go to Solution.
04-15-2020 05:24 AM
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).
04-15-2020 05:11 AM
04-15-2020 05:32 AM
04-15-2020 05:24 AM
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).
04-15-2020 05:40 AM
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide