libyang  0.16.105
YANG data modeling language library
Manipulating Data

There are many functions to create or modify an existing data tree. You can add new nodes, reconnect nodes from one tree to another (or e.g. from one list instance to another) or remove nodes. The functions doesn't allow you to put a node to a wrong place (by checking the module), but not all validation checks can be made directly (or you have to make a valid change by multiple tree modifications) when the tree is being changed. Therefore, there is lyd_validate() function supposed to be called to make sure that the current data tree is valid. If working with RPCs, they are invalid also in case the data nodes are not ordered according to the schema, which you can fix easily with lyd_schema_sort(). Note, that not performing validation after some data tree changes can cause failure of various libyang functions later. All functions performing data tree changes are marked with the PARTIAL CHANGE flag in their documentation meaning they leave at least partly non-validated data tree.

Creating data is generally possible in two ways, they can be combined. You can add nodes one-by-one based on the node name and/or its parent (lyd_new(), lyd_new_anydata_*(), lyd_new_leaf(), and their output variants) or address the nodes using a simple XPath addressing (lyd_new_path()). The latter enables to create a whole path of nodes, requires less information about the modified data, and is generally simpler to use. The path format specifics can be found here.

Working with two data subtrees can also be performed two ways. Usually, you would use lyd_insert*() functions. They are generally meant for simple inserts of a node into a data tree. For more complicated inserts and when merging 2 trees use lyd_merge(). It offers additional options and is basically a more powerful insert.

Also remember, that when you are creating/inserting a node, all the objects in that operation must belong to the same context.

Modifying the single data tree in multiple threads is not safe.

Functions List