# API for getting 'confd\_cs\_node\*'

**URL:** <https://dmap-community.ductus.global/t/api-for-getting-confd-cs-node/280>\
**Category:** Core Engine and APIs\
**Created:** [November 6, 2015, 10:46am UTC](https://dmap-community.ductus.global/t/api-for-getting-confd-cs-node/280 "2015-11-06T10:46:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![arunmu](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@arunmu](https://dmap-community.ductus.global/u/arunmu)\
**Post date:** [November 6, 2015, 10:46am UTC](https://dmap-community.ductus.global/t/api-for-getting-confd-cs-node/280/1 "2015-11-06T10:46:53Z")

</div>

Hi,  
I am looking for a API that would provide me with ‘confd\_cs\_node’ pointer to corresponding node represented by its ‘xml\_tag’ structure for the below yang structure.

```
 notification new-route {
        description "Notification for new route";
        leaf name {
          description "Route name";
          type string;
        }
    
        leaf id {
          description "Route ID";
          type uint16;
        }
      }

  notification temp-alarm {
    description "Temperature threshold alarm";
    leaf message {
      description "Alarm message";
      type string;
    }

    leaf curr-temp {
      description "Current temperature";
      type uint16;
    }

    leaf thresh-temp {
      description "Threshold temperature";
      type uint16;
    }
  }

```

I have the xml\_tag structure populated with correct values for ‘temp-alarm’. Now, I need the ‘confd\_cs\_node’ structure for that node.

The only API I see is ‘confd\_find\_cs\_root’ which gives me the ‘confd\_cs\_node’ for ‘new-route’ node and since ‘temp-alarm’ is not a child of it, I cant traverse from there.

Is there any API to get the ‘confd\_cs\_node’ of the node specifies by the xml\_tag directly ?

Thanks.

---

<div class="post-metadata">

**Author:** ![cohult](https://yyz2.discourse-cdn.com/flex010/user_avatar/dmap-community.ductus.global/cohult/32/221_2.png) [@cohult](https://dmap-community.ductus.global/u/cohult)\
**Post date:** [November 6, 2015, 12:26pm UTC](https://dmap-community.ductus.global/t/api-for-getting-confd-cs-node/280/2 "2015-11-06T12:26:54Z")

</div>

From the confd\_lib\_lib man page:

> struct confd\_cs\_node \*confd\_find\_cs\_root(int ns);  
> When schema information is available to the library, this function returns the root of the tree representaton of the namespace given by ns, i.e. a pointer to the struct confd\_cs\_node for the (first) toplevel node. For namespaces that are augmented into other namespaces such that they do not have a toplevel node, this function returns NULL - the nodes of such a namespace are found below the augment target node(s) in other tree(s). See confd\_types(3).  
> struct confd\_cs\_node \*confd\_find\_cs\_node(const confd\_hkeypath\_t \*hkey- path, int len);  
> Utility function which finds the struct confd\_cs\_node corresponding to the len first elements of the hashed keypath. To make the search consider the full keypath, pass the len element from the confd\_hkeypath\_t structure (i.e. mykeypath-\>len). See confd\_types(3).  
> struct confd\_cs\_node \*confd\_find\_cs\_node\_child(const struct confd\_cs\_node \*parent, struct xml\_tag xmltag);  
> Utility function which finds the struct confd\_cs\_node corresponding to the child node given as xmltag. See confd\_types(3).  
> struct confd\_cs\_node \*confd\_cs\_node\_cd(const struct confd\_cs\_node \*start, const char \*fmt, …);  
> Utility function which finds the resulting struct confd\_cs\_node given an (optional) starting node and a (relative or absolute) string keypath. I.e. this function navigates the tree in a manner corresponding to cdb\_cd()/maapi\_cd(). Note however that the confd\_cs\_node tree does not have a node correspond- ing to “/”. It is possible to pass start as NULL, in which case the path must be absolute (i.e. start with a “/”).  
> Since the key values are not relevant for the tree navigation, the key elements can be omitted, i.e. a “tagpath” can be used - if present, key elements are ignored, whether given in the {…} form or the CDB-only [N] form. See confd\_types(3).  
> If the path can not be found, NULL is returned, confd\_errno is set to CONFD\_ERR\_BADPATH, and confd\_lasterr() can be used to retrieve a string that describes the reason for the failure.

One reference you can use is the print\_modifications() in this example:  
[http://discuss.tail-f.com/t/how-to-convert-a-confd-tag-value-array-to-xml/154](http://discuss.tail-f.com/t/how-to-convert-a-confd-tag-value-array-to-xml/154)

---

<div class="post-metadata">

**Author:** ![per](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@per](https://dmap-community.ductus.global/u/per)\
**Post date:** [November 6, 2015, 1:00pm UTC](https://dmap-community.ductus.global/t/api-for-getting-confd-cs-node/280/3 "2015-11-06T13:00:33Z")

</div>

There is no function to do exactly what you ask for, since the confd\_cs\_node tree lacks a “super-root” corresponding to “/”, which would have your struct xml\_tag as a child that could be found with confd\_find\_cs\_node\_child(). But you can walk the toplevel nodes of the module/namespace by following the ‘next’ pointer in the node that you got with confd\_find\_cs\_root(). I.e. you could have a function like (untested)

```
struct confd_cs_node *get_toplevel_node(struct xml_tag *tag)
{
    struct confd_cs_node *node = confd_find_cs_root(tag->ns);
    while (node != NULL) {
        if (node->tag == tag->tag && node->ns == tag->ns)
            return node;
        node = node->next;
    }
    return NULL;
}
```

---

<div class="post-metadata">

**Author:** ![per](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@per](https://dmap-community.ductus.global/u/per)\
**Post date:** [November 6, 2015, 1:14pm UTC](https://dmap-community.ductus.global/t/api-for-getting-confd-cs-node/280/4 "2015-11-06T13:14:43Z")

</div>

Actually a slightly better version (returns NULL faster if the tag is wrong, since you can walk into a different module by following the ‘next’ pointer ) is (still untested)

```
struct confd_cs_node *get_toplevel_node(struct xml_tag *tag)
{
    struct confd_cs_node *node = confd_find_cs_root(tag->ns);
    while (node != NULL && node->ns == tag->ns) {
        if (node->tag == tag->tag)
            return node;
        node = node->next;
    }
    return NULL;
}
```

---

<div class="post-metadata">

**Author:** ![arunmu](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@arunmu](https://dmap-community.ductus.global/u/arunmu)\
**Post date:** [November 23, 2015, 6:45pm UTC](https://dmap-community.ductus.global/t/api-for-getting-confd-cs-node/280/5 "2015-11-23T18:45:31Z")

</div>

Thanks Per.  
I did something like that but did not have to iterate since I knew at what level my node would be present.

---

<div class="post-metadata">

**Author:** ![per](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@per](https://dmap-community.ductus.global/u/per)\
**Post date:** [November 24, 2015, 7:14am UTC](https://dmap-community.ductus.global/t/api-for-getting-confd-cs-node/280/6 "2015-11-24T07:14:04Z")

</div>

Good! Actually on second (third:-) thought, I realize that my answer was unnecessarily complex, focusing on your question rather than the actual problem: the simple way to find a given toplevel node is to use confd\_cs\_node\_cd(), mentioned in cohult’s answer. E.g. for the ‘new-route’ notification, assuming that ‘new-route’ is unique at the top level: confd\_cs\_node\_cd(NULL, “/new-route”). If it is not unique, you need to include the namespace prefix - can be a good idea to do that anyway for “future-proofing” (a module added at a later point may introduce new toplevel elements such that the one you want is no longer unique: confd\_cs\_node\_cd(NULL, “/prefix:new-route”).
