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"
}
}
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
argument
keyword in the above example is an optional substatement to anextension
keyword and is used to indicate that the new keyword takes (mandates) an argument (a string, such as"Frog"
).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.
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.
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:This would be a valid XML instance according to the above model:
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.