Search code examples
ietf-netmod-yang

Why leaf node with name type does not work in yang model?


I have this simple yang model

leaf type {
    type string;
    description "some description";
}

This is not working. Can someone please explain if string 'type' is invalid for leaf name in yang?


Solution

  • A leaf statement argument must be an identifier and there is no restriction that prohibits usage of YANG keywords where an identifier is expected (all built-in keywords are also identifiers). Both leaf type {...} and leaf leaf {...} are valid YANG statements.

    The "leaf" statement is used to define a leaf node in the schema tree. It takes one argument, which is an identifier, followed by a block of substatements that holds detailed leaf information.

    RFC7950, Section 7.6

    Here's what the specification says about identifiers:

    Identifiers are used to identify different kinds of YANG items by name. Each identifier starts with an uppercase or lowercase ASCII letter or an underscore character, followed by zero or more ASCII letters, digits, underscore characters, hyphens, and dots. Implementations MUST support identifiers up to 64 characters in length and MAY support longer identifiers. Identifiers are case sensitive. The identifier syntax is formally defined by the rule "identifier" in Section 14. Identifiers can be specified as quoted or unquoted strings.

    RFC7950, Section 6.2

    The grammar rule mentioned above:

    identifier          = (ALPHA / "_")
                         *(ALPHA / DIGIT / "_" / "-" / ".")
    

    Here's what it says about the namespace of leaf statements (a namespace imposes a unique name requirement within its scope, with the purpose of preventing name clashes):

    o All leafs, leaf-lists, lists, containers, choices, rpcs, actions, notifications, anydatas, and anyxmls defined (directly or through a "uses" statement) within a parent node or at the top level of the module or its submodules share the same identifier namespace. This namespace is scoped to the parent node or module, unless the parent node is a case node. In that case, the namespace is scoped to the closest ancestor node that is not a case or choice node.

    RFC7950, Section 6.2.1