Site icon Karneliuk

pygnmi 2. How to use pyGNMI?

Hello my friend,

In the previous article we have mentioned that we have started building our own Python package, which will allow you to easily interact with the network functions over gNMI. Today we want to share with you some progress and explain, how you can start benefiting from it right now.


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.

Is automation really so important?

Amount of network-related tasks in each company is growing years, if company is doing well. The number of employees is not. That creates a need for business relying on technologies to “do more with less”. The automation is a key enabler for this approach. And we are keen to help your business (or yourself) to find a right approach to network automation and successfully implement it. In our trainings:

Our network automation training has two faces: either live or self-paced. So you can choose yourself, what works better for you. On our side, we guide you from the foundation of the automation for the small networks till advanced automation use cases in big data centres, service providers, and clouds. You will lean how to structure the data using YANG modules, how to serialise it using JSON, XML, Protocol Buffers (Protobuf) depending on the application requirements and how to manage the fleet of your IT and network workloads relying gRPC, gNMI, NETCONF, REST API and many others with Bash, Ansible and Python. In addition to that you will master the solid foundation in Linux administration, Linux networking, network function virtualisation (NFV) with KVM and containerisation with Docker. That is all you need to be successful in network automation in one place.

Start your automation training today.

Brief description

The idea behind pyGNMI is simple. That is a Python package, which you can use in your code to interact with the network functions using the gNMI. There is a number of issue we would need to solve during the development, as we have stated previously:

So far we have started slowly with the development of the insecure gRPC channel with the Capabilities and Set operations. Per gNMI specification:

Using this two requests you can already start building a monitoring of your network functions using gNMI or, in general, start exploring how to work with it.

Usage

First thing first, you need to install the pyGNMI. You can do that from the Pypi or GitHub. We are trying to keep them in sync, so both resources should have the most actual stable version of the code. The installation is done using pip:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ pip install pygnmi
Collecting pygnmi
  Using cached https://files.pythonhosted.org/packages/a0/5e/00863e634772473e569025648ea5a2aa8cf78dd7d3a3c2e9fe4a1d91c351/pygnmi-0.1.8.tar.gz
Collecting grpcio (from pygnmi)
  Using cached https://files.pythonhosted.org/packages/48/16/d229a9d7d45bafecfe6b4c9ebf2053ec929efc2ffbed0b1a89ef190ef4d9/grpcio-1.32.0-cp37-cp37m-macosx_10_9_x86_64.whl
Collecting grpcio-tools (from pygnmi)
  Using cached https://files.pythonhosted.org/packages/06/c5/2687a0ef17b4444f2ea663572b982728eee93f629f64c6fe08fdae9368a9/grpcio_tools-1.32.0-cp37-cp37m-macosx_10_9_x86_64.whl
Collecting six>=1.5.2 (from grpcio->pygnmi)
  Using cached https://files.pythonhosted.org/packages/ee/ff/48bde5c0f013094d729fe4b0316ba2a24774b3ff1c52d924a8a4cb04078a/six-1.15.0-py2.py3-none-any.whl
Collecting protobuf<4.0dev,>=3.5.0.post1 (from grpcio-tools->pygnmi)
  Using cached https://files.pythonhosted.org/packages/9c/d2/10c325d657155ad7bd942f9d2652ae8e3e7a4be66723e9789099283733df/protobuf-3.13.0-cp37-cp37m-macosx_10_9_x86_64.whl
Requirement already satisfied: setuptools in ./venv/lib/python3.7/site-packages (from protobuf<4.0dev,>=3.5.0.post1->grpcio-tools->pygnmi) (41.2.0)
Installing collected packages: six, grpcio, protobuf, grpcio-tools, pygnmi
  Running setup.py install for pygnmi ... done
Successfully installed grpcio-1.32.0 grpcio-tools-1.32.0 protobuf-3.13.0 pygnmi-0.1.8 six-1.15.0

As gNMI is based on the gRPC transport, we build it our library on top of the existing gRPC.io. That library under the hood does the gRPC session handling for us.

The next step is to import the appropriate class gNMIclient from pygnmi.client, which handles the session. The class is implemented as a context manager, so it will establish the gRPC session and opening and will close it for you, once job is done:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ cat test.py
#!/usr/bin/env python

# Modules
from pygnmi.client import gNMIclient

# Variables
host = ('169.254.255.64', 57400)

# Body
if __name__ == '__main__':
    with gNMIclient(target=host, username='admin', password='admin', insecure=True) as gc:
         result = gc.get(path=['openconfig-interfaces:interfaces', 'openconfig-acl:acl'])
         
    print(result)

The parameters you need to provide towards the channel are the following:

KeyData typeMeaning
targettuplegNMI host, IP address is the first key and port is the second
usernamestringusername to connect to gNMI host
passwordstringpassword to connect to gNMI host
insecurebooleantype of session: with the SSL encryption (False) or without (True)
pathstringpath towards the certificate if insecure=False

Once the object is created, you can perform requests per gNMI specification, which are implemented as methods. So far we have implemented two requests:

requestarguments
capabilities()no arguments
get()path, which is a list of strings containing the gNMI paths to resources you want to pull

