forked from CESNET/libyang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leafref.c
135 lines (114 loc) · 4 KB
/
leafref.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* @file leafref.c
* @author Radek Krejci <rkrejci@cesnet.cz>
* @brief Built-in leafref type plugin.
*
* Copyright (c) 2019-2021 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*/
#define _GNU_SOURCE
#include "plugins_types.h"
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "libyang.h"
/* additional internal headers for some useful simple macros */
#include "common.h"
#include "compat.h"
#include "plugins_internal.h" /* LY_TYPE_*_STR */
const struct lyplg_type_record plugins_leafref[];
API LY_ERR
lyplg_type_store_leafref(const struct ly_ctx *ctx, const struct lysc_type *type, const char *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)
{
LY_ERR ret = LY_SUCCESS;
struct lysc_type_leafref *type_lr = (struct lysc_type_leafref *)type;
assert(type_lr->realtype);
/* store the value as the real type of the leafref target */
ret = type_lr->realtype->plugin->store(ctx, type_lr->realtype, value, value_len, options, format, prefix_data,
hints, ctx_node, storage, unres, err);
if (ret == LY_EINCOMPLETE) {
/* it is irrelevant whether the target type needs some resolving */
ret = LY_SUCCESS;
}
LY_CHECK_RET(ret);
if (type_lr->require_instance) {
/* needs to be resolved */
return LY_EINCOMPLETE;
} else {
return LY_SUCCESS;
}
}
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)
{
LY_ERR ret = LY_SUCCESS;
struct lysc_type_leafref *type_lr = (struct lysc_type_leafref *)type;
char *errmsg = NULL;
*err = NULL;
if (!type_lr->require_instance) {
/* redundant to resolve */
return LY_SUCCESS;
}
/* check leafref target existence */
if (lyplg_type_resolve_leafref(type_lr, ctx_node, storage, tree, NULL, &errmsg)) {
ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, errmsg);
if (errmsg != NULL) {
free(errmsg);
}
}
return ret;
}
API LY_ERR
lyplg_type_compare_leafref(const struct lyd_value *val1, const struct lyd_value *val2)
{
return val1->realtype->plugin->compare(val1, val2);
}
API const char *
lyplg_type_print_leafref(const struct lyd_value *value, LY_VALUE_FORMAT format, void *prefix_data, ly_bool *dynamic)
{
return value->realtype->plugin->print(value, format, prefix_data, dynamic);
}
API LY_ERR
lyplg_type_dup_leafref(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
{
return original->realtype->plugin->duplicate(ctx, original, dup);
}
API void
lyplg_type_free_leafref(const struct ly_ctx *ctx, struct lyd_value *value)
{
if (value->realtype->plugin != &plugins_leafref[0].plugin) {
/* leafref's realtype is again leafref only in case of incomplete store */
value->realtype->plugin->free(ctx, value);
}
}
/**
* @brief Plugin information for leafref type implementation.
*
* Note that external plugins are supposed to use:
*
* LYPLG_TYPES = {
*/
const struct lyplg_type_record plugins_leafref[] = {
{
.module = "",
.revision = NULL,
.name = LY_TYPE_LEAFREF_STR,
.plugin.id = "libyang 2 - leafref, version 1",
.plugin.store = lyplg_type_store_leafref,
.plugin.validate = lyplg_type_validate_leafref,
.plugin.compare = lyplg_type_compare_leafref,
.plugin.print = lyplg_type_print_leafref,
.plugin.duplicate = lyplg_type_dup_leafref,
.plugin.free = lyplg_type_free_leafref
},
{0}
};