Loading...
Searching...
No Matches
instanceid.c
Go to the documentation of this file.
#define _GNU_SOURCE /* strdup */
#include "plugins_types.h"
#include <stdint.h>
#include <stdlib.h>
#include "libyang.h"
/* additional internal headers for some useful simple macros */
#include "compat.h"
#include "ly_common.h"
#include "path.h"
#include "plugins_internal.h" /* LY_TYPE_*_STR */
static LY_ERR
instanceid_path2str(const struct ly_path *path, LY_VALUE_FORMAT format, void *prefix_data, char **str)
{
LY_ERR ret = LY_SUCCESS;
LY_ARRAY_COUNT_TYPE u, v;
char *result = NULL, quot;
const struct lys_module *mod = NULL, *local_mod = NULL;
struct ly_set *mods = NULL;
ly_bool inherit_prefix = 0, d;
const char *strval;
switch (format) {
case LY_VALUE_XML:
/* null the local module so that all the prefixes are printed */
mods = prefix_data;
local_mod = mods->objs[0];
mods->objs[0] = NULL;
/* fallthrough */
case LY_VALUE_SCHEMA:
case LY_VALUE_SCHEMA_RESOLVED:
/* everything is prefixed */
inherit_prefix = 0;
break;
case LY_VALUE_CANON:
case LY_VALUE_JSON:
case LY_VALUE_LYB:
case LY_VALUE_STR_NS:
/* the same prefix is inherited and skipped */
inherit_prefix = 1;
break;
}
LY_ARRAY_FOR(path, u) {
/* new node */
if (!inherit_prefix || (mod != path[u].node->module)) {
mod = path[u].node->module;
ret = ly_strcat(&result, "/%s:%s", lyplg_type_get_prefix(mod, format, prefix_data), path[u].node->name);
} else {
ret = ly_strcat(&result, "/%s", path[u].node->name);
}
LY_CHECK_GOTO(ret, cleanup);
/* node predicates */
LY_ARRAY_FOR(path[u].predicates, v) {
struct ly_path_predicate *pred = &path[u].predicates[v];
switch (pred->type) {
case LY_PATH_PREDTYPE_POSITION:
/* position predicate */
ret = ly_strcat(&result, "[%" PRIu64 "]", pred->position);
break;
case LY_PATH_PREDTYPE_LIST:
/* key-predicate */
strval = pred->value.realtype->plugin->print(path[u].node->module->ctx, &pred->value, format, prefix_data,
&d, NULL);
/* default quote */
quot = '\'';
if (strchr(strval, quot)) {
quot = '"';
}
if (inherit_prefix) {
/* always the same prefix as the parent */
ret = ly_strcat(&result, "[%s=%c%s%c]", pred->key->name, quot, strval, quot);
} else {
ret = ly_strcat(&result, "[%s:%s=%c%s%c]", lyplg_type_get_prefix(pred->key->module, format, prefix_data),
pred->key->name, quot, strval, quot);
}
if (d) {
free((char *)strval);
}
break;
case LY_PATH_PREDTYPE_LEAFLIST:
/* leaf-list-predicate */
strval = pred->value.realtype->plugin->print(path[u].node->module->ctx, &pred->value, format, prefix_data,
&d, NULL);
/* default quote */
quot = '\'';
if (strchr(strval, quot)) {
quot = '"';
}
ret = ly_strcat(&result, "[.=%c%s%c]", quot, strval, quot);
if (d) {
free((char *)strval);
}
break;
case LY_PATH_PREDTYPE_LIST_VAR:
LOGINT(path[u].node->module->ctx);
ret = LY_EINT;
goto cleanup;
}
LY_CHECK_GOTO(ret, cleanup);
}
}
cleanup:
if (local_mod) {
mods->objs[0] = (void *)local_mod;
}
if (ret) {
free(result);
} else {
*str = result;
}
return ret;
}
LIBYANG_API_DEF LY_ERR
lyplg_type_store_instanceid(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
uint32_t options, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node,
struct lyd_value *storage, struct lys_glob_unres *unres, struct ly_err_item **err)
{
LY_ERR ret = LY_SUCCESS;
struct lysc_type_instanceid *type_inst = (struct lysc_type_instanceid *)type;
struct ly_path *path;
char *canon;
/* init storage */
memset(storage, 0, sizeof *storage);
storage->realtype = type;
/* check hints */
ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
LY_CHECK_GOTO(ret, cleanup);
/* compile instance-identifier into path */
if (format == LY_VALUE_LYB) {
/* value in LYB format is the same as in JSON format. */
ret = lyplg_type_lypath_new(ctx, value, value_len, options, LY_VALUE_JSON, prefix_data, ctx_node,
unres, &path, err);
} else {
ret = lyplg_type_lypath_new(ctx, value, value_len, options, format, prefix_data, ctx_node,
unres, &path, err);
}
LY_CHECK_GOTO(ret, cleanup);
/* store value */
storage->target = path;
/* check status */
ret = lyplg_type_lypath_check_status(ctx_node, path, format, prefix_data, err);
LY_CHECK_GOTO(ret, cleanup);
/* store canonical value */
if (format == LY_VALUE_CANON) {
if (options & LYPLG_TYPE_STORE_DYNAMIC) {
ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
options &= ~LYPLG_TYPE_STORE_DYNAMIC;
LY_CHECK_GOTO(ret, cleanup);
} else {
ret = lydict_insert(ctx, value, value_len, &storage->_canonical);
LY_CHECK_GOTO(ret, cleanup);
}
} else {
/* JSON format with prefix is the canonical one */
ret = instanceid_path2str(path, LY_VALUE_JSON, NULL, &canon);
LY_CHECK_GOTO(ret, cleanup);
ret = lydict_insert_zc(ctx, canon, &storage->_canonical);
LY_CHECK_GOTO(ret, cleanup);
}
cleanup:
if (options & LYPLG_TYPE_STORE_DYNAMIC) {
free((void *)value);
}
if (ret) {
lyplg_type_free_instanceid(ctx, storage);
}
if (!ret && type_inst->require_instance) {
/* needs to be resolved */
return LY_EINCOMPLETE;
} else {
return ret;
}
}
LIBYANG_API_DEF LY_ERR
lyplg_type_validate_instanceid(const struct ly_ctx *ctx, const struct lysc_type *UNUSED(type),
const struct lyd_node *ctx_node, const struct lyd_node *tree, struct lyd_value *storage,
struct ly_err_item **err)
{
LY_ERR ret = LY_SUCCESS;
struct lysc_type_instanceid *type_inst = (struct lysc_type_instanceid *)storage->realtype;
const char *value;
char *path;
*err = NULL;
if (!type_inst->require_instance) {
/* redundant to resolve */
return LY_SUCCESS;
}
/* find the target in data */
if ((ret = ly_path_eval(storage->target, tree, NULL, NULL))) {
value = lyd_value_get_canonical(ctx, storage);
path = lyd_path(ctx_node, LYD_PATH_STD, NULL, 0);
return ly_err_new(err, ret, LYVE_DATA, path, strdup("instance-required"), LY_ERRMSG_NOINST, value);
}
return LY_SUCCESS;
}
LIBYANG_API_DEF const void *
lyplg_type_print_instanceid(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
void *prefix_data, ly_bool *dynamic, size_t *value_len)
{
char *ret = NULL;
struct ly_path *p = NULL;
const struct ly_path *target;
struct ly_err_item *err;
if ((format == LY_VALUE_CANON) || (format == LY_VALUE_JSON) || (format == LY_VALUE_LYB)) {
if (dynamic) {
*dynamic = 0;
}
if (value_len) {
*value_len = strlen(value->_canonical);
}
return value->_canonical;
}
if (!value->target) {
/* schema default value, compile it first */
if (lyplg_type_lypath_new(ctx, value->_canonical, strlen(value->_canonical), 0, LY_VALUE_JSON, NULL, NULL,
NULL, &p, &err)) {
if (err) {
ly_err_print(ctx, err);
ly_err_free(err);
}
goto cleanup;
}
target = p;
} else {
target = value->target;
}
/* print the value in the specific format */
if (instanceid_path2str(target, format, prefix_data, &ret)) {
goto cleanup;
}
*dynamic = 1;
if (value_len) {
*value_len = strlen(ret);
}
cleanup:
ly_path_free(p);
return ret;
}
LIBYANG_API_DEF LY_ERR
lyplg_type_dup_instanceid(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
{
LY_ERR ret;
struct ly_err_item *err;
memset(dup, 0, sizeof *dup);
/* canonical value */
ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
LY_CHECK_GOTO(ret, error);
if (!original->target) {
/* schema default value, needs to be compiled */
if (lyplg_type_lypath_new(ctx, original->_canonical, strlen(original->_canonical), 0, LY_VALUE_JSON, NULL, NULL,
NULL, &dup->target, &err)) {
if (err) {
ly_err_print(ctx, err);
ly_err_free(err);
}
goto error;
}
} else {
/* copy path */
ret = ly_path_dup(ctx, original->target, &dup->target);
LY_CHECK_GOTO(ret, error);
}
dup->realtype = original->realtype;
return LY_SUCCESS;
error:
lyplg_type_free_instanceid(ctx, dup);
return ret;
}
LIBYANG_API_DEF void
lyplg_type_free_instanceid(const struct ly_ctx *ctx, struct lyd_value *value)
{
lydict_remove(ctx, value->_canonical);
value->_canonical = NULL;
ly_path_free(value->target);
}
const struct lyplg_type_record plugins_instanceid[] = {
{
.module = "",
.revision = NULL,
.name = LY_TYPE_INST_STR,
.plugin.id = "libyang 2 - instance-identifier, version 1",
.plugin.store = lyplg_type_store_instanceid,
.plugin.validate = lyplg_type_validate_instanceid,
.plugin.compare = lyplg_type_compare_simple,
.plugin.sort = lyplg_type_sort_simple,
.plugin.print = lyplg_type_print_instanceid,
.plugin.duplicate = lyplg_type_dup_instanceid,
.plugin.free = lyplg_type_free_instanceid,
.plugin.lyb_data_len = -1,
},
{0}
};
ly_ctx
libyang context handler.
lydict_insert
LIBYANG_API_DECL LY_ERR lydict_insert(const struct ly_ctx *ctx, const char *value, size_t len, const char **str_p)
Insert string into dictionary. If the string is already present, only a reference counter is incremen...
lydict_remove
LIBYANG_API_DECL LY_ERR lydict_remove(const struct ly_ctx *ctx, const char *value)
Remove specified string from the dictionary. It decrement reference counter for the string and if it ...
lydict_insert_zc
LIBYANG_API_DECL LY_ERR lydict_insert_zc(const struct ly_ctx *ctx, char *value, const char **str_p)
Insert string into dictionary - zerocopy version. If the string is already present,...
ly_err_item::err
LY_ERR err
Definition log.h:284
ly_err_print
LIBYANG_API_DECL void ly_err_print(const struct ly_ctx *ctx, const struct ly_err_item *eitem)
Print the error structure as if just generated.
LY_ERR
LY_ERR
libyang's error codes returned by the libyang functions.
Definition log.h:237
LYVE_DATA
@ LYVE_DATA
Definition log.h:274
LY_EINT
@ LY_EINT
Definition log.h:244
LY_SUCCESS
@ LY_SUCCESS
Definition log.h:238
LY_EINCOMPLETE
@ LY_EINCOMPLETE
Definition log.h:247
ly_err_item
Libyang full error structure.
Definition log.h:282
ly_set
Structure to hold a set of (not necessary somehow connected) objects. Usually used for lyd_node,...
Definition set.h:47
lyplg_type_record::revision
const char *const char * revision
Definition plugins_types.h:638
ly_err_free
LIBYANG_API_DECL LY_ERR LIBYANG_API_DECL void ly_err_free(void *ptr)
Destructor for the error records created with ly_err_new().
lyplg_type_get_prefix
LIBYANG_API_DECL const char * lyplg_type_get_prefix(const struct lys_module *mod, LY_VALUE_FORMAT format, void *prefix_data)
Get format-specific prefix for a module.
lyplg_type_lypath_check_status
LIBYANG_API_DECL LY_ERR lyplg_type_lypath_check_status(const struct lysc_node *ctx_node, const struct ly_path *path, LY_VALUE_FORMAT format, void *prefix_data, struct ly_err_item **err)
Check that the lypath instance-identifier value is allowed based on the status of the nodes.
lyplg_type_check_hints
LIBYANG_API_DECL LY_ERR lyplg_type_check_hints(uint32_t hints, const char *value, size_t value_len, LY_DATA_TYPE type, int *base, struct ly_err_item **err)
Check that the type is suitable for the parser's hints (if any) in the specified format.
ly_err_new
LIBYANG_API_DECL LY_ERR ly_err_new(struct ly_err_item **err, LY_ERR ecode, LY_VECODE vecode, char *data_path, char *apptag, const char *err_format,...) _FORMAT_PRINTF(6
Create and fill error structure.
lyplg_type_lypath_new
LIBYANG_API_DECL LY_ERR lyplg_type_lypath_new(const struct ly_ctx *ctx, const char *value, size_t value_len, uint32_t options, LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *ctx_node, struct lys_glob_unres *unres, struct ly_path **path, struct ly_err_item **err)
Helper function to create internal schema path representation for instance-identifier value represent...
lyplg_type_record
Definition plugins_types.h:635
lyplg_type_store_instanceid
LIBYANG_API_DEF LY_ERR lyplg_type_store_instanceid(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len, uint32_t options, LY_VALUE_FORMAT format, void *prefix_data, uint32_t hints, const struct lysc_node *ctx_node, struct lyd_value *storage, struct lys_glob_unres *unres, struct ly_err_item **err)
Implementation of lyplg_type_store_clb for the built-in instance-identifier type.
Definition instanceid.c:158
lyplg_type_dup_instanceid
LIBYANG_API_DEF LY_ERR lyplg_type_dup_instanceid(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
Implementation of lyplg_type_dup_clb for the built-in instance-identifier type.
Definition instanceid.c:306
lyplg_type_print_instanceid
LIBYANG_API_DEF const void * lyplg_type_print_instanceid(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format, void *prefix_data, ly_bool *dynamic, size_t *value_len)
Implementation of lyplg_type_print_clb for the built-in instance-identifier type.
Definition instanceid.c:256
lyplg_type_free_instanceid
LIBYANG_API_DEF void lyplg_type_free_instanceid(const struct ly_ctx *ctx, struct lyd_value *value)
Implementation of lyplg_type_free_clb for the built-in instance-identifier type.
Definition instanceid.c:342
lyplg_type_sort_simple
LIBYANG_API_DEF int lyplg_type_sort_simple(const struct ly_ctx *ctx, const struct lyd_value *val1, const struct lyd_value *val2)
Implementation of lyplg_type_sort_clb for a generic simple type.
lyplg_type_compare_simple
LIBYANG_API_DECL LY_ERR lyplg_type_compare_simple(const struct ly_ctx *ctx, const struct lyd_value *val1, const struct lyd_value *val2)
Implementation of lyplg_type_compare_clb for a generic simple type.
LYPLG_TYPE_STORE_DYNAMIC
#define LYPLG_TYPE_STORE_DYNAMIC
Definition plugins_types.h:471
lysc_type::basetype
LY_DATA_TYPE basetype
Definition tree_schema.h:1287
lysc_type_instanceid::require_instance
uint8_t require_instance
Definition tree_schema.h:1390
lys_module
Available YANG schema tree structures representing YANG module.
Definition tree_schema.h:2137
lysc_node
Compiled YANG data node.
Definition tree_schema.h:1421
lysc_type
Definition tree_schema.h:1283
lysc_type_instanceid
Definition tree_schema.h:1383
LY_ARRAY_FOR
#define LY_ARRAY_FOR(ARRAY,...)
Sized-array iterator (for-loop).
Definition tree.h:167
LY_VALUE_FORMAT
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition tree.h:234
LY_ARRAY_COUNT_TYPE
#define LY_ARRAY_COUNT_TYPE
Type (i.e. size) of the sized array's size counter.
Definition tree.h:104
LY_VALUE_JSON
@ LY_VALUE_JSON
Definition tree.h:239
LY_VALUE_SCHEMA
@ LY_VALUE_SCHEMA
Definition tree.h:236
LY_VALUE_CANON
@ LY_VALUE_CANON
Definition tree.h:235
LY_VALUE_XML
@ LY_VALUE_XML
Definition tree.h:238
LY_VALUE_STR_NS
@ LY_VALUE_STR_NS
Definition tree.h:241
LY_VALUE_SCHEMA_RESOLVED
@ LY_VALUE_SCHEMA_RESOLVED
Definition tree.h:237
LY_VALUE_LYB
@ LY_VALUE_LYB
Definition tree.h:240
lyplg_type_validate_instanceid
LIBYANG_API_DEF LY_ERR lyplg_type_validate_instanceid(const struct ly_ctx *ctx, const struct lysc_type *UNUSED(type), const struct lyd_node *ctx_node, const struct lyd_node *tree, struct lyd_value *storage, struct ly_err_item **err)
Definition instanceid.c:229
plugins_instanceid
const struct lyplg_type_record plugins_instanceid[]
Plugin information for instance-identifier type implementation.
Definition instanceid.c:356
libyang.h
The main libyang public header.
ly_bool
uint8_t ly_bool
Type to indicate boolean value.
Definition log.h:35
plugins_types.h
API for (user) types plugins.
lyd_value_get_canonical
LIBYANG_API_DECL const char * lyd_value_get_canonical(const struct ly_ctx *ctx, const struct lyd_value *value)
Get the (canonical) value of a lyd_value.
lyd_path
LIBYANG_API_DECL char * lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
Generate path of the given node in the requested format.
LYD_PATH_STD
@ LYD_PATH_STD
Definition tree_data.h:2338
lyd_value::realtype
const struct lysc_type * realtype
Definition tree_data.h:571
lyd_value::_canonical
const char * _canonical
Definition tree_data.h:568
lyd_node
Generic structure for a data node.
Definition tree_data.h:795
lyd_value
YANG data representation.
Definition tree_data.h:567