Is there a way to combine two types in one leaf?


I am new to YANG Data Modeling Language and I would like to combine two types in single leaf. For example in _3gpp-common-yang-types.yang file there is grouping PLMNId which contains MCC and MNC leafs. I would like to concat the typedefs of MCC and MNC and create a new leaf called PLMNId. I could go to typedefs and get the patterns and combine them but i would like to stick to 3gpp standards and somehow reference them in case there occurs a change in those typedefs. I would like my leaf to be updated whenever those standard typedefs are updated. Is there a way to do that? If so can you please give an example? I saw type union but it just greedily selects the first type that input fits, it does not combine them.

Thanks in advance.

Here is the portion of _3gpp-common-yang-types.yang file:

  typedef Mcc {
    description "The mobile country code consists of three decimal digits, 
      The first digit of the mobile country code identifies the geographic 
      region (the digits 1 and 8 are not used):";
    type string {
      pattern '[02-79][0-9][0-9]';
    reference "3GPP TS 23.003 subclause 2.2 and 12.1";

  typedef Mnc {
    description "The mobile network code consists of two or three 
      decimal digits (for example: MNC of 001 is not the same as MNC of 01)";
    type string {
      pattern '[0-9][0-9][0-9]|[0-9][0-9]';
    reference "3GPP TS 23.003 subclause 2.2 and 12.1";

  grouping PLMNId {
    leaf mcc {
      mandatory true;
      type Mcc;
    leaf mnc {
      mandatory true;
      type Mnc;
    reference "TS 23.658";

I removed my previous comment, sorry - i missed your mention about union before…

Maybe for your use-case, choice statement could fit? You define your new elements to be one of the choice cases, each case having one of the mentioned types → Mcc/Mnc…
So possibly something like:

choice my-plmn-id {
  case mcc {
    leaf mcc { type Mcc; }
  case mnc {
    leaf mnc { type Mnc; }

Name is different for each, so it’s technically two different leaves, not one, but by using choice, you can have only one of the two leaves set/configured at one time.

IMHO you need to have some explicit difference visible to user so he knows which of two types is being used either way…

Thanks for reply,

Actually, in my case I want to include both Mcc and Mnc, not only one of them. I want a leaf that fits the patterns of Mcc and Mnc

I see, you may need then to create custom datatype for leaf :frowning:
Maybe someone comes with alternative solution.

Thanks for your answer Josephm anyway.

Yes, I may need custom datatype but also need to refer 3gpp standards in someway. I am thinking of creating a leaf with type string and then using a must statement that somehow points to typedef Mcc and Mnc in _3gpp-common-yang-types.yang file and checks whether the given string is combination of two typedefs. Don’t know if this is applicable though.

Anyone with further knowledge on how to concat two typedefs in one leaf is more than welcome! :pray:

this with your requirement of having “one leaf” implies that, due to how the patterns are defined, you will not be able to tell which case is being set actually for some digit combinations… Is the 299 that user set Mcc, or Mnc?

If you don’t need this information, you should be good to go with custom typedef.
If you do, you may end up needing the choice statement between two leaves, each with own original typedef usage…

I realized I explained myself not very clear, sorry about that.

I want a leaf that has a pattern of mcc+mnc (concatenation of mcc and mnc string patterns from their typedefs).

I can go to mcc and mnc typedefs and copy the patterns there and create a new custom typedef which contains the concatenation of mcc and mnc typedef patterns.

But I do not want that because if any of these original patterns change from 3gpp standards, i need to update my custom typedef again.

In other words, I do not want to go to mcc and mnc typedefs and copy again the patterns and concat again in my custom typedef.

I would like to ask if we can somehow reference those typedefs and concat them for a leaf.

Hope I explained better now:)

ooh right, so it’s concatenation of two strings each with their own type into single string… i fixated my point of view as “alternatives”, thus my confusion, sorry :slight_smile:

I think you won’t be able to do anything automatic on YANG level only.

Following are just brainstorming ideas, maybe someone else chimes-in with further options.

You could implement validation callback for the string leaf, but you’re back to the need to update it’s implementation if target typedef format changes.

You could theoretically add a YANG transformation for your one leaf (more fiddly parts, can affect workflow due to its pros & cons):

  • keep two separate original leaves with original typedefs (leaf mnc, mcc), BUT:

  • hide these leaves mnc, mcc from the YANG model in the YANG schema tree
    (see chapter - “Hidden Data” in confd user guide)

  • add new single leaf with transformation callback, that builds/proxies its value from/into hidden leaves mnc&mcc.

  • update your integration code to use not single transformed leaf values, but the actual two hidden items

Transformation however might be needlessly complex just to evade rather unlikely standardized YANG update (not impossible though). It might be easier to have few extra intact integration tests to guard against this type of change for product updates…