Skip to content

Commit

Permalink
lib: change type of inner to struct lyd_node *, cleaner code
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Hopps <[email protected]>
  • Loading branch information
choppsv1 committed Jan 11, 2024
1 parent 32a4c40 commit 79df49c
Showing 1 changed file with 39 additions and 30 deletions.
69 changes: 39 additions & 30 deletions lib/northbound_oper.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ PREDECL_LIST(nb_op_walks);
* This is our information about a node on the branch we are looking at
*/
struct nb_op_node_info {
struct lyd_node_inner *inner;
struct lyd_node *inner;
const struct lysc_node *schema; /* inner schema in case we rm inner */
struct yang_list_keys keys; /* if list, keys to locate element */
const void *list_entry; /* opaque entry from user or NULL */
Expand Down Expand Up @@ -183,7 +183,7 @@ static struct lyd_node *ys_root_node(struct nb_op_yield_state *ys)
{
if (!darr_len(ys->node_infos))
return NULL;
return &ys->node_infos[0].inner->node;
return ys->node_infos[0].inner;
}

static void ys_trim_xpath(struct nb_op_yield_state *ys)
Expand All @@ -210,7 +210,7 @@ static void ys_free_inner(struct nb_op_yield_state *ys,
struct nb_op_node_info *ni)
{
if (!CHECK_FLAG(ni->schema->nodetype, LYS_CASE | LYS_CHOICE))
lyd_free_tree(&ni->inner->node);
lyd_free_tree(ni->inner);
ni->inner = NULL;
}

Expand Down Expand Up @@ -415,7 +415,7 @@ static enum nb_error nb_op_ys_finalize_node_info(struct nb_op_yield_state *ys,
uint index)
{
struct nb_op_node_info *ni = &ys->node_infos[index];
struct lyd_node_inner *inner = ni->inner;
struct lyd_node *inner = ni->inner;
struct nb_node *nn = ni->schema->priv;
bool yield_ok = ys->finish != NULL;

Expand All @@ -425,13 +425,13 @@ static enum nb_error nb_op_ys_finalize_node_info(struct nb_op_yield_state *ys,
ni->list_entry = index == 0 ? NULL : ni[-1].list_entry;

/* Assert that we are walking the rightmost branch */
assert(!inner->parent || &inner->node == inner->parent->child->prev);
assert(!inner->parent || inner == inner->parent->child->prev);

if (CHECK_FLAG(inner->schema->nodetype,
LYS_CASE | LYS_CHOICE | LYS_CONTAINER)) {
/* containers have only zero or one child on a branch of a tree */
inner = (struct lyd_node_inner *)inner->child;
assert(!inner || inner->prev == &inner->node);
inner = ((struct lyd_node_inner *)inner)->child;
assert(!inner || inner->prev == inner);
ni->lookup_next_ok = yield_ok &&
(index == 0 || ni[-1].lookup_next_ok);
return NB_OK;
Expand All @@ -442,7 +442,7 @@ static enum nb_error nb_op_ys_finalize_node_info(struct nb_op_yield_state *ys,
ni->lookup_next_ok = yield_ok && ni->has_lookup_next &&
(index == 0 || ni[-1].lookup_next_ok);

nb_op_get_keys(inner, &ni->keys);
nb_op_get_keys((struct lyd_node_inner *)inner, &ni->keys);

/* A list entry cannot be present in a tree w/o it's keys */
assert(ni->keys.num == yang_snode_num_keys(inner->schema));
Expand Down Expand Up @@ -489,7 +489,7 @@ static enum nb_error nb_op_ys_finalize_node_info(struct nb_op_yield_state *ys,
static enum nb_error nb_op_ys_init_node_infos(struct nb_op_yield_state *ys)
{
struct nb_op_node_info *ni;
struct lyd_node_inner *inner;
struct lyd_node *inner;
struct lyd_node *node = NULL;
enum nb_error ret;
uint i, len;
Expand Down Expand Up @@ -521,18 +521,18 @@ static enum nb_error nb_op_ys_init_node_infos(struct nb_op_yield_state *ys)
if (!node)
return NB_ERR_NOT_FOUND;

inner = (struct lyd_node_inner *)node;
inner = node;
for (len = 1; inner->parent; len++)
inner = inner->parent;
inner = &inner->parent->node;

darr_append_nz_mt(ys->node_infos, len, MTYPE_NB_NODE_INFOS);

/*
* For each node find the prefix of the xpath query that identified it
* -- save the prefix length.
*/
inner = (struct lyd_node_inner *)node;
for (i = len; i > 0; i--, inner = inner->parent) {
inner = node;
for (i = len; i > 0; i--, inner = &inner->parent->node) {
ni = &ys->node_infos[i - 1];
ni->inner = inner;
ni->schema = inner->schema;
Expand All @@ -542,7 +542,7 @@ static enum nb_error nb_op_ys_init_node_infos(struct nb_op_yield_state *ys)
* since the number of nodes is small and only done once per
* query.
*/
tmp = yang_dnode_get_path(&inner->node, NULL, 0);
tmp = yang_dnode_get_path(inner, NULL, 0);
ni->xpath_len = strlen(tmp);

/* Replace users supplied xpath with the libyang returned value */
Expand Down Expand Up @@ -610,8 +610,8 @@ static enum nb_error nb_op_iter_leaf(struct nb_op_yield_state *ys,
return NB_OK;

/* Add a dnode to our tree */
err = lyd_new_term(&ni->inner->node, snode->module, snode->name,
data->value, false, NULL);
err = lyd_new_term(ni->inner, snode->module, snode->name, data->value,
false, NULL);
if (err) {
yang_data_free(data);
return NB_ERR_RESOURCE;
Expand Down Expand Up @@ -652,7 +652,7 @@ static enum nb_error nb_op_iter_leaflist(struct nb_op_yield_state *ys,
continue;

/* Add a dnode to our tree */
err = lyd_new_term(&ni->inner->node, snode->module, snode->name,
err = lyd_new_term(ni->inner, snode->module, snode->name,
data->value, false, NULL);
if (err) {
yang_data_free(data);
Expand Down Expand Up @@ -917,6 +917,13 @@ static enum nb_error __walk(struct nb_op_yield_state *ys, bool is_resume)
if (!walk_stem_tip)
return NB_ERR_NOT_FOUND;

if (ys->schema_path[0]->nodetype == LYS_CHOICE) {
flog_err(EC_LIB_NB_OPERATIONAL_DATA,
"%s: unable to walk root level choice node from module: %s",
__func__, ys->schema_path[0]->module->name);
return NB_ERR;
}

/*
* If we are resuming then start with the list container on top.
* Otherwise get the first child of the container we are walking,
Expand Down Expand Up @@ -972,7 +979,7 @@ static enum nb_error __walk(struct nb_op_yield_state *ys, bool is_resume)
* remove the node from the tree.
*/
if (sib->nodetype == LYS_CONTAINER &&
!lyd_child(&ni->inner->node) &&
!lyd_child(ni->inner) &&
!nb_op_empty_container_ok(sib, ys->xpath,
ni->list_entry))
ys_free_inner(ys, ni);
Expand Down Expand Up @@ -1061,11 +1068,11 @@ static enum nb_error __walk(struct nb_op_yield_state *ys, bool is_resume)

if (sib->nodetype != LYS_CONTAINER) {
/* Case/choice use parent inner. */
node = &ni->inner->node;
/* TODO: thus we don't support root level choice */
node = ni->inner;
} else {
err = lyd_new_inner(&ni->inner->node,
sib->module, sib->name,
false, &node);
err = lyd_new_inner(ni->inner, sib->module,
sib->name, false, &node);
if (err) {
ret = NB_ERR_RESOURCE;
goto done;
Expand All @@ -1074,7 +1081,7 @@ static enum nb_error __walk(struct nb_op_yield_state *ys, bool is_resume)

/* push this choice/container node on top of the stack */
ni = darr_appendz(ys->node_infos);
ni->inner = (struct lyd_node_inner *)node;
ni->inner = node;
ni->schema = sib;
ni->lookup_next_ok = ni[-1].lookup_next_ok;
ni->list_entry = ni[-1].list_entry;
Expand Down Expand Up @@ -1172,7 +1179,7 @@ static enum nb_error __walk(struct nb_op_yield_state *ys, bool is_resume)
at_clevel <= darr_lasti(ys->query_tokens) &&
!ys->non_specific_predicate[at_clevel] &&
nb_op_schema_path_has_predicate(ys, at_clevel)) {
err = lyd_new_path(&pni->inner->node, NULL,
err = lyd_new_path(pni->inner, NULL,
ys->query_tokens[at_clevel],
NULL, 0, &node);
if (!err)
Expand Down Expand Up @@ -1295,7 +1302,7 @@ static enum nb_error __walk(struct nb_op_yield_state *ys, bool is_resume)
* and do not reap if true.
*/
if (!list_start && ni->inner &&
!lyd_child_no_keys(&ni->inner->node) &&
!lyd_child_no_keys(ni->inner) &&
/* not the top element with a key match */
!((darr_ilen(ys->node_infos) ==
darr_ilen(ys->schema_path) - 1) &&
Expand Down Expand Up @@ -1434,8 +1441,10 @@ static enum nb_error __walk(struct nb_op_yield_state *ys, bool is_resume)
*/

if (!node) {
err = yang_lyd_new_list(ni[-1].inner, sib,
&ni->keys, &node);
err = yang_lyd_new_list((struct lyd_node_inner *)
ni[-1]
.inner,
sib, &ni->keys, &node);
if (err) {
darr_pop(ys->node_infos);
ret = NB_ERR_RESOURCE;
Expand All @@ -1446,7 +1455,7 @@ static enum nb_error __walk(struct nb_op_yield_state *ys, bool is_resume)
/*
* Save the new list entry with the list node info
*/
ni->inner = (struct lyd_node_inner *)node;
ni->inner = node;
ni->schema = node->schema;
ni->list_entry = list_entry;
ni->niters += 1;
Expand Down Expand Up @@ -1550,13 +1559,13 @@ static void nb_op_trim_yield_state(struct nb_op_yield_state *ys)
assert(ni->has_lookup_next);

DEBUGD(&nb_dbg_events, "NB oper-state: deleting tree at level %d", i);
__free_siblings(&ni->inner->node);
__free_siblings(ni->inner);
ys_free_inner(ys, ni);

while (--i > 0) {
DEBUGD(&nb_dbg_events,
"NB oper-state: deleting siblings at level: %d", i);
__free_siblings(&ys->node_infos[i].inner->node);
__free_siblings(ys->node_infos[i].inner);
}
DEBUGD(&nb_dbg_events, "NB oper-state: stop trimming: new top: %d",
(int)darr_lasti(ys->node_infos));
Expand Down

0 comments on commit 79df49c

Please sign in to comment.