Yang model mandatory node only when condition is true

2.1k Views Asked by At

I have an XML file:

<a>
    <b>true</b>
    <c>foo</c>
</a>

and a Yang model:

container a {
   leaf b {
      type boolean;
   }

   leaf c {
      type string;
   }
}

Node 'c' is mandatory only when node 'b' equals 'true'. If I add a mandatory: true constraint to node 'c' it will become mandatory for all values of 'b'.

How to change the Yang model so that node 'c' is mandatory when 'b' is 'true' and optional when 'b' is false?

2

There are 2 best solutions below

0
Piotr Babij On BEST ANSWER

You can use the must statement with an XPath condition:

container a {
   must 'not(b) or boolean(c)'
  
   leaf b {
      type boolean;
   }

   leaf c {
      type string;
   }
}
0
frank On

Yang1.1 support optional mandatory

container a {  
   leaf b {
      type boolean;
   }

   leaf c {
      when "../b";
      mandatory true;
      type string;
   }
}