module test-module {
  yang-version 1.1;
  namespace "urn:ietf:params:xml:ns:yang:test-module";
  prefix tm;

  import ietf-inet-types {
    prefix inet;
  }

  import referenced-data {
    prefix rd;
  }

  organization "organization";
  description
    "example yang module";
  contact
    "example@example.org";

  typedef threshold-power-dBm {
    type union {
      type decimal64 {
        fraction-digits 2;
      }
      type enumeration {
        enum off {
          description "No threshold configured";
        }
      }
    }
    description "Power in dBm";
  }

  container main {
    leaf enum {
      type enumeration {
        enum "yes" {
          value 1;
        }
        enum "no" {
          value 2;
        }
        enum "maybe" {
          value 3;
        }
      }
    }
    leaf options {
      type bits {
        bit strict;
        bit recursive;
        bit logging;
      }
    }
    leaf raw {
      type binary;
    }
    leaf dec64 {
      type decimal64{
         fraction-digits 2;
      }
    }
    leaf i8 {
      type int8;
    }
    leaf i16 {
      type int16;
    }
    leaf i32 {
      type int32;
    }
    leaf i64 {
      type int64;
    }
    leaf ui8 {
      type uint8;
    }
    leaf ui16 {
      type uint16;
    }
    leaf ui32 {
      type uint32;
    }
    leaf ui64 {
      type uint64;
    }
    leaf empty {
      type empty;
    }
    leaf boolean {
      type boolean;
    }
    leaf string {
      type string;
    }
    leaf id_ref {
      type identityref{
        base base_id;
      }
    }
    leaf-list numbers {
      type uint8;
    }
    leaf instance_id {
      type instance-identifier;
    }
    anyxml xml-data;
    anydata any-data;
  }

  identity base_id;

  identity id_1 {
    base base_id;
  }

  identity id_2 {
    base base_id;
  }

  list list {
    key "key";
    leaf key {
      type string;
    }
    leaf id_ref {
      type identityref{
        base base_id;
      }
    }
    leaf instance_id {
       type instance-identifier;
    }
    leaf union {
      type union{
          type uint8;
          type enumeration {
              enum "infinity";
          }
      }
    }
    container wireless{
        presence "wireless is enabled";
        leaf vendor_name{
          type string;
        }
    }
  }

  container transfer {
    choice how {
        default interval;
        case interval {
            leaf interval {
                type uint16;
                default 30;
                units minutes;
            }
        }
        case daily {
            leaf daily {
                type empty;
            }
            leaf time-of-day {
                type string;
                units 24-hour-clock;
                default 1am;
            }
        }
        case manual {
            leaf manual {
                type empty;
            }
        }
    }
  }

  container location {
    presence true;
    leaf name {
        type string;
    }

    leaf longitude {
        type string;
        mandatory true;
    }

    leaf latitude {
        type string;
        mandatory true;
    }
  }

  leaf hexnumber{
    type string {
            length "0..4";
            pattern "[0-9a-fA-F]*";
        }
  }

  container interface {
    leaf ifType {
        type enumeration {
          enum ethernet;
            enum atm;
        }
    }
    leaf ifMTU {
        type uint32;
    }
    must "count(ifType) = 0 or ifType != 'ethernet' or " +
         "(ifType = 'ethernet' and ifMTU = 1500)" {
         error-message "An ethernet MTU must be 1500";
    }
    must "count(ifType) = 0 or ifType != 'atm' or " +
         "(ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)" {
         error-message "An atm MTU must be  64 .. 17966";
    }
 }

  list user {
    description "This is a list of users in the system.";
    ordered-by user;
    config true;

    key "name";

    leaf name {
        type string;
    }
    leaf type {
        type string;
    }
    leaf full-name {
        type string;
    }
  }

  leaf-list ordered-numbers {
    ordered-by user;
    type uint8;
  }

  list with_def {
    config true;
    key "name";
    leaf name {
        type string;
    }
    leaf num {
        type int8;
        default 0;
    }
  }

  rpc activate-software-image {
    input {
      leaf image-name {
        type string;
        must ". != /top-level-default";
      }
      leaf location {
        type string;
        default "/";
        must ". != 'invalid location'";
      }
    }
    output {
      leaf status {
        type string;
        must ". != 'invalid status'";
      }
      leaf version {
        type string;
      }
      leaf location {
        type string;
        default "/";
      }
      container init-log {
        list log-msg {
          key "msg time";
          leaf msg {
            type string;
          }
          leaf time {
            type uint32;
          }
          leaf msg-type {
            type enumeration {
              enum "error" {
                value 1;
              }
              enum "warning" {
                value 2;
              }
              enum "debug" {
                value 3;
              }
            }
          }
        }
      }
    }
  }

  leaf top-level-default {
    type string;
    default "default value";
  }

  container university {
    container students {
        list student {
            ordered-by user;
            config true;

            key "name";

            leaf name {
                type string;
            }

            leaf age {
                type uint8;
            }
        }
    }

    container classes {
        list class {
            config true;

            key "title";

            leaf title {
                type string;
            }

            list student {
                key "name";

                leaf name {
                    type leafref {
                        path "../../../../students/student/name";
                    }
                }
                leaf age {
                    type leafref {
                        path "../../../../students/student[name = current()/../name]/age";
                    }
                }
            }
        }
    }
  }

  container leafref-chain {
    leaf A {
        type leafref {
            path "../B";
        }
    }

    leaf B {
        type leafref {
            path "../C";
        }
    }

    leaf C {
        type leafref {
            path "../D";
        }
    }

    leaf D {
        type string;
    }
  }

  grouping link {
     container source {
       leaf address {
         type inet:ipv4-address;
       }
       leaf interface {
         type string;
       }
     }
     container destination {
       leaf address {
         type inet:ipv4-address;
       }
       leaf interface {
         type string;
       }
     }
     leaf MTU {
       type uint16;
       default 1500;
     }
  }

  notification link-discovered {
    uses link;
  }

  notification link-removed {
     uses link;
  }

  container kernel-modules {
    list kernel-module {
      key "name";

      leaf name {
        type string;
      }

      leaf location {
        type string;
        default "/lib/modules";
      }

      leaf loaded {
        type boolean;
      }

      action load {
        input {
          leaf params {
            mandatory "true";
            type string;
          }
          leaf force {
            type boolean;
            default "false";
            when "../../loaded = 'false'";
          }
          leaf dry-run {
            type boolean;
            default false;
          }
        }
        output {
          leaf return-code {
            type uint8;
          }
        }
      }

      action unload { }

      action get-dependencies {
        output {
          leaf-list dependency {
            type string;
          }
          leaf location {
            type leafref {
              path "/kernel-modules/kernel-module[name = current()/../../name]/location";
            }
          }
          leaf location2 {
            type leafref {
              path "../../location";
            }
          }
        }
      }

      notification status-change {
        leaf loaded {
          type boolean;
        }
        leaf time-of-change {
          type uint32;
          must '. > /rd:magic_number or count(/rd:magic_number) = 0' {
             error-message "time-of-change must be greater than magic_number";
          }
        }
      }
    }
  }

 typedef uniontpdf {
   type union {
      type string {
        pattern "disabled";
      }
      type uint8 {
        range "1..100";
      }
  }
  default "disabled";
 }

 typedef enumtpdf {
    type enumeration {
       enum "a" {
          value 1;
       }
       enum "b" {
          value 2;
       }
       enum "c" {
          value 3;
       }
    }
 }

 typedef multiplunions {
    type union {
       type uniontpdf;
       type enumtpdf;
       type union {
          type boolean;
          type decimal64 {
             fraction-digits 2;
          }
       }
    }
 }

 typedef leafreftpdf {
   type leafref {
      path "/list/key";
   }
 }

 typedef inttpdf {
   type uint8 {
      range "1..100";
   }
 }

 container tpdfs {
    leaf unival {
       type uniontpdf;
	 }

    leaf leafrefval {
       type leafreftpdf;
    }

    leaf intval {
       type inttpdf;
    }

    leaf undecided {
       type multiplunions;
    }
 }

 leaf dec64-in-union {
   type threshold-power-dBm;
 }

 container presence-container {
   presence "presence-container";

    leaf topleaf1 {
      type int8;
    }

    leaf topleaf2 {
      type int8;
    }

    container child1 {
      leaf child1-leaf {
        type int8;
      }

      container grandchild1 {
        leaf grandchild1-leaf {
          type int8;
          default 10;
        }
      }
    }

    container child2 {
      leaf child2-leaf {
        type int8;
       }

      container grandchild2 {
        leaf grandchild2-leaf1 {
          type int8;
        }
        leaf grandchild2-leaf2 {
          type int8;
        }
        leaf grandchild2-leaf3 {
          type int8;
        }
      }
    }
  }
}
