libyang 5.4.9
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
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
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, uint64_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 struct lyd_value *storage, struct lys_glob_unres *unres, struct ly_err_item **err)
172{
173 LY_ERR ret = LY_SUCCESS;
174 struct lyxp_expr *exp = NULL;
175 uint32_t value_size, prefix_opt = 0;
176 struct ly_path *path = NULL;
177 char *canon;
178
179 /* init storage */
180 memset(storage, 0, sizeof *storage);
181 storage->realtype = type;
182
183 /* check value length */
184 ret = lyplg_type_check_value_size("node-instance-identifier", format, value_size_bits, LYPLG_LYB_SIZE_VARIABLE_BYTES,
185 0, &value_size, err);
186 LY_CHECK_GOTO(ret, cleanup);
187
188 /* check hints */
189 ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
190 LY_CHECK_GOTO(ret, cleanup);
191
192 if ((((char *)value)[0] == '/') && (value_size == 1)) {
193 /* special path */
194 format = LY_VALUE_CANON;
195 goto store;
196 }
197
198 switch (format) {
199 case LY_VALUE_SCHEMA:
201 case LY_VALUE_XML:
202 prefix_opt = LY_PATH_PREFIX_MANDATORY;
203 break;
204 case LY_VALUE_CANON:
205 case LY_VALUE_LYB:
206 case LY_VALUE_JSON:
207 case LY_VALUE_STR_NS:
208 prefix_opt = LY_PATH_PREFIX_STRICT_INHERIT;
209 break;
210 }
211
212 /* parse the value */
213 ret = ly_path_parse(ctx, ctx_node, value, value_size, 0, LY_PATH_BEGIN_ABSOLUTE, prefix_opt, LY_PATH_PRED_SIMPLE, &exp);
214 if (ret) {
215 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL,
216 "Invalid node-instance-identifier \"%.*s\" value - syntax error.", (int)value_size, (char *)value);
217 goto cleanup;
218 }
219
220 if (options & LYPLG_TYPE_STORE_IMPLEMENT) {
221 /* implement all prefixes */
222 LY_CHECK_GOTO(ret = lys_compile_expr_implement(ctx, exp, format, prefix_data, 1, unres, NULL), cleanup);
223 }
224
225 /* resolve it on schema tree, use JSON format instead of LYB because for this type they are equal but for some
226 * nested types (such as numbers in predicates in the path) LYB would be invalid */
227 ret = ly_path_compile(ctx, ctx_node, exp, (ctx_node && (ctx_node->flags & LYS_IS_OUTPUT)) ?
228 LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, 1, (format == LY_VALUE_LYB) ?
229 LY_VALUE_JSON : format, prefix_data, &path);
230 if (ret) {
231 ret = ly_err_new(err, ret, LYVE_DATA, NULL, NULL,
232 "Invalid node-instance-identifier \"%.*s\" value - semantic error.", (int)value_size, (char *)value);
233 goto cleanup;
234 }
235
236store:
237 /* store value */
238 storage->target = path;
239
240 /* store canonical value */
241 if (format == LY_VALUE_CANON) {
242 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
243 ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
244 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
245 LY_CHECK_GOTO(ret, cleanup);
246 } else {
247 ret = lydict_insert(ctx, value, value_size, &storage->_canonical);
248 LY_CHECK_GOTO(ret, cleanup);
249 }
250 } else {
251 /* JSON format with prefix is the canonical one */
252 ret = node_instanceid_path2str(path, LY_VALUE_JSON, NULL, &canon);
253 LY_CHECK_GOTO(ret, cleanup);
254
255 ret = lydict_insert_zc(ctx, canon, &storage->_canonical);
256 LY_CHECK_GOTO(ret, cleanup);
257 }
258
259cleanup:
260 lyxp_expr_free(exp);
261 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
262 free((void *)value);
263 }
264
265 if (ret) {
266 lyplg_type_free_instanceid(ctx, storage);
267 }
268 return ret;
269}
270
274static const void *
275lyplg_type_print_node_instanceid(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *value, LY_VALUE_FORMAT format,
276 void *prefix_data, ly_bool *dynamic, uint64_t *value_size_bits)
277{
278 char *ret;
279
280 if ((format == LY_VALUE_CANON) || (format == LY_VALUE_JSON) || (format == LY_VALUE_LYB)) {
281 if (dynamic) {
282 *dynamic = 0;
283 }
284 if (value_size_bits) {
285 *value_size_bits = strlen(value->_canonical) * 8;
286 }
287 return value->_canonical;
288 }
289
290 /* print the value in the specific format */
291 if (node_instanceid_path2str(value->target, format, prefix_data, &ret)) {
292 return NULL;
293 }
294 *dynamic = 1;
295 if (value_size_bits) {
296 *value_size_bits = strlen(ret) * 8;
297 }
298 return ret;
299}
300
304static LY_ERR
305lyplg_type_dup_node_instanceid(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
306{
307 LY_ERR ret;
308
309 memset(dup, 0, sizeof *dup);
310
311 /* canonical value */
312 ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
313 LY_CHECK_GOTO(ret, error);
314
315 if (original->target) {
316 /* copy path */
317 ret = ly_path_dup(ctx, original->target, &dup->target);
318 LY_CHECK_GOTO(ret, error);
319 } /* else is the special path "/" that has no target stored */
320
321 dup->realtype = original->realtype;
322 return LY_SUCCESS;
323
324error:
326 return ret;
327}
328
337 {
338 .module = "ietf-netconf-acm",
339 .revision = NULL,
340 .name = "node-instance-identifier",
341
342 .plugin.id = "ly2 node-instance-identifier",
343 .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
344 .plugin.store = lyplg_type_store_node_instanceid,
345 .plugin.validate_value = NULL,
346 .plugin.validate_tree = NULL,
347 .plugin.compare = lyplg_type_compare_simple,
348 .plugin.sort = lyplg_type_sort_simple,
349 .plugin.print = lyplg_type_print_node_instanceid,
350 .plugin.duplicate = lyplg_type_dup_node_instanceid,
351 .plugin.free = lyplg_type_free_instanceid,
352 },
353 {0}
354};
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:289
@ LY_EVALID
Definition log.h:260
@ LY_SUCCESS
Definition log.h:253
Libyang full error structure.
Definition log.h:297
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
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:309
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_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: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:527
const char * _canonical
Definition tree_data.h:524
YANG data representation.
Definition tree_data.h:523