YANG - mandatory choice

2.1k Views Asked by At

From RFC 7950#7.9.4:

The behavior of the [mandatory] constraint depends on the type of the choice's closest ancestor node in the schema tree that is not a non-presence(see Section 7.5.1):

  • If no such ancestor exists in the schema tree, the constraint is enforced.
  • Otherwise, if this ancestor is a case node, the constraint is enforced if any other node from the case exists.
  • Otherwise, it is enforced if the ancestor node exists.

Now: the first and the last point seems to be quite straightforward, but I can't get the second one.

Is it trying to say that if first non-presence container ancestror is a case node, then the constraint must be enforced if the case has more than one child? Basically, it means that I must enforce the mandatory if the case contains a uses?

1

There are 1 best solutions below

7
On BEST ANSWER

A mandatory choice means that exactly one case branch (case's data node descendants) MUST exist in a valid instance document - the RFC refers to this with the term "valid data". The second bullet is an exception.

The second bullet applies to nested choices - if a choice's ancestor is a case node you are dealing with a choice nested within another choice.

module choice-case {
    yang-version 1.1;
    namespace "org:example:choice-case";
    prefix "cc";

    container top {
        choice choice {
            case mandatory-choice { // <-- case mentioned in 2nd bullet
                choice choice {
                    mandatory true; // <-- constraint not enforced until f is instantiated
                    case a-b-c { 
                        leaf a {type string;}
                        leaf b {type string;}
                        leaf c {type string;}
                    }
                    case d-e { 
                        leaf d {type string;}
                        leaf e {type string;}
                    }
                }
                leaf f {
                    type string;
                }
            }
        }
    }
}

In the above example the mandatory choice constraint is not enforced, until leaf f is also present in the instance document. If leaf nodes from either a-b-c or d-e branch are instantiated, the constraint will also be enforced, but the condition will always be satisfied.

Is it trying to say that if first non-presence container ancestror is a case node, then the constraint must be enforced if the case has more than one child?

No. The constraint is enforced if there are instances of such children in the instance document.

Basically, it means that I must enforce the mandatory if the case contains a uses?

Mandatory constraints apply to instance documents, not the schema - the schema only dictates the constraints. A uses will never exist in a context where mandatory constraints are enforced. Only instantiated data nodes defined by the referenced grouping may exist in such a context and may be considered when enforcing the constraint.