Search code examples
opendaylightietf-netmod-yang

YANG: How can I include a container from another module?


I am writing a YANG module where I want to include a container from another module i.e. I want to define a NEW container in the module that I am writing that references a container from another module. Example of failed attempt:

 module newmodule {
 yang-version 1.1;
 namespace "urn:nist:params:xml:ns:yang:newmodule";
 prefix newmodule;

    import ietf-access-control-list {
      prefix "acl";
    }

    container newprofile {
      uses acl:access-lists;
    }
  }

I have only included the essential parts above. Here acl:access-lists is a container.

Is it possible to compose containers like this? I've tried successfully to build containers from groupings. However, in this case I have no control over the contents of ietf-access-control-list.


Solution

  • You should import the first module in the second one, then augment the first with the second:

    Let's say the first module contains:

    module first {
      yang-version 1.1;
      namespace "http://first";
      prefix first;
    
      container first-container {
        description
          "First container";
    }
    

    The second module shall have

    module second {
      yang-version 1.1;
      namespace "http://second";
      prefix second;
    
      import first {
        prefix first;
      }
    
      augment "/first:first-container" {
        container second-container {
          description
            "Second container";
        }
      }
    }
    

    YANG 1.1 allows that operation:

    YANG allows a module to insert additional nodes into data models, including both the current module (and its submodules) and an external module. This is useful, for example, for vendors to add vendor-specific parameters to standard data models in an interoperable way.

    The "augment" statement defines the location in the data model hierarchy where new nodes are inserted, and the "when" statement defines the conditions when the new nodes are valid.