idnits 2.17.1 draft-lhotka-netmod-yang-json-00.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 (October 05, 2012) is 4214 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 October 05, 2012 5 Expires: April 8, 2013 7 Modeling JSON Text with YANG 8 draft-lhotka-netmod-yang-json-00 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 April 8, 2013. 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 . . . . . . . . . . . . . . . . . . . 8 62 3.3.6. The "binary" Type . . . . . . . . . . . . . . . . . . 8 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 . . . . . . . . . . . . . . . . . . . . . . . . . 9 69 3.5. IANA Considerations . . . . . . . . . . . . . . . . . . . 11 70 3.6. Security Considerations . . . . . . . . . . . . . . . . . 11 71 3.7. Acknowledgments . . . . . . . . . . . . . . . . . . . . . 11 72 4. References . . . . . . . . . . . . . . . . . . . . . . . . . . 12 73 4.1. Normative References . . . . . . . . . . . . . . . . . . . 12 74 4.2. Informative References . . . . . . . . . . . . . . . . . . 12 75 Author's Address . . . . . . . . . . . . . . . . . . . . . . . . . 13 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 are rarely needed in JSON text. 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 feature 150 o identity 152 o instance identifier 154 o leaf 156 o leaf-list 158 o list 160 o module 162 o submodule 164 The following terms are defined in [XMLNS]: 166 o local name 168 o prefixed name 170 o qualified name 172 3. Specification of the Translation Procedure 174 The translation procedure defines a 1-1 correspondence between the 175 subset of YANG-compatible XML documents and JSON text. This means 176 that the translation can be applied in both directions and is always 177 invertible. 179 Any YANG-compatible XML document can be translated, except documents 180 with mixed content. This is only a minor limitation since mixed 181 content is marginal in YANG - it is allowed only in "anyxml" nodes. 183 The following subsections specify rules mainly for translating XML 184 documents to JSON text. Rules for the inverse translation are stated 185 only where necessary, otherwise they can be easily inferred. 187 REQUIRED parameters of the translation procedure are: 189 o YANG data model, 191 o type of the input XML document, 193 o optional features (defined via the "feature" statement) that are 194 considered active. 196 The permissible types of XML documents are listed in Table 1 together 197 with the corresponding part of the data model that is used for the 198 translation. 200 +------------------------------+--------------------------------+ 201 | Document Type | Data Model Section | 202 +------------------------------+--------------------------------+ 203 | configuration and state data | main data tree | 204 | | | 205 | configuration | main data tree ("config true") | 206 | | | 207 | RPC input parameters | "input" nodes under "rpc" | 208 | | | 209 | RPC output parameters | "output" nodes under "rpc" | 210 | | | 211 | notification | "notification" nodes | 212 +------------------------------+--------------------------------+ 214 Table 1: YANG Document Types 216 A particular application may decide to use only a subset of document 217 types from Table 1. For instance, YANG-API Protocol [YANG-API] does 218 not use notifications. 220 XML documents can be translated to JSON text only if they are valid 221 instances of the YANG data model and selected document type, also 222 taking into account the active features, if there are any. 224 3.1. Names and Namespaces 226 The local part of a JSON name is always identical to the local name 227 of the corresponding XML element. 229 Each JSON name lives in a namespace which is uniquely identified by 230 the name of the YANG module where the corresponding data node is 231 defined. If the data node is defined in a submodule, then the 232 namespace identifier is the name of the main module to which the 233 submodule belongs. The translation procedure MUST correctly map YANG 234 namespace URIs to YANG module names and vice versa. 236 The namespace SHALL be expressed in JSON text by prefixing the local 237 name in the following way: 239 : 241 Figure 1: Encoding a namespace identifier with a local name. 243 The namespace identifier MUST be used for local names that are 244 ambiguous, i.e., whenever the data model permits a sibling node with 245 the same local name. Otherwise, the namespace identifier is 246 OPTIONAL. 248 When mapping namespaces from JSON text to XML, the resulting XML 249 document may use default namespace declarations (via the "xmlns" 250 attribute), prefix-based namespace declarations (via attributes 251 beginning with "xmlns:"), or any combination thereof following the 252 rules stated in [XMLNS]. If prefixed names are used, their prefix 253 SHOULD be the one defined by the "prefix" statement in the YANG 254 module where each data node is defined. 256 3.2. Mapping XML Elements to JSON Objects 258 XML elements are translated to JSON objects in a straightforward way: 260 o An XML element which is modeled as YANG leaf is translated to a 261 name/value pair and the JSON datatype of the value is derived from 262 the YANG datatype of the leaf (see Section 3.3 for the datatype 263 mapping rules). 265 o An XML element which is modeled as YANG container is translated to 266 a JSON object. 268 o A sequence of one or more sibling XML elements with the same 269 qualified name, which is modeled as YANG list or leaf-list, is 270 translated to a name/array pair. If the sequence is modeled as a 271 leaf-list in YANG, then the array elements are primitive values 272 whose type depends on the datatype of the leaf-list (see 273 Section 3.3). If the sequence is modeled as a list in YANG, then 274 the array elements are JSON objects. 276 Note that the same XML element may be translated in different ways, 277 depending on the YANG data model. For example, 279 42 281 is translated to 283 "foo": 42 285 if the "foo" node is defined as a leaf with the "uint8" datatype, or 286 to 288 "foo": ["42"] 290 if the "foo" node is defined as a leaf-list with the "string" 291 datatype. 293 3.3. Mapping YANG Datatypes to JSON Values 295 3.3.1. Numeric Types 297 A value of one of the YANG numeric types ("int8", "int16", "int32", 298 "int64", "uint8", "uint16", "uint32", "uint64" and "decimal64") is 299 mapped to a JSON number using the same lexical representation. 301 3.3.2. The "string" Type 303 A "string" value is mapped to an identical JSON string, subject to 304 JSON encoding rules. 306 3.3.3. The "boolean" Type 308 A "boolean" value is mapped to the corresponding JSON value 'true' or 309 'false'. 311 3.3.4. The "enumeration" Type 313 An "enumeration" value is mapped in the same way as a string except 314 that the permitted values are defined by "enum" statements in YANG. 316 3.3.5. The "bits" Type 318 A "bits" value is mapped to a string identical to the lexical 319 representation of this value in XML, i.e., space-separated names 320 representing the individual bit values that are set. 322 3.3.6. The "binary" Type 324 A "binary" value is mapped to a JSON string identical to the lexical 325 representation of this value in XML, i.e., base64-encoded binary 326 data. 328 3.3.7. The "leafref" Type 330 A "leafref" value is mapped according to the same rules as the type 331 of the leaf being referred to. 333 3.3.8. The "identityref" Type 335 An "identityref" value is mapped to a string representing the 336 qualified name of the identity. Its namespace MAY be expressed as 337 shown in Figure 1. If the namespace part is not present, the 338 namespace of the name of the JSON object containing the value is 339 assumed. 341 3.3.9. The "empty" Type 343 An "empty" value is mapped to '[null]', i.e., an array with the 344 'null' value being its only element. 346 This representation was chosen instead of using simply 'null' in 347 order to facilitate the use of empty leafs in common programming 348 languages. When used in a boolean context, the '[null]' value, 349 unlike 'null', evaluates to 'true'. 351 3.3.10. The "union" Type 353 YANG "union" type represents a choice among multiple alternative 354 types. The actual type of the XML value MUST be determined using the 355 procedure specified in Sec. 9.12 of [RFC6020] and the mapping rules 356 for that type are used. 358 3.3.11. The "instance-identifier" Type 360 An "instance-identifier" value is a string representing a simplified 361 XPath specification. It is mapped to an analogical JSON string in 362 which all occurrences of XML namespace prefixes are either removed or 363 replaced with the corresponding module name according to the rules of 364 Section 3.1. 366 When translating such a value from JSON to XML, all components of the 367 instance-identifier MUST be given appropriate XML namespace prefixes. 368 It is RECOMMENDED that these prefixes be those defined via the 369 "prefix" statement in the corresponding YANG modules. 371 3.4. Example 373 Consider a simple data model defined by the following YANG module: 375 module ex-json { 377 namespace "http://example.com/ex-json"; 379 prefix "ej"; 381 import ietf-inet-types { 382 prefix "inet"; 383 } 385 container top { 386 list address { 387 key "seqno"; 388 leaf seqno { 389 type uint8; 390 } 391 leaf ip { 392 type inet:ip-address; 393 mandatory "true"; 394 } 395 } 396 container phases { 397 typedef angle { 398 type decimal64 { 399 fraction-digits "2"; 400 } 401 units "radians"; 402 } 403 leaf max-phase { 404 default "6.28"; 405 type angle; 406 } 407 leaf-list phase { 408 type angle; 409 must ". <= ../max-phase"; 410 min-elements "1"; 411 } 412 } 413 } 414 } 416 Figure 2: Example YANG module. 418 By using the translation procedure defined in this document, we can 419 conclude that the following JSON text is valid according to the data 420 model: 422 { 423 "top": { 424 "address": [ 425 { 426 "seqno": 1, 427 "ip": "192.0.2.1" 428 }, 429 { 430 "seqno": 2, 431 "ip": "2001:db8:0:1::1" 432 } 433 ], 434 "phases": { 435 "phase": [ 436 "0.79", 437 "1.04", 438 "3.14" 439 ] 440 } 441 } 442 } 444 Figure 3: Example JSON text. 446 3.5. IANA Considerations 448 TBD. 450 3.6. Security Considerations 452 TBD. 454 3.7. Acknowledgments 456 The author wishes to thank Andy Bierman, Martin Bjorklund and Phil 457 Shafer for their helpful comments and suggestions. 459 4. References 461 4.1. Normative References 463 [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate 464 Requirement Levels", BCP 14, RFC 2119, March 1997. 466 [RFC4627] Crockford, D., "The application/json Media Type for 467 JavaScript Object Notation (JSON)", RFC 4627, July 2006. 469 [RFC6020] Bjorklund, M., Ed., "YANG - A Data Modeling Language for 470 Network Configuration Protocol (NETCONF)", RFC 6020, 471 September 2010. 473 [RFC6241] Enns, R., Bjorklund, M., Schoenwaelder, J., and A. 474 Bierman, "NETCONF Configuration Protocol", RFC 6241, 475 June 2011. 477 [XML] Bray, T., Paoli, J., Sperberg-McQueen, C., Maler, E., and 478 F. Yergeau, "Extensible Markup Language (XML) 1.0 (Fifth 479 Edition)", World Wide Web Consortium Recommendation REC- 480 xml-20081126, November 2008, 481 . 483 [XMLNS] Bray, T., Hollander, D., Layman, A., Tobin, R., and H. 484 Thompson, "Namespaces in XML 1.0 (Third Edition)", World 485 Wide Web Consortium Recommendation REC-xml-names-20091208, 486 December 2009, 487 . 489 4.2. Informative References 491 [XPath] Clark, J., "XML Path Language (XPath) Version 1.0", World 492 Wide Web Consortium Recommendation REC-xpath-19991116, 493 November 1999, 494 . 496 [YANG-API] 497 Bierman, A. and M. Bjorklund, "YANG-API Protocol", 498 draft-bierman-netconf-yang-api-00 (work in progress), 499 May 2012. 501 Author's Address 503 Ladislav Lhotka 504 CZ.NIC 506 Email: lhotka@nic.cz