2

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.

Mel
  • 5,837
  • 10
  • 37
  • 42
HPG
  • 33
  • 5

1 Answers1

4

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.

predi
  • 5,528
  • 32
  • 60
  • Do I have to import this part in my YANG module as it doesn't give me the same result. Or I need to import some org.so library module modulus { yang-version 1.1; namespace "org:so:modulus"; prefix "som"; – HPG Dec 13 '17 at 13:03
  • @HPG, no, my YANG example is pure standard YANG. You can remove the "yang-version" statement. – predi Dec 13 '17 at 13:07