draft-ietf-netmod-yang-json-00.txt   draft-ietf-netmod-yang-json-01.txt 
NETMOD L. Lhotka NETMOD Working Group L. Lhotka
Internet-Draft CZ.NIC Internet-Draft CZ.NIC
Intended status: Standards Track April 21, 2014 Intended status: Standards Track October 13, 2014
Expires: October 23, 2014 Expires: April 16, 2015
JSON Encoding of Data Modeled with YANG JSON Encoding of Data Modeled with YANG
draft-ietf-netmod-yang-json-00 draft-ietf-netmod-yang-json-01
Abstract Abstract
This document defines rules for representing configuration and state This document defines encoding rules for representing configuration,
data defined using YANG as JSON text. It does so by specifying a state data, RPC input and output parameters, and notifications
procedure for translating the subset of YANG-compatible XML documents defined using YANG as JavaScript Object Notation (JSON) text.
to JSON text, and vice versa. A JSON encoding of XML attributes is
also defined so as to allow for including metadata in JSON documents.
Status of This Memo Status of This Memo
This Internet-Draft is submitted in full conformance with the This Internet-Draft is submitted in full conformance with the
provisions of BCP 78 and BCP 79. provisions of BCP 78 and BCP 79.
Internet-Drafts are working documents of the Internet Engineering Internet-Drafts are working documents of the Internet Engineering
Task Force (IETF). Note that other groups may also distribute Task Force (IETF). Note that other groups may also distribute
working documents as Internet-Drafts. The list of current Internet- working documents as Internet-Drafts. The list of current Internet-
Drafts is at http://datatracker.ietf.org/drafts/current/. Drafts is at http://datatracker.ietf.org/drafts/current/.
Internet-Drafts are draft documents valid for a maximum of six months Internet-Drafts are draft documents valid for a maximum of six months
and may be updated, replaced, or obsoleted by other documents at any and may be updated, replaced, or obsoleted by other documents at any
time. It is inappropriate to use Internet-Drafts as reference time. It is inappropriate to use Internet-Drafts as reference
material or to cite them other than as "work in progress." material or to cite them other than as "work in progress."
This Internet-Draft will expire on October 23, 2014. This Internet-Draft will expire on April 16, 2015.
Copyright Notice Copyright Notice
Copyright (c) 2014 IETF Trust and the persons identified as the Copyright (c) 2014 IETF Trust and the persons identified as the
document authors. All rights reserved. document authors. All rights reserved.
This document is subject to BCP 78 and the IETF Trust's Legal This document is subject to BCP 78 and the IETF Trust's Legal
Provisions Relating to IETF Documents Provisions Relating to IETF Documents
(http://trustee.ietf.org/license-info) in effect on the date of (http://trustee.ietf.org/license-info) in effect on the date of
publication of this document. Please review these documents publication of this document. Please review these documents
carefully, as they describe your rights and restrictions with respect carefully, as they describe your rights and restrictions with respect
to this document. Code Components extracted from this document must to this document. Code Components extracted from this document must
include Simplified BSD License text as described in Section 4.e of include Simplified BSD License text as described in Section 4.e of
the Trust Legal Provisions and are provided without warranty as the Trust Legal Provisions and are provided without warranty as
described in the Simplified BSD License. described in the Simplified BSD License.
Table of Contents Table of Contents
1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . 2 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . 2
2. Terminology and Notation . . . . . . . . . . . . . . . . . . 4 2. Terminology and Notation . . . . . . . . . . . . . . . . . . 3
3. Specification of the Translation Procedure . . . . . . . . . 5 3. Validation of JSON-encoded
3.1. Names and Namespaces . . . . . . . . . . . . . . . . . . 6 Instance Data . . . . . . . . . . . . . . . . . . . . . . . . 3
3.2. Mapping XML Elements to JSON Objects . . . . . . . . . . 8 4. Names and Namespaces . . . . . . . . . . . . . . . . . . . . 4
3.2.1. The "leaf" Data Node . . . . . . . . . . . . . . . . 8 5. Encoding of YANG Data Node Instances . . . . . . . . . . . . 6
3.2.2. The "container" Data Node . . . . . . . . . . . . . . 8 5.1. The "leaf" Data Node . . . . . . . . . . . . . . . . . . 6
3.2.3. The "leaf-list" Data Node . . . . . . . . . . . . . . 9 5.2. The "container" Data Node . . . . . . . . . . . . . . . . 7
3.2.4. The "list" Data Node . . . . . . . . . . . . . . . . 9 5.3. The "leaf-list" Data Node . . . . . . . . . . . . . . . . 7
3.2.5. The "anyxml" Data Node . . . . . . . . . . . . . . . 10 5.4. The "list" Data Node . . . . . . . . . . . . . . . . . . 7
3.3. Mapping YANG Datatypes to JSON Values . . . . . . . . . . 11 5.5. The "anyxml" Data Node . . . . . . . . . . . . . . . . . 8
3.3.1. Numeric Datatypes . . . . . . . . . . . . . . . . . . 11 6. The Mapping of YANG Datatypes to JSON Values . . . . . . . . 8
3.3.2. The "string" Type . . . . . . . . . . . . . . . . . . 11 6.1. Numeric Datatypes . . . . . . . . . . . . . . . . . . . . 9
3.3.3. The "boolean" Type . . . . . . . . . . . . . . . . . 11 6.2. The "string" Type . . . . . . . . . . . . . . . . . . . . 9
3.3.4. The "enumeration" Type . . . . . . . . . . . . . . . 11 6.3. The "boolean" Type . . . . . . . . . . . . . . . . . . . 9
3.3.5. The "bits" Type . . . . . . . . . . . . . . . . . . . 12 6.4. The "enumeration" Type . . . . . . . . . . . . . . . . . 9
3.3.6. The "binary" Type . . . . . . . . . . . . . . . . . . 12 6.5. The "bits" Type . . . . . . . . . . . . . . . . . . . . . 9
3.3.7. The "leafref" Type . . . . . . . . . . . . . . . . . 12 6.6. The "binary" Type . . . . . . . . . . . . . . . . . . . . 9
3.3.8. The "identityref" Type . . . . . . . . . . . . . . . 12 6.7. The "leafref" Type . . . . . . . . . . . . . . . . . . . 10
3.3.9. The "empty" Type . . . . . . . . . . . . . . . . . . 12 6.8. The "identityref" Type . . . . . . . . . . . . . . . . . 10
3.3.10. The "union" Type . . . . . . . . . . . . . . . . . . 13 6.9. The "empty" Type . . . . . . . . . . . . . . . . . . . . 10
3.3.11. The "instance-identifier" Type . . . . . . . . . . . 13 6.10. The "union" Type . . . . . . . . . . . . . . . . . . . . 11
4. Encoding Metadata in JSON . . . . . . . . . . . . . . . . . . 14 6.11. The "instance-identifier" Type . . . . . . . . . . . . . 11
5. IANA Considerations . . . . . . . . . . . . . . . . . . . . . 16 7. I-JSON Compliance . . . . . . . . . . . . . . . . . . . . . . 12
6. Security Considerations . . . . . . . . . . . . . . . . . . . 16 8. Security Considerations . . . . . . . . . . . . . . . . . . . 13
7. Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . 17 9. Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . 13
8. References . . . . . . . . . . . . . . . . . . . . . . . . . 17 10. References . . . . . . . . . . . . . . . . . . . . . . . . . 13
8.1. Normative References . . . . . . . . . . . . . . . . . . 17 10.1. Normative References . . . . . . . . . . . . . . . . . . 13
8.2. Informative References . . . . . . . . . . . . . . . . . 17 10.2. Informative References . . . . . . . . . . . . . . . . . 14
Appendix A. A Complete Example . . . . . . . . . . . . . . . . . 18 Appendix A. A Complete Example . . . . . . . . . . . . . . . . . 14
Author's Address . . . . . . . . . . . . . . . . . . . . . . . . 20 Appendix B. Change Log . . . . . . . . . . . . . . . . . . . . . 16
B.1. Changes Between Revisions -00 and -01 . . . . . . . . . . 16
Author's Address . . . . . . . . . . . . . . . . . . . . . . . . 17
1. Introduction 1. Introduction
The aim of this document is define rules for representing The NETCONF protocol [RFC6241] uses XML [W3C.REC-xml-20081126] for
configuration and state data defined using the YANG data modeling encoding data in its Content Layer. Other management protocols might
language [RFC6020] as JavaScript Object Notation (JSON) want to use other encodings while still benefiting from using YANG
text [RFC7159]. The result can be potentially applied in two [RFC6020] as the data modeling language.
different ways:
1. JSON may be used instead of the standard XML [XML] encoding in
the context of the NETCONF protocol [RFC6241] and/or with
existing data models expressed in YANG. An example application
is the RESTCONF Protocol [RESTCONF].
2. 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.
JSON mapping rules could be specified in a similar way as the XML
mapping rules in [RFC6020]. This would however require solving
several problems. To begin with, YANG uses XPath [XPath] quite
extensively, but XPath is not defined for JSON and such a definition
would be far from straightforward.
In order to avoid these technical difficulties, this document employs
an alternative approach: it defines a relatively simple procedure
which allows for translating 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
[RFC6020].
The translation procedure is adapted to YANG specifics and
requirements, namely:
1. 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.
2. Various document types are supported, namely configuration data,
configuration + state data, RPC input and output parameters, and
notifications.
3. XML namespaces specified in the data model are mapped to
namespaces of JSON objects. However, explicit namespace
identifiers are rarely needed in JSON text.
4. Section 4 defines JSON encoding of XML attributes. Although XML
attributes cannot be modeled with YANG, they are often used for
attaching metadata to elements, and a standard JSON encoding is
therefore needed.
5. Translation of XML mixed content, comments and processing
instructions is outside the scope of this document.
Item 1 above also means that, depending on the data model, the same
XML element can be translated to different JSON objects. For
example,
<foo>123</foo>
is translated to
"foo": 123
if the "foo" node is defined as a leaf with the "uint8" datatype, or For example, the RESTCONF protocol [I-D.ietf-netconf-restconf]
to supports two encodings: XML (media type "application/yang.data+xml")
and JSON (media type "application/yang.data+json).
"foo": ["123"] The specification of the YANG data modelling language [RFC6020]
defines only XML encoding for data instances, i.e. contents of
configuration datastores, state data, RFC input and output
parameters, and event notifications. The aim of this document is to
define rules for encoding the same data as JavaScript Object Notation
(JSON) text [RFC7159].
if the "foo" node is defined as a leaf-list with the "string" In order to achieve maximum interoperability while allowing
datatype, and the <foo> element has no siblings of the same name. implementations to use a variety of available JSON parsers, the JSON
encoding rules follow, as much as possible, the constraints of the
I-JSON restricted profile [I-D.ietf-json-i-json]. Section Section 7
discusses I-JSON conformance in more detail.
2. Terminology and Notation 2. Terminology and Notation
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
"SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this
document are to be interpreted as described in [RFC2119]. document are to be interpreted as described in [RFC2119].
The following terms are defined in [RFC6020]: The following terms are defined in [RFC6020]:
o anyxml o anyxml
o augment o augment
o container o container
o data node o data node
o data tree
o datatype
o feature
o identity o identity
o instance identifier o instance identifier
o leaf o leaf
o leaf-list o leaf-list
o list o list
o module o module
o submodule o submodule
The following terms are defined in [XMLNS]: 3. Validation of JSON-encoded Instance Data
o local name
o prefixed name
o qualified name
3. Specification of the Translation Procedure
The translation procedure defines a 1-1 correspondence between the Instance data validation as defined in [RFC6020] is only applicable
subset of YANG-compatible XML documents and JSON text. This means to XML-encoded data. For one, semantic constraints in "must"
that the translation can be applied in both directions and it is statements are expressed using XPath 1.0 [W3C.REC-xpath-19991116],
always invertible. which can be properly interpreted only in the XML context.
The translation procedure is applicable only to data hierarchies that This document along with the corresponding "XML Mapping Rules"
are modelled by a YANG data model. An input XML document MAY contain sections from [RFC6020] also define an implicit schema-driven mapping
enclosing elements representing NETCONF "Operations" and "Messages" of JSON-encoded instances to XML-encoded instances (and vice versa).
layers. However, these enclosing elements do not appear in the This mapping is mostly straightforward. In cases where doubts could
resulting JSON document. arise, this document gives explicit instructions for mapping JSON-
encoded instances to XML.
Any YANG-compatible XML document can be translated, except documents In order to validate a JSON instance document, it MUST first be
with mixed content. This is only a minor limitation since mixed mapped, at least conceptually, to the corresponding XML instance
content is marginal in YANG - it is allowed only in anyxml data document. By definition, the JSON document is then valid if and only
nodes. if the XML document is valid according to the rules stated in
[RFC6020].
The following sections specify rules mainly for translating XML 4. Names and Namespaces
documents to JSON text. Rules for the inverse translation are stated
only where necessary, otherwise they can be easily inferred.
REQUIRED parameters of the translation procedure are: Instances of YANG data nodes (leafs, containers, leaf-lists, lists
and anyxml nodes) are always encoded as members of a JSON object,
i.e., as name/value pairs. This section defines how the name part is
formed, and the following sections deal with the value part.
o YANG data model consisting of a set of YANG modules, Except in the cases specified below, the member name is identical to
the identifier of the corresponding YANG data node. Every such name
belongs to a namespace which is associated with the YANG module where
the corresponding data node is defined. If the data node is defined
in a submodule, then the namespace is determined by the main module
to which the submodule belongs.
o type of the input document, If the namespace of a member name has to be explicitly specified, the
module name SHALL be used as a prefix to the (local) member name.
Both parts of the member name SHALL be separated with a colon
character (":"). In other words, the namespace-qualified name will
have the following form:
o optional features (defined via the "feature" statement) that are <module name>:<local name>
considered active.
The permissible types of input documents are listed in Table 1 Figure 1: Encoding a namespace identifier with a local name.
together with the corresponding part of the data model that is used
for the translation.
+------------------------------+---------------------------------+ Names with namespace identifiers in the form shown in Figure 1 MUST
| Document Type | Data Model Section | be used for all top-level YANG data nodes, and also for all nodes
+------------------------------+---------------------------------+ whose parent node belongs to a different namespace. Otherwise, names
| configuration and state data | main data tree | with namespace identifiers MUST NOT be used.
| | |
| configuration | main data tree ("config true") |
| | |
| RPC input parameters | "input" data nodes under "rpc" |
| | |
| RPC output parameters | "output" data nodes under "rpc" |
| | |
| notification | "notification" data nodes |
+------------------------------+---------------------------------+
Table 1: YANG Document Types For example, consider the following YANG module:
When translating XML to JSON, the type of the input document can module foomod {
often be determined form the encapsulating elements belonging to the
"Operations" or "Messages" layer as defined by the NETCONF protocol
(see Sec. 1.2 in [RFC6241]).
A particular application MAY decide to support only a subset of namespace "http://example.com/foomod";
document types from Table 1.
XML documents can be translated to JSON text only if they are valid prefix "foo";
instances of the YANG data model and selected document type, also
taking into account the active features, if there are any.
The resulting JSON document is always a single object ([RFC7159], container top {
Sec. 4) whose members are translated from the original XML document leaf foo {
using the rules specified in the following sections. type uint8;
}
}
}
3.1. Names and Namespaces If the data model consists only of this module, then the following is
a valid JSON-encoded configuration:
The local part of a JSON name is always identical to the local name {
of the corresponding XML element. "foomod:top": {
"foo": 54
}
}
Each JSON name lives in a namespace which is uniquely identified by Note that the top-level container instance contains the namespace
the name of the YANG module where the corresponding data node is identifier (module name) but the "foo" leaf doesn't because it is
defined. If the data node is defined in a submodule, then the defined in the same module as its parent container.
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.
The namespace SHALL be expressed in JSON text by prefixing the local Now, assume the container "top" is augmented from another module,
name in the following way: "barmod":
<module name>:<local name> module barmod {
Figure 1: Encoding a namespace identifier with a local name. namespace "http://example.com/barmod";
The namespace identifier MUST be used for local names that are prefix "bar";
ambiguous, i.e., whenever the data model permits a sibling data node
with the same local name. Otherwise, the namespace identifier is
OPTIONAL.
For example, consider the following YANG module: import foomod {
prefix "foo";
}
module foomod { augment "/foo:top" {
namespace "http://example.com/foomod"; leaf bar {
prefix "fm"; type boolean;
container foo {
leaf bar {
type boolean;
}
}
} }
}
}
If the data model consists only of this module, then the following is A valid JSON-encoded configuration containing both leafs may then
a valid JSON document: look like this:
{ {
"foo": { "foomod:top": {
"bar": true "foo": 54,
} "barmod:bar": true
} }
}
Now, assume the container "foo" is augmented from another module: The name of the "bar" leaf must be prefixed with the namespace
identifier because its parent is defined in a different module, hence
it belongs to another namespace.
module barmod { Explicit namespace identifiers are sometimes needed when encoding
namespace "http://example.com/barmod"; values of the "identityref" and "instances-identifier" types. The
prefix "bm"; same form as shown in Figure 1 is then used as well. See Sections
import foomod { 6.8 and 6.11 for details.
prefix fm;
}
augment "/fm:foo" {
leaf bar {
type uint8;
}
}
}
In the data model combining "foomod" and "barmod", we have two 5. Encoding of YANG Data Node Instances
sibling data nodes with the same local name, namely "bar". In this
case, a valid JSON document has to specify an explicit namespace
identifier (module name) for both leaves:
{ Every complete JSON instance document, such as a configuration
"foo": { datastore content, is an object. Its members are instances of all
"foomod:bar": true, top-level data nodes defined by the YANG data model.
"barmod:bar": 123
}
}
3.2. Mapping XML Elements to JSON Objects Character encoding MUST be UTF-8.
An XML element that is modelled as a YANG data node is translated to Any data node instance is encoded as a name/value pair where the name
a name/value pair where the name is formed from the name of the XML is formed from the data node identifier using the rules of Section 4.
element using the rules in Section 3.1. The value depends on the The value depends on the category of the data node as explained in
type of the data node as specified in the following sections. the following subsections.
3.2.1. The "leaf" Data Node 5.1. The "leaf" Data Node
An XML element that is modeled as YANG leaf is translated to a name/ A leaf instance is encoded as a name/value pair where the value can
value pair and the type of the value is derived from the YANG be a string, number, literal 'true' or 'false' or the special array
datatype of the leaf (see Section 3.3 for the datatype mapping '[null]', depending on the type of the leaf (see Section 6 for the
rules). type encoding rules).
Example: For the leaf node definition Example: For the leaf node definition
leaf foo { leaf foo {
type uint8; type uint8;
} }
the XML element
<foo>123</foo>
corresponds to the JSON name/value pair the following is a valid JSON-encoded instance:
"foo": 123 "foo": 123
3.2.2. The "container" Data Node 5.2. The "container" Data Node
An XML element that is modeled as YANG container is translated to a An container instance is encoded as a name/object pair. The
name/object pair. container's child data nodes are encoded as members of the object.
Example: For the container definition Example: For the container definition
container bar {
leaf foo {
type uint8;
}
}
the XML element
<bar> container bar {
<foo>123</foo> leaf foo {
</bar> type uint8;
}
}
corresponds to the JSON name/value pair the following is a valid instance:
"bar": { "bar": {
"foo": 123 "foo": 123
} }
3.2.3. The "leaf-list" Data Node 5.3. The "leaf-list" Data Node
A sequence of one or more sibling XML elements with the same A leaf-list is encoded as a name/array pair, and the array elements
qualified name that is modeled as YANG leaf-list is translated to a are values whose type depends on the datatype of the leaf-list (see
name/array pair, and the array elements are primitive values whose Section 6).
type depends on the datatype of the leaf-list (see Section 3.3).
Example: For the leaf-list definition Example: For the leaf-list definition
leaf-list foo { leaf-list foo {
type uint8; type uint8;
} }
the XML elements
<foo>123</foo>
<foo>0</foo
correspond to the JSON name/value pair the following is a valid instance:
"foo": [123, 0] "foo": [123, 0]
3.2.4. The "list" Data Node 5.4. The "list" Data Node
A sequence of one or more sibling XML elements with the same A list instance is encoded as a name/array pair, and the array
qualified name that is modeled as YANG list is translated to a name/ elements are JSON objects.
array pair, and the array elements are JSON objects.
Unlike the XML encoding, where the list keys are required to come Unlike the XML encoding, where list keys are required to precede any
before any other siblings, and in the order specified by the data other siblings, and to appear in the order specified by the data
model, the order of members within a JSON list entry is arbitrary, model, the order of members within a JSON-encoded list entry is
because JSON objects are fundamentally unordered collections of arbitrary because JSON objects are fundamentally unordered
members. collections of members.
Example: For the list definition Example: For the list definition
list bar {
key foo;
leaf foo {
type uint8;
}
leaf baz {
type string;
}
}
list bar { the following is a valid instance:
key foo;
leaf foo {
type uint8;
}
leaf baz {
type string;
}
}
the XML elements "bar": [
{
"foo": 123,
"baz": "zig"
},
{
"baz": "zag",
"foo": 0
}
]
<bar> 5.5. The "anyxml" Data Node
<foo>123</foo>
<baz>zig</baz>
</bar>
<bar>
<foo>0</foo>
<baz>zag</baz>
</bar>
correspond to the JSON name/value pair An anyxml instance is translated to a name/value pair. The value can
be of any valid JSON type, i.e. an object, array, number, string or
any of the literals 'true', 'false' and 'null'.
"bar": [ This document defines no mapping between the contents of JSON- and
{ XML-encoded anyxml instances. It is not necessary because anyxml
"foo": 123, contents are not subject to YANG-based validation (see sec. 7.10 in
"baz": "zig" [RFC6020]).
},
{
"foo": 0,
"baz": "zag"
}
]
3.2.5. The "anyxml" Data Node Example: For the anyxml definition
An XML element that is modeled as a YANG anyxml data node is anyxml bar;
translated to a name/object pair. The content of such an element is
not modelled by YANG, and there may not be a straightforward mapping
to JSON text (e.g., if it is a mixed XML content). Therefore,
translation of anyxml contents is necessarily application-specific
and outside the scope of this document.
Example: For the anyxml definition the following is a valid instance:
anyxml bar; "bar": [true, null, true]
the XML element 6. The Mapping of YANG Datatypes to JSON Values
<bar> The type of the JSON value in an instance of the leaf or leaf-list
<p xmlns="http://www.w3.org/1999/xhtml"> data node depends on the datatype of that data node as specified in
This is <em>very</em> cool. the following subsections.
</p>
</bar>
may be translated to the following JSON name/value pair: 6.1. Numeric Datatypes
{ A value of the "int8", "int16", "int32", "uint8", "uint16" is
"bar": { represented as a JSON number.
"p": "This is *very* cool."
}
}
3.3. Mapping YANG Datatypes to JSON Values A value of the "int64", "uint64" or "decimal64" type is encoded as a
JSON string whose contents is the lexical representation of that
numeric value as specified in sections 9.2.1 and 9.3.1 of [RFC6020].
3.3.1. Numeric Datatypes For example, if the type of the leaf "foo" in Section 5.1 was
"unit64" instead of "uint8", the instance would have to be encoded as
A value of one of the YANG numeric datatypes ("int8", "int16", "foo": "123"
"int32", "int64", "uint8", "uint16", "uint32", "uint64" and
"decimal64") is mapped to a JSON number using the same lexical
representation.
3.3.2. The "string" Type The special handling of 64-bit numbers follows from I-JSON
recommendation to encode numbers exceeding the IEEE 754-2000 double
precision range as strings, see sec. 2.2 in [I-D.ietf-json-i-json].
A "string" value is mapped to an identical JSON string, subject to 6.2. The "string" Type
JSON encoding rules.
3.3.3. The "boolean" Type A "string" value encoded as a JSON string, subject to JSON encoding
rules.
A "boolean" value is mapped to the corresponding JSON value 'true' or 6.3. The "boolean" Type
'false'.
3.3.4. The "enumeration" Type A "boolean" value is mapped to the corresponding JSON literal name
'true' or 'false'.
6.4. The "enumeration" Type
An "enumeration" value is mapped in the same way as a string except An "enumeration" value is mapped in the same way as a string except
that the permitted values are defined by "enum" statements in YANG. that the permitted values are defined by "enum" statements in YANG.
See sec. 9.6 in [RFC6020].
3.3.5. The "bits" Type 6.5. The "bits" Type
A "bits" value is mapped to a string identical to the lexical A "bits" value is mapped to a JSON string identical to the lexical
representation of this value in XML, i.e., space-separated names representation of this value in XML, i.e., space-separated names
representing the individual bit values that are set. representing the individual bit values that are set. See sec. 9.7 in
[RFC6020].
3.3.6. The "binary" Type 6.6. The "binary" Type
A "binary" value is mapped to a JSON string identical to the lexical A "binary" value is mapped to a JSON string identical to the lexical
representation of this value in XML, i.e., base64-encoded binary representation of this value in XML, i.e., base64-encoded binary
data. data. See sec. 9.8 in [RFC6020].
3.3.7. The "leafref" Type 6.7. The "leafref" Type
A "leafref" value is mapped according to the same rules as the type A "leafref" value is mapped according to the same rules as the type
of the leaf being referred to. of the leaf being referred to.
3.3.8. The "identityref" Type 6.8. The "identityref" Type
An "identityref" value is mapped to a string representing the
qualified name of the identity. Its namespace MAY be expressed as
shown in Figure 1. If the namespace part is not present, the
namespace of the name of the JSON object containing the value is
assumed.
3.3.9. The "empty" Type
An "empty" value is mapped to '[null]', i.e., an array with the
'null' value being its only element.
This encoding 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'.
Example: For the leaf definition
leaf foo {
type empty;
}
the XML element
<foo/>
corresponds to the JSON name/value pair
"foo": [null]
3.3.10. The "union" Type
YANG "union" type represents a choice among multiple alternative An "identityref" value is mapped to a string representing the name of
types. The actual type of the XML value MUST be determined using the an identity. Its namespace MUST be expressed as shown in Figure 1 if
procedure specified in Sec. 9.12 of [RFC6020] and the mapping rules it is different from the namespace of the leaf node containing the
for that type are used. identityref value, and MAY be expressed otherwise.
For example, consider the following YANG definition: For example, consider the following schematic module:
leaf-list bar { module exmod {
type union { ...
type uint16; import ietf-interfaces {
type string; prefix if;
} }
import iana-if-type {
prefix ianaift;
}
...
leaf type {
type identityref {
base "if:interface-type";
} }
}
}
The sequence of three XML elements A valid instance of the "type" leaf is then encoded as follows:
<bar>6378</bar>
<bar>14.5</bar>
<bar>infinity</bar>
will then be translated to this name/array pair:
"bar": [6378, "14.5", "infinity"]
3.3.11. The "instance-identifier" Type
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
Section 3.1.
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.
For example, assume "ex" is the prefix defined for the "example"
module. Then the XML-encoded instance identifier
/ex:system/ex:user[ex:name='fred']
corresponds to the following JSON-encoded instance identifier: "type": "iana-if-type:ethernetCsmacd"
/example:system/example:user[example:name='fred'] The namespace identifier "iana-if-type" must be present in this case
because the "ethernetCsmacd" identity is not defined in the same
module as the "type" leaf.
or simply 6.9. The "empty" Type
/system/user[name='fred'] An "empty" value is mapped to '[null]', i.e., an array with the
'null' literal being its only element.
if the local names of the data nodes "system", "user" and "name" are This encoding was chosen instead of using simply 'null' in order to
unambiguous. facilitate the use of empty leafs in common programming languages.
When used in a boolean context, the '[null]' value, unlike 'null',
evaluates to true.
4. Encoding Metadata in JSON Example: For the leaf definition
By design, YANG does not allow for modeling XML attributes. However, leaf foo {
attributes are often used in XML instance documents for attaching type empty;
various types of metadata information to elements. It is therefore }
desirable to have a standard way for representing attributes in JSON
documents as well.
The metadata encoding defined in the rest of this section satisfies a valid instance is
the following two important requirements:
1. There has to be a way for adding metadata to instances of all "foo": [null]
types of YANG data nodes, i.e., leafs, containers, list and leaf-
list entries, and anyxml nodes.
2. The encoding of YANG data node instances as defined in the 6.10. The "union" Type
previous sections must not change.
Existing proposals for metadata encoding in JSON, such as A value of the "union" type is encoded as the value of any of the
[JSON-META], are oriented on rather specific uses of metadata, and member types.
fall short with respect to the first requirement.
All attributes assigned to an XML element are mapped in JSON to Unlike XML, JSON conveys part of the type information already in the
members (name/value pairs) of a single object, henceforth denoted as encoding. When validating a value of the "union" type, this
the metadata object. The placement of this object depends on the information MUST also be taken into account.
type of the element from YANG viewpoint, as specified in the
following paragraphs.
For an XML element that is translated to a JSON object (i.e., a For example, consider the following YANG definition:
container, anyxml node and list entry), the metadata object is added
as a new member of that object with the name "@".
Examples: leaf bar {
type union {
type uint16;
type string;
}
}
o If "cask" is a container or anyxml node, the XML instance with In RESTCONF [I-D.ietf-netconf-restconf], it is fully acceptable to
attributes set the value of "bar" in the following way when using the
"application/yang.data+xml" media type:
<cask foo="a" bar="b"> <bar>13.5</bar>
...
</cask>
is mapped to the following JSON object: because the value may be interpreted as a string, i.e., the second
member type of the union. When using the "application/
yang.data+json" media type, however, this is an error:
"cask": { "bar": 13.5
"@": {
"foo": "a",
"bar": "b"
}
...
}
o If "seq" is a list, then the pair of XML elements In this case, the JSON encoding indicates the value is supposed to be
a number rather than string.
<seq foo="a"> 6.11. The "instance-identifier" Type
<name>one</name>
</seq>
<seq bar="b">
<name>two</name>
</seq>
is mapped to the following JSON array: An "instance-identifier" value is encoded as a string that is
analogical to the lexical representation in XML encoding, see
sec. 9.13.3 in [RFC6020]. The only difference is that XML namespace
prefixes used for qualifying node names in the instance-identifier
value are replaced by the corresponding module names according to the
rules of Section 4.
"seq": [ Conversely, when translating such a value from JSON to XML, the
{ namespace identifier (YANG module name) in each component of the
"@": { instance-identifier MUST be replaced by the XML namespace prefix that
"foo": "a" is associated with the namespace URI reference of the module.
},
"name": "one"
},
{
"@": {
"bar": "b"
},
"name": "two"
}
]
In order to assign attributes to a leaf instance, a sibling name/ For example, assume "ex" is the prefix associated with the namespace
value pair is added, where the name is the symbol "@" concatenated URI that is defined in the "example" module. Then the XML-encoded
with the identifier of the leaf. instance-identifier
For example, the element /ex:system/ex:user[ex:name='fred']
<flag foo="a" bar="b">true</foo> corresponds to the following JSON-encoded instance-identifier:
is mapped to the following two name/value pairs: /example:system/example:user[example:name='fred']
"flag": true, 7. I-JSON Compliance
"@flag": {
"foo": "a",
"bar": "b"
}
Finally, for a leaf-list instance, which is represented as a JSON I-JSON [I-D.ietf-json-i-json] is a restricted profile of JSON that
array with primitive values, attributes may be assigned to one or guarantees maximum interoperability for protocols that use JSON in
more entries by adding a sibling name/value pair, where the name is their messages, no matter what JSON encoders/decoders are used in
the symbol "@" concatenated with the identifier of the leaf-list, and protocol implementations. The encoding defined in this document
the value is a JSON array whose i-th element is the metadata object therefore observes the I-JSON requirements and recommendations as
with attributes assigned to the i-th entry of the leaf-list, or nil closely as possible.
if the i-th entry has no attributes.
Trailing nil values in the array, i.e., those following the last non- In particular, the following properties are guaranteed:
nil metadata object, MAY be omitted.
For example, a leaf-list instance with four entries o Character encoding is UTF-8.
<folio>6</folio> o Member names within the same JSON object are always unique.
<folio foo="a">3</folio>
<folio bar="b">7</folio>
<folio>8</folio>
is mapped to the following two name/value pairs: o The order of JSON object members is never relied upon.
"folio": [6, 3, 7, 8], o Numbers of any type supported by YANG can be exchanged reliably.
"@folio": [nil, {"foo": "a"}, {"bar": "b"}] See Section 6.1 for details.
The encoding of attributes as specified above has the following two The only two cases where a JSON instance document encoded according
limitations: to this document may deviate from I-JSON were dictated by the need to
be able to encode the same instance data in both JSON and XML. These
two exceptions are:
o Mapping of namespaces of XML attributes is undefined. o Leaf values encoded as strings may contain code points identifying
Noncharacters that belong to the XML character set (see sec. 2.2
in [W3C.REC-xml-20081126]).
o Attribute values can only be strings, other data types are not o Values of the "binary" type are encoded with the base64 encoding
supported. scheme (see sec. 9.8.2 in [RFC6020]) whereas I-JSON recommends
base64url instead. However, the use of base64 should not cause
any interoperability problems because these values never appear in
an URL.
5. IANA Considerations 8. Security Considerations
TBD - register application/yang.data+json media type? This document defines an alternative encoding for data modeled in the
YANG data modeling language. As such, it doesn't contribute any new
security issues beyond those discussed in sec. 15 of [RFC6020].
6. Security Considerations JSON is rather different from XML, and JSON parsers may thus suffer
from other types of vulnerabilities than their XML counterparts. To
minimize these security risks, it is important that client and server
software supporting JSON encoding behaves as required in sec. 3 of
[I-D.ietf-json-i-json]. That is, any received JSON data that violate
any of I-JSON strict constraints MUST NOT be trusted nor acted upon.
Violations due to the presence of Unicode Noncharacters in the data
exceptions (see Section 7) SHOULD be carefully examined.
TBD. 9. Acknowledgments
7. Acknowledgments The author wishes to thank Andy Bierman, Martin Bjorklund, Juergen
Schoenwaelder and Phil Shafer for their helpful comments and
suggestions.
The author wishes to thank Andy Bierman, Martin Bjorklund and Phil 10. References
Shafer for their helpful comments and suggestions.
8. References 10.1. Normative References
8.1. Normative References [I-D.ietf-json-i-json]
Bray, T., "The I-JSON Message Format", draft-ietf-json-
i-json-03 (work in progress), August 2014.
[RFC2119] Bradner, S., "Key words for use in RFCs to Indicate [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate
Requirement Levels", BCP 14, RFC 2119, March 1997. Requirement Levels", BCP 14, RFC 2119, March 1997.
[RFC6020] Bjorklund, M., Ed., "YANG - A Data Modeling Language for [RFC6020] Bjorklund, M., "YANG - A Data Modeling Language for the
Network Configuration Protocol (NETCONF)", RFC 6020, Network Configuration Protocol (NETCONF)", RFC 6020,
September 2010. October 2010.
[RFC6241] Enns, R., Bjorklund, M., Schoenwaelder, J., and A. [RFC6241] Enns, R., Bjorklund, M., Schoenwaelder, J., and A.
Bierman, "NETCONF Configuration Protocol", RFC 6241, June Bierman, "Network Configuration Protocol (NETCONF)", RFC
2011. 6241, June 2011.
[RFC7159] Bray, T., Ed., "The JavaScript Object Notation (JSON) Data [RFC7159] Bray, T., "The JavaScript Object Notation (JSON) Data
Interchange Format", RFC 7159, March 2014. Interchange Format", RFC 7159, March 2014.
[XMLNS] Bray, T., Hollander, D., Layman, A., Tobin, R., and H. [W3C.REC-xml-20081126]
Thompson, "Namespaces in XML 1.0 (Third Edition)", World Bray, T., Paoli, J., Sperberg-McQueen, M., Maler, E., and
Wide Web Consortium Recommendation REC-xml-names-20091208,
December 2009,
<http://www.w3.org/TR/2009/REC-xml-names-20091208>.
[XML] Bray, T., Paoli, J., Sperberg-McQueen, C., Maler, E., and
F. Yergeau, "Extensible Markup Language (XML) 1.0 (Fifth F. Yergeau, "Extensible Markup Language (XML) 1.0 (Fifth
Edition)", World Wide Web Consortium Recommendation REC- Edition)", World Wide Web Consortium Recommendation REC-
xml-20081126, November 2008, xml-20081126, November 2008,
<http://www.w3.org/TR/2006/REC-xml-20060816>. <http://www.w3.org/TR/2008/REC-xml-20081126>.
8.2. Informative References
[IF-CFG] Bjorklund, M., "A YANG Data Model for Interface 10.2. Informative References
Management", draft-ietf-netmod-interfaces-cfg-16 (work in
progress), January 2014.
[JSON-META] [I-D.ietf-netconf-restconf]
Sakimura, N., "JSON Metadata", draft-sakimura-json- Bierman, A., Bjorklund, M., and K. Watsen, "RESTCONF
metadata-01 (work in progress), November 2013. Protocol", draft-ietf-netconf-restconf-02 (work in
progress), October 2014.
[RESTCONF] [RFC7223] Bjorklund, M., "A YANG Data Model for Interface
Bierman, A., Bjorklund, M., Watsen, K., and R. Fernando, Management", RFC 7223, May 2014.
"RESTCONF Protocol", draft-ietf-netconf-restconf-00 (work
in progress), March 2014.
[XPath] Clark, J., "XML Path Language (XPath) Version 1.0", World [W3C.REC-xpath-19991116]
Wide Web Consortium Recommendation REC-xpath-19991116, Clark, J. and S. DeRose, "XML Path Language (XPath)
November 1999, Version 1.0", World Wide Web Consortium Recommendation
REC-xpath-19991116, November 1999,
<http://www.w3.org/TR/1999/REC-xpath-19991116>. <http://www.w3.org/TR/1999/REC-xpath-19991116>.
Appendix A. A Complete Example Appendix A. A Complete Example
The JSON document shown below was translated from a reply to the The JSON document shown below represents the same data as the reply
NETCONF <get> request that can be found in Appendix D of [IF-CFG]. to the NETCONF <get> request appearing in Appendix D of [RFC7223].
The data model is a combination of two YANG modules: "ietf- The data model is a combination of two YANG modules: "ietf-
interfaces" and "ex-vlan" (the latter is an example module from interfaces" and "ex-vlan" (the latter is an example module from
Appendix C of [IF-CFG]). The "if-mib" feature defined in the "ietf- Appendix C of [RFC7223]). The "if-mib" feature defined in the "ietf-
interfaces" module is considered to be active. interfaces" module is considered to be active.
{ {
"interfaces": { "ietf-interfaces:interfaces": {
"interface": [ "interface": [
{ {
"name": "eth0", "name": "eth0",
"type": "iana-if-type:ethernetCsmacd", "type": "iana-if-type:ethernetCsmacd",
"enabled": false "enabled": false
}, },
{ {
"name": "eth1", "name": "eth1",
"type": "iana-if-type:ethernetCsmacd", "type": "iana-if-type:ethernetCsmacd",
"enabled": true, "enabled": true,
"vlan-tagging": true "ex-vlan:vlan-tagging": true
}, },
{ {
"name": "eth1.10", "name": "eth1.10",
"type": "iana-if-type:l2vlan", "type": "iana-if-type:l2vlan",
"enabled": true, "enabled": true,
"base-interface": "eth1", "ex-vlan:base-interface": "eth1",
"vlan-id": 10 "ex-vlan:vlan-id": 10
}, },
{ {
"name": "lo1", "name": "lo1",
"type": "iana-if-type:softwareLoopback", "type": "iana-if-type:softwareLoopback",
"enabled": true "enabled": true
} }
] ]
}, },
"interfaces-state": { "ietf-interfaces:interfaces-state": {
"interface": [ "interface": [
{ {
"name": "eth0", "name": "eth0",
"type": "iana-if-type:ethernetCsmacd", "type": "iana-if-type:ethernetCsmacd",
"admin-status": "down", "admin-status": "down",
"oper-status": "down", "oper-status": "down",
"if-index": 2, "if-index": 2,
"phys-address": "00:01:02:03:04:05", "phys-address": "00:01:02:03:04:05",
"statistics": { "statistics": {
"discontinuity-time": "2013-04-01T03:00:00+00:00" "discontinuity-time": "2013-04-01T03:00:00+00:00"
skipping to change at page 20, line 21 skipping to change at page 16, line 33
"oper-status": "up", "oper-status": "up",
"if-index": 1, "if-index": 1,
"statistics": { "statistics": {
"discontinuity-time": "2013-04-01T03:00:00+00:00" "discontinuity-time": "2013-04-01T03:00:00+00:00"
} }
} }
] ]
} }
} }
Appendix B. Change Log
RFC Editor: Remove this section upon publication as an RFC.
B.1. Changes Between Revisions -00 and -01
o Metadata encoding was moved to a separate I-D, draft-lhotka-
netmod-yang-metadata.
o JSON encoding is now defined directly rather than via XML-JSON
mapping.
o The rules for namespace encoding has changed. This affect both
node instance names and instance-identifiers.
o I-JSON-related changes. The most significant is the string
encoding of 64-bit numbers.
o When validating union type, the partial type info present in JSON
encoding is taken into account.
o Added section about I-JSON compliance.
o Updated the example in appendix.
o Wrote Security Considerations.
o Removed IANA Considerations as there are none.
Author's Address Author's Address
Ladislav Lhotka Ladislav Lhotka
CZ.NIC CZ.NIC
Email: lhotka@nic.cz Email: lhotka@nic.cz
 End of changes. 160 change blocks. 
569 lines changed or deleted 436 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/