ConfD User Community

Query regarding current() usage in must statement

Hi,

I have a yang model as below where a list has been defined to have a max of 1 element only and need to find a way to ensure that the parallel leaf ct1-sec-id doesn’t take the same value as ct1-id[id] elem created. I am not quite sure if I am using the “current()” function correctly here since the “Current()” call should resolve to the value of ct1-sec-id leaf rather than the top level list element.

Can someone suggest how I could go about ensuring the must statement is satisfied here with respect to syntax?

container ct1 {
  list ct1-id {
    key "id";
    max-elements 1;
    leaf id {
        type int;
    }
  }
  
  leaf ct1-sec-id {
    must "/ct1/ct1-id[id=current()] != ct1-sec-id";
    type int;
  }
}

If i interpret your requirement correctly, it means that there must not be a key element of value ct1-sec-id in the ct1-id{} list, so something like:

container ct1 {
  list ct1-id {
    key "id";
    max-elements 1;
    leaf id { type int32; }
  }

  leaf ct1-sec-id {
    must "not(../ct1-id[id=current()])";
    type int32;
  }
}

Some commands tested:

xubuntu-dev(config)# ct1 ct1-id 123
xubuntu-dev(config-ct1-id-123)# ct1 ct1-sec-id 123
xubuntu-dev(config)# commit
Aborted: 'ct1 ct1-sec-id' (value "123"): the 'must' expression "not(../ct1-id[id=current()])" failed
xubuntu-dev(config)# ct1 ct1-sec-id 234
xubuntu-dev(config)# commit
Commit complete.
xubuntu-dev(config)#
1 Like

Thanks! yes that was the requirement i.e. its to ensure ct1-sec-id is never equal to any the only element inside ct1-id list.

Also I assume we would need a must statement inside the leaf id of the ct1-id list to prevent modifications of the c1-id list element which could make it equal to ct1-sec-id?

list ct1-id {
key “id”;
max-elements 1;
leaf id {
must “not(current() = …/…/ct1-sec-id)”;
type int32;
}
}

that is not necessary, you don’t need to have two-directional check, one must statement is sufficient → e.g. commands for same YANG above:

xubuntu-dev(config)# show full-configuration ct1
ct1 ct1-sec-id 234
ct1 ct1-id 123
!
xubuntu-dev(config)# ct1 ct1-id 234
xubuntu-dev(config-ct1-id-234)# no ct1 ct1-id 123
xubuntu-dev(config-ct1-id-234)# commit
Aborted: 'ct1 ct1-sec-id' (value "234"): the 'must' expression "not(../ct1-id[id=current()])" failed
xubuntu-dev(config-ct1-id-234)#
1 Like