Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file tree_data_helpers.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Parsing and validation helper functions for data trees |
| 5 | * |
| 6 | * Copyright (c) 2015 - 2018 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | #include "common.h" |
| 15 | |
| 16 | #include <assert.h> |
| 17 | #include <stdlib.h> |
| 18 | |
| 19 | #include "log.h" |
| 20 | #include "dict.h" |
| 21 | #include "plugins_types.h" |
| 22 | #include "tree_data.h" |
| 23 | #include "tree_schema.h" |
| 24 | |
| 25 | struct lyd_node ** |
| 26 | lyd_node_children_p(struct lyd_node *node) |
| 27 | { |
| 28 | assert(node); |
| 29 | switch (node->schema->nodetype) { |
| 30 | case LYS_CONTAINER: |
| 31 | case LYS_LIST: |
| 32 | return &((struct lyd_node_inner*)node)->child; |
| 33 | default: |
| 34 | return NULL; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | API const struct lyd_node * |
| 39 | lyd_node_children(const struct lyd_node *node) |
| 40 | { |
| 41 | struct lyd_node **children; |
| 42 | |
| 43 | if (!node) { |
| 44 | return NULL; |
| 45 | } |
| 46 | |
| 47 | children = lyd_node_children_p((struct lyd_node*)node); |
| 48 | if (children) { |
| 49 | return *children; |
| 50 | } else { |
| 51 | return NULL; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | LY_ERR |
| 56 | lyd_parse_check_options(struct ly_ctx *ctx, int options, const char *func) |
| 57 | { |
| 58 | int x = options & LYD_OPT_TYPEMASK; |
| 59 | |
| 60 | /* LYD_OPT_WHENAUTODEL can be used only with LYD_OPT_DATA or LYD_OPT_CONFIG */ |
| 61 | if (options & LYD_OPT_WHENAUTODEL) { |
| 62 | if ((x == LYD_OPT_EDIT) || (x == LYD_OPT_NOTIF_FILTER)) { |
| 63 | LOGERR(ctx, LY_EINVAL, "%s: Invalid options 0x%x (LYD_OPT_DATA_WHENAUTODEL can be used only with LYD_OPT_DATA or LYD_OPT_CONFIG)", |
| 64 | func, options); |
| 65 | return LY_EINVAL; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | if (options & (LYD_OPT_DATA_ADD_YANGLIB | LYD_OPT_DATA_NO_YANGLIB)) { |
| 70 | if (x != LYD_OPT_DATA) { |
| 71 | LOGERR(ctx, LY_EINVAL, "%s: Invalid options 0x%x (LYD_OPT_DATA_*_YANGLIB can be used only with LYD_OPT_DATA)", |
| 72 | func, options); |
| 73 | return LY_EINVAL; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /* "is power of 2" algorithm, with 0 exception */ |
| 78 | if (x && !(x && !(x & (x - 1)))) { |
| 79 | LOGERR(ctx, LY_EINVAL, "%s: Invalid options 0x%x (multiple data type flags set).", func, options); |
| 80 | return LY_EINVAL; |
| 81 | } |
| 82 | |
| 83 | return LY_SUCCESS; |
| 84 | } |
| 85 | |
| 86 | LY_ERR |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 87 | lyd_value_parse(struct lyd_node_term *node, const char *value, size_t value_len, int dynamic, |
| 88 | ly_type_resolve_prefix get_prefix, void *parser) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 89 | { |
| 90 | LY_ERR ret = LY_SUCCESS; |
| 91 | struct ly_err_item *err = NULL; |
| 92 | struct ly_ctx *ctx; |
| 93 | struct lysc_type *type; |
Radek Krejci | 51cddef | 2019-05-20 15:40:27 +0200 | [diff] [blame] | 94 | void *priv = NULL; |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 95 | int options = LY_TYPE_OPTS_VALIDATE | LY_TYPE_OPTS_CANONIZE | LY_TYPE_OPTS_STORE | (dynamic ? LY_TYPE_OPTS_DYNAMIC : 0); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 96 | assert(node); |
| 97 | |
| 98 | ctx = node->schema->module->ctx; |
| 99 | type = ((struct lysc_node_leaf*)node->schema)->type; |
| 100 | if (type->plugin->validate) { |
Radek Krejci | aca7403 | 2019-06-04 08:53:06 +0200 | [diff] [blame] | 101 | ret = type->plugin->validate(ctx, type, value, value_len, options, get_prefix, parser, &node->value.canonized, &err, &priv); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 102 | if (ret) { |
| 103 | ly_err_print(err); |
| 104 | LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg); |
| 105 | ly_err_free(err); |
Radek Krejci | 51cddef | 2019-05-20 15:40:27 +0200 | [diff] [blame] | 106 | goto error; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 107 | } |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 108 | } else if (dynamic) { |
| 109 | node->value.canonized = lydict_insert_zc(ctx, (char*)value); |
| 110 | } else { |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 111 | node->value.canonized = lydict_insert(ctx, value, value_len); |
| 112 | } |
| 113 | |
Radek Krejci | 5819f7c | 2019-05-31 14:53:29 +0200 | [diff] [blame] | 114 | if (type->plugin->store) { |
Radek Krejci | 51cddef | 2019-05-20 15:40:27 +0200 | [diff] [blame] | 115 | ret = type->plugin->store(ctx, type, options, &node->value, &err, &priv); |
| 116 | if (ret) { |
| 117 | ly_err_print(err); |
| 118 | LOGVAL(ctx, LY_VLOG_STR, err->path, err->vecode, err->msg); |
| 119 | ly_err_free(err); |
| 120 | goto error; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | error: |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 125 | return ret; |
| 126 | } |
| 127 | |