libyang 3.1.0
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
Loading...
Searching...
No Matches
leafref.c
Go to the documentation of this file.
1
14#define _GNU_SOURCE /* strdup */
15
16#include "plugins_types.h"
17
18#include <assert.h>
19#include <stdint.h>
20#include <stdlib.h>
21
22#include "libyang.h"
23
24/* additional internal headers for some useful simple macros */
25#include "compat.h"
26#include "ly_common.h"
27#include "plugins_internal.h" /* LY_TYPE_*_STR */
28#include "tree_data_internal.h" /* lyd_link_leafref_node */
29
39LIBYANG_API_DEF LY_ERR
40lyplg_type_store_leafref(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
41 uint32_t options, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node,
42 struct lyd_value *storage, struct lys_glob_unres *unres, struct ly_err_item **err)
43{
44 LY_ERR ret = LY_SUCCESS;
45 struct lysc_type_leafref *type_lr = (struct lysc_type_leafref *)type;
46
47 assert(type_lr->realtype);
48
49 /* store the value as the real type of the leafref target */
50 ret = type_lr->realtype->plugin->store(ctx, type_lr->realtype, value, value_len, options, format, prefix_data,
51 hints, ctx_node, storage, unres, err);
52 if (ret == LY_EINCOMPLETE) {
53 /* it is irrelevant whether the target type needs some resolving */
54 ret = LY_SUCCESS;
55 }
56 LY_CHECK_RET(ret);
57
58 if (type_lr->require_instance) {
59 /* needs to be resolved */
60 return LY_EINCOMPLETE;
61 } else {
62 return LY_SUCCESS;
63 }
64}
65
66LIBYANG_API_DEF LY_ERR
67lyplg_type_validate_leafref(const struct ly_ctx *ctx, const struct lysc_type *type, const struct lyd_node *ctx_node,
68 const struct lyd_node *tree, struct lyd_value *storage, struct ly_err_item **err)
69{
70 LY_ERR ret;
71 struct lysc_type_leafref *type_lr = (struct lysc_type_leafref *)type;
72 char *errmsg = NULL, *path;
73 struct ly_set *targets = NULL;
74 uint32_t i;
75
76 *err = NULL;
77
78 if (!type_lr->require_instance) {
79 /* redundant to resolve */
80 return LY_SUCCESS;
81 }
82
83 ret = lyplg_type_resolve_leafref(type_lr, ctx_node, storage, tree, (ly_ctx_get_options(ctx) & LY_CTX_LEAFREF_LINKING) ? &targets : NULL, &errmsg);
84 if (ret != LY_SUCCESS) {
85 path = lyd_path(ctx_node, LYD_PATH_STD, NULL, 0);
86 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, path, strdup("instance-required"), "%s", errmsg);
87 free(errmsg);
88 goto cleanup;
89 }
90
92 for (i = 0; i < targets->count; ++i) {
93 ret = lyd_link_leafref_node((struct lyd_node_term *)targets->dnodes[i], (struct lyd_node_term *)ctx_node);
94 LY_CHECK_GOTO(ret, cleanup);
95 }
96 }
97
98cleanup:
99 ly_set_free(targets, NULL);
100 return ret;
101}
102
103LIBYANG_API_DEF LY_ERR
104lyplg_type_compare_leafref(const struct ly_ctx *ctx, const struct lyd_value *val1, const struct lyd_value *val2)
105{
106 return val1->realtype->plugin->compare(ctx, val1, val2);
107}
108
109LIBYANG_API_DEF int
110lyplg_type_sort_leafref(const struct ly_ctx *ctx, const struct lyd_value *val1, const struct lyd_value *val2)
111{
112 return val1->realtype->plugin->sort(ctx, val1, val2);
113}
114
115LIBYANG_API_DEF const void *
116lyplg_type_print_leafref(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
117 void *prefix_data, ly_bool *dynamic, size_t *value_len)
118{
119 return value->realtype->plugin->print(ctx, value, format, prefix_data, dynamic, value_len);
120}
121
122LIBYANG_API_DEF LY_ERR
123lyplg_type_dup_leafref(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
124{
125 return original->realtype->plugin->duplicate(ctx, original, dup);
126}
127
128LIBYANG_API_DEF void
129lyplg_type_free_leafref(const struct ly_ctx *ctx, struct lyd_value *value)
130{
131 value->realtype->plugin->free(ctx, value);
132}
133
142 {
143 .module = "",
144 .revision = NULL,
145 .name = LY_TYPE_LEAFREF_STR,
146
147 .plugin.id = "libyang 2 - leafref, version 1",
148 .plugin.store = lyplg_type_store_leafref,
149 .plugin.validate = lyplg_type_validate_leafref,
150 .plugin.compare = lyplg_type_compare_leafref,
151 .plugin.sort = lyplg_type_sort_leafref,
152 .plugin.print = lyplg_type_print_leafref,
153 .plugin.duplicate = lyplg_type_dup_leafref,
154 .plugin.free = lyplg_type_free_leafref,
155 .plugin.lyb_data_len = -1,
156 },
157 {0}
158};
LIBYANG_API_DECL uint16_t ly_ctx_get_options(const struct ly_ctx *ctx)
Get the currently set context's options.
libyang context handler.
#define LY_CTX_LEAFREF_LINKING
Definition context.h:201
LY_ERR
libyang's error codes returned by the libyang functions.
Definition log.h:237
@ LYVE_DATA
Definition log.h:274
@ LY_EVALID
Definition log.h:245
@ LY_SUCCESS
Definition log.h:238
@ LY_EINCOMPLETE
Definition log.h:247
Libyang full error structure.
Definition log.h:282
uint32_t count
Definition set.h:49
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,...
Structure to hold a set of (not necessary somehow connected) objects. Usually used for lyd_node,...
Definition set.h:47
const char *const char * revision
lyplg_type_print_clb print
lyplg_type_store_clb store
lyplg_type_compare_clb compare
lyplg_type_dup_clb duplicate
lyplg_type_free_clb free
lyplg_type_sort_clb sort
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.
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.
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:104
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
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:110
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:116
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
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:123
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:129
struct lyxp_expr * path
struct lyplg_type * plugin
struct lysc_type * realtype
Compiled YANG data node.
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition tree.h:234
const struct lyplg_type_record plugins_leafref[]
Plugin information for leafref type implementation.
Definition leafref.c:141
The main libyang public header.
uint8_t ly_bool
Type to indicate boolean value.
Definition log.h:35
API for (user) types plugins.
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
Definition tree_data.h:2332
const struct lysc_type * realtype
Definition tree_data.h:575
Generic structure for a data node.
Definition tree_data.h:799
Data node structure for the terminal data tree nodes - leaves and leaf-lists.
Definition tree_data.h:851
YANG data representation.
Definition tree_data.h:571