libyang  6.4.33
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
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 
48 static LY_ERR
49 instanceid_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 
152 cleanup:
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 
164 static LY_ERR
165 lyplg_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 
219 cleanup:
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 
235 static LY_ERR
236 lyplg_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), const struct lyd_val_unres *val_unres,
238  struct lyd_value *storage, struct ly_err_item **err)
239 {
240  LY_ERR r;
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  r = ly_path_eval(storage->target, ctx_node, val_unres, NULL, NULL);
257  if (r == LY_EINCOMPLETE) {
258  return r;
259  } else if (r) {
260  value = lyd_value_get_canonical(ctx, storage);
261  path = lyd_path(ctx_node, LYD_PATH_STD, NULL, 0);
262  return ly_err_new(err, LY_EVALID, LYVE_DATA, path, strdup("instance-required"), LY_ERRMSG_NOINST, value);
263  }
264 
265  return LY_SUCCESS;
266 }
267 
268 static const void *
269 lyplg_type_print_instanceid(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *value, LY_VALUE_FORMAT format,
270  void *prefix_data, ly_bool *dynamic, uint64_t *value_size_bits)
271 {
272  char *ret;
273 
274  switch (format) {
275  case LY_VALUE_CANON:
276  case LY_VALUE_JSON:
277  case LY_VALUE_LYB:
278  case LY_VALUE_CBOR:
279  if (dynamic) {
280  *dynamic = 0;
281  }
282  if (value_size_bits) {
283  *value_size_bits = strlen(value->_canonical) * 8;
284  }
285  return value->_canonical;
286  default:
287  break;
288  }
289 
290  /* print the value in the specific format */
291  if (instanceid_path2str(value->target, format, prefix_data, &ret)) {
292  return NULL;
293  }
294 
295  *dynamic = 1;
296  if (value_size_bits) {
297  *value_size_bits = strlen(ret) * 8;
298  }
299 
300  return ret;
301 }
302 
303 static LY_ERR
304 lyplg_type_dup_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  /* copy path */
315  ret = ly_path_dup(ctx, original->target, &dup->target);
316  LY_CHECK_GOTO(ret, error);
317 
318  dup->realtype = original->realtype;
319  return LY_SUCCESS;
320 
321 error:
322  lyplg_type_free_instanceid(ctx, dup);
323  return ret;
324 }
325 
326 LIBYANG_API_DEF void
327 lyplg_type_free_instanceid(const struct ly_ctx *ctx, struct lyd_value *value)
328 {
329  lydict_remove(ctx, value->_canonical);
330  value->_canonical = NULL;
331  ly_path_free(value->target);
332 }
333 
341 const struct lyplg_type_record plugins_instanceid[] = {
342  {
343  .module = "",
344  .revision = NULL,
345  .name = LY_TYPE_INST_STR,
346 
347  .plugin.id = "ly2 instance-identifier",
348  .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
349  .plugin.store = lyplg_type_store_instanceid,
350  .plugin.validate_value = NULL,
351  .plugin.validate_tree = lyplg_type_validate_tree_instanceid,
352  .plugin.compare = lyplg_type_compare_simple,
353  .plugin.sort = lyplg_type_sort_simple,
354  .plugin.print = lyplg_type_print_instanceid,
355  .plugin.duplicate = lyplg_type_dup_instanceid,
356  .plugin.free = lyplg_type_free_instanceid,
357  },
358  {0}
359 };
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_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 * 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 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 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 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:327
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
LY_DATA_TYPE basetype
Definition: tree_schema.h:1278
Available YANG schema tree structures representing YANG module.
Definition: tree_schema.h:2242
Compiled YANG data node.
Definition: tree_schema.h:1412
const struct lyplg_type_record plugins_instanceid[]
Plugin information for instance-identifier type implementation.
Definition: instanceid.c:341
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:29
libyang sized array API.
API for (user) types plugins.
@ LYD_PATH_STD
Definition: tree_data.h:2350
const struct lysc_type * realtype
Definition: tree_data.h:614
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.
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.
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