Network Automation using YANG Models across XE, XR, & NX

Python Class Binding Generation

With our models downloaded, we can now generate real Python classes in a pip installable package created by ydk-gen.

Package Generation with Ad-hoc Bundles

This exercise demonstrates how one can take yang models downloaded directly from the device and pass them into ydk-gen so that ydk-gen can parse the yang model and translate it into a usable Python package.

To do this, we run ydk-gen's generate.py script, instructing ydk-gen to create a Python package called "openconfig-interfaces-lab", with the package contents being sourced from the yang files we supply as part of the adhoc-bundle definition. Later on we will explore building packages using bundle files as opposed to having to manually supply each element of the bundle.

Change directories into the ydk-gen repository you cloned in the prior module and run the following.

    
        cd /workspace/ydk-gen
        python3.6 generate.py --adhoc-bundle-name openconfig-interfaces-lab --adhoc-bundle ../openconfig-extensions.yang ../openconfig-interfaces.yang
    
This call syntax instructs YDK-Gen's generate.py script to create a new pip installable Python bundle named "openconfig-interfaces-lab", with the bundle containing Python classes automatically generated from the openconfig-interfaces.yang model. Note that we also provided the opeonconfig-extensions.yang model as well to since it contains elements that are required by openconfig-interfaces. When building ad-hoc bundles with YDK-Gen as we are now, one must specify the *.yang dependencies in the order that they are required, i.e., since openconfig-interfaces.yang required openconfig-extensions.yang, openconfig-extensions.yang must be specified earlier in the arguments list.

If everything was a success, the output should end with something similar to this:

        
            Successfully created source distribution at /workspace/ydk-gen/gen-api/python/openconfig_interfaces_lab-bundle/dist
            =================================================
            Successfully generated Python YDK at /workspace/ydk-gen/gen-api/python/openconfig_interfaces_lab-bundle
            Please refer to the README for information on how to install the package in your environment
            Code generation and installation completed successfully!
            Total time taken:  14 seconds
        
    

With this step completed, we now have a pip installable Python module containig Python classes based on the *.yang models that we specified. Progress to the next section to install your newly generated Python package.