ConfD User Community

Can I load data from a file into CDB from my application?

Your program can load data from files at any time using the Management Agent API (MAAPI)

Quick example:

Our YANG model:

module my-device {
  namespace "http://tail-f.com/ns/example/blocking/1.0";
  prefix my-device;

  container my-device {
    container blocking {
      leaf-list network-element-command {
        type string;
      }  
    }
  }
}

With ConfD started using the YANG module above we can set a new configuration, store the configuration to an XML file, and delete the configuration using the confd_cmd and confd_load tools that come with the ConfD releases:

$ confd_load -d -d -F p -p /my-device
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">
TRACE MAAPI_SAVE_CONFIG_RESULT  --> CONFD_OK
</config>
TRACE MAAPI_END_USER_SESSION  --> CONFD_OK

$ confd_cmd -d -d -c "mset /my-device/blocking/network-element-command \"test1 test2 test3 test4\”"
mset "/my-device/blocking/network-element-command" "test1 test2 test3 test4\”"
TRACE Connected (maapi) to ConfD
TRACE MAAPI_START_USER_SESSION  --> CONFD_OK
TRACE MAAPI_START_TRANS  --> CONFD_OK
TRACE MAAPI_SET_ELEM2 /my-device/blocking/network-element-command --> 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 /my-device
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">
  <my-device xmlns="http://tail-f.com/ns/example/blocking/1.0">
    <blocking>
      <network-element-command>test1</network-element-command>
      <network-element-command>test2</network-element-command>
      <network-element-command>test3</network-element-command>
      <network-element-command>test4”</network-element-command>
    </blocking>
  </my-device>
TRACE MAAPI_SAVE_CONFIG_RESULT  --> CONFD_OK
</config>

$ confd_load -F p -p /my-device > test.xml
$ confd_cmd -d -d -c "mdel /my-device/blocking/network-element-command"
mdel "/my-device/blocking/network-element-command"
TRACE Connected (maapi) to ConfD
TRACE MAAPI_START_USER_SESSION  --> CONFD_OK
TRACE MAAPI_START_TRANS  --> CONFD_OK
TRACE MAAPI_DELETE /my-device/blocking/network-element-command --> CONFD_OK
TRACE MAAPI_APPLY_TRANS  --> CONFD_OK
TRACE MAAPI_STOP_TRANS  --> CONFD_OK
TRACE MAAPI_END_USER_SESSION  --> CONFD_OK

$ confd_load -F p -p /my-device
<config xmlns="http://tail-f.com/ns/config/1.0">
</config>

We can now load the configuration from the XML file by for example using the confd_load tool:

$ cat test.xml
<config xmlns="http://tail-f.com/ns/config/1.0">
  <my-device xmlns="http://tail-f.com/ns/example/blocking/1.0">
    <blocking>
      <network-element-command>test1</network-element-command>
      <network-element-command>test2</network-element-command>
      <network-element-command>test3</network-element-command>
      <network-element-command>test4”</network-element-command>
    </blocking>
  </my-device>
</config>

$ confd_load -d -d -m -l test.xml
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_LOAD_CONFIG_FILE  --> CONFD_OK
TRACE MAAPI_APPLY_TRANS  --> CONFD_OK
TRACE MAAPI_END_USER_SESSION  --> CONFD_OK

Or from a program using MAAPI. Below is a ConfD C-API example program initiating a transaction towards the running data store (should be candidate if you have the candidate enabled) setting leafs in the leaf-list as implemented by the YANG module above:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/poll.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#include "confd_lib.h"
#include "confd_maapi.h"

#include “my-test.h"

static int runtest()
{
    int msock, th;
    struct confd_ip ip;
    const char *groups[] = { "admin" };
    char *confd_addr = "127.0.0.1";
    int confd_port = CONFD_PORT;
    struct sockaddr_in addr;
    char *context = "maapi";

    addr.sin_addr.s_addr = inet_addr(confd_addr);
    addr.sin_family = AF_INET;
    addr.sin_port = htons(confd_port);

    if ((msock = socket(PF_INET, SOCK_STREAM, 0)) < 0 ) {
        confd_fatal("failed to create socket\n");
    }

    if (maapi_connect(msock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
        confd_fatal("failed to connect to confd\n");
    }

    ip.af = AF_INET;
    inet_pton(AF_INET, "127.0.0.1", &ip.ip.v4);

    if ((maapi_start_user_session(msock, "admin", context,
                                  groups, sizeof(groups) / sizeof(*groups),
                                  &ip, CONFD_PROTO_TCP) != CONFD_OK)) {
        confd_fatal("failed to start user session");
    }


    if ((th = maapi_start_trans(msock, CONFD_RUNNING, CONFD_READ_WRITE)) < 0) {
      confd_fatal("failed to start trans\n");
    }

    maapi_set_namespace(msock, th, routes__ns);

    maapi_load_config(msock, th, (MAAPI_CONFIG_MERGE|MAAPI_CONFIG_XML), “test.xml");

// if we wanted to set the leaf-list values from our program we would do
// something like this:
#if 0
    if (maapi_set_elem2(msock, th, "test1 test2 test3 test4", “/my-device/blocking/network-element-command") != CONFD_OK) {
      confd_fatal("set_elem2 failed\n");
    }
#endif

    if (maapi_apply_trans(msock, th, 0) != CONFD_OK) {
      confd_fatal("apply_trans failed\n");
    }

    if (maapi_finish_trans(msock, th) != CONFD_OK) {
      confd_fatal("finish_trans failed\n");
    } 

    maapi_end_user_session(msock);
    close(msock);
    return CONFD_OK;
}

int main(int argc, char **argv)
{
    confd_init("maapiapp", stderr, CONFD_SILENT);
    if(runtest() != CONFD_OK)
      exit(1);
    exit(0);
}

Our configuration in test.xml is now loaded into ConfD CDB:

$ confd_load -d -d -F p -p /my-device
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">
  <my-device xmlns="http://tail-f.com/ns/example/blocking/1.0">
    <blocking>
      <network-element-command>test1</network-element-command>
      <network-element-command>test2</network-element-command>
      <network-element-command>test3</network-element-command>
      <network-element-command>test4”</network-element-command>
    </blocking>
  </my-device>
TRACE MAAPI_SAVE_CONFIG_RESULT  --> CONFD_OK
</config>
TRACE MAAPI_END_USER_SESSION  --> CONFD_OK

if (maapi_set_elem2(msock, th, “test1 test2 test3 test4”, “/my-device/blocking/network-element-command") != CONFD_OK) {
confd_fatal(“set_elem2 failed\n”);
}

can you please give brief of what happens when this method is executed. does this method stores data into cdb. Because the data in the rpc reply is not showing the data I am setting.