blob: 9df84b6fa03ecdd61d7ed19a2e16ab3a95a21113 [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
Michal Vaskodcf98e62016-05-05 17:53:53 +0200136 if (node->nacm && (!lys_parent(node) || lys_parent(node)->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
Michal Vaskodcf98e62016-05-05 17:53:53 +0200161 if ((node->nacm & LYS_NACM_DENYW) && (!lys_parent(node) || !(lys_parent(node)->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 }
Michal Vaskodcf98e62016-05-05 17:53:53 +0200165 if ((node->nacm & LYS_NACM_DENYA) && (!lys_parent(node) || !(lys_parent(node)->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 Vaskodcf98e62016-05-05 17:53:53 +0200208 if (lys_parent(node)) {
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
Radek Krejci9ff0a922016-07-14 13:08:05 +0200237yang_print_iffeature(struct lyout *out, int level, const struct lys_module *module, struct lys_iffeature *iffeature)
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200238{
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200239 ly_print(out, "%*sif-feature \"", LEVEL, INDENT);
Radek Krejci9ff0a922016-07-14 13:08:05 +0200240 ly_print_iffeature(out, module, iffeature);
241 ly_print(out, "\";\n");
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200242}
243
244static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100245yang_print_feature(struct lyout *out, int level, const struct lys_feature *feat)
Michal Vasko4773b762015-07-07 12:15:10 +0200246{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100247 int i, flag = 0;
Michal Vasko30f6e912015-07-07 12:24:27 +0200248
Radek Krejci32cce7c2015-12-09 16:44:13 +0100249 ly_print(out, "%*sfeature %s", LEVEL, INDENT, feat->name);
Michal Vasko4773b762015-07-07 12:15:10 +0200250 level++;
251
Radek Krejci32cce7c2015-12-09 16:44:13 +0100252 yang_print_snode_common(out, level, (struct lys_node *)feat, &flag);
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200253 for (i = 0; i < feat->iffeature_size; ++i) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100254 yang_print_open(out, &flag);
Radek Krejci9ff0a922016-07-14 13:08:05 +0200255 yang_print_iffeature(out, level, feat->module, &feat->iffeature[i]);
Michal Vasko30f6e912015-07-07 12:24:27 +0200256 }
Michal Vasko4773b762015-07-07 12:15:10 +0200257
258 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100259 yang_print_close(out, level, flag);
Michal Vasko4773b762015-07-07 12:15:10 +0200260}
261
262static void
Radek Krejci32cce7c2015-12-09 16:44:13 +0100263yang_print_restr(struct lyout *out, int level, const struct lys_restr *restr, int *flag)
Radek Krejci41726f92015-06-19 13:11:05 +0200264{
Radek Krejci0bd5db42015-06-19 13:30:07 +0200265 if (restr->dsc != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100266 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100267 yang_print_text(out, level, "description", restr->dsc, 0);
Radek Krejci41726f92015-06-19 13:11:05 +0200268 }
Radek Krejci0bd5db42015-06-19 13:30:07 +0200269 if (restr->ref != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100270 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100271 yang_print_text(out, level, "reference", restr->ref, 0);
Radek Krejci41726f92015-06-19 13:11:05 +0200272 }
Radek Krejci0bd5db42015-06-19 13:30:07 +0200273 if (restr->eapptag != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100274 yang_print_open(out, flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200275 ly_print(out, "%*serror-app-tag \"%s\";\n", LEVEL, INDENT, restr->eapptag);
Radek Krejci41726f92015-06-19 13:11:05 +0200276 }
Radek Krejci0bd5db42015-06-19 13:30:07 +0200277 if (restr->emsg != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100278 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100279 yang_print_text(out, level, "error-message", restr->emsg, 0);
Radek Krejci41726f92015-06-19 13:11:05 +0200280 }
281}
282
283static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100284yang_print_when(struct lyout *out, int level, const struct lys_module *module, const struct lys_when *when)
Michal Vasko1f0428a2015-07-07 14:55:04 +0200285{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100286 int flag = 0;
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100287 const char *str;
Michal Vaskof9893382015-10-09 14:03:04 +0200288
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100289 str = transform_json2schema(module, when->cond);
290 if (!str) {
Michal Vaskof9893382015-10-09 14:03:04 +0200291 ly_print(out, "(!error!)");
292 return;
293 }
294
Michal Vasko25cb6c62016-02-12 14:36:21 +0100295 ly_print(out, "%*swhen \"", LEVEL, INDENT);
296 yang_encode(out, str, -1);
297 ly_print(out, "\"");
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100298 lydict_remove(module->ctx, str);
Michal Vaskof9893382015-10-09 14:03:04 +0200299
Michal Vasko1f0428a2015-07-07 14:55:04 +0200300 level++;
301 if (when->dsc) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100302 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100303 yang_print_text(out, level, "description", when->dsc, 0);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200304 }
305 if (when->ref) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100306 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100307 yang_print_text(out, level, "reference", when->ref, 0);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200308 }
309 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100310 yang_print_close(out, level, flag);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200311}
312
313static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100314yang_print_type(struct lyout *out, int level, const struct lys_module *module, const struct lys_type *type)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200315{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200316 int i;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100317 int flag = 0, flag2;
Michal Vasko0fb82c62015-10-20 13:41:53 +0200318 const char *str;
Radek Krejcic071c542016-01-27 14:57:51 +0100319 struct lys_module *mod;
Radek Krejci25d782a2015-05-22 15:03:23 +0200320
Michal Vasko0fb82c62015-10-20 13:41:53 +0200321 if (type->module_name) {
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100322 ly_print(out, "%*stype %s:%s", LEVEL, INDENT,
323 transform_module_name2import_prefix(module, type->module_name), type->der->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200324 } else {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100325 ly_print(out, "%*stype %s", LEVEL, INDENT, type->der->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200326 }
327 level++;
328 switch (type->base) {
329 case LY_TYPE_BINARY:
Michal Vasko4634cda2016-02-16 09:22:09 +0100330 if (type->info.binary.length) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100331 yang_print_open(out, &flag);
Michal Vasko25cb6c62016-02-12 14:36:21 +0100332 ly_print(out, "%*slength \"", LEVEL, INDENT);
333 yang_encode(out, type->info.binary.length->expr, -1);
334 ly_print(out, "\"");
Radek Krejci32cce7c2015-12-09 16:44:13 +0100335 flag2 = 0;
336 yang_print_restr(out, level + 1, type->info.binary.length, &flag2);
337 yang_print_close(out, level, flag2);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200338 }
339 break;
340 case LY_TYPE_BITS:
341 for (i = 0; i < type->info.bits.count; ++i) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100342 yang_print_open(out, &flag);
Michal Vasko3f053ef2016-02-12 14:27:13 +0100343 ly_print(out, "%*sbit %s", LEVEL, INDENT, type->info.bits.bit[i].name);
344 flag2 = 0;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200345 level++;
Michal Vasko3f053ef2016-02-12 14:27:13 +0100346 yang_print_snode_common(out, level, (struct lys_node *)&type->info.bits.bit[i], &flag2);
347 if (!(type->info.bits.bit[i].flags & LYS_AUTOASSIGNED)) {
348 yang_print_open(out, &flag2);
349 ly_print(out, "%*sposition %u;\n", LEVEL, INDENT, type->info.bits.bit[i].pos);
350 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200351 level--;
Michal Vasko3f053ef2016-02-12 14:27:13 +0100352 yang_print_close(out, level, flag2);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200353 }
354 break;
355 case LY_TYPE_DEC64:
Michal Vasko4634cda2016-02-16 09:22:09 +0100356 if (type->info.dec64.dig) {
357 yang_print_open(out, &flag);
358 ly_print(out, "%*sfraction-digits %d;\n", LEVEL, INDENT, type->info.dec64.dig);
359 }
Michal Vaskoea505ee2015-07-07 14:01:00 +0200360 if (type->info.dec64.range != NULL) {
Michal Vasko4634cda2016-02-16 09:22:09 +0100361 yang_print_open(out, &flag);
Michal Vasko25cb6c62016-02-12 14:36:21 +0100362 ly_print(out, "%*srange \"", LEVEL, INDENT);
363 yang_encode(out, type->info.dec64.range->expr, -1);
364 ly_print(out, "\"");
Radek Krejci32cce7c2015-12-09 16:44:13 +0100365 flag2 = 0;
366 yang_print_restr(out, level + 1, type->info.dec64.range, &flag2);
367 yang_print_close(out, level, flag2);
Michal Vaskoea505ee2015-07-07 14:01:00 +0200368 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200369 break;
370 case LY_TYPE_ENUM:
371 for (i = 0; i < type->info.enums.count; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100372 yang_print_open(out, &flag);
Michal Vasko3f053ef2016-02-12 14:27:13 +0100373 ly_print(out, "%*senum \"%s\"", LEVEL, INDENT, type->info.enums.enm[i].name);
374 flag2 = 0;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200375 level++;
Michal Vasko3f053ef2016-02-12 14:27:13 +0100376 yang_print_snode_common(out, level, (struct lys_node *)&type->info.enums.enm[i], &flag2);
377 if (!(type->info.enums.enm[i].flags & LYS_AUTOASSIGNED)) {
378 yang_print_open(out, &flag2);
379 ly_print(out, "%*svalue %d;\n", LEVEL, INDENT, type->info.enums.enm[i].value);
380 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200381 level--;
Michal Vasko3f053ef2016-02-12 14:27:13 +0100382 yang_print_close(out, level, flag2);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200383 }
384 break;
385 case LY_TYPE_IDENT:
Michal Vasko4634cda2016-02-16 09:22:09 +0100386 if (type->info.ident.ref) {
387 yang_print_open(out, &flag);
Radek Krejcic4283442016-04-22 09:19:27 +0200388 mod = lys_main_module(type->info.ident.ref->module);
389 if (lys_main_module(module) == mod) {
Michal Vasko4634cda2016-02-16 09:22:09 +0100390 ly_print(out, "%*sbase %s;\n", LEVEL, INDENT, type->info.ident.ref->name);
391 } else {
392 ly_print(out, "%*sbase %s:%s;\n", LEVEL, INDENT, transform_module_name2import_prefix(module, mod->name),
393 type->info.ident.ref->name);
394 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200395 }
396 break;
397 case LY_TYPE_INST:
Radek Krejciaf351422015-06-19 14:49:38 +0200398 if (type->info.inst.req == 1) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100399 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100400 ly_print(out, "%*srequire-instance true;\n", LEVEL, INDENT);
Radek Krejciaf351422015-06-19 14:49:38 +0200401 } else if (type->info.inst.req == -1) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100402 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100403 ly_print(out, "%*srequire-instance false;\n", LEVEL, INDENT);
Radek Krejciaf351422015-06-19 14:49:38 +0200404 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200405 break;
406 case LY_TYPE_INT8:
407 case LY_TYPE_INT16:
408 case LY_TYPE_INT32:
409 case LY_TYPE_INT64:
410 case LY_TYPE_UINT8:
411 case LY_TYPE_UINT16:
412 case LY_TYPE_UINT32:
413 case LY_TYPE_UINT64:
Michal Vasko4634cda2016-02-16 09:22:09 +0100414 if (type->info.num.range) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100415 yang_print_open(out, &flag);
Michal Vasko25cb6c62016-02-12 14:36:21 +0100416 ly_print(out, "%*srange \"", LEVEL, INDENT);
417 yang_encode(out, type->info.num.range->expr, -1);
418 ly_print(out, "\"");
Radek Krejci32cce7c2015-12-09 16:44:13 +0100419 flag2 = 0;
420 yang_print_restr(out, level + 1, type->info.num.range, &flag2);
421 yang_print_close(out, level, flag2);
Radek Krejcif2860132015-06-20 12:37:20 +0200422 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200423 break;
424 case LY_TYPE_LEAFREF:
Radek Krejci0dbff6a2016-07-17 12:40:18 +0200425 if (ly_strequal(type->der->name, "leafref", 0)) {
Michal Vasko4634cda2016-02-16 09:22:09 +0100426 yang_print_open(out, &flag);
427 str = transform_json2schema(module, type->info.lref.path);
428 ly_print(out, "%*spath \"%s\";\n", LEVEL, INDENT, str);
429 lydict_remove(module->ctx, str);
430 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200431 break;
432 case LY_TYPE_STRING:
Radek Krejci5fbc9162015-06-19 14:11:11 +0200433 if (type->info.str.length) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100434 yang_print_open(out, &flag);
Michal Vasko25cb6c62016-02-12 14:36:21 +0100435 ly_print(out, "%*slength \"", LEVEL, INDENT);
436 yang_encode(out, type->info.str.length->expr, -1);
437 ly_print(out, "\"");
Radek Krejci32cce7c2015-12-09 16:44:13 +0100438 flag2 = 0;
439 yang_print_restr(out, level + 1, type->info.str.length, &flag2);
440 yang_print_close(out, level, flag2);
Radek Krejci061bd522015-06-19 13:45:16 +0200441 }
Radek Krejci5fbc9162015-06-19 14:11:11 +0200442 for (i = 0; i < type->info.str.pat_count; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100443 yang_print_open(out, &flag);
Michal Vasko25cb6c62016-02-12 14:36:21 +0100444 ly_print(out, "%*spattern \"", LEVEL, INDENT);
445 yang_encode(out, type->info.str.patterns[i].expr, -1);
446 ly_print(out, "\"");
Radek Krejci32cce7c2015-12-09 16:44:13 +0100447 flag2 = 0;
448 yang_print_restr(out, level + 1, &type->info.str.patterns[i], &flag2);
449 yang_print_close(out, level, flag2);
Radek Krejci5fbc9162015-06-19 14:11:11 +0200450 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200451 break;
452 case LY_TYPE_UNION:
453 for (i = 0; i < type->info.uni.count; ++i) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100454 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200455 yang_print_type(out, level, module, &type->info.uni.types[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200456 }
457 break;
458 default:
459 /* other types do not have substatements */
460 break;
461 }
462 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100463 yang_print_close(out, level, flag);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200464}
465
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200466static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100467yang_print_must(struct lyout *out, int level, const struct lys_module *module, const struct lys_restr *must)
Michal Vasko7f976ee2015-06-09 13:55:41 +0200468{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100469 int flag = 0;
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100470 const char *str;
Michal Vaskof9893382015-10-09 14:03:04 +0200471
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100472 str = transform_json2schema(module, must->expr);
473 if (!str) {
Michal Vaskof9893382015-10-09 14:03:04 +0200474 ly_print(out, "(!error!)");
475 return;
476 }
477
Michal Vasko25cb6c62016-02-12 14:36:21 +0100478 ly_print(out, "%*smust \"", LEVEL, INDENT);
479 yang_encode(out, str, -1);
480 ly_print(out, "\"");
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100481 lydict_remove(module->ctx, str);
Michal Vaskof9893382015-10-09 14:03:04 +0200482
Radek Krejci32cce7c2015-12-09 16:44:13 +0100483 yang_print_restr(out, level + 1, must, &flag);
484 yang_print_close(out, level, flag);
Michal Vasko7f976ee2015-06-09 13:55:41 +0200485}
486
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200487static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100488yang_print_unique(struct lyout *out, int level, const struct lys_unique *uniq)
Michal Vasko1ef07972015-07-07 14:01:35 +0200489{
490 int i;
491
Radek Krejci76b07902015-10-09 09:11:25 +0200492 ly_print(out, "%*sunique \"", LEVEL, INDENT);
Radek Krejci581ce772015-11-10 17:22:40 +0100493 for (i = 0; i < uniq->expr_size; i++) {
494 ly_print(out, "%s%s", uniq->expr[i], i + 1 < uniq->expr_size ? " " : "");
Michal Vasko1ef07972015-07-07 14:01:35 +0200495 }
Radek Krejci76b07902015-10-09 09:11:25 +0200496 ly_print(out, "\";\n");
Michal Vasko1ef07972015-07-07 14:01:35 +0200497}
498
499static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100500yang_print_refine(struct lyout *out, int level, const struct lys_module *module, const struct lys_refine *refine)
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200501{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200502 int i;
Michal Vaskoa8b25952015-10-20 15:30:25 +0200503 const char *str;
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200504
Michal Vasko5d112852016-02-12 16:47:13 +0100505 str = transform_json2schema(module, refine->target_name);
Michal Vaskoa8b25952015-10-20 15:30:25 +0200506 ly_print(out, "%*srefine \"%s\" {\n", LEVEL, INDENT, str);
507 lydict_remove(module->ctx, str);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200508 level++;
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200509
Radek Krejci1574a8d2015-08-03 14:16:52 +0200510 if (refine->flags & LYS_CONFIG_W) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100511 ly_print(out, "%*sconfig true;\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200512 } else if (refine->flags & LYS_CONFIG_R) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100513 ly_print(out, "%*sconfig false;\n", LEVEL, INDENT);
Radek Krejci6a113852015-07-03 16:04:20 +0200514 }
515
Radek Krejci1574a8d2015-08-03 14:16:52 +0200516 if (refine->flags & LYS_MAND_TRUE) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100517 ly_print(out, "%*smandatory true;\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200518 } else if (refine->flags & LYS_MAND_FALSE) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100519 ly_print(out, "%*smandatory false;\n", LEVEL, INDENT);
Radek Krejci6a113852015-07-03 16:04:20 +0200520 }
521
Radek Krejci32cce7c2015-12-09 16:44:13 +0100522 yang_print_snode_common(out, level, (struct lys_node *)refine, NULL);
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200523
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200524 for (i = 0; i < refine->must_size; ++i) {
Michal Vaskof9893382015-10-09 14:03:04 +0200525 yang_print_must(out, level, module, &refine->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200526 }
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200527
Radek Krejci76512572015-08-04 09:47:08 +0200528 if (refine->target_type & (LYS_LEAF | LYS_CHOICE)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200529 if (refine->mod.dflt != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200530 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, refine->mod.dflt);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200531 }
Radek Krejci76512572015-08-04 09:47:08 +0200532 } else if (refine->target_type == LYS_CONTAINER) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200533 if (refine->mod.presence != NULL) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100534 yang_print_text(out, level, "presence", refine->mod.presence, 1);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200535 }
Radek Krejci76512572015-08-04 09:47:08 +0200536 } else if (refine->target_type & (LYS_LIST | LYS_LEAFLIST)) {
Radek Krejci0f04a6c2016-04-14 16:16:36 +0200537 if (refine->flags & LYS_RFN_MINSET) {
Radek Krejci76b07902015-10-09 09:11:25 +0200538 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, refine->mod.list.min);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200539 }
Radek Krejci0f04a6c2016-04-14 16:16:36 +0200540 if (refine->flags & LYS_RFN_MAXSET) {
Radek Krejci0d7b2472016-02-12 11:11:03 +0100541 if (refine->mod.list.max) {
542 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, refine->mod.list.max);
543 } else {
544 ly_print(out, "%*smax-elements \"unbounded\";\n", LEVEL, INDENT);
545 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200546 }
547 }
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200548
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200549 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200550 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200551}
552
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200553static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100554yang_print_deviation(struct lyout *out, int level, const struct lys_module *module,
555 const struct lys_deviation *deviation)
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200556{
557 int i, j;
Michal Vaskoa8b25952015-10-20 15:30:25 +0200558 const char *str;
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200559
Michal Vasko5d112852016-02-12 16:47:13 +0100560 str = transform_json2schema(module, deviation->target_name);
Michal Vaskoa8b25952015-10-20 15:30:25 +0200561 ly_print(out, "%*sdeviation \"%s\" {\n", LEVEL, INDENT, str);
562 lydict_remove(module->ctx, str);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200563 level++;
564
565 if (deviation->dsc) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100566 yang_print_text(out, level, "description", deviation->dsc, 0);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200567 }
568 if (deviation->ref) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100569 yang_print_text(out, level, "reference", deviation->ref, 0);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200570 }
571
572 for (i = 0; i < deviation->deviate_size; ++i) {
Radek Krejci76b07902015-10-09 09:11:25 +0200573 ly_print(out, "%*sdeviate ", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200574 if (deviation->deviate[i].mod == LY_DEVIATE_NO) {
Michal Vaskod875e882016-05-19 10:57:30 +0200575 ly_print(out, "not-supported;\n");
576 continue;
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200577 } else if (deviation->deviate[i].mod == LY_DEVIATE_ADD) {
Radek Krejci76b07902015-10-09 09:11:25 +0200578 ly_print(out, "add {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200579 } else if (deviation->deviate[i].mod == LY_DEVIATE_RPL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200580 ly_print(out, "replace {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200581 } else if (deviation->deviate[i].mod == LY_DEVIATE_DEL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200582 ly_print(out, "delete {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200583 }
584 level++;
585
Radek Krejci1574a8d2015-08-03 14:16:52 +0200586 if (deviation->deviate[i].flags & LYS_CONFIG_W) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100587 ly_print(out, "%*sconfig true;\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200588 } else if (deviation->deviate[i].flags & LYS_CONFIG_R) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100589 ly_print(out, "%*sconfig false;\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200590 }
591
Radek Krejci1574a8d2015-08-03 14:16:52 +0200592 if (deviation->deviate[i].flags & LYS_MAND_TRUE) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100593 ly_print(out, "%*smandatory true;\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200594 } else if (deviation->deviate[i].flags & LYS_MAND_FALSE) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100595 ly_print(out, "%*smandatory false;\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200596 }
597
598 if (deviation->deviate[i].dflt) {
Michal Vaskod875e882016-05-19 10:57:30 +0200599 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, deviation->deviate[i].dflt);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200600 }
601
Radek Krejci0d7b2472016-02-12 11:11:03 +0100602 if (deviation->deviate[i].min_set) {
Radek Krejci76b07902015-10-09 09:11:25 +0200603 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, deviation->deviate[i].min);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200604 }
Radek Krejci0d7b2472016-02-12 11:11:03 +0100605 if (deviation->deviate[i].max_set) {
606 if (deviation->deviate[i].max) {
607 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, deviation->deviate[i].max);
608 } else {
609 ly_print(out, "%*smax-elements \"unbounded\";\n", LEVEL, INDENT);
610 }
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200611 }
612
613 for (j = 0; j < deviation->deviate[i].must_size; ++j) {
Michal Vaskof9893382015-10-09 14:03:04 +0200614 yang_print_must(out, level, module, &deviation->deviate[i].must[j]);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200615 }
616
617 for (j = 0; j < deviation->deviate[i].unique_size; ++j) {
Radek Krejci76b07902015-10-09 09:11:25 +0200618 yang_print_unique(out, level, &deviation->deviate[i].unique[j]);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200619 }
620
621 if (deviation->deviate[i].type) {
Radek Krejci76b07902015-10-09 09:11:25 +0200622 yang_print_type(out, level, module, deviation->deviate[i].type);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200623 }
624
625 if (deviation->deviate[i].units) {
Michal Vaskod875e882016-05-19 10:57:30 +0200626 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, deviation->deviate[i].units);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200627 }
628
629 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200630 ly_print(out, "%*s}\n", LEVEL, INDENT);
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
637static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100638yang_print_augment(struct lyout *out, int level, const struct lys_module *module,
639 const struct lys_node_augment *augment)
Michal Vasko6f25f212015-07-07 15:42:07 +0200640{
641 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200642 struct lys_node *sub;
Michal Vasko488c19e2015-10-20 15:21:00 +0200643 const char *str;
Michal Vasko6f25f212015-07-07 15:42:07 +0200644
Michal Vasko5d112852016-02-12 16:47:13 +0100645 str = transform_json2schema(module, augment->target_name);
Michal Vasko488c19e2015-10-20 15:21:00 +0200646 ly_print(out, "%*saugment \"%s\" {\n", LEVEL, INDENT, str);
647 lydict_remove(module->ctx, str);
Michal Vasko6f25f212015-07-07 15:42:07 +0200648 level++;
649
Radek Krejci32cce7c2015-12-09 16:44:13 +0100650 yang_print_nacmext(out, level, (struct lys_node *)augment, module, NULL);
651 yang_print_snode_common(out, level, (struct lys_node *)augment, NULL);
Michal Vasko6f25f212015-07-07 15:42:07 +0200652
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200653 for (i = 0; i < augment->iffeature_size; i++) {
Radek Krejci9ff0a922016-07-14 13:08:05 +0200654 yang_print_iffeature(out, level, module, &augment->iffeature[i]);
Michal Vasko6f25f212015-07-07 15:42:07 +0200655 }
656
657 if (augment->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200658 yang_print_when(out, level, module, augment->when);
Michal Vasko6f25f212015-07-07 15:42:07 +0200659 }
660
661 LY_TREE_FOR(augment->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100662 /* only our augment */
663 if (sub->parent != (struct lys_node *)augment) {
664 continue;
665 }
Radek Krejci76b07902015-10-09 09:11:25 +0200666 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200667 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200668 LYS_USES | LYS_ANYXML | LYS_CASE | LYS_ACTION);
Michal Vasko6f25f212015-07-07 15:42:07 +0200669 }
670
671 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200672 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko6f25f212015-07-07 15:42:07 +0200673}
674
675static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100676yang_print_typedef(struct lyout *out, int level, const struct lys_module *module, const struct lys_tpdf *tpdf)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200677{
Radek Krejci76b07902015-10-09 09:11:25 +0200678 ly_print(out, "%*stypedef %s {\n", LEVEL, INDENT, tpdf->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200679 level++;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200680
Radek Krejci32cce7c2015-12-09 16:44:13 +0100681 yang_print_snode_common(out, level, (struct lys_node *)tpdf, NULL);
Radek Krejci76b07902015-10-09 09:11:25 +0200682 yang_print_type(out, level, module, &tpdf->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200683 if (tpdf->units != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200684 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, tpdf->units);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200685 }
686 if (tpdf->dflt != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200687 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, tpdf->dflt);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200688 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200689
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200690 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200691 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200692}
693
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200694static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100695yang_print_identity(struct lyout *out, int level, const struct lys_ident *ident)
Radek Krejci6793db02015-05-22 17:49:54 +0200696{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100697 int flag = 0;
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100698 struct lys_module *mod;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100699
700 ly_print(out, "%*sidentity %s", LEVEL, INDENT, ident->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200701 level++;
Radek Krejci6793db02015-05-22 17:49:54 +0200702
Radek Krejci32cce7c2015-12-09 16:44:13 +0100703 yang_print_snode_common(out, level, (struct lys_node *)ident, &flag);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200704 if (ident->base) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100705 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200706 ly_print(out, "%*sbase ", LEVEL, INDENT);
Radek Krejcic4283442016-04-22 09:19:27 +0200707 mod = lys_main_module(ident->base->module);
708 if (lys_main_module(ident->module) != mod) {
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100709 ly_print(out, "%s:", transform_module_name2import_prefix(ident->module, mod->name));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200710 }
Radek Krejci76b07902015-10-09 09:11:25 +0200711 ly_print(out, "%s;\n", ident->base->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200712 }
Radek Krejci6793db02015-05-22 17:49:54 +0200713
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200714 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100715 yang_print_close(out, level, flag);
Radek Krejci6793db02015-05-22 17:49:54 +0200716}
717
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200718static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100719yang_print_container(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200720{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100721 int i, flag = 0;
Radek Krejci76512572015-08-04 09:47:08 +0200722 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200723 struct lys_node_container *cont = (struct lys_node_container *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200724
Radek Krejci32cce7c2015-12-09 16:44:13 +0100725 ly_print(out, "%*scontainer %s", LEVEL, INDENT, node->name);
Michal Vasko7f976ee2015-06-09 13:55:41 +0200726
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200727 level++;
Radek Krejci6a113852015-07-03 16:04:20 +0200728
Radek Krejci32cce7c2015-12-09 16:44:13 +0100729 yang_print_nacmext(out, level, node, node->module, &flag);
Radek Krejci6a113852015-07-03 16:04:20 +0200730
Radek Krejci8d81e002015-12-10 11:18:59 +0100731 if (cont->when) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100732 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100733 yang_print_when(out, level, node->module, cont->when);
734 }
735
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200736 for (i = 0; i < cont->iffeature_size; i++) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100737 yang_print_open(out, &flag);
Radek Krejci9ff0a922016-07-14 13:08:05 +0200738 yang_print_iffeature(out, level, node->module, &cont->iffeature[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200739 }
Michal Vasko7f976ee2015-06-09 13:55:41 +0200740
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200741 for (i = 0; i < cont->must_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100742 yang_print_open(out, &flag);
Michal Vaskof9893382015-10-09 14:03:04 +0200743 yang_print_must(out, level, node->module, &cont->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200744 }
Michal Vasko7f976ee2015-06-09 13:55:41 +0200745
Radek Krejci8d81e002015-12-10 11:18:59 +0100746 if (cont->presence != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100747 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100748 yang_print_text(out, level, "presence", cont->presence, 1);
Michal Vasko4773b762015-07-07 12:15:10 +0200749 }
750
Radek Krejci8d81e002015-12-10 11:18:59 +0100751 yang_print_snode_common2(out, level, node, &flag);
752
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200753 for (i = 0; i < cont->tpdf_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100754 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200755 yang_print_typedef(out, level, node->module, &cont->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200756 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200757
Radek Krejci1d82ef62015-08-07 14:44:40 +0200758 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100759 /* augments */
760 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200761 continue;
762 }
Radek Krejci32cce7c2015-12-09 16:44:13 +0100763 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200764 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200765 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200766 LYS_USES | LYS_GROUPING | LYS_ANYXML | LYS_ACTION);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200767 }
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200768
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200769 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100770 yang_print_close(out, level, flag);
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200771}
772
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200773static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100774yang_print_case(struct lyout *out, int level, const struct lys_node *node)
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200775{
Michal Vasko4773b762015-07-07 12:15:10 +0200776 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200777 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200778 struct lys_node_case *cas = (struct lys_node_case *)node;
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200779
Radek Krejci76b07902015-10-09 09:11:25 +0200780 ly_print(out, "%*scase %s {\n", LEVEL, INDENT, cas->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200781 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100782 yang_print_nacmext(out, level, node, node->module, NULL);
783 yang_print_snode_common2(out, level, node, NULL);
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200784
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200785 for (i = 0; i < cas->iffeature_size; i++) {
Radek Krejci9ff0a922016-07-14 13:08:05 +0200786 yang_print_iffeature(out, level, node->module, &cas->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200787 }
788
Michal Vasko1f0428a2015-07-07 14:55:04 +0200789 if (cas->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200790 yang_print_when(out, level, node->module, cas->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200791 }
792
Radek Krejci1d82ef62015-08-07 14:44:40 +0200793 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100794 /* augments */
795 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200796 continue;
797 }
Radek Krejci76b07902015-10-09 09:11:25 +0200798 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200799 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
800 LYS_USES | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200801 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200802
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200803 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200804 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200805}
806
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200807static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100808yang_print_choice(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200809{
Michal Vasko4773b762015-07-07 12:15:10 +0200810 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200811 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200812 struct lys_node_choice *choice = (struct lys_node_choice *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200813
Radek Krejci76b07902015-10-09 09:11:25 +0200814 ly_print(out, "%*schoice %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +0200815
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200816 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100817 yang_print_nacmext(out, level, node, node->module, NULL);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200818 if (choice->dflt != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200819 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, choice->dflt->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200820 }
Michal Vasko16083662015-06-09 14:00:45 +0200821
Radek Krejci32cce7c2015-12-09 16:44:13 +0100822 yang_print_snode_common2(out, level, node, NULL);
Michal Vasko4773b762015-07-07 12:15:10 +0200823
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200824 for (i = 0; i < choice->iffeature_size; i++) {
Radek Krejci9ff0a922016-07-14 13:08:05 +0200825 yang_print_iffeature(out, level, node->module, &choice->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200826 }
827
Michal Vasko1f0428a2015-07-07 14:55:04 +0200828 if (choice->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200829 yang_print_when(out, level, node->module, choice->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200830 }
831
Radek Krejci1d82ef62015-08-07 14:44:40 +0200832 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100833 /* augments */
834 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200835 continue;
836 }
Radek Krejci76b07902015-10-09 09:11:25 +0200837 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200838 LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYXML | LYS_CASE);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200839 }
840 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200841 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200842}
843
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200844static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100845yang_print_leaf(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200846{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200847 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200848 struct lys_node_leaf *leaf = (struct lys_node_leaf *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200849
Radek Krejci76b07902015-10-09 09:11:25 +0200850 ly_print(out, "%*sleaf %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +0200851
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200852 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100853 yang_print_nacmext(out, level, node, node->module, NULL);
Radek Krejci8d81e002015-12-10 11:18:59 +0100854 if (leaf->when) {
855 yang_print_when(out, level, node->module, leaf->when);
856 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200857 for (i = 0; i < leaf->iffeature_size; i++) {
Radek Krejci9ff0a922016-07-14 13:08:05 +0200858 yang_print_iffeature(out, level, node->module, &leaf->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200859 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200860 for (i = 0; i < leaf->must_size; i++) {
Michal Vaskof9893382015-10-09 14:03:04 +0200861 yang_print_must(out, level, node->module, &leaf->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200862 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100863 yang_print_snode_common2(out, level, node, NULL);
Radek Krejci76b07902015-10-09 09:11:25 +0200864 yang_print_type(out, level, node->module, &leaf->type);
Michal Vaskod875e882016-05-19 10:57:30 +0200865 if (leaf->units) {
Radek Krejci76b07902015-10-09 09:11:25 +0200866 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, leaf->units);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200867 }
Michal Vaskod875e882016-05-19 10:57:30 +0200868 if (leaf->dflt) {
869 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, leaf->dflt);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200870 }
871 level--;
Michal Vasko16083662015-06-09 14:00:45 +0200872
Radek Krejci76b07902015-10-09 09:11:25 +0200873 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko16083662015-06-09 14:00:45 +0200874}
875
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200876static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100877yang_print_anyxml(struct lyout *out, int level, const struct lys_node *node)
Michal Vasko16083662015-06-09 14:00:45 +0200878{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100879 int i, flag = 0;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200880 struct lys_node_anyxml *anyxml = (struct lys_node_anyxml *)node;
Michal Vasko16083662015-06-09 14:00:45 +0200881
Radek Krejci32cce7c2015-12-09 16:44:13 +0100882 ly_print(out, "%*sanyxml %s", LEVEL, INDENT, anyxml->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200883 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100884 yang_print_nacmext(out, level, node, node->module, &flag);
885 yang_print_snode_common2(out, level, node, &flag);
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200886 for (i = 0; i < anyxml->iffeature_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100887 yang_print_open(out, &flag);
Radek Krejci9ff0a922016-07-14 13:08:05 +0200888 yang_print_iffeature(out, level, node->module, &anyxml->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200889 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200890 for (i = 0; i < anyxml->must_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100891 yang_print_open(out, &flag);
Michal Vaskof9893382015-10-09 14:03:04 +0200892 yang_print_must(out, level, node->module, &anyxml->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200893 }
Michal Vasko1f0428a2015-07-07 14:55:04 +0200894 if (anyxml->when) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100895 yang_print_open(out, &flag);
Michal Vaskof9893382015-10-09 14:03:04 +0200896 yang_print_when(out, level, node->module, anyxml->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200897 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200898 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100899 yang_print_close(out, level, flag);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200900}
901
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200902static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100903yang_print_leaflist(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200904{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200905 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200906 struct lys_node_leaflist *llist = (struct lys_node_leaflist *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200907
Radek Krejci76b07902015-10-09 09:11:25 +0200908 ly_print(out, "%*sleaf-list %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +0200909
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200910 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100911 yang_print_nacmext(out, level, node, node->module, NULL);
Radek Krejci8d81e002015-12-10 11:18:59 +0100912 if (llist->when) {
913 yang_print_when(out, level, llist->module, llist->when);
914 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200915 for (i = 0; i < llist->iffeature_size; i++) {
Radek Krejci9ff0a922016-07-14 13:08:05 +0200916 yang_print_iffeature(out, level, node->module, &llist->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200917 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100918 for (i = 0; i < llist->must_size; i++) {
919 yang_print_must(out, level, node->module, &llist->must[i]);
920 }
921 yang_print_snode_common2(out, level, node, NULL);
922 yang_print_type(out, level, node->module, &llist->type);
923 if (llist->units != NULL) {
924 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, llist->units);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200925 }
926 if (llist->min > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200927 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, llist->min);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200928 }
929 if (llist->max > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200930 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, llist->max);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200931 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100932 if (llist->flags & LYS_USERORDERED) {
933 ly_print(out, "%*sordered-by user;\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200934 }
935 level--;
Michal Vasko16083662015-06-09 14:00:45 +0200936
Radek Krejci76b07902015-10-09 09:11:25 +0200937 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200938}
939
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200940static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100941yang_print_list(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200942{
Michal Vasko1ef07972015-07-07 14:01:35 +0200943 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200944 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200945 struct lys_node_list *list = (struct lys_node_list *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200946
Radek Krejci76b07902015-10-09 09:11:25 +0200947 ly_print(out, "%*slist %s {\n", LEVEL, INDENT, node->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200948 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100949 yang_print_nacmext(out, level, node, node->module, NULL);
Radek Krejci8d81e002015-12-10 11:18:59 +0100950 if (list->when) {
951 yang_print_when(out, level, list->module, list->when);
952 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200953 for (i = 0; i < list->iffeature_size; i++) {
Radek Krejci9ff0a922016-07-14 13:08:05 +0200954 yang_print_iffeature(out, level, node->module, &list->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200955 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100956 for (i = 0; i < list->must_size; i++) {
957 yang_print_must(out, level, list->module, &list->must[i]);
958 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200959 if (list->keys_size) {
Radek Krejci76b07902015-10-09 09:11:25 +0200960 ly_print(out, "%*skey \"", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200961 for (i = 0; i < list->keys_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200962 ly_print(out, "%s%s", list->keys[i]->name, i + 1 < list->keys_size ? " " : "");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200963 }
Radek Krejci76b07902015-10-09 09:11:25 +0200964 ly_print(out, "\";\n");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200965 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200966 for (i = 0; i < list->unique_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200967 yang_print_unique(out, level, &list->unique[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200968 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100969 yang_print_snode_common2(out, level, node, NULL);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200970 if (list->min > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200971 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, list->min);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200972 }
973 if (list->max > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200974 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, list->max);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200975 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100976 if (list->flags & LYS_USERORDERED) {
977 ly_print(out, "%*sordered-by user;\n", LEVEL, INDENT);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200978 }
Michal Vaskodff818b2015-06-09 14:01:12 +0200979
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200980 for (i = 0; i < list->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200981 yang_print_typedef(out, level, list->module, &list->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200982 }
Radek Krejci1d82ef62015-08-07 14:44:40 +0200983 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100984 /* augments */
985 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200986 continue;
987 }
Radek Krejci76b07902015-10-09 09:11:25 +0200988 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200989 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
Michal Vaskoca7cbc42016-07-01 11:36:53 +0200990 LYS_USES | LYS_GROUPING | LYS_ANYXML | LYS_ACTION);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200991 }
992 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200993 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200994}
995
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200996static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100997yang_print_grouping(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200998{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200999 int i;
Michal Vasko0c5e9282016-02-15 13:11:57 +01001000 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001001 struct lys_node_grp *grp = (struct lys_node_grp *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001002
Radek Krejci76b07902015-10-09 09:11:25 +02001003 ly_print(out, "%*sgrouping %s {\n", LEVEL, INDENT, node->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001004 level++;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001005
Radek Krejci32cce7c2015-12-09 16:44:13 +01001006 yang_print_snode_common(out, level, node, NULL);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001007
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001008 for (i = 0; i < grp->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +02001009 yang_print_typedef(out, level, node->module, &grp->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001010 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001011
Michal Vasko0c5e9282016-02-15 13:11:57 +01001012 LY_TREE_FOR(node->child, sub) {
1013 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +02001014 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
Michal Vaskoca7cbc42016-07-01 11:36:53 +02001015 LYS_USES | LYS_GROUPING | LYS_ANYXML | LYS_ACTION);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001016 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001017
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001018 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001019 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001020}
1021
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001022static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001023yang_print_uses(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001024{
Radek Krejci32cce7c2015-12-09 16:44:13 +01001025 int i, flag = 0;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001026 struct lys_node_uses *uses = (struct lys_node_uses *)node;
Michal Vasko1bb7a5a2016-02-05 14:28:02 +01001027 struct lys_module *mod;
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001028
Radek Krejci76b07902015-10-09 09:11:25 +02001029 ly_print(out, "%*suses ", LEVEL, INDENT);
Michal Vasko1bb7a5a2016-02-05 14:28:02 +01001030 if (node->child) {
Michal Vasko6c629ac2016-02-15 14:08:23 +01001031 mod = lys_node_module(node->child);
Michal Vasko1dae8ec2016-02-15 14:49:01 +01001032 if (lys_node_module(node) != mod) {
Michal Vasko1bb7a5a2016-02-05 14:28:02 +01001033 ly_print(out, "%s:", transform_module_name2import_prefix(node->module, mod->name));
1034 }
Michal Vaskoc39c4b12015-07-07 14:55:33 +02001035 }
Radek Krejci32cce7c2015-12-09 16:44:13 +01001036 ly_print(out, "%s", uses->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001037 level++;
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001038
Radek Krejci32cce7c2015-12-09 16:44:13 +01001039 yang_print_nacmext(out, level, node, node->module, &flag);
1040 yang_print_snode_common(out, level, node, &flag);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001041 for (i = 0; i < uses->iffeature_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001042 yang_print_open(out, &flag);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001043 yang_print_iffeature(out, level, node->module, &uses->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +02001044 }
Michal Vasko1f0428a2015-07-07 14:55:04 +02001045 if (uses->when) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001046 yang_print_open(out, &flag);
Michal Vaskof9893382015-10-09 14:03:04 +02001047 yang_print_when(out, level, node->module, uses->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +02001048 }
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001049
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001050 for (i = 0; i < uses->refine_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001051 yang_print_open(out, &flag);
Michal Vaskof9893382015-10-09 14:03:04 +02001052 yang_print_refine(out, level, node->module, &uses->refine[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001053 }
Michal Vasko00b7cfe2015-06-09 13:56:38 +02001054
Michal Vasko6f25f212015-07-07 15:42:07 +02001055 for (i = 0; i < uses->augment_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_augment(out, level, node->module, &uses->augment[i]);
Michal Vasko6f25f212015-07-07 15:42:07 +02001058 }
1059
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001060 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001061 yang_print_close(out, level, flag);
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001062}
1063
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001064static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001065yang_print_input_output(struct lyout *out, int level, const struct lys_node *node)
Michal Vasko5bbae102015-06-16 12:16:44 +02001066{
Michal Vaskof4d3d742015-06-16 11:51:09 +02001067 int i;
Radek Krejci76512572015-08-04 09:47:08 +02001068 struct lys_node *sub;
Michal Vasko44fb6382016-06-29 11:12:27 +02001069 struct lys_node_inout *inout = (struct lys_node_inout *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +02001070
Radek Krejci76b07902015-10-09 09:11:25 +02001071 ly_print(out, "%*s%s {\n", LEVEL, INDENT, (inout->nodetype == LYS_INPUT ? "input" : "output"));
Michal Vaskof4d3d742015-06-16 11:51:09 +02001072
1073 level++;
1074 for (i = 0; i < inout->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +02001075 yang_print_typedef(out, level, node->module, &inout->tpdf[i]);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001076 }
1077
Radek Krejci1d82ef62015-08-07 14:44:40 +02001078 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +01001079 /* augments */
1080 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +02001081 continue;
1082 }
Radek Krejci76b07902015-10-09 09:11:25 +02001083 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +02001084 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
1085 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001086 }
1087
1088 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001089 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001090}
1091
1092static void
Michal Vaskoca7cbc42016-07-01 11:36:53 +02001093yang_print_rpc_action(struct lyout *out, int level, const struct lys_node *node)
Michal Vaskof4d3d742015-06-16 11:51:09 +02001094{
Radek Krejci32cce7c2015-12-09 16:44:13 +01001095 int i, flag = 0;
Radek Krejci76512572015-08-04 09:47:08 +02001096 struct lys_node *sub;
Michal Vasko44fb6382016-06-29 11:12:27 +02001097 struct lys_node_rpc_action *rpc = (struct lys_node_rpc_action *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +02001098
Michal Vaskoca7cbc42016-07-01 11:36:53 +02001099 ly_print(out, "%*s%s %s", LEVEL, INDENT, (node->nodetype == LYS_RPC ? "rpc" : "action"), node->name);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001100
1101 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001102 yang_print_snode_common(out, level, node, &flag);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001103
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001104 for (i = 0; i < rpc->iffeature_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001105 yang_print_open(out, &flag);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001106 yang_print_iffeature(out, level, node->module, &rpc->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +02001107 }
1108
Michal Vaskof4d3d742015-06-16 11:51:09 +02001109 for (i = 0; i < rpc->tpdf_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001110 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001111 yang_print_typedef(out, level, node->module, &rpc->tpdf[i]);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001112 }
1113
Radek Krejci1d82ef62015-08-07 14:44:40 +02001114 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +01001115 /* augments */
1116 if (sub->parent != node) {
Radek Krejcic071c542016-01-27 14:57:51 +01001117 continue;
1118 }
Radek Krejci32cce7c2015-12-09 16:44:13 +01001119 yang_print_open(out, &flag);
1120 yang_print_snode(out, level, sub, LYS_GROUPING | LYS_INPUT | LYS_OUTPUT);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001121 }
1122
1123 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001124 yang_print_close(out, level, flag);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001125}
1126
1127static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001128yang_print_notif(struct lyout *out, int level, const struct lys_node *node)
Michal Vasko7690bc12015-06-16 12:26:05 +02001129{
Radek Krejci32cce7c2015-12-09 16:44:13 +01001130 int i, flag = 0;
Radek Krejci76512572015-08-04 09:47:08 +02001131 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001132 struct lys_node_notif *notif = (struct lys_node_notif *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +02001133
Radek Krejci32cce7c2015-12-09 16:44:13 +01001134 ly_print(out, "%*snotification %s", LEVEL, INDENT, node->name);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001135
1136 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001137 yang_print_snode_common(out, level, node, &flag);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001138
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001139 for (i = 0; i < notif->iffeature_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001140 yang_print_open(out, &flag);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001141 yang_print_iffeature(out, level, node->module, &notif->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +02001142 }
1143
Michal Vaskof4d3d742015-06-16 11:51:09 +02001144 for (i = 0; i < notif->tpdf_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001145 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001146 yang_print_typedef(out, level, node->module, &notif->tpdf[i]);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001147 }
1148
Radek Krejci1d82ef62015-08-07 14:44:40 +02001149 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +01001150 /* augments */
1151 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +02001152 continue;
1153 }
Radek Krejci32cce7c2015-12-09 16:44:13 +01001154 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001155 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +02001156 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
1157 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001158 }
1159
1160 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001161 yang_print_close(out, level, flag);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001162}
1163
1164static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001165yang_print_snode(struct lyout *out, int level, const struct lys_node *node, int mask)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001166{
Radek Krejci1d82ef62015-08-07 14:44:40 +02001167 switch (node->nodetype & mask) {
Radek Krejci76512572015-08-04 09:47:08 +02001168 case LYS_CONTAINER:
Radek Krejci76b07902015-10-09 09:11:25 +02001169 yang_print_container(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001170 break;
Radek Krejci76512572015-08-04 09:47:08 +02001171 case LYS_CHOICE:
Radek Krejci76b07902015-10-09 09:11:25 +02001172 yang_print_choice(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001173 break;
Radek Krejci76512572015-08-04 09:47:08 +02001174 case LYS_LEAF:
Radek Krejci76b07902015-10-09 09:11:25 +02001175 yang_print_leaf(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001176 break;
Radek Krejci76512572015-08-04 09:47:08 +02001177 case LYS_LEAFLIST:
Radek Krejci76b07902015-10-09 09:11:25 +02001178 yang_print_leaflist(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001179 break;
Radek Krejci76512572015-08-04 09:47:08 +02001180 case LYS_LIST:
Radek Krejci76b07902015-10-09 09:11:25 +02001181 yang_print_list(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001182 break;
Radek Krejci76512572015-08-04 09:47:08 +02001183 case LYS_USES:
Radek Krejci76b07902015-10-09 09:11:25 +02001184 yang_print_uses(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001185 break;
Radek Krejci76512572015-08-04 09:47:08 +02001186 case LYS_GROUPING:
Radek Krejci76b07902015-10-09 09:11:25 +02001187 yang_print_grouping(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001188 break;
Radek Krejci76512572015-08-04 09:47:08 +02001189 case LYS_ANYXML:
Radek Krejci76b07902015-10-09 09:11:25 +02001190 yang_print_anyxml(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001191 break;
Radek Krejci76512572015-08-04 09:47:08 +02001192 case LYS_CASE:
Radek Krejci76b07902015-10-09 09:11:25 +02001193 yang_print_case(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001194 break;
Michal Vaskoca7cbc42016-07-01 11:36:53 +02001195 case LYS_ACTION:
1196 yang_print_rpc_action(out, level, node);
1197 break;
Radek Krejci76512572015-08-04 09:47:08 +02001198 case LYS_INPUT:
1199 case LYS_OUTPUT:
Radek Krejci76b07902015-10-09 09:11:25 +02001200 yang_print_input_output(out, level, node);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001201 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001202 default:
1203 break;
1204 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001205}
1206
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001207int
Michal Vasko1e62a092015-12-01 12:27:20 +01001208yang_print_model(struct lyout *out, const struct lys_module *module)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001209{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001210 unsigned int i;
1211 int level = 0;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001212#define LEVEL (level*2)
1213
Radek Krejci1d82ef62015-08-07 14:44:40 +02001214 struct lys_node *node;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001215
Radek Krejci8d81e002015-12-10 11:18:59 +01001216 /* (sub)module-header-stmts */
Michal Vasko116172e2015-07-07 11:54:37 +02001217 if (module->type) {
Radek Krejci76b07902015-10-09 09:11:25 +02001218 ly_print(out, "submodule %s {%s\n", module->name, (module->deviated ? " // DEVIATED" : ""));
Michal Vasko116172e2015-07-07 11:54:37 +02001219 level++;
Radek Krejci8d81e002015-12-10 11:18:59 +01001220 if (module->version) {
Radek Krejcic071c542016-01-27 14:57:51 +01001221 ly_print(out, "%*syang-version %s;\n", LEVEL, INDENT,
1222 ((struct lys_submodule *)module)->belongsto->version == 2 ? "1.1" : "1");
Radek Krejci8d81e002015-12-10 11:18:59 +01001223 }
Radek Krejci76b07902015-10-09 09:11:25 +02001224 ly_print(out, "%*sbelongs-to %s {\n", LEVEL, INDENT, ((struct lys_submodule *)module)->belongsto->name);
Michal Vasko116172e2015-07-07 11:54:37 +02001225 level++;
Radek Krejci8d81e002015-12-10 11:18:59 +01001226 ly_print(out, "%*sprefix %s;\n", LEVEL, INDENT, module->prefix);
Michal Vasko116172e2015-07-07 11:54:37 +02001227 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001228 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko116172e2015-07-07 11:54:37 +02001229 } else {
Radek Krejci76b07902015-10-09 09:11:25 +02001230 ly_print(out, "module %s {%s\n", module->name, (module->deviated ? " // DEVIATED" : ""));
Michal Vasko116172e2015-07-07 11:54:37 +02001231 level++;
Radek Krejci8d81e002015-12-10 11:18:59 +01001232 if (module->version) {
Radek Krejcic071c542016-01-27 14:57:51 +01001233 ly_print(out, "%*syang-version %s;\n", LEVEL, INDENT, module->version == 2 ? "1.1" : "1");
Radek Krejci8d81e002015-12-10 11:18:59 +01001234 }
Radek Krejci76b07902015-10-09 09:11:25 +02001235 ly_print(out, "%*snamespace \"%s\";\n", LEVEL, INDENT, module->ns);
Radek Krejci8d81e002015-12-10 11:18:59 +01001236 ly_print(out, "%*sprefix %s;\n", LEVEL, INDENT, module->prefix);
Michal Vasko116172e2015-07-07 11:54:37 +02001237 }
Radek Krejcib0594bf2015-05-21 23:51:27 +02001238
Radek Krejci8d81e002015-12-10 11:18:59 +01001239 /* linkage-stmts */
1240 if (module->imp_size || module->inc_size) {
1241 ly_print(out, "\n");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001242 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001243 for (i = 0; i < module->imp_size; i++) {
Radek Krejcic071c542016-01-27 14:57:51 +01001244 if (module->imp[i].external) {
1245 continue;
1246 }
Michal Vaskoc8e3ce02016-02-12 14:28:35 +01001247 ly_print(out, "%*simport %s {\n", LEVEL, INDENT, module->imp[i].module->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001248 level++;
Radek Krejci8d81e002015-12-10 11:18:59 +01001249 ly_print(out, "%*sprefix %s;\n", LEVEL, INDENT, module->imp[i].prefix);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001250 if (module->imp[i].rev[0]) {
Michal Vasko86dfd262016-02-15 14:26:31 +01001251 ly_print(out, "%*srevision-date %s;\n", LEVEL, INDENT, module->imp[i].rev);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001252 }
Michal Vasko8bfe3812016-07-27 13:37:52 +02001253 if (module->imp[i].dsc) {
1254 yang_print_text(out, level, "description", module->imp[i].dsc, 0);
1255 }
1256 if (module->imp[i].ref) {
1257 yang_print_text(out, level, "reference", module->imp[i].ref, 0);
1258 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001259 level--;
Michal Vaskoc8e3ce02016-02-12 14:28:35 +01001260 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001261 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001262 for (i = 0; i < module->inc_size; i++) {
Radek Krejcic071c542016-01-27 14:57:51 +01001263 if (module->inc[i].external) {
1264 continue;
1265 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001266 if (module->inc[i].rev[0]) {
Radek Krejci76b07902015-10-09 09:11:25 +02001267 ly_print(out, "%*sinclude \"%s\" {\n", LEVEL, INDENT, module->inc[i].submodule->name);
Radek Krejci8d81e002015-12-10 11:18:59 +01001268 level++;
Michal Vasko3a4e2d02016-02-15 15:12:46 +01001269 ly_print(out, "%*srevision-date %s;\n", LEVEL, INDENT, module->inc[i].rev);
Michal Vasko8bfe3812016-07-27 13:37:52 +02001270 if (module->inc[i].dsc) {
1271 yang_print_text(out, level, "description", module->inc[i].dsc, 0);
1272 }
1273 if (module->inc[i].ref) {
1274 yang_print_text(out, level, "reference", module->inc[i].ref, 0);
1275 }
Radek Krejci8d81e002015-12-10 11:18:59 +01001276 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001277 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001278 } else {
Radek Krejci76b07902015-10-09 09:11:25 +02001279 ly_print(out, "%*sinclude \"%s\";\n", LEVEL, INDENT, module->inc[i].submodule->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001280 }
1281 }
Radek Krejciefaeba32015-05-27 14:30:57 +02001282
Radek Krejci8d81e002015-12-10 11:18:59 +01001283 /* meta-stmts */
1284 if (module->org || module->contact || module->dsc || module->ref) {
1285 ly_print(out, "\n");
1286 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001287 if (module->org) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001288 yang_print_text(out, level, "organization", module->org, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001289 }
1290 if (module->contact) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001291 yang_print_text(out, level, "contact", module->contact, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001292 }
1293 if (module->dsc) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001294 yang_print_text(out, level, "description", module->dsc, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001295 }
1296 if (module->ref) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001297 yang_print_text(out, level, "reference", module->ref, 0);
1298 }
1299
1300 /* revision-stmts */
1301 if (module->rev_size) {
1302 ly_print(out, "\n");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001303 }
1304 for (i = 0; i < module->rev_size; i++) {
1305 if (module->rev[i].dsc || module->rev[i].ref) {
Radek Krejci76b07902015-10-09 09:11:25 +02001306 ly_print(out, "%*srevision \"%s\" {\n", LEVEL, INDENT, module->rev[i].date);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001307 level++;
1308 if (module->rev[i].dsc) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001309 yang_print_text(out, level, "description", module->rev[i].dsc, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001310 }
1311 if (module->rev[i].ref) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001312 yang_print_text(out, level, "reference", module->rev[i].ref, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001313 }
1314 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001315 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001316 } else {
Michal Vasko86dfd262016-02-15 14:26:31 +01001317 ly_print(out, "%*srevision %s;\n", LEVEL, INDENT, module->rev[i].date);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001318 }
1319 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001320
Radek Krejci8d81e002015-12-10 11:18:59 +01001321 /* body-stmts */
Michal Vasko30f6e912015-07-07 12:24:27 +02001322 for (i = 0; i < module->features_size; i++) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001323 ly_print(out, "\n");
Radek Krejci76b07902015-10-09 09:11:25 +02001324 yang_print_feature(out, level, &module->features[i]);
Michal Vasko30f6e912015-07-07 12:24:27 +02001325 }
1326
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001327 for (i = 0; i < module->ident_size; i++) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001328 ly_print(out, "\n");
Radek Krejci76b07902015-10-09 09:11:25 +02001329 yang_print_identity(out, level, &module->ident[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001330 }
Radek Krejci6793db02015-05-22 17:49:54 +02001331
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001332 for (i = 0; i < module->tpdf_size; i++) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001333 ly_print(out, "\n");
Radek Krejci76b07902015-10-09 09:11:25 +02001334 yang_print_typedef(out, level, module, &module->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001335 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001336
Radek Krejci8d81e002015-12-10 11:18:59 +01001337 for (i = 0; i < module->deviation_size; ++i) {
1338 ly_print(out, "\n");
1339 yang_print_deviation(out, level, module, &module->deviation[i]);
1340 }
1341
Radek Krejcic4283442016-04-22 09:19:27 +02001342 LY_TREE_FOR(lys_main_module(module)->data, node) {
Radek Krejcic071c542016-01-27 14:57:51 +01001343 if (node->module != module) {
Michal Vasko0c5e9282016-02-15 13:11:57 +01001344 /* data from submodules */
Radek Krejcic071c542016-01-27 14:57:51 +01001345 continue;
1346 }
Radek Krejci8d81e002015-12-10 11:18:59 +01001347 ly_print(out, "\n");
Radek Krejci92720552015-10-05 15:28:27 +02001348 switch(node->nodetype) {
1349 case LYS_RPC:
Michal Vaskoca7cbc42016-07-01 11:36:53 +02001350 yang_print_rpc_action(out, level, node);
Radek Krejci92720552015-10-05 15:28:27 +02001351 break;
1352 case LYS_NOTIF:
Radek Krejcibac81762015-10-09 10:19:52 +02001353 yang_print_notif(out, level, node);
Radek Krejci92720552015-10-05 15:28:27 +02001354 break;
1355 default:
Radek Krejcibac81762015-10-09 10:19:52 +02001356 yang_print_snode(out, level, node,
Radek Krejci92720552015-10-05 15:28:27 +02001357 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
1358 LYS_USES | LYS_GROUPING | LYS_ANYXML);
1359 break;
1360 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001361 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001362
Michal Vasko6f25f212015-07-07 15:42:07 +02001363 for (i = 0; i < module->augment_size; i++) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001364 ly_print(out, "\n");
Radek Krejci76b07902015-10-09 09:11:25 +02001365 yang_print_augment(out, level, module, &module->augment[i]);
Michal Vasko6f25f212015-07-07 15:42:07 +02001366 }
1367
Radek Krejci76b07902015-10-09 09:11:25 +02001368 ly_print(out, "}\n");
Michal Vasko95068c42016-03-24 14:58:11 +01001369 ly_print_flush(out);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001370
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001371 return EXIT_SUCCESS;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001372#undef LEVEL
1373}