Site icon Karneliuk

Automation 1. First impression from pySROS – new Python library for automation of Nokia SR OS devices

Hello my friend,

For quite a while we were silent (literally, the last post was month ago), as we were busy with extremely busy with completion of an important customer engagement. It was successfully completed and we are back on track with writing new interesting and useful (we hope so :-)) materials for you! And today we talk about our favourite topic – automation. To be precise, automation of Nokia SR OS devices with a new Python library called pySROS created by Nokia folks.


1
2
3
4
5
No part of this blogpost could be reproduced, stored in a
retrieval system, or transmitted in any form or by any
means, electronic, mechanical or photocopying, recording,
or otherwise, for commercial purposes without the
prior permission of the author.

Why Is That Useful for Me?

If you have ever been in a situation, when there are much more work than hours in a day, then you know how important is to be able to do the job quickly and accurately. It is even better, if such job is done not by yourself, but rather by someone else or something else. This is exactly, where the automation of network and any other component of IT infrastructure comes to the stage. The main purpose of the automation is to take the burden of the routine tasks from you, so that you can focus on something more meaningful, what tools cannot do yet: design and development of new customer services and solution as well as design of new automation workflows. And we are here to help you with that.

At our trainings, zero-to-hero network automation and automation with Nornir (2nd step after zero-to-hero network automation), we give you detailed knowledge of all the technologies relevant:

Expert level training on Network Automation is great. Lots of practice will take you beyond common DevNet topics and learning more about emerging technologies.

Michael Morgayle @ NextGenTek

Technologies themselves are good, but it is not good enough for us. Therefore, we put all of them in the context of the real use cases, which our team has solved and are solving in various projects in the service providers, enterprise and data centre networks and systems across the Europe and USA. That gives you opportunity to ask questions to understand the solutions in-depts and have discussions about your own projects. And on top of that, each technology is provided with online demos and you are doing the lab afterwards to master your skills. Such a mixture creates a unique learning environment, which all students value so much. Join us and unleash your potential.

Both trainings are available in live class and online formats. And more trainings coming soon!

Start your automation study today.

Brief Description

During the chat with my ex-colleagues and friends, James Cumming and Greg Hankins, they mentioned that since Nokia SR OS 21.7.R1 they have introduced a capability to run directly on the Nokia SR OS router the new Python library, they have created just a few months ago called pySROS. I never heard about this Python library before and, therefore, it made me curious to explore, what it is and what it can. It appeared that pySROS was published at the end of July 2021, what could explain why I have never heard about it before.

Learn practical Python for network automation at out training

So, what the pySROS is?

The quick look through the official documentation suggests the following key elements:

Out of this information, we can guess that this module operates via NETCONF, which is very positive news, as currently NETCONF is the most popular protocol for network automation as it is supported by the vast majority of the recent operating systems by vendors worldwide: besides Nokia, it is supported by Cisco in IOS XE / IOS XR / NX-OS, by Juniper in JUNOS, by Arista in EOS, by 6WIND in their Router OS and many others.

Join our Zero-to-Hero Network Automation Training to learn NETCONF in-depth on the message level and learn how to automate Cisco, Nokia and Arista with it using Ansible and Python.

Another guess could be that pySROS operates Nokia Native YANG modules. The good thing is that it is not bundled to Nokia native YANG modules only and can be used with all the modules active on the Nokia SR OS network devices. This fact makes it possible to use pySROS for automation of your Nokia router with OpenConfig YANG modules and NETCONF.

That all sounds appealing and we would like to give it a try. At the end of the day, as Linus Towards says “Talk is cheap. Show me the code”. Today we take a look how to run the pySROS outside the Nokia router and in the following articles we’ll show how to do that from the device itself and what are the limitations.

Lab Setup

The setup for lab is relatively simple as you could see from the following diagram:

On the left hand side you could see our host, where we will create and run the Python script relying pySROS. In our lab setup this host runs Debian Linux 11 with Python 3.9. On the right hand side you can see Nokia SR OS router, which per requirements run SR OS 21.7.R1.

Scenario Description

Today we’ll just scratch the surface of pySROS. To be precise, we will:

In subsequent blogposts we’ll cover the further details, which you may need to develop your network automation solution with pySROS.

If you want experts to build and operate automation for your network, we are happy to do that for you. Leave a request and we will get in touch with you.

Python Script Development

Step #1. Create a Python Virtual Environment and Install pySROS

Create a directory, where you will be storing script details and create the virtual environment for your Python interpreter:


1
2
3
# mkdir nokia
# cd nokia
/nokia# python3 -m venv venv

It will take a few moments and depends on the resources your host have. Once done, active the created Python’s virtual environment and make sure you have a desired version:


1
2
3
/nokia# source venv/bin/activate
(venv) /nokia# python -V
Python 3.9.2

The environment is all set and you can proceed with installation of pysros itself:


