Table of Contents

In this section we will use pygnmi, python gnmi client library, to send “set”, “delete” and “subscribe” commands to network devices.

The gnmi commands “capabilities” and “get” are discussed in the previous section.

pygnmi subscribe and set code examples

As we have discussed in the previous section, we use nonrir beside gnmi client python library to use inventory advantage of nornir library.

Nornir inventory files and config file are already discussed and prepared in the previous section and we will use them also for the demonstration of pygnmi set, delete and subscribe commands.

majid@ubuntu2204tls:~/devnet/pyhton_nornir/2023/14.gNMI$ cat config.yaml
---

inventory:
  plugin: SimpleInventory
  options:
    host_file: "hosts.yaml"
    group_file: "groups.yaml"
    defaults_file: "defaults.yaml"
majid@ubuntu2204tls:~/devnet/pyhton_nornir/2023/14.gNMI$ cat hosts.yaml
---

R1:
  hostname: "192.168.2.91"
  username: "rayka"
  password: "rayka-co.com"
  port: "6030"
  groups:
    - arista_group
majid@ubuntu2204tls:~/devnet/pyhton_nornir/2023/14.gNMI$ cat groups.yaml
---

arista_group:
  platform: eos
majid@ubuntu2204tls:~/devnet/pyhton_nornir/2023/14.gNMI$ cat defaults.yaml
---

username: "rayka"
password: "rayka-co.com"
platform: "eos"

pygnmi set command code example

This is the script to change the description of the interface.

from nornir import InitNornir
from pygnmi.client import gNMIclient
from rich import print as rprint

nr = InitNornir(config_file="config.yaml")

u = [
    (
        "openconfig:/interfaces/interface[name=Ethernet1]",
        {"config": {"name": "Ethernet1", "enabled": True, "description": "TestTEST"}},
    )
]


def set_with_pygnmi(task,config):
  with gNMIclient(
    target=(task.host.hostname, task.host.port),
    username=task.host.username,
    password=task.host.password,
    insecure=True) as client:

      result = client.set(update=u)
      rprint(result)

results = nr.run(task=set_with_pygnmi, config=u)

The Example and payload to change the description of the interface, is taken from arista networks github community. The only changes that I have made is to combine it with nornir capability.

In the header of the script, nornir and gNMIclient are imported.

A “client” object is created for each device from “gNMIclient” task and inventory information of each device.  

Then “set” command is used to change the configuration. The payload of the new configuration is uploaded through “update” parameter.

The payload itself is stored in “u” variable which changes the description of ethernet1 interface.

The function is then run through nornir “nr” object initialized in the beginning of the script.

Now let’s run the script to see the result.

majid@ubuntu2204tls:~/devnet/pyhton_nornir/2023/14.gNMI$ python3 14.5.gNMI_set_with_pygnmi.py
{'timestamp': 1685665170322657165, 'prefix': None, 'response': [{'path': 'interfaces/interface[name=Ethernet1]', 'op': 'UPDATE'}]}
R1(config-if-Et1)#do sh runn int eth1
interface Ethernet1
   description TestTEST
   no switchport
   ip address 10.10.10.1/24

pygnmi delete command code example

In the next script we will delete the description of the interface that we have already created.

from nornir import InitNornir
from pygnmi.client import gNMIclient
from rich import print as rprint

nr = InitNornir(config_file="config.yaml")

path1 = ["/interfaces/interface[name=Ethernet1]/config/description"]

def delete_with_pygnmi(task, path):
    with gNMIclient(
      target=(task.host.hostname, task.host.port),
      username=task.host.username,
      password=task.host.password,
      insecure=True) as client:

      response = client.set(delete=path)
      print(respone)

results = nr.run(task=delete_with_pygnmi, path=path1)

For deleting the configuration, we use still gnmi “set” command. The only difference is that we use “delete” parameter instead of “update” parameter.

The path to the description in the Yang data model is specified in the “delete” parameter.

The path to the description section in yang data model is configured inside “path1” variable.

Then the function to delete the description is executed through nornir object.

Let’s run the script to make sure that description will be deleted.

R1(config-if-Et1)#do sh runn int eth1
interface Ethernet1
   no switchport
   ip address 10.10.10.1/24

pygnmi subscribe command code example

And finally, as the last script in gnmi protocol, we use the most important gnmi command, “subscribe”.

from nornir import InitNornir
from pygnmi.client import gNMIclient, telemetryParser

nr = InitNornir(config_file="config.yaml")

subscribe1 = {
    'subscription': [
      {
        'path': 'interfaces/interface[name=Ethernet1]/state/counters/in-octets',
        'mode': 'sample',
        'sample_interval': 10000000000
      },
      {
        'path': '/interfaces/interface[name=Ethernet1]/state/oper-status',
        'mode': 'on_change',
      },
    ],
    'mode': 'stream',
    'encoding': 'json'
}

def subscribe_with_pygnmi(task):
  with gNMIclient(
    target=(task.host.hostname, task.host.port),
    username=task.host.username,
    password=task.host.password,
    insecure=True) as client:

    response = client.subscribe(subscribe=subscribe1)
    for response1 in response:
        print(telemetryParser(response1))

nr.run(task=subscribe_with_pygnmi)

With “subscribe” command, any user or application can subscribe itself to any data in yang data model. Then the data will be streamed to the subscriber intervalley or whenever changes.

Two examples are prepared for the subscribe command.  The mode as stream mode and encoding type as json is configured.

The first example is to subscribe to ethernet1 interface incoming statistics to be streamed every 10 seconds.

The second example stream interface etheret1 status whenever it changes.

Subscribe examples are run using “gNMIclient” client object, “subscribe” command and “subscribe” parameter.

Let’s run the script to see stream result.

To see interface status changes, we disable and enable the ethernet1 interface intentionally with shutdown and without shutdown command.

R1(config-if-Et1)#shutdown
R1(config-if-Et1)#no shutdown
majid@ubuntu2204tls:~/devnet/pyhton_nornir/2023/14.gNMI$ python3 14.7.gNMI_subscribe_with_pygnmi.py
{'update': {'update': [{'path': 'interfaces/interface[name=Ethernet1]/state/counters/in-octets', 'val': 28108}], 'timestamp': 1685665269191696247}}
{'update': {'update': [{'path': 'interfaces/interface[name=Ethernet1]/state/oper-status', 'val': 'UP'}], 'timestamp': 1685664984174413677}}
{'sync_response': True}
{'update': {'update': [{'path': 'interfaces/interface[name=Ethernet1]/state/oper-status', 'val': 'DOWN'}], 'timestamp': 1685665283029735566}}
{'update': {'update': [{'path': 'interfaces/interface[name=Ethernet1]/state/oper-status', 'val': 'UP'}], 'timestamp': 1685665284438159470}}
{'update': {'update': [{'path': 'interfaces/interface[name=Ethernet1]/state/counters/in-octets', 'val': 28364}], 'timestamp': 1685665287962128755}}
{'update': {'update': [{'path': 'interfaces/interface[name=Ethernet1]/state/counters/in-octets', 'val': 29146}], 'timestamp': 1685665295688685303}}
...
Back to: YANG based Network Automation using NETCONF RESTCONF gNMI > Network Automation using gRPC/gNMI

Leave a Reply

Your email address will not be published. Required fields are marked *


Post comment