Reputation: 4364
How to generate Java code from a yang-version 1.1 module with the OpenDaylight Yangtools maven plugin?
I have a yang-version 1.1 model (first part shown next)
module o-ran-sc-my-desc-v1 {
yang-version 1.1;
namespace "urn:o-ran:my-desc:1.0";
prefix rxad;
organization
"O-RAN Software Community";
contact
"www.o-ran.org";
I started with the YANG Tools guide https://wiki.opendaylight.org/view/YANG_Tools:User_Guide to construct a POM file and generate code. That has old versions tho and an invalid code generator class name. I upgraded to plugin version 4.0.1, code generator version 3.0.9, both are the very latest in Maven central, and figured out the name of the code generator class. Finally got something working in maven but now I get this code generator error:
[ERROR] Failed to execute goal org.opendaylight.yangtools:yang-maven-plugin:4.0.1:generate-sources (default) on project o1-netconf-client:
Execution default of goal org.opendaylight.yangtools:yang-maven-plugin:4.0.1:generate-sources failed: An API incompatibility was
encountered while executing org.opendaylight.yangtools:yang-maven-plugin:4.0.1:generate-sources: java.lang.NoSuchMethodError:
org.opendaylight.yangtools.yang.model.util.SchemaNodeUtils.getAllTypeDefinitions(Lorg/opendaylight/yangtools/yang/model/api/DataNodeContainer;)Ljava/util/Collection;
Relevant portion of POM posted below for completeness.
<plugin>
<groupId>org.opendaylight.yangtools</groupId>
<artifactId>yang-maven-plugin</artifactId>
<version>4.0.1</version>
<executions>
<execution>
<goals>
<goal>generate-sources</goal>
</goals>
<configuration>
<!-- directory containing yang files to parse and generate code -->
<yangFilesRootDir>my/agent/yang</yangFilesRootDir>
<codeGenerators>
<generator>
<codeGeneratorClass>
org.opendaylight.mdsal.binding.maven.api.gen.plugin.CodeGeneratorImpl
</codeGeneratorClass>
<!-- directory into which generated files will be placed -->
<outputBaseDir>
target/generated-sources/sal
</outputBaseDir>
</generator>
</codeGenerators>
<!-- if true, plugin will search for yang files also in dependent
projects -->
<inspectDependencies>true</inspectDependencies>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.opendaylight.mdsal</groupId>
<artifactId>maven-sal-api-gen-plugin</artifactId>
<version>3.0.9</version>
<type>jar</type>
</dependency>
</dependencies>
</plugin>
Is it possible that I am using incompatible versions?
Upvotes: 3
Views: 2593
Reputation: 16
The other answers are a bit outdated but did point me in the right direction. I'm using opendaylight version 14.0.4 and was able to use the yang-maven-plugin without depending on their parent pom.
<dependencies>
<dependency>
<groupId>org.opendaylight.yangtools</groupId>
<artifactId>binding-generator</artifactId>
<version>14.0.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.opendaylight.yangtools</groupId>
<artifactId>yang-maven-plugin</artifactId>
<version>14.0.4</version>
<dependencies>
<dependency>
<groupId>org.opendaylight.yangtools</groupId>
<artifactId>binding-codegen</artifactId>
<version>14.0.4</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>generate-sources</goal>
</goals>
<configuration>
<yangFilesRootDir>${project.basedir}/src/main/resources/openconfig
</yangFilesRootDir>
<inspectDependencies>true</inspectDependencies>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Upvotes: 0
Reputation: 151
I found adding the following to the POM without any other additions was sufficient to compile the YANGs.
<parent>
<groupId>org.opendaylight.mdsal</groupId>
<artifactId>binding-parent</artifactId>
<version>6.0.8</version>
<relativePath/>
</parent>
<groupId>your.group.id</groupId>
<artifactId>YourArtifactId</artifactId>
<version>1.0-SNAPSHOT</version>
You can also use the Open Network Operating System (ONOS) YANG compiler in a similar way.
As you can see from the below maven POM entry, you can configure the location of the YANG files to compile.
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onos-yang-compiler-maven-plugin</artifactId>
<version>2.4.4</version>
</dependency>
<plugin>
<groupId>org.onosproject</groupId>
<artifactId>onos-yang-maven-plugin</artifactId>
<version>1.11</version>
<executions>
<execution>
<configuration>
<yangFilesDir>src/main/yang</yangFilesDir>
</configuration>
<goals>
<goal>yang2java</goal>
</goals>
</execution>
</executions>
</plugin>
This will generate the interfaces and java classes into target/generated-sources which will be available as compiled classes for import from the jar file that the POM component creates.
You can read more about how to configure the ONOS compiler and what it does at: https://wiki.onosproject.org/display/ONOS/YANG+Compiler
For those interested in the distinction between OpenDaylight and ONOS, this is a very brief summary of the main differences: https://cloudsmartz.com/insights/onos-and-odl-know-the-difference/
Upvotes: 0
Reputation: 4364
Found a solution for generating Java binding classes from a yang-version 1.1 model using Open Daylight:
The working POM appears below, it's absurdly short, hope this saves the next person some frustration.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.opendaylight.mdsal</groupId>
<artifactId>binding-parent</artifactId>
<version>5.0.9</version>
<relativePath></relativePath>
</parent>
<groupId>org.your.group.id.goes.here</groupId>
<artifactId>o1-netconf-client</artifactId>
<packaging>jar</packaging>
<name>Descriptive Name Goes Here</name>
<version>0.0.1-SNAPSHOT</version>
</project>
When I run "mvn install" the executed steps include generate source, test, package as a jar etc. This is the critical one:
[INFO] --- yang-maven-plugin:4.0.6:generate-sources (binding) @ o1-netconf-client ---
[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.mdsal.binding.maven.api.gen.plugin.CodeGeneratorImpl
[INFO] yang-to-sources: Inspecting /Users/me/path/to/files/o1-netconf-client/src/main/yang
[INFO] yang-to-sources: Found 0 dependencies in 16.91 ms
[INFO] yang-to-sources: Project model files found: 2
[INFO] yang-to-sources: 2 YANG models processed in 174.2 ms
[INFO] yang-to-sources: Sources will be generated to /Users/me/path/to/files/o1-netconf-client/target/generated-sources/mdsal-binding
[INFO] Found 13 Binding types in 106.8 ms
[INFO] Generating 21 Binding source files into 8 directories
[INFO] yang-to-sources: Sources generated by org.opendaylight.mdsal.binding.maven.api.gen.plugin.CodeGeneratorImpl: 26 in 211.1 ms
If you want to roll your own, pick compatible versions from here: https://docs.opendaylight.org/projects/integration-distribution/en/latest/platform-versions.html
Upvotes: 1