cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1901
Views
5
Helpful
3
Replies

YANG enum constraints

Hi all,

 

A quick YANG question; Consider a list of sites. These sites can be one of three types; hub, branch or transit. Only one hub can exist, but multiple branches and transits are allowed. How would I create this constraint within my YANG model?

 

list sites {

  key site-id;

  leaf site-id {

    mandatory true;

    type uint16;

  }

  leaf site-type {

    type enumeration {

      enum hub;

      enum transit;

      enum branch;

    }

  }

}

2 Accepted Solutions

Accepted Solutions

vleijon
Cisco Employee
Cisco Employee

You use a must expression. So it looks something like

  must "count(/sites[site-type='hub']) < 2";

 

 

View solution in original post

Apparent I had an issue with the path. Solved it by using this syntax instead:

 

must "count(current()/../../sites[site-type='hub']) < 2"

 

The sites were in a list of WANs, but the path {/wan/sites[site-type]} didn't work for me either. Probably since I would have to reference the key of the WAN list (name). I dunno, but the above works.. :)

View solution in original post

3 Replies 3

vleijon
Cisco Employee
Cisco Employee

You use a must expression. So it looks something like

  must "count(/sites[site-type='hub']) < 2";

 

 

 Thanks :)

 

I get this error;

 

warning: The 'must' expression will fail: the node 'sites' from module 'wan' (in node 'site-type' in module 'wan' from 'wan') is not found.

 

I've added this to the module;

 

list sites {

  key site-id;

  leaf site-id {

    mandatory true;

    type uint16;

  }

  leaf site-type {

    type enumeration {

      enum hub;

      enum transit;

      enum branch;

    }

    must "count(/sites[site-type='hub']) < 2" {

     error-message "Only one HUB can be present";

    }

  }

}

Apparent I had an issue with the path. Solved it by using this syntax instead:

 

must "count(current()/../../sites[site-type='hub']) < 2"

 

The sites were in a list of WANs, but the path {/wan/sites[site-type]} didn't work for me either. Probably since I would have to reference the key of the WAN list (name). I dunno, but the above works.. :)