简体   繁体   English

YANG - 建模非强制性容器

[英]YANG - Modeling non-mandatory containers

Currently I am working with YANG as part of a (legacy) Python project. 目前我正在与YANG合作,作为(遗留)Python项目的一部分。

I am somewhat stuck at the task of defining a schema, which shall then be used to verify data, organized as a Python dictionary. 我有点坚持定义一个模式的任务,然后用它来验证数据,组织为一个Python字典。 If it is possible, I would "like" to keep the current structure, since a lot of the codebase is using this data. 如果有可能,我会“喜欢”保留当前结构,因为很多代码库都在使用这些数据。

An "unaltered" piece of data: 一个“未改变的”数据:

"namespace": {                      # Mandatory
    "management": {                 # Optional
        "interfaces": {             # Mandatory
            "m0": {                 # Optional
                "leaf1": "..."
            }
        }
    },
    "benchmark": {                  # Optional
        "interfaces": {             # Mandatory
            "b0": {                 # Optional
                "leaf1": "...",
                "leaf2": "..."
            },
            "b1": {                 # Optional
                "leaf1": "...",
                "leaf2": "..."
            }
        }
    }
}

My problem is that everything marked as "optional" (in the example) would be modeled as a container but it seems that they cannot be defined as optional (ie: mandatory false;) according to RFC6020 . 我的问题是标记为“可选”(在示例中)的所有内容都将被建模为容器,但似乎根据RFC6020它们不能被定义为可选(即:强制性假;)。

Therefore, I defined a model that is using lists. 因此,我定义了一个使用列表的模型。 Meaning some nodes of the Python Dict (management, benchmark, m0, b0, b1) are now list elements and cannot be accessed in the current fashion, eg: data['namespace']['management']... 这意味着Python Dict的一些节点(管理,基准,m0,b0,b1)现在是列表元素,无法以当前方式访问,例如: data['namespace']['management']...

The modified example looks like this: 修改后的示例如下所示:

"namespace": [
    {
        "desc": "management",
        "interfaces": [
            {
                "leaf1": "..."
            }
        ]
    },
    {
        "desc": "benchmark",
        "interfaces": [
            {
                "leaf1": "...",
                "leaf2": "..."
            },
            {
                "leaf1": "...",
                "leaf2": "..."
            }
        ]
    }
]

The describing (snippet from my current) YANG model: 描述(我当前的片段)YANG模型:

list namespace {
    description "Namespace definitions.";
    key desc;

    leaf desc { type string; }

    uses leaf-definitions;

    list interfaces {
        key leaf1;
        uses leaf-definitions;
    }
}

The verification is successful and the conversion of the data (itself) is not a problem, but it is resulting in a big pile of broken code. 验证是成功的,数据(本身)的转换不是问题,但它导致大量破坏的代码。

This leads to my question(s): 这导致了我的问题:

  1. Am I correct - are containers in YANG always mandatory? 我是否正确 - 杨的容器总是强制性的吗?
  2. Is there maybe another way to model this scenario? 是否有其他方式来模拟这种情况? (Without breaking "too much") (没有打破“太多”)

I am very thankful for your input, since I am rather new to YANG! 我非常感谢你的意见,因为我对杨很新!

Am I correct - are containers in YANG always mandatory? 我是否正确 - 杨的容器总是强制性的吗?

Quite the contrary. 恰恰相反。 They are always optional, unless they contain mandatory nodes (mandatory leaf, a list or leaf-list with min-elements > 0, etc.). 它们始终是可选的,除非它们包含必需的节点(必需的叶子,min-elements> 0的列表或叶子列表等)。 In other words, containers inherit this property from their descendants. 换句话说,容器从其后代继承此属性。 This of course only applies to non-presence containers. 这当然仅适用于不存在的容器。 A presence container with mandatory children does not inherit this property, since that would defeat its purpose (presence). 具有强制子项的状态容器不会继承此属性,因为这会破坏其目的(存在)。 You probably missed the definition of a mandatory node in RFC6020: 您可能错过了RFC6020中强制节点的定义:

  A mandatory node is one of: o A leaf, choice, or anyxml node with a "mandatory" statement with the value "true". o A list or leaf-list node with a "min-elements" statement with a value greater than zero. o A container node without a "presence" statement, which has at least one mandatory node as a child. 

This should already be helpful for your second question. 这应该对你的第二个问题有所帮助。

Is there maybe another way to model this scenario? 是否有其他方式来模拟这种情况? (Without breaking "too much") (没有打破“太多”)

Abuse presence containers. 滥用存在容器。 They are always optional. 它们总是可选的。 You could also probably avoid using the lists by introducing some mandatory children to a non-presence container. 您也可以通过向非在线容器引入一些必需的子项来避免使用列表。 Based on your initial data: 根据您的初始数据:

module mandatory-optional-branch {
  namespace "org:example:mandatory-optional-branch";
  prefix "mob";

  grouping leafs {
    leaf leaf1 {type string;}
    leaf leaf2 {type string;}
  }

  list namespace { // mandatory
    config false;
    min-elements 1;
    max-elements 1;
    container management { // optional by nature
      presence "I have mandatory children, but am not mandatory. Yay for me.
                Of course my presence should have some meaning.";
      list interfaces { // mandatory
        min-elements 1;
        max-elements 1;
        container m0 { // optional - no mandatory node children
          leaf leaf1 {type string;}
        }
      }
    }
    container benchmark { // optional by nature
      presence "Same as 'management' above.";
      list interfaces { // mandatory
        min-elements 1;
        max-elements 1;
        container b0 { // optional - no mandatory node children
          uses leafs;
        }
        container b1 { // optional - no mandatory node children
          uses leafs;
        }
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM