How to create dependency of leaf to another and then allow only those values which are configured on dependent node

Hi Team,

I have one leaf which is dependent on other leaf node(of different grouping) and i want to set its value only if it is configured in dependent leaf node.

ex.

container A{
     leaf x{
         type uint;
     }
}

container B{
    leaf Y{
         type leafref{
           path "../../A/x"
    }

when ,
if x is configured then only allow to set y else y should not set
case 1:

# A  10
# B 10 -> should work

case 2:

#B 10 -> should not work as x do not contain this value

Hello,

if you want to make it conditional - e.g. when you can / cannot create leaf you need a “when” statement in YANG (RFC 6020 - YANG - A Data Modeling Language for the Network Configuration Protocol (NETCONF)).

YANG snippet:

  container A {
    leaf X { type int32; }
  }

  container B {
    leaf Y {
      when "../../A/X";
      type int32;
    }
  }

CLI behavior on empty database:

  • there’s no leaf X set, Y is not suggested / cannot be configured
dev(config)# B ?
Possible completions:
\<cr>
dev(config)#
dev(config)# B Y 123
---------------^
syntax error: element does not exist
  • let’s add the dependency - X, and now also Y can be configured:
dev(config)# A X 123
dev(config)# B Y ?
Possible completions:
  \<int>
dev(config)# B ?
Possible completions:
  Y
dev(config)# B Y 456

No I wanted whatever value i give to Y it should be acceptable only when X has that value . , This dependency can be achieved via leafref .

Thanks for the support.

One quick question, is there any error message i can put in leafref if not statisfied . Currently it is showing me “syntax error: unknown argument”. I want to change this message

@josephm’s solution sounds like it would meet your requirements, but if it doesn’t, @chiya, are you looking for something like this?

container A{
     leaf x{
         type uint;
     }
}

container B{
    leaf Y{
         type uint;
         when "/A/x";
         tailf:default-ref "/A/x"
    }
}

This will work the same way as @josephm’s suggestion: Y won’t exist in the data model till /A/x is set. As soon as /A/x is set, however, /B/Y will come to be and have whatever value the user set for /A/x.

HTH!