YANG Input based on another input

253 views Asked by At

I have to take an input in rpc only in one previous input is taken. For Example if we input value for field a then value for field b is compulsory.

I am new to it, I tried giving mandatory true for the last value.

1

There are 1 answers

0
predi On

To have a second parameter become conditionally mandatory based on the first parameter, use when and mandatory at the same time.

module a {
    yang-version 1.1;
    namespace "a:uri";
    prefix "a"; 

    rpc foo {
        input {
            leaf first {
                type uint8;
            }
            leaf second {
                when "../first"; // <--
                type uint8;
                mandatory true;  // <--
            }
        }
    }
}

The when "../first"; condition, which is defined for leaf second says something like: "in a valid rpc instance document, leaf 'second' may only appear if leaf 'first' exists". If you add the mandatory true to leaf second, this becomes: "in a valid rpc instance document, leaf 'second' must appear if leaf 'first' exists."

Note that leaf first is optional in my example. rpc foo may only be sent without parameters or with both parameters at the same time.

Why does it work that way?

RFC 7950, Section 7.14.2 describes how input definitions work:

The "input" statement, which is optional, is used to define input parameters to the operation. It does not take an argument. The substatements to "input" define nodes under the operation's input node.

If a leaf in the input tree has a "mandatory" statement with the value "true", the leaf MUST be present in an RPC invocation.

If a leaf in the input tree has a default value, the server MUST use this value in the same cases as those described in Section 7.6.1. In these cases, the server MUST operationally behave as if the leaf was present in the RPC invocation with the default value as its value.

If a leaf-list in the input tree has one or more default values, the server MUST use these values in the same cases as those described in Section 7.7.2. In these cases, the server MUST operationally behave as if the leaf-list was present in the RPC invocation with the default values as its values.

Since the input tree is not part of any datastore, all "config" statements for nodes in the input tree are ignored.

If any node has a "when" statement that would evaluate to "false", then this node MUST NOT be present in the input tree.

Section 8 of the same RFC describes how constraints such as when and mandatory are applied - the when condition takes precedence:

o The "mandatory" constraint is enforced for leafs and choices, unless the node or any of its ancestors has a "when" condition or "if-feature" expression that evaluates to "false".