Reg must constraint

Hi,
we need to define a constraint like, if the destination is v4, then next-hop should not be v6 vice versa.

In the must constraint, can we give the typedef name for the string pattern to be matched ? what i am looking for is, definitions of (regex for ) ipv4-address/prefix(same with v6) are present in type defed. can we use these directly or need to repeat every occurrence in the must.

Thanks
Phani

No, you can’t use a must expression that would verify the actual type used in a union-typed leaf. But in your case you do not have to use the full ipv4/ipv6 regex expressions; I think simple check for ":" in the IP address string is enough to determine the type (unless I’m wrong, every IPv6 address contains colon, no IPv4 address contains colon). Would that work for you?

Hi Mvf,
Thanks for the reply. I tried to use : or . character to differentiate these. But, i could not get the reg ex to match , contains this character . As it needs a full expression. Do you have any suggestion with : or .

Thanks
Phani

No regular expressions needed, I meant something like this:

  leaf destination {
    type inet:ip-address;
  }
  leaf next-hop {
    type inet:ip-address;
    must "contains(., ':') or not(contains(../destination, ':'))" {
      error-message "IPv6 address must be used as next-hop";
    }
    must "not(contains(., ':')) or contains(../destination, ':')" {
      error-message "IPv4 address must be used as next-hop";
    }
  }

This makes sure that if destination is IPv6 address, so must be next-hop, and vice versa.