libyang 3.7.8
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
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, d;
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 strval = pred->value.realtype->plugin->print(path[u].node->module->ctx, &pred->value, format, prefix_data,
108 &d, NULL);
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 if (d) {
123 free((char *)strval);
124 }
125 break;
126 case LY_PATH_PREDTYPE_LEAFLIST:
127 /* leaf-list-predicate */
128 strval = pred->value.realtype->plugin->print(path[u].node->module->ctx, &pred->value, format, prefix_data,
129 &d, NULL);
130
131 /* default quote */
132 quot = '\'';
133 if (strchr(strval, quot)) {
134 quot = '"';
135 }
136 ret = ly_strcat(&result, "[.=%c%s%c]", quot, strval, quot);
137 if (d) {
138 free((char *)strval);
139 }
140 break;
141 case LY_PATH_PREDTYPE_LIST_VAR:
142 /* key-predicate with a variable */
143 if (inherit_prefix) {
144 /* always the same prefix as the parent */
145 ret = ly_strcat(&result, "[%s=$%s]", pred->key->name, pred->variable);
146 } else {
147 ret = ly_strcat(&result, "[%s:%s=$%s]", lyplg_type_get_prefix(pred->key->module, format, prefix_data),
148 pred->key->name, pred->variable);
149 }
150 break;
151 }
152
153 LY_CHECK_GOTO(ret, cleanup);
154 }
155 }
156
157cleanup:
158 if (local_mod) {
159 mods->objs[0] = (void *)local_mod;
160 }
161 if (ret) {
162 free(result);
163 } else {
164 *str = result;
165 }
166 return ret;
167}
168
172static LY_ERR
173lyplg_type_store_node_instanceid(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
174 uint32_t options, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node,
175 struct lyd_value *storage, struct lys_glob_unres *unres, struct ly_err_item **err)
176{
177 LY_ERR ret = LY_SUCCESS;
178 struct lyxp_expr *exp = NULL;
179 uint32_t prefix_opt = 0;
180 struct ly_path *path = NULL;
181 char *canon;
182
183 /* init storage */
184 memset(storage, 0, sizeof *storage);
185 storage->realtype = type;
186
187 /* check hints */
188 ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
189 LY_CHECK_GOTO(ret, cleanup);
190
191 if ((((char *)value)[0] == '/') && (value_len == 1)) {
192 /* special path */
193 format = LY_VALUE_CANON;
194 goto store;
195 }
196
197 switch (format) {
198 case LY_VALUE_SCHEMA:
200 case LY_VALUE_XML:
201 prefix_opt = LY_PATH_PREFIX_MANDATORY;
202 break;
203 case LY_VALUE_CANON:
204 case LY_VALUE_LYB:
205 case LY_VALUE_JSON:
206 case LY_VALUE_STR_NS:
207 prefix_opt = LY_PATH_PREFIX_STRICT_INHERIT;
208 break;
209 }
210
211 /* parse the value */
212 ret = ly_path_parse(ctx, ctx_node, value, value_len, 0, LY_PATH_BEGIN_ABSOLUTE, prefix_opt, LY_PATH_PRED_SIMPLE, &exp);
213 if (ret) {
214 ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL,
215 "Invalid node-instance-identifier \"%.*s\" value - syntax error.", (int)value_len, (char *)value);
216 goto cleanup;
217 }
218
219 if (options & LYPLG_TYPE_STORE_IMPLEMENT) {
220 /* implement all prefixes */
221 LY_CHECK_GOTO(ret = lys_compile_expr_implement(ctx, exp, format, prefix_data, 1, unres, NULL), cleanup);
222 }
223
224 /* resolve it on schema tree, use JSON format instead of LYB because for this type they are equal but for some
225 * nested types (such as numbers in predicates in the path) LYB would be invalid */
226 ret = ly_path_compile(ctx, NULL, ctx_node, NULL, exp, (ctx_node && (ctx_node->flags & LYS_IS_OUTPUT)) ?
227 LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, 1, (format == LY_VALUE_LYB) ?
228 LY_VALUE_JSON : format, prefix_data, &path);
229 if (ret) {
230 ret = ly_err_new(err, ret, LYVE_DATA, NULL, NULL,
231 "Invalid node-instance-identifier \"%.*s\" value - semantic error.", (int)value_len, (char *)value);
232 goto cleanup;
233 }
234
235store:
236 /* store value */
237 storage->target = path;
238
239 /* store canonical value */
240 if (format == LY_VALUE_CANON) {
241 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
242 ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
243 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
244 LY_CHECK_GOTO(ret, cleanup);
245 } else {
246 ret = lydict_insert(ctx, value, value_len, &storage->_canonical);
247 LY_CHECK_GOTO(ret, cleanup);
248 }
249 } else {
250 /* JSON format with prefix is the canonical one */
251 ret = node_instanceid_path2str(path, LY_VALUE_JSON, NULL, &canon);
252 LY_CHECK_GOTO(ret, cleanup);
253
254 ret = lydict_insert_zc(ctx, canon, &storage->_canonical);
255 LY_CHECK_GOTO(ret, cleanup);
256 }
257
258cleanup:
259 lyxp_expr_free(ctx, exp);
260 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
261 free((void *)value);
262 }
263
264 if (ret) {
265 lyplg_type_free_instanceid(ctx, storage);
266 }
267 return ret;
268}
269
273static const void *
274lyplg_type_print_node_instanceid(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *value, LY_VALUE_FORMAT format,
275 void *prefix_data, ly_bool *dynamic, size_t *value_len)
276{
277 char *ret;
278
279 if ((format == LY_VALUE_CANON) || (format == LY_VALUE_JSON) || (format == LY_VALUE_LYB)) {
280 if (dynamic) {
281 *dynamic = 0;
282 }
283 if (value_len) {
284 *value_len = strlen(value->_canonical);
285 }
286 return value->_canonical;
287 }
288
289 /* print the value in the specific format */
290 if (node_instanceid_path2str(value->target, format, prefix_data, &ret)) {
291 return NULL;
292 }
293 *dynamic = 1;
294 if (value_len) {
295 *value_len = strlen(ret);
296 }
297 return ret;
298}
299
303static LY_ERR
304lyplg_type_dup_node_instanceid(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
305{
306 LY_ERR ret;
307
308 memset(dup, 0, sizeof *dup);
309
310 /* canonical value */
311 ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
312 LY_CHECK_GOTO(ret, error);
313
314 if (original->target) {
315 /* copy path */
316 ret = ly_path_dup(ctx, original->target, &dup->target);
317 LY_CHECK_GOTO(ret, error);
318 } /* else is the special path "/" that has no target stored */
319
320 dup->realtype = original->realtype;
321 return LY_SUCCESS;
322
323error:
325 return ret;
326}
327
336 {
337 .module = "ietf-netconf-acm",
338 .revision = "2012-02-22",
339 .name = "node-instance-identifier",
340
341 .plugin.id = "libyang 2 - node-instance-identifier, version 1",
342 .plugin.store = lyplg_type_store_node_instanceid,
343 .plugin.validate = NULL,
344 .plugin.compare = lyplg_type_compare_simple,
345 .plugin.sort = lyplg_type_sort_simple,
346 .plugin.print = lyplg_type_print_node_instanceid,
347 .plugin.duplicate = lyplg_type_dup_node_instanceid,
348 .plugin.free = lyplg_type_free_instanceid,
349 .plugin.lyb_data_len = -1,
350 },
351 {
352 .module = "ietf-netconf-acm",
353 .revision = "2018-02-14",
354 .name = "node-instance-identifier",
355
356 .plugin.id = "libyang 2 - node-instance-identifier, version 1",
357 .plugin.store = lyplg_type_store_node_instanceid,
358 .plugin.validate = NULL,
359 .plugin.compare = lyplg_type_compare_simple,
360 .plugin.sort = lyplg_type_sort_simple,
361 .plugin.print = lyplg_type_print_node_instanceid,
362 .plugin.duplicate = lyplg_type_dup_node_instanceid,
363 .plugin.free = lyplg_type_free_instanceid,
364 .plugin.lyb_data_len = -1,
365 },
366 {0}
367};
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_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
const char * module
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_hints(uint32_t hints, const char *value, size_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 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_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:342
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
Definition: tree_schema.h:1287
uint16_t flags
Definition: tree_schema.h:1423
Available YANG schema tree structures representing YANG module.
Definition: tree_schema.h:2111
Compiled YANG data node.
Definition: tree_schema.h:1421
#define LYS_IS_OUTPUT
Definition: tree_schema.h:724
#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:579
const char * _canonical
Definition: tree_data.h:576
YANG data representation.
Definition: tree_data.h:575