Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1 | /** |
Radek Krejci | aa429e4 | 2015-10-09 15:52:37 +0200 | [diff] [blame] | 2 | * @file tree_data.h |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Radek Krejci | aa429e4 | 2015-10-09 15:52:37 +0200 | [diff] [blame] | 4 | * @brief libyang representation of data trees. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 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 |
Michal Vasko | 8de098c | 2016-02-26 10:00:25 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #ifndef LY_TREE_DATA_H_ |
| 16 | #define LY_TREE_DATA_H_ |
| 17 | |
| 18 | #include <stddef.h> |
| 19 | #include <stdint.h> |
| 20 | |
Mislav Novakovic | e251a65 | 2015-09-29 08:40:12 +0200 | [diff] [blame] | 21 | #include "tree_schema.h" |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 22 | #include "xml.h" |
Mislav Novakovic | e251a65 | 2015-09-29 08:40:12 +0200 | [diff] [blame] | 23 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 24 | #ifdef __cplusplus |
| 25 | extern "C" { |
| 26 | #endif |
| 27 | |
| 28 | /** |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 29 | * @defgroup datatree Data Tree |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 30 | * @{ |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 31 | * |
| 32 | * Data structures and functions to manipulate and access instance data tree. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 33 | */ |
| 34 | |
| 35 | /** |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 36 | * @brief Data input/output formats supported by libyang [parser](@ref howtodataparsers) and |
| 37 | * [printer](@ref howtodataprinters) functions. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 38 | */ |
| 39 | typedef enum { |
| 40 | LYD_UNKNOWN, /**< unknown format, used as return value in case of error */ |
| 41 | LYD_XML, /**< XML format of the instance data */ |
Radek Krejci | 452fb95 | 2015-10-02 16:07:46 +0200 | [diff] [blame] | 42 | LYD_XML_FORMAT, /**< For input data, it is interchangeable with #LYD_XML, for output it formats XML with indentantion */ |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 43 | LYD_JSON, /**< JSON format of the instance data */ |
| 44 | } LYD_FORMAT; |
| 45 | |
| 46 | /** |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 47 | * @brief Attribute structure. |
| 48 | * |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 49 | * The structure provides information about attributes of a data element. Such attributes partially |
| 50 | * maps to annotations from draft-ietf-netmod-yang-metadata. In XML, they are represented as standard |
| 51 | * XML attrbutes. In JSON, they are represented as JSON elements starting with the '@' character |
| 52 | * (for more information, see the yang metadata draft. |
| 53 | * |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 54 | */ |
| 55 | struct lyd_attr { |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 56 | struct lyd_attr *next; /**< pointer to the next attribute of the same element */ |
| 57 | struct lys_module *module; /**< pointer to the attribute's module. |
| 58 | TODO when annotations will be supported, point to the annotation definition |
| 59 | and validate that the attribute is really defined there. Currently, we just |
| 60 | believe that it is defined in the module it says */ |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 61 | const char *name; /**< attribute name */ |
| 62 | const char *value; /**< attribute value */ |
| 63 | }; |
| 64 | |
| 65 | /** |
| 66 | * @brief node's value representation |
| 67 | */ |
| 68 | typedef union lyd_value_u { |
| 69 | const char *binary; /**< base64 encoded, NULL terminated string */ |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 70 | struct lys_type_bit **bit; /**< bitmap of pointers to the schema definition of the bit value that are set, |
| 71 | its size is always the number of defined bits in the schema */ |
Radek Krejci | 489773c | 2015-12-17 13:20:03 +0100 | [diff] [blame] | 72 | int8_t bln; /**< 0 as false, 1 as true */ |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 73 | int64_t dec64; /**< decimal64: value = dec64 / 10^fraction-digits */ |
| 74 | struct lys_type_enum *enm; /**< pointer to the schema definition of the enumeration value */ |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 75 | struct lys_ident *ident; /**< pointer to the schema definition of the identityref value */ |
Radek Krejci | 40f17b9 | 2016-02-03 14:30:43 +0100 | [diff] [blame] | 76 | struct lyd_node *instance; /**< pointer to the instance-identifier target, note that if the tree was modified, |
| 77 | the target (address) can be invalid - the pointer is correctly checked and updated |
| 78 | by lyd_validate() */ |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 79 | int8_t int8; /**< 8-bit signed integer */ |
| 80 | int16_t int16; /**< 16-bit signed integer */ |
| 81 | int32_t int32; /**< 32-bit signed integer */ |
| 82 | int64_t int64; /**< 64-bit signed integer */ |
| 83 | struct lyd_node *leafref; /**< pointer to the referenced leaf/leaflist instance in data tree */ |
| 84 | const char *string; /**< string */ |
| 85 | uint8_t uint8; /**< 8-bit unsigned integer */ |
| 86 | uint16_t uint16; /**< 16-bit signed integer */ |
| 87 | uint32_t uint32; /**< 32-bit signed integer */ |
| 88 | uint64_t uint64; /**< 64-bit signed integer */ |
| 89 | } lyd_val; |
| 90 | |
| 91 | /** |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 92 | * @defgroup validityflags Validity flags |
| 93 | * @ingroup datatree |
| 94 | * |
| 95 | * Validity flags for data nodes. |
| 96 | * |
| 97 | * @{ |
| 98 | */ |
| 99 | #define LYD_VAL_OK 0x00 /**< node is successfully validated including whole subtree */ |
| 100 | #define LYD_VAL_UNIQUE 0x01 /**< Unique value(s) changed, applicable only to ::lys_node_list data nodes */ |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 101 | #define LYD_VAL_NOT 0x1f /**< node was not validated yet */ |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 102 | /** |
| 103 | * @} |
| 104 | */ |
| 105 | |
| 106 | /** |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 107 | * @brief Generic structure for a data node, directly applicable to the data nodes defined as #LYS_CONTAINER, #LYS_LIST |
| 108 | * and #LYS_CHOICE. |
| 109 | * |
| 110 | * Completely fits to containers and choices and is compatible (can be used interchangeably except the #child member) |
| 111 | * with all other lyd_node_* structures. All data nodes are provides as ::lyd_node structure by default. |
| 112 | * According to the schema's ::lys_node#nodetype member, the specific object is supposed to be cast to |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 113 | * ::lyd_node_leaf_list or ::lyd_node_anyxml structures. This structure fits only to #LYS_CONTAINER, #LYS_LIST and |
| 114 | * #LYS_CHOICE values. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 115 | * |
| 116 | * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro. |
| 117 | */ |
| 118 | struct lyd_node { |
| 119 | struct lys_node *schema; /**< pointer to the schema definition of this node */ |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 120 | uint8_t validity:5; /**< [validity flags](@ref validityflags) */ |
| 121 | uint8_t when_status:3; /**< bit for checking if the when-stmt condition is resolved - internal use only, |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 122 | do not use this value! */ |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 123 | |
| 124 | struct lyd_attr *attr; /**< pointer to the list of attributes of this node */ |
| 125 | struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */ |
| 126 | struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is |
| 127 | never NULL. If there is no sibling node, pointer points to the node |
| 128 | itself. In case of the first node, this pointer points to the last |
| 129 | node in the list. */ |
| 130 | struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */ |
| 131 | struct lyd_node *child; /**< pointer to the first child node \note Since other lyd_node_* |
Radek Krejci | ee36089 | 2015-10-06 11:23:14 +0200 | [diff] [blame] | 132 | structures represent end nodes, this member |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 133 | is replaced in those structures. Therefore, be careful with accessing |
| 134 | this member without having information about the node type from the schema's |
| 135 | ::lys_node#nodetype member. */ |
| 136 | }; |
| 137 | |
| 138 | /** |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 139 | * @brief Structure for data nodes defined as #LYS_LEAF or #LYS_LEAFLIST. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 140 | * |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 141 | * Extension for ::lyd_node structure. It replaces the ::lyd_node#child member by |
| 142 | * three new members (#value, #value_str and #value_type) to provide |
| 143 | * information about the value. The first five members (#schema, #attr, #next, |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 144 | * #prev and #parent) are compatible with the ::lyd_node's members. |
| 145 | * |
| 146 | * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro. |
| 147 | */ |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 148 | struct lyd_node_leaf_list { |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 149 | struct lys_node *schema; /**< pointer to the schema definition of this node which is ::lys_node_leaflist |
| 150 | structure */ |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 151 | uint8_t validity:5; /**< [validity flags](@ref validityflags) */ |
| 152 | uint8_t when_status:3; /**< bit for checking if the when-stmt condition is resolved - internal use only, |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 153 | do not use this value! */ |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 154 | |
| 155 | struct lyd_attr *attr; /**< pointer to the list of attributes of this node */ |
| 156 | struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */ |
| 157 | struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is |
| 158 | never NULL. If there is no sibling node, pointer points to the node |
| 159 | itself. In case of the first node, this pointer points to the last |
| 160 | node in the list. */ |
| 161 | struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */ |
| 162 | |
| 163 | /* struct lyd_node *child; should be here, but is not */ |
| 164 | |
| 165 | /* leaflist's specific members */ |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 166 | const char *value_str; /**< string representation of value (for comparison, printing,...) */ |
Radek Krejci | 2323892 | 2015-10-27 17:13:34 +0100 | [diff] [blame] | 167 | lyd_val value; /**< node's value representation */ |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 168 | LY_DATA_TYPE value_type; /**< type of the value in the node, mainly for union to avoid repeating of type detection */ |
| 169 | }; |
| 170 | |
| 171 | /** |
| 172 | * @brief Structure for data nodes defined as #LYS_ANYXML. |
| 173 | * |
| 174 | * Extension for ::lyd_node structure - replaces the ::lyd_node#child member by new #value member. The first five |
| 175 | * members (#schema, #attr, #next, #prev and #parent) are compatible with the ::lyd_node's members. |
| 176 | * |
| 177 | * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro. |
| 178 | */ |
| 179 | struct lyd_node_anyxml { |
| 180 | struct lys_node *schema; /**< pointer to the schema definition of this node which is ::lys_node_anyxml |
| 181 | structure */ |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 182 | uint8_t validity:5; /**< [validity flags](@ref validityflags) */ |
| 183 | uint8_t when_status:3; /**< bit for checking if the when-stmt condition is resolved - internal use only, |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 184 | do not use this value! */ |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 185 | |
| 186 | struct lyd_attr *attr; /**< pointer to the list of attributes of this node */ |
| 187 | struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */ |
| 188 | struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is |
| 189 | never NULL. If there is no sibling node, pointer points to the node |
| 190 | itself. In case of the first node, this pointer points to the last |
| 191 | node in the list. */ |
| 192 | struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */ |
| 193 | |
| 194 | /* struct lyd_node *child; should be here, but is not */ |
| 195 | |
| 196 | /* anyxml's specific members */ |
| 197 | struct lyxml_elem *value; /**< anyxml name is the root element of value! */ |
| 198 | }; |
| 199 | |
| 200 | /** |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 201 | * @defgroup parseroptions Data parser options |
| 202 | * @ingroup datatree |
| 203 | * |
| 204 | * Various options to change the data tree parsers behavior. |
| 205 | * |
| 206 | * Default behavior: |
| 207 | * - in case of XML, parser reads all data from its input (file, memory, XML tree) including the case of not well-formed |
| 208 | * XML document (multiple top-level elements) and if there is an unknown element, it is skipped including its subtree |
| 209 | * (see the next point). This can be changed by the #LYD_OPT_NOSIBLINGS option which make parser to read only a single |
| 210 | * tree (with a single root element) from its input. |
| 211 | * - parser silently ignores the data without a matching node in schema trees. If the caller want to stop |
| 212 | * parsing in case of presence of unknown data, the #LYD_OPT_STRICT can be used. The strict mode is useful for |
| 213 | * NETCONF servers, since NETCONF clients should always send data according to the capabilities announced by the server. |
| 214 | * On the other hand, the default non-strict mode is useful for clients receiving data from NETCONF server since |
| 215 | * clients are not required to understand everything the server does. Of course, the optimal strategy for clients is |
| 216 | * to use filtering to get only the required data. Having an unknown element of the known namespace is always an error. |
| 217 | * The behavior can be changed by #LYD_OPT_STRICT option. |
| 218 | * - using obsolete statements (status set to obsolete) just generates a warning, but the processing continues. The |
| 219 | * behavior can be changed by #LYD_OPT_OBSOLETE option. |
| 220 | * - parser expects that the provided data provides complete datastore content (both the configuration and state data) |
| 221 | * and performs data validation according to all YANG rules. This can be a problem in case of representing NETCONF's |
| 222 | * subtree filter data, edit-config's data or other type of data set - such data do not represent a complete data set |
| 223 | * and some of the validation rules can fail. Therefore there are other options (within lower 8 bits) to make parser |
| 224 | * to accept such a data. |
Radek Krejci | 7b4309c | 2016-03-23 10:30:29 +0100 | [diff] [blame^] | 225 | * - for validation, parser needs to add default nodes into the data tree. By default, these additional (implicit) |
| 226 | * nodes are removed before the parser returns. However, if caller use one of the LYD_WD_* option, the default nodes |
| 227 | * added by parser are kept in the resulting tree or even the explicit nodes with the default values can be removed |
| 228 | * (in case of #LYD_WD_TRIM option). |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 229 | * @{ |
| 230 | */ |
| 231 | |
| 232 | #define LYD_OPT_DATA 0x00 /**< Default type of data - complete datastore content with configuration as well as |
| 233 | state data. */ |
| 234 | #define LYD_OPT_CONFIG 0x01 /**< A configuration datastore - complete datastore without state data. |
| 235 | Validation modifications: |
| 236 | - status data are not allowed */ |
| 237 | #define LYD_OPT_GET 0x02 /**< Data content from a NETCONF reply message to the NETCONF \<get\> operation. |
| 238 | Validation modifications: |
| 239 | - mandatory nodes can be omitted |
| 240 | - leafrefs and instance-identifier are not resolved |
| 241 | - list's keys/unique nodes are not required (so duplication is not checked) */ |
| 242 | #define LYD_OPT_GETCONFIG 0x04 /**< Data content from a NETCONF reply message to the NETCONF \<get-config\> operation |
| 243 | Validation modifications: |
| 244 | - mandatory nodes can be omitted |
| 245 | - leafrefs and instance-identifier are not resolved |
| 246 | - list's keys/unique nodes are not required (so duplication is not checked) |
| 247 | - status data are not allowed */ |
| 248 | #define LYD_OPT_EDIT 0x08 /**< Content of the NETCONF \<edit-config\>'s config element. |
| 249 | Validation modifications: |
| 250 | - mandatory nodes can be omitted |
| 251 | - leafrefs and instance-identifier are not resolved |
| 252 | - status data are not allowed */ |
| 253 | #define LYD_OPT_RPC 0x10 /**< Data represents RPC's input parameters. */ |
| 254 | #define LYD_OPT_RPCREPLY 0x20 /**< Data represents RPC's output parameters (maps to NETCONF <rpc-reply> data). */ |
| 255 | #define LYD_OPT_NOTIF 0x40 /**< Data represents an event notification data. */ |
| 256 | #define LYD_OPT_FILTER 0x80 /**< Data represents NETCONF subtree filter. Validation modifications: |
| 257 | - leafs/leaf-lists with no data are allowed (even not allowed e.g. by length restriction) |
| 258 | - multiple instances of container/leaf/.. are allowed |
| 259 | - list's keys/unique nodes are not required |
| 260 | - mandatory nodes can be omitted |
| 261 | - leafrefs and instance-identifier are not resolved |
| 262 | - data from different choice's branches are allowed */ |
| 263 | #define LYD_OPT_TYPEMASK 0xff /**< Mask to filter data type options. Always only a single data type option (only |
| 264 | single bit from the lower 8 bits) can be set. */ |
| 265 | |
| 266 | #define LYD_OPT_STRICT 0x0100 /**< Instead of silent ignoring data without schema definition, raise an error. */ |
| 267 | #define LYD_OPT_DESTRUCT 0x0200 /**< Free the provided XML tree during parsing the data. With this option, the |
| 268 | provided XML tree is affected and all succesfully parsed data are freed. |
| 269 | This option is applicable only to lyd_parse_xml() function. */ |
| 270 | #define LYD_OPT_OBSOLETE 0x0400 /**< Raise an error when an obsolete statement (status set to obsolete) is used. */ |
| 271 | #define LYD_OPT_NOSIBLINGS 0x0800 /**< Parse only a single XML tree from the input. This option applies only to |
| 272 | XML input data. */ |
Radek Krejci | 93fab98 | 2016-02-03 15:58:19 +0100 | [diff] [blame] | 273 | #define LYD_OPT_TRUSTED 0x1000 /**< Data comes from a trusted source and it is not needed to validate them. Data |
| 274 | are connected with the schema, but the most validation checks (mandatory nodes, |
| 275 | list instance uniqueness, etc.) are not performed. This option does not make |
| 276 | sense for lyd_validate() so it is ignored by this function. */ |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 277 | #define LYD_OPT_NOAUTODEL 0x2000 /**< Avoid automatic delete of subtrees with false when-stmt condition. The flag is |
| 278 | applicable only in combination with LYD_OPT_DATA and LYD_OPT_CONFIG flags. |
| 279 | If used, libyang generates validation error instead of silently removing the |
| 280 | constrained subtree. */ |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 281 | |
Radek Krejci | 7b4309c | 2016-03-23 10:30:29 +0100 | [diff] [blame^] | 282 | #define LYD_WD_MASK 0xF0000 /**< Mask for with-defaults modes */ |
| 283 | #define LYD_WD_TRIM 0x10000 /**< Remove all nodes with the value equal to their default value */ |
| 284 | #define LYD_WD_ALL 0x20000 /**< Explicitly add all missing nodes with their default value */ |
| 285 | #define LYD_WD_ALL_TAG 0x40000 /**< Same as LYD_WD_ALL but also adds attribute 'default' with value 'true' to |
| 286 | all nodes that has its default value. The 'default' attribute has namespace: |
| 287 | urn:ietf:params:xml:ns:netconf:default:1.0 and thus the attributes are |
| 288 | created only when the ietf-netconf-with-defaults module is present in libyang |
| 289 | context. */ |
| 290 | #define LYD_WD_IMPL_TAG 0x80000 /**< Same as LYD_WD_ALL_TAG but the attributes are added only to the nodes that |
| 291 | are being created and were not part of the original data tree despite their |
| 292 | value is equal to their default value. There is the same limitation regarding |
| 293 | the presence of ietf-netconf-with-defaults module in libyang context. */ |
| 294 | |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 295 | /**@} parseroptions */ |
| 296 | |
| 297 | /** |
| 298 | * @brief Parse (and validate according to appropriate schema from the given context) data. |
| 299 | * |
| 300 | * In case of LY_XML format, the data string is parsed completely. It means that when it contains |
| 301 | * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The |
| 302 | * returned data node is a root of the first tree with other trees connected via the next pointer. |
| 303 | * This behavior can be changed by #LYD_OPT_NOSIBLINGS option. |
| 304 | * |
| 305 | * @param[in] ctx Context to connect with the data tree being built here. |
| 306 | * @param[in] data Serialized data in the specified format. |
| 307 | * @param[in] format Format of the input data to be parsed. |
| 308 | * @param[in] options Parser options, see @ref parseroptions. |
| 309 | * @param[in] ... Additional argument must be supplied when #LYD_OPT_RPCREPLY value is specified in \p options. The |
| 310 | * argument is supposed to provide pointer to the RPC schema node for the reply's request |
| 311 | * (const struct ::lys_node* rpc). |
| 312 | * @return Pointer to the built data tree or NULL in case of empty \p data. To free the returned structure, |
| 313 | * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error, |
| 314 | * #ly_errno contains appropriate error code (see #LY_ERR). |
| 315 | */ |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 316 | struct lyd_node *lyd_parse_mem(struct ly_ctx *ctx, const char *data, LYD_FORMAT format, int options, ...); |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 317 | |
| 318 | /** |
| 319 | * @brief Read data from the given file descriptor. |
| 320 | * |
| 321 | * \note Current implementation supports only reading data from standard (disk) file, not from sockets, pipes, etc. |
| 322 | * |
| 323 | * In case of LY_XML format, the file content is parsed completely. It means that when it contains |
| 324 | * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The |
| 325 | * returned data node is a root of the first tree with other trees connected via the next pointer. |
| 326 | * This behavior can be changed by #LYD_OPT_NOSIBLINGS option. |
| 327 | * |
| 328 | * @param[in] ctx Context to connect with the data tree being built here. |
| 329 | * @param[in] fd The standard file descriptor of the file containing the data tree in the specified format. |
| 330 | * @param[in] format Format of the input data to be parsed. |
| 331 | * @param[in] options Parser options, see @ref parseroptions. |
| 332 | * @param[in] ... Additional argument must be supplied when #LYD_OPT_RPCREPLY value is specified in \p options. The |
| 333 | * argument is supposed to provide pointer to the RPC schema node for the reply's request |
| 334 | * (const struct ::lys_node* rpc). |
| 335 | * @return Pointer to the built data tree or NULL in case of empty file. To free the returned structure, |
| 336 | * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error, |
| 337 | * #ly_errno contains appropriate error code (see #LY_ERR). |
| 338 | */ |
| 339 | struct lyd_node *lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options, ...); |
| 340 | |
| 341 | /** |
| 342 | * @brief Read data from the given file path. |
| 343 | * |
| 344 | * In case of LY_XML format, the file content is parsed completely. It means that when it contains |
| 345 | * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The |
| 346 | * returned data node is a root of the first tree with other trees connected via the next pointer. |
| 347 | * This behavior can be changed by #LYD_OPT_NOSIBLINGS option. |
| 348 | * |
| 349 | * @param[in] ctx Context to connect with the data tree being built here. |
| 350 | * @param[in] path Path to the file containing the data tree in the specified format. |
| 351 | * @param[in] format Format of the input data to be parsed. |
| 352 | * @param[in] options Parser options, see @ref parseroptions. |
| 353 | * @param[in] ... Additional argument must be supplied when #LYD_OPT_RPCREPLY value is specified in \p options. The |
| 354 | * argument is supposed to provide pointer to the RPC schema node for the reply's request |
| 355 | * (const struct ::lys_node* rpc). |
| 356 | * @return Pointer to the built data tree or NULL in case of empty file. To free the returned structure, |
| 357 | * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error, |
| 358 | * #ly_errno contains appropriate error code (see #LY_ERR). |
| 359 | */ |
| 360 | struct lyd_node *lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options, ...); |
| 361 | |
| 362 | /** |
| 363 | * @brief Parse (and validate according to appropriate schema from the given context) XML tree. |
| 364 | * |
| 365 | * The output data tree is parsed from the given XML tree previously parsed by one of the |
| 366 | * lyxml_read* functions. |
| 367 | * |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 368 | * If there are some sibling elements of the \p root (data were read with #LYXML_PARSE_MULTIROOT option |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 369 | * or the provided root is a root element of a subtree), all the sibling nodes (previous as well as |
| 370 | * following) are processed as well. The returned data node is a root of the first tree with other |
| 371 | * trees connected via the next pointer. This behavior can be changed by #LYD_OPT_NOSIBLINGS option. |
| 372 | * |
| 373 | * When the function is used with #LYD_OPT_DESTRUCT, all the successfully parsed data including the |
| 374 | * XML \p root and all its siblings (if #LYD_OPT_NOSIBLINGS is not used) are freed. Only with |
| 375 | * #LYD_OPT_DESTRUCT option the \p root pointer is changed - if all the data are parsed, it is set |
| 376 | * to NULL, otherwise it will hold the XML tree without the successfully parsed elements. |
| 377 | * |
| 378 | * The context must be the same as the context used to parse XML tree by lyxml_read* function. |
| 379 | * |
| 380 | * @param[in] ctx Context to connect with the data tree being built here. |
| 381 | * @param[in,out] root XML tree to parse (convert) to data tree. By default, parser do not change the XML tree. However, |
| 382 | * when #LYD_OPT_DESTRUCT is specified in \p options, parser frees all successfully parsed data. |
| 383 | * @param[in] options Parser options, see @ref parseroptions. |
| 384 | * @param[in] ... Additional argument must be supplied when #LYD_OPT_RPCREPLY value is specified in \p options. The |
| 385 | * argument is supposed to provide pointer to the RPC schema node for the reply's request |
| 386 | * (const struct ::lys_node* rpc). |
| 387 | * @return Pointer to the built data tree or NULL in case of empty \p root. To free the returned structure, |
| 388 | * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error, |
| 389 | * #ly_errno contains appropriate error code (see #LY_ERR). |
| 390 | */ |
| 391 | struct lyd_node *lyd_parse_xml(struct ly_ctx *ctx, struct lyxml_elem **root, int options,...); |
| 392 | |
| 393 | /** |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 394 | * @brief Create a new container node in a data tree. |
| 395 | * |
| 396 | * @param[in] parent Parent node for the node being created. NULL in case of creating top level element. |
Radek Krejci | ee36089 | 2015-10-06 11:23:14 +0200 | [diff] [blame] | 397 | * @param[in] module Module with the node being created. |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 398 | * @param[in] name Schema node name of the new data node. The node can be #LYS_CONTAINER, #LYS_LIST, |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 399 | * #LYS_NOTIF, or #LYS_RPC. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 400 | * @return New node, NULL on error. |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 401 | */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 402 | struct lyd_node *lyd_new(struct lyd_node *parent, const struct lys_module *module, const char *name); |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 403 | |
| 404 | /** |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 405 | * @brief Create a new leaf or leaflist node in a data tree with a string value that is converted to |
| 406 | * the actual value. |
| 407 | * |
| 408 | * @param[in] parent Parent node for the node being created. NULL in case of creating top level element. |
Radek Krejci | ee36089 | 2015-10-06 11:23:14 +0200 | [diff] [blame] | 409 | * @param[in] module Module with the node being created. |
| 410 | * @param[in] name Schema node name of the new data node. |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 411 | * @param[in] val_str String form of the value of the node being created. In case the type is #LY_TYPE_INST |
| 412 | * or #LY_TYPE_IDENT, JSON node-id format is expected (nodes are prefixed with module names, not XML namespaces). |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 413 | * @return New node, NULL on error. |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 414 | */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 415 | struct lyd_node *lyd_new_leaf(struct lyd_node *parent, const struct lys_module *module, const char *name, |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 416 | const char *val_str); |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 417 | |
| 418 | /** |
Radek Krejci | b9b4d00 | 2016-01-18 13:08:51 +0100 | [diff] [blame] | 419 | * @brief Change value of a leaf node. |
| 420 | * |
| 421 | * Despite the prototype allows to provide a leaflist node as \p leaf parameter, only leafs are accepted. |
| 422 | * |
| 423 | * @param[in] leaf A leaf node to change. |
| 424 | * @param[in] val_str String form of the new value to be set to the \p leaf. In case the type is #LY_TYPE_INST |
| 425 | * or #LY_TYPE_IDENT, JSON node-id format is expected (nodes are prefixed with module names, not XML namespaces). |
| 426 | * @return 0 on success, non-zero on error. |
| 427 | */ |
| 428 | int lyd_change_leaf(struct lyd_node_leaf_list *leaf, const char *val_str); |
| 429 | |
| 430 | /** |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 431 | * @brief Create a new anyxml node in a data tree. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 432 | * |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 433 | * @param[in] parent Parent node for the node being created. NULL in case of creating top level element. |
Radek Krejci | ee36089 | 2015-10-06 11:23:14 +0200 | [diff] [blame] | 434 | * @param[in] module Module with the node being created. |
| 435 | * @param[in] name Schema node name of the new data node. |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 436 | * @param[in] val_xml Value of the node being created. Must be a well-formed XML. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 437 | * @return New node, NULL on error. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 438 | */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 439 | struct lyd_node *lyd_new_anyxml(struct lyd_node *parent, const struct lys_module *module, const char *name, |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 440 | const char *val_xml); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 441 | |
| 442 | /** |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame] | 443 | * @brief Create a new container node in a data tree, whose schema parent is #LYS_OUTPUT. |
Michal Vasko | 0df122f | 2015-12-14 13:38:21 +0100 | [diff] [blame] | 444 | * |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame] | 445 | * @param[in] schema Schema node of the container. |
Michal Vasko | 0df122f | 2015-12-14 13:38:21 +0100 | [diff] [blame] | 446 | * @return New node, NULL on error. |
| 447 | */ |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame] | 448 | struct lyd_node *lyd_output_new(const struct lys_node *schema); |
| 449 | |
| 450 | /** |
| 451 | * @brief Create a new leaf or leaflist node in a data tree, whose schema parent is #LYS_OUTPUT. |
| 452 | * |
| 453 | * @param[in] schema Schema node of the leaf. |
| 454 | * @param[in] val_str String form of the value of the node being created. In case the type is #LY_TYPE_INST |
| 455 | * or #LY_TYPE_IDENT, JSON node-id format is expected (nodes are prefixed with module names, not XML namespaces). |
| 456 | * @return New node, NULL on error. |
| 457 | */ |
| 458 | struct lyd_node *lyd_output_new_leaf(const struct lys_node *schema, const char *val_str); |
| 459 | |
| 460 | /** |
| 461 | * @brief Create a new anyxml node in a data tree, whose schema parent is #LYS_OUTPUT. |
| 462 | * |
| 463 | * @param[in] schema Schema node of the leaf. |
| 464 | * @param[in] val_xml Value of the node being created. Must be a well-formed XML. |
| 465 | * @return New node, NULL on error. |
| 466 | */ |
| 467 | struct lyd_node *lyd_output_new_anyxml(const struct lys_node *schema, const char *val_xml); |
Michal Vasko | 0df122f | 2015-12-14 13:38:21 +0100 | [diff] [blame] | 468 | |
| 469 | /** |
Michal Vasko | f529928 | 2016-03-16 13:32:02 +0100 | [diff] [blame] | 470 | * @defgroup pathoptions Data path creation options |
| 471 | * @ingroup datatree |
| 472 | * |
| 473 | * Various options to change lyd_new_path() behavior. |
| 474 | * |
| 475 | * Default behavior: |
| 476 | * - only the last node in the path is to be created. If any parents do not exist, returns an error. |
| 477 | * - if the target node already exists, an error is returned. |
| 478 | * @{ |
| 479 | */ |
| 480 | |
| 481 | #define LYD_PATH_OPT_UPDATE 0x01 /**< If the target node exists and is a leaf, it is updated with the new value. */ |
| 482 | #define LYD_PATH_OPT_RECURSIVE 0x02 /**< If any parents of the target node do not exist, create them, too. */ |
| 483 | |
| 484 | /** @} pathoptions */ |
| 485 | |
| 486 | /** |
| 487 | * @brief Create a new data node based on a simple XPath. |
| 488 | * |
| 489 | * @param[in] data_tree Existing data tree to add to/modify. Can be NULL. |
| 490 | * @param[in] ctx Context to use. Mandatory if \p data_tree is NULL. |
| 491 | * @param[in] path Simple schema XPath of the new node. It can contain only simple node addressing with optional |
| 492 | * module names as prefixes. List nodes must have predicates, one for each list key in the correct order and |
Michal Vasko | 0f14ba6 | 2016-03-21 15:38:11 +0100 | [diff] [blame] | 493 | * with it's value as well, see @ref howtoxpath. |
Michal Vasko | f529928 | 2016-03-16 13:32:02 +0100 | [diff] [blame] | 494 | * @param[in] value Value of the new leaf/lealf-list. If creating other nodes of other types, set to NULL. |
| 495 | * @param[in] options Bitmask of options flags, see @ref pathoptions. |
| 496 | * @return First created node, NULL on error. |
| 497 | */ |
| 498 | struct lyd_node *lyd_new_path(struct lyd_node *data_tree, struct ly_ctx *ctx, const char *path, const char *value, int options); |
| 499 | |
| 500 | /** |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 501 | * @brief Create a copy of the specified data tree \p node. Namespaces are copied as needed, |
| 502 | * schema references are kept the same. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 503 | * |
| 504 | * @param[in] node Data tree node to be duplicated. |
| 505 | * @param[in] recursive 1 if all children are supposed to be also duplicated. |
| 506 | * @return Created copy of the provided data \p node. |
| 507 | */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 508 | struct lyd_node *lyd_dup(const struct lyd_node *node, int recursive); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 509 | |
| 510 | /** |
| 511 | * @brief Insert the \p node element as child to the \p parent element. The \p node is inserted as a last child of the |
| 512 | * \p parent. |
| 513 | * |
| 514 | * If the node is part of some other tree, it is automatically unlinked. |
| 515 | * If the node is the first node of a node list (with no parent), all |
| 516 | * the subsequent nodes are also inserted. |
| 517 | * |
| 518 | * @param[in] parent Parent node for the \p node being inserted. |
| 519 | * @param[in] node The node being inserted. |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 520 | * @return 0 on success, nonzero in case of error, e.g. when the node is being inserted to an inappropriate place |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 521 | * in the data tree. |
| 522 | */ |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 523 | int lyd_insert(struct lyd_node *parent, struct lyd_node *node); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 524 | |
| 525 | /** |
Michal Vasko | 3f7dba1 | 2015-10-15 13:09:27 +0200 | [diff] [blame] | 526 | * @brief Insert the \p node element after the \p sibling element. If \p node and \p siblings are already |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 527 | * siblings (just moving \p node position), skip validation. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 528 | * |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 529 | * @param[in] sibling The data tree node before which the \p node will be inserted. |
Radek Krejci | 20a5f29 | 2016-02-09 15:04:49 +0100 | [diff] [blame] | 530 | * @param[in] node The data tree node to be inserted. If the node is connected somewhere, it is unlinked first. |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 531 | * @return 0 on success, nonzero in case of error, e.g. when the node is being inserted to an inappropriate place |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 532 | * in the data tree. |
| 533 | */ |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 534 | int lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 535 | |
| 536 | /** |
Radek Krejci | 20a5f29 | 2016-02-09 15:04:49 +0100 | [diff] [blame] | 537 | * @brief Insert the \p node element after the \p sibling element. If \p node and \p siblings are already |
| 538 | * siblings (just moving \p node position), skip validation. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 539 | * |
Michal Vasko | 3f7dba1 | 2015-10-15 13:09:27 +0200 | [diff] [blame] | 540 | * @param[in] sibling The data tree node before which the \p node will be inserted. If \p node and \p siblings |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 541 | * are already siblings (just moving \p node position), skip validation. |
Radek Krejci | 20a5f29 | 2016-02-09 15:04:49 +0100 | [diff] [blame] | 542 | * @param[in] node The data tree node to be inserted. If the node is connected somewhere, it is unlinked first. |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 543 | * @return 0 on success, nonzero in case of error, e.g. when the node is being inserted to an inappropriate place |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 544 | * in the data tree. |
| 545 | */ |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 546 | int lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node); |
| 547 | |
| 548 | /** |
Michal Vasko | 105cef1 | 2016-02-04 12:06:26 +0100 | [diff] [blame] | 549 | * @brief Search in the given data for instances of nodes matching the provided XPath expression. |
| 550 | * |
| 551 | * The \p data is used to find the data root and function then searches in the whole tree and all sibling trees. |
Michal Vasko | 7fdf9b3 | 2016-03-01 15:59:48 +0100 | [diff] [blame] | 552 | * The XPath expression is evaluated on data -> skip all non-data nodes (input, output, choice, case). |
Michal Vasko | 105cef1 | 2016-02-04 12:06:26 +0100 | [diff] [blame] | 553 | * |
Michal Vasko | 7fdf9b3 | 2016-03-01 15:59:48 +0100 | [diff] [blame] | 554 | * Expr examples: |
| 555 | * "/ietf-yang-library:modules-state/module[name = 'ietf-yang-library']/namespace" |
| 556 | * "/ietf-netconf:get-config/source" |
| 557 | * |
| 558 | * @param[in] data Node in the data tree considered the context node. If the node is a configuration one, |
Michal Vasko | 105cef1 | 2016-02-04 12:06:26 +0100 | [diff] [blame] | 559 | * any state nodes in its tree are not accessible! |
| 560 | * @param[in] expr XPath expression filtering the matching nodes. |
| 561 | * @return Set of found data nodes (use dset member of ::ly_set). If no nodes are matching \p expr or the result |
| 562 | * would be a number, a string, or a boolean, the returned set is empty. In case of an error, NULL is returned. |
| 563 | */ |
| 564 | struct ly_set *lyd_get_node(const struct lyd_node *data, const char *expr); |
| 565 | |
| 566 | /** |
Radek Krejci | c5b6b91 | 2016-01-18 16:35:35 +0100 | [diff] [blame] | 567 | * @brief Search in the given data for instances of the provided schema node. |
| 568 | * |
| 569 | * The \p data is used to find the data root and function then searches in the whole tree and all sibling trees. |
| 570 | * |
| 571 | * @param[in] data A node in the data tree to search. |
| 572 | * @param[in] schema Schema node of the data nodes caller want to find. |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 573 | * @return Set of found data nodes (use dset member of ::ly_set). If no data node is found, the returned set is empty. |
Radek Krejci | c5b6b91 | 2016-01-18 16:35:35 +0100 | [diff] [blame] | 574 | * In case of error, NULL is returned. |
| 575 | */ |
Michal Vasko | 105cef1 | 2016-02-04 12:06:26 +0100 | [diff] [blame] | 576 | struct ly_set *lyd_get_node2(const struct lyd_node *data, const struct lys_node *schema); |
Radek Krejci | c5b6b91 | 2016-01-18 16:35:35 +0100 | [diff] [blame] | 577 | |
| 578 | /** |
Michal Vasko | 6a1ab6f | 2016-02-04 12:08:11 +0100 | [diff] [blame] | 579 | * @brief Get all key nodes of a \p list instance. |
| 580 | * |
| 581 | * @param[in] list List node, whose keys will be searched for. |
| 582 | * @return Set of list keys (use dset member of ::ly_set). If no keys are found/defined, the returned set is empty. |
| 583 | * In case of error, NULL is returned. |
| 584 | */ |
| 585 | struct ly_set *lyd_get_list_keys(const struct lyd_node *list); |
| 586 | |
| 587 | /** |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 588 | * @brief Validate \p node data subtree. |
| 589 | * |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 590 | * @param[in, out] node Data tree to be validated. In case the options includes #LYD_OPT_AUTODEL, libyang can modify the |
| 591 | * provided tree including the root \p node. |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 592 | * @param[in] options Options for the inserting data to the target data tree options, see @ref parseroptions. |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 593 | * @param[in] ... libyang context for the data (used only in case the \p node is NULL, so in case of checking empty data tree) |
| 594 | * @return 0 on success (if options include #LYD_OPT_FILTER, some nodes can be deleted as an |
Radek Krejci | 15c733d | 2016-01-27 16:31:17 +0100 | [diff] [blame] | 595 | * optimization, which can have a bad consequences when the \p node stores a subtree instead of a tree with |
| 596 | * a top-level node(s)), nonzero in case of an error. |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 597 | */ |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 598 | int lyd_validate(struct lyd_node **node, int options, ...); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 599 | |
| 600 | /** |
Radek Krejci | 7b4309c | 2016-03-23 10:30:29 +0100 | [diff] [blame^] | 601 | * @brief Add default nodes into the data tree. |
| 602 | * |
| 603 | * @param[in] ctx Optional parameter. If provided, default nodes from all modules in the context will be added (so it |
| 604 | * has no effect for #LYD_WD_TRIM). If NULL, only the modules explicitly mentioned in data tree are |
| 605 | * taken into account. |
| 606 | * @param[in] root Data tree root. In case of #LYD_WD_TRIM the data tree can be modified so the root can be changed or |
| 607 | * removed. In other modes and with empty data tree, new default nodes can be created so the root pointer |
| 608 | * will contain/return the newly created data tree. |
| 609 | * @param[in] options Options for the inserting data to the target data tree options, see @ref parseroptions. The |
| 610 | * LYD_WD_* options are used to select functionality: |
| 611 | * - #LYD_WD_TRIM - remove all nodes that have value equal to their default value |
| 612 | * - #LYD_WD_ALL - add default nodes |
| 613 | * - #LYD_WD_ALL_TAG - add default nodes and add attribute 'default' with value 'true' to all nodes having their default value |
| 614 | * - #LYD_WD_IMPL_TAG - add default nodes, but add attribute 'default' only to the added nodes |
| 615 | * @note The *_TAG modes require to have ietf-netconf-with-defaults module in the context of the data tree. |
| 616 | * @return EXIT_SUCCESS ot EXIT_FAILURE |
| 617 | */ |
| 618 | int lyd_wd_add(struct ly_ctx *ctx, struct lyd_node **root, int options); |
| 619 | |
| 620 | /** |
Michal Vasko | 55f60be | 2015-10-14 13:12:58 +0200 | [diff] [blame] | 621 | * @brief Unlink the specified data subtree. All referenced namespaces are copied. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 622 | * |
| 623 | * Note, that the node's connection with the schema tree is kept. Therefore, in case of |
| 624 | * reconnecting the node to a data tree using lyd_paste() it is necessary to paste it |
| 625 | * to the appropriate place in the data tree following the schema. |
| 626 | * |
| 627 | * @param[in] node Data tree node to be unlinked (together with all children). |
| 628 | * @return 0 for success, nonzero for error |
| 629 | */ |
| 630 | int lyd_unlink(struct lyd_node *node); |
| 631 | |
| 632 | /** |
| 633 | * @brief Free (and unlink) the specified data (sub)tree. |
| 634 | * |
| 635 | * @param[in] node Root of the (sub)tree to be freed. |
| 636 | */ |
| 637 | void lyd_free(struct lyd_node *node); |
| 638 | |
| 639 | /** |
Radek Krejci | 8146840 | 2016-01-07 13:52:40 +0100 | [diff] [blame] | 640 | * @brief Free (and unlink) the specified data (sub)tree and all its siblings (preceding as well as following). |
| 641 | * |
| 642 | * @param[in] node One of the siblings root element of the (sub)trees to be freed. |
| 643 | */ |
| 644 | void lyd_free_withsiblings(struct lyd_node *node); |
| 645 | |
| 646 | /** |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 647 | * @brief Insert attribute into the data node. |
| 648 | * |
| 649 | * @param[in] parent Data node where to place the attribute |
Radek Krejci | 70ecd72 | 2016-03-21 09:04:00 +0100 | [diff] [blame] | 650 | * @param[in] mod An alternative way to specify attribute's module (namespace) used in case the \p name does |
| 651 | * not include prefix. If neither prefix in the \p name nor mod is specified, the attribute's |
| 652 | * module is inherited from the \p parent node. It is not allowed to have attributes with no |
| 653 | * module (namespace). |
| 654 | * @param[in] name Attribute name. The string can include the attribute's module (namespace) as the name's |
| 655 | * prefix (prefix:name). Prefix must be the name of one of the schema in the \p parent's context. |
| 656 | * If the prefix is not specified, the \p mod parameter is used. If neither of these parameters is |
| 657 | * usable, attribute inherits module (namespace) from the \p parent node. It is not allowed to |
| 658 | * have attributes with no module (namespace). |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 659 | * @param[in] value Attribute value |
| 660 | * @return pointer to the created attribute (which is already connected in \p parent) or NULL on error. |
| 661 | */ |
Radek Krejci | 70ecd72 | 2016-03-21 09:04:00 +0100 | [diff] [blame] | 662 | struct lyd_attr *lyd_insert_attr(struct lyd_node *parent, const struct lys_module *mod, const char *name, |
| 663 | const char *value); |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 664 | |
| 665 | /** |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 666 | * @brief Destroy data attribute |
| 667 | * |
| 668 | * If the attribute to destroy is a member of a node attribute list, it is necessary to |
| 669 | * provide the node itself as \p parent to keep the list consistent. |
| 670 | * |
| 671 | * @param[in] ctx Context where the attribute was created (usually it is the context of the \p parent) |
| 672 | * @param[in] parent Parent node where the attribute is placed |
| 673 | * @param[in] attr Attribute to destroy |
| 674 | * @param[in] recursive Zero to destroy only the attribute, non-zero to destroy also all the subsequent attributes |
| 675 | * in the list. |
| 676 | */ |
| 677 | void lyd_free_attr(struct ly_ctx *ctx, struct lyd_node *parent, struct lyd_attr *attr, int recursive); |
| 678 | |
| 679 | /** |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 680 | * @brief Print data tree in the specified format. |
| 681 | * |
| 682 | * Same as lyd_print(), but it allocates memory and store the data into it. |
| 683 | * It is up to caller to free the returned string by free(). |
| 684 | * |
| 685 | * @param[out] strp Pointer to store the resulting dump. |
| 686 | * @param[in] root Root node of the data tree to print. It can be actually any (not only real root) |
| 687 | * node of the data tree to print the specific subtree. |
| 688 | * @param[in] format Data output format. |
| 689 | * @param[in] options [printer flags](@ref printerflags). |
| 690 | * @return 0 on success, 1 on failure (#ly_errno is set). |
| 691 | */ |
| 692 | int lyd_print_mem(char **strp, const struct lyd_node *root, LYD_FORMAT format, int options); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 693 | |
| 694 | /** |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 695 | * @brief Print data tree in the specified format. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 696 | * |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 697 | * Same as lyd_print(), but output is written into the specified file descriptor. |
| 698 | * |
| 699 | * @param[in] root Root node of the data tree to print. It can be actually any (not only real root) |
| 700 | * node of the data tree to print the specific subtree. |
| 701 | * @param[in] fd File descriptor where to print the data. |
| 702 | * @param[in] format Data output format. |
| 703 | * @param[in] options [printer flags](@ref printerflags). |
| 704 | * @return 0 on success, 1 on failure (#ly_errno is set). |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 705 | */ |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 706 | int lyd_print_fd(int fd, const struct lyd_node *root, LYD_FORMAT format, int options); |
| 707 | |
| 708 | /** |
| 709 | * @brief Print data tree in the specified format. |
| 710 | * |
| 711 | * To write data into a file descriptor, use lyd_print_fd(). |
| 712 | * |
| 713 | * @param[in] root Root node of the data tree to print. It can be actually any (not only real root) |
| 714 | * node of the data tree to print the specific subtree. |
| 715 | * @param[in] f File stream where to print the data. |
| 716 | * @param[in] format Data output format. |
| 717 | * @param[in] options [printer flags](@ref printerflags). |
| 718 | * @return 0 on success, 1 on failure (#ly_errno is set). |
| 719 | */ |
| 720 | int lyd_print_file(FILE *f, const struct lyd_node *root, LYD_FORMAT format, int options); |
| 721 | |
| 722 | /** |
| 723 | * @brief Print data tree in the specified format. |
| 724 | * |
| 725 | * Same as lyd_print(), but output is written via provided callback. |
| 726 | * |
| 727 | * @param[in] root Root node of the data tree to print. It can be actually any (not only real root) |
| 728 | * node of the data tree to print the specific subtree. |
| 729 | * @param[in] writeclb Callback function to write the data (see write(1)). |
| 730 | * @param[in] arg Optional caller-specific argument to be passed to the \p writeclb callback. |
| 731 | * @param[in] format Data output format. |
| 732 | * @param[in] options [printer flags](@ref printerflags). |
| 733 | * @return 0 on success, 1 on failure (#ly_errno is set). |
| 734 | */ |
| 735 | int lyd_print_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg, |
| 736 | const struct lyd_node *root, LYD_FORMAT format, int options); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 737 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 738 | /**@} */ |
| 739 | |
| 740 | #ifdef __cplusplus |
| 741 | } |
| 742 | #endif |
| 743 | |
| 744 | #endif /* LY_TREE_DATA_H_ */ |