libyang 6.4.3
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
Loading...
Searching...
No Matches
instanceid.c
Go to the documentation of this file.
1
15#define _GNU_SOURCE /* strdup */
16
17#include "plugins_types.h"
18
19#include <assert.h>
20#include <stdint.h>
21#include <stdlib.h>
22
23#include "compat.h"
24#include "dict.h"
25#include "ly_array.h"
26#include "ly_common.h"
27#include "path.h"
28#include "plugins_internal.h"
29
48static LY_ERR
49instanceid_path2str(const struct ly_path *path, LY_VALUE_FORMAT format, void *prefix_data, char **str)
50{
51 LY_ERR ret = LY_SUCCESS;
52 LYA_COUNT_T u, v;
53 char *result = NULL, quot;
54 const struct lys_module *mod = NULL, *local_mod = NULL;
55 struct ly_set *mods = NULL;
56 ly_bool inherit_prefix = 0;
57 const char *strval;
58
59 switch (format) {
60 case LY_VALUE_XML:
61 case LY_VALUE_STR_NS:
62 /* zero the local module so that all the prefixes are printed */
63 mods = prefix_data;
64 local_mod = mods->objs[0];
65 mods->objs[0] = NULL;
66 break;
67 case LY_VALUE_SCHEMA:
69 /* nothing to do */
70 break;
71 case LY_VALUE_CANON:
72 case LY_VALUE_CBOR:
73 case LY_VALUE_JSON:
74 case LY_VALUE_LYB:
75 /* zero the local module so that the first node is always prefixed */
76 prefix_data = NULL;
77 break;
78 }
79
80 switch (format) {
81 case LY_VALUE_XML:
82 case LY_VALUE_SCHEMA:
84 /* everything is prefixed */
85 inherit_prefix = 0;
86 break;
87 case LY_VALUE_CANON:
88 case LY_VALUE_CBOR:
89 case LY_VALUE_JSON:
90 case LY_VALUE_LYB:
91 case LY_VALUE_STR_NS:
92 /* the same prefix is inherited and skipped */
93 inherit_prefix = 1;
94 break;
95 }
96
97 LYA_FOR(path, u) {
98 /* new node */
99 if (!inherit_prefix || (mod != path[u].node->module)) {
100 mod = path[u].node->module;
101 ret = ly_strcat(&result, "/%s:%s", lyplg_type_get_prefix(mod, format, prefix_data), path[u].node->name);
102 } else {
103 ret = ly_strcat(&result, "/%s", path[u].node->name);
104 }
105 LY_CHECK_GOTO(ret, cleanup);
106
107 /* node predicates */
108 LYA_FOR(path[u].predicates, v) {
109 struct ly_path_predicate *pred = &path[u].predicates[v];
110
111 switch (pred->type) {
112 case LY_PATH_PREDTYPE_POSITION:
113 /* position predicate */
114 ret = ly_strcat(&result, "[%" PRIu64 "]", pred->position);
115 break;
116 case LY_PATH_PREDTYPE_LIST:
117 /* key-predicate */
118 ret = lyplg_type_print_val(pred->key, pred->value, format, prefix_data, &strval);
119 LY_CHECK_GOTO(ret, cleanup);
120
121 /* default quote */
122 LY_CHECK_GOTO(ret = ly_val_get_quot(pred->key->module->ctx, strval, &quot), cleanup);
123 if (inherit_prefix) {
124 /* always the same prefix as the parent */
125 ret = ly_strcat(&result, "[%s=%c%s%c]", pred->key->name, quot, strval, quot);
126 } else {
127 ret = ly_strcat(&result, "[%s:%s=%c%s%c]", lyplg_type_get_prefix(pred->key->module, format, prefix_data),
128 pred->key->name, quot, strval, quot);
129 }
130 lydict_remove(pred->key->module->ctx, strval);
131 break;
132 case LY_PATH_PREDTYPE_LEAFLIST:
133 /* leaf-list-predicate */
134 ret = lyplg_type_print_val(path[u].node, pred->value, format, prefix_data, &strval);
135 LY_CHECK_GOTO(ret, cleanup);
136
137 /* default quote */
138 LY_CHECK_GOTO(ret = ly_val_get_quot(path[u].node->module->ctx, strval, &quot), cleanup);
139 ret = ly_strcat(&result, "[.=%c%s%c]", quot, strval, quot);
140 lydict_remove(path[u].node->module->ctx, strval);
141 break;
142 case LY_PATH_PREDTYPE_LIST_VAR:
143 LOGINT(path[u].node->module->ctx);
144 ret = LY_EINT;
145 goto cleanup;
146 }
147
148 LY_CHECK_GOTO(ret, cleanup);
149 }
150 }
151
152cleanup:
153 if (local_mod) {
154 mods->objs[0] = (void *)local_mod;
155 }
156 if (ret) {
157 free(result);
158 } else {
159 *str = result;
160 }
161 return ret;
162}
163
164static LY_ERR
165lyplg_type_store_instanceid(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, uint64_t value_size_bits,
166 uint32_t options, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node,
167 struct lyd_value *storage, struct lys_glob_unres *unres, struct ly_err_item **err)
168{
169 LY_ERR ret = LY_SUCCESS;
170 struct lysc_type_instanceid *type_inst = (struct lysc_type_instanceid *)type;
171 uint32_t value_size;
172 struct ly_path *path;
173 char *canon;
174
175 /* init storage */
176 memset(storage, 0, sizeof *storage);
177 storage->realtype = type;
178
179 /* check value length */
180 ret = lyplg_type_check_value_size("instance-identifier", format, value_size_bits, LYPLG_LYB_SIZE_VARIABLE_BYTES, 0,
181 &value_size, err);
182 LY_CHECK_GOTO(ret, cleanup);
183
184 /* check hints */
185 ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
186 LY_CHECK_GOTO(ret, cleanup);
187
188 /* compile instance-identifier into path */
189 ret = lyplg_type_lypath_new(ctx, value, value_size, options, (format == LY_VALUE_LYB) ? LY_VALUE_JSON : format,
190 prefix_data, ctx_node, unres, &path, err);
191 LY_CHECK_GOTO(ret, cleanup);
192
193 /* store value */
194 storage->target = path;
195
196 /* check status */
197 ret = lyplg_type_lypath_check_status(ctx_node, path, format, prefix_data, err);
198 LY_CHECK_GOTO(ret, cleanup);
199
200 /* store canonical value */
201 if (format == LY_VALUE_CANON) {
202 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
203 ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
204 options &= ~LYPLG_TYPE_STORE_DYNAMIC;
205 LY_CHECK_GOTO(ret, cleanup);
206 } else {
207 ret = lydict_insert(ctx, value, value_size, &storage->_canonical);
208 LY_CHECK_GOTO(ret, cleanup);
209 }
210 } else {
211 /* JSON format with prefix is the canonical one */
212 ret = instanceid_path2str(path, LY_VALUE_JSON, NULL, &canon);
213 LY_CHECK_GOTO(ret, cleanup);
214
215 ret = lydict_insert_zc(ctx, canon, &storage->_canonical);
216 LY_CHECK_GOTO(ret, cleanup);
217 }
218
219cleanup:
220 if (options & LYPLG_TYPE_STORE_DYNAMIC) {
221 free((void *)value);
222 }
223
224 if (ret) {
225 lyplg_type_free_instanceid(ctx, storage);
226 }
227 if (!ret && type_inst->require_instance) {
228 /* needs to be resolved */
229 return LY_EINCOMPLETE;
230 } else {
231 return ret;
232 }
233}
234
235static LY_ERR
236lyplg_type_validate_tree_instanceid(const struct ly_ctx *ctx, const struct lysc_type *UNUSED(type),
237 const struct lyd_node *ctx_node, const struct lyd_node *UNUSED(tree), struct lyd_value *storage,
238 struct ly_err_item **err)
239{
240 LY_ERR ret = LY_SUCCESS;
241 struct lysc_type_instanceid *type_inst = (struct lysc_type_instanceid *)storage->realtype;
242 const char *value;
243 char *path;
244
245 /* instance-identifier must be a leaf or leaf-list */
246 assert(ctx_node);
247
248 *err = NULL;
249
250 if (!type_inst->require_instance) {
251 /* redundant to resolve */
252 return LY_SUCCESS;
253 }
254
255 /* find the target in data */
256 if ((ret = ly_path_eval(storage->target, ctx_node, NULL, NULL))) {
257 value = lyd_value_get_canonical(ctx, storage);
258 path = lyd_path(ctx_node, LYD_PATH_STD, NULL, 0);
259 return ly_err_new(err, ret, LYVE_DATA, path, strdup("instance-required"), LY_ERRMSG_NOINST, value);
260 }
261
262 return LY_SUCCESS;
263}
264
265static const void *
266lyplg_type_print_instanceid(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *value, LY_VALUE_FORMAT format,
267 void *prefix_data, ly_bool *dynamic, uint64_t *value_size_bits)
268{
269 char *ret;
270
271 switch (format) {
272 case LY_VALUE_CANON:
273 case LY_VALUE_JSON:
274 case LY_VALUE_LYB:
275 case LY_VALUE_CBOR:
276 if (dynamic) {
277 *dynamic = 0;
278 }
279 if (value_size_bits) {
280 *value_size_bits = strlen(value->_canonical) * 8;
281 }
282 return value->_canonical;
283 default:
284 break;
285 }
286
287 /* print the value in the specific format */
288 if (instanceid_path2str(value->target, format, prefix_data, &ret)) {
289 return NULL;
290 }
291
292 *dynamic = 1;
293 if (value_size_bits) {
294 *value_size_bits = strlen(ret) * 8;
295 }
296
297 return ret;
298}
299
300static LY_ERR
301lyplg_type_dup_instanceid(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
302{
303 LY_ERR ret;
304
305 memset(dup, 0, sizeof *dup);
306
307 /* canonical value */
308 ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
309 LY_CHECK_GOTO(ret, error);
310
311 /* copy path */
312 ret = ly_path_dup(ctx, original->target, &dup->target);
313 LY_CHECK_GOTO(ret, error);
314
315 dup->realtype = original->realtype;
316 return LY_SUCCESS;
317
318error:
320 return ret;
321}
322
323LIBYANG_API_DEF void
324lyplg_type_free_instanceid(const struct ly_ctx *ctx, struct lyd_value *value)
325{
326 lydict_remove(ctx, value->_canonical);
327 value->_canonical = NULL;
328 ly_path_free(value->target);
329}
330
339 {
340 .module = "",
341 .revision = NULL,
342 .name = LY_TYPE_INST_STR,
343
344 .plugin.id = "ly2 instance-identifier",
345 .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
346 .plugin.store = lyplg_type_store_instanceid,
347 .plugin.validate_value = NULL,
348 .plugin.validate_tree = lyplg_type_validate_tree_instanceid,
349 .plugin.compare = lyplg_type_compare_simple,
350 .plugin.sort = lyplg_type_sort_simple,
351 .plugin.print = lyplg_type_print_instanceid,
352 .plugin.duplicate = lyplg_type_dup_instanceid,
353 .plugin.free = lyplg_type_free_instanceid,
354 },
355 {0}
356};
libyang dictionary
#define LYA_FOR(ARRAY, INDEX)
Helper macro to go through sized-arrays with a numeric iterator.
Definition ly_array.h:55
#define LYA_COUNT_T
Type (i.e. size) of the sized array's size counter.
Definition ly_array.h:38
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_EINT
Definition log.h:259
@ LY_SUCCESS
Definition log.h:253
@ LY_EINCOMPLETE
Definition log.h:262
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
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_lypath_new(const struct ly_ctx *ctx, const char *value, uint32_t value_len, uint32_t options, LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *ctx_node, struct lys_glob_unres *unres, struct ly_path **path, struct ly_err_item **err)
Helper function to create internal schema path representation for instance-identifier value represent...
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 LY_ERR lyplg_type_lypath_check_status(const struct lysc_node *ctx_node, const struct ly_path *path, LY_VALUE_FORMAT format, void *prefix_data, struct ly_err_item **err)
Check that the lypath instance-identifier value is allowed based on the status of the nodes.
LIBYANG_API_DEF 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:324
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
LY_DATA_TYPE basetype
Available YANG schema tree structures representing YANG module.
Compiled YANG data node.
const struct lyplg_type_record plugins_instanceid[]
Plugin information for instance-identifier type implementation.
Definition instanceid.c:338
uint8_t ly_bool
Type to indicate boolean value.
Definition log.h:36
libyang sized array API.
API for (user) types plugins.
LIBYANG_API_DECL const char * lyd_value_get_canonical(const struct ly_ctx *ctx, const struct lyd_value *value)
Get the (canonical) value of a lyd_value.
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:2350
const struct lysc_type * realtype
Definition tree_data.h:614
const char * _canonical
Definition tree_data.h:611
Generic structure for a data node.
Definition tree_data.h:875
YANG data representation.
Definition tree_data.h:610
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition utils.h:93
@ LY_VALUE_JSON
Definition utils.h:98
@ LY_VALUE_SCHEMA
Definition utils.h:95
@ LY_VALUE_CBOR
Definition utils.h:99
@ LY_VALUE_CANON
Definition utils.h:94
@ LY_VALUE_XML
Definition utils.h:97
@ LY_VALUE_STR_NS
Definition utils.h:101
@ LY_VALUE_SCHEMA_RESOLVED
Definition utils.h:96
@ LY_VALUE_LYB
Definition utils.h:100