r12 - 2011-02-16 - 17:48:08 - LadaLhotkaYou are here: TWiki >  Main Web > YangTools > DSDLMappingTutorial

DSDL Mapping Tutorial


One of the functions implemented in pyang is the mapping of YANG data models to DSDL schemas – RELAX NG, Schematron and DSRL. The resulting schemas may be used with off-the-shelf XML tools for validating NETCONF datastores and messages and other related purposes.

The mapping procedure is described in RFC 6110.

This tutorial works with a very simple data model consisting of two YANG modules. We first show how to perform the DSDL mapping procedure with the yang2dsdl script that is a part of pyang distribution, and then experiment with validating configuration datastore contents.

Help The software tools presented in this tutorial produce compact XML data with no intervening whitespace. The corresponding files that are attached to this wiki page and referred to in the text are their pretty-printed versions obtained by using xmllint.

The Data Model

As an example, we use the YANG module acme-system shown at the bottom of WebHome (see also acme-system.yang). To make things slightly more interesting, we augment this module by adding three new nodes to the entries of the "interface" list. This is accomplished by the following module (see also extra-interface-nodes.yang):

module extra-interface-nodes {
  namespace "http://foo.example.com/extra-if-nodes";
  prefix ein;
  import acme-system {
    prefix acme;
  import ietf-inet-types {
    prefix inet;
  augment "/acme:system/acme:interface" {
    leaf enabled {
      type boolean;
      default "false";
      must ".='false' or ../ipv4-address and ../subnet-mask-length" {
        error-message "IP address and mask must be configured " +
                      "for enabled interfaces.";
    leaf ipv4-address {
      type inet:ipv4-address;
    leaf subnet-mask-length {
      type uint8 {
        range "0..32";

Our data model now defines a compound document containing nodes in the namespaces of both the contributing modules, acme-system and extra-interface-nodes.

Generating the Schemas

The DSDL schemas can be generated quite easily (at least on Linux and Unix systems) from the two YANG modules by using the yang2dsdl shell script which is provided with the pyang distribution. The details about its usage can be found in the manual page or by running yang2dsdl -h.

But before we start, we need to decide which target document type the generated schemas are to address. Currently, yang2dsdl supports the following targets that are selected via the -t command line option:

datastore contents (configuration and state data) encapsulated in <nc:data> document element;
configuration datastore contents encapsulated in <nc:config> document element.
a complete NETCONF message containing a reply to <nc:get> operation;
a complete NETCONF message containing a reply to <nc:get-config> operation;
RPC request;
RPC reply;
event notification.

Help The nc prefix denotes the NETCONF namespace associated with URI "urn:ietf:params:xml:ns:netconf:base:1.0".

Only the first four targets make sense in our case as the YANG modules define neither RPC operations nor event notifications. Moreover, there are no operational state data or statistics in our modules, so the replies to <get> and <get-config> are bound to be identical.

Another detail to consider are the file names in which the output schemas will be stored. The names have the following general form:


The first part, BASE, can be chosen by the user while the other two cannot. TARGET is always set to the selected target (see above) and EXT is a file extension specific for each DSDL schema language – rng is used for RELAX NG, sch for Schematron and dsrl for DSRL.

If the BASE part isn't provided by the user, the script constructs a default one, which is a concatenation of the names of all input YANG modules connected with the underscore character _. The result in our example – acme-system_extra-interface-nodes – would lead to rather long file names. We therefore decide to specify a shorter BASE part explicitly, say aug-acme-system.

All right, let's generate the DSDL schemas for the "data" target. We needn't use the -t option here as "data" is the default:

$ yang2dsdl -b aug-acme-system acme-system.yang extra-interface-nodes.yang
== Generating RELAX NG schema './aug-acme-system-data.rng'

== Generating Schematron schema './aug-acme-system-data.sch'

== Generating DSRL schema './aug-acme-system-data.dsrl'

As indicated by the program output, the resulting DSDL schemas were written to three files:

But wait, the script also created a fourth file, namely aug-acme-system-gdefs.rng. This is actually a part of the RELAX NG schema containing all global definitions (mapped from YANG top-level groupings and typedefs). For technical reasons, these definitions have to be included from an external file.

An Instance Configuration Datastore

In order to be able to see the generated DSDL schemas in action, we need to create an XML document containing, in our case, a "raw" configuration datastore. If we have an appropriate schema-aware editor, we can provide it with the RELAX NG schema (or Schematron as well). An excellent choice is GNU Emacs with James Clark's nXML mode.

The following is a valid datastore content (see also aug-acme-system-data.xml):

<?xml version="1.0" encoding="utf-8"?>
<nc:data xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
  <system xmlns="http://acme.example.com/system"
    <domain-search>acme.example.com foo.example.com</domain-search>

Emacs with nXML mode is also a user-friendly RELAX NG validator. For example, we can try to change the value of the <ein:subnet-mask-length> element to 42, which violates a datatype constraint – this value must be in the range between 0 and 32. As a result, the wrong value is underlined in Emacs and the status line indicates that the edited XML document is invalid.


We can use the yang2dsdl script for validating the above datastore content (the -s flag instructs the script to use the existing DSDL schemas, without it the schemas would be regenerated):

$ yang2dsdl -s -b aug-acme-system -v aug-acme-system-data.xml
== Using pre-generated schemas

== Validating grammar and datatypes ...
aug-acme-system-data.xml validates

== Adding default values... done.

== Validating semantic constraints ...
No errors found.

Sure enough, no errors were detected in the instance document. Now, let's change the value of the <ein:subnet-mask-length> element to 42 again and re-run the same command:

$ yang2dsdl -s -b aug-acme-system -v aug-acme-system-data.xml
== Using pre-generated schemas

== Validating grammar and datatypes ...
Relax-NG validity error : Extra element subnet-mask-length in interleave
aug-acme-system-data.xml:12: element subnet-mask-length: Relax-NG validity error : Element interface failed to validate content
Relax-NG validity error : Extra element interface in interleave
aug-acme-system-data.xml:6: element interface: Relax-NG validity error : Element system failed to validate content
aug-acme-system-data.xml fails to validate

The error message is not particularly informative – it is a direct output of the RELAX NG validator built in the xmllint tool – but at least the conclusion in the last line is correct.

Next, change the value of <ein:subnet-mask-length> back to 24 but also introduce two new errors in the second <interface> entry:

  1. Change the value of <name> from eth1 to eth0. Both list entries now have the same key.
  2. Add a new element:
    This violates the constraint stated by the 'must' statement in the extra-interface-nodes module.

In this case, RELAX NG validators won't complain since the instance document remains grammatically valid and all datatype constraints are satisfied as well. The errors we deal with here are semantic. So let's try the validation with yang2dsdl again:

$ yang2dsdl -s -b aug-acme-system -v aug-acme-system-data.xml
== Using pre-generated schemas

== Validating grammar and datatypes ...
aug-acme-system-data.xml validates

== Adding default values... done.

== Validating semantic constraints ...
--- Validity error at "/acme:system/acme:interface":
    Duplicate key "acme:name"
--- Failed assert at "/acme:system/acme:interface/ein:enabled":
    IP address and mask must be configured for enabled interfaces.

The error messages are now very clear, Schematron rocks!

Feel free to make any other modifications in the instance document and re-run the validation to see what happens.

Developers and technically inclined readers may find further technical details about the DSDL mapping and instance validation in DSDLTechDetails.

Edit | WYSIWYG | Attach | Printable | Raw View | Backlinks: Web, All Webs | History: r12 < r11 < r10 < r9 < r8 | More topic actions
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback