blob: 94c5dc020469850e2d97327be9ecd5d6ddf1ed72 [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>
David Sedlákc5b20842019-08-13 10:18:31 +02004 * @brief YIN parser header file.
David Sedlákb1ce3f82019-06-05 14:37:26 +02005 *
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
Radek Krejci535ea9f2020-05-29 16:01:05 +020018#include <stdint.h>
David Sedlák2b626ee2019-06-03 16:40:18 +020019#include <stdio.h>
David Sedlákb1ce3f82019-06-05 14:37:26 +020020
David Sedlák2b626ee2019-06-03 16:40:18 +020021#include "log.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020022#include "tree.h"
23#include "tree_schema.h"
24#include "tree_schema_internal.h"
David Sedlák2b626ee2019-06-03 16:40:18 +020025
David Sedlákf6251182019-06-06 10:22:13 +020026/* list of yin attribute strings */
Michal Vasko22df3f02020-08-24 13:29:22 +020027extern const char * const yin_attr_list[];
David Sedlákf6251182019-06-06 10:22:13 +020028#define yin_attr2str(STMT) yin_attr_list[STMT]
29
David Sedlák26ea1432019-08-14 13:42:23 +020030#define VALID_VALS1 " Only valid value is \"%s\"."
31#define VALID_VALS2 " Valid values are \"%s\" and \"%s\"."
32#define VALID_VALS3 " Valid values are \"%s\", \"%s\" and \"%s\"."
33#define VALID_VALS4 " Valid values are \"%s\", \"%s\", \"%s\" and \"%s\"."
34
David Sedlák4ffcec82019-07-25 15:10:21 +020035/* shortcut to determin if keyword can in general be subelement of deviation regardles of it's type */
Radek Krejcid6b76452019-09-03 17:03:03 +020036#define isdevsub(kw) (kw == LY_STMT_CONFIG || kw == LY_STMT_DEFAULT || kw == LY_STMT_MANDATORY || \
37 kw == LY_STMT_MAX_ELEMENTS || kw == LY_STMT_MIN_ELEMENTS || \
38 kw == LY_STMT_MUST || kw == LY_STMT_TYPE || kw == LY_STMT_UNIQUE || \
39 kw == LY_STMT_UNITS || kw == LY_STMT_EXTENSION_INSTANCE)
David Sedlák4ffcec82019-07-25 15:10:21 +020040
David Sedlákc5b20842019-08-13 10:18:31 +020041enum yin_argument {
David Sedlák1bccdfa2019-06-17 15:55:27 +020042 YIN_ARG_UNKNOWN = 0, /**< parsed argument can not be matched with any supported yin argument keyword */
David Sedlák2b626ee2019-06-03 16:40:18 +020043 YIN_ARG_NAME, /**< argument name */
44 YIN_ARG_TARGET_NODE, /**< argument target-node */
45 YIN_ARG_MODULE, /**< argument module */
46 YIN_ARG_VALUE, /**< argument value */
47 YIN_ARG_TEXT, /**< argument text */
48 YIN_ARG_CONDITION, /**< argument condition */
49 YIN_ARG_URI, /**< argument uri */
50 YIN_ARG_DATE, /**< argument data */
51 YIN_ARG_TAG, /**< argument tag */
Michal Vasko69730152020-10-09 16:30:07 +020052 YIN_ARG_NONE /**< empty (special value) */
David Sedlák7ff55a92019-06-17 11:11:41 +020053};
54
David Sedlákc5b20842019-08-13 10:18:31 +020055/* flags to set constraints of subelements */
David Sedlák3ffbc522019-07-02 17:49:28 +020056#define YIN_SUBELEM_MANDATORY 0x01 /**< is set when subelement is mandatory */
57#define YIN_SUBELEM_UNIQUE 0x02 /**< is set when subelement is unique */
David Sedláke1a30302019-07-10 13:49:38 +020058#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 +020059#define YIN_SUBELEM_VER2 0x08 /**< subelemnt is allowed only in modules with version at least 2 (YANG 1.1) */
Michal Vasko7f45cf22020-10-01 12:49:44 +020060#define YIN_SUBELEM_TEXT 0x10 /**< is set when a statement should be parsed into text instead of nodeid */
David Sedlák21f87cd2019-07-03 16:53:23 +020061
62#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 +020063 time to simply check validity of given constraints */
64
65struct yin_subelement {
Radek Krejci7eb54ba2020-05-18 16:30:04 +020066 enum ly_stmt type; /**< type of keyword */
David Sedlák4a650532019-07-10 11:55:18 +020067 void *dest; /**< meta infromation passed to responsible function (mostly information about where parsed subelement should be stored) */
Michal Vasko7f45cf22020-10-01 12:49:44 +020068 uint16_t flags; /**< describes constraints of subelement can be set to YIN_SUBELEM_MANDATORY, YIN_SUBELEM_UNIQUE, YIN_SUBELEM_FIRST, YIN_SUBELEM_VER2, and YIN_SUBELEM_DEFAULT_TEXT */
David Sedlák3ffbc522019-07-02 17:49:28 +020069};
70
David Sedlákb4e44562019-07-04 15:42:12 +020071/* Meta information passed to yin_parse_argument function,
72 holds information about where content of argument element will be stored. */
David Sedlák3ffbc522019-07-02 17:49:28 +020073struct yin_argument_meta {
David Sedlákb4e44562019-07-04 15:42:12 +020074 uint16_t *flags; /**< Argument flags */
75 const char **argument; /**< Argument value */
David Sedlák3ffbc522019-07-02 17:49:28 +020076};
77
David Sedlák6881b512019-08-13 12:52:00 +020078/**
79 * @brief Meta information passed to functions working with tree_schema,
80 * that require additional information about parent node.
81 */
David Sedlák8a83bbb2019-07-18 14:46:00 +020082struct tree_node_meta {
83 struct lysp_node *parent; /**< parent node */
David Sedlákbf8a2b72019-08-14 16:48:10 +020084 struct lysp_node **nodes; /**< linked list of siblings */
David Sedlák8a83bbb2019-07-18 14:46:00 +020085};
86
David Sedlák6881b512019-08-13 12:52:00 +020087/**
88 * @brief Meta information passed to yin_parse_import function.
89 */
David Sedlák298ff6d2019-07-26 14:29:03 +020090struct import_meta {
91 const char *prefix; /**< module prefix. */
92 struct lysp_import **imports; /**< imports to add to. */
93};
94
David Sedlák6881b512019-08-13 12:52:00 +020095/**
96 * @brief Meta information passed to yin_parse_include function.
97 */
98struct include_meta {
99 const char *name; /**< Module/submodule name. */
100 struct lysp_include **includes; /**< [Sized array](@ref sizedarrays) of parsed includes to add to. */
101};
102
103/**
David Sedlák81497a32019-08-13 16:56:26 +0200104 * @brief Meta information passed to yin_parse_inout function.
David Sedlák6881b512019-08-13 12:52:00 +0200105 */
106struct inout_meta {
107 struct lysp_node *parent; /**< Parent node. */
108 struct lysp_action_inout *inout_p; /**< inout_p Input/output pointer to write to. */
109};
110
111/**
112 * @brief Meta information passed to yin_parse_minmax function.
113 */
David Sedlák4ffcec82019-07-25 15:10:21 +0200114struct minmax_dev_meta {
David Sedlák6881b512019-08-13 12:52:00 +0200115 uint32_t *lim; /**< min/max value to write to. */
116 uint16_t *flags; /**< min/max flags to write to. */
117 struct lysp_ext_instance **exts; /**< extension instances to add to. */
David Sedlák4ffcec82019-07-25 15:10:21 +0200118};
119
David Sedlák2b626ee2019-06-03 16:40:18 +0200120/**
121 * @brief Match argument name.
122 *
123 * @param[in] name String representing name.
124 * @param[in] len Lenght of the name.
125 *
David Sedlákbf8a2b72019-08-14 16:48:10 +0200126 * @return yin_argument values.
David Sedlák2b626ee2019-06-03 16:40:18 +0200127 */
David Sedlákc5b20842019-08-13 10:18:31 +0200128enum yin_argument yin_match_argument_name(const char *name, size_t len);
David Sedlák2b626ee2019-06-03 16:40:18 +0200129
130/**
David Sedlák555c7202019-07-04 12:14:12 +0200131 * @brief Generic function for content parsing
David Sedlák2b626ee2019-06-03 16:40:18 +0200132 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200133 * @param[in,out] ctx Yin parser context for logging and to store current state.
Radek Krejci12b1c152019-09-05 16:20:48 +0200134 * @param[in] subelem_info array of valid subelement types and meta information
David Sedlák555c7202019-07-04 12:14:12 +0200135 * @param[in] subelem_info_size Size of subelem_info array.
David Sedlák555c7202019-07-04 12:14:12 +0200136 * @param[in] current_element Type of current element.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200137 * @param[out] text_content Where the text content of element should be stored if any. Text content is ignored if set to NULL.
138 * @param[in,out] exts Extension instance to add to. Can be set to null if element cannot have extension as subelements.
David Sedlákc5b20842019-08-13 10:18:31 +0200139 *
David Sedlák2b214ac2019-06-06 16:11:03 +0200140 * @return LY_ERR values.
David Sedlák2b626ee2019-06-03 16:40:18 +0200141 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100142LY_ERR yin_parse_content(struct lys_yin_parser_ctx *ctx, struct yin_subelement *subelem_info, size_t subelem_info_size,
Radek Krejci0f969882020-08-21 16:56:47 +0200143 enum ly_stmt current_element, const char **text_content, struct lysp_ext_instance **exts);
David Sedlák2b626ee2019-06-03 16:40:18 +0200144
David Sedlákd9d3a312019-06-04 09:47:10 +0200145/**
David Sedlák4a650532019-07-10 11:55:18 +0200146 * @brief Check that val is valid UTF8 character sequence of val_type.
147 * Doesn't check empty string, only character validity.
148 *
149 * @param[in] ctx Yin parser context for logging.
150 * @param[in] val_type Type of the input string to select method of checking character validity.
David Sedlák4a650532019-07-10 11:55:18 +0200151 *
152 * @return LY_ERR values.
153 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100154LY_ERR yin_validate_value(struct lys_yin_parser_ctx *ctx, enum yang_arg val_type);
David Sedlák4a650532019-07-10 11:55:18 +0200155
156/**
David Sedlákb4e44562019-07-04 15:42:12 +0200157 * @brief Match yang keyword from yin data.
David Sedlák1bccdfa2019-06-17 15:55:27 +0200158 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200159 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákb4e44562019-07-04 15:42:12 +0200160 * @param[in] name Start of keyword name
David Sedlák8f7a1172019-06-20 14:42:18 +0200161 * @param[in] name_len Lenght of keyword name.
162 * @param[in] prefix Start of keyword prefix.
David Sedlákc5b20842019-08-13 10:18:31 +0200163 * @param[in] prefix_len Lenght of prefix.
Radek Krejcid6b76452019-09-03 17:03:03 +0200164 * @param[in] parrent Identification of parrent element, use LY_STMT_NONE for elements without parrent.
David Sedlák1bccdfa2019-06-17 15:55:27 +0200165 *
166 * @return yang_keyword values.
167 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100168enum ly_stmt yin_match_keyword(struct lys_yin_parser_ctx *ctx, const char *name, size_t name_len,
Radek Krejci0f969882020-08-21 16:56:47 +0200169 const char *prefix, size_t prefix_len, enum ly_stmt parrent);
David Sedlák1bccdfa2019-06-17 15:55:27 +0200170
David Sedlákb6e65972019-06-19 10:44:13 +0200171/**
David Sedlákb1a78352019-06-28 16:16:29 +0200172 * @brief Parse instance of extension.
173 *
David Sedlákda8ffa32019-07-08 14:17:10 +0200174 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákb4e44562019-07-04 15:42:12 +0200175 * @param[in] subelem Type of the keyword this extension instance is a subelement of.
176 * @param[in] subelem_index Index of the keyword instance this extension instance is a subelement of
177 * @param[in,out] exts Extension instance to add to.
David Sedlákb1a78352019-06-28 16:16:29 +0200178 *
179 * @return LY_ERR values.
180 */
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200181LY_ERR yin_parse_extension_instance(struct lys_yin_parser_ctx *ctx, LYEXT_SUBSTMT subelem, LY_ARRAY_COUNT_TYPE subelem_index,
Radek Krejci0f969882020-08-21 16:56:47 +0200182 struct lysp_ext_instance **exts);
David Sedlákb1a78352019-06-28 16:16:29 +0200183
184/**
185 * @brief Parse yin element into generic structure.
186 *
Michal Vaskob36053d2020-03-26 15:49:30 +0100187 * @param[in,out] ctx Yin parser context for XML context, logging, and to store current state.
David Sedlák071f7662019-09-12 02:02:51 +0200188 * @param[in] parent Identification of parent element.
David Sedlákb1a78352019-06-28 16:16:29 +0200189 * @param[out] element Where the element structure should be stored.
190 *
191 * @return LY_ERR values.
192 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100193LY_ERR yin_parse_element_generic(struct lys_yin_parser_ctx *ctx, enum ly_stmt parent, struct lysp_stmt **element);
David Sedlákb1a78352019-06-28 16:16:29 +0200194
David Sedlák4f03b932019-07-26 13:01:47 +0200195/**
David Sedlák298ff6d2019-07-26 14:29:03 +0200196 * @brief Parse module element.
David Sedlák4f03b932019-07-26 13:01:47 +0200197 *
198 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlák4f03b932019-07-26 13:01:47 +0200199 * @param[out] mod Parsed module structure.
200 *
201 * @return LY_ERR values.
202 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100203LY_ERR yin_parse_mod(struct lys_yin_parser_ctx *ctx, struct lysp_module *mod);
David Sedlák4f03b932019-07-26 13:01:47 +0200204
205/**
David Sedlák298ff6d2019-07-26 14:29:03 +0200206 * @brief Parse submodule element.
207 *
208 * @param[in,out] ctx Yin parser context for logging and to store current state.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200209 * @param[in] mod_attrs Attributes of submodule element.
David Sedlákbf8a2b72019-08-14 16:48:10 +0200210 * @param[out] submod Parsed submodule structure.
David Sedlák298ff6d2019-07-26 14:29:03 +0200211 *
212 * @return LY_ERR values.
213 */
Michal Vaskob36053d2020-03-26 15:49:30 +0100214LY_ERR yin_parse_submod(struct lys_yin_parser_ctx *ctx, struct lysp_submodule *submod);
David Sedlák4f03b932019-07-26 13:01:47 +0200215
David Sedlák2b626ee2019-06-03 16:40:18 +0200216#endif /* LY_PARSER_YIN_H_*/