This tutorial is adapated from the YANG tutorial presented at IETF 71. 

YANG Overview:
YANG is a data modeling language used to model configuration and state data manipulated by the Network Configuration Protocol NETCONF), NETCONF remote procedure calls, and NETCONF notifications.

YANG modules defines the netconf data tree and generally has the following structure:

Header statements
yang-version, namespace, prefix
Linkage statement
import and include
Meta information
organization, contact
Revision history
revision
Data definitions
data nodes

YANG Sample Module:

module acme-module {
namespace "http://acme.example.com/module";
prefix acme;
import "yang-types" {
prefix yang;
}
include "acme-system";
organization "ACME Inc.";
contact This email address is being protected from spambots. You need JavaScript enabled to view it.;
description "The module for entities
implementing the ACME products";
revision 2007-06-09 {
description "Initial revision.";
}
data definitions...
}


Leaf statement:

A leaf has only one instance value and has no children.

YANG Example:
leaf host-name {
type string;
description "Hostname...";
}


NETCONF XML Encoding:
<host-name>my.example.com</host-name>

Leaf-list statements:
A leaf-list has only multiple instances value and simillar to leaf node it has no children.

YANG Example:

leaf-list domain-search {
type string;
description "List of domain names to search";
}

NETCONF XML Encoding:

<domain-search>high.example.com</domain-search>
<domain-search>low.example.com</domain-search>
<domain-search>everywhere.example.com</domain-search>

List statement:
A list is uniquely identified by key(s) and holds related children. It can have multiple instances.
YANG Example:

list user {
key name;
leaf name {
type string;
}
 leaf uid {
 type uint32;
}
leaf full-name {
  type string;
 }
leaf class {
  type string;
default viewer;
}
}

NETCONF XML Encoding:
<user>
<name>glocks</name>
<full-name>Goldie</full-name>
<class>intruder</class>
</user>
<user>
<name>snowey</name>
<full-name>Snow</full-name>
<class>free-loader</class>
</user>
<user>
<name>rzull</name>
<full-name>Repun</full-name>
</user>

 Container statement:
A container node contains other nodes. It does not have real meaning, unless it has <presence> substatement that assigns it a specific meaning in the data tree.

YANG Example:
container system {
container services {
container ssh {
 presence "Enables SSH";
 description "SSH service specific configuration";
// more leafs, containers and stuff here...
}
}
}
NETCONF XML Encoding:
<system>
<services>
<ssh/>
</services>
</system>

Choice statement

This statement allow only one member of the choice to exist in a valid config datastore.


YANG Example:

choice transfer-method {
leaf transfer-interval {
description "Frequency at which file transfer happens";
 type uint {
range "15 .. 2880";
 }
units minutes;
}
leaf transfer-on-commit {
description "Transfer after each commit";
 type empty;
}
}


NETCONF XML Encoding:

<transfer-on-commit/>


Buit-in Types:

Integral: int8,int16, int32, int64, ,uint8,uint16,unit32, uint64
String: string, enumeration, boolean (true or false)
Binary Data: binary
Bit fields: bits
References: instance-identifier, keyref
Other: empty


Define New Types 
YANG Example:

typedef percent {
type uint16 {
  range "0 .. 100";
}
description "Percentage";
}
leaf completed {
type percent;
}

NETCONF XML Encoding:

<completed>20</completed>


RPC Statement
This statement defines RPC. YANG example:

rpc activate-software-image {
input {
leaf image-name {
type string;
}
}
output {
 leaf status {
 type string;
 }
}
}
<rpc xmlns="urn:mumble">
<activate-software-image>
<image-name>image.tgz</image-name>
 </activate-software-image>
</rpc>


Notification statement

This statement defines notification. YANG Example:

notification link-failure {
description "A link failure has been detected";
leaf if-index {
type int32 { range "1 .. max"; }
}
leaf if-name {
type keyref {
path "/interfaces/interface/name";
}
 }
}

Semantics Statements:

See RFC6020 for details on the following statements:

unique: Ensure unique values within list siblings

keyref: Ensure referential integrity
config: Indicate if a node is config data or not

default: Supply default value for leafs
error-app-tag: Define the tag used when constraint fails
error-message:Define the message used ....
mandatory: Node must exist in valid config datastore

when: Node only exist if the when condition holds true
must: Formally define constraints on valid data
range, length, pattern, regex: Define constraints on integer and string type nodes