From 1b82e5aaa490814a222d2eeabbb499640005a24b Mon Sep 17 00:00:00 2001 From: Olof hagsand Date: Fri, 30 Aug 2024 12:45:22 +0200 Subject: [PATCH] Add CONTROLLER_YANG_DUMP_DIR debug option for dumping all YANG files --- src/controller.h | 10 ++++++++++ src/controller_device_recv.c | 28 +++++++++++++++++++++++++++- src/controller_device_send.c | 2 ++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/controller.h b/src/controller.h index bcb6e91..86dc9f0 100644 --- a/src/controller.h +++ b/src/controller.h @@ -72,6 +72,16 @@ */ #undef CONTROLLER_EXTRA_PUSH_SYNC +/*! Dump all schemas in disc from devices regardless of local file cache, debug option + * + * Save all files under ${CONTROLLER_YANG_DUMP_DIR}/${devname}/ + * Since this disables the local store cache, all yang files from all devices will be fetched + * which may result in slower and more disk-space. + * NOTE: all devname dirs must be created a priori, they are not created automatically + */ +#undef CONTROLLER_YANG_DUMP_DIR +// #define CONTROLLER_YANG_DUMP_DIR "/usr/local/share/clixon/controller/dump" + #define ACTION_PROCESS "Action process" /*! Controller debug levels diff --git a/src/controller_device_recv.c b/src/controller_device_recv.c index 8b58555..e57a64a 100644 --- a/src/controller_device_recv.c +++ b/src/controller_device_recv.c @@ -498,7 +498,11 @@ device_state_recv_get_schema(device_handle dh, clixon_err(OE_YANG, 0, "schema mount dir not set"); goto done; } - cprintf(cb, "%s/%s", dir, modname); +#ifdef CONTROLLER_YANG_DUMP_DIR + cprintf(cb, "%s", CONTROLLER_YANG_DUMP_DIR); + cprintf(cb, "/%s", device_handle_name_get(dh)); +#endif + cprintf(cb, "/%s", modname); if (revision) cprintf(cb, "@%s", revision); cprintf(cb, ".yang"); @@ -512,6 +516,28 @@ device_state_recv_get_schema(device_handle dh, goto done; } fflush(f); +#ifdef CONTROLLER_YANG_DUMP_DIR + { + cbuf *cb2 = NULL; + + /* Check if exists as local file */ + if ((ret = yang_file_find_match(h, modname, revision, NULL)) < 0) + goto done; + if (ret == 0) { + /* If not exists, copy back to mount-dir */ + if ((cb2 = cbuf_new()) == NULL){ + clixon_err(OE_UNIX, errno, "cbuf_new"); + goto done; + } + cprintf(cb, "%s", dir); + cprintf(cb, "/%s", modname); + if (revision) + cprintf(cb, "@%s", revision); + if (clicon_file_copy(cbuf_get(cb), cbuf_get(cb2)) < 0) + goto done; + } + } +#endif retval = 1; done: if (f) diff --git a/src/controller_device_send.c b/src/controller_device_send.c index b18c66e..09abb06 100644 --- a/src/controller_device_send.c +++ b/src/controller_device_send.c @@ -242,6 +242,7 @@ device_send_get_schema_next(clixon_handle h, name = xml_find_body(x, "name"); revision = xml_find_body(x, "revision"); (*nr)++; +#ifndef CONTROLLER_YANG_DUMP_DIR /* Check if already loaded */ if (yang_find_module_by_name_revision(yspec, name, revision) != NULL) continue; @@ -250,6 +251,7 @@ device_send_get_schema_next(clixon_handle h, goto done; if (ret == 1) continue; +#endif /* May be some concurrency here if several devices requests same yang simultaneously * To avoid that one needs to keep track if another request has been sent. */