From 6dafda072a2ea29939a7a9ba8b9b9eee4c81b289 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Tue, 4 Jul 2023 17:49:06 +0200 Subject: [PATCH] klinfix: add klix_rpc() for poweroff and reboot CLI commands This change allows us to verify common RPC commands from within the CLI, starting with the poweroff and reboot commands. Signed-off-by: Joachim Wiberg --- src/klinfix/src/klinfix.c | 48 +++++++++++++++++++++++++++++++++++++++ src/klinfix/xml/infix.xml | 4 ++-- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/klinfix/src/klinfix.c b/src/klinfix/src/klinfix.c index 8abd910f..3feefc9e 100644 --- a/src/klinfix/src/klinfix.c +++ b/src/klinfix/src/klinfix.c @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -140,6 +141,51 @@ err: return -1; } +int klix_rpc(kcontext_t *ctx) +{ + sr_session_ctx_t *sess; + sr_conn_ctx_t *conn; + const char *xpath; + sr_val_t *output; + size_t cnt; + int err; + + xpath = kcontext_script(ctx); + if (!xpath) { + fprintf(stderr, "Error: cannot find rpc xpath\n"); + goto err; + } + + if (sr_connect(SR_CONN_DEFAULT, &conn)) { + fprintf(stderr, "Error: Connection to datastore failed\n"); + goto err; + } + + if (sr_session_start(conn, SR_DS_OPERATIONAL, &sess)) { + fprintf(stderr, "Error: Unable to open transaction to running-config\n"); + goto err_disconnect; + } + + if ((err = sr_rpc_send(sess, xpath, NULL, 0, 0, &output, &cnt))) { + fprintf(stderr, "Failed sending RPC %s: %s", xpath, sr_strerror(err)); + goto err_disconnect; + } + + for (size_t i = 0; i < cnt; i++) { + sr_print_val(&output[i]); + puts(""); + } + + sr_free_values(output, cnt); + sr_disconnect(conn); + return 0; + +err_disconnect: + sr_disconnect(conn); +err: + return -1; +} + int kplugin_klinfix_fini(kcontext_t *ctx) { return 0; @@ -151,5 +197,7 @@ int kplugin_klinfix_init(kcontext_t *ctx) kplugin_add_syms(plugin, ksym_new("klix_copy", klix_copy)); kplugin_add_syms(plugin, ksym_new("klix_commit", klix_commit)); + kplugin_add_syms(plugin, ksym_new("klix_rpc", klix_rpc)); + return 0; } diff --git a/src/klinfix/xml/infix.xml b/src/klinfix/xml/infix.xml index 0bd6a059..9e7ab4e3 100644 --- a/src/klinfix/xml/infix.xml +++ b/src/klinfix/xml/infix.xml @@ -93,11 +93,11 @@ - poweroff + /ietf-system:system-shutdown - reboot + /ietf-system:system-restart