Search code examples
yang

How do you model an array in YANG?


Summary

I want to create a YANG model for an ordered list of not-necessarily-unique numbers. For example, [0, 0, 0, 1, 0, 5, 17, 0, 0].

Problem

A list requires a key. My data does not have keys. I cannot use the values as keys because I want to allow repeats.

list items {
    key value; // cannot do this. Need to allow repeats
    leaf value {
        type uint8;
    }
    ordered-by user;
}

leaf-list values must be unique. I need to allow repeats, so this won't work.

leaf-list items { // cannot do this. Need to allow repeats
    type uint8;
    ordered-by user;
}

What I Want to Avoid

Is adding a separate unused leaf to a list really the only way?

list items {
    key annoying-useless-thing;   // DO NOT USE
    leaf annoying-useless-thing { // DO NOT USE
        type uint8;               // DO NOT USE
    }                             // DO NOT USE
    leaf value {
        type uint8;
    }
    ordered-by user;
}

Now all my xpaths change from <my-path>/items to <my-path>/items/value and I have some useless annoying-useless-thing nodes that I need to worry about keeping unique every time... I now have config data that isn't really part of the config.


Solution

  • Like you stated, there is no array-like YANG statement where instance repetitions would be allowed, with the exception of keyless config "false" lists and config "false" leaf-lists, which won't help you model configuration.

    You could model this as a single leaf of type "string" with a carefully crafted pattern restriction. That would complicate your XPath expressions a bit (you mention them in your question so I assume they are important), but with some YANG 1.1 re-match() XPath function dark magic it should be possible to impose further restrictions based on the leaf's value elsewhere - at least in part.

    A quick example:

      leaf items {
        type string {
          pattern '\s?(0|[1-9][0-9]*)(\s?,\s?(0|[1-9][0-9]*))*\s?';
        }
      }
    

    And if you don't need to leverage built in YANG value validation, there's always the good old normative text in YANG descriptions, where you can describe the intention behind the value in detail. That won't help non-humans though.