blob: 2e9cf2f73ed0eb926ea20a8c38c2a03ce161a994 [file] [log] [blame]
Radek Krejcida04f4a2015-05-21 12:54:09 +02001/**
Radek Krejciefdd0ce2015-05-26 16:48:29 +02002 * @file printer/yang.c
Radek Krejcida04f4a2015-05-21 12:54:09 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Radek Krejciefdd0ce2015-05-26 16:48:29 +02004 * @brief YANG printer for libyang data model structure
Radek Krejcida04f4a2015-05-21 12:54:09 +02005 *
6 * Copyright (c) 2015 CESNET, z.s.p.o.
7 *
Radek Krejci54f6fb32016-02-24 12:56:39 +01008 * 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
Michal Vasko8de098c2016-02-26 10:00:25 +010011 *
Radek Krejci54f6fb32016-02-24 12:56:39 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejcida04f4a2015-05-21 12:54:09 +020013 */
14
15#include <stdlib.h>
Radek Krejcida04f4a2015-05-21 12:54:09 +020016#include <string.h>
Michal Vasko25cb6c62016-02-12 14:36:21 +010017#include <stdint.h>
Radek Krejcida04f4a2015-05-21 12:54:09 +020018
Radek Krejci998a0b82015-08-17 13:14:36 +020019#include "common.h"
Radek Krejci76b07902015-10-09 09:11:25 +020020#include "printer.h"
Michal Vasko2d162e12015-09-24 14:33:29 +020021#include "tree_schema.h"
Radek Krejcida04f4a2015-05-21 12:54:09 +020022
23#define INDENT ""
24#define LEVEL (level*2)
25
Michal Vasko1e62a092015-12-01 12:27:20 +010026static void yang_print_snode(struct lyout *out, int level, const struct lys_node *node, int mask);
Radek Krejcida04f4a2015-05-21 12:54:09 +020027
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020028static void
Michal Vasko25cb6c62016-02-12 14:36:21 +010029yang_encode(struct lyout *out, const char *text, int len)
30{
31 int i, start_len;
32 const char *start;
33 char special = 0;
34
35 if (!len) {
36 return;
37 }
38
39 if (len < 0) {
40 len = strlen(text);
41 }
42
43 start = text;
44 start_len = 0;
45 for (i = 0; i < len; ++i) {
46 switch (text[i]) {
47 case '\n':
48 case '\t':
49 case '\"':
50 case '\\':
51 special = text[i];
52 break;
53 default:
54 ++start_len;
55 break;
56 }
57
58 if (special) {
59 ly_write(out, start, start_len);
60 switch (special) {
61 case '\n':
62 ly_write(out, "\\n", 2);
63 break;
64 case '\t':
65 ly_write(out, "\\t", 2);
66 break;
67 case '\"':
68 ly_write(out, "\\\"", 2);
69 break;
70 case '\\':
71 ly_write(out, "\\\\", 2);
72 break;
73 }
74
75 start += start_len + 1;
76 start_len = 0;
77
78 special = 0;
79 }
80 }
81
82 ly_write(out, start, start_len);
83}
84
85static void
Radek Krejci32cce7c2015-12-09 16:44:13 +010086yang_print_open(struct lyout *out, int *flag)
87{
88 if (flag && !*flag) {
89 *flag = 1;
90 ly_print(out, " {\n");
91 }
92}
93
94static void
95yang_print_close(struct lyout *out, int level, int flag)
96{
97 if (flag) {
98 ly_print(out, "%*s}\n", LEVEL, INDENT);
99 } else {
100 ly_print(out, ";\n");
101 }
102}
103
104static void
Radek Krejci8d81e002015-12-10 11:18:59 +0100105yang_print_text(struct lyout *out, int level, const char *name, const char *text, int singleline)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200106{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200107 const char *s, *t;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200108
Radek Krejci8d81e002015-12-10 11:18:59 +0100109 if (singleline) {
110 ly_print(out, "%*s%s \"", LEVEL, INDENT, name);
111 } else {
112 ly_print(out, "%*s%s\n", LEVEL, INDENT, name);
113 level++;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200114
Radek Krejci8d81e002015-12-10 11:18:59 +0100115 ly_print(out, "%*s\"", LEVEL, INDENT);
116 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200117 t = text;
118 while ((s = strchr(t, '\n'))) {
Michal Vasko25cb6c62016-02-12 14:36:21 +0100119 yang_encode(out, t, s - t);
120 ly_print(out, "\n");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200121 t = s + 1;
Michal Vasko25cb6c62016-02-12 14:36:21 +0100122 ly_print(out, "%*s ", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200123 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200124
Radek Krejci8d81e002015-12-10 11:18:59 +0100125 ly_print(out, "%s\";\n", t);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200126 level--;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200127
128}
129
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200130static void
Radek Krejci32cce7c2015-12-09 16:44:13 +0100131yang_print_nacmext(struct lyout *out, int level, const struct lys_node *node, const struct lys_module *module, int *flag)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200132{
Radek Krejci6764bb32015-07-03 15:16:04 +0200133 int i, j;
Radek Krejci86909d22015-07-03 16:11:25 +0200134 const char *prefix = NULL;
Radek Krejci6764bb32015-07-03 15:16:04 +0200135
Radek Krejci1d82ef62015-08-07 14:44:40 +0200136 if (node->nacm && (!node->parent || node->parent->nacm != node->nacm)) {
Radek Krejci6764bb32015-07-03 15:16:04 +0200137 /* locate ietf-netconf-acm module in imports */
Michal Vaskodb7aa992015-07-08 09:37:38 +0200138 if (!strcmp(module->name, "ietf-netconf-acm")) {
139 prefix = module->prefix;
Radek Krejci6764bb32015-07-03 15:16:04 +0200140 } else {
141 /* search in imports */
Michal Vaskodb7aa992015-07-08 09:37:38 +0200142 for (i = 0; i < module->imp_size; i++) {
143 if (!strcmp(module->imp[i].module->name, "ietf-netconf-acm")) {
144 prefix = module->imp[i].prefix;
Radek Krejci6764bb32015-07-03 15:16:04 +0200145 break;
146 }
147 }
148 /* and in imports of includes */
149 if (!prefix) {
Michal Vaskodb7aa992015-07-08 09:37:38 +0200150 for (j = 0; j < module->inc_size; j++) {
151 for (i = 0; i < module->inc[j].submodule->imp_size; i++) {
152 if (!strcmp(module->inc[j].submodule->imp[i].module->name, "ietf-netconf-acm")) {
153 prefix = module->inc[j].submodule->imp[i].prefix;
Radek Krejci6764bb32015-07-03 15:16:04 +0200154 break;
155 }
156 }
157 }
158 }
159 }
160
Radek Krejci1d82ef62015-08-07 14:44:40 +0200161 if ((node->nacm & LYS_NACM_DENYW) && (!node->parent || !(node->parent->nacm & LYS_NACM_DENYW))) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100162 yang_print_open(out, flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200163 ly_print(out, "%*s%s:default-deny-write;\n", LEVEL, INDENT, prefix);
Radek Krejci6764bb32015-07-03 15:16:04 +0200164 }
Radek Krejci1d82ef62015-08-07 14:44:40 +0200165 if ((node->nacm & LYS_NACM_DENYA) && (!node->parent || !(node->parent->nacm & LYS_NACM_DENYA))) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100166 yang_print_open(out, flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200167 ly_print(out, "%*s%s:default-deny-all;\n", LEVEL, INDENT, prefix);
Radek Krejci6764bb32015-07-03 15:16:04 +0200168 }
169 }
Radek Krejci6a113852015-07-03 16:04:20 +0200170}
171
172/*
173 * Covers:
174 * description, reference, status
175 */
176static void
Radek Krejci32cce7c2015-12-09 16:44:13 +0100177yang_print_snode_common(struct lyout *out, int level, const struct lys_node *node, int *flag)
Radek Krejci6a113852015-07-03 16:04:20 +0200178{
Radek Krejci1d82ef62015-08-07 14:44:40 +0200179 if (node->flags & LYS_STATUS_CURR) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100180 yang_print_open(out, flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200181 ly_print(out, "%*sstatus \"current\";\n", LEVEL, INDENT);
Radek Krejci1d82ef62015-08-07 14:44:40 +0200182 } else if (node->flags & LYS_STATUS_DEPRC) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100183 yang_print_open(out, flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200184 ly_print(out, "%*sstatus \"deprecated\";\n", LEVEL, INDENT);
Radek Krejci1d82ef62015-08-07 14:44:40 +0200185 } else if (node->flags & LYS_STATUS_OBSLT) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100186 yang_print_open(out, flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200187 ly_print(out, "%*sstatus \"obsolete\";\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200188 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200189
Radek Krejci1d82ef62015-08-07 14:44:40 +0200190 if (node->dsc) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100191 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100192 yang_print_text(out, level, "description", node->dsc, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200193 }
Radek Krejci1d82ef62015-08-07 14:44:40 +0200194 if (node->ref) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100195 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100196 yang_print_text(out, level, "reference", node->ref, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200197 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200198}
199
200/*
201 * Covers:
Michal Vasko2b8faed2015-06-09 12:51:20 +0200202 * config, mandatory
Radek Krejcida04f4a2015-05-21 12:54:09 +0200203 * description, reference, status
204 */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200205static void
Radek Krejci32cce7c2015-12-09 16:44:13 +0100206yang_print_snode_common2(struct lyout *out, int level, const struct lys_node *node, int *flag)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200207{
Michal Vasko43271ee2016-02-05 14:27:09 +0100208 if (node->parent) {
Radek Krejci32c7bd62016-04-14 17:47:04 +0200209 if (node->flags & LYS_CONFIG_SET) {
Michal Vasko43271ee2016-02-05 14:27:09 +0100210 /* print config when it differs from the parent ... */
211 if (node->flags & LYS_CONFIG_W) {
212 yang_print_open(out, flag);
213 ly_print(out, "%*sconfig true;\n", LEVEL, INDENT);
214 } else if (node->flags & LYS_CONFIG_R) {
215 yang_print_open(out, flag);
216 ly_print(out, "%*sconfig false;\n", LEVEL, INDENT);
217 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200218 }
Michal Vasko43271ee2016-02-05 14:27:09 +0100219 } else if (node->flags & LYS_CONFIG_R) {
220 /* ... or it's a top-level state node */
221 yang_print_open(out, flag);
222 ly_print(out, "%*sconfig false;\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200223 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200224
Radek Krejci1d82ef62015-08-07 14:44:40 +0200225 if (node->flags & LYS_MAND_TRUE) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100226 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100227 ly_print(out, "%*smandatory true;\n", LEVEL, INDENT);
Radek Krejci1d82ef62015-08-07 14:44:40 +0200228 } else if (node->flags & LYS_MAND_FALSE) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100229 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100230 ly_print(out, "%*smandatory false;\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200231 }
Michal Vasko2b8faed2015-06-09 12:51:20 +0200232
Radek Krejci32cce7c2015-12-09 16:44:13 +0100233 yang_print_snode_common(out, level, node, flag);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200234}
235
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200236static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100237yang_print_iffeature(struct lyout *out, int level, const struct lys_module *module, const struct lys_feature *feat)
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200238{
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100239 struct lys_module *mod;
240
Radek Krejci76b07902015-10-09 09:11:25 +0200241 ly_print(out, "%*sif-feature ", LEVEL, INDENT);
Radek Krejcic4283442016-04-22 09:19:27 +0200242 mod = lys_main_module(feat->module);
243 if (lys_main_module(module) != mod) {
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100244 ly_print(out, "%s:", transform_module_name2import_prefix(module, mod->name));
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200245 }
Radek Krejci76b07902015-10-09 09:11:25 +0200246 ly_print(out, "%s;\n", feat->name);
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200247}
248
249static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100250yang_print_feature(struct lyout *out, int level, const struct lys_feature *feat)
Michal Vasko4773b762015-07-07 12:15:10 +0200251{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100252 int i, flag = 0;
Michal Vasko30f6e912015-07-07 12:24:27 +0200253
Radek Krejci32cce7c2015-12-09 16:44:13 +0100254 ly_print(out, "%*sfeature %s", LEVEL, INDENT, feat->name);
Michal Vasko4773b762015-07-07 12:15:10 +0200255 level++;
256
Radek Krejci32cce7c2015-12-09 16:44:13 +0100257 yang_print_snode_common(out, level, (struct lys_node *)feat, &flag);
Michal Vasko30f6e912015-07-07 12:24:27 +0200258 for (i = 0; i < feat->features_size; ++i) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100259 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200260 yang_print_iffeature(out, level, feat->module, feat->features[i]);
Michal Vasko30f6e912015-07-07 12:24:27 +0200261 }
Michal Vasko4773b762015-07-07 12:15:10 +0200262
263 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100264 yang_print_close(out, level, flag);
Michal Vasko4773b762015-07-07 12:15:10 +0200265}
266
267static void
Radek Krejci32cce7c2015-12-09 16:44:13 +0100268yang_print_restr(struct lyout *out, int level, const struct lys_restr *restr, int *flag)
Radek Krejci41726f92015-06-19 13:11:05 +0200269{
Radek Krejci0bd5db42015-06-19 13:30:07 +0200270 if (restr->dsc != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100271 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100272 yang_print_text(out, level, "description", restr->dsc, 0);
Radek Krejci41726f92015-06-19 13:11:05 +0200273 }
Radek Krejci0bd5db42015-06-19 13:30:07 +0200274 if (restr->ref != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100275 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100276 yang_print_text(out, level, "reference", restr->ref, 0);
Radek Krejci41726f92015-06-19 13:11:05 +0200277 }
Radek Krejci0bd5db42015-06-19 13:30:07 +0200278 if (restr->eapptag != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100279 yang_print_open(out, flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200280 ly_print(out, "%*serror-app-tag \"%s\";\n", LEVEL, INDENT, restr->eapptag);
Radek Krejci41726f92015-06-19 13:11:05 +0200281 }
Radek Krejci0bd5db42015-06-19 13:30:07 +0200282 if (restr->emsg != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100283 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100284 yang_print_text(out, level, "error-message", restr->emsg, 0);
Radek Krejci41726f92015-06-19 13:11:05 +0200285 }
286}
287
288static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100289yang_print_when(struct lyout *out, int level, const struct lys_module *module, const struct lys_when *when)
Michal Vasko1f0428a2015-07-07 14:55:04 +0200290{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100291 int flag = 0;
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100292 const char *str;
Michal Vaskof9893382015-10-09 14:03:04 +0200293
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100294 str = transform_json2schema(module, when->cond);
295 if (!str) {
Michal Vaskof9893382015-10-09 14:03:04 +0200296 ly_print(out, "(!error!)");
297 return;
298 }
299
Michal Vasko25cb6c62016-02-12 14:36:21 +0100300 ly_print(out, "%*swhen \"", LEVEL, INDENT);
301 yang_encode(out, str, -1);
302 ly_print(out, "\"");
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100303 lydict_remove(module->ctx, str);
Michal Vaskof9893382015-10-09 14:03:04 +0200304
Michal Vasko1f0428a2015-07-07 14:55:04 +0200305 level++;
306 if (when->dsc) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100307 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100308 yang_print_text(out, level, "description", when->dsc, 0);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200309 }
310 if (when->ref) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100311 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100312 yang_print_text(out, level, "reference", when->ref, 0);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200313 }
314 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100315 yang_print_close(out, level, flag);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200316}
317
318static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100319yang_print_type(struct lyout *out, int level, const struct lys_module *module, const struct lys_type *type)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200320{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200321 int i;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100322 int flag = 0, flag2;
Michal Vasko0fb82c62015-10-20 13:41:53 +0200323 const char *str;
Radek Krejcic071c542016-01-27 14:57:51 +0100324 struct lys_module *mod;
Radek Krejci25d782a2015-05-22 15:03:23 +0200325
Michal Vasko0fb82c62015-10-20 13:41:53 +0200326 if (type->module_name) {
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100327 ly_print(out, "%*stype %s:%s", LEVEL, INDENT,
328 transform_module_name2import_prefix(module, type->module_name), type->der->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200329 } else {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100330 ly_print(out, "%*stype %s", LEVEL, INDENT, type->der->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200331 }
332 level++;
333 switch (type->base) {
334 case LY_TYPE_BINARY:
Michal Vasko4634cda2016-02-16 09:22:09 +0100335 if (type->info.binary.length) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100336 yang_print_open(out, &flag);
Michal Vasko25cb6c62016-02-12 14:36:21 +0100337 ly_print(out, "%*slength \"", LEVEL, INDENT);
338 yang_encode(out, type->info.binary.length->expr, -1);
339 ly_print(out, "\"");
Radek Krejci32cce7c2015-12-09 16:44:13 +0100340 flag2 = 0;
341 yang_print_restr(out, level + 1, type->info.binary.length, &flag2);
342 yang_print_close(out, level, flag2);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200343 }
344 break;
345 case LY_TYPE_BITS:
346 for (i = 0; i < type->info.bits.count; ++i) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100347 yang_print_open(out, &flag);
Michal Vasko3f053ef2016-02-12 14:27:13 +0100348 ly_print(out, "%*sbit %s", LEVEL, INDENT, type->info.bits.bit[i].name);
349 flag2 = 0;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200350 level++;
Michal Vasko3f053ef2016-02-12 14:27:13 +0100351 yang_print_snode_common(out, level, (struct lys_node *)&type->info.bits.bit[i], &flag2);
352 if (!(type->info.bits.bit[i].flags & LYS_AUTOASSIGNED)) {
353 yang_print_open(out, &flag2);
354 ly_print(out, "%*sposition %u;\n", LEVEL, INDENT, type->info.bits.bit[i].pos);
355 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200356 level--;
Michal Vasko3f053ef2016-02-12 14:27:13 +0100357 yang_print_close(out, level, flag2);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200358 }
359 break;
360 case LY_TYPE_DEC64:
Michal Vasko4634cda2016-02-16 09:22:09 +0100361 if (type->info.dec64.dig) {
362 yang_print_open(out, &flag);
363 ly_print(out, "%*sfraction-digits %d;\n", LEVEL, INDENT, type->info.dec64.dig);
364 }
Michal Vaskoea505ee2015-07-07 14:01:00 +0200365 if (type->info.dec64.range != NULL) {
Michal Vasko4634cda2016-02-16 09:22:09 +0100366 yang_print_open(out, &flag);
Michal Vasko25cb6c62016-02-12 14:36:21 +0100367 ly_print(out, "%*srange \"", LEVEL, INDENT);
368 yang_encode(out, type->info.dec64.range->expr, -1);
369 ly_print(out, "\"");
Radek Krejci32cce7c2015-12-09 16:44:13 +0100370 flag2 = 0;
371 yang_print_restr(out, level + 1, type->info.dec64.range, &flag2);
372 yang_print_close(out, level, flag2);
Michal Vaskoea505ee2015-07-07 14:01:00 +0200373 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200374 break;
375 case LY_TYPE_ENUM:
376 for (i = 0; i < type->info.enums.count; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100377 yang_print_open(out, &flag);
Michal Vasko3f053ef2016-02-12 14:27:13 +0100378 ly_print(out, "%*senum \"%s\"", LEVEL, INDENT, type->info.enums.enm[i].name);
379 flag2 = 0;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200380 level++;
Michal Vasko3f053ef2016-02-12 14:27:13 +0100381 yang_print_snode_common(out, level, (struct lys_node *)&type->info.enums.enm[i], &flag2);
382 if (!(type->info.enums.enm[i].flags & LYS_AUTOASSIGNED)) {
383 yang_print_open(out, &flag2);
384 ly_print(out, "%*svalue %d;\n", LEVEL, INDENT, type->info.enums.enm[i].value);
385 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200386 level--;
Michal Vasko3f053ef2016-02-12 14:27:13 +0100387 yang_print_close(out, level, flag2);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200388 }
389 break;
390 case LY_TYPE_IDENT:
Michal Vasko4634cda2016-02-16 09:22:09 +0100391 if (type->info.ident.ref) {
392 yang_print_open(out, &flag);
Radek Krejcic4283442016-04-22 09:19:27 +0200393 mod = lys_main_module(type->info.ident.ref->module);
394 if (lys_main_module(module) == mod) {
Michal Vasko4634cda2016-02-16 09:22:09 +0100395 ly_print(out, "%*sbase %s;\n", LEVEL, INDENT, type->info.ident.ref->name);
396 } else {
397 ly_print(out, "%*sbase %s:%s;\n", LEVEL, INDENT, transform_module_name2import_prefix(module, mod->name),
398 type->info.ident.ref->name);
399 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200400 }
401 break;
402 case LY_TYPE_INST:
Radek Krejciaf351422015-06-19 14:49:38 +0200403 if (type->info.inst.req == 1) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100404 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100405 ly_print(out, "%*srequire-instance true;\n", LEVEL, INDENT);
Radek Krejciaf351422015-06-19 14:49:38 +0200406 } else if (type->info.inst.req == -1) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100407 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100408 ly_print(out, "%*srequire-instance false;\n", LEVEL, INDENT);
Radek Krejciaf351422015-06-19 14:49:38 +0200409 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200410 break;
411 case LY_TYPE_INT8:
412 case LY_TYPE_INT16:
413 case LY_TYPE_INT32:
414 case LY_TYPE_INT64:
415 case LY_TYPE_UINT8:
416 case LY_TYPE_UINT16:
417 case LY_TYPE_UINT32:
418 case LY_TYPE_UINT64:
Michal Vasko4634cda2016-02-16 09:22:09 +0100419 if (type->info.num.range) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100420 yang_print_open(out, &flag);
Michal Vasko25cb6c62016-02-12 14:36:21 +0100421 ly_print(out, "%*srange \"", LEVEL, INDENT);
422 yang_encode(out, type->info.num.range->expr, -1);
423 ly_print(out, "\"");
Radek Krejci32cce7c2015-12-09 16:44:13 +0100424 flag2 = 0;
425 yang_print_restr(out, level + 1, type->info.num.range, &flag2);
426 yang_print_close(out, level, flag2);
Radek Krejcif2860132015-06-20 12:37:20 +0200427 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200428 break;
429 case LY_TYPE_LEAFREF:
Michal Vasko4634cda2016-02-16 09:22:09 +0100430 if (type->info.lref.path) {
431 yang_print_open(out, &flag);
432 str = transform_json2schema(module, type->info.lref.path);
433 ly_print(out, "%*spath \"%s\";\n", LEVEL, INDENT, str);
434 lydict_remove(module->ctx, str);
435 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200436 break;
437 case LY_TYPE_STRING:
Radek Krejci5fbc9162015-06-19 14:11:11 +0200438 if (type->info.str.length) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100439 yang_print_open(out, &flag);
Michal Vasko25cb6c62016-02-12 14:36:21 +0100440 ly_print(out, "%*slength \"", LEVEL, INDENT);
441 yang_encode(out, type->info.str.length->expr, -1);
442 ly_print(out, "\"");
Radek Krejci32cce7c2015-12-09 16:44:13 +0100443 flag2 = 0;
444 yang_print_restr(out, level + 1, type->info.str.length, &flag2);
445 yang_print_close(out, level, flag2);
Radek Krejci061bd522015-06-19 13:45:16 +0200446 }
Radek Krejci5fbc9162015-06-19 14:11:11 +0200447 for (i = 0; i < type->info.str.pat_count; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100448 yang_print_open(out, &flag);
Michal Vasko25cb6c62016-02-12 14:36:21 +0100449 ly_print(out, "%*spattern \"", LEVEL, INDENT);
450 yang_encode(out, type->info.str.patterns[i].expr, -1);
451 ly_print(out, "\"");
Radek Krejci32cce7c2015-12-09 16:44:13 +0100452 flag2 = 0;
453 yang_print_restr(out, level + 1, &type->info.str.patterns[i], &flag2);
454 yang_print_close(out, level, flag2);
Radek Krejci5fbc9162015-06-19 14:11:11 +0200455 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200456 break;
457 case LY_TYPE_UNION:
458 for (i = 0; i < type->info.uni.count; ++i) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100459 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200460 yang_print_type(out, level, module, &type->info.uni.types[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200461 }
462 break;
463 default:
464 /* other types do not have substatements */
465 break;
466 }
467 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100468 yang_print_close(out, level, flag);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200469}
470
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200471static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100472yang_print_must(struct lyout *out, int level, const struct lys_module *module, const struct lys_restr *must)
Michal Vasko7f976ee2015-06-09 13:55:41 +0200473{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100474 int flag = 0;
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100475 const char *str;
Michal Vaskof9893382015-10-09 14:03:04 +0200476
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100477 str = transform_json2schema(module, must->expr);
478 if (!str) {
Michal Vaskof9893382015-10-09 14:03:04 +0200479 ly_print(out, "(!error!)");
480 return;
481 }
482
Michal Vasko25cb6c62016-02-12 14:36:21 +0100483 ly_print(out, "%*smust \"", LEVEL, INDENT);
484 yang_encode(out, str, -1);
485 ly_print(out, "\"");
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100486 lydict_remove(module->ctx, str);
Michal Vaskof9893382015-10-09 14:03:04 +0200487
Radek Krejci32cce7c2015-12-09 16:44:13 +0100488 yang_print_restr(out, level + 1, must, &flag);
489 yang_print_close(out, level, flag);
Michal Vasko7f976ee2015-06-09 13:55:41 +0200490}
491
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200492static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100493yang_print_unique(struct lyout *out, int level, const struct lys_unique *uniq)
Michal Vasko1ef07972015-07-07 14:01:35 +0200494{
495 int i;
496
Radek Krejci76b07902015-10-09 09:11:25 +0200497 ly_print(out, "%*sunique \"", LEVEL, INDENT);
Radek Krejci581ce772015-11-10 17:22:40 +0100498 for (i = 0; i < uniq->expr_size; i++) {
499 ly_print(out, "%s%s", uniq->expr[i], i + 1 < uniq->expr_size ? " " : "");
Michal Vasko1ef07972015-07-07 14:01:35 +0200500 }
Radek Krejci76b07902015-10-09 09:11:25 +0200501 ly_print(out, "\";\n");
Michal Vasko1ef07972015-07-07 14:01:35 +0200502}
503
504static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100505yang_print_refine(struct lyout *out, int level, const struct lys_module *module, const struct lys_refine *refine)
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200506{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200507 int i;
Michal Vaskoa8b25952015-10-20 15:30:25 +0200508 const char *str;
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200509
Michal Vasko5d112852016-02-12 16:47:13 +0100510 str = transform_json2schema(module, refine->target_name);
Michal Vaskoa8b25952015-10-20 15:30:25 +0200511 ly_print(out, "%*srefine \"%s\" {\n", LEVEL, INDENT, str);
512 lydict_remove(module->ctx, str);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200513 level++;
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200514
Radek Krejci1574a8d2015-08-03 14:16:52 +0200515 if (refine->flags & LYS_CONFIG_W) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100516 ly_print(out, "%*sconfig true;\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200517 } else if (refine->flags & LYS_CONFIG_R) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100518 ly_print(out, "%*sconfig false;\n", LEVEL, INDENT);
Radek Krejci6a113852015-07-03 16:04:20 +0200519 }
520
Radek Krejci1574a8d2015-08-03 14:16:52 +0200521 if (refine->flags & LYS_MAND_TRUE) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100522 ly_print(out, "%*smandatory true;\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200523 } else if (refine->flags & LYS_MAND_FALSE) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100524 ly_print(out, "%*smandatory false;\n", LEVEL, INDENT);
Radek Krejci6a113852015-07-03 16:04:20 +0200525 }
526
Radek Krejci32cce7c2015-12-09 16:44:13 +0100527 yang_print_snode_common(out, level, (struct lys_node *)refine, NULL);
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200528
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200529 for (i = 0; i < refine->must_size; ++i) {
Michal Vaskof9893382015-10-09 14:03:04 +0200530 yang_print_must(out, level, module, &refine->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200531 }
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200532
Radek Krejci76512572015-08-04 09:47:08 +0200533 if (refine->target_type & (LYS_LEAF | LYS_CHOICE)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200534 if (refine->mod.dflt != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200535 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, refine->mod.dflt);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200536 }
Radek Krejci76512572015-08-04 09:47:08 +0200537 } else if (refine->target_type == LYS_CONTAINER) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200538 if (refine->mod.presence != NULL) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100539 yang_print_text(out, level, "presence", refine->mod.presence, 1);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200540 }
Radek Krejci76512572015-08-04 09:47:08 +0200541 } else if (refine->target_type & (LYS_LIST | LYS_LEAFLIST)) {
Radek Krejci0f04a6c2016-04-14 16:16:36 +0200542 if (refine->flags & LYS_RFN_MINSET) {
Radek Krejci76b07902015-10-09 09:11:25 +0200543 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, refine->mod.list.min);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200544 }
Radek Krejci0f04a6c2016-04-14 16:16:36 +0200545 if (refine->flags & LYS_RFN_MAXSET) {
Radek Krejci0d7b2472016-02-12 11:11:03 +0100546 if (refine->mod.list.max) {
547 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, refine->mod.list.max);
548 } else {
549 ly_print(out, "%*smax-elements \"unbounded\";\n", LEVEL, INDENT);
550 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200551 }
552 }
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200553
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200554 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200555 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200556}
557
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200558static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100559yang_print_deviation(struct lyout *out, int level, const struct lys_module *module,
560 const struct lys_deviation *deviation)
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200561{
562 int i, j;
Michal Vaskoa8b25952015-10-20 15:30:25 +0200563 const char *str;
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200564
Michal Vasko5d112852016-02-12 16:47:13 +0100565 str = transform_json2schema(module, deviation->target_name);
Michal Vaskoa8b25952015-10-20 15:30:25 +0200566 ly_print(out, "%*sdeviation \"%s\" {\n", LEVEL, INDENT, str);
567 lydict_remove(module->ctx, str);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200568 level++;
569
570 if (deviation->dsc) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100571 yang_print_text(out, level, "description", deviation->dsc, 0);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200572 }
573 if (deviation->ref) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100574 yang_print_text(out, level, "reference", deviation->ref, 0);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200575 }
576
577 for (i = 0; i < deviation->deviate_size; ++i) {
Radek Krejci76b07902015-10-09 09:11:25 +0200578 ly_print(out, "%*sdeviate ", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200579 if (deviation->deviate[i].mod == LY_DEVIATE_NO) {
Radek Krejci76b07902015-10-09 09:11:25 +0200580 ly_print(out, "not-supported {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200581 } else if (deviation->deviate[i].mod == LY_DEVIATE_ADD) {
Radek Krejci76b07902015-10-09 09:11:25 +0200582 ly_print(out, "add {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200583 } else if (deviation->deviate[i].mod == LY_DEVIATE_RPL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200584 ly_print(out, "replace {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200585 } else if (deviation->deviate[i].mod == LY_DEVIATE_DEL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200586 ly_print(out, "delete {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200587 }
588 level++;
589
Radek Krejci1574a8d2015-08-03 14:16:52 +0200590 if (deviation->deviate[i].flags & LYS_CONFIG_W) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100591 ly_print(out, "%*sconfig true;\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200592 } else if (deviation->deviate[i].flags & LYS_CONFIG_R) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100593 ly_print(out, "%*sconfig false;\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200594 }
595
Radek Krejci1574a8d2015-08-03 14:16:52 +0200596 if (deviation->deviate[i].flags & LYS_MAND_TRUE) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100597 ly_print(out, "%*smandatory true;\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200598 } else if (deviation->deviate[i].flags & LYS_MAND_FALSE) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100599 ly_print(out, "%*smandatory false;\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200600 }
601
602 if (deviation->deviate[i].dflt) {
Radek Krejci76b07902015-10-09 09:11:25 +0200603 ly_print(out, "%*sdefault %s;\n", LEVEL, INDENT, deviation->deviate[i].dflt);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200604 }
605
Radek Krejci0d7b2472016-02-12 11:11:03 +0100606 if (deviation->deviate[i].min_set) {
Radek Krejci76b07902015-10-09 09:11:25 +0200607 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, deviation->deviate[i].min);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200608 }
Radek Krejci0d7b2472016-02-12 11:11:03 +0100609 if (deviation->deviate[i].max_set) {
610 if (deviation->deviate[i].max) {
611 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, deviation->deviate[i].max);
612 } else {
613 ly_print(out, "%*smax-elements \"unbounded\";\n", LEVEL, INDENT);
614 }
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200615 }
616
617 for (j = 0; j < deviation->deviate[i].must_size; ++j) {
Michal Vaskof9893382015-10-09 14:03:04 +0200618 yang_print_must(out, level, module, &deviation->deviate[i].must[j]);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200619 }
620
621 for (j = 0; j < deviation->deviate[i].unique_size; ++j) {
Radek Krejci76b07902015-10-09 09:11:25 +0200622 yang_print_unique(out, level, &deviation->deviate[i].unique[j]);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200623 }
624
625 if (deviation->deviate[i].type) {
Radek Krejci76b07902015-10-09 09:11:25 +0200626 yang_print_type(out, level, module, deviation->deviate[i].type);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200627 }
628
629 if (deviation->deviate[i].units) {
Radek Krejci76b07902015-10-09 09:11:25 +0200630 ly_print(out, "%*sunits %s;\n", LEVEL, INDENT, deviation->deviate[i].units);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200631 }
632
633 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200634 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200635 }
636
637 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200638 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200639}
640
641static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100642yang_print_augment(struct lyout *out, int level, const struct lys_module *module,
643 const struct lys_node_augment *augment)
Michal Vasko6f25f212015-07-07 15:42:07 +0200644{
645 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200646 struct lys_node *sub;
Michal Vasko488c19e2015-10-20 15:21:00 +0200647 const char *str;
Michal Vasko6f25f212015-07-07 15:42:07 +0200648
Michal Vasko5d112852016-02-12 16:47:13 +0100649 str = transform_json2schema(module, augment->target_name);
Michal Vasko488c19e2015-10-20 15:21:00 +0200650 ly_print(out, "%*saugment \"%s\" {\n", LEVEL, INDENT, str);
651 lydict_remove(module->ctx, str);
Michal Vasko6f25f212015-07-07 15:42:07 +0200652 level++;
653
Radek Krejci32cce7c2015-12-09 16:44:13 +0100654 yang_print_nacmext(out, level, (struct lys_node *)augment, module, NULL);
655 yang_print_snode_common(out, level, (struct lys_node *)augment, NULL);
Michal Vasko6f25f212015-07-07 15:42:07 +0200656
657 for (i = 0; i < augment->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200658 yang_print_iffeature(out, level, module, augment->features[i]);
Michal Vasko6f25f212015-07-07 15:42:07 +0200659 }
660
661 if (augment->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200662 yang_print_when(out, level, module, augment->when);
Michal Vasko6f25f212015-07-07 15:42:07 +0200663 }
664
665 LY_TREE_FOR(augment->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100666 /* only our augment */
667 if (sub->parent != (struct lys_node *)augment) {
668 continue;
669 }
Radek Krejci76b07902015-10-09 09:11:25 +0200670 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200671 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
672 LYS_USES | LYS_ANYXML | LYS_CASE);
Michal Vasko6f25f212015-07-07 15:42:07 +0200673 }
674
675 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200676 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko6f25f212015-07-07 15:42:07 +0200677}
678
679static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100680yang_print_typedef(struct lyout *out, int level, const struct lys_module *module, const struct lys_tpdf *tpdf)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200681{
Radek Krejci76b07902015-10-09 09:11:25 +0200682 ly_print(out, "%*stypedef %s {\n", LEVEL, INDENT, tpdf->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200683 level++;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200684
Radek Krejci32cce7c2015-12-09 16:44:13 +0100685 yang_print_snode_common(out, level, (struct lys_node *)tpdf, NULL);
Radek Krejci76b07902015-10-09 09:11:25 +0200686 yang_print_type(out, level, module, &tpdf->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200687 if (tpdf->units != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200688 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, tpdf->units);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200689 }
690 if (tpdf->dflt != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200691 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, tpdf->dflt);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200692 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200693
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200694 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200695 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200696}
697
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200698static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100699yang_print_identity(struct lyout *out, int level, const struct lys_ident *ident)
Radek Krejci6793db02015-05-22 17:49:54 +0200700{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100701 int flag = 0;
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100702 struct lys_module *mod;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100703
704 ly_print(out, "%*sidentity %s", LEVEL, INDENT, ident->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200705 level++;
Radek Krejci6793db02015-05-22 17:49:54 +0200706
Radek Krejci32cce7c2015-12-09 16:44:13 +0100707 yang_print_snode_common(out, level, (struct lys_node *)ident, &flag);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200708 if (ident->base) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100709 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200710 ly_print(out, "%*sbase ", LEVEL, INDENT);
Radek Krejcic4283442016-04-22 09:19:27 +0200711 mod = lys_main_module(ident->base->module);
712 if (lys_main_module(ident->module) != mod) {
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100713 ly_print(out, "%s:", transform_module_name2import_prefix(ident->module, mod->name));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200714 }
Radek Krejci76b07902015-10-09 09:11:25 +0200715 ly_print(out, "%s;\n", ident->base->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200716 }
Radek Krejci6793db02015-05-22 17:49:54 +0200717
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200718 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100719 yang_print_close(out, level, flag);
Radek Krejci6793db02015-05-22 17:49:54 +0200720}
721
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200722static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100723yang_print_container(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200724{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100725 int i, flag = 0;
Radek Krejci76512572015-08-04 09:47:08 +0200726 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200727 struct lys_node_container *cont = (struct lys_node_container *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200728
Radek Krejci32cce7c2015-12-09 16:44:13 +0100729 ly_print(out, "%*scontainer %s", LEVEL, INDENT, node->name);
Michal Vasko7f976ee2015-06-09 13:55:41 +0200730
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200731 level++;
Radek Krejci6a113852015-07-03 16:04:20 +0200732
Radek Krejci32cce7c2015-12-09 16:44:13 +0100733 yang_print_nacmext(out, level, node, node->module, &flag);
Radek Krejci6a113852015-07-03 16:04:20 +0200734
Radek Krejci8d81e002015-12-10 11:18:59 +0100735 if (cont->when) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100736 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100737 yang_print_when(out, level, node->module, cont->when);
738 }
739
740 for (i = 0; i < cont->features_size; i++) {
741 yang_print_open(out, &flag);
742 yang_print_iffeature(out, level, node->module, cont->features[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200743 }
Michal Vasko7f976ee2015-06-09 13:55:41 +0200744
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200745 for (i = 0; i < cont->must_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100746 yang_print_open(out, &flag);
Michal Vaskof9893382015-10-09 14:03:04 +0200747 yang_print_must(out, level, node->module, &cont->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200748 }
Michal Vasko7f976ee2015-06-09 13:55:41 +0200749
Radek Krejci8d81e002015-12-10 11:18:59 +0100750 if (cont->presence != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100751 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100752 yang_print_text(out, level, "presence", cont->presence, 1);
Michal Vasko4773b762015-07-07 12:15:10 +0200753 }
754
Radek Krejci8d81e002015-12-10 11:18:59 +0100755 yang_print_snode_common2(out, level, node, &flag);
756
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200757 for (i = 0; i < cont->tpdf_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100758 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200759 yang_print_typedef(out, level, node->module, &cont->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200760 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200761
Radek Krejci1d82ef62015-08-07 14:44:40 +0200762 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100763 /* augments */
764 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200765 continue;
766 }
Radek Krejci32cce7c2015-12-09 16:44:13 +0100767 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200768 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200769 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
770 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200771 }
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200772
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200773 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100774 yang_print_close(out, level, flag);
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200775}
776
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200777static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100778yang_print_case(struct lyout *out, int level, const struct lys_node *node)
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200779{
Michal Vasko4773b762015-07-07 12:15:10 +0200780 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200781 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200782 struct lys_node_case *cas = (struct lys_node_case *)node;
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200783
Radek Krejci76b07902015-10-09 09:11:25 +0200784 ly_print(out, "%*scase %s {\n", LEVEL, INDENT, cas->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200785 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100786 yang_print_nacmext(out, level, node, node->module, NULL);
787 yang_print_snode_common2(out, level, node, NULL);
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200788
Michal Vasko4773b762015-07-07 12:15:10 +0200789 for (i = 0; i < cas->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200790 yang_print_iffeature(out, level, node->module, cas->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200791 }
792
Michal Vasko1f0428a2015-07-07 14:55:04 +0200793 if (cas->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200794 yang_print_when(out, level, node->module, cas->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200795 }
796
Radek Krejci1d82ef62015-08-07 14:44:40 +0200797 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100798 /* augments */
799 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200800 continue;
801 }
Radek Krejci76b07902015-10-09 09:11:25 +0200802 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200803 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
804 LYS_USES | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200805 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200806
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200807 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200808 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200809}
810
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200811static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100812yang_print_choice(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200813{
Michal Vasko4773b762015-07-07 12:15:10 +0200814 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200815 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200816 struct lys_node_choice *choice = (struct lys_node_choice *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200817
Radek Krejci76b07902015-10-09 09:11:25 +0200818 ly_print(out, "%*schoice %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +0200819
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200820 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100821 yang_print_nacmext(out, level, node, node->module, NULL);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200822 if (choice->dflt != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200823 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, choice->dflt->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200824 }
Michal Vasko16083662015-06-09 14:00:45 +0200825
Radek Krejci32cce7c2015-12-09 16:44:13 +0100826 yang_print_snode_common2(out, level, node, NULL);
Michal Vasko4773b762015-07-07 12:15:10 +0200827
828 for (i = 0; i < choice->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200829 yang_print_iffeature(out, level, node->module, choice->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200830 }
831
Michal Vasko1f0428a2015-07-07 14:55:04 +0200832 if (choice->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200833 yang_print_when(out, level, node->module, choice->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200834 }
835
Radek Krejci1d82ef62015-08-07 14:44:40 +0200836 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100837 /* augments */
838 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200839 continue;
840 }
Radek Krejci76b07902015-10-09 09:11:25 +0200841 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200842 LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYXML | LYS_CASE);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200843 }
844 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200845 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200846}
847
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200848static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100849yang_print_leaf(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200850{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200851 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200852 struct lys_node_leaf *leaf = (struct lys_node_leaf *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200853
Radek Krejci76b07902015-10-09 09:11:25 +0200854 ly_print(out, "%*sleaf %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +0200855
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200856 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100857 yang_print_nacmext(out, level, node, node->module, NULL);
Radek Krejci8d81e002015-12-10 11:18:59 +0100858 if (leaf->when) {
859 yang_print_when(out, level, node->module, leaf->when);
860 }
Michal Vasko4773b762015-07-07 12:15:10 +0200861 for (i = 0; i < leaf->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200862 yang_print_iffeature(out, level, node->module, leaf->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200863 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200864 for (i = 0; i < leaf->must_size; i++) {
Michal Vaskof9893382015-10-09 14:03:04 +0200865 yang_print_must(out, level, node->module, &leaf->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200866 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100867 yang_print_snode_common2(out, level, node, NULL);
Radek Krejci76b07902015-10-09 09:11:25 +0200868 yang_print_type(out, level, node->module, &leaf->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200869 if (leaf->units != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200870 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, leaf->units);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200871 }
872 if (leaf->dflt != NULL) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100873 switch (leaf->type.base) {
874 case LY_TYPE_STRING:
875 case LY_TYPE_LEAFREF:
876 case LY_TYPE_IDENT:
877 case LY_TYPE_UNION:
878 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, leaf->dflt);
879 break;
880 default:
881 ly_print(out, "%*sdefault %s;\n", LEVEL, INDENT, leaf->dflt);
882 break;
883 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200884 }
885 level--;
Michal Vasko16083662015-06-09 14:00:45 +0200886
Radek Krejci76b07902015-10-09 09:11:25 +0200887 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko16083662015-06-09 14:00:45 +0200888}
889
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200890static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100891yang_print_anyxml(struct lyout *out, int level, const struct lys_node *node)
Michal Vasko16083662015-06-09 14:00:45 +0200892{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100893 int i, flag = 0;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200894 struct lys_node_anyxml *anyxml = (struct lys_node_anyxml *)node;
Michal Vasko16083662015-06-09 14:00:45 +0200895
Radek Krejci32cce7c2015-12-09 16:44:13 +0100896 ly_print(out, "%*sanyxml %s", LEVEL, INDENT, anyxml->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200897 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100898 yang_print_nacmext(out, level, node, node->module, &flag);
899 yang_print_snode_common2(out, level, node, &flag);
Michal Vasko4773b762015-07-07 12:15:10 +0200900 for (i = 0; i < anyxml->features_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100901 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200902 yang_print_iffeature(out, level, node->module, anyxml->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200903 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200904 for (i = 0; i < anyxml->must_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100905 yang_print_open(out, &flag);
Michal Vaskof9893382015-10-09 14:03:04 +0200906 yang_print_must(out, level, node->module, &anyxml->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200907 }
Michal Vasko1f0428a2015-07-07 14:55:04 +0200908 if (anyxml->when) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100909 yang_print_open(out, &flag);
Michal Vaskof9893382015-10-09 14:03:04 +0200910 yang_print_when(out, level, node->module, anyxml->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200911 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200912 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100913 yang_print_close(out, level, flag);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200914}
915
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200916static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100917yang_print_leaflist(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200918{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200919 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200920 struct lys_node_leaflist *llist = (struct lys_node_leaflist *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200921
Radek Krejci76b07902015-10-09 09:11:25 +0200922 ly_print(out, "%*sleaf-list %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +0200923
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200924 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100925 yang_print_nacmext(out, level, node, node->module, NULL);
Radek Krejci8d81e002015-12-10 11:18:59 +0100926 if (llist->when) {
927 yang_print_when(out, level, llist->module, llist->when);
928 }
Michal Vasko4773b762015-07-07 12:15:10 +0200929 for (i = 0; i < llist->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200930 yang_print_iffeature(out, level, node->module, llist->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200931 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100932 for (i = 0; i < llist->must_size; i++) {
933 yang_print_must(out, level, node->module, &llist->must[i]);
934 }
935 yang_print_snode_common2(out, level, node, NULL);
936 yang_print_type(out, level, node->module, &llist->type);
937 if (llist->units != NULL) {
938 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, llist->units);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200939 }
940 if (llist->min > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200941 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, llist->min);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200942 }
943 if (llist->max > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200944 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, llist->max);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200945 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100946 if (llist->flags & LYS_USERORDERED) {
947 ly_print(out, "%*sordered-by user;\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200948 }
949 level--;
Michal Vasko16083662015-06-09 14:00:45 +0200950
Radek Krejci76b07902015-10-09 09:11:25 +0200951 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200952}
953
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200954static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100955yang_print_list(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200956{
Michal Vasko1ef07972015-07-07 14:01:35 +0200957 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200958 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200959 struct lys_node_list *list = (struct lys_node_list *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200960
Radek Krejci76b07902015-10-09 09:11:25 +0200961 ly_print(out, "%*slist %s {\n", LEVEL, INDENT, node->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200962 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100963 yang_print_nacmext(out, level, node, node->module, NULL);
Radek Krejci8d81e002015-12-10 11:18:59 +0100964 if (list->when) {
965 yang_print_when(out, level, list->module, list->when);
966 }
Michal Vasko4773b762015-07-07 12:15:10 +0200967 for (i = 0; i < list->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200968 yang_print_iffeature(out, level, node->module, list->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200969 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100970 for (i = 0; i < list->must_size; i++) {
971 yang_print_must(out, level, list->module, &list->must[i]);
972 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200973 if (list->keys_size) {
Radek Krejci76b07902015-10-09 09:11:25 +0200974 ly_print(out, "%*skey \"", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200975 for (i = 0; i < list->keys_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200976 ly_print(out, "%s%s", list->keys[i]->name, i + 1 < list->keys_size ? " " : "");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200977 }
Radek Krejci76b07902015-10-09 09:11:25 +0200978 ly_print(out, "\";\n");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200979 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200980 for (i = 0; i < list->unique_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200981 yang_print_unique(out, level, &list->unique[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200982 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100983 yang_print_snode_common2(out, level, node, NULL);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200984 if (list->min > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200985 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, list->min);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200986 }
987 if (list->max > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200988 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, list->max);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200989 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100990 if (list->flags & LYS_USERORDERED) {
991 ly_print(out, "%*sordered-by user;\n", LEVEL, INDENT);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200992 }
Michal Vaskodff818b2015-06-09 14:01:12 +0200993
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200994 for (i = 0; i < list->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200995 yang_print_typedef(out, level, list->module, &list->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200996 }
Radek Krejci1d82ef62015-08-07 14:44:40 +0200997 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100998 /* augments */
999 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +02001000 continue;
1001 }
Radek Krejci76b07902015-10-09 09:11:25 +02001002 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +02001003 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
1004 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001005 }
1006 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001007 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001008}
1009
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001010static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001011yang_print_grouping(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001012{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001013 int i;
Michal Vasko0c5e9282016-02-15 13:11:57 +01001014 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001015 struct lys_node_grp *grp = (struct lys_node_grp *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001016
Radek Krejci76b07902015-10-09 09:11:25 +02001017 ly_print(out, "%*sgrouping %s {\n", LEVEL, INDENT, node->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001018 level++;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001019
Radek Krejci32cce7c2015-12-09 16:44:13 +01001020 yang_print_snode_common(out, level, node, NULL);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001021
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001022 for (i = 0; i < grp->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +02001023 yang_print_typedef(out, level, node->module, &grp->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001024 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001025
Michal Vasko0c5e9282016-02-15 13:11:57 +01001026 LY_TREE_FOR(node->child, sub) {
1027 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +02001028 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
1029 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001030 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001031
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001032 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001033 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001034}
1035
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001036static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001037yang_print_uses(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001038{
Radek Krejci32cce7c2015-12-09 16:44:13 +01001039 int i, flag = 0;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001040 struct lys_node_uses *uses = (struct lys_node_uses *)node;
Michal Vasko1bb7a5a2016-02-05 14:28:02 +01001041 struct lys_module *mod;
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001042
Radek Krejci76b07902015-10-09 09:11:25 +02001043 ly_print(out, "%*suses ", LEVEL, INDENT);
Michal Vasko1bb7a5a2016-02-05 14:28:02 +01001044 if (node->child) {
Michal Vasko6c629ac2016-02-15 14:08:23 +01001045 mod = lys_node_module(node->child);
Michal Vasko1dae8ec2016-02-15 14:49:01 +01001046 if (lys_node_module(node) != mod) {
Michal Vasko1bb7a5a2016-02-05 14:28:02 +01001047 ly_print(out, "%s:", transform_module_name2import_prefix(node->module, mod->name));
1048 }
Michal Vaskoc39c4b12015-07-07 14:55:33 +02001049 }
Radek Krejci32cce7c2015-12-09 16:44:13 +01001050 ly_print(out, "%s", uses->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001051 level++;
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001052
Radek Krejci32cce7c2015-12-09 16:44:13 +01001053 yang_print_nacmext(out, level, node, node->module, &flag);
1054 yang_print_snode_common(out, level, node, &flag);
Michal Vasko4773b762015-07-07 12:15:10 +02001055 for (i = 0; i < uses->features_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001056 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001057 yang_print_iffeature(out, level, node->module, uses->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +02001058 }
Michal Vasko1f0428a2015-07-07 14:55:04 +02001059 if (uses->when) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001060 yang_print_open(out, &flag);
Michal Vaskof9893382015-10-09 14:03:04 +02001061 yang_print_when(out, level, node->module, uses->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +02001062 }
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001063
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001064 for (i = 0; i < uses->refine_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001065 yang_print_open(out, &flag);
Michal Vaskof9893382015-10-09 14:03:04 +02001066 yang_print_refine(out, level, node->module, &uses->refine[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001067 }
Michal Vasko00b7cfe2015-06-09 13:56:38 +02001068
Michal Vasko6f25f212015-07-07 15:42:07 +02001069 for (i = 0; i < uses->augment_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001070 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001071 yang_print_augment(out, level, node->module, &uses->augment[i]);
Michal Vasko6f25f212015-07-07 15:42:07 +02001072 }
1073
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001074 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001075 yang_print_close(out, level, flag);
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001076}
1077
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001078static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001079yang_print_input_output(struct lyout *out, int level, const struct lys_node *node)
Michal Vasko5bbae102015-06-16 12:16:44 +02001080{
Michal Vaskof4d3d742015-06-16 11:51:09 +02001081 int i;
Radek Krejci76512572015-08-04 09:47:08 +02001082 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001083 struct lys_node_rpc_inout *inout = (struct lys_node_rpc_inout *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +02001084
Radek Krejci76b07902015-10-09 09:11:25 +02001085 ly_print(out, "%*s%s {\n", LEVEL, INDENT, (inout->nodetype == LYS_INPUT ? "input" : "output"));
Michal Vaskof4d3d742015-06-16 11:51:09 +02001086
1087 level++;
1088 for (i = 0; i < inout->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +02001089 yang_print_typedef(out, level, node->module, &inout->tpdf[i]);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001090 }
1091
Radek Krejci1d82ef62015-08-07 14:44:40 +02001092 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +01001093 /* augments */
1094 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +02001095 continue;
1096 }
Radek Krejci76b07902015-10-09 09:11:25 +02001097 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +02001098 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
1099 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001100 }
1101
1102 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001103 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001104}
1105
1106static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001107yang_print_rpc(struct lyout *out, int level, const struct lys_node *node)
Michal Vaskof4d3d742015-06-16 11:51:09 +02001108{
Radek Krejci32cce7c2015-12-09 16:44:13 +01001109 int i, flag = 0;
Radek Krejci76512572015-08-04 09:47:08 +02001110 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001111 struct lys_node_rpc *rpc = (struct lys_node_rpc *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +02001112
Radek Krejci32cce7c2015-12-09 16:44:13 +01001113 ly_print(out, "%*srpc %s", LEVEL, INDENT, node->name);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001114
1115 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001116 yang_print_snode_common(out, level, node, &flag);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001117
Michal Vasko4773b762015-07-07 12:15:10 +02001118 for (i = 0; i < rpc->features_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001119 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001120 yang_print_iffeature(out, level, node->module, rpc->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +02001121 }
1122
Michal Vaskof4d3d742015-06-16 11:51:09 +02001123 for (i = 0; i < rpc->tpdf_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001124 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001125 yang_print_typedef(out, level, node->module, &rpc->tpdf[i]);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001126 }
1127
Radek Krejci1d82ef62015-08-07 14:44:40 +02001128 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +01001129 /* augments */
1130 if (sub->parent != node) {
Radek Krejcic071c542016-01-27 14:57:51 +01001131 continue;
1132 }
Radek Krejci32cce7c2015-12-09 16:44:13 +01001133 yang_print_open(out, &flag);
1134 yang_print_snode(out, level, sub, LYS_GROUPING | LYS_INPUT | LYS_OUTPUT);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001135 }
1136
1137 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001138 yang_print_close(out, level, flag);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001139}
1140
1141static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001142yang_print_notif(struct lyout *out, int level, const struct lys_node *node)
Michal Vasko7690bc12015-06-16 12:26:05 +02001143{
Radek Krejci32cce7c2015-12-09 16:44:13 +01001144 int i, flag = 0;
Radek Krejci76512572015-08-04 09:47:08 +02001145 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001146 struct lys_node_notif *notif = (struct lys_node_notif *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +02001147
Radek Krejci32cce7c2015-12-09 16:44:13 +01001148 ly_print(out, "%*snotification %s", LEVEL, INDENT, node->name);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001149
1150 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001151 yang_print_snode_common(out, level, node, &flag);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001152
Michal Vasko4773b762015-07-07 12:15:10 +02001153 for (i = 0; i < notif->features_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001154 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001155 yang_print_iffeature(out, level, node->module, notif->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +02001156 }
1157
Michal Vaskof4d3d742015-06-16 11:51:09 +02001158 for (i = 0; i < notif->tpdf_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001159 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001160 yang_print_typedef(out, level, node->module, &notif->tpdf[i]);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001161 }
1162
Radek Krejci1d82ef62015-08-07 14:44:40 +02001163 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +01001164 /* augments */
1165 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +02001166 continue;
1167 }
Radek Krejci32cce7c2015-12-09 16:44:13 +01001168 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001169 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +02001170 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
1171 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001172 }
1173
1174 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001175 yang_print_close(out, level, flag);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001176}
1177
1178static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001179yang_print_snode(struct lyout *out, int level, const struct lys_node *node, int mask)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001180{
Radek Krejci1d82ef62015-08-07 14:44:40 +02001181 switch (node->nodetype & mask) {
Radek Krejci76512572015-08-04 09:47:08 +02001182 case LYS_CONTAINER:
Radek Krejci76b07902015-10-09 09:11:25 +02001183 yang_print_container(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001184 break;
Radek Krejci76512572015-08-04 09:47:08 +02001185 case LYS_CHOICE:
Radek Krejci76b07902015-10-09 09:11:25 +02001186 yang_print_choice(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001187 break;
Radek Krejci76512572015-08-04 09:47:08 +02001188 case LYS_LEAF:
Radek Krejci76b07902015-10-09 09:11:25 +02001189 yang_print_leaf(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001190 break;
Radek Krejci76512572015-08-04 09:47:08 +02001191 case LYS_LEAFLIST:
Radek Krejci76b07902015-10-09 09:11:25 +02001192 yang_print_leaflist(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001193 break;
Radek Krejci76512572015-08-04 09:47:08 +02001194 case LYS_LIST:
Radek Krejci76b07902015-10-09 09:11:25 +02001195 yang_print_list(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001196 break;
Radek Krejci76512572015-08-04 09:47:08 +02001197 case LYS_USES:
Radek Krejci76b07902015-10-09 09:11:25 +02001198 yang_print_uses(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001199 break;
Radek Krejci76512572015-08-04 09:47:08 +02001200 case LYS_GROUPING:
Radek Krejci76b07902015-10-09 09:11:25 +02001201 yang_print_grouping(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001202 break;
Radek Krejci76512572015-08-04 09:47:08 +02001203 case LYS_ANYXML:
Radek Krejci76b07902015-10-09 09:11:25 +02001204 yang_print_anyxml(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001205 break;
Radek Krejci76512572015-08-04 09:47:08 +02001206 case LYS_CASE:
Radek Krejci76b07902015-10-09 09:11:25 +02001207 yang_print_case(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001208 break;
Radek Krejci76512572015-08-04 09:47:08 +02001209 case LYS_INPUT:
1210 case LYS_OUTPUT:
Radek Krejci76b07902015-10-09 09:11:25 +02001211 yang_print_input_output(out, level, node);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001212 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001213 default:
1214 break;
1215 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001216}
1217
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001218int
Michal Vasko1e62a092015-12-01 12:27:20 +01001219yang_print_model(struct lyout *out, const struct lys_module *module)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001220{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001221 unsigned int i;
1222 int level = 0;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001223#define LEVEL (level*2)
1224
Radek Krejci1d82ef62015-08-07 14:44:40 +02001225 struct lys_node *node;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001226
Radek Krejci8d81e002015-12-10 11:18:59 +01001227 /* (sub)module-header-stmts */
Michal Vasko116172e2015-07-07 11:54:37 +02001228 if (module->type) {
Radek Krejci76b07902015-10-09 09:11:25 +02001229 ly_print(out, "submodule %s {%s\n", module->name, (module->deviated ? " // DEVIATED" : ""));
Michal Vasko116172e2015-07-07 11:54:37 +02001230 level++;
Radek Krejci8d81e002015-12-10 11:18:59 +01001231 if (module->version) {
Radek Krejcic071c542016-01-27 14:57:51 +01001232 ly_print(out, "%*syang-version %s;\n", LEVEL, INDENT,
1233 ((struct lys_submodule *)module)->belongsto->version == 2 ? "1.1" : "1");
Radek Krejci8d81e002015-12-10 11:18:59 +01001234 }
Radek Krejci76b07902015-10-09 09:11:25 +02001235 ly_print(out, "%*sbelongs-to %s {\n", LEVEL, INDENT, ((struct lys_submodule *)module)->belongsto->name);
Michal Vasko116172e2015-07-07 11:54:37 +02001236 level++;
Radek Krejci8d81e002015-12-10 11:18:59 +01001237 ly_print(out, "%*sprefix %s;\n", LEVEL, INDENT, module->prefix);
Michal Vasko116172e2015-07-07 11:54:37 +02001238 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001239 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko116172e2015-07-07 11:54:37 +02001240 } else {
Radek Krejci76b07902015-10-09 09:11:25 +02001241 ly_print(out, "module %s {%s\n", module->name, (module->deviated ? " // DEVIATED" : ""));
Michal Vasko116172e2015-07-07 11:54:37 +02001242 level++;
Radek Krejci8d81e002015-12-10 11:18:59 +01001243 if (module->version) {
Radek Krejcic071c542016-01-27 14:57:51 +01001244 ly_print(out, "%*syang-version %s;\n", LEVEL, INDENT, module->version == 2 ? "1.1" : "1");
Radek Krejci8d81e002015-12-10 11:18:59 +01001245 }
Radek Krejci76b07902015-10-09 09:11:25 +02001246 ly_print(out, "%*snamespace \"%s\";\n", LEVEL, INDENT, module->ns);
Radek Krejci8d81e002015-12-10 11:18:59 +01001247 ly_print(out, "%*sprefix %s;\n", LEVEL, INDENT, module->prefix);
Michal Vasko116172e2015-07-07 11:54:37 +02001248 }
Radek Krejcib0594bf2015-05-21 23:51:27 +02001249
Radek Krejci8d81e002015-12-10 11:18:59 +01001250 /* linkage-stmts */
1251 if (module->imp_size || module->inc_size) {
1252 ly_print(out, "\n");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001253 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001254 for (i = 0; i < module->imp_size; i++) {
Radek Krejcic071c542016-01-27 14:57:51 +01001255 if (module->imp[i].external) {
1256 continue;
1257 }
Michal Vaskoc8e3ce02016-02-12 14:28:35 +01001258 ly_print(out, "%*simport %s {\n", LEVEL, INDENT, module->imp[i].module->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001259 level++;
Radek Krejci8d81e002015-12-10 11:18:59 +01001260 ly_print(out, "%*sprefix %s;\n", LEVEL, INDENT, module->imp[i].prefix);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001261 if (module->imp[i].rev[0]) {
Michal Vasko86dfd262016-02-15 14:26:31 +01001262 ly_print(out, "%*srevision-date %s;\n", LEVEL, INDENT, module->imp[i].rev);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001263 }
1264 level--;
Michal Vaskoc8e3ce02016-02-12 14:28:35 +01001265 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001266 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001267 for (i = 0; i < module->inc_size; i++) {
Radek Krejcic071c542016-01-27 14:57:51 +01001268 if (module->inc[i].external) {
1269 continue;
1270 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001271 if (module->inc[i].rev[0]) {
Radek Krejci76b07902015-10-09 09:11:25 +02001272 ly_print(out, "%*sinclude \"%s\" {\n", LEVEL, INDENT, module->inc[i].submodule->name);
Radek Krejci8d81e002015-12-10 11:18:59 +01001273 level++;
Michal Vasko3a4e2d02016-02-15 15:12:46 +01001274 ly_print(out, "%*srevision-date %s;\n", LEVEL, INDENT, module->inc[i].rev);
Radek Krejci8d81e002015-12-10 11:18:59 +01001275 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001276 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001277 } else {
Radek Krejci76b07902015-10-09 09:11:25 +02001278 ly_print(out, "%*sinclude \"%s\";\n", LEVEL, INDENT, module->inc[i].submodule->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001279 }
1280 }
Radek Krejciefaeba32015-05-27 14:30:57 +02001281
Radek Krejci8d81e002015-12-10 11:18:59 +01001282 /* meta-stmts */
1283 if (module->org || module->contact || module->dsc || module->ref) {
1284 ly_print(out, "\n");
1285 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001286 if (module->org) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001287 yang_print_text(out, level, "organization", module->org, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001288 }
1289 if (module->contact) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001290 yang_print_text(out, level, "contact", module->contact, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001291 }
1292 if (module->dsc) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001293 yang_print_text(out, level, "description", module->dsc, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001294 }
1295 if (module->ref) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001296 yang_print_text(out, level, "reference", module->ref, 0);
1297 }
1298
1299 /* revision-stmts */
1300 if (module->rev_size) {
1301 ly_print(out, "\n");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001302 }
1303 for (i = 0; i < module->rev_size; i++) {
1304 if (module->rev[i].dsc || module->rev[i].ref) {
Radek Krejci76b07902015-10-09 09:11:25 +02001305 ly_print(out, "%*srevision \"%s\" {\n", LEVEL, INDENT, module->rev[i].date);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001306 level++;
1307 if (module->rev[i].dsc) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001308 yang_print_text(out, level, "description", module->rev[i].dsc, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001309 }
1310 if (module->rev[i].ref) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001311 yang_print_text(out, level, "reference", module->rev[i].ref, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001312 }
1313 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001314 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001315 } else {
Michal Vasko86dfd262016-02-15 14:26:31 +01001316 ly_print(out, "%*srevision %s;\n", LEVEL, INDENT, module->rev[i].date);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001317 }
1318 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001319
Radek Krejci8d81e002015-12-10 11:18:59 +01001320 /* body-stmts */
Michal Vasko30f6e912015-07-07 12:24:27 +02001321 for (i = 0; i < module->features_size; i++) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001322 ly_print(out, "\n");
Radek Krejci76b07902015-10-09 09:11:25 +02001323 yang_print_feature(out, level, &module->features[i]);
Michal Vasko30f6e912015-07-07 12:24:27 +02001324 }
1325
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001326 for (i = 0; i < module->ident_size; i++) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001327 ly_print(out, "\n");
Radek Krejci76b07902015-10-09 09:11:25 +02001328 yang_print_identity(out, level, &module->ident[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001329 }
Radek Krejci6793db02015-05-22 17:49:54 +02001330
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001331 for (i = 0; i < module->tpdf_size; i++) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001332 ly_print(out, "\n");
Radek Krejci76b07902015-10-09 09:11:25 +02001333 yang_print_typedef(out, level, module, &module->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001334 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001335
Radek Krejci8d81e002015-12-10 11:18:59 +01001336 for (i = 0; i < module->deviation_size; ++i) {
1337 ly_print(out, "\n");
1338 yang_print_deviation(out, level, module, &module->deviation[i]);
1339 }
1340
Radek Krejcic4283442016-04-22 09:19:27 +02001341 LY_TREE_FOR(lys_main_module(module)->data, node) {
Radek Krejcic071c542016-01-27 14:57:51 +01001342 if (node->module != module) {
Michal Vasko0c5e9282016-02-15 13:11:57 +01001343 /* data from submodules */
Radek Krejcic071c542016-01-27 14:57:51 +01001344 continue;
1345 }
Radek Krejci8d81e002015-12-10 11:18:59 +01001346 ly_print(out, "\n");
Radek Krejci92720552015-10-05 15:28:27 +02001347 switch(node->nodetype) {
1348 case LYS_RPC:
Radek Krejcibac81762015-10-09 10:19:52 +02001349 yang_print_rpc(out, level, node);
Radek Krejci92720552015-10-05 15:28:27 +02001350 break;
1351 case LYS_NOTIF:
Radek Krejcibac81762015-10-09 10:19:52 +02001352 yang_print_notif(out, level, node);
Radek Krejci92720552015-10-05 15:28:27 +02001353 break;
1354 default:
Radek Krejcibac81762015-10-09 10:19:52 +02001355 yang_print_snode(out, level, node,
Radek Krejci92720552015-10-05 15:28:27 +02001356 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
1357 LYS_USES | LYS_GROUPING | LYS_ANYXML);
1358 break;
1359 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001360 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001361
Michal Vasko6f25f212015-07-07 15:42:07 +02001362 for (i = 0; i < module->augment_size; i++) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001363 ly_print(out, "\n");
Radek Krejci76b07902015-10-09 09:11:25 +02001364 yang_print_augment(out, level, module, &module->augment[i]);
Michal Vasko6f25f212015-07-07 15:42:07 +02001365 }
1366
Radek Krejci76b07902015-10-09 09:11:25 +02001367 ly_print(out, "}\n");
Michal Vasko95068c42016-03-24 14:58:11 +01001368 ly_print_flush(out);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001369
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001370 return EXIT_SUCCESS;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001371#undef LEVEL
1372}