The result of each call is provided back as a Python dictionary, so you can use it directly a you need without any further conversion. The pygnmi is dealing with the complexity of the Protobuf for you. Here the result of the execution of the capabilities request against Nokia SR OS based network function:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ cat test.py
#!/usr/bin/env python

# Modules
from pygnmi.client import gNMIclient

# Variables
host = ('169.254.255.64', '57400')

# Body
if __name__ == '__main__':
    with gNMIclient(target=host, username='admin', password='admin', insecure=True) as gc:
        result = gc.capabilities()
         
    print(result)


$ python test.py
{'supported_models': [{'name': 'nokia-conf', 'organization': 'Nokia', 'version': '20.5.R1'}, {'name': 'nokia-state', 'organization': 'Nokia', 'version': '20.5.R1'}, {'name': 'nokia-li-state', 'organization': 'Nokia', 'version': '20.5.R1'}, {'name': 'nokia-li-conf', 'organization': 'Nokia', 'version': '20.5.R1'}, {'name': 'nokia-openconfig-if-ip-augments', 'organization': 'Nokia', 'version': '20.5.R1'}, {'name'...
!
! FURTHER OUTPUT IS OMITTED

Refer to the corresponding article to get info, how to enable gNMI on Nokia SR OS or Arista EOS router.

And the result of the get request for several paths:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ cat test.py
#!/usr/bin/env python

# Modules
from pygnmi.client import gNMIclient

# Variables
host = ('169.254.255.64', '57400')

# Body
if __name__ == '__main__':
    with gNMIclient(target=host, username='admin', password='admin', insecure=True) as gc:
         result = gc.get(path=['openconfig-interfaces:interfaces', 'openconfig-acl:acl'])
         
    print(result)


$ python test.py
{'notification': [{'timestamp': 1602618401033156685, 'update': [{'path': 'interfaces', 'json_val': {'interface': [{'name': 'system', 'config': {'name': 'system', 'type': 'softwareLoopback', 'enabled': True}, 'state': {'name': 'system', 'type': 'softwareLoopback'}, 'subinterfaces': {'subinterface': [{'index': 0, 'config': {'index': 0, 'enabled': True}, 'ipv4': {'addresses': {'address': [{'ip': '10.0.255.11', 'config': {'ip': '10.0.255.11', 'prefix-length': 32}}]}}}]}}, {'name': '1/1/c1/1', 'config': {'name': '1/1/c1/1', 'type': 'ethernetCsmacd', 'enabled': True}, 'state': {'name': '1/1/c1/1', 'type': 'ethernetCsmacd', 'mtu': 9212, 'description': '10-Gig Ethernet', 'enabled': True, 'ifindex': 1610899521, 'admin-status': 'UP', 'oper-status': 'UP', 'last-change': '16320000', 'counters': {'in-octets': '46', 'in-unicast-pkts': '0', 'in-broadcast-pkts': '1', 'in-multicast-pkts': '0', 'in-discards': '0', 'in-errors': '0', 'in-unknown-protos': '0', 'in-fcs-errors': '0', 'out-octets': '0', 'out-unicast-pkts': '0', 'out-broadcast-pkts': '0', 'out-multicast-pkts': '0', 'out-discards': '0', 'out-errors': '0', 'carrier-transitions': '0'}}, 'hold-time': {'state': {'up': 0, 'down': 0}}, 'subinterfaces': {'subinterface': [{'index': 0, 'config': {'index': 0, 'enabled': True}, 'ipv4': {'addresses': {'address': [{'ip': '10.0.0.0', 'config': {'ip': '10.0.0.0', 'prefix-length': 31}}]}}}]}, 'ethernet': {'state': {'mac-address': '52:54:00:64:00:01', 'auto-negotiate': False, 'hw-mac-address': '52:54:00:64:00:01', 'negotiated-duplex-mode': 'FULL', 'negotiated-port-speed': 'SPEED_10GB', 'counters': {'in-mac-pause-frames': '0', 'in-oversize-frames': '0', 'in-jabber-frames': '0', 'in-fragment-frames': '0', 'in-crc-errors': '0', 'out-mac-pause-frames': '0'}}}}]}}]}, {'timestamp': 1602618401033657507, 'update': [{'path': 'acl', 'json_val': {'state': {'counter-capability': 'AGGREGATE_ONLY'}}}]}]}

So the first step is done and we encourage you to start using us and give us feedback, what you like, what not and if you see any bugs.

GitHub repository

The latest updates of the pyGNMI, as well as other our products you can find in our GitHub.

Lessons learned

The process of the packaging of the Python modules for Pypi is not trivial. However, there are plenty of the good documents in the Web sharing the step-by-step approach. Personally we have used this one, so thanks author for creating that.

Conclusion

We have created two first calls and have had a lot of fun solving the packaging challenges. Perhaps, they are the most simple one, but we believe it is already a good starting point. Our plan is to cover all calls first and then to add the secure channel capabilities. Stay tuned. Take care and good bye.

Support us







P.S.

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

BR,

Anton Karneliuk

Exit mobile version