sFlow using YANG and Python - amybuck/SONiC-NAS GitHub Wiki
The Python example shows how to use a YANG model to configure a new sFlow entry. See SONiC Object Library Application Templates for information on how to write an application to configure SONiC NAS Host-Adapter. The example uses the SONiC Object Library Python utility cps_utils
to create objects and transactions.
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"
cps_get_oid
is a Python utility that executes a CPS 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'