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 * |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 48 | lyd_node_children(const struct lyd_node *node) |
| 49 | { |
| 50 | struct lyd_node **children; |
| 51 | |
| 52 | if (!node) { |
| 53 | return NULL; |
| 54 | } |
| 55 | |
| 56 | children = lyd_node_children_p((struct lyd_node*)node); |
| 57 | if (children) { |
| 58 | return *children; |
| 59 | } else { |
| 60 | return NULL; |
| 61 | } |
| 62 | } |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 63 | |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 64 | API const struct lys_module * |
| 65 | lyd_owner_module(const struct lyd_node *node) |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 66 | { |
| 67 | const struct lysc_node *schema; |
| 68 | |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 69 | if (!node || !node->schema) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 70 | return NULL; |
| 71 | } |
Michal Vasko | 9b368d3 | 2020-02-14 13:53:31 +0100 | [diff] [blame] | 72 | |
| 73 | for (schema = node->schema; schema->parent; schema = schema->parent); |
| 74 | return schema->module; |
| 75 | } |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 76 | |
| 77 | const struct lys_module * |
| 78 | lyd_mod_next_module(struct lyd_node *tree, const struct lys_module **modules, int mod_count, const struct ly_ctx *ctx, |
| 79 | uint32_t *i, struct lyd_node **first) |
| 80 | { |
| 81 | struct lyd_node *iter; |
| 82 | const struct lys_module *mod; |
| 83 | |
| 84 | /* get the next module */ |
| 85 | if (modules && mod_count) { |
| 86 | if (*i < (unsigned)mod_count) { |
| 87 | mod = modules[(*i)++]; |
| 88 | } else { |
| 89 | mod = NULL; |
| 90 | } |
| 91 | } else { |
| 92 | do { |
| 93 | mod = ly_ctx_get_module_iter(ctx, i); |
| 94 | } while (mod && !mod->implemented); |
| 95 | } |
| 96 | |
| 97 | /* find its data */ |
| 98 | *first = NULL; |
| 99 | if (mod) { |
| 100 | LY_LIST_FOR(tree, iter) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 101 | if (lyd_owner_module(iter) == mod) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 102 | *first = iter; |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return mod; |
| 109 | } |
| 110 | |
| 111 | const struct lys_module * |
| 112 | lyd_data_next_module(struct lyd_node **next, struct lyd_node **first) |
| 113 | { |
| 114 | const struct lys_module *mod; |
| 115 | |
| 116 | if (!*next) { |
| 117 | /* all data traversed */ |
| 118 | *first = NULL; |
| 119 | return NULL; |
| 120 | } |
| 121 | |
| 122 | *first = *next; |
| 123 | |
| 124 | /* prepare next */ |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 125 | mod = lyd_owner_module(*next); |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 126 | LY_LIST_FOR(*next, *next) { |
Michal Vasko | c193ce9 | 2020-03-06 11:04:48 +0100 | [diff] [blame] | 127 | if (lyd_owner_module(*next) != mod) { |
Michal Vasko | b1b5c26 | 2020-03-05 14:29:47 +0100 | [diff] [blame] | 128 | break; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return mod; |
| 133 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 134 | |
| 135 | LY_ERR |
| 136 | lyd_parse_check_keys(struct lyd_node *node) |
| 137 | { |
| 138 | const struct lysc_node *skey = NULL; |
| 139 | const struct lyd_node *key; |
| 140 | |
| 141 | assert(node->schema->nodetype == LYS_LIST); |
| 142 | |
| 143 | key = lyd_node_children(node); |
| 144 | while ((skey = lys_getnext(skey, node->schema, NULL, 0)) && (skey->flags & LYS_KEY)) { |
| 145 | if (!key || (key->schema != skey)) { |
| 146 | LOGVAL(node->schema->module->ctx, LY_VLOG_LYD, node, LY_VCODE_NOKEY, skey->name); |
| 147 | return LY_EVALID; |
| 148 | } |
| 149 | |
| 150 | key = key->next; |
| 151 | } |
| 152 | |
| 153 | return LY_SUCCESS; |
| 154 | } |