NETMOD Working Group L. Lhotka
Internet-Draft CZ.NIC
Intended status: Standards Track September 10, 2015
Expires: March 13, 2016

JSON Encoding of Data Modeled with YANG
draft-ietf-netmod-yang-json-05

Abstract

This document defines encoding rules for representing configuration, state data, RPC operation or action input and output parameters, and notifications defined using YANG as JavaScript Object Notation (JSON) text.

Status of This Memo

This Internet-Draft is submitted in full conformance with the provisions of BCP 78 and BCP 79.

Internet-Drafts are working documents of the Internet Engineering Task Force (IETF). Note that other groups may also distribute working documents as Internet-Drafts. The list of current Internet-Drafts is at http://datatracker.ietf.org/drafts/current/.

Internet-Drafts are draft documents valid for a maximum of six months and may be updated, replaced, or obsoleted by other documents at any time. It is inappropriate to use Internet-Drafts as reference material or to cite them other than as "work in progress."

This Internet-Draft will expire on March 13, 2016.

Copyright Notice

Copyright (c) 2015 IETF Trust and the persons identified as the document authors. All rights reserved.

This document is subject to BCP 78 and the IETF Trust's Legal Provisions Relating to IETF Documents (http://trustee.ietf.org/license-info) in effect on the date of publication of this document. Please review these documents carefully, as they describe your rights and restrictions with respect to this document. Code Components extracted from this document must include Simplified BSD License text as described in Section 4.e of the Trust Legal Provisions and are provided without warranty as described in the Simplified BSD License.


Table of Contents

1. Introduction

The NETCONF protocol [RFC6241] uses XML [W3C.REC-xml-20081126] for encoding data in its Content Layer. Other management protocols might want to use other encodings while still benefiting from using YANG [I-D.ietf-netmod-rfc6020bis] as the data modeling language.

For example, the RESTCONF protocol [I-D.ietf-netconf-restconf] supports two encodings: XML (media type "application/yang.data+xml") and JSON (media type "application/yang.data+json).

The specification of YANG 1.1 data modelling language [I-D.ietf-netmod-rfc6020bis] defines only XML encoding for data instances, i.e., contents of configuration datastores, state data, RPC operation or action 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].

2. Terminology and Notation

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 [RFC2119].

The following terms are defined in [I-D.ietf-netmod-rfc6020bis]:

3. Properties of the JSON Encoding

This document defines JSON encoding for YANG data trees and their subtrees. It is always assumed that the top-level structure in JSON-encoded data is an object.

Instances of YANG data nodes (leafs, containers, leaf-lists, lists, anydata and anyxml nodes) are encoded as members of a JSON object, i.e., name/value pairs. Section 4 defines how the name part is formed, and the following sections deal with the value part.

Unlike XML element content, JSON values carry partial type information (number, string, boolean). The JSON encoding is defined so that this information is never in conflict with the data type of the corresponding YANG leaf or leaf-list.

With the exception of anyxml and schema-less anydata nodes, it is possible to map a JSON-encoded data tree to XML encoding as defined in [I-D.ietf-netmod-rfc6020bis], and vice versa. However, such conversions require the YANG data model to be available.

In order to achieve maximum interoperability while allowing implementations to use a variety of existing JSON parsers, the JSON encoding rules follow, as much as possible, the constraints of the I-JSON restricted profile [RFC7493]. Section 7 discusses I-JSON conformance in more detail.

4. Names and Namespaces

An object member name MUST be in one of the following forms:

The name of a module determines the namespace of all data node names defined in that module. If a data node is defined in a submodule, then the namespace-qualified member name uses the name of the main module to which the submodule belongs.

ABNF syntax [RFC5234] of a member name is shown in Figure 1, where the production for "identifier" is defined in sec. 13 of [I-D.ietf-netmod-rfc6020bis].

        member-name = [identifier ":"] identifier
        

Figure 1: ABNF production for a JSON member name.

A namespace-qualified member name MUST be used for all members of a top-level JSON object, and then also whenever the namespaces of the data node and its parent node are different. In all other cases, the simple form of the member name MUST be used.

module foomod {

  namespace "http://example.com/foomod";

  prefix "foo";

  container top {
    leaf foo {
      type uint8;
    }
  }
}

For example, consider the following YANG module:

{
  "foomod:top": {
    "foo": 54
  }
}
        

If the data model consists only of this module, then the following is a valid JSON-encoded configuration:

Note that the member of the top-level object uses the namespace-qualified name but the "foo" leaf doesn't because it is defined in the same module as its parent container "top".

module barmod {

  namespace "http://example.com/barmod";

  prefix "bar";

  import foomod {
    prefix "foo";
  }

