Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file parser_schema.h |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Schema parsers for libyang |
| 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_PARSER_SCHEMA_H_ |
| 16 | #define LY_PARSER_SCHEMA_H_ |
| 17 | |
Jan Kundrát | a2fd210 | 2021-12-09 22:29:57 +0100 | [diff] [blame] | 18 | #include "log.h" |
| 19 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 20 | #ifdef __cplusplus |
| 21 | extern "C" { |
| 22 | #endif |
| 23 | |
| 24 | struct ly_in; |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 25 | struct lys_module; |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 26 | |
| 27 | /** |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 28 | * @page howtoSchemaParsers Parsing YANG Modules |
| 29 | * |
| 30 | * YANG module parsers allow to read YANG module from a specific format. libyang supports the following module formats: |
| 31 | * |
| 32 | * - YANG |
| 33 | * |
| 34 | * Basic YANG schemas format described in [RFC 6020](http://tools.ietf.org/html/rfc6020) and |
| 35 | * [RFC 7951](http://tools.ietf.org/html/rfc7951) (so both YANG 1.0 and YANG 1.1 versions are supported). |
| 36 | * |
| 37 | * - YIN |
| 38 | * |
| 39 | * Alternative XML-based format to YANG - YANG Independent Notation. The details can be found in |
| 40 | * [RFC 6020](http://tools.ietf.org/html/rfc6020#section-11) and |
| 41 | * [RFC 7951](http://tools.ietf.org/html/rfc7951#section-13). |
| 42 | * |
| 43 | * When the [context](@ref howtoContext) is created, it already contains the following YANG modules, which |
| 44 | * are implemented internally by libyang: |
| 45 | * - ietf-yang-metadata@2016-08-05 |
| 46 | * - yang@2020-06-17 |
| 47 | * - ietf-inet-types@2013-07-15 |
| 48 | * - ietf-yang-types@2013-07-15 |
| 49 | * - ietf-datastores@2018-02-14 |
| 50 | * - ietf-yang-library@2019-01-04 |
| 51 | * |
| 52 | * The `yang` module is the libyang's internal module to provide namespace and definitions of for various YANG |
| 53 | * attributes described in [RFC 7951](https://tools.ietf.org/html/rfc6243) (such as `insert` attribute for |
| 54 | * edit-config's data). |
| 55 | * |
| 56 | * Other modules can be added to the context manually with the functions listed below. Besides them, |
| 57 | * it is also possible to use ::ly_ctx_load_module() which tries to find the required module automatically - using |
| 58 | * ::ly_module_imp_clb or automatic search in working directory and in the context's search directories. For details, see |
| 59 | * [how the context works](@ref howtoContext). |
| 60 | * |
| 61 | * YANG modules are loaded in two steps. First, the input YANG/YIN data are parsed into \b lysp_* structures that reflect |
| 62 | * the structure of the input module and submodule(s). Mostly just syntax checks are done, no reference or type checking is |
| 63 | * performed in this step. If the module is supposed to be implemented, not just imported by another module, the second step |
| 64 | * is to compile it. The compiled tree may significantly differ from the source (parsed) tree structure. All the references |
| 65 | * are resolved, groupings are instantiated, types are resolved (and compiled by joining all the relevant restrictions |
| 66 | * when derived from another types) and many other syntactical checks are done. |
| 67 | * |
aPiecek | b0445f2 | 2021-06-24 11:34:07 +0200 | [diff] [blame] | 68 | * There is the main parsing function ::lys_parse() working with the libyang [input handler](@ref howtoInput). However, |
| 69 | * to simplify some of the use-cases, it is also possible to use other functions accepting input data from various sources. |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 70 | * |
| 71 | * Functions List |
| 72 | * -------------- |
| 73 | * - ::lys_parse() |
| 74 | * - ::lys_parse_mem() |
| 75 | * - ::lys_parse_fd() |
| 76 | * - ::lys_parse_path() |
| 77 | * |
| 78 | * - ::lys_search_localfile() |
| 79 | * - ::ly_ctx_set_module_imp_clb() |
| 80 | * - ::ly_ctx_load_module() |
| 81 | */ |
| 82 | |
| 83 | /** |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 84 | * @addtogroup schematree |
| 85 | * @{ |
| 86 | */ |
| 87 | |
| 88 | /** |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 89 | * @brief Schema input formats accepted by libyang [parser functions](@ref howtoSchemaParsers). |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 90 | */ |
| 91 | typedef enum { |
| 92 | LYS_IN_UNKNOWN = 0, /**< unknown format, used as return value in case of error */ |
| 93 | LYS_IN_YANG = 1, /**< YANG schema input format */ |
| 94 | LYS_IN_YIN = 3 /**< YIN schema input format */ |
| 95 | } LYS_INFORMAT; |
| 96 | |
| 97 | /** |
| 98 | * @brief Load a schema into the specified context. |
| 99 | * |
| 100 | * @param[in] ctx libyang context where to process the data model. |
| 101 | * @param[in] in The input handle to provide the dumped data model in the specified format. |
Radek Krejci | 545b487 | 2020-11-15 10:15:12 +0100 | [diff] [blame] | 102 | * @param[in] format Format of the schema to parse. Can be 0 to try to detect format from the input handler. |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 103 | * @param[in] features Array of features to enable ended with NULL. If NULL, no features are enabled. |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 104 | * @param[out] module Optional parsed module. |
| 105 | * @return LY_ERR value. |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 106 | */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 107 | LY_ERR lys_parse(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, const char **features, |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 108 | struct lys_module **module); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 109 | |
| 110 | /** |
| 111 | * @brief Load a schema into the specified context. |
| 112 | * |
aPiecek | b0445f2 | 2021-06-24 11:34:07 +0200 | [diff] [blame] | 113 | * This function is considered for a simple use, if you have a complex use-case, |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 114 | * consider use of ::lys_parse() with a standalone input handler. |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 115 | * |
| 116 | * @param[in] ctx libyang context where to process the data model. |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 117 | * @param[in] data The string containing the dumped data model in the specified format. |
Michal Vasko | 64c9173 | 2021-06-08 16:41:21 +0200 | [diff] [blame] | 118 | * @param[in] format Format of the schema to parse. |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 119 | * @param[out] module Optional parsed module. |
| 120 | * @return LY_ERR value. |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 121 | */ |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 122 | LY_ERR lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, struct lys_module **module); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 123 | |
| 124 | /** |
| 125 | * @brief Read a schema from file descriptor into the specified context. |
| 126 | * |
| 127 | * \note Current implementation supports only reading data from standard (disk) file, not from sockets, pipes, etc. |
| 128 | * |
aPiecek | b0445f2 | 2021-06-24 11:34:07 +0200 | [diff] [blame] | 129 | * This function is considered for a simple use, if you have a complex use-case, |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 130 | * consider use of ::lys_parse() with a standalone input handler. |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 131 | * |
| 132 | * @param[in] ctx libyang context where to process the data model. |
| 133 | * @param[in] fd File descriptor of a regular file (e.g. sockets are not supported) containing the schema |
| 134 | * in the specified format. |
Michal Vasko | 64c9173 | 2021-06-08 16:41:21 +0200 | [diff] [blame] | 135 | * @param[in] format Format of the schema to parse. |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 136 | * @param[out] module Optional parsed module. |
| 137 | * @return LY_ERR value. |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 138 | */ |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 139 | LY_ERR lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, struct lys_module **module); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 140 | |
| 141 | /** |
| 142 | * @brief Load a schema into the specified context from a file. |
| 143 | * |
aPiecek | b0445f2 | 2021-06-24 11:34:07 +0200 | [diff] [blame] | 144 | * This function is considered for a simple use, if you have a complex use-case, |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 145 | * consider use of ::lys_parse() with a standalone input handler. |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 146 | * |
| 147 | * @param[in] ctx libyang context where to process the data model. |
| 148 | * @param[in] path Path to the file with the model in the specified format. |
Michal Vasko | 64c9173 | 2021-06-08 16:41:21 +0200 | [diff] [blame] | 149 | * @param[in] format Format of the schema to parse. |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 150 | * @param[out] module Optional parsed module. |
| 151 | * @return LY_ERR value. |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 152 | */ |
Michal Vasko | 4de7d07 | 2021-07-09 09:13:18 +0200 | [diff] [blame] | 153 | LY_ERR lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, struct lys_module **module); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 154 | |
| 155 | /** |
| 156 | * @brief Search for the schema file in the specified searchpaths. |
| 157 | * |
| 158 | * @param[in] searchpaths NULL-terminated array of paths to be searched (recursively). Current working |
| 159 | * directory is searched automatically (but non-recursively if not in the provided list). Caller can use |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 160 | * result of the ::ly_ctx_get_searchdirs(). |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 161 | * @param[in] cwd Flag to implicitly search also in the current working directory (non-recursively). |
| 162 | * @param[in] name Name of the schema to find. |
| 163 | * @param[in] revision Revision of the schema to find. If NULL, the newest found schema filepath is returned. |
| 164 | * @param[out] localfile Mandatory output variable containing absolute path of the found schema. If no schema |
| 165 | * complying the provided restriction is found, NULL is set. |
| 166 | * @param[out] format Optional output variable containing expected format of the schema document according to the |
| 167 | * file suffix. |
| 168 | * @return LY_ERR value (LY_SUCCESS is returned even if the file is not found, then the *localfile is NULL). |
| 169 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 170 | LY_ERR lys_search_localfile(const char * const *searchpaths, ly_bool cwd, const char *name, const char *revision, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 171 | char **localfile, LYS_INFORMAT *format); |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 172 | |
| 173 | /** @} schematree */ |
| 174 | |
| 175 | #ifdef __cplusplus |
| 176 | } |
| 177 | #endif |
| 178 | |
| 179 | #endif /* LY_PARSER_SCHEMA_H_ */ |