2

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?

Community
  • 1
  • 1
Fylax
  • 1,138
  • 2
  • 15
  • 24

1 Answers1

1

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.

predi
  • 5,528
  • 32
  • 60
  • Just for clarity: this means that outer choice is not mandatory, but if in instance document I have outer choice with instantiated `mandatory-choice` then it can be empty since its inner choice is mandatory. Correct? – Fylax Oct 08 '18 at 07:34
  • @Fylax, a choice/case cannot be instantiated, it only imposes restrictions on valid data nodes. Only choice's data node descendants in one of the case branches may be instantiated (such as the `a-b-c` or `d-e` leafs in my example). If you remove the outer choice (and case), you then need at least one of the leafs instantiated for the document to be valid (if container `top` also exists), but with it in place, you don't need any of them to be. – predi Oct 08 '18 at 10:27
  • I think I got it. `outer-choice` does not require any instantiated descendant. But if I'm instantiating in with `mandatory-choice` then I need to instantiate `a-b-c` or `d-e` as well. Right? – Fylax Oct 08 '18 at 11:34
  • 1
    @Fylax, yes, something like that. Perhaps I picked a bad example. Imagine another leaf `f` that is a sibling to the nested mandatory choice, the `mandatory-choice` case being their parent. Now if `f` is instantiated, you must also have something from either `a-b-c` or `d-e` instantiated. – predi Oct 08 '18 at 11:40
  • @Fylax, I edited the answer. Hope the example is much clearer now. :) – predi Oct 08 '18 at 12:06
  • Shouldn't it be "the constraint is enforced until `f` is also present [...]"? The RFC seems to suggest that "if there is instantiated a `choice-in-case-sibling`, the constraint is enforced. – Fylax Oct 08 '18 at 12:38
  • 1
    @Fylax, not sure that I follow. If there is no `f` in the instance document, the mandatory constraint on the choice sibling will be ignored, hence not enforced. If `f` is there, the mandatory constraint must not be ignored any more - it needs to be evaluated and must evaluate to true, hence is enforced. – predi Oct 08 '18 at 14:48