  augment "/foo:top" {
    leaf bar {
      type boolean;
    }
  }
}

Now, assume the container "top" is augmented from another module, "barmod":

{
  "foomod:top": {
    "foo": 54,
    "barmod:bar": true
  }
}
        

A valid JSON-encoded configuration containing both leafs may then look like this:

The name of the "bar" leaf is prefixed with the namespace identifier because its parent is defined in a different module.

Explicit namespace identifiers are sometimes needed when encoding values of the "identityref" and "instances-identifier" types. The same form of namespace-qualified name as defined above is then used. See Sections 6.8 and 6.11 for details.

5. Encoding of YANG Data Node Instances

Every data node instance is encoded as a name/value pair where the name is formed from the data node identifier using the rules of Section 4. The value depends on the category of the data node as explained in the following subsections.

Character encoding MUST be UTF-8.

5.1. The "leaf" Data Node

A leaf instance is encoded as a name/value pair where the value can be a string, number, literal "true" or "false", or the special array "[null]", depending on the type of the leaf (see Section 6 for the type encoding rules).

leaf foo {
  type uint8;
}
          
"foo": 123
          

Example: For the leaf node definition

5.2. The "container" Data Node

A container instance is encoded as a name/object pair. The container's child data nodes are encoded as members of the object.

container bar {
  leaf foo {
    type uint8;
  }
}
          
"bar": {
  "foo": 123
}
          

Example: For the container definition

5.3. The "leaf-list" Data Node

A leaf-list is encoded as a name/array pair, and the array elements are values of some scalar type, which can be a string, number, literal "true" or "false", or the special array "[null]", depending on the type of the leaf-list (see Section 6 for the type encoding rules).

The ordering of array elements follows the same rules as the ordering of XML elements representing leaf-list entries in the XML encoding. Specifically, the "ordered-by" properties (sec. 7.7.7 in [I-D.ietf-netmod-rfc6020bis]) MUST be observed.

leaf-list foo {
  type uint8;
}
          
"foo": [123, 0]
          

Example: For the leaf-list definition

5.4. The "list" Data Node

A list instance is encoded as a name/array pair, and the array elements are JSON objects.

The ordering of array elements follows the same rules as the ordering of XML elements representing list entries in the XML encoding. Specifically, the "ordered-by" properties (sec. 7.7.7 in [I-D.ietf-netmod-rfc6020bis]) MUST be observed.

Unlike the XML encoding, where list keys are required to precede any other siblings within a list entry, and appear in the order specified by the data model, the order of members in a JSON-encoded list entry is arbitrary because JSON objects are fundamentally unordered collections of members.

list bar {
  key foo;
  leaf foo {
    type uint8;
  }
  leaf baz {
    type string;
  }
}
          
"bar": [
  {
    "foo": 123,
    "baz": "zig"
  },
  {
    "baz": "zag",
    "foo": 0
  }
]
          

Example: For the list definition

5.5. The "anydata" Data Node

Anydata data node is a new feature in YANG 1.1. It serves as a container for an unknown set of nodes that however appear as normal YANG-modeled data. A data model for anydata content may or may not exist at run time. In the latter case, no universal mapping between JSON- and XML-encoded instances is available.

An anydata instance is encoded in the same way as a container, i.e., as a value/object pair. The requirement that anydata content can be modeled by YANG implies the following rules for JSON text inside the object:

anydata data;
	  
"data": {
  "ietf-notification:notification": {
    "eventTime": "2014-07-29T13:43:01Z",
    "example-event:event": {
      "event-class: "fault",
      "reporting-entity": {
        "card": "Ethernet0"
      },
      "severity": "major"
    }
  }
}
	  

Example: for the anydata definition

5.6. The "anyxml" Data Node

An anyxml instance is encoded as a JSON name/value pair which MUST satisfy I-JSON constraints. Otherwise it is unrestricted, i.e., the value can be an object, array, number, string or one of the literals "true", "false" and "null".

There is no universal procedure for mapping JSON-encoded anyxml instances to XML, and vice versa.

anyxml bar;
          
"bar": [true, null, true]
          

Example: For the anyxml definition

6. Representing YANG Data Types in JSON Values

The type of the JSON value in an instance of the leaf or leaf-list data node depends on the type of that data node as specified in the following subsections.

6.1. Numeric Types

A value of the types "int8", "int16", "int32", "uint8", "uint16" and "uint32" is represented as a JSON number.

A value of the "int64", "uint64" or "decimal64" type is represented as a JSON string whose content is the lexical representation of the corresponding YANG type as specified in sections 9.2.1 and 9.3.1 of [I-D.ietf-netmod-rfc6020bis].

