Can you have a custom attribute in yang schema?

1.8k Views Asked by At

I wanted to know if we could define a custom field or attribute in one of the elements leaf,list etc. For eg: Is this possible? How can we define such fields if its possible.

model Animal{

  leaf amphibian{
      type String;
      custom "Frog";     // Custom field which has a value "Frog"
   }
}
1

There are 1 best solutions below

0
On BEST ANSWER

An "attribute" as in a "new YANG keyword"

If by "attribute" you are referring to a new YANG keyword, that has special meaning to you, then yes. YANG supports extensions. From RFC6020:

The "extension" statement allows the definition of new statements within the YANG language. This new statement definition can be imported and used by other modules.

The statement's argument is an identifier that is the new keyword for the extension and must be followed by a block of substatements that holds detailed extension information. The purpose of the "extension" statement is to define a keyword, so that it can be imported and used by other modules.

The extension can be used like a normal YANG statement, with the statement name followed by an argument if one is defined by the extension, and an optional block of substatements. The statement's name is created by combining the prefix of the module in which the extension was defined, a colon (":"), and the extension's keyword, with no interleaving whitespace.

extension custom {
  argument object;
  description "A new YANG keyword that carries special semantics.";
}

prefix:custom "Frog"; // usage of an extension

The argument keyword in the above example is an optional substatement to an extension keyword and is used to indicate that the new keyword takes (mandates) an argument (a string, such as "Frog").

The "argument" statement, which is optional, takes as an argument a string that is the name of the argument to the keyword. If no argument statement is present, the keyword expects no argument when it is used. 7.17.2

Why do you need a named argument for the new keyword? YANG may be mapped to YIN, the alternative syntax expressed as XML and that is where this name becomes important.

The argument's name is used in the YIN mapping, where it is used as an XML attribute or element name, depending on the argument's "yin- element" statement. 7.17.2

You cannot restrict the value space of this argument per se - a YANG compiler will always recognize it as a string. But there is nothing stopping you from requiring implementations to expect certain values - you are defining the meaning of the new keyword after all.

extension custom {
  argument value;
  description 
    "The value of "custom" statement's argument MUST be an integer
    in the range from 1 to 5.";
}

prefix:custom 3;

Description statements contain normative text, so if your extension contains a description substatement, stating that "value of this statement's argument MUST be an integer", then an implementation will have to respect this text.

An "attribute" as in an "XML attribute" (or a JSON equivalent)

If you meant an "attribute" in instance documents (XML, JSON,...) modeled with YANG, then the answer is no - for the most part. Pure YANG does not support modeling attributes.

However, there is a published specification for a YANG extension that allows such attributes to be defined. You can read more about that here (RFC7952). The extension is called annotation and is used to define YANG metadata, additional information that augments data that may already be modeled with YANG. Here's an example:

module using-annotation {
  namespace "org:example:using-annotation";
  prefix "ua";
  
  import ietf-yang-metadata {
    prefix "md";
  }

  md:annotation my-annotation {
    type string;
    description "This is my annotation.";
  }

  container top {
    leaf some-leaf {
      type string;
    }
  }  
}

This would be a valid XML instance according to the above model:

  <ua:top xmlns:ua="org:example:using-annotation">
    <ua:some-leaf ua:my-annotation="Yay!">foo</ua:some-leaf>
  </ua:top>

It allows an XML attribute to become valid almost anywhere.

Reading RFC7952 may also be useful for learning how the extension statement actually works and how to properly define a YANG extension.