Skip to content

Issue with LYP_WD_TRIM and empty presence containers #570

@rwestphal

Description

@rwestphal

The libyang documentation says the following about the LYP_WD_TRIM flag:

Do not print the nodes with the value equal to their default value

However I noticed that this flag also prevents presence containers from being printed if they are empty. The code below can be used to reproduce the problem:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <err.h>

#include <libyang/libyang.h>

int main(int argc, char *argv[])
{
        struct ly_ctx *ly_ctx;
        struct lyd_node *config = NULL;

        /* Initialization. */
        ly_ctx = ly_ctx_new(NULL, 0);
        if (!ly_ctx)
                errx(1, "ly_ctx_new()");

        ly_ctx_set_searchdir(ly_ctx, "/usr/local/share/yang/");

        /* Load YANG module. */
        ly_ctx_load_module(ly_ctx, "frr-ripd", NULL);

        /* Initialize data node. */
        lyd_validate(&config, LYD_OPT_STRICT | LYD_OPT_CONFIG, ly_ctx);

        /* Create presence container. */
        lyd_new_path(config, ly_ctx, "/frr-ripd:ripd/instance", NULL, 0,
                     LYD_PATH_OPT_UPDATE);

        printf("lyd_print_fd(..., LYP_FORMAT | LYP_WD_TRIM | LYP_WITHSIBLINGS):\n");
        lyd_print_fd(1, config, LYD_JSON,
                     LYP_FORMAT | LYP_WD_TRIM | LYP_WITHSIBLINGS);

        printf("\n");
        printf("lyd_print_fd(..., LYP_FORMAT | LYP_WITHSIBLINGS):\n");
        lyd_print_fd(1, config, LYD_JSON, LYP_FORMAT | LYP_WITHSIBLINGS);

        /* Cleanup. */
        ly_ctx_destroy(ly_ctx, NULL);

        return 0;
}

YANG module: https://gist.github.com/rwestphal/2fc93f3f928fee1127c2913977c00e8d

Output of the program:

# ./show 
lyd_print_fd(..., LYP_FORMAT | LYP_WD_TRIM | LYP_WITHSIBLINGS):
{

}

lyd_print_fd(..., LYP_FORMAT | LYP_WITHSIBLINGS):
{
  "frr-ripd:ripd": {
    "instance": {
    }
  }
}

Notice how the instance presence container wasn't printed when LYP_WD_TRIM was used. The same problem happens when printing using the XML format.

Any help would be appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions