예제 #1
0
static int
setup_f(void **state)
{
    struct state *st;
    const char *augschema = "ietf-ip";
    const char *typeschema = "iana-if-type";
    const char *ietfdir = TESTS_DIR"/schema/yin/ietf/";
    const struct lys_module *mod;

    (*state) = st = calloc(1, sizeof *st);
    if (!st) {
        fprintf(stderr, "Memory allocation error.\n");
        return -1;
    }

    /* libyang context */
    st->ctx = ly_ctx_new(ietfdir);
    if (!st->ctx) {
        fprintf(stderr, "Failed to create context.\n");
        goto error;
    }

    /* schema */
    mod = ly_ctx_load_module(st->ctx, augschema, NULL);
    if (!mod) {
        fprintf(stderr, "Failed to load data module \"%s\".\n", augschema);
        goto error;
    }
    lys_features_enable(mod, "*");

    mod = ly_ctx_get_module(st->ctx, "ietf-interfaces", NULL);
    if (!mod) {
        fprintf(stderr, "Failed to get data module \"ietf-interfaces\".\n");
        goto error;
    }
    lys_features_enable(mod, "*");

    mod = ly_ctx_load_module(st->ctx, typeschema, NULL);
    if (!mod) {
        fprintf(stderr, "Failed to load data module \"%s\".\n", typeschema);
        goto error;
    }

    /* data */
    st->dt = lyd_parse_mem(st->ctx, data, LYD_XML, LYD_OPT_CONFIG);
    if (!st->dt) {
        fprintf(stderr, "Failed to build the data tree.\n");
        goto error;
    }

    return 0;

error:
    ly_ctx_destroy(st->ctx, NULL);
    free(st);
    (*state) = NULL;

    return -1;
}
예제 #2
0
void
test_parse_print_yang(void **state)
{
    struct state *st = (*state);
    struct stat s;
    int fd;

    *state = st = calloc(1, sizeof *st);
    assert_ptr_not_equal(st, NULL);

    st->ctx = ly_ctx_new(TESTS_DIR"/data/files");
    assert_ptr_not_equal(st->ctx, NULL);

    st->mod = lys_parse_path(st->ctx, TESTS_DIR"/data/files/all.yang", LYS_IN_YANG);
    assert_ptr_not_equal(st->mod, NULL);

    st->mod = lys_parse_path(st->ctx, TESTS_DIR"/data/files/all-dev.yang", LYS_IN_YANG);
    assert_ptr_not_equal(st->mod, NULL);

    fd = open(TESTS_DIR"/data/files/all-dev.yang", O_RDONLY);
    fstat(fd, &s);
    st->str1 = malloc(s.st_size + 1);
    assert_ptr_not_equal(st->str1, NULL);
    assert_int_equal(read(fd, st->str1, s.st_size), s.st_size);
    st->str1[s.st_size] = '\0';

    lys_print_mem(&(st->str2), st->mod, LYS_OUT_YANG, NULL);

    assert_string_equal(st->str1, st->str2);

    close(fd);
    fd = -1;
    free(st->str1);
    st->str1 = NULL;
    free(st->str2);
    st->str2 = NULL;

    st->mod = ly_ctx_get_module(st->ctx, "all", NULL);
    assert_ptr_not_equal(st->mod, NULL);

    fd = open(TESTS_DIR"/data/files/all.yang", O_RDONLY);
    fstat(fd, &s);
    st->str1 = malloc(s.st_size + 1);
    assert_ptr_not_equal(st->str1, NULL);
    assert_int_equal(read(fd, st->str1, s.st_size), s.st_size);
    st->str1[s.st_size] = '\0';

    lys_print_mem(&(st->str2), st->mod, LYS_OUT_YANG, NULL);

    assert_string_equal(st->str1, st->str2);
}
예제 #3
0
struct nc_server_reply *
my_getconfig_rpc_clb(struct lyd_node *rpc, struct nc_session *session)
{
    struct lyd_node *data;
    const struct lys_module *module;

    assert_string_equal(rpc->schema->name, "get-config");
    assert_ptr_equal(session, server_session);

    module = ly_ctx_get_module(session->ctx, "ietf-netconf-acm", NULL);
    assert_non_null(module);

    data = lyd_new(NULL, module, "nacm");
    assert_non_null(data);

    return nc_server_reply_data(data, NC_PARAMTYPE_FREE);
}
예제 #4
0
static int
lyb_parse_model(struct ly_ctx *ctx, const char *data, const struct lys_module **mod, struct lyb_state *lybs)
{
    int r, ret = 0;
    char *mod_name = NULL, mod_rev[11];
    uint16_t rev = 0;
    uint8_t tmp_buf[2];

    /* model name */
    ret += (r = lyb_read_string(data, &mod_name, 1, lybs));
    LYB_HAVE_READ_GOTO(r, data, error);

    /* revision */
    ret += (r = lyb_read(data, tmp_buf, sizeof tmp_buf, lybs));
    LYB_HAVE_READ_GOTO(r, data, error);
    rev = tmp_buf[0] | (tmp_buf[1] << 8);

    if (rev) {
        sprintf(mod_rev, "%04u-%02u-%02u", ((rev & 0xFE00) >> 9) + 2000, (rev & 0x01E0) >> 5, (rev & 0x001F));
        *mod = ly_ctx_get_module(ctx, mod_name, mod_rev, 0);
    } else {
예제 #5
0
파일: context.c 프로젝트: l90005352/libyang
API const struct lys_node *
ly_ctx_get_node(const struct ly_ctx *ctx, const char *nodeid)
{
    const struct lys_node *ret;
    const struct lys_module *module;
    char *mod_name;
    int parsed;

    if (!ctx || !nodeid) {
        ly_errno = LY_EINVAL;
        return NULL;
    }

    if ((nodeid[0] != '/') || ((parsed = parse_identifier(nodeid + 1)) < 1)) {
        ly_errno = LY_EINVAL;
        return NULL;
    }

    /* get the correct module */
    mod_name = strndup(nodeid + 1, parsed);
    if (!mod_name) {
        LOGMEM;
        return NULL;
    }
    module = ly_ctx_get_module(ctx, mod_name, NULL);
    free(mod_name);
    if (!module) {
        ly_errno = LY_EINVAL;
        return NULL;
    }

    /* now we can parse the whole schema */
    if (resolve_schema_nodeid(nodeid, NULL, module, LYS_AUGMENT, &ret)) {
        ly_errno = LY_EINVAL;
        return NULL;
    }

    return ret;
}
예제 #6
0
파일: context.c 프로젝트: l90005352/libyang
API struct lyd_node *
ly_ctx_info(struct ly_ctx *ctx)
{
    int i;
    char id[8];
    const struct lys_module *mod;
    struct lyd_node *root, *cont;

    mod = ly_ctx_get_module(ctx, "ietf-yang-library", NULL);
    if (!mod) {
        mod = lyp_search_file(ctx, NULL, "ietf-yang-library", NULL);
    }
    if (!mod || !mod->data || strcmp(mod->data->next->name, "modules")) {
        return NULL;
    }

    root = lyd_new(NULL, mod, "modules");
    if (!root) {
        return NULL;
    }

    for (i = 0; i < ctx->models.used; ++i) {
        cont = lyd_new(root, NULL, "module");
        if (!cont) {
            lyd_free(root);
            return NULL;
        }

        if (!lyd_new_leaf(cont, NULL, "name", ctx->models.list[i]->name)) {
            lyd_free(root);
            return NULL;
        }
        if (!lyd_new_leaf(cont, NULL, "revision", (ctx->models.list[i]->rev_size ?
                              ctx->models.list[i]->rev[0].date : ""))) {
            lyd_free(root);
            return NULL;
        }
        if (ctx->models.list[i]->uri
                && !lyd_new_leaf(cont, NULL, "schema", ctx->models.list[i]->uri)) {
            lyd_free(root);
            return NULL;
        }
        if (!lyd_new_leaf(cont, NULL, "namespace", ctx->models.list[i]->ns)) {
            lyd_free(root);
            return NULL;
        }
        if (ylib_feature(cont, ctx->models.list[i])) {
            lyd_free(root);
            return NULL;
        }
        if (ylib_deviation(cont, ctx->models.list[i], ctx)) {
            lyd_free(root);
            return NULL;
        }
        if (ctx->models.list[i]->implemented
                && !lyd_new_leaf(cont, NULL, "conformance", "implement")) {
            lyd_free(root);
            return NULL;
        }
        if (!ctx->models.list[i]->implemented
                && !lyd_new_leaf(cont, NULL, "conformance", "import")) {
            lyd_free(root);
            return NULL;
        }
        if (ylib_submodules(cont, ctx->models.list[i])) {
            lyd_free(root);
            return NULL;
        }
    }

    sprintf(id, "%u", ctx->models.module_set_id);
    if (!lyd_new_leaf(root, mod, "module-set-id", id)) {
        lyd_free(root);
        return NULL;
    }

    if (lyd_validate(root, 0)) {
        lyd_free(root);
        return NULL;
    }

    return root;
}
예제 #7
0
    uint8_t tmp_buf[2];

    /* model name */
    ret += (r = lyb_read_string(data, &mod_name, 1, lybs));
    LYB_HAVE_READ_GOTO(r, data, error);

    /* revision */
    ret += (r = lyb_read(data, tmp_buf, sizeof tmp_buf, lybs));
    LYB_HAVE_READ_GOTO(r, data, error);
    rev = tmp_buf[0] | (tmp_buf[1] << 8);

    if (rev) {
        sprintf(mod_rev, "%04u-%02u-%02u", ((rev & 0xFE00) >> 9) + 2000, (rev & 0x01E0) >> 5, (rev & 0x001F));
        *mod = ly_ctx_get_module(ctx, mod_name, mod_rev, 0);
    } else {
        *mod = ly_ctx_get_module(ctx, mod_name, NULL, 0);
    }
    if (ctx->data_clb) {
        if (!*mod) {
            *mod = ctx->data_clb(ctx, mod_name, NULL, 0, ctx->data_clb_data);
        } else if (!(*mod)->implemented) {
            *mod = ctx->data_clb(ctx, mod_name, (*mod)->ns, LY_MODCLB_NOT_IMPLEMENTED, ctx->data_clb_data);
        }
    }

    if (!*mod) {
        LOGERR(ctx, LY_EINVAL, "Invalid context for LYB data parsing, missing module \"%s%s%s\".",
               mod_name, rev ? "@" : "", rev ? mod_rev : "");
        goto error;
    } else if (!(*mod)->implemented) {
        LOGERR(ctx, LY_EINVAL, "Invalid context for LYB data parsing, module \"%s%s%s\" not implemented.",
예제 #8
0
파일: commands.c 프로젝트: cejkato2/libyang
int
cmd_feature(const char *arg)
{
    int c, i, argc, option_index, ret = 1, task = -1;
    char **argv = NULL, *ptr, **names, **enable_state;
    const char *feat_name = NULL;
    struct ly_module *model, *parent_model;
    static struct option long_options[] = {
        {"help", no_argument, 0, 'h'},
        {"print", no_argument, 0, 'p'},
        {"enable", required_argument, 0, 'e'},
        {"disable", required_argument, 0, 'd'},
        {NULL, 0, 0, 0}
    };

    argc = 1;
    argv = malloc(2*sizeof *argv);
    *argv = strdup(arg);
    ptr = strtok(*argv, " ");
    while ((ptr = strtok(NULL, " "))) {
        argv = realloc(argv, (argc+2)*sizeof *argv);
        argv[argc++] = ptr;
    }
    argv[argc] = NULL;

    optind = 0;
    while (1) {
        option_index = 0;
        c = getopt_long(argc, argv, "hpe:d:", long_options, &option_index);
        if (c == -1) {
            break;
        }

        switch (c) {
        case 'h':
            cmd_feature_help();
            ret = 0;
            goto cleanup;
        case 'p':
            if (task != -1) {
                fprintf(stderr, "Only one of print, enable, or disable can be specified.\n");
                goto cleanup;
            }
            task = 0;
            break;
        case 'e':
            if (task != -1) {
                fprintf(stderr, "Only one of print, enable, or disable can be specified.\n");
                goto cleanup;
            }
            task = 1;
            feat_name = optarg;
            break;
        case 'd':
            if (task != -1) {
                fprintf(stderr, "Only one of print, enable, or disable can be specified.\n");
                goto cleanup;
            }
            task = 2;
            feat_name = optarg;
            break;
        case '?':
            fprintf(stderr, "Unknown option \"%d\".\n", (char)c);
            goto cleanup;
        }
    }

    /* model name */
    if (optind == argc) {
        fprintf(stderr, "Missing the model name.\n");
        goto cleanup;
    }
    model = ly_ctx_get_module(ctx, argv[optind], NULL, 0);
    if (model == NULL) {
        names = ly_ctx_get_module_names(ctx);
        for (i = 0; names[i]; i++) {
            if (!model) {
                parent_model = ly_ctx_get_module(ctx, names[i], NULL, 0);
                model = (struct ly_module *)ly_ctx_get_submodule(parent_model, argv[optind], NULL, 0);
            }
            free(names[i]);
        }
        free(names);
    }
    if (model == NULL) {
        fprintf(stderr, "No model \"%s\" found.\n", argv[optind]);
        goto cleanup;
    }

    if (task == -1) {
        fprintf(stderr, "One of print, enable, or disable must be specified.\n");
        goto cleanup;
    }

    if (task == 0) {
        printf("%s features:\n", model->name);

        names = ly_get_features(model, &enable_state);
        for (i = 0; names[i]; ++i) {
            printf("\t%s %s\n", names[i], enable_state[i]);
            free(names[i]);
            free(enable_state[i]);
        }
        free(names);
        free(enable_state);
        if (!i) {
            printf("\t(none)\n");
        }
    } else if (task == 1) {
        if (ly_features_enable(model, feat_name)) {
            fprintf(stderr, "Feature \"%s\" not found.\n", feat_name);
            ret = 1;
        }
    } else if (task == 2) {
        if (ly_features_disable(model, feat_name)) {
            fprintf(stderr, "Feature \"%s\" not found.\n", feat_name);
            ret = 1;
        }
    }

cleanup:
    free(*argv);
    free(argv);

    return ret;
}
예제 #9
0
파일: commands.c 프로젝트: cejkato2/libyang
int
cmd_print(const char *arg)
{
    int c, i, argc, option_index, ret = 1;
    char **argv = NULL, *ptr, *target_node = NULL, **names;
    const char *out_path = NULL;
    struct ly_module *model, *parent_model;
    LY_MOUTFORMAT format = LY_OUT_TREE;
    FILE *output = stdout;
    static struct option long_options[] = {
        {"help", no_argument, 0, 'h'},
        {"format", required_argument, 0, 'f'},
        {"output", required_argument, 0, 'o'},
        {"target-node", required_argument, 0, 't'},
        {NULL, 0, 0, 0}
    };

    argc = 1;
    argv = malloc(2*sizeof *argv);
    *argv = strdup(arg);
    ptr = strtok(*argv, " ");
    while ((ptr = strtok(NULL, " "))) {
        argv = realloc(argv, (argc+2)*sizeof *argv);
        argv[argc++] = ptr;
    }
    argv[argc] = NULL;

    optind = 0;
    while (1) {
        option_index = 0;
        c = getopt_long(argc, argv, "hf:o:t:", long_options, &option_index);
        if (c == -1) {
            break;
        }

        switch (c) {
        case 'h':
            cmd_print_help();
            ret = 0;
            goto cleanup;
        case 'f':
            if (!strcmp(optarg, "yang")) {
                format = LY_OUT_YANG;
            } else if (!strcmp(optarg, "tree")) {
                format = LY_OUT_TREE;
            } else if (!strcmp(optarg, "info")) {
                format = LY_OUT_INFO;
            } else {
                fprintf(stderr, "Unknown output format \"%s\".\n", optarg);
                goto cleanup;
            }
            break;
        case 'o':
            if (out_path) {
                fprintf(stderr, "Output specified twice.\n");
                goto cleanup;
            }
            out_path = optarg;
            break;
        case 't':
            target_node = optarg;
            break;
        case '?':
            fprintf(stderr, "Unknown option \"%d\".\n", (char)c);
            goto cleanup;
        }
    }

    /* file name */
    if (optind == argc) {
        fprintf(stderr, "Missing the model name.\n");
        goto cleanup;
    }

    model = ly_ctx_get_module(ctx, argv[optind], NULL, 0);
    if (model == NULL) {
        names = ly_ctx_get_module_names(ctx);
        for (i = 0; names[i]; i++) {
            if (!model) {
                parent_model = ly_ctx_get_module(ctx, names[i], NULL, 0);
                model = (struct ly_module *)ly_ctx_get_submodule(parent_model, argv[optind], NULL, 0);
            }
            free(names[i]);
        }
        free(names);
    }

    if (model == NULL) {
        fprintf(stderr, "No model \"%s\" found.\n", argv[optind]);
        goto cleanup;
    }

    if (out_path) {
        output = fopen(out_path, "w");
        if (!output) {
            fprintf(stderr, "Could not open the output file (%s).\n", strerror(errno));
            goto cleanup;
        }
    }

    ret = ly_model_print(output, model, format, target_node);

cleanup:
    free(*argv);
    free(argv);

    if (output && (output != stdout)) {
        fclose(output);
    }

    return ret;
}
예제 #10
0
파일: common.c 프로젝트: milanlenco/libyang
static const char *
_transform_json2xml(const struct lys_module *module, const char *expr, int schema, const char ***prefixes,
                    const char ***namespaces, uint32_t *ns_count)
{
    const char *in, *id, *prefix;
    char *out, *col, *name;
    size_t out_size, out_used, id_len;
    const struct lys_module *mod;
    uint32_t i;

    assert(module && expr && ((!prefixes && !namespaces && !ns_count) || (prefixes && namespaces && ns_count)));

    if (ns_count) {
        *ns_count = 0;
        *prefixes = NULL;
        *namespaces = NULL;
    }

    in = expr;
    out_size = strlen(in) + 1;
    out = malloc(out_size);
    if (!out) {
        LOGMEM;
        return NULL;
    }
    out_used = 0;

    while (1) {
        col = strchr(in, ':');
        /* we're finished, copy the remaining part */
        if (!col) {
            strcpy(&out[out_used], in);
            out_used += strlen(in) + 1;
            assert(out_size == out_used);
            return lydict_insert_zc(module->ctx, out);
        }
        id = strpbrk_backwards(col - 1, "/ [\'\"", (col - in) - 1);
        if ((id[0] == '/') || (id[0] == ' ') || (id[0] == '[') || (id[0] == '\'') || (id[0] == '\"')) {
            ++id;
        }
        id_len = col - id;

        /* get the module */
        if (!schema) {
            name = strndup(id, id_len);
            mod = ly_ctx_get_module(module->ctx, name, NULL);
            free(name);
            if (!mod) {
                LOGVAL(LYE_INMOD_LEN, LY_VLOG_NONE, NULL, id_len, id);
                goto fail;
            }
            prefix = mod->prefix;
        } else {
            name = strndup(id, id_len);
            prefix = transform_module_name2import_prefix(module, name);
            free(name);
            if (!prefix) {
                LOGVAL(LYE_INMOD_LEN, LY_VLOG_NONE, NULL, id_len, id);
                goto fail;
            }
        }

        /* remember the namespace definition (only if it's new) */
        if (!schema && ns_count) {
            for (i = 0; i < *ns_count; ++i) {
                if (ly_strequal((*namespaces)[i], mod->ns, 1)) {
                    break;
                }
            }
            if (i == *ns_count) {
                ++(*ns_count);
                *prefixes = ly_realloc(*prefixes, *ns_count * sizeof **prefixes);
                if (!(*prefixes)) {
                    LOGMEM;
                    goto fail;
                }
                *namespaces = ly_realloc(*namespaces, *ns_count * sizeof **namespaces);
                if (!(*namespaces)) {
                    LOGMEM;
                    goto fail;
                }
                (*prefixes)[*ns_count - 1] = mod->prefix;
                (*namespaces)[*ns_count - 1] = mod->ns;
            }
        }

        /* adjust out size */
        out_size += strlen(prefix) - id_len;
        out = ly_realloc(out, out_size);
        if (!out) {
            LOGMEM;
            goto fail;
        }

        /* copy the data before prefix */
        strncpy(&out[out_used], in, id-in);
        out_used += id - in;

        /* copy the model prefix */
        strcpy(&out[out_used], prefix);
        out_used += strlen(prefix);

        /* copy ':' */
        out[out_used] = ':';
        ++out_used;

        /* finally adjust in pointer for next round */
        in = col + 1;
    }

    /* unreachable */
    LOGINT;

fail:
    if (!schema && ns_count) {
        free(*prefixes);
        free(*namespaces);
    }
    free(out);
    return NULL;
}