Error while using Maapi_Create to load default configuration into CDB

we are using Maapi API’s (maapi_create,maapi_setelem) to load default configuration into CDB.
But we are facing one issue. some of the list elements of our yang model are dynamically
created so we should not have any list element after loading Default configuration.

whenever we try to load without creating this list element into CDB we are getting this error: CONFD_ERR_BAD_KEYREF
if we set these elements then objects are successfully loaded into CDB.

IMO it means that since we have not set some of the list elements we are receiving this error.
Is there any way we can bypass this so that our element creation will be successful ??

Maapi Code Flow :

maapi_create(sock,th,/wavelengths/wl{first});
maapi_set_elem(sock,th,/wavelengths/wl/name,w1)

Sample yang Model :

container wavelengths {
        list wl {
          key name;
          leaf name {
           type string;
         }
         container connections {  **---> Dynamic Entries which should not be loaded into CDB**
          list connection { 
            key id;
            leaf id {
             type uint32;
            }
        }
    }
}

Hi,

I am not sure what the maapi_set_elem is supposed to do here?

Example using the confd_cmd and confd_load tools:

$ confd_cmd -d -d -c 'mcreate /wavelengths/wl{"first"}'
mcreate "/wavelengths/wl{"first"}"
TRACE Connected (maapi) to ConfD
TRACE MAAPI_START_USER_SESSION  --> CONFD_OK
TRACE MAAPI_START_TRANS  --> CONFD_OK
TRACE MAAPI_CREATE /wavelengths/wl{"first"} --> CONFD_OK
TRACE MAAPI_APPLY_TRANS  --> CONFD_OK
TRACE MAAPI_STOP_TRANS  --> CONFD_OK
TRACE MAAPI_END_USER_SESSION  --> CONFD_OK
$ confd_load -d -d -F p -p /wavelengths
TRACE Connected (maapi) to ConfD
starting user session ctxt=system user=system groups=[system]
TRACE MAAPI_START_USER_SESSION  --> CONFD_OK
TRACE MAAPI_START_TRANS  --> CONFD_OK
TRACE MAAPI_SAVE_CONFIG  --> CONFD_OK
TRACE Connected (stream) to ConfD
<config xmlns="http://tail-f.com/ns/config/1.0">
  <wavelengths xmlns="http://tail-f.com/ns/example/test">
    <wl>
      <name>first</name>
    </wl>
  </wavelengths>
TRACE MAAPI_SAVE_CONFIG_RESULT  --> CONFD_OK
</config>
TRACE MAAPI_END_USER_SESSION  --> CONFD_OK
$ confd_cmd -d -d -c 'mcreate /wavelengths/wl{"second"}'
mcreate "/wavelengths/wl{"second"}"
TRACE Connected (maapi) to ConfD
TRACE MAAPI_START_USER_SESSION  --> CONFD_OK
TRACE MAAPI_START_TRANS  --> CONFD_OK
TRACE MAAPI_CREATE /wavelengths/wl{"second"} --> CONFD_OK
TRACE MAAPI_APPLY_TRANS  --> CONFD_OK
TRACE MAAPI_STOP_TRANS  --> CONFD_OK
TRACE MAAPI_END_USER_SESSION  --> CONFD_OK
$ confd_load -d -d -F p -p /wavelengths
TRACE Connected (maapi) to ConfD
starting user session ctxt=system user=system groups=[system]
TRACE MAAPI_START_USER_SESSION  --> CONFD_OK
TRACE MAAPI_START_TRANS  --> CONFD_OK
TRACE MAAPI_SAVE_CONFIG  --> CONFD_OK
TRACE Connected (stream) to ConfD
<config xmlns="http://tail-f.com/ns/config/1.0">
  <wavelengths xmlns="http://tail-f.com/ns/example/test">
    <wl>
      <name>first</name>
    </wl>
    <wl>
      <name>second</name>
    </wl>
  </wavelengths>
TRACE MAAPI_SAVE_CONFIG_RESULT  --> CONFD_OK
</config>
TRACE MAAPI_END_USER_SESSION  --> CONFD_OK

When you create list entry using maapi_create(), you do not need to maapi_set_elem() for key values - this is done “automatically” on _create() call…

I’m not sure what you mean by “loading” in this case - for what command or operation specifically you get error.

yes. you are right. maapi_set_elem() is not needed for updating key element but here I am using maapi_set_elem() to set values for leafs inside a list.

In your sample yang model there are no elements inside your lists except the key that need to be set when the list entry is created?

Example:

$ confd_cmd -d -d -c 'mcreate /wavelengths/wl{"first"}; mcreate /wavelengths/wl{"first"}/connections/connection{32}'
mcreate "/wavelengths/wl{"first"}" ; mcreate "/wavelengths/wl{"first"}/connections/connection{32}"
TRACE Connected (maapi) to ConfD
TRACE MAAPI_START_USER_SESSION  --> CONFD_OK
TRACE MAAPI_START_TRANS  --> CONFD_OK
TRACE MAAPI_CREATE /wavelengths/wl{"first"} --> CONFD_OK
TRACE MAAPI_CREATE /wavelengths/wl{"first"}/connections/connection{32} --> CONFD_OK
TRACE MAAPI_APPLY_TRANS  --> CONFD_OK
TRACE MAAPI_STOP_TRANS  --> CONFD_OK
TRACE MAAPI_END_USER_SESSION  --> CONFD_OK

Previously I was using maapi_set_elem() for both key element & non-key elements for a list. I think my yang example is not reflecting that part.
I am not getting any error if i use maapi_set_elem() on key element of list.

My Main Query is that, is it possible to create a new entry (/wavelengths/wl{"first"}) in CDB without calling maapi_create() on /wavelengths/wl{"first"}/connections/connection{31} ?

I was getting CONFD_ERR_BAD_KEYREF error if i don’t create connections/connection{31} using maapi_create. so i want to know whether is it possible or not.

I am not using confd_load/confd_cmd to populate these entries in CDB. I am doing it through c program. does it make any difference ?

Thanks for the Help.

hi,

it is still not completely clear when specifically, for which confd-lib procedure call/operation you get that error…

your sample yang model allows creating instance of “wavelengths/wl{first}” list.
you don’t need to do any internal “wavelengths/wl{first}/connections/connection{31}” entry…

In addition to @josephm 's comment, confd_cmd / confd_load are c programs too that you can use as a reference. That’s why I use them to demo how to do things. See:
$CONFD_DIR/src/confd/tools/confd_cmd.c
$CONFD_DIR/src/confd/tools/confd_load.c

Call Flow is as follows –

maapi_start_trans()
maapi_create() - wl list entry
maapi_set_elem() - for key leaf -- redundant
maapi_set_elem() - for non key leaf
maapi_apply_trans() -- getting error `CONFD_ERR_BAD_KEYREF` 

but if follow the below call flow then it is working –

maapi_start_trans()
maapi_create() - wl list entry
maapi_set_elem() - for key leaf -- redundant
maapi_set_elem() - for non key leaf
maapi_create() - connections list entry
maapi_set_elem() - for key leaf -- redundant
maapi_apply_trans() -- No Error.

I will try by removing the maapi_set_elem() on key leaf element to see whether is it working or not.

Thanks for the Reply.

try verifying return code of each of the steps - maapi_create()/maapi_set_elem()/… one of them should give you CONFD_ERR giving closer hint on some inconsistent operation - e.g. keypath not prepared correctly etc.

if not, you may have some problem in config you are trying to “commit” due to e.g. YANG must statements failing, validation callpoints rejecting transaction, YANG “mandatory true;” statements being not fulfilled etc.