Loading...
Searching...
No Matches
leafref.c
Go to the documentation of this file.
#define _GNU_SOURCE /* strdup */
#include "plugins_types.h"
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include "libyang.h"
/* additional internal headers for some useful simple macros */
#include "compat.h"
#include "ly_common.h"
#include "plugins_internal.h" /* LY_TYPE_*_STR */
#include "tree_data_internal.h" /* lyd_link_leafref_node */
LIBYANG_API_DEF LY_ERR
lyplg_type_store_leafref(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
uint32_t options, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node,
struct lyd_value *storage, struct lys_glob_unres *unres, struct ly_err_item **err)
{
LY_ERR rc = LY_SUCCESS;
struct lysc_type_leafref *type_lr = (struct lysc_type_leafref *)type;
assert(type_lr->realtype);
/* store the value as the real type of the leafref target */
rc = type_lr->realtype->plugin->store(ctx, type_lr->realtype, value, value_len, options, format, prefix_data,
hints, ctx_node, storage, unres, err);
if (rc == LY_EINCOMPLETE) {
/* it is irrelevant whether the target type needs some resolving */
rc = LY_SUCCESS;
}
LY_CHECK_RET(rc);
if (type_lr->require_instance) {
/* needs to be resolved */
return LY_EINCOMPLETE;
} else {
return LY_SUCCESS;
}
}
LIBYANG_API_DEF LY_ERR
lyplg_type_validate_leafref(const struct ly_ctx *ctx, const struct lysc_type *type, const struct lyd_node *ctx_node,
const struct lyd_node *tree, struct lyd_value *storage, struct ly_err_item **err)
{
LY_ERR rc = LY_SUCCESS;
struct lysc_type_leafref *type_lr = (struct lysc_type_leafref *)type;
char *errmsg = NULL, *path;
struct ly_set *targets = NULL;
uint32_t i;
*err = NULL;
if (!type_lr->require_instance) {
/* redundant to resolve */
return LY_SUCCESS;
}
rc = lyplg_type_resolve_leafref(type_lr, ctx_node, storage, tree,
(ly_ctx_get_options(ctx) & LY_CTX_LEAFREF_LINKING) ? &targets : NULL, &errmsg);
if (rc) {
path = lyd_path(ctx_node, LYD_PATH_STD, NULL, 0);
rc = ly_err_new(err, LY_EVALID, LYVE_DATA, path, strdup("instance-required"), "%s", errmsg);
free(errmsg);
goto cleanup;
}
if (ly_ctx_get_options(ctx) & LY_CTX_LEAFREF_LINKING) {
for (i = 0; i < targets->count; ++i) {
rc = lyd_link_leafref_node((struct lyd_node_term *)targets->dnodes[i], (struct lyd_node_term *)ctx_node);
LY_CHECK_GOTO(rc, cleanup);
}
}
cleanup:
ly_set_free(targets, NULL);
return rc;
}
LIBYANG_API_DEF LY_ERR
lyplg_type_compare_leafref(const struct ly_ctx *ctx, const struct lyd_value *val1, const struct lyd_value *val2)
{
return val1->realtype->plugin->compare(ctx, val1, val2);
}
LIBYANG_API_DEF int
lyplg_type_sort_leafref(const struct ly_ctx *ctx, const struct lyd_value *val1, const struct lyd_value *val2)
{
return val1->realtype->plugin->sort(ctx, val1, val2);
}
LIBYANG_API_DEF const void *
lyplg_type_print_leafref(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
void *prefix_data, ly_bool *dynamic, size_t *value_len)
{
return value->realtype->plugin->print(ctx, value, format, prefix_data, dynamic, value_len);
}
LIBYANG_API_DEF LY_ERR
lyplg_type_dup_leafref(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
{
return original->realtype->plugin->duplicate(ctx, original, dup);
}
LIBYANG_API_DEF void
lyplg_type_free_leafref(const struct ly_ctx *ctx, struct lyd_value *value)
{
value->realtype->plugin->free(ctx, value);
}
const struct lyplg_type_record plugins_leafref[] = {
{
.module = "",
.revision = NULL,
.name = LY_TYPE_LEAFREF_STR,
.plugin.id = "libyang 2 - leafref, version 1",
.plugin.store = lyplg_type_store_leafref,
.plugin.validate = lyplg_type_validate_leafref,
.plugin.compare = lyplg_type_compare_leafref,
.plugin.sort = lyplg_type_sort_leafref,
.plugin.print = lyplg_type_print_leafref,
.plugin.duplicate = lyplg_type_dup_leafref,
.plugin.free = lyplg_type_free_leafref,
.plugin.lyb_data_len = -1,
},
{0}
};
ly_ctx_get_options
LIBYANG_API_DECL uint16_t ly_ctx_get_options(const struct ly_ctx *ctx)
Get the currently set context's options.
ly_ctx
libyang context handler.
LY_CTX_LEAFREF_LINKING
#define LY_CTX_LEAFREF_LINKING
Definition context.h:201
LY_ERR
LY_ERR
libyang's error codes returned by the libyang functions.
Definition log.h:237
LYVE_DATA
@ LYVE_DATA
Definition log.h:274
LY_EVALID
@ LY_EVALID
Definition log.h:245
LY_SUCCESS
@ LY_SUCCESS
Definition log.h:238
LY_EINCOMPLETE
@ LY_EINCOMPLETE
Definition log.h:247
ly_err_item
Libyang full error structure.
Definition log.h:282
ly_set::count
uint32_t count
Definition set.h:49
ly_set_free
LIBYANG_API_DECL void ly_set_free(struct ly_set *set, void(*destructor)(void *obj))
Free the ly_set data. If the destructor is not provided, it frees only the set structure content,...
ly_set
Structure to hold a set of (not necessary somehow connected) objects. Usually used for lyd_node,...
Definition set.h:47
lyplg_type::print
lyplg_type_print_clb print
Definition plugins_types.h:628
lyplg_type::store
lyplg_type_store_clb store
Definition plugins_types.h:624
lyplg_type::compare
lyplg_type_compare_clb compare
Definition plugins_types.h:626
lyplg_type::duplicate
lyplg_type_dup_clb duplicate
Definition plugins_types.h:629
lyplg_type::free
lyplg_type_free_clb free
Definition plugins_types.h:630
lyplg_type::sort
lyplg_type_sort_clb sort
Definition plugins_types.h:627
lyplg_type_resolve_leafref
LIBYANG_API_DECL LY_ERR lyplg_type_resolve_leafref(const struct lysc_type_leafref *lref, const struct lyd_node *node, struct lyd_value *value, const struct lyd_node *tree, struct ly_set **targets, char **errmsg)
Find leafref target in data.
ly_err_new
LIBYANG_API_DECL LY_ERR ly_err_new(struct ly_err_item **err, LY_ERR ecode, LY_VECODE vecode, char *data_path, char *apptag, const char *err_format,...) _FORMAT_PRINTF(6
Create and fill error structure.
lyplg_type_record
Definition plugins_types.h:635
lyplg_type_compare_leafref
LIBYANG_API_DEF LY_ERR lyplg_type_compare_leafref(const struct ly_ctx *ctx, const struct lyd_value *val1, const struct lyd_value *val2)
Implementation of lyplg_type_compare_clb for the built-in leafref type.
Definition leafref.c:105
lyplg_type_validate_leafref
LIBYANG_API_DEF LY_ERR lyplg_type_validate_leafref(const struct ly_ctx *ctx, const struct lysc_type *type, const struct lyd_node *ctx_node, const struct lyd_node *tree, struct lyd_value *storage, struct ly_err_item **err)
Implementation of lyplg_type_validate_clb for the built-in leafref type.
Definition leafref.c:67
lyplg_type_sort_leafref
LIBYANG_API_DEF int lyplg_type_sort_leafref(const struct ly_ctx *ctx, const struct lyd_value *val1, const struct lyd_value *val2)
Implementation of lyplg_type_sort_clb for the built-in leafref type.
Definition leafref.c:111
lyplg_type_print_leafref
LIBYANG_API_DEF const void * lyplg_type_print_leafref(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format, void *prefix_data, ly_bool *dynamic, size_t *value_len)
Implementation of lyplg_type_print_clb for the built-in leafref type.
Definition leafref.c:117
lyplg_type_store_leafref
LIBYANG_API_DEF LY_ERR lyplg_type_store_leafref(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len, uint32_t options, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node, struct lyd_value *storage, struct lys_glob_unres *unres, struct ly_err_item **err)
Implementation of lyplg_type_store_clb for the built-in leafref type.
Definition leafref.c:40
lyplg_type_dup_leafref
LIBYANG_API_DEF LY_ERR lyplg_type_dup_leafref(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
Implementation of lyplg_type_dup_clb for the built-in leafref type.
Definition leafref.c:124
lyplg_type_free_leafref
LIBYANG_API_DEF void lyplg_type_free_leafref(const struct ly_ctx *ctx, struct lyd_value *value)
Implementation of lyplg_type_free_clb for the built-in leafref type.
Definition leafref.c:130
lysc_type_leafref::path
struct lyxp_expr * path
Definition tree_schema.h:1366
lysc_type::plugin
struct lyplg_type * plugin
Definition tree_schema.h:1286
lysc_type_leafref::realtype
struct lysc_type * realtype
Definition tree_schema.h:1368
lysc_type_leafref::require_instance
uint8_t require_instance
Definition tree_schema.h:1369
lysc_node
Compiled YANG data node.
Definition tree_schema.h:1421
lysc_type
Definition tree_schema.h:1283
lysc_type_leafref
Definition tree_schema.h:1359
LY_VALUE_FORMAT
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition tree.h:234
plugins_leafref
const struct lyplg_type_record plugins_leafref[]
Plugin information for leafref type implementation.
Definition leafref.c:142
libyang.h
The main libyang public header.
ly_bool
uint8_t ly_bool
Type to indicate boolean value.
Definition log.h:35
plugins_types.h
API for (user) types plugins.
lyd_path
LIBYANG_API_DECL char * lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
Generate path of the given node in the requested format.
LYD_PATH_STD
@ LYD_PATH_STD
Definition tree_data.h:2338
lyd_value::realtype
const struct lysc_type * realtype
Definition tree_data.h:571
lyd_node
Generic structure for a data node.
Definition tree_data.h:795
lyd_node_term
Data node structure for the terminal data tree nodes - leaves and leaf-lists.
Definition tree_data.h:847
lyd_value
YANG data representation.
Definition tree_data.h:567