blob: 1ca6d4cba8cfed4a870b14e5716452a00bfda6ea [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/**
Radek Krejci4f28eda2018-11-12 11:46:16 +010054 * @brief internal context for compilation
55 */
56struct lysc_ctx {
57 struct ly_ctx *ctx;
58 struct lys_module *mod;
59 uint16_t path_len;
60#define LYSC_CTX_BUFSIZE 4078
61 char path[LYSC_CTX_BUFSIZE];
62};
63
64/**
Radek Krejci70853c52018-10-15 14:46:16 +020065 * @brief Check the currently present prefixes in the module for collision with the new one.
66 *
Radek Krejcibbe09a92018-11-08 09:36:54 +010067 * @param[in] ctx Context for logging.
Radek Krejci70853c52018-10-15 14:46:16 +020068 * @param[in] module Schema tree to check.
69 * @param[in] value Newly added prefix value (including its location to distinguish collision with itself).
70 * @return LY_EEXIST when prefix is already used in the module, LY_SUCCESS otherwise
71 */
72LY_ERR lysp_check_prefix(struct ly_parser_ctx *ctx, struct lysp_module *module, const char **value);
73
Radek Krejci86d106e2018-10-18 09:53:19 +020074/**
75 * @brief Check date string (4DIGIT "-" 2DIGIT "-" 2DIGIT)
76 *
Radek Krejcibbe09a92018-11-08 09:36:54 +010077 * @param[in] ctx Optional context for logging.
Radek Krejci86d106e2018-10-18 09:53:19 +020078 * @param[in] date Date string to check (non-necessarily terminated by \0)
79 * @param[in] date_len Length of the date string, 10 expected.
80 * @param[in] stmt Statement name for error message.
81 * @return LY_ERR value.
82 */
Radek Krejcibbe09a92018-11-08 09:36:54 +010083LY_ERR lysp_check_date(struct ly_parser_ctx *ctx, const char *date, int date_len, const char *stmt);
84
85/**
86 * @brief Check names of typedefs in the parsed module to detect collisions.
87 *
88 * @param[in] ctx Parser context, module where the type is being defined is taken from here.
89 * @return LY_ERR value.
90 */
91LY_ERR lysp_check_typedefs(struct ly_parser_ctx *ctx);
Radek Krejci86d106e2018-10-18 09:53:19 +020092
93/**
94 * @brief Just move the newest revision into the first position, does not sort the rest
95 * @param[in] revs Sized-array of the revisions in a printable schema tree.
96 */
97void lysp_sort_revisions(struct lysp_revision *revs);
98
99/**
Radek Krejcibbe09a92018-11-08 09:36:54 +0100100 * @brief Find type specified type definition
101 *
102 * @param[in] id Name of the type including possible prefix. Module where the prefix is being searched is start_module.
103 * @param[in] start_node Context node where the type is being instantiated to be able to search typedefs in parents.
104 * @param[in] start_module Module where the type is being instantiated for search for typedefs.
Radek Krejci4f28eda2018-11-12 11:46:16 +0100105 * @param[out] type Built-in type identifier of the id. If #LY_TYPE_UNKNOWN, tpdf is expected to contain found YANG schema typedef statement.
Radek Krejcibbe09a92018-11-08 09:36:54 +0100106 * @param[out] tpdf Found type definition.
107 * @param[out] node Node where the found typedef is defined, NULL in case of a top-level typedef.
108 * @param[out] module Module where the found typedef is being defined, NULL in case of built-in YANG types.
109 */
110LY_ERR lysp_type_find(const char *id, struct lysp_node *start_node, struct lysp_module *start_module,
Radek Krejci4f28eda2018-11-12 11:46:16 +0100111 LY_DATA_TYPE *type, const struct lysp_tpdf **tpdf, struct lysp_node **node, struct lysp_module **module);
Radek Krejcibbe09a92018-11-08 09:36:54 +0100112
113/**
Radek Krejci086c7132018-10-26 15:29:04 +0200114 * @brief Find and parse module of the given name.
115 *
116 * @param[in] ctx libyang context.
117 * @param[in] name Name of the module to load.
118 * @param[in] revison Optional revision of the module to load. If NULL, the newest revision is loaded.
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100119 * @param[in] implement Flag if the loaded module is supposed to be marked as implemented.
120 * @param[in] require_parsed Flag to require parsed module structure in case the module is already in the context,
121 * but only the compiled structure is available.
Radek Krejci086c7132018-10-26 15:29:04 +0200122 * @param[out] mod Parsed module structure.
123 * @return LY_ERR value.
124 */
Radek Krejci6d6e4e42018-10-29 13:28:19 +0100125LY_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 +0200126
127/**
Radek Krejcid33273d2018-10-25 14:55:52 +0200128 * @brief Parse included submodule into the simply parsed YANG module.
129 *
Radek Krejci3b1f9292018-11-08 10:58:35 +0100130 * @param[in] ctx parser context
Radek Krejcid33273d2018-10-25 14:55:52 +0200131 * @param[in] mod Module including a submodule.
Radek Krejcid33273d2018-10-25 14:55:52 +0200132 * @param[in,out] inc Include structure holding all available information about the include statement, the parsed
133 * submodule is stored into this structure.
134 * @return LY_ERR value.
135 */
Radek Krejci3b1f9292018-11-08 10:58:35 +0100136LY_ERR lysp_load_submodule(struct ly_parser_ctx *ctx, struct lysp_module *mod, struct lysp_include *inc);
Radek Krejcid33273d2018-10-25 14:55:52 +0200137
138/**
Radek Krejcibbe09a92018-11-08 09:36:54 +0100139 * @brief Get address of a node's typedefs list if any.
140 *
141 * Decides the node's type and in case it has an typedefs list, returns its address.
142 * @param[in] node Node to check.
143 * @return Address of the node's tpdf member if any, NULL otherwise.
144 */
145struct lysp_tpdf **lysp_node_typedefs(struct lysp_node *node);
146
147/**
148 * @brief Get address of a node's actions list if any.
149 *
150 * Decides the node's type and in case it has an actions list, returns its address.
151 * @param[in] node Node to check.
152 * @return Address of the node's actions member if any, NULL otherwise.
153 */
154struct lysp_action **lysp_node_actions(struct lysp_node *node);
155
156/**
157 * @brief Get address of a node's notifications list if any.
158 *
159 * Decides the node's type and in case it has a notifications list, returns its address.
160 * @param[in] node Node to check.
161 * @return Address of the node's notifs member if any, NULL otherwise.
162 */
163struct lysp_notif **lysp_node_notifs(struct lysp_node *node);
164
165/**
166 * @brief Get address of a node's child pointer if any.
167 *
168 * Decides the node's type and in case it has a children list, returns its address.
169 * @param[in] node Node to check.
170 * @return Address of the node's child member if any, NULL otherwise.
171 */
172struct lysp_node **lysp_node_children(struct lysp_node *node);
173
174/**
175 * @brief Get address of a node's child pointer if any.
176 *
177 * Decides the node's type and in case it has a children list, returns its address.
178 * @param[in] node Node to check.
179 * @return Address of the node's child member if any, NULL otherwise.
180 */
181struct lysc_node **lysc_node_children(struct lysc_node *node);
182
183/**
Radek Krejcice8c1592018-10-29 15:35:51 +0100184 * @brief Find the module referenced by prefix in the provided parsed mod.
185 *
186 * @param[in] mod Schema module where the prefix was used.
187 * @param[in] prefix Prefix used to reference a module.
188 * @param[in] len Length of the prefix since it is not necessary NULL-terminated.
189 * @return Pointer to the module or NULL if the module is not found.
190 */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100191struct lysp_module *lysp_module_find_prefix(struct lysp_module *mod, const char *prefix, size_t len);
Radek Krejcice8c1592018-10-29 15:35:51 +0100192
193/**
194 * @brief Find the module referenced by prefix in the provided compiled mod.
195 *
196 * @param[in] mod Schema module where the prefix was used.
197 * @param[in] prefix Prefix used to reference a module.
198 * @param[in] len Length of the prefix since it is not necessary NULL-terminated.
199 * @return Pointer to the module or NULL if the module is not found.
200 */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100201struct lysc_module *lysc_module_find_prefix(struct lysc_module *mod, const char *prefix, size_t len);
Radek Krejcice8c1592018-10-29 15:35:51 +0100202
203/**
Radek Krejci4f28eda2018-11-12 11:46:16 +0100204 * @brief Check statement's status for invalid combination.
205 *
206 * The modX parameters are used just to determine if both flags are in the same module,
207 * so any of the schema module structure can be used, but both modules must be provided
208 * in the same type.
209 *
210 * @param[in] ctx Compile context for logging.
211 * @param[in] flags1 Flags of the referencing node.
212 * @param[in] mod1 Module of the referencing node,
213 * @param[in] name1 Schema node name of the referencing node.
214 * @param[in] flags2 Flags of the referenced node.
215 * @param[in] mod2 Module of the referenced node,
216 * @param[in] name2 Schema node name of the referenced node.
217 * @return LY_ERR value
218 */
219LY_ERR lysc_check_status(struct lysc_ctx *ctx,
220 uint16_t flags1, void *mod1, const char *name1,
221 uint16_t flags2, void *mod2, const char *name2);
222
223/**
Radek Krejci151a5b72018-10-19 14:21:44 +0200224 * @brief Find the module referenced by prefix in the provided mod.
225 *
226 * @param[in] mod Schema module where the prefix was used.
227 * @param[in] prefix Prefix used to reference a module.
228 * @param[in] len Length of the prefix since it is not necessary NULL-terminated.
229 * @return Pointer to the module or NULL if the module is not found.
230 */
Radek Krejcice8c1592018-10-29 15:35:51 +0100231struct lys_module *lys_module_find_prefix(struct lys_module *mod, const char *prefix, size_t len);
Radek Krejci151a5b72018-10-19 14:21:44 +0200232
233/**
Radek Krejcid33273d2018-10-25 14:55:52 +0200234 * @brief Parse YANG module and submodule from a string.
235 *
236 * In contrast to public lys_parse_mem(), also submodules can be parsed here. However,
237 * while the modules are added into the context, submodules not. The latest_revision
238 * flag is updated in both cases.
239 *
240 * @param[in] ctx libyang context where to process the data model.
241 * @param[in] data The string containing the dumped data model in the specified
242 * format.
243 * @param[in] format Format of the input data (YANG or YIN).
Radek Krejcid33273d2018-10-25 14:55:52 +0200244 * @param[in] implement Flag if the schema is supposed to be marked as implemented.
Radek Krejci3b1f9292018-11-08 10:58:35 +0100245 * @param[in] main_ctx Parser context of the main module in case of parsing submodule.
Radek Krejci9ed7a192018-10-31 16:23:51 +0100246 * @param[in] custom_check Callback to check the parsed schema before it is accepted.
247 * @param[in] check_data Caller's data to pass to the custom_check callback.
Radek Krejcid33273d2018-10-25 14:55:52 +0200248 * @return Pointer to the data model structure or NULL on error.
249 */
Radek Krejci3b1f9292018-11-08 10:58:35 +0100250struct 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 +0100251 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, void *data), void *check_data);
Radek Krejcid33273d2018-10-25 14:55:52 +0200252
253/**
254 * @brief Parse YANG module and submodule from a file descriptor.
255 *
256 * In contrast to public lys_parse_mem(), also submodules can be parsed here. However,
257 * while the modules are added into the context, submodules not. The latest_revision
258 * flag is updated in both cases.
259 *
260 * \note Current implementation supports only reading data from standard (disk) file, not from sockets, pipes, etc.
261 *
262 * @param[in] ctx libyang context where to process the data model.
263 * @param[in] fd File descriptor of a regular file (e.g. sockets are not supported) containing the schema
264 * in the specified format.
265 * @param[in] format Format of the input data (YANG or YIN).
Radek Krejcid33273d2018-10-25 14:55:52 +0200266 * @param[in] implement Flag if the schema is supposed to be marked as implemented.
Radek Krejci3b1f9292018-11-08 10:58:35 +0100267 * @param[in] main_ctx Parser context of the main module in case of parsing submodule.
Radek Krejci9ed7a192018-10-31 16:23:51 +0100268 * @param[in] custom_check Callback to check the parsed schema before it is accepted.
269 * @param[in] check_data Caller's data to pass to the custom_check callback.
Radek Krejcid33273d2018-10-25 14:55:52 +0200270 * @return Pointer to the data model structure or NULL on error.
271 */
Radek Krejci3b1f9292018-11-08 10:58:35 +0100272struct 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 +0100273 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, void *data), void *check_data);
Radek Krejcid33273d2018-10-25 14:55:52 +0200274
275/**
276 * @brief Parse YANG module and submodule from a file descriptor.
277 *
278 * In contrast to public lys_parse_mem(), also submodules can be parsed here. However,
279 * while the modules are added into the context, submodules not. The latest_revision
280 * flag is updated in both cases.
281 *
282 * \note Current implementation supports only reading data from standard (disk) file, not from sockets, pipes, etc.
283 *
284 * @brief REad a schema into the specified context from a file.
285 *
286 * @param[in] ctx libyang context where to process the data model.
287 * @param[in] path Path to the file with the model in the specified format.
288 * @param[in] format Format of the input data (YANG or YIN).
Radek Krejcid33273d2018-10-25 14:55:52 +0200289 * @param[in] implement Flag if the schema is supposed to be marked as implemented.
Radek Krejci3b1f9292018-11-08 10:58:35 +0100290 * @param[in] main_ctx Parser context of the main module in case of parsing submodule.
Radek Krejci9ed7a192018-10-31 16:23:51 +0100291 * @param[in] custom_check Callback to check the parsed schema before it is accepted.
292 * @param[in] check_data Caller's data to pass to the custom_check callback.
Radek Krejcid33273d2018-10-25 14:55:52 +0200293 * @return Pointer to the data model structure or NULL on error.
294 */
Radek Krejci3b1f9292018-11-08 10:58:35 +0100295struct 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 +0100296 LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, void *data), void *check_data);
Radek Krejcid33273d2018-10-25 14:55:52 +0200297
298/**
299 * @brief Load the (sub)module into the context.
300 *
301 * This function does not check the presence of the (sub)module in context, it should be done before calling this function.
302 *
303 * module_name and submodule_name are alternatives - only one of the
304 *
305 * @param[in] ctx libyang context where to work.
306 * @param[in] name Name of the (sub)module to load.
307 * @param[in] revision Optional revision of the (sub)module to load, if NULL the newest revision is being loaded.
308 * @param[in] implement Flag if the (sub)module is supposed to be marked as implemented.
Radek Krejci3b1f9292018-11-08 10:58:35 +0100309 * @param[in] main_ctx Parser context of the main module in case of loading submodule.
Radek Krejcid33273d2018-10-25 14:55:52 +0200310 * @param[out] result Parsed YANG schema tree of the requested module. If it is a module, it is already in the context!
311 * @return LY_ERR value, in case of LY_SUCCESS, the \arg result is always provided.
312 */
Radek Krejci3b1f9292018-11-08 10:58:35 +0100313LY_ERR lys_module_localfile(struct ly_ctx *ctx, const char *name, const char *revision, int implement, struct ly_parser_ctx *main_ctx,
314 struct lys_module **result);
Radek Krejci086c7132018-10-26 15:29:04 +0200315
316/**
317 * @brief Make the module implemented.
318 * Does not check for collision in context, it must be done before calling the function, this is a simple switch.
319 * @param[in] mod Module to make implemented.
320 */
321void lys_module_implement(struct lys_module *mod);
322
Radek Krejcid33273d2018-10-25 14:55:52 +0200323/**
Radek Krejci19a96102018-11-15 13:38:09 +0100324 * @brief Free the compiled node structure.
325 * @param[in] ctx libyang context where the string data resides in a dictionary.
326 * @param[in,out] node Compiled node structure to be freed.
327 */
328void lysc_node_free(struct ly_ctx *ctx, struct lysc_node *node);
329
330/**
331 * @brief Free the compiled schema structure.
332 * @param[in,out] module Compiled schema module structure to free.
333 * @param[in] private_destructor Function to remove private data from the compiled schema tree.
334 */
335void lysc_module_free(struct lysc_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv));
336
337/**
Radek Krejci86d106e2018-10-18 09:53:19 +0200338 * @brief Free the schema structure. It just frees, it does not remove the schema from its context.
339 * @param[in,out] module Schema module structure to free.
340 * @param[in] private_destructor Function to remove private data from the compiled schema tree.
341 */
342void lys_module_free(struct lys_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv));
343
344/**
345 * @brief
346 */
Radek Krejcibbe09a92018-11-08 09:36:54 +0100347LY_ERR yang_parse(struct ly_parser_ctx *ctx, const char *data, struct lysp_module **mod_p);
Radek Krejci86d106e2018-10-18 09:53:19 +0200348
Radek Krejci70853c52018-10-15 14:46:16 +0200349#endif /* LY_TREE_SCHEMA_INTERNAL_H_ */