blob: 88f9178e41c461b4f0d8224da1d1e16723db59aa [file] [log] [blame]
FredGand944bdc2019-11-05 21:57:07 +08001/**
2 * @file printer_yin.c
3 * @author Fred Gan <ganshaolong@vip.qq.com>
Michal Vaskob26d09d2022-08-22 09:52:19 +02004 * @author Michal Vasko <mvasko@cesnet.cz>
FredGand944bdc2019-11-05 21:57:07 +08005 * @brief YIN printer
6 *
Michal Vaskob26d09d2022-08-22 09:52:19 +02007 * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
FredGand944bdc2019-11-05 21:57:07 +08008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
Radek Krejci535ea9f2020-05-29 16:01:05 +020016#define _GNU_SOURCE
FredGand944bdc2019-11-05 21:57:07 +080017
FredGand944bdc2019-11-05 21:57:07 +080018#include <stdint.h>
19#include <stdio.h>
20#include <stdlib.h>
FredGand944bdc2019-11-05 21:57:07 +080021
Radek Krejci535ea9f2020-05-29 16:01:05 +020022#include "common.h"
Radek Krejciaa45bda2020-07-20 07:43:38 +020023#include "compat.h"
FredGand944bdc2019-11-05 21:57:07 +080024#include "log.h"
Radek Krejci47fab892020-11-05 17:02:41 +010025#include "out.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020026#include "out_internal.h"
FredGand944bdc2019-11-05 21:57:07 +080027#include "printer_internal.h"
28#include "tree.h"
29#include "tree_schema.h"
30#include "tree_schema_internal.h"
FredGand944bdc2019-11-05 21:57:07 +080031#include "xml.h"
Michal Vasko004d3152020-06-11 19:59:22 +020032#include "xpath.h"
FredGand944bdc2019-11-05 21:57:07 +080033
34/**
35 * @brief YIN printer context.
36 */
Radek Krejciadcf63d2021-02-09 10:21:18 +010037struct lys_ypr_ctx {
Radek Krejcif8d7f9a2021-03-10 14:32:36 +010038 union {
39 struct {
40 struct ly_out *out; /**< output specification */
41 uint16_t level; /**< current indentation level: 0 - no formatting, >= 1 indentation levels */
42 uint32_t options; /**< Schema output options (see @ref schemaprinterflags). */
43 const struct lys_module *module; /**< schema to print */
44 };
45 struct lyspr_ctx printer_ctx;
46 };
47
48 /* YIN printer specific members */
FredGand944bdc2019-11-05 21:57:07 +080049};
50
FredGand944bdc2019-11-05 21:57:07 +080051static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +010052ypr_open(struct lys_ypr_ctx *pctx, const char *elem_name, const char *attr_name, const char *attr_value, int8_t flag)
FredGand944bdc2019-11-05 21:57:07 +080053{
Michal Vasko61ad1ff2022-02-10 15:48:39 +010054 ly_print_(pctx->out, "%*s<%s", INDENT, elem_name);
FredGand944bdc2019-11-05 21:57:07 +080055
56 if (attr_name) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +010057 ly_print_(pctx->out, " %s=\"", attr_name);
58 lyxml_dump_text(pctx->out, attr_value, 1);
59 ly_print_(pctx->out, "\"%s", flag == -1 ? "/>\n" : flag == 1 ? ">\n" : "");
FredGand944bdc2019-11-05 21:57:07 +080060 } else if (flag) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +010061 ly_print_(pctx->out, flag == -1 ? "/>\n" : ">\n");
FredGand944bdc2019-11-05 21:57:07 +080062 }
63}
64
65static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +010066ypr_close(struct lys_ypr_ctx *pctx, const char *elem_name, int8_t flag)
FredGand944bdc2019-11-05 21:57:07 +080067{
68 if (flag) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +010069 ly_print_(pctx->out, "%*s</%s>\n", INDENT, elem_name);
FredGand944bdc2019-11-05 21:57:07 +080070 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +010071 ly_print_(pctx->out, "/>\n");
FredGand944bdc2019-11-05 21:57:07 +080072 }
73}
74
75/*
76 * par_close_flag
77 * 0 - parent not yet closed, printing >\n, setting flag to 1
78 * 1 or NULL - parent already closed, do nothing
79 */
80static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +010081ypr_close_parent(struct lys_ypr_ctx *pctx, int8_t *par_close_flag)
FredGand944bdc2019-11-05 21:57:07 +080082{
83 if (par_close_flag && !(*par_close_flag)) {
84 (*par_close_flag) = 1;
Michal Vasko61ad1ff2022-02-10 15:48:39 +010085 ly_print_(pctx->out, ">\n");
FredGand944bdc2019-11-05 21:57:07 +080086 }
87}
88
89static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +010090ypr_yin_arg(struct lys_ypr_ctx *pctx, const char *arg, const char *text)
FredGand944bdc2019-11-05 21:57:07 +080091{
Michal Vasko61ad1ff2022-02-10 15:48:39 +010092 ly_print_(pctx->out, "%*s<%s>", INDENT, arg);
93 lyxml_dump_text(pctx->out, text, 0);
94 ly_print_(pctx->out, "</%s>\n", arg);
FredGand944bdc2019-11-05 21:57:07 +080095}
96
FredGand944bdc2019-11-05 21:57:07 +080097static void
Michal Vaskob26d09d2022-08-22 09:52:19 +020098yprp_stmt(struct lys_ypr_ctx *pctx, struct lysp_stmt *stmt)
99{
100 struct lysp_stmt *childstmt;
101 int8_t flag = stmt->child ? 1 : -1;
102
103 /* TODO:
104 the extension instance substatements in extension instances (LY_STMT_EXTENSION_INSTANCE)
105 cannot find the compiled information, so it is needed to be done,
106 currently it is ignored */
107 if (stmt_attr_info[stmt->kw].name) {
108 if (stmt_attr_info[stmt->kw].flags & STMT_FLAG_YIN) {
109 ypr_open(pctx, stmt->stmt, NULL, NULL, flag);
110 ypr_yin_arg(pctx, stmt_attr_info[stmt->kw].arg, stmt->arg);
111 } else {
112 ypr_open(pctx, stmt->stmt, stmt_attr_info[stmt->kw].arg, stmt->arg, flag);
113 }
114 }
115
116 if (stmt->child) {
117 LEVEL++;
118 LY_LIST_FOR(stmt->child, childstmt) {
119 yprp_stmt(pctx, childstmt);
120 }
121 LEVEL--;
122 ypr_close(pctx, stmt->stmt, flag);
123 }
124}
125
126static void
127yprp_extension_instance(struct lys_ypr_ctx *pctx, enum ly_stmt substmt, uint8_t substmt_index,
128 struct lysp_ext_instance *ext, int8_t *flag)
129{
130 struct lysp_stmt *stmt;
131 int8_t inner_flag;
132 struct lysp_ext *ext_def;
133
134 if ((ext->flags & LYS_INTERNAL) || (ext->parent_stmt != substmt) || (ext->parent_stmt_index != substmt_index)) {
135 return;
136 }
137
138 lysp_ext_find_definition(pctx->module->ctx, ext, NULL, &ext_def);
139 if (!ext_def) {
140 return;
141 }
142
143 ypr_close_parent(pctx, flag);
144 inner_flag = 0;
145
146 if (ext_def->argname) {
147 lysp_ext_instance_resolve_argument(pctx->module->ctx, ext, ext_def);
148 }
149
150 ypr_open(pctx, ext->name, (ext_def->flags & LYS_YINELEM_TRUE) ? NULL : ext_def->argname, ext->argument, inner_flag);
151 LEVEL++;
152 if (ext_def->flags & LYS_YINELEM_TRUE) {
153 const char *prefix, *name, *id;
154 size_t prefix_len, name_len;
155
156 ypr_close_parent(pctx, &inner_flag);
157
158 /* we need to use the same namespace as for the extension instance element */
159 id = ext->name;
160 ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len);
161 ly_print_(pctx->out, "%*s<%.*s:%s>", INDENT, (int)prefix_len, prefix, ext_def->argname);
162 lyxml_dump_text(pctx->out, ext->argument, 0);
163 ly_print_(pctx->out, "</%.*s:%s>\n", (int)prefix_len, prefix, ext_def->argname);
164 }
165 LY_LIST_FOR(ext->child, stmt) {
166 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
167 continue;
168 }
169
170 ypr_close_parent(pctx, &inner_flag);
171 yprp_stmt(pctx, stmt);
172 }
173 LEVEL--;
174 ypr_close(pctx, ext->name, inner_flag);
175}
176
177static void
178yprp_extension_instances(struct lys_ypr_ctx *pctx, enum ly_stmt substmt, uint8_t substmt_index,
179 struct lysp_ext_instance *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800180{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200181 LY_ARRAY_COUNT_TYPE u;
Michal Vaskob26d09d2022-08-22 09:52:19 +0200182
183 LY_ARRAY_FOR(exts, u) {
184 yprp_extension_instance(pctx, substmt, substmt_index, &exts[u], flag);
185 }
186}
187
188static void
189ypr_substmt(struct lys_ypr_ctx *pctx, enum ly_stmt substmt, uint8_t substmt_index, const char *text, void *exts)
190{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200191 int8_t extflag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800192
193 if (!text) {
194 /* nothing to print */
195 return;
196 }
197
Radek Krejcieccf6602021-02-05 19:42:54 +0100198 if (stmt_attr_info[substmt].flags & STMT_FLAG_YIN) {
FredGand944bdc2019-11-05 21:57:07 +0800199 extflag = 1;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100200 ypr_open(pctx, stmt_attr_info[substmt].name, NULL, NULL, extflag);
FredGand944bdc2019-11-05 21:57:07 +0800201 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100202 ypr_open(pctx, stmt_attr_info[substmt].name, stmt_attr_info[substmt].arg, text, extflag);
FredGand944bdc2019-11-05 21:57:07 +0800203 }
204
205 LEVEL++;
Michal Vaskob26d09d2022-08-22 09:52:19 +0200206 yprp_extension_instances(pctx, substmt, substmt_index, exts, &extflag);
FredGand944bdc2019-11-05 21:57:07 +0800207
208 /* argument as yin-element */
Radek Krejcieccf6602021-02-05 19:42:54 +0100209 if (stmt_attr_info[substmt].flags & STMT_FLAG_YIN) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100210 ypr_yin_arg(pctx, stmt_attr_info[substmt].arg, text);
FredGand944bdc2019-11-05 21:57:07 +0800211 }
212
213 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100214 ypr_close(pctx, stmt_attr_info[substmt].name, extflag);
FredGand944bdc2019-11-05 21:57:07 +0800215}
216
217static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100218ypr_unsigned(struct lys_ypr_ctx *pctx, enum ly_stmt substmt, uint8_t substmt_index, void *exts, unsigned long int attr_value)
FredGand944bdc2019-11-05 21:57:07 +0800219{
220 char *str;
Michal Vasko69730152020-10-09 16:30:07 +0200221
Radek Krejci1deb5be2020-08-26 16:43:36 +0200222 if (asprintf(&str, "%lu", attr_value) == -1) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100223 LOGMEM(pctx->module->ctx);
FredGand944bdc2019-11-05 21:57:07 +0800224 return;
225 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100226 ypr_substmt(pctx, substmt, substmt_index, str, exts);
FredGand944bdc2019-11-05 21:57:07 +0800227 free(str);
228}
229
230static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100231ypr_signed(struct lys_ypr_ctx *pctx, enum ly_stmt substmt, uint8_t substmt_index, void *exts, signed long int attr_value)
FredGand944bdc2019-11-05 21:57:07 +0800232{
233 char *str;
234
Radek Krejci1deb5be2020-08-26 16:43:36 +0200235 if (asprintf(&str, "%ld", attr_value) == -1) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100236 LOGMEM(pctx->module->ctx);
FredGand944bdc2019-11-05 21:57:07 +0800237 return;
238 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100239 ypr_substmt(pctx, substmt, substmt_index, str, exts);
FredGand944bdc2019-11-05 21:57:07 +0800240 free(str);
241}
242
243static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100244yprp_revision(struct lys_ypr_ctx *pctx, const struct lysp_revision *rev)
FredGand944bdc2019-11-05 21:57:07 +0800245{
246 if (rev->dsc || rev->ref || rev->exts) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100247 ypr_open(pctx, "revision", "date", rev->date, 1);
FredGand944bdc2019-11-05 21:57:07 +0800248 LEVEL++;
Michal Vaskob26d09d2022-08-22 09:52:19 +0200249 yprp_extension_instances(pctx, LY_STMT_REVISION, 0, rev->exts, NULL);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100250 ypr_substmt(pctx, LY_STMT_DESCRIPTION, 0, rev->dsc, rev->exts);
251 ypr_substmt(pctx, LY_STMT_REFERENCE, 0, rev->ref, rev->exts);
FredGand944bdc2019-11-05 21:57:07 +0800252 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100253 ypr_close(pctx, "revision", 1);
FredGand944bdc2019-11-05 21:57:07 +0800254 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100255 ypr_open(pctx, "revision", "date", rev->date, -1);
FredGand944bdc2019-11-05 21:57:07 +0800256 }
257}
258
259static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100260ypr_mandatory(struct lys_ypr_ctx *pctx, uint16_t flags, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800261{
262 if (flags & LYS_MAND_MASK) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100263 ypr_close_parent(pctx, flag);
264 ypr_substmt(pctx, LY_STMT_MANDATORY, 0, (flags & LYS_MAND_TRUE) ? "true" : "false", exts);
FredGand944bdc2019-11-05 21:57:07 +0800265 }
266}
267
268static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100269ypr_config(struct lys_ypr_ctx *pctx, uint16_t flags, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800270{
271 if (flags & LYS_CONFIG_MASK) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100272 ypr_close_parent(pctx, flag);
273 ypr_substmt(pctx, LY_STMT_CONFIG, 0, (flags & LYS_CONFIG_W) ? "true" : "false", exts);
FredGand944bdc2019-11-05 21:57:07 +0800274 }
275}
276
277static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100278ypr_status(struct lys_ypr_ctx *pctx, uint16_t flags, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800279{
280 const char *status = NULL;
281
282 if (flags & LYS_STATUS_CURR) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100283 ypr_close_parent(pctx, flag);
FredGand944bdc2019-11-05 21:57:07 +0800284 status = "current";
285 } else if (flags & LYS_STATUS_DEPRC) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100286 ypr_close_parent(pctx, flag);
FredGand944bdc2019-11-05 21:57:07 +0800287 status = "deprecated";
288 } else if (flags & LYS_STATUS_OBSLT) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100289 ypr_close_parent(pctx, flag);
FredGand944bdc2019-11-05 21:57:07 +0800290 status = "obsolete";
291 }
292
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100293 ypr_substmt(pctx, LY_STMT_STATUS, 0, status, exts);
FredGand944bdc2019-11-05 21:57:07 +0800294}
295
296static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100297ypr_description(struct lys_ypr_ctx *pctx, const char *dsc, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800298{
299 if (dsc) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100300 ypr_close_parent(pctx, flag);
301 ypr_substmt(pctx, LY_STMT_DESCRIPTION, 0, dsc, exts);
FredGand944bdc2019-11-05 21:57:07 +0800302 }
303}
304
305static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100306ypr_reference(struct lys_ypr_ctx *pctx, const char *ref, void *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800307{
308 if (ref) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100309 ypr_close_parent(pctx, flag);
310 ypr_substmt(pctx, LY_STMT_REFERENCE, 0, ref, exts);
FredGand944bdc2019-11-05 21:57:07 +0800311 }
312}
313
314static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100315yprp_iffeatures(struct lys_ypr_ctx *pctx, struct lysp_qname *iffs, struct lysp_ext_instance *exts, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800316{
Michal Vaskob26d09d2022-08-22 09:52:19 +0200317 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200318 int8_t extflag;
FredGand944bdc2019-11-05 21:57:07 +0800319
Michal Vasko7f45cf22020-10-01 12:49:44 +0200320 LY_ARRAY_FOR(iffs, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100321 ypr_close_parent(pctx, flag);
FredGand944bdc2019-11-05 21:57:07 +0800322 extflag = 0;
323
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100324 ly_print_(pctx->out, "%*s<if-feature name=\"%s", INDENT, iffs[u].str);
FredGand944bdc2019-11-05 21:57:07 +0800325
326 /* extensions */
327 LEVEL++;
Michal Vaskob26d09d2022-08-22 09:52:19 +0200328 yprp_extension_instances(pctx, LY_STMT_IF_FEATURE, u, exts, &extflag);
FredGand944bdc2019-11-05 21:57:07 +0800329 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100330 ly_print_(pctx->out, "\"/>\n");
FredGand944bdc2019-11-05 21:57:07 +0800331 }
332}
333
334static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100335yprp_extension(struct lys_ypr_ctx *pctx, const struct lysp_ext *ext)
FredGand944bdc2019-11-05 21:57:07 +0800336{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200337 int8_t flag = 0, flag2 = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200338 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800339
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100340 ypr_open(pctx, "extension", "name", ext->name, flag);
FredGand944bdc2019-11-05 21:57:07 +0800341 LEVEL++;
342
343 if (ext->exts) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100344 ypr_close_parent(pctx, &flag);
Michal Vaskob26d09d2022-08-22 09:52:19 +0200345 yprp_extension_instances(pctx, LY_STMT_EXTENSION, 0, ext->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800346 }
347
Radek Krejci9f87b0c2021-03-05 14:45:26 +0100348 if (ext->argname) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100349 ypr_close_parent(pctx, &flag);
350 ypr_open(pctx, "argument", "name", ext->argname, flag2);
FredGand944bdc2019-11-05 21:57:07 +0800351
352 LEVEL++;
353 if (ext->exts) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200354 u = -1;
Radek Krejcifc596f92021-02-26 22:40:26 +0100355 while ((u = lysp_ext_instance_iter(ext->exts, u + 1, LY_STMT_ARGUMENT)) != LY_ARRAY_COUNT(ext->exts)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100356 ypr_close_parent(pctx, &flag2);
Michal Vaskob26d09d2022-08-22 09:52:19 +0200357 yprp_extension_instance(pctx, LY_STMT_ARGUMENT, 0, &ext->exts[u], &flag2);
FredGand944bdc2019-11-05 21:57:07 +0800358 }
359 }
360 if ((ext->flags & LYS_YINELEM_MASK) ||
Radek Krejcifc596f92021-02-26 22:40:26 +0100361 (ext->exts && (lysp_ext_instance_iter(ext->exts, 0, LY_STMT_YIN_ELEMENT) != LY_ARRAY_COUNT(ext->exts)))) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100362 ypr_close_parent(pctx, &flag2);
363 ypr_substmt(pctx, LY_STMT_YIN_ELEMENT, 0, (ext->flags & LYS_YINELEM_TRUE) ? "true" : "false", ext->exts);
FredGand944bdc2019-11-05 21:57:07 +0800364 }
365 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100366 ypr_close(pctx, "argument", flag2);
FredGand944bdc2019-11-05 21:57:07 +0800367 }
368
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100369 ypr_status(pctx, ext->flags, ext->exts, &flag);
370 ypr_description(pctx, ext->dsc, ext->exts, &flag);
371 ypr_reference(pctx, ext->ref, ext->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800372
373 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100374 ypr_close(pctx, "extension", flag);
FredGand944bdc2019-11-05 21:57:07 +0800375}
376
377static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100378yprp_feature(struct lys_ypr_ctx *pctx, const struct lysp_feature *feat)
FredGand944bdc2019-11-05 21:57:07 +0800379{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200380 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800381
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100382 ypr_open(pctx, "feature", "name", feat->name, flag);
FredGand944bdc2019-11-05 21:57:07 +0800383 LEVEL++;
Michal Vaskob26d09d2022-08-22 09:52:19 +0200384 yprp_extension_instances(pctx, LY_STMT_FEATURE, 0, feat->exts, &flag);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100385 yprp_iffeatures(pctx, feat->iffeatures, feat->exts, &flag);
386 ypr_status(pctx, feat->flags, feat->exts, &flag);
387 ypr_description(pctx, feat->dsc, feat->exts, &flag);
388 ypr_reference(pctx, feat->ref, feat->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800389 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100390 ypr_close(pctx, "feature", flag);
FredGand944bdc2019-11-05 21:57:07 +0800391}
392
393static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100394yprp_identity(struct lys_ypr_ctx *pctx, const struct lysp_ident *ident)
FredGand944bdc2019-11-05 21:57:07 +0800395{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200396 int8_t flag = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200397 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800398
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100399 ypr_open(pctx, "identity", "name", ident->name, flag);
FredGand944bdc2019-11-05 21:57:07 +0800400 LEVEL++;
401
Michal Vaskob26d09d2022-08-22 09:52:19 +0200402 yprp_extension_instances(pctx, LY_STMT_IDENTITY, 0, ident->exts, &flag);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100403 yprp_iffeatures(pctx, ident->iffeatures, ident->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800404
405 LY_ARRAY_FOR(ident->bases, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100406 ypr_close_parent(pctx, &flag);
407 ypr_substmt(pctx, LY_STMT_BASE, u, ident->bases[u], ident->exts);
FredGand944bdc2019-11-05 21:57:07 +0800408 }
409
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100410 ypr_status(pctx, ident->flags, ident->exts, &flag);
411 ypr_description(pctx, ident->dsc, ident->exts, &flag);
412 ypr_reference(pctx, ident->ref, ident->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800413
414 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100415 ypr_close(pctx, "identity", flag);
FredGand944bdc2019-11-05 21:57:07 +0800416}
417
418static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100419yprp_restr(struct lys_ypr_ctx *pctx, const struct lysp_restr *restr, enum ly_stmt stmt, const char *attr, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800420{
421 (void)flag;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200422 int8_t inner_flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800423
424 if (!restr) {
425 return;
426 }
427
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100428 ly_print_(pctx->out, "%*s<%s %s=\"", INDENT, ly_stmt2str(stmt), attr);
429 lyxml_dump_text(pctx->out,
Radek Krejcif13b87b2020-12-01 22:02:17 +0100430 (restr->arg.str[0] != LYSP_RESTR_PATTERN_NACK && restr->arg.str[0] != LYSP_RESTR_PATTERN_ACK) ?
431 restr->arg.str : &restr->arg.str[1], 1);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100432 ly_print_(pctx->out, "\"");
FredGand944bdc2019-11-05 21:57:07 +0800433
434 LEVEL++;
Michal Vaskob26d09d2022-08-22 09:52:19 +0200435 yprp_extension_instances(pctx, stmt, 0, restr->exts, &inner_flag);
Radek Krejcif13b87b2020-12-01 22:02:17 +0100436 if (restr->arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100437 ypr_close_parent(pctx, &inner_flag);
FredGand944bdc2019-11-05 21:57:07 +0800438 /* special byte value in pattern's expression: 0x15 - invert-match, 0x06 - match */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100439 ypr_substmt(pctx, LY_STMT_MODIFIER, 0, "invert-match", restr->exts);
FredGand944bdc2019-11-05 21:57:07 +0800440 }
441 if (restr->emsg) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100442 ypr_close_parent(pctx, &inner_flag);
443 ypr_substmt(pctx, LY_STMT_ERROR_MESSAGE, 0, restr->emsg, restr->exts);
FredGand944bdc2019-11-05 21:57:07 +0800444 }
445 if (restr->eapptag) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100446 ypr_close_parent(pctx, &inner_flag);
447 ypr_substmt(pctx, LY_STMT_ERROR_APP_TAG, 0, restr->eapptag, restr->exts);
FredGand944bdc2019-11-05 21:57:07 +0800448 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100449 ypr_description(pctx, restr->dsc, restr->exts, &inner_flag);
450 ypr_reference(pctx, restr->ref, restr->exts, &inner_flag);
FredGand944bdc2019-11-05 21:57:07 +0800451
452 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100453 ypr_close(pctx, ly_stmt2str(stmt), inner_flag);
FredGand944bdc2019-11-05 21:57:07 +0800454}
455
456static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100457yprp_when(struct lys_ypr_ctx *pctx, struct lysp_when *when, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800458{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200459 int8_t inner_flag = 0;
Michal Vasko69730152020-10-09 16:30:07 +0200460
FredGand944bdc2019-11-05 21:57:07 +0800461 if (!when) {
462 return;
463 }
464
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100465 ypr_close_parent(pctx, flag);
466 ly_print_(pctx->out, "%*s<when condition=\"", INDENT);
467 lyxml_dump_text(pctx->out, when->cond, 1);
468 ly_print_(pctx->out, "\"");
FredGand944bdc2019-11-05 21:57:07 +0800469
470 LEVEL++;
Michal Vaskob26d09d2022-08-22 09:52:19 +0200471 yprp_extension_instances(pctx, LY_STMT_WHEN, 0, when->exts, &inner_flag);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100472 ypr_description(pctx, when->dsc, when->exts, &inner_flag);
473 ypr_reference(pctx, when->ref, when->exts, &inner_flag);
FredGand944bdc2019-11-05 21:57:07 +0800474 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100475 ypr_close(pctx, "when", inner_flag);
FredGand944bdc2019-11-05 21:57:07 +0800476}
477
478static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100479yprp_enum(struct lys_ypr_ctx *pctx, const struct lysp_type_enum *items, LY_DATA_TYPE type, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800480{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200481 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200482 int8_t inner_flag;
Michal Vasko69730152020-10-09 16:30:07 +0200483
FredGand944bdc2019-11-05 21:57:07 +0800484 (void)flag;
485
486 LY_ARRAY_FOR(items, u) {
487 if (type == LY_TYPE_BITS) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100488 ly_print_(pctx->out, "%*s<bit name=\"", INDENT);
489 lyxml_dump_text(pctx->out, items[u].name, 1);
490 ly_print_(pctx->out, "\"");
FredGand944bdc2019-11-05 21:57:07 +0800491 } else { /* LY_TYPE_ENUM */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100492 ly_print_(pctx->out, "%*s<enum name=\"", INDENT);
493 lyxml_dump_text(pctx->out, items[u].name, 1);
494 ly_print_(pctx->out, "\"");
FredGand944bdc2019-11-05 21:57:07 +0800495 }
496 inner_flag = 0;
497 LEVEL++;
Michal Vaskob26d09d2022-08-22 09:52:19 +0200498 yprp_extension_instances(pctx, LY_STMT_ENUM, 0, items[u].exts, &inner_flag);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100499 yprp_iffeatures(pctx, items[u].iffeatures, items[u].exts, &inner_flag);
FredGand944bdc2019-11-05 21:57:07 +0800500 if (items[u].flags & LYS_SET_VALUE) {
501 if (type == LY_TYPE_BITS) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100502 ypr_close_parent(pctx, &inner_flag);
503 ypr_unsigned(pctx, LY_STMT_POSITION, 0, items[u].exts, items[u].value);
FredGand944bdc2019-11-05 21:57:07 +0800504 } else { /* LY_TYPE_ENUM */
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100505 ypr_close_parent(pctx, &inner_flag);
506 ypr_signed(pctx, LY_STMT_VALUE, 0, items[u].exts, items[u].value);
FredGand944bdc2019-11-05 21:57:07 +0800507 }
508 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100509 ypr_status(pctx, items[u].flags, items[u].exts, &inner_flag);
510 ypr_description(pctx, items[u].dsc, items[u].exts, &inner_flag);
511 ypr_reference(pctx, items[u].ref, items[u].exts, &inner_flag);
FredGand944bdc2019-11-05 21:57:07 +0800512 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100513 ypr_close(pctx, type == LY_TYPE_BITS ? "bit" : "enum", inner_flag);
FredGand944bdc2019-11-05 21:57:07 +0800514 }
515}
516
517static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100518yprp_type(struct lys_ypr_ctx *pctx, const struct lysp_type *type)
FredGand944bdc2019-11-05 21:57:07 +0800519{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200520 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200521 int8_t flag = 0;
Radek Krejci7eb54ba2020-05-18 16:30:04 +0200522
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100523 if (!pctx || !type) {
FredGand944bdc2019-11-05 21:57:07 +0800524 return;
525 }
526
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100527 ypr_open(pctx, "type", "name", type->name, flag);
FredGand944bdc2019-11-05 21:57:07 +0800528 LEVEL++;
529
Michal Vaskob26d09d2022-08-22 09:52:19 +0200530 yprp_extension_instances(pctx, LY_STMT_TYPE, 0, type->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800531
532 if (type->range || type->length || type->patterns || type->bits || type->enums) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100533 ypr_close_parent(pctx, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800534 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100535 yprp_restr(pctx, type->range, LY_STMT_RANGE, "value", &flag);
536 yprp_restr(pctx, type->length, LY_STMT_LENGTH, "value", &flag);
FredGand944bdc2019-11-05 21:57:07 +0800537 LY_ARRAY_FOR(type->patterns, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100538 yprp_restr(pctx, &type->patterns[u], LY_STMT_PATTERN, "value", &flag);
FredGand944bdc2019-11-05 21:57:07 +0800539 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100540 yprp_enum(pctx, type->bits, LY_TYPE_BITS, &flag);
541 yprp_enum(pctx, type->enums, LY_TYPE_ENUM, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800542
543 if (type->path) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100544 ypr_close_parent(pctx, &flag);
545 ypr_substmt(pctx, LY_STMT_PATH, 0, type->path->expr, type->exts);
FredGand944bdc2019-11-05 21:57:07 +0800546 }
547 if (type->flags & LYS_SET_REQINST) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100548 ypr_close_parent(pctx, &flag);
549 ypr_substmt(pctx, LY_STMT_REQUIRE_INSTANCE, 0, type->require_instance ? "true" : "false", type->exts);
FredGand944bdc2019-11-05 21:57:07 +0800550 }
551 if (type->flags & LYS_SET_FRDIGITS) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100552 ypr_close_parent(pctx, &flag);
553 ypr_unsigned(pctx, LY_STMT_FRACTION_DIGITS, 0, type->exts, type->fraction_digits);
FredGand944bdc2019-11-05 21:57:07 +0800554 }
555 LY_ARRAY_FOR(type->bases, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100556 ypr_close_parent(pctx, &flag);
557 ypr_substmt(pctx, LY_STMT_BASE, u, type->bases[u], type->exts);
FredGand944bdc2019-11-05 21:57:07 +0800558 }
559 LY_ARRAY_FOR(type->types, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100560 ypr_close_parent(pctx, &flag);
561 yprp_type(pctx, &type->types[u]);
FredGand944bdc2019-11-05 21:57:07 +0800562 }
563
564 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100565 ypr_close(pctx, "type", flag);
FredGand944bdc2019-11-05 21:57:07 +0800566}
567
568static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100569yprp_typedef(struct lys_ypr_ctx *pctx, const struct lysp_tpdf *tpdf)
FredGand944bdc2019-11-05 21:57:07 +0800570{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100571 ypr_open(pctx, "typedef", "name", tpdf->name, 1);
FredGand944bdc2019-11-05 21:57:07 +0800572 LEVEL++;
573
Michal Vaskob26d09d2022-08-22 09:52:19 +0200574 yprp_extension_instances(pctx, LY_STMT_TYPEDEF, 0, tpdf->exts, NULL);
FredGand944bdc2019-11-05 21:57:07 +0800575
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100576 yprp_type(pctx, &tpdf->type);
FredGand944bdc2019-11-05 21:57:07 +0800577
578 if (tpdf->units) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100579 ypr_substmt(pctx, LY_STMT_UNITS, 0, tpdf->units, tpdf->exts);
FredGand944bdc2019-11-05 21:57:07 +0800580 }
Michal Vasko7f45cf22020-10-01 12:49:44 +0200581 if (tpdf->dflt.str) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100582 ypr_substmt(pctx, LY_STMT_DEFAULT, 0, tpdf->dflt.str, tpdf->exts);
FredGand944bdc2019-11-05 21:57:07 +0800583 }
584
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100585 ypr_status(pctx, tpdf->flags, tpdf->exts, NULL);
586 ypr_description(pctx, tpdf->dsc, tpdf->exts, NULL);
587 ypr_reference(pctx, tpdf->ref, tpdf->exts, NULL);
FredGand944bdc2019-11-05 21:57:07 +0800588
589 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100590 ypr_close(pctx, "typedef", 1);
FredGand944bdc2019-11-05 21:57:07 +0800591}
592
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100593static void yprp_node(struct lys_ypr_ctx *pctx, const struct lysp_node *node);
594static void yprp_action(struct lys_ypr_ctx *pctx, const struct lysp_node_action *action);
595static void yprp_notification(struct lys_ypr_ctx *pctx, const struct lysp_node_notif *notif);
FredGand944bdc2019-11-05 21:57:07 +0800596
597static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100598yprp_grouping(struct lys_ypr_ctx *pctx, const struct lysp_node_grp *grp)
FredGand944bdc2019-11-05 21:57:07 +0800599{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200600 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200601 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800602 struct lysp_node *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100603 struct lysp_node_action *action;
604 struct lysp_node_notif *notif;
605 struct lysp_node_grp *subgrp;
FredGand944bdc2019-11-05 21:57:07 +0800606
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100607 ypr_open(pctx, "grouping", "name", grp->name, flag);
FredGand944bdc2019-11-05 21:57:07 +0800608 LEVEL++;
609
Michal Vaskob26d09d2022-08-22 09:52:19 +0200610 yprp_extension_instances(pctx, LY_STMT_GROUPING, 0, grp->exts, &flag);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100611 ypr_status(pctx, grp->flags, grp->exts, &flag);
612 ypr_description(pctx, grp->dsc, grp->exts, &flag);
613 ypr_reference(pctx, grp->ref, grp->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800614
615 LY_ARRAY_FOR(grp->typedefs, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100616 ypr_close_parent(pctx, &flag);
617 yprp_typedef(pctx, &grp->typedefs[u]);
FredGand944bdc2019-11-05 21:57:07 +0800618 }
619
Radek Krejci2a9fc652021-01-22 17:44:34 +0100620 LY_LIST_FOR(grp->groupings, subgrp) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100621 ypr_close_parent(pctx, &flag);
622 yprp_grouping(pctx, subgrp);
FredGand944bdc2019-11-05 21:57:07 +0800623 }
624
Radek Krejci01180ac2021-01-27 08:48:22 +0100625 LY_LIST_FOR(grp->child, data) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100626 ypr_close_parent(pctx, &flag);
627 yprp_node(pctx, data);
FredGand944bdc2019-11-05 21:57:07 +0800628 }
629
Radek Krejci2a9fc652021-01-22 17:44:34 +0100630 LY_LIST_FOR(grp->actions, action) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100631 ypr_close_parent(pctx, &flag);
632 yprp_action(pctx, action);
Radek Krejci2a9fc652021-01-22 17:44:34 +0100633 }
634
635 LY_LIST_FOR(grp->notifs, notif) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100636 ypr_close_parent(pctx, &flag);
637 yprp_notification(pctx, notif);
FredGand944bdc2019-11-05 21:57:07 +0800638 }
639
640 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100641 ypr_close(pctx, "grouping", flag);
FredGand944bdc2019-11-05 21:57:07 +0800642}
643
644static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100645yprp_inout(struct lys_ypr_ctx *pctx, const struct lysp_node_action_inout *inout, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800646{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200647 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800648 struct lysp_node *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100649 struct lysp_node_grp *grp;
FredGand944bdc2019-11-05 21:57:07 +0800650
Radek Krejci01180ac2021-01-27 08:48:22 +0100651 if (!inout->child) {
Michal Vasko7f45cf22020-10-01 12:49:44 +0200652 /* input/output is empty */
FredGand944bdc2019-11-05 21:57:07 +0800653 return;
654 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100655 ypr_close_parent(pctx, flag);
FredGand944bdc2019-11-05 21:57:07 +0800656
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100657 ypr_open(pctx, inout->name, NULL, NULL, *flag);
FredGand944bdc2019-11-05 21:57:07 +0800658 LEVEL++;
659
Michal Vaskob26d09d2022-08-22 09:52:19 +0200660 yprp_extension_instances(pctx, lys_nodetype2stmt(inout->nodetype), 0, inout->exts, NULL);
FredGand944bdc2019-11-05 21:57:07 +0800661 LY_ARRAY_FOR(inout->musts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100662 yprp_restr(pctx, &inout->musts[u], LY_STMT_MUST, "condition", NULL);
FredGand944bdc2019-11-05 21:57:07 +0800663 }
664 LY_ARRAY_FOR(inout->typedefs, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100665 yprp_typedef(pctx, &inout->typedefs[u]);
FredGand944bdc2019-11-05 21:57:07 +0800666 }
Radek Krejci2a9fc652021-01-22 17:44:34 +0100667 LY_LIST_FOR(inout->groupings, grp) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100668 yprp_grouping(pctx, grp);
FredGand944bdc2019-11-05 21:57:07 +0800669 }
670
Radek Krejci01180ac2021-01-27 08:48:22 +0100671 LY_LIST_FOR(inout->child, data) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100672 yprp_node(pctx, data);
FredGand944bdc2019-11-05 21:57:07 +0800673 }
674
675 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100676 ypr_close(pctx, inout->name, 1);
FredGand944bdc2019-11-05 21:57:07 +0800677}
678
679static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100680yprp_notification(struct lys_ypr_ctx *pctx, const struct lysp_node_notif *notif)
FredGand944bdc2019-11-05 21:57:07 +0800681{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200682 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200683 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800684 struct lysp_node *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100685 struct lysp_node_grp *grp;
FredGand944bdc2019-11-05 21:57:07 +0800686
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100687 ypr_open(pctx, "notification", "name", notif->name, flag);
FredGand944bdc2019-11-05 21:57:07 +0800688
689 LEVEL++;
Michal Vaskob26d09d2022-08-22 09:52:19 +0200690 yprp_extension_instances(pctx, LY_STMT_NOTIFICATION, 0, notif->exts, &flag);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100691 yprp_iffeatures(pctx, notif->iffeatures, notif->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800692
693 LY_ARRAY_FOR(notif->musts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100694 ypr_close_parent(pctx, &flag);
695 yprp_restr(pctx, &notif->musts[u], LY_STMT_MUST, "condition", &flag);
FredGand944bdc2019-11-05 21:57:07 +0800696 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100697 ypr_status(pctx, notif->flags, notif->exts, &flag);
698 ypr_description(pctx, notif->dsc, notif->exts, &flag);
699 ypr_reference(pctx, notif->ref, notif->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800700
701 LY_ARRAY_FOR(notif->typedefs, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100702 ypr_close_parent(pctx, &flag);
703 yprp_typedef(pctx, &notif->typedefs[u]);
FredGand944bdc2019-11-05 21:57:07 +0800704 }
705
Radek Krejci2a9fc652021-01-22 17:44:34 +0100706 LY_LIST_FOR(notif->groupings, grp) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100707 ypr_close_parent(pctx, &flag);
708 yprp_grouping(pctx, grp);
FredGand944bdc2019-11-05 21:57:07 +0800709 }
710
Radek Krejci01180ac2021-01-27 08:48:22 +0100711 LY_LIST_FOR(notif->child, data) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100712 ypr_close_parent(pctx, &flag);
713 yprp_node(pctx, data);
FredGand944bdc2019-11-05 21:57:07 +0800714 }
715
716 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100717 ypr_close(pctx, "notification", flag);
FredGand944bdc2019-11-05 21:57:07 +0800718}
719
720static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100721yprp_action(struct lys_ypr_ctx *pctx, const struct lysp_node_action *action)
FredGand944bdc2019-11-05 21:57:07 +0800722{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200723 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200724 int8_t flag = 0;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100725 struct lysp_node_grp *grp;
FredGand944bdc2019-11-05 21:57:07 +0800726
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100727 ypr_open(pctx, action->parent ? "action" : "rpc", "name", action->name, flag);
FredGand944bdc2019-11-05 21:57:07 +0800728
729 LEVEL++;
Michal Vaskob26d09d2022-08-22 09:52:19 +0200730 yprp_extension_instances(pctx, lys_nodetype2stmt(action->nodetype), 0, action->exts, &flag);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100731 yprp_iffeatures(pctx, action->iffeatures, action->exts, &flag);
732 ypr_status(pctx, action->flags, action->exts, &flag);
733 ypr_description(pctx, action->dsc, action->exts, &flag);
734 ypr_reference(pctx, action->ref, action->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800735
736 LY_ARRAY_FOR(action->typedefs, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100737 ypr_close_parent(pctx, &flag);
738 yprp_typedef(pctx, &action->typedefs[u]);
FredGand944bdc2019-11-05 21:57:07 +0800739 }
740
Radek Krejci2a9fc652021-01-22 17:44:34 +0100741 LY_LIST_FOR(action->groupings, grp) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100742 ypr_close_parent(pctx, &flag);
743 yprp_grouping(pctx, grp);
FredGand944bdc2019-11-05 21:57:07 +0800744 }
745
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100746 yprp_inout(pctx, &action->input, &flag);
747 yprp_inout(pctx, &action->output, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800748
749 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100750 ypr_close(pctx, action->parent ? "action" : "rpc", flag);
FredGand944bdc2019-11-05 21:57:07 +0800751}
752
753static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100754yprp_node_common1(struct lys_ypr_ctx *pctx, const struct lysp_node *node, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800755{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100756 ypr_open(pctx, lys_nodetype2str(node->nodetype), "name", node->name, *flag);
FredGand944bdc2019-11-05 21:57:07 +0800757 LEVEL++;
758
Michal Vaskob26d09d2022-08-22 09:52:19 +0200759 yprp_extension_instances(pctx, lys_nodetype2stmt(node->nodetype), 0, node->exts, flag);
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100760 yprp_when(pctx, lysp_node_when(node), flag);
761 yprp_iffeatures(pctx, node->iffeatures, node->exts, flag);
FredGand944bdc2019-11-05 21:57:07 +0800762}
763
764static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100765yprp_node_common2(struct lys_ypr_ctx *pctx, const struct lysp_node *node, int8_t *flag)
FredGand944bdc2019-11-05 21:57:07 +0800766{
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100767 ypr_config(pctx, node->flags, node->exts, flag);
FredGand944bdc2019-11-05 21:57:07 +0800768 if (node->nodetype & (LYS_CHOICE | LYS_LEAF | LYS_ANYDATA)) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100769 ypr_mandatory(pctx, node->flags, node->exts, flag);
FredGand944bdc2019-11-05 21:57:07 +0800770 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100771 ypr_status(pctx, node->flags, node->exts, flag);
772 ypr_description(pctx, node->dsc, node->exts, flag);
773 ypr_reference(pctx, node->ref, node->exts, flag);
FredGand944bdc2019-11-05 21:57:07 +0800774}
775
776static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100777yprp_container(struct lys_ypr_ctx *pctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +0800778{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200779 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200780 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800781 struct lysp_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100782 struct lysp_node_action *action;
783 struct lysp_node_notif *notif;
784 struct lysp_node_grp *grp;
FredGand944bdc2019-11-05 21:57:07 +0800785 struct lysp_node_container *cont = (struct lysp_node_container *)node;
786
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100787 yprp_node_common1(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800788
789 LY_ARRAY_FOR(cont->musts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100790 ypr_close_parent(pctx, &flag);
791 yprp_restr(pctx, &cont->musts[u], LY_STMT_MUST, "condition", &flag);
FredGand944bdc2019-11-05 21:57:07 +0800792 }
793 if (cont->presence) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100794 ypr_close_parent(pctx, &flag);
795 ypr_substmt(pctx, LY_STMT_PRESENCE, 0, cont->presence, cont->exts);
FredGand944bdc2019-11-05 21:57:07 +0800796 }
797
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100798 yprp_node_common2(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800799
800 LY_ARRAY_FOR(cont->typedefs, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100801 ypr_close_parent(pctx, &flag);
802 yprp_typedef(pctx, &cont->typedefs[u]);
FredGand944bdc2019-11-05 21:57:07 +0800803 }
804
Radek Krejci2a9fc652021-01-22 17:44:34 +0100805 LY_LIST_FOR(cont->groupings, grp) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100806 ypr_close_parent(pctx, &flag);
807 yprp_grouping(pctx, grp);
FredGand944bdc2019-11-05 21:57:07 +0800808 }
809
810 LY_LIST_FOR(cont->child, child) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100811 ypr_close_parent(pctx, &flag);
812 yprp_node(pctx, child);
FredGand944bdc2019-11-05 21:57:07 +0800813 }
814
Radek Krejci2a9fc652021-01-22 17:44:34 +0100815 LY_LIST_FOR(cont->actions, action) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100816 ypr_close_parent(pctx, &flag);
817 yprp_action(pctx, action);
FredGand944bdc2019-11-05 21:57:07 +0800818 }
819
Radek Krejci2a9fc652021-01-22 17:44:34 +0100820 LY_LIST_FOR(cont->notifs, notif) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100821 ypr_close_parent(pctx, &flag);
822 yprp_notification(pctx, notif);
FredGand944bdc2019-11-05 21:57:07 +0800823 }
824
825 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100826 ypr_close(pctx, "container", flag);
FredGand944bdc2019-11-05 21:57:07 +0800827}
828
829static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100830yprp_case(struct lys_ypr_ctx *pctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +0800831{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200832 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800833 struct lysp_node *child;
834 struct lysp_node_case *cas = (struct lysp_node_case *)node;
835
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100836 yprp_node_common1(pctx, node, &flag);
837 yprp_node_common2(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800838
839 LY_LIST_FOR(cas->child, child) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100840 ypr_close_parent(pctx, &flag);
841 yprp_node(pctx, child);
FredGand944bdc2019-11-05 21:57:07 +0800842 }
843
844 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100845 ypr_close(pctx, "case", flag);
FredGand944bdc2019-11-05 21:57:07 +0800846}
847
848static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100849yprp_choice(struct lys_ypr_ctx *pctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +0800850{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200851 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800852 struct lysp_node *child;
853 struct lysp_node_choice *choice = (struct lysp_node_choice *)node;
854
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100855 yprp_node_common1(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800856
Michal Vasko7f45cf22020-10-01 12:49:44 +0200857 if (choice->dflt.str) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100858 ypr_close_parent(pctx, &flag);
859 ypr_substmt(pctx, LY_STMT_DEFAULT, 0, choice->dflt.str, choice->exts);
FredGand944bdc2019-11-05 21:57:07 +0800860 }
861
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100862 yprp_node_common2(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800863
864 LY_LIST_FOR(choice->child, child) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100865 ypr_close_parent(pctx, &flag);
866 yprp_node(pctx, child);
FredGand944bdc2019-11-05 21:57:07 +0800867 }
868
869 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100870 ypr_close(pctx, "choice", flag);
FredGand944bdc2019-11-05 21:57:07 +0800871}
872
873static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100874yprp_leaf(struct lys_ypr_ctx *pctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +0800875{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200876 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800877 struct lysp_node_leaf *leaf = (struct lysp_node_leaf *)node;
878
Radek Krejci1deb5be2020-08-26 16:43:36 +0200879 int8_t flag = 1;
Michal Vasko69730152020-10-09 16:30:07 +0200880
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100881 yprp_node_common1(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800882
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100883 yprp_type(pctx, &leaf->type);
884 ypr_substmt(pctx, LY_STMT_UNITS, 0, leaf->units, leaf->exts);
FredGand944bdc2019-11-05 21:57:07 +0800885 LY_ARRAY_FOR(leaf->musts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100886 yprp_restr(pctx, &leaf->musts[u], LY_STMT_MUST, "condition", &flag);
FredGand944bdc2019-11-05 21:57:07 +0800887 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100888 ypr_substmt(pctx, LY_STMT_DEFAULT, 0, leaf->dflt.str, leaf->exts);
FredGand944bdc2019-11-05 21:57:07 +0800889
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100890 yprp_node_common2(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800891
892 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100893 ypr_close(pctx, "leaf", flag);
FredGand944bdc2019-11-05 21:57:07 +0800894}
895
896static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100897yprp_leaflist(struct lys_ypr_ctx *pctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +0800898{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200899 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +0800900 struct lysp_node_leaflist *llist = (struct lysp_node_leaflist *)node;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200901 int8_t flag = 1;
FredGand944bdc2019-11-05 21:57:07 +0800902
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100903 yprp_node_common1(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800904
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100905 yprp_type(pctx, &llist->type);
906 ypr_substmt(pctx, LY_STMT_UNITS, 0, llist->units, llist->exts);
FredGand944bdc2019-11-05 21:57:07 +0800907 LY_ARRAY_FOR(llist->musts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100908 yprp_restr(pctx, &llist->musts[u], LY_STMT_MUST, "condition", NULL);
FredGand944bdc2019-11-05 21:57:07 +0800909 }
910 LY_ARRAY_FOR(llist->dflts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100911 ypr_substmt(pctx, LY_STMT_DEFAULT, u, llist->dflts[u].str, llist->exts);
FredGand944bdc2019-11-05 21:57:07 +0800912 }
913
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100914 ypr_config(pctx, node->flags, node->exts, NULL);
FredGand944bdc2019-11-05 21:57:07 +0800915
916 if (llist->flags & LYS_SET_MIN) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100917 ypr_unsigned(pctx, LY_STMT_MIN_ELEMENTS, 0, llist->exts, llist->min);
FredGand944bdc2019-11-05 21:57:07 +0800918 }
919 if (llist->flags & LYS_SET_MAX) {
920 if (llist->max) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100921 ypr_unsigned(pctx, LY_STMT_MAX_ELEMENTS, 0, llist->exts, llist->max);
FredGand944bdc2019-11-05 21:57:07 +0800922 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100923 ypr_substmt(pctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", llist->exts);
FredGand944bdc2019-11-05 21:57:07 +0800924 }
925 }
926
927 if (llist->flags & LYS_ORDBY_MASK) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100928 ypr_substmt(pctx, LY_STMT_ORDERED_BY, 0, (llist->flags & LYS_ORDBY_USER) ? "user" : "system", llist->exts);
FredGand944bdc2019-11-05 21:57:07 +0800929 }
930
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100931 ypr_status(pctx, node->flags, node->exts, &flag);
932 ypr_description(pctx, node->dsc, node->exts, &flag);
933 ypr_reference(pctx, node->ref, node->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800934
935 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100936 ypr_close(pctx, "leaf-list", flag);
FredGand944bdc2019-11-05 21:57:07 +0800937}
938
939static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100940yprp_list(struct lys_ypr_ctx *pctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +0800941{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200942 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +0200943 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +0800944 struct lysp_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +0100945 struct lysp_node_action *action;
946 struct lysp_node_notif *notif;
947 struct lysp_node_grp *grp;
FredGand944bdc2019-11-05 21:57:07 +0800948 struct lysp_node_list *list = (struct lysp_node_list *)node;
949
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100950 yprp_node_common1(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800951
952 LY_ARRAY_FOR(list->musts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100953 ypr_close_parent(pctx, &flag);
954 yprp_restr(pctx, &list->musts[u], LY_STMT_MUST, "condition", &flag);
FredGand944bdc2019-11-05 21:57:07 +0800955 }
956 if (list->key) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100957 ypr_close_parent(pctx, &flag);
958 ypr_substmt(pctx, LY_STMT_KEY, 0, list->key, list->exts);
FredGand944bdc2019-11-05 21:57:07 +0800959 }
960 LY_ARRAY_FOR(list->uniques, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100961 ypr_close_parent(pctx, &flag);
962 ypr_substmt(pctx, LY_STMT_UNIQUE, u, list->uniques[u].str, list->exts);
FredGand944bdc2019-11-05 21:57:07 +0800963 }
964
Michal Vasko846e3ab2022-03-01 09:54:27 +0100965 ypr_config(pctx, node->flags, node->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800966
967 if (list->flags & LYS_SET_MIN) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100968 ypr_unsigned(pctx, LY_STMT_MIN_ELEMENTS, 0, list->exts, list->min);
FredGand944bdc2019-11-05 21:57:07 +0800969 }
970 if (list->flags & LYS_SET_MAX) {
971 if (list->max) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100972 ypr_unsigned(pctx, LY_STMT_MAX_ELEMENTS, 0, list->exts, list->max);
FredGand944bdc2019-11-05 21:57:07 +0800973 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100974 ypr_substmt(pctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", list->exts);
FredGand944bdc2019-11-05 21:57:07 +0800975 }
976 }
977
978 if (list->flags & LYS_ORDBY_MASK) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100979 ypr_close_parent(pctx, &flag);
980 ypr_substmt(pctx, LY_STMT_ORDERED_BY, 0, (list->flags & LYS_ORDBY_USER) ? "user" : "system", list->exts);
FredGand944bdc2019-11-05 21:57:07 +0800981 }
982
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100983 ypr_status(pctx, node->flags, node->exts, &flag);
984 ypr_description(pctx, node->dsc, node->exts, &flag);
985 ypr_reference(pctx, node->ref, node->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +0800986
987 LY_ARRAY_FOR(list->typedefs, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100988 ypr_close_parent(pctx, &flag);
989 yprp_typedef(pctx, &list->typedefs[u]);
FredGand944bdc2019-11-05 21:57:07 +0800990 }
991
Radek Krejci2a9fc652021-01-22 17:44:34 +0100992 LY_LIST_FOR(list->groupings, grp) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100993 ypr_close_parent(pctx, &flag);
994 yprp_grouping(pctx, grp);
FredGand944bdc2019-11-05 21:57:07 +0800995 }
996
997 LY_LIST_FOR(list->child, child) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +0100998 ypr_close_parent(pctx, &flag);
999 yprp_node(pctx, child);
FredGand944bdc2019-11-05 21:57:07 +08001000 }
1001
Radek Krejci2a9fc652021-01-22 17:44:34 +01001002 LY_LIST_FOR(list->actions, action) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001003 ypr_close_parent(pctx, &flag);
1004 yprp_action(pctx, action);
FredGand944bdc2019-11-05 21:57:07 +08001005 }
1006
Radek Krejci2a9fc652021-01-22 17:44:34 +01001007 LY_LIST_FOR(list->notifs, notif) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001008 ypr_close_parent(pctx, &flag);
1009 yprp_notification(pctx, notif);
FredGand944bdc2019-11-05 21:57:07 +08001010 }
1011
1012 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001013 ypr_close(pctx, "list", flag);
FredGand944bdc2019-11-05 21:57:07 +08001014}
1015
1016static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001017yprp_refine(struct lys_ypr_ctx *pctx, struct lysp_refine *refine)
FredGand944bdc2019-11-05 21:57:07 +08001018{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001019 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001020 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +08001021
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001022 ypr_open(pctx, "refine", "target-node", refine->nodeid, flag);
FredGand944bdc2019-11-05 21:57:07 +08001023 LEVEL++;
1024
Michal Vaskob26d09d2022-08-22 09:52:19 +02001025 yprp_extension_instances(pctx, LY_STMT_REFINE, 0, refine->exts, &flag);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001026 yprp_iffeatures(pctx, refine->iffeatures, refine->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +08001027
1028 LY_ARRAY_FOR(refine->musts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001029 ypr_close_parent(pctx, &flag);
1030 yprp_restr(pctx, &refine->musts[u], LY_STMT_MUST, "condition", &flag);
FredGand944bdc2019-11-05 21:57:07 +08001031 }
1032
1033 if (refine->presence) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001034 ypr_close_parent(pctx, &flag);
1035 ypr_substmt(pctx, LY_STMT_PRESENCE, 0, refine->presence, refine->exts);
FredGand944bdc2019-11-05 21:57:07 +08001036 }
1037
1038 LY_ARRAY_FOR(refine->dflts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001039 ypr_close_parent(pctx, &flag);
1040 ypr_substmt(pctx, LY_STMT_DEFAULT, u, refine->dflts[u].str, refine->exts);
FredGand944bdc2019-11-05 21:57:07 +08001041 }
1042
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001043 ypr_config(pctx, refine->flags, refine->exts, &flag);
1044 ypr_mandatory(pctx, refine->flags, refine->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +08001045
1046 if (refine->flags & LYS_SET_MIN) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001047 ypr_close_parent(pctx, &flag);
1048 ypr_unsigned(pctx, LY_STMT_MIN_ELEMENTS, 0, refine->exts, refine->min);
FredGand944bdc2019-11-05 21:57:07 +08001049 }
1050 if (refine->flags & LYS_SET_MAX) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001051 ypr_close_parent(pctx, &flag);
FredGand944bdc2019-11-05 21:57:07 +08001052 if (refine->max) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001053 ypr_unsigned(pctx, LY_STMT_MAX_ELEMENTS, 0, refine->exts, refine->max);
FredGand944bdc2019-11-05 21:57:07 +08001054 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001055 ypr_substmt(pctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", refine->exts);
FredGand944bdc2019-11-05 21:57:07 +08001056 }
1057 }
1058
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001059 ypr_description(pctx, refine->dsc, refine->exts, &flag);
1060 ypr_reference(pctx, refine->ref, refine->exts, &flag);
FredGand944bdc2019-11-05 21:57:07 +08001061
1062 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001063 ypr_close(pctx, "refine", flag);
FredGand944bdc2019-11-05 21:57:07 +08001064}
1065
1066static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001067yprp_augment(struct lys_ypr_ctx *pctx, const struct lysp_node_augment *aug)
FredGand944bdc2019-11-05 21:57:07 +08001068{
FredGand944bdc2019-11-05 21:57:07 +08001069 struct lysp_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001070 struct lysp_node_action *action;
1071 struct lysp_node_notif *notif;
FredGand944bdc2019-11-05 21:57:07 +08001072
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001073 ypr_open(pctx, "augment", "target-node", aug->nodeid, 1);
FredGand944bdc2019-11-05 21:57:07 +08001074 LEVEL++;
1075
Michal Vaskob26d09d2022-08-22 09:52:19 +02001076 yprp_extension_instances(pctx, LY_STMT_AUGMENT, 0, aug->exts, NULL);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001077 yprp_when(pctx, aug->when, NULL);
1078 yprp_iffeatures(pctx, aug->iffeatures, aug->exts, NULL);
1079 ypr_status(pctx, aug->flags, aug->exts, NULL);
1080 ypr_description(pctx, aug->dsc, aug->exts, NULL);
1081 ypr_reference(pctx, aug->ref, aug->exts, NULL);
FredGand944bdc2019-11-05 21:57:07 +08001082
1083 LY_LIST_FOR(aug->child, child) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001084 yprp_node(pctx, child);
FredGand944bdc2019-11-05 21:57:07 +08001085 }
1086
Radek Krejci2a9fc652021-01-22 17:44:34 +01001087 LY_LIST_FOR(aug->actions, action) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001088 yprp_action(pctx, action);
FredGand944bdc2019-11-05 21:57:07 +08001089 }
1090
Radek Krejci2a9fc652021-01-22 17:44:34 +01001091 LY_LIST_FOR(aug->notifs, notif) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001092 yprp_notification(pctx, notif);
FredGand944bdc2019-11-05 21:57:07 +08001093 }
1094
1095 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001096 ypr_close(pctx, "augment", 1);
FredGand944bdc2019-11-05 21:57:07 +08001097}
1098
FredGand944bdc2019-11-05 21:57:07 +08001099static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001100yprp_uses(struct lys_ypr_ctx *pctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +08001101{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001102 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001103 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +08001104 struct lysp_node_uses *uses = (struct lysp_node_uses *)node;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001105 struct lysp_node_augment *aug;
FredGand944bdc2019-11-05 21:57:07 +08001106
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001107 yprp_node_common1(pctx, node, &flag);
1108 yprp_node_common2(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +08001109
1110 LY_ARRAY_FOR(uses->refines, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001111 ypr_close_parent(pctx, &flag);
1112 yprp_refine(pctx, &uses->refines[u]);
FredGand944bdc2019-11-05 21:57:07 +08001113 }
1114
Radek Krejci2a9fc652021-01-22 17:44:34 +01001115 LY_LIST_FOR(uses->augments, aug) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001116 ypr_close_parent(pctx, &flag);
1117 yprp_augment(pctx, aug);
FredGand944bdc2019-11-05 21:57:07 +08001118 }
1119
1120 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001121 ypr_close(pctx, "uses", flag);
FredGand944bdc2019-11-05 21:57:07 +08001122}
1123
1124static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001125yprp_anydata(struct lys_ypr_ctx *pctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +08001126{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001127 LY_ARRAY_COUNT_TYPE u;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001128 int8_t flag = 0;
FredGand944bdc2019-11-05 21:57:07 +08001129 struct lysp_node_anydata *any = (struct lysp_node_anydata *)node;
1130
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001131 yprp_node_common1(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +08001132
1133 LY_ARRAY_FOR(any->musts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001134 ypr_close_parent(pctx, &flag);
1135 yprp_restr(pctx, &any->musts[u], LY_STMT_MUST, "condition", &flag);
FredGand944bdc2019-11-05 21:57:07 +08001136 }
1137
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001138 yprp_node_common2(pctx, node, &flag);
FredGand944bdc2019-11-05 21:57:07 +08001139
1140 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001141 ypr_close(pctx, lys_nodetype2str(node->nodetype), flag);
FredGand944bdc2019-11-05 21:57:07 +08001142}
1143
1144static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001145yprp_node(struct lys_ypr_ctx *pctx, const struct lysp_node *node)
FredGand944bdc2019-11-05 21:57:07 +08001146{
FredGand944bdc2019-11-05 21:57:07 +08001147 switch (node->nodetype) {
1148 case LYS_CONTAINER:
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001149 yprp_container(pctx, node);
FredGand944bdc2019-11-05 21:57:07 +08001150 break;
1151 case LYS_CHOICE:
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001152 yprp_choice(pctx, node);
FredGand944bdc2019-11-05 21:57:07 +08001153 break;
1154 case LYS_LEAF:
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001155 yprp_leaf(pctx, node);
FredGand944bdc2019-11-05 21:57:07 +08001156 break;
1157 case LYS_LEAFLIST:
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001158 yprp_leaflist(pctx, node);
FredGand944bdc2019-11-05 21:57:07 +08001159 break;
1160 case LYS_LIST:
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001161 yprp_list(pctx, node);
FredGand944bdc2019-11-05 21:57:07 +08001162 break;
1163 case LYS_USES:
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001164 yprp_uses(pctx, node);
FredGand944bdc2019-11-05 21:57:07 +08001165 break;
1166 case LYS_ANYXML:
1167 case LYS_ANYDATA:
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001168 yprp_anydata(pctx, node);
FredGand944bdc2019-11-05 21:57:07 +08001169 break;
1170 case LYS_CASE:
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001171 yprp_case(pctx, node);
FredGand944bdc2019-11-05 21:57:07 +08001172 break;
1173 default:
1174 break;
1175 }
1176}
1177
1178static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001179yprp_deviation(struct lys_ypr_ctx *pctx, const struct lysp_deviation *deviation)
FredGand944bdc2019-11-05 21:57:07 +08001180{
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001181 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001182 struct lysp_deviate_add *add;
1183 struct lysp_deviate_rpl *rpl;
1184 struct lysp_deviate_del *del;
1185 struct lysp_deviate *elem;
1186
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001187 ypr_open(pctx, "deviation", "target-node", deviation->nodeid, 1);
FredGand944bdc2019-11-05 21:57:07 +08001188 LEVEL++;
1189
Michal Vaskob26d09d2022-08-22 09:52:19 +02001190 yprp_extension_instances(pctx, LY_STMT_DEVIATION, 0, deviation->exts, NULL);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001191 ypr_description(pctx, deviation->dsc, deviation->exts, NULL);
1192 ypr_reference(pctx, deviation->ref, deviation->exts, NULL);
FredGand944bdc2019-11-05 21:57:07 +08001193
1194 LY_LIST_FOR(deviation->deviates, elem) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001195 ly_print_(pctx->out, "%*s<deviate value=\"", INDENT);
FredGand944bdc2019-11-05 21:57:07 +08001196 if (elem->mod == LYS_DEV_NOT_SUPPORTED) {
1197 if (elem->exts) {
Michal Vasko12ef5362022-09-16 15:13:58 +02001198 ly_print_(pctx->out, "not-supported\">\n");
FredGand944bdc2019-11-05 21:57:07 +08001199 LEVEL++;
1200
Michal Vaskob26d09d2022-08-22 09:52:19 +02001201 yprp_extension_instances(pctx, LY_STMT_DEVIATE, 0, elem->exts, NULL);
FredGand944bdc2019-11-05 21:57:07 +08001202 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001203 ly_print_(pctx->out, "not-supported\"/>\n");
FredGand944bdc2019-11-05 21:57:07 +08001204 continue;
1205 }
1206 } else if (elem->mod == LYS_DEV_ADD) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001207 add = (struct lysp_deviate_add *)elem;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001208 ly_print_(pctx->out, "add\">\n");
FredGand944bdc2019-11-05 21:57:07 +08001209 LEVEL++;
1210
Michal Vaskob26d09d2022-08-22 09:52:19 +02001211 yprp_extension_instances(pctx, LY_STMT_DEVIATE, 0, add->exts, NULL);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001212 ypr_substmt(pctx, LY_STMT_UNITS, 0, add->units, add->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001213 LY_ARRAY_FOR(add->musts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001214 yprp_restr(pctx, &add->musts[u], LY_STMT_MUST, "condition", NULL);
FredGand944bdc2019-11-05 21:57:07 +08001215 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001216 LY_ARRAY_FOR(add->uniques, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001217 ypr_substmt(pctx, LY_STMT_UNIQUE, u, add->uniques[u].str, add->exts);
FredGand944bdc2019-11-05 21:57:07 +08001218 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001219 LY_ARRAY_FOR(add->dflts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001220 ypr_substmt(pctx, LY_STMT_DEFAULT, u, add->dflts[u].str, add->exts);
FredGand944bdc2019-11-05 21:57:07 +08001221 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001222 ypr_config(pctx, add->flags, add->exts, NULL);
1223 ypr_mandatory(pctx, add->flags, add->exts, NULL);
FredGand944bdc2019-11-05 21:57:07 +08001224 if (add->flags & LYS_SET_MIN) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001225 ypr_unsigned(pctx, LY_STMT_MIN_ELEMENTS, 0, add->exts, add->min);
FredGand944bdc2019-11-05 21:57:07 +08001226 }
1227 if (add->flags & LYS_SET_MAX) {
1228 if (add->max) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001229 ypr_unsigned(pctx, LY_STMT_MAX_ELEMENTS, 0, add->exts, add->max);
FredGand944bdc2019-11-05 21:57:07 +08001230 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001231 ypr_substmt(pctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", add->exts);
FredGand944bdc2019-11-05 21:57:07 +08001232 }
1233 }
1234 } else if (elem->mod == LYS_DEV_REPLACE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001235 rpl = (struct lysp_deviate_rpl *)elem;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001236 ly_print_(pctx->out, "replace\">\n");
FredGand944bdc2019-11-05 21:57:07 +08001237 LEVEL++;
1238
Michal Vaskob26d09d2022-08-22 09:52:19 +02001239 yprp_extension_instances(pctx, LY_STMT_DEVIATE, 0, rpl->exts, NULL);
FredGand944bdc2019-11-05 21:57:07 +08001240 if (rpl->type) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001241 yprp_type(pctx, rpl->type);
FredGand944bdc2019-11-05 21:57:07 +08001242 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001243 ypr_substmt(pctx, LY_STMT_UNITS, 0, rpl->units, rpl->exts);
1244 ypr_substmt(pctx, LY_STMT_DEFAULT, 0, rpl->dflt.str, rpl->exts);
1245 ypr_config(pctx, rpl->flags, rpl->exts, NULL);
1246 ypr_mandatory(pctx, rpl->flags, rpl->exts, NULL);
FredGand944bdc2019-11-05 21:57:07 +08001247 if (rpl->flags & LYS_SET_MIN) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001248 ypr_unsigned(pctx, LY_STMT_MIN_ELEMENTS, 0, rpl->exts, rpl->min);
FredGand944bdc2019-11-05 21:57:07 +08001249 }
1250 if (rpl->flags & LYS_SET_MAX) {
1251 if (rpl->max) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001252 ypr_unsigned(pctx, LY_STMT_MAX_ELEMENTS, 0, rpl->exts, rpl->max);
FredGand944bdc2019-11-05 21:57:07 +08001253 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001254 ypr_substmt(pctx, LY_STMT_MAX_ELEMENTS, 0, "unbounded", rpl->exts);
FredGand944bdc2019-11-05 21:57:07 +08001255 }
1256 }
1257 } else if (elem->mod == LYS_DEV_DELETE) {
Michal Vasko22df3f02020-08-24 13:29:22 +02001258 del = (struct lysp_deviate_del *)elem;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001259 ly_print_(pctx->out, "delete\">\n");
FredGand944bdc2019-11-05 21:57:07 +08001260 LEVEL++;
1261
Michal Vaskob26d09d2022-08-22 09:52:19 +02001262 yprp_extension_instances(pctx, LY_STMT_DEVIATE, 0, del->exts, NULL);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001263 ypr_substmt(pctx, LY_STMT_UNITS, 0, del->units, del->exts);
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001264 LY_ARRAY_FOR(del->musts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001265 yprp_restr(pctx, &del->musts[u], LY_STMT_MUST, "condition", NULL);
FredGand944bdc2019-11-05 21:57:07 +08001266 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001267 LY_ARRAY_FOR(del->uniques, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001268 ypr_substmt(pctx, LY_STMT_UNIQUE, u, del->uniques[u].str, del->exts);
FredGand944bdc2019-11-05 21:57:07 +08001269 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02001270 LY_ARRAY_FOR(del->dflts, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001271 ypr_substmt(pctx, LY_STMT_DEFAULT, u, del->dflts[u].str, del->exts);
FredGand944bdc2019-11-05 21:57:07 +08001272 }
1273 }
1274
1275 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001276 ypr_close(pctx, "deviate", 1);
FredGand944bdc2019-11-05 21:57:07 +08001277 }
1278
1279 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001280 ypr_close(pctx, "deviation", 1);
FredGand944bdc2019-11-05 21:57:07 +08001281}
1282
FredGand944bdc2019-11-05 21:57:07 +08001283static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001284ypr_xmlns(struct lys_ypr_ctx *pctx, const struct lys_module *module, uint16_t indent)
FredGand944bdc2019-11-05 21:57:07 +08001285{
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001286 ly_print_(pctx->out, "%*sxmlns=\"%s\"", indent + INDENT, YIN_NS_URI);
1287 ly_print_(pctx->out, "\n%*sxmlns:%s=\"%s\"", indent + INDENT, module->prefix, module->ns);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001288}
FredGand944bdc2019-11-05 21:57:07 +08001289
Michal Vasko7c8439f2020-08-05 13:25:19 +02001290static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001291ypr_import_xmlns(struct lys_ypr_ctx *pctx, const struct lysp_module *modp, uint16_t indent)
Michal Vasko7c8439f2020-08-05 13:25:19 +02001292{
1293 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001294
1295 LY_ARRAY_FOR(modp->imports, u){
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001296 ly_print_(pctx->out, "\n%*sxmlns:%s=\"%s\"", indent + INDENT, modp->imports[u].prefix, modp->imports[u].module->ns);
FredGand944bdc2019-11-05 21:57:07 +08001297 }
1298}
1299
FredGand944bdc2019-11-05 21:57:07 +08001300static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001301yin_print_parsed_linkage(struct lys_ypr_ctx *pctx, const struct lysp_module *modp)
FredGand944bdc2019-11-05 21:57:07 +08001302{
Michal Vasko7c8439f2020-08-05 13:25:19 +02001303 LY_ARRAY_COUNT_TYPE u;
FredGand944bdc2019-11-05 21:57:07 +08001304
FredGand944bdc2019-11-05 21:57:07 +08001305 LY_ARRAY_FOR(modp->imports, u) {
Michal Vasko3e9bc2f2020-11-04 17:13:56 +01001306 if (modp->imports[u].flags & LYS_INTERNAL) {
1307 continue;
1308 }
1309
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001310 ypr_open(pctx, "import", "module", modp->imports[u].name, 1);
FredGand944bdc2019-11-05 21:57:07 +08001311 LEVEL++;
Michal Vaskob26d09d2022-08-22 09:52:19 +02001312 yprp_extension_instances(pctx, LY_STMT_IMPORT, 0, modp->imports[u].exts, NULL);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001313 ypr_substmt(pctx, LY_STMT_PREFIX, 0, modp->imports[u].prefix, modp->imports[u].exts);
FredGand944bdc2019-11-05 21:57:07 +08001314 if (modp->imports[u].rev[0]) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001315 ypr_substmt(pctx, LY_STMT_REVISION_DATE, 0, modp->imports[u].rev, modp->imports[u].exts);
FredGand944bdc2019-11-05 21:57:07 +08001316 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001317 ypr_substmt(pctx, LY_STMT_DESCRIPTION, 0, modp->imports[u].dsc, modp->imports[u].exts);
1318 ypr_substmt(pctx, LY_STMT_REFERENCE, 0, modp->imports[u].ref, modp->imports[u].exts);
FredGand944bdc2019-11-05 21:57:07 +08001319 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001320 ypr_close(pctx, "import", 1);
FredGand944bdc2019-11-05 21:57:07 +08001321 }
1322 LY_ARRAY_FOR(modp->includes, u) {
Radek Krejci771928a2021-01-19 13:42:36 +01001323 if (modp->includes[u].injected) {
1324 /* do not print the includes injected from submodules */
1325 continue;
1326 }
FredGand944bdc2019-11-05 21:57:07 +08001327 if (modp->includes[u].rev[0] || modp->includes[u].dsc || modp->includes[u].ref || modp->includes[u].exts) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001328 ypr_open(pctx, "include", "module", modp->includes[u].name, 1);
FredGand944bdc2019-11-05 21:57:07 +08001329 LEVEL++;
Michal Vaskob26d09d2022-08-22 09:52:19 +02001330 yprp_extension_instances(pctx, LY_STMT_INCLUDE, 0, modp->includes[u].exts, NULL);
FredGand944bdc2019-11-05 21:57:07 +08001331 if (modp->includes[u].rev[0]) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001332 ypr_substmt(pctx, LY_STMT_REVISION_DATE, 0, modp->includes[u].rev, modp->includes[u].exts);
FredGand944bdc2019-11-05 21:57:07 +08001333 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001334 ypr_substmt(pctx, LY_STMT_DESCRIPTION, 0, modp->includes[u].dsc, modp->includes[u].exts);
1335 ypr_substmt(pctx, LY_STMT_REFERENCE, 0, modp->includes[u].ref, modp->includes[u].exts);
FredGand944bdc2019-11-05 21:57:07 +08001336 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001337 ly_print_(pctx->out, "%*s}\n", INDENT);
FredGand944bdc2019-11-05 21:57:07 +08001338 } else {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001339 ypr_open(pctx, "include", "module", modp->includes[u].name, -1);
FredGand944bdc2019-11-05 21:57:07 +08001340 }
1341 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02001342}
FredGand944bdc2019-11-05 21:57:07 +08001343
Michal Vasko7c8439f2020-08-05 13:25:19 +02001344static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001345yin_print_parsed_body(struct lys_ypr_ctx *pctx, const struct lysp_module *modp)
Michal Vasko7c8439f2020-08-05 13:25:19 +02001346{
1347 LY_ARRAY_COUNT_TYPE u;
1348 struct lysp_node *data;
Radek Krejci2a9fc652021-01-22 17:44:34 +01001349 struct lysp_node_action *action;
1350 struct lysp_node_notif *notif;
1351 struct lysp_node_grp *grp;
1352 struct lysp_node_augment *aug;
FredGand944bdc2019-11-05 21:57:07 +08001353
FredGand944bdc2019-11-05 21:57:07 +08001354 LY_ARRAY_FOR(modp->extensions, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001355 yprp_extension(pctx, &modp->extensions[u]);
FredGand944bdc2019-11-05 21:57:07 +08001356 }
1357 if (modp->exts) {
Michal Vaskob26d09d2022-08-22 09:52:19 +02001358 yprp_extension_instances(pctx, LY_STMT_MODULE, 0, modp->exts, NULL);
FredGand944bdc2019-11-05 21:57:07 +08001359 }
1360
1361 LY_ARRAY_FOR(modp->features, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001362 yprp_feature(pctx, &modp->features[u]);
FredGand944bdc2019-11-05 21:57:07 +08001363 }
1364
1365 LY_ARRAY_FOR(modp->identities, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001366 yprp_identity(pctx, &modp->identities[u]);
FredGand944bdc2019-11-05 21:57:07 +08001367 }
1368
1369 LY_ARRAY_FOR(modp->typedefs, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001370 yprp_typedef(pctx, &modp->typedefs[u]);
FredGand944bdc2019-11-05 21:57:07 +08001371 }
1372
Radek Krejci2a9fc652021-01-22 17:44:34 +01001373 LY_LIST_FOR(modp->groupings, grp) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001374 yprp_grouping(pctx, grp);
FredGand944bdc2019-11-05 21:57:07 +08001375 }
1376
1377 LY_LIST_FOR(modp->data, data) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001378 yprp_node(pctx, data);
FredGand944bdc2019-11-05 21:57:07 +08001379 }
1380
Radek Krejci2a9fc652021-01-22 17:44:34 +01001381 LY_LIST_FOR(modp->augments, aug) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001382 yprp_augment(pctx, aug);
FredGand944bdc2019-11-05 21:57:07 +08001383 }
1384
Radek Krejci2a9fc652021-01-22 17:44:34 +01001385 LY_LIST_FOR(modp->rpcs, action) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001386 yprp_action(pctx, action);
FredGand944bdc2019-11-05 21:57:07 +08001387 }
1388
Radek Krejci2a9fc652021-01-22 17:44:34 +01001389 LY_LIST_FOR(modp->notifs, notif) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001390 yprp_notification(pctx, notif);
FredGand944bdc2019-11-05 21:57:07 +08001391 }
1392
1393 LY_ARRAY_FOR(modp->deviations, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001394 yprp_deviation(pctx, &modp->deviations[u]);
FredGand944bdc2019-11-05 21:57:07 +08001395 }
Michal Vasko7c8439f2020-08-05 13:25:19 +02001396}
1397
1398LY_ERR
Michal Vasko7997d5a2021-02-22 10:55:56 +01001399yin_print_parsed_module(struct ly_out *out, const struct lysp_module *modp, uint32_t options)
Michal Vasko7c8439f2020-08-05 13:25:19 +02001400{
1401 LY_ARRAY_COUNT_TYPE u;
Michal Vasko7997d5a2021-02-22 10:55:56 +01001402 const struct lys_module *module = modp->mod;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001403 struct lys_ypr_ctx pctx_ = {.out = out, .level = 0, .module = module, .options = options}, *pctx = &pctx_;
Michal Vasko7c8439f2020-08-05 13:25:19 +02001404
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001405 ly_print_(pctx->out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1406 ly_print_(pctx->out, "%*s<module name=\"%s\"\n", INDENT, module->name);
1407 ypr_xmlns(pctx, module, XML_NS_INDENT);
1408 ypr_import_xmlns(pctx, modp, XML_NS_INDENT);
1409 ly_print_(pctx->out, ">\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001410
1411 LEVEL++;
1412
1413 /* module-header-stmts */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001414 if (modp->version) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001415 ypr_substmt(pctx, LY_STMT_YANG_VERSION, 0, modp->version == LYS_VERSION_1_1 ? "1.1" : "1", modp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001416 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001417 ypr_substmt(pctx, LY_STMT_NAMESPACE, 0, module->ns, modp->exts);
1418 ypr_substmt(pctx, LY_STMT_PREFIX, 0, module->prefix, modp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001419
1420 /* linkage-stmts (import/include) */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001421 yin_print_parsed_linkage(pctx, modp);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001422
1423 /* meta-stmts */
1424 if (module->org || module->contact || module->dsc || module->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02001425 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001426 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001427 ypr_substmt(pctx, LY_STMT_ORGANIZATION, 0, module->org, modp->exts);
1428 ypr_substmt(pctx, LY_STMT_CONTACT, 0, module->contact, modp->exts);
1429 ypr_substmt(pctx, LY_STMT_DESCRIPTION, 0, module->dsc, modp->exts);
1430 ypr_substmt(pctx, LY_STMT_REFERENCE, 0, module->ref, modp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001431
1432 /* revision-stmts */
1433 if (modp->revs) {
Michal Vasko5233e962020-08-14 14:26:20 +02001434 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001435 }
1436 LY_ARRAY_FOR(modp->revs, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001437 yprp_revision(pctx, &modp->revs[u]);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001438 }
1439
1440 /* body-stmts */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001441 yin_print_parsed_body(pctx, modp);
FredGand944bdc2019-11-05 21:57:07 +08001442
1443 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001444 ly_print_(out, "%*s</module>\n", INDENT);
FredGand944bdc2019-11-05 21:57:07 +08001445 ly_print_flush(out);
1446
1447 return LY_SUCCESS;
1448}
1449
Michal Vasko7c8439f2020-08-05 13:25:19 +02001450static void
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001451yprp_belongsto(struct lys_ypr_ctx *pctx, const struct lysp_submodule *submodp)
Michal Vasko7c8439f2020-08-05 13:25:19 +02001452{
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001453 ypr_open(pctx, "belongs-to", "module", submodp->mod->name, 1);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001454 LEVEL++;
Michal Vaskob26d09d2022-08-22 09:52:19 +02001455 yprp_extension_instances(pctx, LY_STMT_BELONGS_TO, 0, submodp->exts, NULL);
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001456 ypr_substmt(pctx, LY_STMT_PREFIX, 0, submodp->prefix, submodp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001457 LEVEL--;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001458 ypr_close(pctx, "belongs-to", 1);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001459}
1460
1461LY_ERR
Michal Vasko7997d5a2021-02-22 10:55:56 +01001462yin_print_parsed_submodule(struct ly_out *out, const struct lysp_submodule *submodp, uint32_t options)
Michal Vasko7c8439f2020-08-05 13:25:19 +02001463{
1464 LY_ARRAY_COUNT_TYPE u;
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001465 struct lys_ypr_ctx pctx_ = {.out = out, .level = 0, .module = submodp->mod, .options = options}, *pctx = &pctx_;
Michal Vasko7c8439f2020-08-05 13:25:19 +02001466
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001467 ly_print_(pctx->out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
1468 ly_print_(pctx->out, "%*s<submodule name=\"%s\"\n", INDENT, submodp->name);
1469 ypr_xmlns(pctx, submodp->mod, XML_NS_INDENT);
1470 ypr_import_xmlns(pctx, (struct lysp_module *)submodp, XML_NS_INDENT);
1471 ly_print_(pctx->out, ">\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001472
1473 LEVEL++;
1474
1475 /* submodule-header-stmts */
1476 if (submodp->version) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001477 ypr_substmt(pctx, LY_STMT_YANG_VERSION, 0, submodp->version == LYS_VERSION_1_1 ? "1.1" : "1", submodp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001478 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001479 yprp_belongsto(pctx, submodp);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001480
1481 /* linkage-stmts (import/include) */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001482 yin_print_parsed_linkage(pctx, (struct lysp_module *)submodp);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001483
1484 /* meta-stmts */
1485 if (submodp->org || submodp->contact || submodp->dsc || submodp->ref) {
Michal Vasko5233e962020-08-14 14:26:20 +02001486 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001487 }
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001488 ypr_substmt(pctx, LY_STMT_ORGANIZATION, 0, submodp->org, submodp->exts);
1489 ypr_substmt(pctx, LY_STMT_CONTACT, 0, submodp->contact, submodp->exts);
1490 ypr_substmt(pctx, LY_STMT_DESCRIPTION, 0, submodp->dsc, submodp->exts);
1491 ypr_substmt(pctx, LY_STMT_REFERENCE, 0, submodp->ref, submodp->exts);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001492
1493 /* revision-stmts */
1494 if (submodp->revs) {
Michal Vasko5233e962020-08-14 14:26:20 +02001495 ly_print_(out, "\n");
Michal Vasko7c8439f2020-08-05 13:25:19 +02001496 }
1497 LY_ARRAY_FOR(submodp->revs, u) {
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001498 yprp_revision(pctx, &submodp->revs[u]);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001499 }
1500
1501 /* body-stmts */
Michal Vasko61ad1ff2022-02-10 15:48:39 +01001502 yin_print_parsed_body(pctx, (struct lysp_module *)submodp);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001503
1504 LEVEL--;
Michal Vasko5233e962020-08-14 14:26:20 +02001505 ly_print_(out, "%*s</submodule>\n", INDENT);
Michal Vasko7c8439f2020-08-05 13:25:19 +02001506 ly_print_flush(out);
1507
1508 return LY_SUCCESS;
1509}