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