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