blob: c0a411496af9a64652e58a298ac4fb04c1ca364b [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>
23#include <stdio.h>
24#include <string.h>
25
Radek Krejci998a0b82015-08-17 13:14:36 +020026#include "common.h"
Michal Vasko2d162e12015-09-24 14:33:29 +020027#include "tree_schema.h"
Radek Krejcida04f4a2015-05-21 12:54:09 +020028
29#define INDENT ""
30#define LEVEL (level*2)
31
Radek Krejci1d82ef62015-08-07 14:44:40 +020032static void yang_print_snode(FILE *f, int level, struct lys_node *node, int mask);
Radek Krejcida04f4a2015-05-21 12:54:09 +020033
Michal Vaskoc39c4b12015-07-07 14:55:33 +020034static const char*
Radek Krejcib8048692015-08-05 13:36:34 +020035get_module_import_prefix(struct lys_module *main_mod, struct lys_module *imp_mod)
Michal Vaskoc39c4b12015-07-07 14:55:33 +020036{
37 int i, j;
38
39 for (i = 0; i < main_mod->imp_size; ++i) {
40 if (main_mod->imp[i].module == imp_mod) {
41 return main_mod->imp[i].prefix;
42 }
43 }
44
45 for (j = 0; j < main_mod->inc_size; ++j) {
46 for (i = 0; i < main_mod->inc[j].submodule->imp_size; ++i) {
47 if (main_mod->inc[j].submodule->imp[i].module == imp_mod) {
48 return main_mod->inc[j].submodule->imp[i].prefix;
49 }
50 }
51 }
52
53 return NULL;
54}
55
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020056static void
Michal Vasko5bbae102015-06-16 12:16:44 +020057yang_print_text(FILE *f, int level, const char *name, const char *text)
Radek Krejcida04f4a2015-05-21 12:54:09 +020058{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020059 const char *s, *t;
Radek Krejcida04f4a2015-05-21 12:54:09 +020060
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020061 fprintf(f, "%*s%s\n", LEVEL, INDENT, name);
62 level++;
Radek Krejcida04f4a2015-05-21 12:54:09 +020063
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020064 fprintf(f, "%*s\"", LEVEL, INDENT);
65 t = text;
66 while ((s = strchr(t, '\n'))) {
Michal Vasko5bbae102015-06-16 12:16:44 +020067 fwrite(t, sizeof *t, (s - t) + 1, f);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020068 t = s + 1;
69 fprintf(f, "%*s", LEVEL, INDENT);
70 }
Radek Krejcida04f4a2015-05-21 12:54:09 +020071
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020072 fprintf(f, "%s\";\n\n", t);
73 level--;
Radek Krejcida04f4a2015-05-21 12:54:09 +020074
75}
76
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020077static void
Radek Krejci1d82ef62015-08-07 14:44:40 +020078yang_print_nacmext(FILE *f, int level, struct lys_node *node, struct lys_module *module)
Radek Krejcida04f4a2015-05-21 12:54:09 +020079{
Radek Krejci6764bb32015-07-03 15:16:04 +020080 int i, j;
Radek Krejci86909d22015-07-03 16:11:25 +020081 const char *prefix = NULL;
Radek Krejci6764bb32015-07-03 15:16:04 +020082
Radek Krejci1d82ef62015-08-07 14:44:40 +020083 if (node->nacm && (!node->parent || node->parent->nacm != node->nacm)) {
Radek Krejci6764bb32015-07-03 15:16:04 +020084 /* locate ietf-netconf-acm module in imports */
Michal Vaskodb7aa992015-07-08 09:37:38 +020085 if (!strcmp(module->name, "ietf-netconf-acm")) {
86 prefix = module->prefix;
Radek Krejci6764bb32015-07-03 15:16:04 +020087 } else {
88 /* search in imports */
Michal Vaskodb7aa992015-07-08 09:37:38 +020089 for (i = 0; i < module->imp_size; i++) {
90 if (!strcmp(module->imp[i].module->name, "ietf-netconf-acm")) {
91 prefix = module->imp[i].prefix;
Radek Krejci6764bb32015-07-03 15:16:04 +020092 break;
93 }
94 }
95 /* and in imports of includes */
96 if (!prefix) {
Michal Vaskodb7aa992015-07-08 09:37:38 +020097 for (j = 0; j < module->inc_size; j++) {
98 for (i = 0; i < module->inc[j].submodule->imp_size; i++) {
99 if (!strcmp(module->inc[j].submodule->imp[i].module->name, "ietf-netconf-acm")) {
100 prefix = module->inc[j].submodule->imp[i].prefix;
Radek Krejci6764bb32015-07-03 15:16:04 +0200101 break;
102 }
103 }
104 }
105 }
106 }
107
Radek Krejci1d82ef62015-08-07 14:44:40 +0200108 if ((node->nacm & LYS_NACM_DENYW) && (!node->parent || !(node->parent->nacm & LYS_NACM_DENYW))) {
Radek Krejci6764bb32015-07-03 15:16:04 +0200109 fprintf(f, "%*s%s:default-deny-write;\n", LEVEL, INDENT, prefix);
110 }
Radek Krejci1d82ef62015-08-07 14:44:40 +0200111 if ((node->nacm & LYS_NACM_DENYA) && (!node->parent || !(node->parent->nacm & LYS_NACM_DENYA))) {
Radek Krejci6764bb32015-07-03 15:16:04 +0200112 fprintf(f, "%*s%s:default-deny-all;\n", LEVEL, INDENT, prefix);
113 }
114 }
Radek Krejci6a113852015-07-03 16:04:20 +0200115}
116
117/*
118 * Covers:
119 * description, reference, status
120 */
121static void
Radek Krejci1d82ef62015-08-07 14:44:40 +0200122yang_print_snode_common(FILE *f, int level, struct lys_node *node)
Radek Krejci6a113852015-07-03 16:04:20 +0200123{
Radek Krejci1d82ef62015-08-07 14:44:40 +0200124 if (node->flags & LYS_STATUS_CURR) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200125 fprintf(f, "%*sstatus \"current\";\n", LEVEL, INDENT);
Radek Krejci1d82ef62015-08-07 14:44:40 +0200126 } else if (node->flags & LYS_STATUS_DEPRC) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200127 fprintf(f, "%*sstatus \"deprecated\";\n", LEVEL, INDENT);
Radek Krejci1d82ef62015-08-07 14:44:40 +0200128 } else if (node->flags & LYS_STATUS_OBSLT) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200129 fprintf(f, "%*sstatus \"obsolete\";\n", LEVEL, INDENT);
130 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200131
Radek Krejci1d82ef62015-08-07 14:44:40 +0200132 if (node->dsc) {
133 yang_print_text(f, level, "description", node->dsc);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200134 }
Radek Krejci1d82ef62015-08-07 14:44:40 +0200135 if (node->ref) {
136 yang_print_text(f, level, "reference", node->ref);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200137 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200138}
139
140/*
141 * Covers:
Michal Vasko2b8faed2015-06-09 12:51:20 +0200142 * config, mandatory
Radek Krejcida04f4a2015-05-21 12:54:09 +0200143 * description, reference, status
144 */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200145static void
Radek Krejci1d82ef62015-08-07 14:44:40 +0200146yang_print_snode_common2(FILE *f, int level, struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200147{
Radek Krejci1d82ef62015-08-07 14:44:40 +0200148 if (!node->parent || (node->parent->flags & LYS_CONFIG_MASK) != (node->flags & LYS_CONFIG_MASK)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200149 /* print config only when it differs from the parent or in root */
Radek Krejci1d82ef62015-08-07 14:44:40 +0200150 if (node->flags & LYS_CONFIG_W) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200151 fprintf(f, "%*sconfig \"true\";\n", LEVEL, INDENT);
Radek Krejci1d82ef62015-08-07 14:44:40 +0200152 } else if (node->flags & LYS_CONFIG_R) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200153 fprintf(f, "%*sconfig \"false\";\n", LEVEL, INDENT);
154 }
155 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200156
Radek Krejci1d82ef62015-08-07 14:44:40 +0200157 if (node->flags & LYS_MAND_TRUE) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200158 fprintf(f, "%*smandatory \"true\";\n", LEVEL, INDENT);
Radek Krejci1d82ef62015-08-07 14:44:40 +0200159 } else if (node->flags & LYS_MAND_FALSE) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200160 fprintf(f, "%*smandatory \"false\";\n", LEVEL, INDENT);
161 }
Michal Vasko2b8faed2015-06-09 12:51:20 +0200162
Radek Krejci1d82ef62015-08-07 14:44:40 +0200163 yang_print_snode_common(f, level, node);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200164}
165
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200166static void
Radek Krejcib8048692015-08-05 13:36:34 +0200167yang_print_iffeature(FILE *f, int level, struct lys_module *module, struct lys_feature *feat)
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200168{
169 fprintf(f, "%*sif-feature ", LEVEL, INDENT);
170 if ((feat->module != module) && !feat->module->type) {
171 fprintf(f, "%s:", get_module_import_prefix(module, feat->module));
172 }
173 fprintf(f, "%s;\n", feat->name);
174}
175
176static void
Radek Krejcib8048692015-08-05 13:36:34 +0200177yang_print_feature(FILE *f, int level, struct lys_feature *feat)
Michal Vasko4773b762015-07-07 12:15:10 +0200178{
Michal Vasko30f6e912015-07-07 12:24:27 +0200179 int i;
180
Michal Vasko4773b762015-07-07 12:15:10 +0200181 fprintf(f, "%*sfeature %s {\n", LEVEL, INDENT, feat->name);
182 level++;
183
Radek Krejci1d82ef62015-08-07 14:44:40 +0200184 yang_print_snode_common(f, level, (struct lys_node *)feat);
Michal Vasko30f6e912015-07-07 12:24:27 +0200185 for (i = 0; i < feat->features_size; ++i) {
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200186 yang_print_iffeature(f, level, feat->module, feat->features[i]);
Michal Vasko30f6e912015-07-07 12:24:27 +0200187 }
Michal Vasko4773b762015-07-07 12:15:10 +0200188
189 level--;
190 fprintf(f, "%*s}\n", LEVEL, INDENT);
191}
192
193static void
Radek Krejci1574a8d2015-08-03 14:16:52 +0200194yang_print_restr(FILE *f, int level, struct lys_restr *restr)
Radek Krejci41726f92015-06-19 13:11:05 +0200195{
Radek Krejci0bd5db42015-06-19 13:30:07 +0200196 if (restr->dsc != NULL) {
197 yang_print_text(f, level, "description", restr->dsc);
Radek Krejci41726f92015-06-19 13:11:05 +0200198 }
Radek Krejci0bd5db42015-06-19 13:30:07 +0200199 if (restr->ref != NULL) {
200 yang_print_text(f, level, "reference", restr->ref);
Radek Krejci41726f92015-06-19 13:11:05 +0200201 }
Radek Krejci0bd5db42015-06-19 13:30:07 +0200202 if (restr->eapptag != NULL) {
203 fprintf(f, "%*serror-app-tag \"%s\";\n", LEVEL, INDENT, restr->eapptag);
Radek Krejci41726f92015-06-19 13:11:05 +0200204 }
Radek Krejci0bd5db42015-06-19 13:30:07 +0200205 if (restr->emsg != NULL) {
206 yang_print_text(f, level, "error-message", restr->emsg);
Radek Krejci41726f92015-06-19 13:11:05 +0200207 }
208}
209
210static void
Radek Krejci76512572015-08-04 09:47:08 +0200211yang_print_when(FILE *f, int level, struct lys_when *when)
Michal Vasko1f0428a2015-07-07 14:55:04 +0200212{
213 fprintf(f, "%*swhen \"%s\" {\n", LEVEL, INDENT, when->cond);
214 level++;
215 if (when->dsc) {
216 yang_print_text(f, level, "description", when->dsc);
217 }
218 if (when->ref) {
219 yang_print_text(f, level, "reference", when->ref);
220 }
221 level--;
222 fprintf(f, "%*s}\n", LEVEL, INDENT);
223}
224
225static void
Radek Krejcib8048692015-08-05 13:36:34 +0200226yang_print_type(FILE *f, int level, struct lys_module *module, struct lys_type *type)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200227{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200228 int i;
Radek Krejci25d782a2015-05-22 15:03:23 +0200229
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200230 if (type->prefix) {
231 fprintf(f, "%*stype %s:%s {\n", LEVEL, INDENT, type->prefix, type->der->name);
232 } else {
233 fprintf(f, "%*stype %s {\n", LEVEL, INDENT, type->der->name);
234 }
235 level++;
236 switch (type->base) {
237 case LY_TYPE_BINARY:
238 if (type->info.binary.length != NULL) {
Radek Krejci41726f92015-06-19 13:11:05 +0200239 fprintf(f, "%*slength \"%s\" {\n", LEVEL, INDENT, type->info.binary.length->expr);
Radek Krejci0bd5db42015-06-19 13:30:07 +0200240 yang_print_restr(f, level + 1, type->info.binary.length);
Radek Krejci41726f92015-06-19 13:11:05 +0200241 fprintf(f, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200242 }
243 break;
244 case LY_TYPE_BITS:
245 for (i = 0; i < type->info.bits.count; ++i) {
Radek Krejci994b6f62015-06-18 16:47:27 +0200246 fprintf(f, "%*sbit %s {\n", LEVEL, INDENT, type->info.bits.bit[i].name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200247 level++;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200248 yang_print_snode_common(f, level, (struct lys_node *)&type->info.bits.bit[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200249 fprintf(f, "%*sposition %u;\n", LEVEL, INDENT, type->info.bits.bit[i].pos);
250 level--;
251 fprintf(f, "%*s}\n", LEVEL, INDENT);
252 }
253 break;
254 case LY_TYPE_DEC64:
255 fprintf(f, "%*sfraction-digits %d;\n", LEVEL, INDENT, type->info.dec64.dig);
Michal Vaskoea505ee2015-07-07 14:01:00 +0200256 if (type->info.dec64.range != NULL) {
257 fprintf(f, "%*srange \"%s\" {\n", LEVEL, INDENT, type->info.dec64.range->expr);
258 yang_print_restr(f, level + 1, type->info.dec64.range);
259 fprintf(f, "%*s}\n", LEVEL, INDENT);
260 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200261 break;
262 case LY_TYPE_ENUM:
263 for (i = 0; i < type->info.enums.count; i++) {
Radek Krejci1574a8d2015-08-03 14:16:52 +0200264 fprintf(f, "%*senum %s {\n", LEVEL, INDENT, type->info.enums.enm[i].name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200265 level++;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200266 yang_print_snode_common(f, level, (struct lys_node *)&type->info.enums.enm[i]);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200267 fprintf(f, "%*svalue %d;\n", LEVEL, INDENT, type->info.enums.enm[i].value);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200268 level--;
269 fprintf(f, "%*s}\n", LEVEL, INDENT);
270 }
271 break;
272 case LY_TYPE_IDENT:
273 if (module == type->info.ident.ref->module) {
274 fprintf(f, "%*sbase %s;\n", LEVEL, INDENT, type->info.ident.ref->name);
275 } else {
276 fprintf(f, "%*sbase %s:%s;\n", LEVEL, INDENT, type->info.ident.ref->module->prefix,
277 type->info.ident.ref->name);
278 }
279 break;
280 case LY_TYPE_INST:
Radek Krejciaf351422015-06-19 14:49:38 +0200281 if (type->info.inst.req == 1) {
282 fprintf(f, "%*srequire-instance \"true\";\n", LEVEL, INDENT);
283 } else if (type->info.inst.req == -1) {
284 fprintf(f, "%*srequire-instance \"false\";\n", LEVEL, INDENT);
285 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200286 break;
287 case LY_TYPE_INT8:
288 case LY_TYPE_INT16:
289 case LY_TYPE_INT32:
290 case LY_TYPE_INT64:
291 case LY_TYPE_UINT8:
292 case LY_TYPE_UINT16:
293 case LY_TYPE_UINT32:
294 case LY_TYPE_UINT64:
Radek Krejcif2860132015-06-20 12:37:20 +0200295 if (type->info.num.range != NULL) {
296 fprintf(f, "%*srange \"%s\" {\n", LEVEL, INDENT, type->info.num.range->expr);
297 yang_print_restr(f, level + 1, type->info.num.range);
298 fprintf(f, "%*s}\n", LEVEL, INDENT);
299 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200300 break;
301 case LY_TYPE_LEAFREF:
302 fprintf(f, "%*spath \"%s\";\n", LEVEL, INDENT, type->info.lref.path);
303 break;
304 case LY_TYPE_STRING:
Radek Krejci5fbc9162015-06-19 14:11:11 +0200305 if (type->info.str.length) {
Radek Krejci061bd522015-06-19 13:45:16 +0200306 fprintf(f, "%*slength \"%s\" {\n", LEVEL, INDENT, type->info.str.length->expr);
307 yang_print_restr(f, level + 1, type->info.str.length);
308 fprintf(f, "%*s}\n", LEVEL, INDENT);
309 }
Radek Krejci5fbc9162015-06-19 14:11:11 +0200310 for (i = 0; i < type->info.str.pat_count; i++) {
311 fprintf(f, "%*spattern \"%s\" {\n", LEVEL, INDENT, type->info.str.patterns[i].expr);
312 yang_print_restr(f, level + 1, &type->info.str.patterns[i]);
313 fprintf(f, "%*s}\n", LEVEL, INDENT);
314 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200315 break;
316 case LY_TYPE_UNION:
317 for (i = 0; i < type->info.uni.count; ++i) {
Radek Krejci1574a8d2015-08-03 14:16:52 +0200318 yang_print_type(f, level, module, &type->info.uni.types[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200319 }
320 break;
321 default:
322 /* other types do not have substatements */
323 break;
324 }
325 level--;
326 fprintf(f, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200327}
328
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200329static void
Radek Krejci1574a8d2015-08-03 14:16:52 +0200330yang_print_must(FILE *f, int level, struct lys_restr *must)
Michal Vasko7f976ee2015-06-09 13:55:41 +0200331{
Radek Krejci0bd5db42015-06-19 13:30:07 +0200332 fprintf(f, "%*smust \"%s\" {\n", LEVEL, INDENT, must->expr);
Radek Krejci41726f92015-06-19 13:11:05 +0200333 yang_print_restr(f, level + 1, must);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200334 fprintf(f, "%*s}\n", LEVEL, INDENT);
Michal Vasko7f976ee2015-06-09 13:55:41 +0200335}
336
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200337static void
Radek Krejci76512572015-08-04 09:47:08 +0200338yang_print_unique(FILE *f, int level, struct lys_unique *uniq)
Michal Vasko1ef07972015-07-07 14:01:35 +0200339{
340 int i;
341
342 fprintf(f, "%*sunique \"", LEVEL, INDENT);
343 for (i = 0; i < uniq->leafs_size; i++) {
344 fprintf(f, "%s%s", uniq->leafs[i]->name, i + 1 < uniq->leafs_size ? " " : "");
345 }
346 fprintf(f, "\";\n");
347}
348
349static void
Radek Krejci76512572015-08-04 09:47:08 +0200350yang_print_refine(FILE *f, int level, struct lys_refine *refine)
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200351{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200352 int i;
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200353
Radek Krejci76512572015-08-04 09:47:08 +0200354 fprintf(f, "%*srefine \"%s\" {\n", LEVEL, INDENT, refine->target_name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200355 level++;
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200356
Radek Krejci1574a8d2015-08-03 14:16:52 +0200357 if (refine->flags & LYS_CONFIG_W) {
Radek Krejci6a113852015-07-03 16:04:20 +0200358 fprintf(f, "%*sconfig \"true\";\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200359 } else if (refine->flags & LYS_CONFIG_R) {
Radek Krejci6a113852015-07-03 16:04:20 +0200360 fprintf(f, "%*sconfig \"false\";\n", LEVEL, INDENT);
361 }
362
Radek Krejci1574a8d2015-08-03 14:16:52 +0200363 if (refine->flags & LYS_MAND_TRUE) {
Radek Krejci6a113852015-07-03 16:04:20 +0200364 fprintf(f, "%*smandatory \"true\";\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200365 } else if (refine->flags & LYS_MAND_FALSE) {
Radek Krejci6a113852015-07-03 16:04:20 +0200366 fprintf(f, "%*smandatory \"false\";\n", LEVEL, INDENT);
367 }
368
Radek Krejci1d82ef62015-08-07 14:44:40 +0200369 yang_print_snode_common(f, level, (struct lys_node *)refine);
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200370
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200371 for (i = 0; i < refine->must_size; ++i) {
372 yang_print_must(f, level, &refine->must[i]);
373 }
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200374
Radek Krejci76512572015-08-04 09:47:08 +0200375 if (refine->target_type & (LYS_LEAF | LYS_CHOICE)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200376 if (refine->mod.dflt != NULL) {
377 fprintf(f, "%*sdefault \"%s\";\n", LEVEL, INDENT, refine->mod.dflt);
378 }
Radek Krejci76512572015-08-04 09:47:08 +0200379 } else if (refine->target_type == LYS_CONTAINER) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200380 if (refine->mod.presence != NULL) {
381 yang_print_text(f, level, "presence", refine->mod.presence);
382 }
Radek Krejci76512572015-08-04 09:47:08 +0200383 } else if (refine->target_type & (LYS_LIST | LYS_LEAFLIST)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200384 if (refine->mod.list.min > 0) {
385 fprintf(f, "%*smin-elements %u;\n", LEVEL, INDENT, refine->mod.list.min);
386 }
387 if (refine->mod.list.max > 0) {
388 fprintf(f, "%*smax-elements %u;\n", LEVEL, INDENT, refine->mod.list.max);
389 }
390 }
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200391
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200392 level--;
393 fprintf(f, "%*s}\n", LEVEL, INDENT);
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200394}
395
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200396static void
Radek Krejcia52656e2015-08-05 13:41:50 +0200397yang_print_deviation(FILE *f, int level, struct lys_module *module, struct lys_deviation *deviation)
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200398{
399 int i, j;
400
401 fprintf(f, "%*sdeviation \"%s\" {\n", LEVEL, INDENT, deviation->target_name);
402 level++;
403
404 if (deviation->dsc) {
405 yang_print_text(f, level, "description", deviation->dsc);
406 }
407 if (deviation->ref) {
408 yang_print_text(f, level, "reference", deviation->ref);
409 }
410
411 for (i = 0; i < deviation->deviate_size; ++i) {
412 fprintf(f, "%*sdeviate ", LEVEL, INDENT);
413 if (deviation->deviate[i].mod == LY_DEVIATE_NO) {
414 fprintf(f, "not-supported {\n");
415 } else if (deviation->deviate[i].mod == LY_DEVIATE_ADD) {
416 fprintf(f, "add {\n");
417 } else if (deviation->deviate[i].mod == LY_DEVIATE_RPL) {
418 fprintf(f, "replace {\n");
419 } else if (deviation->deviate[i].mod == LY_DEVIATE_DEL) {
420 fprintf(f, "delete {\n");
421 }
422 level++;
423
Radek Krejci1574a8d2015-08-03 14:16:52 +0200424 if (deviation->deviate[i].flags & LYS_CONFIG_W) {
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200425 fprintf(f, "%*sconfig \"true\";\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200426 } else if (deviation->deviate[i].flags & LYS_CONFIG_R) {
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200427 fprintf(f, "%*sconfig \"false\";\n", LEVEL, INDENT);
428 }
429
Radek Krejci1574a8d2015-08-03 14:16:52 +0200430 if (deviation->deviate[i].flags & LYS_MAND_TRUE) {
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200431 fprintf(f, "%*smandatory \"true\";\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200432 } else if (deviation->deviate[i].flags & LYS_MAND_FALSE) {
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200433 fprintf(f, "%*smandatory \"false\";\n", LEVEL, INDENT);
434 }
435
436 if (deviation->deviate[i].dflt) {
437 fprintf(f, "%*sdefault %s;\n", LEVEL, INDENT, deviation->deviate[i].dflt);
438 }
439
440 if (deviation->deviate[i].min) {
441 fprintf(f, "%*smin-elements %u;\n", LEVEL, INDENT, deviation->deviate[i].min);
442 }
443 if (deviation->deviate[i].max) {
444 fprintf(f, "%*smax-elements %u;\n", LEVEL, INDENT, deviation->deviate[i].max);
445 }
446
447 for (j = 0; j < deviation->deviate[i].must_size; ++j) {
448 yang_print_must(f, level, &deviation->deviate[i].must[j]);
449 }
450
451 for (j = 0; j < deviation->deviate[i].unique_size; ++j) {
452 yang_print_unique(f, level, &deviation->deviate[i].unique[j]);
453 }
454
455 if (deviation->deviate[i].type) {
456 yang_print_type(f, level, module, deviation->deviate[i].type);
457 }
458
459 if (deviation->deviate[i].units) {
460 fprintf(f, "%*sunits %s;\n", LEVEL, INDENT, deviation->deviate[i].units);
461 }
462
463 level--;
464 fprintf(f, "%*s}\n", LEVEL, INDENT);
465 }
466
467 level--;
468 fprintf(f, "%*s}\n", LEVEL, INDENT);
469}
470
471static void
Radek Krejcib8048692015-08-05 13:36:34 +0200472yang_print_augment(FILE *f, int level, struct lys_module *module, struct lys_node_augment *augment)
Michal Vasko6f25f212015-07-07 15:42:07 +0200473{
474 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200475 struct lys_node *sub;
Michal Vasko6f25f212015-07-07 15:42:07 +0200476
477 fprintf(f, "%*saugment \"%s\" {\n", LEVEL, INDENT, augment->target_name);
478 level++;
479
Radek Krejci76512572015-08-04 09:47:08 +0200480 yang_print_nacmext(f, level, (struct lys_node *)augment, module);
Radek Krejci1d82ef62015-08-07 14:44:40 +0200481 yang_print_snode_common(f, level, (struct lys_node *)augment);
Michal Vasko6f25f212015-07-07 15:42:07 +0200482
483 for (i = 0; i < augment->features_size; i++) {
484 yang_print_iffeature(f, level, module, augment->features[i]);
485 }
486
487 if (augment->when) {
488 yang_print_when(f, level, augment->when);
489 }
490
491 LY_TREE_FOR(augment->child, sub) {
Radek Krejci1d82ef62015-08-07 14:44:40 +0200492 yang_print_snode(f, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200493 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
494 LYS_USES | LYS_ANYXML | LYS_CASE);
Michal Vasko6f25f212015-07-07 15:42:07 +0200495 }
496
497 level--;
498 fprintf(f, "%*s}\n", LEVEL, INDENT);
499}
500
501static void
Radek Krejcib8048692015-08-05 13:36:34 +0200502yang_print_typedef(FILE *f, int level, struct lys_module *module, struct lys_tpdf *tpdf)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200503{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200504 fprintf(f, "%*stypedef %s {\n", LEVEL, INDENT, tpdf->name);
505 level++;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200506
Radek Krejci1d82ef62015-08-07 14:44:40 +0200507 yang_print_snode_common(f, level, (struct lys_node *)tpdf);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200508 yang_print_type(f, level, module, &tpdf->type);
509 if (tpdf->units != NULL) {
510 fprintf(f, "%*sunits \"%s\";\n", LEVEL, INDENT, tpdf->units);
511 }
512 if (tpdf->dflt != NULL) {
513 fprintf(f, "%*sdefault \"%s\";\n", LEVEL, INDENT, tpdf->dflt);
514 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200515
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200516 level--;
517 fprintf(f, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200518}
519
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200520static void
Radek Krejcia52656e2015-08-05 13:41:50 +0200521yang_print_identity(FILE *f, int level, struct lys_ident *ident)
Radek Krejci6793db02015-05-22 17:49:54 +0200522{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200523 fprintf(f, "%*sidentity %s {\n", LEVEL, INDENT, ident->name);
524 level++;
Radek Krejci6793db02015-05-22 17:49:54 +0200525
Radek Krejci1d82ef62015-08-07 14:44:40 +0200526 yang_print_snode_common(f, level, (struct lys_node *)ident);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200527 if (ident->base) {
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200528 fprintf(f, "%*sbase ", LEVEL, INDENT);
529 if ((ident->module != ident->base->module) && !ident->base->module->type) {
530 fprintf(f, "%s:", get_module_import_prefix(ident->module, ident->base->module));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200531 }
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200532 fprintf(f, "%s;\n", ident->base->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200533 }
Radek Krejci6793db02015-05-22 17:49:54 +0200534
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200535 level--;
536 fprintf(f, "%*s}\n", LEVEL, INDENT);
Radek Krejci6793db02015-05-22 17:49:54 +0200537
538}
539
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200540static void
Radek Krejci1d82ef62015-08-07 14:44:40 +0200541yang_print_container(FILE *f, int level, struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200542{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200543 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200544 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200545 struct lys_node_container *cont = (struct lys_node_container *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200546
Radek Krejci1d82ef62015-08-07 14:44:40 +0200547 fprintf(f, "%*scontainer %s {\n", LEVEL, INDENT, node->name);
Michal Vasko7f976ee2015-06-09 13:55:41 +0200548
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200549 level++;
Radek Krejci6a113852015-07-03 16:04:20 +0200550
Radek Krejci1d82ef62015-08-07 14:44:40 +0200551 yang_print_nacmext(f, level, node, node->module);
Radek Krejci6a113852015-07-03 16:04:20 +0200552
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200553 if (cont->presence != NULL) {
554 yang_print_text(f, level, "presence", cont->presence);
555 }
Michal Vasko7f976ee2015-06-09 13:55:41 +0200556
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200557 for (i = 0; i < cont->must_size; i++) {
558 yang_print_must(f, level, &cont->must[i]);
559 }
Michal Vasko7f976ee2015-06-09 13:55:41 +0200560
Radek Krejci1d82ef62015-08-07 14:44:40 +0200561 yang_print_snode_common2(f, level, node);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200562
Michal Vasko4773b762015-07-07 12:15:10 +0200563 for (i = 0; i < cont->features_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +0200564 yang_print_iffeature(f, level, node->module, cont->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200565 }
566
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200567 for (i = 0; i < cont->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +0200568 yang_print_typedef(f, level, node->module, &cont->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200569 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200570
Michal Vasko1f0428a2015-07-07 14:55:04 +0200571 if (cont->when) {
572 yang_print_when(f, level, cont->when);
573 }
574
Radek Krejci1d82ef62015-08-07 14:44:40 +0200575 LY_TREE_FOR(node->child, sub) {
Michal Vasko15b48702015-07-07 15:49:34 +0200576 /* augment */
Radek Krejci1d82ef62015-08-07 14:44:40 +0200577 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200578 continue;
579 }
Radek Krejci1d82ef62015-08-07 14:44:40 +0200580 yang_print_snode(f, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200581 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
582 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200583 }
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200584
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200585 level--;
586 fprintf(f, "%*s}\n", LEVEL, INDENT);
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200587}
588
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200589static void
Radek Krejci1d82ef62015-08-07 14:44:40 +0200590yang_print_case(FILE *f, int level, struct lys_node *node)
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200591{
Michal Vasko4773b762015-07-07 12:15:10 +0200592 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200593 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200594 struct lys_node_case *cas = (struct lys_node_case *)node;
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200595
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200596 fprintf(f, "%*scase %s {\n", LEVEL, INDENT, cas->name);
597 level++;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200598 yang_print_nacmext(f, level, node, node->module);
599 yang_print_snode_common2(f, level, node);
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200600
Michal Vasko4773b762015-07-07 12:15:10 +0200601 for (i = 0; i < cas->features_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +0200602 yang_print_iffeature(f, level, node->module, cas->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200603 }
604
Michal Vasko1f0428a2015-07-07 14:55:04 +0200605 if (cas->when) {
606 yang_print_when(f, level, cas->when);
607 }
608
Radek Krejci1d82ef62015-08-07 14:44:40 +0200609 LY_TREE_FOR(node->child, sub) {
Michal Vasko15b48702015-07-07 15:49:34 +0200610 /* augment */
Radek Krejci1d82ef62015-08-07 14:44:40 +0200611 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200612 continue;
613 }
Radek Krejci1d82ef62015-08-07 14:44:40 +0200614 yang_print_snode(f, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200615 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
616 LYS_USES | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200617 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200618
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200619 level--;
620 fprintf(f, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200621}
622
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200623static void
Radek Krejci1d82ef62015-08-07 14:44:40 +0200624yang_print_choice(FILE *f, int level, struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200625{
Michal Vasko4773b762015-07-07 12:15:10 +0200626 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200627 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200628 struct lys_node_choice *choice = (struct lys_node_choice *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200629
Radek Krejci1d82ef62015-08-07 14:44:40 +0200630 fprintf(f, "%*schoice %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +0200631
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200632 level++;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200633 yang_print_nacmext(f, level, node, node->module);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200634 if (choice->dflt != NULL) {
635 fprintf(f, "%*sdefault \"%s\";\n", LEVEL, INDENT, choice->dflt->name);
636 }
Michal Vasko16083662015-06-09 14:00:45 +0200637
Radek Krejci1d82ef62015-08-07 14:44:40 +0200638 yang_print_snode_common2(f, level, node);
Michal Vasko4773b762015-07-07 12:15:10 +0200639
640 for (i = 0; i < choice->features_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +0200641 yang_print_iffeature(f, level, node->module, choice->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200642 }
643
Michal Vasko1f0428a2015-07-07 14:55:04 +0200644 if (choice->when) {
645 yang_print_when(f, level, choice->when);
646 }
647
Radek Krejci1d82ef62015-08-07 14:44:40 +0200648 LY_TREE_FOR(node->child, sub) {
Michal Vasko15b48702015-07-07 15:49:34 +0200649 /* augment */
Radek Krejci1d82ef62015-08-07 14:44:40 +0200650 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200651 continue;
652 }
Radek Krejci1d82ef62015-08-07 14:44:40 +0200653 yang_print_snode(f, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200654 LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYXML | LYS_CASE);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200655 }
656 level--;
657 fprintf(f, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200658}
659
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200660static void
Radek Krejci1d82ef62015-08-07 14:44:40 +0200661yang_print_leaf(FILE *f, int level, struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200662{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200663 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200664 struct lys_node_leaf *leaf = (struct lys_node_leaf *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200665
Radek Krejci1d82ef62015-08-07 14:44:40 +0200666 fprintf(f, "%*sleaf %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +0200667
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200668 level++;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200669 yang_print_nacmext(f, level, node, node->module);
670 yang_print_snode_common2(f, level, node);
Michal Vasko4773b762015-07-07 12:15:10 +0200671 for (i = 0; i < leaf->features_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +0200672 yang_print_iffeature(f, level, node->module, leaf->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200673 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200674 for (i = 0; i < leaf->must_size; i++) {
675 yang_print_must(f, level, &leaf->must[i]);
676 }
Michal Vasko1f0428a2015-07-07 14:55:04 +0200677 if (leaf->when) {
678 yang_print_when(f, level, leaf->when);
679 }
Radek Krejci1d82ef62015-08-07 14:44:40 +0200680 yang_print_type(f, level, node->module, &leaf->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200681 if (leaf->units != NULL) {
682 fprintf(f, "%*sunits \"%s\";\n", LEVEL, INDENT, leaf->units);
683 }
684 if (leaf->dflt != NULL) {
685 fprintf(f, "%*sdefault \"%s\";\n", LEVEL, INDENT, leaf->dflt);
686 }
687 level--;
Michal Vasko16083662015-06-09 14:00:45 +0200688
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200689 fprintf(f, "%*s}\n", LEVEL, INDENT);
Michal Vasko16083662015-06-09 14:00:45 +0200690}
691
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200692static void
Radek Krejci1d82ef62015-08-07 14:44:40 +0200693yang_print_anyxml(FILE *f, int level, struct lys_node *node)
Michal Vasko16083662015-06-09 14:00:45 +0200694{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200695 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200696 struct lys_node_anyxml *anyxml = (struct lys_node_anyxml *)node;
Michal Vasko16083662015-06-09 14:00:45 +0200697
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200698 fprintf(f, "%*sanyxml %s {\n", LEVEL, INDENT, anyxml->name);
699 level++;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200700 yang_print_nacmext(f, level, node, node->module);
701 yang_print_snode_common2(f, level, node);
Michal Vasko4773b762015-07-07 12:15:10 +0200702 for (i = 0; i < anyxml->features_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +0200703 yang_print_iffeature(f, level, node->module, anyxml->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200704 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200705 for (i = 0; i < anyxml->must_size; i++) {
706 yang_print_must(f, level, &anyxml->must[i]);
707 }
Michal Vasko1f0428a2015-07-07 14:55:04 +0200708 if (anyxml->when) {
709 yang_print_when(f, level, anyxml->when);
710 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200711 level--;
712 fprintf(f, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200713}
714
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200715static void
Radek Krejci1d82ef62015-08-07 14:44:40 +0200716yang_print_leaflist(FILE *f, int level, struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200717{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200718 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200719 struct lys_node_leaflist *llist = (struct lys_node_leaflist *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200720
Radek Krejci1d82ef62015-08-07 14:44:40 +0200721 fprintf(f, "%*sleaf-list %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +0200722
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200723 level++;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200724 yang_print_nacmext(f, level, node, node->module);
725 yang_print_snode_common2(f, level, node);
Michal Vasko4773b762015-07-07 12:15:10 +0200726 for (i = 0; i < llist->features_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +0200727 yang_print_iffeature(f, level, node->module, llist->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200728 }
Radek Krejci1574a8d2015-08-03 14:16:52 +0200729 if (llist->flags & LYS_USERORDERED) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200730 fprintf(f, "%*sordered-by user;\n", LEVEL, INDENT);
731 }
732 if (llist->min > 0) {
733 fprintf(f, "%*smin-elements %u;\n", LEVEL, INDENT, llist->min);
734 }
735 if (llist->max > 0) {
736 fprintf(f, "%*smax-elements %u;\n", LEVEL, INDENT, llist->max);
737 }
738 for (i = 0; i < llist->must_size; i++) {
739 yang_print_must(f, level, &llist->must[i]);
740 }
Michal Vasko1f0428a2015-07-07 14:55:04 +0200741 if (llist->when) {
742 yang_print_when(f, level, llist->when);
743 }
Radek Krejci1d82ef62015-08-07 14:44:40 +0200744 yang_print_type(f, level, node->module, &llist->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200745 if (llist->units != NULL) {
746 fprintf(f, "%*sunits \"%s\";\n", LEVEL, INDENT, llist->units);
747 }
748 level--;
Michal Vasko16083662015-06-09 14:00:45 +0200749
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200750 fprintf(f, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200751}
752
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200753static void
Radek Krejci1d82ef62015-08-07 14:44:40 +0200754yang_print_list(FILE *f, int level, struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200755{
Michal Vasko1ef07972015-07-07 14:01:35 +0200756 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200757 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200758 struct lys_node_list *list = (struct lys_node_list *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200759
Radek Krejci1d82ef62015-08-07 14:44:40 +0200760 fprintf(f, "%*slist %s {\n", LEVEL, INDENT, node->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200761 level++;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200762 yang_print_nacmext(f, level, node, node->module);
763 yang_print_snode_common2(f, level, node);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200764
Michal Vasko4773b762015-07-07 12:15:10 +0200765 for (i = 0; i < list->features_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +0200766 yang_print_iffeature(f, level, node->module, list->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200767 }
768
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200769 if (list->keys_size) {
770 fprintf(f, "%*skey \"", LEVEL, INDENT);
771 for (i = 0; i < list->keys_size; i++) {
772 fprintf(f, "%s%s", list->keys[i]->name, i + 1 < list->keys_size ? " " : "");
773 }
774 fprintf(f, "\";\n");
775 }
Radek Krejcid7f0d012015-05-25 15:04:52 +0200776
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200777 for (i = 0; i < list->unique_size; i++) {
Michal Vasko1ef07972015-07-07 14:01:35 +0200778 yang_print_unique(f, level, &list->unique[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200779 }
Michal Vaskodff818b2015-06-09 14:01:12 +0200780
Radek Krejci1574a8d2015-08-03 14:16:52 +0200781 if (list->flags & LYS_USERORDERED) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200782 fprintf(f, "%*sordered-by user;\n", LEVEL, INDENT);
783 }
784 if (list->min > 0) {
785 fprintf(f, "%*smin-elements %u;\n", LEVEL, INDENT, list->min);
786 }
787 if (list->max > 0) {
788 fprintf(f, "%*smax-elements %u;\n", LEVEL, INDENT, list->max);
789 }
790 for (i = 0; i < list->must_size; i++) {
791 yang_print_must(f, level, &list->must[i]);
792 }
Michal Vasko1f0428a2015-07-07 14:55:04 +0200793 if (list->when) {
794 yang_print_when(f, level, list->when);
795 }
Michal Vaskodff818b2015-06-09 14:01:12 +0200796
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200797 for (i = 0; i < list->tpdf_size; i++) {
798 yang_print_typedef(f, level, list->module, &list->tpdf[i]);
799 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200800
Radek Krejci1d82ef62015-08-07 14:44:40 +0200801 LY_TREE_FOR(node->child, sub) {
Michal Vasko15b48702015-07-07 15:49:34 +0200802 /* augment */
Radek Krejci1d82ef62015-08-07 14:44:40 +0200803 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200804 continue;
805 }
Radek Krejci1d82ef62015-08-07 14:44:40 +0200806 yang_print_snode(f, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200807 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
808 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200809 }
810 level--;
811 fprintf(f, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200812}
813
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200814static void
Radek Krejci1d82ef62015-08-07 14:44:40 +0200815yang_print_grouping(FILE *f, int level, struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200816{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200817 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200818 struct lys_node *child;
819 struct lys_node_grp *grp = (struct lys_node_grp *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200820
Radek Krejci1d82ef62015-08-07 14:44:40 +0200821 fprintf(f, "%*sgrouping %s {\n", LEVEL, INDENT, node->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200822 level++;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200823
Radek Krejci1d82ef62015-08-07 14:44:40 +0200824 yang_print_snode_common(f, level, node);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200825
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200826 for (i = 0; i < grp->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +0200827 yang_print_typedef(f, level, node->module, &grp->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200828 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200829
Radek Krejci1d82ef62015-08-07 14:44:40 +0200830 LY_TREE_FOR(node->child, child) {
831 yang_print_snode(f, level, child,
Radek Krejci76512572015-08-04 09:47:08 +0200832 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
833 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200834 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200835
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200836 level--;
837 fprintf(f, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200838}
839
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200840static void
Radek Krejci1d82ef62015-08-07 14:44:40 +0200841yang_print_uses(FILE *f, int level, struct lys_node *node)
Radek Krejcic7c9a6c2015-05-25 16:35:06 +0200842{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200843 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200844 struct lys_node_uses *uses = (struct lys_node_uses *)node;
Radek Krejcic7c9a6c2015-05-25 16:35:06 +0200845
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200846 fprintf(f, "%*suses ", LEVEL, INDENT);
Radek Krejci1d82ef62015-08-07 14:44:40 +0200847 if (node->child && (node->module != node->child->module) && !node->child->module->type) {
848 fprintf(f, "%s:", get_module_import_prefix(node->module, node->child->module));
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200849 }
850 fprintf(f, "%s {\n",uses->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200851 level++;
Radek Krejcic7c9a6c2015-05-25 16:35:06 +0200852
Radek Krejci1d82ef62015-08-07 14:44:40 +0200853 yang_print_nacmext(f, level, node, node->module);
854 yang_print_snode_common(f, level, node);
Michal Vasko4773b762015-07-07 12:15:10 +0200855 for (i = 0; i < uses->features_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +0200856 yang_print_iffeature(f, level, node->module, uses->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200857 }
Michal Vasko1f0428a2015-07-07 14:55:04 +0200858 if (uses->when) {
859 yang_print_when(f, level, uses->when);
860 }
Radek Krejcic7c9a6c2015-05-25 16:35:06 +0200861
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200862 for (i = 0; i < uses->refine_size; i++) {
863 yang_print_refine(f, level, &uses->refine[i]);
864 }
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200865
Michal Vasko6f25f212015-07-07 15:42:07 +0200866 for (i = 0; i < uses->augment_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +0200867 yang_print_augment(f, level, node->module, &uses->augment[i]);
Michal Vasko6f25f212015-07-07 15:42:07 +0200868 }
869
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200870 level--;
871 fprintf(f, "%*s}\n", LEVEL, INDENT);
Radek Krejcic7c9a6c2015-05-25 16:35:06 +0200872}
873
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200874static void
Radek Krejci1d82ef62015-08-07 14:44:40 +0200875yang_print_input_output(FILE *f, int level, struct lys_node *node)
Michal Vasko5bbae102015-06-16 12:16:44 +0200876{
Michal Vaskof4d3d742015-06-16 11:51:09 +0200877 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200878 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200879 struct lys_node_rpc_inout *inout = (struct lys_node_rpc_inout *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +0200880
Radek Krejci76512572015-08-04 09:47:08 +0200881 fprintf(f, "%*s%s {\n", LEVEL, INDENT, (inout->nodetype == LYS_INPUT ? "input" : "output"));
Michal Vaskof4d3d742015-06-16 11:51:09 +0200882
883 level++;
884 for (i = 0; i < inout->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +0200885 yang_print_typedef(f, level, node->module, &inout->tpdf[i]);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200886 }
887
Radek Krejci1d82ef62015-08-07 14:44:40 +0200888 LY_TREE_FOR(node->child, sub) {
Michal Vasko15b48702015-07-07 15:49:34 +0200889 /* augment */
Radek Krejci1d82ef62015-08-07 14:44:40 +0200890 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200891 continue;
892 }
Radek Krejci1d82ef62015-08-07 14:44:40 +0200893 yang_print_snode(f, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200894 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
895 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200896 }
897
898 level--;
899 fprintf(f, "%*s}\n", LEVEL, INDENT);
900}
901
902static void
Radek Krejci1d82ef62015-08-07 14:44:40 +0200903yang_print_rpc(FILE *f, int level, struct lys_node *node)
Michal Vaskof4d3d742015-06-16 11:51:09 +0200904{
905 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200906 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200907 struct lys_node_rpc *rpc = (struct lys_node_rpc *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +0200908
Radek Krejci1d82ef62015-08-07 14:44:40 +0200909 fprintf(f, "%*srpc %s {\n", LEVEL, INDENT, node->name);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200910
911 level++;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200912 yang_print_snode_common(f, level, node);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200913
Michal Vasko4773b762015-07-07 12:15:10 +0200914 for (i = 0; i < rpc->features_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +0200915 yang_print_iffeature(f, level, node->module, rpc->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200916 }
917
Michal Vaskof4d3d742015-06-16 11:51:09 +0200918 for (i = 0; i < rpc->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +0200919 yang_print_typedef(f, level, node->module, &rpc->tpdf[i]);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200920 }
921
Radek Krejci1d82ef62015-08-07 14:44:40 +0200922 LY_TREE_FOR(node->child, sub) {
923 yang_print_snode(f, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200924 LYS_GROUPING | LYS_INPUT | LYS_OUTPUT);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200925 }
926
927 level--;
928 fprintf(f, "%*s}\n", LEVEL, INDENT);
929}
930
931static void
Radek Krejci1d82ef62015-08-07 14:44:40 +0200932yang_print_notif(FILE *f, int level, struct lys_node *node)
Michal Vasko7690bc12015-06-16 12:26:05 +0200933{
Michal Vaskof4d3d742015-06-16 11:51:09 +0200934 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200935 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200936 struct lys_node_notif *notif = (struct lys_node_notif *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +0200937
Radek Krejci1d82ef62015-08-07 14:44:40 +0200938 fprintf(f, "%*snotification %s {\n", LEVEL, INDENT, node->name);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200939
940 level++;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200941 yang_print_snode_common(f, level, node);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200942
Michal Vasko4773b762015-07-07 12:15:10 +0200943 for (i = 0; i < notif->features_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +0200944 yang_print_iffeature(f, level, node->module, notif->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200945 }
946
Michal Vaskof4d3d742015-06-16 11:51:09 +0200947 for (i = 0; i < notif->tpdf_size; i++) {
Radek Krejci1d82ef62015-08-07 14:44:40 +0200948 yang_print_typedef(f, level, node->module, &notif->tpdf[i]);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200949 }
950
Radek Krejci1d82ef62015-08-07 14:44:40 +0200951 LY_TREE_FOR(node->child, sub) {
Michal Vasko15b48702015-07-07 15:49:34 +0200952 /* augment */
Radek Krejci1d82ef62015-08-07 14:44:40 +0200953 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200954 continue;
955 }
Radek Krejci1d82ef62015-08-07 14:44:40 +0200956 yang_print_snode(f, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200957 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
958 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200959 }
960
961 level--;
962 fprintf(f, "%*s}\n", LEVEL, INDENT);
963}
964
965static void
Radek Krejci1d82ef62015-08-07 14:44:40 +0200966yang_print_snode(FILE *f, int level, struct lys_node *node, int mask)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200967{
Radek Krejci1d82ef62015-08-07 14:44:40 +0200968 switch (node->nodetype & mask) {
Radek Krejci76512572015-08-04 09:47:08 +0200969 case LYS_CONTAINER:
Radek Krejci1d82ef62015-08-07 14:44:40 +0200970 yang_print_container(f, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200971 break;
Radek Krejci76512572015-08-04 09:47:08 +0200972 case LYS_CHOICE:
Radek Krejci1d82ef62015-08-07 14:44:40 +0200973 yang_print_choice(f, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200974 break;
Radek Krejci76512572015-08-04 09:47:08 +0200975 case LYS_LEAF:
Radek Krejci1d82ef62015-08-07 14:44:40 +0200976 yang_print_leaf(f, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200977 break;
Radek Krejci76512572015-08-04 09:47:08 +0200978 case LYS_LEAFLIST:
Radek Krejci1d82ef62015-08-07 14:44:40 +0200979 yang_print_leaflist(f, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200980 break;
Radek Krejci76512572015-08-04 09:47:08 +0200981 case LYS_LIST:
Radek Krejci1d82ef62015-08-07 14:44:40 +0200982 yang_print_list(f, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200983 break;
Radek Krejci76512572015-08-04 09:47:08 +0200984 case LYS_USES:
Radek Krejci1d82ef62015-08-07 14:44:40 +0200985 yang_print_uses(f, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200986 break;
Radek Krejci76512572015-08-04 09:47:08 +0200987 case LYS_GROUPING:
Radek Krejci1d82ef62015-08-07 14:44:40 +0200988 yang_print_grouping(f, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200989 break;
Radek Krejci76512572015-08-04 09:47:08 +0200990 case LYS_ANYXML:
Radek Krejci1d82ef62015-08-07 14:44:40 +0200991 yang_print_anyxml(f, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200992 break;
Radek Krejci76512572015-08-04 09:47:08 +0200993 case LYS_CASE:
Radek Krejci1d82ef62015-08-07 14:44:40 +0200994 yang_print_case(f, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200995 break;
Radek Krejci76512572015-08-04 09:47:08 +0200996 case LYS_INPUT:
997 case LYS_OUTPUT:
Radek Krejci1d82ef62015-08-07 14:44:40 +0200998 yang_print_input_output(f, level, node);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200999 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001000 default:
1001 break;
1002 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001003}
1004
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001005int
Radek Krejcib8048692015-08-05 13:36:34 +02001006yang_print_model(FILE *f, struct lys_module *module)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001007{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001008 unsigned int i;
1009 int level = 0;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001010#define LEVEL (level*2)
1011
Radek Krejci1d82ef62015-08-07 14:44:40 +02001012 struct lys_node *node;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001013
Michal Vasko116172e2015-07-07 11:54:37 +02001014 if (module->type) {
Michal Vaskoe0af1e22015-07-07 14:02:02 +02001015 fprintf(f, "submodule %s {%s\n", module->name, (module->deviated ? " // DEVIATED" : ""));
Michal Vasko116172e2015-07-07 11:54:37 +02001016 level++;
Radek Krejcib8048692015-08-05 13:36:34 +02001017 fprintf(f, "%*sbelongs-to %s {\n", LEVEL, INDENT, ((struct lys_submodule *)module)->belongsto->name);
Michal Vasko116172e2015-07-07 11:54:37 +02001018 level++;
1019 fprintf(f, "%*sprefix \"%s\";\n", LEVEL, INDENT, module->prefix);
1020 level--;
1021 fprintf(f, "%*s}\n", LEVEL, INDENT);
1022 } else {
Michal Vaskoe0af1e22015-07-07 14:02:02 +02001023 fprintf(f, "module %s {%s\n", module->name, (module->deviated ? " // DEVIATED" : ""));
Michal Vasko116172e2015-07-07 11:54:37 +02001024 level++;
1025 fprintf(f, "%*snamespace \"%s\";\n", LEVEL, INDENT, module->ns);
1026 fprintf(f, "%*sprefix \"%s\";\n", LEVEL, INDENT, module->prefix);
1027 }
Radek Krejcib0594bf2015-05-21 23:51:27 +02001028
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001029 if (module->version) {
Michal Vasko6c722a62015-07-07 11:54:57 +02001030 fprintf(f, "%*syang-version %s;\n", LEVEL, INDENT, module->version == 1 ? "1" : "1.1");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001031 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001032
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001033 for (i = 0; i < module->imp_size; i++) {
1034 fprintf(f, "%*simport \"%s\" {\n", LEVEL, INDENT, module->imp[i].module->name);
1035 level++;
Michal Vaskoc39c4b12015-07-07 14:55:33 +02001036 fprintf(f, "%*sprefix \"%s\";\n", LEVEL, INDENT, module->imp[i].prefix);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001037 if (module->imp[i].rev[0]) {
1038 yang_print_text(f, level, "revision-date", module->imp[i].rev);
1039 }
1040 level--;
1041 fprintf(f, "%*s}\n", LEVEL, INDENT);
1042 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001043
Michal Vaskoe0af1e22015-07-07 14:02:02 +02001044 for (i = 0; i < module->deviation_size; ++i) {
1045 yang_print_deviation(f, level, module, &module->deviation[i]);
1046 }
1047
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001048 for (i = 0; i < module->inc_size; i++) {
1049 if (module->inc[i].rev[0]) {
1050 fprintf(f, "%*sinclude \"%s\" {\n", LEVEL, INDENT, module->inc[i].submodule->name);
Michal Vasko3d7b8562015-07-07 15:40:34 +02001051 yang_print_text(f, level + 1, "revision-date", module->inc[i].rev);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001052 fprintf(f, "%*s}\n", LEVEL, INDENT);
1053 } else {
1054 fprintf(f, "%*sinclude \"%s\";\n", LEVEL, INDENT, module->inc[i].submodule->name);
1055 }
1056 }
Radek Krejciefaeba32015-05-27 14:30:57 +02001057
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001058 if (module->org) {
1059 yang_print_text(f, level, "organization", module->org);
1060 }
1061 if (module->contact) {
1062 yang_print_text(f, level, "contact", module->contact);
1063 }
1064 if (module->dsc) {
1065 yang_print_text(f, level, "description", module->dsc);
1066 }
1067 if (module->ref) {
1068 yang_print_text(f, level, "reference", module->ref);
1069 }
1070 for (i = 0; i < module->rev_size; i++) {
1071 if (module->rev[i].dsc || module->rev[i].ref) {
1072 fprintf(f, "%*srevision \"%s\" {\n", LEVEL, INDENT, module->rev[i].date);
1073 level++;
1074 if (module->rev[i].dsc) {
1075 yang_print_text(f, level, "description", module->rev[i].dsc);
1076 }
1077 if (module->rev[i].ref) {
1078 yang_print_text(f, level, "reference", module->rev[i].ref);
1079 }
1080 level--;
1081 fprintf(f, "%*s}\n", LEVEL, INDENT);
1082 } else {
1083 yang_print_text(f, level, "revision", module->rev[i].date);
1084 }
1085 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001086
Michal Vasko30f6e912015-07-07 12:24:27 +02001087 for (i = 0; i < module->features_size; i++) {
1088 yang_print_feature(f, level, &module->features[i]);
1089 }
1090
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001091 for (i = 0; i < module->ident_size; i++) {
1092 yang_print_identity(f, level, &module->ident[i]);
1093 }
Radek Krejci6793db02015-05-22 17:49:54 +02001094
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001095 for (i = 0; i < module->tpdf_size; i++) {
1096 yang_print_typedef(f, level, module, &module->tpdf[i]);
1097 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001098
Radek Krejci1d82ef62015-08-07 14:44:40 +02001099 LY_TREE_FOR(module->data, node) {
1100 yang_print_snode(f, level, node,
Radek Krejci76512572015-08-04 09:47:08 +02001101 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
1102 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001103 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001104
Michal Vasko6f25f212015-07-07 15:42:07 +02001105 for (i = 0; i < module->augment_size; i++) {
1106 yang_print_augment(f, level, module, &module->augment[i]);
1107 }
1108
Radek Krejci1d82ef62015-08-07 14:44:40 +02001109 LY_TREE_FOR(module->rpc, node) {
1110 yang_print_rpc(f, level, node);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001111 }
1112
Radek Krejci1d82ef62015-08-07 14:44:40 +02001113 LY_TREE_FOR(module->notif, node) {
1114 yang_print_notif(f, level, node);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001115 }
1116
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001117 fprintf(f, "}\n");
Radek Krejcida04f4a2015-05-21 12:54:09 +02001118
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001119 return EXIT_SUCCESS;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001120#undef LEVEL
1121}