blob: 14928590be81cbcea25780466c57aede9afc3137 [file] [log] [blame]
Radek Krejcida04f4a2015-05-21 12:54:09 +02001/**
Radek Krejciefdd0ce2015-05-26 16:48:29 +02002 * @file parser.h
Radek Krejcida04f4a2015-05-21 12:54:09 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Radek Krejciefdd0ce2015-05-26 16:48:29 +02004 * @brief Parsers for libyang
Radek Krejcida04f4a2015-05-21 12:54:09 +02005 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci54f6fb32016-02-24 12:56:39 +01008 * 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
Michal Vasko8de098c2016-02-26 10:00:25 +010011 *
Radek Krejci54f6fb32016-02-24 12:56:39 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejcida04f4a2015-05-21 12:54:09 +020013 */
14
Radek Krejciefdd0ce2015-05-26 16:48:29 +020015#ifndef LY_PARSER_H_
16#define LY_PARSER_H_
Radek Krejcida04f4a2015-05-21 12:54:09 +020017
Michal Vasko0aee5c12016-06-17 14:27:26 +020018#include <pcre.h>
Radek Krejcib7970f22017-03-17 16:12:12 +010019#include <sys/mman.h>
Michal Vasko0aee5c12016-06-17 14:27:26 +020020
Radek Krejci3045cf32015-05-28 10:58:52 +020021#include "libyang.h"
Michal Vasko2d162e12015-09-24 14:33:29 +020022#include "tree_schema.h"
Michal Vaskoc3d9f8c2015-07-31 14:37:24 +020023#include "tree_internal.h"
Radek Krejcida04f4a2015-05-21 12:54:09 +020024
Andrew Fort892203a2018-02-16 16:37:16 -080025#ifdef __APPLE__
Andrew Fort2341f3c2018-03-26 15:27:42 -070026# ifndef MAP_ANONYMOUS
27# define MAP_ANONYMOUS MAP_ANON
28# endif
Andrew Fort892203a2018-02-16 16:37:16 -080029#endif
30
Radek Krejcida04f4a2015-05-21 12:54:09 +020031/**
32 * @defgroup yin YIN format support
33 * @{
34 */
Radek Krejciff4874d2016-03-07 12:30:50 +010035struct lys_module *yin_read_module(struct ly_ctx *ctx, const char *data, const char *revision, int implement);
36struct lys_submodule *yin_read_submodule(struct lys_module *module, const char *data,struct unres_schema *unres);
Radek Krejci3045cf32015-05-28 10:58:52 +020037
38/**@} yin */
Radek Krejcida04f4a2015-05-21 12:54:09 +020039
Radek Krejcib20c62d2015-07-07 17:07:14 +020040/**
41 * @defgroup xmldata XML data format support
42 * @{
43 */
Radek Krejci25b9fd32015-08-10 15:06:07 +020044struct lyd_node *xml_read_data(struct ly_ctx *ctx, const char *data, int options);
Radek Krejcib20c62d2015-07-07 17:07:14 +020045
46/**@} xmldata */
47
Radek Krejci5449d472015-10-26 14:35:56 +010048/**
49 * @defgroup jsondata JSON data format support
50 * @{
51 */
Michal Vasko945b96b2016-10-18 11:49:12 +020052struct lyd_node *lyd_parse_json(struct ly_ctx *ctx, const char *data, int options, const struct lyd_node *rpc_act,
PavolVican832f5432018-02-21 00:54:45 +010053 const struct lyd_node *data_tree, const char *yang_data_name);
Radek Krejci5449d472015-10-26 14:35:56 +010054
55/**@} jsondata */
56
Radek Krejci7212e0a2017-03-08 15:58:22 +010057/**
Michal Vasko1e82a3b2018-07-03 12:16:58 +020058 * @defgroup lybdata LYB data format support
59 * @{
60 */
Michal Vasko228431e2018-07-10 15:47:11 +020061struct lyd_node *lyd_parse_lyb(struct ly_ctx *ctx, const char *data, int options, const struct lyd_node *data_tree,
62 const char *yang_data_name, int *parsed);
Michal Vasko1e82a3b2018-07-03 12:16:58 +020063
64/**@} lybdata */
65
66/**
Radek Krejci7212e0a2017-03-08 15:58:22 +010067 * internal options values for schema parsers
68 */
69#define LYS_PARSE_OPT_CFG_NOINHERIT 0x01 /**< do not inherit config flag */
70#define LYS_PARSE_OPT_CFG_IGNORE 0x02 /**< ignore config flag (in rpc, actions, notifications) */
71#define LYS_PARSE_OPT_CFG_MASK 0x03
72#define LYS_PARSE_OPT_INGRP 0x04 /**< flag to know that parser is inside a grouping */
73
Radek Krejci06a9aa02017-02-17 10:50:24 +010074/* list of YANG statement strings */
75extern const char *ly_stmt_str[];
76
Radek Krejcic6556022016-01-27 15:16:45 +010077enum LY_IDENT {
78 LY_IDENT_SIMPLE, /* only syntax rules */
79 LY_IDENT_FEATURE,
80 LY_IDENT_IDENTITY,
81 LY_IDENT_TYPE,
82 LY_IDENT_NODE,
83 LY_IDENT_NAME, /* uniqueness across the siblings */
Radek Krejcia1a6b762016-11-14 09:53:38 +090084 LY_IDENT_PREFIX,
85 LY_IDENT_EXTENSION
Radek Krejcic6556022016-01-27 15:16:45 +010086};
Radek Krejci07d0fb92017-01-13 14:11:05 +010087int lyp_yin_fill_ext(void *parent, LYEXT_PAR parent_type, LYEXT_SUBSTMT substmt, uint8_t substmt_index,
Radek Krejci2b999ac2017-01-18 16:22:12 +010088 struct lys_module *module, struct lyxml_elem *yin, struct lys_ext_instance ***ext,
Michal Vasko42ddd0f2018-11-21 11:57:22 +010089 uint16_t ext_index, struct unres_schema *unres);
Radek Krejci63a91a92015-07-29 13:31:04 +020090
Radek Krejci8d6b7422017-02-03 14:42:13 +010091int lyp_yin_parse_complex_ext(struct lys_module *mod, struct lys_ext_instance_complex *ext,
92 struct lyxml_elem *yin, struct unres_schema *unres);
93int lyp_yin_parse_subnode_ext(struct lys_module *mod, void *elem, LYEXT_PAR elem_type,
94 struct lyxml_elem *yin, LYEXT_SUBSTMT type, uint8_t i, struct unres_schema *unres);
95
Michal Vasko8ea2b7f2015-09-29 14:30:53 +020096struct lys_type *lyp_get_next_union_type(struct lys_type *type, struct lys_type *prev_type, int *found);
97
Michal Vasko7675c622017-03-02 10:50:07 +010098/* return: 0 - ret set, ok; 1 - ret not set, no log, unknown meta; -1 - ret not set, log, fatal error */
99int lyp_fill_attr(struct ly_ctx *ctx, struct lyd_node *parent, const char *module_ns, const char *module_name,
Michal Vasko35f46a82018-05-30 10:44:11 +0200100 const char *attr_name, const char *attr_value, struct lyxml_elem *xml, int options, struct lyd_attr **ret);
Michal Vasko7675c622017-03-02 10:50:07 +0100101
102int lyp_check_edit_attr(struct ly_ctx *ctx, struct lyd_attr *attr, struct lyd_node *parent, int *editbits);
103
Radek Krejci1899d6a2016-11-03 13:48:07 +0100104struct lys_type *lyp_parse_value(struct lys_type *type, const char **value_, struct lyxml_elem *xml,
Michal Vasko31a2d322018-01-12 13:36:12 +0100105 struct lyd_node_leaf_list *leaf, struct lyd_attr *attr, struct lys_module *local_mod,
Michal Vasko35f46a82018-05-30 10:44:11 +0200106 int store, int dflt, int trusted);
Radek Krejci6dc53a22015-08-17 13:27:59 +0200107
Michal Vasko53b7da02018-02-13 15:28:42 +0100108int lyp_check_length_range(struct ly_ctx *ctx, const char *expr, struct lys_type *type);
Radek Krejci6dc53a22015-08-17 13:27:59 +0200109
Michal Vasko53b7da02018-02-13 15:28:42 +0100110int lyp_check_pattern(struct ly_ctx *ctx, const char *pattern, pcre **pcre_precomp);
111int lyp_precompile_pattern(struct ly_ctx *ctx, const char *pattern, pcre** pcre_cmp, pcre_extra **pcre_std);
Michal Vasko0aee5c12016-06-17 14:27:26 +0200112
Michal Vasko88c29542015-11-27 14:57:53 +0100113int fill_yin_type(struct lys_module *module, struct lys_node *parent, struct lyxml_elem *yin, struct lys_type *type,
Radek Krejci3a5501d2016-07-18 22:03:34 +0200114 int tpdftype, struct unres_schema *unres);
Michal Vasko88c29542015-11-27 14:57:53 +0100115
Radek Krejci4372b4e2016-04-14 17:42:16 +0200116int lyp_check_status(uint16_t flags1, struct lys_module *mod1, const char *name1,
117 uint16_t flags2, struct lys_module *mod2, const char *name2,
Radek Krejci48464ed2016-03-17 15:44:09 +0100118 const struct lys_node *node);
Radek Krejcicf509982015-12-15 09:22:44 +0100119
Michal Vasko10681e82018-01-16 14:54:16 +0100120void lyp_del_includedup(struct lys_module *mod, int free_subs);
Michal Vasko7b460e52017-02-10 14:50:26 +0100121
Pavol Vican810892e2016-07-12 16:55:34 +0200122int dup_typedef_check(const char *type, struct lys_tpdf *tpdf, int size);
Pavol Vicand6cda452016-07-13 15:08:29 +0200123
124int dup_identities_check(const char *id, struct lys_module *module);
125
Radek Krejci5449d472015-10-26 14:35:56 +0100126/**
Michal Vaskob1b19442016-07-13 12:26:01 +0200127 * @brief Get know if the node is part of the RPC/action's input/output
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100128 *
129 * @param node Schema node to be examined.
130 * @return 1 for true, 0 for false
131 */
Michal Vaskob1b19442016-07-13 12:26:01 +0200132int lyp_is_rpc_action(struct lys_node *node);
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100133
134/**
Michal Vaskoe746a502017-08-11 10:56:40 +0200135 * @brief Check validity of data parser options.
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100136 *
137 * @param options Parser options to be checked.
Radek Krejci86bafc22017-08-02 15:08:14 +0200138 * @param func name of the function where called
Michal Vaskoa55d96b2016-12-07 12:20:42 +0100139 * @return 0 for ok, 1 when multiple data types bits are set, or incompatible options are used together.
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100140 */
Michal Vasko53b7da02018-02-13 15:28:42 +0100141int lyp_data_check_options(struct ly_ctx *ctx, int options, const char *func);
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100142
Michal Vasko53b7da02018-02-13 15:28:42 +0100143int lyp_check_identifier(struct ly_ctx *ctx, const char *id, enum LY_IDENT type, struct lys_module *module, struct lys_node *parent);
144int lyp_check_date(struct ly_ctx *ctx, const char *date);
Radek Krejcidf24cbe2016-11-08 11:55:51 +0100145int lyp_check_mandatory_augment(struct lys_node_augment *node, const struct lys_node *target);
Radek Krejcie00d2312016-08-12 15:27:49 +0200146int lyp_check_mandatory_choice(struct lys_node *node);
Radek Krejcic6556022016-01-27 15:16:45 +0100147
Radek Krejcid4c1d0f2017-01-19 16:11:38 +0100148int lyp_check_include(struct lys_module *module, const char *value,
Pavol Vican0adf01d2016-03-22 12:29:33 +0100149 struct lys_include *inc, struct unres_schema *unres);
Radek Krejcid4c1d0f2017-01-19 16:11:38 +0100150int lyp_check_include_missing(struct lys_module *main_module);
Pavol Vican0adf01d2016-03-22 12:29:33 +0100151int lyp_check_import(struct lys_module *module, const char *value, struct lys_import *imp);
Radek Krejci9e757e02017-03-08 17:18:09 +0100152int lyp_check_circmod_add(struct lys_module *module);
153void lyp_check_circmod_pop(struct ly_ctx *ctx);
Pavol Vican0da132e2016-03-21 12:03:03 +0100154
Radek Krejci95f22ae2017-01-20 14:25:53 +0100155void lyp_sort_revisions(struct lys_module *module);
Radek Krejcibf285832017-01-26 16:05:41 +0100156int lyp_rfn_apply_ext(struct lys_module *module);
Radek Krejcife311c32017-01-27 16:58:59 +0100157int lyp_deviation_apply_ext(struct lys_module *module);
PavolVicanc4b798e2017-02-20 23:15:27 +0100158int lyp_mand_check_ext(struct lys_ext_instance_complex *ext, const char *ext_name);
Radek Krejci95f22ae2017-01-20 14:25:53 +0100159
PavolVican205e67b2018-02-21 00:38:31 +0100160const char *lyp_get_yang_data_template_name(const struct lyd_node *node);
161const struct lys_node *lyp_get_yang_data_template(const struct lys_module *module, const char *yang_data_name, int yang_data_name_len);
162
Michal Vasko42ddd0f2018-11-21 11:57:22 +0100163void lyp_ext_instance_rm(struct ly_ctx *ctx, struct lys_ext_instance ***ext, uint16_t *size, uint16_t index);
Radek Krejci6bd2c022017-02-01 15:04:49 +0100164
Radek Krejci4a49bdf2016-01-12 17:17:01 +0100165/**
Pavol Vicanaa1eba92016-03-09 12:27:11 +0100166 * @brief Propagate imports and includes into the main module
167 *
168 * @param module Main module
Radek Krejcib8f98c12016-06-24 10:30:46 +0200169 * @param inc Filled include structure
Pavol Vicanaa1eba92016-03-09 12:27:11 +0100170 * @return 0 for success, 1 for failure
171 */
Radek Krejcib8f98c12016-06-24 10:30:46 +0200172int lyp_propagate_submodule(struct lys_module *module, struct lys_include *inc);
Pavol Vicanaa1eba92016-03-09 12:27:11 +0100173
Michal Vaskodc3b7ce2017-04-06 14:24:29 +0200174/* return: -1 = error, 0 = succes, 1 = already there (if it was disabled, it is enabled first) */
PavolVican9e81c6a2017-02-09 13:09:07 +0100175int lyp_ctx_check_module(struct lys_module *module);
176
177int lyp_ctx_add_module(struct lys_module *module);
Pavol Vicanc99b59e2016-03-22 15:46:39 +0100178
Pavol Vicanaa1eba92016-03-09 12:27:11 +0100179/**
Michal Vaskob3d6d192018-11-05 10:02:10 +0100180 * @brief Add annotations definitions of attributes and URL config used in ietf-netconf RPCs.
Radek Krejci5b190662017-04-13 08:56:14 +0200181 */
Michal Vaskob3d6d192018-11-05 10:02:10 +0100182int lyp_add_ietf_netconf_annotations_config(struct lys_module *mod);
Radek Krejci5b190662017-04-13 08:56:14 +0200183
184/**
Radek Krejci0fb11502017-01-31 16:45:42 +0100185 * @brief mmap() wrapper for parsers. To unmap, use lyp_munmap().
186 *
187 * @param[in] prot The desired memory protection as in case of mmap().
188 * @param[in] fd File descriptor for getting data.
189 * @param[in] addsize Number of additional bytes to be allocated (and zeroed) after the implicitly added
190 * string-terminating NULL byte.
191 * @param[out] length length of the allocated memory.
Michal Vasko53b7da02018-02-13 15:28:42 +0100192 * @param[out] addr Pointer to the memory where the file data is mapped.
193 * @return 0 on success, non-zero on error.
Radek Krejci0fb11502017-01-31 16:45:42 +0100194 */
Michal Vasko53b7da02018-02-13 15:28:42 +0100195int lyp_mmap(struct ly_ctx *ctx, int fd, size_t addsize, size_t *length, void **addr);
Radek Krejci0fb11502017-01-31 16:45:42 +0100196
197/**
198 * @brief Unmap function for the data mapped by lyp_mmap()
199 */
200int lyp_munmap(void *addr, size_t length);
201
202/**
Radek Krejci5449d472015-10-26 14:35:56 +0100203 * Store UTF-8 character specified as 4byte integer into the dst buffer.
204 * Returns number of written bytes (4 max), expects that dst has enough space.
205 *
206 * UTF-8 mapping:
207 * 00000000 -- 0000007F: 0xxxxxxx
208 * 00000080 -- 000007FF: 110xxxxx 10xxxxxx
209 * 00000800 -- 0000FFFF: 1110xxxx 10xxxxxx 10xxxxxx
210 * 00010000 -- 001FFFFF: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
211 *
212 */
Michal Vasko53b7da02018-02-13 15:28:42 +0100213unsigned int pututf8(struct ly_ctx *ctx, char *dst, int32_t value);
214unsigned int copyutf8(struct ly_ctx *ctx, char *dst, const char *src);
Radek Krejci5449d472015-10-26 14:35:56 +0100215
Michal Vasko921eb6b2017-10-13 10:01:39 +0200216/**
217 * @brief Find a module. First, imports from \p module with matching \p prefix, \p name, or both are checked,
218 * \p module itself is also compared, and lastly a callback is used if allowed.
219 *
220 * @param[in] module Module with imports.
221 * @param[in] prefix Module prefix to search for.
222 * @param[in] pref_len Module \p prefix length. If 0, the whole prefix is used, if not NULL.
223 * @param[in] name Module name to search for.
224 * @param[in] name_len Module \p name length. If 0, the whole name is used, if not NULL.
225 * @param[in] in_data Whether to use data callback if not found after trying all the rest.
226 * Import callback is never used because there is no use-case for that.
227 *
228 * @return Matching module, NULL if not found.
229 */
230const struct lys_module *lyp_get_module(const struct lys_module *module, const char *prefix, int pref_len,
231 const char *name, int name_len, int in_data);
232
233/**
234 * @brief Find an import from \p module with matching namespace, the \p module itself is also considered.
235 *
236 * @param[in] module Module with imports.
237 * @param[in] ns Namespace to be found.
238 */
239const struct lys_module *lyp_get_import_module_ns(const struct lys_module *module, const char *ns);
240
Michal Vaskoc6cd3f02018-03-02 14:07:42 +0100241/*
242 * Internal functions implementing YANG (extension and user type) plugin support
243 * - implemented in plugins.c
244 */
245
246/**
247 * @brief If available, get the extension plugin for the specified extension
248 *
249 * @param[in] name Name of the extension
250 * @param[in] module Name of the extension's module
251 * @param[in] revision Revision of the extension's module
252 * @return pointer to the extension plugin structure, NULL if no plugin available
253 */
254struct lyext_plugin *ext_get_plugin(const char *name, const char *module, const char *revision);
255
256/**
257 * @brief Try to store a value as a user type defined by a plugin.
258 *
259 * @param[in] mod Module of the type.
260 * @param[in] type_name Type (typedef) name.
261 * @param[in] value_str Value to store as a string.
262 * @param[in,out] value Filled value to be overwritten by the user store callback.
263 * @return 0 on successful storing, 1 if the type is not a user type, -1 on error.
264 */
265int lytype_store(const struct lys_module *mod, const char *type_name, const char *value_str, lyd_val *value);
266
267/**
268 * @brief Free a user type stored value.
269 *
270 * @param[in] mod Module of the type.
271 * @param[in] type_name Type (typedef) name.
272 * @param[in] value Value union to free.
273 */
274void lytype_free(const struct lys_module *mod, const char *type_name, lyd_val value);
275
Radek Krejciefdd0ce2015-05-26 16:48:29 +0200276#endif /* LY_PARSER_H_ */