summaryrefslogtreecommitdiff
path: root/mgmtd
diff options
context:
space:
mode:
authorDonald Sharp <donaldsharp72@gmail.com>2024-02-01 14:56:07 -0500
committerGitHub <noreply@github.com>2024-02-01 14:56:07 -0500
commite24f43e64e0c6d3ea236e526ce0bf4d82c36cad2 (patch)
tree42a9ba087a7a0babcc8ef64413220772fba82c4f /mgmtd
parent62443d7f66ee52d7d71547b5941bf6bb91b3399b (diff)
parent75219184544ab91b5425f27a125077cea9821b0b (diff)
Merge pull request #15272 from LabNConsulting/chopps/mgmtd-notif-test
tests: add mgmtd backend notification test
Diffstat (limited to 'mgmtd')
-rw-r--r--mgmtd/mgmt_testc.c102
1 files changed, 83 insertions, 19 deletions
diff --git a/mgmtd/mgmt_testc.c b/mgmtd/mgmt_testc.c
index 70cd2bb0cd..02a308f328 100644
--- a/mgmtd/mgmt_testc.c
+++ b/mgmtd/mgmt_testc.c
@@ -8,6 +8,7 @@
#include <zebra.h>
#include <lib/version.h>
+#include "darr.h"
#include "libfrr.h"
#include "mgmt_be_client.h"
@@ -15,9 +16,9 @@
/* Local Prototypes */
/* ---------------- */
-static void ripd_notification(struct mgmt_be_client *client, uintptr_t usr_data,
- struct mgmt_be_client_notification_cb *this,
- const char *notif_data);
+static void async_notification(struct mgmt_be_client *client, uintptr_t usr_data,
+ struct mgmt_be_client_notification_cb *this,
+ const char *notif_data);
static void sigusr1(void);
static void sigint(void);
@@ -42,7 +43,16 @@ struct zebra_privs_t __privs = {
.cap_num_i = 0,
};
-struct option longopts[] = {{0}};
+#define OPTION_LISTEN 2000
+#define OPTION_NOTIF_COUNT 2001
+#define OPTION_TIMEOUT 2002
+const struct option longopts[] = {
+ { "listen", no_argument, NULL, OPTION_LISTEN },
+ { "notif-count", required_argument, NULL, OPTION_NOTIF_COUNT },
+ { "timeout", required_argument, NULL, OPTION_TIMEOUT },
+ { 0 }
+};
+
/* Master of threads. */
struct event_loop *master;
@@ -85,17 +95,13 @@ FRR_DAEMON_INFO(mgmtd_testc, MGMTD_TESTC,
);
/* clang-format on */
-struct mgmt_be_client_notification_cb __notify_cbs[] = { {
- .xpath = "frr-ripd",
- .format = LYD_JSON,
- .callback = ripd_notification,
-} };
+struct mgmt_be_client_notification_cb *__notify_cbs;
-struct mgmt_be_client_cbs __client_cbs = {
- .notify_cbs = __notify_cbs,
- .nnotify_cbs = array_size(__notify_cbs),
-};
+struct mgmt_be_client_cbs __client_cbs = {};
+struct event *event_timeout;
+int o_notif_count = 1;
+int o_timeout;
/* --------- */
/* Functions */
@@ -107,22 +113,43 @@ static void sigusr1(void)
zlog_rotate();
}
+static void quit(int exit_code)
+{
+ EVENT_OFF(event_timeout);
+ frr_fini();
+ darr_free(__client_cbs.notify_cbs);
+ exit(exit_code);
+}
+
static void sigint(void)
{
zlog_notice("Terminating on signal");
- frr_fini();
- exit(0);
+ quit(0);
}
-static void ripd_notification(struct mgmt_be_client *client, uintptr_t usr_data,
- struct mgmt_be_client_notification_cb *this,
- const char *notif_data)
+static void timeout(struct event *event)
{
- zlog_notice("Received RIPd notification");
+ zlog_notice("Timeout, exiting");
+ quit(1);
+}
+
+static void async_notification(struct mgmt_be_client *client, uintptr_t usr_data,
+ struct mgmt_be_client_notification_cb *this,
+ const char *notif_data)
+{
+ zlog_notice("Received YANG notification");
+
+ printf("%s\n", notif_data);
+
+ if (o_notif_count && !--o_notif_count)
+ quit(0);
}
int main(int argc, char **argv)
{
+ int f_listen = 0;
+ int i;
+
frr_preinit(&mgmtd_testc_di, argc, argv);
frr_opt_add("", longopts, "");
@@ -135,6 +162,15 @@ int main(int argc, char **argv)
break;
switch (opt) {
+ case OPTION_LISTEN:
+ f_listen = 1;
+ break;
+ case OPTION_NOTIF_COUNT:
+ o_notif_count = atoi(optarg);
+ break;
+ case OPTION_TIMEOUT:
+ o_timeout = atoi(optarg);
+ break;
case 0:
break;
default:
@@ -144,10 +180,38 @@ int main(int argc, char **argv)
master = frr_init();
+ /*
+ * Setup notification listen
+ */
+ argv += optind;
+ argc -= optind;
+ if (!argc && f_listen) {
+ fprintf(stderr,
+ "Must specify at least one notification xpath to listen to\n");
+ exit(1);
+ }
+ if (argc && f_listen) {
+ struct mgmt_be_client_notification_cb *cb;
+
+ for (i = 0; i < argc; i++) {
+ zlog_notice("Listen on xpath: %s", argv[i]);
+ cb = darr_append(__notify_cbs);
+ cb->xpath = argv[i];
+ cb->format = LYD_JSON;
+ cb->callback = async_notification;
+ }
+ __client_cbs.notify_cbs = __notify_cbs;
+ __client_cbs.nnotify_cbs = darr_len(__notify_cbs);
+ }
+
mgmt_be_client = mgmt_be_client_create("mgmtd-testc", &__client_cbs, 0,
master);
frr_config_fork();
+
+ if (o_timeout)
+ event_add_timer(master, timeout, NULL, o_timeout, &event_timeout);
+
frr_run(master);
/* Reached. */