blob: 7554ad0a828a3ab343690df7a16e2d42e2eb2315 [file] [log] [blame]
David Sedlákb1ce3f82019-06-05 14:37:26 +02001/**
2 * @file parser_yin.h
3 * @author David Sedlák <xsedla1d@stud.fit.vutbr.cz>
4 * @brief YIN parser.
5 *
6 * Copyright (c) 2015 - 2019 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 */
David Sedlák2b626ee2019-06-03 16:40:18 +020014
15#ifndef LY_PARSER_YIN_H_
16#define LY_PARSER_YIN_H_
17
18#include <stdio.h>
David Sedlákb1ce3f82019-06-05 14:37:26 +020019#include <stdlib.h>
20
David Sedlák2b626ee2019-06-03 16:40:18 +020021#include "log.h"
22#include "xml.h"
David Sedlák2b626ee2019-06-03 16:40:18 +020023
David Sedlákf6251182019-06-06 10:22:13 +020024/* list of yin attribute strings */
25extern const char *const yin_attr_list[];
26#define yin_attr2str(STMT) yin_attr_list[STMT]
27
David Sedlák2b214ac2019-06-06 16:11:03 +020028#define YIN_NS_URI "urn:ietf:params:xml:ns:yang:yin:1"
David Sedlákd6e56892019-07-01 15:40:24 +020029#define name2fullname(name, prefix_len) (prefix_len != 0 ? name - (prefix_len + 1) : name)
David Sedlákf250ecf2019-07-01 11:02:05 +020030#define namelen2fulllen(name_len, prefix_len) (prefix_len != 0 ? name_len + prefix_len + 1 : name_len)
David Sedlák2b214ac2019-06-06 16:11:03 +020031
David Sedlák4ffcec82019-07-25 15:10:21 +020032/* shortcut to determin if keyword can in general be subelement of deviation regardles of it's type */
33#define isdevsub(kw) (kw == YANG_CONFIG || kw == YANG_DEFAULT || kw == YANG_MANDATORY || \
34 kw == YANG_MAX_ELEMENTS || kw == YANG_MIN_ELEMENTS || \
35 kw == YANG_MUST || kw == YANG_TYPE || kw == YANG_UNIQUE || \
36 kw == YANG_UNITS || kw == YANG_CUSTOM)
37
38/* get deviate type from */
39
David Sedlák2b626ee2019-06-03 16:40:18 +020040enum YIN_ARGUMENT {
David Sedlák1bccdfa2019-06-17 15:55:27 +020041 YIN_ARG_UNKNOWN = 0, /**< parsed argument can not be matched with any supported yin argument keyword */
David Sedlák2b626ee2019-06-03 16:40:18 +020042 YIN_ARG_NAME, /**< argument name */
43 YIN_ARG_TARGET_NODE, /**< argument target-node */
44 YIN_ARG_MODULE, /**< argument module */
45 YIN_ARG_VALUE, /**< argument value */
46 YIN_ARG_TEXT, /**< argument text */
47 YIN_ARG_CONDITION, /**< argument condition */
48 YIN_ARG_URI, /**< argument uri */
49 YIN_ARG_DATE, /**< argument data */
50 YIN_ARG_TAG, /**< argument tag */
David Sedlák1bccdfa2019-06-17 15:55:27 +020051 YIN_ARG_NONE, /**< empty (special value) */
David Sedlák7ff55a92019-06-17 11:11:41 +020052};
53
David Sedlák1bccdfa2019-06-17 15:55:27 +020054/**
55 * @brief structure to store instance of xml attribute
56 */
David Sedlák7ff55a92019-06-17 11:11:41 +020057struct yin_arg_record {
David Sedlák1bccdfa2019-06-17 15:55:27 +020058 const char *prefix; /**< start of prefix */
59 size_t prefix_len; /**< length of prefix */
60 const char *name; /**< start of name */
61 size_t name_len; /**< length of name */
62 char *content; /**< start of content */
63 size_t content_len; /**< length of content */
64 int dynamic_content; /**< is set to 1 iff content is dynamically allocated 0 otherwise */
David Sedlák2b626ee2019-06-03 16:40:18 +020065};
66
David Sedlák3ffbc522019-07-02 17:49:28 +020067/* flags to encode cardinality of subelement */
68#define YIN_SUBELEM_MANDATORY 0x01 /**< is set when subelement is mandatory */
69#define YIN_SUBELEM_UNIQUE 0x02 /**< is set when subelement is unique */
David Sedláke1a30302019-07-10 13:49:38 +020070#define YIN_SUBELEM_FIRST 0x04 /**< is set when subelement is actually yang argument mapped to yin element */
David Sedlák0c2bab92019-07-22 15:33:19 +020071#define YIN_SUBELEM_VER2 0x08 /**< subelemnt is allowed only in modules with version at least 2 (YANG 1.1) */
David Sedlák21f87cd2019-07-03 16:53:23 +020072
73#define YIN_SUBELEM_PARSED 0x80 /**< is set during parsing when given subelement is encountered for the first
David Sedlák3ffbc522019-07-02 17:49:28 +020074 time to simply check validity of given constraints */
75
76struct yin_subelement {
77 enum yang_keyword type; /**< type of keyword */
David Sedlák4a650532019-07-10 11:55:18 +020078 void *dest; /**< meta infromation passed to responsible function (mostly information about where parsed subelement should be stored) */
David Sedlákb4e44562019-07-04 15:42:12 +020079 uint8_t flags; /**< describes cardianlity of subelement can be set to YIN_SUBELEM_MANDATORY and YIN_SUBELEM_UNIQUE and YIN_SUBELEM_FIRST */
David Sedlák3ffbc522019-07-02 17:49:28 +020080};
81
David Sedlákb4e44562019-07-04 15:42:12 +020082/* helper structure just to make code look simpler */
David Sedlák3ffbc522019-07-02 17:49:28 +020083struct sized_string {
84 const char *value;
85 size_t len;
86};
87
David Sedlákb4e44562019-07-04 15:42:12 +020088/* Meta information passed to yin_parse_argument function,
89 holds information about where content of argument element will be stored. */
David Sedlák3ffbc522019-07-02 17:49:28 +020090struct yin_argument_meta {
David Sedlákb4e44562019-07-04 15:42:12 +020091 uint16_t *flags; /**< Argument flags */
92 const char **argument; /**< Argument value */
David Sedlák3ffbc522019-07-02 17:49:28 +020093};
94
David Sedlák04e17b22019-07-19 15:29:48 +020095/* Meta information passed to functions working with tree schema such as yin_parse_any */
David Sedlák8a83bbb2019-07-18 14:46:00 +020096struct tree_node_meta {
97 struct lysp_node *parent; /**< parent node */
98 struct lysp_node **siblings; /**< linked list of siblings */
99};
100
David Sedlák04e17b22019-07-19 15:29:48 +0200101/* Meta information passed to yin_parse_typedef */
102struct typedef_meta {
103 struct lysp_node *parent; /**< parent node */
David Sedlák0d6de5a2019-07-22 13:25:44 +0200104 struct lysp_tpdf **typedefs; /**< [Sized array](@ref sizedarrays) of typedefs to add to */
105};
106
David Sedlák0c2bab92019-07-22 15:33:19 +0200107/* Meta information passed to yin_parse_augment function */
David Sedlák0d6de5a2019-07-22 13:25:44 +0200108struct augment_meta {
109 struct lysp_node *parent; /**< parent node */
110 struct lysp_augment **augments; /**< [Sized array](@ref sizedarrays) of augments to add to */
David Sedlák04e17b22019-07-19 15:29:48 +0200111};
112
David Sedlák0c2bab92019-07-22 15:33:19 +0200113/* Meta information passed to yin_parse_include function */
114struct include_meta {
David Sedlák05404f62019-07-24 14:11:53 +0200115 const char *name; /**< Module/submodule name. */
116 struct lysp_include **includes; /**< [Sized array](@ref sizedarrays) of parsed includes to add to. */
David Sedlák0c2bab92019-07-22 15:33:19 +0200117};
118
David Sedlák05404f62019-07-24 14:11:53 +0200119/* Meta information passed to yin_parse_notification function */
David Sedlák031b9e72019-07-23 15:19:37 +0200120struct notif_meta {
David Sedlák05404f62019-07-24 14:11:53 +0200121 struct lysp_node *parent; /**< Parent node. */
122 struct lysp_notif **notifs; /**< [Sized array](@ref sizedarrays) of notifications to add to. */
David Sedláke3ce9ef2019-07-23 16:34:30 +0200123};
124
David Sedlák05404f62019-07-24 14:11:53 +0200125/* Meta information passed to yin_parse_grouping function */
David Sedláke3ce9ef2019-07-23 16:34:30 +0200126struct grouping_meta {
David Sedlák05404f62019-07-24 14:11:53 +0200127 struct lysp_node *parent; /**< Parent node. */
128 struct lysp_grp **groupings; /**< [Sized array](@ref sizedarrays) of groupings to add to. */
129};
130
131/* Meta information passed to yin_parse_grouping function */
132struct inout_meta {
David Sedlák85d0eca2019-07-24 15:15:21 +0200133 struct lysp_node *parent; /**< Parent node. */
David Sedlák05404f62019-07-24 14:11:53 +0200134 struct lysp_action_inout *inout_p; /**< inout_p Input/output pointer to write to. */
David Sedlák031b9e72019-07-23 15:19:37 +0200135};
136
David Sedlák992fb7c2019-07-24 16:51:01 +0200137/* Meta information passed to yin_parse_action function */
David Sedlák85d0eca2019-07-24 15:15:21 +0200138struct action_meta {
139 struct lysp_node *parent; /**< Parent node. */
140 struct lysp_action **actions; /**< Actions to add to. */
141};
142
David Sedlák298ff6d2019-07-26 14:29:03 +0200143/* Meta information passed to yin_parse_import function */
144struct import_meta {
145 const char *prefix; /**< module prefix. */
146 struct lysp_import **imports; /**< imports to add to. */
147};
148
David Sedlák4ffcec82019-07-25 15:10:21 +0200149struct minmax_dev_meta {
150 uint32_t *lim;
151 uint16_t *flags;
152 struct lysp_ext_instance **exts;
153};
154
David Sedlák2b626ee2019-06-03 16:40:18 +0200155/**
156 * @brief Match argument name.
157 *
158 * @param[in] name String representing name.
159 * @param[in] len Lenght of the name.
160 *
David Sedlákb1ce3f82019-06-05 14:37:26 +0200161 * @return YIN_ARGUMENT value.
David Sedlák2b626ee2019-06-03 16:40:18 +0200162 */
David Sedlák060b00e2019-06-19 11:12:06 +0200163enum YIN_ARGUMENT yin_match_argument_name(const char *name, size_t len);
David Sedlák2b626ee2019-06-03 16:40:18 +0200164
165/**
David Sedlák555c7202019-07-04 12:14:12 +0200166 * @brief Generic function for content parsing
David Sedlák2b626ee2019-06-03 16:40:18 +0200167 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200168 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlák555c7202019-07-04 12:14:12 +0200169 * @param[in] subelem_info array of valid subelement types and meta information,
170 * array must be ordered by subelem_info->type in ascending order.
171 * @param[in] subelem_info_size Size of subelem_info array.
172 * @param[in,out] data Data to read from, always moved to currently handled character.
173 * @param[in] current_element Type of current element.
David Sedlákb4e44562019-07-04 15:42:12 +0200174 * @param[out] text_content Where the text content of element should be stored if any. Text content is ignored if set to NULL.
David Sedlákda8ffa32019-07-08 14:17:10 +0200175 * @param[in,out] exts Extension instance to add to. Can be se to null if element cannot have extension as subelements.
David Sedlákb4e44562019-07-04 15:42:12 +0200176
David Sedlák2b214ac2019-06-06 16:11:03 +0200177 * @return LY_ERR values.
David Sedlák2b626ee2019-06-03 16:40:18 +0200178 */
David Sedlákda8ffa32019-07-08 14:17:10 +0200179LY_ERR yin_parse_content(struct yin_parser_ctx *ctx, struct yin_subelement *subelem_info, signed char subelem_info_size,
David Sedlák555c7202019-07-04 12:14:12 +0200180 const char **data, enum yang_keyword current_element, const char **text_content,
181 struct lysp_ext_instance **exts);
David Sedlák2b626ee2019-06-03 16:40:18 +0200182
David Sedlákd9d3a312019-06-04 09:47:10 +0200183/**
David Sedlák4a650532019-07-10 11:55:18 +0200184 * @brief Check that val is valid UTF8 character sequence of val_type.
185 * Doesn't check empty string, only character validity.
186 *
187 * @param[in] ctx Yin parser context for logging.
188 * @param[in] val_type Type of the input string to select method of checking character validity.
189 * @param[in] val Input to validate.
190 * @param[in] len Length of input.
191 *
192 * @return LY_ERR values.
193 */
194LY_ERR yin_validate_value(struct yin_parser_ctx *ctx, enum yang_arg val_type, char *val, size_t len);
195
196/**
David Sedlákb4e44562019-07-04 15:42:12 +0200197 * @brief Match yang keyword from yin data.
David Sedlák1bccdfa2019-06-17 15:55:27 +0200198 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200199 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákb4e44562019-07-04 15:42:12 +0200200 * @param[in] name Start of keyword name
David Sedlák8f7a1172019-06-20 14:42:18 +0200201 * @param[in] name_len Lenght of keyword name.
202 * @param[in] prefix Start of keyword prefix.
203 * @param[in] prefix_len lenght of prefix.
David Sedlákc1771b12019-07-10 15:55:46 +0200204 * @param[in] parrent Identification of parrent element, use YANG_NONE for elements without parrent.
David Sedlák1bccdfa2019-06-17 15:55:27 +0200205 *
206 * @return yang_keyword values.
207 */
David Sedlákda8ffa32019-07-08 14:17:10 +0200208enum yang_keyword yin_match_keyword(struct yin_parser_ctx *ctx, const char *name, size_t name_len,
David Sedlákc1771b12019-07-10 15:55:46 +0200209 const char *prefix, size_t prefix_len, enum yang_keyword parrent);
David Sedlák1bccdfa2019-06-17 15:55:27 +0200210
David Sedlákb6e65972019-06-19 10:44:13 +0200211/**
David Sedlákbba38e52019-07-09 15:20:01 +0200212 * @brief load all attributes of element into ([sized array](@ref sizedarrays)). Caller is suposed to free the array.
David Sedlák8f7a1172019-06-20 14:42:18 +0200213 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200214 * @param[in,out] ctx Yin parser context for logging and to store current state.
215 * @param[in,out] data Data to read from, always moved to currently handled character.
David Sedlákbba38e52019-07-09 15:20:01 +0200216 * @param[out] attrs ([Sized array](@ref sizedarrays)) of attributes.
David Sedlák8f7a1172019-06-20 14:42:18 +0200217 *
218 * @return LY_ERR values.
219 */
David Sedlákda8ffa32019-07-08 14:17:10 +0200220LY_ERR yin_load_attributes(struct yin_parser_ctx *ctx, const char **data, struct yin_arg_record **attrs);
David Sedlákb6e65972019-06-19 10:44:13 +0200221
David Sedlák554e36d2019-06-20 16:00:04 +0200222/**
David Sedlákb1a78352019-06-28 16:16:29 +0200223 * @brief Parse instance of extension.
224 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200225 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákbba38e52019-07-09 15:20:01 +0200226 * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of extension instance.
David Sedlákb1a78352019-06-28 16:16:29 +0200227 * @param[in,out] data Data to read from, always moved to currently handled character.
228 * @param[in] ext_name Name of the extension element.
229 * @param[in] ext_name_len Length of extension name.
David Sedlákb4e44562019-07-04 15:42:12 +0200230 * @param[in] subelem Type of the keyword this extension instance is a subelement of.
231 * @param[in] subelem_index Index of the keyword instance this extension instance is a subelement of
232 * @param[in,out] exts Extension instance to add to.
David Sedlákb1a78352019-06-28 16:16:29 +0200233 *
234 * @return LY_ERR values.
235 */
David Sedlák1f90d252019-07-10 17:09:32 +0200236LY_ERR yin_parse_extension_instance(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
David Sedlák619db942019-07-03 14:47:30 +0200237 const char *ext_name, int ext_name_len, LYEXT_SUBSTMT subelem,
238 uint32_t subelem_index, struct lysp_ext_instance **exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200239
240/**
241 * @brief Parse yin element into generic structure.
242 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200243 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákb1a78352019-06-28 16:16:29 +0200244 * @param[in] name Name of element.
245 * @param[in] name_len Length of elements Name.
246 * @param[in,out] data Data to read from, always moved to currently handled character.
247 * @param[out] element Where the element structure should be stored.
248 *
249 * @return LY_ERR values.
250 */
David Sedlák4ffcec82019-07-25 15:10:21 +0200251LY_ERR yin_parse_element_generic(struct yin_parser_ctx *ctx, const char *name, size_t name_len, const char **data,
252 struct lysp_stmt **element);
David Sedlákb1a78352019-06-28 16:16:29 +0200253
David Sedlák4f03b932019-07-26 13:01:47 +0200254/**
David Sedlák298ff6d2019-07-26 14:29:03 +0200255 * @brief Parse module element.
David Sedlák4f03b932019-07-26 13:01:47 +0200256 *
257 * @param[in,out] ctx Yin parser context for logging and to store current state.
258 * @param[in] mod_attrs Attributes of module element.
259 * @param[in,out] data Data to read from.
260 * @param[out] mod Parsed module structure.
261 *
262 * @return LY_ERR values.
263 */
David Sedlák298ff6d2019-07-26 14:29:03 +0200264LY_ERR yin_parse_mod(struct yin_parser_ctx *ctx, struct yin_arg_record *mod_attrs,
265 const char **data, struct lysp_module *mod);
David Sedlák4f03b932019-07-26 13:01:47 +0200266
267
268/**
David Sedlák298ff6d2019-07-26 14:29:03 +0200269 * @brief Parse submodule element.
270 *
271 * @param[in,out] ctx Yin parser context for logging and to store current state.
272 * @param[in] mod_attrs Attributes of module element.
273 * @param[in,out] data Data to read from.
274 * @param[out] mod Parsed module structure.
275 *
276 * @return LY_ERR values.
277 */
278LY_ERR yin_parse_submod(struct yin_parser_ctx *ctx, struct yin_arg_record *mod_attrs,
279 const char **data, struct lysp_submodule *submod);
280
281/**
David Sedlák4f03b932019-07-26 13:01:47 +0200282 * @brief free argument record, content loaded from lyxml_get_string() can be
283 * dynamically allocated in some cases so it must be also freed.
284 *
285 * @param ctx unused just to fulfill
286 */
287void free_arg_rec(struct yin_parser_ctx *ctx, struct yin_arg_record *record);
288
David Sedlák2b626ee2019-06-03 16:40:18 +0200289#endif /* LY_PARSER_YIN_H_*/