03-18-2020 01:30 AM
Hello,
I have the following yang model:
module cr-art { namespace "http://example.com/cr-art"; prefix cr-art; ... list cr-art { key DEVICE; uses ncs:service-data; ncs:servicepoint cr-art-servicepoint; leaf DEVICE { type leafref { path "/ncs:devices/ncs:device/ncs:name"; } } ...
Now I am trying to get a list of all the devices configured in service cr-art under python. So far I have tried:
for i in root.cr_art.cr_art.DEVICE: self.log.info(i)
this returns:
Error: Python cb_create error. 'List' object has no attribute 'cr_art'
I have also tried:
for i in root.cr_art.DEVICE: self.log.info(i)
Which returns:
Error: Python cb_create error. 'List' object has no attribute 'DEVICE'
The only thing that does not return an error is:
devices_in_cr_art = [] for i in root.cr_art: devices_in_cr_art.append(i) self.log.info(devices_in_cr_art)
But it prints the following in the logs:
- [ListElement name=cr-art tag=1779143679 keys={artmad1-01}, ListElement name=cr-art tag=1779143679 keys={artmno1-01}]
What I need is only the values of "keys", but I do not seem to be able to access them in an easy way.
Any suggestion, please?
Thanks
Solved! Go to Solution.
03-18-2020 08:39 AM
Yes, there is documentation for the python APIs inside the documentation folder of NSO. If you go to the root folder for the documentation, you should then find the python API docs at the following path: doc/api/python/index.html
For the methods of the basic YANG object types (List, Leaf, Container, etc.), check out the ncs.maagic link from that index webpage.
03-18-2020 09:32 AM
03-18-2020 02:43 AM
You should be able to access them by calling the DEVICE object within each ListElement as you iterate over the List. Try the following:
devices_in_cr_art = [] for i in root.cr_art: devices_in_cr_art.append(i.DEVICE) self.log.info(devices_in_cr_art)
03-18-2020 03:23 AM
Thanks,
That worked!
In fact, what I was trying to do is to check if a value is in the list of devices configured for the service. Something like:
for rv in service.ROUTER_VECINO: if rv.NOMBRE in root.cr_art.DEVICE: self.log.info("Yes, it is")
But that rises an error. What works, thanks to your previous suggestion, is:
devices_in_cr_art = [] for i in root.cr_art: devices_in_cr_art.append(i.DEVICE) for rv in service.ROUTER_VECINO: if rv.NOMBRE in devices_in_cr_art: self.log.info("Yes, it is")
I am now wondering if there is a better way to do this or this is the only valid approach.
03-18-2020 06:42 AM
The NSO List object type in python has a method called "exists" that you may be able to use for this. I think in your case it would look something like the following:
for rv in service.ROUTER_VECINO: if root.cr_art.exists(rv.NOMBRE): self.log.info("Yes, it is")
This should iterate over service.ROUTER_VECINO, and for each value test whether it exists as a key in the list root.cr_art.
03-18-2020 07:40 AM
Great! It works.
Where can I learn about other methods available for List objects in NSO? Is there any document or man page where I can find them?
Thanks,
Fran
03-18-2020 08:39 AM
Yes, there is documentation for the python APIs inside the documentation folder of NSO. If you go to the root folder for the documentation, you should then find the python API docs at the following path: doc/api/python/index.html
For the methods of the basic YANG object types (List, Leaf, Container, etc.), check out the ncs.maagic link from that index webpage.
03-18-2020 09:32 AM
03-19-2020 05:15 AM
@vleijon wrote:
As an alternative, you can run pydoc ncs.maagic.List at the commandline.
Thanks, this is very helpful!
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