Subject: [netmod] Should an empty enumeration be allowed within a union?

From: Nick Hancock <[email-masked]>
Date: Mon, 17 Jan 2022 15:08:06 +0000

Hi,

We need some advice on whether the following can be considered as 
valid YANG syntax and whether the behavior is that which we expect.

We have a use case (simplified in the example below) where a client is
required to define a certain concrete integer value for a server to 
use for some purpose, which is represented by the leaf 'foo' in the 
example below. 

On some servers we want to offer an additional option of an automatic 
assignment of this value by the server. To this end, we defined a 
union of an integer type and an enumeration with one enum 
'auto-select', as shown below. Since the support of 'auto-select' is 
optional, it is tagged with an if-feature statement. Note that there 
will also be additional data nodes in the schema tree that are 
associated with this configuration and will also be dependent on this 
feature.
      
      leaf foo {
        type union {
          type uint8;
          type enumeration {
            enum auto-select {
              if-feature "auto-select-foo";
              description
                "The server automatically selects a value for foo.";
            }
          }
        }
        mandatory true;
        description
          "The value of foo.";
      }
      
According to RFC 7950 section 9.6.4, an enum statement must be 
present within an enumeration definition. If the feature 'auto-select-
foo' is not supported by a server, does the enumeration still comply 
with this requirement given that there is an enum statement present? 

According to yanger it does not. Is yanger wrong? 

If the YANG is compiled without the feature, yanger currently reports 
the following error:
  "error: a type enumeration must have at least one enum statement".

The requirement that an enumeration must have at least one enum
would make sense, if the type for a leaf was just an enumeration, 
because otherwise a client would not be able to configure a valid 
value. 

However, it could be argued that in the case of the union, there would
be no issue with the enumeration being an empty set as the client 
would nevertheless be able to define a valid value and fulfill the 
'mandatory' requirement as a result of the other types in the union, 
in the example above the uint8.

What is NETMOD's opinion on this? Could the above example be seen as 
valid YANG syntax?

If this is not the case, then we have an issue as we would have to 
remove the if-feature statement from the enum. Having done this, a 
client would then be able to configure the value 'auto-select' on 
servers that do not support the feature, which is of course is
undesirable.

Assuming that the use of an empty enumeration is valid, if the feature
'auto-select-foo' is not supported, is it true that only an integer of
type unit8 would then be allowed?

Regards
Nick

---

From: Martin Björklund <[email-masked]>
Date: Tue, 18 Jan 2022 08:58:34 +0100 (CET)

Hi,

> in the example above the uint8.

I agree.

> What is NETMOD's opinion on this? Could the above example be seen as 

I think so.  I think yanger is wrong in this case.  Perhaps it would
be possible to come up with some clever little rules in the spec that
make this case legal, but make the same enumeration w/o the union
illegal.  But I don't think it would be worth it; with the
expressiveness of YANG it is possible to create models that don't make
any sense, e.g.,

  type string {
    pattern "x";
    pattern "x" {
      modifier invert-match;
    }
  }

or

  leaf foo {
    type int32;
    mandatory true;
  }
  leaf bar {
    mandatory true;
    type int32;
    must "../foo = 'hello'";
  }

etc.

So I think that even this nonsensical example is "legal":

  leaf foo {
    type enumeration {
      enum x {
        if-feature bar;
      }
    }
  }

> type unit8 would then be allowed?

Yes, that's the only reasonable interpretation.

/martin