简体   繁体   English

您可以在yang模式中拥有自定义属性吗?

[英]Can you have a custom attribute in yang schema?

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" “新YANG关键字”中的“属性”

If by "attribute" you are referring to a new YANG keyword, that has special meaning to you, then yes. 如果通过“属性”指的是对您具有特殊意义的新YANG关键字,则可以。 YANG supports extensions. YANG支持扩展。 From RFC6020 : RFC6020开始

The "extension" statement allows the definition of new statements within the YANG language. “扩展”语句允许在YANG语言中定义新语句。 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. 该扩展名可以像普通的YANG语句一样使用,该语句名后跟一个参数(如果扩展名定义了该参数),以及一个可选的子语句块。 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" ). 上例中的argument关键字是extension关键字的可选子语句,用于指示new关键字接受(强制)参数(字符串,例如"Frog" )。

The "argument" statement, which is optional, takes as an argument a string that is the name of the argument to the keyword. “ argument”语句是可选的,它以字符串作为参数作为参数,该字符串是关键字的参数名称。 If no argument statement is present, the keyword expects no argument when it is used. 如果不存在任何自变量语句,则使用该关键字时不包含自变量。 7.17.2 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. YANG可能会映射到YIN,YIN是表示为XML的替代语法,因此该名称变得很重要。

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. 参数的名称在YIN映射中使用,根据XML的“ yin-element”语句,它用作XML属性或元素名称。 7.17.2 7.17.2

You cannot restrict the value space of this argument per se - a YANG compiler will always recognize it as a string. 您本身不能限制此参数的值空间-YANG编译器将始终将其识别为字符串。 But there is nothing stopping you from requiring implementations to expect certain values - you are defining the meaning of the new keyword after all. 但是没有什么可以阻止您要求实现期望某些值-毕竟,您正在定义new关键字的含义。

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. 描述语句包含规范性文本,因此,如果您的扩展名包含description子语句,说明“此语句的参数的值必须为整数”,则实现将必须遵守此文本。

An "attribute" as in an "XML attribute" (or a JSON equivalent) 如“ XML属性”(或等效的JSON)中的“属性”

If you meant an "attribute" in instance documents (XML, JSON,...) modeled with YANG, then the answer is no - for the most part. 如果您的意思是使用YANG建模的实例文档(XML,JSON等)中的“属性”,那么答案是否定的-大多数情况下。 Pure YANG does not support modeling attributes. Pure YANG不支持建模属性。

However, there is a published specification for a YANG extension that allows such attributes to be defined. 但是,存在针对YANG扩展的已发布规范,该规范允许定义此类属性。 You can read more about that here (RFC7952). 您可以在此处 (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. 该扩展称为annotation ,用于定义YANG元数据,这是补充可能已使用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: 根据上述模型,这将是有效的XML实例:

  <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. 它允许XML属性几乎在任何地方都有效。

Reading RFC7952 may also be useful for learning how the extension statement actually works and how to properly define a YANG extension . 阅读RFC7952也可能有助于了解extension语句的实际工作方式以及如何正确定义YANG扩展

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM