blob: 6e413ff4cb18af857c351c98be0e8bf5b4eac062 [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;
Michal Vasko0fb82c62015-10-20 13:41:53 +0200239 const char *str;
Radek Krejci25d782a2015-05-22 15:03:23 +0200240
Michal Vasko0fb82c62015-10-20 13:41:53 +0200241 if (type->module_name) {
242 str = transform_expr_json2xml(module, type->module_name, NULL, NULL, NULL);
243 ly_print(out, "%*stype %s:%s {\n", LEVEL, INDENT, str, type->der->name);
244 lydict_remove(module->ctx, str);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200245 } else {
Radek Krejci76b07902015-10-09 09:11:25 +0200246 ly_print(out, "%*stype %s {\n", LEVEL, INDENT, type->der->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200247 }
248 level++;
249 switch (type->base) {
250 case LY_TYPE_BINARY:
251 if (type->info.binary.length != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200252 ly_print(out, "%*slength \"%s\" {\n", LEVEL, INDENT, type->info.binary.length->expr);
253 yang_print_restr(out, level + 1, type->info.binary.length);
254 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200255 }
256 break;
257 case LY_TYPE_BITS:
258 for (i = 0; i < type->info.bits.count; ++i) {
Radek Krejci76b07902015-10-09 09:11:25 +0200259 ly_print(out, "%*sbit %s {\n", LEVEL, INDENT, type->info.bits.bit[i].name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200260 level++;
Radek Krejci76b07902015-10-09 09:11:25 +0200261 yang_print_snode_common(out, level, (struct lys_node *)&type->info.bits.bit[i]);
262 ly_print(out, "%*sposition %u;\n", LEVEL, INDENT, type->info.bits.bit[i].pos);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200263 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200264 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200265 }
266 break;
267 case LY_TYPE_DEC64:
Radek Krejci76b07902015-10-09 09:11:25 +0200268 ly_print(out, "%*sfraction-digits %d;\n", LEVEL, INDENT, type->info.dec64.dig);
Michal Vaskoea505ee2015-07-07 14:01:00 +0200269 if (type->info.dec64.range != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200270 ly_print(out, "%*srange \"%s\" {\n", LEVEL, INDENT, type->info.dec64.range->expr);
271 yang_print_restr(out, level + 1, type->info.dec64.range);
272 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskoea505ee2015-07-07 14:01:00 +0200273 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200274 break;
275 case LY_TYPE_ENUM:
276 for (i = 0; i < type->info.enums.count; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200277 ly_print(out, "%*senum %s {\n", LEVEL, INDENT, type->info.enums.enm[i].name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200278 level++;
Radek Krejci76b07902015-10-09 09:11:25 +0200279 yang_print_snode_common(out, level, (struct lys_node *)&type->info.enums.enm[i]);
280 ly_print(out, "%*svalue %d;\n", LEVEL, INDENT, type->info.enums.enm[i].value);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200281 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200282 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200283 }
284 break;
285 case LY_TYPE_IDENT:
286 if (module == type->info.ident.ref->module) {
Radek Krejci76b07902015-10-09 09:11:25 +0200287 ly_print(out, "%*sbase %s;\n", LEVEL, INDENT, type->info.ident.ref->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200288 } else {
Radek Krejci76b07902015-10-09 09:11:25 +0200289 ly_print(out, "%*sbase %s:%s;\n", LEVEL, INDENT, type->info.ident.ref->module->prefix,
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200290 type->info.ident.ref->name);
291 }
292 break;
293 case LY_TYPE_INST:
Radek Krejciaf351422015-06-19 14:49:38 +0200294 if (type->info.inst.req == 1) {
Radek Krejci76b07902015-10-09 09:11:25 +0200295 ly_print(out, "%*srequire-instance \"true\";\n", LEVEL, INDENT);
Radek Krejciaf351422015-06-19 14:49:38 +0200296 } else if (type->info.inst.req == -1) {
Radek Krejci76b07902015-10-09 09:11:25 +0200297 ly_print(out, "%*srequire-instance \"false\";\n", LEVEL, INDENT);
Radek Krejciaf351422015-06-19 14:49:38 +0200298 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200299 break;
300 case LY_TYPE_INT8:
301 case LY_TYPE_INT16:
302 case LY_TYPE_INT32:
303 case LY_TYPE_INT64:
304 case LY_TYPE_UINT8:
305 case LY_TYPE_UINT16:
306 case LY_TYPE_UINT32:
307 case LY_TYPE_UINT64:
Radek Krejcif2860132015-06-20 12:37:20 +0200308 if (type->info.num.range != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200309 ly_print(out, "%*srange \"%s\" {\n", LEVEL, INDENT, type->info.num.range->expr);
310 yang_print_restr(out, level + 1, type->info.num.range);
311 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcif2860132015-06-20 12:37:20 +0200312 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200313 break;
314 case LY_TYPE_LEAFREF:
Michal Vasko514c4f32015-10-20 13:43:25 +0200315 str = transform_expr_json2xml(module, type->info.lref.path, NULL, NULL, NULL);
316 ly_print(out, "%*spath \"%s\";\n", LEVEL, INDENT, str);
317 lydict_remove(module->ctx, str);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200318 break;
319 case LY_TYPE_STRING:
Radek Krejci5fbc9162015-06-19 14:11:11 +0200320 if (type->info.str.length) {
Radek Krejci76b07902015-10-09 09:11:25 +0200321 ly_print(out, "%*slength \"%s\" {\n", LEVEL, INDENT, type->info.str.length->expr);
322 yang_print_restr(out, level + 1, type->info.str.length);
323 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci061bd522015-06-19 13:45:16 +0200324 }
Radek Krejci5fbc9162015-06-19 14:11:11 +0200325 for (i = 0; i < type->info.str.pat_count; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200326 ly_print(out, "%*spattern \"%s\" {\n", LEVEL, INDENT, type->info.str.patterns[i].expr);
327 yang_print_restr(out, level + 1, &type->info.str.patterns[i]);
328 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci5fbc9162015-06-19 14:11:11 +0200329 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200330 break;
331 case LY_TYPE_UNION:
332 for (i = 0; i < type->info.uni.count; ++i) {
Radek Krejci76b07902015-10-09 09:11:25 +0200333 yang_print_type(out, level, module, &type->info.uni.types[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200334 }
335 break;
336 default:
337 /* other types do not have substatements */
338 break;
339 }
340 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200341 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200342}
343
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200344static void
Michal Vaskof9893382015-10-09 14:03:04 +0200345yang_print_must(struct lyout *out, int level, struct lys_module *module, struct lys_restr *must)
Michal Vasko7f976ee2015-06-09 13:55:41 +0200346{
Michal Vaskof9893382015-10-09 14:03:04 +0200347 const char *xml_expr;
348
349 xml_expr = transform_expr_json2xml(module, must->expr, NULL, NULL, NULL);
350 if (!xml_expr) {
351 ly_print(out, "(!error!)");
352 return;
353 }
354
355 ly_print(out, "%*smust \"%s\" {\n", LEVEL, INDENT, xml_expr);
356 lydict_remove(module->ctx, xml_expr);
357
Radek Krejci76b07902015-10-09 09:11:25 +0200358 yang_print_restr(out, level + 1, must);
359 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko7f976ee2015-06-09 13:55:41 +0200360}
361
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200362static void
Radek Krejci76b07902015-10-09 09:11:25 +0200363yang_print_unique(struct lyout *out, int level, struct lys_unique *uniq)
Michal Vasko1ef07972015-07-07 14:01:35 +0200364{
365 int i;
366
Radek Krejci76b07902015-10-09 09:11:25 +0200367 ly_print(out, "%*sunique \"", LEVEL, INDENT);
Michal Vasko1ef07972015-07-07 14:01:35 +0200368 for (i = 0; i < uniq->leafs_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200369 ly_print(out, "%s%s", uniq->leafs[i]->name, i + 1 < uniq->leafs_size ? " " : "");
Michal Vasko1ef07972015-07-07 14:01:35 +0200370 }
Radek Krejci76b07902015-10-09 09:11:25 +0200371 ly_print(out, "\";\n");
Michal Vasko1ef07972015-07-07 14:01:35 +0200372}
373
374static void
Michal Vaskof9893382015-10-09 14:03:04 +0200375yang_print_refine(struct lyout *out, int level, struct lys_module *module, struct lys_refine *refine)
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200376{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200377 int i;
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200378
Radek Krejci76b07902015-10-09 09:11:25 +0200379 ly_print(out, "%*srefine \"%s\" {\n", LEVEL, INDENT, refine->target_name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200380 level++;
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200381
Radek Krejci1574a8d2015-08-03 14:16:52 +0200382 if (refine->flags & LYS_CONFIG_W) {
Radek Krejci76b07902015-10-09 09:11:25 +0200383 ly_print(out, "%*sconfig \"true\";\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200384 } else if (refine->flags & LYS_CONFIG_R) {
Radek Krejci76b07902015-10-09 09:11:25 +0200385 ly_print(out, "%*sconfig \"false\";\n", LEVEL, INDENT);
Radek Krejci6a113852015-07-03 16:04:20 +0200386 }
387
Radek Krejci1574a8d2015-08-03 14:16:52 +0200388 if (refine->flags & LYS_MAND_TRUE) {
Radek Krejci76b07902015-10-09 09:11:25 +0200389 ly_print(out, "%*smandatory \"true\";\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200390 } else if (refine->flags & LYS_MAND_FALSE) {
Radek Krejci76b07902015-10-09 09:11:25 +0200391 ly_print(out, "%*smandatory \"false\";\n", LEVEL, INDENT);
Radek Krejci6a113852015-07-03 16:04:20 +0200392 }
393
Radek Krejci76b07902015-10-09 09:11:25 +0200394 yang_print_snode_common(out, level, (struct lys_node *)refine);
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200395
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200396 for (i = 0; i < refine->must_size; ++i) {
Michal Vaskof9893382015-10-09 14:03:04 +0200397 yang_print_must(out, level, module, &refine->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200398 }
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200399
Radek Krejci76512572015-08-04 09:47:08 +0200400 if (refine->target_type & (LYS_LEAF | LYS_CHOICE)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200401 if (refine->mod.dflt != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200402 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, refine->mod.dflt);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200403 }
Radek Krejci76512572015-08-04 09:47:08 +0200404 } else if (refine->target_type == LYS_CONTAINER) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200405 if (refine->mod.presence != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200406 yang_print_text(out, level, "presence", refine->mod.presence);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200407 }
Radek Krejci76512572015-08-04 09:47:08 +0200408 } else if (refine->target_type & (LYS_LIST | LYS_LEAFLIST)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200409 if (refine->mod.list.min > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200410 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, refine->mod.list.min);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200411 }
412 if (refine->mod.list.max > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200413 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, refine->mod.list.max);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200414 }
415 }
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200416
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200417 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200418 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200419}
420
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200421static void
Radek Krejci76b07902015-10-09 09:11:25 +0200422yang_print_deviation(struct lyout *out, int level, struct lys_module *module, struct lys_deviation *deviation)
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200423{
424 int i, j;
425
Radek Krejci76b07902015-10-09 09:11:25 +0200426 ly_print(out, "%*sdeviation \"%s\" {\n", LEVEL, INDENT, deviation->target_name);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200427 level++;
428
429 if (deviation->dsc) {
Radek Krejci76b07902015-10-09 09:11:25 +0200430 yang_print_text(out, level, "description", deviation->dsc);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200431 }
432 if (deviation->ref) {
Radek Krejci76b07902015-10-09 09:11:25 +0200433 yang_print_text(out, level, "reference", deviation->ref);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200434 }
435
436 for (i = 0; i < deviation->deviate_size; ++i) {
Radek Krejci76b07902015-10-09 09:11:25 +0200437 ly_print(out, "%*sdeviate ", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200438 if (deviation->deviate[i].mod == LY_DEVIATE_NO) {
Radek Krejci76b07902015-10-09 09:11:25 +0200439 ly_print(out, "not-supported {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200440 } else if (deviation->deviate[i].mod == LY_DEVIATE_ADD) {
Radek Krejci76b07902015-10-09 09:11:25 +0200441 ly_print(out, "add {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200442 } else if (deviation->deviate[i].mod == LY_DEVIATE_RPL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200443 ly_print(out, "replace {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200444 } else if (deviation->deviate[i].mod == LY_DEVIATE_DEL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200445 ly_print(out, "delete {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200446 }
447 level++;
448
Radek Krejci1574a8d2015-08-03 14:16:52 +0200449 if (deviation->deviate[i].flags & LYS_CONFIG_W) {
Radek Krejci76b07902015-10-09 09:11:25 +0200450 ly_print(out, "%*sconfig \"true\";\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200451 } else if (deviation->deviate[i].flags & LYS_CONFIG_R) {
Radek Krejci76b07902015-10-09 09:11:25 +0200452 ly_print(out, "%*sconfig \"false\";\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200453 }
454
Radek Krejci1574a8d2015-08-03 14:16:52 +0200455 if (deviation->deviate[i].flags & LYS_MAND_TRUE) {
Radek Krejci76b07902015-10-09 09:11:25 +0200456 ly_print(out, "%*smandatory \"true\";\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200457 } else if (deviation->deviate[i].flags & LYS_MAND_FALSE) {
Radek Krejci76b07902015-10-09 09:11:25 +0200458 ly_print(out, "%*smandatory \"false\";\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200459 }
460
461 if (deviation->deviate[i].dflt) {
Radek Krejci76b07902015-10-09 09:11:25 +0200462 ly_print(out, "%*sdefault %s;\n", LEVEL, INDENT, deviation->deviate[i].dflt);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200463 }
464
465 if (deviation->deviate[i].min) {
Radek Krejci76b07902015-10-09 09:11:25 +0200466 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, deviation->deviate[i].min);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200467 }
468 if (deviation->deviate[i].max) {
Radek Krejci76b07902015-10-09 09:11:25 +0200469 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, deviation->deviate[i].max);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200470 }
471
472 for (j = 0; j < deviation->deviate[i].must_size; ++j) {
Michal Vaskof9893382015-10-09 14:03:04 +0200473 yang_print_must(out, level, module, &deviation->deviate[i].must[j]);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200474 }
475
476 for (j = 0; j < deviation->deviate[i].unique_size; ++j) {
Radek Krejci76b07902015-10-09 09:11:25 +0200477 yang_print_unique(out, level, &deviation->deviate[i].unique[j]);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200478 }
479
480 if (deviation->deviate[i].type) {
Radek Krejci76b07902015-10-09 09:11:25 +0200481 yang_print_type(out, level, module, deviation->deviate[i].type);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200482 }
483
484 if (deviation->deviate[i].units) {
Radek Krejci76b07902015-10-09 09:11:25 +0200485 ly_print(out, "%*sunits %s;\n", LEVEL, INDENT, deviation->deviate[i].units);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200486 }
487
488 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200489 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200490 }
491
492 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200493 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200494}
495
496static void
Radek Krejci76b07902015-10-09 09:11:25 +0200497yang_print_augment(struct lyout *out, int level, struct lys_module *module, struct lys_node_augment *augment)
Michal Vasko6f25f212015-07-07 15:42:07 +0200498{
499 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200500 struct lys_node *sub;
Michal Vasko6f25f212015-07-07 15:42:07 +0200501
Radek Krejci76b07902015-10-09 09:11:25 +0200502 ly_print(out, "%*saugment \"%s\" {\n", LEVEL, INDENT, augment->target_name);
Michal Vasko6f25f212015-07-07 15:42:07 +0200503 level++;
504
Radek Krejci76b07902015-10-09 09:11:25 +0200505 yang_print_nacmext(out, level, (struct lys_node *)augment, module);
506 yang_print_snode_common(out, level, (struct lys_node *)augment);
Michal Vasko6f25f212015-07-07 15:42:07 +0200507
508 for (i = 0; i < augment->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200509 yang_print_iffeature(out, level, module, augment->features[i]);
Michal Vasko6f25f212015-07-07 15:42:07 +0200510 }
511
512 if (augment->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200513 yang_print_when(out, level, module, augment->when);
Michal Vasko6f25f212015-07-07 15:42:07 +0200514 }
515
516 LY_TREE_FOR(augment->child, sub) {
Radek Krejci76b07902015-10-09 09:11:25 +0200517 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200518 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
519 LYS_USES | LYS_ANYXML | LYS_CASE);
Michal Vasko6f25f212015-07-07 15:42:07 +0200520 }
521
522 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200523 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko6f25f212015-07-07 15:42:07 +0200524}
525
526static void
Radek Krejci76b07902015-10-09 09:11:25 +0200527yang_print_typedef(struct lyout *out, int level, struct lys_module *module, struct lys_tpdf *tpdf)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200528{
Radek Krejci76b07902015-10-09 09:11:25 +0200529 ly_print(out, "%*stypedef %s {\n", LEVEL, INDENT, tpdf->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200530 level++;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200531
Radek Krejci76b07902015-10-09 09:11:25 +0200532 yang_print_snode_common(out, level, (struct lys_node *)tpdf);
533 yang_print_type(out, level, module, &tpdf->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200534 if (tpdf->units != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200535 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, tpdf->units);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200536 }
537 if (tpdf->dflt != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200538 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, tpdf->dflt);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200539 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200540
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200541 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200542 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200543}
544
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200545static void
Radek Krejci76b07902015-10-09 09:11:25 +0200546yang_print_identity(struct lyout *out, int level, struct lys_ident *ident)
Radek Krejci6793db02015-05-22 17:49:54 +0200547{
Radek Krejci76b07902015-10-09 09:11:25 +0200548 ly_print(out, "%*sidentity %s {\n", LEVEL, INDENT, ident->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200549 level++;
Radek Krejci6793db02015-05-22 17:49:54 +0200550
Radek Krejci76b07902015-10-09 09:11:25 +0200551 yang_print_snode_common(out, level, (struct lys_node *)ident);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200552 if (ident->base) {
Radek Krejci76b07902015-10-09 09:11:25 +0200553 ly_print(out, "%*sbase ", LEVEL, INDENT);
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200554 if ((ident->module != ident->base->module) && !ident->base->module->type) {
Radek Krejci76b07902015-10-09 09:11:25 +0200555 ly_print(out, "%s:", get_module_import_prefix(ident->module, ident->base->module));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200556 }
Radek Krejci76b07902015-10-09 09:11:25 +0200557 ly_print(out, "%s;\n", ident->base->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200558 }
Radek Krejci6793db02015-05-22 17:49:54 +0200559
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200560 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200561 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6793db02015-05-22 17:49:54 +0200562
563}
564
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200565static void
Radek Krejci76b07902015-10-09 09:11:25 +0200566yang_print_container(struct lyout *out, int level, struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200567{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200568 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200569 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200570 struct lys_node_container *cont = (struct lys_node_container *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200571
Radek Krejci76b07902015-10-09 09:11:25 +0200572 ly_print(out, "%*scontainer %s {\n", LEVEL, INDENT, node->name);
Michal Vasko7f976ee2015-06-09 13:55:41 +0200573
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200574 level++;
Radek Krejci6a113852015-07-03 16:04:20 +0200575
Radek Krejci76b07902015-10-09 09:11:25 +0200576 yang_print_nacmext(out, level, node, node->module);
Radek Krejci6a113852015-07-03 16:04:20 +0200577
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200578 if (cont->presence != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200579 yang_print_text(out, level, "presence", cont->presence);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200580 }
Michal Vasko7f976ee2015-06-09 13:55:41 +0200581
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200582 for (i = 0; i < cont->must_size; i++) {
Michal Vaskof9893382015-10-09 14:03:04 +0200583 yang_print_must(out, level, node->module, &cont->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200584 }
Michal Vasko7f976ee2015-06-09 13:55:41 +0200585
Radek Krejci76b07902015-10-09 09:11:25 +0200586 yang_print_snode_common2(out, level, node);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200587
Michal Vasko4773b762015-07-07 12:15:10 +0200588 for (i = 0; i < cont->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200589 yang_print_iffeature(out, level, node->module, cont->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200590 }
591
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200592 for (i = 0; i < cont->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200593 yang_print_typedef(out, level, node->module, &cont->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200594 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200595
Michal Vasko1f0428a2015-07-07 14:55:04 +0200596 if (cont->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200597 yang_print_when(out, level, node->module, cont->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200598 }
599
Radek Krejci1d82ef62015-08-07 14:44:40 +0200600 LY_TREE_FOR(node->child, sub) {
Michal Vasko15b48702015-07-07 15:49:34 +0200601 /* augment */
Radek Krejci1d82ef62015-08-07 14:44:40 +0200602 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200603 continue;
604 }
Radek Krejci76b07902015-10-09 09:11:25 +0200605 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200606 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
607 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200608 }
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200609
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200610 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200611 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200612}
613
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200614static void
Radek Krejci76b07902015-10-09 09:11:25 +0200615yang_print_case(struct lyout *out, int level, struct lys_node *node)
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200616{
Michal Vasko4773b762015-07-07 12:15:10 +0200617 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200618 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200619 struct lys_node_case *cas = (struct lys_node_case *)node;
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200620
Radek Krejci76b07902015-10-09 09:11:25 +0200621 ly_print(out, "%*scase %s {\n", LEVEL, INDENT, cas->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200622 level++;
Radek Krejci76b07902015-10-09 09:11:25 +0200623 yang_print_nacmext(out, level, node, node->module);
624 yang_print_snode_common2(out, level, node);
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200625
Michal Vasko4773b762015-07-07 12:15:10 +0200626 for (i = 0; i < cas->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200627 yang_print_iffeature(out, level, node->module, cas->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200628 }
629
Michal Vasko1f0428a2015-07-07 14:55:04 +0200630 if (cas->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200631 yang_print_when(out, level, node->module, cas->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200632 }
633
Radek Krejci1d82ef62015-08-07 14:44:40 +0200634 LY_TREE_FOR(node->child, sub) {
Michal Vasko15b48702015-07-07 15:49:34 +0200635 /* augment */
Radek Krejci1d82ef62015-08-07 14:44:40 +0200636 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200637 continue;
638 }
Radek Krejci76b07902015-10-09 09:11:25 +0200639 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200640 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
641 LYS_USES | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200642 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200643
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200644 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200645 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200646}
647
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200648static void
Radek Krejci76b07902015-10-09 09:11:25 +0200649yang_print_choice(struct lyout *out, int level, struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200650{
Michal Vasko4773b762015-07-07 12:15:10 +0200651 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200652 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200653 struct lys_node_choice *choice = (struct lys_node_choice *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200654
Radek Krejci76b07902015-10-09 09:11:25 +0200655 ly_print(out, "%*schoice %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +0200656
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200657 level++;
Radek Krejci76b07902015-10-09 09:11:25 +0200658 yang_print_nacmext(out, level, node, node->module);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200659 if (choice->dflt != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200660 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, choice->dflt->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200661 }
Michal Vasko16083662015-06-09 14:00:45 +0200662
Radek Krejci76b07902015-10-09 09:11:25 +0200663 yang_print_snode_common2(out, level, node);
Michal Vasko4773b762015-07-07 12:15:10 +0200664
665 for (i = 0; i < choice->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200666 yang_print_iffeature(out, level, node->module, choice->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200667 }
668
Michal Vasko1f0428a2015-07-07 14:55:04 +0200669 if (choice->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200670 yang_print_when(out, level, node->module, choice->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200671 }
672
Radek Krejci1d82ef62015-08-07 14:44:40 +0200673 LY_TREE_FOR(node->child, sub) {
Michal Vasko15b48702015-07-07 15:49:34 +0200674 /* augment */
Radek Krejci1d82ef62015-08-07 14:44:40 +0200675 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200676 continue;
677 }
Radek Krejci76b07902015-10-09 09:11:25 +0200678 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200679 LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYXML | LYS_CASE);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200680 }
681 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200682 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200683}
684
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200685static void
Radek Krejci76b07902015-10-09 09:11:25 +0200686yang_print_leaf(struct lyout *out, int level, struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200687{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200688 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200689 struct lys_node_leaf *leaf = (struct lys_node_leaf *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200690
Radek Krejci76b07902015-10-09 09:11:25 +0200691 ly_print(out, "%*sleaf %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +0200692
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200693 level++;
Radek Krejci76b07902015-10-09 09:11:25 +0200694 yang_print_nacmext(out, level, node, node->module);
695 yang_print_snode_common2(out, level, node);
Michal Vasko4773b762015-07-07 12:15:10 +0200696 for (i = 0; i < leaf->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200697 yang_print_iffeature(out, level, node->module, leaf->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200698 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200699 for (i = 0; i < leaf->must_size; i++) {
Michal Vaskof9893382015-10-09 14:03:04 +0200700 yang_print_must(out, level, node->module, &leaf->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200701 }
Michal Vasko1f0428a2015-07-07 14:55:04 +0200702 if (leaf->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200703 yang_print_when(out, level, node->module, leaf->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200704 }
Radek Krejci76b07902015-10-09 09:11:25 +0200705 yang_print_type(out, level, node->module, &leaf->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200706 if (leaf->units != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200707 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, leaf->units);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200708 }
709 if (leaf->dflt != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200710 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, leaf->dflt);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200711 }
712 level--;
Michal Vasko16083662015-06-09 14:00:45 +0200713
Radek Krejci76b07902015-10-09 09:11:25 +0200714 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko16083662015-06-09 14:00:45 +0200715}
716
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200717static void
Radek Krejci76b07902015-10-09 09:11:25 +0200718yang_print_anyxml(struct lyout *out, int level, struct lys_node *node)
Michal Vasko16083662015-06-09 14:00:45 +0200719{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200720 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200721 struct lys_node_anyxml *anyxml = (struct lys_node_anyxml *)node;
Michal Vasko16083662015-06-09 14:00:45 +0200722
Radek Krejci76b07902015-10-09 09:11:25 +0200723 ly_print(out, "%*sanyxml %s {\n", LEVEL, INDENT, anyxml->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200724 level++;
Radek Krejci76b07902015-10-09 09:11:25 +0200725 yang_print_nacmext(out, level, node, node->module);
726 yang_print_snode_common2(out, level, node);
Michal Vasko4773b762015-07-07 12:15:10 +0200727 for (i = 0; i < anyxml->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200728 yang_print_iffeature(out, level, node->module, anyxml->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200729 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200730 for (i = 0; i < anyxml->must_size; i++) {
Michal Vaskof9893382015-10-09 14:03:04 +0200731 yang_print_must(out, level, node->module, &anyxml->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200732 }
Michal Vasko1f0428a2015-07-07 14:55:04 +0200733 if (anyxml->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200734 yang_print_when(out, level, node->module, anyxml->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200735 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200736 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200737 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200738}
739
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200740static void
Radek Krejci76b07902015-10-09 09:11:25 +0200741yang_print_leaflist(struct lyout *out, int level, struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200742{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200743 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200744 struct lys_node_leaflist *llist = (struct lys_node_leaflist *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200745
Radek Krejci76b07902015-10-09 09:11:25 +0200746 ly_print(out, "%*sleaf-list %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +0200747
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200748 level++;
Radek Krejci76b07902015-10-09 09:11:25 +0200749 yang_print_nacmext(out, level, node, node->module);
750 yang_print_snode_common2(out, level, node);
Michal Vasko4773b762015-07-07 12:15:10 +0200751 for (i = 0; i < llist->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200752 yang_print_iffeature(out, level, node->module, llist->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200753 }
Radek Krejci1574a8d2015-08-03 14:16:52 +0200754 if (llist->flags & LYS_USERORDERED) {
Radek Krejci76b07902015-10-09 09:11:25 +0200755 ly_print(out, "%*sordered-by user;\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200756 }
757 if (llist->min > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200758 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, llist->min);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200759 }
760 if (llist->max > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200761 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, llist->max);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200762 }
763 for (i = 0; i < llist->must_size; i++) {
Michal Vaskof9893382015-10-09 14:03:04 +0200764 yang_print_must(out, level, node->module, &llist->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200765 }
Michal Vasko1f0428a2015-07-07 14:55:04 +0200766 if (llist->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200767 yang_print_when(out, level, llist->module, llist->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200768 }
Radek Krejci76b07902015-10-09 09:11:25 +0200769 yang_print_type(out, level, node->module, &llist->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200770 if (llist->units != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200771 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, llist->units);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200772 }
773 level--;
Michal Vasko16083662015-06-09 14:00:45 +0200774
Radek Krejci76b07902015-10-09 09:11:25 +0200775 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200776}
777
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200778static void
Radek Krejci76b07902015-10-09 09:11:25 +0200779yang_print_list(struct lyout *out, int level, struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200780{
Michal Vasko1ef07972015-07-07 14:01:35 +0200781 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200782 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200783 struct lys_node_list *list = (struct lys_node_list *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200784
Radek Krejci76b07902015-10-09 09:11:25 +0200785 ly_print(out, "%*slist %s {\n", LEVEL, INDENT, node->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200786 level++;
Radek Krejci76b07902015-10-09 09:11:25 +0200787 yang_print_nacmext(out, level, node, node->module);
788 yang_print_snode_common2(out, level, node);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200789
Michal Vasko4773b762015-07-07 12:15:10 +0200790 for (i = 0; i < list->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200791 yang_print_iffeature(out, level, node->module, list->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200792 }
793
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200794 if (list->keys_size) {
Radek Krejci76b07902015-10-09 09:11:25 +0200795 ly_print(out, "%*skey \"", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200796 for (i = 0; i < list->keys_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200797 ly_print(out, "%s%s", list->keys[i]->name, i + 1 < list->keys_size ? " " : "");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200798 }
Radek Krejci76b07902015-10-09 09:11:25 +0200799 ly_print(out, "\";\n");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200800 }
Radek Krejcid7f0d012015-05-25 15:04:52 +0200801
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200802 for (i = 0; i < list->unique_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200803 yang_print_unique(out, level, &list->unique[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200804 }
Michal Vaskodff818b2015-06-09 14:01:12 +0200805
Radek Krejci1574a8d2015-08-03 14:16:52 +0200806 if (list->flags & LYS_USERORDERED) {
Radek Krejci76b07902015-10-09 09:11:25 +0200807 ly_print(out, "%*sordered-by user;\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200808 }
809 if (list->min > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200810 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, list->min);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200811 }
812 if (list->max > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200813 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, list->max);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200814 }
815 for (i = 0; i < list->must_size; i++) {
Michal Vaskof9893382015-10-09 14:03:04 +0200816 yang_print_must(out, level, list->module, &list->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200817 }
Michal Vasko1f0428a2015-07-07 14:55:04 +0200818 if (list->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200819 yang_print_when(out, level, list->module, list->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200820 }
Michal Vaskodff818b2015-06-09 14:01:12 +0200821
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200822 for (i = 0; i < list->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200823 yang_print_typedef(out, level, list->module, &list->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200824 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200825
Radek Krejci1d82ef62015-08-07 14:44:40 +0200826 LY_TREE_FOR(node->child, sub) {
Michal Vasko15b48702015-07-07 15:49:34 +0200827 /* augment */
Radek Krejci1d82ef62015-08-07 14:44:40 +0200828 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200829 continue;
830 }
Radek Krejci76b07902015-10-09 09:11:25 +0200831 yang_print_snode(out, level, sub,
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 }
835 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200836 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200837}
838
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200839static void
Radek Krejci76b07902015-10-09 09:11:25 +0200840yang_print_grouping(struct lyout *out, int level, struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200841{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200842 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200843 struct lys_node *child;
844 struct lys_node_grp *grp = (struct lys_node_grp *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200845
Radek Krejci76b07902015-10-09 09:11:25 +0200846 ly_print(out, "%*sgrouping %s {\n", LEVEL, INDENT, node->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200847 level++;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200848
Radek Krejci76b07902015-10-09 09:11:25 +0200849 yang_print_snode_common(out, level, node);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200850
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200851 for (i = 0; i < grp->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200852 yang_print_typedef(out, level, node->module, &grp->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200853 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200854
Radek Krejci1d82ef62015-08-07 14:44:40 +0200855 LY_TREE_FOR(node->child, child) {
Radek Krejci76b07902015-10-09 09:11:25 +0200856 yang_print_snode(out, level, child,
Radek Krejci76512572015-08-04 09:47:08 +0200857 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
858 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200859 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200860
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200861 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200862 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200863}
864
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200865static void
Radek Krejci76b07902015-10-09 09:11:25 +0200866yang_print_uses(struct lyout *out, int level, struct lys_node *node)
Radek Krejcic7c9a6c2015-05-25 16:35:06 +0200867{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200868 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200869 struct lys_node_uses *uses = (struct lys_node_uses *)node;
Radek Krejcic7c9a6c2015-05-25 16:35:06 +0200870
Radek Krejci76b07902015-10-09 09:11:25 +0200871 ly_print(out, "%*suses ", LEVEL, INDENT);
Radek Krejci1d82ef62015-08-07 14:44:40 +0200872 if (node->child && (node->module != node->child->module) && !node->child->module->type) {
Radek Krejci76b07902015-10-09 09:11:25 +0200873 ly_print(out, "%s:", get_module_import_prefix(node->module, node->child->module));
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200874 }
Radek Krejci76b07902015-10-09 09:11:25 +0200875 ly_print(out, "%s {\n",uses->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200876 level++;
Radek Krejcic7c9a6c2015-05-25 16:35:06 +0200877
Radek Krejci76b07902015-10-09 09:11:25 +0200878 yang_print_nacmext(out, level, node, node->module);
879 yang_print_snode_common(out, level, node);
Michal Vasko4773b762015-07-07 12:15:10 +0200880 for (i = 0; i < uses->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200881 yang_print_iffeature(out, level, node->module, uses->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200882 }
Michal Vasko1f0428a2015-07-07 14:55:04 +0200883 if (uses->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200884 yang_print_when(out, level, node->module, uses->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200885 }
Radek Krejcic7c9a6c2015-05-25 16:35:06 +0200886
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200887 for (i = 0; i < uses->refine_size; i++) {
Michal Vaskof9893382015-10-09 14:03:04 +0200888 yang_print_refine(out, level, node->module, &uses->refine[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200889 }
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200890
Michal Vasko6f25f212015-07-07 15:42:07 +0200891 for (i = 0; i < uses->augment_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200892 yang_print_augment(out, level, node->module, &uses->augment[i]);
Michal Vasko6f25f212015-07-07 15:42:07 +0200893 }
894
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200895 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200896 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcic7c9a6c2015-05-25 16:35:06 +0200897}
898
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200899static void
Radek Krejci76b07902015-10-09 09:11:25 +0200900yang_print_input_output(struct lyout *out, int level, struct lys_node *node)
Michal Vasko5bbae102015-06-16 12:16:44 +0200901{
Michal Vaskof4d3d742015-06-16 11:51:09 +0200902 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200903 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200904 struct lys_node_rpc_inout *inout = (struct lys_node_rpc_inout *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +0200905
Radek Krejci76b07902015-10-09 09:11:25 +0200906 ly_print(out, "%*s%s {\n", LEVEL, INDENT, (inout->nodetype == LYS_INPUT ? "input" : "output"));
Michal Vaskof4d3d742015-06-16 11:51:09 +0200907
908 level++;
909 for (i = 0; i < inout->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200910 yang_print_typedef(out, level, node->module, &inout->tpdf[i]);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200911 }
912
Radek Krejci1d82ef62015-08-07 14:44:40 +0200913 LY_TREE_FOR(node->child, sub) {
Michal Vasko15b48702015-07-07 15:49:34 +0200914 /* augment */
Radek Krejci1d82ef62015-08-07 14:44:40 +0200915 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200916 continue;
917 }
Radek Krejci76b07902015-10-09 09:11:25 +0200918 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200919 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
920 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200921 }
922
923 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200924 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200925}
926
927static void
Radek Krejci76b07902015-10-09 09:11:25 +0200928yang_print_rpc(struct lyout *out, int level, struct lys_node *node)
Michal Vaskof4d3d742015-06-16 11:51:09 +0200929{
930 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200931 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200932 struct lys_node_rpc *rpc = (struct lys_node_rpc *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +0200933
Radek Krejci76b07902015-10-09 09:11:25 +0200934 ly_print(out, "%*srpc %s {\n", LEVEL, INDENT, node->name);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200935
936 level++;
Radek Krejci76b07902015-10-09 09:11:25 +0200937 yang_print_snode_common(out, level, node);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200938
Michal Vasko4773b762015-07-07 12:15:10 +0200939 for (i = 0; i < rpc->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200940 yang_print_iffeature(out, level, node->module, rpc->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200941 }
942
Michal Vaskof4d3d742015-06-16 11:51:09 +0200943 for (i = 0; i < rpc->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200944 yang_print_typedef(out, level, node->module, &rpc->tpdf[i]);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200945 }
946
Radek Krejci1d82ef62015-08-07 14:44:40 +0200947 LY_TREE_FOR(node->child, sub) {
Radek Krejci76b07902015-10-09 09:11:25 +0200948 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200949 LYS_GROUPING | LYS_INPUT | LYS_OUTPUT);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200950 }
951
952 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200953 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200954}
955
956static void
Radek Krejci76b07902015-10-09 09:11:25 +0200957yang_print_notif(struct lyout *out, int level, struct lys_node *node)
Michal Vasko7690bc12015-06-16 12:26:05 +0200958{
Michal Vaskof4d3d742015-06-16 11:51:09 +0200959 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200960 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200961 struct lys_node_notif *notif = (struct lys_node_notif *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +0200962
Radek Krejci76b07902015-10-09 09:11:25 +0200963 ly_print(out, "%*snotification %s {\n", LEVEL, INDENT, node->name);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200964
965 level++;
Radek Krejci76b07902015-10-09 09:11:25 +0200966 yang_print_snode_common(out, level, node);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200967
Michal Vasko4773b762015-07-07 12:15:10 +0200968 for (i = 0; i < notif->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200969 yang_print_iffeature(out, level, node->module, notif->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200970 }
971
Michal Vaskof4d3d742015-06-16 11:51:09 +0200972 for (i = 0; i < notif->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200973 yang_print_typedef(out, level, node->module, &notif->tpdf[i]);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200974 }
975
Radek Krejci1d82ef62015-08-07 14:44:40 +0200976 LY_TREE_FOR(node->child, sub) {
Michal Vasko15b48702015-07-07 15:49:34 +0200977 /* augment */
Radek Krejci1d82ef62015-08-07 14:44:40 +0200978 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200979 continue;
980 }
Radek Krejci76b07902015-10-09 09:11:25 +0200981 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200982 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
983 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200984 }
985
986 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200987 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200988}
989
990static void
Radek Krejci76b07902015-10-09 09:11:25 +0200991yang_print_snode(struct lyout *out, int level, struct lys_node *node, int mask)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200992{
Radek Krejci1d82ef62015-08-07 14:44:40 +0200993 switch (node->nodetype & mask) {
Radek Krejci76512572015-08-04 09:47:08 +0200994 case LYS_CONTAINER:
Radek Krejci76b07902015-10-09 09:11:25 +0200995 yang_print_container(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200996 break;
Radek Krejci76512572015-08-04 09:47:08 +0200997 case LYS_CHOICE:
Radek Krejci76b07902015-10-09 09:11:25 +0200998 yang_print_choice(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200999 break;
Radek Krejci76512572015-08-04 09:47:08 +02001000 case LYS_LEAF:
Radek Krejci76b07902015-10-09 09:11:25 +02001001 yang_print_leaf(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001002 break;
Radek Krejci76512572015-08-04 09:47:08 +02001003 case LYS_LEAFLIST:
Radek Krejci76b07902015-10-09 09:11:25 +02001004 yang_print_leaflist(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001005 break;
Radek Krejci76512572015-08-04 09:47:08 +02001006 case LYS_LIST:
Radek Krejci76b07902015-10-09 09:11:25 +02001007 yang_print_list(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001008 break;
Radek Krejci76512572015-08-04 09:47:08 +02001009 case LYS_USES:
Radek Krejci76b07902015-10-09 09:11:25 +02001010 yang_print_uses(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001011 break;
Radek Krejci76512572015-08-04 09:47:08 +02001012 case LYS_GROUPING:
Radek Krejci76b07902015-10-09 09:11:25 +02001013 yang_print_grouping(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001014 break;
Radek Krejci76512572015-08-04 09:47:08 +02001015 case LYS_ANYXML:
Radek Krejci76b07902015-10-09 09:11:25 +02001016 yang_print_anyxml(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001017 break;
Radek Krejci76512572015-08-04 09:47:08 +02001018 case LYS_CASE:
Radek Krejci76b07902015-10-09 09:11:25 +02001019 yang_print_case(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001020 break;
Radek Krejci76512572015-08-04 09:47:08 +02001021 case LYS_INPUT:
1022 case LYS_OUTPUT:
Radek Krejci76b07902015-10-09 09:11:25 +02001023 yang_print_input_output(out, level, node);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001024 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001025 default:
1026 break;
1027 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001028}
1029
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001030int
Radek Krejci76b07902015-10-09 09:11:25 +02001031yang_print_model(struct lyout *out, struct lys_module *module)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001032{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001033 unsigned int i;
1034 int level = 0;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001035#define LEVEL (level*2)
1036
Radek Krejci1d82ef62015-08-07 14:44:40 +02001037 struct lys_node *node;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001038
Michal Vasko116172e2015-07-07 11:54:37 +02001039 if (module->type) {
Radek Krejci76b07902015-10-09 09:11:25 +02001040 ly_print(out, "submodule %s {%s\n", module->name, (module->deviated ? " // DEVIATED" : ""));
Michal Vasko116172e2015-07-07 11:54:37 +02001041 level++;
Radek Krejci76b07902015-10-09 09:11:25 +02001042 ly_print(out, "%*sbelongs-to %s {\n", LEVEL, INDENT, ((struct lys_submodule *)module)->belongsto->name);
Michal Vasko116172e2015-07-07 11:54:37 +02001043 level++;
Radek Krejci76b07902015-10-09 09:11:25 +02001044 ly_print(out, "%*sprefix \"%s\";\n", LEVEL, INDENT, module->prefix);
Michal Vasko116172e2015-07-07 11:54:37 +02001045 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001046 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko116172e2015-07-07 11:54:37 +02001047 } else {
Radek Krejci76b07902015-10-09 09:11:25 +02001048 ly_print(out, "module %s {%s\n", module->name, (module->deviated ? " // DEVIATED" : ""));
Michal Vasko116172e2015-07-07 11:54:37 +02001049 level++;
Radek Krejci76b07902015-10-09 09:11:25 +02001050 ly_print(out, "%*snamespace \"%s\";\n", LEVEL, INDENT, module->ns);
1051 ly_print(out, "%*sprefix \"%s\";\n", LEVEL, INDENT, module->prefix);
Michal Vasko116172e2015-07-07 11:54:37 +02001052 }
Radek Krejcib0594bf2015-05-21 23:51:27 +02001053
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001054 if (module->version) {
Radek Krejci76b07902015-10-09 09:11:25 +02001055 ly_print(out, "%*syang-version %s;\n", LEVEL, INDENT, module->version == 1 ? "1" : "1.1");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001056 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001057
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001058 for (i = 0; i < module->imp_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +02001059 ly_print(out, "%*simport \"%s\" {\n", LEVEL, INDENT, module->imp[i].module->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001060 level++;
Radek Krejci76b07902015-10-09 09:11:25 +02001061 ly_print(out, "%*sprefix \"%s\";\n", LEVEL, INDENT, module->imp[i].prefix);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001062 if (module->imp[i].rev[0]) {
Radek Krejci76b07902015-10-09 09:11:25 +02001063 yang_print_text(out, level, "revision-date", module->imp[i].rev);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001064 }
1065 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001066 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001067 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001068
Michal Vaskoe0af1e22015-07-07 14:02:02 +02001069 for (i = 0; i < module->deviation_size; ++i) {
Radek Krejci76b07902015-10-09 09:11:25 +02001070 yang_print_deviation(out, level, module, &module->deviation[i]);
Michal Vaskoe0af1e22015-07-07 14:02:02 +02001071 }
1072
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001073 for (i = 0; i < module->inc_size; i++) {
1074 if (module->inc[i].rev[0]) {
Radek Krejci76b07902015-10-09 09:11:25 +02001075 ly_print(out, "%*sinclude \"%s\" {\n", LEVEL, INDENT, module->inc[i].submodule->name);
1076 yang_print_text(out, level + 1, "revision-date", module->inc[i].rev);
1077 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001078 } else {
Radek Krejci76b07902015-10-09 09:11:25 +02001079 ly_print(out, "%*sinclude \"%s\";\n", LEVEL, INDENT, module->inc[i].submodule->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001080 }
1081 }
Radek Krejciefaeba32015-05-27 14:30:57 +02001082
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001083 if (module->org) {
Radek Krejci76b07902015-10-09 09:11:25 +02001084 yang_print_text(out, level, "organization", module->org);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001085 }
1086 if (module->contact) {
Radek Krejci76b07902015-10-09 09:11:25 +02001087 yang_print_text(out, level, "contact", module->contact);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001088 }
1089 if (module->dsc) {
Radek Krejci76b07902015-10-09 09:11:25 +02001090 yang_print_text(out, level, "description", module->dsc);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001091 }
1092 if (module->ref) {
Radek Krejci76b07902015-10-09 09:11:25 +02001093 yang_print_text(out, level, "reference", module->ref);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001094 }
1095 for (i = 0; i < module->rev_size; i++) {
1096 if (module->rev[i].dsc || module->rev[i].ref) {
Radek Krejci76b07902015-10-09 09:11:25 +02001097 ly_print(out, "%*srevision \"%s\" {\n", LEVEL, INDENT, module->rev[i].date);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001098 level++;
1099 if (module->rev[i].dsc) {
Radek Krejci76b07902015-10-09 09:11:25 +02001100 yang_print_text(out, level, "description", module->rev[i].dsc);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001101 }
1102 if (module->rev[i].ref) {
Radek Krejci76b07902015-10-09 09:11:25 +02001103 yang_print_text(out, level, "reference", module->rev[i].ref);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001104 }
1105 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001106 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001107 } else {
Radek Krejci76b07902015-10-09 09:11:25 +02001108 yang_print_text(out, level, "revision", module->rev[i].date);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001109 }
1110 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001111
Michal Vasko30f6e912015-07-07 12:24:27 +02001112 for (i = 0; i < module->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +02001113 yang_print_feature(out, level, &module->features[i]);
Michal Vasko30f6e912015-07-07 12:24:27 +02001114 }
1115
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001116 for (i = 0; i < module->ident_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +02001117 yang_print_identity(out, level, &module->ident[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001118 }
Radek Krejci6793db02015-05-22 17:49:54 +02001119
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001120 for (i = 0; i < module->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +02001121 yang_print_typedef(out, level, module, &module->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001122 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001123
Radek Krejci1d82ef62015-08-07 14:44:40 +02001124 LY_TREE_FOR(module->data, node) {
Radek Krejci92720552015-10-05 15:28:27 +02001125 switch(node->nodetype) {
1126 case LYS_RPC:
Radek Krejcibac81762015-10-09 10:19:52 +02001127 yang_print_rpc(out, level, node);
Radek Krejci92720552015-10-05 15:28:27 +02001128 break;
1129 case LYS_NOTIF:
Radek Krejcibac81762015-10-09 10:19:52 +02001130 yang_print_notif(out, level, node);
Radek Krejci92720552015-10-05 15:28:27 +02001131 break;
1132 default:
Radek Krejcibac81762015-10-09 10:19:52 +02001133 yang_print_snode(out, level, node,
Radek Krejci92720552015-10-05 15:28:27 +02001134 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
1135 LYS_USES | LYS_GROUPING | LYS_ANYXML);
1136 break;
1137 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001138 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001139
Michal Vasko6f25f212015-07-07 15:42:07 +02001140 for (i = 0; i < module->augment_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +02001141 yang_print_augment(out, level, module, &module->augment[i]);
Michal Vasko6f25f212015-07-07 15:42:07 +02001142 }
1143
Radek Krejci76b07902015-10-09 09:11:25 +02001144 ly_print(out, "}\n");
Radek Krejcida04f4a2015-05-21 12:54:09 +02001145
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001146 return EXIT_SUCCESS;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001147#undef LEVEL
1148}