Skip to content

Commit

Permalink
Merge pull request FRRouting#26 from ranjanyash54/load_config
Browse files Browse the repository at this point in the history
cmgd: load config command
  • Loading branch information
pushpasis authored Aug 25, 2021
2 parents a91cc2e + 5453fcf commit 56975f8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
29 changes: 29 additions & 0 deletions cmgd/cmgd_db.c
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,35 @@ int cmgd_db_delete_data_nodes(
return 0;
}

int cmgd_db_load_config_from_file(const char * file_path, bool merge)
{
struct lyd_node *iter;
LY_ERR ret;
cmgd_db_hndl_t candidate_db;
cmgd_db_ctxt_t parsed;

ret = lyd_parse_data_path(ly_native_ctx, file_path, LYD_JSON, LYD_PARSE_STRICT, 0, &iter);

if (ret != LY_SUCCESS) {
yang_dnode_free(iter);
return ret;
}

parsed.root.cfg_root = nb_config_new(iter);
parsed.config_db = true;

candidate_db = cmgd_db_get_hndl_by_id(cm, CMGD_DB_CANDIDATE);

if (merge)
cmgd_db_merge_dbs((cmgd_db_hndl_t)&parsed, candidate_db);
else
cmgd_db_copy_dbs((cmgd_db_hndl_t)&parsed, candidate_db);

nb_config_free(parsed.root.cfg_root);

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
2 changes: 2 additions & 0 deletions cmgd/cmgd_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ extern 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);

extern int cmgd_db_load_config_from_file(const char * file_path, bool merge);

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);
Expand Down
30 changes: 30 additions & 0 deletions cmgd/cmgd_vty.c
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,35 @@ DEFPY(show_cmgd_map_xpath,
return CMD_SUCCESS;
}

DEFPY(cmgd_load_config,
cmgd_load_config_cmd,
"cmgd load-config filepath WORD$path <merge|replace>",
CMGD_STR
"Load config from file\n"
"Path to the file\n"
"Path\n"
"Merge with candidate\n"
"Replace candidate")
{
bool merge = false;
int idx_merge = 4;
int ret;

if (strncmp(argv[idx_merge]->arg, "merge", sizeof("merge")) == 0)
merge = true;
else if (strncmp(argv[idx_merge]->arg, "replace", sizeof("replace")) == 0)
merge = false;
else {
vty_out(vty, "Chosen option: %s not valid\n", argv[idx_merge]->arg);
return CMD_SUCCESS;
}

ret = cmgd_db_load_config_from_file(path, merge);
if (ret != 0)
vty_out(vty, "Error with parsing the file with error code %d\n", ret);
return CMD_SUCCESS;
}

DEFPY(cmgd_lock_db_candidate,
cmgd_lock_db_cand_cmd,
"cmgd lock-database candidate",
Expand Down Expand Up @@ -540,6 +569,7 @@ void cmgd_vty_init(void)
install_element(CONFIG_NODE, &cmgd_unlock_db_cand_cmd);
install_element(CONFIG_NODE, &cmgd_set_config_data_cmd);
install_element(CONFIG_NODE, &cmgd_delete_config_data_cmd);
install_element(CONFIG_NODE, &cmgd_load_config_cmd);

/*
* TODO: Register and handlers for auto-completion here.
Expand Down

0 comments on commit 56975f8

Please sign in to comment.