blob: 4765b752d8a61c11017ec6c4412bd616b28c551b [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
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
25struct lyd_node **
26lyd_node_children_p(struct lyd_node *node)
27{
28 assert(node);
29 switch (node->schema->nodetype) {
30 case LYS_CONTAINER:
31 case LYS_LIST:
Radek Krejci26a5dfb2019-07-26 14:51:06 +020032 case LYS_ACTION:
33 case LYS_NOTIF:
Radek Krejcie7b95092019-05-15 11:03:07 +020034 return &((struct lyd_node_inner*)node)->child;
35 default:
36 return NULL;
37 }
38}
39
40API const struct lyd_node *
41lyd_node_children(const struct lyd_node *node)
42{
43 struct lyd_node **children;
44
45 if (!node) {
46 return NULL;
47 }
48
49 children = lyd_node_children_p((struct lyd_node*)node);
50 if (children) {
51 return *children;
52 } else {
53 return NULL;
54 }
55}
Michal Vasko9b368d32020-02-14 13:53:31 +010056
57const struct lys_module *
58lyd_top_node_module(const struct lyd_node *node)
59{
60 const struct lysc_node *schema;
61
62 assert(node && !node->parent);
63
64 for (schema = node->schema; schema->parent; schema = schema->parent);
65 return schema->module;
66}
Michal Vaskob1b5c262020-03-05 14:29:47 +010067
68const struct lys_module *
69lyd_mod_next_module(struct lyd_node *tree, const struct lys_module **modules, int mod_count, const struct ly_ctx *ctx,
70 uint32_t *i, struct lyd_node **first)
71{
72 struct lyd_node *iter;
73 const struct lys_module *mod;
74
75 /* get the next module */
76 if (modules && mod_count) {
77 if (*i < (unsigned)mod_count) {
78 mod = modules[(*i)++];
79 } else {
80 mod = NULL;
81 }
82 } else {
83 do {
84 mod = ly_ctx_get_module_iter(ctx, i);
85 } while (mod && !mod->implemented);
86 }
87
88 /* find its data */
89 *first = NULL;
90 if (mod) {
91 LY_LIST_FOR(tree, iter) {
92 if (lyd_top_node_module(iter) == mod) {
93 *first = iter;
94 break;
95 }
96 }
97 }
98
99 return mod;
100}
101
102const struct lys_module *
103lyd_data_next_module(struct lyd_node **next, struct lyd_node **first)
104{
105 const struct lys_module *mod;
106
107 if (!*next) {
108 /* all data traversed */
109 *first = NULL;
110 return NULL;
111 }
112
113 *first = *next;
114
115 /* prepare next */
116 mod = lyd_top_node_module(*next);
117 LY_LIST_FOR(*next, *next) {
118 if (lyd_top_node_module(*next) != mod) {
119 break;
120 }
121 }
122
123 return mod;
124}