# Create an element in CDB

**URL:** <https://dmap-community.ductus.global/t/create-an-element-in-cdb/1530>\
**Category:** CDB and CDB API\
**Created:** [September 15, 2017, 8:31pm UTC](https://dmap-community.ductus.global/t/create-an-element-in-cdb/1530 "2017-09-15T20:31:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![srinadh](https://avatars.discourse-cdn.com/v4/letter/s/8c91f0/32.png) [@srinadh](https://dmap-community.ductus.global/u/srinadh)\
**Post date:** [September 15, 2017, 8:31pm UTC](https://dmap-community.ductus.global/t/create-an-element-in-cdb/1530/1 "2017-09-15T20:31:12Z")

</div>

I have few parameters that have default values in my implementation. I am trying to add those parameters with respective values using maapi API.

cos-classifier is a leaf with two enum values in the yang file

However, it does not work for me… maapi\_set\_elem2() is returning an error all the time…  
is there something that I am missing here?  
I tried using “pcp” instead of “1” and even “enum\<1\>” as third argument to maapi\_set\_elem2().

```
maapi_set_namespace(msock, maapi_thandle, link__ns);

syslog(LOG_INFO, "setting cos-classifier to be pcp");
if (maapi_set_elem2(msock, maapi_thandle, "1", "/link/cos-classifier") != CONFD_OK) {
    syslog(LOG_ERR, "cos-classifier init to pcp failed");
}

maapi_apply_trans(msock, maapi_thandle, 0);

if (maapi_finish_trans(msock, maapi_thandle) != CONFD_OK) {
    syslog(LOG_ERR, "%s: failed to finish transaction", __FUNCTION__ );
}
syslog(LOG_INFO, "%s: Changes applied to CDB successfully", __FUNCTION__ );

maapi_close(msock);
```

---

<div class="post-metadata">

**Author:** ![waitai](https://yyz2.discourse-cdn.com/flex010/user_avatar/dmap-community.ductus.global/waitai/32/436_2.png) [@waitai](https://dmap-community.ductus.global/u/waitai)\
**Post date:** [September 15, 2017, 9:29pm UTC](https://dmap-community.ductus.global/t/create-an-element-in-cdb/1530/2 "2017-09-15T21:29:30Z")

</div>

Since you didn’t provide the YANG model for your project, I will use the intro/1-2-3 example to illustrate how maapi\_set\_elem2( ) works with enum values.

Following is a snippet of dhcpd.yang for the example:

```
  typedef loglevel {
    type enumeration {
        enum kern;
        enum mail;
        enum local7;
    }
  }

  container dhcp {
    …
    leaf logFacility {
      type loglevel;
      default local7;
    }
    …
  }

```

You should be using the names of the enum as defined in the YANG when working with the maapi\_set\_elem2( ) API call. I will demonstrate using the confd\_cmd tool which is a wrapper of the ConfD library functions in which the man-page for it can be found in the ConfD User Guide. The mset command translates to maapi\_set\_elem2( ).

```
$ confd_cmd -d -c 'mget /dhcp/logFacility'
mget "/dhcp/logFacility"
local7
$ confd_cmd -d -c 'mset /dhcp/logFacility kern'
mset "/dhcp/logFacility" "kern"
$ confd_cmd -d -c 'mget /dhcp/logFacility'
mget "/dhcp/logFacility"
kern
$
```

---

<div class="post-metadata">

**Author:** ![srinadh](https://avatars.discourse-cdn.com/v4/letter/s/8c91f0/32.png) [@srinadh](https://dmap-community.ductus.global/u/srinadh)\
**Post date:** [September 15, 2017, 9:48pm UTC](https://dmap-community.ductus.global/t/create-an-element-in-cdb/1530/3 "2017-09-15T21:48:06Z")

</div>

Here is my yang definition.  
typedef cndlp-classifier-mode {  
type enumeration {  
enum vid {  
tailf:info “Classification based on vlan tag”;  
}  
enum pcp {  
tailf:info “Classification based on pcp bits”;  
}  
}  
tailf:info “classifier mode [vid|pcp]”;  
}

```
 leaf cos-classifier {
      when "../mode = 'differentiated'";
      type cndlp-types:cndlp-classifier-mode;
      tailf:info "Mode of traffic treatment based on vlan or pcp";
  }

```

I tried three different ways: all of them returning an error…  
if (maapi\_set\_elem2(msock, maapi\_thandle, “1”, “/link/cos-classifier”) != CONFD\_OK) {  
if (maapi\_set\_elem2(msock, maapi\_thandle, “pcp”, “/link/cos-classifier”) != CONFD\_OK) {  
if (maapi\_set\_elem2(msock, maapi\_thandle, “enum\<1\>”, “/link/cos-classifier”) != CONFD\_OK) {

---

<div class="post-metadata">

**Author:** ![waitai](https://yyz2.discourse-cdn.com/flex010/user_avatar/dmap-community.ductus.global/waitai/32/436_2.png) [@waitai](https://dmap-community.ductus.global/u/waitai)\
**Post date:** [September 15, 2017, 10:35pm UTC](https://dmap-community.ductus.global/t/create-an-element-in-cdb/1530/4 "2017-09-15T22:35:30Z")

</div>

I have tried your YANG model without the when statement and everything works as expected.

Is your mode configured correctly? What error message did you get? Try it again by commenting out the when statement.

---

<div class="post-metadata">

**Author:** ![srinadh](https://avatars.discourse-cdn.com/v4/letter/s/8c91f0/32.png) [@srinadh](https://dmap-community.ductus.global/u/srinadh)\
**Post date:** [September 15, 2017, 10:41pm UTC](https://dmap-community.ductus.global/t/create-an-element-in-cdb/1530/5 "2017-09-15T22:41:28Z")

</div>

Thanks @waitai  
Thats a great catch; didnot occur to me!