1
(venv) /nokia# pip install pysros

This process is relatively quick. The pysros will install itself and its dependencies. Quick look through the installed dependencies shows that pysros relies on ncclient for NETCONF session handling.

That could explain the introduction of new device type sros in the newest version of ncclient published in May 2021.

Step #2. Develop a Python Script with pySROS

Now, when the environment is set, you can get started with automation of Nokia SR OS. Here is the sample script we will use for this lab:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python

# Modules
import os
from dotenv import load_dotenv
from pysros.management import connect


# Body
if __name__ == "__main__":
    ## Get connectivity details
    load_dotenv()
    host={
            "ip_address": os.getenv("NOKIA_IP"),
            "username": os.getenv("NOKIA_USER"),
            "password": os.getenv("NOKIA_PASS")
         }

    ## Interecting with the device
    connect_obj = connect(host=host["ip_address"], username=host["username"],
                          password=host["password"])

    results = connect_obj.running.get("/nokia-conf:configure/card[slot-number=1]")
    connect_obj.disconnect()

    ## Printing results
    tl = os.get_terminal_size()
    print(f"{'=' * tl.columns}\n")

    ## Printing collected data
    print(results)

    print(f"\n{'=' * tl.columns}\n")

Let’s go step by step, what is happening here:

  1. First of all, from the file management of module pysros we import connect method, which is used to establish the NETCONF session with Nokia SR OS device. Besides that, you may need os to make the output a little bit better as well as dotenv module to import credentials from Linux environment (read how to do that).
  2. Then using load_env() you can import the Linux environment in the Python provided you stored them in the file .env inside the same directory.
  3. Using getenv() function from os module, you can actually used the Linux environment variables. We suggest to create a simple dictionary to store them.
  4. Finally, using the connect() method you create an object, which is handling the NETCONF session towards the device. Using this object you can communicate with the device by pulling the data from or pushing a new configuration to the device.
  5. Apply method running.get() to the created object which means the NETCONF get operation applied to the running datastore. The argument is a path following the corresponding YANG module in XPath format, which is also widely used in the gNMI. Save the result in a variable.
  6. Once you collected the information you need, you can terminate the session using disconnect() method.
  7. Finally, it is time to print the results. In order to distinguish one script execution from another one, we suggest to use a separator, which will be a line of “=” symbols for the whole width of your terminal window. Here the function get_terminal_size() from os module is useful.
  8. Print the collected results.

Your first script with pySROS is ready! Let’s test, how it works.

Step #3. Run Your pySROS Script

It is as simple as this:


1
2
3
4
5
6
# python pysros_get.py
==========================================================================================================

{'admin-state': Leaf('enable'), 'card-type': Leaf('iom-1'), 'level': Leaf('he'), 'mda': {1: Container({'admin-state': Leaf('enable'), 'mda-type': Leaf('me6-100gb-qsfp28'), 'mda-slot': Leaf(1)})}, 'slot-number': Leaf(1)}

==========================================================================================================

The output is actually quite interesting. The pySROS doesn’t bring new transport and relies on ncclient. However, what it does bring is the proper YANG type identification. In other words, you can see whether certain element is a Leaf, List-Leaf, Container or List. This is a useful information, when you write an application for NETCONF-based device management.

Join our Network Automation Training to learn about all YANG data types, their meanings and applications.

Looks good and we have collected some pieces of configuration from our network device. Using the same method you can collect also operational information, just change the path to point to the corresponding YANG module and resource:


1
2
3
4
5
! OUTPUT IS TRUNCATED FOR BREVITY
!
    results = connect_obj.running.get("/nokia-conf:configure/card[slot-number=1]")
!
! OUTPUT IS TRUNCATED FOR BREVITY

Literally, just change a single line and re-run the code:


1
2
3
4
5
6
7
8
9
# python pysros_get.py
==========================================================================================================

