Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1 | /** |
Radek Krejci | aa429e4 | 2015-10-09 15:52:37 +0200 | [diff] [blame] | 2 | * @file tree_data.h |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Radek Krejci | aa429e4 | 2015-10-09 15:52:37 +0200 | [diff] [blame] | 4 | * @brief libyang representation of data trees. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
Michal Vasko | 8de098c | 2016-02-26 10:00:25 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #ifndef LY_TREE_DATA_H_ |
| 16 | #define LY_TREE_DATA_H_ |
| 17 | |
| 18 | #include <stddef.h> |
| 19 | #include <stdint.h> |
| 20 | |
Mislav Novakovic | e251a65 | 2015-09-29 08:40:12 +0200 | [diff] [blame] | 21 | #include "tree_schema.h" |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 22 | #include "xml.h" |
Mislav Novakovic | e251a65 | 2015-09-29 08:40:12 +0200 | [diff] [blame] | 23 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 24 | #ifdef __cplusplus |
| 25 | extern "C" { |
| 26 | #endif |
| 27 | |
| 28 | /** |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 29 | * @defgroup datatree Data Tree |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 30 | * @{ |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 31 | * |
| 32 | * Data structures and functions to manipulate and access instance data tree. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 33 | */ |
| 34 | |
| 35 | /** |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 36 | * @brief Data input/output formats supported by libyang [parser](@ref howtodataparsers) and |
| 37 | * [printer](@ref howtodataprinters) functions. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 38 | */ |
| 39 | typedef enum { |
| 40 | LYD_UNKNOWN, /**< unknown format, used as return value in case of error */ |
| 41 | LYD_XML, /**< XML format of the instance data */ |
| 42 | LYD_JSON, /**< JSON format of the instance data */ |
| 43 | } LYD_FORMAT; |
| 44 | |
| 45 | /** |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 46 | * @brief List of possible value types stored in ::lyd_node_anydata. |
| 47 | */ |
| 48 | typedef enum { |
| 49 | LYD_ANYDATA_CONSTSTRING, /**< value is constant string (const char *) which is internally duplicated for storing |
| 50 | in the anydata structure; XML sensitive characters (such as & or \>) are automatically |
| 51 | escaped when the anydata is printed in XML format */ |
| 52 | LYD_ANYDATA_STRING, /**< value is dynamically allocated string (char*), so the data are used directly without |
| 53 | duplication and caller is supposed to not manipulate with the data after a successful |
| 54 | call (including calling free() on the provided data); XML sensitive characters |
| 55 | (such as & or \>) are automatically escaped when the anydata is printed in XML format */ |
| 56 | LYD_ANYDATA_DATATREE, /**< value is struct lyd_node* (first sibling), the structure is directly connected into |
| 57 | the anydata node without duplication, caller is supposed to not manipulate with the |
| 58 | data after a successful call (including calling lyd_free() on the provided data) */ |
| 59 | LYD_ANYDATA_XML /**< value is struct lyxml_elem*, the structure is directly connected into |
| 60 | the anydata node without duplication, caller is supposed to not manipulate with the |
| 61 | data after a successful call (including calling lyxml_free() on the provided data)*/ |
| 62 | } LYD_ANYDATA_VALUETYPE; |
| 63 | |
| 64 | /** |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 65 | * @brief Attribute structure. |
| 66 | * |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 67 | * The structure provides information about attributes of a data element. Such attributes partially |
| 68 | * maps to annotations from draft-ietf-netmod-yang-metadata. In XML, they are represented as standard |
| 69 | * XML attrbutes. In JSON, they are represented as JSON elements starting with the '@' character |
| 70 | * (for more information, see the yang metadata draft. |
| 71 | * |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 72 | */ |
| 73 | struct lyd_attr { |
Radek Krejci | 5f9e8c9 | 2015-10-30 10:01:06 +0100 | [diff] [blame] | 74 | struct lyd_attr *next; /**< pointer to the next attribute of the same element */ |
| 75 | struct lys_module *module; /**< pointer to the attribute's module. |
| 76 | TODO when annotations will be supported, point to the annotation definition |
| 77 | and validate that the attribute is really defined there. Currently, we just |
| 78 | believe that it is defined in the module it says */ |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 79 | const char *name; /**< attribute name */ |
| 80 | const char *value; /**< attribute value */ |
| 81 | }; |
| 82 | |
| 83 | /** |
| 84 | * @brief node's value representation |
| 85 | */ |
| 86 | typedef union lyd_value_u { |
| 87 | const char *binary; /**< base64 encoded, NULL terminated string */ |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 88 | struct lys_type_bit **bit; /**< bitmap of pointers to the schema definition of the bit value that are set, |
| 89 | its size is always the number of defined bits in the schema */ |
Radek Krejci | 489773c | 2015-12-17 13:20:03 +0100 | [diff] [blame] | 90 | int8_t bln; /**< 0 as false, 1 as true */ |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 91 | int64_t dec64; /**< decimal64: value = dec64 / 10^fraction-digits */ |
| 92 | 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] | 93 | 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] | 94 | struct lyd_node *instance; /**< pointer to the instance-identifier target, note that if the tree was modified, |
| 95 | the target (address) can be invalid - the pointer is correctly checked and updated |
| 96 | by lyd_validate() */ |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 97 | int8_t int8; /**< 8-bit signed integer */ |
| 98 | int16_t int16; /**< 16-bit signed integer */ |
| 99 | int32_t int32; /**< 32-bit signed integer */ |
| 100 | int64_t int64; /**< 64-bit signed integer */ |
| 101 | struct lyd_node *leafref; /**< pointer to the referenced leaf/leaflist instance in data tree */ |
| 102 | const char *string; /**< string */ |
| 103 | uint8_t uint8; /**< 8-bit unsigned integer */ |
| 104 | uint16_t uint16; /**< 16-bit signed integer */ |
| 105 | uint32_t uint32; /**< 32-bit signed integer */ |
| 106 | uint64_t uint64; /**< 64-bit signed integer */ |
| 107 | } lyd_val; |
| 108 | |
| 109 | /** |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 110 | * @defgroup validityflags Validity flags |
| 111 | * @ingroup datatree |
| 112 | * |
| 113 | * Validity flags for data nodes. |
| 114 | * |
| 115 | * @{ |
| 116 | */ |
| 117 | #define LYD_VAL_OK 0x00 /**< node is successfully validated including whole subtree */ |
| 118 | #define LYD_VAL_UNIQUE 0x01 /**< Unique value(s) changed, applicable only to ::lys_node_list data nodes */ |
Radek Krejci | d788a52 | 2016-07-25 14:57:38 +0200 | [diff] [blame] | 119 | #define LYD_VAL_MAND 0x02 /**< Some child added/removed and it is needed to perform check for mandatory |
| 120 | node or min/max constraints of direct list/leaflist children, applicable only |
| 121 | to ::lys_node_list and ::lys_node_container data nodes */ |
Radek Krejci | 991a396 | 2016-05-05 15:00:14 +0200 | [diff] [blame] | 122 | #define LYD_VAL_NOT 0x07 /**< node was not validated yet */ |
| 123 | #define LYD_VAL_INUSE 0x08 /**< Internal flag for note about various processing on data, should be used only |
| 124 | internally and removed before the libyang returns to the caller */ |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 125 | /** |
| 126 | * @} |
| 127 | */ |
| 128 | |
| 129 | /** |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 130 | * @brief Generic structure for a data node, directly applicable to the data nodes defined as #LYS_CONTAINER, #LYS_LIST |
| 131 | * and #LYS_CHOICE. |
| 132 | * |
| 133 | * Completely fits to containers and choices and is compatible (can be used interchangeably except the #child member) |
| 134 | * with all other lyd_node_* structures. All data nodes are provides as ::lyd_node structure by default. |
| 135 | * 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] | 136 | * ::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] | 137 | * #LYS_CHOICE values. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 138 | * |
| 139 | * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro. |
| 140 | */ |
| 141 | struct lyd_node { |
| 142 | struct lys_node *schema; /**< pointer to the schema definition of this node */ |
Radek Krejci | 1eefeb3 | 2016-04-15 16:01:46 +0200 | [diff] [blame] | 143 | uint8_t validity:4; /**< [validity flags](@ref validityflags) */ |
| 144 | uint8_t dflt:1; /**< flag for default node (applicable only on leafs) to be marked with default attribute */ |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 145 | 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] | 146 | do not use this value! */ |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 147 | |
| 148 | struct lyd_attr *attr; /**< pointer to the list of attributes of this node */ |
| 149 | struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */ |
| 150 | struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is |
| 151 | never NULL. If there is no sibling node, pointer points to the node |
| 152 | itself. In case of the first node, this pointer points to the last |
| 153 | node in the list. */ |
| 154 | struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */ |
| 155 | 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] | 156 | structures represent end nodes, this member |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 157 | is replaced in those structures. Therefore, be careful with accessing |
| 158 | this member without having information about the node type from the schema's |
| 159 | ::lys_node#nodetype member. */ |
| 160 | }; |
| 161 | |
| 162 | /** |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 163 | * @brief Structure for data nodes defined as #LYS_LEAF or #LYS_LEAFLIST. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 164 | * |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 165 | * Extension for ::lyd_node structure. It replaces the ::lyd_node#child member by |
| 166 | * three new members (#value, #value_str and #value_type) to provide |
| 167 | * information about the value. The first five members (#schema, #attr, #next, |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 168 | * #prev and #parent) are compatible with the ::lyd_node's members. |
| 169 | * |
| 170 | * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro. |
| 171 | */ |
Michal Vasko | 4c18331 | 2015-09-25 10:41:47 +0200 | [diff] [blame] | 172 | struct lyd_node_leaf_list { |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 173 | struct lys_node *schema; /**< pointer to the schema definition of this node which is ::lys_node_leaflist |
| 174 | structure */ |
Radek Krejci | 1eefeb3 | 2016-04-15 16:01:46 +0200 | [diff] [blame] | 175 | uint8_t validity:4; /**< [validity flags](@ref validityflags) */ |
| 176 | uint8_t dflt:1; /**< flag for default node (applicable only on leafs) to be marked with default attribute */ |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 177 | 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] | 178 | do not use this value! */ |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 179 | |
| 180 | struct lyd_attr *attr; /**< pointer to the list of attributes of this node */ |
| 181 | struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */ |
| 182 | struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is |
| 183 | never NULL. If there is no sibling node, pointer points to the node |
| 184 | itself. In case of the first node, this pointer points to the last |
| 185 | node in the list. */ |
| 186 | struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */ |
| 187 | |
| 188 | /* struct lyd_node *child; should be here, but is not */ |
| 189 | |
| 190 | /* leaflist's specific members */ |
Michal Vasko | 6a02770 | 2016-06-30 10:32:14 +0200 | [diff] [blame] | 191 | const char *value_str; /**< string representation of value (for comparison, printing,...), always corresponds to value_type */ |
| 192 | lyd_val value; /**< node's value representation, always corresponds to schema->type.base */ |
| 193 | LY_DATA_TYPE value_type; /**< type of the value in the node, mainly for union to avoid repeating of type detection, |
| 194 | if (schema->type.base == LY_TYPE_LEAFREF), then value_type may be |
| 195 | (LY_TYPE_LEAFREF_UNRES | leafref target value_type) and (value.leafref == NULL) */ |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | /** |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 199 | * @brief Structure for data nodes defined as #LYS_ANYDATA or #LYS_ANYXML. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 200 | * |
| 201 | * Extension for ::lyd_node structure - replaces the ::lyd_node#child member by new #value member. The first five |
| 202 | * members (#schema, #attr, #next, #prev and #parent) are compatible with the ::lyd_node's members. |
| 203 | * |
| 204 | * To traverse through all the child elements or attributes, use #LY_TREE_FOR or #LY_TREE_FOR_SAFE macro. |
| 205 | */ |
Radek Krejci | bf2abff | 2016-08-23 15:51:52 +0200 | [diff] [blame] | 206 | struct lyd_node_anydata { |
| 207 | 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] | 208 | structure */ |
Radek Krejci | 1eefeb3 | 2016-04-15 16:01:46 +0200 | [diff] [blame] | 209 | uint8_t validity:4; /**< [validity flags](@ref validityflags) */ |
| 210 | uint8_t dflt:1; /**< flag for default node (applicable only on leafs) to be marked with default attribute */ |
Radek Krejci | 0b7704f | 2016-03-18 12:16:14 +0100 | [diff] [blame] | 211 | 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] | 212 | do not use this value! */ |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 213 | |
| 214 | struct lyd_attr *attr; /**< pointer to the list of attributes of this node */ |
| 215 | struct lyd_node *next; /**< pointer to the next sibling node (NULL if there is no one) */ |
| 216 | struct lyd_node *prev; /**< pointer to the previous sibling node \note Note that this pointer is |
| 217 | never NULL. If there is no sibling node, pointer points to the node |
| 218 | itself. In case of the first node, this pointer points to the last |
| 219 | node in the list. */ |
| 220 | struct lyd_node *parent; /**< pointer to the parent node, NULL in case of root node */ |
| 221 | |
| 222 | /* struct lyd_node *child; should be here, but is not */ |
| 223 | |
| 224 | /* anyxml's specific members */ |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 225 | LYD_ANYDATA_VALUETYPE value_type;/**< type of the stored anydata value */ |
| 226 | union { |
| 227 | const char *str; /**< string value, in case of printing as XML, characters like '<' or '&' are escaped */ |
| 228 | struct lyxml_elem *xml; /**< xml tree */ |
| 229 | struct lyd_node *tree; /**< libyang data tree, does not change the root's parent, so it is not possible |
| 230 | to get from the data tree into the anydata/anyxml */ |
| 231 | } value; |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 232 | }; |
| 233 | |
| 234 | /** |
Radek Krejci | 991a396 | 2016-05-05 15:00:14 +0200 | [diff] [blame] | 235 | * @brief list of possible types of differencies in #lyd_difflist |
| 236 | */ |
| 237 | typedef enum { |
Radek Krejci | 9e6f0b8 | 2016-05-13 17:33:16 +0200 | [diff] [blame] | 238 | LYD_DIFF_END = 0, /**< end of the differences list */ |
Radek Krejci | 9e6f0b8 | 2016-05-13 17:33:16 +0200 | [diff] [blame] | 239 | LYD_DIFF_DELETED, /**< deleted node |
| 240 | - Node is present in the first tree, but not in the second tree. |
| 241 | - To make both trees the same the node in lyd_difflist::first can be deleted from the |
| 242 | first tree. The pointer at the same index in the lyd_difflist::second array is |
| 243 | NULL */ |
| 244 | LYD_DIFF_CHANGED, /**< value of a leaf or anyxml is changed, the lyd_difflist::first and lyd_difflist::second |
| 245 | 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] | 246 | LYD_DIFF_MOVEDAFTER1, /**< user-ordered (leaf-)list item was moved. |
| 247 | - 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] | 248 | to the first tree in the strict order they appear in the difflist. The |
| 249 | lyd_difflist::first points to the first tree node being moved and the |
| 250 | lyd_difflist::second points to the first tree node after which the first node is |
| 251 | supposed to be moved. If the second pointer is NULL, the node is being moved into |
| 252 | the beginning as the first node of the (leaf-)list instances. */ |
Radek Krejci | 22d2ca9 | 2016-05-17 16:23:51 +0200 | [diff] [blame] | 253 | LYD_DIFF_CREATED, /**< newly created node |
| 254 | - Node is present in the second tree, but not in the first tree. |
| 255 | - To make both trees the same the node in lyd_difflist::second is supposed to be |
| 256 | inserted (copied via lyd_dup()) into the node (as a child) at the same index in the |
| 257 | lyd_difflist::first array (where is its parent). If the lyd_difflist::first at the |
| 258 | index is NULL, the missing node is top-level. */ |
| 259 | LYD_DIFF_MOVEDAFTER2 /**< similar to LYD_DIFF_MOVEDAFTER1, but this time the moved item is in the second tree. |
| 260 | This type is always used in combination with (as a successor of) #LYD_DIFF_CREATED |
| 261 | as an instruction to move the newly created node to a specific position. Note, that |
| 262 | due to applicability to the second tree, the meaning of lyd_difflist:first and |
| 263 | lyd_difflist:second is inverse in comparison to #LYD_DIFF_MOVEDAFTER1. The |
| 264 | lyd_difflist::second points to the (previously) created node in the second tree and |
| 265 | the lyd_difflist::first points to the predecessor node in the second tree. If the |
| 266 | predecessor is NULL, the node is supposed to bes the first sibling. */ |
Radek Krejci | 991a396 | 2016-05-05 15:00:14 +0200 | [diff] [blame] | 267 | } LYD_DIFFTYPE; |
| 268 | |
| 269 | /** |
| 270 | * @brief Structure for the result of lyd_diff(), describing differences between two data trees. |
| 271 | */ |
| 272 | struct lyd_difflist { |
| 273 | LYD_DIFFTYPE *type; /**< array of the differences types, terminated by #LYD_DIFF_END value. */ |
| 274 | struct lyd_node **first; /**< array of nodes in the first tree for the specific type of difference, see the |
| 275 | description of #LYD_DIFFTYPE values for more information. */ |
| 276 | struct lyd_node **second;/**< array of nodes in the second tree for the specific type of difference, see the |
| 277 | description of #LYD_DIFFTYPE values for more information. */ |
| 278 | }; |
| 279 | |
| 280 | /** |
| 281 | * @brief Free the result of lyd_diff(). It frees the structure of the lyd_diff() result, not the referenced nodes. |
| 282 | * |
| 283 | * @param[in] diff The lyd_diff() result to free. |
| 284 | */ |
| 285 | void lyd_free_diff(struct lyd_difflist *diff); |
| 286 | |
| 287 | /** |
| 288 | * @brief Compare two data trees and provide list of differences. |
| 289 | * |
| 290 | * Note, that the \p first and the \p second must have the same schema parent (or they must be top-level elements). |
| 291 | * In case of using #LYD_OPT_NOSIBLINGS, they both must be instances of the same schema node. |
| 292 | * |
Radek Krejci | 913100d | 2016-05-09 17:23:51 +0200 | [diff] [blame] | 293 | * Order of the resulting set follows these rules: |
Radek Krejci | 22d2ca9 | 2016-05-17 16:23:51 +0200 | [diff] [blame] | 294 | * - To change the first tree into the second tree, the resulting transactions are supposed to be applied in the order |
| 295 | * they appear in the result. First, the changed (#LYD_DIFF_CHANGED) nodes are described followed by the deleted |
| 296 | * (#LYD_DIFF_DELETED) nodes. Then, the moving of the user-ordered nodes present in both trees (#LYD_DIFF_MOVEDAFTER1) |
| 297 | * follows and the last transactions in the results are the newly created (#LYD_DIFF_CREATED) nodes. These nodes are |
| 298 | * supposed to be added as the last siblings, but in some case they can need additional move. In such a case, the |
| 299 | * #LYD_DIFF_MOVEDAFTER2 transactions can appear. |
| 300 | * - The order of the changed (#LYD_DIFF_CHANGED) and created (#LYD_DIFF_CREATED) follows the nodes order in the |
| 301 | * second tree - the current siblings are processed first and then the children are processed. Note, that this is |
| 302 | * actually not the BFS: |
Radek Krejci | 9e47ddf | 2016-05-18 15:01:09 +0200 | [diff] [blame] | 303 | * |
Radek Krejci | 913100d | 2016-05-09 17:23:51 +0200 | [diff] [blame] | 304 | * 1 2 |
| 305 | * / \ / \ |
| 306 | * 3 4 7 8 |
| 307 | * / \ |
| 308 | * 5 6 |
Radek Krejci | 9e47ddf | 2016-05-18 15:01:09 +0200 | [diff] [blame] | 309 | * |
Radek Krejci | 22d2ca9 | 2016-05-17 16:23:51 +0200 | [diff] [blame] | 310 | * - The order of the deleted (#LYD_DIFF_DELETED) nodes is the DFS: |
Radek Krejci | 9e47ddf | 2016-05-18 15:01:09 +0200 | [diff] [blame] | 311 | * |
| 312 | * 1 6 |
| 313 | * / \ / \ |
| 314 | * 2 5 7 8 |
| 315 | * / \ |
| 316 | * 3 4 |
Radek Krejci | 913100d | 2016-05-09 17:23:51 +0200 | [diff] [blame] | 317 | * |
| 318 | * To change the first tree into the second one, it is necessary to follow the order of transactions described in |
| 319 | * the result. Note, that it is not possible just to use the transactions in the reverse order to transform the |
| 320 | * second tree into the first one. The transactions can be generalized (to be used on a different instance of the |
| 321 | * first tree) using lyd_path() to get identifiers for the nodes used in the transactions. |
| 322 | * |
Radek Krejci | 9a6a5dd | 2016-05-05 15:56:24 +0200 | [diff] [blame] | 323 | * @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] | 324 | * taken into comparison. If NULL, all the \p second nodes are supposed to be top level and they will |
| 325 | * be marked as #LYD_DIFF_CREATED. |
Radek Krejci | 9a6a5dd | 2016-05-05 15:56:24 +0200 | [diff] [blame] | 326 | * @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] | 327 | * 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] | 328 | * @param[in] options The @ref diffoptions are accepted. |
Radek Krejci | 991a396 | 2016-05-05 15:00:14 +0200 | [diff] [blame] | 329 | * @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] | 330 | * 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] | 331 | */ |
| 332 | struct lyd_difflist *lyd_diff(struct lyd_node *first, struct lyd_node *second, int options); |
| 333 | |
| 334 | /** |
Radek Krejci | 99d737f | 2016-09-06 11:19:52 +0200 | [diff] [blame] | 335 | * @defgroup diffoptions Diff options |
| 336 | * @ingroup datatree |
| 337 | * |
| 338 | * @{ |
| 339 | */ |
| 340 | /* LYD_DIFFOPT_NOSIBLINGS value is the same as LYD_OPT_NOSIBLINGS due to backward compatibility. The LYD_OPT_NOSIBLINGS |
| 341 | * was used previously as an option for lyd_diff(). */ |
| 342 | #define LYD_DIFFOPT_NOSIBLINGS 0x0800 /**< The both trees to diff have to instantiate the same schema node so only the |
| 343 | single subtree is compared. */ |
| 344 | #define LYD_DIFFOPT_WITHDEFAULTS 0x0001 /**< Take default nodes with their values into account and handle them as part |
| 345 | of both trees. In this case, a node with defined default value cannot be |
| 346 | deleted, because when it is removed from a tree, it is implicitly replaced |
| 347 | by the default node, so the node is not #LYD_DIFF_DELETED, but |
| 348 | #LYD_DIFF_CHANGED. Note that in this case, applying the resulting |
| 349 | transactions on the first tree does not result to the exact second tree, |
| 350 | because instead of having implicit default nodes you are going to have |
| 351 | explicit default nodes. */ |
| 352 | /**@} diffoptions */ |
| 353 | |
| 354 | /** |
Radek Krejci | 6d53828 | 2016-05-05 14:24:12 +0200 | [diff] [blame] | 355 | * @brief Build path (usable as XPath) of the data node. |
| 356 | * @param[in] node Data node to be processed. Note that the node should be from a complete data tree, having a subtree |
| 357 | * (after using lyd_unlink()) can cause generating invalid paths. |
| 358 | * @return NULL on error, on success the buffer for the resulting path is allocated and caller is supposed to free it |
| 359 | * with free(). |
| 360 | */ |
| 361 | char *lyd_path(struct lyd_node *node); |
| 362 | |
| 363 | /** |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 364 | * @defgroup parseroptions Data parser options |
| 365 | * @ingroup datatree |
| 366 | * |
| 367 | * Various options to change the data tree parsers behavior. |
| 368 | * |
| 369 | * Default behavior: |
| 370 | * - in case of XML, parser reads all data from its input (file, memory, XML tree) including the case of not well-formed |
| 371 | * XML document (multiple top-level elements) and if there is an unknown element, it is skipped including its subtree |
| 372 | * (see the next point). This can be changed by the #LYD_OPT_NOSIBLINGS option which make parser to read only a single |
| 373 | * tree (with a single root element) from its input. |
| 374 | * - parser silently ignores the data without a matching node in schema trees. If the caller want to stop |
| 375 | * parsing in case of presence of unknown data, the #LYD_OPT_STRICT can be used. The strict mode is useful for |
| 376 | * NETCONF servers, since NETCONF clients should always send data according to the capabilities announced by the server. |
| 377 | * On the other hand, the default non-strict mode is useful for clients receiving data from NETCONF server since |
| 378 | * clients are not required to understand everything the server does. Of course, the optimal strategy for clients is |
| 379 | * to use filtering to get only the required data. Having an unknown element of the known namespace is always an error. |
| 380 | * The behavior can be changed by #LYD_OPT_STRICT option. |
| 381 | * - using obsolete statements (status set to obsolete) just generates a warning, but the processing continues. The |
| 382 | * behavior can be changed by #LYD_OPT_OBSOLETE option. |
| 383 | * - parser expects that the provided data provides complete datastore content (both the configuration and state data) |
| 384 | * and performs data validation according to all YANG rules. This can be a problem in case of representing NETCONF's |
| 385 | * subtree filter data, edit-config's data or other type of data set - such data do not represent a complete data set |
| 386 | * and some of the validation rules can fail. Therefore there are other options (within lower 8 bits) to make parser |
| 387 | * to accept such a data. |
Radek Krejci | f3c218d | 2016-03-24 12:40:08 +0100 | [diff] [blame] | 388 | * - when parser evaluates when-stmt condition to false, the constrained subtree is automatically removed. If the |
| 389 | * #LYD_OPT_NOAUTODEL is used, error is raised instead of silent auto delete. The option (and also this default |
| 390 | * 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] | 391 | * @{ |
| 392 | */ |
| 393 | |
| 394 | #define LYD_OPT_DATA 0x00 /**< Default type of data - complete datastore content with configuration as well as |
| 395 | state data. */ |
| 396 | #define LYD_OPT_CONFIG 0x01 /**< A configuration datastore - complete datastore without state data. |
| 397 | Validation modifications: |
| 398 | - status data are not allowed */ |
| 399 | #define LYD_OPT_GET 0x02 /**< Data content from a NETCONF reply message to the NETCONF \<get\> operation. |
| 400 | Validation modifications: |
| 401 | - mandatory nodes can be omitted |
| 402 | - leafrefs and instance-identifier are not resolved |
| 403 | - list's keys/unique nodes are not required (so duplication is not checked) */ |
| 404 | #define LYD_OPT_GETCONFIG 0x04 /**< Data content from a NETCONF reply message to the NETCONF \<get-config\> operation |
| 405 | Validation modifications: |
| 406 | - mandatory nodes can be omitted |
| 407 | - leafrefs and instance-identifier are not resolved |
| 408 | - list's keys/unique nodes are not required (so duplication is not checked) |
| 409 | - status data are not allowed */ |
| 410 | #define LYD_OPT_EDIT 0x08 /**< Content of the NETCONF \<edit-config\>'s config element. |
| 411 | Validation modifications: |
| 412 | - mandatory nodes can be omitted |
| 413 | - leafrefs and instance-identifier are not resolved |
| 414 | - status data are not allowed */ |
| 415 | #define LYD_OPT_RPC 0x10 /**< Data represents RPC's input parameters. */ |
| 416 | #define LYD_OPT_RPCREPLY 0x20 /**< Data represents RPC's output parameters (maps to NETCONF <rpc-reply> data). */ |
| 417 | #define LYD_OPT_NOTIF 0x40 /**< Data represents an event notification data. */ |
Michal Vasko | b1b1944 | 2016-07-13 12:26:01 +0200 | [diff] [blame] | 418 | /* 0x80 reserved, formerly LYD_OPT_FILTER, now used internally */ |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 419 | #define LYD_OPT_TYPEMASK 0xff /**< Mask to filter data type options. Always only a single data type option (only |
| 420 | single bit from the lower 8 bits) can be set. */ |
| 421 | |
| 422 | #define LYD_OPT_STRICT 0x0100 /**< Instead of silent ignoring data without schema definition, raise an error. */ |
| 423 | #define LYD_OPT_DESTRUCT 0x0200 /**< Free the provided XML tree during parsing the data. With this option, the |
| 424 | provided XML tree is affected and all succesfully parsed data are freed. |
| 425 | This option is applicable only to lyd_parse_xml() function. */ |
| 426 | #define LYD_OPT_OBSOLETE 0x0400 /**< Raise an error when an obsolete statement (status set to obsolete) is used. */ |
| 427 | #define LYD_OPT_NOSIBLINGS 0x0800 /**< Parse only a single XML tree from the input. This option applies only to |
| 428 | XML input data. */ |
Radek Krejci | 93fab98 | 2016-02-03 15:58:19 +0100 | [diff] [blame] | 429 | #define LYD_OPT_TRUSTED 0x1000 /**< Data comes from a trusted source and it is not needed to validate them. Data |
| 430 | are connected with the schema, but the most validation checks (mandatory nodes, |
| 431 | list instance uniqueness, etc.) are not performed. This option does not make |
| 432 | sense for lyd_validate() so it is ignored by this function. */ |
Radek Krejci | 03b71f7 | 2016-03-16 11:10:09 +0100 | [diff] [blame] | 433 | #define LYD_OPT_NOAUTODEL 0x2000 /**< Avoid automatic delete of subtrees with false when-stmt condition. The flag is |
| 434 | applicable only in combination with LYD_OPT_DATA and LYD_OPT_CONFIG flags. |
| 435 | If used, libyang generates validation error instead of silently removing the |
| 436 | constrained subtree. */ |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 437 | |
| 438 | /**@} parseroptions */ |
| 439 | |
| 440 | /** |
| 441 | * @brief Parse (and validate according to appropriate schema from the given context) data. |
| 442 | * |
| 443 | * In case of LY_XML format, the data string is parsed completely. It means that when it contains |
| 444 | * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The |
| 445 | * returned data node is a root of the first tree with other trees connected via the next pointer. |
| 446 | * This behavior can be changed by #LYD_OPT_NOSIBLINGS option. |
| 447 | * |
| 448 | * @param[in] ctx Context to connect with the data tree being built here. |
| 449 | * @param[in] data Serialized data in the specified format. |
| 450 | * @param[in] format Format of the input data to be parsed. |
| 451 | * @param[in] options Parser options, see @ref parseroptions. |
Michal Vasko | 6b44d71 | 2016-09-12 16:25:46 +0200 | [diff] [blame] | 452 | * @param[in] ... Variable arguments depend on \p options. If they include: |
| 453 | * - #LYD_OPT_DATA: |
| 454 | * - #LYD_OPT_CONFIG: |
| 455 | * - #LYD_OPT_GET: |
| 456 | * - #LYD_OPT_GETCONFIG: |
| 457 | * - #LYD_OPT_EDIT: |
| 458 | * - no variable arguments expected. |
| 459 | * - #LYD_OPT_RPC: |
| 460 | * - #LYD_OPT_NOTIF: |
| 461 | * - struct lyd_node *data_tree - additional data tree that will be used |
| 462 | * when checking any "when" or "must" conditions in the parsed tree that require |
| 463 | * some nodes outside their subtree. It must be a list of top-level elements! |
| 464 | * - #LYD_OPT_RPCREPLY: |
| 465 | * - const struct ::lys_node *rpc_act - pointer to the RPC or action schema node |
| 466 | * operation of the reply. |
| 467 | * - struct lyd_node *data_tree - additional data tree that will be used |
| 468 | * when checking any "when" or "must" conditions in the parsed tree that require |
| 469 | * 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] | 470 | * @return Pointer to the built data tree or NULL in case of empty \p data. To free the returned structure, |
| 471 | * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error, |
| 472 | * #ly_errno contains appropriate error code (see #LY_ERR). |
| 473 | */ |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 474 | 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] | 475 | |
| 476 | /** |
| 477 | * @brief Read data from the given file descriptor. |
| 478 | * |
| 479 | * \note Current implementation supports only reading data from standard (disk) file, not from sockets, pipes, etc. |
| 480 | * |
| 481 | * In case of LY_XML format, the file content is parsed completely. It means that when it contains |
| 482 | * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The |
| 483 | * returned data node is a root of the first tree with other trees connected via the next pointer. |
| 484 | * This behavior can be changed by #LYD_OPT_NOSIBLINGS option. |
| 485 | * |
| 486 | * @param[in] ctx Context to connect with the data tree being built here. |
| 487 | * @param[in] fd The standard file descriptor of the file containing the data tree in the specified format. |
| 488 | * @param[in] format Format of the input data to be parsed. |
| 489 | * @param[in] options Parser options, see @ref parseroptions. |
Michal Vasko | 6b44d71 | 2016-09-12 16:25:46 +0200 | [diff] [blame] | 490 | * @param[in] ... Variable arguments depend on \p options. If they include: |
| 491 | * - #LYD_OPT_DATA: |
| 492 | * - #LYD_OPT_CONFIG: |
| 493 | * - #LYD_OPT_GET: |
| 494 | * - #LYD_OPT_GETCONFIG: |
| 495 | * - #LYD_OPT_EDIT: |
| 496 | * - no variable arguments expected. |
| 497 | * - #LYD_OPT_RPC: |
| 498 | * - #LYD_OPT_NOTIF: |
| 499 | * - struct lyd_node *data_tree - additional data tree that will be used |
| 500 | * when checking any "when" or "must" conditions in the parsed tree that require |
| 501 | * some nodes outside their subtree. It must be a list of top-level elements! |
| 502 | * - #LYD_OPT_RPCREPLY: |
| 503 | * - const struct ::lys_node *rpc_act - pointer to the RPC or action schema node |
| 504 | * operation of the reply. |
| 505 | * - struct lyd_node *data_tree - additional data tree that will be used |
| 506 | * when checking any "when" or "must" conditions in the parsed tree that require |
| 507 | * 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] | 508 | * @return Pointer to the built data tree or NULL in case of empty file. To free the returned structure, |
| 509 | * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error, |
| 510 | * #ly_errno contains appropriate error code (see #LY_ERR). |
| 511 | */ |
| 512 | struct lyd_node *lyd_parse_fd(struct ly_ctx *ctx, int fd, LYD_FORMAT format, int options, ...); |
| 513 | |
| 514 | /** |
| 515 | * @brief Read data from the given file path. |
| 516 | * |
| 517 | * In case of LY_XML format, the file content is parsed completely. It means that when it contains |
| 518 | * a non well-formed XML with multiple root elements, all those sibling XML trees are parsed. The |
| 519 | * returned data node is a root of the first tree with other trees connected via the next pointer. |
| 520 | * This behavior can be changed by #LYD_OPT_NOSIBLINGS option. |
| 521 | * |
| 522 | * @param[in] ctx Context to connect with the data tree being built here. |
| 523 | * @param[in] path Path to the file containing the data tree in the specified format. |
| 524 | * @param[in] format Format of the input data to be parsed. |
| 525 | * @param[in] options Parser options, see @ref parseroptions. |
Michal Vasko | 6b44d71 | 2016-09-12 16:25:46 +0200 | [diff] [blame] | 526 | * @param[in] ... Variable arguments depend on \p options. If they include: |
| 527 | * - #LYD_OPT_DATA: |
| 528 | * - #LYD_OPT_CONFIG: |
| 529 | * - #LYD_OPT_GET: |
| 530 | * - #LYD_OPT_GETCONFIG: |
| 531 | * - #LYD_OPT_EDIT: |
| 532 | * - no variable arguments expected. |
| 533 | * - #LYD_OPT_RPC: |
| 534 | * - #LYD_OPT_NOTIF: |
| 535 | * - struct lyd_node *data_tree - additional data tree that will be used |
| 536 | * when checking any "when" or "must" conditions in the parsed tree that require |
| 537 | * some nodes outside their subtree. It must be a list of top-level elements! |
| 538 | * - #LYD_OPT_RPCREPLY: |
| 539 | * - const struct ::lys_node *rpc_act - pointer to the RPC or action schema node |
| 540 | * operation of the reply. |
| 541 | * - struct lyd_node *data_tree - additional data tree that will be used |
| 542 | * when checking any "when" or "must" conditions in the parsed tree that require |
| 543 | * 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] | 544 | * @return Pointer to the built data tree or NULL in case of empty file. To free the returned structure, |
| 545 | * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error, |
| 546 | * #ly_errno contains appropriate error code (see #LY_ERR). |
| 547 | */ |
| 548 | struct lyd_node *lyd_parse_path(struct ly_ctx *ctx, const char *path, LYD_FORMAT format, int options, ...); |
| 549 | |
| 550 | /** |
| 551 | * @brief Parse (and validate according to appropriate schema from the given context) XML tree. |
| 552 | * |
| 553 | * The output data tree is parsed from the given XML tree previously parsed by one of the |
| 554 | * lyxml_read* functions. |
| 555 | * |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 556 | * 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] | 557 | * or the provided root is a root element of a subtree), all the sibling nodes (previous as well as |
| 558 | * following) are processed as well. The returned data node is a root of the first tree with other |
| 559 | * trees connected via the next pointer. This behavior can be changed by #LYD_OPT_NOSIBLINGS option. |
| 560 | * |
| 561 | * When the function is used with #LYD_OPT_DESTRUCT, all the successfully parsed data including the |
| 562 | * XML \p root and all its siblings (if #LYD_OPT_NOSIBLINGS is not used) are freed. Only with |
| 563 | * #LYD_OPT_DESTRUCT option the \p root pointer is changed - if all the data are parsed, it is set |
| 564 | * to NULL, otherwise it will hold the XML tree without the successfully parsed elements. |
| 565 | * |
| 566 | * The context must be the same as the context used to parse XML tree by lyxml_read* function. |
| 567 | * |
| 568 | * @param[in] ctx Context to connect with the data tree being built here. |
| 569 | * @param[in,out] root XML tree to parse (convert) to data tree. By default, parser do not change the XML tree. However, |
| 570 | * when #LYD_OPT_DESTRUCT is specified in \p options, parser frees all successfully parsed data. |
| 571 | * @param[in] options Parser options, see @ref parseroptions. |
Michal Vasko | 6b44d71 | 2016-09-12 16:25:46 +0200 | [diff] [blame] | 572 | * @param[in] ... Variable arguments depend on \p options. If they include: |
| 573 | * - #LYD_OPT_DATA: |
| 574 | * - #LYD_OPT_CONFIG: |
| 575 | * - #LYD_OPT_GET: |
| 576 | * - #LYD_OPT_GETCONFIG: |
| 577 | * - #LYD_OPT_EDIT: |
| 578 | * - no variable arguments expected. |
| 579 | * - #LYD_OPT_RPC: |
| 580 | * - #LYD_OPT_NOTIF: |
| 581 | * - struct lyd_node *data_tree - additional data tree that will be used |
| 582 | * when checking any "when" or "must" conditions in the parsed tree that require |
| 583 | * some nodes outside their subtree. It must be a list of top-level elements! |
| 584 | * - #LYD_OPT_RPCREPLY: |
| 585 | * - const struct ::lys_node *rpc_act - pointer to the RPC or action schema node |
| 586 | * operation of the reply. |
| 587 | * - struct lyd_node *data_tree - additional data tree that will be used |
| 588 | * when checking any "when" or "must" conditions in the parsed tree that require |
| 589 | * 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] | 590 | * @return Pointer to the built data tree or NULL in case of empty \p root. To free the returned structure, |
| 591 | * use lyd_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error, |
| 592 | * #ly_errno contains appropriate error code (see #LY_ERR). |
| 593 | */ |
| 594 | struct lyd_node *lyd_parse_xml(struct ly_ctx *ctx, struct lyxml_elem **root, int options,...); |
| 595 | |
| 596 | /** |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 597 | * @brief Create a new container node in a data tree. |
| 598 | * |
| 599 | * @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] | 600 | * @param[in] module Module with the node being created. |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 601 | * @param[in] name Schema node name of the new data node. The node can be #LYS_CONTAINER, #LYS_LIST, |
Michal Vasko | a45cf2b | 2015-10-23 09:45:36 +0200 | [diff] [blame] | 602 | * #LYS_NOTIF, or #LYS_RPC. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 603 | * @return New node, NULL on error. |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 604 | */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 605 | 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] | 606 | |
| 607 | /** |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 608 | * @brief Create a new leaf or leaflist node in a data tree with a string value that is converted to |
| 609 | * the actual value. |
| 610 | * |
| 611 | * @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] | 612 | * @param[in] module Module with the node being created. |
| 613 | * @param[in] name Schema node name of the new data node. |
Michal Vasko | 3e671b5 | 2015-10-23 16:23:15 +0200 | [diff] [blame] | 614 | * @param[in] val_str String form of the value of the node being created. In case the type is #LY_TYPE_INST |
| 615 | * 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] | 616 | * @return New node, NULL on error. |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 617 | */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 618 | 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] | 619 | const char *val_str); |
Michal Vasko | 8ea2b7f | 2015-09-29 14:30:53 +0200 | [diff] [blame] | 620 | |
| 621 | /** |
Radek Krejci | b9b4d00 | 2016-01-18 13:08:51 +0100 | [diff] [blame] | 622 | * @brief Change value of a leaf node. |
| 623 | * |
| 624 | * Despite the prototype allows to provide a leaflist node as \p leaf parameter, only leafs are accepted. |
Michal Vasko | 3a55a8a | 2016-04-13 14:19:53 +0200 | [diff] [blame] | 625 | * Also, changing the value of a list key is prohibited. |
Radek Krejci | b9b4d00 | 2016-01-18 13:08:51 +0100 | [diff] [blame] | 626 | * |
Radek Krejci | 0562dbc | 2016-04-18 14:18:26 +0200 | [diff] [blame] | 627 | * As for the other data tree manipulation functions, the change is not fully validated to allow multiple changes |
| 628 | * in the data tree. Therefore, when all changes on the data tree are done, caller is supposed to call lyd_validate() |
| 629 | * to check that the result is valid data tree. Specifically, if a leafref leaf is changed, it is not checked that |
| 630 | * the (leafref) value is correct. |
| 631 | * |
Radek Krejci | b9b4d00 | 2016-01-18 13:08:51 +0100 | [diff] [blame] | 632 | * @param[in] leaf A leaf node to change. |
| 633 | * @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 |
| 634 | * or #LY_TYPE_IDENT, JSON node-id format is expected (nodes are prefixed with module names, not XML namespaces). |
| 635 | * @return 0 on success, non-zero on error. |
| 636 | */ |
| 637 | int lyd_change_leaf(struct lyd_node_leaf_list *leaf, const char *val_str); |
| 638 | |
| 639 | /** |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 640 | * @brief Create a new anydata or anyxml node in a data tree. |
| 641 | * |
| 642 | * 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] | 643 | * |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 644 | * @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] | 645 | * @param[in] module Module with the node being created. |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 646 | * @param[in] name Schema node name of the new data node. The schema node determines if the anydata or anyxml node |
| 647 | * is created. |
| 648 | * @param[in] value Pointer to the value data to be stored in the anydata/anyxml node. The type of the data is |
| 649 | * determined according to the \p value_type parameter. |
| 650 | * @param[in] value_type Type of the provided data \p value. |
Michal Vasko | 1dca688 | 2015-10-22 14:29:42 +0200 | [diff] [blame] | 651 | * @return New node, NULL on error. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 652 | */ |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 653 | struct lyd_node *lyd_new_anydata(struct lyd_node *parent, const struct lys_module *module, const char *name, |
| 654 | void *value, LYD_ANYDATA_VALUETYPE value_type); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 655 | |
| 656 | /** |
Michal Vasko | 98a5a74 | 2016-05-11 11:02:56 +0200 | [diff] [blame] | 657 | * @brief Create a new container node in a data tree. Ignore RPC input nodes and instead use RPC output ones. |
Michal Vasko | 0df122f | 2015-12-14 13:38:21 +0100 | [diff] [blame] | 658 | * |
Michal Vasko | 98a5a74 | 2016-05-11 11:02:56 +0200 | [diff] [blame] | 659 | * @param[in] parent Parent node for the node being created. NULL in case of creating top level element. |
| 660 | * @param[in] module Module with the node being created. |
| 661 | * @param[in] name Schema node name of the new data node. The node can be #LYS_CONTAINER, #LYS_LIST, |
| 662 | * #LYS_NOTIF, or #LYS_RPC. |
Michal Vasko | 0df122f | 2015-12-14 13:38:21 +0100 | [diff] [blame] | 663 | * @return New node, NULL on error. |
| 664 | */ |
Michal Vasko | 98a5a74 | 2016-05-11 11:02:56 +0200 | [diff] [blame] | 665 | 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] | 666 | |
| 667 | /** |
Michal Vasko | 98a5a74 | 2016-05-11 11:02:56 +0200 | [diff] [blame] | 668 | * @brief Create a new leaf or leaflist node in a data tree with a string value that is converted to |
| 669 | * the actual value. Ignore RPC input nodes and instead use RPC output ones. |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame] | 670 | * |
Michal Vasko | 98a5a74 | 2016-05-11 11:02:56 +0200 | [diff] [blame] | 671 | * @param[in] parent Parent node for the node being created. NULL in case of creating top level element. |
| 672 | * @param[in] module Module with the node being created. |
| 673 | * @param[in] name Schema node name of the new data node. |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame] | 674 | * @param[in] val_str String form of the value of the node being created. In case the type is #LY_TYPE_INST |
| 675 | * or #LY_TYPE_IDENT, JSON node-id format is expected (nodes are prefixed with module names, not XML namespaces). |
| 676 | * @return New node, NULL on error. |
| 677 | */ |
Michal Vasko | 98a5a74 | 2016-05-11 11:02:56 +0200 | [diff] [blame] | 678 | struct lyd_node *lyd_new_output_leaf(struct lyd_node *parent, const struct lys_module *module, const char *name, |
| 679 | const char *val_str); |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame] | 680 | |
| 681 | /** |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 682 | * @brief Create a new anydata or anyxml node in a data tree. Ignore RPC input nodes and instead use |
Michal Vasko | 98a5a74 | 2016-05-11 11:02:56 +0200 | [diff] [blame] | 683 | * RPC output ones. |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame] | 684 | * |
Michal Vasko | 98a5a74 | 2016-05-11 11:02:56 +0200 | [diff] [blame] | 685 | * @param[in] parent Parent node for the node being created. NULL in case of creating top level element. |
| 686 | * @param[in] module Module with the node being created. |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 687 | * @param[in] name Schema node name of the new data node. The schema node determines if the anydata or anyxml node |
| 688 | * is created. |
| 689 | * @param[in] value Pointer to the value data to be stored in the anydata/anyxml node. The type of the data is |
| 690 | * determined according to the \p value_type parameter. Data are supposed to be dynamically allocated. |
| 691 | * Since it is directly attached into the created data node, caller is supposed to not manipulate with |
| 692 | * the data after a successful call (including calling free() on the provided data). |
| 693 | * @param[in] value_type Type of the provided data \p value. |
Michal Vasko | 50c0a87 | 2016-01-13 14:34:11 +0100 | [diff] [blame] | 694 | * @return New node, NULL on error. |
| 695 | */ |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 696 | struct lyd_node *lyd_new_output_anydata(struct lyd_node *parent, const struct lys_module *module, const char *name, |
| 697 | void *value, LYD_ANYDATA_VALUETYPE value_type); |
Michal Vasko | 0df122f | 2015-12-14 13:38:21 +0100 | [diff] [blame] | 698 | |
| 699 | /** |
Michal Vasko | f529928 | 2016-03-16 13:32:02 +0100 | [diff] [blame] | 700 | * @defgroup pathoptions Data path creation options |
| 701 | * @ingroup datatree |
| 702 | * |
| 703 | * Various options to change lyd_new_path() behavior. |
| 704 | * |
| 705 | * Default behavior: |
Michal Vasko | f529928 | 2016-03-16 13:32:02 +0100 | [diff] [blame] | 706 | * - if the target node already exists, an error is returned. |
Michal Vasko | 9db078d | 2016-03-23 11:08:51 +0100 | [diff] [blame] | 707 | * - 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] | 708 | * - 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] | 709 | * @{ |
| 710 | */ |
| 711 | |
Michal Vasko | 72d3510 | 2016-03-31 10:03:38 +0200 | [diff] [blame] | 712 | #define LYD_PATH_OPT_UPDATE 0x01 /**< If the target node exists and is a leaf, it is updated with the new value and returned. |
| 713 | If the target node exists and is not a leaf, NULL is returned and no error set. */ |
Michal Vasko | 9db078d | 2016-03-23 11:08:51 +0100 | [diff] [blame] | 714 | #define LYD_PATH_OPT_NOPARENT 0x02 /**< If any parents of the target node exist, return an error. */ |
Michal Vasko | 2411b94 | 2016-03-23 13:50:03 +0100 | [diff] [blame] | 715 | #define LYD_PATH_OPT_OUTPUT 0x04 /**< Changes the behavior to ignoring RPC input schema nodes and using only output ones. */ |
Michal Vasko | f529928 | 2016-03-16 13:32:02 +0100 | [diff] [blame] | 716 | |
| 717 | /** @} pathoptions */ |
| 718 | |
| 719 | /** |
| 720 | * @brief Create a new data node based on a simple XPath. |
| 721 | * |
Michal Vasko | 8d18ef5 | 2016-04-06 12:21:46 +0200 | [diff] [blame] | 722 | * The new node is normally inserted at the end, either as the last child of a parent or as the last sibling |
| 723 | * 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] | 724 | * required and always guaranteed. |
Michal Vasko | 58f74f1 | 2016-03-24 13:26:06 +0100 | [diff] [blame] | 725 | * |
Michal Vasko | 8c41964 | 2016-04-13 14:22:01 +0200 | [diff] [blame] | 726 | * If \p path points to a list key and the list does not exist, the key value from the predicate is used |
| 727 | * and \p value is ignored. |
| 728 | * |
Michal Vasko | 8947293 | 2016-07-15 09:45:15 +0200 | [diff] [blame] | 729 | * @param[in] data_tree Existing data tree to add to/modify. If creating RPCs, there should only be one RPC and |
| 730 | * either input or output. Can be NULL. |
Michal Vasko | f529928 | 2016-03-16 13:32:02 +0100 | [diff] [blame] | 731 | * @param[in] ctx Context to use. Mandatory if \p data_tree is NULL. |
Michal Vasko | 9db078d | 2016-03-23 11:08:51 +0100 | [diff] [blame] | 732 | * @param[in] path Simple data XPath of the new node. It can contain only simple node addressing with optional |
Michal Vasko | f529928 | 2016-03-16 13:32:02 +0100 | [diff] [blame] | 733 | * module names as prefixes. List nodes must have predicates, one for each list key in the correct order and |
Michal Vasko | 1acf850 | 2016-05-05 09:14:07 +0200 | [diff] [blame] | 734 | * with its value as well, leaves and leaf-lists can have predicates too that have preference over \p value, |
| 735 | * see @ref howtoxpath. |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 736 | * @param[in] value Value of the new leaf/lealf-list (const char*). If creating anydata or anyxml, the following |
| 737 | * \p value_type parameter is required to be specified correctly. If creating nodes of other types, the |
| 738 | * parameter is ignored. |
| 739 | * @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] | 740 | * @param[in] options Bitmask of options flags, see @ref pathoptions. |
Michal Vasko | 8c41964 | 2016-04-13 14:22:01 +0200 | [diff] [blame] | 741 | * @return First created (or updated with #LYD_PATH_OPT_UPDATE) node, |
Michal Vasko | 17bb490 | 2016-04-05 15:20:51 +0200 | [diff] [blame] | 742 | * 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] | 743 | * NULL and ly_errno is set on error. |
Michal Vasko | f529928 | 2016-03-16 13:32:02 +0100 | [diff] [blame] | 744 | */ |
Radek Krejci | 4582601 | 2016-08-24 15:07:57 +0200 | [diff] [blame] | 745 | struct lyd_node *lyd_new_path(struct lyd_node *data_tree, struct ly_ctx *ctx, const char *path, void *value, |
| 746 | LYD_ANYDATA_VALUETYPE value_type, int options); |
Michal Vasko | f529928 | 2016-03-16 13:32:02 +0100 | [diff] [blame] | 747 | |
| 748 | /** |
Michal Vasko | c0797f8 | 2015-10-14 15:51:25 +0200 | [diff] [blame] | 749 | * @brief Create a copy of the specified data tree \p node. Namespaces are copied as needed, |
| 750 | * schema references are kept the same. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 751 | * |
| 752 | * @param[in] node Data tree node to be duplicated. |
| 753 | * @param[in] recursive 1 if all children are supposed to be also duplicated. |
| 754 | * @return Created copy of the provided data \p node. |
| 755 | */ |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 756 | struct lyd_node *lyd_dup(const struct lyd_node *node, int recursive); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 757 | |
| 758 | /** |
Michal Vasko | 45fb282 | 2016-04-18 13:32:17 +0200 | [diff] [blame] | 759 | * @brief Merge a (sub)tree into a data tree. Missing nodes are merged, leaf values updated. |
| 760 | * If \p target and \p source do not share the top-level schema node, even if they |
| 761 | * are from different modules, \p source parents up to top-level node will be created and |
| 762 | * linked to the \p target (but only containers can be created this way, lists need keys, |
| 763 | * so if lists are missing, an error will be returned). |
| 764 | * |
| 765 | * In short, this function will always try to return a fully valid data tree and will fail |
Michal Vasko | cf6dc7e | 2016-04-18 16:00:37 +0200 | [diff] [blame] | 766 | * if it is not possible. Also, in some less common cases, despite both trees \p target and |
| 767 | * \p source are valid, the resulting tree may be invalid and this function will succeed. |
| 768 | * If you know there are such possibilities in your data trees or you are not sure, always |
| 769 | * validate the resulting merged \p target tree. |
Michal Vasko | 45fb282 | 2016-04-18 13:32:17 +0200 | [diff] [blame] | 770 | * |
Michal Vasko | cf6dc7e | 2016-04-18 16:00:37 +0200 | [diff] [blame] | 771 | * @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] | 772 | * @param[in] source Data tree to merge \p target with. Must be valid (at least as a subtree). |
| 773 | * @param[in] options Bitmask of 2 option flags: |
| 774 | * LYD_OPT_DESTRUCT - spend \p source in the function, otherwise \p source is left untouched, |
| 775 | * LYD_OPT_NOSIBLINGS - merge only the \p source subtree (ignore siblings), otherwise merge |
| 776 | * \p source and all its succeeding siblings (preceeding ones are still ignored!). |
| 777 | * @return 0 on success, nonzero in case of an error. |
| 778 | */ |
| 779 | int lyd_merge(struct lyd_node *target, const struct lyd_node *source, int options); |
| 780 | |
| 781 | /** |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 782 | * @brief Insert the \p node element as child to the \p parent element. The \p node is inserted as a last child of the |
| 783 | * \p parent. |
| 784 | * |
| 785 | * If the node is part of some other tree, it is automatically unlinked. |
Radek Krejci | a1c33bf | 2016-09-07 12:38:49 +0200 | [diff] [blame] | 786 | * If the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted. |
| 787 | * If the key of a list is being inserted, it is placed into a correct position instead of being placed as the last |
| 788 | * element. |
Radek Krejci | fd0bcf0 | 2016-09-09 13:28:34 +0200 | [diff] [blame] | 789 | * If the target tree includes the default instance of the node being inserted, the default node is silently replaced |
| 790 | * by the new node. On the other hand, if a default node is being inserted and the target tree already contains |
| 791 | * non-default instance, the default node is not inserted (it is skipped and freed). |
| 792 | * If a non-default node is being inserted and there is already its non-default instance in the target tree, the new |
| 793 | * node is inserted and it is up to the caller to solve the presence of multiple instances afterwards. |
| 794 | * |
| 795 | * Note that this function differs from lyd_insert_before() and lyd_insert_after() because the position of the |
| 796 | * node being inserted is determined automatically according to the rules described above. In contrast to |
| 797 | * lyd_insert_parent(), lyd_insert() can not be used for top-level elements since the \p parent parameter must not be |
| 798 | * NULL. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 799 | * |
| 800 | * @param[in] parent Parent node for the \p node being inserted. |
| 801 | * @param[in] node The node being inserted. |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 802 | * @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] | 803 | * in the data tree. |
| 804 | */ |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 805 | int lyd_insert(struct lyd_node *parent, struct lyd_node *node); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 806 | |
| 807 | /** |
Radek Krejci | fd0bcf0 | 2016-09-09 13:28:34 +0200 | [diff] [blame] | 808 | * @brief Insert the \p node element as a last sibling of the specified \p sibling element. |
| 809 | * |
| 810 | * If the node is part of some other tree, it is automatically unlinked. |
| 811 | * If the node is the first node of a node list (with no parent), all the subsequent nodes are also inserted. |
| 812 | * If the key of a list is being inserted, it is placed into a correct position instead of being placed as the last |
| 813 | * element. |
| 814 | * If the target tree includes the default instance of the node being inserted, the default node is silently replaced |
| 815 | * (so it is not inserted as the last sibling in this case) by the new node. On the other hand, if a default node is |
| 816 | * being inserted and the target tree already contains non-default instance, the default node is not inserted (it is |
| 817 | * skipped and freed). If a non-default node is being inserted and there is already its non-default instance in the |
| 818 | * target tree, the new node is inserted and it is up to the caller to solve the presence of multiple instances |
| 819 | * afterwards. |
| 820 | * |
| 821 | * Note that this function differs from lyd_insert_before() and lyd_insert_after() because the position of the |
| 822 | * node being inserted is determined automatically as in the case of lyd_insert(). In contrast to lyd_insert(), |
| 823 | * lyd_insert_sibling() can be used to insert top-level elements. |
| 824 | * |
| 825 | * @param[in,out] sibling Sibling node as a reference where to insert the \p node. When function succeeds, the sibling |
| 826 | * is always set to point to the first sibling node. Note that in some cases described above, the provided sibling |
| 827 | * node could be removed from the tree. |
| 828 | * @param[in] node The node being inserted. |
| 829 | * @return 0 on success, nonzero in case of error, e.g. when the node is being inserted to an inappropriate place |
| 830 | * in the data tree. |
| 831 | */ |
| 832 | int lyd_insert_sibling(struct lyd_node **sibling, struct lyd_node *node); |
| 833 | |
| 834 | /** |
Michal Vasko | 3f7dba1 | 2015-10-15 13:09:27 +0200 | [diff] [blame] | 835 | * @brief Insert the \p node element after the \p sibling element. If \p node and \p siblings are already |
Radek Krejci | ca7efb7 | 2016-01-18 13:06:01 +0100 | [diff] [blame] | 836 | * siblings (just moving \p node position), skip validation. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 837 | * |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 838 | * @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] | 839 | * @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] | 840 | * @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] | 841 | * in the data tree. |
| 842 | */ |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 843 | int lyd_insert_before(struct lyd_node *sibling, struct lyd_node *node); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 844 | |
| 845 | /** |
Radek Krejci | 20a5f29 | 2016-02-09 15:04:49 +0100 | [diff] [blame] | 846 | * @brief Insert the \p node element after the \p sibling element. If \p node and \p siblings are already |
| 847 | * siblings (just moving \p node position), skip validation. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 848 | * |
Michal Vasko | 3f7dba1 | 2015-10-15 13:09:27 +0200 | [diff] [blame] | 849 | * @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] | 850 | * are already siblings (just moving \p node position), skip validation. |
Radek Krejci | 20a5f29 | 2016-02-09 15:04:49 +0100 | [diff] [blame] | 851 | * @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] | 852 | * @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] | 853 | * in the data tree. |
| 854 | */ |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 855 | int lyd_insert_after(struct lyd_node *sibling, struct lyd_node *node); |
| 856 | |
| 857 | /** |
Radek Krejci | c090d99 | 2016-09-07 16:26:03 +0200 | [diff] [blame] | 858 | * @brief Insert the \p new element instead of the \p old element. |
| 859 | * |
| 860 | * If the \p new is the first node of a node list (with no parent), all the subsequent nodes are also inserted. |
| 861 | * If the \p new is NULL and \p destroy is true, it works like lyd_free(old). |
| 862 | * |
| 863 | * @param[in] old The specific node supposed to be replaced. |
| 864 | * @param[in] new The new (list of) node(s) to be inserted instead of \p old |
| 865 | * @param[in] destroy Flag for freeing the \p old. |
| 866 | * @return 0 on success, nonzero in case of error. |
| 867 | */ |
| 868 | int lyd_replace(struct lyd_node *old, struct lyd_node *new, int destroy); |
| 869 | |
| 870 | /** |
Michal Vasko | 2411b94 | 2016-03-23 13:50:03 +0100 | [diff] [blame] | 871 | * @brief Order siblings according to the schema node ordering. |
| 872 | * |
Michal Vasko | 58f74f1 | 2016-03-24 13:26:06 +0100 | [diff] [blame] | 873 | * If the siblings include data nodes from other modules, they are |
| 874 | * sorted based on the module order in the context. |
| 875 | * |
| 876 | * @param[in] sibling Node, whose siblings will be sorted. |
| 877 | * @param[in] recursive Whether sort all siblings of siblings, recursively. |
| 878 | * @return 0 on success, nonzero in case of an error. |
Michal Vasko | 2411b94 | 2016-03-23 13:50:03 +0100 | [diff] [blame] | 879 | */ |
Michal Vasko | 58f74f1 | 2016-03-24 13:26:06 +0100 | [diff] [blame] | 880 | int lyd_schema_sort(struct lyd_node *sibling, int recursive); |
Michal Vasko | 2411b94 | 2016-03-23 13:50:03 +0100 | [diff] [blame] | 881 | |
| 882 | /** |
Michal Vasko | 105cef1 | 2016-02-04 12:06:26 +0100 | [diff] [blame] | 883 | * @brief Search in the given data for instances of nodes matching the provided XPath expression. |
| 884 | * |
Michal Vasko | 7fdf9b3 | 2016-03-01 15:59:48 +0100 | [diff] [blame] | 885 | * The XPath expression is evaluated on data -> skip all non-data nodes (input, output, choice, case). |
Michal Vasko | 105cef1 | 2016-02-04 12:06:26 +0100 | [diff] [blame] | 886 | * |
Michal Vasko | 7fdf9b3 | 2016-03-01 15:59:48 +0100 | [diff] [blame] | 887 | * Expr examples: |
| 888 | * "/ietf-yang-library:modules-state/module[name = 'ietf-yang-library']/namespace" |
| 889 | * "/ietf-netconf:get-config/source" |
| 890 | * |
Michal Vasko | 46a4bf9 | 2016-09-08 08:23:49 +0200 | [diff] [blame] | 891 | * @param[in] data Node in the data tree considered the context node if \p expr is relative, |
| 892 | * otherwise any node. |
Michal Vasko | 105cef1 | 2016-02-04 12:06:26 +0100 | [diff] [blame] | 893 | * @param[in] expr XPath expression filtering the matching nodes. |
Michal Vasko | 46a4bf9 | 2016-09-08 08:23:49 +0200 | [diff] [blame] | 894 | * @return Set of found data nodes. If no nodes are matching \p expr or the result |
Michal Vasko | 105cef1 | 2016-02-04 12:06:26 +0100 | [diff] [blame] | 895 | * would be a number, a string, or a boolean, the returned set is empty. In case of an error, NULL is returned. |
| 896 | */ |
Michal Vasko | f06fb5b | 2016-09-08 10:05:56 +0200 | [diff] [blame] | 897 | struct ly_set *lyd_find_xpath(const struct lyd_node *data, const char *expr); |
Michal Vasko | 105cef1 | 2016-02-04 12:06:26 +0100 | [diff] [blame] | 898 | |
| 899 | /** |
Radek Krejci | c5b6b91 | 2016-01-18 16:35:35 +0100 | [diff] [blame] | 900 | * @brief Search in the given data for instances of the provided schema node. |
| 901 | * |
| 902 | * The \p data is used to find the data root and function then searches in the whole tree and all sibling trees. |
| 903 | * |
| 904 | * @param[in] data A node in the data tree to search. |
| 905 | * @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] | 906 | * @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] | 907 | * In case of error, NULL is returned. |
| 908 | */ |
Michal Vasko | f06fb5b | 2016-09-08 10:05:56 +0200 | [diff] [blame] | 909 | 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] | 910 | |
| 911 | /** |
Radek Krejci | d788a52 | 2016-07-25 14:57:38 +0200 | [diff] [blame] | 912 | * @brief Get the first sibling of the given node. |
| 913 | * |
| 914 | * @param[in] node Node which first sibling is going to be the result. |
| 915 | * @return The first sibling of the given node or the node itself if it is the first child of the parent. |
| 916 | */ |
| 917 | struct lyd_node *lyd_first_sibling(struct lyd_node *node); |
| 918 | |
| 919 | /** |
Radek Krejci | be90a90 | 2016-06-14 16:11:51 +0200 | [diff] [blame] | 920 | * @brief Resolve the leafref. |
| 921 | * |
| 922 | * This function is considered to be a part of a low level API and it should be used deliberately. |
| 923 | * |
| 924 | * @param[in] leafref The leafref node to resolve. |
| 925 | * @return |
| 926 | * - EXIT_SUCCESS on success, |
| 927 | * - EXIT_FAILURE when target does not exist, |
| 928 | * - -1 on error. |
| 929 | */ |
| 930 | int lyd_validate_leafref(struct lyd_node_leaf_list *leafref); |
| 931 | |
| 932 | /** |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 933 | * @brief Validate \p node data subtree. |
| 934 | * |
Michal Vasko | dedea83 | 2016-04-19 11:24:45 +0200 | [diff] [blame] | 935 | * @param[in,out] node Data tree to be validated. In case the \p options does not includes #LYD_OPT_NOAUTODEL, libyang |
Michal Vasko | b2f40be | 2016-09-08 16:03:48 +0200 | [diff] [blame] | 936 | * can modify the provided tree including the root \p node. |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 937 | * @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^] | 938 | * @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] | 939 | * - #LYD_OPT_DATA: |
| 940 | * - #LYD_OPT_CONFIG: |
| 941 | * - #LYD_OPT_GET: |
| 942 | * - #LYD_OPT_GETCONFIG: |
| 943 | * - #LYD_OPT_EDIT: |
Michal Vasko | cdb9017 | 2016-09-13 09:34:36 +0200 | [diff] [blame^] | 944 | * - struct ly_ctx *ctx - context to use when \p node is NULL (for checking an empty tree), |
| 945 | * otherwise can be NULL. |
Michal Vasko | 6b44d71 | 2016-09-12 16:25:46 +0200 | [diff] [blame] | 946 | * - #LYD_OPT_RPC: |
| 947 | * - #LYD_OPT_RPCREPLY: |
| 948 | * - #LYD_OPT_NOTIF: |
| 949 | * - 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^] | 950 | * any "when" or "must" conditions in the \p node tree |
| 951 | * that require some nodes outside their subtree. If set, |
| 952 | * it must be a list of top-level elements! |
Radek Krejci | 92ece00 | 2016-04-04 15:45:05 +0200 | [diff] [blame] | 953 | * @return 0 on success, nonzero in case of an error. |
Michal Vasko | 2433739 | 2015-10-16 09:58:16 +0200 | [diff] [blame] | 954 | */ |
Michal Vasko | cdb9017 | 2016-09-13 09:34:36 +0200 | [diff] [blame^] | 955 | int lyd_validate(struct lyd_node **node, int options, void *var_arg); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 956 | |
| 957 | /** |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 958 | * @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] | 959 | * |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 960 | * @param[in] node The leaf or leaf-list to check. Note, that leaf-list is marked as default only when the complete |
| 961 | * and only the default set is present (node's siblings are also checked). |
| 962 | * @return 1 if the node contains the default value, 0 otherwise. |
Radek Krejci | 7b4309c | 2016-03-23 10:30:29 +0100 | [diff] [blame] | 963 | */ |
Radek Krejci | 46180b5 | 2016-08-31 16:01:32 +0200 | [diff] [blame] | 964 | int lyd_wd_default(struct lyd_node_leaf_list *node); |
Radek Krejci | 6b8f6ac | 2016-03-23 12:33:04 +0100 | [diff] [blame] | 965 | |
| 966 | /** |
Michal Vasko | 55f60be | 2015-10-14 13:12:58 +0200 | [diff] [blame] | 967 | * @brief Unlink the specified data subtree. All referenced namespaces are copied. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 968 | * |
| 969 | * Note, that the node's connection with the schema tree is kept. Therefore, in case of |
| 970 | * reconnecting the node to a data tree using lyd_paste() it is necessary to paste it |
| 971 | * to the appropriate place in the data tree following the schema. |
| 972 | * |
| 973 | * @param[in] node Data tree node to be unlinked (together with all children). |
| 974 | * @return 0 for success, nonzero for error |
| 975 | */ |
| 976 | int lyd_unlink(struct lyd_node *node); |
| 977 | |
| 978 | /** |
Radek Krejci | fd0bcf0 | 2016-09-09 13:28:34 +0200 | [diff] [blame] | 979 | * @brief Free (and unlink) the specified data subtree. Use carefully, since libyang silently creates default nodes, |
| 980 | * 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] | 981 | * |
| 982 | * @param[in] node Root of the (sub)tree to be freed. |
| 983 | */ |
| 984 | void lyd_free(struct lyd_node *node); |
| 985 | |
| 986 | /** |
Radek Krejci | fd0bcf0 | 2016-09-09 13:28:34 +0200 | [diff] [blame] | 987 | * @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] | 988 | * |
| 989 | * @param[in] node One of the siblings root element of the (sub)trees to be freed. |
| 990 | */ |
| 991 | void lyd_free_withsiblings(struct lyd_node *node); |
| 992 | |
| 993 | /** |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 994 | * @brief Insert attribute into the data node. |
| 995 | * |
| 996 | * @param[in] parent Data node where to place the attribute |
Radek Krejci | 70ecd72 | 2016-03-21 09:04:00 +0100 | [diff] [blame] | 997 | * @param[in] mod An alternative way to specify attribute's module (namespace) used in case the \p name does |
| 998 | * not include prefix. If neither prefix in the \p name nor mod is specified, the attribute's |
| 999 | * module is inherited from the \p parent node. It is not allowed to have attributes with no |
| 1000 | * module (namespace). |
| 1001 | * @param[in] name Attribute name. The string can include the attribute's module (namespace) as the name's |
| 1002 | * prefix (prefix:name). Prefix must be the name of one of the schema in the \p parent's context. |
| 1003 | * If the prefix is not specified, the \p mod parameter is used. If neither of these parameters is |
| 1004 | * usable, attribute inherits module (namespace) from the \p parent node. It is not allowed to |
| 1005 | * have attributes with no module (namespace). |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 1006 | * @param[in] value Attribute value |
| 1007 | * @return pointer to the created attribute (which is already connected in \p parent) or NULL on error. |
| 1008 | */ |
Radek Krejci | 70ecd72 | 2016-03-21 09:04:00 +0100 | [diff] [blame] | 1009 | struct lyd_attr *lyd_insert_attr(struct lyd_node *parent, const struct lys_module *mod, const char *name, |
| 1010 | const char *value); |
Radek Krejci | 134610e | 2015-10-20 17:15:34 +0200 | [diff] [blame] | 1011 | |
| 1012 | /** |
Radek Krejci | 88f2930 | 2015-10-30 15:42:33 +0100 | [diff] [blame] | 1013 | * @brief Destroy data attribute |
| 1014 | * |
| 1015 | * If the attribute to destroy is a member of a node attribute list, it is necessary to |
| 1016 | * provide the node itself as \p parent to keep the list consistent. |
| 1017 | * |
| 1018 | * @param[in] ctx Context where the attribute was created (usually it is the context of the \p parent) |
| 1019 | * @param[in] parent Parent node where the attribute is placed |
| 1020 | * @param[in] attr Attribute to destroy |
| 1021 | * @param[in] recursive Zero to destroy only the attribute, non-zero to destroy also all the subsequent attributes |
| 1022 | * in the list. |
| 1023 | */ |
| 1024 | void lyd_free_attr(struct ly_ctx *ctx, struct lyd_node *parent, struct lyd_attr *attr, int recursive); |
| 1025 | |
| 1026 | /** |
Radek Krejci | 6910a03 | 2016-04-13 10:06:21 +0200 | [diff] [blame] | 1027 | * @brief Return main module of the data tree node. |
| 1028 | * |
| 1029 | * In case of regular YANG module, it returns ::lys_node#module pointer, |
| 1030 | * but in case of submodule, it returns pointer to the main module. |
| 1031 | * |
| 1032 | * @param[in] node Data tree node to be examined |
| 1033 | * @return pointer to the main module (schema structure), NULL in case of error. |
| 1034 | */ |
| 1035 | struct lys_module *lyd_node_module(const struct lyd_node *node); |
| 1036 | |
| 1037 | /** |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 1038 | * @brief Print data tree in the specified format. |
| 1039 | * |
| 1040 | * Same as lyd_print(), but it allocates memory and store the data into it. |
| 1041 | * It is up to caller to free the returned string by free(). |
| 1042 | * |
| 1043 | * @param[out] strp Pointer to store the resulting dump. |
| 1044 | * @param[in] root Root node of the data tree to print. It can be actually any (not only real root) |
| 1045 | * node of the data tree to print the specific subtree. |
| 1046 | * @param[in] format Data output format. |
| 1047 | * @param[in] options [printer flags](@ref printerflags). |
| 1048 | * @return 0 on success, 1 on failure (#ly_errno is set). |
| 1049 | */ |
| 1050 | 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] | 1051 | |
| 1052 | /** |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 1053 | * @brief Print data tree in the specified format. |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1054 | * |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 1055 | * Same as lyd_print(), but output is written into the specified file descriptor. |
| 1056 | * |
| 1057 | * @param[in] root Root node of the data tree to print. It can be actually any (not only real root) |
| 1058 | * node of the data tree to print the specific subtree. |
| 1059 | * @param[in] fd File descriptor where to print the data. |
| 1060 | * @param[in] format Data output format. |
| 1061 | * @param[in] options [printer flags](@ref printerflags). |
| 1062 | * @return 0 on success, 1 on failure (#ly_errno is set). |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1063 | */ |
Radek Krejci | def5002 | 2016-02-01 16:38:32 +0100 | [diff] [blame] | 1064 | int lyd_print_fd(int fd, const struct lyd_node *root, LYD_FORMAT format, int options); |
| 1065 | |
| 1066 | /** |
| 1067 | * @brief Print data tree in the specified format. |
| 1068 | * |
| 1069 | * To write data into a file descriptor, use lyd_print_fd(). |
| 1070 | * |
| 1071 | * @param[in] root Root node of the data tree to print. It can be actually any (not only real root) |
| 1072 | * node of the data tree to print the specific subtree. |
| 1073 | * @param[in] f File stream where to print the data. |
| 1074 | * @param[in] format Data output format. |
| 1075 | * @param[in] options [printer flags](@ref printerflags). |
| 1076 | * @return 0 on success, 1 on failure (#ly_errno is set). |
| 1077 | */ |
| 1078 | int lyd_print_file(FILE *f, const struct lyd_node *root, LYD_FORMAT format, int options); |
| 1079 | |
| 1080 | /** |
| 1081 | * @brief Print data tree in the specified format. |
| 1082 | * |
| 1083 | * Same as lyd_print(), but output is written via provided callback. |
| 1084 | * |
| 1085 | * @param[in] root Root node of the data tree to print. It can be actually any (not only real root) |
| 1086 | * node of the data tree to print the specific subtree. |
| 1087 | * @param[in] writeclb Callback function to write the data (see write(1)). |
| 1088 | * @param[in] arg Optional caller-specific argument to be passed to the \p writeclb callback. |
| 1089 | * @param[in] format Data output format. |
| 1090 | * @param[in] options [printer flags](@ref printerflags). |
| 1091 | * @return 0 on success, 1 on failure (#ly_errno is set). |
| 1092 | */ |
| 1093 | int lyd_print_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg, |
| 1094 | const struct lyd_node *root, LYD_FORMAT format, int options); |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1095 | |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 1096 | /**@} */ |
| 1097 | |
| 1098 | #ifdef __cplusplus |
| 1099 | } |
| 1100 | #endif |
| 1101 | |
| 1102 | #endif /* LY_TREE_DATA_H_ */ |