libyang 4.3.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 /* null the local module so that all the prefixes are printed */
67 mods = prefix_data;
68 local_mod = mods->objs[0];
69 mods->objs[0] = NULL;
70
71 /* fallthrough */
72 case LY_VALUE_SCHEMA:
74 /* everything is prefixed */
75 inherit_prefix = 0;
76 break;
77 case LY_VALUE_CANON:
78 case LY_VALUE_JSON:
79 case LY_VALUE_LYB:
80 case LY_VALUE_STR_NS:
81 /* the same prefix is inherited and skipped */
82 inherit_prefix = 1;
83 break;
84 }
85
86 LY_ARRAY_FOR(path, u) {
87 /* new node */
88 if (!inherit_prefix || (mod != path[u].node->module)) {
89 mod = path[u].node->module;
90 ret = ly_strcat(&result, "/%s:%s", lyplg_type_get_prefix(mod, format, prefix_data), path[u].node->name);
91 } else {
92 ret = ly_strcat(&result, "/%s", path[u].node->name);
93 }
94 LY_CHECK_GOTO(ret, cleanup);
95
96 /* node predicates */
97 LY_ARRAY_FOR(path[u].predicates, v) {
98 struct ly_path_predicate *pred = &path[u].predicates[v];
99
100 switch (pred->type) {
101 case LY_PATH_PREDTYPE_POSITION:
102 /* position predicate */
103 ret = ly_strcat(&result, "[%" PRIu64 "]", pred->position);
104 break;
105 case LY_PATH_PREDTYPE_LIST:
106 /* key-predicate */
107 ret = lyplg_type_print_val(pred->key, pred->value, format, prefix_data, &strval);
108 LY_CHECK_GOTO(ret, cleanup);
109
110 /* default quote */
111 quot = '\'';
112 if (strchr(strval, quot)) {
113 quot = '"';
114 }
115 if (inherit_prefix) {
116 /* always the same prefix as the parent */
117 ret = ly_strcat(&result, "[%s=%c%s%c]", pred->key->name, quot, strval, quot);
118 } else {
119 ret = ly_strcat(&result, "[%s:%s=%c%s%c]", lyplg_type_get_prefix(pred->key->module, format, prefix_data),
120 pred->key->name, quot, strval, quot);
121 }
122 lydict_remove(pred->key->module->ctx, strval);
123 break;
124 case LY_PATH_PREDTYPE_LEAFLIST:
125 /* leaf-list-predicate */
126 ret = lyplg_type_print_val(path[u].node, pred->value, format, prefix_data, &strval);
127 LY_CHECK_GOTO(ret, cleanup);
128
129 /* default quote */
130 quot = '\'';
131 if (strchr(strval, quot)) {
132 quot = '"';
133 }
134 ret = ly_strcat(&result, "[.=%c%s%c]", quot, strval, quot);
135 lydict_remove(path[u].node->module->ctx, strval);
136 break;
137 case LY_PATH_PREDTYPE_LIST_VAR:
138 /* key-predicate with a variable */
139 if (inherit_prefix) {
140 /* always the same prefix as the parent */
141 ret = ly_strcat(&result, "[%s=$%s]", pred->key->name, pred->variable);
142 } else {
143 ret = ly_strcat(&result, "[%s:%s=$%s]", lyplg_type_get_prefix(pred->key->module, format, prefix_data),
144 pred->key->name, pred->variable);
145 }
146 break;
147 }
148
149 LY_CHECK_GOTO(ret, cleanup);
150 }
151 }
152
153cleanup:
154 if (local_mod) {
155 mods->objs[0] = (void *)local_mod;
156 }
157 if (ret) {
158 free(result);
159 } else {
160 *str = result;
161 }
162 return ret;
163}
164
168static LY_ERR
169lyplg_type_store_node_instanceid(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, uint32_t value_size_bits,
170 uint32_t options, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node,
171 const struct lysc_ext_instance *top_ext, struct lyd_value *storage, struct lys_glob_unres *unres,
172 struct ly_err_item **err)
173{
174 LY_ERR ret = LY_SUCCESS;
175 struct lyxp_expr *exp = NULL;
176 uint32_t value_size, prefix_opt = 0;
177 struct ly_path *path = NULL;
178 char *canon;
179
180 /* init storage */
181 memset(storage, 0, sizeof *storage);
182 storage->realtype = type;
183
184 /* check value length */
185 ret = lyplg_type_check_value_size("node-instance-identifier", format, value_size_bits, LYPLG_LYB_SIZE_VARIABLE_BYTES,
186 0, &value_size, err);
187 LY_CHECK_GOTO(ret, cleanup);
188
189 /* check hints */
190 ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
191 LY_CHECK_GOTO(ret, cleanup);
192
193 if ((((char *)value)[0] == '/') && (value_size == 1)) {
194 /* special path */
195 format = LY_VALUE_CANON;
196 goto store;
197 }
198
199 switch (format) {
200 case LY_VALUE_SCHEMA:
202 case LY_VALUE_XML:
203 prefix_opt = LY_PATH_PREFIX_MANDATORY;
204 break;
205 case LY_VALUE_CANON:
206 case LY_VALUE_LYB:
207 case LY_VALUE_JSON:
208 case LY_VALUE_STR_NS:
209 prefix_opt = LY_PATH_PREFIX_STRICT_INHERIT;
210 break;
211 }
212
213 /* parse the value */
214 ret = ly_path_parse(ctx, ctx_node, value, value_size, 0, LY_PATH_BEGIN_ABSOLUTE, prefix_opt, LY_PATH_PRED_SIMPLE, &exp);
215 if (ret) {
216 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL,
217 "Invalid node-instance-identifier \"%.*s\" value - syntax error.", (int)value_size, (char *)value);
218 goto cleanup;
219 }
220
221 if (options & LYPLG_TYPE_STORE_IMPLEMENT) {
222 /* implement all prefixes */
223 LY_CHECK_GOTO(ret = lys_compile_expr_implement(ctx, exp, format, prefix_data, 1, unres, NULL), cleanup);
224 }
225
226 /* resolve it on schema tree, use JSON format instead of LYB because for this type they are equal but for some
227 * nested types (such as numbers in predicates in the path) LYB would be invalid */
228 ret = ly_path_compile(ctx, NULL, ctx_node, top_ext, exp, (ctx_node && (ctx_node->flags & LYS_IS_OUTPUT)) ?
229 LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, 1, (format == LY_VALUE_LYB) ?
230 LY_VALUE_JSON : format, prefix_data, &path);
231 if (ret) {
232 ret = ly_err_new(err, ret, LYVE_DATA, NULL, NULL,
233 "Invalid node-instance-identifier \"%.*s\" value - semantic error.", (int)value_size, (char *)value);
234 goto cleanup;
235 }
236
237store:
238 /* store value */
239 storage->target = path;
240
241 /* store canonical value */
242 if (format == LY_VALUE_CANON) {
243 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
244 ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
245 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
246 LY_CHECK_GOTO(ret, cleanup);
247 } else {
248 ret = lydict_insert(ctx, value, value_size, &storage->_canonical);
249 LY_CHECK_GOTO(ret, cleanup);
250 }
251 } else {
252 /* JSON format with prefix is the canonical one */
253 ret = node_instanceid_path2str(path, LY_VALUE_JSON, NULL, &canon);
254 LY_CHECK_GOTO(ret, cleanup);
255
256 ret = lydict_insert_zc(ctx, canon, &storage->_canonical);
257 LY_CHECK_GOTO(ret, cleanup);
258 }
259
260cleanup:
261 lyxp_expr_free(exp);
262 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
263 free((void *)value);
264 }
265
266 if (ret) {
267 lyplg_type_free_instanceid(ctx, storage);
268 }
269 return ret;
270}
271
275static const void *
276lyplg_type_print_node_instanceid(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *value, LY_VALUE_FORMAT format,
277 void *prefix_data, ly_bool *dynamic, uint32_t *value_size_bits)
278{
279 char *ret;
280
281 if ((format == LY_VALUE_CANON) || (format == LY_VALUE_JSON) || (format == LY_VALUE_LYB)) {
282 if (dynamic) {
283 *dynamic = 0;
284 }
285 if (value_size_bits) {
286 *value_size_bits = strlen(value->_canonical) * 8;
287 }
288 return value->_canonical;
289 }
290
291 /* print the value in the specific format */
292 if (node_instanceid_path2str(value->target, format, prefix_data, &ret)) {
293 return NULL;
294 }
295 *dynamic = 1;
296 if (value_size_bits) {
297 *value_size_bits = strlen(ret) * 8;
298 }
299 return ret;
300}
301
305static LY_ERR
306lyplg_type_dup_node_instanceid(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
307{
308 LY_ERR ret;
309
310 memset(dup, 0, sizeof *dup);
311
312 /* canonical value */
313 ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
314 LY_CHECK_GOTO(ret, error);
315
316 if (original->target) {
317 /* copy path */
318 ret = ly_path_dup(ctx, original->target, &dup->target);
319 LY_CHECK_GOTO(ret, error);
320 } /* else is the special path "/" that has no target stored */
321
322 dup->realtype = original->realtype;
323 return LY_SUCCESS;
324
325error:
327 return ret;
328}
329
338 {
339 .module = "ietf-netconf-acm",
340 .revision = "2012-02-22",
341 .name = "node-instance-identifier",
342
343 .plugin.id = "ly2 node-instance-identifier",
344 .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
345 .plugin.store = lyplg_type_store_node_instanceid,
346 .plugin.validate_value = NULL,
347 .plugin.validate_tree = NULL,
348 .plugin.compare = lyplg_type_compare_simple,
349 .plugin.sort = lyplg_type_sort_simple,
350 .plugin.print = lyplg_type_print_node_instanceid,
351 .plugin.duplicate = lyplg_type_dup_node_instanceid,
352 .plugin.free = lyplg_type_free_instanceid,
353 },
354 {
355 .module = "ietf-netconf-acm",
356 .revision = "2018-02-14",
357 .name = "node-instance-identifier",
358
359 .plugin.id = "ly2 node-instance-identifier",
360 .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
361 .plugin.store = lyplg_type_store_node_instanceid,
362 .plugin.validate_value = NULL,
363 .plugin.validate_tree = NULL,
364 .plugin.compare = lyplg_type_compare_simple,
365 .plugin.sort = lyplg_type_sort_simple,
366 .plugin.print = lyplg_type_print_node_instanceid,
367 .plugin.duplicate = lyplg_type_dup_node_instanceid,
368 .plugin.free = lyplg_type_free_instanceid,
369 },
370 {0}
371};
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:237
@ LYVE_DATA
Definition log.h:274
@ LY_EVALID
Definition log.h:245
@ LY_SUCCESS
Definition log.h:238
Libyang full error structure.
Definition log.h:282
Structure to hold a set of (not necessary somehow connected) objects. Usually used for lyd_node,...
Definition set.h:47
YANG extension compiled instance.
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 LY_ERR lyplg_type_check_value_size(const char *type_name, LY_VALUE_FORMAT format, uint32_t value_size_bits, enum lyplg_lyb_size_type lyb_size_type, uint32_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 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_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:312
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_DECL void lyplg_type_lyb_size_variable_bytes(const struct lysc_type *type, enum lyplg_lyb_size_type *size_type, uint32_t *fixed_size_bits)
Implementation of lyplg_type_lyb_size_clb for a type with variable length rounded to 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.
#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_CANON
Definition tree.h:235
@ LY_VALUE_XML
Definition tree.h:238
@ LY_VALUE_STR_NS
Definition tree.h:241
@ LY_VALUE_SCHEMA_RESOLVED
Definition tree.h:237
@ LY_VALUE_LYB
Definition tree.h:240
The main libyang public header.
uint8_t ly_bool
Type to indicate boolean value.
Definition log.h:35
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:567
const char * _canonical
Definition tree_data.h:564
YANG data representation.
Definition tree_data.h:563