Enum typedef customizable ranges?

I would like to define an enum, but re-use it for multiple similar leafs that have restrictions on how many of the values are valid. Is this possible?

example:

enum UP/DOWN/TESTING, each having a unique value.

One leaf would use all 3 enum values, the other would restricted to UP/DOWN only.

I tried using “range” inside the type, but it said type cannot have a range restriction statement.

You can do that, provided your YANG module is using version 1.1, i.e. you have

yang-version 1.1;

somewhere near the top of your module. With that, you can restrict an enumeration type with a set of enums which must obviously be a subset of the base type’s enum set, see RFC 7950. So in your case, you can have

  typedef directions {
    type enumeration {
      enum UP;
      enum DOWN;
      enum TESTING;
    }
  }

  leaf elevator-direction {
    type directions {
      enum UP;
      enum DOWN;
    }
  }

If this is not enough or YANG 1.1 is not an option, you can use identities instead, they are much more flexible (maybe too much flexible for your use case). Yet another option is the enumeration type combined with must statement; but I think this should be your last resort.

1 Like

That’s perfect, thank you.