0

From the regex pattern for specific type of MAC address is mentioned here.

^([0-9A-Fa-f]{2}[:]){5}([0-9A-Fa-f]{2})$

While defining the MAC address variable under this pattern, how do I can define an exception of default value as empty string in yang files.

Example:

Yang file, regex definition

  typedef mac-address-type {
    type string {
      pattern "[a-fA-F0-9]{2}(:[a-fA-F0-9]{2}){5}";
    }
  }

Variable definition:

type aye:mac-address-type;  

How do this variable also accept an empty string in default value.

suneet saini
  • 592
  • 3
  • 16

1 Answers1

1

Define a new type that includes the existing one into its definition and therefore expands its value space. YANG has a union built-in type that allows you to do just that. A value is considered to be valid if it matches at least one type in the union of types.

In YANG version 1.1:

  typedef mac-address-type {
    type string {
      pattern "[a-fA-F0-9]{2}(:[a-fA-F0-9]{2}){5}";
    }
  }

  leaf mac-or-empty {
    type union {
      type aye:mac-address-type;
      type empty;
    }
    default "";
  }

In YANG version 1:

  typedef mac-address-type {
    type string {
      pattern "[a-fA-F0-9]{2}(:[a-fA-F0-9]{2}){5}";
    }
  }

  leaf mac-or-empty {
    type union {
      type mac-address-type;
      type string {
        pattern ''; // length 0; // as an alternative 
      }
    }
    default "";
  }

This would mean that the valid value space for the mac-or-empty leaf would be MAC addresses or an empty string.

Note: a MAC address type has already been published by the IETF as part of ietf-inet-types YANG module (ietf-inet-types:mac-address), RFC 6991, so there's no need to define your own.

The union built-in type is described in detail in RFC 7950, Section 9.12.

predi
  • 5,528
  • 32
  • 60
  • error: the value "" does not match its base type - no member type matched for the default value. To avoid this error, "typedef empty" also need to defined. – suneet saini May 22 '23 at 11:34
  • 1
    @suneetsaini, ah, you seem to be relying on YANG version 1. Indeed, the above is only valid in YANG version 1.1, because this was one of the changes between versions. – predi May 22 '23 at 11:51
  • @suneetsaini, at least I'm assuming that's what happened in your case. In should be irrelevant, whether a type is defined via a typedef or inlined like in my examples. – predi May 22 '23 at 12:00
  • Thanks for answer. One more question if there is any other option in which we can define "leaf" without union. – suneet saini May 24 '23 at 14:02
  • @suneetsaini, the only way I can think of would be to define its type exactly as per your requirement; `type string { pattern '([a-fA-F0-9]{2}(:[a-fA-F0-9]{2}){5})?'; }` for example. You cannot re-use an existing typedef that way, however. – predi May 25 '23 at 06:41