blob: 031005d598a3bd3bacebf6bac1ee81398d06c685 [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 *
Radek Krejci54f6fb32016-02-24 12:56:39 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vasko8de098c2016-02-26 10:00:25 +010011 *
Radek Krejci54f6fb32016-02-24 12:56:39 +010012 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejcida04f4a2015-05-21 12:54:09 +020013 */
14
15#include <stdlib.h>
Radek Krejcida04f4a2015-05-21 12:54:09 +020016#include <string.h>
Michal Vasko25cb6c62016-02-12 14:36:21 +010017#include <stdint.h>
Radek Krejcida04f4a2015-05-21 12:54:09 +020018
Radek Krejci998a0b82015-08-17 13:14:36 +020019#include "common.h"
Radek Krejci76b07902015-10-09 09:11:25 +020020#include "printer.h"
Michal Vasko2d162e12015-09-24 14:33:29 +020021#include "tree_schema.h"
Radek Krejcida04f4a2015-05-21 12:54:09 +020022
23#define INDENT ""
24#define LEVEL (level*2)
25
Michal Vasko1e62a092015-12-01 12:27:20 +010026static void yang_print_snode(struct lyout *out, int level, const struct lys_node *node, int mask);
Radek Krejcida04f4a2015-05-21 12:54:09 +020027
Radek Krejci6e4ffbb2015-06-16 10:34:41 +020028static void
Michal Vasko25cb6c62016-02-12 14:36:21 +010029yang_encode(struct lyout *out, const char *text, int len)
30{
31 int i, start_len;
32 const char *start;
33 char special = 0;
34
35 if (!len) {
36 return;
37 }
38
39 if (len < 0) {
40 len = strlen(text);
41 }
42
43 start = text;
44 start_len = 0;
45 for (i = 0; i < len; ++i) {
46 switch (text[i]) {
47 case '\n':
48 case '\t':
49 case '\"':
50 case '\\':
51 special = text[i];
52 break;
53 default:
54 ++start_len;
55 break;
56 }
57
58 if (special) {
59 ly_write(out, start, start_len);
60 switch (special) {
61 case '\n':
62 ly_write(out, "\\n", 2);
63 break;
64 case '\t':
65 ly_write(out, "\\t", 2);
66 break;
67 case '\"':
68 ly_write(out, "\\\"", 2);
69 break;
70 case '\\':
71 ly_write(out, "\\\\", 2);
72 break;
73 }
74
75 start += start_len + 1;
76 start_len = 0;
77
78 special = 0;
79 }
80 }
81
82 ly_write(out, start, start_len);
83}
84
85static void
Radek Krejci32cce7c2015-12-09 16:44:13 +010086yang_print_open(struct lyout *out, int *flag)
87{
88 if (flag && !*flag) {
89 *flag = 1;
90 ly_print(out, " {\n");
91 }
92}
93
94static void
95yang_print_close(struct lyout *out, int level, int flag)
96{
97 if (flag) {
98 ly_print(out, "%*s}\n", LEVEL, INDENT);
99 } else {
100 ly_print(out, ";\n");
101 }
102}
103
104static void
Radek Krejci8d81e002015-12-10 11:18:59 +0100105yang_print_text(struct lyout *out, int level, const char *name, const char *text, int singleline)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200106{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200107 const char *s, *t;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200108
Radek Krejci8d81e002015-12-10 11:18:59 +0100109 if (singleline) {
110 ly_print(out, "%*s%s \"", LEVEL, INDENT, name);
111 } else {
112 ly_print(out, "%*s%s\n", LEVEL, INDENT, name);
113 level++;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200114
Radek Krejci8d81e002015-12-10 11:18:59 +0100115 ly_print(out, "%*s\"", LEVEL, INDENT);
116 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200117 t = text;
118 while ((s = strchr(t, '\n'))) {
Michal Vasko25cb6c62016-02-12 14:36:21 +0100119 yang_encode(out, t, s - t);
120 ly_print(out, "\n");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200121 t = s + 1;
Michal Vasko25cb6c62016-02-12 14:36:21 +0100122 ly_print(out, "%*s ", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200123 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200124
Radek Krejci8d81e002015-12-10 11:18:59 +0100125 ly_print(out, "%s\";\n", t);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200126 level--;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200127
128}
129
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200130static void
Radek Krejci32cce7c2015-12-09 16:44:13 +0100131yang_print_nacmext(struct lyout *out, int level, const struct lys_node *node, const struct lys_module *module, int *flag)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200132{
Radek Krejci6764bb32015-07-03 15:16:04 +0200133 int i, j;
Radek Krejci86909d22015-07-03 16:11:25 +0200134 const char *prefix = NULL;
Radek Krejci6764bb32015-07-03 15:16:04 +0200135
Radek Krejci1d82ef62015-08-07 14:44:40 +0200136 if (node->nacm && (!node->parent || node->parent->nacm != node->nacm)) {
Radek Krejci6764bb32015-07-03 15:16:04 +0200137 /* locate ietf-netconf-acm module in imports */
Michal Vaskodb7aa992015-07-08 09:37:38 +0200138 if (!strcmp(module->name, "ietf-netconf-acm")) {
139 prefix = module->prefix;
Radek Krejci6764bb32015-07-03 15:16:04 +0200140 } else {
141 /* search in imports */
Michal Vaskodb7aa992015-07-08 09:37:38 +0200142 for (i = 0; i < module->imp_size; i++) {
143 if (!strcmp(module->imp[i].module->name, "ietf-netconf-acm")) {
144 prefix = module->imp[i].prefix;
Radek Krejci6764bb32015-07-03 15:16:04 +0200145 break;
146 }
147 }
148 /* and in imports of includes */
149 if (!prefix) {
Michal Vaskodb7aa992015-07-08 09:37:38 +0200150 for (j = 0; j < module->inc_size; j++) {
151 for (i = 0; i < module->inc[j].submodule->imp_size; i++) {
152 if (!strcmp(module->inc[j].submodule->imp[i].module->name, "ietf-netconf-acm")) {
153 prefix = module->inc[j].submodule->imp[i].prefix;
Radek Krejci6764bb32015-07-03 15:16:04 +0200154 break;
155 }
156 }
157 }
158 }
159 }
160
Radek Krejci1d82ef62015-08-07 14:44:40 +0200161 if ((node->nacm & LYS_NACM_DENYW) && (!node->parent || !(node->parent->nacm & LYS_NACM_DENYW))) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100162 yang_print_open(out, flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200163 ly_print(out, "%*s%s:default-deny-write;\n", LEVEL, INDENT, prefix);
Radek Krejci6764bb32015-07-03 15:16:04 +0200164 }
Radek Krejci1d82ef62015-08-07 14:44:40 +0200165 if ((node->nacm & LYS_NACM_DENYA) && (!node->parent || !(node->parent->nacm & LYS_NACM_DENYA))) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100166 yang_print_open(out, flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200167 ly_print(out, "%*s%s:default-deny-all;\n", LEVEL, INDENT, prefix);
Radek Krejci6764bb32015-07-03 15:16:04 +0200168 }
169 }
Radek Krejci6a113852015-07-03 16:04:20 +0200170}
171
172/*
173 * Covers:
174 * description, reference, status
175 */
176static void
Radek Krejci32cce7c2015-12-09 16:44:13 +0100177yang_print_snode_common(struct lyout *out, int level, const struct lys_node *node, int *flag)
Radek Krejci6a113852015-07-03 16:04:20 +0200178{
Radek Krejci1d82ef62015-08-07 14:44:40 +0200179 if (node->flags & LYS_STATUS_CURR) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100180 yang_print_open(out, flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200181 ly_print(out, "%*sstatus \"current\";\n", LEVEL, INDENT);
Radek Krejci1d82ef62015-08-07 14:44:40 +0200182 } else if (node->flags & LYS_STATUS_DEPRC) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100183 yang_print_open(out, flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200184 ly_print(out, "%*sstatus \"deprecated\";\n", LEVEL, INDENT);
Radek Krejci1d82ef62015-08-07 14:44:40 +0200185 } else if (node->flags & LYS_STATUS_OBSLT) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100186 yang_print_open(out, flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200187 ly_print(out, "%*sstatus \"obsolete\";\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200188 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200189
Radek Krejci1d82ef62015-08-07 14:44:40 +0200190 if (node->dsc) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100191 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100192 yang_print_text(out, level, "description", node->dsc, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200193 }
Radek Krejci1d82ef62015-08-07 14:44:40 +0200194 if (node->ref) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100195 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100196 yang_print_text(out, level, "reference", node->ref, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200197 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200198}
199
200/*
201 * Covers:
Michal Vasko2b8faed2015-06-09 12:51:20 +0200202 * config, mandatory
Radek Krejcida04f4a2015-05-21 12:54:09 +0200203 * description, reference, status
204 */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200205static void
Radek Krejci32cce7c2015-12-09 16:44:13 +0100206yang_print_snode_common2(struct lyout *out, int level, const struct lys_node *node, int *flag)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200207{
Michal Vasko43271ee2016-02-05 14:27:09 +0100208 if (node->parent) {
209 if ((node->parent->flags & LYS_CONFIG_MASK) != (node->flags & LYS_CONFIG_MASK)) {
210 /* print config when it differs from the parent ... */
211 if (node->flags & LYS_CONFIG_W) {
212 yang_print_open(out, flag);
213 ly_print(out, "%*sconfig true;\n", LEVEL, INDENT);
214 } else if (node->flags & LYS_CONFIG_R) {
215 yang_print_open(out, flag);
216 ly_print(out, "%*sconfig false;\n", LEVEL, INDENT);
217 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200218 }
Michal Vasko43271ee2016-02-05 14:27:09 +0100219 } else if (node->flags & LYS_CONFIG_R) {
220 /* ... or it's a top-level state node */
221 yang_print_open(out, flag);
222 ly_print(out, "%*sconfig false;\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200223 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200224
Radek Krejci1d82ef62015-08-07 14:44:40 +0200225 if (node->flags & LYS_MAND_TRUE) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100226 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100227 ly_print(out, "%*smandatory true;\n", LEVEL, INDENT);
Radek Krejci1d82ef62015-08-07 14:44:40 +0200228 } else if (node->flags & LYS_MAND_FALSE) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100229 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100230 ly_print(out, "%*smandatory false;\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200231 }
Michal Vasko2b8faed2015-06-09 12:51:20 +0200232
Radek Krejci32cce7c2015-12-09 16:44:13 +0100233 yang_print_snode_common(out, level, node, flag);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200234}
235
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200236static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100237yang_print_iffeature(struct lyout *out, int level, const struct lys_module *module, const struct lys_feature *feat)
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200238{
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100239 struct lys_module *mod;
240
Radek Krejci76b07902015-10-09 09:11:25 +0200241 ly_print(out, "%*sif-feature ", LEVEL, INDENT);
Michal Vasko6c629ac2016-02-15 14:08:23 +0100242 mod = lys_module(feat->module);
Michal Vasko1dae8ec2016-02-15 14:49:01 +0100243 if (lys_module(module) != mod) {
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100244 ly_print(out, "%s:", transform_module_name2import_prefix(module, mod->name));
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200245 }
Radek Krejci76b07902015-10-09 09:11:25 +0200246 ly_print(out, "%s;\n", feat->name);
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200247}
248
249static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100250yang_print_feature(struct lyout *out, int level, const struct lys_feature *feat)
Michal Vasko4773b762015-07-07 12:15:10 +0200251{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100252 int i, flag = 0;
Michal Vasko30f6e912015-07-07 12:24:27 +0200253
Radek Krejci32cce7c2015-12-09 16:44:13 +0100254 ly_print(out, "%*sfeature %s", LEVEL, INDENT, feat->name);
Michal Vasko4773b762015-07-07 12:15:10 +0200255 level++;
256
Radek Krejci32cce7c2015-12-09 16:44:13 +0100257 yang_print_snode_common(out, level, (struct lys_node *)feat, &flag);
Michal Vasko30f6e912015-07-07 12:24:27 +0200258 for (i = 0; i < feat->features_size; ++i) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100259 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200260 yang_print_iffeature(out, level, feat->module, feat->features[i]);
Michal Vasko30f6e912015-07-07 12:24:27 +0200261 }
Michal Vasko4773b762015-07-07 12:15:10 +0200262
263 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100264 yang_print_close(out, level, flag);
Michal Vasko4773b762015-07-07 12:15:10 +0200265}
266
267static void
Radek Krejci32cce7c2015-12-09 16:44:13 +0100268yang_print_restr(struct lyout *out, int level, const struct lys_restr *restr, int *flag)
Radek Krejci41726f92015-06-19 13:11:05 +0200269{
Radek Krejci0bd5db42015-06-19 13:30:07 +0200270 if (restr->dsc != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100271 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100272 yang_print_text(out, level, "description", restr->dsc, 0);
Radek Krejci41726f92015-06-19 13:11:05 +0200273 }
Radek Krejci0bd5db42015-06-19 13:30:07 +0200274 if (restr->ref != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100275 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100276 yang_print_text(out, level, "reference", restr->ref, 0);
Radek Krejci41726f92015-06-19 13:11:05 +0200277 }
Radek Krejci0bd5db42015-06-19 13:30:07 +0200278 if (restr->eapptag != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100279 yang_print_open(out, flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200280 ly_print(out, "%*serror-app-tag \"%s\";\n", LEVEL, INDENT, restr->eapptag);
Radek Krejci41726f92015-06-19 13:11:05 +0200281 }
Radek Krejci0bd5db42015-06-19 13:30:07 +0200282 if (restr->emsg != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100283 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100284 yang_print_text(out, level, "error-message", restr->emsg, 0);
Radek Krejci41726f92015-06-19 13:11:05 +0200285 }
286}
287
288static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100289yang_print_when(struct lyout *out, int level, const struct lys_module *module, const struct lys_when *when)
Michal Vasko1f0428a2015-07-07 14:55:04 +0200290{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100291 int flag = 0;
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100292 const char *str;
Michal Vaskof9893382015-10-09 14:03:04 +0200293
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100294 str = transform_json2schema(module, when->cond);
295 if (!str) {
Michal Vaskof9893382015-10-09 14:03:04 +0200296 ly_print(out, "(!error!)");
297 return;
298 }
299
Michal Vasko25cb6c62016-02-12 14:36:21 +0100300 ly_print(out, "%*swhen \"", LEVEL, INDENT);
301 yang_encode(out, str, -1);
302 ly_print(out, "\"");
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100303 lydict_remove(module->ctx, str);
Michal Vaskof9893382015-10-09 14:03:04 +0200304
Michal Vasko1f0428a2015-07-07 14:55:04 +0200305 level++;
306 if (when->dsc) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100307 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100308 yang_print_text(out, level, "description", when->dsc, 0);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200309 }
310 if (when->ref) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100311 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100312 yang_print_text(out, level, "reference", when->ref, 0);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200313 }
314 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100315 yang_print_close(out, level, flag);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200316}
317
318static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100319yang_print_type(struct lyout *out, int level, const struct lys_module *module, const struct lys_type *type)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200320{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200321 int i;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100322 int flag = 0, flag2;
Michal Vasko0fb82c62015-10-20 13:41:53 +0200323 const char *str;
Radek Krejcic071c542016-01-27 14:57:51 +0100324 struct lys_module *mod;
Radek Krejci25d782a2015-05-22 15:03:23 +0200325
Michal Vasko0fb82c62015-10-20 13:41:53 +0200326 if (type->module_name) {
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100327 ly_print(out, "%*stype %s:%s", LEVEL, INDENT,
328 transform_module_name2import_prefix(module, type->module_name), type->der->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200329 } else {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100330 ly_print(out, "%*stype %s", LEVEL, INDENT, type->der->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200331 }
332 level++;
333 switch (type->base) {
334 case LY_TYPE_BINARY:
Michal Vasko4634cda2016-02-16 09:22:09 +0100335 if (type->info.binary.length) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100336 yang_print_open(out, &flag);
Michal Vasko25cb6c62016-02-12 14:36:21 +0100337 ly_print(out, "%*slength \"", LEVEL, INDENT);
338 yang_encode(out, type->info.binary.length->expr, -1);
339 ly_print(out, "\"");
Radek Krejci32cce7c2015-12-09 16:44:13 +0100340 flag2 = 0;
341 yang_print_restr(out, level + 1, type->info.binary.length, &flag2);
342 yang_print_close(out, level, flag2);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200343 }
344 break;
345 case LY_TYPE_BITS:
346 for (i = 0; i < type->info.bits.count; ++i) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100347 yang_print_open(out, &flag);
Michal Vasko3f053ef2016-02-12 14:27:13 +0100348 ly_print(out, "%*sbit %s", LEVEL, INDENT, type->info.bits.bit[i].name);
349 flag2 = 0;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200350 level++;
Michal Vasko3f053ef2016-02-12 14:27:13 +0100351 yang_print_snode_common(out, level, (struct lys_node *)&type->info.bits.bit[i], &flag2);
352 if (!(type->info.bits.bit[i].flags & LYS_AUTOASSIGNED)) {
353 yang_print_open(out, &flag2);
354 ly_print(out, "%*sposition %u;\n", LEVEL, INDENT, type->info.bits.bit[i].pos);
355 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200356 level--;
Michal Vasko3f053ef2016-02-12 14:27:13 +0100357 yang_print_close(out, level, flag2);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200358 }
359 break;
360 case LY_TYPE_DEC64:
Michal Vasko4634cda2016-02-16 09:22:09 +0100361 if (type->info.dec64.dig) {
362 yang_print_open(out, &flag);
363 ly_print(out, "%*sfraction-digits %d;\n", LEVEL, INDENT, type->info.dec64.dig);
364 }
Michal Vaskoea505ee2015-07-07 14:01:00 +0200365 if (type->info.dec64.range != NULL) {
Michal Vasko4634cda2016-02-16 09:22:09 +0100366 yang_print_open(out, &flag);
Michal Vasko25cb6c62016-02-12 14:36:21 +0100367 ly_print(out, "%*srange \"", LEVEL, INDENT);
368 yang_encode(out, type->info.dec64.range->expr, -1);
369 ly_print(out, "\"");
Radek Krejci32cce7c2015-12-09 16:44:13 +0100370 flag2 = 0;
371 yang_print_restr(out, level + 1, type->info.dec64.range, &flag2);
372 yang_print_close(out, level, flag2);
Michal Vaskoea505ee2015-07-07 14:01:00 +0200373 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200374 break;
375 case LY_TYPE_ENUM:
376 for (i = 0; i < type->info.enums.count; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100377 yang_print_open(out, &flag);
Michal Vasko3f053ef2016-02-12 14:27:13 +0100378 ly_print(out, "%*senum \"%s\"", LEVEL, INDENT, type->info.enums.enm[i].name);
379 flag2 = 0;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200380 level++;
Michal Vasko3f053ef2016-02-12 14:27:13 +0100381 yang_print_snode_common(out, level, (struct lys_node *)&type->info.enums.enm[i], &flag2);
382 if (!(type->info.enums.enm[i].flags & LYS_AUTOASSIGNED)) {
383 yang_print_open(out, &flag2);
384 ly_print(out, "%*svalue %d;\n", LEVEL, INDENT, type->info.enums.enm[i].value);
385 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200386 level--;
Michal Vasko3f053ef2016-02-12 14:27:13 +0100387 yang_print_close(out, level, flag2);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200388 }
389 break;
390 case LY_TYPE_IDENT:
Michal Vasko4634cda2016-02-16 09:22:09 +0100391 if (type->info.ident.ref) {
392 yang_print_open(out, &flag);
393 mod = lys_module(type->info.ident.ref->module);
394 if (lys_module(module) == mod) {
395 ly_print(out, "%*sbase %s;\n", LEVEL, INDENT, type->info.ident.ref->name);
396 } else {
397 ly_print(out, "%*sbase %s:%s;\n", LEVEL, INDENT, transform_module_name2import_prefix(module, mod->name),
398 type->info.ident.ref->name);
399 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200400 }
401 break;
402 case LY_TYPE_INST:
Radek Krejciaf351422015-06-19 14:49:38 +0200403 if (type->info.inst.req == 1) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100404 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100405 ly_print(out, "%*srequire-instance true;\n", LEVEL, INDENT);
Radek Krejciaf351422015-06-19 14:49:38 +0200406 } else if (type->info.inst.req == -1) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100407 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100408 ly_print(out, "%*srequire-instance false;\n", LEVEL, INDENT);
Radek Krejciaf351422015-06-19 14:49:38 +0200409 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200410 break;
411 case LY_TYPE_INT8:
412 case LY_TYPE_INT16:
413 case LY_TYPE_INT32:
414 case LY_TYPE_INT64:
415 case LY_TYPE_UINT8:
416 case LY_TYPE_UINT16:
417 case LY_TYPE_UINT32:
418 case LY_TYPE_UINT64:
Michal Vasko4634cda2016-02-16 09:22:09 +0100419 if (type->info.num.range) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100420 yang_print_open(out, &flag);
Michal Vasko25cb6c62016-02-12 14:36:21 +0100421 ly_print(out, "%*srange \"", LEVEL, INDENT);
422 yang_encode(out, type->info.num.range->expr, -1);
423 ly_print(out, "\"");
Radek Krejci32cce7c2015-12-09 16:44:13 +0100424 flag2 = 0;
425 yang_print_restr(out, level + 1, type->info.num.range, &flag2);
426 yang_print_close(out, level, flag2);
Radek Krejcif2860132015-06-20 12:37:20 +0200427 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200428 break;
429 case LY_TYPE_LEAFREF:
Michal Vasko4634cda2016-02-16 09:22:09 +0100430 if (type->info.lref.path) {
431 yang_print_open(out, &flag);
432 str = transform_json2schema(module, type->info.lref.path);
433 ly_print(out, "%*spath \"%s\";\n", LEVEL, INDENT, str);
434 lydict_remove(module->ctx, str);
435 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200436 break;
437 case LY_TYPE_STRING:
Radek Krejci5fbc9162015-06-19 14:11:11 +0200438 if (type->info.str.length) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100439 yang_print_open(out, &flag);
Michal Vasko25cb6c62016-02-12 14:36:21 +0100440 ly_print(out, "%*slength \"", LEVEL, INDENT);
441 yang_encode(out, type->info.str.length->expr, -1);
442 ly_print(out, "\"");
Radek Krejci32cce7c2015-12-09 16:44:13 +0100443 flag2 = 0;
444 yang_print_restr(out, level + 1, type->info.str.length, &flag2);
445 yang_print_close(out, level, flag2);
Radek Krejci061bd522015-06-19 13:45:16 +0200446 }
Radek Krejci5fbc9162015-06-19 14:11:11 +0200447 for (i = 0; i < type->info.str.pat_count; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100448 yang_print_open(out, &flag);
Michal Vasko25cb6c62016-02-12 14:36:21 +0100449 ly_print(out, "%*spattern \"", LEVEL, INDENT);
450 yang_encode(out, type->info.str.patterns[i].expr, -1);
451 ly_print(out, "\"");
Radek Krejci32cce7c2015-12-09 16:44:13 +0100452 flag2 = 0;
453 yang_print_restr(out, level + 1, &type->info.str.patterns[i], &flag2);
454 yang_print_close(out, level, flag2);
Radek Krejci5fbc9162015-06-19 14:11:11 +0200455 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200456 break;
457 case LY_TYPE_UNION:
458 for (i = 0; i < type->info.uni.count; ++i) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100459 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200460 yang_print_type(out, level, module, &type->info.uni.types[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200461 }
462 break;
463 default:
464 /* other types do not have substatements */
465 break;
466 }
467 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100468 yang_print_close(out, level, flag);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200469}
470
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200471static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100472yang_print_must(struct lyout *out, int level, const struct lys_module *module, const struct lys_restr *must)
Michal Vasko7f976ee2015-06-09 13:55:41 +0200473{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100474 int flag = 0;
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100475 const char *str;
Michal Vaskof9893382015-10-09 14:03:04 +0200476
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100477 str = transform_json2schema(module, must->expr);
478 if (!str) {
Michal Vaskof9893382015-10-09 14:03:04 +0200479 ly_print(out, "(!error!)");
480 return;
481 }
482
Michal Vasko25cb6c62016-02-12 14:36:21 +0100483 ly_print(out, "%*smust \"", LEVEL, INDENT);
484 yang_encode(out, str, -1);
485 ly_print(out, "\"");
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100486 lydict_remove(module->ctx, str);
Michal Vaskof9893382015-10-09 14:03:04 +0200487
Radek Krejci32cce7c2015-12-09 16:44:13 +0100488 yang_print_restr(out, level + 1, must, &flag);
489 yang_print_close(out, level, flag);
Michal Vasko7f976ee2015-06-09 13:55:41 +0200490}
491
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200492static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100493yang_print_unique(struct lyout *out, int level, const struct lys_unique *uniq)
Michal Vasko1ef07972015-07-07 14:01:35 +0200494{
495 int i;
496
Radek Krejci76b07902015-10-09 09:11:25 +0200497 ly_print(out, "%*sunique \"", LEVEL, INDENT);
Radek Krejci581ce772015-11-10 17:22:40 +0100498 for (i = 0; i < uniq->expr_size; i++) {
499 ly_print(out, "%s%s", uniq->expr[i], i + 1 < uniq->expr_size ? " " : "");
Michal Vasko1ef07972015-07-07 14:01:35 +0200500 }
Radek Krejci76b07902015-10-09 09:11:25 +0200501 ly_print(out, "\";\n");
Michal Vasko1ef07972015-07-07 14:01:35 +0200502}
503
504static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100505yang_print_refine(struct lyout *out, int level, const struct lys_module *module, const struct lys_refine *refine)
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200506{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200507 int i;
Michal Vaskoa8b25952015-10-20 15:30:25 +0200508 const char *str;
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200509
Michal Vasko5d112852016-02-12 16:47:13 +0100510 str = transform_json2schema(module, refine->target_name);
Michal Vaskoa8b25952015-10-20 15:30:25 +0200511 ly_print(out, "%*srefine \"%s\" {\n", LEVEL, INDENT, str);
512 lydict_remove(module->ctx, str);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200513 level++;
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200514
Radek Krejci1574a8d2015-08-03 14:16:52 +0200515 if (refine->flags & LYS_CONFIG_W) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100516 ly_print(out, "%*sconfig true;\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200517 } else if (refine->flags & LYS_CONFIG_R) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100518 ly_print(out, "%*sconfig false;\n", LEVEL, INDENT);
Radek Krejci6a113852015-07-03 16:04:20 +0200519 }
520
Radek Krejci1574a8d2015-08-03 14:16:52 +0200521 if (refine->flags & LYS_MAND_TRUE) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100522 ly_print(out, "%*smandatory true;\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200523 } else if (refine->flags & LYS_MAND_FALSE) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100524 ly_print(out, "%*smandatory false;\n", LEVEL, INDENT);
Radek Krejci6a113852015-07-03 16:04:20 +0200525 }
526
Radek Krejci32cce7c2015-12-09 16:44:13 +0100527 yang_print_snode_common(out, level, (struct lys_node *)refine, NULL);
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200528
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200529 for (i = 0; i < refine->must_size; ++i) {
Michal Vaskof9893382015-10-09 14:03:04 +0200530 yang_print_must(out, level, module, &refine->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200531 }
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200532
Radek Krejci76512572015-08-04 09:47:08 +0200533 if (refine->target_type & (LYS_LEAF | LYS_CHOICE)) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200534 if (refine->mod.dflt != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200535 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, refine->mod.dflt);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200536 }
Radek Krejci76512572015-08-04 09:47:08 +0200537 } else if (refine->target_type == LYS_CONTAINER) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200538 if (refine->mod.presence != NULL) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100539 yang_print_text(out, level, "presence", refine->mod.presence, 1);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200540 }
Radek Krejci76512572015-08-04 09:47:08 +0200541 } else if (refine->target_type & (LYS_LIST | LYS_LEAFLIST)) {
Radek Krejci0d7b2472016-02-12 11:11:03 +0100542 /* magic - bit 3 in flags means min set, bit 4 says max set */
543 if (refine->flags & 0x04) {
Radek Krejci76b07902015-10-09 09:11:25 +0200544 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, refine->mod.list.min);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200545 }
Radek Krejci0d7b2472016-02-12 11:11:03 +0100546 if (refine->flags & 0x08) {
547 if (refine->mod.list.max) {
548 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, refine->mod.list.max);
549 } else {
550 ly_print(out, "%*smax-elements \"unbounded\";\n", LEVEL, INDENT);
551 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200552 }
553 }
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200554
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200555 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200556 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200557}
558
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200559static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100560yang_print_deviation(struct lyout *out, int level, const struct lys_module *module,
561 const struct lys_deviation *deviation)
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200562{
563 int i, j;
Michal Vaskoa8b25952015-10-20 15:30:25 +0200564 const char *str;
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200565
Michal Vasko5d112852016-02-12 16:47:13 +0100566 str = transform_json2schema(module, deviation->target_name);
Michal Vaskoa8b25952015-10-20 15:30:25 +0200567 ly_print(out, "%*sdeviation \"%s\" {\n", LEVEL, INDENT, str);
568 lydict_remove(module->ctx, str);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200569 level++;
570
571 if (deviation->dsc) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100572 yang_print_text(out, level, "description", deviation->dsc, 0);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200573 }
574 if (deviation->ref) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100575 yang_print_text(out, level, "reference", deviation->ref, 0);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200576 }
577
578 for (i = 0; i < deviation->deviate_size; ++i) {
Radek Krejci76b07902015-10-09 09:11:25 +0200579 ly_print(out, "%*sdeviate ", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200580 if (deviation->deviate[i].mod == LY_DEVIATE_NO) {
Radek Krejci76b07902015-10-09 09:11:25 +0200581 ly_print(out, "not-supported {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200582 } else if (deviation->deviate[i].mod == LY_DEVIATE_ADD) {
Radek Krejci76b07902015-10-09 09:11:25 +0200583 ly_print(out, "add {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200584 } else if (deviation->deviate[i].mod == LY_DEVIATE_RPL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200585 ly_print(out, "replace {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200586 } else if (deviation->deviate[i].mod == LY_DEVIATE_DEL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200587 ly_print(out, "delete {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200588 }
589 level++;
590
Radek Krejci1574a8d2015-08-03 14:16:52 +0200591 if (deviation->deviate[i].flags & LYS_CONFIG_W) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100592 ly_print(out, "%*sconfig true;\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200593 } else if (deviation->deviate[i].flags & LYS_CONFIG_R) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100594 ly_print(out, "%*sconfig false;\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200595 }
596
Radek Krejci1574a8d2015-08-03 14:16:52 +0200597 if (deviation->deviate[i].flags & LYS_MAND_TRUE) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100598 ly_print(out, "%*smandatory true;\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200599 } else if (deviation->deviate[i].flags & LYS_MAND_FALSE) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100600 ly_print(out, "%*smandatory false;\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200601 }
602
603 if (deviation->deviate[i].dflt) {
Radek Krejci76b07902015-10-09 09:11:25 +0200604 ly_print(out, "%*sdefault %s;\n", LEVEL, INDENT, deviation->deviate[i].dflt);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200605 }
606
Radek Krejci0d7b2472016-02-12 11:11:03 +0100607 if (deviation->deviate[i].min_set) {
Radek Krejci76b07902015-10-09 09:11:25 +0200608 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, deviation->deviate[i].min);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200609 }
Radek Krejci0d7b2472016-02-12 11:11:03 +0100610 if (deviation->deviate[i].max_set) {
611 if (deviation->deviate[i].max) {
612 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, deviation->deviate[i].max);
613 } else {
614 ly_print(out, "%*smax-elements \"unbounded\";\n", LEVEL, INDENT);
615 }
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200616 }
617
618 for (j = 0; j < deviation->deviate[i].must_size; ++j) {
Michal Vaskof9893382015-10-09 14:03:04 +0200619 yang_print_must(out, level, module, &deviation->deviate[i].must[j]);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200620 }
621
622 for (j = 0; j < deviation->deviate[i].unique_size; ++j) {
Radek Krejci76b07902015-10-09 09:11:25 +0200623 yang_print_unique(out, level, &deviation->deviate[i].unique[j]);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200624 }
625
626 if (deviation->deviate[i].type) {
Radek Krejci76b07902015-10-09 09:11:25 +0200627 yang_print_type(out, level, module, deviation->deviate[i].type);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200628 }
629
630 if (deviation->deviate[i].units) {
Radek Krejci76b07902015-10-09 09:11:25 +0200631 ly_print(out, "%*sunits %s;\n", LEVEL, INDENT, deviation->deviate[i].units);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200632 }
633
634 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200635 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200636 }
637
638 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200639 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200640}
641
642static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100643yang_print_augment(struct lyout *out, int level, const struct lys_module *module,
644 const struct lys_node_augment *augment)
Michal Vasko6f25f212015-07-07 15:42:07 +0200645{
646 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200647 struct lys_node *sub;
Michal Vasko488c19e2015-10-20 15:21:00 +0200648 const char *str;
Michal Vasko6f25f212015-07-07 15:42:07 +0200649
Michal Vasko5d112852016-02-12 16:47:13 +0100650 str = transform_json2schema(module, augment->target_name);
Michal Vasko488c19e2015-10-20 15:21:00 +0200651 ly_print(out, "%*saugment \"%s\" {\n", LEVEL, INDENT, str);
652 lydict_remove(module->ctx, str);
Michal Vasko6f25f212015-07-07 15:42:07 +0200653 level++;
654
Radek Krejci32cce7c2015-12-09 16:44:13 +0100655 yang_print_nacmext(out, level, (struct lys_node *)augment, module, NULL);
656 yang_print_snode_common(out, level, (struct lys_node *)augment, NULL);
Michal Vasko6f25f212015-07-07 15:42:07 +0200657
658 for (i = 0; i < augment->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200659 yang_print_iffeature(out, level, module, augment->features[i]);
Michal Vasko6f25f212015-07-07 15:42:07 +0200660 }
661
662 if (augment->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200663 yang_print_when(out, level, module, augment->when);
Michal Vasko6f25f212015-07-07 15:42:07 +0200664 }
665
666 LY_TREE_FOR(augment->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100667 /* only our augment */
668 if (sub->parent != (struct lys_node *)augment) {
669 continue;
670 }
Radek Krejci76b07902015-10-09 09:11:25 +0200671 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200672 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
673 LYS_USES | LYS_ANYXML | LYS_CASE);
Michal Vasko6f25f212015-07-07 15:42:07 +0200674 }
675
676 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200677 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko6f25f212015-07-07 15:42:07 +0200678}
679
680static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100681yang_print_typedef(struct lyout *out, int level, const struct lys_module *module, const struct lys_tpdf *tpdf)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200682{
Radek Krejci76b07902015-10-09 09:11:25 +0200683 ly_print(out, "%*stypedef %s {\n", LEVEL, INDENT, tpdf->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200684 level++;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200685
Radek Krejci32cce7c2015-12-09 16:44:13 +0100686 yang_print_snode_common(out, level, (struct lys_node *)tpdf, NULL);
Radek Krejci76b07902015-10-09 09:11:25 +0200687 yang_print_type(out, level, module, &tpdf->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200688 if (tpdf->units != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200689 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, tpdf->units);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200690 }
691 if (tpdf->dflt != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200692 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, tpdf->dflt);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200693 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200694
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200695 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200696 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200697}
698
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200699static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100700yang_print_identity(struct lyout *out, int level, const struct lys_ident *ident)
Radek Krejci6793db02015-05-22 17:49:54 +0200701{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100702 int flag = 0;
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100703 struct lys_module *mod;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100704
705 ly_print(out, "%*sidentity %s", LEVEL, INDENT, ident->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200706 level++;
Radek Krejci6793db02015-05-22 17:49:54 +0200707
Radek Krejci32cce7c2015-12-09 16:44:13 +0100708 yang_print_snode_common(out, level, (struct lys_node *)ident, &flag);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200709 if (ident->base) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100710 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200711 ly_print(out, "%*sbase ", LEVEL, INDENT);
Michal Vasko6c629ac2016-02-15 14:08:23 +0100712 mod = lys_module(ident->base->module);
Michal Vasko1dae8ec2016-02-15 14:49:01 +0100713 if (lys_module(ident->module) != mod) {
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100714 ly_print(out, "%s:", transform_module_name2import_prefix(ident->module, mod->name));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200715 }
Radek Krejci76b07902015-10-09 09:11:25 +0200716 ly_print(out, "%s;\n", ident->base->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200717 }
Radek Krejci6793db02015-05-22 17:49:54 +0200718
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200719 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100720 yang_print_close(out, level, flag);
Radek Krejci6793db02015-05-22 17:49:54 +0200721}
722
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200723static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100724yang_print_container(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200725{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100726 int i, flag = 0;
Radek Krejci76512572015-08-04 09:47:08 +0200727 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200728 struct lys_node_container *cont = (struct lys_node_container *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200729
Radek Krejci32cce7c2015-12-09 16:44:13 +0100730 ly_print(out, "%*scontainer %s", LEVEL, INDENT, node->name);
Michal Vasko7f976ee2015-06-09 13:55:41 +0200731
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200732 level++;
Radek Krejci6a113852015-07-03 16:04:20 +0200733
Radek Krejci32cce7c2015-12-09 16:44:13 +0100734 yang_print_nacmext(out, level, node, node->module, &flag);
Radek Krejci6a113852015-07-03 16:04:20 +0200735
Radek Krejci8d81e002015-12-10 11:18:59 +0100736 if (cont->when) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100737 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100738 yang_print_when(out, level, node->module, cont->when);
739 }
740
741 for (i = 0; i < cont->features_size; i++) {
742 yang_print_open(out, &flag);
743 yang_print_iffeature(out, level, node->module, cont->features[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200744 }
Michal Vasko7f976ee2015-06-09 13:55:41 +0200745
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200746 for (i = 0; i < cont->must_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100747 yang_print_open(out, &flag);
Michal Vaskof9893382015-10-09 14:03:04 +0200748 yang_print_must(out, level, node->module, &cont->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200749 }
Michal Vasko7f976ee2015-06-09 13:55:41 +0200750
Radek Krejci8d81e002015-12-10 11:18:59 +0100751 if (cont->presence != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100752 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100753 yang_print_text(out, level, "presence", cont->presence, 1);
Michal Vasko4773b762015-07-07 12:15:10 +0200754 }
755
Radek Krejci8d81e002015-12-10 11:18:59 +0100756 yang_print_snode_common2(out, level, node, &flag);
757
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200758 for (i = 0; i < cont->tpdf_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100759 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200760 yang_print_typedef(out, level, node->module, &cont->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200761 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200762
Radek Krejci1d82ef62015-08-07 14:44:40 +0200763 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100764 /* augments */
765 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200766 continue;
767 }
Radek Krejci32cce7c2015-12-09 16:44:13 +0100768 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200769 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200770 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
771 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200772 }
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200773
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200774 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100775 yang_print_close(out, level, flag);
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200776}
777
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200778static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100779yang_print_case(struct lyout *out, int level, const struct lys_node *node)
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200780{
Michal Vasko4773b762015-07-07 12:15:10 +0200781 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200782 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200783 struct lys_node_case *cas = (struct lys_node_case *)node;
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200784
Radek Krejci76b07902015-10-09 09:11:25 +0200785 ly_print(out, "%*scase %s {\n", LEVEL, INDENT, cas->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200786 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100787 yang_print_nacmext(out, level, node, node->module, NULL);
788 yang_print_snode_common2(out, level, node, NULL);
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200789
Michal Vasko4773b762015-07-07 12:15:10 +0200790 for (i = 0; i < cas->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200791 yang_print_iffeature(out, level, node->module, cas->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200792 }
793
Michal Vasko1f0428a2015-07-07 14:55:04 +0200794 if (cas->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200795 yang_print_when(out, level, node->module, cas->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200796 }
797
Radek Krejci1d82ef62015-08-07 14:44:40 +0200798 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100799 /* augments */
800 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200801 continue;
802 }
Radek Krejci76b07902015-10-09 09:11:25 +0200803 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200804 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
805 LYS_USES | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200806 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200807
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200808 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200809 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200810}
811
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200812static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100813yang_print_choice(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200814{
Michal Vasko4773b762015-07-07 12:15:10 +0200815 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200816 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200817 struct lys_node_choice *choice = (struct lys_node_choice *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200818
Radek Krejci76b07902015-10-09 09:11:25 +0200819 ly_print(out, "%*schoice %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +0200820
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200821 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100822 yang_print_nacmext(out, level, node, node->module, NULL);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200823 if (choice->dflt != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200824 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, choice->dflt->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200825 }
Michal Vasko16083662015-06-09 14:00:45 +0200826
Radek Krejci32cce7c2015-12-09 16:44:13 +0100827 yang_print_snode_common2(out, level, node, NULL);
Michal Vasko4773b762015-07-07 12:15:10 +0200828
829 for (i = 0; i < choice->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200830 yang_print_iffeature(out, level, node->module, choice->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200831 }
832
Michal Vasko1f0428a2015-07-07 14:55:04 +0200833 if (choice->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200834 yang_print_when(out, level, node->module, choice->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200835 }
836
Radek Krejci1d82ef62015-08-07 14:44:40 +0200837 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100838 /* augments */
839 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200840 continue;
841 }
Radek Krejci76b07902015-10-09 09:11:25 +0200842 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200843 LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYXML | LYS_CASE);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200844 }
845 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200846 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200847}
848
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200849static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100850yang_print_leaf(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200851{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200852 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200853 struct lys_node_leaf *leaf = (struct lys_node_leaf *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200854
Radek Krejci76b07902015-10-09 09:11:25 +0200855 ly_print(out, "%*sleaf %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +0200856
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200857 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100858 yang_print_nacmext(out, level, node, node->module, NULL);
Radek Krejci8d81e002015-12-10 11:18:59 +0100859 if (leaf->when) {
860 yang_print_when(out, level, node->module, leaf->when);
861 }
Michal Vasko4773b762015-07-07 12:15:10 +0200862 for (i = 0; i < leaf->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200863 yang_print_iffeature(out, level, node->module, leaf->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200864 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200865 for (i = 0; i < leaf->must_size; i++) {
Michal Vaskof9893382015-10-09 14:03:04 +0200866 yang_print_must(out, level, node->module, &leaf->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200867 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100868 yang_print_snode_common2(out, level, node, NULL);
Radek Krejci76b07902015-10-09 09:11:25 +0200869 yang_print_type(out, level, node->module, &leaf->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200870 if (leaf->units != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200871 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, leaf->units);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200872 }
873 if (leaf->dflt != NULL) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100874 switch (leaf->type.base) {
875 case LY_TYPE_STRING:
876 case LY_TYPE_LEAFREF:
877 case LY_TYPE_IDENT:
878 case LY_TYPE_UNION:
879 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, leaf->dflt);
880 break;
881 default:
882 ly_print(out, "%*sdefault %s;\n", LEVEL, INDENT, leaf->dflt);
883 break;
884 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200885 }
886 level--;
Michal Vasko16083662015-06-09 14:00:45 +0200887
Radek Krejci76b07902015-10-09 09:11:25 +0200888 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko16083662015-06-09 14:00:45 +0200889}
890
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200891static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100892yang_print_anyxml(struct lyout *out, int level, const struct lys_node *node)
Michal Vasko16083662015-06-09 14:00:45 +0200893{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100894 int i, flag = 0;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200895 struct lys_node_anyxml *anyxml = (struct lys_node_anyxml *)node;
Michal Vasko16083662015-06-09 14:00:45 +0200896
Radek Krejci32cce7c2015-12-09 16:44:13 +0100897 ly_print(out, "%*sanyxml %s", LEVEL, INDENT, anyxml->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200898 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100899 yang_print_nacmext(out, level, node, node->module, &flag);
900 yang_print_snode_common2(out, level, node, &flag);
Michal Vasko4773b762015-07-07 12:15:10 +0200901 for (i = 0; i < anyxml->features_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100902 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200903 yang_print_iffeature(out, level, node->module, anyxml->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200904 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200905 for (i = 0; i < anyxml->must_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100906 yang_print_open(out, &flag);
Michal Vaskof9893382015-10-09 14:03:04 +0200907 yang_print_must(out, level, node->module, &anyxml->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200908 }
Michal Vasko1f0428a2015-07-07 14:55:04 +0200909 if (anyxml->when) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100910 yang_print_open(out, &flag);
Michal Vaskof9893382015-10-09 14:03:04 +0200911 yang_print_when(out, level, node->module, anyxml->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200912 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200913 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100914 yang_print_close(out, level, flag);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200915}
916
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200917static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100918yang_print_leaflist(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200919{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200920 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200921 struct lys_node_leaflist *llist = (struct lys_node_leaflist *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200922
Radek Krejci76b07902015-10-09 09:11:25 +0200923 ly_print(out, "%*sleaf-list %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +0200924
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200925 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100926 yang_print_nacmext(out, level, node, node->module, NULL);
Radek Krejci8d81e002015-12-10 11:18:59 +0100927 if (llist->when) {
928 yang_print_when(out, level, llist->module, llist->when);
929 }
Michal Vasko4773b762015-07-07 12:15:10 +0200930 for (i = 0; i < llist->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200931 yang_print_iffeature(out, level, node->module, llist->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200932 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100933 for (i = 0; i < llist->must_size; i++) {
934 yang_print_must(out, level, node->module, &llist->must[i]);
935 }
936 yang_print_snode_common2(out, level, node, NULL);
937 yang_print_type(out, level, node->module, &llist->type);
938 if (llist->units != NULL) {
939 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, llist->units);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200940 }
941 if (llist->min > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200942 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, llist->min);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200943 }
944 if (llist->max > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200945 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, llist->max);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200946 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100947 if (llist->flags & LYS_USERORDERED) {
948 ly_print(out, "%*sordered-by user;\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200949 }
950 level--;
Michal Vasko16083662015-06-09 14:00:45 +0200951
Radek Krejci76b07902015-10-09 09:11:25 +0200952 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200953}
954
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200955static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100956yang_print_list(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200957{
Michal Vasko1ef07972015-07-07 14:01:35 +0200958 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200959 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200960 struct lys_node_list *list = (struct lys_node_list *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200961
Radek Krejci76b07902015-10-09 09:11:25 +0200962 ly_print(out, "%*slist %s {\n", LEVEL, INDENT, node->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200963 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100964 yang_print_nacmext(out, level, node, node->module, NULL);
Radek Krejci8d81e002015-12-10 11:18:59 +0100965 if (list->when) {
966 yang_print_when(out, level, list->module, list->when);
967 }
Michal Vasko4773b762015-07-07 12:15:10 +0200968 for (i = 0; i < list->features_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200969 yang_print_iffeature(out, level, node->module, list->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200970 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100971 for (i = 0; i < list->must_size; i++) {
972 yang_print_must(out, level, list->module, &list->must[i]);
973 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200974 if (list->keys_size) {
Radek Krejci76b07902015-10-09 09:11:25 +0200975 ly_print(out, "%*skey \"", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200976 for (i = 0; i < list->keys_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200977 ly_print(out, "%s%s", list->keys[i]->name, i + 1 < list->keys_size ? " " : "");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200978 }
Radek Krejci76b07902015-10-09 09:11:25 +0200979 ly_print(out, "\";\n");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200980 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200981 for (i = 0; i < list->unique_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200982 yang_print_unique(out, level, &list->unique[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200983 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100984 yang_print_snode_common2(out, level, node, NULL);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200985 if (list->min > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200986 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, list->min);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200987 }
988 if (list->max > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200989 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, list->max);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200990 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100991 if (list->flags & LYS_USERORDERED) {
992 ly_print(out, "%*sordered-by user;\n", LEVEL, INDENT);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200993 }
Michal Vaskodff818b2015-06-09 14:01:12 +0200994
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200995 for (i = 0; i < list->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200996 yang_print_typedef(out, level, list->module, &list->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200997 }
Radek Krejci1d82ef62015-08-07 14:44:40 +0200998 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100999 /* augments */
1000 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +02001001 continue;
1002 }
Radek Krejci76b07902015-10-09 09:11:25 +02001003 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +02001004 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
1005 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001006 }
1007 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001008 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001009}
1010
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001011static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001012yang_print_grouping(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001013{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001014 int i;
Michal Vasko0c5e9282016-02-15 13:11:57 +01001015 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001016 struct lys_node_grp *grp = (struct lys_node_grp *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001017
Radek Krejci76b07902015-10-09 09:11:25 +02001018 ly_print(out, "%*sgrouping %s {\n", LEVEL, INDENT, node->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001019 level++;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001020
Radek Krejci32cce7c2015-12-09 16:44:13 +01001021 yang_print_snode_common(out, level, node, NULL);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001022
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001023 for (i = 0; i < grp->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +02001024 yang_print_typedef(out, level, node->module, &grp->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001025 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001026
Michal Vasko0c5e9282016-02-15 13:11:57 +01001027 LY_TREE_FOR(node->child, sub) {
1028 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +02001029 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
1030 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001031 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001032
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001033 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001034 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001035}
1036
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001037static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001038yang_print_uses(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001039{
Radek Krejci32cce7c2015-12-09 16:44:13 +01001040 int i, flag = 0;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001041 struct lys_node_uses *uses = (struct lys_node_uses *)node;
Michal Vasko1bb7a5a2016-02-05 14:28:02 +01001042 struct lys_module *mod;
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001043
Radek Krejci76b07902015-10-09 09:11:25 +02001044 ly_print(out, "%*suses ", LEVEL, INDENT);
Michal Vasko1bb7a5a2016-02-05 14:28:02 +01001045 if (node->child) {
Michal Vasko6c629ac2016-02-15 14:08:23 +01001046 mod = lys_node_module(node->child);
Michal Vasko1dae8ec2016-02-15 14:49:01 +01001047 if (lys_node_module(node) != mod) {
Michal Vasko1bb7a5a2016-02-05 14:28:02 +01001048 ly_print(out, "%s:", transform_module_name2import_prefix(node->module, mod->name));
1049 }
Michal Vaskoc39c4b12015-07-07 14:55:33 +02001050 }
Radek Krejci32cce7c2015-12-09 16:44:13 +01001051 ly_print(out, "%s", uses->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001052 level++;
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001053
Radek Krejci32cce7c2015-12-09 16:44:13 +01001054 yang_print_nacmext(out, level, node, node->module, &flag);
1055 yang_print_snode_common(out, level, node, &flag);
Michal Vasko4773b762015-07-07 12:15:10 +02001056 for (i = 0; i < uses->features_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001057 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001058 yang_print_iffeature(out, level, node->module, uses->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +02001059 }
Michal Vasko1f0428a2015-07-07 14:55:04 +02001060 if (uses->when) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001061 yang_print_open(out, &flag);
Michal Vaskof9893382015-10-09 14:03:04 +02001062 yang_print_when(out, level, node->module, uses->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +02001063 }
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001064
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001065 for (i = 0; i < uses->refine_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001066 yang_print_open(out, &flag);
Michal Vaskof9893382015-10-09 14:03:04 +02001067 yang_print_refine(out, level, node->module, &uses->refine[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001068 }
Michal Vasko00b7cfe2015-06-09 13:56:38 +02001069
Michal Vasko6f25f212015-07-07 15:42:07 +02001070 for (i = 0; i < uses->augment_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001071 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001072 yang_print_augment(out, level, node->module, &uses->augment[i]);
Michal Vasko6f25f212015-07-07 15:42:07 +02001073 }
1074
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001075 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001076 yang_print_close(out, level, flag);
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001077}
1078
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001079static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001080yang_print_input_output(struct lyout *out, int level, const struct lys_node *node)
Michal Vasko5bbae102015-06-16 12:16:44 +02001081{
Michal Vaskof4d3d742015-06-16 11:51:09 +02001082 int i;
Radek Krejci76512572015-08-04 09:47:08 +02001083 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001084 struct lys_node_rpc_inout *inout = (struct lys_node_rpc_inout *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +02001085
Radek Krejci76b07902015-10-09 09:11:25 +02001086 ly_print(out, "%*s%s {\n", LEVEL, INDENT, (inout->nodetype == LYS_INPUT ? "input" : "output"));
Michal Vaskof4d3d742015-06-16 11:51:09 +02001087
1088 level++;
1089 for (i = 0; i < inout->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +02001090 yang_print_typedef(out, level, node->module, &inout->tpdf[i]);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001091 }
1092
Radek Krejci1d82ef62015-08-07 14:44:40 +02001093 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +01001094 /* augments */
1095 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +02001096 continue;
1097 }
Radek Krejci76b07902015-10-09 09:11:25 +02001098 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +02001099 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
1100 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001101 }
1102
1103 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001104 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001105}
1106
1107static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001108yang_print_rpc(struct lyout *out, int level, const struct lys_node *node)
Michal Vaskof4d3d742015-06-16 11:51:09 +02001109{
Radek Krejci32cce7c2015-12-09 16:44:13 +01001110 int i, flag = 0;
Radek Krejci76512572015-08-04 09:47:08 +02001111 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001112 struct lys_node_rpc *rpc = (struct lys_node_rpc *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +02001113
Radek Krejci32cce7c2015-12-09 16:44:13 +01001114 ly_print(out, "%*srpc %s", LEVEL, INDENT, node->name);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001115
1116 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001117 yang_print_snode_common(out, level, node, &flag);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001118
Michal Vasko4773b762015-07-07 12:15:10 +02001119 for (i = 0; i < rpc->features_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001120 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001121 yang_print_iffeature(out, level, node->module, rpc->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +02001122 }
1123
Michal Vaskof4d3d742015-06-16 11:51:09 +02001124 for (i = 0; i < rpc->tpdf_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001125 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001126 yang_print_typedef(out, level, node->module, &rpc->tpdf[i]);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001127 }
1128
Radek Krejci1d82ef62015-08-07 14:44:40 +02001129 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +01001130 /* augments */
1131 if (sub->parent != node) {
Radek Krejcic071c542016-01-27 14:57:51 +01001132 continue;
1133 }
Radek Krejci32cce7c2015-12-09 16:44:13 +01001134 yang_print_open(out, &flag);
1135 yang_print_snode(out, level, sub, LYS_GROUPING | LYS_INPUT | LYS_OUTPUT);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001136 }
1137
1138 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001139 yang_print_close(out, level, flag);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001140}
1141
1142static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001143yang_print_notif(struct lyout *out, int level, const struct lys_node *node)
Michal Vasko7690bc12015-06-16 12:26:05 +02001144{
Radek Krejci32cce7c2015-12-09 16:44:13 +01001145 int i, flag = 0;
Radek Krejci76512572015-08-04 09:47:08 +02001146 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001147 struct lys_node_notif *notif = (struct lys_node_notif *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +02001148
Radek Krejci32cce7c2015-12-09 16:44:13 +01001149 ly_print(out, "%*snotification %s", LEVEL, INDENT, node->name);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001150
1151 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001152 yang_print_snode_common(out, level, node, &flag);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001153
Michal Vasko4773b762015-07-07 12:15:10 +02001154 for (i = 0; i < notif->features_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001155 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001156 yang_print_iffeature(out, level, node->module, notif->features[i]);
Michal Vasko4773b762015-07-07 12:15:10 +02001157 }
1158
Michal Vaskof4d3d742015-06-16 11:51:09 +02001159 for (i = 0; i < notif->tpdf_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001160 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001161 yang_print_typedef(out, level, node->module, &notif->tpdf[i]);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001162 }
1163
Radek Krejci1d82ef62015-08-07 14:44:40 +02001164 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +01001165 /* augments */
1166 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +02001167 continue;
1168 }
Radek Krejci32cce7c2015-12-09 16:44:13 +01001169 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001170 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +02001171 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
1172 LYS_USES | LYS_GROUPING | LYS_ANYXML);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001173 }
1174
1175 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001176 yang_print_close(out, level, flag);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001177}
1178
1179static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001180yang_print_snode(struct lyout *out, int level, const struct lys_node *node, int mask)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001181{
Radek Krejci1d82ef62015-08-07 14:44:40 +02001182 switch (node->nodetype & mask) {
Radek Krejci76512572015-08-04 09:47:08 +02001183 case LYS_CONTAINER:
Radek Krejci76b07902015-10-09 09:11:25 +02001184 yang_print_container(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001185 break;
Radek Krejci76512572015-08-04 09:47:08 +02001186 case LYS_CHOICE:
Radek Krejci76b07902015-10-09 09:11:25 +02001187 yang_print_choice(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001188 break;
Radek Krejci76512572015-08-04 09:47:08 +02001189 case LYS_LEAF:
Radek Krejci76b07902015-10-09 09:11:25 +02001190 yang_print_leaf(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001191 break;
Radek Krejci76512572015-08-04 09:47:08 +02001192 case LYS_LEAFLIST:
Radek Krejci76b07902015-10-09 09:11:25 +02001193 yang_print_leaflist(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001194 break;
Radek Krejci76512572015-08-04 09:47:08 +02001195 case LYS_LIST:
Radek Krejci76b07902015-10-09 09:11:25 +02001196 yang_print_list(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001197 break;
Radek Krejci76512572015-08-04 09:47:08 +02001198 case LYS_USES:
Radek Krejci76b07902015-10-09 09:11:25 +02001199 yang_print_uses(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001200 break;
Radek Krejci76512572015-08-04 09:47:08 +02001201 case LYS_GROUPING:
Radek Krejci76b07902015-10-09 09:11:25 +02001202 yang_print_grouping(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001203 break;
Radek Krejci76512572015-08-04 09:47:08 +02001204 case LYS_ANYXML:
Radek Krejci76b07902015-10-09 09:11:25 +02001205 yang_print_anyxml(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001206 break;
Radek Krejci76512572015-08-04 09:47:08 +02001207 case LYS_CASE:
Radek Krejci76b07902015-10-09 09:11:25 +02001208 yang_print_case(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001209 break;
Radek Krejci76512572015-08-04 09:47:08 +02001210 case LYS_INPUT:
1211 case LYS_OUTPUT:
Radek Krejci76b07902015-10-09 09:11:25 +02001212 yang_print_input_output(out, level, node);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001213 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001214 default:
1215 break;
1216 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001217}
1218
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001219int
Michal Vasko1e62a092015-12-01 12:27:20 +01001220yang_print_model(struct lyout *out, const struct lys_module *module)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001221{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001222 unsigned int i;
1223 int level = 0;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001224#define LEVEL (level*2)
1225
Radek Krejci1d82ef62015-08-07 14:44:40 +02001226 struct lys_node *node;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001227
Radek Krejci8d81e002015-12-10 11:18:59 +01001228 /* (sub)module-header-stmts */
Michal Vasko116172e2015-07-07 11:54:37 +02001229 if (module->type) {
Radek Krejci76b07902015-10-09 09:11:25 +02001230 ly_print(out, "submodule %s {%s\n", module->name, (module->deviated ? " // DEVIATED" : ""));
Michal Vasko116172e2015-07-07 11:54:37 +02001231 level++;
Radek Krejci8d81e002015-12-10 11:18:59 +01001232 if (module->version) {
Radek Krejcic071c542016-01-27 14:57:51 +01001233 ly_print(out, "%*syang-version %s;\n", LEVEL, INDENT,
1234 ((struct lys_submodule *)module)->belongsto->version == 2 ? "1.1" : "1");
Radek Krejci8d81e002015-12-10 11:18:59 +01001235 }
Radek Krejci76b07902015-10-09 09:11:25 +02001236 ly_print(out, "%*sbelongs-to %s {\n", LEVEL, INDENT, ((struct lys_submodule *)module)->belongsto->name);
Michal Vasko116172e2015-07-07 11:54:37 +02001237 level++;
Radek Krejci8d81e002015-12-10 11:18:59 +01001238 ly_print(out, "%*sprefix %s;\n", LEVEL, INDENT, module->prefix);
Michal Vasko116172e2015-07-07 11:54:37 +02001239 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001240 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko116172e2015-07-07 11:54:37 +02001241 } else {
Radek Krejci76b07902015-10-09 09:11:25 +02001242 ly_print(out, "module %s {%s\n", module->name, (module->deviated ? " // DEVIATED" : ""));
Michal Vasko116172e2015-07-07 11:54:37 +02001243 level++;
Radek Krejci8d81e002015-12-10 11:18:59 +01001244 if (module->version) {
Radek Krejcic071c542016-01-27 14:57:51 +01001245 ly_print(out, "%*syang-version %s;\n", LEVEL, INDENT, module->version == 2 ? "1.1" : "1");
Radek Krejci8d81e002015-12-10 11:18:59 +01001246 }
Radek Krejci76b07902015-10-09 09:11:25 +02001247 ly_print(out, "%*snamespace \"%s\";\n", LEVEL, INDENT, module->ns);
Radek Krejci8d81e002015-12-10 11:18:59 +01001248 ly_print(out, "%*sprefix %s;\n", LEVEL, INDENT, module->prefix);
Michal Vasko116172e2015-07-07 11:54:37 +02001249 }
Radek Krejcib0594bf2015-05-21 23:51:27 +02001250
Radek Krejci8d81e002015-12-10 11:18:59 +01001251 /* linkage-stmts */
1252 if (module->imp_size || module->inc_size) {
1253 ly_print(out, "\n");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001254 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001255 for (i = 0; i < module->imp_size; i++) {
Radek Krejcic071c542016-01-27 14:57:51 +01001256 if (module->imp[i].external) {
1257 continue;
1258 }
Michal Vaskoc8e3ce02016-02-12 14:28:35 +01001259 ly_print(out, "%*simport %s {\n", LEVEL, INDENT, module->imp[i].module->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001260 level++;
Radek Krejci8d81e002015-12-10 11:18:59 +01001261 ly_print(out, "%*sprefix %s;\n", LEVEL, INDENT, module->imp[i].prefix);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001262 if (module->imp[i].rev[0]) {
Michal Vasko86dfd262016-02-15 14:26:31 +01001263 ly_print(out, "%*srevision-date %s;\n", LEVEL, INDENT, module->imp[i].rev);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001264 }
1265 level--;
Michal Vaskoc8e3ce02016-02-12 14:28:35 +01001266 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001267 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001268 for (i = 0; i < module->inc_size; i++) {
Radek Krejcic071c542016-01-27 14:57:51 +01001269 if (module->inc[i].external) {
1270 continue;
1271 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001272 if (module->inc[i].rev[0]) {
Radek Krejci76b07902015-10-09 09:11:25 +02001273 ly_print(out, "%*sinclude \"%s\" {\n", LEVEL, INDENT, module->inc[i].submodule->name);
Radek Krejci8d81e002015-12-10 11:18:59 +01001274 level++;
Michal Vasko3a4e2d02016-02-15 15:12:46 +01001275 ly_print(out, "%*srevision-date %s;\n", LEVEL, INDENT, module->inc[i].rev);
Radek Krejci8d81e002015-12-10 11:18:59 +01001276 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001277 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001278 } else {
Radek Krejci76b07902015-10-09 09:11:25 +02001279 ly_print(out, "%*sinclude \"%s\";\n", LEVEL, INDENT, module->inc[i].submodule->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001280 }
1281 }
Radek Krejciefaeba32015-05-27 14:30:57 +02001282
Radek Krejci8d81e002015-12-10 11:18:59 +01001283 /* meta-stmts */
1284 if (module->org || module->contact || module->dsc || module->ref) {
1285 ly_print(out, "\n");
1286 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001287 if (module->org) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001288 yang_print_text(out, level, "organization", module->org, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001289 }
1290 if (module->contact) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001291 yang_print_text(out, level, "contact", module->contact, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001292 }
1293 if (module->dsc) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001294 yang_print_text(out, level, "description", module->dsc, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001295 }
1296 if (module->ref) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001297 yang_print_text(out, level, "reference", module->ref, 0);
1298 }
1299
1300 /* revision-stmts */
1301 if (module->rev_size) {
1302 ly_print(out, "\n");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001303 }
1304 for (i = 0; i < module->rev_size; i++) {
1305 if (module->rev[i].dsc || module->rev[i].ref) {
Radek Krejci76b07902015-10-09 09:11:25 +02001306 ly_print(out, "%*srevision \"%s\" {\n", LEVEL, INDENT, module->rev[i].date);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001307 level++;
1308 if (module->rev[i].dsc) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001309 yang_print_text(out, level, "description", module->rev[i].dsc, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001310 }
1311 if (module->rev[i].ref) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001312 yang_print_text(out, level, "reference", module->rev[i].ref, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001313 }
1314 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001315 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001316 } else {
Michal Vasko86dfd262016-02-15 14:26:31 +01001317 ly_print(out, "%*srevision %s;\n", LEVEL, INDENT, module->rev[i].date);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001318 }
1319 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001320
Radek Krejci8d81e002015-12-10 11:18:59 +01001321 /* body-stmts */
Michal Vasko30f6e912015-07-07 12:24:27 +02001322 for (i = 0; i < module->features_size; i++) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001323 ly_print(out, "\n");
Radek Krejci76b07902015-10-09 09:11:25 +02001324 yang_print_feature(out, level, &module->features[i]);
Michal Vasko30f6e912015-07-07 12:24:27 +02001325 }
1326
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001327 for (i = 0; i < module->ident_size; i++) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001328 ly_print(out, "\n");
Radek Krejci76b07902015-10-09 09:11:25 +02001329 yang_print_identity(out, level, &module->ident[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001330 }
Radek Krejci6793db02015-05-22 17:49:54 +02001331
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001332 for (i = 0; i < module->tpdf_size; i++) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001333 ly_print(out, "\n");
Radek Krejci76b07902015-10-09 09:11:25 +02001334 yang_print_typedef(out, level, module, &module->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001335 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001336
Radek Krejci8d81e002015-12-10 11:18:59 +01001337 for (i = 0; i < module->deviation_size; ++i) {
1338 ly_print(out, "\n");
1339 yang_print_deviation(out, level, module, &module->deviation[i]);
1340 }
1341
Michal Vasko0c5e9282016-02-15 13:11:57 +01001342 LY_TREE_FOR(lys_module(module)->data, node) {
Radek Krejcic071c542016-01-27 14:57:51 +01001343 if (node->module != module) {
Michal Vasko0c5e9282016-02-15 13:11:57 +01001344 /* data from submodules */
Radek Krejcic071c542016-01-27 14:57:51 +01001345 continue;
1346 }
Radek Krejci8d81e002015-12-10 11:18:59 +01001347 ly_print(out, "\n");
Radek Krejci92720552015-10-05 15:28:27 +02001348 switch(node->nodetype) {
1349 case LYS_RPC:
Radek Krejcibac81762015-10-09 10:19:52 +02001350 yang_print_rpc(out, level, node);
Radek Krejci92720552015-10-05 15:28:27 +02001351 break;
1352 case LYS_NOTIF:
Radek Krejcibac81762015-10-09 10:19:52 +02001353 yang_print_notif(out, level, node);
Radek Krejci92720552015-10-05 15:28:27 +02001354 break;
1355 default:
Radek Krejcibac81762015-10-09 10:19:52 +02001356 yang_print_snode(out, level, node,
Radek Krejci92720552015-10-05 15:28:27 +02001357 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
1358 LYS_USES | LYS_GROUPING | LYS_ANYXML);
1359 break;
1360 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001361 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001362
Michal Vasko6f25f212015-07-07 15:42:07 +02001363 for (i = 0; i < module->augment_size; i++) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001364 ly_print(out, "\n");
Radek Krejci76b07902015-10-09 09:11:25 +02001365 yang_print_augment(out, level, module, &module->augment[i]);
Michal Vasko6f25f212015-07-07 15:42:07 +02001366 }
1367
Radek Krejci76b07902015-10-09 09:11:25 +02001368 ly_print(out, "}\n");
Michal Vasko95068c42016-03-24 14:58:11 +01001369 ly_print_flush(out);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001370
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001371 return EXIT_SUCCESS;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001372#undef LEVEL
1373}