leafref inside grouping to grouping

1.2k Views Asked by At

I want to create a leafref from one grouping to another, is it possible?
I have the following files:

a.yang:

module a
{
    namespace "http://something.com/a";
    prefix a;

    import b
    {
        prefix b;
    }

    description "a configuration";

    container a
    {
        uses b:group1;
        uses b:group2;
    }
}

so, module a has two instances, group1 & group2.
I want a leafref from group2 to point on a leaf from group1.

b.yang:

module b
{
    namespace "http://something.com/b";
    prefix b;

    description "b configurations";

    grouping group1
    {
        container group1
        {
            leaf parameter-x
            {
                type uint8;
            }
        }
    }

    grouping group2
    {
        container group2
        {
            leaf ref-parameter-x
            {
                type leafref
                {
                    path "????/parameter-x of group1";
                }
            }
        }
    }
}
2

There are 2 best solutions below

0
On

Solved with augment. I do not know if possible with grouping.

0
On

This is not possible in YANG and, if you think about it, with good reason. When you create a grouping, you create a hierarchy of reusable non-namespaced data nodes which only start to make sense once the grouping is used. You can use those anywhere and they may even be used by someone else who imports your module (provided that groupings are defined at module top-level). By anywhere, I mean at any level within a YANG module. This means that you cannot use absolute nor relative paths to identify a leaf from your grouping:

container top {
  uses group1;
  container foo {
    uses group1;
  }
}

There are already two paths that could identify your leaf (again assuming top-level):

  • /top/group1/parameter-x
  • /top/foo/group1/parameter-x

So which one of these absolute expressions is the correct one to specify in your second grouping? If the answer is "either one", you cannot model this. If however you have a specific leaf in mind, you could do this (used a typedef to make it obvious that we are talking about a specific leaf):

typedef my-special-leaf {
  type leafref {
    path "/top/foo/group1/parameter-x";
  }
}

grouping group-2 {
  container group-2 {
    leaf special {
      type my-special-leaf;
    }
  }
}

Leafrefs are used for leafs that may get instantiated and you know this for a fact (are part of the schema tree). If you only define the leaf inside a grouping, it has not yet become a part of the schema tree. Not until used (anywhere but inside another grouping).

A good reusable grouping definition assumes nothing about the location at which it will be used.