libyang  2.0.194
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 
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 "common.h"
26 #include "compat.h"
27 #include "plugins_internal.h" /* LY_TYPE_*_STR */
28 
38 LIBYANG_API_DEF 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 LIBYANG_API_DEF 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, *path;
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  path = lyd_path(ctx_node, LYD_PATH_STD, NULL, 0);
83  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, path, strdup("instance-required"), "%s", errmsg);
84  free(errmsg);
85  return ret;
86  }
87 
88  return LY_SUCCESS;
89 }
90 
91 LIBYANG_API_DEF LY_ERR
92 lyplg_type_compare_leafref(const struct lyd_value *val1, const struct lyd_value *val2)
93 {
94  return val1->realtype->plugin->compare(val1, val2);
95 }
96 
97 LIBYANG_API_DEF const void *
98 lyplg_type_print_leafref(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
99  void *prefix_data, ly_bool *dynamic, size_t *value_len)
100 {
101  return value->realtype->plugin->print(ctx, value, format, prefix_data, dynamic, value_len);
102 }
103 
104 LIBYANG_API_DEF LY_ERR
105 lyplg_type_dup_leafref(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
106 {
107  return original->realtype->plugin->duplicate(ctx, original, dup);
108 }
109 
110 LIBYANG_API_DEF void
111 lyplg_type_free_leafref(const struct ly_ctx *ctx, struct lyd_value *value)
112 {
113  value->realtype->plugin->free(ctx, value);
114 }
115 
123 const struct lyplg_type_record plugins_leafref[] = {
124  {
125  .module = "",
126  .revision = NULL,
127  .name = LY_TYPE_LEAFREF_STR,
128 
129  .plugin.id = "libyang 2 - leafref, version 1",
130  .plugin.store = lyplg_type_store_leafref,
131  .plugin.validate = lyplg_type_validate_leafref,
132  .plugin.compare = lyplg_type_compare_leafref,
133  .plugin.sort = NULL,
134  .plugin.print = lyplg_type_print_leafref,
135  .plugin.duplicate = lyplg_type_dup_leafref,
136  .plugin.free = lyplg_type_free_leafref,
137  .plugin.lyb_data_len = -1,
138  },
139  {0}
140 };
libyang context handler.
LY_ERR
libyang's error codes returned by the libyang functions.
Definition: log.h:244
@ LYVE_DATA
Definition: log.h:281
@ LY_EVALID
Definition: log.h:252
@ LY_SUCCESS
Definition: log.h:245
@ LY_EINCOMPLETE
Definition: log.h:254
Libyang full error structure.
Definition: log.h:289
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
LIBYANG_API_DECL LY_ERR ly_err_new(struct ly_err_item **err, LY_ERR ecode, LY_VECODE vecode, char *path, char *apptag, const char *err_format,...) _FORMAT_PRINTF(6
Create and fill error structure.
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 lyd_node **target, char **errmsg)
Find leafref target in data.
LIBYANG_API_DEF 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
LIBYANG_API_DEF 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:98
LIBYANG_API_DEF 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:92
LIBYANG_API_DEF 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:105
LIBYANG_API_DEF 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:111
struct lyxp_expr * path
Definition: tree_schema.h:1602
struct lyplg_type * plugin
Definition: tree_schema.h:1535
struct lysc_type * realtype
Definition: tree_schema.h:1605
uint8_t require_instance
Definition: tree_schema.h:1606
Compiled YANG data node.
Definition: tree_schema.h:1650
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: tree.h:235
const struct lyplg_type_record plugins_leafref[]
Plugin information for leafref type implementation.
Definition: leafref.c:123
LIBYANG_API_DEF 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
The main libyang public header.
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:27
API for (user) types plugins.
@ LYD_PATH_STD
Definition: tree_data.h:2315
const struct lysc_type * realtype
Definition: tree_data.h:564
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.
Generic structure for a data node.
Definition: tree_data.h:798
YANG data representation.
Definition: tree_data.h:560