ietf-netmod-yang

Leaf Node 'when' or 'must' statement Usage


I am in the process of learning OpenDayLight and Yang and can't figure out how to put a constraint on a leaf node. I have a leaf node (vpn-id). When the l3vpn-type node equals 'bgp', I want data for this one to be allowed for vpn-id. If the leaf node does not equal 'bgp' and the vpn-id is entered I want to throw an error. I have tested this in OpenDayLight and it always allows me to save the data no matter what the data has in it.

Also, I am having a hard time finding Yang examples so I can teach myself. Suggestions are welcome.

module DaveTest {
 namespace "urn:aaa:ddd:DaveTest";
 prefix dave-module;

 description "Dave testing file";

 revision "2017-04-17" {
      description "Initial version.";
 }

 container testing-vars {

      list test-list {

           key "vpn-transaction-id l3vpn-type";
           unique "vpn-transaction-id";

           leaf vpn-transaction-id {
                type string;
           }

           leaf l3vpn-type {
                type enumeration {
                     enum "bgp";
                     enum "static";
                     enum "gre tunnel";
                }
                mandatory true;
           }

           leaf vpn-id {
                when "../l3vpn-type = 'bgp'";
                type string;
           }
      }              
 }

Solution

  • Your usage of the when statement is correct. You have made vpn-id conditional, so that it is only allowed to appear if l3vpn-type has a value of bgp. Perhaps it is not working as expected due to a bug in ODL.

    If you wish to learn about YANG, reading tag wiki would be a good start (there are links at the bottom). There are repositories of YANG modules made by various groups that can be found online, such as this one. Perhaps reading standard modules would be a better start, as they are peer reviewed and follow guidelines for YANG authors. They can be found in inside published RFCs, usually with their name containing "YANG data model for" phrase.