blob: 8606373c83b0890ee43e9a4082d25cbd0dc47411 [file] [log] [blame]
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001/**
2 * @file schema_compile_node.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Header for schema compilation of common nodes.
5 *
6 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#ifndef LY_SCHEMA_COMPILE_NODE_H_
16#define LY_SCHEMA_COMPILE_NODE_H_
17
18#include <stddef.h>
19
20#include "log.h"
21#include "tree_data.h"
22#include "tree_schema.h"
23
24struct ly_set;
25struct lysc_ctx;
26
27/**
28 * @brief Get the XPath context node for the given schema node.
29 * @param[in] start The schema node where the XPath expression appears.
30 * @return The context node to evaluate XPath expression in given schema node.
31 * @return NULL in case the context node is the root node.
32 */
33struct lysc_node *lysc_xpath_context(struct lysc_node *start);
34
35/**
36 * @brief Compile information from the when statement by either standard compilation or by reusing
37 * another compiled when structure.
38 *
39 * @param[in] ctx Compile context.
40 * @param[in] when_p Parsed when structure.
41 * @param[in] flags Flags of the parsed node with the when statement.
42 * @param[in] ctx_node Context node for the when statement.
43 * @param[in] node Compiled node to which add the compiled when.
44 * @param[in,out] when_c Optional, pointer to the previously compiled @p when_p to be reused. Set to NULL
45 * for the first call.
46 * @return LY_ERR value.
47 */
48LY_ERR lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, const struct lysc_node *ctx_node,
49 struct lysc_node *node, struct lysc_when **when_c);
50
51/**
52 * @brief Checks pattern syntax.
53 *
54 * @param[in] ctx Context.
55 * @param[in] log_path Path for logging errors.
56 * @param[in] pattern Pattern to check.
57 * @param[in,out] pcre2_code Compiled PCRE2 pattern. If NULL, the compiled information used to validate pattern are freed.
58 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
59 */
60LY_ERR lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *log_path, const char *pattern, pcre2_code **code);
61
62/**
63 * @brief Compile information about the leaf/leaf-list's type.
64 *
65 * @param[in] ctx Compile context.
66 * @param[in] context_pnode Schema node where the type/typedef is placed to correctly find the base types.
67 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
68 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
69 * @param[in] context_name Name of the context node or referencing typedef for logging.
70 * @param[in] type_p Parsed type to compile.
71 * @param[out] type Newly created (or reused with increased refcount) type structure with the filled information about the type.
72 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
73 * @param[out] dflt Default value for the type.
74 * @return LY_ERR value.
75 */
76LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags,
77 struct lysp_module *context_mod, const char *context_name, struct lysp_type *type_p,
78 struct lysc_type **type, const char **units, struct lysp_qname **dflt);
79
80/**
81 * @brief Compile parsed RPC/action schema node information.
82 *
83 * @param[in] ctx Compile context
84 * @param[in] action_p Parsed RPC/action schema node.
85 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
86 * @param[in,out] action Prepared (empty) compiled action structure to fill.
87 * @param[in] uses_status If the RPC/action is being placed instead of uses, here we have the uses's status value (as node's flags).
88 * Zero means no uses, non-zero value with no status bit set mean the default status.
89 * @return LY_SUCCESS on success,
90 * @return LY_EVALID on validation error,
91 * @return LY_EDENIED on not-supported deviation.
92 */
93LY_ERR lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p, struct lysc_node *parent,
94 struct lysc_action *action, uint16_t uses_status);
95
96/**
97 * @brief Compile parsed Notification schema node information.
98 *
99 * @param[in] ctx Compile context
100 * @param[in] notif_p Parsed Notification schema node.
101 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
102 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
103 * @param[in] uses_status If the Notification is being placed instead of uses, here we have the uses's status value (as node's flags).
104 * Zero means no uses, non-zero value with no status bit set mean the default status.
105 * @return LY_SUCCESS on success,
106 * @return LY_EVALID on validation error,
107 * @return LY_EDENIED on not-supported deviation.
108 */
109LY_ERR lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p, struct lysc_node *parent,
110 struct lysc_notif *notif, uint16_t uses_status);
111
112/**
113 * @brief Find the node according to the given descendant/absolute schema nodeid.
114 * Used in unique, refine and augment statements.
115 *
116 * @param[in] ctx Compile context
117 * @param[in] nodeid Descendant-schema-nodeid (according to the YANG grammar)
118 * @param[in] nodeid_len Length of the given nodeid, if it is not NULL-terminated string.
119 * @param[in] ctx_node Context node for a relative nodeid.
120 * @param[in] cur_mod Current module for the nodeid (where it was "instantiated").
121 * @param[in] format Format of any prefixes.
122 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
123 * @param[in] nodetype Optional (can be 0) restriction for target's nodetype. If target exists, but does not match
124 * the given nodetype, LY_EDENIED is returned (and target is provided), but no error message is printed.
125 * The value can be even an ORed value to allow multiple nodetypes.
126 * @param[out] target Found target node if any.
127 * @param[out] result_flag Output parameter to announce if the schema nodeid goes through the action's input/output or a Notification.
128 * The LYSC_OPT_RPC_INPUT, LYSC_OPT_RPC_OUTPUT and LYSC_OPT_NOTIFICATION are used as flags.
129 * @return LY_ERR values - LY_ENOTFOUND, LY_EVALID, LY_EDENIED or LY_SUCCESS.
130 */
131LY_ERR lysc_resolve_schema_nodeid(struct lysc_ctx *ctx, const char *nodeid, size_t nodeid_len,
132 const struct lysc_node *ctx_node, const struct lys_module *cur_mod, LY_PREFIX_FORMAT format, void *prefix_data,
133 uint16_t nodetype, const struct lysc_node **target, uint16_t *result_flag);
134
135/**
136 * @brief Compile choice children.
137 *
138 * @param[in] ctx Compile context
139 * @param[in] child_p Parsed choice children nodes.
140 * @param[in] node Compiled choice node to compile and add children to.
141 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
142 */
143LY_ERR lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node,
144 struct ly_set *child_set);
145
146/**
147 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
148 *
149 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
150 * the flag to such parents from a mandatory children.
151 *
152 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
153 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
154 * (mandatory children was removed).
155 */
156void lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add);
157
158/**
159 * @brief Validate grouping that was defined but not used in the schema itself.
160 *
161 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
162 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
163 *
164 * @param[in] ctx Compile context.
165 * @param[in] pnode Parsed parent node of the grouping, NULL for top-level.
166 * @param[in] grp Parsed grouping node to check.
167 * @return LY_ERR value.
168 */
169LY_ERR lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysp_grp *grp);
170
171/**
172 * @brief Compile parsed schema node information.
173 *
174 * @param[in] ctx Compile context
175 * @param[in] pnode Parsed schema node.
176 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
177 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
178 * the compile context.
179 * @param[in] uses_status If the node is being placed instead of uses, here we have the uses's status value (as node's flags).
180 * Zero means no uses, non-zero value with no status bit set mean the default status.
181 * @param[in,out] child_set Optional set to add all the compiled nodes into (can be more in case of uses).
182 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
183 */
184LY_ERR lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
185 struct ly_set *child_set);
186
187#endif /* LY_SCHEMA_COMPILE_NODE_H_ */