首页 > 解决方案 > 如何使用 yang-version 1.1 模块的 ODL Yangtools maven 插件生成代码

问题描述

如何使用 OpenDaylight Yangtools maven 插件从 yang-version 1.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 文件并生成代码。那有旧版本和无效的代码生成器类名。我升级到插件版本 4.0.1,代码生成器版本 3.0.9,两者都是 Maven 中心的最新版本,并弄清楚了代码生成器类的名称。终于在 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>

我是否有可能使用不兼容的版本?

标签: opendaylightietf-netmod-yang

解决方案


找到了使用 Open Daylight 从 yang-version 1.1 模型生成 Java 绑定类的解决方案:

  1. 将父 pom 设置为 Open Daylight 文件。父级指定兼容版本,定义代码生成器等。
  2. 将 yang 文件放在目录 src/main/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


推荐阅读