cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Announcements

405
Views
0
Helpful
11
Replies
khgrant
Cisco Employee

Adding/Removing devices using python APIs

 

Hello team,

I need to provision and remove devices on NSO using a provisioning script written in python.

I need to achieve the same as with this CLI:

 

root@ncs# config
root@ncs(config)# devices device iosv-1
root@ncs(config-device-iosv-1)# address 172.16.203.102
root@ncs(config-device-iosv-1)# authgroup root
root@ncs(config-device-iosv-1)# state admin-state unlocked
root@ncs(config-device-iosv-1)# device-type cli ned-id cisco-ios protocol ssh
root@ncs(config-device-iosv-1)# commit

 

Could you point me to the API documentation I should use for this and to the relevant calls I should look into? I have tried to find my wait through the official documentation that comes with NSO 4.0 but haven been able to sort this out yet.

Thanks for your help,

Cheers,
Francesco.

 

1 ACCEPTED SOLUTION

Accepted Solutions
khgrant
Cisco Employee

 

Hi,

 

Please checkout the attached script that will create a device using the JSON RPC API.

 

Fredrik

 

View solution in original post

11 REPLIES 11
khgrant
Cisco Employee

 

Hi,

 

Please checkout the attached script that will create a device using the JSON RPC API.

 

Fredrik

 

khgrant
Cisco Employee

Hi Fredrik,

thanks a lot for your prompt response. I guess this is exactly what I was looking for

I will give it a try later on this afternoon.

Thanks,
Francesco.

khgrant
Cisco Employee

 

Here another option using the Python API. This is in DevNet already.

 

 

I load the devices names from a csv file (ip,name,device_types,ned) and delete team based on name using a single transaction.

 

 

Regards,

 

Roque

 

 

 

'''

 

Created on Dec 9, 2014

 

 

@author: rogaglia

 

'''

 

import csv

 

import ncs

 

 

 

 

def high_level_delete(infilename) :

 

 

    # Resources cleanup, i.e. closing cursor, finishing transaction,

 

    # end user session and closing the maapi socket is done when respective

 

    # with-block is exited

 

   

 

    with ncs.maapi.wctx.connect(ip = '127.0.0.1', port = ncs._constants.NCS_PORT) as c :

 

        with ncs.maapi.wctx.session(c, 'admin') as s :

 

            with ncs.maapi.wctx.trans(s, readWrite = ncs._constants.READ_WRITE) as t :

 

                with open(infilename,'r') as fo:

 

                    for row in csv.reader(fo.read().splitlines()):

 

                        ip,name,device_types,ned = row

 

                        print "Deleting Device: %s" % name

 

                        path = '/ncs:devices/device{"%s"}' % name

 

                        # I check if device exists

 

                        if t.exists(path) :

 

                        # If it exists, I delete it

 

                            t.delete(path)

 

                # I commit all the deletions in one transaction                   

 

                t.apply()

 

                       

 

if __name__ == '__main__' :

 

    infilename = "lab_equipment_list.csv"

 

    high_level_delete(in filename)

 

 

 

 

Roque Gagliano

 

khgrant
Cisco Employee

 

Considering this could be done using

 

    • JSON API (as Fredrik showed)

    • Python API (as Roque showed)

    • NETCONF

    • Other?

 

What are the relative merits of each?

 

 

Cheers,

 

  1. KJ.

 

khgrant
Cisco Employee

Hi Roque,

thanks a lot, this makes it even easier, I will explore that API as well.

I have one query for Fredrik on the JsonRPC: what would be the json-rpc command to delete the device?

Thanks for your help!

Cheers,
Francesco.

khgrant
Cisco Employee

Hi Roque,

sorry I forgot to ask you: where in devnet can I find those scripts? Could you point me to the link?

Thanks,
Francesco.

khgrant
Cisco Employee

 

KJ,

 

 

I'm replying here despite a major risk of spurring endless debate ;-)

 

 

