简体   繁体   English

如何使用 Leafref 在 YANG model 中实现递归?

[英]How to implement recursion in YANG model using leafref?

I have the following XML that I want to model with YANG.我有以下 XML 我想用 YANG model。 The XML contains a list of nodes and each node contains a list of neighbor nodes. XML 包含一个节点列表,每个节点包含一个相邻节点列表。

<nodes>
  <node>
    <id>1</id>
    <node>
      <id>2</id>
    </node>
  </node>
  <node>
    <id>3</id>
    <node>
      <id>4</id>
    </node>
  </node>
</nodes>

Please find below the YANG model I tried to create.请在我尝试创建的 YANG model 下面找到。 Unfortunately, Yang does not support circular references in grouping.不幸的是,Yang 不支持分组中的循环引用。

grouping node {
  list node {
    leaf id {
      type int32;
    }
    uses node;
  }
}

container nodes {
   uses node;
}

I saw in draft-ietf-netmod-routing-cfg-16 and on ietf mail archive that a way to emulate recursion is to use leafref.我在draft-ietf-netmod-routing-cfg-16ietf 邮件存档中看到模拟递归的一种方法是使用 Leafref。 How can the above xml be modeled with grouping and leafref?上面的xml怎么用grouping和leafref来建模呢?

As you said, recursion using groupings is not supported.正如您所说,不支持使用分组进行递归。 The simplest approach would be to have a flat list of nodes, where each node has a parent node, referenced as a leafref.最简单的方法是有一个平面的节点列表,其中每个节点都有一个父节点,被引用为一个叶子引用。 Something like:就像是:

container nodes {
  list node {
    key id;
    leaf id { type int32; }
    leaf parent-id {
      type leafref {
        path "../../node/id";
      }
    }
  }
}

The equivalent XML would be:等效的 XML 将是:

<nodes>
  <node>
    <id>1</id>
  <node>
  <node>
    <id>2</id>
    <parent-id>1</parent-id>
  </node>
  <node>
    <id>3</id>
  <node>
  <node>
    <id>4</id>
    <parent-id>3</parent-id>
  </node>
</nodes>

You could do the opposite, where a node refers its children (via a leafref leaf-list) instead of its parents.你可以做相反的事情,一个节点引用它的孩子(通过一个leafref叶子列表)而不是它的父母。


Of course, using the same data node directly recursively does work:当然,直接递归使用相同的数据节点确实有效:

container nodes {
  list node {
    key id;
    leaf id { type int32; }

    list node {
      key id;
      leaf id { type int32; }

      list node {
        key id;
        leaf id { type int32; }

        // and so on
      }
    }
  }
}

but this does not allow indefinitely deep hierarchies.但这不允许无限深的层次结构。 I would avoid this pattern, though, not a clean model design.不过,我会避免这种模式,而不是干净的 model 设计。

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

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