Created
April 22, 2018 19:45
-
-
Save rwestphal/24ac6747071d26003e2b4074148350f7 to your computer and use it in GitHub Desktop.
libyang issue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <stdint.h> | |
#include <string.h> | |
#include <err.h> | |
#include <libyang/libyang.h> | |
static struct ly_ctx *ly_ctx; | |
static struct lyd_node *candidate; | |
static void print(struct lyd_node *config) | |
{ | |
lyd_print_fd(2, config, LYD_JSON, | |
LYP_FORMAT | LYP_WD_ALL | LYP_KEEPEMPTYCONT | |
| LYP_WITHSIBLINGS); | |
} | |
static void validate(struct lyd_node **config) | |
{ | |
if (lyd_validate(config, LYD_OPT_STRICT | LYD_OPT_CONFIG, ly_ctx) != 0) | |
errx(1, "lyd_validate(): %s", ly_errmsg(ly_ctx)); | |
} | |
static void ly_log_cb(LY_LOG_LEVEL level, const char *msg, const char *path) | |
{ | |
/* do nothing */ | |
} | |
int main(int argc, char **argv) | |
{ | |
const struct lys_module *module; | |
struct lyd_node *node; | |
const char *xpath = "/frr-ripd:ripd/instance/allow-ecmp"; | |
/* initialization */ | |
ly_ctx = ly_ctx_new(NULL, 0); | |
if (!ly_ctx) | |
errx(1, "ly_ctx_new(): %s", ly_errmsg(ly_ctx)); | |
ly_set_log_clb(ly_log_cb, 0); | |
ly_ctx_set_searchdir(ly_ctx, "/usr/local/share/yang/"); | |
module = lys_parse_path(ly_ctx, "/usr/local/share/yang/frr-ripd.yang", | |
LYS_IN_YANG); | |
if (module == NULL) | |
errx(1, "lys_parse_path(): %s", ly_errmsg(ly_ctx)); | |
validate(&candidate); | |
printf("=================\n"); | |
printf("CANDIDATE CONFIG:\n"); | |
printf("=================\n"); | |
print(candidate); | |
printf("=================\n"); | |
printf("UPDATING CONFIG:\n"); | |
printf("=================\n"); | |
ly_errno = 0; | |
node = lyd_new_path(candidate, ly_ctx, xpath, (void *)"true", 0, | |
LYD_PATH_OPT_UPDATE | LYD_PATH_OPT_NOPARENT); | |
if (node == NULL && ly_errno) | |
warnx("lyd_new_path(\"%s\"): %s", xpath, ly_errmsg(ly_ctx)); | |
validate(&candidate); | |
printf("=================\n"); | |
printf("CANDIDATE CONFIG:\n"); | |
printf("=================\n"); | |
print(candidate); | |
/* cleanup */ | |
lyd_free_withsiblings(candidate); | |
ly_ctx_remove_module(module, NULL); | |
ly_ctx_destroy(ly_ctx, NULL); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment