Regular expression for a MAC Address with default value exception

84 views Asked by At

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.

1

There are 1 answers

5
predi On

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.