叶节点 'when' 或 'must' 语句用法

Leaf Node 'when' or 'must' statement Usage

我正在学习 OpenDayLight 和 Yang,不知道如何对叶节点施加约束。我有一个叶节点 (vpn-id)。当 l3vpn-type 节点等于 'bgp' 时,我希望 vpn-id 允许此节点的数据。如果叶节点不等于 'bgp' 并且输入了 vpn-id 我想抛出一个错误。我已经在 OpenDayLight 中对此进行了测试,它始终允许我保存数据,无论其中包含什么数据。

另外,我很难找到杨的例子,所以我可以自学。欢迎提出建议。

module DaveTest {
 namespace "urn:aaa:ddd:DaveTest";
 prefix dave-module;

 description "Dave testing file";

 revision "2017-04-17" {
      description "Initial version.";
 }

 container testing-vars {

      list test-list {

           key "vpn-transaction-id l3vpn-type";
           unique "vpn-transaction-id";

           leaf vpn-transaction-id {
                type string;
           }

           leaf l3vpn-type {
                type enumeration {
                     enum "bgp";
                     enum "static";
                     enum "gre tunnel";
                }
                mandatory true;
           }

           leaf vpn-id {
                when "../l3vpn-type = 'bgp'";
                type string;
           }
      }              
 }

您对 when 语句的用法是正确的。您已将 vpn-id 设为条件,因此仅当 l3vpn-type 的值为 bgp 时才允许出现。可能由于 ODL 中的错误,它没有按预期工作。

如果你想了解 YANG,阅读 tag wiki would be a good start (there are links at the bottom). There are repositories of YANG modules made by various groups that can be found online, such as this one. Perhaps reading standard modules would be a better start, as they are peer reviewed and follow guidelines for YANG authors. They can be found in inside published RFCs,通常他们的名字包含 "YANG data model for" 短语。

您可以添加 must 声明如下:

leaf vpn-id {
    must "../l3vpn-type = 'bgp'";
    type string;
}