I am writing a Yang model. Is there away to set a leaf (string or enumeration) with a value based on another field. So for example, I want to say if x then field a value is b, if z then field a value is c.
Edit: I am new to yang and still trying to learn it, if there are any other ideas or operators I can use to solve this issue please do not hesitate to share. :D Thank you very much.
You can use when
and must
constructs: YANG 1.1, Section 7.5.3 says:
The must statement, which is optional, takes as an argument a string that contains an XPath expression (see Section 6.4). It is used to formally declare a constraint on valid data. The constraintis enforced according to the rules in Section 8.
And Section 7.5.4.3 lays it out:
container interface {
leaf ifType {
type enumeration {
enum ethernet;
enum atm;
}
}
leaf ifMTU {
type uint32;
}
must 'ifType != "ethernet" or ifMTU = 1500' {
error-message "An Ethernet MTU must be 1500";
}
must 'ifType != "atm" or'
+ ' (ifMTU <= 17966 and ifMTU >= 64)' {
error-message "An ATM MTU must be 64 .. 17966";
}
}
On when
, Section 7.21.5 reads,
The when statement makes its parent data definition statement conditional. The node defined by the parent data definition statement is only valid when the condition specified by the when statement is satisfied. The statement's argument is an XPath expression (see Section 6.4), which is used to formally specify this condition.
ConfD provides a tutorial on XPath in NETCONF and YANG; this examples comes out of it:
augment /system/login/user {
when “class != ’wheel’”;
leaf uid {
type uint16 {
range “1000 .. 30000”;
}
}
}