Skip to content

Pyangbind

PyangBind is a pyang plugin.

About Pyang

Please visit this link if you need help about Pyang.

About PyangBind

It generates Python classes from a YANG module.

It converts YANG module into a Python module. This Python module can be then used to generate data which conforms with the data model defined in YANG.

Install Pyang and Pyangbind

pip install pyang
pip install pyangbind
pip3 freeze | grep pyang

Get YANG modules

We need YANG modules so we can use Pyang and Pyangbind.

Create a directory

mkdir yang_modules

Clone the OpenConfig repository

git clone https://github.com/openconfig/public.git

Run this command to verify

ls public

Copy all the YANG files from OpenConfig to the yang_modules directory

cp public/release/models/*.yang yang_modules/.
cp -R public/release/models/*/*.yang yang_modules/.
cp public/third_party/ietf/*.yang yang_modules/.

Move to the yang_modules directory

cd yang_modules/

Verify it has all the YANG files published on the OpenConfig repository

ls

Use Pyangbind to generate a Python module from a YANG module

pyang --plugindir $HOME/.local/lib/python3.6/site-packages/pyangbind/plugin/ -f pybind -o oc_bgp.py openconfig-bgp.yang

The above command generated the python module oc_bgp.py from the openconfig-bgp.yang file. Run this command to verify:

ls oc_bgp.py

Use the new python module to generate an OpenConfig configuration file

The file pyangbind_demo.py uses the new python module oc_bgp.py and generates this OpenConfig configuration file demo.json

python3 pyangbind_demo.py

Use gNMI SET RPC to configure a device

This OpenConfig configuration file demo.json can be loaded on a switch using the gNMI Set RPC

Install gNMIc

Please visit this link if you need help with gNMIc installation

Required device configuration

Please visit this link if you need help to configure EOS for gNMI

Use gNMIc to configure the swicth

Check the device configuration before

gnmic -a 192.0.2.117:6030 --insecure -u arista -p arista get   \
    --path '/network-instances/network-instance[name=default]/protocols/protocol[name=BGP]/bgp'
show run section bgp

Use gNMIc to configure the swicth

gnmic -a 192.0.2.117:6030 --insecure -u arista -p arista set  \
    --replace-path '/network-instances/network-instance[name=default]/protocols/protocol[name=BGP]/bgp' \
    --replace-file demo.json

Check the device configuration after

gnmic -a 192.0.2.117:6030 --insecure -u arista -p arista get  \
    --path '/network-instances/network-instance[name=default]/protocols/protocol[name=BGP]/bgp'
show run section bgp