blob: 56a459d80445debb775d7ce5f6a073fca52dc122 [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 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of the Company nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 */
21
22#include <stdlib.h>
Radek Krejcida04f4a2015-05-21 12:54:09 +020023#include <string.h>
Michal Vasko25cb6c62016-02-12 14:36:21 +010024#include <stdint.h>
Radek Krejcida04f4a2015-05-21 12:54:09 +020025
Radek Krejci998a0b82015-08-17 13:14:36 +020026#include "common.h"
Radek Krejci76b07902015-10-09 09:11:25 +020027#include "printer.h"
Michal Vasko2d162e12015-09-24 14:33:29 +020028#include "tree_schema.h"
Radek Krejcida04f4a2015-05-21 12:54:09 +020029
30#define INDENT ""
31#define LEVEL (level*2)
32
Michal Vasko1e62a092015-12-01 12:27:20 +010033static void yang_print_snode(struct lyout *out, int level, const struct lys_node *node, int mask);
Radek Krejcida04f4a2015-05-21 12:54:09 +020034
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020035static void
Michal Vasko25cb6c62016-02-12 14:36:21 +010036yang_encode(struct lyout *out, const char *text, int len)
37{
38 int i, start_len;
39 const char *start;
40 char special = 0;
41
42 if (!len) {
43 return;
44 }
45
46 if (len < 0) {
47 len = strlen(text);
48 }
49
50 start = text;
51 start_len = 0;
52 for (i = 0; i < len; ++i) {
53 switch (text[i]) {
54 case '\n':
55 case '\t':
56 case '\"':
57 case '\\':
58 special = text[i];
59 break;
60 default:
61 ++start_len;
62 break;
63 }
64
65 if (special) {
66 ly_write(out, start, start_len);
67 switch (special) {
68 case '\n':
69 ly_write(out, "\\n", 2);
70 break;
71 case '\t':
72 ly_write(out, "\\t", 2);
73 break;
74 case '\"':
75 ly_write(out, "\\\"", 2);
76 break;
77 case '\\':
78 ly_write(out, "\\\\", 2);
79 break;
80 }
81
82 start += start_len + 1;
83 start_len = 0;
84
85 special = 0;
86 }
87 }
88
89 ly_write(out, start, start_len);
90}
91
92static void
Radek Krejci32cce7c2015-12-09 16:44:13 +010093yang_print_open(struct lyout *out, int *flag)
94{
95 if (flag && !*flag) {
96 *flag = 1;
97 ly_print(out, " {\n");
98 }
99}
100
101static void
102yang_print_close(struct lyout *out, int level, int flag)
103{
104 if (flag) {
105 ly_print(out, "%*s}\n", LEVEL, INDENT);
106 } else {
107 ly_print(out, ";\n");
108 }
109}
110
111static void
Radek Krejci8d81e002015-12-10 11:18:59 +0100112yang_print_text(struct lyout *out, int level, const char *name, const char *text, int singleline)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200113{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200114 const char *s, *t;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200115
Radek Krejci8d81e002015-12-10 11:18:59 +0100116 if (singleline) {
117 ly_print(out, "%*s%s \"", LEVEL, INDENT, name);
118 } else {
119 ly_print(out, "%*s%s\n", LEVEL, INDENT, name);
120 level++;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200121
Radek Krejci8d81e002015-12-10 11:18:59 +0100122 ly_print(out, "%*s\"", LEVEL, INDENT);
123 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200124 t = text;
125 while ((s = strchr(t, '\n'))) {
Michal Vasko25cb6c62016-02-12 14:36:21 +0100126 yang_encode(out, t, s - t);
127 ly_print(out, "\n");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200128 t = s + 1;
Michal Vasko25cb6c62016-02-12 14:36:21 +0100129 ly_print(out, "%*s ", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200130 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200131
Radek Krejci8d81e002015-12-10 11:18:59 +0100132 ly_print(out, "%s\";\n", t);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200133 level--;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200134
135}
136
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200137static void
Radek Krejci32cce7c2015-12-09 16:44:13 +0100138yang_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 +0200139{
Radek Krejci6764bb32015-07-03 15:16:04 +0200140 int i, j;
Radek Krejci86909d22015-07-03 16:11:25 +0200141 const char *prefix = NULL;
Radek Krejci6764bb32015-07-03 15:16:04 +0200142
Radek Krejci1d82ef62015-08-07 14:44:40 +0200143 if (node->nacm && (!node->parent || node->parent->nacm != node->nacm)) {
Radek Krejci6764bb32015-07-03 15:16:04 +0200144 /* locate ietf-netconf-acm module in imports */
Michal Vaskodb7aa992015-07-08 09:37:38 +0200145 if (!strcmp(module->name, "ietf-netconf-acm")) {
146 prefix = module->prefix;
Radek Krejci6764bb32015-07-03 15:16:04 +0200147 } else {
148 /* search in imports */
Michal Vaskodb7aa992015-07-08 09:37:38 +0200149 for (i = 0; i < module->imp_size; i++) {
150 if (!strcmp(module->imp[i].module->name, "ietf-netconf-acm")) {
151 prefix = module->imp[i].prefix;
Radek Krejci6764bb32015-07-03 15:16:04 +0200152 break;
153 }
154 }
155 /* and in imports of includes */
156 if (!prefix) {
Michal Vaskodb7aa992015-07-08 09:37:38 +0200157 for (j = 0; j < module->inc_size; j++) {
158 for (i = 0; i < module->inc[j].submodule->imp_size; i++) {
159 if (!strcmp(module->inc[j].submodule->imp[i].module->name, "ietf-netconf-acm")) {
160 prefix = module->inc[j].submodule->imp[i].prefix;
Radek Krejci6764bb32015-07-03 15:16:04 +0200161 break;
162 }
163 }
164 }
165 }
166 }
167
Radek Krejci1d82ef62015-08-07 14:44:40 +0200168 if ((node->nacm & LYS_NACM_DENYW) && (!node->parent || !(node->parent->nacm & LYS_NACM_DENYW))) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100169 yang_print_open(out, flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200170 ly_print(out, "%*s%s:default-deny-write;\n", LEVEL, INDENT, prefix);
Radek Krejci6764bb32015-07-03 15:16:04 +0200171 }
Radek Krejci1d82ef62015-08-07 14:44:40 +0200172 if ((node->nacm & LYS_NACM_DENYA) && (!node->parent || !(node->parent->nacm & LYS_NACM_DENYA))) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100173 yang_print_open(out, flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200174 ly_print(out, "%*s%s:default-deny-all;\n", LEVEL, INDENT, prefix);
Radek Krejci6764bb32015-07-03 15:16:04 +0200175 }
176 }
Radek Krejci6a113852015-07-03 16:04:20 +0200177}
178
179/*
180 * Covers:
181 * description, reference, status
182 */
183static void
Radek Krejci32cce7c2015-12-09 16:44:13 +0100184yang_print_snode_common(struct lyout *out, int level, const struct lys_node *node, int *flag)
Radek Krejci6a113852015-07-03 16:04:20 +0200185{
Radek Krejci1d82ef62015-08-07 14:44:40 +0200186 if (node->flags & LYS_STATUS_CURR) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100187 yang_print_open(out, flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200188 ly_print(out, "%*sstatus \"current\";\n", LEVEL, INDENT);
Radek Krejci1d82ef62015-08-07 14:44:40 +0200189 } else if (node->flags & LYS_STATUS_DEPRC) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100190 yang_print_open(out, flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200191 ly_print(out, "%*sstatus \"deprecated\";\n", LEVEL, INDENT);
Radek Krejci1d82ef62015-08-07 14:44:40 +0200192 } else if (node->flags & LYS_STATUS_OBSLT) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100193 yang_print_open(out, flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200194 ly_print(out, "%*sstatus \"obsolete\";\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200195 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200196
Radek Krejci1d82ef62015-08-07 14:44:40 +0200197 if (node->dsc) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100198 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100199 yang_print_text(out, level, "description", node->dsc, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200200 }
Radek Krejci1d82ef62015-08-07 14:44:40 +0200201 if (node->ref) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100202 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100203 yang_print_text(out, level, "reference", node->ref, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200204 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200205}
206
207/*
208 * Covers:
Michal Vasko2b8faed2015-06-09 12:51:20 +0200209 * config, mandatory
Radek Krejcida04f4a2015-05-21 12:54:09 +0200210 * description, reference, status
211 */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200212static void
Radek Krejci32cce7c2015-12-09 16:44:13 +0100213yang_print_snode_common2(struct lyout *out, int level, const struct lys_node *node, int *flag)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200214{
Michal Vasko43271ee2016-02-05 14:27:09 +0100215 if (node->parent) {
216 if ((node->parent->flags & LYS_CONFIG_MASK) != (node->flags & LYS_CONFIG_MASK)) {
217 /* print config when it differs from the parent ... */
218 if (node->flags & LYS_CONFIG_W) {
219 yang_print_open(out, flag);
220 ly_print(out, "%*sconfig true;\n", LEVEL, INDENT);
221 } else if (node->flags & LYS_CONFIG_R) {
222 yang_print_open(out, flag);
223 ly_print(out, "%*sconfig false;\n", LEVEL, INDENT);
224 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200225 }
Michal Vasko43271ee2016-02-05 14:27:09 +0100226 } else if (node->flags & LYS_CONFIG_R) {
227 /* ... or it's a top-level state node */
228 yang_print_open(out, flag);
229 ly_print(out, "%*sconfig false;\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200230 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200231
Radek Krejci1d82ef62015-08-07 14:44:40 +0200232 if (node->flags & LYS_MAND_TRUE) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100233 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100234 ly_print(out, "%*smandatory true;\n", LEVEL, INDENT);
Radek Krejci1d82ef62015-08-07 14:44:40 +0200235 } else if (node->flags & LYS_MAND_FALSE) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100236 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100237 ly_print(out, "%*smandatory false;\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200238 }
Michal Vasko2b8faed2015-06-09 12:51:20 +0200239
Radek Krejci32cce7c2015-12-09 16:44:13 +0100240 yang_print_snode_common(out, level, node, flag);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200241}
242
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200243static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100244yang_print_iffeature(struct lyout *out, int level, const struct lys_module *module, const struct lys_feature *feat)
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200245{
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100246 struct lys_module *mod;
247
Radek Krejci76b07902015-10-09 09:11:25 +0200248 ly_print(out, "%*sif-feature ", LEVEL, INDENT);
Michal Vasko6c629ac2016-02-15 14:08:23 +0100249 mod = lys_module(feat->module);
Michal Vasko1dae8ec2016-02-15 14:49:01 +0100250 if (lys_module(module) != mod) {
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100251 ly_print(out, "%s:", transform_module_name2import_prefix(module, mod->name));
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200252 }
Radek Krejci76b07902015-10-09 09:11:25 +0200253 ly_print(out, "%s;\n", feat->name);
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200254}
255
256static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100257yang_print_feature(struct lyout *out, int level, const struct lys_feature *feat)
Michal Vasko4773b762015-07-07 12:15:10 +0200258{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100259 int i, flag = 0;
Michal Vasko30f6e912015-07-07 12:24:27 +0200260
Radek Krejci32cce7c2015-12-09 16:44:13 +0100261 ly_print(out, "%*sfeature %s", LEVEL, INDENT, feat->name);
Michal Vasko4773b762015-07-07 12:15:10 +0200262 level++;
263
Radek Krejci32cce7c2015-12-09 16:44:13 +0100264 yang_print_snode_common(out, level, (struct lys_node *)feat, &flag);
Michal Vasko30f6e912015-07-07 12:24:27 +0200265 for (i = 0; i < feat->features_size; ++i) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100266 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200267 yang_print_iffeature(out, level, feat->module, feat->features[i]);
Michal Vasko30f6e912015-07-07 12:24:27 +0200268 }
Michal Vasko4773b762015-07-07 12:15:10 +0200269
270 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100271 yang_print_close(out, level, flag);
Michal Vasko4773b762015-07-07 12:15:10 +0200272}
273
274static void
Radek Krejci32cce7c2015-12-09 16:44:13 +0100275yang_print_restr(struct lyout *out, int level, const struct lys_restr *restr, int *flag)
Radek Krejci41726f92015-06-19 13:11:05 +0200276{
Radek Krejci0bd5db42015-06-19 13:30:07 +0200277 if (restr->dsc != 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, "description", restr->dsc, 0);
Radek Krejci41726f92015-06-19 13:11:05 +0200280 }
Radek Krejci0bd5db42015-06-19 13:30:07 +0200281 if (restr->ref != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100282 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100283 yang_print_text(out, level, "reference", restr->ref, 0);
Radek Krejci41726f92015-06-19 13:11:05 +0200284 }
Radek Krejci0bd5db42015-06-19 13:30:07 +0200285 if (restr->eapptag != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100286 yang_print_open(out, flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200287 ly_print(out, "%*serror-app-tag \"%s\";\n", LEVEL, INDENT, restr->eapptag);
Radek Krejci41726f92015-06-19 13:11:05 +0200288 }
Radek Krejci0bd5db42015-06-19 13:30:07 +0200289 if (restr->emsg != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100290 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100291 yang_print_text(out, level, "error-message", restr->emsg, 0);
Radek Krejci41726f92015-06-19 13:11:05 +0200292 }
293}
294
295static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100296yang_print_when(struct lyout *out, int level, const struct lys_module *module, const struct lys_when *when)
Michal Vasko1f0428a2015-07-07 14:55:04 +0200297{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100298 int flag = 0;
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100299 const char *str;
Michal Vaskof9893382015-10-09 14:03:04 +0200300
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100301 str = transform_json2schema(module, when->cond);
302 if (!str) {
Michal Vaskof9893382015-10-09 14:03:04 +0200303 ly_print(out, "(!error!)");
304 return;
305 }
306
Michal Vasko25cb6c62016-02-12 14:36:21 +0100307 ly_print(out, "%*swhen \"", LEVEL, INDENT);
308 yang_encode(out, str, -1);
309 ly_print(out, "\"");
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100310 lydict_remove(module->ctx, str);
Michal Vaskof9893382015-10-09 14:03:04 +0200311
Michal Vasko1f0428a2015-07-07 14:55:04 +0200312 level++;
313 if (when->dsc) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100314 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100315 yang_print_text(out, level, "description", when->dsc, 0);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200316 }
317 if (when->ref) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100318 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100319 yang_print_text(out, level, "reference", when->ref, 0);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200320 }
321 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100322 yang_print_close(out, level, flag);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200323}
324
325static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100326yang_print_type(struct lyout *out, int level, const struct lys_module *module, const struct lys_type *type)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200327{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200328 int i;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100329 int flag = 0, flag2;
Michal Vasko0fb82c62015-10-20 13:41:53 +0200330 const char *str;
Radek Krejcic071c542016-01-27 14:57:51 +0100331 struct lys_module *mod;
Radek Krejci25d782a2015-05-22 15:03:23 +0200332
Michal Vasko0fb82c62015-10-20 13:41:53 +0200333 if (type->module_name) {
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100334 ly_print(out, "%*stype %s:%s", LEVEL, INDENT,
335 transform_module_name2import_prefix(module, type->module_name), type->der->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200336 } else {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100337 ly_print(out, "%*stype %s", LEVEL, INDENT, type->der->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200338 }
339 level++;
340 switch (type->base) {
341 case LY_TYPE_BINARY:
342 if (type->info.binary.length != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100343 yang_print_open(out, &flag);
Michal Vasko25cb6c62016-02-12 14:36:21 +0100344 ly_print(out, "%*slength \"", LEVEL, INDENT);
345 yang_encode(out, type->info.binary.length->expr, -1);
346 ly_print(out, "\"");
Radek Krejci32cce7c2015-12-09 16:44:13 +0100347 flag2 = 0;
348 yang_print_restr(out, level + 1, type->info.binary.length, &flag2);
349 yang_print_close(out, level, flag2);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200350 }
351 break;
352 case LY_TYPE_BITS:
353 for (i = 0; i < type->info.bits.count; ++i) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100354 yang_print_open(out, &flag);
Michal Vasko3f053ef2016-02-12 14:27:13 +0100355 ly_print(out, "%*sbit %s", LEVEL, INDENT, type->info.bits.bit[i].name);
356 flag2 = 0;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200357 level++;
Michal Vasko3f053ef2016-02-12 14:27:13 +0100358 yang_print_snode_common(out, level, (struct lys_node *)&type->info.bits.bit[i], &flag2);
359 if (!(type->info.bits.bit[i].flags & LYS_AUTOASSIGNED)) {
360 yang_print_open(out, &flag2);
361 ly_print(out, "%*sposition %u;\n", LEVEL, INDENT, type->info.bits.bit[i].pos);
362 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200363 level--;
Michal Vasko3f053ef2016-02-12 14:27:13 +0100364 yang_print_close(out, level, flag2);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200365 }
366 break;
367 case LY_TYPE_DEC64:
Radek Krejci32cce7c2015-12-09 16:44:13 +0100368 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200369 ly_print(out, "%*sfraction-digits %d;\n", LEVEL, INDENT, type->info.dec64.dig);
Michal Vaskoea505ee2015-07-07 14:01:00 +0200370 if (type->info.dec64.range != NULL) {
Michal Vasko25cb6c62016-02-12 14:36:21 +0100371 ly_print(out, "%*srange \"", LEVEL, INDENT);
372 yang_encode(out, type->info.dec64.range->expr, -1);
373 ly_print(out, "\"");
Radek Krejci32cce7c2015-12-09 16:44:13 +0100374 flag2 = 0;
375 yang_print_restr(out, level + 1, type->info.dec64.range, &flag2);
376 yang_print_close(out, level, flag2);
Michal Vaskoea505ee2015-07-07 14:01:00 +0200377 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200378 break;
379 case LY_TYPE_ENUM:
380 for (i = 0; i < type->info.enums.count; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100381 yang_print_open(out, &flag);
Michal Vasko3f053ef2016-02-12 14:27:13 +0100382 ly_print(out, "%*senum \"%s\"", LEVEL, INDENT, type->info.enums.enm[i].name);
383 flag2 = 0;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200384 level++;
Michal Vasko3f053ef2016-02-12 14:27:13 +0100385 yang_print_snode_common(out, level, (struct lys_node *)&type->info.enums.enm[i], &flag2);
386 if (!(type->info.enums.enm[i].flags & LYS_AUTOASSIGNED)) {
387 yang_print_open(out, &flag2);
388 ly_print(out, "%*svalue %d;\n", LEVEL, INDENT, type->info.enums.enm[i].value);
389 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200390 level--;
Michal Vasko3f053ef2016-02-12 14:27:13 +0100391 yang_print_close(out, level, flag2);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200392 }
393 break;
394 case LY_TYPE_IDENT:
Radek Krejci32cce7c2015-12-09 16:44:13 +0100395 yang_print_open(out, &flag);
Michal Vasko6c629ac2016-02-15 14:08:23 +0100396 mod = lys_module(type->info.ident.ref->module);
Michal Vasko1dae8ec2016-02-15 14:49:01 +0100397 if (lys_module(module) == mod) {
Radek Krejci76b07902015-10-09 09:11:25 +0200398 ly_print(out, "%*sbase %s;\n", LEVEL, INDENT, type->info.ident.ref->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200399 } else {
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100400 ly_print(out, "%*sbase %s:%s;\n", LEVEL, INDENT, transform_module_name2import_prefix(module, mod->name),
401 type->info.ident.ref->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200402 }
403 break;
404 case LY_TYPE_INST:
Radek Krejciaf351422015-06-19 14:49:38 +0200405 if (type->info.inst.req == 1) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100406 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100407 ly_print(out, "%*srequire-instance true;\n", LEVEL, INDENT);
Radek Krejciaf351422015-06-19 14:49:38 +0200408 } else if (type->info.inst.req == -1) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100409 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100410 ly_print(out, "%*srequire-instance false;\n", LEVEL, INDENT);
Radek Krejciaf351422015-06-19 14:49:38 +0200411 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200412 break;
413 case LY_TYPE_INT8:
414 case LY_TYPE_INT16:
415 case LY_TYPE_INT32:
416 case LY_TYPE_INT64:
417 case LY_TYPE_UINT8:
418 case LY_TYPE_UINT16:
419 case LY_TYPE_UINT32:
420 case LY_TYPE_UINT64:
Radek Krejcif2860132015-06-20 12:37:20 +0200421 if (type->info.num.range != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100422 yang_print_open(out, &flag);
Michal Vasko25cb6c62016-02-12 14:36:21 +0100423 ly_print(out, "%*srange \"", LEVEL, INDENT);
424 yang_encode(out, type->info.num.range->expr, -1);
425 ly_print(out, "\"");
Radek Krejci32cce7c2015-12-09 16:44:13 +0100426 flag2 = 0;
427 yang_print_restr(out, level + 1, type->info.num.range, &flag2);
428 yang_print_close(out, level, flag2);
Radek Krejcif2860132015-06-20 12:37:20 +0200429 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200430 break;
431 case LY_TYPE_LEAFREF:
Radek Krejci32cce7c2015-12-09 16:44:13 +0100432 yang_print_open(out, &flag);
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100433 str = transform_json2schema(module, type->info.lref.path);
Michal Vasko514c4f32015-10-20 13:43:25 +0200434 ly_print(out, "%*spath \"%s\";\n", LEVEL, INDENT, str);
435 lydict_remove(module->ctx, str);
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 Krejci0d7b2472016-02-12 11:11:03 +0100542 /* magic - bit 3 in flags means min set, bit 4 says max set */
543 if (refine->flags & 0x04) {
Radek Krejci76b07902015-10-09 09:11:25 +0200544 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, refine->mod.list.min);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200545 }
Radek Krejci0d7b2472016-02-12 11:11:03 +0100546 if (refine->flags & 0x08) {
547 if (refine->mod.list.max) {
548 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, refine->mod.list.max);
549 } else {
550 ly_print(out, "%*smax-elements \"unbounded\";\n", LEVEL, INDENT);
551 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200552 }
553 }
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200554
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200555 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200556 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200557}
558
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200559static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100560yang_print_deviation(struct lyout *out, int level, const struct lys_module *module,
561 const struct lys_deviation *deviation)
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200562{
563 int i, j;
Michal Vaskoa8b25952015-10-20 15:30:25 +0200564 const char *str;
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200565
Michal Vasko5d112852016-02-12 16:47:13 +0100566 str = transform_json2schema(module, deviation->target_name);
Michal Vaskoa8b25952015-10-20 15:30:25 +0200567 ly_print(out, "%*sdeviation \"%s\" {\n", LEVEL, INDENT, str);
568 lydict_remove(module->ctx, str);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200569 level++;
570
571 if (deviation->dsc) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100572 yang_print_text(out, level, "description", deviation->dsc, 0);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200573 }
574 if (deviation->ref) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100575 yang_print_text(out, level, "reference", deviation->ref, 0);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200576 }
577
578 for (i = 0; i < deviation->deviate_size; ++i) {
Radek Krejci76b07902015-10-09 09:11:25 +0200579 ly_print(out, "%*sdeviate ", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200580 if (deviation->deviate[i].mod == LY_DEVIATE_NO) {
Radek Krejci76b07902015-10-09 09:11:25 +0200581 ly_print(out, "not-supported {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200582 } else if (deviation->deviate[i].mod == LY_DEVIATE_ADD) {
Radek Krejci76b07902015-10-09 09:11:25 +0200583 ly_print(out, "add {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200584 } else if (deviation->deviate[i].mod == LY_DEVIATE_RPL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200585 ly_print(out, "replace {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200586 } else if (deviation->deviate[i].mod == LY_DEVIATE_DEL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200587 ly_print(out, "delete {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200588 }
589 level++;
590
Radek Krejci1574a8d2015-08-03 14:16:52 +0200591 if (deviation->deviate[i].flags & LYS_CONFIG_W) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100592 ly_print(out, "%*sconfig true;\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200593 } else if (deviation->deviate[i].flags & LYS_CONFIG_R) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100594 ly_print(out, "%*sconfig false;\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200595 }
596
Radek Krejci1574a8d2015-08-03 14:16:52 +0200597 if (deviation->deviate[i].flags & LYS_MAND_TRUE) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100598 ly_print(out, "%*smandatory true;\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200599 } else if (deviation->deviate[i].flags & LYS_MAND_FALSE) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100600 ly_print(out, "%*smandatory false;\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200601 }
602
603 if (deviation->deviate[i].dflt) {
Radek Krejci76b07902015-10-09 09:11:25 +0200604 ly_print(out, "%*sdefault %s;\n", LEVEL, INDENT, deviation->deviate[i].dflt);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200605 }
606
Radek Krejci0d7b2472016-02-12 11:11:03 +0100607 if (deviation->deviate[i].min_set) {
Radek Krejci76b07902015-10-09 09:11:25 +0200608 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, deviation->deviate[i].min);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200609 }
Radek Krejci0d7b2472016-02-12 11:11:03 +0100610 if (deviation->deviate[i].max_set) {
611 if (deviation->deviate[i].max) {
612 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, deviation->deviate[i].max);
613 } else {
614 ly_print(out, "%*smax-elements \"unbounded\";\n", LEVEL, INDENT);
615 }
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200616 }
617
618 for (j = 0; j < deviation->deviate[i].must_size; ++j) {
Michal Vaskof9893382015-10-09 14:03:04 +0200619 yang_print_must(out, level, module, &deviation->deviate[i].must[j]);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200620 }
621
622 for (j = 0; j < deviation->deviate[i].unique_size; ++j) {
Radek Krejci76b07902015-10-09 09:11:25 +0200623 yang_print_unique(out, level, &deviation->deviate[i].unique[j]);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200624 }
625
626 if (deviation->deviate[i].type) {
Radek Krejci76b07902015-10-09 09:11:25 +0200627 yang_print_type(out, level, module, deviation->deviate[i].type);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200628 }
629
630 if (deviation->deviate[i].units) {
Radek Krejci76b07902015-10-09 09:11:25 +0200631 ly_print(out, "%*sunits %s;\n", LEVEL, INDENT, deviation->deviate[i].units);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200632 }
633
634 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200635 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200636 }
637
638 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200639 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200640}
641
642static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100643yang_print_augment(struct lyout *out, int level, const struct lys_module *module,
644 const struct lys_node_augment *augment)
Michal Vasko6f25f212015-07-07 15:42:07 +0200645{
646 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200647 struct lys_node *sub;
Michal Vasko488c19e2015-10-20 15:21:00 +0200648 const char *str;
Michal Vasko6f25f212015-07-07 15:42:07 +0200649
Michal Vasko5d112852016-02-12 16:47:13 +0100650 str = transform_json2schema(module, augment->target_name);
Michal Vasko488c19e2015-10-20 15:21:00 +0200651 ly_print(out, "%*saugment \"%s\" {\n", LEVEL, INDENT, str);
652 lydict_remove(module->ctx, str);
Michal Vasko6f25f212015-07-07 15:42:07 +0200653 level++;
654
Radek Krejci32cce7c2015-12-09 16:44:13 +0100655 yang_print_nacmext(out, level, (struct lys_node *)augment, module, NULL);
656 yang_print_snode_common(out, level, (struct lys_node *)augment, NULL);
Michal Vasko6f25f212015-07-07 15:42:07 +0200657
658 for (i = 0; i < augment->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200659 yang_print_iffeature(out, level, module, augment->features[i]);
Michal Vasko6f25f212015-07-07 15:42:07 +0200660 }
661
662 if (augment->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200663 yang_print_when(out, level, module, augment->when);
Michal Vasko6f25f212015-07-07 15:42:07 +0200664 }
665
666 LY_TREE_FOR(augment->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100667 /* only our augment */
668 if (sub->parent != (struct lys_node *)augment) {
669 continue;
670 }
Radek Krejci76b07902015-10-09 09:11:25 +0200671 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200672 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
673 LYS_USES | LYS_ANYXML | LYS_CASE);
Michal Vasko6f25f212015-07-07 15:42:07 +0200674 }
675
676 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200677 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko6f25f212015-07-07 15:42:07 +0200678}
679
680static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100681yang_print_typedef(struct lyout *out, int level, const struct lys_module *module, const struct lys_tpdf *tpdf)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200682{
Radek Krejci76b07902015-10-09 09:11:25 +0200683 ly_print(out, "%*stypedef %s {\n", LEVEL, INDENT, tpdf->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200684 level++;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200685
Radek Krejci32cce7c2015-12-09 16:44:13 +0100686 yang_print_snode_common(out, level, (struct lys_node *)tpdf, NULL);
Radek Krejci76b07902015-10-09 09:11:25 +0200687 yang_print_type(out, level, module, &tpdf->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200688 if (tpdf->units != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200689 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, tpdf->units);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200690 }
691 if (tpdf->dflt != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200692 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, tpdf->dflt);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200693 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200694
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200695 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200696 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200697}
698
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200699static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100700yang_print_identity(struct lyout *out, int level, const struct lys_ident *ident)
Radek Krejci6793db02015-05-22 17:49:54 +0200701{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100702 int flag = 0;
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100703 struct lys_module *mod;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100704
705 ly_print(out, "%*sidentity %s", LEVEL, INDENT, ident->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200706 level++;
Radek Krejci6793db02015-05-22 17:49:54 +0200707
Radek Krejci32cce7c2015-12-09 16:44:13 +0100708 yang_print_snode_common(out, level, (struct lys_node *)ident, &flag);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200709 if (ident->base) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100710 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200711 ly_print(out, "%*sbase ", LEVEL, INDENT);
Michal Vasko6c629ac2016-02-15 14:08:23 +0100712 mod = lys_module(ident->base->module);
Michal Vasko1dae8ec2016-02-15 14:49:01 +0100713 if (lys_module(ident->module) != mod) {
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100714 ly_print(out, "%s:", transform_module_name2import_prefix(ident->module, mod->name));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200715 }
Radek Krejci76b07902015-10-09 09:11:25 +0200716 ly_print(out, "%s;\n", ident->base->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200717 }
Radek Krejci6793db02015-05-22 17:49:54 +0200718
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200719 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100720 yang_print_close(out, level, flag);
Radek Krejci6793db02015-05-22 17:49:54 +0200721}
722
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200723static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100724yang_print_container(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200725{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100726 int i, flag = 0;
Radek Krejci76512572015-08-04 09:47:08 +0200727 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200728 struct lys_node_container *cont = (struct lys_node_container *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200729
Radek Krejci32cce7c2015-12-09 16:44:13 +0100730 ly_print(out, "%*scontainer %s", LEVEL, INDENT, node->name);
Michal Vasko7f976ee2015-06-09 13:55:41 +0200731
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200732 level++;
Radek Krejci6a113852015-07-03 16:04:20 +0200733
Radek Krejci32cce7c2015-12-09 16:44:13 +0100734 yang_print_nacmext(out, level, node, node->module, &flag);
Radek Krejci6a113852015-07-03 16:04:20 +0200735
Radek Krejci8d81e002015-12-10 11:18:59 +0100736 if (cont->when) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100737 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100738 yang_print_when(out, level, node->module, cont->when);
739 }
740
741 for (i = 0; i < cont->features_size; i++) {
742 yang_print_open(out, &flag);
743 yang_print_iffeature(out, level, node->module, cont->features[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200744 }
Michal Vasko7f976ee2015-06-09 13:55:41 +0200745
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200746 for (i = 0; i < cont->must_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100747 yang_print_open(out, &flag);
Michal Vaskof9893382015-10-09 14:03:04 +0200748 yang_print_must(out, level, node->module, &cont->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200749 }
Michal Vasko7f976ee2015-06-09 13:55:41 +0200750
Radek Krejci8d81e002015-12-10 11:18:59 +0100751 if (cont->presence != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100752 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100753 yang_print_text(out, level, "presence", cont->presence, 1);
Michal Vasko4773b762015-07-07 12:15:10 +0200754 }
755
Radek Krejci8d81e002015-12-10 11:18:59 +0100756 yang_print_snode_common2(out, level, node, &flag);
757
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200758 for (i = 0; i < cont->tpdf_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100759 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200760 yang_print_typedef(out, level, node->module, &cont->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200761 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200762
Radek Krejci1d82ef62015-08-07 14:44:40 +0200763 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100764 /* augments */
765 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200766 continue;
767 }
Radek Krejci32cce7c2015-12-09 16:44:13 +0100768 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200769 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200770 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
771 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200772 }
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200773
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200774 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100775 yang_print_close(out, level, flag);
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200776}
777
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200778static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100779yang_print_case(struct lyout *out, int level, const struct lys_node *node)
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200780{
Michal Vasko4773b762015-07-07 12:15:10 +0200781 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200782 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200783 struct lys_node_case *cas = (struct lys_node_case *)node;
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200784
Radek Krejci76b07902015-10-09 09:11:25 +0200785 ly_print(out, "%*scase %s {\n", LEVEL, INDENT, cas->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200786 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100787 yang_print_nacmext(out, level, node, node->module, NULL);
788 yang_print_snode_common2(out, level, node, NULL);
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200789
Michal Vasko4773b762015-07-07 12:15:10 +0200790 for (i = 0; i < cas->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200791 yang_print_iffeature(out, level, node->module, cas->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200792 }
793
Michal Vasko1f0428a2015-07-07 14:55:04 +0200794 if (cas->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200795 yang_print_when(out, level, node->module, cas->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200796 }
797
Radek Krejci1d82ef62015-08-07 14:44:40 +0200798 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100799 /* augments */
800 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200801 continue;
802 }
Radek Krejci76b07902015-10-09 09:11:25 +0200803 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200804 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
805 LYS_USES | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200806 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200807
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200808 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200809 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200810}
811
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200812static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100813yang_print_choice(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200814{
Michal Vasko4773b762015-07-07 12:15:10 +0200815 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200816 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200817 struct lys_node_choice *choice = (struct lys_node_choice *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200818
Radek Krejci76b07902015-10-09 09:11:25 +0200819 ly_print(out, "%*schoice %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +0200820
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200821 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100822 yang_print_nacmext(out, level, node, node->module, NULL);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200823 if (choice->dflt != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200824 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, choice->dflt->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200825 }
Michal Vasko16083662015-06-09 14:00:45 +0200826
Radek Krejci32cce7c2015-12-09 16:44:13 +0100827 yang_print_snode_common2(out, level, node, NULL);
Michal Vasko4773b762015-07-07 12:15:10 +0200828
829 for (i = 0; i < choice->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200830 yang_print_iffeature(out, level, node->module, choice->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200831 }
832
Michal Vasko1f0428a2015-07-07 14:55:04 +0200833 if (choice->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200834 yang_print_when(out, level, node->module, choice->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200835 }
836
Radek Krejci1d82ef62015-08-07 14:44:40 +0200837 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100838 /* augments */
839 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200840 continue;
841 }
Radek Krejci76b07902015-10-09 09:11:25 +0200842 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200843 LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYXML | LYS_CASE);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200844 }
845 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200846 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200847}
848
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200849static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100850yang_print_leaf(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200851{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200852 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200853 struct lys_node_leaf *leaf = (struct lys_node_leaf *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200854
Radek Krejci76b07902015-10-09 09:11:25 +0200855 ly_print(out, "%*sleaf %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +0200856
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200857 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100858 yang_print_nacmext(out, level, node, node->module, NULL);
Radek Krejci8d81e002015-12-10 11:18:59 +0100859 if (leaf->when) {
860 yang_print_when(out, level, node->module, leaf->when);
861 }
Michal Vasko4773b762015-07-07 12:15:10 +0200862 for (i = 0; i < leaf->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200863 yang_print_iffeature(out, level, node->module, leaf->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200864 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200865 for (i = 0; i < leaf->must_size; i++) {
Michal Vaskof9893382015-10-09 14:03:04 +0200866 yang_print_must(out, level, node->module, &leaf->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200867 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100868 yang_print_snode_common2(out, level, node, NULL);
Radek Krejci76b07902015-10-09 09:11:25 +0200869 yang_print_type(out, level, node->module, &leaf->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200870 if (leaf->units != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200871 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, leaf->units);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200872 }
873 if (leaf->dflt != NULL) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100874 switch (leaf->type.base) {
875 case LY_TYPE_STRING:
876 case LY_TYPE_LEAFREF:
877 case LY_TYPE_IDENT:
878 case LY_TYPE_UNION:
879 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, leaf->dflt);
880 break;
881 default:
882 ly_print(out, "%*sdefault %s;\n", LEVEL, INDENT, leaf->dflt);
883 break;
884 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200885 }
886 level--;
Michal Vasko16083662015-06-09 14:00:45 +0200887
Radek Krejci76b07902015-10-09 09:11:25 +0200888 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko16083662015-06-09 14:00:45 +0200889}
890
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200891static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100892yang_print_anyxml(struct lyout *out, int level, const struct lys_node *node)
Michal Vasko16083662015-06-09 14:00:45 +0200893{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100894 int i, flag = 0;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200895 struct lys_node_anyxml *anyxml = (struct lys_node_anyxml *)node;
Michal Vasko16083662015-06-09 14:00:45 +0200896
Radek Krejci32cce7c2015-12-09 16:44:13 +0100897 ly_print(out, "%*sanyxml %s", LEVEL, INDENT, anyxml->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200898 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100899 yang_print_nacmext(out, level, node, node->module, &flag);
900 yang_print_snode_common2(out, level, node, &flag);
Michal Vasko4773b762015-07-07 12:15:10 +0200901 for (i = 0; i < anyxml->features_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100902 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200903 yang_print_iffeature(out, level, node->module, anyxml->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200904 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200905 for (i = 0; i < anyxml->must_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100906 yang_print_open(out, &flag);
Michal Vaskof9893382015-10-09 14:03:04 +0200907 yang_print_must(out, level, node->module, &anyxml->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200908 }
Michal Vasko1f0428a2015-07-07 14:55:04 +0200909 if (anyxml->when) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100910 yang_print_open(out, &flag);
Michal Vaskof9893382015-10-09 14:03:04 +0200911 yang_print_when(out, level, node->module, anyxml->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200912 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200913 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100914 yang_print_close(out, level, flag);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200915}
916
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200917static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100918yang_print_leaflist(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200919{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200920 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200921 struct lys_node_leaflist *llist = (struct lys_node_leaflist *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200922
Radek Krejci76b07902015-10-09 09:11:25 +0200923 ly_print(out, "%*sleaf-list %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +0200924
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200925 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100926 yang_print_nacmext(out, level, node, node->module, NULL);
Radek Krejci8d81e002015-12-10 11:18:59 +0100927 if (llist->when) {
928 yang_print_when(out, level, llist->module, llist->when);
929 }
Michal Vasko4773b762015-07-07 12:15:10 +0200930 for (i = 0; i < llist->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200931 yang_print_iffeature(out, level, node->module, llist->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200932 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100933 for (i = 0; i < llist->must_size; i++) {
934 yang_print_must(out, level, node->module, &llist->must[i]);
935 }
936 yang_print_snode_common2(out, level, node, NULL);
937 yang_print_type(out, level, node->module, &llist->type);
938 if (llist->units != NULL) {
939 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, llist->units);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200940 }
941 if (llist->min > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200942 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, llist->min);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200943 }
944 if (llist->max > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200945 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, llist->max);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200946 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100947 if (llist->flags & LYS_USERORDERED) {
948 ly_print(out, "%*sordered-by user;\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200949 }
950 level--;
Michal Vasko16083662015-06-09 14:00:45 +0200951
Radek Krejci76b07902015-10-09 09:11:25 +0200952 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200953}
954
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200955static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100956yang_print_list(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200957{
Michal Vasko1ef07972015-07-07 14:01:35 +0200958 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200959 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200960 struct lys_node_list *list = (struct lys_node_list *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200961
Radek Krejci76b07902015-10-09 09:11:25 +0200962 ly_print(out, "%*slist %s {\n", LEVEL, INDENT, node->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200963 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100964 yang_print_nacmext(out, level, node, node->module, NULL);
Radek Krejci8d81e002015-12-10 11:18:59 +0100965 if (list->when) {
966 yang_print_when(out, level, list->module, list->when);
967 }
Michal Vasko4773b762015-07-07 12:15:10 +0200968 for (i = 0; i < list->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200969 yang_print_iffeature(out, level, node->module, list->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200970 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100971 for (i = 0; i < list->must_size; i++) {
972 yang_print_must(out, level, list->module, &list->must[i]);
973 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200974 if (list->keys_size) {
Radek Krejci76b07902015-10-09 09:11:25 +0200975 ly_print(out, "%*skey \"", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200976 for (i = 0; i < list->keys_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200977 ly_print(out, "%s%s", list->keys[i]->name, i + 1 < list->keys_size ? " " : "");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200978 }
Radek Krejci76b07902015-10-09 09:11:25 +0200979 ly_print(out, "\";\n");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200980 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200981 for (i = 0; i < list->unique_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200982 yang_print_unique(out, level, &list->unique[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200983 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100984 yang_print_snode_common2(out, level, node, NULL);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200985 if (list->min > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200986 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, list->min);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200987 }
988 if (list->max > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200989 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, list->max);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200990 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100991 if (list->flags & LYS_USERORDERED) {
992 ly_print(out, "%*sordered-by user;\n", LEVEL, INDENT);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200993 }
Michal Vaskodff818b2015-06-09 14:01:12 +0200994
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200995 for (i = 0; i < list->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200996 yang_print_typedef(out, level, list->module, &list->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200997 }
Radek Krejci1d82ef62015-08-07 14:44:40 +0200998 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100999 /* augments */
1000 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +02001001 continue;
1002 }
Radek Krejci76b07902015-10-09 09:11:25 +02001003 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +02001004 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
1005 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001006 }
1007 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001008 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001009}
1010
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001011static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001012yang_print_grouping(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001013{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001014 int i;
Michal Vasko0c5e9282016-02-15 13:11:57 +01001015 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001016 struct lys_node_grp *grp = (struct lys_node_grp *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001017
Radek Krejci76b07902015-10-09 09:11:25 +02001018 ly_print(out, "%*sgrouping %s {\n", LEVEL, INDENT, node->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001019 level++;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001020
Radek Krejci32cce7c2015-12-09 16:44:13 +01001021 yang_print_snode_common(out, level, node, NULL);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001022
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001023 for (i = 0; i < grp->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +02001024 yang_print_typedef(out, level, node->module, &grp->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001025 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001026
Michal Vasko0c5e9282016-02-15 13:11:57 +01001027 LY_TREE_FOR(node->child, sub) {
1028 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +02001029 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
1030 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001031 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001032
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001033 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001034 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001035}
1036
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001037static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001038yang_print_uses(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001039{
Radek Krejci32cce7c2015-12-09 16:44:13 +01001040 int i, flag = 0;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001041 struct lys_node_uses *uses = (struct lys_node_uses *)node;
Michal Vasko1bb7a5a2016-02-05 14:28:02 +01001042 struct lys_module *mod;
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001043
Radek Krejci76b07902015-10-09 09:11:25 +02001044 ly_print(out, "%*suses ", LEVEL, INDENT);
Michal Vasko1bb7a5a2016-02-05 14:28:02 +01001045 if (node->child) {
Michal Vasko6c629ac2016-02-15 14:08:23 +01001046 mod = lys_node_module(node->child);
Michal Vasko1dae8ec2016-02-15 14:49:01 +01001047 if (lys_node_module(node) != mod) {
Michal Vasko1bb7a5a2016-02-05 14:28:02 +01001048 ly_print(out, "%s:", transform_module_name2import_prefix(node->module, mod->name));
1049 }
Michal Vaskoc39c4b12015-07-07 14:55:33 +02001050 }
Radek Krejci32cce7c2015-12-09 16:44:13 +01001051 ly_print(out, "%s", uses->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001052 level++;
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001053
Radek Krejci32cce7c2015-12-09 16:44:13 +01001054 yang_print_nacmext(out, level, node, node->module, &flag);
1055 yang_print_snode_common(out, level, node, &flag);
Michal Vasko4773b762015-07-07 12:15:10 +02001056 for (i = 0; i < uses->features_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001057 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001058 yang_print_iffeature(out, level, node->module, uses->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +02001059 }
Michal Vasko1f0428a2015-07-07 14:55:04 +02001060 if (uses->when) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001061 yang_print_open(out, &flag);
Michal Vaskof9893382015-10-09 14:03:04 +02001062 yang_print_when(out, level, node->module, uses->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +02001063 }
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001064
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001065 for (i = 0; i < uses->refine_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001066 yang_print_open(out, &flag);
Michal Vaskof9893382015-10-09 14:03:04 +02001067 yang_print_refine(out, level, node->module, &uses->refine[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001068 }
Michal Vasko00b7cfe2015-06-09 13:56:38 +02001069
Michal Vasko6f25f212015-07-07 15:42:07 +02001070 for (i = 0; i < uses->augment_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001071 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001072 yang_print_augment(out, level, node->module, &uses->augment[i]);
Michal Vasko6f25f212015-07-07 15:42:07 +02001073 }
1074
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001075 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001076 yang_print_close(out, level, flag);
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001077}
1078
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001079static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001080yang_print_input_output(struct lyout *out, int level, const struct lys_node *node)
Michal Vasko5bbae102015-06-16 12:16:44 +02001081{
Michal Vaskof4d3d742015-06-16 11:51:09 +02001082 int i;
Radek Krejci76512572015-08-04 09:47:08 +02001083 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001084 struct lys_node_rpc_inout *inout = (struct lys_node_rpc_inout *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +02001085
Radek Krejci76b07902015-10-09 09:11:25 +02001086 ly_print(out, "%*s%s {\n", LEVEL, INDENT, (inout->nodetype == LYS_INPUT ? "input" : "output"));
Michal Vaskof4d3d742015-06-16 11:51:09 +02001087
1088 level++;
1089 for (i = 0; i < inout->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +02001090 yang_print_typedef(out, level, node->module, &inout->tpdf[i]);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001091 }
1092
Radek Krejci1d82ef62015-08-07 14:44:40 +02001093 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +01001094 /* augments */
1095 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +02001096 continue;
1097 }
Radek Krejci76b07902015-10-09 09:11:25 +02001098 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +02001099 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
1100 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001101 }
1102
1103 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001104 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001105}
1106
1107static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001108yang_print_rpc(struct lyout *out, int level, const struct lys_node *node)
Michal Vaskof4d3d742015-06-16 11:51:09 +02001109{
Radek Krejci32cce7c2015-12-09 16:44:13 +01001110 int i, flag = 0;
Radek Krejci76512572015-08-04 09:47:08 +02001111 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001112 struct lys_node_rpc *rpc = (struct lys_node_rpc *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +02001113
Radek Krejci32cce7c2015-12-09 16:44:13 +01001114 ly_print(out, "%*srpc %s", LEVEL, INDENT, node->name);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001115
1116 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001117 yang_print_snode_common(out, level, node, &flag);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001118
Michal Vasko4773b762015-07-07 12:15:10 +02001119 for (i = 0; i < rpc->features_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001120 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001121 yang_print_iffeature(out, level, node->module, rpc->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +02001122 }
1123
Michal Vaskof4d3d742015-06-16 11:51:09 +02001124 for (i = 0; i < rpc->tpdf_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001125 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001126 yang_print_typedef(out, level, node->module, &rpc->tpdf[i]);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001127 }
1128
Radek Krejci1d82ef62015-08-07 14:44:40 +02001129 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +01001130 /* augments */
1131 if (sub->parent != node) {
Radek Krejcic071c542016-01-27 14:57:51 +01001132 continue;
1133 }
Radek Krejci32cce7c2015-12-09 16:44:13 +01001134 yang_print_open(out, &flag);
1135 yang_print_snode(out, level, sub, LYS_GROUPING | LYS_INPUT | LYS_OUTPUT);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001136 }
1137
1138 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001139 yang_print_close(out, level, flag);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001140}
1141
1142static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001143yang_print_notif(struct lyout *out, int level, const struct lys_node *node)
Michal Vasko7690bc12015-06-16 12:26:05 +02001144{
Radek Krejci32cce7c2015-12-09 16:44:13 +01001145 int i, flag = 0;
Radek Krejci76512572015-08-04 09:47:08 +02001146 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001147 struct lys_node_notif *notif = (struct lys_node_notif *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +02001148
Radek Krejci32cce7c2015-12-09 16:44:13 +01001149 ly_print(out, "%*snotification %s", LEVEL, INDENT, node->name);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001150
1151 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001152 yang_print_snode_common(out, level, node, &flag);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001153
Michal Vasko4773b762015-07-07 12:15:10 +02001154 for (i = 0; i < notif->features_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001155 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001156 yang_print_iffeature(out, level, node->module, notif->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +02001157 }
1158
Michal Vaskof4d3d742015-06-16 11:51:09 +02001159 for (i = 0; i < notif->tpdf_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001160 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001161 yang_print_typedef(out, level, node->module, &notif->tpdf[i]);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001162 }
1163
Radek Krejci1d82ef62015-08-07 14:44:40 +02001164 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +01001165 /* augments */
1166 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +02001167 continue;
1168 }
Radek Krejci32cce7c2015-12-09 16:44:13 +01001169 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001170 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +02001171 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
1172 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001173 }
1174
1175 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001176 yang_print_close(out, level, flag);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001177}
1178
1179static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001180yang_print_snode(struct lyout *out, int level, const struct lys_node *node, int mask)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001181{
Radek Krejci1d82ef62015-08-07 14:44:40 +02001182 switch (node->nodetype & mask) {
Radek Krejci76512572015-08-04 09:47:08 +02001183 case LYS_CONTAINER:
Radek Krejci76b07902015-10-09 09:11:25 +02001184 yang_print_container(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001185 break;
Radek Krejci76512572015-08-04 09:47:08 +02001186 case LYS_CHOICE:
Radek Krejci76b07902015-10-09 09:11:25 +02001187 yang_print_choice(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001188 break;
Radek Krejci76512572015-08-04 09:47:08 +02001189 case LYS_LEAF:
Radek Krejci76b07902015-10-09 09:11:25 +02001190 yang_print_leaf(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001191 break;
Radek Krejci76512572015-08-04 09:47:08 +02001192 case LYS_LEAFLIST:
Radek Krejci76b07902015-10-09 09:11:25 +02001193 yang_print_leaflist(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001194 break;
Radek Krejci76512572015-08-04 09:47:08 +02001195 case LYS_LIST:
Radek Krejci76b07902015-10-09 09:11:25 +02001196 yang_print_list(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001197 break;
Radek Krejci76512572015-08-04 09:47:08 +02001198 case LYS_USES:
Radek Krejci76b07902015-10-09 09:11:25 +02001199 yang_print_uses(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001200 break;
Radek Krejci76512572015-08-04 09:47:08 +02001201 case LYS_GROUPING:
Radek Krejci76b07902015-10-09 09:11:25 +02001202 yang_print_grouping(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001203 break;
Radek Krejci76512572015-08-04 09:47:08 +02001204 case LYS_ANYXML:
Radek Krejci76b07902015-10-09 09:11:25 +02001205 yang_print_anyxml(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001206 break;
Radek Krejci76512572015-08-04 09:47:08 +02001207 case LYS_CASE:
Radek Krejci76b07902015-10-09 09:11:25 +02001208 yang_print_case(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001209 break;
Radek Krejci76512572015-08-04 09:47:08 +02001210 case LYS_INPUT:
1211 case LYS_OUTPUT:
Radek Krejci76b07902015-10-09 09:11:25 +02001212 yang_print_input_output(out, level, node);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001213 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001214 default:
1215 break;
1216 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001217}
1218
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001219int
Michal Vasko1e62a092015-12-01 12:27:20 +01001220yang_print_model(struct lyout *out, const struct lys_module *module)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001221{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001222 unsigned int i;
1223 int level = 0;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001224#define LEVEL (level*2)
1225
Radek Krejci1d82ef62015-08-07 14:44:40 +02001226 struct lys_node *node;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001227
Radek Krejci8d81e002015-12-10 11:18:59 +01001228 /* (sub)module-header-stmts */
Michal Vasko116172e2015-07-07 11:54:37 +02001229 if (module->type) {
Radek Krejci76b07902015-10-09 09:11:25 +02001230 ly_print(out, "submodule %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,
1234 ((struct lys_submodule *)module)->belongsto->version == 2 ? "1.1" : "1");
Radek Krejci8d81e002015-12-10 11:18:59 +01001235 }
Radek Krejci76b07902015-10-09 09:11:25 +02001236 ly_print(out, "%*sbelongs-to %s {\n", LEVEL, INDENT, ((struct lys_submodule *)module)->belongsto->name);
Michal Vasko116172e2015-07-07 11:54:37 +02001237 level++;
Radek Krejci8d81e002015-12-10 11:18:59 +01001238 ly_print(out, "%*sprefix %s;\n", LEVEL, INDENT, module->prefix);
Michal Vasko116172e2015-07-07 11:54:37 +02001239 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001240 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko116172e2015-07-07 11:54:37 +02001241 } else {
Radek Krejci76b07902015-10-09 09:11:25 +02001242 ly_print(out, "module %s {%s\n", module->name, (module->deviated ? " // DEVIATED" : ""));
Michal Vasko116172e2015-07-07 11:54:37 +02001243 level++;
Radek Krejci8d81e002015-12-10 11:18:59 +01001244 if (module->version) {
Radek Krejcic071c542016-01-27 14:57:51 +01001245 ly_print(out, "%*syang-version %s;\n", LEVEL, INDENT, module->version == 2 ? "1.1" : "1");
Radek Krejci8d81e002015-12-10 11:18:59 +01001246 }
Radek Krejci76b07902015-10-09 09:11:25 +02001247 ly_print(out, "%*snamespace \"%s\";\n", LEVEL, INDENT, module->ns);
Radek Krejci8d81e002015-12-10 11:18:59 +01001248 ly_print(out, "%*sprefix %s;\n", LEVEL, INDENT, module->prefix);
Michal Vasko116172e2015-07-07 11:54:37 +02001249 }
Radek Krejcib0594bf2015-05-21 23:51:27 +02001250
Radek Krejci8d81e002015-12-10 11:18:59 +01001251 /* linkage-stmts */
1252 if (module->imp_size || module->inc_size) {
1253 ly_print(out, "\n");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001254 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001255 for (i = 0; i < module->imp_size; i++) {
Radek Krejcic071c542016-01-27 14:57:51 +01001256 if (module->imp[i].external) {
1257 continue;
1258 }
Michal Vaskoc8e3ce02016-02-12 14:28:35 +01001259 ly_print(out, "%*simport %s {\n", LEVEL, INDENT, module->imp[i].module->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001260 level++;
Radek Krejci8d81e002015-12-10 11:18:59 +01001261 ly_print(out, "%*sprefix %s;\n", LEVEL, INDENT, module->imp[i].prefix);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001262 if (module->imp[i].rev[0]) {
Michal Vasko86dfd262016-02-15 14:26:31 +01001263 ly_print(out, "%*srevision-date %s;\n", LEVEL, INDENT, module->imp[i].rev);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001264 }
1265 level--;
Michal Vaskoc8e3ce02016-02-12 14:28:35 +01001266 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001267 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001268 for (i = 0; i < module->inc_size; i++) {
Radek Krejcic071c542016-01-27 14:57:51 +01001269 if (module->inc[i].external) {
1270 continue;
1271 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001272 if (module->inc[i].rev[0]) {
Radek Krejci76b07902015-10-09 09:11:25 +02001273 ly_print(out, "%*sinclude \"%s\" {\n", LEVEL, INDENT, module->inc[i].submodule->name);
Radek Krejci8d81e002015-12-10 11:18:59 +01001274 level++;
Michal Vasko86dfd262016-02-15 14:26:31 +01001275 ly_print(out, "%*srevision-date %s\n;", LEVEL, INDENT, module->inc[i].rev);
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
Michal Vasko0c5e9282016-02-15 13:11:57 +01001342 LY_TREE_FOR(lys_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:
Radek Krejcibac81762015-10-09 10:19:52 +02001350 yang_print_rpc(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");
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}