#define _GNU_SOURCE
#include "validation.h"
#include <assert.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "compat.h"
#include "diff.h"
#include "hash_table.h"
#include "log.h"
#include "ly_common.h"
#include "parser_data.h"
#include "parser_internal.h"
#include "plugins_exts.h"
#include "plugins_exts/metadata.h"
#include "plugins_types.h"
#include "set.h"
#include "tree.h"
#include "tree_data.h"
#include "tree_data_internal.h"
#include "tree_schema.h"
#include "tree_schema_internal.h"
#include "xpath.h"
#define LY_VAL_ERR_GOTO(r, err_cmd, val_opts, label) \
if (r) { \
err_cmd; \
if ((r != LY_EVALID) || !(val_opts & LYD_VALIDATE_MULTI_ERROR)) { \
goto label; \
} \
}
LY_ERR
lyd_val_diff_add(const struct lyd_node *node, enum lyd_diff_op op, struct lyd_node **diff)
{
LY_ERR ret = LY_SUCCESS;
struct lyd_node *new_diff = NULL;
const struct lyd_node *prev_inst;
char *key = NULL, *value = NULL, *position = NULL;
size_t buflen = 0, bufused = 0;
uint32_t pos;
assert((op == LYD_DIFF_OP_DELETE) || (op == LYD_DIFF_OP_CREATE));
if ((op == LYD_DIFF_OP_CREATE) && lysc_is_userordered(node->schema)) {
if (lysc_is_dup_inst_list(node->schema)) {
pos = lyd_list_pos(node);
if (pos > 1) {
if (asprintf(&position, "%" PRIu32, pos - 1) == -1) {
LOGMEM(LYD_CTX(node));
ret = LY_EMEM;
goto cleanup;
}
} else {
position = strdup("");
LY_CHECK_ERR_GOTO(!position, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, cleanup);
}
} else {
if (node->prev->next && (node->prev->schema == node->schema)) {
prev_inst = node->prev;
} else {
prev_inst = NULL;
}
if (node->schema->nodetype == LYS_LIST) {
if (prev_inst) {
LY_CHECK_GOTO(ret = lyd_path_list_predicate(prev_inst, &key, &buflen, &bufused, 0), cleanup);
} else {
key = strdup("");
LY_CHECK_ERR_GOTO(!key, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, cleanup);
}
} else {
if (prev_inst) {
value = strdup(lyd_get_value(prev_inst));
LY_CHECK_ERR_GOTO(!value, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, cleanup);
} else {
value = strdup("");
LY_CHECK_ERR_GOTO(!value, LOGMEM(LYD_CTX(node)); ret = LY_EMEM, cleanup);
}
}
}
}
LY_CHECK_GOTO(ret = lyd_diff_add(node, op, NULL, NULL, key, value, position, NULL, NULL, &new_diff), cleanup);
ret = lyd_diff_merge_all(diff, new_diff, 0);
cleanup:
lyd_free_tree(new_diff);
free(key);
free(value);
free(position);
return ret;
}
static LY_ERR
lyd_validate_node_when(const struct lyd_node *tree, const struct lyd_node *node, const struct lysc_node *schema,
uint32_t xpath_options, const struct lysc_when **disabled)
{
LY_ERR r;
const struct lyd_node *ctx_node;
struct lyxp_set xp_set;
LY_ARRAY_COUNT_TYPE u;
assert(!node->schema || (node->schema == schema));
*disabled = NULL;
do {
const struct lysc_when *when;
struct lysc_when **when_list = lysc_node_when(schema);
LY_ARRAY_FOR(when_list, u) {
when = when_list[u];
if (when->context == schema) {
ctx_node = node;
} else {
assert((!when->context && !node->parent) || (when->context == node->parent->schema));
ctx_node = lyd_parent(node);
}
memset(&xp_set, 0, sizeof xp_set);
r = lyxp_eval(LYD_CTX(node), when->cond, schema->module, LY_VALUE_SCHEMA_RESOLVED, when->prefixes,
ctx_node, ctx_node, tree, NULL, &xp_set, LYXP_SCHEMA | xpath_options);
lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
LY_CHECK_RET(r);
if (!xp_set.val.bln) {
*disabled = when;
return LY_SUCCESS;
}
}
schema = schema->parent;
} while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
return LY_SUCCESS;
}
static ly_bool
lyd_validate_autodel_node_del(struct lyd_node **first, struct lyd_node *del, const struct lys_module *mod,
int np_cont_diff, struct lyd_node **node, struct ly_set *node_types, struct lyd_node **diff)
{
struct lyd_node *iter;
ly_bool node_autodel = 0;
uint32_t idx;
lyd_del_move_root(first, del, mod);
if (node && (del == *node)) {
*node = (*node)->next;
node_autodel = 1;
}
if (diff) {
if (!np_cont_diff && (del->schema->nodetype == LYS_CONTAINER) && !(del->schema->flags & LYS_PRESENCE)) {
LY_LIST_FOR(lyd_child(del), iter) {
lyd_val_diff_add(iter, LYD_DIFF_OP_DELETE, diff);
}
} else {
lyd_val_diff_add(del, LYD_DIFF_OP_DELETE, diff);
}
}
if (node_types && node_types->count) {
LYD_TREE_DFS_BEGIN(del, iter) {
if (ly_set_contains(node_types, iter, &idx)) {
ly_set_rm_index(node_types, idx, NULL);
}
LYD_TREE_DFS_END(del, iter);
}
}
lyd_free_tree(del);
return node_autodel;
}
static LY_ERR
lyd_validate_unres_when(struct lyd_node **tree, const struct lys_module *mod, struct ly_set *node_when, uint32_t val_opts,
uint32_t xpath_options, struct ly_set *node_types, struct lyd_node **diff)
{
LY_ERR rc = LY_SUCCESS, r;
uint32_t i;
const struct lysc_when *disabled;
struct lyd_node *node = NULL;
if (!node_when->count) {
return LY_SUCCESS;
}
i = node_when->count;
do {
--i;
node = node_when->dnodes[i];
LOG_LOCSET(node->schema, node);
r = lyd_validate_node_when(*tree, node, node->schema, xpath_options, &disabled);
if (!r) {
if (disabled) {
if (node->flags & LYD_WHEN_TRUE) {
lyd_validate_autodel_node_del(tree, node, mod, 1, NULL, node_types, diff);
} else if (val_opts & LYD_VALIDATE_OPERATIONAL) {
LOGWRN(LYD_CTX(node), "When condition \"%s\" not satisfied.", disabled->cond->expr);
} else {
LOGVAL(LYD_CTX(node), LY_VCODE_NOWHEN, disabled->cond->expr);
r = LY_EVALID;
LY_VAL_ERR_GOTO(r, rc = r, val_opts, error);
}
} else {
node->flags |= LYD_WHEN_TRUE;
}
ly_set_rm_index_ordered(node_when, i, NULL);
} else if (r != LY_EINCOMPLETE) {
LY_VAL_ERR_GOTO(r, rc = r, val_opts, error);
}
LOG_LOCBACK(1, 1);
} while (i);
return rc;
error:
LOG_LOCBACK(1, 1);
return rc;
}
LY_ERR
lyd_validate_unres(struct lyd_node **tree, const struct lys_module *mod, enum lyd_type data_type, struct ly_set *node_when,
uint32_t when_xp_opts, struct ly_set *node_types, struct ly_set *meta_types, struct ly_set *ext_node,
struct ly_set *ext_val, uint32_t val_opts, struct lyd_node **diff)
{
LY_ERR r, rc = LY_SUCCESS;
uint32_t i;
if (ext_val && ext_val->count) {
i = ext_val->count;
do {
--i;
struct lyd_ctx_ext_val *ext_v = ext_val->objs[i];
r = ext_v->ext->def->plugin->validate(ext_v->ext, ext_v->sibling, *tree, data_type, val_opts, diff);
LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
ly_set_rm_index(ext_val, i, free);
} while (i);
}
if (ext_node && ext_node->count) {
i = ext_node->count;
do {
--i;
struct lyd_ctx_ext_node *ext_n = ext_node->objs[i];
r = ext_n->ext->def->plugin->node(ext_n->ext, ext_n->node, val_opts);
LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
ly_set_rm_index(ext_node, i, free);
} while (i);
}
if (node_when) {
uint32_t prev_count;
do {
prev_count = node_when->count;
r = lyd_validate_unres_when(tree, mod, node_when, val_opts, when_xp_opts, node_types, diff);
LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
} while (prev_count > node_when->count);
assert(!node_when->count || ((rc == LY_EVALID) && (val_opts & LYD_VALIDATE_MULTI_ERROR)));
}
if (node_types && node_types->count) {
i = node_types->count;
do {
--i;
struct lyd_node_term *node = node_types->objs[i];
struct lysc_type *type = ((struct lysc_node_leaf *)node->schema)->type;
LOG_LOCSET(NULL, &node->node);
r = lyd_value_validate_incomplete(LYD_CTX(node), type, &node->value, &node->node, *tree);
LOG_LOCBACK(0, 1);
LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
ly_set_rm_index(node_types, i, NULL);
} while (i);
}
if (meta_types && meta_types->count) {
i = meta_types->count;
do {
--i;
struct lyd_meta *meta = meta_types->objs[i];
struct lysc_type *type;
lyplg_ext_get_storage(meta->annotation, LY_STMT_TYPE, sizeof type, (const void **)&type);
r = lyd_value_validate_incomplete(LYD_CTX(meta->parent), type, &meta->value, meta->parent, *tree);
LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
ly_set_rm_index(meta_types, i, NULL);
} while (i);
}
cleanup:
return rc;
}
static LY_ERR
lyd_validate_duplicates(const struct lyd_node *first, const struct lyd_node *node, uint32_t val_opts)
{
struct lyd_node **match_p, *match;
ly_bool fail = 0;
assert(node->flags & LYD_NEW);
if (lysc_is_dup_inst_list(node->schema)) {
return LY_SUCCESS;
}
if (node->parent && node->parent->children_ht) {
lyd_find_sibling_first(first, node, &match);
assert(match);
if (match != node) {
fail = 1;
} else if (!lyht_find_next(node->parent->children_ht, &node, node->hash, (void **)&match_p)) {
fail = 1;
}
} else {
for ( ; first; first = first->next) {
if (first == node) {
continue;
}
if (node->schema->nodetype & (LYD_NODE_ANY | LYS_LEAF)) {
if (first->schema == node->schema) {
fail = 1;
break;
}
} else if (!lyd_compare_single(first, node, 0)) {
fail = 1;
break;
}
}
}
if (fail) {
if ((node->schema->nodetype & (LYS_LIST | LYS_LEAFLIST)) && (val_opts & LYD_VALIDATE_OPERATIONAL)) {
LOG_LOCSET(NULL, node);
LOGWRN(node->schema->module->ctx, "Duplicate instance of \"%s\".", node->schema->name);
LOG_LOCBACK(0, 1);
} else {
LOG_LOCSET(NULL, node);
LOGVAL(node->schema->module->ctx, LY_VCODE_DUP, node->schema->name);
LOG_LOCBACK(0, 1);
return LY_EVALID;
}
}
return LY_SUCCESS;
}
static LY_ERR
lyd_validate_cases(struct lyd_node **first, const struct lys_module *mod, const struct lysc_node_choice *choic,
struct lyd_node **diff)
{
const struct lysc_node *scase, *iter, *old_case = NULL, *new_case = NULL;
struct lyd_node *match, *to_del;
ly_bool found;
LOG_LOCSET(&choic->node, NULL);
LY_LIST_FOR((struct lysc_node *)choic->cases, scase) {
found = 0;
iter = NULL;
match = NULL;
while ((match = lys_getnext_data(match, *first, &iter, scase, NULL))) {
if (match->flags & LYD_NEW) {
found = 2;
break;
} else {
if (found == 0) {
found = 1;
}
}
}
if (found == 1) {
if (old_case) {
LOGVAL(choic->module->ctx, LY_VCODE_DUPCASE, old_case->name, scase->name);
LOG_LOCBACK(1, 0);
return LY_EVALID;
}
old_case = scase;
} else if (found == 2) {
if (new_case) {
LOGVAL(choic->module->ctx, LY_VCODE_DUPCASE, new_case->name, scase->name);
LOG_LOCBACK(1, 0);
return LY_EVALID;
}
new_case = scase;
}
}
LOG_LOCBACK(1, 0);
if (old_case && new_case) {
iter = NULL;
match = NULL;
to_del = NULL;
while ((match = lys_getnext_data(match, *first, &iter, old_case, NULL))) {
lyd_del_move_root(first, to_del, mod);
lyd_free_tree(to_del);
if (diff) {
LY_CHECK_RET(lyd_val_diff_add(match, LYD_DIFF_OP_DELETE, diff));
}
to_del = match;
}
lyd_del_move_root(first, to_del, mod);
lyd_free_tree(to_del);
}
return LY_SUCCESS;
}
static int
lyd_val_has_default(const struct lysc_node *schema)
{
switch (schema->nodetype) {
case LYS_LEAF:
if (((struct lysc_node_leaf *)schema)->dflt) {
return 1;
}
break;
case LYS_LEAFLIST:
if (((struct lysc_node_leaflist *)schema)->dflts) {
return 1;
}
break;
case LYS_CONTAINER:
if (!(schema->flags & LYS_PRESENCE)) {
return 1;
}
break;
default:
break;
}
return 0;
}
static ly_bool
lyd_validate_autodel_leaflist_dflt(struct lyd_node **first, struct lyd_node **node, const struct lys_module *mod,
struct lyd_node **diff)
{
const struct lysc_node *schema;
struct lyd_node *iter, *next;
ly_bool found = 0, node_autodel = 0;
assert((*node)->flags & LYD_NEW);
schema = (*node)->schema;
assert(schema->nodetype == LYS_LEAFLIST);
LYD_LIST_FOR_INST(*first, schema, iter) {
if (!(iter->flags & LYD_DEFAULT)) {
found = 1;
break;
}
}
if (!found) {
return 0;
}
LYD_LIST_FOR_INST_SAFE(*first, schema, next, iter) {
if (iter->flags & LYD_DEFAULT) {
if (lyd_validate_autodel_node_del(first, iter, mod, 0, node, NULL, diff)) {
node_autodel = 1;
}
}
}
return node_autodel;
}
static ly_bool
lyd_validate_autodel_cont_leaf_dflt(struct lyd_node **first, struct lyd_node **node, const struct lys_module *mod,
struct lyd_node **diff)
{
const struct lysc_node *schema;
struct lyd_node *iter, *next;
ly_bool found = 0, node_autodel = 0;
assert((*node)->flags & LYD_NEW);
schema = (*node)->schema;
assert(schema->nodetype & (LYS_LEAF | LYS_CONTAINER));
LYD_LIST_FOR_INST(*first, schema, iter) {
if (!(iter->flags & LYD_DEFAULT)) {
found = 1;
break;
}
}
if (found) {
LYD_LIST_FOR_INST_SAFE(*first, schema, next, iter) {
if (iter->flags & LYD_DEFAULT) {
if (lyd_validate_autodel_node_del(first, iter, mod, 0, node, NULL, diff)) {
node_autodel = 1;
}
}
}
} else {
LYD_LIST_FOR_INST(*first, schema, iter) {
if ((iter->flags & LYD_DEFAULT) && !(iter->flags & LYD_NEW)) {
if (lyd_validate_autodel_node_del(first, iter, mod, 0, node, NULL, diff)) {
node_autodel = 1;
}
break;
}
}
}
return node_autodel;
}
static ly_bool
lyd_validate_autodel_case_dflt(struct lyd_node **first, struct lyd_node **node, const struct lys_module *mod,
struct lyd_node **diff)
{
const struct lysc_node *schema;
struct lysc_node_choice *choic;
struct lyd_node *iter = NULL;
const struct lysc_node *slast = NULL;
ly_bool node_autodel = 0;
assert((*node)->flags & LYD_DEFAULT);
schema = (*node)->schema;
if (!schema->parent || (schema->parent->nodetype != LYS_CASE)) {
return 0;
}
choic = (struct lysc_node_choice *)schema->parent->parent;
assert(choic->nodetype == LYS_CHOICE);
if (choic->dflt && (choic->dflt == (struct lysc_node_case *)schema->parent)) {
return 0;
}
while ((iter = lys_getnext_data(iter, *first, &slast, schema->parent, NULL))) {
if (!(iter->flags & LYD_DEFAULT)) {
break;
}
}
if (!iter) {
if (lyd_validate_autodel_node_del(first, *node, mod, 0, node, NULL, diff)) {
node_autodel = 1;
}
}
return node_autodel;
}
static LY_ERR
lyd_validate_choice_r(struct lyd_node **first, const struct lysc_node *sparent, const struct lys_module *mod,
uint32_t val_opts, struct lyd_node **diff)
{
LY_ERR r, rc = LY_SUCCESS;
const struct lysc_node *snode = NULL;
while (*first && (snode = lys_getnext(snode, sparent, mod ? mod->compiled : NULL, LYS_GETNEXT_WITHCHOICE))) {
if (snode->nodetype == LYS_CHOICE) {
r = lyd_validate_cases(first, mod, (struct lysc_node_choice *)snode, diff);
LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
r = lyd_validate_choice_r(first, snode, mod, val_opts, diff);
LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
}
}
cleanup:
return rc;
}
LY_ERR
lyd_validate_new(struct lyd_node **first, const struct lysc_node *sparent, const struct lys_module *mod,
uint32_t val_opts, struct lyd_node **diff)
{
LY_ERR r, rc = LY_SUCCESS;
struct lyd_node *node;
const struct lysc_node *last_dflt_schema = NULL;
assert(first && (sparent || mod));
r = lyd_validate_choice_r(first, sparent, mod, val_opts, diff);
LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
node = *first;
while (node) {
if (!node->schema || (mod && (lyd_owner_module(node) != mod))) {
break;
}
if (!(node->flags & (LYD_NEW | LYD_DEFAULT))) {
node = node->next;
continue;
}
if (lyd_val_has_default(node->schema) && (node->schema != last_dflt_schema) && (node->flags & LYD_NEW)) {
last_dflt_schema = node->schema;
if (node->schema->nodetype == LYS_LEAFLIST) {
if (lyd_validate_autodel_leaflist_dflt(first, &node, mod, diff)) {
continue;
}
} else {
if (lyd_validate_autodel_cont_leaf_dflt(first, &node, mod, diff)) {
continue;
}
}
}
if (node->flags & LYD_NEW) {
r = lyd_validate_duplicates(*first, node, val_opts);
LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
node->flags &= ~LYD_NEW;
}
if (node->flags & LYD_DEFAULT) {
if (lyd_validate_autodel_case_dflt(first, &node, mod, diff)) {
continue;
}
}
node = node->next;
}
cleanup:
return rc;
}
static LY_ERR
lyd_validate_dummy_when(const struct lyd_node *first, const struct lyd_node *parent, const struct lysc_node *snode,
const struct lysc_when **disabled)
{
LY_ERR rc = LY_SUCCESS;
struct lyd_node *tree, *dummy = NULL;
uint32_t xp_opts;
if (parent) {
tree = (struct lyd_node *)parent;
while (tree->parent) {
tree = lyd_parent(tree);
}
tree = lyd_first_sibling(tree);
} else {
tree = lyd_first_sibling(first);
}
rc = lyd_new_opaq((struct lyd_node *)parent, snode->module->ctx, snode->name, NULL, NULL, snode->module->name, &dummy);
LY_CHECK_GOTO(rc, cleanup);
if (!parent) {
if (first) {
lyd_insert_sibling((struct lyd_node *)first, dummy, &tree);
} else {
assert(!tree);
tree = dummy;
}
}
if (snode->flags & LYS_CONFIG_W) {
xp_opts = LYXP_ACCESS_TREE_CONFIG;
} else {
xp_opts = LYXP_ACCESS_TREE_ALL;
}
rc = lyd_validate_node_when(tree, dummy, snode, xp_opts, disabled);
if (rc == LY_EINCOMPLETE) {
LOGINT(snode->module->ctx);
rc = LY_EINT;
goto cleanup;
} else if (rc) {
goto cleanup;
}
cleanup:
lyd_free_tree(dummy);
return rc;
}
static LY_ERR
lyd_validate_mandatory(const struct lyd_node *first, const struct lyd_node *parent, const struct lysc_node *snode,
uint32_t val_opts)
{
const struct lysc_when *disabled;
if (snode->nodetype == LYS_CHOICE) {
if (lys_getnext_data(NULL, first, NULL, snode, NULL)) {
return LY_SUCCESS;
}
} else {
assert(snode->nodetype & (LYS_LEAF | LYS_CONTAINER | LYD_NODE_ANY));
if (!lyd_find_sibling_val(first, snode, NULL, 0, NULL)) {
return LY_SUCCESS;
}
}
disabled = NULL;
if (lysc_has_when(snode)) {
LY_CHECK_RET(lyd_validate_dummy_when(first, parent, snode, &disabled));
}
if (!disabled) {
if (val_opts & LYD_VALIDATE_OPERATIONAL) {
LOG_LOCSET(parent ? NULL : snode, parent);
if (snode->nodetype == LYS_CHOICE) {
LOGWRN(snode->module->ctx, "Mandatory choice \"%s\" data do not exist.", snode->name);
} else {
LOGWRN(snode->module->ctx, "Mandatory node \"%s\" instance does not exist.", snode->name);
}
LOG_LOCBACK(parent ? 0 : 1, parent ? 1 : 0);
} else {
LOG_LOCSET(parent ? NULL : snode, parent);
if (snode->nodetype == LYS_CHOICE) {
LOGVAL_APPTAG(snode->module->ctx, "missing-choice", LY_VCODE_NOMAND_CHOIC, snode->name);
} else {
LOGVAL(snode->module->ctx, LY_VCODE_NOMAND, snode->name);
}
LOG_LOCBACK(parent ? 0 : 1, parent ? 1 : 0);
return LY_EVALID;
}
}
return LY_SUCCESS;
}
static LY_ERR
lyd_validate_minmax(const struct lyd_node *first, const struct lyd_node *parent, const struct lysc_node *snode,
uint32_t min, uint32_t max, uint32_t val_opts)
{
uint32_t count = 0;
struct lyd_node *iter;
const struct lysc_when *disabled;
assert(min || max);
LYD_LIST_FOR_INST(first, snode, iter) {
++count;
if (min && (count == min)) {
min = 0;
if (!max) {
break;
}
}
if (max && (count > max)) {
break;
}
}
if (min) {
assert(count < min);
disabled = NULL;
if (lysc_has_when(snode)) {
LY_CHECK_RET(lyd_validate_dummy_when(first, parent, snode, &disabled));
}
if (disabled) {
min = 0;
}
}
if (max && (count <= max)) {
max = 0;
}
if (min) {
if (val_opts & LYD_VALIDATE_OPERATIONAL) {
LOG_LOCSET(snode, NULL);
LOGWRN(snode->module->ctx, "Too few \"%s\" instances.", snode->name);
LOG_LOCBACK(1, 0);
} else {
LOG_LOCSET(snode, NULL);
LOGVAL_APPTAG(snode->module->ctx, "too-few-elements", LY_VCODE_NOMIN, snode->name);
LOG_LOCBACK(1, 0);
return LY_EVALID;
}
} else if (max) {
if (val_opts & LYD_VALIDATE_OPERATIONAL) {
LOG_LOCSET(NULL, iter);
LOGWRN(snode->module->ctx, "Too many \"%s\" instances.", snode->name);
LOG_LOCBACK(0, 1);
} else {
LOG_LOCSET(NULL, iter);
LOGVAL_APPTAG(snode->module->ctx, "too-many-elements", LY_VCODE_NOMAX, snode->name);
LOG_LOCBACK(0, 1);
return LY_EVALID;
}
}
return LY_SUCCESS;
}
static struct lyd_node *
lyd_val_uniq_find_leaf(const struct lysc_node_leaf *uniq_leaf, const struct lyd_node *list)
{
struct lyd_node *node;
const struct lysc_node *iter;
size_t depth = 0, i;
for (iter = &uniq_leaf->node; iter && (iter != list->schema); iter = lysc_data_parent(iter)) {
++depth;
}
node = (struct lyd_node *)list;
while (node && depth) {
for (i = depth - 1, iter = &uniq_leaf->node; i; iter = lysc_data_parent(iter)) {
--i;
}
assert(iter->nodetype & (LYS_CONTAINER | LYS_LEAF));
lyd_find_sibling_val(lyd_child(node), iter, NULL, 0, &node);
--depth;
}
return node;
}
struct lyd_val_uniq_arg {
LY_ARRAY_COUNT_TYPE action;
uint32_t val_opts;
};
static ly_bool
lyd_val_uniq_list_equal(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *cb_data)
{
struct ly_ctx *ctx;
struct lysc_node_list *slist;
struct lyd_node *diter, *first, *second;
struct lyd_value *val1, *val2;
char *path1, *path2, *uniq_str, *ptr;
LY_ARRAY_COUNT_TYPE u, v;
struct lyd_val_uniq_arg *arg = cb_data;
const uint32_t uniq_err_msg_size = 1024;
assert(val1_p && val2_p);
first = *((struct lyd_node **)val1_p);
second = *((struct lyd_node **)val2_p);
assert(first && (first->schema->nodetype == LYS_LIST));
assert(second && (second->schema == first->schema));
ctx = first->schema->module->ctx;
slist = (struct lysc_node_list *)first->schema;
if (arg->action > 0) {
u = arg->action - 1;
if (u < LY_ARRAY_COUNT(slist->uniques)) {
goto uniquecheck;
}
}
LY_ARRAY_FOR(slist->uniques, u) {
uniquecheck:
LY_ARRAY_FOR(slist->uniques[u], v) {
diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], first);
if (diter) {
val1 = &((struct lyd_node_term *)diter)->value;
} else {
val1 = slist->uniques[u][v]->dflt;
}
diter = lyd_val_uniq_find_leaf(slist->uniques[u][v], second);
if (diter) {
val2 = &((struct lyd_node_term *)diter)->value;
} else {
val2 = slist->uniques[u][v]->dflt;
}
if (!val1 || !val2 || val1->realtype->plugin->compare(ctx, val1, val2)) {
break;
}
}
if (v && (v == LY_ARRAY_COUNT(slist->uniques[u]))) {
path1 = lyd_path(first, LYD_PATH_STD, NULL, 0);
path2 = lyd_path(second, LYD_PATH_STD, NULL, 0);
uniq_str = malloc(uniq_err_msg_size);
uniq_str[0] = '\0';
ptr = uniq_str;
LY_ARRAY_FOR(slist->uniques[u], v) {
if (v) {
strcpy(ptr, " ");
++ptr;
}
ptr = lysc_path_until((struct lysc_node *)slist->uniques[u][v], &slist->node, LYSC_PATH_LOG,
ptr, uniq_err_msg_size - (ptr - uniq_str));
if (!ptr) {
break;
}
ptr += strlen(ptr);
}
LOG_LOCSET(NULL, second);
if (arg->val_opts & LYD_VALIDATE_OPERATIONAL) {
LOGWRN(ctx, "Unique data leaf(s) \"%s\" not satisfied in \"%s\" and \"%s\".", uniq_str, path1, path2);
} else {
LOGVAL_APPTAG(ctx, "data-not-unique", LY_VCODE_NOUNIQ, uniq_str, path1, path2);
}
LOG_LOCBACK(0, 1);
free(path1);
free(path2);
free(uniq_str);
if (!(arg->val_opts & LYD_VALIDATE_OPERATIONAL)) {
return 1;
}
}
if (arg->action > 0) {
return 0;
}
}
return 0;
}
static LY_ERR
lyd_validate_unique(const struct lyd_node *first, const struct lysc_node *snode, const struct lysc_node_leaf ***uniques,
uint32_t val_opts)
{
const struct lyd_node *diter;
struct ly_set *set;
LY_ARRAY_COUNT_TYPE u, v, x = 0;
LY_ERR ret = LY_SUCCESS;
uint32_t hash, i;
size_t key_len;
ly_bool dyn;
const void *hash_key;
struct lyd_val_uniq_arg arg, *args = NULL;
struct ly_ht **uniqtables = NULL;
struct lyd_value *val;
struct ly_ctx *ctx = snode->module->ctx;
assert(uniques);
LY_CHECK_RET(ly_set_new(&set));
LY_LIST_FOR(first, diter) {
if (diter->schema == snode) {
ret = ly_set_add(set, (void *)diter, 1, NULL);
LY_CHECK_GOTO(ret, cleanup);
}
}
if (set->count == 2) {
arg.action = 0;
arg.val_opts = val_opts;
if (lyd_val_uniq_list_equal(&set->objs[0], &set->objs[1], 0, &arg)) {
ret = LY_EVALID;
goto cleanup;
}
} else if (set->count > 2) {
uniqtables = malloc(LY_ARRAY_COUNT(uniques) * sizeof *uniqtables);
args = malloc(LY_ARRAY_COUNT(uniques) * sizeof *args);
LY_CHECK_ERR_GOTO(!uniqtables || !args, LOGMEM(ctx); ret = LY_EMEM, cleanup);
x = LY_ARRAY_COUNT(uniques);
for (v = 0; v < x; v++) {
args[v].action = v + 1;
args[v].val_opts = val_opts;
uniqtables[v] = lyht_new(lyht_get_fixed_size(set->count), sizeof(struct lyd_node *),
lyd_val_uniq_list_equal, &args[v], 0);
LY_CHECK_ERR_GOTO(!uniqtables[v], LOGMEM(ctx); ret = LY_EMEM, cleanup);
}
for (i = 0; i < set->count; i++) {
for (u = 0; u < x; u++) {
val = NULL;
for (v = hash = 0; v < LY_ARRAY_COUNT(uniques[u]); v++) {
diter = lyd_val_uniq_find_leaf(uniques[u][v], set->objs[i]);
if (diter) {
val = &((struct lyd_node_term *)diter)->value;
} else {
val = uniques[u][v]->dflt;
}
if (!val) {
break;
}
hash_key = val->realtype->plugin->print(NULL, val, LY_VALUE_LYB, NULL, &dyn, &key_len);
hash = lyht_hash_multi(hash, hash_key, key_len);
if (dyn) {
free((void *)hash_key);
}
}
if (!val) {
continue;
}
hash = lyht_hash_multi(hash, NULL, 0);
ret = lyht_insert(uniqtables[u], &set->objs[i], hash, NULL);
if (ret == LY_EEXIST) {
ret = LY_EVALID;
}
LY_CHECK_GOTO(ret != LY_SUCCESS, cleanup);
}
}
}
cleanup:
ly_set_free(set, NULL);
for (v = 0; v < x; v++) {
if (!uniqtables[v]) {
break;
}
lyht_free(uniqtables[v], NULL);
}
free(uniqtables);
free(args);
return ret;
}
static LY_ERR
lyd_validate_siblings_schema_r(const struct lyd_node *first, const struct lyd_node *parent,
const struct lysc_node *sparent, const struct lysc_module *mod, uint32_t val_opts, uint32_t int_opts)
{
LY_ERR r, rc = LY_SUCCESS;
const struct lysc_node *snode = NULL, *scase;
struct lysc_node_list *slist;
struct lysc_node_leaflist *sllist;
uint32_t getnext_opts;
getnext_opts = LYS_GETNEXT_WITHCHOICE | (int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0);
while ((snode = lys_getnext(snode, sparent, mod, getnext_opts))) {
if ((val_opts & LYD_VALIDATE_NO_STATE) && (snode->flags & LYS_CONFIG_R)) {
continue;
}
if (snode->nodetype == LYS_LIST) {
slist = (struct lysc_node_list *)snode;
if (slist->min || slist->max) {
r = lyd_validate_minmax(first, parent, snode, slist->min, slist->max, val_opts);
LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
}
} else if (snode->nodetype == LYS_LEAFLIST) {
sllist = (struct lysc_node_leaflist *)snode;
if (sllist->min || sllist->max) {
r = lyd_validate_minmax(first, parent, snode, sllist->min, sllist->max, val_opts);
LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
}
} else if (snode->flags & LYS_MAND_TRUE) {
r = lyd_validate_mandatory(first, parent, snode, val_opts);
LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
}
if (snode->nodetype == LYS_LIST) {
slist = (struct lysc_node_list *)snode;
if (slist->uniques) {
r = lyd_validate_unique(first, snode, (const struct lysc_node_leaf ***)slist->uniques, val_opts);
LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
}
}
if (snode->nodetype == LYS_CHOICE) {
LY_LIST_FOR(lysc_node_child(snode), scase) {
if (lys_getnext_data(NULL, first, NULL, scase, NULL)) {
r = lyd_validate_siblings_schema_r(first, parent, scase, mod, val_opts, int_opts);
LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
break;
}
}
}
}
cleanup:
return rc;
}
static void
lyd_validate_obsolete(const struct lyd_node *node)
{
const struct lysc_node *snode;
snode = node->schema;
do {
if (snode->flags & LYS_STATUS_OBSLT) {
LOG_LOCSET(NULL, node);
LOGWRN(snode->module->ctx, "Obsolete schema node \"%s\" instantiated in data.", snode->name);
LOG_LOCBACK(0, 1);
break;
}
snode = snode->parent;
} while (snode && (snode->nodetype & (LYS_CHOICE | LYS_CASE)));
}
static LY_ERR
lyd_validate_must(const struct lyd_node *node, uint32_t val_opts, uint32_t int_opts, uint32_t xpath_options)
{
LY_ERR r, rc = LY_SUCCESS;
struct lyxp_set xp_set;
struct lysc_must *musts;
const struct lyd_node *tree;
const struct lysc_node *schema;
const char *emsg, *eapptag;
LY_ARRAY_COUNT_TYPE u;
assert((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_REPLY)) != (LYD_INTOPT_RPC | LYD_INTOPT_REPLY));
assert((int_opts & (LYD_INTOPT_ACTION | LYD_INTOPT_REPLY)) != (LYD_INTOPT_ACTION | LYD_INTOPT_REPLY));
if (node->schema->nodetype & (LYS_ACTION | LYS_RPC)) {
if (int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION)) {
schema = &((struct lysc_node_action *)node->schema)->input.node;
} else if (int_opts & LYD_INTOPT_REPLY) {
schema = &((struct lysc_node_action *)node->schema)->output.node;
} else {
LOGINT_RET(LYD_CTX(node));
}
} else {
schema = node->schema;
}
musts = lysc_node_musts(schema);
if (!musts) {
return LY_SUCCESS;
}
for (tree = node; tree->parent; tree = lyd_parent(tree)) {}
tree = lyd_first_sibling(tree);
LY_ARRAY_FOR(musts, u) {
memset(&xp_set, 0, sizeof xp_set);
r = lyxp_eval(LYD_CTX(node), musts[u].cond, node->schema->module, LY_VALUE_SCHEMA_RESOLVED,
musts[u].prefixes, node, node, tree, NULL, &xp_set, LYXP_SCHEMA | xpath_options);
if (r == LY_EINCOMPLETE) {
LOGERR(LYD_CTX(node), LY_EINCOMPLETE,
"Must \"%s\" depends on a node with a when condition, which has not been evaluated.", musts[u].cond->expr);
}
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
lyxp_set_cast(&xp_set, LYXP_SET_BOOLEAN);
if (!xp_set.val.bln) {
if (val_opts & LYD_VALIDATE_OPERATIONAL) {
emsg = musts[u].emsg;
LOG_LOCSET(NULL, node);
if (emsg) {
LOGWRN(LYD_CTX(node), "%s", emsg);
} else {
LOGWRN(LYD_CTX(node), "Must condition \"%s\" not satisfied.", musts[u].cond->expr);
}
LOG_LOCBACK(0, 1);
} else {
emsg = musts[u].emsg;
eapptag = musts[u].eapptag ? musts[u].eapptag : "must-violation";
LOG_LOCSET(NULL, node);
if (emsg) {
LOGVAL_APPTAG(LYD_CTX(node), eapptag, LYVE_DATA, "%s", emsg);
} else {
LOGVAL_APPTAG(LYD_CTX(node), eapptag, LY_VCODE_NOMUST, musts[u].cond->expr);
}
LOG_LOCBACK(0, 1);
r = LY_EVALID;
LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
}
}
}
cleanup:
return rc;
}
static LY_ERR
lyd_validate_final_r(struct lyd_node *first, const struct lyd_node *parent, const struct lysc_node *sparent,
const struct lys_module *mod, uint32_t val_opts, uint32_t int_opts, uint32_t must_xp_opts)
{
LY_ERR r, rc = LY_SUCCESS;
const char *innode;
struct lyd_node *node;
LY_LIST_FOR(first, node) {
if (node->flags & LYD_EXT) {
continue;
}
if (!node->schema) {
r = lyd_parse_opaq_error(node);
goto next_iter;
}
if (!node->parent && mod && (lyd_owner_module(node) != mod)) {
break;
}
innode = NULL;
if ((val_opts & LYD_VALIDATE_NO_STATE) && (node->schema->flags & LYS_CONFIG_R)) {
innode = "state";
} else if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION)) && (node->schema->flags & LYS_IS_OUTPUT)) {
innode = "output";
} else if ((int_opts & LYD_INTOPT_REPLY) && (node->schema->flags & LYS_IS_INPUT)) {
innode = "input";
} else if (!(int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_REPLY)) && (node->schema->nodetype == LYS_RPC)) {
innode = "rpc";
} else if (!(int_opts & (LYD_INTOPT_ACTION | LYD_INTOPT_REPLY)) && (node->schema->nodetype == LYS_ACTION)) {
innode = "action";
} else if (!(int_opts & LYD_INTOPT_NOTIF) && (node->schema->nodetype == LYS_NOTIF)) {
innode = "notification";
}
if (innode) {
LOG_LOCSET(NULL, node);
LOGVAL(LYD_CTX(node), LY_VCODE_UNEXPNODE, innode, node->schema->name);
LOG_LOCBACK(0, 1);
r = LY_EVALID;
goto next_iter;
}
lyd_validate_obsolete(node);
if ((r = lyd_validate_must(node, val_opts, int_opts, must_xp_opts))) {
goto next_iter;
}
next_iter:
LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
}
r = lyd_validate_siblings_schema_r(first, parent, sparent, mod ? mod->compiled : NULL, val_opts, int_opts);
LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
LY_LIST_FOR(first, node) {
if (!node->schema || (!node->parent && mod && (lyd_owner_module(node) != mod))) {
break;
}
r = lyd_validate_final_r(lyd_child(node), node, node->schema, NULL, val_opts, int_opts, must_xp_opts);
LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
lyd_cont_set_dflt(node);
}
cleanup:
return rc;
}
static LY_ERR
lyd_validate_nested_ext(struct lyd_node *sibling, struct ly_set *ext_val)
{
struct lyd_node *node;
struct lyd_ctx_ext_val *ext_v;
struct lysc_ext_instance *nested_exts, *ext = NULL;
LY_ARRAY_COUNT_TYPE u;
if (!sibling->parent || !sibling->parent->schema) {
LOGINT_RET(LYD_CTX(sibling));
}
LY_LIST_FOR(sibling, node) {
if (!(node->flags & LYD_EXT)) {
LOGINT_RET(LYD_CTX(sibling));
}
}
nested_exts = sibling->parent->schema->exts;
LY_ARRAY_FOR(nested_exts, u) {
if (nested_exts[u].def->plugin->validate) {
if (ext) {
LOGINT_RET(LYD_CTX(sibling));
}
ext = &nested_exts[u];
}
}
if (!ext) {
LOGINT_RET(LYD_CTX(sibling));
}
ext_v = malloc(sizeof *ext_v);
LY_CHECK_ERR_RET(!ext_v, LOGMEM(LYD_CTX(sibling)), LY_EMEM);
ext_v->ext = ext;
ext_v->sibling = sibling;
LY_CHECK_RET(ly_set_add(ext_val, ext_v, 1, NULL));
return LY_SUCCESS;
}
LY_ERR
lyd_validate_node_ext(struct lyd_node *node, struct ly_set *ext_node)
{
struct lyd_ctx_ext_node *ext_n;
struct lysc_ext_instance *exts;
LY_ARRAY_COUNT_TYPE u;
exts = node->schema->exts;
LY_ARRAY_FOR(exts, u) {
if (exts[u].def->plugin && exts[u].def->plugin->node) {
ext_n = malloc(sizeof *ext_n);
LY_CHECK_ERR_RET(!ext_n, LOGMEM(LYD_CTX(node)), LY_EMEM);
ext_n->ext = &exts[u];
ext_n->node = node;
LY_CHECK_RET(ly_set_add(ext_node, ext_n, 1, NULL));
}
}
return LY_SUCCESS;
}
static LY_ERR
lyd_validate_subtree(struct lyd_node *root, struct ly_set *node_when, struct ly_set *node_types,
struct ly_set *meta_types, struct ly_set *ext_node, struct ly_set *ext_val, uint32_t val_opts,
struct lyd_node **diff)
{
LY_ERR r, rc = LY_SUCCESS;
const struct lyd_meta *meta;
const struct lysc_type *type;
struct lyd_node *node;
uint32_t impl_opts;
LYD_TREE_DFS_BEGIN(root, node) {
if (node->flags & LYD_EXT) {
return lyd_validate_nested_ext(node, ext_val);
}
if (!node->schema) {
goto next_node;
}
LY_LIST_FOR(node->meta, meta) {
lyplg_ext_get_storage(meta->annotation, LY_STMT_TYPE, sizeof type, (const void **)&type);
if (type->plugin->validate) {
r = ly_set_add(meta_types, (void *)meta, 1, NULL);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
}
}
if ((node->schema->nodetype & LYD_NODE_TERM) && ((struct lysc_node_leaf *)node->schema)->type->plugin->validate) {
r = ly_set_add(node_types, (void *)node, 1, NULL);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
} else if (node->schema->nodetype & LYD_NODE_INNER) {
r = lyd_validate_new(lyd_node_child_p(node), node->schema, NULL, val_opts, diff);
LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
impl_opts = 0;
if (val_opts & LYD_VALIDATE_NO_STATE) {
impl_opts |= LYD_IMPLICIT_NO_STATE;
}
if (val_opts & LYD_VALIDATE_NO_DEFAULTS) {
impl_opts |= LYD_IMPLICIT_NO_DEFAULTS;
}
r = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, NULL, NULL, NULL, impl_opts, diff);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
}
if (lysc_has_when(node->schema)) {
r = ly_set_add(node_when, (void *)node, 1, NULL);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
}
r = lyd_validate_node_ext(node, ext_node);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
next_node:
LYD_TREE_DFS_END(root, node);
}
cleanup:
return rc;
}
LY_ERR
lyd_validate(struct lyd_node **tree, const struct lys_module *module, const struct ly_ctx *ctx, uint32_t val_opts,
ly_bool validate_subtree, struct ly_set *node_when_p, struct ly_set *node_types_p, struct ly_set *meta_types_p,
struct ly_set *ext_node_p, struct ly_set *ext_val_p, struct lyd_node **diff)
{
LY_ERR r, rc = LY_SUCCESS;
struct lyd_node *first, *next, **first2, *iter;
const struct lys_module *mod;
struct ly_set node_types = {0}, meta_types = {0}, node_when = {0}, ext_node = {0}, ext_val = {0};
uint32_t i = 0, impl_opts;
assert(tree && ctx);
assert((node_when_p && node_types_p && meta_types_p && ext_node_p && ext_val_p) ||
(!node_when_p && !node_types_p && !meta_types_p && !ext_node_p && !ext_val_p));
if (!node_when_p) {
node_when_p = &node_when;
node_types_p = &node_types;
meta_types_p = &meta_types;
ext_node_p = &ext_node;
ext_val_p = &ext_val;
}
next = *tree;
while (1) {
if (val_opts & LYD_VALIDATE_PRESENT) {
mod = lyd_data_next_module(&next, &first);
} else {
mod = lyd_mod_next_module(next, module, ctx, &i, &first);
}
if (!mod) {
break;
}
if (!first || (first == *tree)) {
first2 = tree;
} else {
first2 = &first;
}
r = lyd_validate_new(first2, *first2 ? lysc_data_parent((*first2)->schema) : NULL, mod, val_opts, diff);
LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
impl_opts = 0;
if (val_opts & LYD_VALIDATE_NO_STATE) {
impl_opts |= LYD_IMPLICIT_NO_STATE;
}
if (val_opts & LYD_VALIDATE_NO_DEFAULTS) {
impl_opts |= LYD_IMPLICIT_NO_DEFAULTS;
}
r = lyd_new_implicit_r(lyd_parent(*first2), first2, NULL, mod, validate_subtree ? NULL : node_when_p,
validate_subtree ? NULL : node_types_p, validate_subtree ? NULL : ext_node_p, impl_opts, diff);
LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
first = *first2;
lyd_first_module_sibling(&first, mod);
if (!first || (first == *tree)) {
first2 = tree;
} else {
first2 = &first;
}
if (validate_subtree) {
LY_LIST_FOR(*first2, iter) {
if (lyd_owner_module(iter) != mod) {
break;
}
r = lyd_validate_subtree(iter, node_when_p, node_types_p, meta_types_p, ext_node_p, ext_val_p,
val_opts, diff);
LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
}
}
r = lyd_validate_unres(first2, mod, LYD_TYPE_DATA_YANG, node_when_p, 0, node_types_p, meta_types_p,
ext_node_p, ext_val_p, val_opts, diff);
LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
if (!(val_opts & LYD_VALIDATE_NOT_FINAL)) {
r = lyd_validate_final_r(*first2, NULL, NULL, mod, val_opts, 0, 0);
LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
}
}
cleanup:
ly_set_erase(&node_when, NULL);
ly_set_erase(&node_types, NULL);
ly_set_erase(&meta_types, NULL);
ly_set_erase(&ext_node, free);
ly_set_erase(&ext_val, free);
return rc;
}
LIBYANG_API_DEF LY_ERR
lyd_validate_all(struct lyd_node **tree, const struct ly_ctx *ctx, uint32_t val_opts, struct lyd_node **diff)
{
LY_CHECK_ARG_RET(NULL, tree, *tree || ctx, LY_EINVAL);
LY_CHECK_CTX_EQUAL_RET(*tree ? LYD_CTX(*tree) : NULL, ctx, LY_EINVAL);
if (!ctx) {
ctx = LYD_CTX(*tree);
}
if (diff) {
*diff = NULL;
}
return lyd_validate(tree, NULL, ctx, val_opts, 1, NULL, NULL, NULL, NULL, NULL, diff);
}
LIBYANG_API_DEF LY_ERR
lyd_validate_module(struct lyd_node **tree, const struct lys_module *module, uint32_t val_opts, struct lyd_node **diff)
{
LY_CHECK_ARG_RET(NULL, tree, module, !(val_opts & LYD_VALIDATE_PRESENT), LY_EINVAL);
LY_CHECK_CTX_EQUAL_RET(*tree ? LYD_CTX(*tree) : NULL, module->ctx, LY_EINVAL);
if (diff) {
*diff = NULL;
}
return lyd_validate(tree, module, module->ctx, val_opts, 1, NULL, NULL, NULL, NULL, NULL, diff);
}
LIBYANG_API_DEF LY_ERR
lyd_validate_module_final(struct lyd_node *tree, const struct lys_module *module, uint32_t val_opts)
{
LY_ERR r, rc = LY_SUCCESS;
struct lyd_node *first;
const struct lys_module *mod;
uint32_t i = 0;
LY_CHECK_ARG_RET(NULL, module, !(val_opts & (LYD_VALIDATE_PRESENT | LYD_VALIDATE_NOT_FINAL)), LY_EINVAL);
LY_CHECK_CTX_EQUAL_RET(LYD_CTX(tree), module->ctx, LY_EINVAL);
mod = lyd_mod_next_module(tree, module, module->ctx, &i, &first);
assert(mod);
r = lyd_validate_final_r(first, NULL, NULL, mod, val_opts, 0, 0);
LY_VAL_ERR_GOTO(r, rc = r, val_opts, cleanup);
cleanup:
return rc;
}
static void
lyd_val_op_merge_find(const struct lyd_node *op_tree, const struct lyd_node *op_node, const struct lyd_node *tree,
struct lyd_node **op_subtree, struct lyd_node **tree_sibling, struct lyd_node **tree_parent)
{
const struct lyd_node *tree_iter, *op_iter;
struct lyd_node *match = NULL;
uint32_t i, cur_depth, op_depth;
*op_subtree = NULL;
*tree_sibling = NULL;
*tree_parent = NULL;
op_depth = 0;
for (op_iter = op_node; op_iter != op_tree; op_iter = lyd_parent(op_iter)) {
++op_depth;
}
tree_iter = tree;
cur_depth = op_depth;
while (cur_depth && tree_iter) {
lyd_find_sibling_first(tree_iter, op_iter, &match);
if (!match) {
break;
}
tree_iter = lyd_child(match);
--cur_depth;
op_iter = op_node;
for (i = 0; i < cur_depth; ++i) {
op_iter = lyd_parent(op_iter);
}
}
assert(op_iter);
*op_subtree = (struct lyd_node *)op_iter;
if (!tree || tree_iter) {
*tree_sibling = (struct lyd_node *)tree_iter;
} else {
assert(match);
*tree_parent = match;
}
}
static LY_ERR
_lyd_validate_op(struct lyd_node *op_tree, struct lyd_node *op_node, const struct lyd_node *dep_tree, enum lyd_type data_type,
uint32_t int_opts, ly_bool validate_subtree, struct ly_set *node_when_p, struct ly_set *node_types_p,
struct ly_set *meta_types_p, struct ly_set *ext_node_p, struct ly_set *ext_val_p, struct lyd_node **diff)
{
LY_ERR rc = LY_SUCCESS;
struct lyd_node *tree_sibling, *tree_parent, *op_subtree, *op_parent, *op_sibling_before, *op_sibling_after, *child;
struct ly_set node_types = {0}, meta_types = {0}, node_when = {0}, ext_node = {0}, ext_val = {0};
assert(op_tree && op_node);
assert((node_when_p && node_types_p && meta_types_p && ext_node_p && ext_val_p) ||
(!node_when_p && !node_types_p && !meta_types_p && !ext_node_p && !ext_val_p));
if (!node_when_p) {
node_when_p = &node_when;
node_types_p = &node_types;
meta_types_p = &meta_types;
ext_node_p = &ext_node;
ext_val_p = &ext_val;
}
lyd_val_op_merge_find(op_tree, op_node, dep_tree, &op_subtree, &tree_sibling, &tree_parent);
op_sibling_before = op_subtree->prev->next ? op_subtree->prev : NULL;
op_sibling_after = op_subtree->next;
op_parent = lyd_parent(op_subtree);
lyd_unlink(op_subtree);
lyd_insert_node(tree_parent, &tree_sibling, op_subtree, LYD_INSERT_NODE_DEFAULT);
if (!dep_tree) {
dep_tree = tree_sibling;
}
if (int_opts & LYD_INTOPT_REPLY) {
rc = lyd_new_implicit_r(op_node, lyd_node_child_p(op_node), NULL, NULL, node_when_p, node_types_p,
ext_node_p, LYD_IMPLICIT_OUTPUT, diff);
LY_CHECK_GOTO(rc, cleanup);
if (validate_subtree) {
LY_LIST_FOR(lyd_child(op_node), child) {
rc = lyd_validate_subtree(child, node_when_p, node_types_p, meta_types_p, ext_node_p, ext_val_p, 0, diff);
LY_CHECK_GOTO(rc, cleanup);
}
}
} else {
if (validate_subtree) {
rc = lyd_validate_subtree(op_node, node_when_p, node_types_p, meta_types_p, ext_node_p, ext_val_p, 0, diff);
LY_CHECK_GOTO(rc, cleanup);
}
}
LY_CHECK_GOTO(rc = lyd_validate_unres((struct lyd_node **)&dep_tree, NULL, data_type, node_when_p, LYXP_IGNORE_WHEN,
node_types_p, meta_types_p, ext_node_p, ext_val_p, 0, diff), cleanup);
lyd_validate_obsolete(op_node);
LY_CHECK_GOTO(rc = lyd_validate_must(op_node, 0, int_opts, LYXP_IGNORE_WHEN), cleanup);
rc = lyd_validate_final_r(lyd_child(op_node), op_node, op_node->schema, NULL, 0, int_opts, LYXP_IGNORE_WHEN);
LY_CHECK_GOTO(rc, cleanup);
cleanup:
lyd_unlink(op_subtree);
if (op_sibling_before) {
lyd_insert_after_node(NULL, op_sibling_before, op_subtree);
lyd_insert_hash(op_subtree);
} else if (op_sibling_after) {
lyd_insert_before_node(op_sibling_after, op_subtree);
lyd_insert_hash(op_subtree);
} else if (op_parent) {
lyd_insert_node(op_parent, NULL, op_subtree, LYD_INSERT_NODE_DEFAULT);
}
ly_set_erase(&node_when, NULL);
ly_set_erase(&node_types, NULL);
ly_set_erase(&meta_types, NULL);
ly_set_erase(&ext_node, free);
ly_set_erase(&ext_val, free);
return rc;
}
LIBYANG_API_DEF LY_ERR
lyd_validate_op(struct lyd_node *op_tree, const struct lyd_node *dep_tree, enum lyd_type data_type, struct lyd_node **diff)
{
struct lyd_node *op_node;
uint32_t int_opts;
struct ly_set ext_val = {0};
LY_ERR rc;
LY_CHECK_ARG_RET(NULL, op_tree, !dep_tree || !dep_tree->parent, (data_type == LYD_TYPE_RPC_YANG) ||
(data_type == LYD_TYPE_NOTIF_YANG) || (data_type == LYD_TYPE_REPLY_YANG), LY_EINVAL);
if (diff) {
*diff = NULL;
}
if (data_type == LYD_TYPE_RPC_YANG) {
int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION;
} else if (data_type == LYD_TYPE_NOTIF_YANG) {
int_opts = LYD_INTOPT_NOTIF;
} else {
int_opts = LYD_INTOPT_REPLY;
}
if (op_tree->schema && (op_tree->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))) {
op_node = op_tree;
while (op_tree->parent) {
op_tree = lyd_parent(op_tree);
}
} else {
while (op_tree->parent) {
op_tree = lyd_parent(op_tree);
}
LYD_TREE_DFS_BEGIN(op_tree, op_node) {
if (!op_node->schema) {
return lyd_parse_opaq_error(op_node);
} else if (op_node->flags & LYD_EXT) {
LY_CHECK_RET(lyd_validate_nested_ext(op_node, &ext_val));
rc = lyd_validate_unres((struct lyd_node **)&dep_tree, NULL, data_type, NULL, 0, NULL, NULL, NULL,
&ext_val, 0, diff);
ly_set_erase(&ext_val, free);
return rc;
}
if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_REPLY)) &&
(op_node->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
break;
} else if ((int_opts & LYD_INTOPT_NOTIF) && (op_node->schema->nodetype == LYS_NOTIF)) {
break;
}
LYD_TREE_DFS_END(op_tree, op_node);
}
}
if (int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_REPLY)) {
if (!op_node || !(op_node->schema->nodetype & (LYS_RPC | LYS_ACTION))) {
LOGERR(LYD_CTX(op_tree), LY_EINVAL, "No RPC/action to validate found.");
return LY_EINVAL;
}
} else {
if (!op_node || (op_node->schema->nodetype != LYS_NOTIF)) {
LOGERR(LYD_CTX(op_tree), LY_EINVAL, "No notification to validate found.");
return LY_EINVAL;
}
}
return _lyd_validate_op(op_tree, op_node, dep_tree, data_type, int_opts, 1, NULL, NULL, NULL, NULL, NULL, diff);
}