leafref require-instance allows to carry non existing values

842 views Asked by At

want to get clarity on the following:

say, (omitting key for brevity)
list l1 {
   leaf lx {
     leafref /x/y;
     require-instance false;
     mandatory false;
   }
}
  1. because mandatory is false, I can have a l1 instance without the leaf lx.
  2. because require-instance is false, i can have a list instance with lx of any value (of valid type) whether a corresponding /x/y exists or not.

And, suppose the leaf is mandatory;

list l1 {
   leaf lx {
     leafref /x/y;
     require-instance false;
     mandatory true;
   }
}

now, list instance must carry the leaf lx. And any value (of correct type) is valid because require-instance is false.

correct ?

when should I use this facility ?

1

There are 1 answers

0
Piotr Babij On BEST ANSWER

If require-instance is set to false then the value space of the leafref node is the same as the value space of the referred node. This may be useful if the value space of the referred node is particulary complex, has lots of restrictions etc. So, basically, module creators can reuse what they have defined earlier.

If require-instance is set to true (or omitted) then when there are no leaf instances that specify the value-space of a leafref node then it's value space is empty. Thus, you cannot create a valid instance of a leafref node because there are no possible values for it.

Below is the relevant part of the YANG 1.1 RFC 7950:

9.9. The leafref Built-In Type

The leafref built-in type is restricted to the value space of some leaf or leaf-list node in the schema tree and optionally further restricted by corresponding instance nodes in the data tree. The "path" substatement (Section 9.9.2) is used to identify the referred leaf or leaf-list node in the schema tree. The value space of the referring node is the value space of the referred node.

and a fragment about the require-instance statement:

9.9.3. The "require-instance" Statement

[...] If this statement is not present, it defaults to "true".

If "require-instance" is "true", it means that the instance being referred to MUST exist for the data to be valid. This constraint is enforced according to the rules in Section 8.

If "require-instance" is "false", it means that the instance being referred to MAY exist in valid data.

IMHO this part of the YANG 1.1 RFC 7950 is a bit misleading. First, it says that the value space of the referring node is the value space of the referred node but later on it says that there is an instance existence restriction by default. This means that, by default, the value space od the referring node is a set of leaf instance values of the referred node and not the complete value space of that node.