libyang  6.4.13
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 "compat.h"
21 #include "dict.h"
22 #include "ly_array.h"
23 #include "ly_common.h"
24 #include "path.h"
25 #include "plugins_internal.h"
26 #include "xpath.h"
27 
46 static LY_ERR
47 node_instanceid_path2str(const struct ly_path *path, LY_VALUE_FORMAT format, void *prefix_data, char **str)
48 {
49  LY_ERR ret = LY_SUCCESS;
50  LYA_COUNT_T u, v;
51  char *result = NULL, quot;
52  const struct lys_module *mod = NULL, *local_mod = NULL;
53  struct ly_set *mods;
54  ly_bool inherit_prefix = 0;
55  const char *strval;
56 
57  if (!path) {
58  /* special path */
59  ret = ly_strcat(&result, "/");
60  goto cleanup;
61  }
62 
63  switch (format) {
64  case LY_VALUE_XML:
65  case LY_VALUE_STR_NS:
66  /* zero 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  break;
71  case LY_VALUE_SCHEMA:
73  /* nothing to do */
74  break;
75  case LY_VALUE_CANON:
76  case LY_VALUE_CBOR:
77  case LY_VALUE_JSON:
78  case LY_VALUE_LYB:
79  /* zero the local module so that the first node is always prefixed */
80  prefix_data = NULL;
81  break;
82  }
83 
84  switch (format) {
85  case LY_VALUE_XML:
86  case LY_VALUE_SCHEMA:
88  /* everything is prefixed */
89  inherit_prefix = 0;
90  break;
91  case LY_VALUE_CANON:
92  case LY_VALUE_CBOR:
93  case LY_VALUE_JSON:
94  case LY_VALUE_LYB:
95  case LY_VALUE_STR_NS:
96  /* the same prefix is inherited and skipped */
97  inherit_prefix = 1;
98  break;
99  }
100 
101  LYA_FOR(path, u) {
102  /* new node */
103  if (!inherit_prefix || (mod != path[u].node->module)) {
104  mod = path[u].node->module;
105  ret = ly_strcat(&result, "/%s:%s", lyplg_type_get_prefix(mod, format, prefix_data), path[u].node->name);
106  } else {
107  ret = ly_strcat(&result, "/%s", path[u].node->name);
108  }
109  LY_CHECK_GOTO(ret, cleanup);
110 
111  /* node predicates */
112  LYA_FOR(path[u].predicates, v) {
113  struct ly_path_predicate *pred = &path[u].predicates[v];
114 
115  switch (pred->type) {
116  case LY_PATH_PREDTYPE_POSITION:
117  /* position predicate */
118  ret = ly_strcat(&result, "[%" PRIu64 "]", pred->position);
119  break;
120  case LY_PATH_PREDTYPE_LIST:
121  /* key-predicate */
122  ret = lyplg_type_print_val(pred->key, pred->value, format, prefix_data, &strval);
123  LY_CHECK_GOTO(ret, cleanup);
124 
125  /* default quote */
126  LY_CHECK_GOTO(ret = ly_val_get_quot(pred->key->module->ctx, strval, &quot), cleanup);
127  if (inherit_prefix) {
128  /* always the same prefix as the parent */
129  ret = ly_strcat(&result, "[%s=%c%s%c]", pred->key->name, quot, strval, quot);
130  } else {
131  ret = ly_strcat(&result, "[%s:%s=%c%s%c]", lyplg_type_get_prefix(pred->key->module, format, prefix_data),
132  pred->key->name, quot, strval, quot);
133  }
134  lydict_remove(pred->key->module->ctx, strval);
135  break;
136  case LY_PATH_PREDTYPE_LEAFLIST:
137  /* leaf-list-predicate */
138  ret = lyplg_type_print_val(path[u].node, pred->value, format, prefix_data, &strval);
139  LY_CHECK_GOTO(ret, cleanup);
140 
141  /* default quote */
142  LY_CHECK_GOTO(ret = ly_val_get_quot(path[u].node->module->ctx, strval, &quot), cleanup);
143  ret = ly_strcat(&result, "[.=%c%s%c]", quot, strval, quot);
144  lydict_remove(path[u].node->module->ctx, strval);
145  break;
146  case LY_PATH_PREDTYPE_LIST_VAR:
147  /* key-predicate with a variable */
148  if (inherit_prefix) {
149  /* always the same prefix as the parent */
150  ret = ly_strcat(&result, "[%s=$%s]", pred->key->name, pred->variable);
151  } else {
152  ret = ly_strcat(&result, "[%s:%s=$%s]", lyplg_type_get_prefix(pred->key->module, format, prefix_data),
153  pred->key->name, pred->variable);
154  }
155  break;
156  }
157 
158  LY_CHECK_GOTO(ret, cleanup);
159  }
160  }
161 
162 cleanup:
163  if (local_mod) {
164  mods->objs[0] = (void *)local_mod;
165  }
166  if (ret) {
167  free(result);
168  } else {
169  *str = result;
170  }
171  return ret;
172 }
173 
177 static LY_ERR
178 lyplg_type_store_node_instanceid(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, uint64_t value_size_bits,
179  uint32_t options, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node,
180  struct lyd_value *storage, struct lys_glob_unres *unres, struct ly_err_item **err)
181 {
182  LY_ERR ret = LY_SUCCESS;
183  struct lyxp_expr *exp = NULL;
184  uint32_t value_size, prefix_opt = 0;
185  struct ly_path *path = NULL;
186  char *canon;
187 
188  /* init storage */
189  memset(storage, 0, sizeof *storage);
190  storage->realtype = type;
191 
192  /* check value length */
193  ret = lyplg_type_check_value_size("node-instance-identifier", format, value_size_bits, LYPLG_LYB_SIZE_VARIABLE_BYTES,
194  0, &value_size, err);
195  LY_CHECK_GOTO(ret, cleanup);
196 
197  /* check hints */
198  ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
199  LY_CHECK_GOTO(ret, cleanup);
200 
201  if ((((char *)value)[0] == '/') && (value_size == 1)) {
202  /* special path */
203  format = LY_VALUE_CANON;
204  goto store;
205  }
206 
207  switch (format) {
208  case LY_VALUE_SCHEMA:
210  case LY_VALUE_XML:
211  prefix_opt = LY_PATH_PREFIX_MANDATORY;
212  break;
213  case LY_VALUE_CANON:
214  case LY_VALUE_LYB:
215  case LY_VALUE_CBOR:
216  case LY_VALUE_JSON:
217  case LY_VALUE_STR_NS:
218  prefix_opt = LY_PATH_PREFIX_STRICT_INHERIT;
219  break;
220  }
221 
222  /* parse the value */
223  ret = ly_path_parse(ctx, ctx_node, value, value_size, 0, LY_PATH_BEGIN_ABSOLUTE, prefix_opt, LY_PATH_PRED_SIMPLE, &exp);
224  if (ret) {
225  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL,
226  "Invalid node-instance-identifier \"%.*s\" value - syntax error.", (int)value_size, (char *)value);
227  goto cleanup;
228  }
229 
230  if (options & LYPLG_TYPE_STORE_IMPLEMENT) {
231  /* implement all prefixes */
232  LY_CHECK_GOTO(ret = lys_compile_expr_implement(ctx, exp, format, prefix_data, 1, unres, NULL), cleanup);
233  }
234 
235  /* resolve it on schema tree, use JSON format instead of LYB because for this type they are equal but for some
236  * nested types (such as numbers in predicates in the path) LYB would be invalid */
237  ret = ly_path_compile(ctx, ctx_node, exp, (ctx_node && (ctx_node->flags & LYS_IS_OUTPUT)) ?
238  LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, 1, (format == LY_VALUE_LYB) ?
239  LY_VALUE_JSON : format, prefix_data, &path);
240  if (ret) {
241  ret = ly_err_new(err, ret, LYVE_DATA, NULL, NULL,
242  "Invalid node-instance-identifier \"%.*s\" value - semantic error.", (int)value_size, (char *)value);
243  goto cleanup;
244  }
245 
246 store:
247  /* store value */
248  storage->target = path;
249 
250  /* store canonical value */
251  if (format == LY_VALUE_CANON) {
252  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
253  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
254  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
255  LY_CHECK_GOTO(ret, cleanup);
256  } else {
257  ret = lydict_insert(ctx, value, value_size, &storage->_canonical);
258  LY_CHECK_GOTO(ret, cleanup);
259  }
260  } else {
261  /* JSON format with prefix is the canonical one */
262  ret = node_instanceid_path2str(path, LY_VALUE_JSON, NULL, &canon);
263  LY_CHECK_GOTO(ret, cleanup);
264 
265  ret = lydict_insert_zc(ctx, canon, &storage->_canonical);
266  LY_CHECK_GOTO(ret, cleanup);
267  }
268 
269 cleanup:
270  lyxp_expr_free(exp);
271  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
272  free((void *)value);
273  }
274 
275  if (ret) {
276  lyplg_type_free_instanceid(ctx, storage);
277  }
278  return ret;
279 }
280 
284 static const void *
285 lyplg_type_print_node_instanceid(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *value, LY_VALUE_FORMAT format,
286  void *prefix_data, ly_bool *dynamic, uint64_t *value_size_bits)
287 {
288  char *ret;
289 
290  if ((format == LY_VALUE_CANON) || (format == LY_VALUE_JSON) || (format == LY_VALUE_LYB)) {
291  if (dynamic) {
292  *dynamic = 0;
293  }
294  if (value_size_bits) {
295  *value_size_bits = strlen(value->_canonical) * 8;
296  }
297  return value->_canonical;
298  }
299 
300  /* print the value in the specific format */
301  if (node_instanceid_path2str(value->target, format, prefix_data, &ret)) {
302  return NULL;
303  }
304  *dynamic = 1;
305  if (value_size_bits) {
306  *value_size_bits = strlen(ret) * 8;
307  }
308  return ret;
309 }
310 
314 static LY_ERR
315 lyplg_type_dup_node_instanceid(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
316 {
317  LY_ERR ret;
318 
319  memset(dup, 0, sizeof *dup);
320 
321  /* canonical value */
322  ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
323  LY_CHECK_GOTO(ret, error);
324 
325  if (original->target) {
326  /* copy path */
327  ret = ly_path_dup(ctx, original->target, &dup->target);
328  LY_CHECK_GOTO(ret, error);
329  } /* else is the special path "/" that has no target stored */
330 
331  dup->realtype = original->realtype;
332  return LY_SUCCESS;
333 
334 error:
335  lyplg_type_free_instanceid(ctx, dup);
336  return ret;
337 }
338 
347  {
348  .module = "ietf-netconf-acm",
349  .revision = NULL,
350  .name = "node-instance-identifier",
351 
352  .plugin.id = "ly2 node-instance-identifier",
353  .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
354  .plugin.store = lyplg_type_store_node_instanceid,
355  .plugin.validate_value = NULL,
356  .plugin.validate_tree = NULL,
357  .plugin.compare = lyplg_type_compare_simple,
358  .plugin.sort = lyplg_type_sort_simple,
359  .plugin.print = lyplg_type_print_node_instanceid,
360  .plugin.duplicate = lyplg_type_dup_node_instanceid,
361  .plugin.free = lyplg_type_free_instanceid,
362  },
363  {0}
364 };
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_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
const char * module
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: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 an 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
Definition: tree_schema.h:1278
uint16_t flags
Definition: tree_schema.h:1414
Available YANG schema tree structures representing YANG module.
Definition: tree_schema.h:2242
Compiled YANG data node.
Definition: tree_schema.h:1412
#define LYS_IS_OUTPUT
Definition: tree_schema.h:716
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:29
libyang sized array API.
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:614
const char * _canonical
Definition: tree_data.h:611
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