libyang  2.0.7
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
leafref.c
Go to the documentation of this file.
1 
15 #include "plugins_types.h"
16 
17 #include <assert.h>
18 #include <stdint.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 
22 #include "libyang.h"
23 
24 /* additional internal headers for some useful simple macros */
25 #include "common.h"
26 #include "compat.h"
27 #include "plugins_internal.h" /* LY_TYPE_*_STR */
28 
38 API LY_ERR
39 lyplg_type_store_leafref(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
40  uint32_t options, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node,
41  struct lyd_value *storage, struct lys_glob_unres *unres, struct ly_err_item **err)
42 {
43  LY_ERR ret = LY_SUCCESS;
44  struct lysc_type_leafref *type_lr = (struct lysc_type_leafref *)type;
45 
46  assert(type_lr->realtype);
47 
48  /* store the value as the real type of the leafref target */
49  ret = type_lr->realtype->plugin->store(ctx, type_lr->realtype, value, value_len, options, format, prefix_data,
50  hints, ctx_node, storage, unres, err);
51  if (ret == LY_EINCOMPLETE) {
52  /* it is irrelevant whether the target type needs some resolving */
53  ret = LY_SUCCESS;
54  }
55  LY_CHECK_RET(ret);
56 
57  if (type_lr->require_instance) {
58  /* needs to be resolved */
59  return LY_EINCOMPLETE;
60  } else {
61  return LY_SUCCESS;
62  }
63 }
64 
65 API LY_ERR
66 lyplg_type_validate_leafref(const struct ly_ctx *UNUSED(ctx), const struct lysc_type *type, const struct lyd_node *ctx_node,
67  const struct lyd_node *tree, struct lyd_value *storage, struct ly_err_item **err)
68 {
69  LY_ERR ret;
70  struct lysc_type_leafref *type_lr = (struct lysc_type_leafref *)type;
71  char *errmsg = NULL;
72 
73  *err = NULL;
74 
75  if (!type_lr->require_instance) {
76  /* redundant to resolve */
77  return LY_SUCCESS;
78  }
79 
80  /* check leafref target existence */
81  if (lyplg_type_resolve_leafref(type_lr, ctx_node, storage, tree, NULL, &errmsg)) {
82  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, errmsg);
83  free(errmsg);
84  return ret;
85  }
86 
87  return LY_SUCCESS;
88 }
89 
90 API LY_ERR
91 lyplg_type_compare_leafref(const struct lyd_value *val1, const struct lyd_value *val2)
92 {
93  return val1->realtype->plugin->compare(val1, val2);
94 }
95 
96 API const void *
97 lyplg_type_print_leafref(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
98  void *prefix_data, ly_bool *dynamic, size_t *value_len)
99 {
100  return value->realtype->plugin->print(ctx, value, format, prefix_data, dynamic, value_len);
101 }
102 
103 API LY_ERR
104 lyplg_type_dup_leafref(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
105 {
106  return original->realtype->plugin->duplicate(ctx, original, dup);
107 }
108 
109 API void
110 lyplg_type_free_leafref(const struct ly_ctx *ctx, struct lyd_value *value)
111 {
112  value->realtype->plugin->free(ctx, value);
113 }
114 
122 const struct lyplg_type_record plugins_leafref[] = {
123  {
124  .module = "",
125  .revision = NULL,
126  .name = LY_TYPE_LEAFREF_STR,
127 
128  .plugin.id = "libyang 2 - leafref, version 1",
129  .plugin.store = lyplg_type_store_leafref,
130  .plugin.validate = lyplg_type_validate_leafref,
131  .plugin.compare = lyplg_type_compare_leafref,
132  .plugin.sort = NULL,
133  .plugin.print = lyplg_type_print_leafref,
134  .plugin.duplicate = lyplg_type_dup_leafref,
135  .plugin.free = lyplg_type_free_leafref
136  },
137  {0}
138 };
libyang context handler.
LY_ERR
libyang's error codes returned by the libyang functions.
Definition: log.h:242
@ LYVE_DATA
Definition: log.h:279
@ LY_EVALID
Definition: log.h:250
@ LY_SUCCESS
Definition: log.h:243
@ LY_EINCOMPLETE
Definition: log.h:252
Libyang full error structure.
Definition: log.h:287
const char * module
lyplg_type_print_clb print
lyplg_type_store_clb store
lyplg_type_compare_clb compare
lyplg_type_dup_clb duplicate
lyplg_type_free_clb free
LY_ERR lyplg_type_resolve_leafref(const struct lysc_type_leafref *lref, const struct lyd_node *node, struct lyd_value *value, const struct lyd_node *tree, struct lyd_node **target, char **errmsg)
Find leafref target in data.
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.
API void lyplg_type_free_leafref(const struct ly_ctx *ctx, struct lyd_value *value)
Implementation of lyplg_type_free_clb for the built-in leafref type.
Definition: leafref.c:110
API LY_ERR lyplg_type_store_leafref(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 leafref type.
Definition: leafref.c:39
API LY_ERR lyplg_type_compare_leafref(const struct lyd_value *val1, const struct lyd_value *val2)
Implementation of lyplg_type_compare_clb for the built-in leafref type.
Definition: leafref.c:91
API LY_ERR lyplg_type_dup_leafref(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
Implementation of lyplg_type_dup_clb for the built-in leafref type.
Definition: leafref.c:104
API const void * lyplg_type_print_leafref(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format, void *prefix_data, ly_bool *dynamic, size_t *value_len)
Implementation of lyplg_type_print_clb for the built-in leafref type.
Definition: leafref.c:97
struct lyplg_type * plugin
Definition: tree_schema.h:1530
struct lysc_type * realtype
Definition: tree_schema.h:1599
uint8_t require_instance
Definition: tree_schema.h:1600
Compiled YANG data node.
Definition: tree_schema.h:1644
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: tree.h:235
API LY_ERR lyplg_type_validate_leafref(const struct ly_ctx *UNUSED(ctx), const struct lysc_type *type, const struct lyd_node *ctx_node, const struct lyd_node *tree, struct lyd_value *storage, struct ly_err_item **err)
Definition: leafref.c:66
const struct lyplg_type_record plugins_leafref[]
Plugin information for leafref type implementation.
Definition: leafref.c:122
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
Generic structure for a data node.
Definition: tree_data.h:754
YANG data representation.
Definition: tree_data.h:531