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

YANG and Choice - what does the XML look like?

我正在尝试找出将 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 模型中选择的部分的名称。

我已经尝试了多种安排,但是 none 奏效了。我也无法在 SOF 或 Google 上找到任何有关 XML.

中实施的选择示例的信息

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

在你的例子中,这意味着一个可选的选择(异或):

  • 包含 <action>(列表)and/or <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.