Considering this could be done using

 

    • JSON API (as Fredrik showed)

    • Python API (as Roque showed)

    • NETCONF

  All these three have similar power, so in many ways it would be a matter of taste and your experience. NETCONF stands out from the rest being an IETF standard. It is also by far the most stable/most well tested of these interfaces. The JSON and Python APIs are Tail-f proprietary. The Python API is probably the easiest/most direct one of the three, assuming you know a bit of Python. Both JSON and NETCONF would be more or less language independent. 

    • Other?

 

Another one often mentioned in these lists is RESTCONF. It will likely soon also become an IETF standard. It has less power than, and in some cases will be a bit slower than the three interfaces above. This is by design. IETF focused on making something "simple and easy to use". It can certainly be debated if they succeeded in that, I see quite a few support requests on RESTCONF. Anyway, the fact is that many of the more advanced NETCONF features have been removed. Since the RESTCONF interface isn't quite standardized yet, but has been implemented in a number of products (e.g. NSO), there will be some changes to the implementations once the standard freezes, so be aware that you'll have to update your code as the protocol implementation changes.

 

 

Java is certainly not all dead yet ;-) It's also has the full expressive power. It's more stable than the Python API today, because it's been out there several times longer.

 

 

Best Regards,

 

/jan

 

khgrant
Cisco Employee

 

KJ,

 

 

I'm replying here despite a major risk of spurring endless debate ;-)

 

 

Considering this could be done using

 

    • JSON API (as Fredrik showed)

    • Python API (as Roque showed)

    • NETCONF

  All these three have similar power, so in many ways it would be a matter of taste and your experience. NETCONF stands out from the rest being an IETF standard. It is also by far the most stable/most well tested of these interfaces. The JSON and Python APIs are Tail-f proprietary. The Python API is probably the easiest/most direct one of the three, assuming you know a bit of Python. Both JSON and NETCONF would be more or less language independent. 

    • Other?

 

Another one often mentioned in these lists is RESTCONF. It will likely soon also become an IETF standard. It has less power than, and in some cases will be a bit slower than the three interfaces above. This is by design. IETF focused on making something "simple and easy to use". It can certainly be debated if they succeeded in that, I see quite a few support requests on RESTCONF. Anyway, the fact is that many of the more advanced NETCONF features have been removed. Since the RESTCONF interface isn't quite standardized yet, but has been implemented in a number of products (e.g. NSO), there will be some changes to the implementations once the standard freezes, so be aware that you'll have to update your code as the protocol implementation changes.

 

 

Java is certainly not all dead yet ;-) It's also has the full expressive power. It's more stable than the Python API today, because it's been out there several times longer.

 

 

Best Regards,

 

/jan

 

khgrant
Cisco Employee

 

Thanks. Would there also be a difference w.r.t. Where the client code resides? I’m thinking that JSON, NETCONF and REST(CONF) all are RPCs on a wire protocol, which allows the client to reside anywhere. The Python API (and Java API) I guess means that the client lives on hte NSO server?

 

 

Cheers,

 

KJ.

 

khgrant
Cisco Employee

 

KJ,

 

 

Thanks. Would there also be a difference w.r.t. Where the client code resides? I’m thinking that JSON, NETCONF and REST(CONF) all are RPCs on a wire protocol, which allows the client to reside anywhere. The Python API (and Java API) I guess means that the client lives on hte NSO server?

 

 

If connectivity is all you care about, all of these would work remotely.

 

 

There is a major difference in how secure the transports are, however. NETCONF and RESTCONF are secure, transported over SSH or TLS. JSON RPC can go over HTTP or HTTPS, so it depends.

 

 

The direct Python and Java APIs run over TCP, so insecure. For any use of an insecure protocol, you could of course let the traffic go through for example an SSH tunnel, which removes the problem. So essentially, it doesn't matter, as long as you know what you're doing and are ok with tunnels.

 

 

Best Regards,

 

/jan

 

khgrant
Cisco Employee

Hi Jan,

thanks for all the hints.

@Fredrik I managed to find my way through the json-rpc, I understood the logic from the documentation, so I was able to delete the device, plus other operations.

I have one last question on the python maapi. If I want to import the ncs python package on a client machine without having to install the whole ncs, is there a wheel or a ncs client package I can install?

Thanks for your help!

Cheers,
Francesco.

Create
Recognize Your Peers
Content for Community-Ad