schemaietf-netmod-yang

How to augment an enumeration in YANG


Is there a way to augment an enumeration in another module in YANG ? There is no way, in my case, to put all the values in the first module where the enumeration was defined.

Knowing that the enumeration is inside a grouping as follows:

grouping mygrouping {
    ...
    container mycontainer {
        ...
        list mylist {
            leaf type {
                type enumeration {
                    enum type1
                    enum type2
                    ...
                    enum typen
                }
            }
        }
    }
}

The grouping is used in the new module, but I couldn't augment the leaf type to add new types in the enumeration.


Solution

  • In YANG, enumerations are for well-known static set of options. For extensible options, you can use identityrefs. This allows identities to be used in multiple files, and to define a leaf with a identityref type, which can then take any of the values of the defined identities.

    Think of it as a decentralized enumeration. It's not really 'augmenting' but it does allow to introduce new options to a value without changing the original module. Of course, this does assume that you can actually change the original leaf with the enumeration.

    Definition of identities in YANG RFC: https://www.rfc-editor.org/rfc/rfc6020#section-7.16 Some reference on enumeration versus identities: https://www.rfc-editor.org/rfc/rfc8407#section-4.11.1


    Update: one option that is 'sort of' augmenting enums is to define the original enum in a typedef, and then extend it via a union:

    typedef myenum {
        enum val1 { value 1; }
        enum val2 { value 2; }
        enum val3 { value 3; }
    }
    
    ...
    
    leaf myleaf {
        type union {
            type myenum;
            type enumeration {
                enum val4 { value 4; }
                enum val5 { value 5; }
            }
        }
    }
    

    So in this case, the myleaf can have values val1, val2, val3, val4, val5, which means the original enum was indeed 'augmented'.

    Of course this means it is not really an enum, but a union between two enums, which are arranged so that their values don't intersect (something that unions do allow). This may or may not be a simplification, on both client and server side - depending on the implementation.