Friday, 26 June 2020

Doing YANG Wrong: Part 2 - Python Bindings

Part 2: Python bindings for models

Having a model is one thing.  Using it requires you to ingest that model somewhere, apply values to its elements (leaves) as necessary/appropriate and then submit that completed model to the appliance for application to the config.

Im a python guy, you might like Go, or ruby or whatever. thats up to you, but I use python right now, which means I use pyangbind and pyang to create pythonic modules I can import into a script, and then interact with the model attributes like I would any other object in python. We can then push that out to a device from that script.

I will assume you have pyangbind working, if not, use the first few steps from here.

So, using the collection of model files we extracted for openconfig interfaces on our csr1000v, lets try to make a python module for interacting with this modelset.

pyang --plugindir $PYBINDPLUGIN -f pybind -o interface_setup.py *.yang

[email protected]:13: warning: imported module iana-if-type not used
[email protected]:186: warning: prefix "ianaift" is not defined
[email protected]:12: warning: imported module iana-if-type not used
[email protected]:346: warning: prefix "ianaift" is not defined
[email protected]:15: warning: imported module iana-if-type not used
[email protected]:370: warning: prefix "ianaift" is not defined
INFO: encountered (<pyang.error.Position object at 0x7f04005cc9d0>, 'UNUSED_IMPORT', u'iana-if-type')
INFO: encountered (<pyang.error.Position object at 0x7f0400519a90>, 'WPREFIX_NOT_DEFINED', u'ianaift')
FATAL: pyangbind cannot build module that pyang has found errors with.
Ugh. So we have an import somewhere in the model that isnt actually needed, and another one that isnt actually defined. End result: bad dog - no biscuit.

This one had me up against a wall for ages. I tried a pyang flag called --yang-remove-unused-imports but that didnt work either.

I then decided to look at these warnings properly. Turns out the iana-if-type module is imported with a prefix "ift", but later on these are refered to as "ianaift". Someone changed one part of the module, but not the other. In other words, the module on the CSR is technically broken.


I changed the line in the three affected openconfig modules to
import iana-if-type { prefix ianaift; }
and boom We have a python module.

Now that we have that basic module in place, we can build a script to deliver our first two requirements; an Interface and an IP.


No comments:

Post a Comment

My Fiber7-X VyOS Config

As I discussed in my 10/25Gbit internet at home post, I recently moved away from an appliance based router at home, to instead use a fancy(...