Getting element count for a inner list in validation point

Hi,

I have a list inside, list in yang file. Now I want to limit the total number of instances of the inner list, in all the outer list. But in maapi, I cannot iterate through the outer list and find number of instances of inner list.

How do I write my validation code?

Thanks, Prasanth

You certainly can, as in “it is possible”, but it’s better not to do it:

  container c {
    must "count(outer/inner) <= 10" {
      error-message "too many list entries";
    }
    list outer {
      key k;
      leaf k {
        type string;
      }
      list inner {
        key k;
        leaf k {
          type string;
        }
      }
    }
  }

But perhaps you actually wanted to do something other than counting in your validation callback…

Thanks, Per.

The problem is the count is dynamic and I cannot hard cord it in yang file. I calculate it in application code. There I don’t have a way to get maapi_num_instance of an inner node. (Note that iteration with index is not possible in maapi)

Well, it doesn’t need to be hard-coded to use it in a ‘must’ expression:

  container c {
    must "count(outer/inner) <= max-entries" {
      error-message "too many list entries";
    }
    leaf max-entries {
      type uint32;
      default 0;
      tailf:hidden full;
    }

Maybe that is dynamic enough? You can’t be arbitrarily dynamic - having a value that is lower than the currently existing number of entries would mean that the existing configuration is invalid, which is Really Bad™ (and you won’t be able to set such a value in the model above and commit it, which is Good™).

Btw, if this is really about resource limitations, validation is not the place to check such things - it should only verify that the configuration-to-be is logically valid. Use a CDB two-phase subscriber and check resource limitations in the prepare phase.

Yes, you just need to walk the outer list with a get_next traversal, and add up the entries for the inner lists (there can be an inner list for each outer list entry) - something like (untested, and error checking omitted):

struct maapi_cursor mc;
int num_entries = 0;

maapi_init_cursor(sock, th, &mc, "/c/outer");
maapi_get_next(&mc);
while (mc.n != 0) {
    num_entries +=
        maapi_num_instances(sock, th, "/c/outer{%*x}/inner", mc.n, mc.keys);
    maapi_get_next(&mc);
}

In the general case of an arbitrary depth of nested lists, you need to do get_next traversal on all but the innermost list (using a separate cursor for each list).

Thanks a lot. This is exactly what I needed.