blob: eab747755c98ba924c759438d92d7fefd39ed8e0 [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:
Radek Krejci76b07902015-10-09 09:11:25 +0200315 ly_print(out, "%*spath \"%s\";\n", LEVEL, INDENT, type->info.lref.path);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200316 break;
317 case LY_TYPE_STRING:
Radek Krejci5fbc9162015-06-19 14:11:11 +0200318 if (type->info.str.length) {
Radek Krejci76b07902015-10-09 09:11:25 +0200319 ly_print(out, "%*slength \"%s\" {\n", LEVEL, INDENT, type->info.str.length->expr);
320 yang_print_restr(out, level + 1, type->info.str.length);
321 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci061bd522015-06-19 13:45:16 +0200322 }
Radek Krejci5fbc9162015-06-19 14:11:11 +0200323 for (i = 0; i < type->info.str.pat_count; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200324 ly_print(out, "%*spattern \"%s\" {\n", LEVEL, INDENT, type->info.str.patterns[i].expr);
325 yang_print_restr(out, level + 1, &type->info.str.patterns[i]);
326 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci5fbc9162015-06-19 14:11:11 +0200327 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200328 break;
329 case LY_TYPE_UNION:
330 for (i = 0; i < type->info.uni.count; ++i) {
Radek Krejci76b07902015-10-09 09:11:25 +0200331 yang_print_type(out, level, module, &type->info.uni.types[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200332 }
333 break;
334 default:
335 /* other types do not have substatements */
336 break;
337 }
338 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200339 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200340}
341
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200342static void
Michal Vaskof9893382015-10-09 14:03:04 +0200343yang_print_must(struct lyout *out, int level, struct lys_module *module, struct lys_restr *must)
Michal Vasko7f976ee2015-06-09 13:55:41 +0200344{
Michal Vaskof9893382015-10-09 14:03:04 +0200345 const char *xml_expr;
346
347 xml_expr = transform_expr_json2xml(module, must->expr, NULL, NULL, NULL);
348 if (!xml_expr) {
349 ly_print(out, "(!error!)");
350 return;
351 }
352
353 ly_print(out, "%*smust \"%s\" {\n", LEVEL, INDENT, xml_expr);
354 lydict_remove(module->ctx, xml_expr);
355
Radek Krejci76b07902015-10-09 09:11:25 +0200356 yang_print_restr(out, level + 1, must);
357 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko7f976ee2015-06-09 13:55:41 +0200358}
359
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200360static void
Radek Krejci76b07902015-10-09 09:11:25 +0200361yang_print_unique(struct lyout *out, int level, struct lys_unique *uniq)
Michal Vasko1ef07972015-07-07 14:01:35 +0200362{
363 int i;
364
Radek Krejci76b07902015-10-09 09:11:25 +0200365 ly_print(out, "%*sunique \"", LEVEL, INDENT);
Michal Vasko1ef07972015-07-07 14:01:35 +0200366 for (i = 0; i < uniq->leafs_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200367 ly_print(out, "%s%s", uniq->leafs[i]->name, i + 1 < uniq->leafs_size ? " " : "");
Michal Vasko1ef07972015-07-07 14:01:35 +0200368 }
Radek Krejci76b07902015-10-09 09:11:25 +0200369 ly_print(out, "\";\n");
Michal Vasko1ef07972015-07-07 14:01:35 +0200370}
371
372static void
Michal Vaskof9893382015-10-09 14:03:04 +0200373yang_print_refine(struct lyout *out, int level, struct lys_module *module, struct lys_refine *refine)
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200374{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200375 int i;
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200376
Radek Krejci76b07902015-10-09 09:11:25 +0200377 ly_print(out, "%*srefine \"%s\" {\n", LEVEL, INDENT, refine->target_name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200378 level++;
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200379
Radek Krejci1574a8d2015-08-03 14:16:52 +0200380 if (refine->flags & LYS_CONFIG_W) {
Radek Krejci76b07902015-10-09 09:11:25 +0200381 ly_print(out, "%*sconfig \"true\";\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200382 } else if (refine->flags & LYS_CONFIG_R) {
Radek Krejci76b07902015-10-09 09:11:25 +0200383 ly_print(out, "%*sconfig \"false\";\n", LEVEL, INDENT);
Radek Krejci6a113852015-07-03 16:04:20 +0200384 }
385
Radek Krejci1574a8d2015-08-03 14:16:52 +0200386 if (refine->flags & LYS_MAND_TRUE) {
Radek Krejci76b07902015-10-09 09:11:25 +0200387 ly_print(out, "%*smandatory \"true\";\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200388 } else if (refine->flags & LYS_MAND_FALSE) {
Radek Krejci76b07902015-10-09 09:11:25 +0200389 ly_print(out, "%*smandatory \"false\";\n", LEVEL, INDENT);
Radek Krejci6a113852015-07-03 16:04:20 +0200390 }
391
Radek Krejci76b07902015-10-09 09:11:25 +0200392 yang_print_snode_common(out, level, (struct lys_node *)refine);
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200393
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200394 for (i = 0; i < refine->must_size; ++i) {
Michal Vaskof9893382015-10-09 14:03:04 +0200395 yang_print_must(out, level, module, &refine->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200396 }
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200397
Radek Krejci76512572015-08-04 09:47:08 +0200398 if (refine->target_type & (LYS_LEAF | LYS_CHOICE)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200399 if (refine->mod.dflt != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200400 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, refine->mod.dflt);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200401 }
Radek Krejci76512572015-08-04 09:47:08 +0200402 } else if (refine->target_type == LYS_CONTAINER) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200403 if (refine->mod.presence != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200404 yang_print_text(out, level, "presence", refine->mod.presence);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200405 }
Radek Krejci76512572015-08-04 09:47:08 +0200406 } else if (refine->target_type & (LYS_LIST | LYS_LEAFLIST)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200407 if (refine->mod.list.min > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200408 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, refine->mod.list.min);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200409 }
410 if (refine->mod.list.max > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200411 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, refine->mod.list.max);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200412 }
413 }
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200414
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200415 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200416 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200417}
418
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200419static void
Radek Krejci76b07902015-10-09 09:11:25 +0200420yang_print_deviation(struct lyout *out, int level, struct lys_module *module, struct lys_deviation *deviation)
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200421{
422 int i, j;
423
Radek Krejci76b07902015-10-09 09:11:25 +0200424 ly_print(out, "%*sdeviation \"%s\" {\n", LEVEL, INDENT, deviation->target_name);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200425 level++;
426
427 if (deviation->dsc) {
Radek Krejci76b07902015-10-09 09:11:25 +0200428 yang_print_text(out, level, "description", deviation->dsc);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200429 }
430 if (deviation->ref) {
Radek Krejci76b07902015-10-09 09:11:25 +0200431 yang_print_text(out, level, "reference", deviation->ref);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200432 }
433
434 for (i = 0; i < deviation->deviate_size; ++i) {
Radek Krejci76b07902015-10-09 09:11:25 +0200435 ly_print(out, "%*sdeviate ", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200436 if (deviation->deviate[i].mod == LY_DEVIATE_NO) {
Radek Krejci76b07902015-10-09 09:11:25 +0200437 ly_print(out, "not-supported {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200438 } else if (deviation->deviate[i].mod == LY_DEVIATE_ADD) {
Radek Krejci76b07902015-10-09 09:11:25 +0200439 ly_print(out, "add {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200440 } else if (deviation->deviate[i].mod == LY_DEVIATE_RPL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200441 ly_print(out, "replace {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200442 } else if (deviation->deviate[i].mod == LY_DEVIATE_DEL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200443 ly_print(out, "delete {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200444 }
445 level++;
446
Radek Krejci1574a8d2015-08-03 14:16:52 +0200447 if (deviation->deviate[i].flags & LYS_CONFIG_W) {
Radek Krejci76b07902015-10-09 09:11:25 +0200448 ly_print(out, "%*sconfig \"true\";\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200449 } else if (deviation->deviate[i].flags & LYS_CONFIG_R) {
Radek Krejci76b07902015-10-09 09:11:25 +0200450 ly_print(out, "%*sconfig \"false\";\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200451 }
452
Radek Krejci1574a8d2015-08-03 14:16:52 +0200453 if (deviation->deviate[i].flags & LYS_MAND_TRUE) {
Radek Krejci76b07902015-10-09 09:11:25 +0200454 ly_print(out, "%*smandatory \"true\";\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200455 } else if (deviation->deviate[i].flags & LYS_MAND_FALSE) {
Radek Krejci76b07902015-10-09 09:11:25 +0200456 ly_print(out, "%*smandatory \"false\";\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200457 }
458
459 if (deviation->deviate[i].dflt) {
Radek Krejci76b07902015-10-09 09:11:25 +0200460 ly_print(out, "%*sdefault %s;\n", LEVEL, INDENT, deviation->deviate[i].dflt);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200461 }
462
463 if (deviation->deviate[i].min) {
Radek Krejci76b07902015-10-09 09:11:25 +0200464 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, deviation->deviate[i].min);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200465 }
466 if (deviation->deviate[i].max) {
Radek Krejci76b07902015-10-09 09:11:25 +0200467 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, deviation->deviate[i].max);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200468 }
469
470 for (j = 0; j < deviation->deviate[i].must_size; ++j) {
Michal Vaskof9893382015-10-09 14:03:04 +0200471 yang_print_must(out, level, module, &deviation->deviate[i].must[j]);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200472 }
473
474 for (j = 0; j < deviation->deviate[i].unique_size; ++j) {
Radek Krejci76b07902015-10-09 09:11:25 +0200475 yang_print_unique(out, level, &deviation->deviate[i].unique[j]);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200476 }
477
478 if (deviation->deviate[i].type) {
Radek Krejci76b07902015-10-09 09:11:25 +0200479 yang_print_type(out, level, module, deviation->deviate[i].type);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200480 }
481
482 if (deviation->deviate[i].units) {
Radek Krejci76b07902015-10-09 09:11:25 +0200483 ly_print(out, "%*sunits %s;\n", LEVEL, INDENT, deviation->deviate[i].units);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200484 }
485
486 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200487 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200488 }
489
490 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200491 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200492}
493
494static void
Radek Krejci76b07902015-10-09 09:11:25 +0200495yang_print_augment(struct lyout *out, int level, struct lys_module *module, struct lys_node_augment *augment)
Michal Vasko6f25f212015-07-07 15:42:07 +0200496{
497 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200498 struct lys_node *sub;
Michal Vasko6f25f212015-07-07 15:42:07 +0200499
Radek Krejci76b07902015-10-09 09:11:25 +0200500 ly_print(out, "%*saugment \"%s\" {\n", LEVEL, INDENT, augment->target_name);
Michal Vasko6f25f212015-07-07 15:42:07 +0200501 level++;
502
Radek Krejci76b07902015-10-09 09:11:25 +0200503 yang_print_nacmext(out, level, (struct lys_node *)augment, module);
504 yang_print_snode_common(out, level, (struct lys_node *)augment);
Michal Vasko6f25f212015-07-07 15:42:07 +0200505
506 for (i = 0; i < augment->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200507 yang_print_iffeature(out, level, module, augment->features[i]);
Michal Vasko6f25f212015-07-07 15:42:07 +0200508 }
509
510 if (augment->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200511 yang_print_when(out, level, module, augment->when);
Michal Vasko6f25f212015-07-07 15:42:07 +0200512 }
513
514 LY_TREE_FOR(augment->child, sub) {
Radek Krejci76b07902015-10-09 09:11:25 +0200515 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200516 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
517 LYS_USES | LYS_ANYXML | LYS_CASE);
Michal Vasko6f25f212015-07-07 15:42:07 +0200518 }
519
520 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200521 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko6f25f212015-07-07 15:42:07 +0200522}
523
524static void
Radek Krejci76b07902015-10-09 09:11:25 +0200525yang_print_typedef(struct lyout *out, int level, struct lys_module *module, struct lys_tpdf *tpdf)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200526{
Radek Krejci76b07902015-10-09 09:11:25 +0200527 ly_print(out, "%*stypedef %s {\n", LEVEL, INDENT, tpdf->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200528 level++;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200529
Radek Krejci76b07902015-10-09 09:11:25 +0200530 yang_print_snode_common(out, level, (struct lys_node *)tpdf);
531 yang_print_type(out, level, module, &tpdf->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200532 if (tpdf->units != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200533 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, tpdf->units);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200534 }
535 if (tpdf->dflt != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200536 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, tpdf->dflt);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200537 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200538
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200539 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200540 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200541}
542
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200543static void
Radek Krejci76b07902015-10-09 09:11:25 +0200544yang_print_identity(struct lyout *out, int level, struct lys_ident *ident)
Radek Krejci6793db02015-05-22 17:49:54 +0200545{
Radek Krejci76b07902015-10-09 09:11:25 +0200546 ly_print(out, "%*sidentity %s {\n", LEVEL, INDENT, ident->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200547 level++;
Radek Krejci6793db02015-05-22 17:49:54 +0200548
Radek Krejci76b07902015-10-09 09:11:25 +0200549 yang_print_snode_common(out, level, (struct lys_node *)ident);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200550 if (ident->base) {
Radek Krejci76b07902015-10-09 09:11:25 +0200551 ly_print(out, "%*sbase ", LEVEL, INDENT);
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200552 if ((ident->module != ident->base->module) && !ident->base->module->type) {
Radek Krejci76b07902015-10-09 09:11:25 +0200553 ly_print(out, "%s:", get_module_import_prefix(ident->module, ident->base->module));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200554 }
Radek Krejci76b07902015-10-09 09:11:25 +0200555 ly_print(out, "%s;\n", ident->base->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200556 }
Radek Krejci6793db02015-05-22 17:49:54 +0200557
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200558 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200559 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6793db02015-05-22 17:49:54 +0200560
561}
562
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200563static void
Radek Krejci76b07902015-10-09 09:11:25 +0200564yang_print_container(struct lyout *out, int level, struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200565{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200566 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200567 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200568 struct lys_node_container *cont = (struct lys_node_container *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200569
Radek Krejci76b07902015-10-09 09:11:25 +0200570 ly_print(out, "%*scontainer %s {\n", LEVEL, INDENT, node->name);
Michal Vasko7f976ee2015-06-09 13:55:41 +0200571
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200572 level++;
Radek Krejci6a113852015-07-03 16:04:20 +0200573
Radek Krejci76b07902015-10-09 09:11:25 +0200574 yang_print_nacmext(out, level, node, node->module);
Radek Krejci6a113852015-07-03 16:04:20 +0200575
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200576 if (cont->presence != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200577 yang_print_text(out, level, "presence", cont->presence);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200578 }
Michal Vasko7f976ee2015-06-09 13:55:41 +0200579
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200580 for (i = 0; i < cont->must_size; i++) {
Michal Vaskof9893382015-10-09 14:03:04 +0200581 yang_print_must(out, level, node->module, &cont->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200582 }
Michal Vasko7f976ee2015-06-09 13:55:41 +0200583
Radek Krejci76b07902015-10-09 09:11:25 +0200584 yang_print_snode_common2(out, level, node);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200585
Michal Vasko4773b762015-07-07 12:15:10 +0200586 for (i = 0; i < cont->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200587 yang_print_iffeature(out, level, node->module, cont->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200588 }
589
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200590 for (i = 0; i < cont->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200591 yang_print_typedef(out, level, node->module, &cont->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200592 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200593
Michal Vasko1f0428a2015-07-07 14:55:04 +0200594 if (cont->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200595 yang_print_when(out, level, node->module, cont->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200596 }
597
Radek Krejci1d82ef62015-08-07 14:44:40 +0200598 LY_TREE_FOR(node->child, sub) {
Michal Vasko15b48702015-07-07 15:49:34 +0200599 /* augment */
Radek Krejci1d82ef62015-08-07 14:44:40 +0200600 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200601 continue;
602 }
Radek Krejci76b07902015-10-09 09:11:25 +0200603 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200604 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
605 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200606 }
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200607
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200608 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200609 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200610}
611
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200612static void
Radek Krejci76b07902015-10-09 09:11:25 +0200613yang_print_case(struct lyout *out, int level, struct lys_node *node)
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200614{
Michal Vasko4773b762015-07-07 12:15:10 +0200615 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200616 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200617 struct lys_node_case *cas = (struct lys_node_case *)node;
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200618
Radek Krejci76b07902015-10-09 09:11:25 +0200619 ly_print(out, "%*scase %s {\n", LEVEL, INDENT, cas->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200620 level++;
Radek Krejci76b07902015-10-09 09:11:25 +0200621 yang_print_nacmext(out, level, node, node->module);
622 yang_print_snode_common2(out, level, node);
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200623
Michal Vasko4773b762015-07-07 12:15:10 +0200624 for (i = 0; i < cas->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200625 yang_print_iffeature(out, level, node->module, cas->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200626 }
627
Michal Vasko1f0428a2015-07-07 14:55:04 +0200628 if (cas->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200629 yang_print_when(out, level, node->module, cas->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200630 }
631
Radek Krejci1d82ef62015-08-07 14:44:40 +0200632 LY_TREE_FOR(node->child, sub) {
Michal Vasko15b48702015-07-07 15:49:34 +0200633 /* augment */
Radek Krejci1d82ef62015-08-07 14:44:40 +0200634 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200635 continue;
636 }
Radek Krejci76b07902015-10-09 09:11:25 +0200637 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200638 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
639 LYS_USES | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200640 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200641
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200642 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200643 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200644}
645
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200646static void
Radek Krejci76b07902015-10-09 09:11:25 +0200647yang_print_choice(struct lyout *out, int level, struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200648{
Michal Vasko4773b762015-07-07 12:15:10 +0200649 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200650 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200651 struct lys_node_choice *choice = (struct lys_node_choice *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200652
Radek Krejci76b07902015-10-09 09:11:25 +0200653 ly_print(out, "%*schoice %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +0200654
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200655 level++;
Radek Krejci76b07902015-10-09 09:11:25 +0200656 yang_print_nacmext(out, level, node, node->module);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200657 if (choice->dflt != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200658 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, choice->dflt->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200659 }
Michal Vasko16083662015-06-09 14:00:45 +0200660
Radek Krejci76b07902015-10-09 09:11:25 +0200661 yang_print_snode_common2(out, level, node);
Michal Vasko4773b762015-07-07 12:15:10 +0200662
663 for (i = 0; i < choice->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200664 yang_print_iffeature(out, level, node->module, choice->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200665 }
666
Michal Vasko1f0428a2015-07-07 14:55:04 +0200667 if (choice->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200668 yang_print_when(out, level, node->module, choice->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200669 }
670
Radek Krejci1d82ef62015-08-07 14:44:40 +0200671 LY_TREE_FOR(node->child, sub) {
Michal Vasko15b48702015-07-07 15:49:34 +0200672 /* augment */
Radek Krejci1d82ef62015-08-07 14:44:40 +0200673 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200674 continue;
675 }
Radek Krejci76b07902015-10-09 09:11:25 +0200676 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200677 LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYXML | LYS_CASE);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200678 }
679 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200680 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200681}
682
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200683static void
Radek Krejci76b07902015-10-09 09:11:25 +0200684yang_print_leaf(struct lyout *out, int level, struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200685{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200686 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200687 struct lys_node_leaf *leaf = (struct lys_node_leaf *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200688
Radek Krejci76b07902015-10-09 09:11:25 +0200689 ly_print(out, "%*sleaf %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +0200690
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200691 level++;
Radek Krejci76b07902015-10-09 09:11:25 +0200692 yang_print_nacmext(out, level, node, node->module);
693 yang_print_snode_common2(out, level, node);
Michal Vasko4773b762015-07-07 12:15:10 +0200694 for (i = 0; i < leaf->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200695 yang_print_iffeature(out, level, node->module, leaf->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200696 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200697 for (i = 0; i < leaf->must_size; i++) {
Michal Vaskof9893382015-10-09 14:03:04 +0200698 yang_print_must(out, level, node->module, &leaf->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200699 }
Michal Vasko1f0428a2015-07-07 14:55:04 +0200700 if (leaf->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200701 yang_print_when(out, level, node->module, leaf->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200702 }
Radek Krejci76b07902015-10-09 09:11:25 +0200703 yang_print_type(out, level, node->module, &leaf->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200704 if (leaf->units != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200705 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, leaf->units);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200706 }
707 if (leaf->dflt != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200708 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, leaf->dflt);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200709 }
710 level--;
Michal Vasko16083662015-06-09 14:00:45 +0200711
Radek Krejci76b07902015-10-09 09:11:25 +0200712 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko16083662015-06-09 14:00:45 +0200713}
714
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200715static void
Radek Krejci76b07902015-10-09 09:11:25 +0200716yang_print_anyxml(struct lyout *out, int level, struct lys_node *node)
Michal Vasko16083662015-06-09 14:00:45 +0200717{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200718 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200719 struct lys_node_anyxml *anyxml = (struct lys_node_anyxml *)node;
Michal Vasko16083662015-06-09 14:00:45 +0200720
Radek Krejci76b07902015-10-09 09:11:25 +0200721 ly_print(out, "%*sanyxml %s {\n", LEVEL, INDENT, anyxml->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200722 level++;
Radek Krejci76b07902015-10-09 09:11:25 +0200723 yang_print_nacmext(out, level, node, node->module);
724 yang_print_snode_common2(out, level, node);
Michal Vasko4773b762015-07-07 12:15:10 +0200725 for (i = 0; i < anyxml->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200726 yang_print_iffeature(out, level, node->module, anyxml->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200727 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200728 for (i = 0; i < anyxml->must_size; i++) {
Michal Vaskof9893382015-10-09 14:03:04 +0200729 yang_print_must(out, level, node->module, &anyxml->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200730 }
Michal Vasko1f0428a2015-07-07 14:55:04 +0200731 if (anyxml->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200732 yang_print_when(out, level, node->module, anyxml->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200733 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200734 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200735 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200736}
737
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200738static void
Radek Krejci76b07902015-10-09 09:11:25 +0200739yang_print_leaflist(struct lyout *out, int level, struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200740{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200741 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200742 struct lys_node_leaflist *llist = (struct lys_node_leaflist *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200743
Radek Krejci76b07902015-10-09 09:11:25 +0200744 ly_print(out, "%*sleaf-list %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +0200745
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200746 level++;
Radek Krejci76b07902015-10-09 09:11:25 +0200747 yang_print_nacmext(out, level, node, node->module);
748 yang_print_snode_common2(out, level, node);
Michal Vasko4773b762015-07-07 12:15:10 +0200749 for (i = 0; i < llist->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200750 yang_print_iffeature(out, level, node->module, llist->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200751 }
Radek Krejci1574a8d2015-08-03 14:16:52 +0200752 if (llist->flags & LYS_USERORDERED) {
Radek Krejci76b07902015-10-09 09:11:25 +0200753 ly_print(out, "%*sordered-by user;\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200754 }
755 if (llist->min > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200756 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, llist->min);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200757 }
758 if (llist->max > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200759 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, llist->max);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200760 }
761 for (i = 0; i < llist->must_size; i++) {
Michal Vaskof9893382015-10-09 14:03:04 +0200762 yang_print_must(out, level, node->module, &llist->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200763 }
Michal Vasko1f0428a2015-07-07 14:55:04 +0200764 if (llist->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200765 yang_print_when(out, level, llist->module, llist->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200766 }
Radek Krejci76b07902015-10-09 09:11:25 +0200767 yang_print_type(out, level, node->module, &llist->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200768 if (llist->units != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200769 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, llist->units);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200770 }
771 level--;
Michal Vasko16083662015-06-09 14:00:45 +0200772
Radek Krejci76b07902015-10-09 09:11:25 +0200773 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200774}
775
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200776static void
Radek Krejci76b07902015-10-09 09:11:25 +0200777yang_print_list(struct lyout *out, int level, struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200778{
Michal Vasko1ef07972015-07-07 14:01:35 +0200779 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200780 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200781 struct lys_node_list *list = (struct lys_node_list *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200782
Radek Krejci76b07902015-10-09 09:11:25 +0200783 ly_print(out, "%*slist %s {\n", LEVEL, INDENT, node->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200784 level++;
Radek Krejci76b07902015-10-09 09:11:25 +0200785 yang_print_nacmext(out, level, node, node->module);
786 yang_print_snode_common2(out, level, node);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200787
Michal Vasko4773b762015-07-07 12:15:10 +0200788 for (i = 0; i < list->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200789 yang_print_iffeature(out, level, node->module, list->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200790 }
791
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200792 if (list->keys_size) {
Radek Krejci76b07902015-10-09 09:11:25 +0200793 ly_print(out, "%*skey \"", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200794 for (i = 0; i < list->keys_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200795 ly_print(out, "%s%s", list->keys[i]->name, i + 1 < list->keys_size ? " " : "");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200796 }
Radek Krejci76b07902015-10-09 09:11:25 +0200797 ly_print(out, "\";\n");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200798 }
Radek Krejcid7f0d012015-05-25 15:04:52 +0200799
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200800 for (i = 0; i < list->unique_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200801 yang_print_unique(out, level, &list->unique[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200802 }
Michal Vaskodff818b2015-06-09 14:01:12 +0200803
Radek Krejci1574a8d2015-08-03 14:16:52 +0200804 if (list->flags & LYS_USERORDERED) {
Radek Krejci76b07902015-10-09 09:11:25 +0200805 ly_print(out, "%*sordered-by user;\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200806 }
807 if (list->min > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200808 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, list->min);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200809 }
810 if (list->max > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200811 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, list->max);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200812 }
813 for (i = 0; i < list->must_size; i++) {
Michal Vaskof9893382015-10-09 14:03:04 +0200814 yang_print_must(out, level, list->module, &list->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200815 }
Michal Vasko1f0428a2015-07-07 14:55:04 +0200816 if (list->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200817 yang_print_when(out, level, list->module, list->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200818 }
Michal Vaskodff818b2015-06-09 14:01:12 +0200819
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200820 for (i = 0; i < list->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200821 yang_print_typedef(out, level, list->module, &list->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200822 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200823
Radek Krejci1d82ef62015-08-07 14:44:40 +0200824 LY_TREE_FOR(node->child, sub) {
Michal Vasko15b48702015-07-07 15:49:34 +0200825 /* augment */
Radek Krejci1d82ef62015-08-07 14:44:40 +0200826 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200827 continue;
828 }
Radek Krejci76b07902015-10-09 09:11:25 +0200829 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200830 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
831 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200832 }
833 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200834 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200835}
836
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200837static void
Radek Krejci76b07902015-10-09 09:11:25 +0200838yang_print_grouping(struct lyout *out, int level, struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200839{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200840 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200841 struct lys_node *child;
842 struct lys_node_grp *grp = (struct lys_node_grp *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200843
Radek Krejci76b07902015-10-09 09:11:25 +0200844 ly_print(out, "%*sgrouping %s {\n", LEVEL, INDENT, node->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200845 level++;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200846
Radek Krejci76b07902015-10-09 09:11:25 +0200847 yang_print_snode_common(out, level, node);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200848
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200849 for (i = 0; i < grp->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200850 yang_print_typedef(out, level, node->module, &grp->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200851 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200852
Radek Krejci1d82ef62015-08-07 14:44:40 +0200853 LY_TREE_FOR(node->child, child) {
Radek Krejci76b07902015-10-09 09:11:25 +0200854 yang_print_snode(out, level, child,
Radek Krejci76512572015-08-04 09:47:08 +0200855 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
856 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200857 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200858
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200859 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200860 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200861}
862
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200863static void
Radek Krejci76b07902015-10-09 09:11:25 +0200864yang_print_uses(struct lyout *out, int level, struct lys_node *node)
Radek Krejcic7c9a6c2015-05-25 16:35:06 +0200865{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200866 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200867 struct lys_node_uses *uses = (struct lys_node_uses *)node;
Radek Krejcic7c9a6c2015-05-25 16:35:06 +0200868
Radek Krejci76b07902015-10-09 09:11:25 +0200869 ly_print(out, "%*suses ", LEVEL, INDENT);
Radek Krejci1d82ef62015-08-07 14:44:40 +0200870 if (node->child && (node->module != node->child->module) && !node->child->module->type) {
Radek Krejci76b07902015-10-09 09:11:25 +0200871 ly_print(out, "%s:", get_module_import_prefix(node->module, node->child->module));
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200872 }
Radek Krejci76b07902015-10-09 09:11:25 +0200873 ly_print(out, "%s {\n",uses->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200874 level++;
Radek Krejcic7c9a6c2015-05-25 16:35:06 +0200875
Radek Krejci76b07902015-10-09 09:11:25 +0200876 yang_print_nacmext(out, level, node, node->module);
877 yang_print_snode_common(out, level, node);
Michal Vasko4773b762015-07-07 12:15:10 +0200878 for (i = 0; i < uses->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200879 yang_print_iffeature(out, level, node->module, uses->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200880 }
Michal Vasko1f0428a2015-07-07 14:55:04 +0200881 if (uses->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200882 yang_print_when(out, level, node->module, uses->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200883 }
Radek Krejcic7c9a6c2015-05-25 16:35:06 +0200884
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200885 for (i = 0; i < uses->refine_size; i++) {
Michal Vaskof9893382015-10-09 14:03:04 +0200886 yang_print_refine(out, level, node->module, &uses->refine[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200887 }
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200888
Michal Vasko6f25f212015-07-07 15:42:07 +0200889 for (i = 0; i < uses->augment_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200890 yang_print_augment(out, level, node->module, &uses->augment[i]);
Michal Vasko6f25f212015-07-07 15:42:07 +0200891 }
892
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200893 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200894 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcic7c9a6c2015-05-25 16:35:06 +0200895}
896
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200897static void
Radek Krejci76b07902015-10-09 09:11:25 +0200898yang_print_input_output(struct lyout *out, int level, struct lys_node *node)
Michal Vasko5bbae102015-06-16 12:16:44 +0200899{
Michal Vaskof4d3d742015-06-16 11:51:09 +0200900 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200901 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200902 struct lys_node_rpc_inout *inout = (struct lys_node_rpc_inout *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +0200903
Radek Krejci76b07902015-10-09 09:11:25 +0200904 ly_print(out, "%*s%s {\n", LEVEL, INDENT, (inout->nodetype == LYS_INPUT ? "input" : "output"));
Michal Vaskof4d3d742015-06-16 11:51:09 +0200905
906 level++;
907 for (i = 0; i < inout->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200908 yang_print_typedef(out, level, node->module, &inout->tpdf[i]);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200909 }
910
Radek Krejci1d82ef62015-08-07 14:44:40 +0200911 LY_TREE_FOR(node->child, sub) {
Michal Vasko15b48702015-07-07 15:49:34 +0200912 /* augment */
Radek Krejci1d82ef62015-08-07 14:44:40 +0200913 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200914 continue;
915 }
Radek Krejci76b07902015-10-09 09:11:25 +0200916 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200917 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
918 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200919 }
920
921 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200922 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200923}
924
925static void
Radek Krejci76b07902015-10-09 09:11:25 +0200926yang_print_rpc(struct lyout *out, int level, struct lys_node *node)
Michal Vaskof4d3d742015-06-16 11:51:09 +0200927{
928 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200929 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200930 struct lys_node_rpc *rpc = (struct lys_node_rpc *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +0200931
Radek Krejci76b07902015-10-09 09:11:25 +0200932 ly_print(out, "%*srpc %s {\n", LEVEL, INDENT, node->name);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200933
934 level++;
Radek Krejci76b07902015-10-09 09:11:25 +0200935 yang_print_snode_common(out, level, node);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200936
Michal Vasko4773b762015-07-07 12:15:10 +0200937 for (i = 0; i < rpc->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200938 yang_print_iffeature(out, level, node->module, rpc->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200939 }
940
Michal Vaskof4d3d742015-06-16 11:51:09 +0200941 for (i = 0; i < rpc->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200942 yang_print_typedef(out, level, node->module, &rpc->tpdf[i]);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200943 }
944
Radek Krejci1d82ef62015-08-07 14:44:40 +0200945 LY_TREE_FOR(node->child, sub) {
Radek Krejci76b07902015-10-09 09:11:25 +0200946 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200947 LYS_GROUPING | LYS_INPUT | LYS_OUTPUT);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200948 }
949
950 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200951 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200952}
953
954static void
Radek Krejci76b07902015-10-09 09:11:25 +0200955yang_print_notif(struct lyout *out, int level, struct lys_node *node)
Michal Vasko7690bc12015-06-16 12:26:05 +0200956{
Michal Vaskof4d3d742015-06-16 11:51:09 +0200957 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200958 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200959 struct lys_node_notif *notif = (struct lys_node_notif *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +0200960
Radek Krejci76b07902015-10-09 09:11:25 +0200961 ly_print(out, "%*snotification %s {\n", LEVEL, INDENT, node->name);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200962
963 level++;
Radek Krejci76b07902015-10-09 09:11:25 +0200964 yang_print_snode_common(out, level, node);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200965
Michal Vasko4773b762015-07-07 12:15:10 +0200966 for (i = 0; i < notif->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200967 yang_print_iffeature(out, level, node->module, notif->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200968 }
969
Michal Vaskof4d3d742015-06-16 11:51:09 +0200970 for (i = 0; i < notif->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200971 yang_print_typedef(out, level, node->module, &notif->tpdf[i]);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200972 }
973
Radek Krejci1d82ef62015-08-07 14:44:40 +0200974 LY_TREE_FOR(node->child, sub) {
Michal Vasko15b48702015-07-07 15:49:34 +0200975 /* augment */
Radek Krejci1d82ef62015-08-07 14:44:40 +0200976 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200977 continue;
978 }
Radek Krejci76b07902015-10-09 09:11:25 +0200979 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200980 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
981 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200982 }
983
984 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200985 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskof4d3d742015-06-16 11:51:09 +0200986}
987
988static void
Radek Krejci76b07902015-10-09 09:11:25 +0200989yang_print_snode(struct lyout *out, int level, struct lys_node *node, int mask)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200990{
Radek Krejci1d82ef62015-08-07 14:44:40 +0200991 switch (node->nodetype & mask) {
Radek Krejci76512572015-08-04 09:47:08 +0200992 case LYS_CONTAINER:
Radek Krejci76b07902015-10-09 09:11:25 +0200993 yang_print_container(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200994 break;
Radek Krejci76512572015-08-04 09:47:08 +0200995 case LYS_CHOICE:
Radek Krejci76b07902015-10-09 09:11:25 +0200996 yang_print_choice(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200997 break;
Radek Krejci76512572015-08-04 09:47:08 +0200998 case LYS_LEAF:
Radek Krejci76b07902015-10-09 09:11:25 +0200999 yang_print_leaf(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001000 break;
Radek Krejci76512572015-08-04 09:47:08 +02001001 case LYS_LEAFLIST:
Radek Krejci76b07902015-10-09 09:11:25 +02001002 yang_print_leaflist(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001003 break;
Radek Krejci76512572015-08-04 09:47:08 +02001004 case LYS_LIST:
Radek Krejci76b07902015-10-09 09:11:25 +02001005 yang_print_list(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001006 break;
Radek Krejci76512572015-08-04 09:47:08 +02001007 case LYS_USES:
Radek Krejci76b07902015-10-09 09:11:25 +02001008 yang_print_uses(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001009 break;
Radek Krejci76512572015-08-04 09:47:08 +02001010 case LYS_GROUPING:
Radek Krejci76b07902015-10-09 09:11:25 +02001011 yang_print_grouping(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001012 break;
Radek Krejci76512572015-08-04 09:47:08 +02001013 case LYS_ANYXML:
Radek Krejci76b07902015-10-09 09:11:25 +02001014 yang_print_anyxml(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001015 break;
Radek Krejci76512572015-08-04 09:47:08 +02001016 case LYS_CASE:
Radek Krejci76b07902015-10-09 09:11:25 +02001017 yang_print_case(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001018 break;
Radek Krejci76512572015-08-04 09:47:08 +02001019 case LYS_INPUT:
1020 case LYS_OUTPUT:
Radek Krejci76b07902015-10-09 09:11:25 +02001021 yang_print_input_output(out, level, node);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001022 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001023 default:
1024 break;
1025 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001026}
1027
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001028int
Radek Krejci76b07902015-10-09 09:11:25 +02001029yang_print_model(struct lyout *out, struct lys_module *module)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001030{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001031 unsigned int i;
1032 int level = 0;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001033#define LEVEL (level*2)
1034
Radek Krejci1d82ef62015-08-07 14:44:40 +02001035 struct lys_node *node;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001036
Michal Vasko116172e2015-07-07 11:54:37 +02001037 if (module->type) {
Radek Krejci76b07902015-10-09 09:11:25 +02001038 ly_print(out, "submodule %s {%s\n", module->name, (module->deviated ? " // DEVIATED" : ""));
Michal Vasko116172e2015-07-07 11:54:37 +02001039 level++;
Radek Krejci76b07902015-10-09 09:11:25 +02001040 ly_print(out, "%*sbelongs-to %s {\n", LEVEL, INDENT, ((struct lys_submodule *)module)->belongsto->name);
Michal Vasko116172e2015-07-07 11:54:37 +02001041 level++;
Radek Krejci76b07902015-10-09 09:11:25 +02001042 ly_print(out, "%*sprefix \"%s\";\n", LEVEL, INDENT, module->prefix);
Michal Vasko116172e2015-07-07 11:54:37 +02001043 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001044 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko116172e2015-07-07 11:54:37 +02001045 } else {
Radek Krejci76b07902015-10-09 09:11:25 +02001046 ly_print(out, "module %s {%s\n", module->name, (module->deviated ? " // DEVIATED" : ""));
Michal Vasko116172e2015-07-07 11:54:37 +02001047 level++;
Radek Krejci76b07902015-10-09 09:11:25 +02001048 ly_print(out, "%*snamespace \"%s\";\n", LEVEL, INDENT, module->ns);
1049 ly_print(out, "%*sprefix \"%s\";\n", LEVEL, INDENT, module->prefix);
Michal Vasko116172e2015-07-07 11:54:37 +02001050 }
Radek Krejcib0594bf2015-05-21 23:51:27 +02001051
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001052 if (module->version) {
Radek Krejci76b07902015-10-09 09:11:25 +02001053 ly_print(out, "%*syang-version %s;\n", LEVEL, INDENT, module->version == 1 ? "1" : "1.1");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001054 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001055
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001056 for (i = 0; i < module->imp_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +02001057 ly_print(out, "%*simport \"%s\" {\n", LEVEL, INDENT, module->imp[i].module->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001058 level++;
Radek Krejci76b07902015-10-09 09:11:25 +02001059 ly_print(out, "%*sprefix \"%s\";\n", LEVEL, INDENT, module->imp[i].prefix);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001060 if (module->imp[i].rev[0]) {
Radek Krejci76b07902015-10-09 09:11:25 +02001061 yang_print_text(out, level, "revision-date", module->imp[i].rev);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001062 }
1063 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001064 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001065 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001066
Michal Vaskoe0af1e22015-07-07 14:02:02 +02001067 for (i = 0; i < module->deviation_size; ++i) {
Radek Krejci76b07902015-10-09 09:11:25 +02001068 yang_print_deviation(out, level, module, &module->deviation[i]);
Michal Vaskoe0af1e22015-07-07 14:02:02 +02001069 }
1070
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001071 for (i = 0; i < module->inc_size; i++) {
1072 if (module->inc[i].rev[0]) {
Radek Krejci76b07902015-10-09 09:11:25 +02001073 ly_print(out, "%*sinclude \"%s\" {\n", LEVEL, INDENT, module->inc[i].submodule->name);
1074 yang_print_text(out, level + 1, "revision-date", module->inc[i].rev);
1075 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001076 } else {
Radek Krejci76b07902015-10-09 09:11:25 +02001077 ly_print(out, "%*sinclude \"%s\";\n", LEVEL, INDENT, module->inc[i].submodule->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001078 }
1079 }
Radek Krejciefaeba32015-05-27 14:30:57 +02001080
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001081 if (module->org) {
Radek Krejci76b07902015-10-09 09:11:25 +02001082 yang_print_text(out, level, "organization", module->org);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001083 }
1084 if (module->contact) {
Radek Krejci76b07902015-10-09 09:11:25 +02001085 yang_print_text(out, level, "contact", module->contact);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001086 }
1087 if (module->dsc) {
Radek Krejci76b07902015-10-09 09:11:25 +02001088 yang_print_text(out, level, "description", module->dsc);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001089 }
1090 if (module->ref) {
Radek Krejci76b07902015-10-09 09:11:25 +02001091 yang_print_text(out, level, "reference", module->ref);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001092 }
1093 for (i = 0; i < module->rev_size; i++) {
1094 if (module->rev[i].dsc || module->rev[i].ref) {
Radek Krejci76b07902015-10-09 09:11:25 +02001095 ly_print(out, "%*srevision \"%s\" {\n", LEVEL, INDENT, module->rev[i].date);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001096 level++;
1097 if (module->rev[i].dsc) {
Radek Krejci76b07902015-10-09 09:11:25 +02001098 yang_print_text(out, level, "description", module->rev[i].dsc);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001099 }
1100 if (module->rev[i].ref) {
Radek Krejci76b07902015-10-09 09:11:25 +02001101 yang_print_text(out, level, "reference", module->rev[i].ref);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001102 }
1103 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001104 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001105 } else {
Radek Krejci76b07902015-10-09 09:11:25 +02001106 yang_print_text(out, level, "revision", module->rev[i].date);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001107 }
1108 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001109
Michal Vasko30f6e912015-07-07 12:24:27 +02001110 for (i = 0; i < module->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +02001111 yang_print_feature(out, level, &module->features[i]);
Michal Vasko30f6e912015-07-07 12:24:27 +02001112 }
1113
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001114 for (i = 0; i < module->ident_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +02001115 yang_print_identity(out, level, &module->ident[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001116 }
Radek Krejci6793db02015-05-22 17:49:54 +02001117
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001118 for (i = 0; i < module->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +02001119 yang_print_typedef(out, level, module, &module->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001120 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001121
Radek Krejci1d82ef62015-08-07 14:44:40 +02001122 LY_TREE_FOR(module->data, node) {
Radek Krejci92720552015-10-05 15:28:27 +02001123 switch(node->nodetype) {
1124 case LYS_RPC:
Radek Krejcibac81762015-10-09 10:19:52 +02001125 yang_print_rpc(out, level, node);
Radek Krejci92720552015-10-05 15:28:27 +02001126 break;
1127 case LYS_NOTIF:
Radek Krejcibac81762015-10-09 10:19:52 +02001128 yang_print_notif(out, level, node);
Radek Krejci92720552015-10-05 15:28:27 +02001129 break;
1130 default:
Radek Krejcibac81762015-10-09 10:19:52 +02001131 yang_print_snode(out, level, node,
Radek Krejci92720552015-10-05 15:28:27 +02001132 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
1133 LYS_USES | LYS_GROUPING | LYS_ANYXML);
1134 break;
1135 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001136 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001137
Michal Vasko6f25f212015-07-07 15:42:07 +02001138 for (i = 0; i < module->augment_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +02001139 yang_print_augment(out, level, module, &module->augment[i]);
Michal Vasko6f25f212015-07-07 15:42:07 +02001140 }
1141
Radek Krejci76b07902015-10-09 09:11:25 +02001142 ly_print(out, "}\n");
Radek Krejcida04f4a2015-05-21 12:54:09 +02001143
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001144 return EXIT_SUCCESS;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001145#undef LEVEL
1146}