libyang  2.0.7
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 #include "plugins_types.h"
16 
17 #include <assert.h>
18 #include <inttypes.h>
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "libyang.h"
25 
26 /* additional internal headers for some useful simple macros */
27 #include "common.h"
28 #include "compat.h"
29 #include "path.h"
30 #include "plugins_internal.h" /* LY_TYPE_*_STR */
31 
50 static LY_ERR
51 instanceid_path2str(const struct ly_path *path, LY_VALUE_FORMAT format, void *prefix_data, char **str)
52 {
53  LY_ERR ret = LY_SUCCESS;
55  char *result = NULL, quot;
56  const struct lys_module *mod = NULL;
57  ly_bool inherit_prefix, d;
58  const char *strval;
59 
60  switch (format) {
61  case LY_VALUE_XML:
62  case LY_VALUE_SCHEMA:
64  /* everything is prefixed */
65  inherit_prefix = 0;
66  break;
67  case LY_VALUE_CANON:
68  case LY_VALUE_JSON:
69  case LY_VALUE_LYB:
70  /* the same prefix is inherited and skipped */
71  inherit_prefix = 1;
72  break;
73  }
74 
75  LY_ARRAY_FOR(path, u) {
76  /* new node */
77  if (!inherit_prefix || (mod != path[u].node->module)) {
78  mod = path[u].node->module;
79  ret = ly_strcat(&result, "/%s:%s", lyplg_type_get_prefix(mod, format, prefix_data), path[u].node->name);
80  } else {
81  ret = ly_strcat(&result, "/%s", path[u].node->name);
82  }
83  LY_CHECK_GOTO(ret, cleanup);
84 
85  /* node predicates */
86  LY_ARRAY_FOR(path[u].predicates, v) {
87  struct ly_path_predicate *pred = &path[u].predicates[v];
88 
89  switch (path[u].pred_type) {
90  case LY_PATH_PREDTYPE_NONE:
91  break;
92  case LY_PATH_PREDTYPE_POSITION:
93  /* position predicate */
94  ret = ly_strcat(&result, "[%" PRIu64 "]", pred->position);
95  break;
96  case LY_PATH_PREDTYPE_LIST:
97  /* key-predicate */
98  strval = pred->value.realtype->plugin->print(path[u].node->module->ctx, &pred->value, format, prefix_data,
99  &d, NULL);
100 
101  /* default quote */
102  quot = '\'';
103  if (strchr(strval, quot)) {
104  quot = '"';
105  }
106  if (inherit_prefix) {
107  /* always the same prefix as the parent */
108  ret = ly_strcat(&result, "[%s=%c%s%c]", pred->key->name, quot, strval, quot);
109  } else {
110  ret = ly_strcat(&result, "[%s:%s=%c%s%c]", lyplg_type_get_prefix(pred->key->module, format, prefix_data),
111  pred->key->name, quot, strval, quot);
112  }
113  if (d) {
114  free((char *)strval);
115  }
116  break;
117  case LY_PATH_PREDTYPE_LEAFLIST:
118  /* leaf-list-predicate */
119  strval = pred->value.realtype->plugin->print(path[u].node->module->ctx, &pred->value, format, prefix_data,
120  &d, NULL);
121 
122  /* default quote */
123  quot = '\'';
124  if (strchr(strval, quot)) {
125  quot = '"';
126  }
127  ret = ly_strcat(&result, "[.=%c%s%c]", quot, strval, quot);
128  if (d) {
129  free((char *)strval);
130  }
131  break;
132  }
133 
134  LY_CHECK_GOTO(ret, cleanup);
135  }
136  }
137 
138 cleanup:
139  if (ret) {
140  free(result);
141  } else {
142  *str = result;
143  }
144  return ret;
145 }
146 
147 API LY_ERR
148 lyplg_type_store_instanceid(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
149  uint32_t options, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node,
150  struct lyd_value *storage, struct lys_glob_unres *unres, struct ly_err_item **err)
151 {
152  LY_ERR ret = LY_SUCCESS;
153  struct lysc_type_instanceid *type_inst = (struct lysc_type_instanceid *)type;
154  struct ly_path *path;
155  char *canon;
156 
157  /* init storage */
158  memset(storage, 0, sizeof *storage);
159  storage->realtype = type;
160 
161  /* check hints */
162  ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
163  LY_CHECK_GOTO(ret, cleanup);
164 
165  /* compile instance-identifier into path */
166  ret = lyplg_type_lypath_new(ctx, value, value_len, options, format, prefix_data, ctx_node,
167  unres, &path, err);
168  LY_CHECK_GOTO(ret, cleanup);
169 
170  /* store value */
171  storage->target = path;
172 
173  /* store canonical value */
174  if (format == LY_VALUE_CANON) {
175  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
176  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
177  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
178  LY_CHECK_GOTO(ret, cleanup);
179  } else {
180  ret = lydict_insert(ctx, value, value_len, &storage->_canonical);
181  LY_CHECK_GOTO(ret, cleanup);
182  }
183  } else {
184  /* JSON format with prefix is the canonical one */
185  ret = instanceid_path2str(path, LY_VALUE_JSON, NULL, &canon);
186  LY_CHECK_GOTO(ret, cleanup);
187 
188  ret = lydict_insert_zc(ctx, canon, &storage->_canonical);
189  LY_CHECK_GOTO(ret, cleanup);
190  }
191 
192 cleanup:
193  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
194  free((void *)value);
195  }
196 
197  if (ret) {
198  lyplg_type_free_instanceid(ctx, storage);
199  }
200  if (!ret && type_inst->require_instance) {
201  /* needs to be resolved */
202  return LY_EINCOMPLETE;
203  } else {
204  return ret;
205  }
206 }
207 
208 API LY_ERR
209 lyplg_type_validate_instanceid(const struct ly_ctx *ctx, const struct lysc_type *UNUSED(type),
210  const struct lyd_node *UNUSED(ctx_node), const struct lyd_node *tree, struct lyd_value *storage,
211  struct ly_err_item **err)
212 {
213  struct lysc_type_instanceid *type_inst = (struct lysc_type_instanceid *)storage->realtype;
214  LY_ERR ret = LY_SUCCESS;
215 
216  *err = NULL;
217 
218  if (!type_inst->require_instance) {
219  /* redundant to resolve */
220  return LY_SUCCESS;
221  }
222 
223  /* find the target in data */
224  if ((ret = ly_path_eval(storage->target, tree, NULL))) {
225  const char *value = lyplg_type_print_instanceid(ctx, storage, LY_VALUE_CANON, NULL, NULL, NULL);
226  return ly_err_new(err, ret, LYVE_DATA, NULL, NULL, LY_ERRMSG_NOINST, value);
227  }
228 
229  return LY_SUCCESS;
230 }
231 
232 API LY_ERR
233 lyplg_type_compare_instanceid(const struct lyd_value *val1, const struct lyd_value *val2)
234 {
235  LY_ARRAY_COUNT_TYPE u, v;
236 
237  if (val1->realtype != val2->realtype) {
238  return LY_ENOT;
239  }
240 
241  if (val1 == val2) {
242  return LY_SUCCESS;
243  } else if (LY_ARRAY_COUNT(val1->target) != LY_ARRAY_COUNT(val2->target)) {
244  return LY_ENOT;
245  }
246 
247  LY_ARRAY_FOR(val1->target, u) {
248  struct ly_path *s1 = &val1->target[u];
249  struct ly_path *s2 = &val2->target[u];
250 
251  if ((s1->node != s2->node) || (s1->pred_type != s2->pred_type) ||
252  (s1->predicates && (LY_ARRAY_COUNT(s1->predicates) != LY_ARRAY_COUNT(s2->predicates)))) {
253  return LY_ENOT;
254  }
255  if (s1->predicates) {
256  LY_ARRAY_FOR(s1->predicates, v) {
257  struct ly_path_predicate *pred1 = &s1->predicates[v];
258  struct ly_path_predicate *pred2 = &s2->predicates[v];
259 
260  switch (s1->pred_type) {
261  case LY_PATH_PREDTYPE_NONE:
262  break;
263  case LY_PATH_PREDTYPE_POSITION:
264  /* position predicate */
265  if (pred1->position != pred2->position) {
266  return LY_ENOT;
267  }
268  break;
269  case LY_PATH_PREDTYPE_LIST:
270  /* key-predicate */
271  if ((pred1->key != pred2->key) ||
272  ((struct lysc_node_leaf *)pred1->key)->type->plugin->compare(&pred1->value, &pred2->value)) {
273  return LY_ENOT;
274  }
275  break;
276  case LY_PATH_PREDTYPE_LEAFLIST:
277  /* leaf-list predicate */
278  if (((struct lysc_node_leaflist *)s1->node)->type->plugin->compare(&pred1->value, &pred2->value)) {
279  return LY_ENOT;
280  }
281  }
282  }
283  }
284  }
285 
286  return LY_SUCCESS;
287 }
288 
289 API const void *
290 lyplg_type_print_instanceid(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *value, LY_VALUE_FORMAT format,
291  void *prefix_data, ly_bool *dynamic, size_t *value_len)
292 {
293  char *ret;
294 
295  if ((format == LY_VALUE_CANON) || (format == LY_VALUE_JSON) || (format == LY_VALUE_LYB)) {
296  if (dynamic) {
297  *dynamic = 0;
298  }
299  if (value_len) {
300  *value_len = strlen(value->_canonical);
301  }
302  return value->_canonical;
303  }
304 
305  /* print the value in the specific format */
306  if (instanceid_path2str(value->target, format, prefix_data, &ret)) {
307  return NULL;
308  }
309  *dynamic = 1;
310  if (value_len) {
311  *value_len = strlen(ret);
312  }
313  return ret;
314 }
315 
316 API LY_ERR
317 lyplg_type_dup_instanceid(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
318 {
319  LY_ERR ret;
320 
321  memset(dup, 0, sizeof *dup);
322 
323  /* canonical value */
324  ret = lydict_insert(ctx, original->_canonical, ly_strlen(original->_canonical), &dup->_canonical);
325  LY_CHECK_GOTO(ret, error);
326 
327  /* copy path */
328  ret = ly_path_dup(ctx, original->target, &dup->target);
329  LY_CHECK_GOTO(ret, error);
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 
339 API void
340 lyplg_type_free_instanceid(const struct ly_ctx *ctx, struct lyd_value *value)
341 {
342  lydict_remove(ctx, value->_canonical);
343  ly_path_free(ctx, value->target);
344 }
345 
353 const struct lyplg_type_record plugins_instanceid[] = {
354  {
355  .module = "",
356  .revision = NULL,
357  .name = LY_TYPE_INST_STR,
358 
359  .plugin.id = "libyang 2 - instance-identifier, version 1",
360  .plugin.store = lyplg_type_store_instanceid,
361  .plugin.validate = lyplg_type_validate_instanceid,
362  .plugin.compare = lyplg_type_compare_instanceid,
363  .plugin.sort = NULL,
364  .plugin.print = lyplg_type_print_instanceid,
365  .plugin.duplicate = lyplg_type_dup_instanceid,
366  .plugin.free = lyplg_type_free_instanceid
367  },
368  {0}
369 };
libyang context handler.
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 ...
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 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...
LY_ERR
libyang's error codes returned by the libyang functions.
Definition: log.h:242
@ LYVE_DATA
Definition: log.h:279
@ LY_ENOT
Definition: log.h:256
@ LY_SUCCESS
Definition: log.h:243
@ LY_EINCOMPLETE
Definition: log.h:252
Libyang full error structure.
Definition: log.h:287
const char * module
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.
LY_ERR lyplg_type_lypath_new(const struct ly_ctx *ctx, const char *value, size_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...
LY_ERR ly_err_new(struct ly_err_item **err, LY_ERR ecode, LY_VECODE vecode, char *path, char *apptag, const char *err_msg,...)
Create and fill error structure.
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.
API LY_ERR lyplg_type_dup_instanceid(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
Implementation of lyplg_type_dup_clb for the built-in instance-identifier type.
Definition: instanceid.c:317
API 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:340
API LY_ERR lyplg_type_compare_instanceid(const struct lyd_value *val1, const struct lyd_value *val2)
Implementation of lyplg_type_compare_clb for the built-in instance-identifier type.
Definition: instanceid.c:233
API LY_ERR lyplg_type_store_instanceid(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len, uint32_t options, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node, struct lyd_value *storage, struct lys_glob_unres *unres, struct ly_err_item **err)
Implementation of lyplg_type_store_clb for the built-in instance-identifier type.
Definition: instanceid.c:148
#define LYPLG_TYPE_STORE_DYNAMIC
LY_DATA_TYPE basetype
Definition: tree_schema.h:1531
Available YANG schema tree structures representing YANG module.
Definition: tree_schema.h:2295
Compiled YANG data node.
Definition: tree_schema.h:1644
#define LY_ARRAY_COUNT(ARRAY)
Get the number of records in the ARRAY.
Definition: tree.h:148
#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:235
#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:240
@ LY_VALUE_SCHEMA
Definition: tree.h:237
@ LY_VALUE_CANON
Definition: tree.h:236
@ LY_VALUE_XML
Definition: tree.h:239
@ LY_VALUE_SCHEMA_RESOLVED
Definition: tree.h:238
@ LY_VALUE_LYB
Definition: tree.h:241
API LY_ERR lyplg_type_validate_instanceid(const struct ly_ctx *ctx, const struct lysc_type *UNUSED(type), const struct lyd_node *UNUSED(ctx_node), const struct lyd_node *tree, struct lyd_value *storage, struct ly_err_item **err)
Definition: instanceid.c:209
API const void * lyplg_type_print_instanceid(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *value, LY_VALUE_FORMAT format, void *prefix_data, ly_bool *dynamic, size_t *value_len)
Definition: instanceid.c:290
const struct lyplg_type_record plugins_instanceid[]
Plugin information for instance-identifier type implementation.
Definition: instanceid.c:353
The main libyang public header.
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:25
API for (user) types plugins.
const struct lysc_type * realtype
Definition: tree_data.h:535
const char * _canonical
Definition: tree_data.h:532
Generic structure for a data node.
Definition: tree_data.h:754
YANG data representation.
Definition: tree_data.h:531