blob: 8ebfb61fdeb1aa2aaa61808e5f57a544c5ae5ece [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
范昌虎1006995277de5fc2016-10-08 07:33:29 -0400125 yang_encode(out, t, strlen(t));
126 ly_print(out, "\";\n");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200127 level--;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200128
129}
130
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200131static void
Radek Krejci32cce7c2015-12-09 16:44:13 +0100132yang_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 +0200133{
Radek Krejci6764bb32015-07-03 15:16:04 +0200134 int i, j;
Radek Krejci86909d22015-07-03 16:11:25 +0200135 const char *prefix = NULL;
Radek Krejci6764bb32015-07-03 15:16:04 +0200136
Michal Vaskodcf98e62016-05-05 17:53:53 +0200137 if (node->nacm && (!lys_parent(node) || lys_parent(node)->nacm != node->nacm)) {
Radek Krejci6764bb32015-07-03 15:16:04 +0200138 /* locate ietf-netconf-acm module in imports */
Michal Vaskodb7aa992015-07-08 09:37:38 +0200139 if (!strcmp(module->name, "ietf-netconf-acm")) {
140 prefix = module->prefix;
Radek Krejci6764bb32015-07-03 15:16:04 +0200141 } else {
142 /* search in imports */
Michal Vaskodb7aa992015-07-08 09:37:38 +0200143 for (i = 0; i < module->imp_size; i++) {
144 if (!strcmp(module->imp[i].module->name, "ietf-netconf-acm")) {
145 prefix = module->imp[i].prefix;
Radek Krejci6764bb32015-07-03 15:16:04 +0200146 break;
147 }
148 }
149 /* and in imports of includes */
150 if (!prefix) {
Michal Vaskodb7aa992015-07-08 09:37:38 +0200151 for (j = 0; j < module->inc_size; j++) {
152 for (i = 0; i < module->inc[j].submodule->imp_size; i++) {
153 if (!strcmp(module->inc[j].submodule->imp[i].module->name, "ietf-netconf-acm")) {
154 prefix = module->inc[j].submodule->imp[i].prefix;
Radek Krejci6764bb32015-07-03 15:16:04 +0200155 break;
156 }
157 }
158 }
159 }
160 }
161
Michal Vaskodcf98e62016-05-05 17:53:53 +0200162 if ((node->nacm & LYS_NACM_DENYW) && (!lys_parent(node) || !(lys_parent(node)->nacm & LYS_NACM_DENYW))) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100163 yang_print_open(out, flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200164 ly_print(out, "%*s%s:default-deny-write;\n", LEVEL, INDENT, prefix);
Radek Krejci6764bb32015-07-03 15:16:04 +0200165 }
Michal Vaskodcf98e62016-05-05 17:53:53 +0200166 if ((node->nacm & LYS_NACM_DENYA) && (!lys_parent(node) || !(lys_parent(node)->nacm & LYS_NACM_DENYA))) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100167 yang_print_open(out, flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200168 ly_print(out, "%*s%s:default-deny-all;\n", LEVEL, INDENT, prefix);
Radek Krejci6764bb32015-07-03 15:16:04 +0200169 }
170 }
Radek Krejci6a113852015-07-03 16:04:20 +0200171}
172
173/*
174 * Covers:
175 * description, reference, status
176 */
177static void
Radek Krejci32cce7c2015-12-09 16:44:13 +0100178yang_print_snode_common(struct lyout *out, int level, const struct lys_node *node, int *flag)
Radek Krejci6a113852015-07-03 16:04:20 +0200179{
Radek Krejci1d82ef62015-08-07 14:44:40 +0200180 if (node->flags & LYS_STATUS_CURR) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100181 yang_print_open(out, flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200182 ly_print(out, "%*sstatus \"current\";\n", LEVEL, INDENT);
Radek Krejci1d82ef62015-08-07 14:44:40 +0200183 } else if (node->flags & LYS_STATUS_DEPRC) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100184 yang_print_open(out, flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200185 ly_print(out, "%*sstatus \"deprecated\";\n", LEVEL, INDENT);
Radek Krejci1d82ef62015-08-07 14:44:40 +0200186 } else if (node->flags & LYS_STATUS_OBSLT) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100187 yang_print_open(out, flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200188 ly_print(out, "%*sstatus \"obsolete\";\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200189 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200190
Radek Krejci1d82ef62015-08-07 14:44:40 +0200191 if (node->dsc) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100192 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100193 yang_print_text(out, level, "description", node->dsc, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200194 }
Radek Krejci1d82ef62015-08-07 14:44:40 +0200195 if (node->ref) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100196 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100197 yang_print_text(out, level, "reference", node->ref, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200198 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200199}
200
201/*
202 * Covers:
Michal Vasko2b8faed2015-06-09 12:51:20 +0200203 * config, mandatory
Radek Krejcida04f4a2015-05-21 12:54:09 +0200204 * description, reference, status
205 */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200206static void
Radek Krejci32cce7c2015-12-09 16:44:13 +0100207yang_print_snode_common2(struct lyout *out, int level, const struct lys_node *node, int *flag)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200208{
Michal Vaskodcf98e62016-05-05 17:53:53 +0200209 if (lys_parent(node)) {
Radek Krejci32c7bd62016-04-14 17:47:04 +0200210 if (node->flags & LYS_CONFIG_SET) {
Michal Vasko43271ee2016-02-05 14:27:09 +0100211 /* print config when it differs from the parent ... */
212 if (node->flags & LYS_CONFIG_W) {
213 yang_print_open(out, flag);
214 ly_print(out, "%*sconfig true;\n", LEVEL, INDENT);
215 } else if (node->flags & LYS_CONFIG_R) {
216 yang_print_open(out, flag);
217 ly_print(out, "%*sconfig false;\n", LEVEL, INDENT);
218 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200219 }
Michal Vasko43271ee2016-02-05 14:27:09 +0100220 } else if (node->flags & LYS_CONFIG_R) {
221 /* ... or it's a top-level state node */
222 yang_print_open(out, flag);
223 ly_print(out, "%*sconfig false;\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200224 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200225
Radek Krejci21c23052016-11-04 11:05:02 +0100226 if (node->nodetype & (LYS_LEAF | LYS_CHOICE | LYS_ANYDATA)) {
227 if (node->flags & LYS_MAND_TRUE) {
228 yang_print_open(out, flag);
229 ly_print(out, "%*smandatory true;\n", LEVEL, INDENT);
230 } else if (node->flags & LYS_MAND_FALSE) {
231 yang_print_open(out, flag);
232 ly_print(out, "%*smandatory false;\n", LEVEL, INDENT);
233 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200234 }
Michal Vasko2b8faed2015-06-09 12:51:20 +0200235
Radek Krejci32cce7c2015-12-09 16:44:13 +0100236 yang_print_snode_common(out, level, node, flag);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200237}
238
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200239static void
Radek Krejci9ff0a922016-07-14 13:08:05 +0200240yang_print_iffeature(struct lyout *out, int level, const struct lys_module *module, struct lys_iffeature *iffeature)
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200241{
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200242 ly_print(out, "%*sif-feature \"", LEVEL, INDENT);
Radek Krejci9ff0a922016-07-14 13:08:05 +0200243 ly_print_iffeature(out, module, iffeature);
244 ly_print(out, "\";\n");
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200245}
246
247static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100248yang_print_feature(struct lyout *out, int level, const struct lys_feature *feat)
Michal Vasko4773b762015-07-07 12:15:10 +0200249{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100250 int i, flag = 0;
Michal Vasko30f6e912015-07-07 12:24:27 +0200251
Radek Krejci32cce7c2015-12-09 16:44:13 +0100252 ly_print(out, "%*sfeature %s", LEVEL, INDENT, feat->name);
Michal Vasko4773b762015-07-07 12:15:10 +0200253 level++;
254
Radek Krejci32cce7c2015-12-09 16:44:13 +0100255 yang_print_snode_common(out, level, (struct lys_node *)feat, &flag);
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200256 for (i = 0; i < feat->iffeature_size; ++i) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100257 yang_print_open(out, &flag);
Radek Krejci9ff0a922016-07-14 13:08:05 +0200258 yang_print_iffeature(out, level, feat->module, &feat->iffeature[i]);
Michal Vasko30f6e912015-07-07 12:24:27 +0200259 }
Michal Vasko4773b762015-07-07 12:15:10 +0200260
261 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100262 yang_print_close(out, level, flag);
Michal Vasko4773b762015-07-07 12:15:10 +0200263}
264
265static void
Radek Krejci32cce7c2015-12-09 16:44:13 +0100266yang_print_restr(struct lyout *out, int level, const struct lys_restr *restr, int *flag)
Radek Krejci41726f92015-06-19 13:11:05 +0200267{
Radek Krejci0bd5db42015-06-19 13:30:07 +0200268 if (restr->dsc != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100269 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100270 yang_print_text(out, level, "description", restr->dsc, 0);
Radek Krejci41726f92015-06-19 13:11:05 +0200271 }
Radek Krejci0bd5db42015-06-19 13:30:07 +0200272 if (restr->ref != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100273 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100274 yang_print_text(out, level, "reference", restr->ref, 0);
Radek Krejci41726f92015-06-19 13:11:05 +0200275 }
Radek Krejci0bd5db42015-06-19 13:30:07 +0200276 if (restr->eapptag != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100277 yang_print_open(out, flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200278 ly_print(out, "%*serror-app-tag \"%s\";\n", LEVEL, INDENT, restr->eapptag);
Radek Krejci41726f92015-06-19 13:11:05 +0200279 }
Radek Krejci0bd5db42015-06-19 13:30:07 +0200280 if (restr->emsg != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100281 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100282 yang_print_text(out, level, "error-message", restr->emsg, 0);
Radek Krejci41726f92015-06-19 13:11:05 +0200283 }
284}
285
286static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100287yang_print_when(struct lyout *out, int level, const struct lys_module *module, const struct lys_when *when)
Michal Vasko1f0428a2015-07-07 14:55:04 +0200288{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100289 int flag = 0;
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100290 const char *str;
Michal Vaskof9893382015-10-09 14:03:04 +0200291
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100292 str = transform_json2schema(module, when->cond);
293 if (!str) {
Michal Vaskof9893382015-10-09 14:03:04 +0200294 ly_print(out, "(!error!)");
295 return;
296 }
297
Michal Vasko25cb6c62016-02-12 14:36:21 +0100298 ly_print(out, "%*swhen \"", LEVEL, INDENT);
299 yang_encode(out, str, -1);
300 ly_print(out, "\"");
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100301 lydict_remove(module->ctx, str);
Michal Vaskof9893382015-10-09 14:03:04 +0200302
Michal Vasko1f0428a2015-07-07 14:55:04 +0200303 level++;
304 if (when->dsc) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100305 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100306 yang_print_text(out, level, "description", when->dsc, 0);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200307 }
308 if (when->ref) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100309 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100310 yang_print_text(out, level, "reference", when->ref, 0);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200311 }
312 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100313 yang_print_close(out, level, flag);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200314}
315
316static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100317yang_print_type(struct lyout *out, int level, const struct lys_module *module, const struct lys_type *type)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200318{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200319 int i;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100320 int flag = 0, flag2;
Michal Vasko0fb82c62015-10-20 13:41:53 +0200321 const char *str;
Radek Krejcic071c542016-01-27 14:57:51 +0100322 struct lys_module *mod;
Radek Krejci25d782a2015-05-22 15:03:23 +0200323
Michal Vasko0fb82c62015-10-20 13:41:53 +0200324 if (type->module_name) {
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100325 ly_print(out, "%*stype %s:%s", LEVEL, INDENT,
326 transform_module_name2import_prefix(module, type->module_name), type->der->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200327 } else {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100328 ly_print(out, "%*stype %s", LEVEL, INDENT, type->der->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200329 }
330 level++;
331 switch (type->base) {
332 case LY_TYPE_BINARY:
Michal Vasko4634cda2016-02-16 09:22:09 +0100333 if (type->info.binary.length) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100334 yang_print_open(out, &flag);
Michal Vasko25cb6c62016-02-12 14:36:21 +0100335 ly_print(out, "%*slength \"", LEVEL, INDENT);
336 yang_encode(out, type->info.binary.length->expr, -1);
337 ly_print(out, "\"");
Radek Krejci32cce7c2015-12-09 16:44:13 +0100338 flag2 = 0;
339 yang_print_restr(out, level + 1, type->info.binary.length, &flag2);
340 yang_print_close(out, level, flag2);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200341 }
342 break;
343 case LY_TYPE_BITS:
344 for (i = 0; i < type->info.bits.count; ++i) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100345 yang_print_open(out, &flag);
Michal Vasko3f053ef2016-02-12 14:27:13 +0100346 ly_print(out, "%*sbit %s", LEVEL, INDENT, type->info.bits.bit[i].name);
347 flag2 = 0;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200348 level++;
Michal Vasko3f053ef2016-02-12 14:27:13 +0100349 yang_print_snode_common(out, level, (struct lys_node *)&type->info.bits.bit[i], &flag2);
350 if (!(type->info.bits.bit[i].flags & LYS_AUTOASSIGNED)) {
351 yang_print_open(out, &flag2);
352 ly_print(out, "%*sposition %u;\n", LEVEL, INDENT, type->info.bits.bit[i].pos);
353 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200354 level--;
Michal Vasko3f053ef2016-02-12 14:27:13 +0100355 yang_print_close(out, level, flag2);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200356 }
357 break;
358 case LY_TYPE_DEC64:
Radek Krejcib51d5932016-09-08 14:02:52 +0200359 if (!type->der->type.der) {
Michal Vasko4634cda2016-02-16 09:22:09 +0100360 yang_print_open(out, &flag);
361 ly_print(out, "%*sfraction-digits %d;\n", LEVEL, INDENT, type->info.dec64.dig);
362 }
Michal Vaskoea505ee2015-07-07 14:01:00 +0200363 if (type->info.dec64.range != NULL) {
Michal Vasko4634cda2016-02-16 09:22:09 +0100364 yang_print_open(out, &flag);
Michal Vasko25cb6c62016-02-12 14:36:21 +0100365 ly_print(out, "%*srange \"", LEVEL, INDENT);
366 yang_encode(out, type->info.dec64.range->expr, -1);
367 ly_print(out, "\"");
Radek Krejci32cce7c2015-12-09 16:44:13 +0100368 flag2 = 0;
369 yang_print_restr(out, level + 1, type->info.dec64.range, &flag2);
370 yang_print_close(out, level, flag2);
Michal Vaskoea505ee2015-07-07 14:01:00 +0200371 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200372 break;
373 case LY_TYPE_ENUM:
374 for (i = 0; i < type->info.enums.count; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100375 yang_print_open(out, &flag);
Michal Vasko3f053ef2016-02-12 14:27:13 +0100376 ly_print(out, "%*senum \"%s\"", LEVEL, INDENT, type->info.enums.enm[i].name);
377 flag2 = 0;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200378 level++;
Michal Vasko3f053ef2016-02-12 14:27:13 +0100379 yang_print_snode_common(out, level, (struct lys_node *)&type->info.enums.enm[i], &flag2);
380 if (!(type->info.enums.enm[i].flags & LYS_AUTOASSIGNED)) {
381 yang_print_open(out, &flag2);
382 ly_print(out, "%*svalue %d;\n", LEVEL, INDENT, type->info.enums.enm[i].value);
383 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200384 level--;
Michal Vasko3f053ef2016-02-12 14:27:13 +0100385 yang_print_close(out, level, flag2);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200386 }
387 break;
388 case LY_TYPE_IDENT:
Michal Vaskof2d43962016-09-02 11:10:16 +0200389 if (type->info.ident.count) {
Michal Vasko4634cda2016-02-16 09:22:09 +0100390 yang_print_open(out, &flag);
Michal Vaskof2d43962016-09-02 11:10:16 +0200391 for (i = 0; i < type->info.ident.count; ++i) {
392 mod = lys_main_module(type->info.ident.ref[i]->module);
393 if (lys_main_module(module) == mod) {
394 ly_print(out, "%*sbase %s;\n", LEVEL, INDENT, type->info.ident.ref[i]->name);
395 } else {
396 ly_print(out, "%*sbase %s:%s;\n", LEVEL, INDENT, transform_module_name2import_prefix(module, mod->name),
397 type->info.ident.ref[i]->name);
398 }
Michal Vasko4634cda2016-02-16 09:22:09 +0100399 }
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:
Radek Krejci0dbff6a2016-07-17 12:40:18 +0200430 if (ly_strequal(type->der->name, "leafref", 0)) {
Michal Vasko4634cda2016-02-16 09:22:09 +0100431 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);
Radek Krejci0d23e7a2016-08-04 12:46:17 +0200450 yang_encode(out, &type->info.str.patterns[i].expr[1], -1);
Michal Vasko25cb6c62016-02-12 14:36:21 +0100451 ly_print(out, "\"");
Radek Krejci32cce7c2015-12-09 16:44:13 +0100452 flag2 = 0;
Pavol Vican075fdf72016-09-21 09:34:28 +0200453 if (type->info.str.patterns[i].expr[0] == 0x15) {
Radek Krejci0d23e7a2016-08-04 12:46:17 +0200454 yang_print_open(out, &flag2);
Pavol Vican075fdf72016-09-21 09:34:28 +0200455 yang_print_text(out, level + 1, "modifier", "invert-match", 1);
Radek Krejci0d23e7a2016-08-04 12:46:17 +0200456 }
Radek Krejci32cce7c2015-12-09 16:44:13 +0100457 yang_print_restr(out, level + 1, &type->info.str.patterns[i], &flag2);
458 yang_print_close(out, level, flag2);
Radek Krejci5fbc9162015-06-19 14:11:11 +0200459 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200460 break;
461 case LY_TYPE_UNION:
462 for (i = 0; i < type->info.uni.count; ++i) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100463 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200464 yang_print_type(out, level, module, &type->info.uni.types[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200465 }
466 break;
467 default:
468 /* other types do not have substatements */
469 break;
470 }
471 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100472 yang_print_close(out, level, flag);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200473}
474
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200475static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100476yang_print_must(struct lyout *out, int level, const struct lys_module *module, const struct lys_restr *must)
Michal Vasko7f976ee2015-06-09 13:55:41 +0200477{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100478 int flag = 0;
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100479 const char *str;
Michal Vaskof9893382015-10-09 14:03:04 +0200480
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100481 str = transform_json2schema(module, must->expr);
482 if (!str) {
Michal Vaskof9893382015-10-09 14:03:04 +0200483 ly_print(out, "(!error!)");
484 return;
485 }
486
Michal Vasko25cb6c62016-02-12 14:36:21 +0100487 ly_print(out, "%*smust \"", LEVEL, INDENT);
488 yang_encode(out, str, -1);
489 ly_print(out, "\"");
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100490 lydict_remove(module->ctx, str);
Michal Vaskof9893382015-10-09 14:03:04 +0200491
Radek Krejci32cce7c2015-12-09 16:44:13 +0100492 yang_print_restr(out, level + 1, must, &flag);
493 yang_print_close(out, level, flag);
Michal Vasko7f976ee2015-06-09 13:55:41 +0200494}
495
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200496static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100497yang_print_unique(struct lyout *out, int level, const struct lys_unique *uniq)
Michal Vasko1ef07972015-07-07 14:01:35 +0200498{
499 int i;
500
Radek Krejci76b07902015-10-09 09:11:25 +0200501 ly_print(out, "%*sunique \"", LEVEL, INDENT);
Radek Krejci581ce772015-11-10 17:22:40 +0100502 for (i = 0; i < uniq->expr_size; i++) {
503 ly_print(out, "%s%s", uniq->expr[i], i + 1 < uniq->expr_size ? " " : "");
Michal Vasko1ef07972015-07-07 14:01:35 +0200504 }
Radek Krejci76b07902015-10-09 09:11:25 +0200505 ly_print(out, "\";\n");
Michal Vasko1ef07972015-07-07 14:01:35 +0200506}
507
508static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100509yang_print_refine(struct lyout *out, int level, const struct lys_module *module, const struct lys_refine *refine)
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200510{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200511 int i;
Michal Vaskoa8b25952015-10-20 15:30:25 +0200512 const char *str;
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200513
Michal Vasko5d112852016-02-12 16:47:13 +0100514 str = transform_json2schema(module, refine->target_name);
Michal Vaskoa8b25952015-10-20 15:30:25 +0200515 ly_print(out, "%*srefine \"%s\" {\n", LEVEL, INDENT, str);
516 lydict_remove(module->ctx, str);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200517 level++;
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200518
Radek Krejci1574a8d2015-08-03 14:16:52 +0200519 if (refine->flags & LYS_CONFIG_W) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100520 ly_print(out, "%*sconfig true;\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200521 } else if (refine->flags & LYS_CONFIG_R) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100522 ly_print(out, "%*sconfig false;\n", LEVEL, INDENT);
Radek Krejci6a113852015-07-03 16:04:20 +0200523 }
524
Radek Krejci1574a8d2015-08-03 14:16:52 +0200525 if (refine->flags & LYS_MAND_TRUE) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100526 ly_print(out, "%*smandatory true;\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200527 } else if (refine->flags & LYS_MAND_FALSE) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100528 ly_print(out, "%*smandatory false;\n", LEVEL, INDENT);
Radek Krejci6a113852015-07-03 16:04:20 +0200529 }
530
Radek Krejci32cce7c2015-12-09 16:44:13 +0100531 yang_print_snode_common(out, level, (struct lys_node *)refine, NULL);
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200532
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200533 for (i = 0; i < refine->must_size; ++i) {
Michal Vaskof9893382015-10-09 14:03:04 +0200534 yang_print_must(out, level, module, &refine->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200535 }
Radek Krejcibf06c4f2016-07-29 15:49:56 +0200536 for (i = 0; i < refine->iffeature_size; i++) {
537 yang_print_iffeature(out, level, module, &refine->iffeature[i]);
538 }
Pavol Vican3e7c73a2016-08-17 10:24:11 +0200539 for (i = 0; i < refine->dflt_size; ++i) {
540 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, refine->dflt[i]);
541 }
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200542
Pavol Vican3e7c73a2016-08-17 10:24:11 +0200543 if (refine->target_type == LYS_CONTAINER) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200544 if (refine->mod.presence != NULL) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100545 yang_print_text(out, level, "presence", refine->mod.presence, 1);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200546 }
Radek Krejci76512572015-08-04 09:47:08 +0200547 } else if (refine->target_type & (LYS_LIST | LYS_LEAFLIST)) {
Radek Krejci0f04a6c2016-04-14 16:16:36 +0200548 if (refine->flags & LYS_RFN_MINSET) {
Radek Krejci76b07902015-10-09 09:11:25 +0200549 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, refine->mod.list.min);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200550 }
Radek Krejci0f04a6c2016-04-14 16:16:36 +0200551 if (refine->flags & LYS_RFN_MAXSET) {
Radek Krejci0d7b2472016-02-12 11:11:03 +0100552 if (refine->mod.list.max) {
553 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, refine->mod.list.max);
554 } else {
555 ly_print(out, "%*smax-elements \"unbounded\";\n", LEVEL, INDENT);
556 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200557 }
558 }
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200559
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200560 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200561 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200562}
563
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200564static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100565yang_print_deviation(struct lyout *out, int level, const struct lys_module *module,
566 const struct lys_deviation *deviation)
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200567{
Pavol Vican38321d02016-08-16 14:56:02 +0200568 int i, j, k;
Michal Vaskoa8b25952015-10-20 15:30:25 +0200569 const char *str;
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200570
Michal Vasko5d112852016-02-12 16:47:13 +0100571 str = transform_json2schema(module, deviation->target_name);
Michal Vaskoa8b25952015-10-20 15:30:25 +0200572 ly_print(out, "%*sdeviation \"%s\" {\n", LEVEL, INDENT, str);
573 lydict_remove(module->ctx, str);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200574 level++;
575
576 if (deviation->dsc) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100577 yang_print_text(out, level, "description", deviation->dsc, 0);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200578 }
579 if (deviation->ref) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100580 yang_print_text(out, level, "reference", deviation->ref, 0);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200581 }
582
583 for (i = 0; i < deviation->deviate_size; ++i) {
Radek Krejci76b07902015-10-09 09:11:25 +0200584 ly_print(out, "%*sdeviate ", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200585 if (deviation->deviate[i].mod == LY_DEVIATE_NO) {
Michal Vaskod875e882016-05-19 10:57:30 +0200586 ly_print(out, "not-supported;\n");
587 continue;
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200588 } else if (deviation->deviate[i].mod == LY_DEVIATE_ADD) {
Radek Krejci76b07902015-10-09 09:11:25 +0200589 ly_print(out, "add {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200590 } else if (deviation->deviate[i].mod == LY_DEVIATE_RPL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200591 ly_print(out, "replace {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200592 } else if (deviation->deviate[i].mod == LY_DEVIATE_DEL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200593 ly_print(out, "delete {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200594 }
595 level++;
596
Radek Krejci1574a8d2015-08-03 14:16:52 +0200597 if (deviation->deviate[i].flags & LYS_CONFIG_W) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100598 ly_print(out, "%*sconfig true;\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200599 } else if (deviation->deviate[i].flags & LYS_CONFIG_R) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100600 ly_print(out, "%*sconfig false;\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200601 }
602
Radek Krejci1574a8d2015-08-03 14:16:52 +0200603 if (deviation->deviate[i].flags & LYS_MAND_TRUE) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100604 ly_print(out, "%*smandatory true;\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200605 } else if (deviation->deviate[i].flags & LYS_MAND_FALSE) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100606 ly_print(out, "%*smandatory false;\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200607 }
608
Pavol Vican38321d02016-08-16 14:56:02 +0200609 for (k = 0; k < deviation->deviate[i].dflt_size; ++k) {
610 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, deviation->deviate[i].dflt[k]);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200611 }
612
Radek Krejci0d7b2472016-02-12 11:11:03 +0100613 if (deviation->deviate[i].min_set) {
Radek Krejci76b07902015-10-09 09:11:25 +0200614 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, deviation->deviate[i].min);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200615 }
Radek Krejci0d7b2472016-02-12 11:11:03 +0100616 if (deviation->deviate[i].max_set) {
617 if (deviation->deviate[i].max) {
618 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, deviation->deviate[i].max);
619 } else {
620 ly_print(out, "%*smax-elements \"unbounded\";\n", LEVEL, INDENT);
621 }
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200622 }
623
624 for (j = 0; j < deviation->deviate[i].must_size; ++j) {
Michal Vaskof9893382015-10-09 14:03:04 +0200625 yang_print_must(out, level, module, &deviation->deviate[i].must[j]);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200626 }
627
628 for (j = 0; j < deviation->deviate[i].unique_size; ++j) {
Radek Krejci76b07902015-10-09 09:11:25 +0200629 yang_print_unique(out, level, &deviation->deviate[i].unique[j]);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200630 }
631
632 if (deviation->deviate[i].type) {
Radek Krejci76b07902015-10-09 09:11:25 +0200633 yang_print_type(out, level, module, deviation->deviate[i].type);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200634 }
635
636 if (deviation->deviate[i].units) {
Michal Vaskod875e882016-05-19 10:57:30 +0200637 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, deviation->deviate[i].units);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200638 }
639
640 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200641 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200642 }
643
644 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200645 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200646}
647
648static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100649yang_print_augment(struct lyout *out, int level, const struct lys_module *module,
650 const struct lys_node_augment *augment)
Michal Vasko6f25f212015-07-07 15:42:07 +0200651{
652 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200653 struct lys_node *sub;
Michal Vasko488c19e2015-10-20 15:21:00 +0200654 const char *str;
Michal Vasko6f25f212015-07-07 15:42:07 +0200655
Michal Vasko5d112852016-02-12 16:47:13 +0100656 str = transform_json2schema(module, augment->target_name);
Michal Vasko488c19e2015-10-20 15:21:00 +0200657 ly_print(out, "%*saugment \"%s\" {\n", LEVEL, INDENT, str);
658 lydict_remove(module->ctx, str);
Michal Vasko6f25f212015-07-07 15:42:07 +0200659 level++;
660
Radek Krejci32cce7c2015-12-09 16:44:13 +0100661 yang_print_nacmext(out, level, (struct lys_node *)augment, module, NULL);
662 yang_print_snode_common(out, level, (struct lys_node *)augment, NULL);
Michal Vasko6f25f212015-07-07 15:42:07 +0200663
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200664 for (i = 0; i < augment->iffeature_size; i++) {
Radek Krejci9ff0a922016-07-14 13:08:05 +0200665 yang_print_iffeature(out, level, module, &augment->iffeature[i]);
Michal Vasko6f25f212015-07-07 15:42:07 +0200666 }
667
668 if (augment->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200669 yang_print_when(out, level, module, augment->when);
Michal Vasko6f25f212015-07-07 15:42:07 +0200670 }
671
672 LY_TREE_FOR(augment->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100673 /* only our augment */
674 if (sub->parent != (struct lys_node *)augment) {
675 continue;
676 }
Radek Krejci76b07902015-10-09 09:11:25 +0200677 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200678 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
Radek Krejcibf2abff2016-08-23 15:51:52 +0200679 LYS_USES | LYS_ANYDATA | LYS_CASE | LYS_ACTION);
Michal Vasko6f25f212015-07-07 15:42:07 +0200680 }
681
682 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200683 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko6f25f212015-07-07 15:42:07 +0200684}
685
686static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100687yang_print_typedef(struct lyout *out, int level, const struct lys_module *module, const struct lys_tpdf *tpdf)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200688{
Radek Krejci76b07902015-10-09 09:11:25 +0200689 ly_print(out, "%*stypedef %s {\n", LEVEL, INDENT, tpdf->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200690 level++;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200691
Radek Krejci32cce7c2015-12-09 16:44:13 +0100692 yang_print_snode_common(out, level, (struct lys_node *)tpdf, NULL);
Radek Krejci76b07902015-10-09 09:11:25 +0200693 yang_print_type(out, level, module, &tpdf->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200694 if (tpdf->units != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200695 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, tpdf->units);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200696 }
697 if (tpdf->dflt != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200698 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, tpdf->dflt);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200699 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200700
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200701 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200702 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200703}
704
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200705static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100706yang_print_identity(struct lyout *out, int level, const struct lys_ident *ident)
Radek Krejci6793db02015-05-22 17:49:54 +0200707{
Radek Krejci018f1f52016-08-03 16:01:20 +0200708 int flag = 0, i;
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100709 struct lys_module *mod;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100710
711 ly_print(out, "%*sidentity %s", LEVEL, INDENT, ident->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200712 level++;
Radek Krejci6793db02015-05-22 17:49:54 +0200713
Radek Krejci32cce7c2015-12-09 16:44:13 +0100714 yang_print_snode_common(out, level, (struct lys_node *)ident, &flag);
Radek Krejci018f1f52016-08-03 16:01:20 +0200715 for (i = 0; i < ident->base_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100716 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200717 ly_print(out, "%*sbase ", LEVEL, INDENT);
Radek Krejci018f1f52016-08-03 16:01:20 +0200718 mod = lys_main_module(ident->base[i]->module);
Radek Krejcic4283442016-04-22 09:19:27 +0200719 if (lys_main_module(ident->module) != mod) {
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100720 ly_print(out, "%s:", transform_module_name2import_prefix(ident->module, mod->name));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200721 }
Radek Krejci018f1f52016-08-03 16:01:20 +0200722 ly_print(out, "%s;\n", ident->base[i]->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200723 }
Radek Krejci6793db02015-05-22 17:49:54 +0200724
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200725 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100726 yang_print_close(out, level, flag);
Radek Krejci6793db02015-05-22 17:49:54 +0200727}
728
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200729static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100730yang_print_container(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200731{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100732 int i, flag = 0;
Radek Krejci76512572015-08-04 09:47:08 +0200733 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200734 struct lys_node_container *cont = (struct lys_node_container *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200735
Radek Krejci32cce7c2015-12-09 16:44:13 +0100736 ly_print(out, "%*scontainer %s", LEVEL, INDENT, node->name);
Michal Vasko7f976ee2015-06-09 13:55:41 +0200737
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200738 level++;
Radek Krejci6a113852015-07-03 16:04:20 +0200739
Radek Krejci32cce7c2015-12-09 16:44:13 +0100740 yang_print_nacmext(out, level, node, node->module, &flag);
Radek Krejci6a113852015-07-03 16:04:20 +0200741
Radek Krejci8d81e002015-12-10 11:18:59 +0100742 if (cont->when) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100743 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100744 yang_print_when(out, level, node->module, cont->when);
745 }
746
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200747 for (i = 0; i < cont->iffeature_size; i++) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100748 yang_print_open(out, &flag);
Radek Krejci9ff0a922016-07-14 13:08:05 +0200749 yang_print_iffeature(out, level, node->module, &cont->iffeature[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200750 }
Michal Vasko7f976ee2015-06-09 13:55:41 +0200751
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200752 for (i = 0; i < cont->must_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100753 yang_print_open(out, &flag);
Michal Vaskof9893382015-10-09 14:03:04 +0200754 yang_print_must(out, level, node->module, &cont->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200755 }
Michal Vasko7f976ee2015-06-09 13:55:41 +0200756
Radek Krejci8d81e002015-12-10 11:18:59 +0100757 if (cont->presence != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100758 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100759 yang_print_text(out, level, "presence", cont->presence, 1);
Michal Vasko4773b762015-07-07 12:15:10 +0200760 }
761
Radek Krejci8d81e002015-12-10 11:18:59 +0100762 yang_print_snode_common2(out, level, node, &flag);
763
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200764 for (i = 0; i < cont->tpdf_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100765 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200766 yang_print_typedef(out, level, node->module, &cont->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200767 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200768
Radek Krejci1d82ef62015-08-07 14:44:40 +0200769 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100770 /* augments */
771 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200772 continue;
773 }
Radek Krejci32cce7c2015-12-09 16:44:13 +0100774 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200775 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200776 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
Michal Vaskob15cae22016-09-15 09:40:56 +0200777 LYS_USES | LYS_GROUPING | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200778 }
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200779
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200780 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100781 yang_print_close(out, level, flag);
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200782}
783
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200784static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100785yang_print_case(struct lyout *out, int level, const struct lys_node *node)
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200786{
Michal Vasko4773b762015-07-07 12:15:10 +0200787 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200788 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200789 struct lys_node_case *cas = (struct lys_node_case *)node;
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200790
Radek Krejci76b07902015-10-09 09:11:25 +0200791 ly_print(out, "%*scase %s {\n", LEVEL, INDENT, cas->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200792 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100793 yang_print_nacmext(out, level, node, node->module, NULL);
794 yang_print_snode_common2(out, level, node, NULL);
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200795
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200796 for (i = 0; i < cas->iffeature_size; i++) {
Radek Krejci9ff0a922016-07-14 13:08:05 +0200797 yang_print_iffeature(out, level, node->module, &cas->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200798 }
799
Michal Vasko1f0428a2015-07-07 14:55:04 +0200800 if (cas->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200801 yang_print_when(out, level, node->module, cas->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200802 }
803
Radek Krejci1d82ef62015-08-07 14:44:40 +0200804 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100805 /* augments */
806 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200807 continue;
808 }
Radek Krejci76b07902015-10-09 09:11:25 +0200809 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200810 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
Radek Krejcibf2abff2016-08-23 15:51:52 +0200811 LYS_USES | LYS_ANYDATA);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200812 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200813
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200814 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200815 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200816}
817
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200818static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100819yang_print_choice(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200820{
Michal Vasko4773b762015-07-07 12:15:10 +0200821 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200822 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200823 struct lys_node_choice *choice = (struct lys_node_choice *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200824
Radek Krejci76b07902015-10-09 09:11:25 +0200825 ly_print(out, "%*schoice %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +0200826
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200827 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100828 yang_print_nacmext(out, level, node, node->module, NULL);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200829 if (choice->dflt != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200830 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, choice->dflt->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200831 }
Michal Vasko16083662015-06-09 14:00:45 +0200832
Radek Krejci32cce7c2015-12-09 16:44:13 +0100833 yang_print_snode_common2(out, level, node, NULL);
Michal Vasko4773b762015-07-07 12:15:10 +0200834
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200835 for (i = 0; i < choice->iffeature_size; i++) {
Radek Krejci9ff0a922016-07-14 13:08:05 +0200836 yang_print_iffeature(out, level, node->module, &choice->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200837 }
838
Michal Vasko1f0428a2015-07-07 14:55:04 +0200839 if (choice->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200840 yang_print_when(out, level, node->module, choice->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200841 }
842
Radek Krejci1d82ef62015-08-07 14:44:40 +0200843 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100844 /* augments */
845 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200846 continue;
847 }
Radek Krejci76b07902015-10-09 09:11:25 +0200848 yang_print_snode(out, level, sub,
Radek Krejcibf2abff2016-08-23 15:51:52 +0200849 LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_CASE);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200850 }
851 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200852 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200853}
854
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200855static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100856yang_print_leaf(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200857{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200858 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200859 struct lys_node_leaf *leaf = (struct lys_node_leaf *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200860
Radek Krejci76b07902015-10-09 09:11:25 +0200861 ly_print(out, "%*sleaf %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +0200862
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200863 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100864 yang_print_nacmext(out, level, node, node->module, NULL);
Radek Krejci8d81e002015-12-10 11:18:59 +0100865 if (leaf->when) {
866 yang_print_when(out, level, node->module, leaf->when);
867 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200868 for (i = 0; i < leaf->iffeature_size; i++) {
Radek Krejci9ff0a922016-07-14 13:08:05 +0200869 yang_print_iffeature(out, level, node->module, &leaf->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200870 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200871 for (i = 0; i < leaf->must_size; i++) {
Michal Vaskof9893382015-10-09 14:03:04 +0200872 yang_print_must(out, level, node->module, &leaf->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200873 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100874 yang_print_snode_common2(out, level, node, NULL);
Radek Krejci76b07902015-10-09 09:11:25 +0200875 yang_print_type(out, level, node->module, &leaf->type);
Michal Vaskod875e882016-05-19 10:57:30 +0200876 if (leaf->units) {
Radek Krejci76b07902015-10-09 09:11:25 +0200877 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, leaf->units);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200878 }
Michal Vaskod875e882016-05-19 10:57:30 +0200879 if (leaf->dflt) {
880 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, leaf->dflt);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200881 }
882 level--;
Michal Vasko16083662015-06-09 14:00:45 +0200883
Radek Krejci76b07902015-10-09 09:11:25 +0200884 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko16083662015-06-09 14:00:45 +0200885}
886
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200887static void
Radek Krejcibf2abff2016-08-23 15:51:52 +0200888yang_print_anydata(struct lyout *out, int level, const struct lys_node *node)
Michal Vasko16083662015-06-09 14:00:45 +0200889{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100890 int i, flag = 0;
Radek Krejcibf2abff2016-08-23 15:51:52 +0200891 struct lys_node_anydata *any = (struct lys_node_anydata *)node;
Michal Vasko16083662015-06-09 14:00:45 +0200892
Radek Krejcibf2abff2016-08-23 15:51:52 +0200893 ly_print(out, "%*s%s %s", LEVEL, INDENT, any->nodetype == LYS_ANYXML ? "anyxml" : "anydata", any->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200894 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100895 yang_print_nacmext(out, level, node, node->module, &flag);
896 yang_print_snode_common2(out, level, node, &flag);
Radek Krejcibf2abff2016-08-23 15:51:52 +0200897 for (i = 0; i < any->iffeature_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100898 yang_print_open(out, &flag);
Radek Krejcibf2abff2016-08-23 15:51:52 +0200899 yang_print_iffeature(out, level, node->module, &any->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200900 }
Radek Krejcibf2abff2016-08-23 15:51:52 +0200901 for (i = 0; i < any->must_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100902 yang_print_open(out, &flag);
Radek Krejcibf2abff2016-08-23 15:51:52 +0200903 yang_print_must(out, level, node->module, &any->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200904 }
Radek Krejcibf2abff2016-08-23 15:51:52 +0200905 if (any->when) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100906 yang_print_open(out, &flag);
Radek Krejcibf2abff2016-08-23 15:51:52 +0200907 yang_print_when(out, level, node->module, any->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200908 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200909 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100910 yang_print_close(out, level, flag);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200911}
912
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200913static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100914yang_print_leaflist(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200915{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200916 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200917 struct lys_node_leaflist *llist = (struct lys_node_leaflist *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200918
Radek Krejci76b07902015-10-09 09:11:25 +0200919 ly_print(out, "%*sleaf-list %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +0200920
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200921 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100922 yang_print_nacmext(out, level, node, node->module, NULL);
Radek Krejci8d81e002015-12-10 11:18:59 +0100923 if (llist->when) {
924 yang_print_when(out, level, llist->module, llist->when);
925 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200926 for (i = 0; i < llist->iffeature_size; i++) {
Radek Krejci9ff0a922016-07-14 13:08:05 +0200927 yang_print_iffeature(out, level, node->module, &llist->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200928 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100929 for (i = 0; i < llist->must_size; i++) {
930 yang_print_must(out, level, node->module, &llist->must[i]);
931 }
932 yang_print_snode_common2(out, level, node, NULL);
933 yang_print_type(out, level, node->module, &llist->type);
Pavol Vican38321d02016-08-16 14:56:02 +0200934 for (i = 0; i < llist->dflt_size; ++i) {
935 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, llist->dflt[i]);
936 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100937 if (llist->units != NULL) {
938 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, llist->units);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200939 }
940 if (llist->min > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200941 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, llist->min);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200942 }
943 if (llist->max > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200944 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, llist->max);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200945 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100946 if (llist->flags & LYS_USERORDERED) {
947 ly_print(out, "%*sordered-by user;\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200948 }
949 level--;
Michal Vasko16083662015-06-09 14:00:45 +0200950
Radek Krejci76b07902015-10-09 09:11:25 +0200951 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200952}
953
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200954static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100955yang_print_list(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200956{
Michal Vasko1ef07972015-07-07 14:01:35 +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_list *list = (struct lys_node_list *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200960
Radek Krejci76b07902015-10-09 09:11:25 +0200961 ly_print(out, "%*slist %s {\n", LEVEL, INDENT, node->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200962 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100963 yang_print_nacmext(out, level, node, node->module, NULL);
Radek Krejci8d81e002015-12-10 11:18:59 +0100964 if (list->when) {
965 yang_print_when(out, level, list->module, list->when);
966 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200967 for (i = 0; i < list->iffeature_size; i++) {
Radek Krejci9ff0a922016-07-14 13:08:05 +0200968 yang_print_iffeature(out, level, node->module, &list->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200969 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100970 for (i = 0; i < list->must_size; i++) {
971 yang_print_must(out, level, list->module, &list->must[i]);
972 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200973 if (list->keys_size) {
Radek Krejci5c08a992016-11-02 13:30:04 +0100974 ly_print(out, "%*skey \"%s\";\n", LEVEL, INDENT, list->keys_str);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200975 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200976 for (i = 0; i < list->unique_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200977 yang_print_unique(out, level, &list->unique[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200978 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100979 yang_print_snode_common2(out, level, node, NULL);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200980 if (list->min > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200981 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, list->min);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200982 }
983 if (list->max > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200984 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, list->max);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200985 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100986 if (list->flags & LYS_USERORDERED) {
987 ly_print(out, "%*sordered-by user;\n", LEVEL, INDENT);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200988 }
Michal Vaskodff818b2015-06-09 14:01:12 +0200989
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200990 for (i = 0; i < list->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200991 yang_print_typedef(out, level, list->module, &list->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200992 }
Radek Krejci1d82ef62015-08-07 14:44:40 +0200993 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100994 /* augments */
995 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200996 continue;
997 }
Radek Krejci76b07902015-10-09 09:11:25 +0200998 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200999 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
Michal Vaskob15cae22016-09-15 09:40:56 +02001000 LYS_USES | LYS_GROUPING | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001001 }
1002 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001003 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001004}
1005
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001006static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001007yang_print_grouping(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001008{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001009 int i;
Michal Vasko0c5e9282016-02-15 13:11:57 +01001010 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001011 struct lys_node_grp *grp = (struct lys_node_grp *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001012
Radek Krejci76b07902015-10-09 09:11:25 +02001013 ly_print(out, "%*sgrouping %s {\n", LEVEL, INDENT, node->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001014 level++;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001015
Radek Krejci32cce7c2015-12-09 16:44:13 +01001016 yang_print_snode_common(out, level, node, NULL);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001017
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001018 for (i = 0; i < grp->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +02001019 yang_print_typedef(out, level, node->module, &grp->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001020 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001021
Michal Vasko0c5e9282016-02-15 13:11:57 +01001022 LY_TREE_FOR(node->child, sub) {
1023 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +02001024 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
Radek Krejcibf2abff2016-08-23 15:51:52 +02001025 LYS_USES | LYS_GROUPING | LYS_ANYDATA | LYS_ACTION);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001026 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001027
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001028 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001029 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001030}
1031
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001032static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001033yang_print_uses(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001034{
Radek Krejci32cce7c2015-12-09 16:44:13 +01001035 int i, flag = 0;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001036 struct lys_node_uses *uses = (struct lys_node_uses *)node;
Michal Vasko1bb7a5a2016-02-05 14:28:02 +01001037 struct lys_module *mod;
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001038
Radek Krejci76b07902015-10-09 09:11:25 +02001039 ly_print(out, "%*suses ", LEVEL, INDENT);
Michal Vasko1bb7a5a2016-02-05 14:28:02 +01001040 if (node->child) {
Michal Vasko6c629ac2016-02-15 14:08:23 +01001041 mod = lys_node_module(node->child);
Michal Vasko1dae8ec2016-02-15 14:49:01 +01001042 if (lys_node_module(node) != mod) {
Michal Vasko1bb7a5a2016-02-05 14:28:02 +01001043 ly_print(out, "%s:", transform_module_name2import_prefix(node->module, mod->name));
1044 }
Michal Vaskoc39c4b12015-07-07 14:55:33 +02001045 }
Radek Krejci32cce7c2015-12-09 16:44:13 +01001046 ly_print(out, "%s", uses->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001047 level++;
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001048
Radek Krejci32cce7c2015-12-09 16:44:13 +01001049 yang_print_nacmext(out, level, node, node->module, &flag);
1050 yang_print_snode_common(out, level, node, &flag);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001051 for (i = 0; i < uses->iffeature_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001052 yang_print_open(out, &flag);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001053 yang_print_iffeature(out, level, node->module, &uses->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +02001054 }
Michal Vasko1f0428a2015-07-07 14:55:04 +02001055 if (uses->when) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001056 yang_print_open(out, &flag);
Michal Vaskof9893382015-10-09 14:03:04 +02001057 yang_print_when(out, level, node->module, uses->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +02001058 }
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001059
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001060 for (i = 0; i < uses->refine_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001061 yang_print_open(out, &flag);
Michal Vaskof9893382015-10-09 14:03:04 +02001062 yang_print_refine(out, level, node->module, &uses->refine[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001063 }
Michal Vasko00b7cfe2015-06-09 13:56:38 +02001064
Michal Vasko6f25f212015-07-07 15:42:07 +02001065 for (i = 0; i < uses->augment_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001066 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001067 yang_print_augment(out, level, node->module, &uses->augment[i]);
Michal Vasko6f25f212015-07-07 15:42:07 +02001068 }
1069
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001070 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001071 yang_print_close(out, level, flag);
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001072}
1073
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001074static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001075yang_print_input_output(struct lyout *out, int level, const struct lys_node *node)
Michal Vasko5bbae102015-06-16 12:16:44 +02001076{
Michal Vaskof4d3d742015-06-16 11:51:09 +02001077 int i;
Radek Krejci76512572015-08-04 09:47:08 +02001078 struct lys_node *sub;
Michal Vasko44fb6382016-06-29 11:12:27 +02001079 struct lys_node_inout *inout = (struct lys_node_inout *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +02001080
Radek Krejci41a349b2016-10-24 19:21:59 +02001081 if (node->flags & LYS_IMPLICIT) {
1082 /* implicit input/output which is not a part of the schema */
1083 return;
1084 }
1085
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 }
Radek Krejci12032a52016-07-29 15:42:56 +02001092 for (i = 0; i < inout->must_size; i++) {
1093 yang_print_must(out, level, node->module, &inout->must[i]);
1094 }
Michal Vaskof4d3d742015-06-16 11:51:09 +02001095
Radek Krejci1d82ef62015-08-07 14:44:40 +02001096 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +01001097 /* augments */
1098 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +02001099 continue;
1100 }
Radek Krejci76b07902015-10-09 09:11:25 +02001101 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +02001102 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
Radek Krejcibf2abff2016-08-23 15:51:52 +02001103 LYS_USES | LYS_GROUPING | LYS_ANYDATA);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001104 }
1105
1106 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001107 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001108}
1109
1110static void
Michal Vaskoca7cbc42016-07-01 11:36:53 +02001111yang_print_rpc_action(struct lyout *out, int level, const struct lys_node *node)
Michal Vaskof4d3d742015-06-16 11:51:09 +02001112{
Radek Krejci32cce7c2015-12-09 16:44:13 +01001113 int i, flag = 0;
Radek Krejci76512572015-08-04 09:47:08 +02001114 struct lys_node *sub;
Michal Vasko44fb6382016-06-29 11:12:27 +02001115 struct lys_node_rpc_action *rpc = (struct lys_node_rpc_action *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +02001116
Michal Vaskoca7cbc42016-07-01 11:36:53 +02001117 ly_print(out, "%*s%s %s", LEVEL, INDENT, (node->nodetype == LYS_RPC ? "rpc" : "action"), node->name);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001118
1119 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001120 yang_print_snode_common(out, level, node, &flag);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001121
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001122 for (i = 0; i < rpc->iffeature_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001123 yang_print_open(out, &flag);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001124 yang_print_iffeature(out, level, node->module, &rpc->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +02001125 }
1126
Michal Vaskof4d3d742015-06-16 11:51:09 +02001127 for (i = 0; i < rpc->tpdf_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001128 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001129 yang_print_typedef(out, level, node->module, &rpc->tpdf[i]);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001130 }
1131
Radek Krejci1d82ef62015-08-07 14:44:40 +02001132 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +01001133 /* augments */
1134 if (sub->parent != node) {
Radek Krejcic071c542016-01-27 14:57:51 +01001135 continue;
1136 }
Radek Krejci32cce7c2015-12-09 16:44:13 +01001137 yang_print_open(out, &flag);
1138 yang_print_snode(out, level, sub, LYS_GROUPING | LYS_INPUT | LYS_OUTPUT);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001139 }
1140
1141 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001142 yang_print_close(out, level, flag);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001143}
1144
1145static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001146yang_print_notif(struct lyout *out, int level, const struct lys_node *node)
Michal Vasko7690bc12015-06-16 12:26:05 +02001147{
Radek Krejci32cce7c2015-12-09 16:44:13 +01001148 int i, flag = 0;
Radek Krejci76512572015-08-04 09:47:08 +02001149 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001150 struct lys_node_notif *notif = (struct lys_node_notif *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +02001151
Radek Krejci32cce7c2015-12-09 16:44:13 +01001152 ly_print(out, "%*snotification %s", LEVEL, INDENT, node->name);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001153
1154 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001155 yang_print_snode_common(out, level, node, &flag);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001156
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001157 for (i = 0; i < notif->iffeature_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001158 yang_print_open(out, &flag);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001159 yang_print_iffeature(out, level, node->module, &notif->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +02001160 }
1161
Michal Vaskof4d3d742015-06-16 11:51:09 +02001162 for (i = 0; i < notif->tpdf_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001163 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001164 yang_print_typedef(out, level, node->module, &notif->tpdf[i]);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001165 }
1166
Radek Krejci12032a52016-07-29 15:42:56 +02001167 for (i = 0; i < notif->must_size; i++) {
1168 yang_print_open(out, &flag);
1169 yang_print_must(out, level, node->module, &notif->must[i]);
1170 }
1171
Radek Krejci1d82ef62015-08-07 14:44:40 +02001172 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +01001173 /* augments */
1174 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +02001175 continue;
1176 }
Radek Krejci32cce7c2015-12-09 16:44:13 +01001177 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001178 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +02001179 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
Radek Krejcibf2abff2016-08-23 15:51:52 +02001180 LYS_USES | LYS_GROUPING | LYS_ANYDATA);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001181 }
1182
1183 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001184 yang_print_close(out, level, flag);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001185}
1186
1187static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001188yang_print_snode(struct lyout *out, int level, const struct lys_node *node, int mask)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001189{
Radek Krejci1d82ef62015-08-07 14:44:40 +02001190 switch (node->nodetype & mask) {
Radek Krejci76512572015-08-04 09:47:08 +02001191 case LYS_CONTAINER:
Radek Krejci76b07902015-10-09 09:11:25 +02001192 yang_print_container(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001193 break;
Radek Krejci76512572015-08-04 09:47:08 +02001194 case LYS_CHOICE:
Radek Krejci76b07902015-10-09 09:11:25 +02001195 yang_print_choice(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001196 break;
Radek Krejci76512572015-08-04 09:47:08 +02001197 case LYS_LEAF:
Radek Krejci76b07902015-10-09 09:11:25 +02001198 yang_print_leaf(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001199 break;
Radek Krejci76512572015-08-04 09:47:08 +02001200 case LYS_LEAFLIST:
Radek Krejci76b07902015-10-09 09:11:25 +02001201 yang_print_leaflist(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001202 break;
Radek Krejci76512572015-08-04 09:47:08 +02001203 case LYS_LIST:
Radek Krejci76b07902015-10-09 09:11:25 +02001204 yang_print_list(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001205 break;
Radek Krejci76512572015-08-04 09:47:08 +02001206 case LYS_USES:
Radek Krejci76b07902015-10-09 09:11:25 +02001207 yang_print_uses(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001208 break;
Radek Krejci76512572015-08-04 09:47:08 +02001209 case LYS_GROUPING:
Radek Krejci76b07902015-10-09 09:11:25 +02001210 yang_print_grouping(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001211 break;
Radek Krejci76512572015-08-04 09:47:08 +02001212 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02001213 case LYS_ANYDATA:
1214 yang_print_anydata(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001215 break;
Radek Krejci76512572015-08-04 09:47:08 +02001216 case LYS_CASE:
Radek Krejci76b07902015-10-09 09:11:25 +02001217 yang_print_case(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001218 break;
Michal Vaskoca7cbc42016-07-01 11:36:53 +02001219 case LYS_ACTION:
1220 yang_print_rpc_action(out, level, node);
1221 break;
Radek Krejci76512572015-08-04 09:47:08 +02001222 case LYS_INPUT:
1223 case LYS_OUTPUT:
Radek Krejci76b07902015-10-09 09:11:25 +02001224 yang_print_input_output(out, level, node);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001225 break;
Michal Vaskob15cae22016-09-15 09:40:56 +02001226 case LYS_NOTIF:
1227 yang_print_notif(out, level, node);
1228 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001229 default:
1230 break;
1231 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001232}
1233
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001234int
Michal Vasko1e62a092015-12-01 12:27:20 +01001235yang_print_model(struct lyout *out, const struct lys_module *module)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001236{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001237 unsigned int i;
1238 int level = 0;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001239#define LEVEL (level*2)
1240
Radek Krejci1d82ef62015-08-07 14:44:40 +02001241 struct lys_node *node;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001242
Radek Krejci8d81e002015-12-10 11:18:59 +01001243 /* (sub)module-header-stmts */
Michal Vasko116172e2015-07-07 11:54:37 +02001244 if (module->type) {
Michal Vasko89563fc2016-07-28 16:19:35 +02001245 ly_print(out, "submodule %s {%s\n", module->name, (module->deviated == 1 ? " // DEVIATED" : ""));
Michal Vasko116172e2015-07-07 11:54:37 +02001246 level++;
Radek Krejci8d81e002015-12-10 11:18:59 +01001247 if (module->version) {
Radek Krejcic071c542016-01-27 14:57:51 +01001248 ly_print(out, "%*syang-version %s;\n", LEVEL, INDENT,
1249 ((struct lys_submodule *)module)->belongsto->version == 2 ? "1.1" : "1");
Radek Krejci8d81e002015-12-10 11:18:59 +01001250 }
Radek Krejci76b07902015-10-09 09:11:25 +02001251 ly_print(out, "%*sbelongs-to %s {\n", LEVEL, INDENT, ((struct lys_submodule *)module)->belongsto->name);
Michal Vasko116172e2015-07-07 11:54:37 +02001252 level++;
Radek Krejci8d81e002015-12-10 11:18:59 +01001253 ly_print(out, "%*sprefix %s;\n", LEVEL, INDENT, module->prefix);
Michal Vasko116172e2015-07-07 11:54:37 +02001254 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001255 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko116172e2015-07-07 11:54:37 +02001256 } else {
Michal Vasko89563fc2016-07-28 16:19:35 +02001257 ly_print(out, "module %s {%s\n", module->name, (module->deviated == 1 ? " // DEVIATED" : ""));
Michal Vasko116172e2015-07-07 11:54:37 +02001258 level++;
Radek Krejci8d81e002015-12-10 11:18:59 +01001259 if (module->version) {
Radek Krejcic071c542016-01-27 14:57:51 +01001260 ly_print(out, "%*syang-version %s;\n", LEVEL, INDENT, module->version == 2 ? "1.1" : "1");
Radek Krejci8d81e002015-12-10 11:18:59 +01001261 }
Radek Krejci76b07902015-10-09 09:11:25 +02001262 ly_print(out, "%*snamespace \"%s\";\n", LEVEL, INDENT, module->ns);
Radek Krejci8d81e002015-12-10 11:18:59 +01001263 ly_print(out, "%*sprefix %s;\n", LEVEL, INDENT, module->prefix);
Michal Vasko116172e2015-07-07 11:54:37 +02001264 }
Radek Krejcib0594bf2015-05-21 23:51:27 +02001265
Radek Krejci8d81e002015-12-10 11:18:59 +01001266 /* linkage-stmts */
1267 if (module->imp_size || module->inc_size) {
1268 ly_print(out, "\n");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001269 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001270 for (i = 0; i < module->imp_size; i++) {
Michal Vaskoc8e3ce02016-02-12 14:28:35 +01001271 ly_print(out, "%*simport %s {\n", LEVEL, INDENT, module->imp[i].module->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001272 level++;
Radek Krejci8d81e002015-12-10 11:18:59 +01001273 ly_print(out, "%*sprefix %s;\n", LEVEL, INDENT, module->imp[i].prefix);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001274 if (module->imp[i].rev[0]) {
Michal Vasko86dfd262016-02-15 14:26:31 +01001275 ly_print(out, "%*srevision-date %s;\n", LEVEL, INDENT, module->imp[i].rev);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001276 }
Michal Vasko8bfe3812016-07-27 13:37:52 +02001277 if (module->imp[i].dsc) {
1278 yang_print_text(out, level, "description", module->imp[i].dsc, 0);
1279 }
1280 if (module->imp[i].ref) {
1281 yang_print_text(out, level, "reference", module->imp[i].ref, 0);
1282 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001283 level--;
Michal Vaskoc8e3ce02016-02-12 14:28:35 +01001284 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001285 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001286 for (i = 0; i < module->inc_size; i++) {
Radek Krejcic071c542016-01-27 14:57:51 +01001287 if (module->inc[i].external) {
1288 continue;
1289 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001290 if (module->inc[i].rev[0]) {
Radek Krejci76b07902015-10-09 09:11:25 +02001291 ly_print(out, "%*sinclude \"%s\" {\n", LEVEL, INDENT, module->inc[i].submodule->name);
Radek Krejci8d81e002015-12-10 11:18:59 +01001292 level++;
Michal Vasko3a4e2d02016-02-15 15:12:46 +01001293 ly_print(out, "%*srevision-date %s;\n", LEVEL, INDENT, module->inc[i].rev);
Michal Vasko8bfe3812016-07-27 13:37:52 +02001294 if (module->inc[i].dsc) {
1295 yang_print_text(out, level, "description", module->inc[i].dsc, 0);
1296 }
1297 if (module->inc[i].ref) {
1298 yang_print_text(out, level, "reference", module->inc[i].ref, 0);
1299 }
Radek Krejci8d81e002015-12-10 11:18:59 +01001300 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001301 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001302 } else {
Radek Krejci76b07902015-10-09 09:11:25 +02001303 ly_print(out, "%*sinclude \"%s\";\n", LEVEL, INDENT, module->inc[i].submodule->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001304 }
1305 }
Radek Krejciefaeba32015-05-27 14:30:57 +02001306
Radek Krejci8d81e002015-12-10 11:18:59 +01001307 /* meta-stmts */
1308 if (module->org || module->contact || module->dsc || module->ref) {
1309 ly_print(out, "\n");
1310 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001311 if (module->org) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001312 yang_print_text(out, level, "organization", module->org, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001313 }
1314 if (module->contact) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001315 yang_print_text(out, level, "contact", module->contact, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001316 }
1317 if (module->dsc) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001318 yang_print_text(out, level, "description", module->dsc, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001319 }
1320 if (module->ref) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001321 yang_print_text(out, level, "reference", module->ref, 0);
1322 }
1323
1324 /* revision-stmts */
1325 if (module->rev_size) {
1326 ly_print(out, "\n");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001327 }
1328 for (i = 0; i < module->rev_size; i++) {
1329 if (module->rev[i].dsc || module->rev[i].ref) {
Radek Krejci76b07902015-10-09 09:11:25 +02001330 ly_print(out, "%*srevision \"%s\" {\n", LEVEL, INDENT, module->rev[i].date);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001331 level++;
1332 if (module->rev[i].dsc) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001333 yang_print_text(out, level, "description", module->rev[i].dsc, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001334 }
1335 if (module->rev[i].ref) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001336 yang_print_text(out, level, "reference", module->rev[i].ref, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001337 }
1338 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001339 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001340 } else {
Michal Vasko86dfd262016-02-15 14:26:31 +01001341 ly_print(out, "%*srevision %s;\n", LEVEL, INDENT, module->rev[i].date);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001342 }
1343 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001344
Radek Krejci8d81e002015-12-10 11:18:59 +01001345 /* body-stmts */
Michal Vasko30f6e912015-07-07 12:24:27 +02001346 for (i = 0; i < module->features_size; i++) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001347 ly_print(out, "\n");
Radek Krejci76b07902015-10-09 09:11:25 +02001348 yang_print_feature(out, level, &module->features[i]);
Michal Vasko30f6e912015-07-07 12:24:27 +02001349 }
1350
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001351 for (i = 0; i < module->ident_size; i++) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001352 ly_print(out, "\n");
Radek Krejci76b07902015-10-09 09:11:25 +02001353 yang_print_identity(out, level, &module->ident[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001354 }
Radek Krejci6793db02015-05-22 17:49:54 +02001355
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001356 for (i = 0; i < module->tpdf_size; i++) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001357 ly_print(out, "\n");
Radek Krejci76b07902015-10-09 09:11:25 +02001358 yang_print_typedef(out, level, module, &module->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001359 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001360
Radek Krejci8d81e002015-12-10 11:18:59 +01001361 for (i = 0; i < module->deviation_size; ++i) {
1362 ly_print(out, "\n");
1363 yang_print_deviation(out, level, module, &module->deviation[i]);
1364 }
1365
Radek Krejcic4283442016-04-22 09:19:27 +02001366 LY_TREE_FOR(lys_main_module(module)->data, node) {
Radek Krejcic071c542016-01-27 14:57:51 +01001367 if (node->module != module) {
Michal Vasko0c5e9282016-02-15 13:11:57 +01001368 /* data from submodules */
Radek Krejcic071c542016-01-27 14:57:51 +01001369 continue;
1370 }
Radek Krejci8d81e002015-12-10 11:18:59 +01001371 ly_print(out, "\n");
Radek Krejci92720552015-10-05 15:28:27 +02001372 switch(node->nodetype) {
1373 case LYS_RPC:
Michal Vaskoca7cbc42016-07-01 11:36:53 +02001374 yang_print_rpc_action(out, level, node);
Radek Krejci92720552015-10-05 15:28:27 +02001375 break;
1376 case LYS_NOTIF:
Radek Krejcibac81762015-10-09 10:19:52 +02001377 yang_print_notif(out, level, node);
Radek Krejci92720552015-10-05 15:28:27 +02001378 break;
1379 default:
Radek Krejcibac81762015-10-09 10:19:52 +02001380 yang_print_snode(out, level, node,
Radek Krejci92720552015-10-05 15:28:27 +02001381 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
Radek Krejcibf2abff2016-08-23 15:51:52 +02001382 LYS_USES | LYS_GROUPING | LYS_ANYDATA);
Radek Krejci92720552015-10-05 15:28:27 +02001383 break;
1384 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001385 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001386
Michal Vasko6f25f212015-07-07 15:42:07 +02001387 for (i = 0; i < module->augment_size; i++) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001388 ly_print(out, "\n");
Radek Krejci76b07902015-10-09 09:11:25 +02001389 yang_print_augment(out, level, module, &module->augment[i]);
Michal Vasko6f25f212015-07-07 15:42:07 +02001390 }
1391
Radek Krejci76b07902015-10-09 09:11:25 +02001392 ly_print(out, "}\n");
Michal Vasko95068c42016-03-24 14:58:11 +01001393 ly_print_flush(out);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001394
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001395 return EXIT_SUCCESS;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001396#undef LEVEL
1397}