@@ -188,6 +188,61 @@ lyd_validate_node_when(const struct lyd_node *tree, const struct lyd_node *node,
188188 return LY_SUCCESS ;
189189}
190190
191+ /**
192+ * @brief Properly delete a node as part of auto-delete validation tasks.
193+ *
194+ * @param[in,out] first First sibling, is updated if needed.
195+ * @param[in] del Node instance to delete.
196+ * @param[in] mod Module of the siblings, NULL for nested siblings.
197+ * @param[in,out] node Optional current iteration node, update it if it is deleted.
198+ * @param[in,out] node_when Optional set with nodes with "when" conditions, may be removed from.
199+ * @param[in,out] diff Validation diff.
200+ * @return 1 if @p node auto-deleted and updated to its next sibling.
201+ * @return 0 if @p node was not auto-deleted.
202+ */
203+ static ly_bool
204+ lyd_validate_autodel_node_del (struct lyd_node * * first , struct lyd_node * del , const struct lys_module * mod ,
205+ struct lyd_node * * node , struct ly_set * node_types , struct lyd_node * * diff )
206+ {
207+ struct lyd_node * iter ;
208+ ly_bool node_autodel = 0 ;
209+ uint32_t idx ;
210+
211+ /* update pointers */
212+ lyd_del_move_root (first , del , mod );
213+ if (node && (del == * node )) {
214+ * node = (* node )-> next ;
215+ node_autodel = 1 ;
216+ }
217+
218+ if (diff ) {
219+ /* add into diff */
220+ if ((del -> schema -> nodetype == LYS_CONTAINER ) && !(del -> schema -> flags & LYS_PRESENCE )) {
221+ /* we do not want to track NP container changes, but remember any removed children */
222+ LY_LIST_FOR (lyd_child (del ), iter ) {
223+ lyd_val_diff_add (iter , LYD_DIFF_OP_DELETE , diff );
224+ }
225+ } else {
226+ lyd_val_diff_add (del , LYD_DIFF_OP_DELETE , diff );
227+ }
228+ }
229+
230+ if (node_types && node_types -> count ) {
231+ /* remove from node_types set */
232+ LYD_TREE_DFS_BEGIN (del , iter ) {
233+ if (ly_set_contains (node_types , iter , & idx )) {
234+ ly_set_rm_index (node_types , idx , NULL );
235+ }
236+ LYD_TREE_DFS_END (del , iter );
237+ }
238+ }
239+
240+ /* free */
241+ lyd_free_tree (del );
242+
243+ return node_autodel ;
244+ }
245+
191246/**
192247 * @brief Evaluate when conditions of collected unres nodes.
193248 *
@@ -208,9 +263,9 @@ lyd_validate_unres_when(struct lyd_node **tree, const struct lys_module *mod, st
208263 uint32_t xpath_options , struct ly_set * node_types , struct lyd_node * * diff )
209264{
210265 LY_ERR rc = LY_SUCCESS , r ;
211- uint32_t i , idx ;
266+ uint32_t i ;
212267 const struct lysc_when * disabled ;
213- struct lyd_node * node = NULL , * elem ;
268+ struct lyd_node * node = NULL ;
214269
215270 if (!node_when -> count ) {
216271 return LY_SUCCESS ;
@@ -229,29 +284,7 @@ lyd_validate_unres_when(struct lyd_node **tree, const struct lys_module *mod, st
229284 /* when false */
230285 if (node -> flags & LYD_WHEN_TRUE ) {
231286 /* autodelete */
232- lyd_del_move_root (tree , node , mod );
233- if (diff ) {
234- /* add into diff */
235- r = lyd_val_diff_add (node , LYD_DIFF_OP_DELETE , diff );
236- LY_CHECK_ERR_GOTO (r , rc = r , error );
237- }
238-
239- /* remove from node types set, if present */
240- if (node_types && node_types -> count ) {
241- LYD_TREE_DFS_BEGIN (node , elem ) {
242- /* only term nodes with a validation callback can be in node_types */
243- if ((elem -> schema -> nodetype & LYD_NODE_TERM ) &&
244- ((struct lysc_node_leaf * )elem -> schema )-> type -> plugin -> validate &&
245- ly_set_contains (node_types , elem , & idx )) {
246- r = ly_set_rm_index (node_types , idx , NULL );
247- LY_CHECK_ERR_GOTO (r , rc = r , error );
248- }
249- LYD_TREE_DFS_END (node , elem );
250- }
251- }
252-
253- /* free */
254- lyd_free_tree (node );
287+ lyd_validate_autodel_node_del (tree , node , mod , NULL , node_types , diff );
255288 } else if (val_opts & LYD_VALIDATE_OPERATIONAL ) {
256289 /* only a warning */
257290 LOGWRN (LYD_CTX (node ), "When condition \"%s\" not satisfied." , disabled -> cond -> expr );
@@ -568,45 +601,6 @@ lyd_val_has_default(const struct lysc_node *schema)
568601 return 0 ;
569602}
570603
571- /**
572- * @brief Properly delete a node as part of auto-delete validation tasks.
573- *
574- * @param[in,out] first First sibling, is updated if needed.
575- * @param[in] del Node instance to delete.
576- * @param[in] mod Module of the siblings, NULL for nested siblings.
577- * @param[in,out] node Current iteration node, update it if it is deleted.
578- * @param[in,out] diff Validation diff.
579- * @return 1 if @p node auto-deleted and updated to its next sibling.
580- * @return 0 if @p node was not auto-deleted.
581- */
582- static ly_bool
583- lyd_validate_autodel_node_del (struct lyd_node * * first , struct lyd_node * del , const struct lys_module * mod ,
584- struct lyd_node * * node , struct lyd_node * * diff )
585- {
586- struct lyd_node * iter ;
587- ly_bool node_autodel = 0 ;
588-
589- lyd_del_move_root (first , del , mod );
590- if (del == * node ) {
591- * node = (* node )-> next ;
592- node_autodel = 1 ;
593- }
594- if (diff ) {
595- /* add into diff */
596- if ((del -> schema -> nodetype == LYS_CONTAINER ) && !(del -> schema -> flags & LYS_PRESENCE )) {
597- /* we do not want to track NP container changes, but remember any removed children */
598- LY_LIST_FOR (lyd_child (del ), iter ) {
599- lyd_val_diff_add (iter , LYD_DIFF_OP_DELETE , diff );
600- }
601- } else {
602- lyd_val_diff_add (del , LYD_DIFF_OP_DELETE , diff );
603- }
604- }
605- lyd_free_tree (del );
606-
607- return node_autodel ;
608- }
609-
610604/**
611605 * @brief Auto-delete leaf-list default instances to prevent validation errors.
612606 *
@@ -645,7 +639,7 @@ lyd_validate_autodel_leaflist_dflt(struct lyd_node **first, struct lyd_node **no
645639 LYD_LIST_FOR_INST_SAFE (* first , schema , next , iter ) {
646640 if (iter -> flags & LYD_DEFAULT ) {
647641 /* default instance found, remove it */
648- if (lyd_validate_autodel_node_del (first , iter , mod , node , diff )) {
642+ if (lyd_validate_autodel_node_del (first , iter , mod , node , NULL , diff )) {
649643 node_autodel = 1 ;
650644 }
651645 }
@@ -690,7 +684,7 @@ lyd_validate_autodel_cont_leaf_dflt(struct lyd_node **first, struct lyd_node **n
690684 LYD_LIST_FOR_INST_SAFE (* first , schema , next , iter ) {
691685 if (iter -> flags & LYD_DEFAULT ) {
692686 /* default instance, remove it */
693- if (lyd_validate_autodel_node_del (first , iter , mod , node , diff )) {
687+ if (lyd_validate_autodel_node_del (first , iter , mod , node , NULL , diff )) {
694688 node_autodel = 1 ;
695689 }
696690 }
@@ -700,7 +694,7 @@ lyd_validate_autodel_cont_leaf_dflt(struct lyd_node **first, struct lyd_node **n
700694 LYD_LIST_FOR_INST (* first , schema , iter ) {
701695 if ((iter -> flags & LYD_DEFAULT ) && !(iter -> flags & LYD_NEW )) {
702696 /* old default instance, remove it */
703- if (lyd_validate_autodel_node_del (first , iter , mod , node , diff )) {
697+ if (lyd_validate_autodel_node_del (first , iter , mod , node , NULL , diff )) {
704698 node_autodel = 1 ;
705699 }
706700 break ;
@@ -758,7 +752,7 @@ lyd_validate_autodel_case_dflt(struct lyd_node **first, struct lyd_node **node,
758752 if (!iter ) {
759753 /* there are only default nodes of the case meaning it does not exist and neither should any default nodes
760754 * of the case, remove this one default node */
761- if (lyd_validate_autodel_node_del (first , * node , mod , node , diff )) {
755+ if (lyd_validate_autodel_node_del (first , * node , mod , node , NULL , diff )) {
762756 node_autodel = 1 ;
763757 }
764758 }
0 commit comments