model - YANG 和 Choice - XML 是什么样子的?

标签 model schema ietf-netmod-yang yang

我正在尝试找出将 choice 字段从 yang 模型实现到其相应的配置 xml 中的正确语法。不幸的是,RFC 6020 和其他与 Yang 相关的网页中的文档似乎没有显示如何在与 Yang 模型对话的实际 XML 中使用 choice 字段。

例如,这是我的 YANG 模型:

container MYMODEL {
    container PacketOperationConf {
        list RuleID {
            key id;
            leaf id {
                type int32;
            }
            leaf priority { 
                type int32; 
            }
            leaf name { 
                type string; 
            }
            choice type {
                case flow {
                    list action {
                        key order;
                        uses mymodel:action;
                    }
                    container match {
                        uses mymodel:match;
                    }
                }
                case function {
                    container function {
                        
                    }
                }
            }
        }
        ...

并且这个容器有相应的 XML:

<?xml version="1.0" encoding="UTF-8" ?>
<rpc message-id="101"
    xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
    <edit-config>
        <target>
            <running />
        </target>
        <config>
            <MYMODEL xmlns="urn:com:tug:mymodel">
                <PacketOperationConf>
                    <RuleID>
                        <id>101</id>
                        <type>
                            <flow>
                                <action>
                                    <order>1</order>
                                    <set-dl-src-action>
                                        <address>01:02:03:04:05:06</address>
                                    </set-dl-src-action>
                                </action>
                            </flow>
                        </type>
                    </RuleID>
                </PacketOperationConf>
            </MYMODEL>
        </config>
    </edit-config>
</rpc>

但是当我通过 yang2dsdl 运行它时,我得到以下错误:

$ yang2dsdl -t edit-config -v rmbn-full-test-1.xml -d /tmp rmbn-full.yang
== Generating RELAX NG schema '/tmp/rmbn-full-edit-config.rng'
Done.

== Validating grammar and datatypes ...
rmbn-full-test-1.xml:13: element type: Relax-NG validity error : Element RuleID has extra content: type
Relax-NG validity error : Extra element RuleID in interleave
rmbn-full-test-1.xml:11: element RuleID: Relax-NG validity error : Element PacketOperationConf failed to validate content
rmbn-full-test-1.xml fails to validate

所以错误发生是因为它不知道如何处理 type 元素。 type 元素是我在 yang 模型中选择部分的名称。

我已经尝试过各种安排,但都没有奏效。我也无法在 SOF 或 Google 上找到任何有关以 XML 实现的选择示例的信息。

最佳答案

case 和 choice 都是 RFC 所指的模式节点,但它们不是数据节点——可以实例化的模式节点。因此,选择和案例节点永远不会出现在有效实例文档中,它们仅对有效实例文档是什么施加限制。

在您的情况下,这意味着一个可选的选择 (XOR):

  • 包含<action> 的(逻辑)元素组(列表)和/或 <match> (容器),
  • <function>容器。

换句话说,如果<action>出现在实例文档中,<match>也可能出现(反之亦然),但<function>可能不会出现。

这是一个有效的文件(没有实际测试)。

<?xml version="1.0" encoding="UTF-8" ?>
<rpc message-id="101"
    xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
    <edit-config>
        <target>
            <running />
        </target>
        <config>
            <RMBN xmlns="urn:com:tug:rmbn-full">
                <PacketOperationConf>
                    <RuleID>
                        <id>101</id>
                        <action>
                            <order>1</order>
                            <set-dl-src-action>
                                <address>01:02:03:04:05:06</address>
                            </set-dl-src-action>
                        </action>
                    </RuleID>
                </PacketOperationConf>
            </RMBN>
        </config>
    </edit-config>
</rpc>

以下是 RFC6020(YANG 1.0 版)中的一些文本,因为您专门询问了该版本 (Section 7.9)。

The "choice" statement defines a set of alternatives, only one of which may exist at any one time. The argument is an identifier, followed by a block of substatements that holds detailed choice information. The identifier is used to identify the choice node in the schema tree. A choice node does not exist in the data tree.

A choice consists of a number of branches, defined with the "case" substatement. Each branch contains a number of child nodes. The nodes from at most one of the choice's branches exist at the same time.

The "case" statement is used to define branches of the choice. It takes as an argument an identifier, followed by a block of substatements that holds detailed case information.

The identifier is used to identify the case node in the schema tree. A case node does not exist in the data tree.

Within a "case" statement, the "anyxml", "choice", "container", "leaf", "list", "leaf-list", and "uses" statements can be used to define child nodes to the case node. The identifiers of all these child nodes MUST be unique within all cases in a choice.

关于model - YANG 和 Choice - XML 是什么样子的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64272611/

相关文章:

Java Swing : Models and Button color changes

java - Spring v3 找不到元素 'mvc:resources' 的声明

ietf-netmod-yang - yang 中默认值的条件赋值

java - 在 java 中解析 YANG 模型

python - Django/MySQL 唯一约束如何将 NULL 视为相等

grails - 如何间接引用 grails GSP 模型变量,例如通过 .get(...)

django-rest-framework - 如何基于序列化器获取分页响应模式(drf-yasg)

JPA 在运行时动态使用多个数据库模式

opendaylight - 杨: How can I include a container from another module?

asp.net-mvc - 我们应该如何将数据传递到大型 ASP.NET MVC 网站中的 View