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 | */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 14 | |
| 15 | #include <assert.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 16 | #include <stdint.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 17 | #include <stdlib.h> |
| 18 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 19 | #include "common.h" |
| 20 | #include "context.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 21 | #include "log.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 22 | #include "tree.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 23 | #include "tree_data.h" |
| 24 | #include "tree_schema.h" |
| 25 | |
| 26 | struct lyd_node ** |
| 27 | lyd_node_children_p(struct lyd_node *node) |
| 28 | { |
| 29 | assert(node); |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 30 | |
| 31 | if (!node->schema) { |
| 32 | return &((struct lyd_node_opaq *)node)->child; |
| 33 | } else { |
| 34 | switch (node->schema->nodetype) { |
| 35 | case LYS_CONTAINER: |
| 36 | case LYS_LIST: |
Michal Vasko | 1bf0939 | 2020-03-27 12:38:10 +0100 | [diff] [blame] | 37 | case LYS_RPC: |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 38 | case LYS_ACTION: |
| 39 | case LYS_NOTIF: |
| 40 | return &((struct lyd_node_inner *)node)->child; |
| 41 | default: |
| 42 | return NULL; |
| 43 | } |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | |
Radek Krejci | dae0ee8 | 2020-05-06 16:53:24 +0200 | [diff] [blame] | 47 | API struct lyd_node * |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame^] | 48 | lyd_node_children(const struct lyd_node *node, int options) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 49 | { |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame^] | 50 | struct lyd_node **children, *child; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 51 | |
| 52 | if (!node) { |
| 53 | return NULL; |
| 54 | } |
| 55 | |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame^] | 56 | children = lyd_node_children_p((struct lyd_node *)node); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 57 | if (children) { |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame^] | 58 | child = *children; |
| 59 | if (options & LYD_CHILDREN_SKIP_KEYS) { |
| 60 | while (child && (child->schema->flags & LYS_KEY)) { |
| 61 | child = child->next; |
| 62 | } |
| 63 | } |
| 64 | return child; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 65 | } else { |
| 66 | return NULL; |
| 67 | } |
| 68 | } |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 69 | |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 70 | API const struct lys_module * |
| 71 | lyd_owner_module(const struct lyd_node *node) |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 72 | { |
| 73 | const struct lysc_node *schema; |
| 74 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 75 | if (!node || !node->schema) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 76 | return NULL; |
| 77 | } |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 78 | |
| 79 | for (schema = node->schema; schema->parent; schema = schema->parent); |
| 80 | return schema->module; |
| 81 | } |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 82 | |
| 83 | const struct lys_module * |
| 84 | lyd_mod_next_module(struct lyd_node *tree, const struct lys_module **modules, int mod_count, const struct ly_ctx *ctx, |
| 85 | uint32_t *i, struct lyd_node **first) |
| 86 | { |
| 87 | struct lyd_node *iter; |
| 88 | const struct lys_module *mod; |
| 89 | |
| 90 | /* get the next module */ |
| 91 | if (modules && mod_count) { |
| 92 | if (*i < (unsigned)mod_count) { |
| 93 | mod = modules[(*i)++]; |
| 94 | } else { |
| 95 | mod = NULL; |
| 96 | } |
| 97 | } else { |
| 98 | do { |
| 99 | mod = ly_ctx_get_module_iter(ctx, i); |
| 100 | } while (mod && !mod->implemented); |
| 101 | } |
| 102 | |
| 103 | /* find its data */ |
| 104 | *first = NULL; |
| 105 | if (mod) { |
| 106 | LY_LIST_FOR(tree, iter) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 107 | if (lyd_owner_module(iter) == mod) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 108 | *first = iter; |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return mod; |
| 115 | } |
| 116 | |
| 117 | const struct lys_module * |
| 118 | lyd_data_next_module(struct lyd_node **next, struct lyd_node **first) |
| 119 | { |
| 120 | const struct lys_module *mod; |
| 121 | |
| 122 | if (!*next) { |
| 123 | /* all data traversed */ |
| 124 | *first = NULL; |
| 125 | return NULL; |
| 126 | } |
| 127 | |
| 128 | *first = *next; |
| 129 | |
| 130 | /* prepare next */ |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 131 | mod = lyd_owner_module(*next); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 132 | LY_LIST_FOR(*next, *next) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 133 | if (lyd_owner_module(*next) != mod) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 134 | break; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return mod; |
| 139 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 140 | |
| 141 | LY_ERR |
| 142 | lyd_parse_check_keys(struct lyd_node *node) |
| 143 | { |
| 144 | const struct lysc_node *skey = NULL; |
| 145 | const struct lyd_node *key; |
| 146 | |
| 147 | assert(node->schema->nodetype == LYS_LIST); |
| 148 | |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame^] | 149 | key = lyd_node_children(node, 0); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 150 | while ((skey = lys_getnext(skey, node->schema, NULL, 0)) && (skey->flags & LYS_KEY)) { |
| 151 | if (!key || (key->schema != skey)) { |
| 152 | LOGVAL(node->schema->module->ctx, LY_VLOG_LYD, node, LY_VCODE_NOKEY, skey->name); |
| 153 | return LY_EVALID; |
| 154 | } |
| 155 | |
| 156 | key = key->next; |
| 157 | } |
| 158 | |
| 159 | return LY_SUCCESS; |
| 160 | } |