【问题标题】:how to generate code with ODL Yangtools maven plugin for yang-version 1.1 module如何使用 yang-version 1.1 模块的 ODL Yangtools maven 插件生成代码
【发布时间】:2020-05-29 13:40:38
【问题描述】:

如何使用 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>

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

【问题讨论】:

    标签: opendaylight ietf-netmod-yang


    【解决方案1】:

    找到了一个使用 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

    【讨论】:

    • 谢谢你,这很有帮助。您知道如何使用生成的代码构建小型 Java netconf 客户端的示例吗?
    • @michael-h 我从未成功构建客户端。你可以试试lighty.io/components/#client。 IIRC netconf 似乎不支持简单的 REST 样式的调用-响应交互,相反,您的程序基本上变成了一个活动节点,可以进行调用 并且 将被其他节点调用。
    【解决方案2】:

    我发现将以下内容添加到 POM 中而不添加任何其他内容就足以编译 YANG。

    <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>
    

    您也可以以类似的方式使用开放网络操作系统 (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 类生成到 target/generated-sources 中,这些类将作为已编译的类从 POM 组件创建的 jar 文件中导入。

    您可以阅读更多关于如何配置 ONOS 编译器及其作用的信息:https://wiki.onosproject.org/display/ONOS/YANG+Compiler

    对于那些对 OpenDaylight 和 ONOS 之间的区别感兴趣的人,这是对主要区别的非常简短的总结:https://cloudsmartz.com/insights/onos-and-odl-know-the-difference/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-24
      • 2011-06-11
      • 2020-04-10
      • 2023-01-30
      • 2013-05-04
      • 1970-01-01
      • 2017-12-08
      • 1970-01-01
      相关资源
      最近更新 更多