idnits 2.17.1 draft-lhotka-yang-json-01.txt: Checking boilerplate required by RFC 5378 and the IETF Trust (see https://trustee.ietf.org/license-info): ---------------------------------------------------------------------------- No issues found here. Checking nits according to https://www.ietf.org/id-info/1id-guidelines.txt: ---------------------------------------------------------------------------- No issues found here. Checking nits according to https://www.ietf.org/id-info/checklist : ---------------------------------------------------------------------------- No issues found here. Miscellaneous warnings: ---------------------------------------------------------------------------- == The copyright year in the IETF Trust and authors Copyright Line does not match the current year -- The document date (June 05, 2012) is 4340 days in the past. Is this intentional? Checking references for intended status: Proposed Standard ---------------------------------------------------------------------------- (See RFCs 3967 and 4897 for information about using normative references to lower-maturity documents in RFCs) ** Obsolete normative reference: RFC 4627 (Obsoleted by RFC 7158, RFC 7159) -- Possible downref: Non-RFC (?) normative reference: ref. 'XML' -- Possible downref: Non-RFC (?) normative reference: ref. 'XMLNS' == Outdated reference: A later version (-01) exists of draft-bierman-netconf-yang-api-00 Summary: 1 error (**), 0 flaws (~~), 2 warnings (==), 3 comments (--). Run idnits with the --verbose option for more detailed information about the items above. -------------------------------------------------------------------------------- 2 NETMOD L. Lhotka 3 Internet-Draft CZ.NIC 4 Intended status: Standards Track June 05, 2012 5 Expires: December 7, 2012 7 Modeling JSON Text with YANG 8 draft-lhotka-yang-json-01 10 Abstract 12 This document defines rules for mapping data models expressed in YANG 13 to configuration and operational state data encoded as JSON text. It 14 does so by specifying a procedure for translating the subset of YANG- 15 compatible XML documents to JSON text, and vice versa. 17 Status of this Memo 19 This Internet-Draft is submitted in full conformance with the 20 provisions of BCP 78 and BCP 79. 22 Internet-Drafts are working documents of the Internet Engineering 23 Task Force (IETF). Note that other groups may also distribute 24 working documents as Internet-Drafts. The list of current Internet- 25 Drafts is at http://datatracker.ietf.org/drafts/current/. 27 Internet-Drafts are draft documents valid for a maximum of six months 28 and may be updated, replaced, or obsoleted by other documents at any 29 time. It is inappropriate to use Internet-Drafts as reference 30 material or to cite them other than as "work in progress." 32 This Internet-Draft will expire on December 7, 2012. 34 Copyright Notice 36 Copyright (c) 2012 IETF Trust and the persons identified as the 37 document authors. All rights reserved. 39 This document is subject to BCP 78 and the IETF Trust's Legal 40 Provisions Relating to IETF Documents 41 (http://trustee.ietf.org/license-info) in effect on the date of 42 publication of this document. Please review these documents 43 carefully, as they describe your rights and restrictions with respect 44 to this document. Code Components extracted from this document must 45 include Simplified BSD License text as described in Section 4.e of 46 the Trust Legal Provisions and are provided without warranty as 47 described in the Simplified BSD License. 49 Table of Contents 51 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . 3 52 2. Terminology and Notation . . . . . . . . . . . . . . . . . . . 4 53 3. Specification of the Translation Procedure . . . . . . . . . . 5 54 3.1. Names and Namespaces . . . . . . . . . . . . . . . . . . . 6 55 3.2. Mapping XML Elements to JSON Objects . . . . . . . . . . . 6 56 3.3. Mapping YANG Datatypes to JSON Values . . . . . . . . . . 7 57 3.3.1. Numeric Types . . . . . . . . . . . . . . . . . . . . 7 58 3.3.2. The "string" Type . . . . . . . . . . . . . . . . . . 7 59 3.3.3. The "boolean" Type . . . . . . . . . . . . . . . . . . 7 60 3.3.4. The "enumeration" Type . . . . . . . . . . . . . . . . 7 61 3.3.5. The "bits" Type . . . . . . . . . . . . . . . . . . . 7 62 3.3.6. The "binary" Type . . . . . . . . . . . . . . . . . . 7 63 3.3.7. The "leafref" Type . . . . . . . . . . . . . . . . . . 8 64 3.3.8. The "identityref" Type . . . . . . . . . . . . . . . . 8 65 3.3.9. The "empty" Type . . . . . . . . . . . . . . . . . . . 8 66 3.3.10. The "union" Type . . . . . . . . . . . . . . . . . . . 8 67 3.3.11. The "instance-identifier" Type . . . . . . . . . . . . 8 68 3.4. Example . . . . . . . . . . . . . . . . . . . . . . . . . 8 69 3.5. IANA Considerations . . . . . . . . . . . . . . . . . . . 10 70 3.6. Security Considerations . . . . . . . . . . . . . . . . . 10 71 3.7. Acknowledgments . . . . . . . . . . . . . . . . . . . . . 10 72 4. References . . . . . . . . . . . . . . . . . . . . . . . . . . 11 73 4.1. Normative References . . . . . . . . . . . . . . . . . . . 11 74 4.2. Informative References . . . . . . . . . . . . . . . . . . 11 75 Author's Address . . . . . . . . . . . . . . . . . . . . . . . . . 12 77 1. Introduction 79 The aim of this document is define rules for mapping data models 80 expressed in the YANG data modeling language [RFC6020] to 81 configuration and operational state data encoded as JavaScript Object 82 Notation (JSON) text [RFC4627]. The result can be potentially 83 applied in two different ways: 85 1. JSON may be used instead of the standard XML [XML] encoding in 86 the context of the NETCONF protocol [RFC6241] and/or with 87 existing data models expressed in YANG. An example application 88 is the YANG-API Protocol [YANG-API]. 90 2. Other documents that choose JSON to represent structured data can 91 use YANG for defining the data model, i.e., both syntactic and 92 semantic constraints that the data have to satisfy. 94 JSON mapping rules could be specified in a similar way as the XML 95 mapping rules in [RFC6020]. This would however require solving 96 several problems. To begin with, YANG uses XPath [XPath] quite 97 extensively, but XPath is not defined for JSON and such a definition 98 would be far from straightforward. 100 In order to avoid these technical difficulties, this document employs 101 an alternative approach: it defines a relatively simple procedure 102 which allows to translate the subset of XML that can be modeled using 103 YANG to JSON, and vice versa. Consequently, validation of a JSON 104 text against a data model can done by translating the JSON text to 105 XML, which is then validated according to the rules stated in 106 [RFC6020]. 108 The translation procedure is adapted to YANG specifics and 109 requirements, namely: 111 1. The translation is driven by a concrete YANG data model and uses 112 information about data types to achieve better results than 113 generic XML-JSON translation procedures. 115 2. Various document types are supported, namely configuration data, 116 configuration + state data, RPC input and output parameters, and 117 notifications. 119 3. XML namespaces specified in the data model are mapped to 120 namespaces of JSON objects. However, explicit namespace 121 identifiers in JSON text are rarely needed. 123 4. Translation of XML attributes, mixed content, comments and 124 processing instructions is not supported. 126 2. Terminology and Notation 128 The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", 129 "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this 130 document are to be interpreted as described in [RFC2119]. 132 The following terms are defined in [RFC6020]: 134 o anyxml 136 o augment 138 o container 140 o data model 142 o data node 144 o data tree 146 o datatype 148 o identity 150 o instance identifier 152 o leaf 154 o leaf-list 156 o list 158 o module 160 o submodule 162 The following terms are defined in [XMLNS]: 164 o local name 166 o prefixed name 168 o qualified name 170 3. Specification of the Translation Procedure 172 The translation procedure defines a 1-1 correspondence between the 173 subset of YANG-compatible XML documents and JSON text. This means 174 that the translation can be applied in both directions and is always 175 invertible. 177 Any YANG-compatible XML document can be translated, except documents 178 with mixed content. This is only a minor limitation since mixed 179 content is marginal in YANG - it is allowed only in "anyxml" nodes. 181 The following subsections specify rules mainly for translating XML 182 documents to JSON text. Rules for the inverse translation are stated 183 only where necessary, otherwise they can be easily inferred. 185 REQUIRED parameters of the translation procedure are: 187 o YANG data model, 189 o type of the input XML document, 191 o optional features (defined via the "feature" statement) that are 192 considered active. 194 The permissible types of XML documents are listed in Table 1 together 195 with the corresponding part of the data model that is used for the 196 translation. 198 +------------------------------+--------------------------------+ 199 | Document Type | Data Model Section | 200 +------------------------------+--------------------------------+ 201 | configuration and state data | main data tree | 202 | | | 203 | configuration | main data tree ("config true") | 204 | | | 205 | RPC input parameters | "input" nodes under "rpc" | 206 | | | 207 | RPC output parameters | "output" nodes under "rpc" | 208 | | | 209 | notification | "notification" nodes | 210 +------------------------------+--------------------------------+ 212 Table 1: YANG Document Types 214 A particular application may decide to use only a subset of document 215 types from Table 1. For instance, YANG-API Protocol [YANG-API] does 216 not use notifications. 218 XML documents can be translated to JSON text only if they are valid 219 instances of the YANG data model and selected document type, also 220 taking into account the active features, if there are any. 222 3.1. Names and Namespaces 224 The local part of a JSON name is always identical to the local name 225 of the corresponding XML element. 227 Each JSON name lives in a namespace which is uniquely identified by 228 the name of the YANG module where the corresponding data node is 229 defined. If the data node is defined in a submodule, then the 230 namespace identifier is the name of the main module to which the 231 submodule belongs. The translation procedure MUST correctly map YANG 232 namespace URIs to YANG module names and vice versa. 234 The namespace SHALL be expressed in JSON text by prefixing the local 235 name in the following way: 237 : 239 Figure 1: Encoding a namespace identifier with a local name. 241 The namespace identifier MUST be used for local names that are 242 ambiguous, i.e., whenever the data model permits a sibling node with 243 the same local name. Otherwise, the namespace identifier is 244 OPTIONAL. 246 When mapping namespaces from JSON text to XML, the resulting XML 247 document may use default namespace declarations (via the "xmlns" 248 attribute), prefix-based namespace declarations (via attributes 249 beginning with "xmlns:"), or any combination thereof following the 250 rules stated in [XMLNS]. If prefixed names are used, their prefix 251 SHOULD be the one defined by the "prefix" statement in the YANG 252 module where each data node is defined. 254 3.2. Mapping XML Elements to JSON Objects 256 XML elements are translated to JSON objects in a straightforward way: 258 o XML elements that have no siblings of the same qualified name 259 correspond either to a name/value pair or to a JSON object: 261 * An XML element which is modeled as a leaf in YANG is translated 262 to a name/value pair and the JSON datatype of the value is 263 derived from the YANG datatype of the leaf (see Section 3.3 for 264 the datatype mapping rules). 266 * An XML element which is modeled as a container in YANG is 267 translated to an object. 269 o A sequence of sibling XML elements with the same qualified name 270 (modeled as a list or leaf-list in YANG) corresponds to a JSON 271 array. If the sequence is modeled as a leaf-list in YANG, then 272 the array elements are primitive values (strings, numbers or 273 booleans) whose type depends on the datatype of the leaf-list (see 274 Section 3.3). If the sequence is modeled as a list in YANG, then 275 the array elements are JSON objects. 277 3.3. Mapping YANG Datatypes to JSON Values 279 3.3.1. Numeric Types 281 YANG numeric types ("int8", "int16", "int32", "int64", "uint8", 282 "uint16", "uint32", "uint64" and "decimal64") are mapped to JSON 283 numbers whose decimal representation is the YANG canonical form of 284 the number. Hexadecimal values MUST be converted to decimal. 286 3.3.2. The "string" Type 288 A "string" value is mapped to an identical JSON string, subject to 289 JSON encoding rules. 291 3.3.3. The "boolean" Type 293 A "boolean" value is mapped to the corresponding JSON value 'true' or 294 'false'. 296 3.3.4. The "enumeration" Type 298 An "enumeration" value is mapped in the same way as a string except 299 that the permitted values are defined by "enum" statements in YANG. 301 3.3.5. The "bits" Type 303 A "bits" value is mapped to a string identical to the lexical 304 representation of this value in XML, i.e., a space-separated list of 305 bit values. 307 3.3.6. The "binary" Type 309 A "binary" value is mapped to a JSON string identical to the lexical 310 representation of this value in XML, i.e., base64-encoded binary 311 data. 313 3.3.7. The "leafref" Type 315 A "leafref" value is mapped according to the same rules as the type 316 of the leaf being referred to, subject to the same constraints as the 317 XML value. 319 3.3.8. The "identityref" Type 321 An "identityref" value is mapped to a string representing the 322 qualified name of the identity. Its namespace MAY be expressed as 323 shown in Figure 1. If the namespace part is not present, the 324 namespace of the name of the JSON object containing the value is 325 assumed. 327 3.3.9. The "empty" Type 329 An "empty" value is mapped to '[null]', i.e., an array with the 330 'null' value being its only element. 332 This representation was chosen instead of using simply 'null' in 333 order to facilitate the use of "empty" leafs in common programming 334 languages. When used in a boolean context, the '[null]' value, 335 unlike 'null', evaluates to 'true'. 337 3.3.10. The "union" Type 339 YANG "union" type represents a choice among multiple alternative 340 types. The actual type of the XML value MUST be determined using the 341 procedure specified in Sec. 9.12 of [RFC6020] and the mapping rules 342 for that type are used. 344 3.3.11. The "instance-identifier" Type 346 An "instance-identifier" value is a string representing a simplified 347 XPath specification. It is mapped to an analogical JSON string in 348 which all occurrences of XML namespace prefixes are either removed or 349 replaced with the corresponding module name according to the rules of 350 Section 3.1. 352 When translating such a value from JSON to XML, all components of the 353 instance-identifier MUST be given appropriate XML namespace prefixes. 354 It is RECOMMENDED that these prefixes be those defined via the 355 "prefix" statement in the corresponding YANG modules. 357 3.4. Example 359 Consider a simple data model defined by the following YANG module: 361 module ex-json { 362 namespace "http://example.com/ex-json"; 363 prefix ej; 364 import ietf-inet-types { 365 prefix inet; 366 } 367 container top { 368 list address { 369 key "seqno"; 370 leaf seqno { 371 type uint8; 372 } 373 leaf ip { 374 type inet:ip-address; 375 mandatory true; 376 } 377 } 378 container phases { 379 typedef angle { 380 type decimal64 { 381 fraction-digits 2; 382 } 383 units "radians"; 384 } 385 leaf max-phase { 386 default "6.28"; 387 type angle; 388 } 389 leaf-list phase { 390 type angle; 391 must ". <= ../max-phase"; 392 min-elements 1; 393 } 394 } 395 } 396 } 398 Figure 2: Example YANG module. 400 By using the translation procedure defined in this document, we can 401 conclude that the following JSON text is valid according to the data 402 model: 404 { 405 "ex-json:top": { 406 "address": [ 407 { 408 "seqno": 1, 409 "ip": "192.0.2.1" 410 }, 411 { 412 "seqno": 2, 413 "ip": "2001:db8:0:1::1" 414 } 415 ], 416 "phases": { 417 "phase": [0.79, 1.04, 3.14] 418 } 419 } 420 } 422 Figure 3: Example JSON text. 424 Note that the semantic constraint specified by the "must" statement 425 in Figure 2 is satisfied by all elements of the "phase" array because 426 the default value of 6.28 is used for the absent "max-phase" leaf. 428 3.5. IANA Considerations 430 TBD. 432 3.6. Security Considerations 434 TBD. 436 3.7. Acknowledgments 438 The author wishes to thank Andy Bierman, Martin Bjorklund and Phil 439 Shafer for their helpful comments and suggestions. 441 4. References 443 4.1. Normative References 445 [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate 446 Requirement Levels", BCP 14, RFC 2119, March 1997. 448 [RFC4627] Crockford, D., "The application/json Media Type for 449 JavaScript Object Notation (JSON)", RFC 4627, July 2006. 451 [RFC6020] Bjorklund, M., Ed., "YANG - A Data Modeling Language for 452 Network Configuration Protocol (NETCONF)", RFC 6020, 453 September 2010. 455 [RFC6241] Enns, R., Bjorklund, M., Schoenwaelder, J., and A. 456 Bierman, "NETCONF Configuration Protocol", RFC 6241, 457 June 2011. 459 [XML] Bray, T., Paoli, J., Sperberg-McQueen, C., Maler, E., and 460 F. Yergeau, "Extensible Markup Language (XML) 1.0 (Fifth 461 Edition)", World Wide Web Consortium Recommendation REC- 462 xml-20081126, November 2008, 463 . 465 [XMLNS] Bray, T., Hollander, D., Layman, A., Tobin, R., and H. 466 Thompson, "Namespaces in XML 1.0 (Third Edition)", World 467 Wide Web Consortium Recommendation REC-xml-names-20091208, 468 December 2009, 469 . 471 4.2. Informative References 473 [XPath] Clark, J., "XML Path Language (XPath) Version 1.0", World 474 Wide Web Consortium Recommendation REC-xpath-19991116, 475 November 1999, 476 . 478 [YANG-API] 479 Bierman, A. and M. Bjorklund, "YANG-API Protocol", 480 draft-bierman-netconf-yang-api-00 (work in progress), 481 May 2012. 483 Author's Address 485 Ladislav Lhotka 486 CZ.NIC 488 Email: lhotka@nic.cz