Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 1 | /** |
| 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 Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 18 | #define LOGVAL_YANG(CTX, ...) LOGVAL((CTX)->ctx, LY_VLOG_LINE, &(CTX)->line, __VA_ARGS__) |
| 19 | |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 20 | /** |
Radek Krejci | e384647 | 2018-10-15 15:24:51 +0200 | [diff] [blame] | 21 | * @brief List of YANG statement groups - the (sub)module's substatements |
| 22 | */ |
| 23 | enum 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 | */ |
| 34 | enum 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 Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 42 | * @brief internal context for schema parsers |
| 43 | */ |
| 44 | struct ly_parser_ctx { |
| 45 | struct ly_ctx *ctx; |
Radek Krejci | faa1eac | 2018-10-30 14:34:55 +0100 | [diff] [blame] | 46 | struct lysp_module *mod; |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 47 | 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 Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | /** |
| 54 | * @brief Check the currently present prefixes in the module for collision with the new one. |
| 55 | * |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 56 | * @param[in] ctx Context for logging. |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 57 | * @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 | */ |
| 61 | LY_ERR lysp_check_prefix(struct ly_parser_ctx *ctx, struct lysp_module *module, const char **value); |
| 62 | |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 63 | /** |
| 64 | * @brief Check date string (4DIGIT "-" 2DIGIT "-" 2DIGIT) |
| 65 | * |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 66 | * @param[in] ctx Optional context for logging. |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 67 | * @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 Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 72 | LY_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 | */ |
| 80 | LY_ERR lysp_check_typedefs(struct ly_parser_ctx *ctx); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 81 | |
| 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 | */ |
| 86 | void lysp_sort_revisions(struct lysp_revision *revs); |
| 87 | |
| 88 | /** |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 89 | * @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 | */ |
| 98 | LY_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 Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 102 | * @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 Krejci | 6d6e4e4 | 2018-10-29 13:28:19 +0100 | [diff] [blame] | 107 | * @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 Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 110 | * @param[out] mod Parsed module structure. |
| 111 | * @return LY_ERR value. |
| 112 | */ |
Radek Krejci | 6d6e4e4 | 2018-10-29 13:28:19 +0100 | [diff] [blame] | 113 | LY_ERR lysp_load_module(struct ly_ctx *ctx, const char *name, const char *revision, int implement, int require_parsed, struct lys_module **mod); |
Radek Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 114 | |
| 115 | /** |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 116 | * @brief Parse included submodule into the simply parsed YANG module. |
| 117 | * |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame^] | 118 | * @param[in] ctx parser context |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 119 | * @param[in] mod Module including a submodule. |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 120 | * @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 Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame^] | 124 | LY_ERR lysp_load_submodule(struct ly_parser_ctx *ctx, struct lysp_module *mod, struct lysp_include *inc); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 125 | |
| 126 | /** |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 127 | * @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 | */ |
| 133 | struct 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 | */ |
| 142 | struct 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 | */ |
| 151 | struct 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 | */ |
| 160 | struct 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 | */ |
| 169 | struct lysc_node **lysc_node_children(struct lysc_node *node); |
| 170 | |
| 171 | /** |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 172 | * @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 Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 179 | struct lysp_module *lysp_module_find_prefix(struct lysp_module *mod, const char *prefix, size_t len); |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 180 | |
| 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 Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 189 | struct lysc_module *lysc_module_find_prefix(struct lysc_module *mod, const char *prefix, size_t len); |
Radek Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 190 | |
| 191 | /** |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 192 | * @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 Krejci | ce8c159 | 2018-10-29 15:35:51 +0100 | [diff] [blame] | 199 | struct lys_module *lys_module_find_prefix(struct lys_module *mod, const char *prefix, size_t len); |
Radek Krejci | 151a5b7 | 2018-10-19 14:21:44 +0200 | [diff] [blame] | 200 | |
| 201 | /** |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 202 | * @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 Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 212 | * @param[in] implement Flag if the schema is supposed to be marked as implemented. |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame^] | 213 | * @param[in] main_ctx Parser context of the main module in case of parsing submodule. |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 214 | * @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 Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 216 | * @return Pointer to the data model structure or NULL on error. |
| 217 | */ |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame^] | 218 | struct lys_module *lys_parse_mem_(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, int implement, struct ly_parser_ctx *main_ctx, |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 219 | LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, void *data), void *check_data); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 220 | |
| 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 Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 234 | * @param[in] implement Flag if the schema is supposed to be marked as implemented. |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame^] | 235 | * @param[in] main_ctx Parser context of the main module in case of parsing submodule. |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 236 | * @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 Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 238 | * @return Pointer to the data model structure or NULL on error. |
| 239 | */ |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame^] | 240 | struct lys_module *lys_parse_fd_(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, int implement, struct ly_parser_ctx *main_ctx, |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 241 | LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, void *data), void *check_data); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 242 | |
| 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 Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 257 | * @param[in] implement Flag if the schema is supposed to be marked as implemented. |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame^] | 258 | * @param[in] main_ctx Parser context of the main module in case of parsing submodule. |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 259 | * @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 Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 261 | * @return Pointer to the data model structure or NULL on error. |
| 262 | */ |
Radek Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame^] | 263 | struct lys_module *lys_parse_path_(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, int implement, struct ly_parser_ctx *main_ctx, |
Radek Krejci | 9ed7a19 | 2018-10-31 16:23:51 +0100 | [diff] [blame] | 264 | LY_ERR (*custom_check)(struct ly_ctx *ctx, struct lysp_module *mod, void *data), void *check_data); |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 265 | |
| 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 Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame^] | 277 | * @param[in] main_ctx Parser context of the main module in case of loading submodule. |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 278 | * @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 Krejci | 3b1f929 | 2018-11-08 10:58:35 +0100 | [diff] [blame^] | 281 | LY_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 Krejci | 086c713 | 2018-10-26 15:29:04 +0200 | [diff] [blame] | 283 | |
| 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 | */ |
| 289 | void lys_module_implement(struct lys_module *mod); |
| 290 | |
Radek Krejci | d33273d | 2018-10-25 14:55:52 +0200 | [diff] [blame] | 291 | /** |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 292 | * @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 | */ |
| 296 | void lys_module_free(struct lys_module *module, void (*private_destructor)(const struct lysc_node *node, void *priv)); |
| 297 | |
| 298 | /** |
| 299 | * @brief |
| 300 | */ |
Radek Krejci | bbe09a9 | 2018-11-08 09:36:54 +0100 | [diff] [blame] | 301 | LY_ERR yang_parse(struct ly_parser_ctx *ctx, const char *data, struct lysp_module **mod_p); |
Radek Krejci | 86d106e | 2018-10-18 09:53:19 +0200 | [diff] [blame] | 302 | |
Radek Krejci | 70853c5 | 2018-10-15 14:46:16 +0200 | [diff] [blame] | 303 | #endif /* LY_TREE_SCHEMA_INTERNAL_H_ */ |