blob: a73fb0a21f2cee620fe8131137f626a8fce47cf6 [file] [log] [blame]
Radek Krejci70853c52018-10-15 14:46:16 +02001/**
2 * @file tree_schema_internal.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief internal functions for YANG schema trees.
5 *
6 * Copyright (c) 2015 - 2018 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_TREE_SCHEMA_INTERNAL_H_
16#define LY_TREE_SCHEMA_INTERNAL_H_
17
Radek Krejcid33273d2018-10-25 14:55:52 +020018#define LOGVAL_YANG(CTX, ...) LOGVAL((CTX)->ctx, LY_VLOG_LINE, &(CTX)->line, __VA_ARGS__)
19
Radek Krejci70853c52018-10-15 14:46:16 +020020/**
Radek Krejcie3846472018-10-15 15:24:51 +020021 * @brief List of YANG statement groups - the (sub)module's substatements
22 */
23enum yang_module_stmt {
24 Y_MOD_MODULE_HEADER,
25 Y_MOD_LINKAGE,
26 Y_MOD_META,
27 Y_MOD_REVISION,
28 Y_MOD_BODY
29};
30
31/**
32 * @brief Types of arguments of YANG statements
33 */
34enum yang_arg {
35 Y_IDENTIF_ARG, /**< YANG "identifier-arg-str" rule */
36 Y_PREF_IDENTIF_ARG, /**< YANG "identifier-ref-arg-str" rule */
37 Y_STR_ARG, /**< YANG "string" rule */
38 Y_MAYBE_STR_ARG /**< optional YANG "string" rule */
39};
40
41/**
Radek Krejci70853c52018-10-15 14:46:16 +020042 * @brief internal context for schema parsers
43 */
44struct ly_parser_ctx {
45 struct ly_ctx *ctx;
Radek Krejcifaa1eac2018-10-30 14:34:55 +010046 struct lysp_module *mod;
Radek Krejcibbe09a92018-11-08 09:36:54 +010047 struct ly_set tpdfs_nodes;
48 struct ly_set grps_nodes;
49 uint64_t line; /**< line number */
50 uint64_t indent; /**< current position on the line for YANG indentation */
Radek Krejci70853c52018-10-15 14:46:16 +020051};
52
53/**
54 * @brief Check the currently present prefixes in the module for collision with the new one.
55 *
Radek Krejcibbe09a92018-11-08 09:36:54 +010056 * @param[in] ctx Context for logging.
Radek Krejci70853c52018-10-15 14:46:16 +020057 * @param[in] module Schema tree to check.
58 * @param[in] value Newly added prefix value (including its location to distinguish collision with itself).
59 * @return LY_EEXIST when prefix is already used in the module, LY_SUCCESS otherwise
60 */
61LY_ERR lysp_check_prefix(struct ly_parser_ctx *ctx, struct lysp_module *module, const char **value);
62
Radek Krejci86d106e2018-10-18 09:53:19 +020063/**
64 * @brief Check date string (4DIGIT "-" 2DIGIT "-" 2DIGIT)
65 *
Radek Krejcibbe09a92018-11-08 09:36:54 +010066 * @param[in] ctx Optional context for logging.
Radek Krejci86d106e2018-10-18 09:53:19 +020067 * @param[in] date Date string to check (non-necessarily terminated by \0)
68 * @param[in] date_len Length of the date string, 10 expected.
69 * @param[in] stmt Statement name for error message.
70 * @return LY_ERR value.
71 */
Radek Krejcibbe09a92018-11-08 09:36:54 +010072LY_ERR lysp_check_date(struct ly_parser_ctx *ctx, const char *date, int date_len, const char *stmt);
73
74/**
75 * @brief Check names of typedefs in the parsed module to detect collisions.
76 *
77 * @param[in] ctx Parser context, module where the type is being defined is taken from here.
78 * @return LY_ERR value.
79 */
80LY_ERR lysp_check_typedefs(struct ly_parser_ctx *ctx);
Radek Krejci86d106e2018-10-18 09:53:19 +020081
82/**
83 * @brief Just move the newest revision into the first position, does not sort the rest
84 * @param[in] revs Sized-array of the revisions in a printable schema tree.
85 */
86void lysp_sort_revisions(struct lysp_revision *revs);
87
88/**
Radek Krejcibbe09a92018-11-08 09:36:54 +010089 * @brief Find type specified type definition
90 *
91 * @param[in] id Name of the type including possible prefix. Module where the prefix is being searched is start_module.
92 * @param[in] start_node Context node where the type is being instantiated to be able to search typedefs in parents.
93 * @param[in] start_module Module where the type is being instantiated for search for typedefs.
94 * @param[out] tpdf Found type definition.
95 * @param[out] node Node where the found typedef is defined, NULL in case of a top-level typedef.
96 * @param[out] module Module where the found typedef is being defined, NULL in case of built-in YANG types.
97 */
98LY_ERR lysp_type_find(const char *id, struct lysp_node *start_node, struct lysp_module *start_module,
99 const struct lysp_tpdf **tpdf, struct lysp_node **node, struct lysp_module **module);
100
101/**
Radek Krejci086c7132018-10-26 15:29:04 +0200102 * @brief Find and parse module of the given name.
103 *
104 * @param[in] ctx libyang context.
105 * @param[in] name Name of the module to load.
106 * @param[in] revison Optional revision of the module to load. If NULL, the newest revision is loaded.
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100107 * @param[in] implement Flag if the loaded module is supposed to be marked as implemented.
108 * @param[in] require_parsed Flag to require parsed module structure in case the module is already in the context,
109 * but only the compiled structure is available.
Radek Krejci086c7132018-10-26 15:29:04 +0200110 * @param[out] mod Parsed module structure.
111 * @return LY_ERR value.
112 */
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100113LY_ERR lysp_load_module(struct ly_ctx *ctx, const char *name, const char *revision, int implement, int require_parsed, struct lys_module **mod);
Radek Krejci086c7132018-10-26 15:29:04 +0200114
115/**
Radek Krejcid33273d2018-10-25 14:55:52 +0200116 * @brief Parse included submodule into the simply parsed YANG module.
117 *
Radek Krejci3b1f9292018-11-08 10:58:35 +0100118 * @param[in] ctx parser context
Radek Krejcid33273d2018-10-25 14:55:52 +0200119 * @param[in] mod Module including a submodule.
Radek Krejcid33273d2018-10-25 14:55:52 +0200120 * @param[in,out] inc Include structure holding all available information about the include statement, the parsed
121 * submodule is stored into this structure.
122 * @return LY_ERR value.
123 */
Radek Krejci3b1f9292018-11-08 10:58:35 +0100124LY_ERR lysp_load_submodule(struct ly_parser_ctx *ctx, struct lysp_module *mod, struct lysp_include *inc);
Radek Krejcid33273d2018-10-25 14:55:52 +0200125
126/**
Radek Krejcibbe09a92018-11-08 09:36:54 +0100127 * @brief Get address of a node's typedefs list if any.
128 *
129 * Decides the node's type and in case it has an typedefs list, returns its address.
130 * @param[in] node Node to check.
131 * @return Address of the node's tpdf member if any, NULL otherwise.
132 */
133struct lysp_tpdf **lysp_node_typedefs(struct lysp_node *node);
134
135/**
136 * @brief Get address of a node's actions list if any.
137 *
138 * Decides the node's type and in case it has an actions list, returns its address.
139 * @param[in] node Node to check.
140 * @return Address of the node's actions member if any, NULL otherwise.
141 */
142struct lysp_action **lysp_node_actions(struct lysp_node *node);
143
144/**
145 * @brief Get address of a node's notifications list if any.
146 *
147 * Decides the node's type and in case it has a notifications list, returns its address.
148 * @param[in] node Node to check.
149 * @return Address of the node's notifs member if any, NULL otherwise.
150 */
151struct lysp_notif **lysp_node_notifs(struct lysp_node *node);
152
153/**
154 * @brief Get address of a node's child pointer if any.
155 *
156 * Decides the node's type and in case it has a children list, returns its address.
157 * @param[in] node Node to check.
158 * @return Address of the node's child member if any, NULL otherwise.
159 */
160struct lysp_node **lysp_node_children(struct lysp_node *node);
161
162/**
163 * @brief Get address of a node's child pointer if any.
164 *
165 * Decides the node's type and in case it has a children list, returns its address.
166 * @param[in] node Node to check.
167 * @return Address of the node's child member if any, NULL otherwise.
168 */
169struct lysc_node **lysc_node_children(struct lysc_node *node);
170
171/**
Radek Krejcice8c1592018-10-29 15:35:51 +0100172 * @brief Find the module referenced by prefix in the provided parsed mod.
173 *
174 * @param[in] mod Schema module where the prefix was used.
175 * @param[in] prefix Prefix used to reference a module.
176 * @param[in] len Length of the prefix since it is not necessary NULL-terminated.
177 * @return Pointer to the module or NULL if the module is not found.
178 */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100179struct lysp_module *lysp_module_find_prefix(struct lysp_module *mod, const char *prefix, size_t len);
Radek Krejcice8c1592018-10-29 15:35:51 +0100180
181/**
182 * @brief Find the module referenced by prefix in the provided compiled mod.
183 *
184 * @param[in] mod Schema module where the prefix was used.
185 * @param[in] prefix Prefix used to reference a module.
186 * @param[in] len Length of the prefix since it is not necessary NULL-terminated.
187 * @return Pointer to the module or NULL if the module is not found.
188 */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100189struct lysc_module *lysc_module_find_prefix(struct lysc_module *mod, const char *prefix, size_t len);
Radek Krejcice8c1592018-10-29 15:35:51 +0100190
191/**
Radek Krejci151a5b72018-10-19 14:21:44 +0200192 * @brief Find the module referenced by prefix in the provided mod.
193 *
194 * @param[in] mod Schema module where the prefix was used.
195 * @param[in] prefix Prefix used to reference a module.
196 * @param[in] len Length of the prefix since it is not necessary NULL-terminated.
197 * @return Pointer to the module or NULL if the module is not found.
198 */
Radek Krejcice8c1592018-10-29 15:35:51 +0100199struct lys_module *lys_module_find_prefix(struct lys_module *mod, const char *prefix, size_t len);
Radek Krejci151a5b72018-10-19 14:21:44 +0200200
201/**
Radek Krejcid33273d2018-10-25 14:55:52 +0200202 * @brief Parse YANG module and submodule from a string.
203 *
204 * In contrast to public lys_parse_mem(), also submodules can be parsed here. However,
205 * while the modules are added into the context, submodules not. The latest_revision
206 * flag is updated in both cases.
207 *
208 * @param[in] ctx libyang context where to process the data model.
209 * @param[in] data The string containing the dumped data model in the specified
210 * format.
211 * @param[in] format Format of the input data (YANG or YIN).
Radek Krejcid33273d2018-10-25 14:55:52 +0200212 * @param[in] implement Flag if the schema is supposed to be marked as implemented.
Radek Krejci3b1f9292018-11-08 10:58:35 +0100213 * @param[in] main_ctx Parser context of the main module in case of parsing submodule.
Radek Krejci9ed7a192018-10-31 16:23:51 +0100214 * @param[in] custom_check Callback to check the parsed schema before it is accepted.
215 * @param[in] check_data Caller's data to pass to the custom_check callback.
Radek Krejcid33273d2018-10-25 14:55:52 +0200216 * @return Pointer to the data model structure or NULL on error.
217 */
Radek Krejci3b1f9292018-11-08 10:58:35 +0100218struct lys_module *lys_parse_mem_(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, int implement, struct ly_parser_ctx *main_ctx,
Radek Krejci9ed7a192018-10-31 16:23:51 +0100219 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, void *data), void *check_data);
Radek Krejcid33273d2018-10-25 14:55:52 +0200220
221/**
222 * @brief Parse YANG module and submodule from a file descriptor.
223 *
224 * In contrast to public lys_parse_mem(), also submodules can be parsed here. However,
225 * while the modules are added into the context, submodules not. The latest_revision
226 * flag is updated in both cases.
227 *
228 * \note Current implementation supports only reading data from standard (disk) file, not from sockets, pipes, etc.
229 *
230 * @param[in] ctx libyang context where to process the data model.
231 * @param[in] fd File descriptor of a regular file (e.g. sockets are not supported) containing the schema
232 * in the specified format.
233 * @param[in] format Format of the input data (YANG or YIN).
Radek Krejcid33273d2018-10-25 14:55:52 +0200234 * @param[in] implement Flag if the schema is supposed to be marked as implemented.
Radek Krejci3b1f9292018-11-08 10:58:35 +0100235 * @param[in] main_ctx Parser context of the main module in case of parsing submodule.
Radek Krejci9ed7a192018-10-31 16:23:51 +0100236 * @param[in] custom_check Callback to check the parsed schema before it is accepted.
237 * @param[in] check_data Caller's data to pass to the custom_check callback.
Radek Krejcid33273d2018-10-25 14:55:52 +0200238 * @return Pointer to the data model structure or NULL on error.
239 */
Radek Krejci3b1f9292018-11-08 10:58:35 +0100240struct lys_module *lys_parse_fd_(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, int implement, struct ly_parser_ctx *main_ctx,
Radek Krejci9ed7a192018-10-31 16:23:51 +0100241 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, void *data), void *check_data);
Radek Krejcid33273d2018-10-25 14:55:52 +0200242
243/**
244 * @brief Parse YANG module and submodule from a file descriptor.
245 *
246 * In contrast to public lys_parse_mem(), also submodules can be parsed here. However,
247 * while the modules are added into the context, submodules not. The latest_revision
248 * flag is updated in both cases.
249 *
250 * \note Current implementation supports only reading data from standard (disk) file, not from sockets, pipes, etc.
251 *
252 * @brief REad a schema into the specified context from a file.
253 *
254 * @param[in] ctx libyang context where to process the data model.
255 * @param[in] path Path to the file with the model in the specified format.
256 * @param[in] format Format of the input data (YANG or YIN).
Radek Krejcid33273d2018-10-25 14:55:52 +0200257 * @param[in] implement Flag if the schema is supposed to be marked as implemented.
Radek Krejci3b1f9292018-11-08 10:58:35 +0100258 * @param[in] main_ctx Parser context of the main module in case of parsing submodule.
Radek Krejci9ed7a192018-10-31 16:23:51 +0100259 * @param[in] custom_check Callback to check the parsed schema before it is accepted.
260 * @param[in] check_data Caller's data to pass to the custom_check callback.
Radek Krejcid33273d2018-10-25 14:55:52 +0200261 * @return Pointer to the data model structure or NULL on error.
262 */
Radek Krejci3b1f9292018-11-08 10:58:35 +0100263struct lys_module *lys_parse_path_(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, int implement, struct ly_parser_ctx *main_ctx,
Radek Krejci9ed7a192018-10-31 16:23:51 +0100264 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, void *data), void *check_data);
Radek Krejcid33273d2018-10-25 14:55:52 +0200265
266/**
267 * @brief Load the (sub)module into the context.
268 *
269 * This function does not check the presence of the (sub)module in context, it should be done before calling this function.
270 *
271 * module_name and submodule_name are alternatives - only one of the
272 *
273 * @param[in] ctx libyang context where to work.
274 * @param[in] name Name of the (sub)module to load.
275 * @param[in] revision Optional revision of the (sub)module to load, if NULL the newest revision is being loaded.
276 * @param[in] implement Flag if the (sub)module is supposed to be marked as implemented.
Radek Krejci3b1f9292018-11-08 10:58:35 +0100277 * @param[in] main_ctx Parser context of the main module in case of loading submodule.
Radek Krejcid33273d2018-10-25 14:55:52 +0200278 * @param[out] result Parsed YANG schema tree of the requested module. If it is a module, it is already in the context!
279 * @return LY_ERR value, in case of LY_SUCCESS, the \arg result is always provided.
280 */
Radek Krejci3b1f9292018-11-08 10:58:35 +0100281LY_ERR lys_module_localfile(struct ly_ctx *ctx, const char *name, const char *revision, int implement, struct ly_parser_ctx *main_ctx,
282 struct lys_module **result);
Radek Krejci086c7132018-10-26 15:29:04 +0200283
284/**
285 * @brief Make the module implemented.
286 * Does not check for collision in context, it must be done before calling the function, this is a simple switch.
287 * @param[in] mod Module to make implemented.
288 */
289void lys_module_implement(struct lys_module *mod);
290
Radek Krejcid33273d2018-10-25 14:55:52 +0200291/**
Radek Krejci86d106e2018-10-18 09:53:19 +0200292 * @brief Free the schema structure. It just frees, it does not remove the schema from its context.
293 * @param[in,out] module Schema module structure to free.
294 * @param[in] private_destructor Function to remove private data from the compiled schema tree.
295 */
296void lys_module_free(struct lys_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv));
297
298/**
299 * @brief
300 */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100301LY_ERR yang_parse(struct ly_parser_ctx *ctx, const char *data, struct lysp_module **mod_p);
Radek Krejci86d106e2018-10-18 09:53:19 +0200302
Radek Krejci70853c52018-10-15 14:46:16 +0200303#endif /* LY_TREE_SCHEMA_INTERNAL_H_ */