blob: 09a56b74fc7b5bef95a4f1559336296da93178bd [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>
24
Radek Krejci998a0b82015-08-17 13:14:36 +020025#include "common.h"
Radek Krejci76b07902015-10-09 09:11:25 +020026#include "printer.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 Krejci76b07902015-10-09 09:11:25 +020032static void yang_print_snode(struct lyout *out, 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
Radek Krejci76b07902015-10-09 09:11:25 +020057yang_print_text(struct lyout *out, 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 Krejci76b07902015-10-09 09:11:25 +020061 ly_print(out, "%*s%s\n", LEVEL, INDENT, name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020062 level++;
Radek Krejcida04f4a2015-05-21 12:54:09 +020063
Radek Krejci76b07902015-10-09 09:11:25 +020064 ly_print(out, "%*s\"", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020065 t = text;
66 while ((s = strchr(t, '\n'))) {
Radek Krejci76b07902015-10-09 09:11:25 +020067 ly_write(out, t, (s - t) + 1);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020068 t = s + 1;
Radek Krejci76b07902015-10-09 09:11:25 +020069 ly_print(out, "%*s", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020070 }
Radek Krejcida04f4a2015-05-21 12:54:09 +020071
Radek Krejci76b07902015-10-09 09:11:25 +020072 ly_print(out, "%s\";\n\n", t);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020073 level--;
Radek Krejcida04f4a2015-05-21 12:54:09 +020074
75}
76
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020077static void
Radek Krejci76b07902015-10-09 09:11:25 +020078yang_print_nacmext(struct lyout *out, 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 Krejci76b07902015-10-09 09:11:25 +0200109 ly_print(out, "%*s%s:default-deny-write;\n", LEVEL, INDENT, prefix);
Radek Krejci6764bb32015-07-03 15:16:04 +0200110 }
Radek Krejci1d82ef62015-08-07 14:44:40 +0200111 if ((node->nacm & LYS_NACM_DENYA) && (!node->parent || !(node->parent->nacm & LYS_NACM_DENYA))) {
Radek Krejci76b07902015-10-09 09:11:25 +0200112 ly_print(out, "%*s%s:default-deny-all;\n", LEVEL, INDENT, prefix);
Radek Krejci6764bb32015-07-03 15:16:04 +0200113 }
114 }
Radek Krejci6a113852015-07-03 16:04:20 +0200115}
116
117/*
118 * Covers:
119 * description, reference, status
120 */
121static void
Radek Krejci76b07902015-10-09 09:11:25 +0200122yang_print_snode_common(struct lyout *out, 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 Krejci76b07902015-10-09 09:11:25 +0200125 ly_print(out, "%*sstatus \"current\";\n", LEVEL, INDENT);
Radek Krejci1d82ef62015-08-07 14:44:40 +0200126 } else if (node->flags & LYS_STATUS_DEPRC) {
Radek Krejci76b07902015-10-09 09:11:25 +0200127 ly_print(out, "%*sstatus \"deprecated\";\n", LEVEL, INDENT);
Radek Krejci1d82ef62015-08-07 14:44:40 +0200128 } else if (node->flags & LYS_STATUS_OBSLT) {
Radek Krejci76b07902015-10-09 09:11:25 +0200129 ly_print(out, "%*sstatus \"obsolete\";\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200130 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200131
Radek Krejci1d82ef62015-08-07 14:44:40 +0200132 if (node->dsc) {
Radek Krejci76b07902015-10-09 09:11:25 +0200133 yang_print_text(out, level, "description", node->dsc);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200134 }
Radek Krejci1d82ef62015-08-07 14:44:40 +0200135 if (node->ref) {
Radek Krejci76b07902015-10-09 09:11:25 +0200136 yang_print_text(out, 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 Krejci76b07902015-10-09 09:11:25 +0200146yang_print_snode_common2(struct lyout *out, 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 Krejci76b07902015-10-09 09:11:25 +0200151 ly_print(out, "%*sconfig \"true\";\n", LEVEL, INDENT);
Radek Krejci1d82ef62015-08-07 14:44:40 +0200152 } else if (node->flags & LYS_CONFIG_R) {
Radek Krejci76b07902015-10-09 09:11:25 +0200153 ly_print(out, "%*sconfig \"false\";\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200154 }
155 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200156
Radek Krejci1d82ef62015-08-07 14:44:40 +0200157 if (node->flags & LYS_MAND_TRUE) {
Radek Krejci76b07902015-10-09 09:11:25 +0200158 ly_print(out, "%*smandatory \"true\";\n", LEVEL, INDENT);
Radek Krejci1d82ef62015-08-07 14:44:40 +0200159 } else if (node->flags & LYS_MAND_FALSE) {
Radek Krejci76b07902015-10-09 09:11:25 +0200160 ly_print(out, "%*smandatory \"false\";\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200161 }
Michal Vasko2b8faed2015-06-09 12:51:20 +0200162
Radek Krejci76b07902015-10-09 09:11:25 +0200163 yang_print_snode_common(out, level, node);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200164}
165
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200166static void
Radek Krejci76b07902015-10-09 09:11:25 +0200167yang_print_iffeature(struct lyout *out, int level, struct lys_module *module, struct lys_feature *feat)
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200168{
Radek Krejci76b07902015-10-09 09:11:25 +0200169 ly_print(out, "%*sif-feature ", LEVEL, INDENT);
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200170 if ((feat->module != module) && !feat->module->type) {
Radek Krejci76b07902015-10-09 09:11:25 +0200171 ly_print(out, "%s:", get_module_import_prefix(module, feat->module));
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200172 }
Radek Krejci76b07902015-10-09 09:11:25 +0200173 ly_print(out, "%s;\n", feat->name);
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200174}
175
176static void
Radek Krejci76b07902015-10-09 09:11:25 +0200177yang_print_feature(struct lyout *out, 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
Radek Krejci76b07902015-10-09 09:11:25 +0200181 ly_print(out, "%*sfeature %s {\n", LEVEL, INDENT, feat->name);
Michal Vasko4773b762015-07-07 12:15:10 +0200182 level++;
183
Radek Krejci76b07902015-10-09 09:11:25 +0200184 yang_print_snode_common(out, level, (struct lys_node *)feat);
Michal Vasko30f6e912015-07-07 12:24:27 +0200185 for (i = 0; i < feat->features_size; ++i) {
Radek Krejci76b07902015-10-09 09:11:25 +0200186 yang_print_iffeature(out, level, feat->module, feat->features[i]);
Michal Vasko30f6e912015-07-07 12:24:27 +0200187 }
Michal Vasko4773b762015-07-07 12:15:10 +0200188
189 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200190 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko4773b762015-07-07 12:15:10 +0200191}
192
193static void
Radek Krejci76b07902015-10-09 09:11:25 +0200194yang_print_restr(struct lyout *out, 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) {
Radek Krejci76b07902015-10-09 09:11:25 +0200197 yang_print_text(out, level, "description", restr->dsc);
Radek Krejci41726f92015-06-19 13:11:05 +0200198 }
Radek Krejci0bd5db42015-06-19 13:30:07 +0200199 if (restr->ref != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200200 yang_print_text(out, level, "reference", restr->ref);
Radek Krejci41726f92015-06-19 13:11:05 +0200201 }
Radek Krejci0bd5db42015-06-19 13:30:07 +0200202 if (restr->eapptag != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200203 ly_print(out, "%*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) {
Radek Krejci76b07902015-10-09 09:11:25 +0200206 yang_print_text(out, level, "error-message", restr->emsg);
Radek Krejci41726f92015-06-19 13:11:05 +0200207 }
208}
209
210static void
Michal Vaskof9893382015-10-09 14:03:04 +0200211yang_print_when(struct lyout *out, int level, struct lys_module *module, struct lys_when *when)
Michal Vasko1f0428a2015-07-07 14:55:04 +0200212{
Michal Vaskof9893382015-10-09 14:03:04 +0200213 const char *xml_expr;
214
215 xml_expr = transform_expr_json2xml(module, when->cond, NULL, NULL, NULL);
216 if (!xml_expr) {
217 ly_print(out, "(!error!)");
218 return;
219 }
220
221 ly_print(out, "%*swhen \"%s\" {\n", LEVEL, INDENT, xml_expr);
222 lydict_remove(module->ctx, xml_expr);
223
Michal Vasko1f0428a2015-07-07 14:55:04 +0200224 level++;
225 if (when->dsc) {
Radek Krejci76b07902015-10-09 09:11:25 +0200226 yang_print_text(out, level, "description", when->dsc);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200227 }
228 if (when->ref) {
Radek Krejci76b07902015-10-09 09:11:25 +0200229 yang_print_text(out, level, "reference", when->ref);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200230 }
231 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200232 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200233}
234
235static void
Radek Krejci76b07902015-10-09 09:11:25 +0200236yang_print_type(struct lyout *out, int level, struct lys_module *module, struct lys_type *type)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200237{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200238 int i;
Radek Krejci25d782a2015-05-22 15:03:23 +0200239
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200240 if (type->prefix) {
Radek Krejci76b07902015-10-09 09:11:25 +0200241 ly_print(out, "%*stype %s:%s {\n", LEVEL, INDENT, type->prefix, type->der->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200242 } else {
Radek Krejci76b07902015-10-09 09:11:25 +0200243 ly_print(out, "%*stype %s {\n", LEVEL, INDENT, type->der->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200244 }
245 level++;
246 switch (type->base) {
247 case LY_TYPE_BINARY:
248 if (type->info.binary.length != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200249 ly_print(out, "%*slength \"%s\" {\n", LEVEL, INDENT, type->info.binary.length->expr);
250 yang_print_restr(out, level + 1, type->info.binary.length);
251 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200252 }
253 break;
254 case LY_TYPE_BITS:
255 for (i = 0; i < type->info.bits.count; ++i) {
Radek Krejci76b07902015-10-09 09:11:25 +0200256 ly_print(out, "%*sbit %s {\n", LEVEL, INDENT, type->info.bits.bit[i].name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200257 level++;
Radek Krejci76b07902015-10-09 09:11:25 +0200258 yang_print_snode_common(out, level, (struct lys_node *)&type->info.bits.bit[i]);
259 ly_print(out, "%*sposition %u;\n", LEVEL, INDENT, type->info.bits.bit[i].pos);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200260 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200261 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200262 }
263 break;
264 case LY_TYPE_DEC64:
Radek Krejci76b07902015-10-09 09:11:25 +0200265 ly_print(out, "%*sfraction-digits %d;\n", LEVEL, INDENT, type->info.dec64.dig);
Michal Vaskoea505ee2015-07-07 14:01:00 +0200266 if (type->info.dec64.range != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200267 ly_print(out, "%*srange \"%s\" {\n", LEVEL, INDENT, type->info.dec64.range->expr);
268 yang_print_restr(out, level + 1, type->info.dec64.range);
269 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskoea505ee2015-07-07 14:01:00 +0200270 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200271 break;
272 case LY_TYPE_ENUM:
273 for (i = 0; i < type->info.enums.count; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200274 ly_print(out, "%*senum %s {\n", LEVEL, INDENT, type->info.enums.enm[i].name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200275 level++;
Radek Krejci76b07902015-10-09 09:11:25 +0200276 yang_print_snode_common(out, level, (struct lys_node *)&type->info.enums.enm[i]);
277 ly_print(out, "%*svalue %d;\n", LEVEL, INDENT, type->info.enums.enm[i].value);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200278 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200279 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200280 }
281 break;
282 case LY_TYPE_IDENT:
283 if (module == type->info.ident.ref->module) {
Radek Krejci76b07902015-10-09 09:11:25 +0200284 ly_print(out, "%*sbase %s;\n", LEVEL, INDENT, type->info.ident.ref->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200285 } else {
Radek Krejci76b07902015-10-09 09:11:25 +0200286 ly_print(out, "%*sbase %s:%s;\n", LEVEL, INDENT, type->info.ident.ref->module->prefix,
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200287 type->info.ident.ref->name);
288 }
289 break;
290 case LY_TYPE_INST:
Radek Krejciaf351422015-06-19 14:49:38 +0200291 if (type->info.inst.req == 1) {
Radek Krejci76b07902015-10-09 09:11:25 +0200292 ly_print(out, "%*srequire-instance \"true\";\n", LEVEL, INDENT);
Radek Krejciaf351422015-06-19 14:49:38 +0200293 } else if (type->info.inst.req == -1) {
Radek Krejci76b07902015-10-09 09:11:25 +0200294 ly_print(out, "%*srequire-instance \"false\";\n", LEVEL, INDENT);
Radek Krejciaf351422015-06-19 14:49:38 +0200295 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200296 break;
297 case LY_TYPE_INT8:
298 case LY_TYPE_INT16:
299 case LY_TYPE_INT32:
300 case LY_TYPE_INT64:
301 case LY_TYPE_UINT8:
302 case LY_TYPE_UINT16:
303 case LY_TYPE_UINT32:
304 case LY_TYPE_UINT64:
Radek Krejcif2860132015-06-20 12:37:20 +0200305 if (type->info.num.range != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200306 ly_print(out, "%*srange \"%s\" {\n", LEVEL, INDENT, type->info.num.range->expr);
307 yang_print_restr(out, level + 1, type->info.num.range);
308 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcif2860132015-06-20 12:37:20 +0200309 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200310 break;
311 case LY_TYPE_LEAFREF:
Radek Krejci76b07902015-10-09 09:11:25 +0200312 ly_print(out, "%*spath \"%s\";\n", LEVEL, INDENT, type->info.lref.path);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200313 break;
314 case LY_TYPE_STRING:
Radek Krejci5fbc9162015-06-19 14:11:11 +0200315 if (type->info.str.length) {
Radek Krejci76b07902015-10-09 09:11:25 +0200316 ly_print(out, "%*slength \"%s\" {\n", LEVEL, INDENT, type->info.str.length->expr);
317 yang_print_restr(out, level + 1, type->info.str.length);
318 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci061bd522015-06-19 13:45:16 +0200319 }
Radek Krejci5fbc9162015-06-19 14:11:11 +0200320 for (i = 0; i < type->info.str.pat_count; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200321 ly_print(out, "%*spattern \"%s\" {\n", LEVEL, INDENT, type->info.str.patterns[i].expr);
322 yang_print_restr(out, level + 1, &type->info.str.patterns[i]);
323 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci5fbc9162015-06-19 14:11:11 +0200324 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200325 break;
326 case LY_TYPE_UNION:
327 for (i = 0; i < type->info.uni.count; ++i) {
Radek Krejci76b07902015-10-09 09:11:25 +0200328 yang_print_type(out, level, module, &type->info.uni.types[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200329 }
330 break;
331 default:
332 /* other types do not have substatements */
333 break;
334 }
335 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200336 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200337}
338
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200339static void
Michal Vaskof9893382015-10-09 14:03:04 +0200340yang_print_must(struct lyout *out, int level, struct lys_module *module, struct lys_restr *must)
Michal Vasko7f976ee2015-06-09 13:55:41 +0200341{
Michal Vaskof9893382015-10-09 14:03:04 +0200342 const char *xml_expr;
343
344 xml_expr = transform_expr_json2xml(module, must->expr, NULL, NULL, NULL);
345 if (!xml_expr) {
346 ly_print(out, "(!error!)");
347 return;
348 }
349
350 ly_print(out, "%*smust \"%s\" {\n", LEVEL, INDENT, xml_expr);
351 lydict_remove(module->ctx, xml_expr);
352
Radek Krejci76b07902015-10-09 09:11:25 +0200353 yang_print_restr(out, level + 1, must);
354 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko7f976ee2015-06-09 13:55:41 +0200355}
356
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200357static void
Radek Krejci76b07902015-10-09 09:11:25 +0200358yang_print_unique(struct lyout *out, int level, struct lys_unique *uniq)
Michal Vasko1ef07972015-07-07 14:01:35 +0200359{
360 int i;
361
Radek Krejci76b07902015-10-09 09:11:25 +0200362 ly_print(out, "%*sunique \"", LEVEL, INDENT);
Michal Vasko1ef07972015-07-07 14:01:35 +0200363 for (i = 0; i < uniq->leafs_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200364 ly_print(out, "%s%s", uniq->leafs[i]->name, i + 1 < uniq->leafs_size ? " " : "");
Michal Vasko1ef07972015-07-07 14:01:35 +0200365 }
Radek Krejci76b07902015-10-09 09:11:25 +0200366 ly_print(out, "\";\n");
Michal Vasko1ef07972015-07-07 14:01:35 +0200367}
368
369static void
Michal Vaskof9893382015-10-09 14:03:04 +0200370yang_print_refine(struct lyout *out, int level, struct lys_module *module, struct lys_refine *refine)
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200371{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200372 int i;
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200373
Radek Krejci76b07902015-10-09 09:11:25 +0200374 ly_print(out, "%*srefine \"%s\" {\n", LEVEL, INDENT, refine->target_name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200375 level++;
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200376
Radek Krejci1574a8d2015-08-03 14:16:52 +0200377 if (refine->flags & LYS_CONFIG_W) {
Radek Krejci76b07902015-10-09 09:11:25 +0200378 ly_print(out, "%*sconfig \"true\";\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200379 } else if (refine->flags & LYS_CONFIG_R) {
Radek Krejci76b07902015-10-09 09:11:25 +0200380 ly_print(out, "%*sconfig \"false\";\n", LEVEL, INDENT);
Radek Krejci6a113852015-07-03 16:04:20 +0200381 }
382
Radek Krejci1574a8d2015-08-03 14:16:52 +0200383 if (refine->flags & LYS_MAND_TRUE) {
Radek Krejci76b07902015-10-09 09:11:25 +0200384 ly_print(out, "%*smandatory \"true\";\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200385 } else if (refine->flags & LYS_MAND_FALSE) {
Radek Krejci76b07902015-10-09 09:11:25 +0200386 ly_print(out, "%*smandatory \"false\";\n", LEVEL, INDENT);
Radek Krejci6a113852015-07-03 16:04:20 +0200387 }
388
Radek Krejci76b07902015-10-09 09:11:25 +0200389 yang_print_snode_common(out, level, (struct lys_node *)refine);
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200390
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200391 for (i = 0; i < refine->must_size; ++i) {
Michal Vaskof9893382015-10-09 14:03:04 +0200392 yang_print_must(out, level, module, &refine->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200393 }
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200394
Radek Krejci76512572015-08-04 09:47:08 +0200395 if (refine->target_type & (LYS_LEAF | LYS_CHOICE)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200396 if (refine->mod.dflt != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200397 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, refine->mod.dflt);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200398 }
Radek Krejci76512572015-08-04 09:47:08 +0200399 } else if (refine->target_type == LYS_CONTAINER) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200400 if (refine->mod.presence != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200401 yang_print_text(out, level, "presence", refine->mod.presence);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200402 }
Radek Krejci76512572015-08-04 09:47:08 +0200403 } else if (refine->target_type & (LYS_LIST | LYS_LEAFLIST)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200404 if (refine->mod.list.min > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200405 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, refine->mod.list.min);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200406 }
407 if (refine->mod.list.max > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200408 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, refine->mod.list.max);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200409 }
410 }
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200411
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200412 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200413 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200414}
415
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200416static void
Radek Krejci76b07902015-10-09 09:11:25 +0200417yang_print_deviation(struct lyout *out, int level, struct lys_module *module, struct lys_deviation *deviation)
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200418{
419 int i, j;
420
Radek Krejci76b07902015-10-09 09:11:25 +0200421 ly_print(out, "%*sdeviation \"%s\" {\n", LEVEL, INDENT, deviation->target_name);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200422 level++;
423
424 if (deviation->dsc) {
Radek Krejci76b07902015-10-09 09:11:25 +0200425 yang_print_text(out, level, "description", deviation->dsc);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200426 }
427 if (deviation->ref) {
Radek Krejci76b07902015-10-09 09:11:25 +0200428 yang_print_text(out, level, "reference", deviation->ref);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200429 }
430
431 for (i = 0; i < deviation->deviate_size; ++i) {
Radek Krejci76b07902015-10-09 09:11:25 +0200432 ly_print(out, "%*sdeviate ", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200433 if (deviation->deviate[i].mod == LY_DEVIATE_NO) {
Radek Krejci76b07902015-10-09 09:11:25 +0200434 ly_print(out, "not-supported {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200435 } else if (deviation->deviate[i].mod == LY_DEVIATE_ADD) {
Radek Krejci76b07902015-10-09 09:11:25 +0200436 ly_print(out, "add {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200437 } else if (deviation->deviate[i].mod == LY_DEVIATE_RPL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200438 ly_print(out, "replace {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200439 } else if (deviation->deviate[i].mod == LY_DEVIATE_DEL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200440 ly_print(out, "delete {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200441 }
442 level++;
443
Radek Krejci1574a8d2015-08-03 14:16:52 +0200444 if (deviation->deviate[i].flags & LYS_CONFIG_W) {
Radek Krejci76b07902015-10-09 09:11:25 +0200445 ly_print(out, "%*sconfig \"true\";\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200446 } else if (deviation->deviate[i].flags & LYS_CONFIG_R) {
Radek Krejci76b07902015-10-09 09:11:25 +0200447 ly_print(out, "%*sconfig \"false\";\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200448 }
449
Radek Krejci1574a8d2015-08-03 14:16:52 +0200450 if (deviation->deviate[i].flags & LYS_MAND_TRUE) {
Radek Krejci76b07902015-10-09 09:11:25 +0200451 ly_print(out, "%*smandatory \"true\";\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200452 } else if (deviation->deviate[i].flags & LYS_MAND_FALSE) {
Radek Krejci76b07902015-10-09 09:11:25 +0200453 ly_print(out, "%*smandatory \"false\";\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200454 }
455
456 if (deviation->deviate[i].dflt) {
Radek Krejci76b07902015-10-09 09:11:25 +0200457 ly_print(out, "%*sdefault %s;\n", LEVEL, INDENT, deviation->deviate[i].dflt);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200458 }
459
460 if (deviation->deviate[i].min) {
Radek Krejci76b07902015-10-09 09:11:25 +0200461 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, deviation->deviate[i].min);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200462 }
463 if (deviation->deviate[i].max) {
Radek Krejci76b07902015-10-09 09:11:25 +0200464 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, deviation->deviate[i].max);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200465 }
466
467 for (j = 0; j < deviation->deviate[i].must_size; ++j) {
Michal Vaskof9893382015-10-09 14:03:04 +0200468 yang_print_must(out, level, module, &deviation->deviate[i].must[j]);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200469 }
470
471 for (j = 0; j < deviation->deviate[i].unique_size; ++j) {
Radek Krejci76b07902015-10-09 09:11:25 +0200472 yang_print_unique(out, level, &deviation->deviate[i].unique[j]);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200473 }
474
475 if (deviation->deviate[i].type) {
Radek Krejci76b07902015-10-09 09:11:25 +0200476 yang_print_type(out, level, module, deviation->deviate[i].type);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200477 }
478
479 if (deviation->deviate[i].units) {
Radek Krejci76b07902015-10-09 09:11:25 +0200480 ly_print(out, "%*sunits %s;\n", LEVEL, INDENT, deviation->deviate[i].units);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200481 }
482
483 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200484 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200485 }
486
487 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200488 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200489}
490
491static void
Radek Krejci76b07902015-10-09 09:11:25 +0200492yang_print_augment(struct lyout *out, int level, struct lys_module *module, struct lys_node_augment *augment)
Michal Vasko6f25f212015-07-07 15:42:07 +0200493{
494 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200495 struct lys_node *sub;
Michal Vasko6f25f212015-07-07 15:42:07 +0200496
Radek Krejci76b07902015-10-09 09:11:25 +0200497 ly_print(out, "%*saugment \"%s\" {\n", LEVEL, INDENT, augment->target_name);
Michal Vasko6f25f212015-07-07 15:42:07 +0200498 level++;
499
Radek Krejci76b07902015-10-09 09:11:25 +0200500 yang_print_nacmext(out, level, (struct lys_node *)augment, module);
501 yang_print_snode_common(out, level, (struct lys_node *)augment);
Michal Vasko6f25f212015-07-07 15:42:07 +0200502
503 for (i = 0; i < augment->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200504 yang_print_iffeature(out, level, module, augment->features[i]);
Michal Vasko6f25f212015-07-07 15:42:07 +0200505 }
506
507 if (augment->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200508 yang_print_when(out, level, module, augment->when);
Michal Vasko6f25f212015-07-07 15:42:07 +0200509 }
510
511 LY_TREE_FOR(augment->child, sub) {
Radek Krejci76b07902015-10-09 09:11:25 +0200512 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200513 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
514 LYS_USES | LYS_ANYXML | LYS_CASE);
Michal Vasko6f25f212015-07-07 15:42:07 +0200515 }
516
517 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200518 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko6f25f212015-07-07 15:42:07 +0200519}
520
521static void
Radek Krejci76b07902015-10-09 09:11:25 +0200522yang_print_typedef(struct lyout *out, int level, struct lys_module *module, struct lys_tpdf *tpdf)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200523{
Radek Krejci76b07902015-10-09 09:11:25 +0200524 ly_print(out, "%*stypedef %s {\n", LEVEL, INDENT, tpdf->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200525 level++;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200526
Radek Krejci76b07902015-10-09 09:11:25 +0200527 yang_print_snode_common(out, level, (struct lys_node *)tpdf);
528 yang_print_type(out, level, module, &tpdf->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200529 if (tpdf->units != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200530 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, tpdf->units);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200531 }
532 if (tpdf->dflt != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200533 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, tpdf->dflt);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200534 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200535
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200536 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200537 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200538}
539
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200540static void
Radek Krejci76b07902015-10-09 09:11:25 +0200541yang_print_identity(struct lyout *out, int level, struct lys_ident *ident)
Radek Krejci6793db02015-05-22 17:49:54 +0200542{
Radek Krejci76b07902015-10-09 09:11:25 +0200543 ly_print(out, "%*sidentity %s {\n", LEVEL, INDENT, ident->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200544 level++;
Radek Krejci6793db02015-05-22 17:49:54 +0200545
Radek Krejci76b07902015-10-09 09:11:25 +0200546 yang_print_snode_common(out, level, (struct lys_node *)ident);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200547 if (ident->base) {
Radek Krejci76b07902015-10-09 09:11:25 +0200548 ly_print(out, "%*sbase ", LEVEL, INDENT);
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200549 if ((ident->module != ident->base->module) && !ident->base->module->type) {
Radek Krejci76b07902015-10-09 09:11:25 +0200550 ly_print(out, "%s:", get_module_import_prefix(ident->module, ident->base->module));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200551 }
Radek Krejci76b07902015-10-09 09:11:25 +0200552 ly_print(out, "%s;\n", ident->base->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200553 }
Radek Krejci6793db02015-05-22 17:49:54 +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);
Radek Krejci6793db02015-05-22 17:49:54 +0200557
558}
559
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200560static void
Radek Krejci76b07902015-10-09 09:11:25 +0200561yang_print_container(struct lyout *out, int level, struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200562{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200563 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200564 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200565 struct lys_node_container *cont = (struct lys_node_container *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200566
Radek Krejci76b07902015-10-09 09:11:25 +0200567 ly_print(out, "%*scontainer %s {\n", LEVEL, INDENT, node->name);
Michal Vasko7f976ee2015-06-09 13:55:41 +0200568
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200569 level++;
Radek Krejci6a113852015-07-03 16:04:20 +0200570
Radek Krejci76b07902015-10-09 09:11:25 +0200571 yang_print_nacmext(out, level, node, node->module);
Radek Krejci6a113852015-07-03 16:04:20 +0200572
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200573 if (cont->presence != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200574 yang_print_text(out, level, "presence", cont->presence);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200575 }
Michal Vasko7f976ee2015-06-09 13:55:41 +0200576
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200577 for (i = 0; i < cont->must_size; i++) {
Michal Vaskof9893382015-10-09 14:03:04 +0200578 yang_print_must(out, level, node->module, &cont->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200579 }
Michal Vasko7f976ee2015-06-09 13:55:41 +0200580
Radek Krejci76b07902015-10-09 09:11:25 +0200581 yang_print_snode_common2(out, level, node);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200582
Michal Vasko4773b762015-07-07 12:15:10 +0200583 for (i = 0; i < cont->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200584 yang_print_iffeature(out, level, node->module, cont->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200585 }
586
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200587 for (i = 0; i < cont->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200588 yang_print_typedef(out, level, node->module, &cont->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200589 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200590
Michal Vasko1f0428a2015-07-07 14:55:04 +0200591 if (cont->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200592 yang_print_when(out, level, node->module, cont->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200593 }
594
Radek Krejci1d82ef62015-08-07 14:44:40 +0200595 LY_TREE_FOR(node->child, sub) {
Michal Vasko15b48702015-07-07 15:49:34 +0200596 /* augment */
Radek Krejci1d82ef62015-08-07 14:44:40 +0200597 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200598 continue;
599 }
Radek Krejci76b07902015-10-09 09:11:25 +0200600 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200601 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
602 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200603 }
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200604
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200605 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200606 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200607}
608
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200609static void
Radek Krejci76b07902015-10-09 09:11:25 +0200610yang_print_case(struct lyout *out, int level, struct lys_node *node)
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200611{
Michal Vasko4773b762015-07-07 12:15:10 +0200612 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200613 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200614 struct lys_node_case *cas = (struct lys_node_case *)node;
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200615
Radek Krejci76b07902015-10-09 09:11:25 +0200616 ly_print(out, "%*scase %s {\n", LEVEL, INDENT, cas->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200617 level++;
Radek Krejci76b07902015-10-09 09:11:25 +0200618 yang_print_nacmext(out, level, node, node->module);
619 yang_print_snode_common2(out, level, node);
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200620
Michal Vasko4773b762015-07-07 12:15:10 +0200621 for (i = 0; i < cas->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200622 yang_print_iffeature(out, level, node->module, cas->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200623 }
624
Michal Vasko1f0428a2015-07-07 14:55:04 +0200625 if (cas->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200626 yang_print_when(out, level, node->module, cas->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200627 }
628
Radek Krejci1d82ef62015-08-07 14:44:40 +0200629 LY_TREE_FOR(node->child, sub) {
Michal Vasko15b48702015-07-07 15:49:34 +0200630 /* augment */
Radek Krejci1d82ef62015-08-07 14:44:40 +0200631 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200632 continue;
633 }
Radek Krejci76b07902015-10-09 09:11:25 +0200634 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200635 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
636 LYS_USES | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200637 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200638
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200639 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200640 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200641}
642
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200643static void
Radek Krejci76b07902015-10-09 09:11:25 +0200644yang_print_choice(struct lyout *out, int level, struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200645{
Michal Vasko4773b762015-07-07 12:15:10 +0200646 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200647 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200648 struct lys_node_choice *choice = (struct lys_node_choice *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200649
Radek Krejci76b07902015-10-09 09:11:25 +0200650 ly_print(out, "%*schoice %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +0200651
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200652 level++;
Radek Krejci76b07902015-10-09 09:11:25 +0200653 yang_print_nacmext(out, level, node, node->module);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200654 if (choice->dflt != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200655 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, choice->dflt->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200656 }
Michal Vasko16083662015-06-09 14:00:45 +0200657
Radek Krejci76b07902015-10-09 09:11:25 +0200658 yang_print_snode_common2(out, level, node);
Michal Vasko4773b762015-07-07 12:15:10 +0200659
660 for (i = 0; i < choice->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200661 yang_print_iffeature(out, level, node->module, choice->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200662 }
663
Michal Vasko1f0428a2015-07-07 14:55:04 +0200664 if (choice->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200665 yang_print_when(out, level, node->module, choice->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200666 }
667
Radek Krejci1d82ef62015-08-07 14:44:40 +0200668 LY_TREE_FOR(node->child, sub) {
Michal Vasko15b48702015-07-07 15:49:34 +0200669 /* augment */
Radek Krejci1d82ef62015-08-07 14:44:40 +0200670 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200671 continue;
672 }
Radek Krejci76b07902015-10-09 09:11:25 +0200673 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200674 LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYXML | LYS_CASE);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200675 }
676 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200677 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200678}
679
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200680static void
Radek Krejci76b07902015-10-09 09:11:25 +0200681yang_print_leaf(struct lyout *out, int level, struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200682{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200683 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200684 struct lys_node_leaf *leaf = (struct lys_node_leaf *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200685
Radek Krejci76b07902015-10-09 09:11:25 +0200686 ly_print(out, "%*sleaf %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +0200687
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200688 level++;
Radek Krejci76b07902015-10-09 09:11:25 +0200689 yang_print_nacmext(out, level, node, node->module);
690 yang_print_snode_common2(out, level, node);
Michal Vasko4773b762015-07-07 12:15:10 +0200691 for (i = 0; i < leaf->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200692 yang_print_iffeature(out, level, node->module, leaf->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200693 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200694 for (i = 0; i < leaf->must_size; i++) {
Michal Vaskof9893382015-10-09 14:03:04 +0200695 yang_print_must(out, level, node->module, &leaf->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200696 }
Michal Vasko1f0428a2015-07-07 14:55:04 +0200697 if (leaf->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200698 yang_print_when(out, level, node->module, leaf->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200699 }
Radek Krejci76b07902015-10-09 09:11:25 +0200700 yang_print_type(out, level, node->module, &leaf->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200701 if (leaf->units != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200702 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, leaf->units);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200703 }
704 if (leaf->dflt != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200705 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, leaf->dflt);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200706 }
707 level--;
Michal Vasko16083662015-06-09 14:00:45 +0200708
Radek Krejci76b07902015-10-09 09:11:25 +0200709 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko16083662015-06-09 14:00:45 +0200710}
711
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200712static void
Radek Krejci76b07902015-10-09 09:11:25 +0200713yang_print_anyxml(struct lyout *out, int level, struct lys_node *node)
Michal Vasko16083662015-06-09 14:00:45 +0200714{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200715 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200716 struct lys_node_anyxml *anyxml = (struct lys_node_anyxml *)node;
Michal Vasko16083662015-06-09 14:00:45 +0200717
Radek Krejci76b07902015-10-09 09:11:25 +0200718 ly_print(out, "%*sanyxml %s {\n", LEVEL, INDENT, anyxml->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200719 level++;
Radek Krejci76b07902015-10-09 09:11:25 +0200720 yang_print_nacmext(out, level, node, node->module);
721 yang_print_snode_common2(out, level, node);
Michal Vasko4773b762015-07-07 12:15:10 +0200722 for (i = 0; i < anyxml->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200723 yang_print_iffeature(out, level, node->module, anyxml->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200724 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200725 for (i = 0; i < anyxml->must_size; i++) {
Michal Vaskof9893382015-10-09 14:03:04 +0200726 yang_print_must(out, level, node->module, &anyxml->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200727 }
Michal Vasko1f0428a2015-07-07 14:55:04 +0200728 if (anyxml->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200729 yang_print_when(out, level, node->module, anyxml->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200730 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200731 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200732 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200733}
734
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200735static void
Radek Krejci76b07902015-10-09 09:11:25 +0200736yang_print_leaflist(struct lyout *out, int level, struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200737{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200738 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200739 struct lys_node_leaflist *llist = (struct lys_node_leaflist *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200740
Radek Krejci76b07902015-10-09 09:11:25 +0200741 ly_print(out, "%*sleaf-list %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +0200742
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200743 level++;
Radek Krejci76b07902015-10-09 09:11:25 +0200744 yang_print_nacmext(out, level, node, node->module);
745 yang_print_snode_common2(out, level, node);
Michal Vasko4773b762015-07-07 12:15:10 +0200746 for (i = 0; i < llist->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200747 yang_print_iffeature(out, level, node->module, llist->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200748 }
Radek Krejci1574a8d2015-08-03 14:16:52 +0200749 if (llist->flags & LYS_USERORDERED) {
Radek Krejci76b07902015-10-09 09:11:25 +0200750 ly_print(out, "%*sordered-by user;\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200751 }
752 if (llist->min > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200753 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, llist->min);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200754 }
755 if (llist->max > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200756 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, llist->max);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200757 }
758 for (i = 0; i < llist->must_size; i++) {
Michal Vaskof9893382015-10-09 14:03:04 +0200759 yang_print_must(out, level, node->module, &llist->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200760 }
Michal Vasko1f0428a2015-07-07 14:55:04 +0200761 if (llist->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200762 yang_print_when(out, level, llist->module, llist->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200763 }
Radek Krejci76b07902015-10-09 09:11:25 +0200764 yang_print_type(out, level, node->module, &llist->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200765 if (llist->units != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200766 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, llist->units);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200767 }
768 level--;
Michal Vasko16083662015-06-09 14:00:45 +0200769
Radek Krejci76b07902015-10-09 09:11:25 +0200770 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200771}
772
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200773static void
Radek Krejci76b07902015-10-09 09:11:25 +0200774yang_print_list(struct lyout *out, int level, struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200775{
Michal Vasko1ef07972015-07-07 14:01:35 +0200776 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200777 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200778 struct lys_node_list *list = (struct lys_node_list *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200779
Radek Krejci76b07902015-10-09 09:11:25 +0200780 ly_print(out, "%*slist %s {\n", LEVEL, INDENT, node->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200781 level++;
Radek Krejci76b07902015-10-09 09:11:25 +0200782 yang_print_nacmext(out, level, node, node->module);
783 yang_print_snode_common2(out, level, node);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200784
Michal Vasko4773b762015-07-07 12:15:10 +0200785 for (i = 0; i < list->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200786 yang_print_iffeature(out, level, node->module, list->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200787 }
788
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200789 if (list->keys_size) {
Radek Krejci76b07902015-10-09 09:11:25 +0200790 ly_print(out, "%*skey \"", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200791 for (i = 0; i < list->keys_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200792 ly_print(out, "%s%s", list->keys[i]->name, i + 1 < list->keys_size ? " " : "");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200793 }
Radek Krejci76b07902015-10-09 09:11:25 +0200794 ly_print(out, "\";\n");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200795 }
Radek Krejcid7f0d012015-05-25 15:04:52 +0200796
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200797 for (i = 0; i < list->unique_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200798 yang_print_unique(out, level, &list->unique[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200799 }
Michal Vaskodff818b2015-06-09 14:01:12 +0200800
Radek Krejci1574a8d2015-08-03 14:16:52 +0200801 if (list->flags & LYS_USERORDERED) {
Radek Krejci76b07902015-10-09 09:11:25 +0200802 ly_print(out, "%*sordered-by user;\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200803 }
804 if (list->min > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200805 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, list->min);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200806 }
807 if (list->max > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200808 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, list->max);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200809 }
810 for (i = 0; i < list->must_size; i++) {
Michal Vaskof9893382015-10-09 14:03:04 +0200811 yang_print_must(out, level, list->module, &list->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200812 }
Michal Vasko1f0428a2015-07-07 14:55:04 +0200813 if (list->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200814 yang_print_when(out, level, list->module, list->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200815 }
Michal Vaskodff818b2015-06-09 14:01:12 +0200816
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200817 for (i = 0; i < list->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200818 yang_print_typedef(out, level, list->module, &list->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200819 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200820
Radek Krejci1d82ef62015-08-07 14:44:40 +0200821 LY_TREE_FOR(node->child, sub) {
Michal Vasko15b48702015-07-07 15:49:34 +0200822 /* augment */
Radek Krejci1d82ef62015-08-07 14:44:40 +0200823 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200824 continue;
825 }
Radek Krejci76b07902015-10-09 09:11:25 +0200826 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200827 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
828 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200829 }
830 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200831 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200832}
833
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200834static void
Radek Krejci76b07902015-10-09 09:11:25 +0200835yang_print_grouping(struct lyout *out, int level, struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200836{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200837 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200838 struct lys_node *child;
839 struct lys_node_grp *grp = (struct lys_node_grp *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200840
Radek Krejci76b07902015-10-09 09:11:25 +0200841 ly_print(out, "%*sgrouping %s {\n", LEVEL, INDENT, node->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200842 level++;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200843
Radek Krejci76b07902015-10-09 09:11:25 +0200844 yang_print_snode_common(out, level, node);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200845
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200846 for (i = 0; i < grp->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200847 yang_print_typedef(out, level, node->module, &grp->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200848 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200849
Radek Krejci1d82ef62015-08-07 14:44:40 +0200850 LY_TREE_FOR(node->child, child) {
Radek Krejci76b07902015-10-09 09:11:25 +0200851 yang_print_snode(out, level, child,
Radek Krejci76512572015-08-04 09:47:08 +0200852 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
853 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200854 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200855
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200856 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200857 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200858}
859
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200860static void
Radek Krejci76b07902015-10-09 09:11:25 +0200861yang_print_uses(struct lyout *out, int level, struct lys_node *node)
Radek Krejcic7c9a6c2015-05-25 16:35:06 +0200862{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200863 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200864 struct lys_node_uses *uses = (struct lys_node_uses *)node;
Radek Krejcic7c9a6c2015-05-25 16:35:06 +0200865
Radek Krejci76b07902015-10-09 09:11:25 +0200866 ly_print(out, "%*suses ", LEVEL, INDENT);
Radek Krejci1d82ef62015-08-07 14:44:40 +0200867 if (node->child && (node->module != node->child->module) && !node->child->module->type) {
Radek Krejci76b07902015-10-09 09:11:25 +0200868 ly_print(out, "%s:", get_module_import_prefix(node->module, node->child->module));
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200869 }
Radek Krejci76b07902015-10-09 09:11:25 +0200870 ly_print(out, "%s {\n",uses->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200871 level++;
Radek Krejcic7c9a6c2015-05-25 16:35:06 +0200872
Radek Krejci76b07902015-10-09 09:11:25 +0200873 yang_print_nacmext(out, level, node, node->module);
874 yang_print_snode_common(out, level, node);
Michal Vasko4773b762015-07-07 12:15:10 +0200875 for (i = 0; i < uses->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200876 yang_print_iffeature(out, level, node->module, uses->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200877 }
Michal Vasko1f0428a2015-07-07 14:55:04 +0200878 if (uses->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200879 yang_print_when(out, level, node->module, uses->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200880 }
Radek Krejcic7c9a6c2015-05-25 16:35:06 +0200881
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200882 for (i = 0; i < uses->refine_size; i++) {
Michal Vaskof9893382015-10-09 14:03:04 +0200883 yang_print_refine(out, level, node->module, &uses->refine[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200884 }
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200885
Michal Vasko6f25f212015-07-07 15:42:07 +0200886 for (i = 0; i < uses->augment_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200887 yang_print_augment(out, level, node->module, &uses->augment[i]);
Michal Vasko6f25f212015-07-07 15:42:07 +0200888 }
889
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200890 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200891 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcic7c9a6c2015-05-25 16:35:06 +0200892}
893
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200894static void
Radek Krejci76b07902015-10-09 09:11:25 +0200895yang_print_input_output(struct lyout *out, int level, struct lys_node *node)
Michal Vasko5bbae102015-06-16 12:16:44 +0200896{
Michal Vaskof4d3d742015-06-16 11:51:09 +0200897 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200898 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200899 struct lys_node_rpc_inout *inout = (struct lys_node_rpc_inout *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +0200900
Radek Krejci76b07902015-10-09 09:11:25 +0200901 ly_print(out, "%*s%s {\n", LEVEL, INDENT, (inout->nodetype == LYS_INPUT ? "input" : "output"));
Michal Vaskof4d3d742015-06-16 11:51:09 +0200902
903 level++;
904 for (i = 0; i < inout->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200905 yang_print_typedef(out, level, node->module, &inout->tpdf[i]);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200906 }
907
Radek Krejci1d82ef62015-08-07 14:44:40 +0200908 LY_TREE_FOR(node->child, sub) {
Michal Vasko15b48702015-07-07 15:49:34 +0200909 /* augment */
Radek Krejci1d82ef62015-08-07 14:44:40 +0200910 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200911 continue;
912 }
Radek Krejci76b07902015-10-09 09:11:25 +0200913 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200914 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
915 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200916 }
917
918 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200919 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200920}
921
922static void
Radek Krejci76b07902015-10-09 09:11:25 +0200923yang_print_rpc(struct lyout *out, int level, struct lys_node *node)
Michal Vaskof4d3d742015-06-16 11:51:09 +0200924{
925 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200926 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200927 struct lys_node_rpc *rpc = (struct lys_node_rpc *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +0200928
Radek Krejci76b07902015-10-09 09:11:25 +0200929 ly_print(out, "%*srpc %s {\n", LEVEL, INDENT, node->name);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200930
931 level++;
Radek Krejci76b07902015-10-09 09:11:25 +0200932 yang_print_snode_common(out, level, node);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200933
Michal Vasko4773b762015-07-07 12:15:10 +0200934 for (i = 0; i < rpc->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200935 yang_print_iffeature(out, level, node->module, rpc->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200936 }
937
Michal Vaskof4d3d742015-06-16 11:51:09 +0200938 for (i = 0; i < rpc->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200939 yang_print_typedef(out, level, node->module, &rpc->tpdf[i]);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200940 }
941
Radek Krejci1d82ef62015-08-07 14:44:40 +0200942 LY_TREE_FOR(node->child, sub) {
Radek Krejci76b07902015-10-09 09:11:25 +0200943 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200944 LYS_GROUPING | LYS_INPUT | LYS_OUTPUT);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200945 }
946
947 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200948 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200949}
950
951static void
Radek Krejci76b07902015-10-09 09:11:25 +0200952yang_print_notif(struct lyout *out, int level, struct lys_node *node)
Michal Vasko7690bc12015-06-16 12:26:05 +0200953{
Michal Vaskof4d3d742015-06-16 11:51:09 +0200954 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200955 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200956 struct lys_node_notif *notif = (struct lys_node_notif *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +0200957
Radek Krejci76b07902015-10-09 09:11:25 +0200958 ly_print(out, "%*snotification %s {\n", LEVEL, INDENT, node->name);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200959
960 level++;
Radek Krejci76b07902015-10-09 09:11:25 +0200961 yang_print_snode_common(out, level, node);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200962
Michal Vasko4773b762015-07-07 12:15:10 +0200963 for (i = 0; i < notif->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200964 yang_print_iffeature(out, level, node->module, notif->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200965 }
966
Michal Vaskof4d3d742015-06-16 11:51:09 +0200967 for (i = 0; i < notif->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200968 yang_print_typedef(out, level, node->module, &notif->tpdf[i]);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200969 }
970
Radek Krejci1d82ef62015-08-07 14:44:40 +0200971 LY_TREE_FOR(node->child, sub) {
Michal Vasko15b48702015-07-07 15:49:34 +0200972 /* augment */
Radek Krejci1d82ef62015-08-07 14:44:40 +0200973 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200974 continue;
975 }
Radek Krejci76b07902015-10-09 09:11:25 +0200976 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200977 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
978 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200979 }
980
981 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200982 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200983}
984
985static void
Radek Krejci76b07902015-10-09 09:11:25 +0200986yang_print_snode(struct lyout *out, int level, struct lys_node *node, int mask)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200987{
Radek Krejci1d82ef62015-08-07 14:44:40 +0200988 switch (node->nodetype & mask) {
Radek Krejci76512572015-08-04 09:47:08 +0200989 case LYS_CONTAINER:
Radek Krejci76b07902015-10-09 09:11:25 +0200990 yang_print_container(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200991 break;
Radek Krejci76512572015-08-04 09:47:08 +0200992 case LYS_CHOICE:
Radek Krejci76b07902015-10-09 09:11:25 +0200993 yang_print_choice(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200994 break;
Radek Krejci76512572015-08-04 09:47:08 +0200995 case LYS_LEAF:
Radek Krejci76b07902015-10-09 09:11:25 +0200996 yang_print_leaf(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200997 break;
Radek Krejci76512572015-08-04 09:47:08 +0200998 case LYS_LEAFLIST:
Radek Krejci76b07902015-10-09 09:11:25 +0200999 yang_print_leaflist(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001000 break;
Radek Krejci76512572015-08-04 09:47:08 +02001001 case LYS_LIST:
Radek Krejci76b07902015-10-09 09:11:25 +02001002 yang_print_list(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001003 break;
Radek Krejci76512572015-08-04 09:47:08 +02001004 case LYS_USES:
Radek Krejci76b07902015-10-09 09:11:25 +02001005 yang_print_uses(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001006 break;
Radek Krejci76512572015-08-04 09:47:08 +02001007 case LYS_GROUPING:
Radek Krejci76b07902015-10-09 09:11:25 +02001008 yang_print_grouping(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001009 break;
Radek Krejci76512572015-08-04 09:47:08 +02001010 case LYS_ANYXML:
Radek Krejci76b07902015-10-09 09:11:25 +02001011 yang_print_anyxml(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001012 break;
Radek Krejci76512572015-08-04 09:47:08 +02001013 case LYS_CASE:
Radek Krejci76b07902015-10-09 09:11:25 +02001014 yang_print_case(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001015 break;
Radek Krejci76512572015-08-04 09:47:08 +02001016 case LYS_INPUT:
1017 case LYS_OUTPUT:
Radek Krejci76b07902015-10-09 09:11:25 +02001018 yang_print_input_output(out, level, node);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001019 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001020 default:
1021 break;
1022 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001023}
1024
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001025int
Radek Krejci76b07902015-10-09 09:11:25 +02001026yang_print_model(struct lyout *out, struct lys_module *module)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001027{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001028 unsigned int i;
1029 int level = 0;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001030#define LEVEL (level*2)
1031
Radek Krejci1d82ef62015-08-07 14:44:40 +02001032 struct lys_node *node;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001033
Michal Vasko116172e2015-07-07 11:54:37 +02001034 if (module->type) {
Radek Krejci76b07902015-10-09 09:11:25 +02001035 ly_print(out, "submodule %s {%s\n", module->name, (module->deviated ? " // DEVIATED" : ""));
Michal Vasko116172e2015-07-07 11:54:37 +02001036 level++;
Radek Krejci76b07902015-10-09 09:11:25 +02001037 ly_print(out, "%*sbelongs-to %s {\n", LEVEL, INDENT, ((struct lys_submodule *)module)->belongsto->name);
Michal Vasko116172e2015-07-07 11:54:37 +02001038 level++;
Radek Krejci76b07902015-10-09 09:11:25 +02001039 ly_print(out, "%*sprefix \"%s\";\n", LEVEL, INDENT, module->prefix);
Michal Vasko116172e2015-07-07 11:54:37 +02001040 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001041 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko116172e2015-07-07 11:54:37 +02001042 } else {
Radek Krejci76b07902015-10-09 09:11:25 +02001043 ly_print(out, "module %s {%s\n", module->name, (module->deviated ? " // DEVIATED" : ""));
Michal Vasko116172e2015-07-07 11:54:37 +02001044 level++;
Radek Krejci76b07902015-10-09 09:11:25 +02001045 ly_print(out, "%*snamespace \"%s\";\n", LEVEL, INDENT, module->ns);
1046 ly_print(out, "%*sprefix \"%s\";\n", LEVEL, INDENT, module->prefix);
Michal Vasko116172e2015-07-07 11:54:37 +02001047 }
Radek Krejcib0594bf2015-05-21 23:51:27 +02001048
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001049 if (module->version) {
Radek Krejci76b07902015-10-09 09:11:25 +02001050 ly_print(out, "%*syang-version %s;\n", LEVEL, INDENT, module->version == 1 ? "1" : "1.1");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001051 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001052
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001053 for (i = 0; i < module->imp_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +02001054 ly_print(out, "%*simport \"%s\" {\n", LEVEL, INDENT, module->imp[i].module->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001055 level++;
Radek Krejci76b07902015-10-09 09:11:25 +02001056 ly_print(out, "%*sprefix \"%s\";\n", LEVEL, INDENT, module->imp[i].prefix);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001057 if (module->imp[i].rev[0]) {
Radek Krejci76b07902015-10-09 09:11:25 +02001058 yang_print_text(out, level, "revision-date", module->imp[i].rev);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001059 }
1060 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001061 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001062 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001063
Michal Vaskoe0af1e22015-07-07 14:02:02 +02001064 for (i = 0; i < module->deviation_size; ++i) {
Radek Krejci76b07902015-10-09 09:11:25 +02001065 yang_print_deviation(out, level, module, &module->deviation[i]);
Michal Vaskoe0af1e22015-07-07 14:02:02 +02001066 }
1067
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001068 for (i = 0; i < module->inc_size; i++) {
1069 if (module->inc[i].rev[0]) {
Radek Krejci76b07902015-10-09 09:11:25 +02001070 ly_print(out, "%*sinclude \"%s\" {\n", LEVEL, INDENT, module->inc[i].submodule->name);
1071 yang_print_text(out, level + 1, "revision-date", module->inc[i].rev);
1072 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001073 } else {
Radek Krejci76b07902015-10-09 09:11:25 +02001074 ly_print(out, "%*sinclude \"%s\";\n", LEVEL, INDENT, module->inc[i].submodule->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001075 }
1076 }
Radek Krejciefaeba32015-05-27 14:30:57 +02001077
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001078 if (module->org) {
Radek Krejci76b07902015-10-09 09:11:25 +02001079 yang_print_text(out, level, "organization", module->org);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001080 }
1081 if (module->contact) {
Radek Krejci76b07902015-10-09 09:11:25 +02001082 yang_print_text(out, level, "contact", module->contact);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001083 }
1084 if (module->dsc) {
Radek Krejci76b07902015-10-09 09:11:25 +02001085 yang_print_text(out, level, "description", module->dsc);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001086 }
1087 if (module->ref) {
Radek Krejci76b07902015-10-09 09:11:25 +02001088 yang_print_text(out, level, "reference", module->ref);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001089 }
1090 for (i = 0; i < module->rev_size; i++) {
1091 if (module->rev[i].dsc || module->rev[i].ref) {
Radek Krejci76b07902015-10-09 09:11:25 +02001092 ly_print(out, "%*srevision \"%s\" {\n", LEVEL, INDENT, module->rev[i].date);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001093 level++;
1094 if (module->rev[i].dsc) {
Radek Krejci76b07902015-10-09 09:11:25 +02001095 yang_print_text(out, level, "description", module->rev[i].dsc);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001096 }
1097 if (module->rev[i].ref) {
Radek Krejci76b07902015-10-09 09:11:25 +02001098 yang_print_text(out, level, "reference", module->rev[i].ref);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001099 }
1100 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001101 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001102 } else {
Radek Krejci76b07902015-10-09 09:11:25 +02001103 yang_print_text(out, level, "revision", module->rev[i].date);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001104 }
1105 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001106
Michal Vasko30f6e912015-07-07 12:24:27 +02001107 for (i = 0; i < module->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +02001108 yang_print_feature(out, level, &module->features[i]);
Michal Vasko30f6e912015-07-07 12:24:27 +02001109 }
1110
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001111 for (i = 0; i < module->ident_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +02001112 yang_print_identity(out, level, &module->ident[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001113 }
Radek Krejci6793db02015-05-22 17:49:54 +02001114
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001115 for (i = 0; i < module->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +02001116 yang_print_typedef(out, level, module, &module->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001117 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001118
Radek Krejci1d82ef62015-08-07 14:44:40 +02001119 LY_TREE_FOR(module->data, node) {
Radek Krejci92720552015-10-05 15:28:27 +02001120 switch(node->nodetype) {
1121 case LYS_RPC:
Radek Krejcibac81762015-10-09 10:19:52 +02001122 yang_print_rpc(out, level, node);
Radek Krejci92720552015-10-05 15:28:27 +02001123 break;
1124 case LYS_NOTIF:
Radek Krejcibac81762015-10-09 10:19:52 +02001125 yang_print_notif(out, level, node);
Radek Krejci92720552015-10-05 15:28:27 +02001126 break;
1127 default:
Radek Krejcibac81762015-10-09 10:19:52 +02001128 yang_print_snode(out, level, node,
Radek Krejci92720552015-10-05 15:28:27 +02001129 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
1130 LYS_USES | LYS_GROUPING | LYS_ANYXML);
1131 break;
1132 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001133 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001134
Michal Vasko6f25f212015-07-07 15:42:07 +02001135 for (i = 0; i < module->augment_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +02001136 yang_print_augment(out, level, module, &module->augment[i]);
Michal Vasko6f25f212015-07-07 15:42:07 +02001137 }
1138
Radek Krejci76b07902015-10-09 09:11:25 +02001139 ly_print(out, "}\n");
Radek Krejcida04f4a2015-05-21 12:54:09 +02001140
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001141 return EXIT_SUCCESS;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001142#undef LEVEL
1143}