Calling lyd_validate() with the LYD_OPT_OBSOLETE flag does not return an error when the configuration contains an obsolete leaf. The following C file shows the issue:
#include <stdio.h>
#include <libyang/libyang.h>
const char *yang =
"module deprecation {\n"
" yang-version 1.1;\n"
"\n"
" namespace \"urn:deprecation\";\n"
" prefix \"deprecation\";\n"
"\n"
" container test-deprecation {\n"
" leaf deprecated {\n"
" type string;\n"
" status deprecated;\n"
" }\n"
"\n"
" leaf obsolete {\n"
" type string;\n"
" status obsolete;\n"
" }\n"
" }\n"
"}\n"
;
const char *data =
"<test-deprecation xmlns=\"urn:deprecation\">"
"<deprecated>foo</deprecated>"
"<obsolete>bar</obsolete>"
"</test-deprecation>"
;
/* same than lyd_find_path() except it expects to match only one node */
static struct lyd_node *
lyd_find_path_one(const struct lyd_node *root, const char *path)
{
struct ly_set *set = NULL;
struct lyd_node *dnode;
set = lyd_find_path(root, path);
if (set == NULL)
goto fail;
if (set->number != 1)
goto fail;
dnode = set->set.d[0];
ly_set_free(set);
return dnode;
fail:
ly_set_free(set);
return NULL;
}
int main(void)
{
const struct lys_module *mod;
struct ly_ctx *ctx;
struct lyd_node *conf, *dnode;
int flags;
/* libyang context */
ctx = ly_ctx_new(NULL, 0);
if (ctx == NULL) {
fprintf(stderr, "Failed to create context.\n");
return 1;
}
mod = lys_parse_mem(ctx, yang, LYS_IN_YANG);
if (mod == NULL) {
fprintf(stderr, "Failed to parse yang\n");
return 1;
}
conf = lyd_parse_mem(ctx, data, LYD_XML, LYD_OPT_CONFIG);
if (conf == NULL) {
fprintf(stderr, "Failed to parse configuration\n");
return 1;
}
lyd_print_file(stdout, conf, LYD_XML, LYP_FORMAT);
dnode = lyd_find_path_one(conf,
"/deprecation:test-deprecation/obsolete");
if (dnode == NULL) {
fprintf(stderr, "Failed to retrieve obsolete node\n");
return -1;
}
printf("Obsolete flag for '/test-deprecation/obsolete' node: %d\n",
!!(dnode->schema->flags & LYS_STATUS_OBSLT));
printf("== Check validation without obsolete flag:\n");
flags = LYD_OPT_STRICT | LYD_OPT_CONFIG;
ly_errno = LY_SUCCESS;
if (lyd_validate(&conf, flags, ctx) < 0 || ly_errno != LY_SUCCESS) {
printf(" Invalid configuration\n");
} else {
printf(" Valid configuration\n");
}
printf("== Check validation with obsolete flag:\n");
flags |= LYD_OPT_OBSOLETE;
ly_errno = LY_SUCCESS;
if (lyd_validate(&conf, flags, ctx) < 0 || ly_errno != LY_SUCCESS) {
printf(" Invalid configuration\n");
} else {
printf(" Valid configuration\n");
}
return 0;
}
root@ubuntu1804:~$ gcc deprecation.c -lyang -Wall -Werror -Wextra
root@ubuntu1804:~$ ./a.out
<test-deprecation xmlns="urn:deprecation">
<deprecated>foo</deprecated>
<obsolete>bar</obsolete>
</test-deprecation>
Obsolete flag for '/test-deprecation/obsolete' node: 1
== Check validation without obsolete flag:
Valid configuration
== Check validation with obsolete flag:
Valid configuration
Hi,
Calling lyd_validate() with the LYD_OPT_OBSOLETE flag does not return an error when the configuration contains an obsolete leaf. The following C file shows the issue:
Result of execution:
Thanks,
Olivier