Search code examples
ietf-netmod-yang

Yang model for remainder operation (%)


I want to create yang model with for some integer range e.g from 1000 to maximum and values must be entered in steps of 500. Is there any way I can make use of remainder(modulus) % operator in yang or range function like python with steps.

Or I just need to use pattern with some regex.


Solution

  • Use a must constraint to further constrain an integer type value that is already constrained with a range.

    module modulus {
        yang-version 1.1;
        namespace "org:so:modulus";
        prefix "som";
    
        leaf value {
            type int32 {            
                range "1000..max";
            }
            must ". mod 500 = 0" {
                error-message "values must be entered in steps of 500";
            }
        }
    }
    

    XPath specification provides the mod operator.

    <?xml version="1.0" encoding="utf-8"?>
    <data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
      <som:value xmlns:som="org:so:modulus">1501</som:value>
    </data>
    

    Results in:

        Error at (3:3): failed assert at "/nc:data/som:value": values must be entered in steps of 500
    

    While

    <?xml version="1.0" encoding="utf-8"?>
    <data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
      <som:value xmlns:som="org:so:modulus">2000</som:value>
    </data>
    

    is okay.