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 | |
Michal Vasko | fcd974b | 2017-08-22 10:17:49 +0200 | [diff] [blame] | 21 | #include "libyang.h" |
Mislav Novakovic | e251a65 | 2015-09-29 08:40:12 +0200 | [diff] [blame] | 22 | #include "tree_schema.h" |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 23 | #include "xml.h" |
Mislav Novakovic | e251a65 | 2015-09-29 08:40:12 +0200 | [diff] [blame] | 24 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 25 | #ifdef __cplusplus |
| 26 | extern "C" { |
| 27 | #endif |
| 28 | |
| 29 | /** |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 30 | * @defgroup datatree Data Tree |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 31 | * @{ |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 32 | * |
| 33 | * Data structures and functions to manipulate and access instance data tree. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 34 | */ |
| 35 | |
| 36 | /** |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 37 | * @brief Data input/output formats supported by libyang [parser](@ref howtodataparsers) and |
| 38 | * [printer](@ref howtodataprinters) functions. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 39 | */ |
| 40 | typedef enum { |
| 41 | LYD_UNKNOWN, /**< unknown format, used as return value in case of error */ |
| 42 | LYD_XML, /**< XML format of the instance data */ |
| 43 | LYD_JSON, /**< JSON format of the instance data */ |
Michal Vasko | 1e82a3b | 2018-07-03 12:16:58 +0200 | [diff] [blame] | 44 | LYD_LYB, /**< LYB format of the instance data */ |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 45 | } LYD_FORMAT; |
| 46 | |
| 47 | /** |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 48 | * @brief List of possible value types stored in ::lyd_node_anydata. |
| 49 | */ |
| 50 | typedef enum { |
Radek Krejci | 83bf140 | 2016-09-27 15:05:20 +0200 | [diff] [blame] | 51 | LYD_ANYDATA_CONSTSTRING = 0x00, /**< value is constant string (const char *) which is internally duplicated for |
| 52 | storing in the anydata structure; XML sensitive characters (such as & or \>) |
Radek Krejci | e534c13 | 2016-11-23 13:32:31 +0100 | [diff] [blame] | 53 | are automatically escaped when the anydata is printed in XML format. */ |
Radek Krejci | 83bf140 | 2016-09-27 15:05:20 +0200 | [diff] [blame] | 54 | LYD_ANYDATA_STRING = 0x01, /**< value is dynamically allocated string (char*), so the data are used directly |
| 55 | without duplication and caller is supposed to not manipulate with the data |
| 56 | after a successful call (including calling free() on the provided data); XML |
| 57 | sensitive characters (such as & or \>) are automatically escaped when the |
| 58 | anydata is printed in XML format */ |
| 59 | LYD_ANYDATA_JSON = 0x02, /**< value is string containing the data modeled by YANG and encoded as I-JSON. The |
| 60 | string is handled as constant string. In case of using the value as input |
| 61 | parameter, the #LYD_ANYDATA_JSOND can be used for dynamically allocated |
| 62 | string. */ |
| 63 | LYD_ANYDATA_JSOND = 0x03, /**< In case of using value as input parameter, this enumeration is supposed to be |
| 64 | used for dynamically allocated strings (it is actually combination of |
| 65 | #LYD_ANYDATA_JSON and #LYD_ANYDATA_STRING (and it can be also specified as |
| 66 | ORed value of the mentioned values. */ |
| 67 | LYD_ANYDATA_SXML = 0x04, /**< value is string containing the serialized XML data. The string is handled as |
| 68 | constant string. In case of using the value as input parameter, the |
| 69 | #LYD_ANYDATA_SXMLD can be used for dynamically allocated string. */ |
| 70 | LYD_ANYDATA_SXMLD = 0x05, /**< In case of using serialized XML value as input parameter, this enumeration is |
| 71 | supposed to be used for dynamically allocated strings (it is actually |
| 72 | combination of #LYD_ANYDATA_SXML and #LYD_ANYDATA_STRING (and it can be also |
| 73 | specified as ORed value of the mentioned values). */ |
| 74 | LYD_ANYDATA_XML = 0x08, /**< value is struct lyxml_elem*, the structure is directly connected into the |
| 75 | anydata node without duplication, caller is supposed to not manipulate with the |
| 76 | data after a successful call (including calling lyxml_free() on the provided |
| 77 | data) */ |
| 78 | LYD_ANYDATA_DATATREE = 0x10, /**< value is struct lyd_node* (first sibling), the structure is directly connected |
| 79 | into the anydata node without duplication, caller is supposed to not manipulate |
| 80 | with the data after a successful call (including calling lyd_free() on the |
| 81 | provided data) */ |
Michal Vasko | dcaf722 | 2018-08-08 16:27:00 +0200 | [diff] [blame] | 82 | LYD_ANYDATA_LYB = 0x20, /**< value is a memory with serialized data tree in LYB format. The data are handled |
| 83 | as a constant string. In case of using the value as input parameter, |
| 84 | the #LYD_ANYDATA_LYBD can be used for dynamically allocated string. */ |
| 85 | LYD_ANYDATA_LYBD = 0x21, /**< In case of using LYB value as input parameter, this enumeration is |
| 86 | supposed to be used for dynamically allocated strings (it is actually |
| 87 | combination of #LYD_ANYDATA_LYB and #LYD_ANYDATA_STRING (and it can be also |
| 88 | specified as ORed value of the mentioned values). */ |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 89 | } LYD_ANYDATA_VALUETYPE; |
| 90 | |
| 91 | /** |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 92 | * @brief node's value representation |
| 93 | */ |
| 94 | typedef union lyd_value_u { |
| 95 | const char *binary; /**< base64 encoded, NULL terminated string */ |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 96 | struct lys_type_bit **bit; /**< bitmap of pointers to the schema definition of the bit value that are set, |
| 97 | its size is always the number of defined bits in the schema */ |
Radek Krejci | 489773c | 2015-12-17 13:20:03 +0100 | [diff] [blame] | 98 | int8_t bln; /**< 0 as false, 1 as true */ |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 99 | int64_t dec64; /**< decimal64: value = dec64 / 10^fraction-digits */ |
| 100 | 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] | 101 | 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] | 102 | struct lyd_node *instance; /**< pointer to the instance-identifier target, note that if the tree was modified, |
| 103 | the target (address) can be invalid - the pointer is correctly checked and updated |
| 104 | by lyd_validate() */ |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 105 | int8_t int8; /**< 8-bit signed integer */ |
| 106 | int16_t int16; /**< 16-bit signed integer */ |
| 107 | int32_t int32; /**< 32-bit signed integer */ |
| 108 | int64_t int64; /**< 64-bit signed integer */ |
| 109 | struct lyd_node *leafref; /**< pointer to the referenced leaf/leaflist instance in data tree */ |
| 110 | const char *string; /**< string */ |
| 111 | uint8_t uint8; /**< 8-bit unsigned integer */ |
| 112 | uint16_t uint16; /**< 16-bit signed integer */ |
| 113 | uint32_t uint32; /**< 32-bit signed integer */ |
| 114 | uint64_t uint64; /**< 64-bit signed integer */ |
Michal Vasko | c6cd3f0 | 2018-03-02 14:07:42 +0100 | [diff] [blame] | 115 | void *ptr; /**< arbitrary data stored using a type plugin */ |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 116 | } lyd_val; |
| 117 | |
| 118 | /** |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 119 | * @brief Attribute structure. |
| 120 | * |
| 121 | * The structure provides information about attributes of a data element. Such attributes must map to |
| 122 | * annotations as specified in RFC 7952. The only exception is the filter type (in NETCONF get operations) |
Renato Westphal | 99fded7 | 2018-10-22 14:44:24 -0200 | [diff] [blame] | 123 | * and edit-config's operation attributes. In XML, they are represented as standard XML attributes. In JSON, |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 124 | * they are represented as JSON elements starting with the '@' character (for more information, see the |
| 125 | * YANG metadata RFC. |
| 126 | * |
| 127 | */ |
| 128 | struct lyd_attr { |
| 129 | struct lyd_node *parent; /**< data node where the attribute is placed */ |
| 130 | struct lyd_attr *next; /**< pointer to the next attribute of the same element */ |
| 131 | struct lys_ext_instance_complex *annotation; /**< pointer to the attribute/annotation's definition */ |
| 132 | const char *name; /**< attribute name */ |
| 133 | const char *value_str; /**< string representation of value (for comparison, printing,...), always corresponds to value_type */ |
| 134 | lyd_val value; /**< node's value representation, always corresponds to schema->type.base */ |
Michal Vasko | 70bf8e5 | 2018-03-26 11:32:33 +0200 | [diff] [blame] | 135 | LY_DATA_TYPE _PACKED value_type; /**< type of the value in the node, mainly for union to avoid repeating of type detection */ |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 136 | uint8_t value_flags; /**< value type flags */ |
Radek Krejci | a571d94 | 2017-02-24 09:26:49 +0100 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | /** |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 140 | * @defgroup validityflags Validity flags |
| 141 | * @ingroup datatree |
| 142 | * |
| 143 | * Validity flags for data nodes. |
| 144 | * |
| 145 | * @{ |
| 146 | */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 147 | #define LYD_VAL_OK 0x00 /**< Node is successfully validated including whole subtree */ |
Michal Vasko | 185b527 | 2018-09-13 14:26:12 +0200 | [diff] [blame] | 148 | #define LYD_VAL_DUP 0x01 /**< Instance duplication must be checked again, applicable only to ::lys_node_list and |
| 149 | ::lys_node_leaf_list data nodes */ |
| 150 | #define LYD_VAL_UNIQUE 0x02 /**< Unique value(s) changed, applicable only to ::lys_node_list data nodes */ |
| 151 | #define LYD_VAL_MAND 0x04 /**< Some child added/removed and it is needed to perform check for mandatory |
Radek Krejci | d788a52 | 2016-07-25 14:57:38 +0200 | [diff] [blame] | 152 | node or min/max constraints of direct list/leaflist children, applicable only |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 153 | to ::lys_node_list and ::lys_node_container data nodes, but if on any other node |
| 154 | except ::lys_node_leaflist, it means checking that data node for duplicities. |
| 155 | Additionally, it can be set on truly any node type and then status references |
| 156 | are checked for this node if flag #LYD_OPT_OBSOLETE is used. */ |
Michal Vasko | 185b527 | 2018-09-13 14:26:12 +0200 | [diff] [blame] | 157 | #define LYD_VAL_LEAFREF 0x08 /**< Node is a leafref, which needs to be resolved (it is invalid, new possible |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 158 | resolvent, or something similar) */ |
| 159 | #define LYD_VAL_INUSE 0x80 /**< Internal flag for note about various processing on data, should be used only |
| 160 | internally and removed before libyang returns the node to the caller */ |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 161 | /** |
| 162 | * @} |
| 163 | */ |
| 164 | |
| 165 | /** |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 166 | * @brief Generic structure for a data node, directly applicable to the data nodes defined as #LYS_CONTAINER, #LYS_LIST |
| 167 | * and #LYS_CHOICE. |
| 168 | * |
| 169 | * Completely fits to containers and choices and is compatible (can be used interchangeably except the #child member) |
| 170 | * with all other lyd_node_* structures. All data nodes are provides as ::lyd_node structure by default. |
| 171 | * According to the schema's ::lys_node#nodetype member, the specific object is supposed to be cast to |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 172 | * ::lyd_node_leaf_list or ::lyd_node_anydata structures. This structure fits only to #LYS_CONTAINER, #LYS_LIST and |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 173 | * #LYS_CHOICE values. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 174 | * |
Michal Vasko | c30c95a | 2018-08-01 14:35:54 +0200 | [diff] [blame] | 175 | * To traverse all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro. To traverse |
| 176 | * the whole subtree, use #LY_TREE_DFS_BEGIN macro. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 177 | */ |
| 178 | struct lyd_node { |
| 179 | struct lys_node *schema; /**< pointer to the schema definition of this node */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 180 | uint8_t validity; /**< [validity flags](@ref validityflags) */ |
Michal Vasko | e77dc99 | 2017-01-18 12:09:42 +0100 | [diff] [blame] | 181 | uint8_t dflt:1; /**< flag for implicit default node */ |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 182 | 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] | 183 | do not use this value! */ |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 184 | |
| 185 | struct lyd_attr *attr; /**< pointer to the list of attributes of this node */ |
| 186 | struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */ |
| 187 | struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is |
| 188 | never NULL. If there is no sibling node, pointer points to the node |
| 189 | itself. In case of the first node, this pointer points to the last |
| 190 | node in the list. */ |
| 191 | struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */ |
Michal Vasko | 24affa0 | 2018-04-03 09:06:06 +0200 | [diff] [blame] | 192 | |
Michal Vasko | d025ee3 | 2018-06-28 10:04:19 +0200 | [diff] [blame] | 193 | #ifdef LY_ENABLED_LYD_PRIV |
| 194 | void *priv; /**< private user data, not used by libyang */ |
| 195 | #endif |
| 196 | |
Michal Vasko | 24affa0 | 2018-04-03 09:06:06 +0200 | [diff] [blame] | 197 | #ifdef LY_ENABLED_CACHE |
| 198 | uint32_t hash; /**< hash of this particular node (module name + schema name + key string values if list) */ |
| 199 | struct hash_table *ht; /**< hash table with all the direct children (except keys for a list, lists without keys) */ |
| 200 | #endif |
| 201 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 202 | 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] | 203 | structures represent end nodes, this member |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 204 | is replaced in those structures. Therefore, be careful with accessing |
| 205 | this member without having information about the node type from the schema's |
| 206 | ::lys_node#nodetype member. */ |
| 207 | }; |
| 208 | |
| 209 | /** |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 210 | * @brief Structure for data nodes defined as #LYS_LEAF or #LYS_LEAFLIST. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 211 | * |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 212 | * Extension for ::lyd_node structure. It replaces the ::lyd_node#child member by |
| 213 | * three new members (#value, #value_str and #value_type) to provide |
| 214 | * information about the value. The first five members (#schema, #attr, #next, |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 215 | * #prev and #parent) are compatible with the ::lyd_node's members. |
| 216 | * |
| 217 | * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro. |
| 218 | */ |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 219 | struct lyd_node_leaf_list { |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 220 | struct lys_node *schema; /**< pointer to the schema definition of this node which is ::lys_node_leaflist |
| 221 | structure */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 222 | uint8_t validity; /**< [validity flags](@ref validityflags) */ |
Michal Vasko | e77dc99 | 2017-01-18 12:09:42 +0100 | [diff] [blame] | 223 | uint8_t dflt:1; /**< flag for implicit default node */ |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 224 | 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] | 225 | do not use this value! */ |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 226 | |
| 227 | struct lyd_attr *attr; /**< pointer to the list of attributes of this node */ |
| 228 | struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */ |
| 229 | struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is |
| 230 | never NULL. If there is no sibling node, pointer points to the node |
| 231 | itself. In case of the first node, this pointer points to the last |
| 232 | node in the list. */ |
| 233 | struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */ |
| 234 | |
Michal Vasko | d025ee3 | 2018-06-28 10:04:19 +0200 | [diff] [blame] | 235 | #ifdef LY_ENABLED_LYD_PRIV |
| 236 | void *priv; /**< private user data, not used by libyang */ |
| 237 | #endif |
| 238 | |
Michal Vasko | 24affa0 | 2018-04-03 09:06:06 +0200 | [diff] [blame] | 239 | #ifdef LY_ENABLED_CACHE |
| 240 | uint32_t hash; /**< hash of this particular node (module name + schema name + string value if leaf-list) */ |
| 241 | #endif |
| 242 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 243 | /* struct lyd_node *child; should be here, but is not */ |
| 244 | |
| 245 | /* leaflist's specific members */ |
Michal Vasko | 6a02770 | 2016-06-30 10:32:14 +0200 | [diff] [blame] | 246 | const char *value_str; /**< string representation of value (for comparison, printing,...), always corresponds to value_type */ |
| 247 | lyd_val value; /**< node's value representation, always corresponds to schema->type.base */ |
Michal Vasko | 70bf8e5 | 2018-03-26 11:32:33 +0200 | [diff] [blame] | 248 | LY_DATA_TYPE _PACKED value_type; /**< type of the value in the node, mainly for union to avoid repeating of type detection */ |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 249 | uint8_t value_flags; /**< value type flags */ |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 250 | }; |
| 251 | |
| 252 | /** |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 253 | * @brief Flags for values |
| 254 | */ |
| 255 | #define LY_VALUE_UNRES 0x01 /**< flag for unresolved leafref or instance-identifier, |
| 256 | leafref - value union is filled as if being the target node's type, |
| 257 | instance-identifier - value union should not be accessed */ |
| 258 | #define LY_VALUE_USER 0x02 /**< flag for a user type stored value */ |
Renato Westphal | 99fded7 | 2018-10-22 14:44:24 -0200 | [diff] [blame] | 259 | /* 0x80 is reserved for internal use */ |
Michal Vasko | 101658e | 2018-06-05 15:05:54 +0200 | [diff] [blame] | 260 | |
| 261 | /** |
Václav Kubernát | 05b5650 | 2019-03-05 14:56:13 +0100 | [diff] [blame] | 262 | * @brief Anydata value union |
| 263 | */ |
| 264 | typedef union { |
| 265 | const char *str; /**< string value, in case of printing as XML, characters like '<' or '&' are escaped */ |
| 266 | char *mem; /**< raw memory (used for LYB format) */ |
| 267 | struct lyxml_elem *xml; /**< xml tree */ |
| 268 | struct lyd_node *tree; /**< libyang data tree, does not change the root's parent, so it is not possible |
| 269 | to get from the data tree into the anydata/anyxml */ |
| 270 | } lyd_anydata_value; |
| 271 | |
| 272 | /** |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 273 | * @brief Structure for data nodes defined as #LYS_ANYDATA or #LYS_ANYXML. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 274 | * |
| 275 | * Extension for ::lyd_node structure - replaces the ::lyd_node#child member by new #value member. The first five |
| 276 | * members (#schema, #attr, #next, #prev and #parent) are compatible with the ::lyd_node's members. |
| 277 | * |
| 278 | * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro. |
| 279 | */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 280 | struct lyd_node_anydata { |
| 281 | struct lys_node *schema; /**< pointer to the schema definition of this node which is ::lys_node_anydata |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 282 | structure */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 283 | uint8_t validity; /**< [validity flags](@ref validityflags) */ |
Michal Vasko | e77dc99 | 2017-01-18 12:09:42 +0100 | [diff] [blame] | 284 | uint8_t dflt:1; /**< flag for implicit default node */ |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 285 | 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] | 286 | do not use this value! */ |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 287 | |
| 288 | struct lyd_attr *attr; /**< pointer to the list of attributes of this node */ |
| 289 | struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */ |
| 290 | struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is |
| 291 | never NULL. If there is no sibling node, pointer points to the node |
| 292 | itself. In case of the first node, this pointer points to the last |
| 293 | node in the list. */ |
| 294 | struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */ |
| 295 | |
Michal Vasko | d025ee3 | 2018-06-28 10:04:19 +0200 | [diff] [blame] | 296 | #ifdef LY_ENABLED_LYD_PRIV |
| 297 | void *priv; /**< private user data, not used by libyang */ |
| 298 | #endif |
| 299 | |
Michal Vasko | 24affa0 | 2018-04-03 09:06:06 +0200 | [diff] [blame] | 300 | #ifdef LY_ENABLED_CACHE |
| 301 | uint32_t hash; /**< hash of this particular node (module name + schema name) */ |
| 302 | #endif |
| 303 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 304 | /* struct lyd_node *child; should be here, but is not */ |
| 305 | |
| 306 | /* anyxml's specific members */ |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 307 | LYD_ANYDATA_VALUETYPE value_type;/**< type of the stored anydata value */ |
Václav Kubernát | 05b5650 | 2019-03-05 14:56:13 +0100 | [diff] [blame] | 308 | lyd_anydata_value value;/**< stored anydata value */ |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 309 | }; |
| 310 | |
| 311 | /** |
Renato Westphal | 99fded7 | 2018-10-22 14:44:24 -0200 | [diff] [blame] | 312 | * @brief list of possible types of differences in #lyd_difflist |
Radek Krejci | 991a396 | 2016-05-05 15:00:14 +0200 | [diff] [blame] | 313 | */ |
| 314 | typedef enum { |
Radek Krejci | 9e6f0b8 | 2016-05-13 17:33:16 +0200 | [diff] [blame] | 315 | LYD_DIFF_END = 0, /**< end of the differences list */ |
Radek Krejci | 9e6f0b8 | 2016-05-13 17:33:16 +0200 | [diff] [blame] | 316 | LYD_DIFF_DELETED, /**< deleted node |
| 317 | - Node is present in the first tree, but not in the second tree. |
| 318 | - To make both trees the same the node in lyd_difflist::first can be deleted from the |
| 319 | first tree. The pointer at the same index in the lyd_difflist::second array is |
Michal Vasko | 6407fca | 2018-04-24 09:44:11 +0200 | [diff] [blame] | 320 | NULL. |
| 321 | - If the deleted node has some children, these do not appear in the resulting diff |
| 322 | separately. In other words, a deleted node is considered deleted with all |
| 323 | its children. */ |
Radek Krejci | 9e6f0b8 | 2016-05-13 17:33:16 +0200 | [diff] [blame] | 324 | LYD_DIFF_CHANGED, /**< value of a leaf or anyxml is changed, the lyd_difflist::first and lyd_difflist::second |
| 325 | points to the leaf/anyxml instances in the first and the second tree respectively. */ |
Radek Krejci | 22d2ca9 | 2016-05-17 16:23:51 +0200 | [diff] [blame] | 326 | LYD_DIFF_MOVEDAFTER1, /**< user-ordered (leaf-)list item was moved. |
| 327 | - To make both trees the same, all #LYD_DIFF_MOVEDAFTER1 transactions must be applied |
Radek Krejci | 9e6f0b8 | 2016-05-13 17:33:16 +0200 | [diff] [blame] | 328 | to the first tree in the strict order they appear in the difflist. The |
| 329 | lyd_difflist::first points to the first tree node being moved and the |
| 330 | lyd_difflist::second points to the first tree node after which the first node is |
| 331 | supposed to be moved. If the second pointer is NULL, the node is being moved into |
| 332 | the beginning as the first node of the (leaf-)list instances. */ |
Radek Krejci | 22d2ca9 | 2016-05-17 16:23:51 +0200 | [diff] [blame] | 333 | LYD_DIFF_CREATED, /**< newly created node |
| 334 | - Node is present in the second tree, but not in the first tree. |
| 335 | - To make both trees the same the node in lyd_difflist::second is supposed to be |
| 336 | inserted (copied via lyd_dup()) into the node (as a child) at the same index in the |
| 337 | lyd_difflist::first array (where is its parent). If the lyd_difflist::first at the |
Michal Vasko | 6407fca | 2018-04-24 09:44:11 +0200 | [diff] [blame] | 338 | index is NULL, the missing node is top-level. |
| 339 | - If the created node has some children, these do not appear in the resulting diff |
| 340 | separately. In other words, a created node is considered created with all |
| 341 | its children. */ |
Radek Krejci | 22d2ca9 | 2016-05-17 16:23:51 +0200 | [diff] [blame] | 342 | LYD_DIFF_MOVEDAFTER2 /**< similar to LYD_DIFF_MOVEDAFTER1, but this time the moved item is in the second tree. |
| 343 | This type is always used in combination with (as a successor of) #LYD_DIFF_CREATED |
Michal Vasko | 6a2597e | 2019-01-24 16:29:02 +0100 | [diff] [blame] | 344 | as an instruction to move the newly created node to a specific position. If it is not |
| 345 | present, it means that even the parent of the user-ordered instances did not exist |
| 346 | (or was empty) so it is safe to just create the instances in the same order. Note, |
| 347 | that due to applicability to the second tree, the meaning of lyd_difflist:first and |
Radek Krejci | 22d2ca9 | 2016-05-17 16:23:51 +0200 | [diff] [blame] | 348 | lyd_difflist:second is inverse in comparison to #LYD_DIFF_MOVEDAFTER1. The |
| 349 | lyd_difflist::second points to the (previously) created node in the second tree and |
| 350 | the lyd_difflist::first points to the predecessor node in the second tree. If the |
| 351 | predecessor is NULL, the node is supposed to bes the first sibling. */ |
Radek Krejci | 991a396 | 2016-05-05 15:00:14 +0200 | [diff] [blame] | 352 | } LYD_DIFFTYPE; |
| 353 | |
| 354 | /** |
| 355 | * @brief Structure for the result of lyd_diff(), describing differences between two data trees. |
| 356 | */ |
| 357 | struct lyd_difflist { |
| 358 | LYD_DIFFTYPE *type; /**< array of the differences types, terminated by #LYD_DIFF_END value. */ |
| 359 | struct lyd_node **first; /**< array of nodes in the first tree for the specific type of difference, see the |
| 360 | description of #LYD_DIFFTYPE values for more information. */ |
| 361 | struct lyd_node **second;/**< array of nodes in the second tree for the specific type of difference, see the |
| 362 | description of #LYD_DIFFTYPE values for more information. */ |
| 363 | }; |
| 364 | |
| 365 | /** |
| 366 | * @brief Free the result of lyd_diff(). It frees the structure of the lyd_diff() result, not the referenced nodes. |
| 367 | * |
| 368 | * @param[in] diff The lyd_diff() result to free. |
| 369 | */ |
| 370 | void lyd_free_diff(struct lyd_difflist *diff); |
| 371 | |
| 372 | /** |
| 373 | * @brief Compare two data trees and provide list of differences. |
| 374 | * |
| 375 | * Note, that the \p first and the \p second must have the same schema parent (or they must be top-level elements). |
| 376 | * In case of using #LYD_OPT_NOSIBLINGS, they both must be instances of the same schema node. |
| 377 | * |
Radek Krejci | 913100d | 2016-05-09 17:23:51 +0200 | [diff] [blame] | 378 | * Order of the resulting set follows these rules: |
Radek Krejci | 22d2ca9 | 2016-05-17 16:23:51 +0200 | [diff] [blame] | 379 | * - To change the first tree into the second tree, the resulting transactions are supposed to be applied in the order |
| 380 | * they appear in the result. First, the changed (#LYD_DIFF_CHANGED) nodes are described followed by the deleted |
| 381 | * (#LYD_DIFF_DELETED) nodes. Then, the moving of the user-ordered nodes present in both trees (#LYD_DIFF_MOVEDAFTER1) |
| 382 | * follows and the last transactions in the results are the newly created (#LYD_DIFF_CREATED) nodes. These nodes are |
| 383 | * supposed to be added as the last siblings, but in some case they can need additional move. In such a case, the |
| 384 | * #LYD_DIFF_MOVEDAFTER2 transactions can appear. |
| 385 | * - The order of the changed (#LYD_DIFF_CHANGED) and created (#LYD_DIFF_CREATED) follows the nodes order in the |
| 386 | * second tree - the current siblings are processed first and then the children are processed. Note, that this is |
| 387 | * actually not the BFS: |
Radek Krejci | 9e47ddf | 2016-05-18 15:01:09 +0200 | [diff] [blame] | 388 | * |
Radek Krejci | 913100d | 2016-05-09 17:23:51 +0200 | [diff] [blame] | 389 | * 1 2 |
| 390 | * / \ / \ |
| 391 | * 3 4 7 8 |
| 392 | * / \ |
| 393 | * 5 6 |
Radek Krejci | 9e47ddf | 2016-05-18 15:01:09 +0200 | [diff] [blame] | 394 | * |
Radek Krejci | 22d2ca9 | 2016-05-17 16:23:51 +0200 | [diff] [blame] | 395 | * - The order of the deleted (#LYD_DIFF_DELETED) nodes is the DFS: |
Radek Krejci | 9e47ddf | 2016-05-18 15:01:09 +0200 | [diff] [blame] | 396 | * |
| 397 | * 1 6 |
| 398 | * / \ / \ |
| 399 | * 2 5 7 8 |
| 400 | * / \ |
| 401 | * 3 4 |
Radek Krejci | 913100d | 2016-05-09 17:23:51 +0200 | [diff] [blame] | 402 | * |
| 403 | * To change the first tree into the second one, it is necessary to follow the order of transactions described in |
| 404 | * the result. Note, that it is not possible just to use the transactions in the reverse order to transform the |
| 405 | * second tree into the first one. The transactions can be generalized (to be used on a different instance of the |
| 406 | * first tree) using lyd_path() to get identifiers for the nodes used in the transactions. |
| 407 | * |
Radek Krejci | 9a6a5dd | 2016-05-05 15:56:24 +0200 | [diff] [blame] | 408 | * @param[in] first The first (sub)tree to compare. Without #LYD_OPT_NOSIBLINGS option, all siblings are |
Radek Krejci | 4c3bc11 | 2016-05-19 15:09:03 +0200 | [diff] [blame] | 409 | * taken into comparison. If NULL, all the \p second nodes are supposed to be top level and they will |
| 410 | * be marked as #LYD_DIFF_CREATED. |
Radek Krejci | 9a6a5dd | 2016-05-05 15:56:24 +0200 | [diff] [blame] | 411 | * @param[in] second The second (sub)tree to compare. Without #LYD_OPT_NOSIBLINGS option, all siblings are |
Radek Krejci | 4c3bc11 | 2016-05-19 15:09:03 +0200 | [diff] [blame] | 412 | * taken into comparison. If NULL, all the \p first nodes will be marked as #LYD_DIFF_DELETED. |
Radek Krejci | 99d737f | 2016-09-06 11:19:52 +0200 | [diff] [blame] | 413 | * @param[in] options The @ref diffoptions are accepted. |
Radek Krejci | 991a396 | 2016-05-05 15:00:14 +0200 | [diff] [blame] | 414 | * @return NULL on error, the list of differences on success. In case the trees are the same, the first item in the |
Radek Krejci | 9a6a5dd | 2016-05-05 15:56:24 +0200 | [diff] [blame] | 415 | * lyd_difflist::type array is #LYD_DIFF_END. The returned structure is supposed to be freed by lyd_free_diff(). |
Radek Krejci | 991a396 | 2016-05-05 15:00:14 +0200 | [diff] [blame] | 416 | */ |
| 417 | struct lyd_difflist *lyd_diff(struct lyd_node *first, struct lyd_node *second, int options); |
| 418 | |
| 419 | /** |
Radek Krejci | 99d737f | 2016-09-06 11:19:52 +0200 | [diff] [blame] | 420 | * @defgroup diffoptions Diff options |
| 421 | * @ingroup datatree |
| 422 | * |
| 423 | * @{ |
| 424 | */ |
| 425 | /* LYD_DIFFOPT_NOSIBLINGS value is the same as LYD_OPT_NOSIBLINGS due to backward compatibility. The LYD_OPT_NOSIBLINGS |
| 426 | * was used previously as an option for lyd_diff(). */ |
| 427 | #define LYD_DIFFOPT_NOSIBLINGS 0x0800 /**< The both trees to diff have to instantiate the same schema node so only the |
| 428 | single subtree is compared. */ |
| 429 | #define LYD_DIFFOPT_WITHDEFAULTS 0x0001 /**< Take default nodes with their values into account and handle them as part |
Michal Vasko | e6ff428 | 2017-02-07 15:13:36 +0100 | [diff] [blame] | 430 | of both trees. Summary of the modified behavior: |
| 431 | - deleted node is replaced with implicit default node - #LYD_DIFF_CHANGED instead delete |
| 432 | - created node replaces an implicit default node - #LYD_DIFF_CHANGED instead create |
| 433 | - in both cases even if the values match - #LYD_DIFF_CHANGED is still returned, because dlft flag was changed |
| 434 | Note that in this case, applying the resulting |
Radek Krejci | 99d737f | 2016-09-06 11:19:52 +0200 | [diff] [blame] | 435 | transactions on the first tree does not result to the exact second tree, |
| 436 | because instead of having implicit default nodes you are going to have |
| 437 | explicit default nodes. */ |
| 438 | /**@} diffoptions */ |
| 439 | |
| 440 | /** |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 441 | * @brief Build data path (usable as path, see @ref howtoxpath) of the data node. |
Radek Krejci | 6d53828 | 2016-05-05 14:24:12 +0200 | [diff] [blame] | 442 | * @param[in] node Data node to be processed. Note that the node should be from a complete data tree, having a subtree |
| 443 | * (after using lyd_unlink()) can cause generating invalid paths. |
| 444 | * @return NULL on error, on success the buffer for the resulting path is allocated and caller is supposed to free it |
| 445 | * with free(). |
| 446 | */ |
Michal Vasko | 5efa25c | 2017-01-10 11:34:30 +0100 | [diff] [blame] | 447 | char *lyd_path(const struct lyd_node *node); |
| 448 | |
| 449 | /** |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 450 | * @defgroup parseroptions Data parser options |
| 451 | * @ingroup datatree |
| 452 | * |
| 453 | * Various options to change the data tree parsers behavior. |
| 454 | * |
| 455 | * Default behavior: |
| 456 | * - in case of XML, parser reads all data from its input (file, memory, XML tree) including the case of not well-formed |
| 457 | * XML document (multiple top-level elements) and if there is an unknown element, it is skipped including its subtree |
| 458 | * (see the next point). This can be changed by the #LYD_OPT_NOSIBLINGS option which make parser to read only a single |
| 459 | * tree (with a single root element) from its input. |
| 460 | * - parser silently ignores the data without a matching node in schema trees. If the caller want to stop |
| 461 | * parsing in case of presence of unknown data, the #LYD_OPT_STRICT can be used. The strict mode is useful for |
| 462 | * NETCONF servers, since NETCONF clients should always send data according to the capabilities announced by the server. |
| 463 | * On the other hand, the default non-strict mode is useful for clients receiving data from NETCONF server since |
| 464 | * clients are not required to understand everything the server does. Of course, the optimal strategy for clients is |
| 465 | * to use filtering to get only the required data. Having an unknown element of the known namespace is always an error. |
| 466 | * The behavior can be changed by #LYD_OPT_STRICT option. |
| 467 | * - using obsolete statements (status set to obsolete) just generates a warning, but the processing continues. The |
| 468 | * behavior can be changed by #LYD_OPT_OBSOLETE option. |
| 469 | * - parser expects that the provided data provides complete datastore content (both the configuration and state data) |
| 470 | * and performs data validation according to all YANG rules. This can be a problem in case of representing NETCONF's |
| 471 | * subtree filter data, edit-config's data or other type of data set - such data do not represent a complete data set |
| 472 | * and some of the validation rules can fail. Therefore there are other options (within lower 8 bits) to make parser |
| 473 | * to accept such a data. |
Michal Vasko | bbc43b1 | 2018-10-12 09:22:00 +0200 | [diff] [blame] | 474 | * - when parser evaluates when-stmt condition to false, a validation error is raised. If the |
| 475 | * #LYD_OPT_WHENAUTODEL is used, the invalid node is silently removed instead of an error. The option (and also this default |
Radek Krejci | f3c218d | 2016-03-24 12:40:08 +0100 | [diff] [blame] | 476 | * behavior) takes effect only in case of #LYD_OPT_DATA or #LYD_OPT_CONFIG type of data. |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 477 | * @{ |
| 478 | */ |
| 479 | |
| 480 | #define LYD_OPT_DATA 0x00 /**< Default type of data - complete datastore content with configuration as well as |
Radek Krejci | 06f8bb9 | 2017-08-02 15:36:25 +0200 | [diff] [blame] | 481 | state data. To handle possibly missing (but by default required) ietf-yang-library |
| 482 | data, use #LYD_OPT_DATA_NO_YANGLIB or #LYD_OPT_DATA_ADD_YANGLIB options. */ |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 483 | #define LYD_OPT_CONFIG 0x01 /**< A configuration datastore - complete datastore without state data. |
| 484 | Validation modifications: |
| 485 | - status data are not allowed */ |
| 486 | #define LYD_OPT_GET 0x02 /**< Data content from a NETCONF reply message to the NETCONF \<get\> operation. |
| 487 | Validation modifications: |
| 488 | - mandatory nodes can be omitted |
Michal Vasko | 62671b9 | 2017-01-02 13:08:57 +0100 | [diff] [blame] | 489 | - leafrefs and instance-identifier resolution is allowed to fail |
Michal Vasko | ebf7df2 | 2017-03-28 16:08:07 +0200 | [diff] [blame] | 490 | - list's keys/unique nodes are not required (so duplication is not checked) |
| 491 | - must and when evaluation skipped */ |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 492 | #define LYD_OPT_GETCONFIG 0x04 /**< Data content from a NETCONF reply message to the NETCONF \<get-config\> operation |
| 493 | Validation modifications: |
| 494 | - mandatory nodes can be omitted |
Michal Vasko | 62671b9 | 2017-01-02 13:08:57 +0100 | [diff] [blame] | 495 | - leafrefs and instance-identifier resolution is allowed to fail |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 496 | - list's keys/unique nodes are not required (so duplication is not checked) |
Michal Vasko | ebf7df2 | 2017-03-28 16:08:07 +0200 | [diff] [blame] | 497 | - must and when evaluation skipped |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 498 | - status data are not allowed */ |
| 499 | #define LYD_OPT_EDIT 0x08 /**< Content of the NETCONF \<edit-config\>'s config element. |
| 500 | Validation modifications: |
| 501 | - mandatory nodes can be omitted |
Michal Vasko | 62671b9 | 2017-01-02 13:08:57 +0100 | [diff] [blame] | 502 | - leafrefs and instance-identifier resolution is allowed to fail |
Michal Vasko | ebf7df2 | 2017-03-28 16:08:07 +0200 | [diff] [blame] | 503 | - must and when evaluation skipped |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 504 | - status data are not allowed */ |
Michal Vasko | 7525026 | 2017-02-09 15:36:13 +0100 | [diff] [blame] | 505 | #define LYD_OPT_RPC 0x10 /**< Data represents RPC or action input parameters. */ |
| 506 | #define LYD_OPT_RPCREPLY 0x20 /**< Data represents RPC or action output parameters (maps to NETCONF <rpc-reply> data). */ |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 507 | #define LYD_OPT_NOTIF 0x40 /**< Data represents an event notification data. */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 508 | #define LYD_OPT_NOTIF_FILTER 0x80 /**< Data represents a filtered event notification data. |
| 509 | Validation modification: |
| 510 | - the only requirement is that the data tree matches the schema tree */ |
PavolVican | 832f543 | 2018-02-21 00:54:45 +0100 | [diff] [blame] | 511 | #define LYD_OPT_TYPEMASK 0x10000ff /**< Mask to filter data type options. Always only a single data type option (only |
| 512 | single bit from the lower 8 bits) can be set. */ |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 513 | |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 514 | /* 0x100 reserved, used internally */ |
| 515 | #define LYD_OPT_STRICT 0x0200 /**< Instead of silent ignoring data without schema definition, raise an error. */ |
| 516 | #define LYD_OPT_DESTRUCT 0x0400 /**< Free the provided XML tree during parsing the data. With this option, the |
Radek Krejci | 06f8bb9 | 2017-08-02 15:36:25 +0200 | [diff] [blame] | 517 | provided XML tree is affected and all successfully parsed data are freed. |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 518 | This option is applicable only to lyd_parse_xml() function. */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 519 | #define LYD_OPT_OBSOLETE 0x0800 /**< Raise an error when an obsolete statement (status set to obsolete) is used. */ |
| 520 | #define LYD_OPT_NOSIBLINGS 0x1000 /**< Parse only a single XML tree from the input. This option applies only to |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 521 | XML input data. */ |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 522 | #define LYD_OPT_TRUSTED 0x2000 /**< Data comes from a trusted source and it is not needed to validate them. Data |
Radek Krejci | 93fab98 | 2016-02-03 15:58:19 +0100 | [diff] [blame] | 523 | are connected with the schema, but the most validation checks (mandatory nodes, |
Michal Vasko | d7f9bda | 2018-03-16 12:33:35 +0100 | [diff] [blame] | 524 | list instance uniqueness, etc.) are not performed. This option does not make |
| 525 | sense for lyd_validate() so it is ignored by this function. */ |
Michal Vasko | bbc43b1 | 2018-10-12 09:22:00 +0200 | [diff] [blame] | 526 | #define LYD_OPT_WHENAUTODEL 0x4000 /**< Automatically delete subtrees with false when-stmt condition. The flag is |
| 527 | applicable only in combination with #LYD_OPT_DATA and #LYD_OPT_CONFIG flags. |
| 528 | If used, libyang will not generate a validation error. */ |
Michal Vasko | 3cfa318 | 2017-01-17 10:00:58 +0100 | [diff] [blame] | 529 | #define LYD_OPT_NOEXTDEPS 0x8000 /**< Allow external dependencies (external leafrefs, instance-identifiers, must, |
Michal Vasko | f6aa861 | 2017-03-02 10:52:44 +0100 | [diff] [blame] | 530 | and when) to not be resolved/satisfied during validation. */ |
Radek Krejci | 06f8bb9 | 2017-08-02 15:36:25 +0200 | [diff] [blame] | 531 | #define LYD_OPT_DATA_NO_YANGLIB 0x10000 /**< Ignore (possibly) missing ietf-yang-library data. Applicable only with #LYD_OPT_DATA. */ |
| 532 | #define LYD_OPT_DATA_ADD_YANGLIB 0x20000 /**< Add missing ietf-yang-library data into the validated data tree. Applicable |
| 533 | only with #LYD_OPT_DATA. If some ietf-yang-library data are present, they are |
| 534 | preserved and option is ignored. */ |
Michal Vasko | 20555d8 | 2018-12-12 16:30:50 +0100 | [diff] [blame] | 535 | #define LYD_OPT_VAL_DIFF 0x40000 /**< Flag only for validation, store all the data node changes performed by the validation |
| 536 | in a diff structure. */ |
PavolVican | 832f543 | 2018-02-21 00:54:45 +0100 | [diff] [blame] | 537 | #define LYD_OPT_DATA_TEMPLATE 0x1000000 /**< Data represents YANG data template. */ |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 538 | |
| 539 | /**@} parseroptions */ |
| 540 | |
| 541 | /** |
Michal Vasko | 299f983 | 2017-01-06 13:29:22 +0100 | [diff] [blame] | 542 | * @brief Parse (and validate) data from memory. |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 543 | * |
| 544 | * In case of LY_XML format, the data string is parsed completely. It means that when it contains |
| 545 | * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The |
| 546 | * returned data node is a root of the first tree with other trees connected via the next pointer. |
| 547 | * This behavior can be changed by #LYD_OPT_NOSIBLINGS option. |
| 548 | * |
| 549 | * @param[in] ctx Context to connect with the data tree being built here. |
| 550 | * @param[in] data Serialized data in the specified format. |
| 551 | * @param[in] format Format of the input data to be parsed. |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 552 | * @param[in] options Parser options, see @ref parseroptions. \p format LYD_LYB uses #LYD_OPT_TRUSTED implicitly. |
Michal Vasko | 6b44d71 | 2016-09-12 16:25:46 +0200 | [diff] [blame] | 553 | * @param[in] ... Variable arguments depend on \p options. If they include: |
| 554 | * - #LYD_OPT_DATA: |
| 555 | * - #LYD_OPT_CONFIG: |
| 556 | * - #LYD_OPT_GET: |
| 557 | * - #LYD_OPT_GETCONFIG: |
| 558 | * - #LYD_OPT_EDIT: |
| 559 | * - no variable arguments expected. |
| 560 | * - #LYD_OPT_RPC: |
| 561 | * - #LYD_OPT_NOTIF: |
| 562 | * - struct lyd_node *data_tree - additional data tree that will be used |
| 563 | * when checking any "when" or "must" conditions in the parsed tree that require |
| 564 | * some nodes outside their subtree. It must be a list of top-level elements! |
| 565 | * - #LYD_OPT_RPCREPLY: |
Michal Vasko | 71e43e2 | 2019-02-08 10:38:07 +0100 | [diff] [blame] | 566 | * - const struct ::lyd_node *rpc_act - pointer to the whole RPC or (top-level) action operation |
| 567 | * data tree (the request) of the reply. |
Michal Vasko | 945b96b | 2016-10-18 11:49:12 +0200 | [diff] [blame] | 568 | * - const struct ::lyd_node *data_tree - additional data tree that will be used |
Michal Vasko | 6b44d71 | 2016-09-12 16:25:46 +0200 | [diff] [blame] | 569 | * when checking any "when" or "must" conditions in the parsed tree that require |
| 570 | * some nodes outside their subtree. It must be a list of top-level elements! |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 571 | * @return Pointer to the built data tree or NULL in case of empty \p data. To free the returned structure, |
| 572 | * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error, |
| 573 | * #ly_errno contains appropriate error code (see #LY_ERR). |
| 574 | */ |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 575 | 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] | 576 | |
| 577 | /** |
Michal Vasko | 299f983 | 2017-01-06 13:29:22 +0100 | [diff] [blame] | 578 | * @brief Read (and validate) data from the given file descriptor. |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 579 | * |
| 580 | * \note Current implementation supports only reading data from standard (disk) file, not from sockets, pipes, etc. |
| 581 | * |
| 582 | * In case of LY_XML format, the file content is parsed completely. It means that when it contains |
| 583 | * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The |
| 584 | * returned data node is a root of the first tree with other trees connected via the next pointer. |
| 585 | * This behavior can be changed by #LYD_OPT_NOSIBLINGS option. |
| 586 | * |
| 587 | * @param[in] ctx Context to connect with the data tree being built here. |
| 588 | * @param[in] fd The standard file descriptor of the file containing the data tree in the specified format. |
| 589 | * @param[in] format Format of the input data to be parsed. |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 590 | * @param[in] options Parser options, see @ref parseroptions. \p format LYD_LYB uses #LYD_OPT_TRUSTED implicitly. |
Michal Vasko | 6b44d71 | 2016-09-12 16:25:46 +0200 | [diff] [blame] | 591 | * @param[in] ... Variable arguments depend on \p options. If they include: |
| 592 | * - #LYD_OPT_DATA: |
| 593 | * - #LYD_OPT_CONFIG: |
| 594 | * - #LYD_OPT_GET: |
| 595 | * - #LYD_OPT_GETCONFIG: |
| 596 | * - #LYD_OPT_EDIT: |
| 597 | * - no variable arguments expected. |
| 598 | * - #LYD_OPT_RPC: |
| 599 | * - #LYD_OPT_NOTIF: |
| 600 | * - struct lyd_node *data_tree - additional data tree that will be used |
| 601 | * when checking any "when" or "must" conditions in the parsed tree that require |
| 602 | * some nodes outside their subtree. It must be a list of top-level elements! |
| 603 | * - #LYD_OPT_RPCREPLY: |
Michal Vasko | d55f109 | 2016-10-24 11:21:08 +0200 | [diff] [blame] | 604 | * - const struct ::lyd_node *rpc_act - pointer to the whole RPC or action operation data |
| 605 | * tree (the request) of the reply. |
Michal Vasko | 945b96b | 2016-10-18 11:49:12 +0200 | [diff] [blame] | 606 | * - const struct ::lyd_node *data_tree - additional data tree that will be used |
Michal Vasko | 6b44d71 | 2016-09-12 16:25:46 +0200 | [diff] [blame] | 607 | * when checking any "when" or "must" conditions in the parsed tree that require |
| 608 | * some nodes outside their subtree. It must be a list of top-level elements! |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 609 | * @return Pointer to the built data tree or NULL in case of empty file. To free the returned structure, |
| 610 | * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error, |
| 611 | * #ly_errno contains appropriate error code (see #LY_ERR). |
| 612 | */ |
| 613 | struct lyd_node *lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options, ...); |
| 614 | |
| 615 | /** |
Michal Vasko | 299f983 | 2017-01-06 13:29:22 +0100 | [diff] [blame] | 616 | * @brief Read (and validate) data from the given file path. |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 617 | * |
| 618 | * In case of LY_XML format, the file content is parsed completely. It means that when it contains |
| 619 | * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The |
| 620 | * returned data node is a root of the first tree with other trees connected via the next pointer. |
| 621 | * This behavior can be changed by #LYD_OPT_NOSIBLINGS option. |
| 622 | * |
| 623 | * @param[in] ctx Context to connect with the data tree being built here. |
| 624 | * @param[in] path Path to the file containing the data tree in the specified format. |
| 625 | * @param[in] format Format of the input data to be parsed. |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 626 | * @param[in] options Parser options, see @ref parseroptions. \p format LYD_LYB uses #LYD_OPT_TRUSTED implicitly. |
Michal Vasko | 6b44d71 | 2016-09-12 16:25:46 +0200 | [diff] [blame] | 627 | * @param[in] ... Variable arguments depend on \p options. If they include: |
| 628 | * - #LYD_OPT_DATA: |
| 629 | * - #LYD_OPT_CONFIG: |
| 630 | * - #LYD_OPT_GET: |
| 631 | * - #LYD_OPT_GETCONFIG: |
| 632 | * - #LYD_OPT_EDIT: |
| 633 | * - no variable arguments expected. |
| 634 | * - #LYD_OPT_RPC: |
| 635 | * - #LYD_OPT_NOTIF: |
| 636 | * - struct lyd_node *data_tree - additional data tree that will be used |
| 637 | * when checking any "when" or "must" conditions in the parsed tree that require |
| 638 | * some nodes outside their subtree. It must be a list of top-level elements! |
| 639 | * - #LYD_OPT_RPCREPLY: |
Michal Vasko | d55f109 | 2016-10-24 11:21:08 +0200 | [diff] [blame] | 640 | * - const struct ::lyd_node *rpc_act - pointer to the whole RPC or action operation data |
| 641 | * tree (the request) of the reply. |
Michal Vasko | 945b96b | 2016-10-18 11:49:12 +0200 | [diff] [blame] | 642 | * - const struct ::lyd_node *data_tree - additional data tree that will be used |
Michal Vasko | 6b44d71 | 2016-09-12 16:25:46 +0200 | [diff] [blame] | 643 | * when checking any "when" or "must" conditions in the parsed tree that require |
| 644 | * some nodes outside their subtree. It must be a list of top-level elements! |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 645 | * @return Pointer to the built data tree or NULL in case of empty file. To free the returned structure, |
| 646 | * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error, |
| 647 | * #ly_errno contains appropriate error code (see #LY_ERR). |
| 648 | */ |
| 649 | struct lyd_node *lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options, ...); |
| 650 | |
| 651 | /** |
Michal Vasko | 299f983 | 2017-01-06 13:29:22 +0100 | [diff] [blame] | 652 | * @brief Parse (and validate) XML tree. |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 653 | * |
| 654 | * The output data tree is parsed from the given XML tree previously parsed by one of the |
| 655 | * lyxml_read* functions. |
| 656 | * |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 657 | * 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] | 658 | * or the provided root is a root element of a subtree), all the sibling nodes (previous as well as |
| 659 | * following) are processed as well. The returned data node is a root of the first tree with other |
| 660 | * trees connected via the next pointer. This behavior can be changed by #LYD_OPT_NOSIBLINGS option. |
| 661 | * |
| 662 | * When the function is used with #LYD_OPT_DESTRUCT, all the successfully parsed data including the |
| 663 | * XML \p root and all its siblings (if #LYD_OPT_NOSIBLINGS is not used) are freed. Only with |
| 664 | * #LYD_OPT_DESTRUCT option the \p root pointer is changed - if all the data are parsed, it is set |
| 665 | * to NULL, otherwise it will hold the XML tree without the successfully parsed elements. |
| 666 | * |
| 667 | * The context must be the same as the context used to parse XML tree by lyxml_read* function. |
| 668 | * |
| 669 | * @param[in] ctx Context to connect with the data tree being built here. |
| 670 | * @param[in,out] root XML tree to parse (convert) to data tree. By default, parser do not change the XML tree. However, |
| 671 | * when #LYD_OPT_DESTRUCT is specified in \p options, parser frees all successfully parsed data. |
| 672 | * @param[in] options Parser options, see @ref parseroptions. |
Michal Vasko | 6b44d71 | 2016-09-12 16:25:46 +0200 | [diff] [blame] | 673 | * @param[in] ... Variable arguments depend on \p options. If they include: |
| 674 | * - #LYD_OPT_DATA: |
| 675 | * - #LYD_OPT_CONFIG: |
| 676 | * - #LYD_OPT_GET: |
| 677 | * - #LYD_OPT_GETCONFIG: |
| 678 | * - #LYD_OPT_EDIT: |
| 679 | * - no variable arguments expected. |
| 680 | * - #LYD_OPT_RPC: |
| 681 | * - #LYD_OPT_NOTIF: |
| 682 | * - struct lyd_node *data_tree - additional data tree that will be used |
| 683 | * when checking any "when" or "must" conditions in the parsed tree that require |
| 684 | * some nodes outside their subtree. It must be a list of top-level elements! |
| 685 | * - #LYD_OPT_RPCREPLY: |
Michal Vasko | d55f109 | 2016-10-24 11:21:08 +0200 | [diff] [blame] | 686 | * - const struct ::lyd_node *rpc_act - pointer to the whole RPC or action operation data |
| 687 | * tree (the request) of the reply. |
Michal Vasko | 945b96b | 2016-10-18 11:49:12 +0200 | [diff] [blame] | 688 | * - const struct ::lyd_node *data_tree - additional data tree that will be used |
Michal Vasko | 6b44d71 | 2016-09-12 16:25:46 +0200 | [diff] [blame] | 689 | * when checking any "when" or "must" conditions in the parsed tree that require |
| 690 | * some nodes outside their subtree. It must be a list of top-level elements! |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 691 | * @return Pointer to the built data tree or NULL in case of empty \p root. To free the returned structure, |
| 692 | * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error, |
| 693 | * #ly_errno contains appropriate error code (see #LY_ERR). |
| 694 | */ |
| 695 | struct lyd_node *lyd_parse_xml(struct ly_ctx *ctx, struct lyxml_elem **root, int options,...); |
| 696 | |
| 697 | /** |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 698 | * @brief Create a new container node in a data tree. |
| 699 | * |
Michal Vasko | 299f983 | 2017-01-06 13:29:22 +0100 | [diff] [blame] | 700 | * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators). |
| 701 | * |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 702 | * @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] | 703 | * @param[in] module Module with the node being created. |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 704 | * @param[in] name Schema node name of the new data node. The node can be #LYS_CONTAINER, #LYS_LIST, |
Michal Vasko | 945b96b | 2016-10-18 11:49:12 +0200 | [diff] [blame] | 705 | * #LYS_NOTIF, #LYS_RPC, or #LYS_ACTION. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 706 | * @return New node, NULL on error. |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 707 | */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 708 | 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] | 709 | |
| 710 | /** |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 711 | * @brief Create a new leaf or leaflist node in a data tree with a string value that is converted to |
| 712 | * the actual value. |
| 713 | * |
Michal Vasko | 299f983 | 2017-01-06 13:29:22 +0100 | [diff] [blame] | 714 | * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators). |
| 715 | * |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 716 | * @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] | 717 | * @param[in] module Module with the node being created. |
| 718 | * @param[in] name Schema node name of the new data node. |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 719 | * @param[in] val_str String form of the value of the node being created. In case the type is #LY_TYPE_INST |
| 720 | * 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] | 721 | * @return New node, NULL on error. |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 722 | */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 723 | 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] | 724 | const char *val_str); |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 725 | |
| 726 | /** |
Radek Krejci | b9b4d00 | 2016-01-18 13:08:51 +0100 | [diff] [blame] | 727 | * @brief Change value of a leaf node. |
| 728 | * |
Michal Vasko | 299f983 | 2017-01-06 13:29:22 +0100 | [diff] [blame] | 729 | * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators). |
| 730 | * |
Radek Krejci | b9b4d00 | 2016-01-18 13:08:51 +0100 | [diff] [blame] | 731 | * Despite the prototype allows to provide a leaflist node as \p leaf parameter, only leafs are accepted. |
Michal Vasko | 2da8e04 | 2018-05-25 11:10:13 +0200 | [diff] [blame] | 732 | * Also, the leaf will never be default after calling this function successfully. |
Radek Krejci | b9b4d00 | 2016-01-18 13:08:51 +0100 | [diff] [blame] | 733 | * |
| 734 | * @param[in] leaf A leaf node to change. |
| 735 | * @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 |
| 736 | * or #LY_TYPE_IDENT, JSON node-id format is expected (nodes are prefixed with module names, not XML namespaces). |
Michal Vasko | 3c0eb75 | 2018-02-08 16:09:19 +0100 | [diff] [blame] | 737 | * @return 0 if the leaf was changed successfully (either its value changed or at least its default flag was cleared), |
| 738 | * <0 on error, |
fredgan | 31f56fd | 2019-09-26 14:34:03 +0800 | [diff] [blame^] | 739 | * 1 if the (canonical) value matched the original one and no value neither default flag change occurred. |
Radek Krejci | b9b4d00 | 2016-01-18 13:08:51 +0100 | [diff] [blame] | 740 | */ |
| 741 | int lyd_change_leaf(struct lyd_node_leaf_list *leaf, const char *val_str); |
| 742 | |
| 743 | /** |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 744 | * @brief Create a new anydata or anyxml node in a data tree. |
| 745 | * |
Michal Vasko | 299f983 | 2017-01-06 13:29:22 +0100 | [diff] [blame] | 746 | * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators). |
| 747 | * |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 748 | * This function is supposed to be a replacement for the lyd_new_anyxml_str() and lyd_new_anyxml_xml(). |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 749 | * |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 750 | * @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] | 751 | * @param[in] module Module with the node being created. |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 752 | * @param[in] name Schema node name of the new data node. The schema node determines if the anydata or anyxml node |
| 753 | * is created. |
| 754 | * @param[in] value Pointer to the value data to be stored in the anydata/anyxml node. The type of the data is |
| 755 | * determined according to the \p value_type parameter. |
| 756 | * @param[in] value_type Type of the provided data \p value. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 757 | * @return New node, NULL on error. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 758 | */ |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 759 | struct lyd_node *lyd_new_anydata(struct lyd_node *parent, const struct lys_module *module, const char *name, |
| 760 | void *value, LYD_ANYDATA_VALUETYPE value_type); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 761 | |
| 762 | /** |
Michal Vasko | 945b96b | 2016-10-18 11:49:12 +0200 | [diff] [blame] | 763 | * @brief Create a new container node in a data tree. Ignore RPC/action input nodes and instead use RPC/action output ones. |
Michal Vasko | 0df122f | 2015-12-14 13:38:21 +0100 | [diff] [blame] | 764 | * |
Michal Vasko | 299f983 | 2017-01-06 13:29:22 +0100 | [diff] [blame] | 765 | * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators). |
| 766 | * |
Michal Vasko | 98a5a74 | 2016-05-11 11:02:56 +0200 | [diff] [blame] | 767 | * @param[in] parent Parent node for the node being created. NULL in case of creating top level element. |
| 768 | * @param[in] module Module with the node being created. |
Michal Vasko | 945b96b | 2016-10-18 11:49:12 +0200 | [diff] [blame] | 769 | * @param[in] name Schema node name of the new data node. The node should only be #LYS_CONTAINER or #LYS_LIST, |
| 770 | * but accepted are also #LYS_NOTIF, #LYS_RPC, or #LYS_ACTION. |
Michal Vasko | 0df122f | 2015-12-14 13:38:21 +0100 | [diff] [blame] | 771 | * @return New node, NULL on error. |
| 772 | */ |
Michal Vasko | 98a5a74 | 2016-05-11 11:02:56 +0200 | [diff] [blame] | 773 | struct lyd_node *lyd_new_output(struct lyd_node *parent, const struct lys_module *module, const char *name); |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame] | 774 | |
| 775 | /** |
Michal Vasko | 98a5a74 | 2016-05-11 11:02:56 +0200 | [diff] [blame] | 776 | * @brief Create a new leaf or leaflist node in a data tree with a string value that is converted to |
Michal Vasko | 945b96b | 2016-10-18 11:49:12 +0200 | [diff] [blame] | 777 | * the actual value. Ignore RPC/action input nodes and instead use RPC/action output ones. |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame] | 778 | * |
Michal Vasko | 299f983 | 2017-01-06 13:29:22 +0100 | [diff] [blame] | 779 | * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators). |
| 780 | * |
Michal Vasko | 98a5a74 | 2016-05-11 11:02:56 +0200 | [diff] [blame] | 781 | * @param[in] parent Parent node for the node being created. NULL in case of creating top level element. |
| 782 | * @param[in] module Module with the node being created. |
| 783 | * @param[in] name Schema node name of the new data node. |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame] | 784 | * @param[in] val_str String form of the value of the node being created. In case the type is #LY_TYPE_INST |
| 785 | * or #LY_TYPE_IDENT, JSON node-id format is expected (nodes are prefixed with module names, not XML namespaces). |
| 786 | * @return New node, NULL on error. |
| 787 | */ |
Michal Vasko | 98a5a74 | 2016-05-11 11:02:56 +0200 | [diff] [blame] | 788 | struct lyd_node *lyd_new_output_leaf(struct lyd_node *parent, const struct lys_module *module, const char *name, |
| 789 | const char *val_str); |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame] | 790 | |
| 791 | /** |
Michal Vasko | 945b96b | 2016-10-18 11:49:12 +0200 | [diff] [blame] | 792 | * @brief Create a new anydata or anyxml node in a data tree. Ignore RPC/action input nodes and instead use |
| 793 | * RPC/action output ones. |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame] | 794 | * |
Michal Vasko | 299f983 | 2017-01-06 13:29:22 +0100 | [diff] [blame] | 795 | * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators). |
| 796 | * |
Michal Vasko | 98a5a74 | 2016-05-11 11:02:56 +0200 | [diff] [blame] | 797 | * @param[in] parent Parent node for the node being created. NULL in case of creating top level element. |
| 798 | * @param[in] module Module with the node being created. |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 799 | * @param[in] name Schema node name of the new data node. The schema node determines if the anydata or anyxml node |
| 800 | * is created. |
| 801 | * @param[in] value Pointer to the value data to be stored in the anydata/anyxml node. The type of the data is |
| 802 | * determined according to the \p value_type parameter. Data are supposed to be dynamically allocated. |
| 803 | * Since it is directly attached into the created data node, caller is supposed to not manipulate with |
| 804 | * the data after a successful call (including calling free() on the provided data). |
| 805 | * @param[in] value_type Type of the provided data \p value. |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame] | 806 | * @return New node, NULL on error. |
| 807 | */ |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 808 | struct lyd_node *lyd_new_output_anydata(struct lyd_node *parent, const struct lys_module *module, const char *name, |
| 809 | void *value, LYD_ANYDATA_VALUETYPE value_type); |
Michal Vasko | 0df122f | 2015-12-14 13:38:21 +0100 | [diff] [blame] | 810 | |
| 811 | /** |
PavolVican | 832f543 | 2018-02-21 00:54:45 +0100 | [diff] [blame] | 812 | * @brief Create a new yang-data template in a data tree. It creates container, which name is in third parameter. |
| 813 | * |
| 814 | * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators). |
| 815 | * |
| 816 | * @param[in] module Module with the node being created. |
| 817 | * @param[in] name_template Yang-data template name. This name is used for searching of yang-data instance. |
| 818 | * @param[in] name Schema node name of the new data node. This node is container. |
| 819 | * @return New node, NULL on error. |
| 820 | */ |
| 821 | struct lyd_node *lyd_new_yangdata(const struct lys_module *module, const char *name_template, const char *name); |
| 822 | |
| 823 | /** |
Michal Vasko | f529928 | 2016-03-16 13:32:02 +0100 | [diff] [blame] | 824 | * @defgroup pathoptions Data path creation options |
| 825 | * @ingroup datatree |
| 826 | * |
| 827 | * Various options to change lyd_new_path() behavior. |
| 828 | * |
| 829 | * Default behavior: |
Michal Vasko | 3c0eb75 | 2018-02-08 16:09:19 +0100 | [diff] [blame] | 830 | * - if the target node already exists (and is not default), an error is returned. |
Michal Vasko | 9db078d | 2016-03-23 11:08:51 +0100 | [diff] [blame] | 831 | * - the whole path to the target node is created (with any missing parents) if necessary. |
Michal Vasko | 2411b94 | 2016-03-23 13:50:03 +0100 | [diff] [blame] | 832 | * - RPC output schema children are completely ignored in all modules. Input is searched and nodes created normally. |
Michal Vasko | f529928 | 2016-03-16 13:32:02 +0100 | [diff] [blame] | 833 | * @{ |
| 834 | */ |
| 835 | |
Michal Vasko | 3c0eb75 | 2018-02-08 16:09:19 +0100 | [diff] [blame] | 836 | #define LYD_PATH_OPT_UPDATE 0x01 /**< If the target node exists, is a leaf, and it is updated with a new value or its |
| 837 | default flag is changed, it is returned. If the target node exists and is not |
| 838 | a leaf or generally no change occurs in the \p data_tree, NULL is returned and no error set. */ |
Michal Vasko | ae5ad2f | 2018-10-19 11:21:30 +0200 | [diff] [blame] | 839 | #define LYD_PATH_OPT_NOPARENT 0x02 /**< If any parents of the target node do not exist, return an error instead of implicitly |
| 840 | creating them. */ |
Michal Vasko | 945b96b | 2016-10-18 11:49:12 +0200 | [diff] [blame] | 841 | #define LYD_PATH_OPT_OUTPUT 0x04 /**< Changes the behavior to ignoring RPC/action input schema nodes and using only output ones. */ |
Michal Vasko | ae5ad2f | 2018-10-19 11:21:30 +0200 | [diff] [blame] | 842 | #define LYD_PATH_OPT_DFLT 0x08 /**< The created node (nodes, if also creating the parents) is a default one. If working with |
| 843 | data tree of type #LYD_OPT_DATA, #LYD_OPT_CONFIG, #LYD_OPT_RPC, #LYD_OPT_RPCREPLY, or |
| 844 | #LYD_OPT_NOTIF, this flag is never needed and therefore should not be used. However, if |
| 845 | the tree is #LYD_OPT_GET, #LYD_OPT_GETCONFIG, or #LYD_OPT_EDIT, the default nodes are not |
| 846 | created during validation and using this flag one can set them (see @ref howtodatawd). */ |
| 847 | #define LYD_PATH_OPT_NOPARENTRET 0x10 /**< Changes the return value in the way that even if some parents were created in |
| 848 | addition to the path-referenced node, the path-referenced node will always be returned. */ |
Michal Vasko | f932949 | 2018-11-27 12:29:55 +0100 | [diff] [blame] | 849 | #define LYD_PATH_OPT_EDIT 0x20 /**< Allows the creation of special leaves without value. These leaves are valid if used |
| 850 | in a NETCONF edit-config with delete/remove operation. */ |
Michal Vasko | f529928 | 2016-03-16 13:32:02 +0100 | [diff] [blame] | 851 | |
| 852 | /** @} pathoptions */ |
| 853 | |
| 854 | /** |
| 855 | * @brief Create a new data node based on a simple XPath. |
| 856 | * |
Michal Vasko | 299f983 | 2017-01-06 13:29:22 +0100 | [diff] [blame] | 857 | * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators). |
| 858 | * |
Michal Vasko | 8d18ef5 | 2016-04-06 12:21:46 +0200 | [diff] [blame] | 859 | * The new node is normally inserted at the end, either as the last child of a parent or as the last sibling |
| 860 | * if working with top-level elements. However, when manipulating RPC input or output, schema ordering is |
Michal Vasko | 98a5a74 | 2016-05-11 11:02:56 +0200 | [diff] [blame] | 861 | * required and always guaranteed. |
Michal Vasko | 58f74f1 | 2016-03-24 13:26:06 +0100 | [diff] [blame] | 862 | * |
Michal Vasko | 8c41964 | 2016-04-13 14:22:01 +0200 | [diff] [blame] | 863 | * If \p path points to a list key and the list does not exist, the key value from the predicate is used |
| 864 | * and \p value is ignored. |
| 865 | * |
Michal Vasko | 7800b24 | 2018-04-03 11:15:05 +0200 | [diff] [blame] | 866 | * @param[in] data_tree Existing data tree to add to/modify (including siblings). If creating RPCs/actions, there |
| 867 | * should only be one RPC/action and either input or output, not both. Can be NULL. |
Michal Vasko | f529928 | 2016-03-16 13:32:02 +0100 | [diff] [blame] | 868 | * @param[in] ctx Context to use. Mandatory if \p data_tree is NULL. |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 869 | * @param[in] path Simple data path (see @ref howtoxpath). List nodes can have predicates, one for each list key |
| 870 | * in the correct order and with its value as well or using specific instance position, leaves and leaf-lists |
Michal Vasko | 310bc58 | 2018-05-22 10:47:59 +0200 | [diff] [blame] | 871 | * can have predicates too that have preference over \p value. When specifying an identityref value in a predicate, |
| 872 | * you MUST use the module name as the value prefix! |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 873 | * @param[in] value Value of the new leaf/lealf-list (const char*). If creating anydata or anyxml, the following |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 874 | * \p value_type parameter is required to be specified correctly. If creating nodes of other types, the |
| 875 | * parameter is ignored. |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 876 | * @param[in] value_type Type of the provided \p value parameter in case of creating anydata or anyxml node. |
Michal Vasko | f529928 | 2016-03-16 13:32:02 +0100 | [diff] [blame] | 877 | * @param[in] options Bitmask of options flags, see @ref pathoptions. |
Michal Vasko | 8c41964 | 2016-04-13 14:22:01 +0200 | [diff] [blame] | 878 | * @return First created (or updated with #LYD_PATH_OPT_UPDATE) node, |
Michal Vasko | 17bb490 | 2016-04-05 15:20:51 +0200 | [diff] [blame] | 879 | * NULL if #LYD_PATH_OPT_UPDATE was used and the full path exists or the leaf original value matches \p value, |
Michal Vasko | 72d3510 | 2016-03-31 10:03:38 +0200 | [diff] [blame] | 880 | * NULL and ly_errno is set on error. |
Michal Vasko | f529928 | 2016-03-16 13:32:02 +0100 | [diff] [blame] | 881 | */ |
Michal Vasko | 73304a2 | 2019-01-22 09:05:22 +0100 | [diff] [blame] | 882 | struct lyd_node *lyd_new_path(struct lyd_node *data_tree, const struct ly_ctx *ctx, const char *path, void *value, |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 883 | LYD_ANYDATA_VALUETYPE value_type, int options); |
Michal Vasko | f529928 | 2016-03-16 13:32:02 +0100 | [diff] [blame] | 884 | |
| 885 | /** |
Michal Vasko | ae5a53e | 2017-01-05 10:33:41 +0100 | [diff] [blame] | 886 | * @brief Learn the relative instance position of a list or leaf-list within other instances of the |
| 887 | * same schema node. |
| 888 | * |
| 889 | * @param[in] node List or leaf-list to get the position of. |
| 890 | * @return 0 on error or positive integer of the instance position. |
| 891 | */ |
| 892 | unsigned int lyd_list_pos(const struct lyd_node *node); |
| 893 | |
| 894 | /** |
Michal Vasko | 85225a2 | 2018-11-19 13:24:38 +0100 | [diff] [blame] | 895 | * @defgroup dupoptions Data duplication options |
| 896 | * @ingroup datatree |
| 897 | * |
| 898 | * Various options to change lyd_dup() behavior. |
| 899 | * |
| 900 | * Default behavior: |
| 901 | * - only the specified node is duplicated without siblings, parents, or children. |
| 902 | * - all the attributes of the duplicated nodes are also duplicated. |
| 903 | * @{ |
| 904 | */ |
| 905 | |
| 906 | #define LYD_DUP_OPT_RECURSIVE 0x01 /**< Duplicate not just the node but also all the children. */ |
| 907 | #define LYD_DUP_OPT_NO_ATTR 0x02 /**< Do not duplicate attributes of any node. */ |
| 908 | #define LYD_DUP_OPT_WITH_PARENTS 0x04 /**< If a nested node is being duplicated, duplicate also all the parents. |
| 909 | Keys are also duplicated for lists. Return value does not change! */ |
Michal Vasko | 5e16cd2 | 2019-03-08 16:33:09 +0100 | [diff] [blame] | 910 | #define LYD_DUP_OPT_WITH_KEYS 0x08 /**< If a lits key is being duplicated non-recursively, duplicate its keys. |
| 911 | Ignored if used with #LYD_DUP_OPT_RECURSIVE. Return value does not change! */ |
Michal Vasko | 6f24903 | 2019-03-18 10:50:23 +0100 | [diff] [blame] | 912 | #define LYD_DUP_OPT_WITH_WHEN 0x10 /**< Also copy any when evaluation state flags. This is useful in case the copied |
| 913 | nodes are actually still part of the same datastore meaning no dependency data |
| 914 | could have changed. Otherwise nothing is assumed about the copied node when |
| 915 | state and it is evaluated from scratch during validation. */ |
Michal Vasko | 85225a2 | 2018-11-19 13:24:38 +0100 | [diff] [blame] | 916 | |
| 917 | /** @} dupoptions */ |
| 918 | |
| 919 | /** |
Michal Vasko | 39dc899 | 2018-04-03 11:32:00 +0200 | [diff] [blame] | 920 | * @brief Create a copy of the specified data tree \p node. Schema references are kept the same. Use carefully, |
| 921 | * since libyang silently creates default nodes, it is always better to use lyd_dup_withsiblings() to duplicate |
| 922 | * the complete data tree. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 923 | * |
Michal Vasko | 299f983 | 2017-01-06 13:29:22 +0100 | [diff] [blame] | 924 | * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators). |
Michal Vasko | 2f95fe6 | 2016-12-01 09:36:08 +0100 | [diff] [blame] | 925 | * |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 926 | * @param[in] node Data tree node to be duplicated. |
Michal Vasko | 85225a2 | 2018-11-19 13:24:38 +0100 | [diff] [blame] | 927 | * @param[in] options Bitmask of options flags, see @ref dupoptions. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 928 | * @return Created copy of the provided data \p node. |
| 929 | */ |
Michal Vasko | 85225a2 | 2018-11-19 13:24:38 +0100 | [diff] [blame] | 930 | struct lyd_node *lyd_dup(const struct lyd_node *node, int options); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 931 | |
| 932 | /** |
Michal Vasko | 39dc899 | 2018-04-03 11:32:00 +0200 | [diff] [blame] | 933 | * @brief Create a copy of the specified data tree and all its siblings (preceding as well as following). |
| 934 | * Schema references are kept the same. |
| 935 | * |
| 936 | * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators). |
| 937 | * |
| 938 | * @param[in] node Data tree sibling node to be duplicated. |
Michal Vasko | 85225a2 | 2018-11-19 13:24:38 +0100 | [diff] [blame] | 939 | * @param[in] options Bitmask of options flags, see @ref dupoptions. |
Michal Vasko | 39dc899 | 2018-04-03 11:32:00 +0200 | [diff] [blame] | 940 | * @return Created copy of the provided data \p node and all of its siblings. |
| 941 | */ |
Michal Vasko | 85225a2 | 2018-11-19 13:24:38 +0100 | [diff] [blame] | 942 | struct lyd_node *lyd_dup_withsiblings(const struct lyd_node *node, int options); |
Michal Vasko | 39dc899 | 2018-04-03 11:32:00 +0200 | [diff] [blame] | 943 | |
| 944 | /** |
Radek Krejci | a17c85c | 2017-01-06 12:22:34 +0100 | [diff] [blame] | 945 | * @brief Create a copy of the specified data tree \p node in the different context. All the |
| 946 | * schema references and strings are re-mapped into the specified context. |
| 947 | * |
| 948 | * If the target context does not contain the schemas used in the source data tree, error |
| 949 | * is raised and the new data tree is not created. |
| 950 | * |
| 951 | * @param[in] node Data tree node to be duplicated. |
Michal Vasko | 85225a2 | 2018-11-19 13:24:38 +0100 | [diff] [blame] | 952 | * @param[in] options Bitmask of options flags, see @ref dupoptions. |
Radek Krejci | a17c85c | 2017-01-06 12:22:34 +0100 | [diff] [blame] | 953 | * @param[in] ctx Target context for the duplicated data. |
| 954 | * @return Created copy of the provided data \p node. |
| 955 | */ |
Michal Vasko | 85225a2 | 2018-11-19 13:24:38 +0100 | [diff] [blame] | 956 | struct lyd_node *lyd_dup_to_ctx(const struct lyd_node *node, int options, struct ly_ctx *ctx); |
Radek Krejci | a17c85c | 2017-01-06 12:22:34 +0100 | [diff] [blame] | 957 | |
| 958 | /** |
Michal Vasko | 299f983 | 2017-01-06 13:29:22 +0100 | [diff] [blame] | 959 | * @brief Merge a (sub)tree into a data tree. |
| 960 | * |
| 961 | * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators). |
| 962 | * |
| 963 | * Missing nodes are merged, leaf values updated. |
Radek Krejci | 8f4eba5 | 2017-01-06 15:32:41 +0100 | [diff] [blame] | 964 | * |
Michal Vasko | 45fb282 | 2016-04-18 13:32:17 +0200 | [diff] [blame] | 965 | * If \p target and \p source do not share the top-level schema node, even if they |
| 966 | * are from different modules, \p source parents up to top-level node will be created and |
| 967 | * linked to the \p target (but only containers can be created this way, lists need keys, |
| 968 | * so if lists are missing, an error will be returned). |
| 969 | * |
Radek Krejci | 2ffe993 | 2017-01-06 16:29:47 +0100 | [diff] [blame] | 970 | * If the source data tree is in a different context, the resulting data are placed into the context |
| 971 | * of the target tree. |
Michal Vasko | 45fb282 | 2016-04-18 13:32:17 +0200 | [diff] [blame] | 972 | * |
Michal Vasko | cf6dc7e | 2016-04-18 16:00:37 +0200 | [diff] [blame] | 973 | * @param[in] target Top-level (or an RPC output child) data tree to merge to. Must be valid. |
Michal Vasko | 45fb282 | 2016-04-18 13:32:17 +0200 | [diff] [blame] | 974 | * @param[in] source Data tree to merge \p target with. Must be valid (at least as a subtree). |
Radek Krejci | 2ffe993 | 2017-01-06 16:29:47 +0100 | [diff] [blame] | 975 | * @param[in] options Bitmask of the following option flags: |
Michal Vasko | 0300b53 | 2016-09-14 12:16:02 +0200 | [diff] [blame] | 976 | * - #LYD_OPT_DESTRUCT - spend \p source in the function, otherwise \p source is left untouched, |
| 977 | * - #LYD_OPT_NOSIBLINGS - merge only the \p source subtree (ignore siblings), otherwise merge |
fredgan | 31f56fd | 2019-09-26 14:34:03 +0800 | [diff] [blame^] | 978 | * \p source and all its succeeding siblings (preceding ones are still ignored!), |
Michal Vasko | 0300b53 | 2016-09-14 12:16:02 +0200 | [diff] [blame] | 979 | * - #LYD_OPT_EXPLICIT - when merging an explicitly set node and a default node, always put |
| 980 | * the explicit node into \p target, otherwise the node which is in \p source is used. |
Michal Vasko | 45fb282 | 2016-04-18 13:32:17 +0200 | [diff] [blame] | 981 | * @return 0 on success, nonzero in case of an error. |
| 982 | */ |
| 983 | int lyd_merge(struct lyd_node *target, const struct lyd_node *source, int options); |
| 984 | |
Radek Krejci | 2ffe993 | 2017-01-06 16:29:47 +0100 | [diff] [blame] | 985 | /** |
| 986 | * @brief Same as lyd_merge(), but moves the resulting data into the specified context. |
| 987 | * |
| 988 | * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators). |
| 989 | * |
| 990 | * @param[in] trg Top-level (or an RPC output child) data tree to merge to. Must be valid. If its context |
| 991 | * differs from the specified \p ctx of the result, the provided data tree is freed and the new |
Radek Krejci | ab80e3a | 2017-01-09 13:07:31 +0100 | [diff] [blame] | 992 | * tree in the required context is returned on success. To keep the \p trg tree, convert it to the |
| 993 | * target context using lyd_dup_to_ctx() and then call lyd_merge() instead of lyd_merge_to_ctx(). |
Radek Krejci | 2ffe993 | 2017-01-06 16:29:47 +0100 | [diff] [blame] | 994 | * @param[in] src Data tree to merge \p target with. Must be valid (at least as a subtree). |
| 995 | * @param[in] options Bitmask of the following option flags: |
| 996 | * - #LYD_OPT_DESTRUCT - spend \p source in the function, otherwise \p source is left untouched, |
| 997 | * - #LYD_OPT_NOSIBLINGS - merge only the \p source subtree (ignore siblings), otherwise merge |
Renato Westphal | 99fded7 | 2018-10-22 14:44:24 -0200 | [diff] [blame] | 998 | * \p source and all its succeeding siblings (preceding ones are still ignored!), |
Radek Krejci | 2ffe993 | 2017-01-06 16:29:47 +0100 | [diff] [blame] | 999 | * - #LYD_OPT_EXPLICIT - when merging an explicitly set node and a default node, always put |
| 1000 | * the explicit node into \p target, otherwise the node which is in \p source is used. |
| 1001 | * @param[in] ctx Target context in which the result will be created. Note that the successful merge requires to have |
| 1002 | * all the used modules in the source and target data trees loaded in the target context. |
| 1003 | * @return 0 on success, nonzero in case of an error. |
| 1004 | */ |
| 1005 | int lyd_merge_to_ctx(struct lyd_node **trg, const struct lyd_node *src, int options, struct ly_ctx *ctx); |
| 1006 | |
Michal Vasko | 0300b53 | 2016-09-14 12:16:02 +0200 | [diff] [blame] | 1007 | #define LYD_OPT_EXPLICIT 0x0100 |
| 1008 | |
Michal Vasko | 45fb282 | 2016-04-18 13:32:17 +0200 | [diff] [blame] | 1009 | /** |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1010 | * @brief Insert the \p node element as child to the \p parent element. The \p node is inserted as a last child of the |
| 1011 | * \p parent. |
| 1012 | * |
Michal Vasko | 299f983 | 2017-01-06 13:29:22 +0100 | [diff] [blame] | 1013 | * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators). |
| 1014 | * |
Michal Vasko | b6c51f1 | 2016-09-14 12:15:11 +0200 | [diff] [blame] | 1015 | * - if the node is part of some other tree, it is automatically unlinked. |
| 1016 | * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted. |
| 1017 | * - if the key of a list is being inserted, it is placed into a correct position instead of being placed as the last |
Radek Krejci | a1c33bf | 2016-09-07 12:38:49 +0200 | [diff] [blame] | 1018 | * element. |
Michal Vasko | b6c51f1 | 2016-09-14 12:15:11 +0200 | [diff] [blame] | 1019 | * - if the target tree includes the default instance of the node being inserted, the default node is silently replaced |
Michal Vasko | 3c12682 | 2016-09-22 13:48:42 +0200 | [diff] [blame] | 1020 | * by the new node. |
| 1021 | * - if a default node is being inserted and the target tree already contains non-default instance, the existing |
| 1022 | * instance is silently replaced. If it contains the exact same default node, it is replaced as well. |
Michal Vasko | b6c51f1 | 2016-09-14 12:15:11 +0200 | [diff] [blame] | 1023 | * - if a non-default node is being inserted and there is already its non-default instance in the target tree, the new |
Radek Krejci | fd0bcf0 | 2016-09-09 13:28:34 +0200 | [diff] [blame] | 1024 | * node is inserted and it is up to the caller to solve the presence of multiple instances afterwards. |
| 1025 | * |
| 1026 | * Note that this function differs from lyd_insert_before() and lyd_insert_after() because the position of the |
| 1027 | * node being inserted is determined automatically according to the rules described above. In contrast to |
| 1028 | * lyd_insert_parent(), lyd_insert() can not be used for top-level elements since the \p parent parameter must not be |
Michal Vasko | 3c12682 | 2016-09-22 13:48:42 +0200 | [diff] [blame] | 1029 | * NULL. If inserting something larger and not fitting the mentioned use-cases (or simply if unsure), you can always |
| 1030 | * use lyd_merge(), it should be able to handle any situation. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1031 | * |
| 1032 | * @param[in] parent Parent node for the \p node being inserted. |
| 1033 | * @param[in] node The node being inserted. |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 1034 | * @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] | 1035 | * in the data tree. |
| 1036 | */ |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 1037 | int lyd_insert(struct lyd_node *parent, struct lyd_node *node); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1038 | |
| 1039 | /** |
Radek Krejci | fd0bcf0 | 2016-09-09 13:28:34 +0200 | [diff] [blame] | 1040 | * @brief Insert the \p node element as a last sibling of the specified \p sibling element. |
| 1041 | * |
Michal Vasko | 299f983 | 2017-01-06 13:29:22 +0100 | [diff] [blame] | 1042 | * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators). |
| 1043 | * |
Michal Vasko | b6c51f1 | 2016-09-14 12:15:11 +0200 | [diff] [blame] | 1044 | * - if the node is part of some other tree, it is automatically unlinked. |
| 1045 | * - if the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted. |
| 1046 | * - if the key of a list is being inserted, it is placed into a correct position instead of being placed as the last |
Radek Krejci | fd0bcf0 | 2016-09-09 13:28:34 +0200 | [diff] [blame] | 1047 | * element. |
Michal Vasko | b6c51f1 | 2016-09-14 12:15:11 +0200 | [diff] [blame] | 1048 | * - if the target tree includes the default instance of the node being inserted, the default node is silently replaced |
Michal Vasko | 3c12682 | 2016-09-22 13:48:42 +0200 | [diff] [blame] | 1049 | * by the new node. |
| 1050 | * - if a default node is being inserted and the target tree already contains non-default instance, the existing |
| 1051 | * instance is silently replaced. If it contains the exact same default node, it is replaced as well. |
Michal Vasko | b6c51f1 | 2016-09-14 12:15:11 +0200 | [diff] [blame] | 1052 | * - if a non-default node is being inserted and there is already its non-default instance in the target tree, the new |
| 1053 | * node is inserted and it is up to the caller to solve the presence of multiple instances afterwards. |
Radek Krejci | fd0bcf0 | 2016-09-09 13:28:34 +0200 | [diff] [blame] | 1054 | * |
| 1055 | * Note that this function differs from lyd_insert_before() and lyd_insert_after() because the position of the |
| 1056 | * node being inserted is determined automatically as in the case of lyd_insert(). In contrast to lyd_insert(), |
Michal Vasko | 3c12682 | 2016-09-22 13:48:42 +0200 | [diff] [blame] | 1057 | * lyd_insert_sibling() can be used to insert top-level elements. If inserting something larger and not fitting |
| 1058 | * the mentioned use-cases (or simply if unsure), you can always use lyd_merge(), it should be able to handle |
| 1059 | * any situation. |
Radek Krejci | fd0bcf0 | 2016-09-09 13:28:34 +0200 | [diff] [blame] | 1060 | * |
| 1061 | * @param[in,out] sibling Sibling node as a reference where to insert the \p node. When function succeeds, the sibling |
| 1062 | * is always set to point to the first sibling node. Note that in some cases described above, the provided sibling |
| 1063 | * node could be removed from the tree. |
| 1064 | * @param[in] node The node being inserted. |
| 1065 | * @return 0 on success, nonzero in case of error, e.g. when the node is being inserted to an inappropriate place |
| 1066 | * in the data tree. |
| 1067 | */ |
| 1068 | int lyd_insert_sibling(struct lyd_node **sibling, struct lyd_node *node); |
| 1069 | |
| 1070 | /** |
Michal Vasko | 3f7dba1 | 2015-10-15 13:09:27 +0200 | [diff] [blame] | 1071 | * @brief Insert the \p node element after the \p sibling element. If \p node and \p siblings are already |
Michal Vasko | 299f983 | 2017-01-06 13:29:22 +0100 | [diff] [blame] | 1072 | * siblings (just moving \p node position). |
| 1073 | * |
| 1074 | * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators). |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1075 | * |
Michal Vasko | b6c51f1 | 2016-09-14 12:15:11 +0200 | [diff] [blame] | 1076 | * - if the target tree includes the default instance of the node being inserted, the default node is silently removed. |
Michal Vasko | 3c12682 | 2016-09-22 13:48:42 +0200 | [diff] [blame] | 1077 | * - if a default node is being inserted and the target tree already contains non-default instance, the existing |
| 1078 | * instance is removed. If it contains the exact same default node, it is removed as well. |
Michal Vasko | b6c51f1 | 2016-09-14 12:15:11 +0200 | [diff] [blame] | 1079 | * - if a non-default node is being inserted and there is already its non-default instance in the target tree, the new |
| 1080 | * node is inserted and it is up to the caller to solve the presence of multiple instances afterwards. |
| 1081 | * |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1082 | * @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] | 1083 | * @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] | 1084 | * @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] | 1085 | * in the data tree. |
| 1086 | */ |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 1087 | int lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1088 | |
| 1089 | /** |
Radek Krejci | 20a5f29 | 2016-02-09 15:04:49 +0100 | [diff] [blame] | 1090 | * @brief Insert the \p node element after the \p sibling element. If \p node and \p siblings are already |
Michal Vasko | 299f983 | 2017-01-06 13:29:22 +0100 | [diff] [blame] | 1091 | * siblings (just moving \p node position). |
| 1092 | * |
| 1093 | * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators). |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1094 | * |
Michal Vasko | b6c51f1 | 2016-09-14 12:15:11 +0200 | [diff] [blame] | 1095 | * - if the target tree includes the default instance of the node being inserted, the default node is silently removed. |
Michal Vasko | 3c12682 | 2016-09-22 13:48:42 +0200 | [diff] [blame] | 1096 | * - if a default node is being inserted and the target tree already contains non-default instance, the existing |
| 1097 | * instance is removed. If it contains the exact same default node, it is removed as well. |
Michal Vasko | b6c51f1 | 2016-09-14 12:15:11 +0200 | [diff] [blame] | 1098 | * - if a non-default node is being inserted and there is already its non-default instance in the target tree, the new |
| 1099 | * node is inserted and it is up to the caller to solve the presence of multiple instances afterwards. |
| 1100 | * |
Michal Vasko | 3f7dba1 | 2015-10-15 13:09:27 +0200 | [diff] [blame] | 1101 | * @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] | 1102 | * are already siblings (just moving \p node position), skip validation. |
Radek Krejci | 20a5f29 | 2016-02-09 15:04:49 +0100 | [diff] [blame] | 1103 | * @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] | 1104 | * @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] | 1105 | * in the data tree. |
| 1106 | */ |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 1107 | int lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node); |
| 1108 | |
| 1109 | /** |
Michal Vasko | 2411b94 | 2016-03-23 13:50:03 +0100 | [diff] [blame] | 1110 | * @brief Order siblings according to the schema node ordering. |
| 1111 | * |
Michal Vasko | 299f983 | 2017-01-06 13:29:22 +0100 | [diff] [blame] | 1112 | * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators). |
| 1113 | * |
Michal Vasko | 58f74f1 | 2016-03-24 13:26:06 +0100 | [diff] [blame] | 1114 | * If the siblings include data nodes from other modules, they are |
| 1115 | * sorted based on the module order in the context. |
| 1116 | * |
| 1117 | * @param[in] sibling Node, whose siblings will be sorted. |
| 1118 | * @param[in] recursive Whether sort all siblings of siblings, recursively. |
| 1119 | * @return 0 on success, nonzero in case of an error. |
Michal Vasko | 2411b94 | 2016-03-23 13:50:03 +0100 | [diff] [blame] | 1120 | */ |
Michal Vasko | 58f74f1 | 2016-03-24 13:26:06 +0100 | [diff] [blame] | 1121 | int lyd_schema_sort(struct lyd_node *sibling, int recursive); |
Michal Vasko | 2411b94 | 2016-03-23 13:50:03 +0100 | [diff] [blame] | 1122 | |
| 1123 | /** |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1124 | * @brief Search in the given data for instances of nodes matching the provided path. |
Michal Vasko | 105cef1 | 2016-02-04 12:06:26 +0100 | [diff] [blame] | 1125 | * |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1126 | * Learn more about the path format on page @ref howtoxpath. |
Michal Vasko | 105cef1 | 2016-02-04 12:06:26 +0100 | [diff] [blame] | 1127 | * |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1128 | * @param[in] ctx_node Path context node. |
| 1129 | * @param[in] path Data path expression filtering the matching nodes. |
| 1130 | * @return Set of found data nodes. If no nodes are matching \p path or the result |
Michal Vasko | 105cef1 | 2016-02-04 12:06:26 +0100 | [diff] [blame] | 1131 | * would be a number, a string, or a boolean, the returned set is empty. In case of an error, NULL is returned. |
| 1132 | */ |
Michal Vasko | 5057671 | 2017-07-28 12:28:33 +0200 | [diff] [blame] | 1133 | struct ly_set *lyd_find_path(const struct lyd_node *ctx_node, const char *path); |
Michal Vasko | 105cef1 | 2016-02-04 12:06:26 +0100 | [diff] [blame] | 1134 | |
| 1135 | /** |
Radek Krejci | c5b6b91 | 2016-01-18 16:35:35 +0100 | [diff] [blame] | 1136 | * @brief Search in the given data for instances of the provided schema node. |
| 1137 | * |
| 1138 | * The \p data is used to find the data root and function then searches in the whole tree and all sibling trees. |
| 1139 | * |
| 1140 | * @param[in] data A node in the data tree to search. |
| 1141 | * @param[in] schema Schema node of the data nodes caller want to find. |
Michal Vasko | 46a4bf9 | 2016-09-08 08:23:49 +0200 | [diff] [blame] | 1142 | * @return Set of found data nodes. If no data node is found, the returned set is empty. |
Radek Krejci | c5b6b91 | 2016-01-18 16:35:35 +0100 | [diff] [blame] | 1143 | * In case of error, NULL is returned. |
| 1144 | */ |
Michal Vasko | f06fb5b | 2016-09-08 10:05:56 +0200 | [diff] [blame] | 1145 | struct ly_set *lyd_find_instance(const struct lyd_node *data, const struct lys_node *schema); |
Radek Krejci | c5b6b91 | 2016-01-18 16:35:35 +0100 | [diff] [blame] | 1146 | |
| 1147 | /** |
Radek Krejci | d788a52 | 2016-07-25 14:57:38 +0200 | [diff] [blame] | 1148 | * @brief Get the first sibling of the given node. |
| 1149 | * |
| 1150 | * @param[in] node Node which first sibling is going to be the result. |
| 1151 | * @return The first sibling of the given node or the node itself if it is the first child of the parent. |
| 1152 | */ |
| 1153 | struct lyd_node *lyd_first_sibling(struct lyd_node *node); |
| 1154 | |
| 1155 | /** |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 1156 | * @brief Validate \p node data subtree. |
| 1157 | * |
Michal Vasko | bbc43b1 | 2018-10-12 09:22:00 +0200 | [diff] [blame] | 1158 | * @param[in,out] node Data tree to be validated. In case the \p options includes #LYD_OPT_WHENAUTODEL, libyang |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 1159 | * can modify the provided tree including the root \p node. |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 1160 | * @param[in] options Options for the inserting data to the target data tree options, see @ref parseroptions. |
Michal Vasko | cdb9017 | 2016-09-13 09:34:36 +0200 | [diff] [blame] | 1161 | * @param[in] var_arg Variable argument depends on \p options. If they include: |
Michal Vasko | 6b44d71 | 2016-09-12 16:25:46 +0200 | [diff] [blame] | 1162 | * - #LYD_OPT_DATA: |
| 1163 | * - #LYD_OPT_CONFIG: |
| 1164 | * - #LYD_OPT_GET: |
| 1165 | * - #LYD_OPT_GETCONFIG: |
| 1166 | * - #LYD_OPT_EDIT: |
Michal Vasko | cdb9017 | 2016-09-13 09:34:36 +0200 | [diff] [blame] | 1167 | * - struct ly_ctx *ctx - context to use when \p node is NULL (for checking an empty tree), |
| 1168 | * otherwise can be NULL. |
Michal Vasko | 6b44d71 | 2016-09-12 16:25:46 +0200 | [diff] [blame] | 1169 | * - #LYD_OPT_RPC: |
| 1170 | * - #LYD_OPT_RPCREPLY: |
| 1171 | * - #LYD_OPT_NOTIF: |
| 1172 | * - struct ::lyd_node *data_tree - additional data tree that will be used when checking |
Michal Vasko | cdb9017 | 2016-09-13 09:34:36 +0200 | [diff] [blame] | 1173 | * any "when" or "must" conditions in the \p node tree |
| 1174 | * that require some nodes outside their subtree. If set, |
| 1175 | * it must be a list of top-level elements! |
Michal Vasko | 20555d8 | 2018-12-12 16:30:50 +0100 | [diff] [blame] | 1176 | * @param[in] ... Used only if options include #LYD_OPT_VAL_DIFF. In that case a (struct lyd_difflist **) |
| 1177 | * is expected into which all data node changes performed by the validation will be stored. |
| 1178 | * Needs to be properly freed. Meaning of diff type is following: |
| 1179 | * - LYD_DIFF_CREATED: |
| 1180 | * - first - Path identifying the parent node (format of lyd_path()). |
| 1181 | * - second - Duplicated subtree of the created nodes. |
| 1182 | * - LYD_DIFF_DELETED: |
| 1183 | * - first - Unlinked subtree of the deleted nodes. |
| 1184 | * - second - Path identifying the original parent (format of lyd_path()). |
Radek Krejci | 92ece00 | 2016-04-04 15:45:05 +0200 | [diff] [blame] | 1185 | * @return 0 on success, nonzero in case of an error. |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 1186 | */ |
Michal Vasko | 20555d8 | 2018-12-12 16:30:50 +0100 | [diff] [blame] | 1187 | int lyd_validate(struct lyd_node **node, int options, void *var_arg, ...); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1188 | |
| 1189 | /** |
Michal Vasko | 993af1e | 2018-12-10 12:05:17 +0100 | [diff] [blame] | 1190 | * @brief Validate \p node data tree but only subtrees that belong to the schema found in \p modules. All other |
| 1191 | * schemas are effectively disabled for the validation. |
| 1192 | * |
| 1193 | * @param[in,out] node Data tree to be validated. In case the \p options includes #LYD_OPT_WHENAUTODEL, libyang |
| 1194 | * can modify the provided tree including the root \p node. |
| 1195 | * @param[in] modules List of module names to validate. |
| 1196 | * @param[in] mod_count Number of modules in \p modules. |
| 1197 | * @param[in] options Options for the inserting data to the target data tree options, see @ref parseroptions. |
| 1198 | * Accepted data type values include #LYD_OPT_DATA, #LYD_OPT_CONFIG, #LYD_OPT_GET, |
| 1199 | * #LYD_OPT_GETCONFIG, and #LYD_OPT_EDIT. |
Michal Vasko | 20555d8 | 2018-12-12 16:30:50 +0100 | [diff] [blame] | 1200 | * @param[in] ... Used only if options include #LYD_OPT_VAL_DIFF. In that case a (struct lyd_difflist **) |
| 1201 | * is expected into which all data node changes performed by the validation will be stored. |
| 1202 | * Needs to be properly freed. Meaning of diff type is following: |
| 1203 | * - LYD_DIFF_CREATED: |
| 1204 | * - first - Path identifying the parent node (format of lyd_path()). |
| 1205 | * - second - Duplicated subtree of the created nodes. |
| 1206 | * - LYD_DIFF_DELETED: |
| 1207 | * - first - Unlinked subtree of the deleted nodes. |
| 1208 | * - second - Path identifying the original parent (format of lyd_path()). |
Michal Vasko | 993af1e | 2018-12-10 12:05:17 +0100 | [diff] [blame] | 1209 | * @return 0 on success, nonzero in case of an error. |
| 1210 | */ |
Michal Vasko | 20555d8 | 2018-12-12 16:30:50 +0100 | [diff] [blame] | 1211 | int lyd_validate_modules(struct lyd_node **node, const struct lys_module **modules, int mod_count, int options, ...); |
| 1212 | |
| 1213 | /** |
| 1214 | * @brief Free special diff that was returned by lyd_validate() or lyd_validate_modules(). |
| 1215 | * |
| 1216 | * @param[in] diff Diff to free. |
| 1217 | */ |
| 1218 | void lyd_free_val_diff(struct lyd_difflist *diff); |
Michal Vasko | 993af1e | 2018-12-10 12:05:17 +0100 | [diff] [blame] | 1219 | |
| 1220 | /** |
Radek Krejci | f6fac5e | 2017-05-18 15:14:18 +0200 | [diff] [blame] | 1221 | * @brief Check restrictions applicable to the particular leaf/leaf-list on the given string value. |
| 1222 | * |
| 1223 | * Validates the value only using the types' restrictions. Do not check the rest of restrictions dependent on the |
| 1224 | * data tree (must, when statements or uniqueness of the leaf-list item). |
| 1225 | * |
Radek Krejci | e3bd2f3 | 2017-08-07 13:52:28 +0200 | [diff] [blame] | 1226 | * The format of the data must follow rules for the lexical representation of the specific YANG type. Note |
| 1227 | * that if there are some extensions of the lexical representation for the YANG module (default value), they are |
| 1228 | * not supported by this function - it strictly follows rules for the lexical representations in data trees. |
| 1229 | * |
Radek Krejci | f6fac5e | 2017-05-18 15:14:18 +0200 | [diff] [blame] | 1230 | * @param[in] node Schema node of the leaf or leaf-list eventually holding the \p value. |
| 1231 | * @param[in] value Value to be checked (NULL is checked as empty string). |
| 1232 | * @return EXIT_SUCCESS if the \p value conforms to the restrictions, EXIT_FAILURE otherwise. |
| 1233 | */ |
| 1234 | int lyd_validate_value(struct lys_node *node, const char *value); |
| 1235 | |
| 1236 | /** |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 1237 | * @brief Get know if the node contain (despite implicit or explicit) default value. |
Radek Krejci | 7b4309c | 2016-03-23 10:30:29 +0100 | [diff] [blame] | 1238 | * |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 1239 | * @param[in] node The leaf or leaf-list to check. Note, that leaf-list is marked as default only when the complete |
| 1240 | * and only the default set is present (node's siblings are also checked). |
| 1241 | * @return 1 if the node contains the default value, 0 otherwise. |
Radek Krejci | 7b4309c | 2016-03-23 10:30:29 +0100 | [diff] [blame] | 1242 | */ |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 1243 | int lyd_wd_default(struct lyd_node_leaf_list *node); |
Radek Krejci | 6b8f6ac | 2016-03-23 12:33:04 +0100 | [diff] [blame] | 1244 | |
| 1245 | /** |
Michal Vasko | 55f60be | 2015-10-14 13:12:58 +0200 | [diff] [blame] | 1246 | * @brief Unlink the specified data subtree. All referenced namespaces are copied. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1247 | * |
Michal Vasko | 299f983 | 2017-01-06 13:29:22 +0100 | [diff] [blame] | 1248 | * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators). |
| 1249 | * |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1250 | * Note, that the node's connection with the schema tree is kept. Therefore, in case of |
| 1251 | * reconnecting the node to a data tree using lyd_paste() it is necessary to paste it |
| 1252 | * to the appropriate place in the data tree following the schema. |
| 1253 | * |
| 1254 | * @param[in] node Data tree node to be unlinked (together with all children). |
| 1255 | * @return 0 for success, nonzero for error |
| 1256 | */ |
| 1257 | int lyd_unlink(struct lyd_node *node); |
| 1258 | |
| 1259 | /** |
Radek Krejci | fd0bcf0 | 2016-09-09 13:28:34 +0200 | [diff] [blame] | 1260 | * @brief Free (and unlink) the specified data subtree. Use carefully, since libyang silently creates default nodes, |
| 1261 | * it is always better to use lyd_free_withsiblings() to free the complete data tree. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1262 | * |
Michal Vasko | 299f983 | 2017-01-06 13:29:22 +0100 | [diff] [blame] | 1263 | * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators). |
| 1264 | * |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1265 | * @param[in] node Root of the (sub)tree to be freed. |
| 1266 | */ |
| 1267 | void lyd_free(struct lyd_node *node); |
| 1268 | |
| 1269 | /** |
Radek Krejci | fd0bcf0 | 2016-09-09 13:28:34 +0200 | [diff] [blame] | 1270 | * @brief Free (and unlink) the specified data tree and all its siblings (preceding as well as following). |
Radek Krejci | 8146840 | 2016-01-07 13:52:40 +0100 | [diff] [blame] | 1271 | * |
Michal Vasko | f41c25e | 2018-12-07 12:06:17 +0100 | [diff] [blame] | 1272 | * If used on a top-level node it means that the whole data tree is being freed and unnecessary operations |
| 1273 | * are skipped. Always use this function for freeing a whole data tree to achieve better performance. |
| 1274 | * |
Michal Vasko | 299f983 | 2017-01-06 13:29:22 +0100 | [diff] [blame] | 1275 | * __PARTIAL CHANGE__ - validate after the final change on the data tree (see @ref howtodatamanipulators). |
| 1276 | * |
Radek Krejci | 8146840 | 2016-01-07 13:52:40 +0100 | [diff] [blame] | 1277 | * @param[in] node One of the siblings root element of the (sub)trees to be freed. |
| 1278 | */ |
| 1279 | void lyd_free_withsiblings(struct lyd_node *node); |
| 1280 | |
| 1281 | /** |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 1282 | * @brief Insert attribute into the data node. |
| 1283 | * |
| 1284 | * @param[in] parent Data node where to place the attribute |
Radek Krejci | 70ecd72 | 2016-03-21 09:04:00 +0100 | [diff] [blame] | 1285 | * @param[in] mod An alternative way to specify attribute's module (namespace) used in case the \p name does |
| 1286 | * not include prefix. If neither prefix in the \p name nor mod is specified, the attribute's |
| 1287 | * module is inherited from the \p parent node. It is not allowed to have attributes with no |
| 1288 | * module (namespace). |
| 1289 | * @param[in] name Attribute name. The string can include the attribute's module (namespace) as the name's |
| 1290 | * prefix (prefix:name). Prefix must be the name of one of the schema in the \p parent's context. |
| 1291 | * If the prefix is not specified, the \p mod parameter is used. If neither of these parameters is |
| 1292 | * usable, attribute inherits module (namespace) from the \p parent node. It is not allowed to |
| 1293 | * have attributes with no module (namespace). |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 1294 | * @param[in] value Attribute value |
| 1295 | * @return pointer to the created attribute (which is already connected in \p parent) or NULL on error. |
| 1296 | */ |
Radek Krejci | 70ecd72 | 2016-03-21 09:04:00 +0100 | [diff] [blame] | 1297 | struct lyd_attr *lyd_insert_attr(struct lyd_node *parent, const struct lys_module *mod, const char *name, |
| 1298 | const char *value); |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 1299 | |
| 1300 | /** |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 1301 | * @brief Destroy data attribute |
| 1302 | * |
| 1303 | * If the attribute to destroy is a member of a node attribute list, it is necessary to |
| 1304 | * provide the node itself as \p parent to keep the list consistent. |
| 1305 | * |
| 1306 | * @param[in] ctx Context where the attribute was created (usually it is the context of the \p parent) |
| 1307 | * @param[in] parent Parent node where the attribute is placed |
| 1308 | * @param[in] attr Attribute to destroy |
| 1309 | * @param[in] recursive Zero to destroy only the attribute, non-zero to destroy also all the subsequent attributes |
| 1310 | * in the list. |
| 1311 | */ |
| 1312 | void lyd_free_attr(struct ly_ctx *ctx, struct lyd_node *parent, struct lyd_attr *attr, int recursive); |
| 1313 | |
| 1314 | /** |
Radek Krejci | 6910a03 | 2016-04-13 10:06:21 +0200 | [diff] [blame] | 1315 | * @brief Return main module of the data tree node. |
| 1316 | * |
| 1317 | * In case of regular YANG module, it returns ::lys_node#module pointer, |
| 1318 | * but in case of submodule, it returns pointer to the main module. |
| 1319 | * |
| 1320 | * @param[in] node Data tree node to be examined |
| 1321 | * @return pointer to the main module (schema structure), NULL in case of error. |
| 1322 | */ |
| 1323 | struct lys_module *lyd_node_module(const struct lyd_node *node); |
| 1324 | |
| 1325 | /** |
Michal Vasko | 0a8ab41 | 2017-01-09 11:10:08 +0100 | [diff] [blame] | 1326 | * @brief Get the type structure of a leaf. |
| 1327 | * |
| 1328 | * In case of a union, the correct specific type is found. |
| 1329 | * In case of a leafref, the final (if there is a chain of leafrefs) target's type is found. |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 1330 | * |
| 1331 | * @param[in] leaf Leaf to examine. |
Michal Vasko | 0a8ab41 | 2017-01-09 11:10:08 +0100 | [diff] [blame] | 1332 | * @return Found type, NULL on error. |
Michal Vasko | e3886bb | 2017-01-02 11:33:28 +0100 | [diff] [blame] | 1333 | */ |
| 1334 | const struct lys_type *lyd_leaf_type(const struct lyd_node_leaf_list *leaf); |
| 1335 | |
| 1336 | /** |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 1337 | * @brief Print data tree in the specified format. |
| 1338 | * |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 1339 | * @param[out] strp Pointer to store the resulting dump. |
| 1340 | * @param[in] root Root node of the data tree to print. It can be actually any (not only real root) |
| 1341 | * node of the data tree to print the specific subtree. |
| 1342 | * @param[in] format Data output format. |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 1343 | * @param[in] options [printer flags](@ref printerflags). \p format LYD_LYB accepts only #LYP_WITHSIBLINGS option. |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 1344 | * @return 0 on success, 1 on failure (#ly_errno is set). |
| 1345 | */ |
| 1346 | 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] | 1347 | |
| 1348 | /** |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 1349 | * @brief Print data tree in the specified format. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1350 | * |
Michal Vasko | f8942f6 | 2018-09-24 14:12:28 +0200 | [diff] [blame] | 1351 | * @param[in] fd File descriptor where to print the data. |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 1352 | * @param[in] root Root node of the data tree to print. It can be actually any (not only real root) |
| 1353 | * node of the data tree to print the specific subtree. |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 1354 | * @param[in] format Data output format. |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 1355 | * @param[in] options [printer flags](@ref printerflags). \p format LYD_LYB accepts only #LYP_WITHSIBLINGS option. |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 1356 | * @return 0 on success, 1 on failure (#ly_errno is set). |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1357 | */ |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 1358 | int lyd_print_fd(int fd, const struct lyd_node *root, LYD_FORMAT format, int options); |
| 1359 | |
| 1360 | /** |
| 1361 | * @brief Print data tree in the specified format. |
| 1362 | * |
Michal Vasko | f8942f6 | 2018-09-24 14:12:28 +0200 | [diff] [blame] | 1363 | * @param[in] f File stream where to print the data. |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 1364 | * @param[in] root Root node of the data tree to print. It can be actually any (not only real root) |
| 1365 | * node of the data tree to print the specific subtree. |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 1366 | * @param[in] format Data output format. |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 1367 | * @param[in] options [printer flags](@ref printerflags). \p format LYD_LYB accepts only #LYP_WITHSIBLINGS option. |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 1368 | * @return 0 on success, 1 on failure (#ly_errno is set). |
| 1369 | */ |
| 1370 | int lyd_print_file(FILE *f, const struct lyd_node *root, LYD_FORMAT format, int options); |
| 1371 | |
| 1372 | /** |
| 1373 | * @brief Print data tree in the specified format. |
| 1374 | * |
Michal Vasko | f8942f6 | 2018-09-24 14:12:28 +0200 | [diff] [blame] | 1375 | * @param[in] path File path where to print the data. |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 1376 | * @param[in] root Root node of the data tree to print. It can be actually any (not only real root) |
| 1377 | * node of the data tree to print the specific subtree. |
Michal Vasko | f8942f6 | 2018-09-24 14:12:28 +0200 | [diff] [blame] | 1378 | * @param[in] format Data output format. |
| 1379 | * @param[in] options [printer flags](@ref printerflags). \p format LYD_LYB accepts only #LYP_WITHSIBLINGS option. |
| 1380 | * @return 0 on success, 1 on failure (#ly_errno is set). |
| 1381 | */ |
| 1382 | int lyd_print_path(const char *path, const struct lyd_node *root, LYD_FORMAT format, int options); |
| 1383 | |
| 1384 | /** |
| 1385 | * @brief Print data tree in the specified format. |
| 1386 | * |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 1387 | * @param[in] writeclb Callback function to write the data (see write(1)). |
Michal Vasko | f8942f6 | 2018-09-24 14:12:28 +0200 | [diff] [blame] | 1388 | * @param[in] root Root node of the data tree to print. It can be actually any (not only real root) |
| 1389 | * node of the data tree to print the specific subtree. |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 1390 | * @param[in] arg Optional caller-specific argument to be passed to the \p writeclb callback. |
| 1391 | * @param[in] format Data output format. |
Michal Vasko | 228431e | 2018-07-10 15:47:11 +0200 | [diff] [blame] | 1392 | * @param[in] options [printer flags](@ref printerflags). \p format LYD_LYB accepts only #LYP_WITHSIBLINGS option. |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 1393 | * @return 0 on success, 1 on failure (#ly_errno is set). |
| 1394 | */ |
| 1395 | int lyd_print_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg, |
| 1396 | const struct lyd_node *root, LYD_FORMAT format, int options); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1397 | |
Michal Vasko | 4d1f048 | 2016-09-19 14:35:06 +0200 | [diff] [blame] | 1398 | /** |
| 1399 | * @brief Get the double value of a decimal64 leaf/leaf-list. |
| 1400 | * |
| 1401 | * YANG decimal64 type enables higher precision numbers than IEEE 754 double-precision |
| 1402 | * format, so this conversion does not have to be lossless. |
| 1403 | * |
| 1404 | * @param[in] node Leaf/leaf-list of type decimal64. |
| 1405 | * @return Closest double equivalent to the decimal64 value. |
| 1406 | */ |
| 1407 | double lyd_dec64_to_double(const struct lyd_node *node); |
| 1408 | |
Michal Vasko | 196c610 | 2018-08-08 16:27:42 +0200 | [diff] [blame] | 1409 | /** |
| 1410 | * @brief Get the length of a printed LYB data tree. |
| 1411 | * |
| 1412 | * @param[in] data LYB data. |
| 1413 | * @return \p data length or -1 on error. |
| 1414 | */ |
| 1415 | int lyd_lyb_data_length(const char *data); |
| 1416 | |
Michal Vasko | d025ee3 | 2018-06-28 10:04:19 +0200 | [diff] [blame] | 1417 | #ifdef LY_ENABLED_LYD_PRIV |
| 1418 | |
| 1419 | /** |
| 1420 | * @brief Set a schema private pointer to a user pointer. |
| 1421 | * |
| 1422 | * @param[in] node Data node, whose private field will be assigned. |
| 1423 | * @param[in] priv Arbitrary user-specified pointer. |
| 1424 | * @return Previous private object of the \p node (NULL if this is the first call on the \p node). Note, that |
| 1425 | * the caller is in this case responsible (if it is necessary) for freeing the replaced private object. In case |
| 1426 | * of invalid (NULL) \p node, NULL is returned and #ly_errno is set to #LY_EINVAL. |
| 1427 | */ |
| 1428 | void *lyd_set_private(const struct lyd_node *node, void *priv); |
| 1429 | |
| 1430 | #endif |
| 1431 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1432 | /**@} */ |
| 1433 | |
| 1434 | #ifdef __cplusplus |
| 1435 | } |
| 1436 | #endif |
| 1437 | |
| 1438 | #endif /* LY_TREE_DATA_H_ */ |