I have a YANG 1.1 model that contains something of the following form:
container top {
container foo-list {
list foo {
key foo-id;
leaf foo-id {
...
}
container ping {
leaf enabled {
type boolean;
}
}
}
}
container bar-list {
list bar {
key bar-id;
leaf bar-id {
...
}
leaf-list foo-ref {
type leafref {
path "/top/foo-list/foo/foo-id";
}
}
}
}
}`Preformatted text`
I want to add a constraint (‘must’ statement) to foo-ref that says that the foo-ref leaf-list for a given bar must not refer to more than one foo for which ping/enabled is true. I know I can use deref() to obtain the single node that is pointed to by an individual leafref, but I need to somehow obtain the set of nodes ‘pointed to’ by a set of leaf-refs. Is it possible to do this using standard YANG 1.1, and if so, how?
must "count(/top/foo-list/foo[foo-id = current()/foo-ref][ping/enabled = 'true']) <= 1";
says what you want. But be careful, complex must expression are known to have poor performance, and I guess for larger configurations you really might see a considerable performance hit. If that’s the case, you might want to consider a bit different model or a different means of enforcing your constraint.
Thanks, mvf, but I don’t think that does it. For one thing, I think “current()/foo-ref” should just be “current()” because current() is a foo-ref and so does not contain a foo-ref. But more importantly, is it not the case that “top/foo-list/foo[foo-id = current()]” can only ever match a single node?
What I need is to be able to find the set of foo nodes that are referenced by the set of nodes in “…/foo-ref” where … is the bar that is the parent of the node to which the must statement is applied, and then count how many of those foo nodes have ping/enabled = true.
Oh, I omitted an important part - the must statement should be placed on the bar node; so current() points to the current bar instance. This means that foo-id = current()/foo-ref matches exactly those foo instances that are referred to by any of foo-refs under the current bar instance.
Ah - that makes much more sense! It looks to me as though it should do what I want. I will test to see whether it works in practice. I’m certainly learning more about how XPath predicates work, so thank you very much for your help!