Example Data Models and Instances

Doctest snippets in this documentation use the following example data models and their JSON-encoded instances.

Example 1

This example is used in the documentation of the datamodel module.

YANG Library

File: examples/ex1/yang-library-ex1.json

{
  "ietf-yang-library:modules-state": {
    "module-set-id": "ae4bf1ddf85a67ab94a9ab71593cd1c78b7f231d",
    "module": [
      {
        "name": "example-1",
        "namespace": "http://example.com/example-1",
        "revision": "",
        "conformance-type": "implement"
      }
    ]
  }
}

YANG Module example-1

File: examples/ex1/example-1.yang

module example-1 {

  yang-version "1.1";

  namespace "http://example.com/example-1";

  prefix "ex1";

  leaf greeting {
    type string;
    default "Hello, world!";
  }
}

Instance document

File: examples/ex1/example-data.json

{
  "example-1:greeting": "Hi!"
}

Example 2

This example is used in the documentation of the instance module, and also in Quick Start.

YANG Library

File: examples/ex2/yang-library-ex2.json

{
  "ietf-yang-library:modules-state": {
    "module-set-id": "9a9b7d2d28d4d78fa42e12348346990e3fb1c1b9",
    "module": [
      {
        "name": "example-2",
        "namespace": "http://example.com/example-2",
        "revision": "",
        "conformance-type": "implement"
      }
    ]
  }
}

YANG Module example-2

File: examples/ex2/example-2.yang

module example-2 {

  yang-version "1.1";

  namespace "http://example.com/example-2";

  prefix "ex2";

  container bag {
    list foo {
      key "number";
      unique "in-words";
      leaf number {
        type uint8;
      }
      leaf prime {
        type boolean;
	default false;
      }
      leaf in-words {
        type string {
          pattern "[a-z][a-z\\-]+[a-z]" {
	    error-message "must be number in words";
	  }
        }
      }
    }
    leaf bar {
      type boolean;
      mandatory "true";
    }
    leaf baz {
      when "not(../foo/in-words = 'forty-two')";
      type decimal64 {
        fraction-digits "7";
      }
      default "0";
    }
  }
}

Instance document

File: examples/ex2/example-data.json

{
  "example-2:bag": {
    "foo": [
      {
        "number": 6,
        "in-words": "six"
      },
      {
        "number": 3,
        "prime": true,
        "in-words": "three"
      },
      {
        "number": 7,
        "prime": true,
        "in-words": "seven"
      },
      {
        "number": 8,
        "in-words": "eight"
      }
    ],
    "bar": true
  }
}

Example 3

This example is used in the documentation of the schemadata module.

YANG Library

File: examples/ex3/yang-library-ex3.json

{
  "ietf-yang-library:modules-state": {
    "module-set-id": "5798f6c8acd78ccb1171cb36d34fb95ca797c769",
    "module": [
      {
        "name": "example-3-a",
        "feature": [
          "fea1",
          "fea2"
        ],
        "namespace": "http://example.com/example-3/a",
        "revision": "2017-08-01",
        "conformance-type": "implement",
	      "submodule": [
	        {
	          "name": "example-3-suba",
	          "revision": "2017-08-01"
	        }
	      ]
      },
      {
        "name": "example-3-b",
        "namespace": "http://example.com/example-3/b",
        "revision": "2016-08-22",
        "conformance-type": "implement"
      },
      {
        "name": "ietf-yang-metadata",
        "namespace": "urn:ietf:params:xml:ns:yang:ietf-yang-metadata",
        "revision": "2016-08-05",
        "conformance-type": "implement"
      },
      {
        "name": "ietf-inet-types",
        "namespace": "urn:ietf:params:xml:ns:yang:ietf-inet-types",
        "revision": "2010-09-24",
        "conformance-type": "import"
      },
      {
        "name": "ietf-yang-types",
        "namespace": "urn:ietf:params:xml:ns:yang:ietf-yang-types",
        "revision": "2013-07-15",
        "conformance-type": "import"
      },
      {
        "name": "ietf-inet-types",
        "namespace": "urn:ietf:params:xml:ns:yang:ietf-inet-types",
        "revision": "2013-07-15",
        "conformance-type": "import"
      }
    ]
  }
}

YANG Module example-3-a

File: examples/ex3/example-3-a@2017-08-01.yang

module example-3-a {

  yang-version "1.1";

  namespace "http://example.com/example-3/a";

  prefix "ex3a";

  import ietf-yang-types {
    prefix "yang";
  }
  import ietf-yang-metadata {
    prefix "md";
  }

  include example-3-suba {
    revision-date 2016-07-21;
  }

  revision 2017-08-01;

  feature fea1;

  feature fea2;

  identity idX;

  md:annotation last-modified {
    type yang:date-and-time;
    description
      "This annotation contains the date and time when the
       annotated instance was last modified (or created).";
  }
  
  grouping gbar {
    leaf bar {
      type string;
    }
  }

  container top {
    uses gquux;
    leaf foo {
      if-feature "fea1";
      type empty;
    }
  }
}

YANG Submodule example-3-suba

File: examples/ex3/example-3-suba@2017-08-01.yang

submodule example-3-suba {

  yang-version "1.1";

  belongs-to example-3-a {
    prefix "ex3a";
  }

  import ietf-inet-types {
    prefix "inet";
    revision-date 2013-07-15;
  }

  revision 2017-08-01;

  grouping gquux {
    leaf quux {
      type uint8;
    }
  }

  augment "/ex3a:top" {
    uses gbar;
    leaf baz {
      if-feature "fea2";
      type inet:ipv4-address-no-zone;
    }
  }
}

