Skip to content

Commit 27b06ea

Browse files
committed
sysrepo UPDATE deleting all (leaf-)list instances in push oper data
1 parent df96385 commit 27b06ea

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

src/sysrepo.c

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3518,7 +3518,9 @@ sr_delete_item(sr_session_ctx_t *session, const char *path, const sr_edit_option
35183518
const char *op;
35193519
const struct lysc_node *snode;
35203520
struct lyd_node *node;
3521+
struct ly_set *set;
35213522
uint32_t temp_lo = 0;
3523+
uint32_t i;
35223524
int rc;
35233525

35243526
SR_CHECK_ARG_APIRET(!session || !path || !SR_IS_STANDARD_DS(session->ds) || (!SR_IS_CONVENTIONAL_DS(session->ds) &&
@@ -3552,17 +3554,39 @@ sr_delete_item(sr_session_ctx_t *session, const char *path, const sr_edit_option
35523554
}
35533555

35543556
if (session->ds == SR_DS_OPERATIONAL) {
3555-
/* just delete the selected node */
3556-
node = NULL;
3557-
if (session->dt[session->ds].edit->tree &&
3558-
(err_info = sr_lyd_find_path(session->dt[session->ds].edit->tree, path, 1, &node))) {
3557+
if ((err_info = sr_lys_find_path(session->conn->ly_ctx, path, NULL, &snode))) {
3558+
/* invalid path */
35593559
goto cleanup;
35603560
}
3561-
if (node) {
3562-
sr_lyd_free_tree_safe(node, &session->dt[session->ds].edit->tree);
3563-
} else if (opts & SR_EDIT_STRICT) {
3564-
/* not found */
3565-
sr_errinfo_new(&err_info, SR_ERR_NOT_FOUND, "Node \"%s\" not found in session push oper data.", path);
3561+
if ((snode->nodetype & (LYS_LIST | LYS_LEAFLIST)) && (path[strlen(path) - 1] != ']')) {
3562+
/* purge all the (leaf-)list instances */
3563+
set = NULL;
3564+
if (session->dt[session->ds].edit->tree &&
3565+
(err_info = sr_lyd_find_xpath(session->dt[session->ds].edit->tree, path, &set))) {
3566+
goto cleanup;
3567+
}
3568+
if (set->count) {
3569+
for (i = 0; i < set->count; ++i) {
3570+
sr_lyd_free_tree_safe(set->dnodes[i], &session->dt[session->ds].edit->tree);
3571+
}
3572+
} else if (opts & SR_EDIT_STRICT) {
3573+
/* not found */
3574+
sr_errinfo_new(&err_info, SR_ERR_NOT_FOUND, "No nodes \"%s\" found in session push oper data.", path);
3575+
}
3576+
ly_set_free(set, NULL);
3577+
} else {
3578+
/* just delete the selected node */
3579+
node = NULL;
3580+
if (session->dt[session->ds].edit->tree &&
3581+
(err_info = sr_lyd_find_path(session->dt[session->ds].edit->tree, path, 1, &node))) {
3582+
goto cleanup;
3583+
}
3584+
if (node) {
3585+
sr_lyd_free_tree_safe(node, &session->dt[session->ds].edit->tree);
3586+
} else if (opts & SR_EDIT_STRICT) {
3587+
/* not found */
3588+
sr_errinfo_new(&err_info, SR_ERR_NOT_FOUND, "Node \"%s\" not found in session push oper data.", path);
3589+
}
35663590
}
35673591
goto cleanup;
35683592
}

tests/test_edit.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,16 @@ test_purge(void **state)
856856
ret = sr_get_subtree(st->sess, "/test:ll1", 0, &subtree);
857857
assert_int_equal(ret, SR_ERR_OK);
858858
assert_null(subtree);
859+
860+
/* operational ds */
861+
sr_session_switch_ds(st->sess, SR_DS_OPERATIONAL);
862+
ret = sr_set_item_str(st->sess, "/test:ll1", "12", NULL, 0);
863+
assert_int_equal(ret, SR_ERR_OK);
864+
ret = sr_delete_item(st->sess, "/test:l1", 0);
865+
assert_int_equal(ret, SR_ERR_OK);
866+
ret = sr_delete_item(st->sess, "/test:ll1", 0);
867+
assert_int_equal(ret, SR_ERR_OK);
868+
sr_session_switch_ds(st->sess, SR_DS_RUNNING);
859869
}
860870

861871
static void

0 commit comments

Comments
 (0)