libyang  2.2.8
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
leafref.c
Go to the documentation of this file.
1 
14 #define _GNU_SOURCE /* strdup */
15 
16 #include "plugins_types.h"
17 
18 #include <assert.h>
19 #include <stdint.h>
20 #include <stdlib.h>
21 
22 #include "libyang.h"
23 
24 /* additional internal headers for some useful simple macros */
25 #include "compat.h"
26 #include "ly_common.h"
27 #include "plugins_internal.h" /* LY_TYPE_*_STR */
28 #include "tree_data_internal.h" /* lyd_link_leafref_node */
29 
39 LIBYANG_API_DEF LY_ERR
40 lyplg_type_store_leafref(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
41  uint32_t options, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node,
42  struct lyd_value *storage, struct lys_glob_unres *unres, struct ly_err_item **err)
43 {
44  LY_ERR ret = LY_SUCCESS;
45  struct lysc_type_leafref *type_lr = (struct lysc_type_leafref *)type;
46 
47  assert(type_lr->realtype);
48 
49  /* store the value as the real type of the leafref target */
50  ret = type_lr->realtype->plugin->store(ctx, type_lr->realtype, value, value_len, options, format, prefix_data,
51  hints, ctx_node, storage, unres, err);
52  if (ret == LY_EINCOMPLETE) {
53  /* it is irrelevant whether the target type needs some resolving */
54  ret = LY_SUCCESS;
55  }
56  LY_CHECK_RET(ret);
57 
58  if (type_lr->require_instance) {
59  /* needs to be resolved */
60  return LY_EINCOMPLETE;
61  } else {
62  return LY_SUCCESS;
63  }
64 }
65 
66 LIBYANG_API_DEF LY_ERR
67 lyplg_type_validate_leafref(const struct ly_ctx *ctx, const struct lysc_type *type, const struct lyd_node *ctx_node,
68  const struct lyd_node *tree, struct lyd_value *storage, struct ly_err_item **err)
69 {
70  LY_ERR ret;
71  struct lysc_type_leafref *type_lr = (struct lysc_type_leafref *)type;
72  char *errmsg = NULL, *path;
73  struct ly_set *targets = NULL;
74  uint32_t i;
75 
76  *err = NULL;
77 
78  if (!type_lr->require_instance) {
79  /* redundant to resolve */
80  return LY_SUCCESS;
81  }
82 
83  ret = lyplg_type_resolve_leafref(type_lr, ctx_node, storage, tree, (ly_ctx_get_options(ctx) & LY_CTX_LEAFREF_LINKING) ? &targets : NULL, &errmsg);
84  if (ret != LY_SUCCESS) {
85  path = lyd_path(ctx_node, LYD_PATH_STD, NULL, 0);
86  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, path, strdup("instance-required"), "%s", errmsg);
87  free(errmsg);
88  goto cleanup;
89  }
90 
91  if (ly_ctx_get_options(ctx) & LY_CTX_LEAFREF_LINKING) {
92  for (i = 0; i < targets->count; ++i) {
93  ret = lyd_link_leafref_node((struct lyd_node_term *)targets->dnodes[i], (struct lyd_node_term *)ctx_node);
94  LY_CHECK_GOTO(ret, cleanup);
95  }
96  }
97 
98 cleanup:
99  ly_set_free(targets, NULL);
100  return ret;
101 }
102 
103 LIBYANG_API_DEF LY_ERR
104 lyplg_type_compare_leafref(const struct ly_ctx *ctx, const struct lyd_value *val1, const struct lyd_value *val2)
105 {
106  return val1->realtype->plugin->compare(ctx, val1, val2);
107 }
108 
109 LIBYANG_API_DEF int
110 lyplg_type_sort_leafref(const struct ly_ctx *ctx, const struct lyd_value *val1, const struct lyd_value *val2)
111 {
112  return val1->realtype->plugin->sort(ctx, val1, val2);
113 }
114 
115 LIBYANG_API_DEF const void *
116 lyplg_type_print_leafref(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
117  void *prefix_data, ly_bool *dynamic, size_t *value_len)
118 {
119  return value->realtype->plugin->print(ctx, value, format, prefix_data, dynamic, value_len);
120 }
121 
122 LIBYANG_API_DEF LY_ERR
123 lyplg_type_dup_leafref(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
124 {
125  return original->realtype->plugin->duplicate(ctx, original, dup);
126 }
127 
128 LIBYANG_API_DEF void
129 lyplg_type_free_leafref(const struct ly_ctx *ctx, struct lyd_value *value)
130 {
131  value->realtype->plugin->free(ctx, value);
132 }
133 
142  {
143  .module = "",
144  .revision = NULL,
145  .name = LY_TYPE_LEAFREF_STR,
146 
147  .plugin.id = "libyang 2 - leafref, version 1",
148  .plugin.store = lyplg_type_store_leafref,
149  .plugin.validate = lyplg_type_validate_leafref,
150  .plugin.compare = lyplg_type_compare_leafref,
151  .plugin.sort = lyplg_type_sort_leafref,
152  .plugin.print = lyplg_type_print_leafref,
153  .plugin.duplicate = lyplg_type_dup_leafref,
154  .plugin.free = lyplg_type_free_leafref,
155  .plugin.lyb_data_len = -1,
156  },
157  {0}
158 };
struct lysc_type * realtype
Definition: tree_data.h:575
Compiled YANG data node.
Definition: tree_schema.h:1439
struct lyplg_type * plugin
Definition: tree_schema.h:1304
Generic structure for a data node.
Definition: tree_data.h:799
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:28
lyplg_type_free_clb free
LIBYANG_API_DECL 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:40
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.
Data node structure for the terminal data tree nodes - leaves and leaf-lists.
Definition: tree_data.h:851
LIBYANG_API_DECL void ly_set_free(struct ly_set *set, void(*destructor)(void *obj))
Free the ly_set data. If the destructor is not provided, it frees only the set structure content...
struct lyplg_type_record plugins_leafref[]
Plugin information for leafref type implementation.
Definition: leafref.c:141
LIBYANG_API_DECL 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 ly_set **targets, char **errmsg)
Find leafref target in data.
The main libyang public header.
LIBYANG_API_DECL int lyplg_type_sort_leafref(const struct ly_ctx *ctx, const struct lyd_value *val1, const struct lyd_value *val2)
Implementation of lyplg_type_sort_clb for the built-in leafref type.
Definition: leafref.c:110
YANG data representation.
Definition: tree_data.h:571
lyplg_type_store_clb store
struct lyxp_expr * path
Definition: tree_schema.h:1384
uint32_t count
Definition: set.h:49
Libyang full error structure.
Definition: log.h:285
Definition: log.h:277
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 error structure.
LIBYANG_API_DECL 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:116
Structure to hold a set of (not necessary somehow connected) objects. Usually used for lyd_node...
Definition: set.h:47
LIBYANG_API_DECL LY_ERR lyplg_type_compare_leafref(const struct ly_ctx *ctx, 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:104
lyplg_type_dup_clb duplicate
const char * module
lyplg_type_sort_clb sort
uint8_t require_instance
Definition: tree_schema.h:1387
LIBYANG_API_DECL LY_ERR lyplg_type_validate_leafref(const struct ly_ctx *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)
Implementation of lyplg_type_validate_clb for the built-in leafref type.
Definition: leafref.c:67
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: tree.h:234
lyplg_type_print_clb print
Definition: log.h:248
LIBYANG_API_DECL uint16_t ly_ctx_get_options(const struct ly_ctx *ctx)
Get the currently set context&#39;s options.
LIBYANG_API_DECL 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:129
#define LY_CTX_LEAFREF_LINKING
Definition: context.h:231
lyplg_type_compare_clb compare
API for (user) types plugins.
LY_ERR
libyang&#39;s error codes returned by the libyang functions.
Definition: log.h:240
struct lysc_type * realtype
Definition: tree_schema.h:1386
libyang context handler.
LIBYANG_API_DECL 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:123
assert(!value->_canonical)