cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
814
Views
6
Helpful
3
Replies

Is there any API to load merge json format into NSO CDB?

Chu Liang
Cisco Employee
Cisco Employee

Hi dear,

I know there is a cmd tool ncs_load can load the json format payload into CDB, but I'm looking for the API (REST or python) option that can do the same.
AFAIK, the maagic API load_config doesn't the json format.

1 Accepted Solution

Accepted Solutions

Nabsch
Spotlight
Spotlight

Hello,

Here an example.

Command that i used to check before executing my script .

admin@ncs# show devices list | count
Count: 9 lines
admin@ncs# show running-config devices device TEST-LOAD-PYTHON
----------------------------------------------^
syntax error: element does not exist

The device TEST-LOAD-PYTHON wasn't present in CDB before.

 

import ncs
import _ncs

#admin@ncs# show running-config devices device TEST-IOS-XR-3.00 | de-select config | de-select ssh | display json
json_payload="""
{
  "data": {
    "tailf-ncs:devices": {
      "device": [
        {
          "name": "TEST-LOAD-PYTHON",
          "address": "127.0.0.1",
          "port": 10025,
          "authgroup": "default",
          "device-type": {
            "cli": {
              "ned-id": "cisco-iosxr-cli-3.0:cisco-iosxr-cli-3.0"
            }
          },
          "ned-settings": {
          },
          "commit-queue": {
          },
          "state": {
            "admin-state": "unlocked"
          },
          "notifications": {
            "received-notifications": {
            }
          }
        }
      ]
    }
  }
}
"""
with ncs.maapi.Maapi() as m:
    with ncs.maapi.Session(m, 'admin', 'system'):
        with m.start_write_trans() as trans:
            flags = (_ncs.maapi.CONFIG_MERGE + _ncs.maapi.CONFIG_JSON )
            #_ncs.maapi.load_config(m.msock, trans.th, flags, filename) ## Can be used using a file
            _ncs.maapi.load_config_cmds(m.msock, trans.th, flags, json_payload,path="/")
            trans.apply()
            print("Trans applied!")

 

Command that i use to check after executing the script

admin@ncs# show running-config devices device TEST-LOAD-PYTHON
devices device TEST-LOAD-PYTHON
 address   127.0.0.1
 port      10025
 authgroup default
 device-type cli ned-id cisco-iosxr-cli-3.0
 state admin-state unlocked
!
admin@ncs# show devices list | count
Count: 10 lines

 Here Link to doc that might help you 

View solution in original post

3 Replies 3

Nabsch
Spotlight
Spotlight

Hello,

Here an example.

Command that i used to check before executing my script .

admin@ncs# show devices list | count
Count: 9 lines
admin@ncs# show running-config devices device TEST-LOAD-PYTHON
----------------------------------------------^
syntax error: element does not exist

The device TEST-LOAD-PYTHON wasn't present in CDB before.

 

import ncs
import _ncs

#admin@ncs# show running-config devices device TEST-IOS-XR-3.00 | de-select config | de-select ssh | display json
json_payload="""
{
  "data": {
    "tailf-ncs:devices": {
      "device": [
        {
          "name": "TEST-LOAD-PYTHON",
          "address": "127.0.0.1",
          "port": 10025,
          "authgroup": "default",
          "device-type": {
            "cli": {
              "ned-id": "cisco-iosxr-cli-3.0:cisco-iosxr-cli-3.0"
            }
          },
          "ned-settings": {
          },
          "commit-queue": {
          },
          "state": {
            "admin-state": "unlocked"
          },
          "notifications": {
            "received-notifications": {
            }
          }
        }
      ]
    }
  }
}
"""
with ncs.maapi.Maapi() as m:
    with ncs.maapi.Session(m, 'admin', 'system'):
        with m.start_write_trans() as trans:
            flags = (_ncs.maapi.CONFIG_MERGE + _ncs.maapi.CONFIG_JSON )
            #_ncs.maapi.load_config(m.msock, trans.th, flags, filename) ## Can be used using a file
            _ncs.maapi.load_config_cmds(m.msock, trans.th, flags, json_payload,path="/")
            trans.apply()
            print("Trans applied!")

 

Command that i use to check after executing the script

admin@ncs# show running-config devices device TEST-LOAD-PYTHON
devices device TEST-LOAD-PYTHON
 address   127.0.0.1
 port      10025
 authgroup default
 device-type cli ned-id cisco-iosxr-cli-3.0
 state admin-state unlocked
!
admin@ncs# show devices list | count
Count: 10 lines

 Here Link to doc that might help you 

Thanks so much for your helpful example!
Actually, I didn't find "CONFIG_JSON" as the acceptable flag for "load_config” in the document, but indeed it works well! 

Then only way to check is to test . You can report that to the BU or open an SR for that.