cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1582
Views
5
Helpful
7
Replies

Python access to devices present in a service

franjorge
Level 1
Level 1

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

2 Accepted Solutions

Accepted Solutions

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.

View solution in original post

As an alternative, you can run pydoc ncs.maagic.List at the commandline.

View solution in original post

7 Replies 7

tcragg1
Cisco Employee
Cisco Employee

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)

 

 

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.

 

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.

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

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.

As an alternative, you can run pydoc ncs.maagic.List at the commandline.


@vleijon wrote:
As an alternative, you can run pydoc ncs.maagic.List at the commandline.

Thanks, this is very helpful!



 

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the NSO Developer community: