1.7. YANG++ Examples

1.7.1. Template Example

In this example, a bare-bones service template is defined that has 3 virtual members:

  • reset action

  • status action

  • config container

1.7.1.1. Example Base Class

module base-service {
  // ..

  class base-service {
   virtual {
     // concrete class expected to map these 2 statements
     // and provide names for these actions
     action <reset>;
     action <status>;

     // concrete class expected to fill in this config container
     // as needed and use the provided name 'config'
     container config;
   }
 }

}

1.7.1.2. Module Defining a Derived Class

module my-service {
  import base-service { prefix base; }

  class mybase-service {
   parent-class base:base-service {
     map-virtual reset {
       map-path my-reset;
     }
     map-virtual status {
       map-path my-status;
     }
   }

   // local groupings work the same since the class root
   // is a real container or list node
   grouping status-parms {
      leaf status {
        type string;
      }
      leaf last-error {
        type string;
      }
   }

   // no virtual sections in this class makes it a concrete class
   // a concrete definition for each 'virtual' definition is expected.
   // if missing then a deviate (not-supported) is implied
   action my-reset {
     input {
       leaf myparm1 { type string; }
     }
   }

   action my-status {
     output {
       uses status-parms;
     }
   }

   // this is a replacement of the virtual config
   container config {
     list list1 {
       key name1;
       leaf name1 { type string; }
       leaf my-leaf2 { type string; }
     }
   }
 }

1.7.1.3. Module Using The Base Class

A top-level container name /mybase-service is created.

module example-services {
  // ...
  import base-mod { prefix base; }

  // top-level /mybase-service
  uses-class base:base-service {
    root-name mybase-service;
  }
}

1.7.1.4. Objects For Derived Class

In this example, the server maps implemented class 'mybase-service' to the specified (virtual) class 'base-service'.

Real objects created in module 'example-services':

module: example-services
  +--rw mybase-service
     +---x my-reset
     |  +---w input
     |     +---w myparm1?   string
     +---x my-status
     |  +--ro output
     |     +--ro status?       string
     |     +--ro last-error?   string
     +--rw config
        +--rw list1* [name1]
           +--rw name1       string
           +--rw my-leaf2?   string

1.7.1.5. Translation to YANG 1.1 Module

A YANG 1.1 for the 'services' module is shown for the mapping to the implemented class 'mybase-service'.

A generic module is not possible in this case since the specified class is also a virtual class.

module example-services {
    yang-version 1.1;
    prefix srv;
    namespace "urn:example.com:params:xml:ns:yang:example-services";

    container mybase-service {
       action my-reset {
         input {
           leaf myparm1 { type string; }
         }
       }

       action my-status {
         output {
          leaf status {
            type string;
          }
          leaf last-error {
            type string;
          }
         }
       }

       container config {
         list list1 {
           key name1;
           leaf name1 { type string; }
           leaf my-leaf2 { type string; }
         }
       }
    }
}