cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
950
Views
0
Helpful
5
Replies

load-native-config action in NSO python

suvdeshm
Cisco Employee
Cisco Employee

I want to write code for calling the action load-native-config in python.

What is a sample code for doing that?

 

5 Replies 5

Nabsch
Spotlight
Spotlight

Hello,

 

Here an example

 

import ncs

device_name="NETSIM-XR-0"


config="""
hostname TEST-PYTHON

"""


print(f"Loading config {config} on device {device_name}")
with ncs.maapi.Maapi() as m:
        with ncs.maapi.Session(m, 'admin', 'python', groups=['ncsadmin']):
                with m.start_write_trans() as t:
                        root = ncs.maagic.get_root(t)
                        load_native_input = root.devices.device[device_name].load_native_config.get_input()
                        load_native_input.data = config
                        load_native_output = root.devices.device[device_name].load_native_config(load_native_input)

 

Thanks!

From the above solution, when I try to access: load_native_output.result , then I get the below error:-

'ActionParams' object has no attribute 'result'

Try load_native_output.info
See $NCS_DIR/src/ncs/yang/tailf-ncs-devices.yang under load-native-config for input and output parameters.

I am still getting an error parsing the response:--

 

load_native_input = root.devices.device[device_name].load_native_config.get_input() load_native_input.data = config load_native_input.dry_run.outformat = "native" load_output = root.devices.device[device_name].load_native_config(load_native_input) self.log.info("result") self.log.info(load_output.native.device[device_name].data) The last line parses the result and gives an error: <INFO> 19-Oct-2023::11:21:56.182 act-load-native ncs-dp-34481-act-load-native:main-1-usid-130-act-load-native: - action name: act-load-native <INFO> 19-Oct-2023::11:21:56.182 act-load-native ncs-dp-34481-act-load-native:main-1-usid-130-act-load-native: - config in fileinterface TenGigabitEthernet0/1/1 description SERV_005 <INFO> 19-Oct-2023::11:21:56.196 act-load-native ncs-dp-34481-act-load-native:main-1-usid-130-act-load-native: - device name isxe0 <INFO> 19-Oct-2023::11:21:56.547 act-load-native ncs-dp-34481-act-load-native:main-1-usid-130-act-load-native: - result <ERROR> 19-Oct-2023::11:21:56.548 act-load-native ncs-dp-34481-act-load-native:main-1-usid-130-act-load-native: - Error in ActLoadNativeConfigAction <ERROR> 19-Oct-2023::11:21:56.548 act-load-native ncs-dp-34481-act-load-native:main-1-usid-130-act-load-native: - '{xe0} not in /native/device' <ERROR> 19-Oct-2023::11:21:56.549 act-load-native ncs-dp-34481-act-load-native:main-1-usid-130-act-load-native: - Traceback (most recent call last): File "/Users/suvdeshm/run-5.6.41/state/packages-in-use/1/act-load-native/python/act_load_native/main.py", line 99, in cb_action self.log.info(load_output.native.device[device_name].data) File "/Users/suvdeshm/nso-5.6.4/src/ncs/pyapi/ncs/maagic.py", line 1102, in __getitem__ raise KeyError('%s not in %s' % (keystr, self._path)) KeyError: '{xe0} not in /native/device'

The load-native-config action does not commit the transaction when using MAAPI as it does with, for example, RESTCONF.
So, after loading the native config, if you want the output of a dry-run and then commit/apply the transaction, try something like:

 

...
self.log.info("result")
t = ncs.maagic.get_trans(root)
params = t.get_params()
params.dry_run_native()
result = t.apply_params(True, params)
self.log.info(result['local-node'])
t.apply_params(True, t.get_params())

 

See https://developer.cisco.com/docs/nso/api/#!ncs-maapi/ncs.maapi.Transaction.apply_params for details