Value comparizon in yang

Hello Folks,

I have a yang file like this:
image

i want to make prevention or validation like that user can’t type default lease time value which is greater than max lease time.
for this purpose, i tried to use “when” and “must” statement as it’s seen in the figure.
But i couldn’t it. it didn’t work.
how can i do this in yang file.

Many Thanks,
With My Best Regards,

Use-case of these two statements is quite different, check YANG RFC to clarify which one you really want to use…

when statement makes existence of node in configuration conditioned -> if when condition is false, the node is not present in config at all (irrelevant if valid or invalid)!

must statement enforces some condition on the node for it to be valid, so the leaf is configurable, and it may be valid (must cond. == true) or invalid, throwing user a warning, not allowing to make a change to config…

edit: plus, please next time add your yang in text format to allow easy copy for experiments and giving hints, image prevents this and makes users type it manually (if they have time))

assuming you want to enforce some condition for value to be valid (~ must statement), everything seems to work nicely e.g. when when testing with simple addition to examples.confd/intro/1-2-3-start-query-model/dhcpd.yang:

  container dhcp-kea {
    leaf default-lease-time {
      type uint32;
      must ". < ../max-lease-time";
      default 43200;
    }
    leaf max-lease-time {
      type uint32;
      default 86400;
    }
  }

and CLI-C example output:

Entering configuration mode terminal
dev(config)# dhcp-kea default-lease-time 89999
dev(config)# commit
Aborted: 'dhcp-kea default-lease-time' (value "89999"): the 'must' expression ". < ../max-lease-time" failed
dev(config)#

Hello @josephm

First off all thank you for all your answer.

edit: plus, please next time add your yang in text format to allow easy copy for experiments and giving hints, image prevents this and makes users type it manually (if they have time))

definetly, you’re right, i want to be a quick, so that i clipped the image, but i’ll consider this, i’ll put text.

      must ". < ../max-lease-time";

i tried this, but it couldn’t work. but i tried over netconf not CLI.
however, “must” statement didn’t work, then i tried “when” statement. I know that “when” is quite different from “must” statement.

Also, additionaly, i i try this:

must ". < 100";

it works.

you may need to provide reproducible small example showing the exact problem/error message/acceptance of operation/… - also NETCONF seems fine in the example i provided above:

My netconf-console command in interactive mode:

* Enter a NETCONF operation, end with an empty line, or only empty continuing session
  <edit-config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
    <target>
      <running/>
    </target>
    <config xmlns:dh="http://tail-f.com/ns/example/dhcpd">
      <dh:dhcp-kea>
        <dh:default-lease-time operation="merge">89999</dh:default-lease-time>
      </dh:dhcp-kea>
    </config>
  </edit-config>

response:

<?xml version="1.0" encoding="UTF-8"?>
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="3">
  <rpc-error>
    <error-type>application</error-type>
    <error-tag>operation-failed</error-tag>
    <error-severity>error</error-severity>
    <error-app-tag>must-violation</error-app-tag>
    <error-path xmlns:dhcpd="http://tail-f.com/ns/example/dhcpd">
    /rpc/edit-config/config/dhcpd:dhcp-kea/dhcpd:default-lease-time
  </error-path>
    <error-message xml:lang="en">/dhcp-kea/default-lease-time (value "89999"): the 'must' expression ". &lt; ../max-lease-time" failed</error-message>
    <error-info>
      <bad-element>default-lease-time</bad-element>
    </error-info>
  </rpc-error>
</rpc-reply>

While normal value seems to work fine:

  <edit-config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
    <target>
      <running/>
    </target>
    <config xmlns:dh="http://tail-f.com/ns/example/dhcpd">
      <dh:dhcp-kea>
        <dh:default-lease-time operation="merge">123</dh:default-lease-time>
      </dh:dhcp-kea>
    </config>
  </edit-config>

<?xml version="1.0" encoding="UTF-8"?>
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="2">
  <ok/>
</rpc-reply>

Hello @josephm

Ohh no way, i guess, i’m editing wrong place, I’ll inform

Hello @josephm

You’re right, and i also thought correctly in the begining :slight_smile:

      must ". < ../max-lease-time";

it works, i’m editing wrong place in my yang file, so that, i couldn’t realized that,

Sorrt for noise,
But again it was great, it’s more clear now
Many Thanks,
With My Best Regards,

1 Like

no problem, it happens frequently to many of us :slight_smile:

most of the pipeline is not hard, but there are many steps and places so one can easily do a small error someplace…

1 Like