blob: 0c392d32fd1262917364e69a09a6ef4da9f569c3 [file] [log] [blame]
Radek Krejcif0e1ba52020-05-22 15:14:35 +02001/**
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át495dec12021-12-09 22:29:57 +010018#include "log.h"
19
Radek Krejcif0e1ba52020-05-22 15:14:35 +020020#ifdef __cplusplus
21extern "C" {
22#endif
23
24struct ly_in;
Michal Vasko3a41dff2020-07-15 14:30:28 +020025struct lys_module;
Radek Krejcif0e1ba52020-05-22 15:14:35 +020026
27/**
Radek Krejci8678fa42020-08-18 16:07:28 +020028 * @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 *
aPiecekb0445f22021-06-24 11:34:07 +020068 * 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 Krejci8678fa42020-08-18 16:07:28 +020070 *
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 Krejcif0e1ba52020-05-22 15:14:35 +020084 * @addtogroup schematree
85 * @{
86 */
87
88/**
Radek Krejci8678fa42020-08-18 16:07:28 +020089 * @brief Schema input formats accepted by libyang [parser functions](@ref howtoSchemaParsers).
Radek Krejcif0e1ba52020-05-22 15:14:35 +020090 */
91typedef 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 Krejci545b4872020-11-15 10:15:12 +0100102 * @param[in] format Format of the schema to parse. Can be 0 to try to detect format from the input handler.
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100103 * @param[in] features Array of features to enable ended with NULL. If NULL, no features are enabled.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200104 * @param[out] module Optional parsed module.
105 * @return LY_ERR value.
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200106 */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100107LY_ERR lys_parse(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format, const char **features,
Michal Vasko4de7d072021-07-09 09:13:18 +0200108 struct lys_module **module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200109
110/**
111 * @brief Load a schema into the specified context.
112 *
aPiecekb0445f22021-06-24 11:34:07 +0200113 * This function is considered for a simple use, if you have a complex use-case,
Radek Krejci8678fa42020-08-18 16:07:28 +0200114 * consider use of ::lys_parse() with a standalone input handler.
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200115 *
116 * @param[in] ctx libyang context where to process the data model.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200117 * @param[in] data The string containing the dumped data model in the specified format.
Michal Vasko64c91732021-06-08 16:41:21 +0200118 * @param[in] format Format of the schema to parse.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200119 * @param[out] module Optional parsed module.
120 * @return LY_ERR value.
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200121 */
Michal Vasko4de7d072021-07-09 09:13:18 +0200122LY_ERR lys_parse_mem(struct ly_ctx *ctx, const char *data, LYS_INFORMAT format, struct lys_module **module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200123
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 *
aPiecekb0445f22021-06-24 11:34:07 +0200129 * This function is considered for a simple use, if you have a complex use-case,
Radek Krejci8678fa42020-08-18 16:07:28 +0200130 * consider use of ::lys_parse() with a standalone input handler.
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200131 *
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 Vasko64c91732021-06-08 16:41:21 +0200135 * @param[in] format Format of the schema to parse.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200136 * @param[out] module Optional parsed module.
137 * @return LY_ERR value.
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200138 */
Michal Vasko4de7d072021-07-09 09:13:18 +0200139LY_ERR lys_parse_fd(struct ly_ctx *ctx, int fd, LYS_INFORMAT format, struct lys_module **module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200140
141/**
142 * @brief Load a schema into the specified context from a file.
143 *
aPiecekb0445f22021-06-24 11:34:07 +0200144 * This function is considered for a simple use, if you have a complex use-case,
Radek Krejci8678fa42020-08-18 16:07:28 +0200145 * consider use of ::lys_parse() with a standalone input handler.
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200146 *
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 Vasko64c91732021-06-08 16:41:21 +0200149 * @param[in] format Format of the schema to parse.
Michal Vasko3a41dff2020-07-15 14:30:28 +0200150 * @param[out] module Optional parsed module.
151 * @return LY_ERR value.
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200152 */
Michal Vasko4de7d072021-07-09 09:13:18 +0200153LY_ERR lys_parse_path(struct ly_ctx *ctx, const char *path, LYS_INFORMAT format, struct lys_module **module);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200154
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 Krejci8678fa42020-08-18 16:07:28 +0200160 * result of the ::ly_ctx_get_searchdirs().
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200161 * @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 Krejci857189e2020-09-01 13:26:36 +0200170LY_ERR lys_search_localfile(const char * const *searchpaths, ly_bool cwd, const char *name, const char *revision,
Radek Krejci0f969882020-08-21 16:56:47 +0200171 char **localfile, LYS_INFORMAT *format);
Radek Krejcif0e1ba52020-05-22 15:14:35 +0200172
173/** @} schematree */
174
175#ifdef __cplusplus
176}
177#endif
178
179#endif /* LY_PARSER_SCHEMA_H_ */