如何使用OpenDaylight yang to插件从yang-version1.1模块生成Java代码?
我有一个yang-version 1.1模型(第一部分如下所示)
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";我从YANG工具开始,指导https://wiki.opendaylight.org/view/YANG_Tools:User_Guide构建一个POM文件并生成代码。它具有旧版本tho和无效的代码生成器类名。我升级到插件版本4.0.1,代码生成器版本3.0.9,这两个版本都是Maven central中的最新版本,并弄清楚了代码生成器类的名称。终于在maven中运行了一些东西,但是现在我得到了这个代码生成器错误:
[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;POM的相关部分在下面张贴,以确保完整性。
<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>有没有可能我正在使用不兼容的版本?
发布于 2020-02-14 23:59:33
找到了使用Open Daylight从yang版本1.1模型生成Java绑定类的解决方案:
/yang中。该目录的存在将激活#1中所需的配置文件。
下面显示了正在工作的POM,它短得离谱,希望这能为下一个人省去一些麻烦。
<?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>当我运行"mvn install“时,执行的步骤包括生成源代码、测试、打包为jar等。这是关键的一步:
[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如果你想使用你自己的版本,可以从这里选择兼容的版本:https://docs.opendaylight.org/projects/integration-distribution/en/latest/platform-versions.html
发布于 2021-06-12 15:55:30
我发现将以下内容添加到POM中而不添加任何其他内容就足以编译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>您也可以以类似的方式使用Open Network Operating System (ONOS) YANG编译器。
从下面的maven POM条目可以看到,您可以配置要编译的YANG文件的位置。
<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>这将把接口和java类生成到目标/生成的源中,它们将作为编译后的类可用,以便从POM组件创建的jar文件中导入。
有关如何配置ONOS编译器及其功能的更多信息,请访问:https://wiki.onosproject.org/display/ONOS/YANG+Compiler
对于那些对OpenDaylight和ONOS之间的区别感兴趣的人,这是对主要区别的一个非常简短的总结:https://cloudsmartz.com/insights/onos-and-odl-know-the-difference/
https://stackoverflow.com/questions/60214751
复制相似问题