Python
Python Script in NSO
Use NSO Python modules and connect to the NSO datastore from your script, using transactions to read and write data.
Useful for onetime operations like importing device configurations or generating reports from the configurations.
Using the Python code in service packages:
Implement complex calculations and integrations with external systems in the code.
Populate and apply XML templates from Python.
Get the benefits of the FASTMAP algorithm.
Using Python API for Scripting
NSO Python library consist of
low-level APIs: direct mappin of C API
high-level APIs: abstraction of low-level APIs
Management Agent API (MAAPI))
a module that allows for transactional access of configuration and operational data
import ncs
with ncs.maapi.single_write_trans('admin', 'system') as t:
t.get_elem('/devices/device{internet-rtr0}/address')Some Example
Print names and addreses NSO is using to manage devices
Change hostname of device
Add device to NSO
Datastore Navigation (MAAGIC)
enable Python dot notation access to the datastore data
FASTMAP Service
ncs.application module is for developing Python-based NSO applications like services.
Using Python in Service Package
Use ncs-make-package command to create a new Python and template-based service:
ncs-make-package --service-skeleton python-and-template --component-class l2vpn.L2vpn l2vpn
Note: It is not mandatory to provide the --component-class parameter to the ncs-make-package command. Without it, the module name is main.py. It is good practice to provide a meaningful module name for easier searching through log entries that the Python code generates and also for easier navigation between files during development.
see l2vpn/package-meta-data.xml for more information
Below is the image explaining the mapping of service in python code

Example
Creating a Python and Template-based service
ncs-make-package --no-java --service-skeleton python-and-template --component-class loopback.Loopback --dest ${NCS_RUN_DIR}/packages/loopback loopback
edit yang file as following:
Building the package: make -C ${NCS_RUN_DIR}/packages/loopback/src/
Create Python Mapping Logic by editting ${NCS_DIR}/packages/loopback/python/loopback/loopback.py
cb_create function From:
To:
Create and apply a service template
Use NSO CLI to Create a Service Template
Edit the template file: ${NCS_RUN_DIR}/packages/loopback/templates/loopback-template.xml
Or can use below command to edit it inline:
Populate and Apply a Service Template from Python by editting ${NCS_RUN_DIR}/packages/loopback/python/loopback/loopback.py and add the following lines under the IP address calculation portion:
Or edittin the file inline as following:
The following image describe how service provisionin of Python and template services works

You commit a new service instance with loopback interface number, device, and IP prefix.
NSO invokes the Python create_cb() method, parameter IP prefix is available through the service parameter and calculation of usable IP address is done.
The template is populated—IP address from Python code and loopback ID and device directly from the YANG model.
Device configuration is created and pushed to the selected device using NED.
Troubleshooting a Python-based Service
Python Logs
There are two types of logs in NSO
ncs-python-vm.log: Each NSO package starts in its own Python VM. This log collects the output from these VMs. The log also collects errors in Python code that occur when the packages reload or redeploy and prevent registration.
ncs-python-vm-loopback.log: The package-specific log collects log messages that you usually write by using the logging module in the package (in our case service) code.
Connect to the NSO CLI and set the log level-debug so you can see the log message that Python code will write:
Since only the Python code was changed, it is enough to use the packages package loopback redeploy command to update the running code. Then provision a new service instance, which generates a new message in the log:
Open and study the Python log for the loopback service. Find the log in the ${NCS_RUN_DIR}/log/ncs-python-vm-loopback.log file. There should be a line Value of ip-prefix leaf is 192.168.20.0/24:
Python API
Flow of NSO Maagic Python API
Create a transaction
Common options:
single_read_trans
single_write_trans
Access device information
root.devices.device references all the devices in the NSO CDB. Even though the name says '.device' it is a YANG list, not one device.
To determine available attributes, can reference the NSO YANG model, the NSO GUI, or use the print dir() option in Python
Manipulate data
Apply configurations
Close the transaction
Manipulating Configuration
Need to know namespace. This can be found by exploring NSO CLI (| display xpath) or using print(dir()) statement
Enable CDP
Adding or Removin a Loopback
Examples of Python Script using NSO Python API
Check CDP
Check Interface
Reference
Last updated