libyang 6.1.4
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
Loading...
Searching...
No Matches
node_instanceid.c
Go to the documentation of this file.
1
14
15#include "plugins_types.h"
16
17#include <stdint.h>
18#include <stdlib.h>
19
20#include "libyang.h"
21
22/* additional internal headers for some useful simple macros */
23#include "compat.h"
24#include "ly_common.h"
25#include "path.h"
26#include "plugins_internal.h" /* LY_TYPE_*_STR */
27#include "xpath.h"
28
37
47static LY_ERR
48node_instanceid_path2str(const struct ly_path *path, LY_VALUE_FORMAT format, void *prefix_data, char **str)
49{
50 LY_ERR ret = LY_SUCCESS;
52 char *result = NULL, quot;
53 const struct lys_module *mod = NULL, *local_mod = NULL;
54 struct ly_set *mods;
55 ly_bool inherit_prefix = 0;
56 const char *strval;
57
58 if (!path) {
59 /* special path */
60 ret = ly_strcat(&result, "/");
61 goto cleanup;
62 }
63
64 switch (format) {
65 case LY_VALUE_XML:
66 case LY_VALUE_STR_NS:
67 /* zero the local module so that all the prefixes are printed */
68 mods = prefix_data;
69 local_mod = mods->objs[0];
70 mods->objs[0] = NULL;
71 break;
72 case LY_VALUE_SCHEMA:
74 /* nothing to do */
75 break;
76 case LY_VALUE_CANON:
77 case LY_VALUE_CBOR:
78 case LY_VALUE_JSON:
79 case LY_VALUE_LYB:
80 /* zero the local module so that the first node is always prefixed */
81 prefix_data = NULL;
82 break;
83 }
84
85 switch (format) {
86 case LY_VALUE_XML:
87 case LY_VALUE_SCHEMA:
89 /* everything is prefixed */
90 inherit_prefix = 0;
91 break;
92 case LY_VALUE_CANON:
93 case LY_VALUE_CBOR:
94 case LY_VALUE_JSON:
95 case LY_VALUE_LYB:
96 case LY_VALUE_STR_NS:
97 /* the same prefix is inherited and skipped */
98 inherit_prefix = 1;
99 break;
100 }
101
102 LY_ARRAY_FOR(path, u) {
103 /* new node */
104 if (!inherit_prefix || (mod != path[u].node->module)) {
105 mod = path[u].node->module;
106 ret = ly_strcat(&result, "/%s:%s", lyplg_type_get_prefix(mod, format, prefix_data), path[u].node->name);
107 } else {
108 ret = ly_strcat(&result, "/%s", path[u].node->name);
109 }
110 LY_CHECK_GOTO(ret, cleanup);
111
112 /* node predicates */
113 LY_ARRAY_FOR(path[u].predicates, v) {
114 struct ly_path_predicate *pred = &path[u].predicates[v];
115
116 switch (pred->type) {
117 case LY_PATH_PREDTYPE_POSITION:
118 /* position predicate */
119 ret = ly_strcat(&result, "[%" PRIu64 "]", pred->position);
120 break;
121 case LY_PATH_PREDTYPE_LIST:
122 /* key-predicate */
123 ret = lyplg_type_print_val(pred->key, pred->value, format, prefix_data, &strval);
124 LY_CHECK_GOTO(ret, cleanup);
125
126 /* default quote */
127 LY_CHECK_GOTO(ret = ly_val_get_quot(pred->key->module->ctx, strval, &quot), cleanup);
128 if (inherit_prefix) {
129 /* always the same prefix as the parent */
130 ret = ly_strcat(&result, "[%s=%c%s%c]", pred->key->name, quot, strval, quot);
131 } else {
132 ret = ly_strcat(&result, "[%s:%s=%c%s%c]", lyplg_type_get_prefix(pred->key->module, format, prefix_data),
133 pred->key->name, quot, strval, quot);
134 }
135 lydict_remove(pred->key->module->ctx, strval);
136 break;
137 case LY_PATH_PREDTYPE_LEAFLIST:
138 /* leaf-list-predicate */
139 ret = lyplg_type_print_val(path[u].node, pred->value, format, prefix_data, &strval);
140 LY_CHECK_GOTO(ret, cleanup);
141
142 /* default quote */
143 LY_CHECK_GOTO(ret = ly_val_get_quot(path[u].node->module->ctx, strval, &quot), cleanup);
144 ret = ly_strcat(&result, "[.=%c%s%c]", quot, strval, quot);
145 lydict_remove(path[u].node->module->ctx, strval);
146 break;
147 case LY_PATH_PREDTYPE_LIST_VAR:
148 /* key-predicate with a variable */
149 if (inherit_prefix) {
150 /* always the same prefix as the parent */
151 ret = ly_strcat(&result, "[%s=$%s]", pred->key->name, pred->variable);
152 } else {
153 ret = ly_strcat(&result, "[%s:%s=$%s]", lyplg_type_get_prefix(pred->key->module, format, prefix_data),
154 pred->key->name, pred->variable);
155 }
156 break;
157 }
158
159 LY_CHECK_GOTO(ret, cleanup);
160 }
161 }
162
163cleanup:
164 if (local_mod) {
165 mods->objs[0] = (void *)local_mod;
166 }
167 if (ret) {
168 free(result);
169 } else {
170 *str = result;
171 }
172 return ret;
173}
174
178static LY_ERR
179lyplg_type_store_node_instanceid(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, uint64_t value_size_bits,
180 uint32_t options, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node,
181 struct lyd_value *storage, struct lys_glob_unres *unres, struct ly_err_item **err)
182{
183 LY_ERR ret = LY_SUCCESS;
184 struct lyxp_expr *exp = NULL;
185 uint32_t value_size, prefix_opt = 0;
186 struct ly_path *path = NULL;
187 char *canon;
188
189 /* init storage */
190 memset(storage, 0, sizeof *storage);
191 storage->realtype = type;
192
193 /* check value length */
194 ret = lyplg_type_check_value_size("node-instance-identifier", format, value_size_bits, LYPLG_LYB_SIZE_VARIABLE_BYTES,
195 0, &value_size, err);
196 LY_CHECK_GOTO(ret, cleanup);
197
198 /* check hints */
199 ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
200 LY_CHECK_GOTO(ret, cleanup);
201
202 if ((((char *)value)[0] == '/') && (value_size == 1)) {
203 /* special path */
204 format = LY_VALUE_CANON;
205 goto store;
206 }
207
208 switch (format) {
209 case LY_VALUE_SCHEMA:
211 case LY_VALUE_XML:
212 prefix_opt = LY_PATH_PREFIX_MANDATORY;
213 break;
214 case LY_VALUE_CANON:
215 case LY_VALUE_LYB:
216 case LY_VALUE_CBOR:
217 case LY_VALUE_JSON:
218 case LY_VALUE_STR_NS:
219 prefix_opt = LY_PATH_PREFIX_STRICT_INHERIT;
220 break;
221 }
222
223 /* parse the value */
224 ret = ly_path_parse(ctx, ctx_node, value, value_size, 0, LY_PATH_BEGIN_ABSOLUTE, prefix_opt, LY_PATH_PRED_SIMPLE, &exp);
225 if (ret) {
226 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL,
227 "Invalid node-instance-identifier \"%.*s\" value - syntax error.", (int)value_size, (char *)value);
228 goto cleanup;
229 }
230
231 if (options & LYPLG_TYPE_STORE_IMPLEMENT) {
232 /* implement all prefixes */
233 LY_CHECK_GOTO(ret = lys_compile_expr_implement(ctx, exp, format, prefix_data, 1, unres, NULL), cleanup);
234 }
235
236 /* resolve it on schema tree, use JSON format instead of LYB because for this type they are equal but for some
237 * nested types (such as numbers in predicates in the path) LYB would be invalid */
238 ret = ly_path_compile(ctx, ctx_node, exp, (ctx_node && (ctx_node->flags & LYS_IS_OUTPUT)) ?
239 LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, 1, (format == LY_VALUE_LYB) ?
240 LY_VALUE_JSON : format, prefix_data, &path);
241 if (ret) {
242 ret = ly_err_new(err, ret, LYVE_DATA, NULL, NULL,
243 "Invalid node-instance-identifier \"%.*s\" value - semantic error.", (int)value_size, (char *)value);
244 goto cleanup;
245 }
246
247store:
248 /* store value */
249 storage->target = path;
250
251 /* store canonical value */
252 if (format == LY_VALUE_CANON) {
253 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
254 ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
255 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
256 LY_CHECK_GOTO(ret, cleanup);
257 } else {
258 ret = lydict_insert(ctx, value, value_size, &storage->_canonical);
259 LY_CHECK_GOTO(ret, cleanup);
260 }
261 } else {
262 /* JSON format with prefix is the canonical one */
263 ret = node_instanceid_path2str(path, LY_VALUE_JSON, NULL, &canon);
264 LY_CHECK_GOTO(ret, cleanup);
265
266 ret = lydict_insert_zc(ctx, canon, &storage->_canonical);
267 LY_CHECK_GOTO(ret, cleanup);
268 }
269
270cleanup:
271 lyxp_expr_free(exp);
272 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
273 free((void *)value);
274 }
275
276 if (ret) {
277 lyplg_type_free_instanceid(ctx, storage);
278 }
279 return ret;
280}
281
285static const void *
286lyplg_type_print_node_instanceid(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *value, LY_VALUE_FORMAT format,
287 void *prefix_data, ly_bool *dynamic, uint64_t *value_size_bits)
288{
289 char *ret;
290
291 if ((format == LY_VALUE_CANON) || (format == LY_VALUE_JSON) || (format == LY_VALUE_LYB)) {
292 if (dynamic) {
293 *dynamic = 0;
294 }
295 if (value_size_bits) {
296 *value_size_bits = strlen(value->_canonical) * 8;
297 }
298 return value->_canonical;
299 }
300
301 /* print the value in the specific format */
302 if (node_instanceid_path2str(value->target, format, prefix_data, &ret)) {
303 return NULL;
304 }
305 *dynamic = 1;
306 if (value_size_bits) {
307 *value_size_bits = strlen(ret) * 8;
308 }
309 return ret;
310}
311
315static LY_ERR
316lyplg_type_dup_node_instanceid(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
317{
318 LY_ERR ret;
319
320 memset(dup, 0, sizeof *dup);
321
322 /* canonical value */
323 ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
324 LY_CHECK_GOTO(ret, error);
325
326 if (original->target) {
327 /* copy path */
328 ret = ly_path_dup(ctx, original->target, &dup->target);
329 LY_CHECK_GOTO(ret, error);
330 } /* else is the special path "/" that has no target stored */
331
332 dup->realtype = original->realtype;
333 return LY_SUCCESS;
334
335error:
337 return ret;
338}
339
348 {
349 .module = "ietf-netconf-acm",
350 .revision = NULL,
351 .name = "node-instance-identifier",
352
353 .plugin.id = "ly2 node-instance-identifier",
354 .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
355 .plugin.store = lyplg_type_store_node_instanceid,
356 .plugin.validate_value = NULL,
357 .plugin.validate_tree = NULL,
358 .plugin.compare = lyplg_type_compare_simple,
359 .plugin.sort = lyplg_type_sort_simple,
360 .plugin.print = lyplg_type_print_node_instanceid,
361 .plugin.duplicate = lyplg_type_dup_node_instanceid,
362 .plugin.free = lyplg_type_free_instanceid,
363 },
364 {0}
365};
libyang context handler.
LIBYANG_API_DECL LY_ERR lydict_insert(const struct ly_ctx *ctx, const char *value, size_t len, const char **str_p)
Insert string into dictionary. If the string is already present, only a reference counter is incremen...
LIBYANG_API_DECL LY_ERR lydict_remove(const struct ly_ctx *ctx, const char *value)
Remove specified string from the dictionary. It decrement reference counter for the string and if it ...
LIBYANG_API_DECL LY_ERR lydict_insert_zc(const struct ly_ctx *ctx, char *value, const char **str_p)
Insert string into dictionary - zerocopy version. If the string is already present,...
LY_ERR
libyang's error codes returned by the libyang functions.
Definition log.h:252
@ LYVE_DATA
Definition log.h:290
@ LY_EVALID
Definition log.h:260
@ LY_SUCCESS
Definition log.h:253
Libyang full error structure.
Definition log.h:298
Structure to hold a set of (not necessary somehow connected) objects. Usually used for lyd_node,...
Definition set.h:47
LIBYANG_API_DECL LY_ERR lyplg_type_check_hints(uint32_t hints, const char *value, uint32_t value_len, LY_DATA_TYPE type, int *base, struct ly_err_item **err)
Check that the type is suitable for the parser's hints (if any) in the specified format.
LIBYANG_API_DECL const char * lyplg_type_get_prefix(const struct lys_module *mod, LY_VALUE_FORMAT format, void *prefix_data)
Get format-specific prefix for a module.
LIBYANG_API_DECL LY_ERR lyplg_type_check_value_size(const char *type_name, LY_VALUE_FORMAT format, uint64_t value_size_bits, enum lyplg_lyb_size_type lyb_size_type, uint64_t lyb_fixed_size_bits, uint32_t *value_size, struct ly_err_item **err)
Check a value type in bits is correct and as expected.
LIBYANG_API_DECL LY_ERR lyplg_type_print_val(const struct lysc_node *node, const char *canon, LY_VALUE_FORMAT format, void *prefix_data, const char **value)
Convert canonical value into a value in a specific format.
LIBYANG_API_DECL void lyplg_type_free_instanceid(const struct ly_ctx *ctx, struct lyd_value *value)
Implementation of lyplg_type_free_clb for the built-in instance-identifier type.
Definition instanceid.c:325
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_LYB_SIZE_VARIABLE_BYTES
LIBYANG_API_DEF int lyplg_type_sort_simple(const struct ly_ctx *ctx, const struct lyd_value *val1, const struct lyd_value *val2)
Implementation of lyplg_type_sort_clb for a generic simple type.
LIBYANG_API_DECL LY_ERR lyplg_type_compare_simple(const struct ly_ctx *ctx, const struct lyd_value *val1, const struct lyd_value *val2)
Implementation of lyplg_type_compare_clb for a generic simple type.
LIBYANG_API_DECL void lyplg_type_lyb_size_variable_bytes(const struct lysc_type *type, enum lyplg_lyb_size_type *size_type, uint64_t *fixed_size_bits)
Implementation of lyplg_type_lyb_size_clb for a type with variable length rounded to bytes.
#define LYPLG_TYPE_STORE_DYNAMIC
#define LYPLG_TYPE_STORE_IMPLEMENT
LY_DATA_TYPE basetype
uint16_t flags
Available YANG schema tree structures representing YANG module.
Compiled YANG data node.
#define LYS_IS_OUTPUT
#define LY_ARRAY_FOR(ARRAY,...)
Sized-array iterator (for-loop).
Definition tree.h:167
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition tree.h:234
#define LY_ARRAY_COUNT_TYPE
Type (i.e. size) of the sized array's size counter.
Definition tree.h:104
@ LY_VALUE_JSON
Definition tree.h:239
@ LY_VALUE_SCHEMA
Definition tree.h:236
@ LY_VALUE_CBOR
Definition tree.h:240
@ LY_VALUE_CANON
Definition tree.h:235
@ LY_VALUE_XML
Definition tree.h:238
@ LY_VALUE_STR_NS
Definition tree.h:242
@ LY_VALUE_SCHEMA_RESOLVED
Definition tree.h:237
@ LY_VALUE_LYB
Definition tree.h:241
The main libyang public header.
uint8_t ly_bool
Type to indicate boolean value.
Definition log.h:36
const struct lyplg_type_record plugins_node_instanceid[]
Plugin information for instance-identifier type implementation.
API for (user) types plugins.
const struct lysc_type * realtype
Definition tree_data.h:552
const char * _canonical
Definition tree_data.h:549
YANG data representation.
Definition tree_data.h:548