The following examples illustrates how to construct a container data node.
The container nodes can be constructed different ways depending whether it is a top level container or not. If the container is not a top level container, the following code can be used:
...
/* get the container obj template */
obj_template_t *cont_obj =
obj_find_child(parentobj,
modname,
objname);
if (!cont_obj) {
return ERR_NCX_DEF_NOT_FOUND;
}
/* make a container value */
val_value_t *cont_value = val_new_value();
if (!cont_value) {
return ERR_INTERNAL_MEM;
}
val_init_from_template(cont_value, cont_obj);
...On the other hand, if the container is a top level container, and you are constructing it in order to use as a static or virtual data within running datastore. If the container represents operational data, the following example can be used:
...
cfg_template_t *runningcfg = cfg_get_config_id(NCX_CFGID_RUNNING);
if (!runningcfg || !runningcfg->root) {
return ERR_NCX_DEF_NOT_FOUND;
}
/* get the top object node */
obj_template_t *topobj =
obj_find_template_top(mod,
module_name,
object_name);
if (!topobj) {
return ERR_NCX_DEF_NOT_FOUND;
}
/* construct top level container */
val_value_t *cont_value = val_new_value();
if (!cont_value) {
return ERR_INTERNAL_MEM;
}
val_init_from_template(cont_value, topobj);
/* handing off the malloced memory here */
res = val_child_add(cont_value, runningcfg->root);
if (res != NO_ERR) {
val_free_value(cont_value);
return res;
}
...As a result of these two examples, we will have an empty container with no any children. In order to add a child to the container, the following example that creates multiple leaf-list entries and adds them into parent container can be used:
...
status_t res = NO_ERR;
int32 value = 0;
for (value = 10; value <= 15 && res==NO_ERR; value++) {
val_value_t *leaflist_value =
agt_make_int_leaf(parentobj,
leaflistname,
value,
&res);
if (!leaflist_value) {
return res;
}
/* add a new leaf-list entry to parent container */
res = val_child_add(leaflist_value, cont_value);
if (res != NO_ERR) {
val_free_value(leaflist_value);
return res;
}
}
...In order to construct nested containers, the same steps should be applied twice, as illustrated below:
...
cfg_template_t *runningcfg = cfg_get_config_id(NCX_CFGID_RUNNING);
if (!runningcfg || !runningcfg->root) {
return ERR_NCX_DEF_NOT_FOUND;
}
/* get the top object node */
obj_template_t *topobj =
obj_find_template_top(mod,
module_name,
object_name);
if (!topobj) {
return ERR_NCX_DEF_NOT_FOUND;
}
/* construct top level container */
val_value_t *cont_value = val_new_value();
if (!cont_value) {
return ERR_INTERNAL_MEM;
}
val_init_from_template(cont_value, topobj);
/* handing off the malloced memory here */
status_t res = val_child_add(cont_value, runningcfg→root);
if (res != NO_ERR) {
val_free_value(cont_value);
return res;
}
/* get the next container obj template */
obj_template_t *cont_obj =
obj_find_child(topobj,
modname,
objname);
if (!cont_obj) {
val_free_value(cont_value);
return ERR_NCX_DEF_NOT_FOUND;
}
/* make a next container value */
val_value_t *next_cont_value = val_new_value();
if (!next_cont_value) {
val_free_value(cont_value);
return ERR_INTERNAL_MEM;
}
val_init_from_template(next_cont_value, cont_obj);
res = val_child_add(next_cont_value, cont_value);
if (res != NO_ERR) {
val_free_value(next_cont_value);
val_free_value(cont_value);
return res;
}
...