Skip to content

Commit

Permalink
Merge pull request FRRouting#21 from ranjanyash54/json_dump
Browse files Browse the repository at this point in the history
cmgd: Dump the cmgd data tree in a file in JSON or XML format
  • Loading branch information
pushpasis authored Aug 23, 2021
2 parents 4520e88 + 266c813 commit 9f7a857
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
58 changes: 58 additions & 0 deletions cmgd/cmgd_db.c
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,37 @@ int cmgd_db_delete_data_nodes(
return 0;
}

/* Dump the data tree of the specified format in the file pointed by the path */
static int cmgd_db_dump_in_memory(
cmgd_db_hndl_t db_hndl, char *base_xpath,
LYD_FORMAT format, struct ly_out *out)
{
cmgd_db_ctxt_t *db_ctxt;
struct lyd_node *root;
uint32_t options = 0;

db_ctxt = (cmgd_db_ctxt_t *)db_hndl;
if (!db_ctxt)
return -1;
if (base_xpath[0] == '\0')
root = db_ctxt->config_db ? db_ctxt->root.cfg_root->dnode
: db_ctxt->root.dnode_root;
else
root = yang_dnode_get(db_ctxt->config_db
? db_ctxt->root.cfg_root->dnode
: db_ctxt->root.dnode_root,
base_xpath);
if (!root)
return -1;

if (base_xpath[0] == '\0')
lyd_print_all(out, root, format, options);
else
lyd_print_tree(out, root, format, options);

return 0;
}

int cmgd_db_iter_data(
cmgd_db_hndl_t db_hndl, char *base_xpath,
cmgd_db_node_iter_fn iter_fn, void *ctxt, bool donot_free_alloced)
Expand Down Expand Up @@ -487,6 +518,33 @@ int cmgd_db_hndl_send_get_data_req(
return 0;
}

void cmgd_db_dump_tree(
struct vty *vty, cmgd_db_hndl_t db_hndl, const char* xpath,
LYD_FORMAT format)
{
cmgd_db_ctxt_t *db_ctxt;
struct ly_out *out;
char *str;
char base_xpath[CMGD_MAX_XPATH_LEN] = {0};

db_ctxt = (cmgd_db_ctxt_t *)db_hndl;
if (!db_ctxt) {
vty_out(vty, " >>>>> Database Not Initialized!\n");
return;
}

if (xpath) {
strncpy(base_xpath, xpath, CMGD_MAX_XPATH_LEN);
cmgd_remove_trailing_separator(base_xpath, '/');
}

ly_out_new_memory(&str, 0, &out);
cmgd_db_dump_in_memory(db_hndl, base_xpath, format, out);

vty_out(vty, "%s", str);
ly_out_free(out, NULL, 0);
}

void cmgd_db_status_write_one(
struct vty *vty, cmgd_db_hndl_t db_hndl)
{
Expand Down
3 changes: 3 additions & 0 deletions cmgd/cmgd_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ extern int cmgd_db_iter_data(
extern int cmgd_db_hndl_send_get_data_req(
cmgd_db_hndl_t db_hndl, cmgd_database_id_t db_id,
cmgd_yang_getdata_req_t *data_req, int num_reqs);
extern void cmgd_db_dump_tree(
struct vty *vty, cmgd_db_hndl_t db_hndl, const char* xpath,
LYD_FORMAT format);

extern void cmgd_db_status_write_one(
struct vty *vty, cmgd_db_hndl_t db_hndl);
Expand Down
43 changes: 43 additions & 0 deletions cmgd/cmgd_vty.c
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,48 @@ DEFPY(show_cmgd_get_data,
return CMD_SUCCESS;
}

DEFPY(show_cmgd_dump_data,
show_cmgd_dump_data_cmd,
"show cmgd database-contents db-name WORD$dbname [xpath WORD$path] format WORD$format_str",
SHOW_STR
CMGD_STR
"Get Database Contenents from a specific database\n"
"DB name\n"
"<candidate running operational\n"
"XPath expression specifying the YANG data path\n"
"XPath string\n"
"Format the output\n"
"JSON|XML")
{
cmgd_database_id_t database = CMGD_DB_CANDIDATE;
cmgd_db_hndl_t db_hndl;
LYD_FORMAT format = LYD_UNKNOWN;

database = cmgd_db_name2id(dbname);

if (database == CMGD_DB_NONE) {
vty_out(vty, "DB Name %s does not matches any existing database\n",
dbname);
return CMD_SUCCESS;
}

db_hndl = cmgd_db_get_hndl_by_id(cm, database);
if (!db_hndl) {
vty_out(vty, "ERROR: Couldnot access database!\n");
return CMD_ERR_NO_MATCH;
}

format = cmgd_str2format(format_str);
if (format == LYD_UNKNOWN) {
vty_out(vty, "String Format %s does not matches existing format\n",
format_str);
return CMD_SUCCESS;
}

cmgd_db_dump_tree(vty, db_hndl, path, format);
return CMD_SUCCESS;
}

DEFPY(show_cmgd_map_xpath,
show_cmgd_map_xpath_cmd,
"show cmgd yang-xpath-subscription WORD$path",
Expand Down Expand Up @@ -473,6 +515,7 @@ void cmgd_vty_init(void)
install_element(VIEW_NODE, &show_cmgd_db_oper_cmd);
install_element(VIEW_NODE, &show_cmgd_get_config_cmd);
install_element(VIEW_NODE, &show_cmgd_get_data_cmd);
install_element(VIEW_NODE, &show_cmgd_dump_data_cmd);
install_element(VIEW_NODE, &show_cmgd_map_xpath_cmd);

install_element(CONFIG_NODE, &cmgd_commit_apply_cmd);
Expand Down
9 changes: 9 additions & 0 deletions cmgd/cmgd_vty.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ extern int cmgd_hndl_bknd_cmd(const struct cmd_element *, struct vty *, int,
DEFUN_CMD_ELEMENT(cmgd_hndl_bknd_cmd, cmdname, cmdstr, helpstr, \
CMD_ATTR_YANG|CMD_ATTR_HIDDEN, 0)

static inline LYD_FORMAT cmgd_str2format(const char *format_str)
{
if (!strncmp("json", format_str, sizeof("json")))
return LYD_JSON;
else if (!strncmp("xml", format_str, sizeof("xml")))
return LYD_XML;
return LYD_UNKNOWN;
}

#ifdef INCLUDE_CMGD_CMDDEFS_ONLY

#define NB_ENQEUE_CLI_COMMAND(vty, xpath, op, val) \
Expand Down

0 comments on commit 9f7a857

Please sign in to comment.