Is there any builtin to compare inet types (thought it is a derived string)
Is there any builtin to compare inet types (thought it is a derived string)
In what context are you looking for a way to compare them? For most âwell-knownâ types, ConfD will parse the string form, and use the parsed value for its internal representation as well as in the âlanguage APIsâ - see the confd_types(3) manual page.
Basically i am looking something like <, > or equal kind checks
OK, but I asked for the context where you wanted to do the comparison. Iâll take a guess that you want to do it in your application code, in which case I will again point you to the confd_types(3) manual page, where the representation of âwell-knownâ types (YANG built-in, ietf-yang-types and ietf-inet-types, as well as tailf-specific ones) is described when it differs from the âstring with pattern restrictionâ that the YANG modules typically specify.
As for doing the comparison, the C API provides the confd_val_eq() function, described in the confd_lib_lib(3) manual page. < / > comparisons are not generally meaningful - e.g. for inet:ipv4-address, represented as a confd_value_t with type C_IPV4 and the standard struct in_addr for the value. But you can of course extract the values with the CONFD_GET_IPV4() macro and compare them as you see fit, within the bounds of the C language.
Ya. I am aware of doing validation in the application code. Was thinking if something supported where i can use the comparison in the data-model itself than going to the application code for doing validation.
Like the example validate (mtest a_val and b_val). This can be compared using the must and checks done in data-model itself than triggering a validation in application code.
Finally! In the future, please try to provide some detail up-front with your questions, so we donât waste time discussing things that you are specifically not interested in.
Yes indeed, using âmustâ expressions in the data model is basically always preferable to writing code in validation callbacks. The argument to âmustâ is a standard XPath expression, as specified in XML Path Language (XPath) Version 1.0, and described and discussed in many other documents and even books.
XPath has all the ânormalâ comparison operators - =, !=, <=, <, >=, and > - but (simplified but basically true) only = and != can be used with operands that arenât ânumbersâ. Thus the âmore_a_than_bâ constraint in the validate/c example can trivially (and much better than code) be expressed with âmustâ, e.g. as
container mtest {
must "a_number > b_number" {
error-message "a_number is <= b_number";
}
...
The validate/xpath_must example has some more âexoticâ examples of âmustâ expressions, as well as debugging tips. I still donât know what you want to do with the âinet data typesâ (and which of them), but the YANG modules have everything you need for writing âmustâ expressions - ConfDâs internal representation is irrelevant. Unless you want it to be relevant - see the description of the compare() function in the XPATH FUNCTIONS section of the tailf_yang_extensions(5) manual page.
Iâll join in because itâs a question I wanted to ask myself.
Letâs say I have two leaves x and y, both of which are IPv4 addresses (using the corresponding inet type).
Letâs say and x and y are the min and max of a range of IP addresses, for example x=192.168.30.1 and y=192.168.30.254
From the discussion above it seems to me that there is no way to directly compare x and y (for instance x <= y) in a must expression in a yang file. Iâm bound to do this by calling an external validation code (I would code it in python, for instance).
Is that correct?
âno way to directly compare x and y (for instance x <= y)â is correct, but âbound to do this by calling an external validation codeâ is not. Directly compare using = or != should work (but is not what you need), and compare using the compare() function I pointed to should work âindirectlyâ for all the operators. E.g. (untested) âmust âcompare(x, y) = -1ââ.
This the way i am doing it currently. Thanks for all the comments.