123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296 |
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <assert.h>
- #include <syslog.h>
- #include <faux/faux.h>
- #include <faux/str.h>
- #include <faux/argv.h>
- #include <faux/list.h>
- #include <faux/error.h>
- #include <faux/file.h>
- #include <klish/khelper.h>
- #include <klish/kplugin.h>
- #include <klish/kentry.h>
- #include <klish/kscheme.h>
- #include <klish/kcontext.h>
- #include <klish/kpargv.h>
- #include <sysrepo.h>
- #include <sysrepo/xpath.h>
- #include <sysrepo/values.h>
- #include <sysrepo/netconf_acm.h>
- #include "klish_plugin_sysrepo.h"
- #define ERRORMSG "Error: "
- #define ARG_PATH "path"
- #define ARG_FROM_PATH "from_path"
- #define ARG_TO_PATH "to_path"
- // Print sysrepo session errors
- static void srp_print_errors(sr_session_ctx_t *session)
- {
- const sr_error_info_t *err_info = NULL;
- int rc = 0;
- unsigned int i = 0;
- if (!session)
- return;
- rc = sr_session_get_error(session, &err_info);
- if ((rc != SR_ERR_OK) || !err_info)
- return;
- // Show the first error only. Because probably next errors are
- // originated from internal sysrepo code but is not from subscribers.
- // for (i = 0; i < err_info->err_count; i++)
- for (i = 0; i < (err_info->err_count < 1 ? err_info->err_count : 1); i++)
- fprintf(stderr, ERRORMSG "%s\n", err_info->err[i].message);
- }
- // Print sysrepo session errors and then specified error
- static void srp_error(sr_session_ctx_t *session, const char *fmt, ...)
- {
- srp_print_errors(session);
- if (fmt) {
- va_list argptr;
- va_start(argptr, fmt);
- vfprintf(stderr, fmt, argptr);
- va_end(argptr);
- }
- }
- static faux_argv_t *param2argv(const faux_argv_t *cur_path,
- const kpargv_t *pargv, const char *entry_name)
- {
- faux_list_node_t *iter = NULL;
- faux_list_t *pargs = NULL;
- faux_argv_t *args = NULL;
- kparg_t *parg = NULL;
- assert(pargv);
- if (!pargv)
- return NULL;
- pargs = kpargv_find_multi(pargv, entry_name);
- if (cur_path)
- args = faux_argv_dup(cur_path);
- else
- args = faux_argv_new();
- iter = faux_list_head(pargs);
- while ((parg = (kparg_t *)faux_list_each(&iter))) {
- faux_argv_add(args, kparg_value(parg));
- }
- faux_list_free(pargs);
- return args;
- }
- // Candidate from pargv contains possible begin of current word (that must be
- // completed). kpargv's list don't contain candidate but only already parsed
- // words.
- static int srp_compl_or_help(kcontext_t *context, bool_t help,
- pt_e enabled_ptypes, bool_t use_cur_path, bool_t existing_nodes_only)
- {
- faux_argv_t *args = NULL;
- pline_t *pline = NULL;
- sr_session_ctx_t *sess = NULL;
- const char *entry_name = NULL;
- faux_argv_t *cur_path = NULL;
- assert(context);
- sess = srp_udata_sr_sess(context);
- if (use_cur_path)
- cur_path = (faux_argv_t *)srp_udata_path(context);
- entry_name = kentry_name(kcontext_candidate_entry(context));
- args = param2argv(cur_path, kcontext_parent_pargv(context), entry_name);
- pline = pline_parse(sess, args, srp_udata_opts(context));
- faux_argv_free(args);
- pline_print_completions(pline, help, enabled_ptypes, existing_nodes_only);
- pline_free(pline);
- return 0;
- }
- int srp_compl(kcontext_t *context)
- {
- return srp_compl_or_help(context, BOOL_FALSE, PT_COMPL_ALL, BOOL_TRUE, BOOL_FALSE);
- }
- int srp_help(kcontext_t *context)
- {
- return srp_compl_or_help(context, BOOL_TRUE, PT_COMPL_ALL, BOOL_TRUE, BOOL_FALSE);
- }
- int srp_compl_set(kcontext_t *context)
- {
- return srp_compl_or_help(context, BOOL_FALSE, PT_COMPL_SET, BOOL_TRUE, BOOL_FALSE);
- }
- int srp_help_set(kcontext_t *context)
- {
- return srp_compl_or_help(context, BOOL_TRUE, PT_COMPL_SET, BOOL_TRUE, BOOL_FALSE);
- }
- int srp_compl_del(kcontext_t *context)
- {
- return srp_compl_or_help(context, BOOL_FALSE, PT_COMPL_DEL, BOOL_TRUE, BOOL_TRUE);
- }
- int srp_help_del(kcontext_t *context)
- {
- return srp_compl_or_help(context, BOOL_TRUE, PT_COMPL_DEL, BOOL_TRUE, BOOL_TRUE);
- }
- int srp_compl_edit(kcontext_t *context)
- {
- return srp_compl_or_help(context, BOOL_FALSE, PT_COMPL_EDIT, BOOL_TRUE, BOOL_FALSE);
- }
- int srp_compl_edit_abs(kcontext_t *context)
- {
- return srp_compl_or_help(context, BOOL_FALSE, PT_COMPL_EDIT, BOOL_FALSE, BOOL_FALSE);
- }
- int srp_help_edit(kcontext_t *context)
- {
- return srp_compl_or_help(context, BOOL_TRUE, PT_COMPL_EDIT, BOOL_TRUE, BOOL_FALSE);
- }
- int srp_help_edit_abs(kcontext_t *context)
- {
- return srp_compl_or_help(context, BOOL_TRUE, PT_COMPL_EDIT, BOOL_FALSE, BOOL_FALSE);
- }
- int srp_compl_insert(kcontext_t *context)
- {
- return srp_compl_or_help(context, BOOL_FALSE, PT_COMPL_INSERT, BOOL_TRUE, BOOL_TRUE);
- }
- int srp_help_insert(kcontext_t *context)
- {
- return srp_compl_or_help(context, BOOL_TRUE, PT_COMPL_INSERT, BOOL_TRUE, BOOL_TRUE);
- }
- int srp_prompt_edit_path(kcontext_t *context)
- {
- faux_argv_t *cur_path = NULL;
- char *path = NULL;
- assert(context);
- cur_path = (faux_argv_t *)srp_udata_path(context);
- if (cur_path)
- path = faux_argv_line(cur_path);
- kcontext_printf(context, "[edit%s%s]\n",
- path ? " " : "", path ? path : "");
- faux_str_free(path);
- return 0;
- }
- static int srp_check_type(kcontext_t *context,
- pt_e not_accepted_nodes, size_t max_expr_num, bool_t use_cur_path)
- {
- int ret = -1;
- faux_argv_t *args = NULL;
- pline_t *pline = NULL;
- sr_session_ctx_t *sess = NULL;
- const char *entry_name = NULL;
- const char *value = NULL;
- pexpr_t *expr = NULL;
- size_t expr_num = 0;
- faux_argv_t *cur_path = NULL;
- assert(context);
- sess = srp_udata_sr_sess(context);
- if (use_cur_path)
- cur_path = (faux_argv_t *)srp_udata_path(context);
- entry_name = kentry_name(kcontext_candidate_entry(context));
- value = kcontext_candidate_value(context);
- args = param2argv(cur_path, kcontext_parent_pargv(context), entry_name);
- if (value)
- faux_argv_add(args, value);
- pline = pline_parse(sess, args, srp_udata_opts(context));
- faux_argv_free(args);
- if (pline->invalid)
- goto err;
- expr_num = faux_list_len(pline->exprs);
- if (expr_num < 1)
- goto err;
- if ((max_expr_num > 0) && // '0' means unlimited
- (expr_num > max_expr_num))
- goto err;
- expr = pline_current_expr(pline);
- if (expr->pat & not_accepted_nodes)
- goto err;
- ret = 0;
- err:
- pline_free(pline);
- return ret;
- }
- int srp_PLINE_SET(kcontext_t *context)
- {
- return srp_check_type(context, PT_NOT_SET, 0, BOOL_TRUE);
- }
- int srp_PLINE_DEL(kcontext_t *context)
- {
- return srp_check_type(context, PT_NOT_DEL, 1, BOOL_TRUE);
- }
- int srp_PLINE_EDIT(kcontext_t *context)
- {
- return srp_check_type(context, PT_NOT_EDIT, 1, BOOL_TRUE);
- }
- int srp_PLINE_EDIT_ABS(kcontext_t *context)
- {
- return srp_check_type(context, PT_NOT_EDIT, 1, BOOL_FALSE);
- }
- int srp_PLINE_INSERT_FROM(kcontext_t *context)
- {
- return srp_check_type(context, PT_NOT_INSERT, 1, BOOL_TRUE);
- }
- static faux_argv_t *assemble_insert_to(sr_session_ctx_t *sess, const kpargv_t *pargv,
- faux_argv_t *cur_path, const char *candidate_value, pline_opts_t *opts)
- {
- faux_argv_t *args = NULL;
- faux_argv_t *insert_to = NULL;
- pline_t *pline = NULL;
- pexpr_t *expr = NULL;
- size_t i = 0;
- assert(sess);
- args = param2argv(cur_path, pargv, ARG_FROM_PATH);
- pline = pline_parse(sess, args, opts);
- expr = pline_current_expr(pline);
- for (i = 0; i < (expr->args_num - expr->list_pos); i++) {
- faux_argv_node_t *iter = faux_argv_iterr(args);
- faux_argv_del(args, iter);
- }
- insert_to = param2argv(args, pargv, ARG_TO_PATH);
- faux_argv_free(args);
- if (candidate_value)
- faux_argv_add(insert_to, candidate_value);
- pline_free(pline);
- return insert_to;
- }
- int srp_PLINE_INSERT_TO(kcontext_t *context)
- {
- int ret = -1;
- faux_argv_t *args = NULL;
- pline_t *pline = NULL;
- sr_session_ctx_t *sess = NULL;
- const char *value = NULL;
- pexpr_t *expr = NULL;
- size_t expr_num = 0;
- faux_argv_t *cur_path = NULL;
- assert(context);
- sess = srp_udata_sr_sess(context);
- cur_path = (faux_argv_t *)srp_udata_path(context);
- value = kcontext_candidate_value(context);
- args = assemble_insert_to(sess, kcontext_parent_pargv(context),
- cur_path, value, srp_udata_opts(context));
- pline = pline_parse(sess, args, srp_udata_opts(context));
- faux_argv_free(args);
- if (pline->invalid)
- goto err;
- expr_num = faux_list_len(pline->exprs);
- if (expr_num != 1)
- goto err;
- expr = pline_current_expr(pline);
- if (expr->pat & PT_NOT_INSERT)
- goto err;
- ret = 0;
- err:
- pline_free(pline);
- return ret;
- }
- static int srp_compl_or_help_insert_to(kcontext_t *context, bool_t help)
- {
- faux_argv_t *args = NULL;
- pline_t *pline = NULL;
- sr_session_ctx_t *sess = NULL;
- faux_argv_t *cur_path = NULL;
- assert(context);
- sess = srp_udata_sr_sess(context);
- cur_path = (faux_argv_t *)srp_udata_path(context);
- args = assemble_insert_to(sess, kcontext_parent_pargv(context),
- cur_path, NULL, srp_udata_opts(context));
- pline = pline_parse(sess, args, srp_udata_opts(context));
- faux_argv_free(args);
- pline_print_completions(pline, help, PT_COMPL_INSERT, BOOL_TRUE);
- pline_free(pline);
- return 0;
- }
- int srp_compl_insert_to(kcontext_t *context)
- {
- return srp_compl_or_help_insert_to(context, BOOL_FALSE);
- }
- int srp_help_insert_to(kcontext_t *context)
- {
- return srp_compl_or_help_insert_to(context, BOOL_TRUE);
- }
- int srp_set(kcontext_t *context)
- {
- int ret = 0;
- faux_argv_t *args = NULL;
- pline_t *pline = NULL;
- sr_session_ctx_t *sess = NULL;
- faux_list_node_t *iter = NULL;
- pexpr_t *expr = NULL;
- size_t err_num = 0;
- faux_argv_t *cur_path = NULL;
- assert(context);
- sess = srp_udata_sr_sess(context);
- cur_path = (faux_argv_t *)srp_udata_path(context);
- args = param2argv(cur_path, kcontext_pargv(context), ARG_PATH);
- pline = pline_parse(sess, args, srp_udata_opts(context));
- faux_argv_free(args);
- if (pline->invalid) {
- fprintf(stderr, ERRORMSG "Invalid set request\n");
- ret = -1;
- goto cleanup;
- }
- iter = faux_list_head(pline->exprs);
- while ((expr = (pexpr_t *)faux_list_each(&iter))) {
- if (!(expr->pat & PT_SET)) {
- err_num++;
- fprintf(stderr, ERRORMSG "Illegal expression for set operation\n");
- break;
- }
- if (sr_set_item_str(sess, expr->xpath, expr->value, NULL, 0) !=
- SR_ERR_OK) {
- err_num++;
- srp_error(sess, ERRORMSG "Can't set data\n");
- break;
- }
- }
- if (err_num > 0)
- ret = -1;
- if (!sr_has_changes(sess))
- goto cleanup;
- if (err_num > 0) {
- sr_discard_changes(sess);
- goto cleanup;
- }
- if (sr_apply_changes(sess, 0) != SR_ERR_OK) {
- sr_discard_changes(sess);
- srp_error(sess, ERRORMSG "Can't apply changes\n");
- goto cleanup;
- }
- cleanup:
- pline_free(pline);
- return ret;
- }
- int srp_del(kcontext_t *context)
- {
- int ret = -1;
- faux_argv_t *args = NULL;
- pline_t *pline = NULL;
- sr_session_ctx_t *sess = NULL;
- pexpr_t *expr = NULL;
- faux_argv_t *cur_path = NULL;
- assert(context);
- sess = srp_udata_sr_sess(context);
- cur_path = (faux_argv_t *)srp_udata_path(context);
- args = param2argv(cur_path, kcontext_pargv(context), ARG_PATH);
- pline = pline_parse(sess, args, srp_udata_opts(context));
- faux_argv_free(args);
- if (pline->invalid) {
- fprintf(stderr, ERRORMSG "Invalid 'del' request\n");
- goto err;
- }
- if (faux_list_len(pline->exprs) > 1) {
- fprintf(stderr, ERRORMSG "Can't delete more than one object\n");
- goto err;
- }
- expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
- if (!(expr->pat & PT_DEL)) {
- fprintf(stderr, ERRORMSG "Illegal expression for 'del' operation\n");
- goto err;
- }
- if (sr_delete_item(sess, expr->xpath, 0) != SR_ERR_OK) {
- srp_error(sess, ERRORMSG "Can't delete data\n");
- goto err;
- }
- if (sr_apply_changes(sess, 0) != SR_ERR_OK) {
- sr_discard_changes(sess);
- srp_error(sess, ERRORMSG "Can't apply changes\n");
- goto err;
- }
- ret = 0;
- err:
- pline_free(pline);
- return ret;
- }
- int srp_edit(kcontext_t *context)
- {
- int ret = -1;
- faux_argv_t *args = NULL;
- pline_t *pline = NULL;
- sr_session_ctx_t *sess = NULL;
- pexpr_t *expr = NULL;
- faux_argv_t *cur_path = NULL;
- assert(context);
- sess = srp_udata_sr_sess(context);
- cur_path = (faux_argv_t *)srp_udata_path(context);
- args = param2argv(cur_path, kcontext_pargv(context), ARG_PATH);
- pline = pline_parse(sess, args, srp_udata_opts(context));
- if (pline->invalid) {
- fprintf(stderr, ERRORMSG "Invalid 'edit' request\n");
- goto err;
- }
- if (faux_list_len(pline->exprs) > 1) {
- fprintf(stderr, ERRORMSG "Can't process more than one object\n");
- goto err;
- }
- expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
- if (!(expr->pat & PT_EDIT)) {
- fprintf(stderr, ERRORMSG "Illegal expression for 'edit' operation\n");
- goto err;
- }
- if (sr_set_item_str(sess, expr->xpath, NULL, NULL, 0) != SR_ERR_OK) {
- srp_error(sess, ERRORMSG "Can't set editing data\n");
- goto err;
- }
- if (sr_apply_changes(sess, 0) != SR_ERR_OK) {
- sr_discard_changes(sess);
- srp_error(sess, ERRORMSG "Can't apply changes\n");
- goto err;
- }
- // Set new current path
- srp_udata_set_path(context, args);
- ret = 0;
- err:
- if (ret < 0)
- faux_argv_free(args);
- pline_free(pline);
- return ret;
- }
- int srp_top(kcontext_t *context)
- {
- assert(context);
- srp_udata_set_path(context, NULL);
- return 0;
- }
- int srp_up(kcontext_t *context)
- {
- sr_session_ctx_t *sess = NULL;
- faux_argv_t *cur_path = NULL;
- faux_argv_node_t *iter = NULL;
- assert(context);
- sess = srp_udata_sr_sess(context);
- cur_path = (faux_argv_t *)srp_udata_path(context);
- if (!cur_path)
- return -1; // It's top level and can't level up
- // Remove last arguments one by one and wait for legal edit-like pline
- while (faux_argv_len(cur_path) > 0) {
- pline_t *pline = NULL;
- pexpr_t *expr = NULL;
- size_t len = 0;
- iter = faux_argv_iterr(cur_path);
- faux_argv_del(cur_path, iter);
- pline = pline_parse(sess, cur_path, srp_udata_opts(context));
- if (pline->invalid) {
- pline_free(pline);
- continue;
- }
- len = faux_list_len(pline->exprs);
- if (len != 1) {
- pline_free(pline);
- continue;
- }
- expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
- if (!(expr->pat & PT_EDIT)) {
- pline_free(pline);
- continue;
- }
- // Here new path is ok
- pline_free(pline);
- break;
- }
- // Don't store empty path
- if (faux_argv_len(cur_path) == 0)
- srp_udata_set_path(context, NULL);
- return 0;
- }
- int srp_insert(kcontext_t *context)
- {
- int ret = -1;
- pline_t *pline = NULL;
- pline_t *pline_to = NULL;
- sr_session_ctx_t *sess = NULL;
- pexpr_t *expr = NULL;
- pexpr_t *expr_to = NULL;
- faux_argv_t *cur_path = NULL;
- faux_argv_t *insert_from = NULL;
- faux_argv_t *insert_to = NULL;
- sr_move_position_t position = SR_MOVE_LAST;
- kpargv_t *pargv = NULL;
- const char *list_keys = NULL;
- const char *leaflist_value = NULL;
- assert(context);
- sess = srp_udata_sr_sess(context);
- cur_path = (faux_argv_t *)srp_udata_path(context);
- pargv = kcontext_pargv(context);
- // 'from' argument
- insert_from = param2argv(cur_path, pargv, ARG_FROM_PATH);
- pline = pline_parse(sess, insert_from, srp_udata_opts(context));
- faux_argv_free(insert_from);
- if (pline->invalid) {
- fprintf(stderr, ERRORMSG "Invalid 'from' expression\n");
- goto err;
- }
- if (faux_list_len(pline->exprs) > 1) {
- fprintf(stderr, ERRORMSG "Can't process more than one object\n");
- goto err;
- }
- expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
- if (!(expr->pat & PT_INSERT)) {
- fprintf(stderr, ERRORMSG "Illegal 'from' expression for 'insert' operation\n");
- goto err;
- }
- // Position
- if (kpargv_find(pargv, "first"))
- position = SR_MOVE_FIRST;
- else if (kpargv_find(pargv, "last"))
- position = SR_MOVE_LAST;
- else if (kpargv_find(pargv, "before"))
- position = SR_MOVE_BEFORE;
- else if (kpargv_find(pargv, "after"))
- position = SR_MOVE_AFTER;
- else {
- fprintf(stderr, ERRORMSG "Illegal 'position' argument\n");
- goto err;
- }
- // 'to' argument
- if ((SR_MOVE_BEFORE == position) || (SR_MOVE_AFTER == position)) {
- insert_to = assemble_insert_to(sess, pargv, cur_path,
- NULL, srp_udata_opts(context));
- pline_to = pline_parse(sess, insert_to, srp_udata_opts(context));
- faux_argv_free(insert_to);
- if (pline_to->invalid) {
- fprintf(stderr, ERRORMSG "Invalid 'to' expression\n");
- goto err;
- }
- if (faux_list_len(pline_to->exprs) > 1) {
- fprintf(stderr, ERRORMSG "Can't process more than one object\n");
- goto err;
- }
- expr_to = (pexpr_t *)faux_list_data(faux_list_head(pline_to->exprs));
- if (!(expr_to->pat & PT_INSERT)) {
- fprintf(stderr, ERRORMSG "Illegal 'to' expression for 'insert' operation\n");
- goto err;
- }
- if (PAT_LIST_KEY == expr_to->pat)
- list_keys = expr_to->last_keys;
- else // PATH_LEAFLIST_VALUE
- leaflist_value = expr_to->last_keys;
- }
- if (sr_move_item(sess, expr->xpath, position,
- list_keys, leaflist_value, NULL, 0) != SR_ERR_OK) {
- srp_error(sess, ERRORMSG "Can't move element\n");
- goto err;
- }
- if (sr_apply_changes(sess, 0) != SR_ERR_OK) {
- sr_discard_changes(sess);
- srp_error(sess, ERRORMSG "Can't apply changes\n");
- goto err;
- }
- ret = 0;
- err:
- pline_free(pline);
- pline_free(pline_to);
- return ret;
- }
- int srp_verify(kcontext_t *context)
- {
- int ret = -1;
- sr_session_ctx_t *sess = NULL;
- assert(context);
- sess = srp_udata_sr_sess(context);
- // Validate candidate config
- if (sr_validate(sess, NULL, 0) != SR_ERR_OK) {
- srp_error(sess, ERRORMSG "Invalid candidate configuration\n");
- goto err;
- }
- ret = 0;
- err:
- return ret;
- }
- int srp_commit(kcontext_t *context)
- {
- int ret = -1;
- sr_session_ctx_t *sess = NULL;
- assert(context);
- sess = srp_udata_sr_sess(context);
- // Validate candidate config. The copy operation is not enough to fully
- // verify candidate config. It verifies only the part of it. So verify
- // before commit
- if (sr_validate(sess, NULL, 0) != SR_ERR_OK) {
- srp_error(sess, ERRORMSG "Invalid candidate configuration\n");
- goto err;
- }
- // Copy candidate to running-config
- if (sr_session_switch_ds(sess, SR_DS_RUNNING)) {
- srp_error(sess, ERRORMSG "Can't connect to running-config data store\n");
- goto err;
- }
- if (sr_copy_config(sess, NULL, SRP_REPO_EDIT, 0) != SR_ERR_OK) {
- srp_error(sess, ERRORMSG "Can't commit to running-config\n");
- goto err;
- }
- // Copy running-config to startup-config
- if (sr_session_switch_ds(sess, SR_DS_STARTUP)) {
- srp_error(sess, ERRORMSG "Can't connect to startup-config data store\n");
- goto err;
- }
- if (sr_copy_config(sess, NULL, SR_DS_RUNNING, 0) != SR_ERR_OK) {
- srp_error(sess, ERRORMSG "Can't store data to startup-config\n");
- goto err;
- }
- ret = 0;
- err:
- sr_session_switch_ds(sess, SRP_REPO_EDIT);
- return ret;
- }
- int srp_reset(kcontext_t *context)
- {
- int ret = -1;
- sr_session_ctx_t *sess = NULL;
- assert(context);
- sess = srp_udata_sr_sess(context);
- // Copy running-config to candidate config
- if (sr_copy_config(sess, NULL, SR_DS_RUNNING, 0) != SR_ERR_OK) {
- srp_error(sess, ERRORMSG "Can't reset to running-config\n");
- goto err;
- }
- ret = 0;
- err:
- return ret;
- }
- int srp_show_xml(kcontext_t *context)
- {
- int ret = -1;
- faux_argv_t *args = NULL;
- pline_t *pline = NULL;
- sr_session_ctx_t *sess = NULL;
- pexpr_t *expr = NULL;
- faux_argv_t *cur_path = NULL;
- sr_data_t *data = NULL;
- struct ly_out *out = NULL;
- assert(context);
- sess = srp_udata_sr_sess(context);
- cur_path = (faux_argv_t *)srp_udata_path(context);
- args = param2argv(cur_path, kcontext_pargv(context), ARG_PATH);
- pline = pline_parse(sess, args, srp_udata_opts(context));
- faux_argv_free(args);
- if (pline->invalid) {
- fprintf(stderr, ERRORMSG "Invalid 'show' request\n");
- goto err;
- }
- if (faux_list_len(pline->exprs) > 1) {
- fprintf(stderr, ERRORMSG "Can't process more than one object\n");
- goto err;
- }
- expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
- if (!(expr->pat & PT_EDIT)) {
- fprintf(stderr, ERRORMSG "Illegal expression for 'show' operation\n");
- goto err;
- }
- if (sr_get_subtree(sess, expr->xpath, 0, &data) != SR_ERR_OK) {
- srp_error(sess, ERRORMSG "Can't get specified subtree\n");
- goto err;
- }
- if (!data) // Not found
- goto err;
- ly_out_new_file(stdout, &out);
- lyd_print_tree(out, data->tree, LYD_XML, 0);
- ly_out_free(out, NULL, 0);
- // child = lyd_child(data->tree);
- // if (child) {
- // ly_out_new_file(stdout, &out);
- // lyd_print_all(out, child, LYD_XML, 0);
- // }
- struct lyd_meta *meta = lyd_find_meta(data->tree->meta, NULL, "junos-configuration-metadata:active");
- if (meta)
- printf("META\n");
- sr_release_data(data);
- ret = 0;
- err:
- pline_free(pline);
- return ret;
- }
- static int show(kcontext_t *context, sr_datastore_t ds,
- const char *path_var, bool_t use_cur_path)
- {
- int ret = -1;
- faux_argv_t *args = NULL;
- pline_t *pline = NULL;
- sr_session_ctx_t *sess = NULL;
- pexpr_t *expr = NULL;
- faux_argv_t *cur_path = NULL;
- char *xpath = NULL;
- assert(context);
- sess = srp_udata_sr_sess(context);
- if (ds != SRP_REPO_EDIT)
- sr_session_switch_ds(sess, ds);
- if (use_cur_path)
- cur_path = (faux_argv_t *)srp_udata_path(context);
- if (kpargv_find(kcontext_pargv(context), path_var) || cur_path) {
- args = param2argv(cur_path, kcontext_pargv(context), path_var);
- pline = pline_parse(sess, args, srp_udata_opts(context));
- faux_argv_free(args);
- if (pline->invalid) {
- fprintf(stderr, ERRORMSG "Invalid 'show' request\n");
- goto err;
- }
- if (faux_list_len(pline->exprs) > 1) {
- fprintf(stderr, ERRORMSG "Can't process more than one object\n");
- goto err;
- }
- if (!(expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs)))) {
- fprintf(stderr, ERRORMSG "Can't get expression\n");
- goto err;
- }
- if (!(expr->pat & PT_EDIT)) {
- fprintf(stderr, ERRORMSG "Illegal expression for 'show' operation\n");
- goto err;
- }
- if (!expr->xpath) {
- fprintf(stderr, ERRORMSG "Empty expression for 'show' operation\n");
- goto err;
- }
- xpath = expr->xpath;
- }
- show_xpath(sess, xpath, srp_udata_opts(context));
- ret = 0;
- err:
- pline_free(pline);
- if (ds != SRP_REPO_EDIT)
- sr_session_switch_ds(sess, SRP_REPO_EDIT);
- return ret;
- }
- static int show_path(kcontext_t *context, bool_t use_cur_path)
- {
- sr_datastore_t ds = SRP_REPO_EDIT;
- const char *script = NULL;
- assert(context);
- script = kcontext_script(context);
- if (!faux_str_is_empty(script))
- if (!kly_str2ds(script, strlen(script), &ds))
- ds = SRP_REPO_EDIT;
- return show(context, ds, ARG_PATH, use_cur_path);
- }
- int srp_show_abs(kcontext_t *context)
- {
- return show_path(context, BOOL_FALSE);
- }
- int srp_show(kcontext_t *context)
- {
- return show_path(context, BOOL_TRUE);
- }
- int srp_deactivate(kcontext_t *context)
- {
- int ret = -1;
- faux_argv_t *args = NULL;
- pline_t *pline = NULL;
- sr_session_ctx_t *sess = NULL;
- pexpr_t *expr = NULL;
- faux_argv_t *cur_path = NULL;
- sr_data_t *data = NULL;
- assert(context);
- sess = srp_udata_sr_sess(context);
- cur_path = (faux_argv_t *)srp_udata_path(context);
- args = param2argv(cur_path, kcontext_pargv(context), ARG_PATH);
- pline = pline_parse(sess, args, srp_udata_opts(context));
- faux_argv_free(args);
- if (pline->invalid) {
- fprintf(stderr, ERRORMSG "Invalid 'show' request\n");
- goto err;
- }
- if (faux_list_len(pline->exprs) > 1) {
- fprintf(stderr, ERRORMSG "Can't process more than one object\n");
- goto err;
- }
- expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
- if (!(expr->pat & PT_DEL)) {
- fprintf(stderr, ERRORMSG "Illegal expression for 'show' operation\n");
- goto err;
- }
- if (sr_get_subtree(sess, expr->xpath, 0, &data) != SR_ERR_OK) {
- srp_error(sess, ERRORMSG "Can't get specified subtree\n");
- goto err;
- }
- if (!data) // Not found
- goto err;
- if (lyd_new_meta(LYD_CTX(data->tree), data->tree, NULL,
- "junos-configuration-metadata:active", "false", 0, NULL)) {
- fprintf(stderr, ERRORMSG "Can't deactivate\n");
- goto err;
- }
- struct lyd_meta *meta = lyd_find_meta(data->tree->meta, NULL, "junos-configuration-metadata:active");
- if (meta)
- printf("META\n");
- if (sr_has_changes(sess))
- fprintf(stderr, ERRORMSG "Has changes\n");
- if (sr_apply_changes(sess, 0) != SR_ERR_OK) {
- sr_discard_changes(sess);
- srp_error(sess, ERRORMSG "Can't apply changes\n");
- }
- sr_release_data(data);
- if (sr_get_subtree(sess, expr->xpath, 0, &data) != SR_ERR_OK) {
- srp_error(sess, ERRORMSG "Can't get specified subtree\n");
- goto err;
- }
- if (!data) // Not found
- goto err;
- struct ly_out *out = NULL;
- ly_out_new_file(stdout, &out);
- lyd_print_tree(out, data->tree, LYD_XML, 0);
- ly_out_free(out, NULL, 0);
- sr_release_data(data);
- ret = 0;
- err:
- pline_free(pline);
- return ret;
- }
- int srp_diff(kcontext_t *context)
- {
- int ret = -1;
- pline_t *pline = NULL;
- sr_session_ctx_t *sess = NULL;
- sr_data_t *data1 = NULL;
- sr_data_t *data2 = NULL;
- faux_argv_t *cur_path = NULL;
- const char *xpath = NULL;
- struct lyd_node *diff = NULL;
- pline_opts_t masked_opts = {};
- assert(context);
- sess = srp_udata_sr_sess(context);
- cur_path = (faux_argv_t *)srp_udata_path(context);
- if (kpargv_find(kcontext_pargv(context), ARG_PATH) || cur_path) {
- faux_argv_t *args = NULL;
- pexpr_t *expr = NULL;
- args = param2argv(cur_path, kcontext_pargv(context), ARG_PATH);
- pline = pline_parse(sess, args, srp_udata_opts(context));
- faux_argv_free(args);
- if (pline->invalid) {
- fprintf(stderr, ERRORMSG "Invalid 'show' request\n");
- goto err;
- }
- if (faux_list_len(pline->exprs) > 1) {
- fprintf(stderr, ERRORMSG "Can't process more than one object\n");
- goto err;
- }
- if (!(expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs)))) {
- fprintf(stderr, ERRORMSG "Can't get expression\n");
- goto err;
- }
- if (!(expr->pat & PT_EDIT)) {
- fprintf(stderr, ERRORMSG "Illegal expression for 'show' operation\n");
- goto err;
- }
- if (!expr->xpath) {
- fprintf(stderr, ERRORMSG "Empty expression for 'show' operation\n");
- goto err;
- }
- xpath = expr->xpath;
- }
- if (!xpath)
- xpath = "/*";
- if (sr_get_data(sess, xpath, 0, 0, 0, &data2) != SR_ERR_OK) {
- srp_error(sess, ERRORMSG "Can't get specified subtree\n");
- goto err;
- }
- // Running config
- sr_session_switch_ds(sess, SR_DS_RUNNING);
- if (sr_get_data(sess, xpath, 0, 0, 0, &data1) != SR_ERR_OK) {
- srp_error(sess, ERRORMSG "Can't get specified subtree\n");
- goto err;
- }
- if (lyd_diff_siblings(data1 ? data1->tree : NULL, data2 ? data2->tree : NULL,
- 0, &diff) != LY_SUCCESS) {
- srp_error(sess, ERRORMSG "Can't generate diff\n");
- goto err;
- }
- // Hack to don't show oneliners within diff. Mask oneliners flag
- masked_opts = *srp_udata_opts(context);
- masked_opts.oneliners = BOOL_FALSE;
- show_subtree(diff, 0, DIFF_OP_NONE, &masked_opts, BOOL_FALSE);
- lyd_free_siblings(diff);
- ret = 0;
- err:
- if (data1)
- sr_release_data(data1);
- if (data2)
- sr_release_data(data2);
- pline_free(pline);
- sr_session_switch_ds(sess, SRP_REPO_EDIT);
- return ret;
- }
- int srp_compl_xpath(kcontext_t *context)
- {
- sr_session_ctx_t *sess = NULL;
- sr_val_t *vals = NULL;
- size_t val_num = 0;
- size_t i = 0;
- const char *script = NULL;
- const char *raw_xpath = NULL;
- sr_datastore_t ds = SRP_REPO_EDIT;
- assert(context);
- script = kcontext_script(context);
- if (faux_str_is_empty(script))
- return -1;
- if (!kly_parse_ext_xpath(script, &raw_xpath, &ds))
- return -1;
- sess = srp_udata_sr_sess(context);
- if (ds != SRP_REPO_EDIT)
- sr_session_switch_ds(sess, ds);
- sr_get_items(sess, raw_xpath, 0, 0, &vals, &val_num);
- for (i = 0; i < val_num; i++) {
- char *tmp = sr_val_to_str(&vals[i]);
- if (!tmp)
- continue;
- printf("%s\n", tmp);
- free(tmp);
- }
- sr_free_values(vals, val_num);
- if (ds != SRP_REPO_EDIT)
- sr_session_switch_ds(sess, SRP_REPO_EDIT);
- return 0;
- }
- // Function for mass config strings load. It can load stream of KPath strings
- // (without "set" command, only path and value). Function doesn't use
- // pre-connected session because it can be executed within FILTER or utility.
- int srp_mass_set(int fd, sr_datastore_t ds, const faux_argv_t *cur_path,
- const pline_opts_t *opts, const char *user, bool_t stop_on_error)
- {
- int ret = -1;
- int err = SR_ERR_OK;
- sr_conn_ctx_t *conn = NULL;
- sr_session_ctx_t *sess = NULL;
- faux_file_t *file = NULL;
- char *line = NULL;
- size_t err_num = 0;
- sr_subscription_ctx_t *nacm_sub = NULL;
- err = sr_connect(SR_CONN_DEFAULT, &conn);
- if (err) {
- fprintf(stderr, "Error: Can't connect to sysrepo\n");
- goto out;
- }
- err = sr_session_start(conn, ds, &sess);
- if (err) {
- fprintf(stderr, "Error: Can't start session\n");
- goto out;
- }
- sr_session_set_orig_name(sess, user);
- // Init NACM session
- if (opts->enable_nacm) {
- if (sr_nacm_init(sess, 0, &nacm_sub) != SR_ERR_OK) {
- fprintf(stderr, "Error: Can't init NACM\n");
- goto out;
- }
- sr_nacm_set_user(sess, user);
- }
- file = faux_file_fdopen(fd);
- if (!file) {
- fprintf(stderr, "Error: Can't open input stream\n");
- goto out;
- }
- while ((line = faux_file_getline(file))) {
- pline_t *pline = NULL;
- faux_argv_t *args = NULL;
- // Don't process empty strings and strings with only spaces
- if (!faux_str_has_content(line)) {
- faux_str_free(line);
- continue;
- }
- // Add current sysrepo path
- if (cur_path)
- args = faux_argv_dup(cur_path);
- else
- args = faux_argv_new();
- faux_argv_parse(args, line);
- pline = pline_parse(sess, args, opts);
- faux_argv_free(args);
- if (!pline || pline->invalid) {
- err_num++;
- fprintf(stderr, "Error: Illegal: %s\n", line);
- } else {
- faux_list_node_t *iter = NULL;
- pexpr_t *expr = NULL;
- iter = faux_list_head(pline->exprs);
- while ((expr = (pexpr_t *)faux_list_each(&iter))) {
- if (!(expr->pat & PT_SET)) {
- err_num++;
- fprintf(stderr, "Error: Illegal expression for set operation\n");
- break;
- }
- if (sr_set_item_str(sess, expr->xpath, expr->value, NULL, 0) !=
- SR_ERR_OK) {
- err_num++;
- fprintf(stderr, "Error: Can't set data\n");
- break;
- }
- }
- }
- if (stop_on_error && (err_num > 0)) {
- sr_discard_changes(sess);
- goto out;
- }
- pline_free(pline);
- faux_str_free(line);
- }
- if (sr_has_changes(sess)) {
- if (sr_apply_changes(sess, 0) != SR_ERR_OK) {
- sr_discard_changes(sess);
- fprintf(stderr, "Error: Can't apply changes\n");
- goto out;
- }
- }
- ret = 0;
- out:
- faux_file_close(file);
- if (opts->enable_nacm) {
- sr_unsubscribe(nacm_sub);
- sr_nacm_destroy();
- }
- sr_disconnect(conn);
- return ret;
- }
|