How do I declare and use submodules in YANG?

Submodules allow a module designer to split a complex model into several pieces where all the submodules contribute to a single namespace, which is defined by the module that includes the submodules. The “submodule” statement defines the submodule’s name, and groups all statements that belong to the submodule together. The “belongs-to” statement specifies the module to which the submodule belongs.

The “include” statement is used to make content from a submodule available to that submodule’s parent module, or to another submodule of that parent module. A module may include any number of submodules, but each submodule may belong to only one module. When a module includes a submodule, it incorporates the contents of the submodule into the node hierarchy of the module.

In this short example the module top includes two sub modules, and instantiates the groupings defined by the submodules.

top.yang:

module top {
  namespace "http://tail-f.com/top";
  prefix "t";

  include "submodule-one";
  include "submodule-two";

  uses one;
  uses two;
}

The submodules declare that they belong to the module “top” and regular YANG statements follow. Note the lack of namespace declarations, the submodules belong to the module’s namespace.

submodule-one.yang:

submodule submodule-one {
  belongs-to top {
    prefix "t";
  }

  import ietf-inet-types {
    prefix "inet";
  }

  grouping one {
    container something {
      leaf an-integer {
        type int32;
      }
      leaf an-address {
        type inet:ip-address;
      }
    }
  }
}

submodule-two.yang:

submodule submodule-two {
  belongs-to top {
    prefix "t";
  }

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

  grouping two {
    container something-else {
      leaf a-string {
        type string;
      }
      leaf a-mac-address {
        type yang:mac-address;
      }
    }
  }
}