1
votes

I am learning about YANG. I want to a typedef consisting of a Uri and a list of IpAddress. So I tried something like the following:

  typedef mapping {
      type union {

                type inet:uri;

                 list addresses {
                     type inet:IpAddress;
                 }
         }
    }

I later want to use this defined type in a container. But the definition above gave me a compilation error. Are YANG typedefs similar to c++ typedefs? How can I define such structures in YANG?

1
I'm interested in this as well. I asked a similar question some time ago, but never got an answer. Can you try the solution I had found? It didn't work for me, but I suspect I might also have had an other problem.Tama Yoshi

1 Answers

2
votes

From what you describe it seems to me like you want to create a reusable definition of a structure that has two fields: a single uri and a list of addresses. The best tool for this job is YANG's grouping. The RFC says that "grouping is like a "structure" or a "record" in conventional programming languages". The below grouping should be what you are looking for:

grouping mapping {
   leaf uri {
     type inet:uri;
   }
   list address {
     type inet:IpAddress;
   }
 }

Then you can reference such grouping by the uses statement like in the following examples:

container specific_mapping {
    uses mapping;
}

and

list my_mapping {
    key "uri";
    uses mapping;
}

The following instance documents are compatible with the above:

<specific_mapping>
    <uri>some uri</uri>
    <address>address 1</address>
    <address>address 2</address>
</specific_mapping>

and

<my_mapping>
    <uri>some uri</uri>
    <address>address 1</address>
    <address>address 2</address>
</my_mapping>
<my_mapping>
    <uri>other uri</uri>
    <address>address 3</address>
    <address>address 4</address>
</my_mapping>

The typedef statement is explained in this section of YANG 1.1. RFC 7950. It is meant to specify a type of a single leaf so it cannot be used to create the structure you want to have. When you use typedef you define the set of all possible values for that type. This is called value space. The union built-in type that you tried to use is meant to combine value spaces of types listed inside it. list is not a type, so it cannot be used there.