Skip to content

Instantly share code, notes, and snippets.

@rwestphal
Created April 2, 2018 19:03
Show Gist options
  • Save rwestphal/488b0041245c6ef88b334e5db1d9b3d8 to your computer and use it in GitHub Desktop.
Save rwestphal/488b0041245c6ef88b334e5db1d9b3d8 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <err.h>
#include <assert.h>
#include <arpa/inet.h>
#include <stdbool.h>
#include <libyang/libyang.h>
static struct ly_ctx *ctx;
static struct lyd_node *running;
static void print(struct lyd_node *config)
{
struct ly_set *set;
printf("===========================================\n");
printf("CONFIG:\n");
printf("===========================================\n");
lyd_print_fd(2, config, LYD_JSON, LYP_FORMAT | LYP_WD_ALL);
set = lyd_find_path(running, "//*");
if (set == NULL)
errx(1, "lyd_find_path");
printf("Nodes: total(%u)\n", set->number);
for (size_t i = 0; i < set->number; i++) {
struct lyd_node *node = set->set.d[0];
printf(" %s\n", lyd_path(node)); /* memleak */
}
}
static void validate(struct lyd_node *config)
{
printf("===========================================\n");
printf("VALIDATING\n");
printf("===========================================\n");
if (lyd_validate(&config, LYD_OPT_CONFIG, ctx) != 0)
errx(1, "lyd_validate");
}
int main(int argc, char **argv)
{
const struct lys_module *module;
struct lyd_node *node;
struct ly_set *set;
const char *xpath = "/frr-ripd:ripd/instance";
/* initialization */
ctx = ly_ctx_new(NULL, 0);
if (!ctx)
errx(1, "ly_ctx_new");
ly_ctx_set_searchdir(ctx, "/usr/local/share/yang/");
module = lys_parse_path(ctx, "/usr/local/share/yang/frr-ripd.yang",
LYS_IN_YANG);
if (!module)
errx(1, "lys_parse_path");
running = lyd_new(NULL, module, "ripd");
printf("===========================================\n");
printf("ADDING CONTAINER:\n");
printf("===========================================\n");
ly_errno = 0;
node = lyd_new_path(running, ctx, xpath, NULL, 0, LYD_PATH_OPT_UPDATE);
if (node == NULL && ly_errno)
errx(1, "lyd_new_path");
print(running);
validate(running);
print(running);
printf("===========================================\n");
printf("DELETING CONTAINER:\n");
printf("===========================================\n");
set = lyd_find_path(running, xpath);
if (set == NULL || set->number != 1)
errx(1, "lyd_find_path");
node = set->set.d[0];
ly_set_free(set);
lyd_free(node);
//lyd_free_withsiblings(node);
//lyd_unlink(node);
print(running);
validate(running);
print(running);
/* cleanup */
lyd_free_withsiblings(running);
ly_ctx_remove_module(module, NULL);
ly_ctx_destroy(ctx, NULL);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment