libyang  2.0.7
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
identityref.c
Go to the documentation of this file.
1 
15 #define _GNU_SOURCE /* asprintf, strdup */
16 #include <sys/cdefs.h>
17 
18 #include "plugins_types.h"
19 
20 #include <assert.h>
21 #include <stdint.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include "libyang.h"
27 
28 /* additional internal headers for some useful simple macros */
29 #include "common.h"
30 #include "compat.h"
31 #include "plugins_internal.h" /* LY_TYPE_*_STR */
32 
52 static LY_ERR
53 identityref_ident2str(const struct lysc_ident *ident, LY_VALUE_FORMAT format, void *prefix_data, char **str, size_t *str_len)
54 {
55  int len;
56 
57  len = asprintf(str, "%s:%s", lyplg_type_get_prefix(ident->module, format, prefix_data), ident->name);
58  if (len == -1) {
59  return LY_EMEM;
60  }
61 
62  if (str_len) {
63  *str_len = (size_t)len;
64  }
65  return LY_SUCCESS;
66 }
67 
81 static LY_ERR
82 identityref_str2ident(const char *value, size_t value_len, LY_VALUE_FORMAT format, void *prefix_data,
83  const struct ly_ctx *ctx, const struct lysc_node *ctx_node, struct lysc_ident **ident, struct ly_err_item **err)
84 {
85  const char *id_name, *prefix = value;
86  size_t id_len, prefix_len;
87  const struct lys_module *mod;
89  struct lysc_ident *id, *identities;
90 
91  /* locate prefix if any */
92  for (prefix_len = 0; (prefix_len < value_len) && (value[prefix_len] != ':'); ++prefix_len) {}
93  if (prefix_len < value_len) {
94  id_name = &value[prefix_len + 1];
95  id_len = value_len - (prefix_len + 1);
96  } else {
97  prefix_len = 0;
98  id_name = value;
99  id_len = value_len;
100  }
101 
102  if (!id_len) {
103  return ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid empty identityref value.");
104  }
105 
106  mod = lyplg_type_identity_module(ctx, ctx_node, prefix, prefix_len, format, prefix_data);
107  if (!mod) {
108  return ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL,
109  "Invalid identityref \"%.*s\" value - unable to map prefix to YANG schema.", (int)value_len, value);
110  }
111 
112  id = NULL;
113  identities = mod->identities;
114  LY_ARRAY_FOR(identities, u) {
115  if (!ly_strncmp(identities[u].name, id_name, id_len)) {
116  /* we have match */
117  id = &identities[u];
118  break;
119  }
120  }
121  if (!id) {
122  /* no match */
123  return ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL,
124  "Invalid identityref \"%.*s\" value - identity not found in module \"%s\".",
125  (int)value_len, value, mod->name);
126  }
127 
128  *ident = id;
129  return LY_SUCCESS;
130 }
131 
141 static LY_ERR
142 identityref_check_base(const struct lysc_ident *ident, struct lysc_type_identityref *type, const char *value,
143  size_t value_len, struct ly_err_item **err)
144 {
145  LY_ERR ret;
146  size_t str_len;
147  char *str;
149  struct lysc_ident *base;
150 
151  /* check that the identity matches some of the type's base identities */
152  LY_ARRAY_FOR(type->bases, u) {
153  if (!lyplg_type_identity_isderived(type->bases[u], ident)) {
154  /* we have match */
155  break;
156  }
157  }
158 
159  /* it does not, generate a nice error */
160  if (u == LY_ARRAY_COUNT(type->bases)) {
161  str = NULL;
162  str_len = 1;
163  LY_ARRAY_FOR(type->bases, u) {
164  base = type->bases[u];
165  str_len += (u ? 2 : 0) + 1 + strlen(base->module->name) + 1 + strlen(base->name) + 1;
166  str = ly_realloc(str, str_len);
167  sprintf(str + (u ? strlen(str) : 0), "%s\"%s:%s\"", u ? ", " : "", base->module->name, base->name);
168  }
169 
170  /* no match */
171  if (u == 1) {
172  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL,
173  "Invalid identityref \"%.*s\" value - identity not derived from the base %s.",
174  (int)value_len, value, str);
175  } else {
176  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL,
177  "Invalid identityref \"%.*s\" value - identity not derived from all the bases %s.",
178  (int)value_len, value, str);
179  }
180  free(str);
181  return ret;
182  }
183 
184  return LY_SUCCESS;
185 }
186 
187 API LY_ERR
188 lyplg_type_store_identityref(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
189  uint32_t options, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node,
190  struct lyd_value *storage, struct lys_glob_unres *unres, struct ly_err_item **err)
191 {
192  LY_ERR ret = LY_SUCCESS;
193  struct lysc_type_identityref *type_ident = (struct lysc_type_identityref *)type;
194  char *canon;
195  struct lysc_ident *ident;
196 
197  /* init storage */
198  memset(storage, 0, sizeof *storage);
199  storage->realtype = type;
200 
201  /* check hints */
202  ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
203  LY_CHECK_GOTO(ret, cleanup);
204 
205  /* find a matching identity */
206  ret = identityref_str2ident(value, value_len, format, prefix_data, ctx, ctx_node, &ident, err);
207  LY_CHECK_GOTO(ret, cleanup);
208 
209  /* handle identity in a non-implemented module */
210  if (!ident->module->implemented) {
211  if (options & LYPLG_TYPE_STORE_IMPLEMENT) {
212  ret = lyplg_type_make_implemented(ident->module, NULL, unres);
213  LY_CHECK_GOTO(ret, cleanup);
214  } else {
215  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL,
216  "Invalid identityref \"%.*s\" value - identity found in non-implemented module \"%s\".",
217  (int)value_len, (char *)value, ident->module->name);
218  goto cleanup;
219  }
220  }
221 
222  /* check that the identity is derived form all the bases */
223  ret = identityref_check_base(ident, type_ident, value, value_len, err);
224  LY_CHECK_GOTO(ret, cleanup);
225 
226  /* store value */
227  storage->ident = ident;
228 
229  /* store canonical value */
230  if (format == LY_VALUE_CANON) {
231  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
232  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
233  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
234  LY_CHECK_GOTO(ret, cleanup);
235  } else {
236  ret = lydict_insert(ctx, value, value_len, &storage->_canonical);
237  LY_CHECK_GOTO(ret, cleanup);
238  }
239  } else {
240  /* JSON format with prefix is the canonical one */
241  ret = identityref_ident2str(ident, LY_VALUE_JSON, NULL, &canon, NULL);
242  LY_CHECK_GOTO(ret, cleanup);
243 
244  ret = lydict_insert_zc(ctx, canon, &storage->_canonical);
245  LY_CHECK_GOTO(ret, cleanup);
246  }
247 
248 cleanup:
249  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
250  free((void *)value);
251  }
252 
253  if (ret) {
254  lyplg_type_free_simple(ctx, storage);
255  }
256  return ret;
257 }
258 
259 API LY_ERR
260 lyplg_type_compare_identityref(const struct lyd_value *val1, const struct lyd_value *val2)
261 {
262  if (val1->realtype != val2->realtype) {
263  return LY_ENOT;
264  }
265 
266  if (val1->ident == val2->ident) {
267  return LY_SUCCESS;
268  }
269  return LY_ENOT;
270 }
271 
272 API const void *
273 lyplg_type_print_identityref(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *value, LY_VALUE_FORMAT format,
274  void *prefix_data, ly_bool *dynamic, size_t *value_len)
275 {
276  char *ret;
277 
278  if ((format == LY_VALUE_CANON) || (format == LY_VALUE_JSON) || (format == LY_VALUE_LYB)) {
279  if (dynamic) {
280  *dynamic = 0;
281  }
282  if (value_len) {
283  *value_len = strlen(value->_canonical);
284  }
285  return value->_canonical;
286  }
287 
288  /* print the value in the specific format */
289  if (identityref_ident2str(value->ident, format, prefix_data, &ret, value_len)) {
290  return NULL;
291  }
292  *dynamic = 1;
293  return ret;
294 }
295 
303 const struct lyplg_type_record plugins_identityref[] = {
304  {
305  .module = "",
306  .revision = NULL,
307  .name = LY_TYPE_IDENT_STR,
308 
309  .plugin.id = "libyang 2 - identityref, version 1",
310  .plugin.store = lyplg_type_store_identityref,
311  .plugin.validate = NULL,
312  .plugin.compare = lyplg_type_compare_identityref,
313  .plugin.sort = NULL,
314  .plugin.print = lyplg_type_print_identityref,
315  .plugin.duplicate = lyplg_type_dup_simple,
316  .plugin.free = lyplg_type_free_simple
317  },
318  {0}
319 };
libyang context handler.
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_EMEM
Definition: log.h:244
@ LY_ENOT
Definition: log.h:256
@ LY_EVALID
Definition: log.h:250
@ LY_SUCCESS
Definition: log.h:243
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.
const struct lys_module * lyplg_type_identity_module(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *prefix, size_t prefix_len, LY_VALUE_FORMAT format, const void *prefix_data)
Get the corresponding module for the identity value.
LY_ERR lyplg_type_make_implemented(struct lys_module *mod, const char **features, struct lys_glob_unres *unres)
Implement a module (just like lys_set_implemented()), but keep maintaining unresolved items.
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.
LY_ERR lyplg_type_identity_isderived(const struct lysc_ident *base, const struct lysc_ident *derived)
Decide if the derived identity is derived from (based on) the base identity.
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_compare_identityref(const struct lyd_value *val1, const struct lyd_value *val2)
Implementation of lyplg_type_compare_clb for the built-in identityref type.
Definition: identityref.c:260
API LY_ERR lyplg_type_store_identityref(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 identityref type.
Definition: identityref.c:188
LY_ERR lyplg_type_dup_simple(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
Implementation of lyplg_type_dup_clb for a generic simple type.
void lyplg_type_free_simple(const struct ly_ctx *ctx, struct lyd_value *value)
Implementation of lyplg_type_free_clb for a generic simple type.
#define LYPLG_TYPE_STORE_DYNAMIC
#define LYPLG_TYPE_STORE_IMPLEMENT
const char * name
Definition: tree_schema.h:2297
LY_DATA_TYPE basetype
Definition: tree_schema.h:1531
const char * name
Definition: tree_schema.h:1459
struct lysc_ident * identities
Definition: tree_schema.h:2311
struct lys_module * module
Definition: tree_schema.h:1462
ly_bool implemented
Definition: tree_schema.h:2321
struct lysc_ident ** bases
Definition: tree_schema.h:1608
Available YANG schema tree structures representing YANG module.
Definition: tree_schema.h:2295
YANG identity-stmt.
Definition: tree_schema.h:1458
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_CANON
Definition: tree.h:236
@ LY_VALUE_LYB
Definition: tree.h:241
API const void * lyplg_type_print_identityref(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: identityref.c:273
const struct lyplg_type_record plugins_identityref[]
Plugin information for identityref type implementation.
Definition: identityref.c:303
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
YANG data representation.
Definition: tree_data.h:531
void * ly_realloc(void *ptr, size_t size)
Wrapper for realloc() call. The only difference is that if it fails to allocate the requested memory,...