{'equipped-type': Leaf('iom-1'), 'clock-source': Leaf('none'), 'available-mda-slots': Leaf(2), 'installed-mda-slots': Leaf(1), 'memory-size': Leaf(4096), 'last-bootup-reason': Leaf('hard-reboot'), 'equipped-level': Leaf('he'), 'licensed-level': Leaf('he'), 'hardware-data': Container({'part-number': Leaf('7d3b977e6932433a8e2e1959553a84a2'), 'serial-number': Leaf('card-1'), 'manufacturing-date': Leaf('01012003'), 'common-language-equipment-identifier': Leaf('Sim CLEI'), 'field-replaceable-unit': Leaf(True), 'contained-in': Leaf('Chassis 1'), 'oper-state': Leaf('in-service'), 'failure-reason': Leaf(''), 'contains-temperature-sensor': Leaf(False), 'temperature-threshold': Leaf(-1), 'alarm-state': Leaf('alarm-cleared'), 'manufacturing-deviations': Leaf('Sim MfgDeviation card-1'), 'equipped-platform-type': Leaf('platform-7750'), 'manufacturing-assembly-number': Leaf('01-2345-67'), 'power-zone-location': Leaf(1), 'base-mac-address': Leaf('00:00:01:00:00:00'), 'boot-code-version': Leaf('simulated'), 'software-code-version': Leaf('TiMOS-B-21.7.R1 both/x86_64 Nokia 7750 SR Copyright (c) 2000-2021 Nokia.\nAll rights reserved. All use subject to applicable license agreements.\nBuilt on Thu Jul 29 14:54:01 PDT 2021 by builder in /builds/c/217B/R1/panos/main/sros\n'), 'software-last-boot-time': Leaf('2021-10-04T17:50:27.0Z')
!
! OUTPUT IS TRUNCATED FOR BREVITY
!

==========================================================================================================

Step #4. Use Collected Data

One the thoughts you may have by now (at least, it was the first thought in my head, when I saw the output): cool, but is that a custom data classes? How easily can I use it?

Apparently, the answer is that it is relatively easy to use the data. Switching back to original configuration case, let’s print content of admin-state leaf:


1
2
3
4
5
6
7
! OUTPUT IS TRUNCATED FOR BREVITY
!
    ## Printing collected data
    print(results)
    print(results["admin-state"])
!
! OUTPUT IS TRUNCATED FOR BREVITY

So we add one new line. As you see, it is a standard way how you would usually call a key from dictionary, right? Let’s see, what we have in the output now:


1
2
3
4
5
6
7
# python pysros_get.py
==========================================================================================================

{'admin-state': Leaf('enable'), 'card-type': Leaf('iom-1'), 'level': Leaf('he'), 'mda': {1: Container({'admin-state': Leaf('enable'), 'mda-type': Leaf('me6-100gb-qsfp28'), 'mda-slot': Leaf(1)})}, 'slot-number': Leaf(1)}
enable

==========================================================================================================

You see that you can easily access the value. However, after a few tests, it appeared, that despite it looks like it is a string it is not a string, but you can convert it to a sting using str() function, what suggests me that it is a class. To prove that, let’s add a few more lines to our example:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
! OUTPUT IS TRUNCATED FOR BREVITY
!
    ## Printing collected data
    print(results)
    print(results["admin-state"])

    ## Validating data type
    print(isinstance(results["admin-state"], str))
    print(results["admin-state"] == "enable")

    # Converting data type and re-validating it again
    print(isinstance(str(results["admin-state"]), str))
    print(str(results["admin-state"]) == "enable")
!
! OUTPUT IS TRUNCATED FOR BREVITY

Here is what we are doing:

  1. In the part “Validating data type“, you could see a validation of the particular key from the result with the isinstance() function. Also, we are trying to print a result of basic comparison statement, which compares the result against string “enable”.
  2. In the part “Converting data type and re-validating it again“, you could see us converting the result using str() function and then performing the same validations again.

Let’s see the result of our Python script with pySROS now:


1
2
3
4
5
6
7
8
9
10
11
# python pysros_get.py
==========================================================================================================

{'admin-state': Leaf('enable'), 'card-type': Leaf('iom-1'), 'level': Leaf('he'), 'mda': {1: Container({'admin-state': Leaf('enable'), 'mda-type': Leaf('me6-100gb-qsfp28'), 'mda-slot': Leaf(1)})}, 'slot-number': Leaf(1)}
enable
False
False
True
True

==========================================================================================================

You could see that in first two cases, the outputs are False, meaning results[“admin-state”] is not a str. However, the second last outputs are True, what means that value is successfully converted to string.

Examples in GitHub

You can find this and other examples in our GitHub repository.

Lessons Learned

The first run of the script is extremely long. It can be longer that as long as 2+ minutes and requires a substantial amount of resources (2c CPU + 2 GB RAM) at least on your host. So if you develop your automation in a VM or in a container or planning to containerise that in future, this is something you should be aware of. The reason for that per Nokia pySROS documentation is that during the first run the library downloads all the YANG modules and prepackage it in some form of binary file stored in a hidden directory .pysros in your home:


1
2
# ls ~/.pysros/cache/
'model_VZRWQELGMJOS4WR2XTZJCT7IJVQJ5NAG5OOEJRRYRVGB4I2ZZTDQ====.ver'   temp_r_0ydpxj

Conclusion

It was generally very interesting to see a new initiative from Nokia SR OS, which seems now embracing open source more and more. This library can definitely be helpful, if you need to automate Nokia network. In next articles you will see few other scenarios, covering configuration of the device and running the code directly from Nokia SR OS routers. Take care and good bye.

Support us





P.S.

If you have further questions or you need help with your networks, we are happy to assist you, just send us a message. Also don’t forget to share the article on your social media, if you like it.

BR,

Anton Karneliuk

Exit mobile version