Yang to swagger generator
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Yang2Swagger generator

Project is a YANG to Swagger (OpenAPI Specification) generator tool. OpenAPI describes and documents RESTful APIs. The Swagger definition generated with our tool is meant to be compliant with RESTCONF specification. Having the definition you are able to build live documentation services, and generate client or server code using Swagger tools.

Our tool supports:

  • rpc - which are translated into POST operations
  • containers and lists - which are represented in RESTCONF data space URI and Swagger modules.
  • leafs and leaf lists - that are translated into Swagger models' attributes. Generator handles enums as well.
  • leafrefs - which are represented as model attributes with typesUsageTreeBuilder of the referred leafs
  • groupings - which, depending on strategy, are either unpacked into models that use these groupings or optimized model inheritance structures
  • augmentations - which, depending on strategy, are either unpacked into models that use these groupings or optimized model inheritance structures
  • YANG modules documentation - which is added to generated swagger API specification

In this project we use YANG parser from OpenDaylight (ODL) yang-tools project. The generated Swagger specification is available as Java object or serialized either to YAML or JSON file. The project contains a customized Jersey code-generator that can be use to generate server side scaffolding compatible with API specification.


How do I get set up?

Project is build with standard maven maven clean install. As project depends on ODL components settings.xml file configuration might be required as explained https://wiki.opendaylight.org/view/GettingStarted:Development_Environment_Setup#Edit_your_.7E.2F.m2.2Fsettings.xml

The main component of the project is SwaggerGenerator which can be run standalone as well as can be configured as maven plugin. Examples of usage can be found in examples directory in the project.

The generated yaml.swagger file might be used in swagger editor or standalone code generator. As mustache templates used in original jersey code generator apply HTML escaping to @Path parameters we have prepared our own version of the code generator. You might run it standalone or as maven plugin.

Command-line Execution

You can easily run SwaggerGenerator from the command-line:

java -jar ~/.m2/repository/com/mrv/yangtools/swagger-generator-cli/1.0-SNAPSHOT/swagger-generator-cli-1.0-SNAPSHOT-executable.jar
Argument "module ..." is required
 module ...          : List of YANG module names to generate in swagger output
 -output file        : File to generate, containing the output - defaults to stdout
                       (default: )
 -yang-dir path      : Directory to search for YANG modules - defaults to current
                       directory (default: )
 -api-version string : The current version of your API (default: 1.0)
 -format enum        : The output format (options: YAML, JSON) (default: YAML)
 -content-type string: Content type the API generates / consumes (default: application/yang-data+json)

For example:

java -jar ~/.m2/repository/com/mrv/yangtools/swagger-generator-cli/1.0-SNAPSHOT/swagger-generator-cli-1.0-SNAPSHOT-executable.jar \
 -yang-dir examples/build-standalone/src/main/resources \
 -output swagger.yaml \

Maven integration

You can generate yaml.swagger as part of resource generation step in your maven module. You can also choose the name by editing base-module and swagger-format additionalConfigs. To do so please add following plugin configuration to your project:




The swagger specification generator allows for parametrization:

  • -Dgenerator-mime=xml,json to specify mime formats supported in your system
  • -Dgenerator-elements=DATA,RPC to define which elements of yang modules should be consider during swagger definition generation

Please note that swagger-maven-plugin has to be available in your local maven repository.

You might also consider to plug-in code generator into your model definition:

                    <!-- specify the swagger yaml -->
                    <!-- target to generate -->
                    <!-- pass any necessary config options -->

Please note that in this case swagger-codegen-jaxrs has to be available in your local maven repository.