Loading...
Searching...
No Matches
identityref.c
Go to the documentation of this file.
#define _GNU_SOURCE /* asprintf */
#include "plugins_types.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "libyang.h"
/* additional internal headers for some useful simple macros */
#include "compat.h"
#include "ly_common.h"
#include "plugins_internal.h" /* LY_TYPE_*_STR */
static LY_ERR
identityref_ident2str(const struct lysc_ident *ident, LY_VALUE_FORMAT format, void *prefix_data, char **str, size_t *str_len)
{
int len;
const char *prefix;
/* get the prefix, may be NULL for no prefix and the default namespace */
prefix = lyplg_type_get_prefix(ident->module, format, prefix_data);
if (prefix) {
len = asprintf(str, "%s:%s", prefix, ident->name);
} else {
len = asprintf(str, "%s", ident->name);
}
if (len == -1) {
return LY_EMEM;
}
if (str_len) {
*str_len = (size_t)len;
}
return LY_SUCCESS;
}
static LY_ERR
identityref_str2ident(const char *value, size_t value_len, LY_VALUE_FORMAT format, void *prefix_data,
const struct ly_ctx *ctx, const struct lysc_node *ctx_node, struct lysc_ident **ident, struct ly_err_item **err)
{
const char *id_name, *prefix = value;
size_t id_len, prefix_len;
const struct lys_module *mod;
LY_ARRAY_COUNT_TYPE u;
struct lysc_ident *id, *identities;
/* locate prefix if any */
for (prefix_len = 0; (prefix_len < value_len) && (value[prefix_len] != ':'); ++prefix_len) {}
if (prefix_len < value_len) {
id_name = &value[prefix_len + 1];
id_len = value_len - (prefix_len + 1);
} else {
prefix_len = 0;
id_name = value;
id_len = value_len;
}
if (!id_len) {
return ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid empty identityref value.");
}
mod = lyplg_type_identity_module(ctx, ctx_node, prefix, prefix_len, format, prefix_data);
if (!mod) {
return ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL,
"Invalid identityref \"%.*s\" value - unable to map prefix to YANG schema.", (int)value_len, value);
}
id = NULL;
identities = mod->identities;
LY_ARRAY_FOR(identities, u) {
if (!ly_strncmp(identities[u].name, id_name, id_len)) {
/* we have match */
id = &identities[u];
break;
}
}
if (!id) {
/* no match */
return ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL,
"Invalid identityref \"%.*s\" value - identity not found in module \"%s\".",
(int)value_len, value, mod->name);
}
*ident = id;
return LY_SUCCESS;
}
static LY_ERR
identityref_check_base(const struct lysc_ident *ident, struct lysc_type_identityref *type, const char *value,
size_t value_len, struct ly_err_item **err)
{
LY_ERR ret;
size_t str_len;
char *str;
LY_ARRAY_COUNT_TYPE u;
struct lysc_ident *base;
/* check that the identity matches some of the type's base identities */
LY_ARRAY_FOR(type->bases, u) {
if (!lyplg_type_identity_isderived(type->bases[u], ident)) {
/* we have match */
break;
}
}
/* it does not, generate a nice error */
if (u == LY_ARRAY_COUNT(type->bases)) {
str = NULL;
str_len = 1;
LY_ARRAY_FOR(type->bases, u) {
base = type->bases[u];
str_len += (u ? 2 : 0) + 1 + strlen(base->module->name) + 1 + strlen(base->name) + 1;
str = ly_realloc(str, str_len);
sprintf(str + (u ? strlen(str) : 0), "%s\"%s:%s\"", u ? ", " : "", base->module->name, base->name);
}
/* no match */
if (u == 1) {
ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL,
"Invalid identityref \"%.*s\" value - identity not derived from the base %s.",
(int)value_len, value, str);
} else {
ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL,
"Invalid identityref \"%.*s\" value - identity not derived from all the bases %s.",
(int)value_len, value, str);
}
free(str);
return ret;
}
return LY_SUCCESS;
}
static LY_ERR
identityref_check_ident(const struct lysc_ident *ident, const char *value,
size_t value_len, uint32_t options, struct lys_glob_unres *unres, struct ly_err_item **err)
{
LY_ERR ret = LY_SUCCESS;
if (!ident->module->implemented) {
if (options & LYPLG_TYPE_STORE_IMPLEMENT) {
ret = lyplg_type_make_implemented(ident->module, NULL, unres);
} else {
ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL,
"Invalid identityref \"%.*s\" value - identity found in non-implemented module \"%s\".",
(int)value_len, (char *)value, ident->module->name);
}
} else if (lys_identity_iffeature_value(ident) == LY_ENOT) {
ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL,
"Invalid identityref \"%.*s\" value - identity is disabled by if-feature.",
(int)value_len, value);
}
return ret;
}
LIBYANG_API_DEF LY_ERR
lyplg_type_store_identityref(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_identityref *type_ident = (struct lysc_type_identityref *)type;
char *canon;
struct lysc_ident *ident = NULL;
/* 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);
/* find a matching identity */
ret = identityref_str2ident(value, value_len, format, prefix_data, ctx, ctx_node, &ident, err);
LY_CHECK_GOTO(ret, cleanup);
/* check if the identity is enabled */
ret = identityref_check_ident(ident, value, value_len, options, unres, err);
LY_CHECK_GOTO(ret, cleanup);
/* check that the identity is derived form all the bases */
ret = identityref_check_base(ident, type_ident, value, value_len, err);
LY_CHECK_GOTO(ret, cleanup);
if (ctx_node) {
/* check status */
ret = lyplg_type_check_status(ctx_node, ident->flags, format, prefix_data, ident->name, err);
LY_CHECK_GOTO(ret, cleanup);
}
/* store value */
storage->ident = ident;
/* 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 */
if (asprintf(&canon, "%s:%s", ident->module->name, ident->name) == -1) {
LOGMEM(ctx);
ret = LY_EMEM;
goto 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_simple(ctx, storage);
}
return ret;
}
LIBYANG_API_DEF LY_ERR
lyplg_type_compare_identityref(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
const struct lyd_value *val2)
{
if (val1->ident == val2->ident) {
return LY_SUCCESS;
}
return LY_ENOT;
}
LIBYANG_API_DEF int
lyplg_type_sort_identityref(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
const struct lyd_value *val2)
{
return strcmp(val1->ident->name, val2->ident->name);
}
LIBYANG_API_DEF const void *
lyplg_type_print_identityref(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *value, LY_VALUE_FORMAT format,
void *prefix_data, ly_bool *dynamic, size_t *value_len)
{
char *ret;
if (format == LY_VALUE_CANON) {
if (dynamic) {
*dynamic = 0;
}
if (value_len) {
*value_len = strlen(value->_canonical);
}
return value->_canonical;
}
/* print the value in the specific format */
if (identityref_ident2str(value->ident, format, prefix_data, &ret, value_len)) {
return NULL;
}
*dynamic = 1;
return ret;
}
const struct lyplg_type_record plugins_identityref[] = {
{
.module = "",
.revision = NULL,
.name = LY_TYPE_IDENT_STR,
.plugin.id = "libyang 2 - identityref, version 1",
.plugin.store = lyplg_type_store_identityref,
.plugin.validate = NULL,
.plugin.compare = lyplg_type_compare_identityref,
.plugin.sort = lyplg_type_sort_identityref,
.plugin.print = lyplg_type_print_identityref,
.plugin.duplicate = lyplg_type_dup_simple,
.plugin.free = lyplg_type_free_simple,
.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_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
LY_ERR
libyang's error codes returned by the libyang functions.
Definition log.h:237
LYVE_DATA
@ LYVE_DATA
Definition log.h:274
LY_EMEM
@ LY_EMEM
Definition log.h:239
LY_ENOT
@ LY_ENOT
Definition log.h:251
LY_EVALID
@ LY_EVALID
Definition log.h:245
LY_SUCCESS
@ LY_SUCCESS
Definition log.h:238
ly_err_item
Libyang full error structure.
Definition log.h:282
lyplg_type_record::revision
const char *const char * revision
Definition plugins_types.h:638
lyplg_type_make_implemented
LIBYANG_API_DECL LY_ERR lyplg_type_make_implemented(struct lys_module *mod, const char **features, struct lys_glob_unres *unres)
Implement a module (just like lys_set_implemented()), but keep maintaining unresolved items.
lyplg_type_check_status
LIBYANG_API_DECL LY_ERR lyplg_type_check_status(const struct lysc_node *ctx_node, uint16_t val_flags, LY_VALUE_FORMAT format, void *prefix_data, const char *val_name, struct ly_err_item **err)
Check that the value of a type is allowed based on its status.
lyplg_type_identity_isderived
LIBYANG_API_DECL LY_ERR lyplg_type_identity_isderived(const struct lysc_ident *base, const struct lysc_ident *derived)
Decide if the derived identity is derived from (based on) the base identity.
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_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_identity_module
LIBYANG_API_DECL const struct lys_module * lyplg_type_identity_module(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const char *prefix, size_t prefix_len, LY_VALUE_FORMAT format, const void *prefix_data)
Get the corresponding module for the identity value.
lyplg_type_record
Definition plugins_types.h:635
lyplg_type_store_identityref
LIBYANG_API_DEF LY_ERR lyplg_type_store_identityref(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 identityref type.
Definition identityref.c:232
lyplg_type_dup_simple
LIBYANG_API_DECL LY_ERR lyplg_type_dup_simple(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
Implementation of lyplg_type_dup_clb for a generic simple type.
lyplg_type_free_simple
LIBYANG_API_DECL void lyplg_type_free_simple(const struct ly_ctx *ctx, struct lyd_value *value)
Implementation of lyplg_type_free_clb for a generic simple type.
LYPLG_TYPE_STORE_DYNAMIC
#define LYPLG_TYPE_STORE_DYNAMIC
Definition plugins_types.h:471
LYPLG_TYPE_STORE_IMPLEMENT
#define LYPLG_TYPE_STORE_IMPLEMENT
Definition plugins_types.h:475
lys_module::name
const char * name
Definition tree_schema.h:2139
lysc_type::basetype
LY_DATA_TYPE basetype
Definition tree_schema.h:1287
lysc_ident::name
const char * name
Definition tree_schema.h:1213
lys_module::identities
struct lysc_ident * identities
Definition tree_schema.h:2153
lysc_ident::flags
uint16_t flags
Definition tree_schema.h:1220
lysc_type_identityref::bases
struct lysc_ident ** bases
Definition tree_schema.h:1379
lys_identity_iffeature_value
LIBYANG_API_DECL LY_ERR lys_identity_iffeature_value(const struct lysc_ident *ident)
Get how the if-feature statement is evaluated for certain identity.
lys_module
Available YANG schema tree structures representing YANG module.
Definition tree_schema.h:2137
lysc_ident
YANG identity-stmt.
Definition tree_schema.h:1212
lysc_node
Compiled YANG data node.
Definition tree_schema.h:1421
lysc_type
Definition tree_schema.h:1283
lysc_type_identityref
Definition tree_schema.h:1372
LY_ARRAY_COUNT
#define LY_ARRAY_COUNT(ARRAY)
Get the number of records in the ARRAY.
Definition tree.h:148
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_CANON
@ LY_VALUE_CANON
Definition tree.h:235
lyplg_type_sort_identityref
LIBYANG_API_DEF int lyplg_type_sort_identityref(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1, const struct lyd_value *val2)
Definition identityref.c:314
lyplg_type_compare_identityref
LIBYANG_API_DEF LY_ERR lyplg_type_compare_identityref(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1, const struct lyd_value *val2)
Definition identityref.c:304
lyplg_type_print_identityref
LIBYANG_API_DEF const void * lyplg_type_print_identityref(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *value, LY_VALUE_FORMAT format, void *prefix_data, ly_bool *dynamic, size_t *value_len)
Definition identityref.c:321
plugins_identityref
const struct lyplg_type_record plugins_identityref[]
Plugin information for identityref type implementation.
Definition identityref.c:351
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::realtype
const struct lysc_type * realtype
Definition tree_data.h:571
lyd_value::_canonical
const char * _canonical
Definition tree_data.h:568
lyd_value
YANG data representation.
Definition tree_data.h:567
ly_realloc
void * ly_realloc(void *ptr, size_t size)
Wrapper for realloc() call. The only difference is that if it fails to allocate the requested memory,...
LOGMEM
#define LOGMEM(CTX)
Definition tree_edit.h:22