For example, if the type of the leaf "foo" in Section 5.1 was "uint64" instead of "uint8", the instance would have to be encoded as

"foo": "123"
          

The special handling of 64-bit numbers follows from the I-JSON recommendation to encode numbers exceeding the IEEE 754-2008 double precision range as strings, see sec. 2.2 in [RFC7493].

6.2. The "string" Type

A "string" value represented as a JSON string, subject to JSON string encoding rules.

6.3. The "boolean" Type

A "boolean" value is represented as the corresponding JSON literal name "true" or "false".

6.4. The "enumeration" Type

An "enumeration" value is represented as a JSON string - one of the names assigned by "enum" statements in YANG.

The representation is identical to the lexical representation of the "enumeration" type in XML, see sec. 9.6 in [I-D.ietf-netmod-rfc6020bis].

6.5. The "bits" Type

A "bits" value is represented as a JSON string - a space-separated sequence of names of bits that are set. The permitted bit names are assigned by "bit" statements in YANG.

The representation is identical to the lexical representation of the "bits" type, see sec. 9.7 in [I-D.ietf-netmod-rfc6020bis].

6.6. The "binary" Type

A "binary" value is represented as a JSON string - base64-encoding of arbitrary binary data.

The representation is identical to the lexical representation of the "binary" type in XML, see sec. 9.8 in [I-D.ietf-netmod-rfc6020bis].

6.7. The "leafref" Type

A "leafref" value is represented using the same rules as the type of the leaf to which the leafref value refers.

6.8. The "identityref" Type

An "identityref" value is represented as a string - the name of an identity. If the identity is defined in another module than the leaf node containing the identityref value, the namespace-qualified form (Section 4) MUST be used. Otherwise, both the simple and namespace-qualified forms are permitted.

For example, consider the following schematic module:

module exmod {
  ...
  import ietf-interfaces {
    prefix if;
  }
  import iana-if-type {
    prefix ianaift;
  }
  ...
  leaf type {
    type identityref {
      base "if:interface-type";
    }
  }
}
          
"type": "iana-if-type:ethernetCsmacd"
          

A valid instance of the "type" leaf is then encoded as follows:

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.

6.9. The "empty" Type

An "empty" value is represented as "[null]", i.e., an array with the "null" literal being its only element. For the purposes of this document, "[null]" is considered an atomic scalar value.

This encoding of the "empty" type was chosen instead of using simply "null" in order to facilitate the use of empty leafs in common programming languages where the "null" value of a member is treated as if the member is not present.

leaf foo {
  type empty;
}
          
"foo": [null]
          

Example: For the leaf definition

6.10. The "union" Type

A value of the "union" type is encoded as the value of any of the member types.

When validating a value of the "union" type, the type information conveyed by the JSON encoding MUST also be taken into account.

leaf bar {
  type union {
    type uint16;
    type string;
  }
}
          
<bar>13.5</bar>
          
"bar": 13.5
          

For example, consider the following YANG definition: [I-D.ietf-netconf-restconf], it is possible to set the value of "bar" in the following way when using the "application/yang.data+xml" media type:

6.11. The "instance-identifier" Type

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 [I-D.ietf-netmod-rfc6020bis]. However, the encoding of namespaces in instance-identifier values follows the rules stated in Section 4, namely:

/ietf-interfaces:interfaces/interface[name='eth0']/ietf-ip:ipv4/ip
	  

For example,

7. I-JSON Compliance

I-JSON [RFC7493] is a restricted profile of JSON that guarantees maximum interoperability for protocols that use JSON in their messages, no matter what JSON encoders/decoders are used in protocol implementations. The encoding defined in this document therefore observes the I-JSON requirements and recommendations as closely as possible.

In particular, the following properties are guaranteed:

The JSON encoding defined in this document deviates from I-JSON only in the representation of the "binary" type. In order to remain compatible with XML encoding, the base64 encoding scheme is used (Section 6.6), whereas I-JSON recommends base64url instead.

8. Security Considerations

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. 16 of [I-D.ietf-netmod-rfc6020bis].

JSON processing is rather different from XML, and JSON parsers may thus suffer from other types of vulnerabilities than their XML counterparts. To minimize these new security risks, software on the receiving side SHOULD reject all messages that do not comply to the rules of this document and reply with an appropriate error message to the sender.

9. Acknowledgments

The author wishes to thank Andy Bierman, Martin Bjorklund, Dean Bogdanovic, Balazs Lengyel, Juergen Schoenwaelder and Phil Shafer for their helpful comments and suggestions.

10. References

10.1. Normative References

[I-D.ietf-netmod-rfc6020bis] Bjorklund, M., "YANG - A Data Modeling Language for the Network Configuration Protocol (NETCONF)", Internet-Draft draft-ietf-netmod-rfc6020bis-06, July 2015.
[RFC2119] Bradner, S., "Key words for use in RFCs to Indicate Requirement Levels", BCP 14, RFC 2119, DOI 10.17487/RFC2119, March 1997.
[RFC5234] Crocker, D. and P. Overell, "Augmented BNF for Syntax Specifications: ABNF", STD 68, RFC 5234, DOI 10.17487/RFC5234, January 2008.
[RFC6241] Enns, R., Bjorklund, M., Schoenwaelder, J. and A. Bierman, "Network Configuration Protocol (NETCONF)", RFC 6241, DOI 10.17487/RFC6241, June 2011.
[RFC7159] Bray, T., "The JavaScript Object Notation (JSON) Data Interchange Format", RFC 7159, DOI 10.17487/RFC7159, March 2014.
[RFC7493] Bray, T., "The I-JSON Message Format", RFC 7493, DOI 10.17487/RFC7493, March 2015.

10.2. Informative References

[I-D.ietf-netconf-restconf] Bierman, A., Bjorklund, M. and K. Watsen, "RESTCONF Protocol", Internet-Draft draft-ietf-netconf-restconf-07, July 2015.
[RFC7223] Bjorklund, M., "A YANG Data Model for Interface Management", RFC 7223, DOI 10.17487/RFC7223, May 2014.
[W3C.REC-xml-20081126] Bray, T., Paoli, J., Sperberg-McQueen, M., Maler, E. and F. Yergeau, "Extensible Markup Language (XML) 1.0 (Fifth Edition)", World Wide Web Consortium Recommendation REC-xml-20081126, November 2008.

Appendix A. A Complete Example

The JSON document shown below represents the same data as the reply to the NETCONF <get> request appearing in Appendix D of [RFC7223]. The data model is a combination of two YANG modules: "ietf-interfaces" and "ex-vlan" (the latter is an example module from Appendix C of [RFC7223]). The "if-mib" feature defined in the "ietf-interfaces" module is considered to be active.

{
  "ietf-interfaces:interfaces": {
    "interface": [
      {
        "name": "eth0",
        "type": "iana-if-type:ethernetCsmacd",
        "enabled": false
      },
      {
        "name": "eth1",
        "type": "iana-if-type:ethernetCsmacd",
        "enabled": true,
        "ex-vlan:vlan-tagging": true
      },
      {
        "name": "eth1.10",
        "type": "iana-if-type:l2vlan",
        "enabled": true,
        "ex-vlan:base-interface": "eth1",
        "ex-vlan:vlan-id": 10
      },
      {
        "name": "lo1",
        "type": "iana-if-type:softwareLoopback",
        "enabled": true
      }
    ]
  },
  "ietf-interfaces:interfaces-state": {
    "interface": [
      {
        "name": "eth0",
        "type": "iana-if-type:ethernetCsmacd",
        "admin-status": "down",
        "oper-status": "down",
        "if-index": 2,
        "phys-address": "00:01:02:03:04:05",
        "statistics": {
          "discontinuity-time": "2013-04-01T03:00:00+00:00"
        }
      },
      {
        "name": "eth1",
        "type": "iana-if-type:ethernetCsmacd",
        "admin-status": "up",
        "oper-status": "up",
        "if-index": 7,
        "phys-address": "00:01:02:03:04:06",
        "higher-layer-if": [
          "eth1.10"
        ],
        "statistics": {
          "discontinuity-time": "2013-04-01T03:00:00+00:00"
        }
      },
      {
        "name": "eth1.10",
        "type": "iana-if-type:l2vlan",
        "admin-status": "up",
        "oper-status": "up",
        "if-index": 9,
        "lower-layer-if": [
          "eth1"
        ],
        "statistics": {
          "discontinuity-time": "2013-04-01T03:00:00+00:00"
        }
      },
      {
        "name": "eth2",
        "type": "iana-if-type:ethernetCsmacd",
        "admin-status": "down",
        "oper-status": "down",
        "if-index": 8,
        "phys-address": "00:01:02:03:04:07",
        "statistics": {
          "discontinuity-time": "2013-04-01T03:00:00+00:00"
        }
      },
      {
        "name": "lo1",
        "type": "iana-if-type:softwareLoopback",
        "admin-status": "up",
        "oper-status": "up",
        "if-index": 1,
        "statistics": {
          "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 -04 and -05

B.2. Changes Between Revisions -03 and -04

B.3. Changes Between Revisions -02 and -03

B.4. Changes Between Revisions -01 and -02

B.5. Changes Between Revisions -00 and -01

Author's Address

Ladislav Lhotka CZ.NIC EMail: lhotka@nic.cz