2

I have a base model:

model base {
  namespace "urn:base";
  prefix b;
  container c {
    leaf id {
      type uint8;
    }
    leaf temp {
      type uint8;
    }
  }
}

How to add/augment a conditional statement to /base:c/base:temp, so it's available in some specific conditions?

My first approach is to use augment in a second model, but it is not possible for leaf. From https://datatracker.ietf.org/doc/html/rfc6020:

This node is called the augment's target node. The target node MUST be either a container, list, choice, case, input, output, or notification node. It is augmented with the nodes defined in the substatements that follow the "augment" statement.

minhix
  • 31
  • 3
  • Note: a "when" is not a "conditional statement" in YANG lingo. A "when" is a statement that makes its parent statement conditional. – predi Sep 06 '22 at 07:03

3 Answers3

1

If the base model is not one you can author and therefore change, and you really (really) need to change the leaf's definition, a deviation could be another viable option.

You will not be able to make the existing leaf conditional this way, but you can add a condition to it so that the leaf becomes invalid in an instance document, if the condition evaluates to false. See this answer for an example.

A "when" statement cannot be augmented to an existing schema node. Augments are used to add schema node children to an existing schema node target. It also cannot be added via deviations.

predi
  • 5,528
  • 32
  • 60
  • Thanks! This is also figured out on my side as well. I'd upvote your answer but I don't have enough points – minhix Sep 19 '22 at 10:11
1

So finally, I found a way that serves the purpose. And the answer is pretty simple actually. So instead of using augment+when, deviate+must should be used

minhix
  • 31
  • 3
0

When you say conditional statement if you mean something like a pattern or length restriction - that cannot be done via augment !

I think, your best bet here is a choice .. case

 container c {
    leaf id {
      type uint8;
    }
    choice conditional_leaf {
      case case_a {
         leaf that-temp {
           when "<some-condition>";
           type uint8;
        }
      case case_b {
         leaf this-temp {
           when "<some-other-condition>";
           type uint8;
        }
      }
    }
  }

These when conditions could be based on the id field, for e.g. (like a tagged-union in C). Like:

           when "../id = 1";

Note: since choice and case are not data nodes, id will be 1-level above (so ../)

Ani
  • 1,448
  • 1
  • 16
  • 38