libyang  3.7.5
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 rc = 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  rc = 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 (rc == LY_EINCOMPLETE) {
53  /* it is irrelevant whether the target type needs some resolving */
54  rc = LY_SUCCESS;
55  }
56  LY_CHECK_RET(rc);
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 rc = LY_SUCCESS;
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  rc = lyplg_type_resolve_leafref(type_lr, ctx_node, storage, tree,
84  (ly_ctx_get_options(ctx) & LY_CTX_LEAFREF_LINKING) ? &targets : NULL, &errmsg);
85  if (rc) {
86  path = lyd_path(ctx_node, LYD_PATH_STD, NULL, 0);
87  rc = ly_err_new(err, LY_EVALID, LYVE_DATA, path, strdup("instance-required"), "%s", errmsg);
88  free(errmsg);
89  goto cleanup;
90  }
91 
92  if (ly_ctx_get_options(ctx) & LY_CTX_LEAFREF_LINKING) {
93  for (i = 0; i < targets->count; ++i) {
94  rc = lyd_link_leafref_node((struct lyd_node_term *)targets->dnodes[i], (struct lyd_node_term *)ctx_node);
95  LY_CHECK_GOTO(rc, cleanup);
96  }
97  }
98 
99 cleanup:
100  ly_set_free(targets, NULL);
101  return rc;
102 }
103 
104 LIBYANG_API_DEF LY_ERR
105 lyplg_type_compare_leafref(const struct ly_ctx *ctx, const struct lyd_value *val1, const struct lyd_value *val2)
106 {
107  return val1->realtype->plugin->compare(ctx, val1, val2);
108 }
109 
110 LIBYANG_API_DEF int
111 lyplg_type_sort_leafref(const struct ly_ctx *ctx, const struct lyd_value *val1, const struct lyd_value *val2)
112 {
113  return val1->realtype->plugin->sort(ctx, val1, val2);
114 }
115 
116 LIBYANG_API_DEF const void *
117 lyplg_type_print_leafref(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
118  void *prefix_data, ly_bool *dynamic, size_t *value_len)
119 {
120  return value->realtype->plugin->print(ctx, value, format, prefix_data, dynamic, value_len);
121 }
122 
123 LIBYANG_API_DEF LY_ERR
124 lyplg_type_dup_leafref(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
125 {
126  return original->realtype->plugin->duplicate(ctx, original, dup);
127 }
128 
129 LIBYANG_API_DEF void
130 lyplg_type_free_leafref(const struct ly_ctx *ctx, struct lyd_value *value)
131 {
132  value->realtype->plugin->free(ctx, value);
133 }
134 
143  {
144  .module = "",
145  .revision = NULL,
146  .name = LY_TYPE_LEAFREF_STR,
147 
148  .plugin.id = "libyang 2 - leafref, version 1",
149  .plugin.store = lyplg_type_store_leafref,
150  .plugin.validate = lyplg_type_validate_leafref,
151  .plugin.compare = lyplg_type_compare_leafref,
152  .plugin.sort = lyplg_type_sort_leafref,
153  .plugin.print = lyplg_type_print_leafref,
154  .plugin.duplicate = lyplg_type_dup_leafref,
155  .plugin.free = lyplg_type_free_leafref,
156  .plugin.lyb_data_len = -1,
157  },
158  {0}
159 };
struct lysc_type * realtype
Definition: tree_data.h:579
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:803
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:855
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:142
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:111
YANG data representation.
Definition: tree_data.h:575
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:117
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:105
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:130
#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:124
assert(!value->_canonical)