YANG Module example-3-b

File: examples/ex3/example-3-b@2016-08-22.yang

module example-3-b {

  yang-version "1.1";

  namespace "http://example.com/example-3/b";

  prefix "ex3b";

  import ietf-inet-types {
    prefix "oin";
    revision-date 2010-09-24;
  }

  import example-3-a {
    prefix "ex3a";
  }

  revision 2016-08-22;

  identity idY;

  identity idZ {
    base ex3a:idX;
    base idY;
  }

  augment "/ex3a:top" {
    if-feature "ex3a:fea1 or ex3a:fea2";
    uses ex3a:gbar;
    leaf baz {
      type oin:port-number;
    }
  }
}

Example 4

This example is used in the documentation of the schemanode, xpathast and xpathparser modules.

YANG Library

File: examples/ex4/yang-library-ex4.json

{
  "ietf-yang-library:modules-state": {
    "module-set-id": "b5e2c7aa84bd40f60c853ceefb69031ed8f9b96b",
    "module": [
      {
        "name": "example-4-a",
        "namespace": "http://example.com/example-4-a",
        "revision": "",
        "conformance-type": "implement"
      },
      {
        "name": "example-4-b",
        "namespace": "http://example.com/example-4-b",
        "revision": "",
        "conformance-type": "implement"
      },
      {
        "name": "ietf-yang-types",
        "namespace": "urn:ietf:params:xml:ns:yang:ietf-yang-types",
        "revision": "2013-07-15",
        "conformance-type": "import"
      },
      {
        "name": "ietf-netconf-acm",
        "namespace": "urn:ietf:params:xml:ns:yang:ietf-netconf-acm",
        "revision": "2018-02-14",
        "conformance-type": "import"
      }
    ]
  }
}

YANG Module example-4-a

File: examples/ex4/example-4-a.yang

module example-4-a {

  yang-version "1.1";

  namespace "http://example.com/example-4-a";

  prefix "ex4a";

  import ietf-netconf-acm {
    prefix "nacm";
    revision-date 2018-02-14;
  }

  container bag {
    description "Top-level container.";
    presence "true";
    leaf foo {
      type uint8;
      mandatory "true";
      nacm:default-deny-write;
    }
    leaf bar {
      type boolean;
      default "true";
      config "false";
    }
    choice opts {
      default "a";
      case a {
        leaf baz {
          type empty;
        }
      }
    }
  }
}

YANG Module example-4-b

File: examples/ex4/example-4-b.yang

module example-4-b {

  yang-version "1.1";

  namespace "http://example.com/example-4-b";

  prefix "ex4b";

  import example-4-a {
    prefix "ex4a";
  }

  leaf-list quux {
    type decimal64 {
      fraction-digits "4";
    }
    max-elements "2";
    ordered-by "user";
  }

  augment "/ex4a:bag/ex4a:opts" {
    when "/ex4b:quux = 0";
    leaf fooref {
      type leafref {
        path "../ex4a:foo";
      }
    }
  }
}

Instance document

File: examples/ex4/example-data.json

{
  "example-4-a:bag": {
    "foo": 42,
    "bar": false,
    "example-4-b:fooref": 42
  },
  "example-4-b:quux": [
    "3.1415",
    "0"
  ]
}

Example 5

This example is used in the documentation of the datatype and statement modules.

YANG Library

File: examples/ex5/yang-library-ex5.json

{
  "ietf-yang-library:modules-state": {
    "module-set-id": "d540849211ed999e1d41f89f2d6353d26f0b2915",
    "module": [
      {
        "name": "example-5-a",
        "namespace": "http://example.com/example-5-a",
        "revision": "2018-10-25",
        "conformance-type": "implement"
      },
      {
        "name": "example-5-b",
        "namespace": "http://example.com/example-5-b",
        "revision": "",
        "conformance-type": "implement"
      }
    ]
  }
}

YANG Module example-5-a

File: examples/ex5/example-5-a.yang

module example-5-a {

  yang-version "1.1";

  namespace "http://example.com/example-5-a";

  prefix "ex5a";

  import example-5-b {
    prefix "ex5b";
  }

  revision 2018-10-25;

  typedef my-string {
    type string {
      length "2..4";
      pattern "x*y" {
	error-message "xes and y";
      }
    }
    default "xxy";
  }

  leaf binary-leaf {
    type binary;
  }

  leaf bits-leaf {
    type bits {
      bit un {
        position "1";
      }
      bit dos;
      bit tres;
    }
  }

  leaf boolean-leaf {
    type boolean;
  }

  leaf decimal64-leaf {
    type decimal64 {
      fraction-digits "4";
    }
  }

  leaf empty-leaf {
    type empty;
  }

  leaf enumeration-leaf {
    type enumeration {
      enum Bashful;
      enum Doc;
      enum Dopey;
      enum Grumpy;
      enum Happy;
      enum Sleepy;
      enum Sneezy;
    }
  }

  leaf identityref-leaf {
    type identityref {
      base ex5b:base-identity;
    }
    default "ex5b:derived-identity";
  }

  leaf instance-identifier-leaf {
    type instance-identifier;
  }

  leaf leafref-leaf {
    type leafref {
      path "/string-leaf";
      require-instance "true";
    }
  }

  leaf string-leaf {
    type my-string;
  }

  leaf union-leaf {
    type union {
      type my-string;
      type boolean;
    }
  }
}

YANG Module example-5-b

File: examples/ex5/example-5-b.yang

module example-5-b {

  yang-version "1.1";

  namespace "http://example.com/example-5-b";

  prefix "ex5b";

  identity base-identity;

  identity derived-identity {
    base base-identity;
  }
}