Table of Contents
ydk-gen is a developer tool that can generate API's that are modeled in YANG. Currently, it generates language binding for Python and C++ with planned support for other language bindings in the future.
Other tools and libraries are used to deliver ydk-gen
's functionality. In particular:
- YANG model analysis and code generation is implemented using APIs from the pyang library
- Documentation is generated using Sphinx
Of course, many other libraries are used as an integral part of ydk-gen and its dependencies, too many to mention!
The output of ydk-gen is either a core package, that defines services and providers, or a module bundle, consisting of APIs based on YANG models. Each module bundle is generated using a bundle profile and the ydk-gen tool. Developers can either use pre-packaged generated bundles (e.g. ydk-py), or they can define their own bundle, consisting of a set of YANG models, using a bundle profile (e.g. ietf_0_1_1.json
). This gives a developer the ability to customize the scope of their bundle based on their requirements.
Ubuntu (Debian-based): The following packages must be present in your system before installing YDK-Py/YDK-Cpp:
$ sudo apt-get install python-pip zlib1g-dev python-lxml libxml2-dev libxslt1-dev python-dev libssh-dev libcurl4-openssl-dev libtool-bin libpcre3-dev libpcre++-dev libtool pkg-config python3-dev python3-lxml cmake clang
Centos (Fedora-based): The following packages must be present in your system before installing YDK-Py/YDK-Cpp:
$ sudo yum install epel-release
$ sudo yum install python-pip python-devel libxml2-devel libxslt-devel libssh-devel libcurl-devel libtool clang cmake3 pcre-devel
$ sudo ln -fs /usr/bin/cmake3 /usr/bin/cmake
It is recommended to install homebrew and Xcode command line tools on your system before installing YDK-Py/YDK-Cpp:
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
$ xcode-select --install
$ brew install pkg-config cmake libssh xml2 curl pcre
You must install the following requirements::
We recommend that you run ydk-gen under a Python virtual environment (virtualenv
/virtualenvwrapper
). To install support in your system, execute
$ pip install virtualenv virtualenvwrapper
$ source /usr/local/bin/virtualenvwrapper.sh
In some systems (e.g. Debian-based Linux), you may need to install support for Python virtual environments as root
$ sudo pip install virtualenv virtualenvwrapper
$ source /usr/local/bin/virtualenvwrapper.sh
At this point, create a new virtual environment
$ mkvirtualenv -p python2.7 py2
$ git clone https://github.com/CiscoDevNet/ydk-gen.git
$ cd ydk-gen
$ pip install -r requirements.txt
$ ./generate.py --help
Usage: generate.py [options]
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-p, --python Generate Python bundle/core. This is currently the default option
-c, --cpp Generate C++ bundle/core
-o, --one-class-per-module
Generate separate modules for each python class corresponding to yang containers or lists.
--core Install the python/C++ core
--bundle=PROFILE Take options from a bundle profile file describing YANG
-v, --verbose Verbose mode
--generate-doc Generation documentation
--output-directory The output-directory . If not specified the output can be found under `ydk-gen/gen-api/python`
The below steps specify how to use ydk-gen
to generate the core and a bundle. Pre-generated bundles and core are available for python and C++: ydk-py and ydk-cpp.
The first step in using ydk-gen is either using one of the already existing bundle profiles or constructing your own bundle profile, consisting of the YANG models you are interested in:
Construct a bundle profile file, such as ietf_0_1_1.json
and specify its dependencies
A sample bundle profile file is described below. The file is in a JSON format. Specify the name
of your bundle, the version
of the bundle and the ydk_version
, which refers to the version of the ydk core package you want to use with this bundle. The name
of the bundle here is especially important as this will form part of the installation path of the bundle.
{
"name":"cisco-ios-xr",
"version": "0.1.0",
"ydk_version": "0.5.0",
"Author": "Cisco",
"Copyright": "Cisco",
"Description": "Cisco IOS-XR Native Models From Git",
The "models" section of the file describes where to source models from. There are 3 sources:
- Directories
- Specific files
- Git, within which specific relative directories and files may be referenced
The sample below shows the use of git sources only.
"models": {
"git": [
We have a list of git sources. Each source must specify a URL. This URL should be one that allows the repository to be cloned without requiring user intervention, so please use a public URL such as the example below. There are three further options that can be specified:
commitid
- Optional specification of a commit in string form. The files identified will be copied from the context of this commit.dir
- List of relative directory paths within git repository. All .yang files in this directory and any sub-directories will be pulled into the generated bundle.file
- List of relative file paths within the git repository.
Only directory examples are shown below.
{
"url": "https://github.com/YangModels/yang.git",
"dir": [
"vendor/cisco/xr/532"
]
},
{
"url": "https://github.com/YangModels/yang.git",
"commitid": "f6b4e2d59d4eedf31ae8b2fa3119468e4c38259c",
"dir": [
"experimental/openconfig/bgp",
"experimental/openconfig/policy"
]
}
]
},
First, generate the core and install it:
For python:
$ ./generate.py --python --core
$ pip install gen-api/python/ydk/dist/ydk*.tar.gz
For C++:
$ ./generate.py --cpp --core
$ cd gen-api/cpp/ydk/build && make && sudo make install
Then, generate your bundle using a bundle profile and install it:
For python:
$ ./generate.py --python --bundle profiles/<name-of-profile>.json
$ pip install gen-api/python/<name-of-bundle>-bundle/dist/ydk*.tar.gz
Now, doing pip list
should show the ydk
(refering to the core package) and ydk-<name-of-bundle>
packages installed:
$ pip list
...
ydk (0.5.2)
ydk-models-<name-of-bundle> (0.5.1)
...
For C++:
$ ./generate.py --cpp --bundle profiles/<name-of-profile>.json
$ cd gen-api/cpp/<name-of-bundle>-bundle/build && make && make install
Now, you can start creating apps based on the models in your bundle. Assuming you generated a python bundle, the models will be available for importing in your app under ydk.models.<name-of-your-bundle>
. For examples, see ydk-py-samples and C++ samples. Also refer to the documentation for python and for C++.
When generating the YDK documentation for several bundles and the core, it is recommended to generate the bundles without the --generate-doc
option. After generating all the bundles, the combined documentation for all the bundles and the core can be generated using the --core --generate-doc
option. For example, the below sequence of commands will generate the documentation for the three python bundles and the python core (for C++, use --cpp
instead of --python
).
Note that the below process could take a few hours due to the size of the cisco_ios_xr
bundle.
./generate.py --python --bundle profiles/bundles/ietf_0_1_1.json
./generate.py --python --bundle profiles/bundles/openconfig_0_1_1.json
./generate.py --python --bundle profiles/bundles/cisco_ios_xr_6_1_1.json
./generate.py --python --core --generate-doc
Pre-generated documentation for ydk-py and ydk-cpp are available.
- If your environment has both python 2 and python 3 and uses python 2 by default, you may need to use
python3
andpip3
instead ofpython
andpip
in the commands mentioned in this document.
README - install and usage notes
gen-api - generated bundle/core
- python (Python SDK)
- cpp (C++ SDK)
generate.py - script used to generate SDK for yang models
profiles - profile files used during generation
yang - some yang models used for testing
requirements.txt- python dependencies used during installation
sdk - sdk core and stubs for python and cpp
test - test code
Sometimes, developers using ydk-gen may run across errors when generating a YDK bundle using generate.py with some yang models. If there are issues with the .json profile file being used, such errors will be easily evident. Other times, when the problem is not so evident, it is recommended to try running with the --verbose|-v
flag, which may reveal syntax problems with the yang models being used. For example,
./generate.py --python --bundle profiles/bundles/ietf_0_1_1.json --verbose
Also, it may be a good idea to obtain a local copy of the yang models and compile them using pyang
to ensure the validity of the models,
cd /path/to/yang/models
pyang *.yang
First, generate and install the test bundle and core package
$ ./generate.py --core
$ pip install gen-api/python/core/dist/ydk*.tar.gz
$ ./generate.py --bundle profiles/test/ydktest.json
$ pip install gen-api/python/ydktest-bundle/dist/ydk*.tar.gz
To run the sanity tests, do the following:
$ cd ydk-gen/sdk/python
$ python test/test_sanity_types.py
$ python test/test_sanity_levels.py
$ python test/test_sanity_filters.py
First, install the test bundle and core package
$ ./generate.py --core --cpp
$ ./generate.py --bundle profiles/test/ydktest-cpp.json --cpp
$ cd gen-api/cpp/ydktest-bundle/build
$ sudo make install
$ cd -
To run the core and bundle tests, do the following
$ cd ydk-gen/sdk/cpp/ydk
$ mkdir build && cd build
$ cmake .. && sudo make all install test
$ cd ydk-gen/sdk/cpp/tests
$ mkdir build && cd build
$ cmake .. && make all test
Join the YDK community to connect with other users and with the makers of YDK.
The current YDK release version is 0.5.5 (alpha). YDK-Gen is licensed under the Apache 2.0 License.