<?xml version='1.0' encoding='utf-8'?>
<?xml-stylesheet type="text/xsl" href="rfc2629.xslt"?>
<!DOCTYPE rfc SYSTEM "rfc2629-xhtml.ent">
<?rfc toc="yes"?>
<?rfc compact="no"?>
<?rfc subcompact="no"?>
<?rfc symrefs="yes" ?>
<?rfc sortrefs="yes"?>
<?rfc iprnotified="no"?>
<?rfc strict="yes"?>
<rfc xmlns:xi="http://www.w3.org/2001/XInclude" ipr="trust200902" updates="8340" category="std" docName="draft-ietf-netmod-yang-data-ext-05" obsoletes="" submissionType="IETF" xml:lang="en" tocInclude="true" symRefs="true" sortRefs="true" version="3">
  <!-- xml2rfc v2v3 conversion 2.40.0 -->
  <front>
    <title abbrev="YANG Structure">YANG Data Structure Extensions</title>
    <seriesInfo name="Internet-Draft" value="draft-ietf-netmod-yang-data-ext-05"/>
    <author initials="A" surname="Bierman" fullname="Andy Bierman">
      <organization>YumaWorks</organization>
      <address>
        <email>andy@yumaworks.com</email>
      </address>
    </author>
    <author initials="M" surname="Bjorklund" fullname="Martin Bjorklund">
      <organization>Cisco</organization>
      <address>
        <email>mbj@tail-f.com</email>
      </address>
    </author>
    <author initials="K" surname="Watsen" fullname="Kent Watsen">
      <organization>Watsen Networks</organization>
      <address>
        <email>kent+ietf@watsen.net</email>
      </address>
    </author>
    <date/>
    <abstract>
      <t>
This document describes YANG mechanisms for
defining abstract data structures with YANG.
      </t>
    </abstract>
  </front>
  <middle>
    <section anchor="introduction" numbered="true" toc="default">
      <name>Introduction</name>
      <t>
There is a need for standard mechanisms to allow the
definition of abstract data that is not intended to
be implemented as configuration or operational state.
The "yang&nbhy;data" extension statement from RFC 8040 <xref target="RFC8040" format="default"/>
was defined for this purpose but it is limited in its
functionality.
      </t>
      <t>
The intended use of the "yang&nbhy;data" extension was to model all or part
of a protocol message, such as the "errors" definition in the YANG
module "ietf&nbhy;restconf" <xref target="RFC8040" format="default"/>, or the contents of a file.  However,
protocols are often layered such that the header or payload portions
of the message can be extended by external documents.  The YANG
statements that model a protocol need to support this extensibility
that is already found in that protocol.
      </t>
      <t>
This document defines a new YANG extension statement called
"structure", which is similar to but more flexible than the
"yang&nbhy;data" extension from <xref target="RFC8040" format="default"/>.  There is no assumption that a
YANG data structure can only be used as a top-level abstraction, and
it may also be nested within some other data structure.
      </t>
      <t>
This document also defines a new YANG extension statement called
"augment&nbhy;structure", which allows abstract data structures to be
augmented from external modules, similarly to the existing YANG
"augment" statement.  Note that "augment" cannot be used to augment a
YANG data structure since a YANG compiler or other tool is not
required to understand the "structure" extension.
      </t>
      <section anchor="terminology" numbered="true" toc="default">
        <name>Terminology</name>
        <t>
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
"SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and
"OPTIONAL" in this document are to be interpreted as described in
BCP 14 <xref target="RFC2119" format="default"/> <xref target="RFC8174" format="default"/> when, and only when, they appear in all
capitals, as shown here.
        </t>
        <t>
The following terms are used within this document:
        </t>
        <ul spacing="normal">
          <li>
YANG data structure: A data structure defined with the "structure"
statement.
 </li>
        </ul>
        <section anchor="nmda" numbered="true" toc="default">
          <name>NMDA</name>
          <t>
The following terms are defined in the
Network Management Datastore Architecture
(NMDA) <xref target="RFC8342" format="default"/>,
and are not redefined here:
          </t>
          <ul spacing="normal">
            <li>
configuration
 </li>
            <li>
operational state
 </li>
          </ul>
        </section>
        <section anchor="yang" numbered="true" toc="default">
          <name>YANG</name>
          <t>
The following terms are defined in <xref target="RFC7950" format="default"/>:
          </t>
          <ul spacing="normal">
            <li>
absolute-schema-nodeid
 </li>
            <li>
container
 </li>
            <li>
data definition statement
 </li>
            <li>
data node
 </li>
            <li>
leaf
 </li>
            <li>
leaf-list
 </li>
            <li>
list
 </li>
          </ul>
        </section>
      </section>
    </section>
    <section anchor="definitions" numbered="true" toc="default">
      <name>Definitions</name>
      <t>
A YANG data structure is defined with the "structure" extension
statement, defined in the YANG module "ietf&nbhy;yang&nbhy;structure&nbhy;ext".  The
argument to the "structure" extension statement is the name of the
data structure.  The data structures are considered to be in the same
identifier namespace as defined in section 6.2.1 of <xref target="RFC7950" format="default"/>. In
particular, bullet 7:
      </t>
      <artwork name="" type="" align="left" alt=""><![CDATA[
   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.
      ]]></artwork>
      <t>
This means that data structures defined with the "structure" statement
cannot have the same name as sibling nodes from regular YANG data
definition statements or other "structure" statements in the same YANG
module.
      </t>
      <t>
This does not mean a YANG data structure, once defined, has to be used
as a top-level protocol message or other top-level data structure.
      </t>
      <t>
A YANG data structure is encoded in the same way as an "anydata" node.
This means that the name of the structure is encoded as a "container",
with the instantiated children encoded as child nodes to this
node.  For example, this structure:
      </t>
      <artwork name="" type="" align="left" alt=""><![CDATA[
  module example-errors {
    ...

    sx:structure my-error {
      leaf error-number {
        type int;
      }
    }
  }
      ]]></artwork>
      <t>
can be encoded in JSON as:
      </t>
      <artwork name="" type="" align="left" alt=""><![CDATA[
  "example-errors:my-error": {
    "error-number": 131
  }
      ]]></artwork>
    </section>
    <section anchor="yang-data-structures-in-yang-tree-diagrams" numbered="true" toc="default">
      <name>YANG Data Structures in YANG Tree Diagrams</name>
      <t>
A YANG data structure can be printed in a YANG Tree Diagram <xref target="RFC8340" format="default"/>.
This document updates RFC 8340 by defining two new sections in the
tree diagram for a module:
      </t>
      <ol spacing="normal" type="1">
        <li>
YANG data structures, offset by two spaces and identified by the keyword
"structure" followed by the name
of the YANG data structure and a colon (":") character.
 </li>
        <li>
YANG data structure augmentations, offset by 2 spaces and identified
by the keyword "augment&nbhy;structure" followed by the augment target
structure name and a colon (":") character.
 </li>
      </ol>
      <t>
The new sections, including spaces conventions is:
      </t>
      <artwork name="" type="" align="left" alt=""><![CDATA[
  structure <structure-name>:
    +--<node>
       +--<node>
       |  +--<node>
       +--<node>
  structure <structure-name>:
    +--<node>

  augment-structure <structure-name>:
    +--<node>
       +--<node>
       |  +--<node>
       +--<node>
  augment-structure <structure-name>:
    +--<node>
      ]]></artwork>
      <t>
Nodes in YANG data structures are printed according to the rules
defined in section 2.6 in <xref target="RFC8340" format="default"/>.
      </t>
    </section>
    <section anchor="mod" numbered="true" toc="default">
      <name>YANG Data Structure Extensions Module</name>
      <t>
RFC Ed.: update the date below with the date of RFC publication and
remove this note.
      </t>
      <t>&lt;CODE BEGINS&gt; file "ietf-yang-structure-ext@2019-03-07.yang"</t>
      <artwork name="" type="" align="left" alt=""><![CDATA[
module ietf-yang-structure-ext {
  yang-version 1.1;
  namespace "urn:ietf:params:xml:ns:yang:ietf-yang-structure-ext";
  prefix sx;

  organization
    "IETF NETMOD (NETCONF Data Modeling Language) Working Group";
  contact
    "WG Web:   <http://tools.ietf.org/wg/netmod/>
     WG List:  <mailto:netmod@ietf.org>

     Author:   Andy Bierman
               <mailto:andy@yumaworks.com>

     Author:   Martin Bjorklund
               <mailto:mbj@tail-f.com>

     Author:   Kent Watsen
               <mailto:kent+ietf@watsen.net>";
  description
    "This module contains conceptual YANG specifications for defining
     abstract data structures.

     The key words 'MUST', 'MUST NOT', 'REQUIRED', 'SHALL', 'SHALL
     NOT', 'SHOULD', 'SHOULD NOT', 'RECOMMENDED', 'NOT RECOMMENDED',
     'MAY', and 'OPTIONAL' in this document are to be interpreted as
     described in BCP 14 (RFC 2119) (RFC 8174) when, and only when,
     they appear in all capitals, as shown here.

     Copyright (c) 2019 IETF Trust and the persons identified as
     authors of the code.  All rights reserved.

     Redistribution and use in source and binary forms, with or
     without modification, is permitted pursuant to, and subject to
     the license terms contained in, the Simplified BSD License set
     forth in Section 4.c of the IETF Trust's Legal Provisions
     Relating to IETF Documents
     (http://trustee.ietf.org/license-info).

     This version of this YANG module is part of RFC XXXX
     (https://www.rfc-editor.org/info/rfcXXXX); see the RFC itself
     for full legal notices.";

  // RFC Ed.: update the date below with the date of RFC publication
  // and remove this note.

  revision 2019-03-07 {
    description
      "Initial revision.";
    // RFC Ed.: replace XXXX with RFC number and remove this note
    reference
      "RFC XXXX: YANG Structure Extensions.";
  }

  extension structure {
    argument name {
      yin-element true;
    }
    description
      "This extension is used to specify a YANG data structure that
       represents conceptual data defined in YANG.  It is intended to
       describe hierarchical data independent of protocol context or
       specific message encoding format.  Data definition statements
       within a 'structure' extension statement specify the generic
       syntax for the specific YANG data structure, whose name is the
       argument of the 'structure' extension statement.

       Note that this extension does not define a media-type.  A
       specification using this extension MUST specify the message
       encoding rules, including the content media type, if
       applicable.

       The mandatory 'name' parameter value identifies the YANG data
       structure that is being defined.

       This extension is only valid as a top-level statement, i.e.,
       given as a sub-statement to 'module' or 'submodule'.

       The sub-statements of this extension MUST follow the ABNF
       rules below, where the rules are defined in RFC 7950:

         *must-stmt
         [status-stmt]
         [description-stmt]
         [reference-stmt]
         *(typedef-stmt / grouping-stmt)
         *data-def-stmt

       A YANG data structure defined with this extension statement is
       encoded in the same way as an 'anydata' node.  This means
       that the name of the structure is encoded as a 'container',
       with the instantiated child statements encoded as child nodes
       to this node.

       The module name and namespace value for the YANG module using
       the extension statement is assigned to each of the data
       definition statements resulting from the YANG data structure.

       The XPath document element is the extension statement itself,
       such that the child nodes of the document element are
       represented by the data-def-stmt sub-statements within this
       extension.  This conceptual document is the context for the
       following YANG statements:

         - must-stmt
         - when-stmt
         - path-stmt
         - min-elements-stmt
         - max-elements-stmt
         - mandatory-stmt
         - unique-stmt
         - ordered-by
         - instance-identifier data type

       The following data-def-stmt sub-statements are constrained
       when used within a 'structure' extension statement.

         - The list-stmt is not required to have a key-stmt defined.
         - The config-stmt is ignored if present.
      ";
  }

  extension augment-structure {
    argument path {
      yin-element true;
    }
    description
      "This extension is used to specify an augmentation to YANG data
       structure defined with the 'structure' statement.  It is
       intended to describe hierarchical data independent of protocol
       context or specific message encoding format.

       This statement has almost the same structure as the
       'augment-stmt'.  Data definition statements within this
       statement specify the semantics and generic syntax for the
       additional data to be added to the specific YANG data
       structure, identified by the 'path' argument.

       The mandatory 'path' parameter value identifies the YANG
       conceptual data node that is being augmented, represented as
       an absolute-schema-nodeid string, where the first node in the
       absolute-schema-nodeid string identifies the YANG data
       structure to augment, and the rest of the nodes in the string
       identifies the node within the YANG structure to augment.

       This extension is only valid as a top-level statement, i.e.,
       given as a sub-statement to 'module' or 'submodule'.

       The sub-statements of this extension MUST follow the ABNF
       rules below, where the rules are defined in RFC 7950:

         [status-stmt]
         [description-stmt]
         [reference-stmt]
         1*(data-def-stmt / case-stmt)

       The module name and namespace value for the YANG module using
       the extension statement is assigned to instance document data
       conforming to the data definition statements within this
       extension.

       The XPath document element is the augmented extension
       statement itself, such that the child nodes of the document
       element are represented by the data-def-stmt sub-statements
       within the augmented 'structure' statement.

       The context node of the 'augment-structure' statement is
       derived in the same way as the 'augment' statement, as defined
       in section 6.4.1 of [RFC7950]. This conceptual node is
       considered the context node for the following YANG statements:

         - must-stmt
         - when-stmt
         - path-stmt
         - min-elements-stmt
         - max-elements-stmt
         - mandatory-stmt
         - unique-stmt
         - ordered-by
         - instance-identifier data type

       The following data-def-stmt sub-statements are constrained
       when used within an 'augment-structure' extension statement.

         - The list-stmt is not required to have a key-stmt defined.
         - The config-stmt is ignored if present.

       Example:

          module foo {
             import ietf-yang-structure-ext { prefix sx; }

             sx:structure foo-data {
               container foo-con { }
             }
          }

          module bar {
             import ietf-yang-structure-ext { prefix sx; }
             import foo { prefix foo; }

             sx:augment-structure /foo:foo-data/foo:foo-con {
               leaf add-leaf1 { type int32; }
               leaf add-leaf2 { type string; }
             }
          }
      ";
  }
}
      ]]></artwork>
      <t>&lt;CODE ENDS&gt;</t>
    </section>
    <section anchor="iana" numbered="true" toc="default">
      <name>IANA Considerations</name>
      <section anchor="yang-module-registry" numbered="true" toc="default">
        <name>YANG Module Registry</name>
        <t>
This document registers one URI as a namespace in the
"IETF XML Registry" <xref target="RFC3688" format="default"/>:
        </t>
        <artwork name="" type="" align="left" alt=""><![CDATA[
    URI: urn:ietf:params:xml:ns:yang:ietf-yang-structure-ext
    Registrant Contact: The IESG.
    XML: N/A; the requested URI is an XML namespace.
      ]]></artwork>
        <t>
This document registers one YANG module in the "YANG Module Names"
registry <xref target="RFC6020" format="default"/>:
        </t>
        <artwork name="" type="" align="left" alt=""><![CDATA[
    name:         ietf-yang-structure-ext
    namespace:    urn:ietf:params:xml:ns:yang:ietf-yang-structure-ext
    prefix:       sx
    // RFC Ed.: replace XXXX with RFC number and remove this note
    reference:    RFC XXXX
      ]]></artwork>
      </section>
    </section>
    <section anchor="security-considerations" numbered="true" toc="default">
      <name>Security Considerations</name>
      <t>
This document defines YANG extensions that are used to define
conceptual YANG data structures.  It does not introduce any new
vulnerabilities beyond those specified in YANG 1.1 <xref target="RFC7950" format="default"/>.
      </t>
    </section>
  </middle>
  <back>
    <references>
      <name>References</name>
      <references>
        <name>Normative References</name>
        <xi:include href="https://xml2rfc.tools.ietf.org/public/rfc/bibxml/reference.RFC.2119.xml"/>
        <xi:include href="https://xml2rfc.tools.ietf.org/public/rfc/bibxml/reference.RFC.7950.xml"/>
        <xi:include href="https://xml2rfc.tools.ietf.org/public/rfc/bibxml/reference.RFC.8040.xml"/>
        <xi:include href="https://xml2rfc.tools.ietf.org/public/rfc/bibxml/reference.RFC.8174.xml"/>
        <xi:include href="https://xml2rfc.tools.ietf.org/public/rfc/bibxml/reference.RFC.8340.xml"/>
        <xi:include href="https://xml2rfc.tools.ietf.org/public/rfc/bibxml/reference.RFC.8342.xml"/>
      </references>
      <references>
        <name>Informative References</name>
        <xi:include href="https://xml2rfc.tools.ietf.org/public/rfc/bibxml/reference.RFC.3688.xml"/>
        <xi:include href="https://xml2rfc.tools.ietf.org/public/rfc/bibxml/reference.RFC.6020.xml"/>
      </references>
    </references>
    <section anchor="examples" numbered="true" toc="default">
      <name>Examples</name>
      <section anchor="structure-example" numbered="true" toc="default">
        <name>"structure" Example</name>
        <t>
This example shows a simple address book that could be stored as an
artifact.
        </t>
        <artwork name="" type="" align="left" alt=""><![CDATA[
module example-module {
  yang-version 1.1;
  namespace "urn:example:example-module";
  prefix exm;

  import ietf-yang-structure-ext {
    prefix sx;
  }

  sx:structure address-book {
    list address {
      key "last first";
      leaf last {
        type string;
        description "Last name";
      }
      leaf first {
        type string;
        description "First name";
      }
      leaf street {
        type string;
        description "Street name";
      }
      leaf city {
        type string;
        description "City name";
      }
      leaf state {
        type string;
        description "State name";
      }
    }
  }
}

      ]]></artwork>
        <t>
Below is the tree diagram of this module.
        </t>
        <artwork name="" type="" align="left" alt=""><![CDATA[
module: example-module

  structure address-book:
    +-- address* [last first]
       +-- last      string
       +-- first     string
       +-- street?   string
       +-- city?     string
       +-- state?    string
      ]]></artwork>
      </section>
      <section anchor="augment-structure-example" numbered="true" toc="default">
        <name>"augment&nbhy;structure" Example</name>
        <t>
This example adds "county" and "zipcode" leafs to the address book:
        </t>
        <artwork name="" type="" align="left" alt=""><![CDATA[
module example-module-aug {
  yang-version 1.1;
  namespace "urn:example:example-module-aug";
  prefix exma;

  import ietf-yang-structure-ext {
    prefix sx;
  }
  import example-module {
    prefix exm;
  }

  sx:augment-structure "/exm:address-book/exm:address" {
    leaf county {
      type string;
      description "County name";
    }
    leaf zipcode {
      type string;
      description "Postal zipcode";
    }
  }
}

      ]]></artwork>
        <t>
Below is the tree diagram of this module.
        </t>
        <artwork name="" type="" align="left" alt=""><![CDATA[
module: example-module-aug

  augment-structure /exm:address-book/exm:address:
    +-- county?    string
    +-- zipcode?   string
      ]]></artwork>
      </section>
      <section anchor="xml-encoding-example" numbered="true" toc="default">
        <name>XML Encoding Example</name>
        <t>
This example shows how an address book can be encoded in XML:
        </t>
        <artwork name="" type="" align="left" alt=""><![CDATA[
<address-book xmlns="urn:example:example-module">
  <address>
    <last>Flintstone</last>
    <first>Fred</first>
    <street>301 Cobblestone Way</street>
    <city>Bedrock</city>
    <zipcode xmlns="urn:example:example-module-aug">70777</zipcode>
  </address>
  <address>
    <last>Root</last>
    <first>Charlie</first>
    <street>4711 Cobblestone Way</street>
    <city>Bedrock</city>
    <zipcode xmlns="urn:example:example-module-aug">70777</zipcode>
  </address>
</address-book>
      ]]></artwork>
      </section>
      <section anchor="json-encoding-example" numbered="true" toc="default">
        <name>JSON Encoding Example</name>
        <t>
This example shows how an address book can be encoded in JSON:
        </t>
        <artwork name="" type="" align="left" alt=""><![CDATA[
"example-module:address-book": {
  "address": [
    {
      "city": "Bedrock",
      "example-module-aug:zipcode": "70777",
      "first": "Fred",
      "last": "Flintstone",
      "street": "301 Cobblestone Way"
    },
    {
      "city": "Bedrock",
      "example-module-aug:zipcode": "70777",
      "first": "Charlie",
      "last": "Root",
      "street": "4711 Cobblestone Way"
    }
  ]
}
      ]]></artwork>
      </section>
      <section anchor="structure-example-that-defines-a-non-top-level-structure" numbered="true" toc="default">
        <name>"structure" example that defines a non-top-level structure</name>
        <t>
The following example defines a data structure with error information,
that can be included in an &lt;error&nbhy;info&gt; element in an &lt;rpc&nbhy;error&gt;.
        </t>
        <artwork name="" type="" align="left" alt=""><![CDATA[
module example-error-info {
  yang-version 1.1;
  namespace "urn:example:example-error-info";
  prefix exei;

  import ietf-yang-structure-ext {
    prefix sx;
  }

  sx:structure my-example-error-info {
    leaf error-code {
      type uint32;
    }
  }

}
      ]]></artwork>
        <t>
The example below shows how this structure can be used in an
&lt;rpc&nbhy;error&gt;.
        </t>
        <artwork name="" type="" align="left" alt=""><![CDATA[
<rpc-reply message-id="101"
     xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
  <rpc-error>
    <error-type>protocol</error-type>
    <error-tag>operation-failed</error-tag>
    <error-severity>error</error-severity>
    <error-info>
      <my-example-error-info
          xmlns="urn:example:example-error-info">
        <error-code>42</error-code>
      </my-example-error-info>
    </error-info>
  </rpc-error>
</rpc-reply>
      ]]></artwork>
      </section>
    </section>
  </back>
</rfc>
