Skip to content

sFlow using YANG and Python

Amy Buck edited this page Nov 27, 2018 · 2 revisions

This Python example shows how to use a YANG model to configure a new sFlow entry. The Python utility cps_utils is used to create CPS objects and CPS transactions. See CPS application templates for information about how to write an application using the CPS API.

import cps_utils
import nas_os_utils


# Create a CPS object for the YANG container named 'entry'
cps_obj = cps_utils.CPSObject(module='base-sflow/entry')

# Add each property in the YANG container as an attribute to the CPS Object
cps_obj.add_attr ("ifindex", nas_os_utils.if_nametoindex('e101-003-0'))
cps_obj.add_attr ("direction", 1)
cps_obj.add_attr ("sampling-rate", 5000)

# Pair the CPS object with a CPS Operation - in this case it is a Create operation.
cps_update = ('create',cps_obj.get())

# Add the pair to the list of updates in a CPS transaction
cps_trans = cps_utils.CPSTransaction ([cps_update])

# Commit the transaction
r = cps_trans.commit()

if not r:
    print "Error"
else:
    print "Success"

The cps_get_oid is a Python utility that executes a GET API on a YANG container. The result displays the values configured in the software for all attributes in the YANG container.

$ cps_get_oid.py 'base-sflow/entry'    Key: 1.27.1769478.1769474.  base-sflow/entry/ifindex = 16  base-sflow/entry/direction = 1  base-sflow/entry/sampling-rate = 5000  base-sflow/entry/id = 1
Clone this wiki locally