API struct lyd_node * lyd_new_anyxml(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_xml) { struct lyd_node_anyxml *ret; const struct lys_node *siblings, *snode; struct lyxml_elem *root; struct ly_ctx *ctx; char *xml; if ((!parent && !module) || !name) { ly_errno = LY_EINVAL; return NULL; } if (!parent) { siblings = module->data; ctx = module->ctx; } else { if (!parent->schema) { return NULL; } siblings = parent->schema->child; ctx = parent->schema->module->ctx; } if (lys_get_data_sibling(module, siblings, name, LYS_ANYXML, &snode) || !snode) { return NULL; } ret = calloc(1, sizeof *ret); if (!ret) { LOGMEM; return NULL; } ret->schema = (struct lys_node *)snode; ret->prev = (struct lyd_node *)ret; if (parent) { if (lyd_insert(parent, (struct lyd_node *)ret)) { lyd_free((struct lyd_node *)ret); return NULL; } } /* add fake root and top-level and parse the data */ asprintf(&xml, "<root><%s>%s</%s></root>", name, (val_xml ? val_xml : ""), name); root = lyxml_read_data(ctx, xml, 0); free(xml); if (!root) { lyd_free((struct lyd_node *)ret); return NULL; } /* remove the root */ ret->value = root->child; lyxml_unlink_elem(ctx, root->child, 1); lyxml_free(ctx, root); return (struct lyd_node *)ret; }
static void test_instid_unlink(void **state) { struct state *st = (*state); struct lyd_node *node; int r; node = st->data->child->prev; lyd_unlink(node); r = lyd_validate(st->data, 0); assert_int_not_equal(r, 0); lyd_insert(st->data, node); r = lyd_validate(st->data, 0); assert_int_equal(r, 0); }
static void test_leafref_unlink(void **state) { struct state *st = (*state); struct lyd_node *node; int r; node = st->data->child->child->next; lyd_unlink(node); r = lyd_validate(&(st->data), LYD_OPT_CONFIG, NULL); assert_int_not_equal(r, 0); lyd_insert(st->data->child, node); r = lyd_validate(&(st->data), LYD_OPT_CONFIG, NULL); assert_int_equal(r, 0); }
API struct lyd_node * lyd_new(struct lyd_node *parent, const struct lys_module *module, const char *name) { struct lyd_node *ret; const struct lys_node *snode = NULL, *siblings; if ((!parent && !module) || !name) { ly_errno = LY_EINVAL; return NULL; } if (!parent) { siblings = module->data; } else { if (!parent->schema) { return NULL; } siblings = parent->schema->child; } if (lys_get_data_sibling(module, siblings, name, LYS_CONTAINER | LYS_LIST | LYS_NOTIF | LYS_RPC, &snode) || !snode) { return NULL; } ret = calloc(1, sizeof *ret); if (!ret) { LOGMEM; return NULL; } ret->schema = (struct lys_node *)snode; ret->prev = ret; if (parent) { if (lyd_insert(parent, ret)) { lyd_free(ret); return NULL; } } return ret; }
API struct lyd_node * lyd_new_leaf(struct lyd_node *parent, const struct lys_module *module, const char *name, const char *val_str) { struct lyd_node_leaf_list *ret; const struct lys_node *snode = NULL, *siblings; struct lys_type *stype, *type; int found; if ((!parent && !module) || !name) { ly_errno = LY_EINVAL; return NULL; } if (!parent) { siblings = module->data; } else { if (!parent->schema) { ly_errno = LY_EINVAL; return NULL; } siblings = parent->schema->child; } if (lys_get_data_sibling(module, siblings, name, LYS_LEAFLIST | LYS_LEAF, &snode) || !snode) { ly_errno = LY_EINVAL; return NULL; } /* create the new leaf */ ret = calloc(1, sizeof *ret); if (!ret) { LOGMEM; return NULL; } ret->schema = (struct lys_node *)snode; ret->prev = (struct lyd_node *)ret; ret->value_str = lydict_insert((module ? module->ctx : parent->schema->module->ctx), val_str, 0); /* resolve the type correctly */ stype = &((struct lys_node_leaf *)snode)->type; if (stype->base == LY_TYPE_UNION) { found = 0; type = NULL; while ((type = lyp_get_next_union_type(stype, type, &found))) { ret->value_type = type->base; if (!lyp_parse_value(ret, type, 1, NULL, UINT_MAX)) { /* success! */ break; } found = 0; } if (!type) { /* fail */ ly_errno = LY_EINVAL; lyd_free((struct lyd_node *)ret); return NULL; } } else { ret->value_type = stype->base; if (lyp_parse_value(ret, stype, 1, NULL, 0)) { lyd_free((struct lyd_node *)ret); ly_errno = LY_EINVAL; return NULL; } } /* connect to parent */ if (parent) { if (lyd_insert(parent, (struct lyd_node *)ret)) { lyd_free((struct lyd_node *)ret); return NULL; } } return (struct lyd_node *)ret; }