blob: 66bf94fe3097be530b1c720b40cbc3dc969b54e7 [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}
56
57LY_ERR
Radek Krejcif3b6fec2019-07-24 15:53:11 +020058lyd_parse_options_check(struct ly_ctx *ctx, int options, const char *func)
Radek Krejcie7b95092019-05-15 11:03:07 +020059{
60 int x = options & LYD_OPT_TYPEMASK;
61
62 /* LYD_OPT_WHENAUTODEL can be used only with LYD_OPT_DATA or LYD_OPT_CONFIG */
63 if (options & LYD_OPT_WHENAUTODEL) {
64 if ((x == LYD_OPT_EDIT) || (x == LYD_OPT_NOTIF_FILTER)) {
65 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)",
66 func, options);
67 return LY_EINVAL;
68 }
69 }
70
71 if (options & (LYD_OPT_DATA_ADD_YANGLIB | LYD_OPT_DATA_NO_YANGLIB)) {
72 if (x != LYD_OPT_DATA) {
73 LOGERR(ctx, LY_EINVAL, "%s: Invalid options 0x%x (LYD_OPT_DATA_*_YANGLIB can be used only with LYD_OPT_DATA)",
74 func, options);
75 return LY_EINVAL;
76 }
77 }
78
79 /* "is power of 2" algorithm, with 0 exception */
80 if (x && !(x && !(x & (x - 1)))) {
81 LOGERR(ctx, LY_EINVAL, "%s: Invalid options 0x%x (multiple data type flags set).", func, options);
82 return LY_EINVAL;
83 }
84
85 return LY_SUCCESS;
86}
Radek Krejcif3b6fec2019-07-24 15:53:11 +020087
88const char *
89lyd_parse_options_type2str(int options)
90{
91 switch (options & LYD_OPT_TYPEMASK) {
92 case LYD_OPT_DATA:
93 return "complete datastore";
94 case LYD_OPT_CONFIG:
95 return "configuration datastore";
96 case LYD_OPT_GET:
97 return "<get> RPC reply";
98 case LYD_OPT_GETCONFIG:
99 return "<get-config> RPC reply";
100 case LYD_OPT_EDIT:
101 return "<edit-config> configuration";
102 case LYD_OPT_RPC:
103 return "RPC/action input";
104 case LYD_OPT_RPCREPLY:
105 return "RPC/action output";
106 case LYD_OPT_NOTIF:
107 return "Notification";
108 case LYD_OPT_NOTIF_FILTER:
109 return "Notification filter";
110 }
111 LOGINT(NULL);
112 return NULL;
113}