11-12-2021 02:42 AM
Hello,
We have a YANG model, a part of which looks like this:
list c-tag { key "tag"; leaf tag { type types:flexible-vlan-tag; } choice default-or-native { description "The VLAN is either set to default or native"; case native { leaf native { type empty; must "count(../../c-tag/native[current()=.])=1" { error-message "Only one c-tag can be native"; } } } ...
The count statement is meant to limit the number of list elements that have this leaf to 1. The code predates me, but it works. I don't know why it works, just wondering if somebody can clear that up.
As I understand it, the path exits to the list level and then goes back in, presumably to take into account all list elements rather than just the current one. But then what about this [current()=.]?
Solved! Go to Solution.
11-18-2021 05:28 AM
That part is actually unnecessary, in your case. "current()" and "." are the same, and they both return the current node in the context.
Your expression could also be written as
must "count(../../c-tag/native)=1"
meaning, the number of 'c-tag' instances where the 'native' leaf exists is equal to one. For an empty leaf, you cannot do any value comparison - it can just exist or not exist.
11-18-2021 05:28 AM
That part is actually unnecessary, in your case. "current()" and "." are the same, and they both return the current node in the context.
Your expression could also be written as
must "count(../../c-tag/native)=1"
meaning, the number of 'c-tag' instances where the 'native' leaf exists is equal to one. For an empty leaf, you cannot do any value comparison - it can just exist or not exist.
11-18-2021 10:31 AM - edited 11-18-2021 10:37 AM
Interesting, thank you for the clarification. That was my thinking as well, but I was stumped by the use of the square brackets on a leaf node. I am familiar with their use for defining predicates in something like a list, but how could square brackets be used on a leaf?
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide