One document matched: draft-lhotka-netmod-yang-json-00.xml


<?xml version="1.0"?>
<!DOCTYPE rfc SYSTEM "rfc2629.dtd" [
<!ENTITY ex-module SYSTEM "ex-json.yang.aw">
<!ENTITY ex-json SYSTEM "ex-json-config.json.aw">
]>
<?rfc toc="yes"?>
<?rfc symrefs="yes"?>
<?rfc sortrefs="yes"?>
<rfc ipr="trust200902" category="std" docName="draft-lhotka-netmod-yang-json-00">

<front>
  <title abbrev="Modeling JSON with YANG">Modeling JSON Text with
  YANG</title>

  <author initials="L." surname="Lhotka" fullname="Ladislav Lhotka">
    <organization>CZ.NIC</organization>
    <address>
      <email>lhotka@nic.cz</email>
    </address>
  </author>
  <date day="05" month="October" year="2012"/>
  <area>Operations and Management</area>
   <workgroup>NETMOD</workgroup>
  <abstract>
    <t>This document defines rules for mapping data models expressed
    in YANG to configuration and operational state data encoded as
    JSON text. It does so by specifying a procedure for translating
    the subset of YANG-compatible XML documents to JSON text, and vice
    versa.</t>
  </abstract>
</front>

<middle>

  <section anchor="sec.introduction" title="Introduction">
    <t>The aim of this document is define rules for mapping data
    models expressed in the YANG data modeling language <xref target="RFC6020"/> to configuration and operational state data
    encoded as JavaScript Object Notation (JSON) text <xref target="RFC4627"/>. The result can be potentially applied in two
    different ways:
    <list style="numbers">
      <t>JSON may be used instead of the standard XML <xref target="XML"/> encoding in the context of the NETCONF
      protocol <xref target="RFC6241"/> and/or with existing data
      models expressed in YANG. An example application is the YANG-API
      Protocol <xref target="YANG-API"/>.</t>
      <t>Other documents that choose JSON to represent structured data
      can use YANG for defining the data model, i.e., both syntactic
      and semantic constraints that the data have to satisfy.</t>
    </list></t>

    <t>JSON mapping rules could be specified in a similar way as the
    XML mapping rules in <xref target="RFC6020"/>. This would however
    require solving several problems. To begin with, YANG uses XPath
    <xref target="XPath"/> quite extensively, but XPath is not defined
    for JSON and such a definition would be far from
    straightforward.</t>
    <t>In order to avoid these technical difficulties, this document
    employs an alternative approach: it defines a relatively simple
    procedure which allows to translate the subset of XML that can be
    modeled using YANG to JSON, and vice versa. Consequently,
    validation of a JSON text against a data model can done by
    translating the JSON text to XML, which is then validated
    according to the rules stated in <xref target="RFC6020"/>.</t>
    <t>The translation procedure is adapted to YANG specifics and
    requirements, namely:
    <list style="numbers">
      <t>The translation is driven by a concrete YANG data model and
      uses information about data types to achieve better results than
      generic XML-JSON translation procedures.</t>
      <t>Various document types are supported, namely configuration
      data, configuration + state data, RPC input and output
      parameters, and notifications.</t>
      <t>XML namespaces specified in the data model are mapped to
      namespaces of JSON objects. However, explicit namespace
      identifiers are rarely needed in JSON text.</t>
      <t>Translation of XML attributes, mixed content, comments and
      processing instructions is not supported.</t>
    </list></t>
  </section>

  <section anchor="sec.term-not" title="Terminology and Notation">
    
    <t>The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL
    NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL"
    in this document are to be interpreted as described in <xref target="RFC2119"/>.</t>

    <t>The following terms are defined in <xref target="RFC6020"/>:
    <list style="symbols">
      <t>anyxml</t>
      <t>augment</t>
      <t>container</t>
      <t>data model</t>
      <t>data node</t>
      <t>data tree</t>
      <t>datatype</t>
      <t>feature</t>
      <t>identity</t>
      <t>instance identifier</t>
      <t>leaf</t>
      <t>leaf-list</t>
      <t>list</t>
      <t>module</t>
      <t>submodule</t>
    </list></t>

    <t>The following terms are defined in <xref target="XMLNS"/>:
    <list style="symbols">
      <t>local name</t>
      <t>prefixed name</t>
      <t>qualified name</t>
    </list></t>

  </section>

  <section anchor="sec.specification" title="Specification of the Translation Procedure">
    <t>The translation procedure defines a 1-1 correspondence
    between the subset of YANG-compatible XML documents and JSON
    text. This means that the translation can be applied in both
    directions and is always invertible.</t>
    <t>Any YANG-compatible XML document can be translated, except
    documents with mixed content. This is only a minor limitation
    since mixed content is marginal in YANG - it is allowed only in
    "anyxml" nodes.</t>
    <t>The following subsections specify rules mainly for translating
    XML documents to JSON text. Rules for the inverse translation are
    stated only where necessary, otherwise they can be easily
    inferred.</t>
    <t>REQUIRED parameters of the translation procedure are:
    <list style="symbols">
      <t>YANG data model,</t>
      <t>type of the input XML document,</t>
      <t>optional features (defined via the "feature" statement) that
      are considered active.</t>
    </list></t>
    <t>The permissible types of XML documents are listed in <xref target="tab.doctypes"/> together with the corresponding part of
    the data model that is used for the translation.
    </t>

    <texttable anchor="tab.doctypes" title="YANG Document Types">
      <ttcol>Document Type</ttcol>
      <ttcol>Data Model Section</ttcol>
      <c>configuration and state data</c><c>main data tree</c>
      <c>configuration</c><c>main data tree ("config true")</c>
      <c>RPC input parameters</c><c>"input" nodes under "rpc"</c>
      <c>RPC output parameters</c><c>"output" nodes under "rpc"</c>
      <c>notification</c><c>"notification" nodes</c>
    </texttable>

    <t>A particular application may decide to use only a subset of
    document types from <xref target="tab.doctypes"/>. For instance,
    YANG-API Protocol <xref target="YANG-API"/> does not use
    notifications.</t>

    <t>XML documents can be translated to JSON text only if they are
    valid instances of the YANG data model and selected document type,
    also taking into account the active features, if there are
    any.</t>

    <section anchor="sec.names-ns" title="Names and Namespaces">
      <t>The local part of a JSON name is always identical to the
      local name of the corresponding XML element.</t>
      <t>Each JSON name lives in a namespace which is uniquely
      identified by the name of the YANG module where the
      corresponding data node is defined. If the data node is defined
      in a submodule, then the namespace identifier is the name of the
      main module to which the submodule belongs. The translation
      procedure MUST correctly map YANG namespace URIs to YANG module
      names and vice versa.</t>
      <t>The namespace SHALL be expressed in JSON text by
      prefixing the local name in the following way:</t>

      <figure anchor="fig.ns-enc" title="Encoding a namespace identifier with a local name.">
        <artwork><![CDATA[
        <module name>:<local name>]]></artwork>
      </figure>

      <t>The namespace identifier MUST be used for local names that
      are ambiguous, i.e., whenever the data model permits a sibling
      node with the same local name. Otherwise, the namespace
      identifier is OPTIONAL.</t>
      <t>When mapping namespaces from JSON text to XML, the resulting
      XML document may use default namespace declarations (via the
      "xmlns" attribute), prefix-based namespace declarations (via
      attributes beginning with "xmlns:"), or any combination thereof
      following the rules stated in <xref target="XMLNS"/>. If
      prefixed names are used, their prefix SHOULD be the one defined
      by the "prefix" statement in the YANG module where each data
      node is defined.</t>

    </section>

    <section anchor="sec.elements" title="Mapping XML Elements to JSON Objects">
      <t>XML elements are translated to JSON objects in a straightforward way:
      <list style="symbols">
        <t>An XML element which is modeled as YANG leaf is translated
        to a name/value pair and the JSON datatype of the value is
        derived from the YANG datatype of the leaf (see <xref target="sec.datatypes"/> for the datatype mapping rules).</t>
        <t>An XML element which is modeled as YANG container is
        translated to a JSON object.</t>
        <t>A sequence of one or more sibling XML elements with the
        same qualified name, which is modeled as YANG list or
        leaf-list, is translated to a name/array pair. If the sequence
        is modeled as a leaf-list in YANG, then the array elements are
        primitive values whose type depends on the datatype of the
        leaf-list (see <xref target="sec.datatypes"/>). If the
        sequence is modeled as a list in YANG, then the array elements
        are JSON objects.</t>
      </list></t>

      <t>Note that the same XML element may be translated in different
      ways, depending on the YANG data model. For example,

      <figure>
        <artwork>
    <![CDATA[<foo>42</foo>]]>
        </artwork>
      </figure>

      is translated to

      <figure>
        <artwork>
    <![CDATA["foo": 42]]>
        </artwork>
      </figure>

      if the "foo" node is defined as a leaf with the "uint8"
      datatype, or to

      <figure>
        <artwork>
    <![CDATA["foo": ["42"]]]>
        </artwork>
      </figure>

      if the "foo" node is defined as a leaf-list with the "string"
      datatype.</t>

    </section>

    <section anchor="sec.datatypes" title="Mapping YANG Datatypes to JSON Values">

      <section anchor="sec.numtypes" title="Numeric Types">
        <t>A value of one of the YANG numeric types ("int8", "int16",
        "int32", "int64", "uint8", "uint16", "uint32", "uint64" and
        "decimal64") is mapped to a JSON number using the same lexical
        representation.</t>
      </section>

      <section anchor="sec.string" title="The "string" Type">
        <t>A "string" value is mapped to an identical JSON string, subject
        to JSON encoding rules.</t>
      </section>

      <section anchor="sec.boolean" title="The "boolean" Type">
        <t>A "boolean" value is mapped to the corresponding JSON value
        'true' or 'false'.</t>
      </section>

      <section anchor="sec.enum" title="The "enumeration" Type">
        <t>An "enumeration" value is mapped in the same way as a string
        except that the permitted values are defined by "enum"
        statements in YANG.</t>
      </section>

      <section anchor="sec.bits" title="The "bits" Type">
        <t>A "bits" value is mapped to a string identical to the lexical
        representation of this value in XML, i.e., space-separated
        names representing the individual bit values that are set.</t>
      </section>

      <section anchor="sec.binary" title="The "binary" Type">
        <t>A "binary" value is mapped to a JSON string identical to the
        lexical representation of this value in XML, i.e.,
        base64-encoded binary data.</t> 
      </section>

      <section anchor="sec.leafref" title="The "leafref" Type">
        <t>A "leafref" value is mapped according to the same rules as
        the type of the leaf being referred to.</t>
      </section>

      <section anchor="sec.idref" title="The "identityref" Type">
        <t>An "identityref" value is mapped to a string representing the
        qualified name of the identity. Its namespace MAY be expressed
        as shown in <xref target="fig.ns-enc"/>. If the namespace part
        is not present, the namespace of the name of the JSON object
        containing the value is assumed.</t>
      </section>

      <section anchor="sec.empty" title="The "empty" Type">
        <t>An "empty" value is mapped to '[null]', i.e., an array with
        the 'null' value being its only element.</t>
        <t>This representation was chosen instead of using simply
        'null' in order to facilitate the use of empty leafs in common
        programming languages. When used in a boolean context, the
        '[null]' value, unlike 'null', evaluates to 'true'.</t>
      </section>

      <section anchor="sec.union" title="The "union" Type">
        <t>YANG "union" type represents a choice among multiple
        alternative types. The actual type of the XML value MUST be
        determined using the procedure specified in Sec. 9.12 of <xref target="RFC6020"/> and the mapping rules for that type are
        used.</t>
      </section>

      <section anchor="sec.i-i" title="The "instance-identifier" Type">
        <t>An "instance-identifier" value is a string representing a
        simplified XPath specification. It is mapped to an analogical
        JSON string in which all occurrences of XML namespace prefixes are
        either removed or replaced with the corresponding module name
        according to the rules of <xref target="sec.names-ns"/>.</t>
        <t>When translating such a value from JSON to XML, all
        components of the instance-identifier MUST be given
        appropriate XML namespace prefixes. It is RECOMMENDED that
        these prefixes be those defined via the "prefix" statement in
        the corresponding YANG modules.</t>
      </section>

    </section>

    <section anchor="sec.ex" title="Example">
      <t>Consider a simple data model defined by the following YANG
      module:</t>

      <figure anchor="fig.exy" title="Example YANG module.">
        <artwork><![CDATA[
module ex-json {

  namespace "http://example.com/ex-json";

  prefix "ej";

  import ietf-inet-types {
    prefix "inet";
  }

  container top {
    list address {
      key "seqno";
      leaf seqno {
        type uint8;
      }
      leaf ip {
        type inet:ip-address;
        mandatory "true";
      }
    }
    container phases {
      typedef angle {
        type decimal64 {
          fraction-digits "2";
        }
        units "radians";
      }
      leaf max-phase {
        default "6.28";
        type angle;
      }
      leaf-list phase {
        type angle;
        must ". <= ../max-phase";
        min-elements "1";
      }
    }
  }
}
]]></artwork>

      </figure>

      <t>By using the translation procedure defined in this document,
      we can conclude that the following JSON text is valid according
      to the data model:</t>

      <figure anchor="fig.exj" title="Example JSON text.">
        <artwork><![CDATA[
{
  "top": {
    "address": [
      {
        "seqno": 1,
        "ip": "192.0.2.1"
      },
      {
        "seqno": 2,
        "ip": "2001:db8:0:1::1"
      }
    ],
    "phases": {
      "phase": [
        "0.79",
        "1.04",
        "3.14"
      ]
    }
  }
}
]]></artwork>

      </figure>

    </section>

    <section anchor="sec.iana" title="IANA Considerations">
      <t>TBD.</t>
    </section>

    <section anchor="sec.cons" title="Security Considerations">
      <t>TBD.</t>
    </section>

    <section anchor="sec.ack" title="Acknowledgments">
    <t>The author wishes to thank Andy Bierman, Martin Bjorklund and
    Phil Shafer for their helpful comments and suggestions.</t>
    </section>

  </section>
</middle>

<back>

  <references title="Normative References">

    <reference anchor="RFC2119">
      <front>
        <title abbrev="RFC Key Words">Key words for use in RFCs to
        Indicate Requirement Levels</title>
        <author initials="S." surname="Bradner" fullname="Scott Bradner">
          <organization/>
        </author>
        <date year="1997" month="March"/>
      </front>
      <seriesInfo name="BCP" value="14"/>
      <seriesInfo name="RFC" value="2119"/>
      <format type="HTML" target="http://www.ietf.org/html/rfc2119"/>
    </reference>

    <reference anchor="RFC4627">
      <front>
        <title>The application/json Media Type for JavaScript Object
        Notation (JSON)</title>
        <author initials="D." surname="Crockford" fullname="Douglas Crockford">
          <organization/>
        </author>
        <date month="July" year="2006"/>
      </front>
      <seriesInfo name="RFC" value="4627"/>
      <format type="HTML" target="http://www.ietf.org/html/rfc4627"/>
    </reference>

    <reference anchor="RFC6020">
      <front>
        <title>YANG - A Data Modeling Language for Network
        Configuration Protocol (NETCONF)</title>
        <author role="editor" initials="M" surname="Bjorklund" fullname="Martin Bjorklund">
          <organization>Tail-f Systems</organization>
        </author>
        <date month="September" year="2010"/>
      </front>
      <seriesInfo name="RFC" value="6020"/>
      <format type="HTML" target="http://tools.ietf.org/html/rfc6020"/>
    </reference>

    <reference anchor="RFC6241">
      <front>
        <title>NETCONF Configuration Protocol</title>
        <author initials="R." surname="Enns" fullname="R. Enns">
          <organization/>
        </author>
        <author initials="M." surname="Bjorklund" fullname="M. Bjorklund">
          <organization/>
        </author>
        <author initials="J." surname="Schoenwaelder" fullname="J. Schoenwaelder">
          <organization/>
        </author>
        <author initials="A." surname="Bierman" fullname="A. Bierman">
          <organization/>
        </author>
        <date year="2011" month="June"/>
      </front>
      <seriesInfo name="RFC" value="6241"/>
      <format type="HTML" target="http://www.ietf.org/html/rfc6241"/>
    </reference>

    <reference anchor="XML" target="http://www.w3.org/TR/2006/REC-xml-20060816">
      <front>
        <title>Extensible Markup Language (XML) 1.0 (Fifth Edition)</title>
        <author initials="T." surname="Bray" fullname="Tim Bray">
          <organization/>
        </author>
        <author initials="J." surname="Paoli" fullname="Jean Paoli">
          <organization/>
        </author>
        <author initials="C." surname="Sperberg-McQueen" fullname="C. M. Sperberg-McQueen">
          <organization/>
        </author>
        <author initials="E." surname="Maler" fullname="Eve Maler">
          <organization/>
        </author>
        <author initials="F." surname="Yergeau" fullname="Francois Yergeau">
          <organization/>
        </author>
        <date month="November" day="26" year="2008"/>
      </front>
      <seriesInfo name="World Wide Web Consortium Recommendation" value="REC-xml-20081126"/>
      <format type="HTML" target="http://www.w3.org/TR/2008/REC-xml-20081126"/>
    </reference>

    <reference anchor="XMLNS" target="http://www.w3.org/TR/2009/REC-xml-names-20091208">
      <front>
        <title>Namespaces in XML 1.0 (Third Edition)</title>
        <author initials="T." surname="Bray" fullname="Tim Bray">
          <organization/>
        </author>
        <author initials="D." surname="Hollander" fullname="Dave Hollander">
          <organization/>
        </author>
        <author initials="A." surname="Layman" fullname="Andrew Layman">
          <organization/>
        </author>
        <author initials="R." surname="Tobin" fullname="Richard Tobin">
          <organization/>
        </author>
        <author initials="H. S." surname="Thompson" fullname="Henry S. Thompson">
          <organization/>
        </author>
        <date month="December" day="8" year="2009"/>
      </front>
      <seriesInfo name="World Wide Web Consortium Recommendation" value="REC-xml-names-20091208"/>
      <format type="HTML" target="http://www.w3.org/TR/2009/REC-xml-names-20091208"/>
    </reference>

  </references>

  <references title="Informative References">

    <reference anchor="XPath" target="http://www.w3.org/TR/1999/REC-xpath-19991116">
      <front>
        <title>XML Path Language (XPath) Version 1.0</title>
        <author initials="J." surname="Clark" fullname="James Clark">
          <organization/>
        </author>
        <date month="November" day="16" year="1999"/>
      </front>
      <seriesInfo name="World Wide Web Consortium Recommendation" value="REC-xpath-19991116"/>
      <format type="HTML" target="http://www.w3.org/TR/1999/REC-xpath-19991116"/>
    </reference>

    <reference anchor="YANG-API">
      <front>
        <title>YANG-API Protocol</title>
        <author initials="A." surname="Bierman" fullname="A. Bierman">
          <organization/>
        </author>
        <author initials="M." surname="Bjorklund" fullname="M. Bjorklund">
          <organization/>
        </author>
        <date month="May" day="20" year="2012"/>
      </front>
      <seriesInfo name="Internet-Draft" value="draft-bierman-netconf-yang-api-00"/>
      <format type="HTML" target="http://tools.ietf.org/html/draft-bierman-netconf-yang-api-00"/>
    </reference>


  </references>

</back>

</rfc>

PAFTECH AB 2003-20262026-04-23 14:30:39