blob: 71e77e4f545058c1a4902b8c9baf03ed7870ba52 [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
Radek Krejcie534c132016-11-23 13:32:31 +0100173static void
174yang_print_extension_instances(struct lyout *out, int level, const struct lys_module *module,
175 struct lys_ext_instance **ext, unsigned int count)
176{
177 unsigned int u, x;
178 struct lys_module *mod;
179 const char *prefix = NULL;
180 int flag;
181
182 for (u = 0; u < count; u++) {
183 mod = lys_main_module(ext[u]->def->module);
184 if (mod == module) {
185 prefix = module->prefix;
186 } else {
187 for (x = 0; x < module->imp_size; x++) {
188 if (mod == module->imp[x].module) {
189 prefix = module->imp[x].prefix;
190 break;
191 }
192 }
193 }
194
195 flag = 0;
196 ly_print(out, "%*s%s:%s", LEVEL, INDENT, prefix, ext[u]->def->name);
197
198 if (!ext[u]->def->plugin || ext[u]->def->plugin->type == LY_EXT_FLAG) {
199 /* flag extension */
200 if (((struct lys_ext_instance_flag *)ext[u])->arg_value) {
201 ly_print(out, " \"%s\"", ((struct lys_ext_instance_flag *)ext[u])->arg_value);
202 }
203 }
204
205 /* extensions */
206 if (ext[u]->ext_size) {
207 yang_print_open(out, &flag);
208 yang_print_extension_instances(out, level + 1, module, ext[u]->ext, ext[u]->ext_size);
209 }
210
211 /* close extension */
212 yang_print_close(out, level, flag);
213 }
214}
215
Radek Krejci6a113852015-07-03 16:04:20 +0200216/*
217 * Covers:
218 * description, reference, status
219 */
220static void
Radek Krejci32cce7c2015-12-09 16:44:13 +0100221yang_print_snode_common(struct lyout *out, int level, const struct lys_node *node, int *flag)
Radek Krejci6a113852015-07-03 16:04:20 +0200222{
Radek Krejci1d82ef62015-08-07 14:44:40 +0200223 if (node->flags & LYS_STATUS_CURR) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100224 yang_print_open(out, flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200225 ly_print(out, "%*sstatus \"current\";\n", LEVEL, INDENT);
Radek Krejci1d82ef62015-08-07 14:44:40 +0200226 } else if (node->flags & LYS_STATUS_DEPRC) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100227 yang_print_open(out, flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200228 ly_print(out, "%*sstatus \"deprecated\";\n", LEVEL, INDENT);
Radek Krejci1d82ef62015-08-07 14:44:40 +0200229 } else if (node->flags & LYS_STATUS_OBSLT) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100230 yang_print_open(out, flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200231 ly_print(out, "%*sstatus \"obsolete\";\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200232 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200233
Radek Krejci1d82ef62015-08-07 14:44:40 +0200234 if (node->dsc) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100235 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100236 yang_print_text(out, level, "description", node->dsc, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200237 }
Radek Krejci1d82ef62015-08-07 14:44:40 +0200238 if (node->ref) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100239 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100240 yang_print_text(out, level, "reference", node->ref, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200241 }
Radek Krejcie534c132016-11-23 13:32:31 +0100242 if (node->ext_size) {
243 yang_print_open(out, flag);
244 yang_print_extension_instances(out, level, node->module, node->ext, node->ext_size);
245 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200246}
247
248/*
249 * Covers:
Michal Vasko2b8faed2015-06-09 12:51:20 +0200250 * config, mandatory
Radek Krejcida04f4a2015-05-21 12:54:09 +0200251 * description, reference, status
252 */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200253static void
Radek Krejci32cce7c2015-12-09 16:44:13 +0100254yang_print_snode_common2(struct lyout *out, int level, const struct lys_node *node, int *flag)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200255{
Michal Vaskodcf98e62016-05-05 17:53:53 +0200256 if (lys_parent(node)) {
Radek Krejci32c7bd62016-04-14 17:47:04 +0200257 if (node->flags & LYS_CONFIG_SET) {
Michal Vasko43271ee2016-02-05 14:27:09 +0100258 /* print config when it differs from the parent ... */
259 if (node->flags & LYS_CONFIG_W) {
260 yang_print_open(out, flag);
261 ly_print(out, "%*sconfig true;\n", LEVEL, INDENT);
262 } else if (node->flags & LYS_CONFIG_R) {
263 yang_print_open(out, flag);
264 ly_print(out, "%*sconfig false;\n", LEVEL, INDENT);
265 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200266 }
Michal Vasko43271ee2016-02-05 14:27:09 +0100267 } else if (node->flags & LYS_CONFIG_R) {
268 /* ... or it's a top-level state node */
269 yang_print_open(out, flag);
270 ly_print(out, "%*sconfig false;\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200271 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200272
Radek Krejci21c23052016-11-04 11:05:02 +0100273 if (node->nodetype & (LYS_LEAF | LYS_CHOICE | LYS_ANYDATA)) {
274 if (node->flags & LYS_MAND_TRUE) {
275 yang_print_open(out, flag);
276 ly_print(out, "%*smandatory true;\n", LEVEL, INDENT);
277 } else if (node->flags & LYS_MAND_FALSE) {
278 yang_print_open(out, flag);
279 ly_print(out, "%*smandatory false;\n", LEVEL, INDENT);
280 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200281 }
Michal Vasko2b8faed2015-06-09 12:51:20 +0200282
Radek Krejci32cce7c2015-12-09 16:44:13 +0100283 yang_print_snode_common(out, level, node, flag);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200284}
285
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200286static void
Radek Krejci9ff0a922016-07-14 13:08:05 +0200287yang_print_iffeature(struct lyout *out, int level, const struct lys_module *module, struct lys_iffeature *iffeature)
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200288{
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200289 ly_print(out, "%*sif-feature \"", LEVEL, INDENT);
Radek Krejci9ff0a922016-07-14 13:08:05 +0200290 ly_print_iffeature(out, module, iffeature);
291 ly_print(out, "\";\n");
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200292}
293
294static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100295yang_print_feature(struct lyout *out, int level, const struct lys_feature *feat)
Michal Vasko4773b762015-07-07 12:15:10 +0200296{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100297 int i, flag = 0;
Michal Vasko30f6e912015-07-07 12:24:27 +0200298
Radek Krejci32cce7c2015-12-09 16:44:13 +0100299 ly_print(out, "%*sfeature %s", LEVEL, INDENT, feat->name);
Michal Vasko4773b762015-07-07 12:15:10 +0200300 level++;
301
Radek Krejci32cce7c2015-12-09 16:44:13 +0100302 yang_print_snode_common(out, level, (struct lys_node *)feat, &flag);
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200303 for (i = 0; i < feat->iffeature_size; ++i) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100304 yang_print_open(out, &flag);
Radek Krejci9ff0a922016-07-14 13:08:05 +0200305 yang_print_iffeature(out, level, feat->module, &feat->iffeature[i]);
Michal Vasko30f6e912015-07-07 12:24:27 +0200306 }
Michal Vasko4773b762015-07-07 12:15:10 +0200307
308 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100309 yang_print_close(out, level, flag);
Michal Vasko4773b762015-07-07 12:15:10 +0200310}
311
312static void
Radek Krejcia1a6b762016-11-14 09:53:38 +0900313yang_print_extension(struct lyout *out, int level, const struct lys_ext *ext)
314{
315 int flag = 0;
316
317 ly_print(out, "%*sextension %s", LEVEL, INDENT, ext->name);
318 level++;
319
320 yang_print_snode_common(out, level, (struct lys_node *)ext, &flag);
321 if (ext->argument) {
322 yang_print_open(out, &flag);
323 ly_print(out, "%*sargument %s", LEVEL, INDENT, ext->argument);
324
325 if (ext->flags & LYS_YINELEM) {
326 level++;
Radek Krejcie534c132016-11-23 13:32:31 +0100327 ly_print(out, " {\n%*syin-element true;\n", LEVEL, INDENT);
Radek Krejcia1a6b762016-11-14 09:53:38 +0900328 level--;
Radek Krejcie534c132016-11-23 13:32:31 +0100329 ly_print(out, "%*s}\n", LEVEL, INDENT);
330 } else {
331 ly_print(out, ";\n");
Radek Krejcia1a6b762016-11-14 09:53:38 +0900332 }
333 }
334
335 level--;
336 yang_print_close(out, level, flag);
337}
338
339static void
Radek Krejci32cce7c2015-12-09 16:44:13 +0100340yang_print_restr(struct lyout *out, int level, const struct lys_restr *restr, int *flag)
Radek Krejci41726f92015-06-19 13:11:05 +0200341{
Radek Krejci0bd5db42015-06-19 13:30:07 +0200342 if (restr->dsc != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100343 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100344 yang_print_text(out, level, "description", restr->dsc, 0);
Radek Krejci41726f92015-06-19 13:11:05 +0200345 }
Radek Krejci0bd5db42015-06-19 13:30:07 +0200346 if (restr->ref != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100347 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100348 yang_print_text(out, level, "reference", restr->ref, 0);
Radek Krejci41726f92015-06-19 13:11:05 +0200349 }
Radek Krejci0bd5db42015-06-19 13:30:07 +0200350 if (restr->eapptag != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100351 yang_print_open(out, flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200352 ly_print(out, "%*serror-app-tag \"%s\";\n", LEVEL, INDENT, restr->eapptag);
Radek Krejci41726f92015-06-19 13:11:05 +0200353 }
Radek Krejci0bd5db42015-06-19 13:30:07 +0200354 if (restr->emsg != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100355 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100356 yang_print_text(out, level, "error-message", restr->emsg, 0);
Radek Krejci41726f92015-06-19 13:11:05 +0200357 }
358}
359
360static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100361yang_print_when(struct lyout *out, int level, const struct lys_module *module, const struct lys_when *when)
Michal Vasko1f0428a2015-07-07 14:55:04 +0200362{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100363 int flag = 0;
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100364 const char *str;
Michal Vaskof9893382015-10-09 14:03:04 +0200365
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100366 str = transform_json2schema(module, when->cond);
367 if (!str) {
Michal Vaskof9893382015-10-09 14:03:04 +0200368 ly_print(out, "(!error!)");
369 return;
370 }
371
Michal Vasko25cb6c62016-02-12 14:36:21 +0100372 ly_print(out, "%*swhen \"", LEVEL, INDENT);
373 yang_encode(out, str, -1);
374 ly_print(out, "\"");
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100375 lydict_remove(module->ctx, str);
Michal Vaskof9893382015-10-09 14:03:04 +0200376
Michal Vasko1f0428a2015-07-07 14:55:04 +0200377 level++;
378 if (when->dsc) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100379 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100380 yang_print_text(out, level, "description", when->dsc, 0);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200381 }
382 if (when->ref) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100383 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100384 yang_print_text(out, level, "reference", when->ref, 0);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200385 }
386 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100387 yang_print_close(out, level, flag);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200388}
389
390static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100391yang_print_type(struct lyout *out, int level, const struct lys_module *module, const struct lys_type *type)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200392{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200393 int i;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100394 int flag = 0, flag2;
Michal Vasko0fb82c62015-10-20 13:41:53 +0200395 const char *str;
Radek Krejcic071c542016-01-27 14:57:51 +0100396 struct lys_module *mod;
Radek Krejci25d782a2015-05-22 15:03:23 +0200397
Michal Vasko0fb82c62015-10-20 13:41:53 +0200398 if (type->module_name) {
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100399 ly_print(out, "%*stype %s:%s", LEVEL, INDENT,
400 transform_module_name2import_prefix(module, type->module_name), type->der->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200401 } else {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100402 ly_print(out, "%*stype %s", LEVEL, INDENT, type->der->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200403 }
404 level++;
405 switch (type->base) {
406 case LY_TYPE_BINARY:
Michal Vasko4634cda2016-02-16 09:22:09 +0100407 if (type->info.binary.length) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100408 yang_print_open(out, &flag);
Michal Vasko25cb6c62016-02-12 14:36:21 +0100409 ly_print(out, "%*slength \"", LEVEL, INDENT);
410 yang_encode(out, type->info.binary.length->expr, -1);
411 ly_print(out, "\"");
Radek Krejci32cce7c2015-12-09 16:44:13 +0100412 flag2 = 0;
413 yang_print_restr(out, level + 1, type->info.binary.length, &flag2);
414 yang_print_close(out, level, flag2);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200415 }
416 break;
417 case LY_TYPE_BITS:
418 for (i = 0; i < type->info.bits.count; ++i) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100419 yang_print_open(out, &flag);
Michal Vasko3f053ef2016-02-12 14:27:13 +0100420 ly_print(out, "%*sbit %s", LEVEL, INDENT, type->info.bits.bit[i].name);
421 flag2 = 0;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200422 level++;
Michal Vasko3f053ef2016-02-12 14:27:13 +0100423 yang_print_snode_common(out, level, (struct lys_node *)&type->info.bits.bit[i], &flag2);
424 if (!(type->info.bits.bit[i].flags & LYS_AUTOASSIGNED)) {
425 yang_print_open(out, &flag2);
426 ly_print(out, "%*sposition %u;\n", LEVEL, INDENT, type->info.bits.bit[i].pos);
427 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200428 level--;
Michal Vasko3f053ef2016-02-12 14:27:13 +0100429 yang_print_close(out, level, flag2);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200430 }
431 break;
432 case LY_TYPE_DEC64:
Radek Krejcib51d5932016-09-08 14:02:52 +0200433 if (!type->der->type.der) {
Michal Vasko4634cda2016-02-16 09:22:09 +0100434 yang_print_open(out, &flag);
435 ly_print(out, "%*sfraction-digits %d;\n", LEVEL, INDENT, type->info.dec64.dig);
436 }
Michal Vaskoea505ee2015-07-07 14:01:00 +0200437 if (type->info.dec64.range != NULL) {
Michal Vasko4634cda2016-02-16 09:22:09 +0100438 yang_print_open(out, &flag);
Michal Vasko25cb6c62016-02-12 14:36:21 +0100439 ly_print(out, "%*srange \"", LEVEL, INDENT);
440 yang_encode(out, type->info.dec64.range->expr, -1);
441 ly_print(out, "\"");
Radek Krejci32cce7c2015-12-09 16:44:13 +0100442 flag2 = 0;
443 yang_print_restr(out, level + 1, type->info.dec64.range, &flag2);
444 yang_print_close(out, level, flag2);
Michal Vaskoea505ee2015-07-07 14:01:00 +0200445 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200446 break;
447 case LY_TYPE_ENUM:
448 for (i = 0; i < type->info.enums.count; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100449 yang_print_open(out, &flag);
Michal Vasko3f053ef2016-02-12 14:27:13 +0100450 ly_print(out, "%*senum \"%s\"", LEVEL, INDENT, type->info.enums.enm[i].name);
451 flag2 = 0;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200452 level++;
Michal Vasko3f053ef2016-02-12 14:27:13 +0100453 yang_print_snode_common(out, level, (struct lys_node *)&type->info.enums.enm[i], &flag2);
454 if (!(type->info.enums.enm[i].flags & LYS_AUTOASSIGNED)) {
455 yang_print_open(out, &flag2);
456 ly_print(out, "%*svalue %d;\n", LEVEL, INDENT, type->info.enums.enm[i].value);
457 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200458 level--;
Michal Vasko3f053ef2016-02-12 14:27:13 +0100459 yang_print_close(out, level, flag2);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200460 }
461 break;
462 case LY_TYPE_IDENT:
Michal Vaskof2d43962016-09-02 11:10:16 +0200463 if (type->info.ident.count) {
Michal Vasko4634cda2016-02-16 09:22:09 +0100464 yang_print_open(out, &flag);
Michal Vaskof2d43962016-09-02 11:10:16 +0200465 for (i = 0; i < type->info.ident.count; ++i) {
466 mod = lys_main_module(type->info.ident.ref[i]->module);
467 if (lys_main_module(module) == mod) {
468 ly_print(out, "%*sbase %s;\n", LEVEL, INDENT, type->info.ident.ref[i]->name);
469 } else {
470 ly_print(out, "%*sbase %s:%s;\n", LEVEL, INDENT, transform_module_name2import_prefix(module, mod->name),
471 type->info.ident.ref[i]->name);
472 }
Michal Vasko4634cda2016-02-16 09:22:09 +0100473 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200474 }
475 break;
476 case LY_TYPE_INST:
Radek Krejciaf351422015-06-19 14:49:38 +0200477 if (type->info.inst.req == 1) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100478 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100479 ly_print(out, "%*srequire-instance true;\n", LEVEL, INDENT);
Radek Krejciaf351422015-06-19 14:49:38 +0200480 } else if (type->info.inst.req == -1) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100481 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100482 ly_print(out, "%*srequire-instance false;\n", LEVEL, INDENT);
Radek Krejciaf351422015-06-19 14:49:38 +0200483 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200484 break;
485 case LY_TYPE_INT8:
486 case LY_TYPE_INT16:
487 case LY_TYPE_INT32:
488 case LY_TYPE_INT64:
489 case LY_TYPE_UINT8:
490 case LY_TYPE_UINT16:
491 case LY_TYPE_UINT32:
492 case LY_TYPE_UINT64:
Michal Vasko4634cda2016-02-16 09:22:09 +0100493 if (type->info.num.range) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100494 yang_print_open(out, &flag);
Michal Vasko25cb6c62016-02-12 14:36:21 +0100495 ly_print(out, "%*srange \"", LEVEL, INDENT);
496 yang_encode(out, type->info.num.range->expr, -1);
497 ly_print(out, "\"");
Radek Krejci32cce7c2015-12-09 16:44:13 +0100498 flag2 = 0;
499 yang_print_restr(out, level + 1, type->info.num.range, &flag2);
500 yang_print_close(out, level, flag2);
Radek Krejcif2860132015-06-20 12:37:20 +0200501 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200502 break;
503 case LY_TYPE_LEAFREF:
Radek Krejci0dbff6a2016-07-17 12:40:18 +0200504 if (ly_strequal(type->der->name, "leafref", 0)) {
Michal Vasko4634cda2016-02-16 09:22:09 +0100505 yang_print_open(out, &flag);
506 str = transform_json2schema(module, type->info.lref.path);
507 ly_print(out, "%*spath \"%s\";\n", LEVEL, INDENT, str);
508 lydict_remove(module->ctx, str);
509 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200510 break;
511 case LY_TYPE_STRING:
Radek Krejci5fbc9162015-06-19 14:11:11 +0200512 if (type->info.str.length) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100513 yang_print_open(out, &flag);
Michal Vasko25cb6c62016-02-12 14:36:21 +0100514 ly_print(out, "%*slength \"", LEVEL, INDENT);
515 yang_encode(out, type->info.str.length->expr, -1);
516 ly_print(out, "\"");
Radek Krejci32cce7c2015-12-09 16:44:13 +0100517 flag2 = 0;
518 yang_print_restr(out, level + 1, type->info.str.length, &flag2);
519 yang_print_close(out, level, flag2);
Radek Krejci061bd522015-06-19 13:45:16 +0200520 }
Radek Krejci5fbc9162015-06-19 14:11:11 +0200521 for (i = 0; i < type->info.str.pat_count; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100522 yang_print_open(out, &flag);
Michal Vasko25cb6c62016-02-12 14:36:21 +0100523 ly_print(out, "%*spattern \"", LEVEL, INDENT);
Radek Krejci0d23e7a2016-08-04 12:46:17 +0200524 yang_encode(out, &type->info.str.patterns[i].expr[1], -1);
Michal Vasko25cb6c62016-02-12 14:36:21 +0100525 ly_print(out, "\"");
Radek Krejci32cce7c2015-12-09 16:44:13 +0100526 flag2 = 0;
Pavol Vican075fdf72016-09-21 09:34:28 +0200527 if (type->info.str.patterns[i].expr[0] == 0x15) {
Radek Krejci0d23e7a2016-08-04 12:46:17 +0200528 yang_print_open(out, &flag2);
Pavol Vican075fdf72016-09-21 09:34:28 +0200529 yang_print_text(out, level + 1, "modifier", "invert-match", 1);
Radek Krejci0d23e7a2016-08-04 12:46:17 +0200530 }
Radek Krejci32cce7c2015-12-09 16:44:13 +0100531 yang_print_restr(out, level + 1, &type->info.str.patterns[i], &flag2);
532 yang_print_close(out, level, flag2);
Radek Krejci5fbc9162015-06-19 14:11:11 +0200533 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200534 break;
535 case LY_TYPE_UNION:
536 for (i = 0; i < type->info.uni.count; ++i) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100537 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200538 yang_print_type(out, level, module, &type->info.uni.types[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200539 }
540 break;
541 default:
542 /* other types do not have substatements */
543 break;
544 }
Radek Krejcie534c132016-11-23 13:32:31 +0100545
546 /* extensions */
547 if (type->ext_size) {
548 yang_print_open(out, &flag);
549 yang_print_extension_instances(out, level, module, type->ext, type->ext_size);
550 }
551
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200552 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100553 yang_print_close(out, level, flag);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200554}
555
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200556static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100557yang_print_must(struct lyout *out, int level, const struct lys_module *module, const struct lys_restr *must)
Michal Vasko7f976ee2015-06-09 13:55:41 +0200558{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100559 int flag = 0;
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100560 const char *str;
Michal Vaskof9893382015-10-09 14:03:04 +0200561
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100562 str = transform_json2schema(module, must->expr);
563 if (!str) {
Michal Vaskof9893382015-10-09 14:03:04 +0200564 ly_print(out, "(!error!)");
565 return;
566 }
567
Michal Vasko25cb6c62016-02-12 14:36:21 +0100568 ly_print(out, "%*smust \"", LEVEL, INDENT);
569 yang_encode(out, str, -1);
570 ly_print(out, "\"");
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100571 lydict_remove(module->ctx, str);
Michal Vaskof9893382015-10-09 14:03:04 +0200572
Radek Krejci32cce7c2015-12-09 16:44:13 +0100573 yang_print_restr(out, level + 1, must, &flag);
574 yang_print_close(out, level, flag);
Michal Vasko7f976ee2015-06-09 13:55:41 +0200575}
576
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200577static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100578yang_print_unique(struct lyout *out, int level, const struct lys_unique *uniq)
Michal Vasko1ef07972015-07-07 14:01:35 +0200579{
580 int i;
581
Radek Krejci76b07902015-10-09 09:11:25 +0200582 ly_print(out, "%*sunique \"", LEVEL, INDENT);
Radek Krejci581ce772015-11-10 17:22:40 +0100583 for (i = 0; i < uniq->expr_size; i++) {
584 ly_print(out, "%s%s", uniq->expr[i], i + 1 < uniq->expr_size ? " " : "");
Michal Vasko1ef07972015-07-07 14:01:35 +0200585 }
Radek Krejci76b07902015-10-09 09:11:25 +0200586 ly_print(out, "\";\n");
Michal Vasko1ef07972015-07-07 14:01:35 +0200587}
588
589static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100590yang_print_refine(struct lyout *out, int level, const struct lys_module *module, const struct lys_refine *refine)
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200591{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200592 int i;
Michal Vaskoa8b25952015-10-20 15:30:25 +0200593 const char *str;
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200594
Michal Vasko5d112852016-02-12 16:47:13 +0100595 str = transform_json2schema(module, refine->target_name);
Michal Vaskoa8b25952015-10-20 15:30:25 +0200596 ly_print(out, "%*srefine \"%s\" {\n", LEVEL, INDENT, str);
597 lydict_remove(module->ctx, str);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200598 level++;
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200599
Radek Krejci1574a8d2015-08-03 14:16:52 +0200600 if (refine->flags & LYS_CONFIG_W) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100601 ly_print(out, "%*sconfig true;\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200602 } else if (refine->flags & LYS_CONFIG_R) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100603 ly_print(out, "%*sconfig false;\n", LEVEL, INDENT);
Radek Krejci6a113852015-07-03 16:04:20 +0200604 }
605
Radek Krejci1574a8d2015-08-03 14:16:52 +0200606 if (refine->flags & LYS_MAND_TRUE) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100607 ly_print(out, "%*smandatory true;\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200608 } else if (refine->flags & LYS_MAND_FALSE) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100609 ly_print(out, "%*smandatory false;\n", LEVEL, INDENT);
Radek Krejci6a113852015-07-03 16:04:20 +0200610 }
611
Radek Krejci32cce7c2015-12-09 16:44:13 +0100612 yang_print_snode_common(out, level, (struct lys_node *)refine, NULL);
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200613
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200614 for (i = 0; i < refine->must_size; ++i) {
Michal Vaskof9893382015-10-09 14:03:04 +0200615 yang_print_must(out, level, module, &refine->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200616 }
Radek Krejcibf06c4f2016-07-29 15:49:56 +0200617 for (i = 0; i < refine->iffeature_size; i++) {
618 yang_print_iffeature(out, level, module, &refine->iffeature[i]);
619 }
Pavol Vican3e7c73a2016-08-17 10:24:11 +0200620 for (i = 0; i < refine->dflt_size; ++i) {
621 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, refine->dflt[i]);
622 }
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200623
Pavol Vican3e7c73a2016-08-17 10:24:11 +0200624 if (refine->target_type == LYS_CONTAINER) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200625 if (refine->mod.presence != NULL) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100626 yang_print_text(out, level, "presence", refine->mod.presence, 1);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200627 }
Radek Krejci76512572015-08-04 09:47:08 +0200628 } else if (refine->target_type & (LYS_LIST | LYS_LEAFLIST)) {
Radek Krejci0f04a6c2016-04-14 16:16:36 +0200629 if (refine->flags & LYS_RFN_MINSET) {
Radek Krejci76b07902015-10-09 09:11:25 +0200630 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, refine->mod.list.min);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200631 }
Radek Krejci0f04a6c2016-04-14 16:16:36 +0200632 if (refine->flags & LYS_RFN_MAXSET) {
Radek Krejci0d7b2472016-02-12 11:11:03 +0100633 if (refine->mod.list.max) {
634 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, refine->mod.list.max);
635 } else {
636 ly_print(out, "%*smax-elements \"unbounded\";\n", LEVEL, INDENT);
637 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200638 }
639 }
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200640
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200641 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200642 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200643}
644
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200645static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100646yang_print_deviation(struct lyout *out, int level, const struct lys_module *module,
647 const struct lys_deviation *deviation)
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200648{
Pavol Vican38321d02016-08-16 14:56:02 +0200649 int i, j, k;
Michal Vaskoa8b25952015-10-20 15:30:25 +0200650 const char *str;
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200651
Michal Vasko5d112852016-02-12 16:47:13 +0100652 str = transform_json2schema(module, deviation->target_name);
Michal Vaskoa8b25952015-10-20 15:30:25 +0200653 ly_print(out, "%*sdeviation \"%s\" {\n", LEVEL, INDENT, str);
654 lydict_remove(module->ctx, str);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200655 level++;
656
657 if (deviation->dsc) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100658 yang_print_text(out, level, "description", deviation->dsc, 0);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200659 }
660 if (deviation->ref) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100661 yang_print_text(out, level, "reference", deviation->ref, 0);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200662 }
Radek Krejcie534c132016-11-23 13:32:31 +0100663 if (deviation->ext_size) {
664 yang_print_extension_instances(out, level, module, deviation->ext, deviation->ext_size);
665 }
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200666
667 for (i = 0; i < deviation->deviate_size; ++i) {
Radek Krejci76b07902015-10-09 09:11:25 +0200668 ly_print(out, "%*sdeviate ", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200669 if (deviation->deviate[i].mod == LY_DEVIATE_NO) {
Michal Vaskod875e882016-05-19 10:57:30 +0200670 ly_print(out, "not-supported;\n");
671 continue;
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200672 } else if (deviation->deviate[i].mod == LY_DEVIATE_ADD) {
Radek Krejci76b07902015-10-09 09:11:25 +0200673 ly_print(out, "add {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200674 } else if (deviation->deviate[i].mod == LY_DEVIATE_RPL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200675 ly_print(out, "replace {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200676 } else if (deviation->deviate[i].mod == LY_DEVIATE_DEL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200677 ly_print(out, "delete {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200678 }
679 level++;
680
Radek Krejci1574a8d2015-08-03 14:16:52 +0200681 if (deviation->deviate[i].flags & LYS_CONFIG_W) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100682 ly_print(out, "%*sconfig true;\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200683 } else if (deviation->deviate[i].flags & LYS_CONFIG_R) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100684 ly_print(out, "%*sconfig false;\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200685 }
686
Radek Krejci1574a8d2015-08-03 14:16:52 +0200687 if (deviation->deviate[i].flags & LYS_MAND_TRUE) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100688 ly_print(out, "%*smandatory true;\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200689 } else if (deviation->deviate[i].flags & LYS_MAND_FALSE) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100690 ly_print(out, "%*smandatory false;\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200691 }
692
Pavol Vican38321d02016-08-16 14:56:02 +0200693 for (k = 0; k < deviation->deviate[i].dflt_size; ++k) {
694 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, deviation->deviate[i].dflt[k]);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200695 }
696
Radek Krejci0d7b2472016-02-12 11:11:03 +0100697 if (deviation->deviate[i].min_set) {
Radek Krejci76b07902015-10-09 09:11:25 +0200698 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, deviation->deviate[i].min);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200699 }
Radek Krejci0d7b2472016-02-12 11:11:03 +0100700 if (deviation->deviate[i].max_set) {
701 if (deviation->deviate[i].max) {
702 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, deviation->deviate[i].max);
703 } else {
704 ly_print(out, "%*smax-elements \"unbounded\";\n", LEVEL, INDENT);
705 }
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200706 }
707
708 for (j = 0; j < deviation->deviate[i].must_size; ++j) {
Michal Vaskof9893382015-10-09 14:03:04 +0200709 yang_print_must(out, level, module, &deviation->deviate[i].must[j]);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200710 }
711
712 for (j = 0; j < deviation->deviate[i].unique_size; ++j) {
Radek Krejci76b07902015-10-09 09:11:25 +0200713 yang_print_unique(out, level, &deviation->deviate[i].unique[j]);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200714 }
715
716 if (deviation->deviate[i].type) {
Radek Krejci76b07902015-10-09 09:11:25 +0200717 yang_print_type(out, level, module, deviation->deviate[i].type);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200718 }
719
720 if (deviation->deviate[i].units) {
Michal Vaskod875e882016-05-19 10:57:30 +0200721 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, deviation->deviate[i].units);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200722 }
723
724 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200725 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200726 }
727
728 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200729 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200730}
731
732static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100733yang_print_augment(struct lyout *out, int level, const struct lys_module *module,
734 const struct lys_node_augment *augment)
Michal Vasko6f25f212015-07-07 15:42:07 +0200735{
736 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200737 struct lys_node *sub;
Michal Vasko488c19e2015-10-20 15:21:00 +0200738 const char *str;
Michal Vasko6f25f212015-07-07 15:42:07 +0200739
Michal Vasko5d112852016-02-12 16:47:13 +0100740 str = transform_json2schema(module, augment->target_name);
Michal Vasko488c19e2015-10-20 15:21:00 +0200741 ly_print(out, "%*saugment \"%s\" {\n", LEVEL, INDENT, str);
742 lydict_remove(module->ctx, str);
Michal Vasko6f25f212015-07-07 15:42:07 +0200743 level++;
744
Radek Krejci32cce7c2015-12-09 16:44:13 +0100745 yang_print_nacmext(out, level, (struct lys_node *)augment, module, NULL);
746 yang_print_snode_common(out, level, (struct lys_node *)augment, NULL);
Michal Vasko6f25f212015-07-07 15:42:07 +0200747
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200748 for (i = 0; i < augment->iffeature_size; i++) {
Radek Krejci9ff0a922016-07-14 13:08:05 +0200749 yang_print_iffeature(out, level, module, &augment->iffeature[i]);
Michal Vasko6f25f212015-07-07 15:42:07 +0200750 }
751
752 if (augment->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200753 yang_print_when(out, level, module, augment->when);
Michal Vasko6f25f212015-07-07 15:42:07 +0200754 }
755
756 LY_TREE_FOR(augment->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100757 /* only our augment */
758 if (sub->parent != (struct lys_node *)augment) {
759 continue;
760 }
Radek Krejci76b07902015-10-09 09:11:25 +0200761 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200762 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
Radek Krejcibf2abff2016-08-23 15:51:52 +0200763 LYS_USES | LYS_ANYDATA | LYS_CASE | LYS_ACTION);
Michal Vasko6f25f212015-07-07 15:42:07 +0200764 }
765
766 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200767 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko6f25f212015-07-07 15:42:07 +0200768}
769
770static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100771yang_print_typedef(struct lyout *out, int level, const struct lys_module *module, const struct lys_tpdf *tpdf)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200772{
Radek Krejci76b07902015-10-09 09:11:25 +0200773 ly_print(out, "%*stypedef %s {\n", LEVEL, INDENT, tpdf->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200774 level++;
Radek Krejcibd117f02016-11-04 16:28:08 +0100775 const char *dflt;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200776
Radek Krejci32cce7c2015-12-09 16:44:13 +0100777 yang_print_snode_common(out, level, (struct lys_node *)tpdf, NULL);
Radek Krejci76b07902015-10-09 09:11:25 +0200778 yang_print_type(out, level, module, &tpdf->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200779 if (tpdf->units != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200780 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, tpdf->units);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200781 }
782 if (tpdf->dflt != NULL) {
Radek Krejcibd117f02016-11-04 16:28:08 +0100783 if (tpdf->flags & LYS_DFLTJSON) {
784 dflt = transform_json2schema(module, tpdf->dflt);
785 } else {
786 dflt = tpdf->dflt;
787 }
788 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, dflt);
789 if (tpdf->flags & LYS_DFLTJSON) {
790 lydict_remove(module->ctx, dflt);
791 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200792 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200793
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200794 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200795 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200796}
797
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200798static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100799yang_print_identity(struct lyout *out, int level, const struct lys_ident *ident)
Radek Krejci6793db02015-05-22 17:49:54 +0200800{
Radek Krejci018f1f52016-08-03 16:01:20 +0200801 int flag = 0, i;
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100802 struct lys_module *mod;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100803
804 ly_print(out, "%*sidentity %s", LEVEL, INDENT, ident->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200805 level++;
Radek Krejci6793db02015-05-22 17:49:54 +0200806
Radek Krejci32cce7c2015-12-09 16:44:13 +0100807 yang_print_snode_common(out, level, (struct lys_node *)ident, &flag);
Radek Krejci018f1f52016-08-03 16:01:20 +0200808 for (i = 0; i < ident->base_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100809 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200810 ly_print(out, "%*sbase ", LEVEL, INDENT);
Radek Krejci018f1f52016-08-03 16:01:20 +0200811 mod = lys_main_module(ident->base[i]->module);
Radek Krejcic4283442016-04-22 09:19:27 +0200812 if (lys_main_module(ident->module) != mod) {
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100813 ly_print(out, "%s:", transform_module_name2import_prefix(ident->module, mod->name));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200814 }
Radek Krejci018f1f52016-08-03 16:01:20 +0200815 ly_print(out, "%s;\n", ident->base[i]->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200816 }
Radek Krejci6793db02015-05-22 17:49:54 +0200817
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200818 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100819 yang_print_close(out, level, flag);
Radek Krejci6793db02015-05-22 17:49:54 +0200820}
821
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200822static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100823yang_print_container(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200824{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100825 int i, flag = 0;
Radek Krejci76512572015-08-04 09:47:08 +0200826 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200827 struct lys_node_container *cont = (struct lys_node_container *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200828
Radek Krejci32cce7c2015-12-09 16:44:13 +0100829 ly_print(out, "%*scontainer %s", LEVEL, INDENT, node->name);
Michal Vasko7f976ee2015-06-09 13:55:41 +0200830
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200831 level++;
Radek Krejci6a113852015-07-03 16:04:20 +0200832
Radek Krejci32cce7c2015-12-09 16:44:13 +0100833 yang_print_nacmext(out, level, node, node->module, &flag);
Radek Krejci6a113852015-07-03 16:04:20 +0200834
Radek Krejci8d81e002015-12-10 11:18:59 +0100835 if (cont->when) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100836 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100837 yang_print_when(out, level, node->module, cont->when);
838 }
839
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200840 for (i = 0; i < cont->iffeature_size; i++) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100841 yang_print_open(out, &flag);
Radek Krejci9ff0a922016-07-14 13:08:05 +0200842 yang_print_iffeature(out, level, node->module, &cont->iffeature[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200843 }
Michal Vasko7f976ee2015-06-09 13:55:41 +0200844
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200845 for (i = 0; i < cont->must_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100846 yang_print_open(out, &flag);
Michal Vaskof9893382015-10-09 14:03:04 +0200847 yang_print_must(out, level, node->module, &cont->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200848 }
Michal Vasko7f976ee2015-06-09 13:55:41 +0200849
Radek Krejci8d81e002015-12-10 11:18:59 +0100850 if (cont->presence != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100851 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100852 yang_print_text(out, level, "presence", cont->presence, 1);
Michal Vasko4773b762015-07-07 12:15:10 +0200853 }
854
Radek Krejci8d81e002015-12-10 11:18:59 +0100855 yang_print_snode_common2(out, level, node, &flag);
856
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200857 for (i = 0; i < cont->tpdf_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100858 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200859 yang_print_typedef(out, level, node->module, &cont->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200860 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200861
Radek Krejci1d82ef62015-08-07 14:44:40 +0200862 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100863 /* augments */
864 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200865 continue;
866 }
Radek Krejci32cce7c2015-12-09 16:44:13 +0100867 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200868 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200869 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
Michal Vaskob15cae22016-09-15 09:40:56 +0200870 LYS_USES | LYS_GROUPING | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200871 }
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200872
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200873 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100874 yang_print_close(out, level, flag);
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200875}
876
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200877static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100878yang_print_case(struct lyout *out, int level, const struct lys_node *node)
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200879{
Michal Vasko4773b762015-07-07 12:15:10 +0200880 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200881 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200882 struct lys_node_case *cas = (struct lys_node_case *)node;
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200883
Radek Krejci76b07902015-10-09 09:11:25 +0200884 ly_print(out, "%*scase %s {\n", LEVEL, INDENT, cas->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200885 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100886 yang_print_nacmext(out, level, node, node->module, NULL);
887 yang_print_snode_common2(out, level, node, NULL);
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200888
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200889 for (i = 0; i < cas->iffeature_size; i++) {
Radek Krejci9ff0a922016-07-14 13:08:05 +0200890 yang_print_iffeature(out, level, node->module, &cas->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200891 }
892
Michal Vasko1f0428a2015-07-07 14:55:04 +0200893 if (cas->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200894 yang_print_when(out, level, node->module, cas->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200895 }
896
Radek Krejci1d82ef62015-08-07 14:44:40 +0200897 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100898 /* augments */
899 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200900 continue;
901 }
Radek Krejci76b07902015-10-09 09:11:25 +0200902 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200903 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
Radek Krejcibf2abff2016-08-23 15:51:52 +0200904 LYS_USES | LYS_ANYDATA);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200905 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200906
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200907 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200908 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200909}
910
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200911static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100912yang_print_choice(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200913{
Michal Vasko4773b762015-07-07 12:15:10 +0200914 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200915 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200916 struct lys_node_choice *choice = (struct lys_node_choice *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200917
Radek Krejci76b07902015-10-09 09:11:25 +0200918 ly_print(out, "%*schoice %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +0200919
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200920 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100921 yang_print_nacmext(out, level, node, node->module, NULL);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200922 if (choice->dflt != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200923 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, choice->dflt->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200924 }
Michal Vasko16083662015-06-09 14:00:45 +0200925
Radek Krejci32cce7c2015-12-09 16:44:13 +0100926 yang_print_snode_common2(out, level, node, NULL);
Michal Vasko4773b762015-07-07 12:15:10 +0200927
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200928 for (i = 0; i < choice->iffeature_size; i++) {
Radek Krejci9ff0a922016-07-14 13:08:05 +0200929 yang_print_iffeature(out, level, node->module, &choice->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200930 }
931
Michal Vasko1f0428a2015-07-07 14:55:04 +0200932 if (choice->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200933 yang_print_when(out, level, node->module, choice->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200934 }
935
Radek Krejci1d82ef62015-08-07 14:44:40 +0200936 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100937 /* augments */
938 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200939 continue;
940 }
Radek Krejci76b07902015-10-09 09:11:25 +0200941 yang_print_snode(out, level, sub,
Radek Krejcibf2abff2016-08-23 15:51:52 +0200942 LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_CASE);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200943 }
944 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200945 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200946}
947
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200948static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100949yang_print_leaf(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200950{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200951 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200952 struct lys_node_leaf *leaf = (struct lys_node_leaf *)node;
Radek Krejcibd117f02016-11-04 16:28:08 +0100953 const char *dflt;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200954
Radek Krejci76b07902015-10-09 09:11:25 +0200955 ly_print(out, "%*sleaf %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +0200956
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200957 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100958 yang_print_nacmext(out, level, node, node->module, NULL);
Radek Krejci8d81e002015-12-10 11:18:59 +0100959 if (leaf->when) {
960 yang_print_when(out, level, node->module, leaf->when);
961 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200962 for (i = 0; i < leaf->iffeature_size; i++) {
Radek Krejci9ff0a922016-07-14 13:08:05 +0200963 yang_print_iffeature(out, level, node->module, &leaf->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200964 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200965 for (i = 0; i < leaf->must_size; i++) {
Michal Vaskof9893382015-10-09 14:03:04 +0200966 yang_print_must(out, level, node->module, &leaf->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200967 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100968 yang_print_snode_common2(out, level, node, NULL);
Radek Krejci76b07902015-10-09 09:11:25 +0200969 yang_print_type(out, level, node->module, &leaf->type);
Michal Vaskod875e882016-05-19 10:57:30 +0200970 if (leaf->units) {
Radek Krejci76b07902015-10-09 09:11:25 +0200971 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, leaf->units);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200972 }
Michal Vaskod875e882016-05-19 10:57:30 +0200973 if (leaf->dflt) {
Radek Krejcibd117f02016-11-04 16:28:08 +0100974 if (leaf->flags & LYS_DFLTJSON) {
975 dflt = transform_json2schema(node->module, leaf->dflt);
976 } else {
977 dflt = leaf->dflt;
978 }
979 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, dflt);
980 if (leaf->flags & LYS_DFLTJSON) {
981 lydict_remove(node->module->ctx, dflt);
982 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200983 }
984 level--;
Michal Vasko16083662015-06-09 14:00:45 +0200985
Radek Krejci76b07902015-10-09 09:11:25 +0200986 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko16083662015-06-09 14:00:45 +0200987}
988
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200989static void
Radek Krejcibf2abff2016-08-23 15:51:52 +0200990yang_print_anydata(struct lyout *out, int level, const struct lys_node *node)
Michal Vasko16083662015-06-09 14:00:45 +0200991{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100992 int i, flag = 0;
Radek Krejcibf2abff2016-08-23 15:51:52 +0200993 struct lys_node_anydata *any = (struct lys_node_anydata *)node;
Michal Vasko16083662015-06-09 14:00:45 +0200994
Radek Krejcibf2abff2016-08-23 15:51:52 +0200995 ly_print(out, "%*s%s %s", LEVEL, INDENT, any->nodetype == LYS_ANYXML ? "anyxml" : "anydata", any->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200996 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100997 yang_print_nacmext(out, level, node, node->module, &flag);
998 yang_print_snode_common2(out, level, node, &flag);
Radek Krejcibf2abff2016-08-23 15:51:52 +0200999 for (i = 0; i < any->iffeature_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001000 yang_print_open(out, &flag);
Radek Krejcibf2abff2016-08-23 15:51:52 +02001001 yang_print_iffeature(out, level, node->module, &any->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +02001002 }
Radek Krejcibf2abff2016-08-23 15:51:52 +02001003 for (i = 0; i < any->must_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001004 yang_print_open(out, &flag);
Radek Krejcibf2abff2016-08-23 15:51:52 +02001005 yang_print_must(out, level, node->module, &any->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001006 }
Radek Krejcibf2abff2016-08-23 15:51:52 +02001007 if (any->when) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001008 yang_print_open(out, &flag);
Radek Krejcibf2abff2016-08-23 15:51:52 +02001009 yang_print_when(out, level, node->module, any->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +02001010 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001011 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001012 yang_print_close(out, level, flag);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001013}
1014
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001015static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001016yang_print_leaflist(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001017{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001018 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001019 struct lys_node_leaflist *llist = (struct lys_node_leaflist *)node;
Radek Krejcibd117f02016-11-04 16:28:08 +01001020 const char *dflt;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001021
Radek Krejci76b07902015-10-09 09:11:25 +02001022 ly_print(out, "%*sleaf-list %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +02001023
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001024 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001025 yang_print_nacmext(out, level, node, node->module, NULL);
Radek Krejci8d81e002015-12-10 11:18:59 +01001026 if (llist->when) {
1027 yang_print_when(out, level, llist->module, llist->when);
1028 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001029 for (i = 0; i < llist->iffeature_size; i++) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001030 yang_print_iffeature(out, level, node->module, &llist->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +02001031 }
Radek Krejci8d81e002015-12-10 11:18:59 +01001032 for (i = 0; i < llist->must_size; i++) {
1033 yang_print_must(out, level, node->module, &llist->must[i]);
1034 }
1035 yang_print_snode_common2(out, level, node, NULL);
1036 yang_print_type(out, level, node->module, &llist->type);
Pavol Vican38321d02016-08-16 14:56:02 +02001037 for (i = 0; i < llist->dflt_size; ++i) {
Radek Krejcibd117f02016-11-04 16:28:08 +01001038 if (llist->flags & LYS_DFLTJSON) {
1039 dflt = transform_json2schema(node->module, llist->dflt[i]);
1040 } else {
1041 dflt = llist->dflt[i];
1042 }
1043 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, dflt);
1044 if (llist->flags & LYS_DFLTJSON) {
1045 lydict_remove(node->module->ctx, dflt);
1046 }
Pavol Vican38321d02016-08-16 14:56:02 +02001047 }
Radek Krejci8d81e002015-12-10 11:18:59 +01001048 if (llist->units != NULL) {
1049 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, llist->units);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001050 }
1051 if (llist->min > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +02001052 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, llist->min);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001053 }
1054 if (llist->max > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +02001055 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, llist->max);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001056 }
Radek Krejci8d81e002015-12-10 11:18:59 +01001057 if (llist->flags & LYS_USERORDERED) {
1058 ly_print(out, "%*sordered-by user;\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001059 }
1060 level--;
Michal Vasko16083662015-06-09 14:00:45 +02001061
Radek Krejci76b07902015-10-09 09:11:25 +02001062 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001063}
1064
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001065static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001066yang_print_list(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001067{
Michal Vasko1ef07972015-07-07 14:01:35 +02001068 int i;
Radek Krejci76512572015-08-04 09:47:08 +02001069 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001070 struct lys_node_list *list = (struct lys_node_list *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001071
Radek Krejci76b07902015-10-09 09:11:25 +02001072 ly_print(out, "%*slist %s {\n", LEVEL, INDENT, node->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001073 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001074 yang_print_nacmext(out, level, node, node->module, NULL);
Radek Krejci8d81e002015-12-10 11:18:59 +01001075 if (list->when) {
1076 yang_print_when(out, level, list->module, list->when);
1077 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001078 for (i = 0; i < list->iffeature_size; i++) {
Radek Krejci9ff0a922016-07-14 13:08:05 +02001079 yang_print_iffeature(out, level, node->module, &list->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +02001080 }
Radek Krejci8d81e002015-12-10 11:18:59 +01001081 for (i = 0; i < list->must_size; i++) {
1082 yang_print_must(out, level, list->module, &list->must[i]);
1083 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001084 if (list->keys_size) {
Radek Krejci5c08a992016-11-02 13:30:04 +01001085 ly_print(out, "%*skey \"%s\";\n", LEVEL, INDENT, list->keys_str);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001086 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001087 for (i = 0; i < list->unique_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +02001088 yang_print_unique(out, level, &list->unique[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001089 }
Radek Krejci8d81e002015-12-10 11:18:59 +01001090 yang_print_snode_common2(out, level, node, NULL);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001091 if (list->min > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +02001092 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, list->min);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001093 }
1094 if (list->max > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +02001095 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, list->max);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001096 }
Radek Krejci8d81e002015-12-10 11:18:59 +01001097 if (list->flags & LYS_USERORDERED) {
1098 ly_print(out, "%*sordered-by user;\n", LEVEL, INDENT);
Michal Vasko1f0428a2015-07-07 14:55:04 +02001099 }
Michal Vaskodff818b2015-06-09 14:01:12 +02001100
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001101 for (i = 0; i < list->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +02001102 yang_print_typedef(out, level, list->module, &list->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001103 }
Radek Krejci1d82ef62015-08-07 14:44:40 +02001104 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +01001105 /* augments */
1106 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +02001107 continue;
1108 }
Radek Krejci76b07902015-10-09 09:11:25 +02001109 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +02001110 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
Michal Vaskob15cae22016-09-15 09:40:56 +02001111 LYS_USES | LYS_GROUPING | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001112 }
1113 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001114 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001115}
1116
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001117static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001118yang_print_grouping(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001119{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001120 int i;
Michal Vasko0c5e9282016-02-15 13:11:57 +01001121 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001122 struct lys_node_grp *grp = (struct lys_node_grp *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001123
Radek Krejci76b07902015-10-09 09:11:25 +02001124 ly_print(out, "%*sgrouping %s {\n", LEVEL, INDENT, node->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001125 level++;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001126
Radek Krejci32cce7c2015-12-09 16:44:13 +01001127 yang_print_snode_common(out, level, node, NULL);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001128
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001129 for (i = 0; i < grp->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +02001130 yang_print_typedef(out, level, node->module, &grp->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001131 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001132
Michal Vasko0c5e9282016-02-15 13:11:57 +01001133 LY_TREE_FOR(node->child, sub) {
1134 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +02001135 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
Radek Krejcibf2abff2016-08-23 15:51:52 +02001136 LYS_USES | LYS_GROUPING | LYS_ANYDATA | LYS_ACTION);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001137 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001138
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001139 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001140 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001141}
1142
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001143static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001144yang_print_uses(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001145{
Radek Krejci32cce7c2015-12-09 16:44:13 +01001146 int i, flag = 0;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001147 struct lys_node_uses *uses = (struct lys_node_uses *)node;
Michal Vasko1bb7a5a2016-02-05 14:28:02 +01001148 struct lys_module *mod;
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001149
Radek Krejci76b07902015-10-09 09:11:25 +02001150 ly_print(out, "%*suses ", LEVEL, INDENT);
Michal Vasko1bb7a5a2016-02-05 14:28:02 +01001151 if (node->child) {
Michal Vasko6c629ac2016-02-15 14:08:23 +01001152 mod = lys_node_module(node->child);
Michal Vasko1dae8ec2016-02-15 14:49:01 +01001153 if (lys_node_module(node) != mod) {
Michal Vasko1bb7a5a2016-02-05 14:28:02 +01001154 ly_print(out, "%s:", transform_module_name2import_prefix(node->module, mod->name));
1155 }
Michal Vaskoc39c4b12015-07-07 14:55:33 +02001156 }
Radek Krejci32cce7c2015-12-09 16:44:13 +01001157 ly_print(out, "%s", uses->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001158 level++;
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001159
Radek Krejci32cce7c2015-12-09 16:44:13 +01001160 yang_print_nacmext(out, level, node, node->module, &flag);
1161 yang_print_snode_common(out, level, node, &flag);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001162 for (i = 0; i < uses->iffeature_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001163 yang_print_open(out, &flag);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001164 yang_print_iffeature(out, level, node->module, &uses->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +02001165 }
Michal Vasko1f0428a2015-07-07 14:55:04 +02001166 if (uses->when) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001167 yang_print_open(out, &flag);
Michal Vaskof9893382015-10-09 14:03:04 +02001168 yang_print_when(out, level, node->module, uses->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +02001169 }
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001170
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001171 for (i = 0; i < uses->refine_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001172 yang_print_open(out, &flag);
Michal Vaskof9893382015-10-09 14:03:04 +02001173 yang_print_refine(out, level, node->module, &uses->refine[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001174 }
Michal Vasko00b7cfe2015-06-09 13:56:38 +02001175
Michal Vasko6f25f212015-07-07 15:42:07 +02001176 for (i = 0; i < uses->augment_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001177 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001178 yang_print_augment(out, level, node->module, &uses->augment[i]);
Michal Vasko6f25f212015-07-07 15:42:07 +02001179 }
1180
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001181 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001182 yang_print_close(out, level, flag);
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001183}
1184
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001185static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001186yang_print_input_output(struct lyout *out, int level, const struct lys_node *node)
Michal Vasko5bbae102015-06-16 12:16:44 +02001187{
Michal Vaskof4d3d742015-06-16 11:51:09 +02001188 int i;
Radek Krejci76512572015-08-04 09:47:08 +02001189 struct lys_node *sub;
Michal Vasko44fb6382016-06-29 11:12:27 +02001190 struct lys_node_inout *inout = (struct lys_node_inout *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +02001191
Radek Krejci41a349b2016-10-24 19:21:59 +02001192 if (node->flags & LYS_IMPLICIT) {
1193 /* implicit input/output which is not a part of the schema */
1194 return;
1195 }
1196
Radek Krejci76b07902015-10-09 09:11:25 +02001197 ly_print(out, "%*s%s {\n", LEVEL, INDENT, (inout->nodetype == LYS_INPUT ? "input" : "output"));
Michal Vaskof4d3d742015-06-16 11:51:09 +02001198
1199 level++;
Radek Krejci12032a52016-07-29 15:42:56 +02001200 for (i = 0; i < inout->must_size; i++) {
1201 yang_print_must(out, level, node->module, &inout->must[i]);
1202 }
Radek Krejcie534c132016-11-23 13:32:31 +01001203 if (node->ext_size) {
1204 yang_print_extension_instances(out, level, node->module, node->ext, node->ext_size);
1205 }
1206 for (i = 0; i < inout->tpdf_size; i++) {
1207 yang_print_typedef(out, level, node->module, &inout->tpdf[i]);
1208 }
Michal Vaskof4d3d742015-06-16 11:51:09 +02001209
Radek Krejci1d82ef62015-08-07 14:44:40 +02001210 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +01001211 /* augments */
1212 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +02001213 continue;
1214 }
Radek Krejci76b07902015-10-09 09:11:25 +02001215 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +02001216 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
Radek Krejcibf2abff2016-08-23 15:51:52 +02001217 LYS_USES | LYS_GROUPING | LYS_ANYDATA);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001218 }
1219
1220 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001221 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001222}
1223
1224static void
Michal Vaskoca7cbc42016-07-01 11:36:53 +02001225yang_print_rpc_action(struct lyout *out, int level, const struct lys_node *node)
Michal Vaskof4d3d742015-06-16 11:51:09 +02001226{
Radek Krejci32cce7c2015-12-09 16:44:13 +01001227 int i, flag = 0;
Radek Krejci76512572015-08-04 09:47:08 +02001228 struct lys_node *sub;
Michal Vasko44fb6382016-06-29 11:12:27 +02001229 struct lys_node_rpc_action *rpc = (struct lys_node_rpc_action *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +02001230
Michal Vaskoca7cbc42016-07-01 11:36:53 +02001231 ly_print(out, "%*s%s %s", LEVEL, INDENT, (node->nodetype == LYS_RPC ? "rpc" : "action"), node->name);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001232
1233 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001234 yang_print_snode_common(out, level, node, &flag);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001235
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001236 for (i = 0; i < rpc->iffeature_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001237 yang_print_open(out, &flag);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001238 yang_print_iffeature(out, level, node->module, &rpc->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +02001239 }
1240
Michal Vaskof4d3d742015-06-16 11:51:09 +02001241 for (i = 0; i < rpc->tpdf_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001242 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001243 yang_print_typedef(out, level, node->module, &rpc->tpdf[i]);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001244 }
1245
Radek Krejci1d82ef62015-08-07 14:44:40 +02001246 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +01001247 /* augments */
1248 if (sub->parent != node) {
Radek Krejcic071c542016-01-27 14:57:51 +01001249 continue;
1250 }
Radek Krejci32cce7c2015-12-09 16:44:13 +01001251 yang_print_open(out, &flag);
1252 yang_print_snode(out, level, sub, LYS_GROUPING | LYS_INPUT | LYS_OUTPUT);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001253 }
1254
1255 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001256 yang_print_close(out, level, flag);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001257}
1258
1259static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001260yang_print_notif(struct lyout *out, int level, const struct lys_node *node)
Michal Vasko7690bc12015-06-16 12:26:05 +02001261{
Radek Krejci32cce7c2015-12-09 16:44:13 +01001262 int i, flag = 0;
Radek Krejci76512572015-08-04 09:47:08 +02001263 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001264 struct lys_node_notif *notif = (struct lys_node_notif *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +02001265
Radek Krejci32cce7c2015-12-09 16:44:13 +01001266 ly_print(out, "%*snotification %s", LEVEL, INDENT, node->name);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001267
1268 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001269 yang_print_snode_common(out, level, node, &flag);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001270
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001271 for (i = 0; i < notif->iffeature_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001272 yang_print_open(out, &flag);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001273 yang_print_iffeature(out, level, node->module, &notif->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +02001274 }
1275
Michal Vaskof4d3d742015-06-16 11:51:09 +02001276 for (i = 0; i < notif->tpdf_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001277 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001278 yang_print_typedef(out, level, node->module, &notif->tpdf[i]);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001279 }
1280
Radek Krejci12032a52016-07-29 15:42:56 +02001281 for (i = 0; i < notif->must_size; i++) {
1282 yang_print_open(out, &flag);
1283 yang_print_must(out, level, node->module, &notif->must[i]);
1284 }
1285
Radek Krejci1d82ef62015-08-07 14:44:40 +02001286 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +01001287 /* augments */
1288 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +02001289 continue;
1290 }
Radek Krejci32cce7c2015-12-09 16:44:13 +01001291 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001292 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +02001293 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
Radek Krejcibf2abff2016-08-23 15:51:52 +02001294 LYS_USES | LYS_GROUPING | LYS_ANYDATA);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001295 }
1296
1297 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001298 yang_print_close(out, level, flag);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001299}
1300
1301static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001302yang_print_snode(struct lyout *out, int level, const struct lys_node *node, int mask)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001303{
Radek Krejci1d82ef62015-08-07 14:44:40 +02001304 switch (node->nodetype & mask) {
Radek Krejci76512572015-08-04 09:47:08 +02001305 case LYS_CONTAINER:
Radek Krejci76b07902015-10-09 09:11:25 +02001306 yang_print_container(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001307 break;
Radek Krejci76512572015-08-04 09:47:08 +02001308 case LYS_CHOICE:
Radek Krejci76b07902015-10-09 09:11:25 +02001309 yang_print_choice(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001310 break;
Radek Krejci76512572015-08-04 09:47:08 +02001311 case LYS_LEAF:
Radek Krejci76b07902015-10-09 09:11:25 +02001312 yang_print_leaf(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001313 break;
Radek Krejci76512572015-08-04 09:47:08 +02001314 case LYS_LEAFLIST:
Radek Krejci76b07902015-10-09 09:11:25 +02001315 yang_print_leaflist(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001316 break;
Radek Krejci76512572015-08-04 09:47:08 +02001317 case LYS_LIST:
Radek Krejci76b07902015-10-09 09:11:25 +02001318 yang_print_list(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001319 break;
Radek Krejci76512572015-08-04 09:47:08 +02001320 case LYS_USES:
Radek Krejci76b07902015-10-09 09:11:25 +02001321 yang_print_uses(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001322 break;
Radek Krejci76512572015-08-04 09:47:08 +02001323 case LYS_GROUPING:
Radek Krejci76b07902015-10-09 09:11:25 +02001324 yang_print_grouping(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001325 break;
Radek Krejci76512572015-08-04 09:47:08 +02001326 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02001327 case LYS_ANYDATA:
1328 yang_print_anydata(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001329 break;
Radek Krejci76512572015-08-04 09:47:08 +02001330 case LYS_CASE:
Radek Krejci76b07902015-10-09 09:11:25 +02001331 yang_print_case(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001332 break;
Michal Vaskoca7cbc42016-07-01 11:36:53 +02001333 case LYS_ACTION:
1334 yang_print_rpc_action(out, level, node);
1335 break;
Radek Krejci76512572015-08-04 09:47:08 +02001336 case LYS_INPUT:
1337 case LYS_OUTPUT:
Radek Krejci76b07902015-10-09 09:11:25 +02001338 yang_print_input_output(out, level, node);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001339 break;
Michal Vaskob15cae22016-09-15 09:40:56 +02001340 case LYS_NOTIF:
1341 yang_print_notif(out, level, node);
1342 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001343 default:
1344 break;
1345 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001346}
1347
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001348int
Michal Vasko1e62a092015-12-01 12:27:20 +01001349yang_print_model(struct lyout *out, const struct lys_module *module)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001350{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001351 unsigned int i;
1352 int level = 0;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001353#define LEVEL (level*2)
1354
Radek Krejci1d82ef62015-08-07 14:44:40 +02001355 struct lys_node *node;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001356
Radek Krejci8d81e002015-12-10 11:18:59 +01001357 /* (sub)module-header-stmts */
Michal Vasko116172e2015-07-07 11:54:37 +02001358 if (module->type) {
Michal Vasko89563fc2016-07-28 16:19:35 +02001359 ly_print(out, "submodule %s {%s\n", module->name, (module->deviated == 1 ? " // DEVIATED" : ""));
Michal Vasko116172e2015-07-07 11:54:37 +02001360 level++;
Radek Krejci8d81e002015-12-10 11:18:59 +01001361 if (module->version) {
Radek Krejcic071c542016-01-27 14:57:51 +01001362 ly_print(out, "%*syang-version %s;\n", LEVEL, INDENT,
1363 ((struct lys_submodule *)module)->belongsto->version == 2 ? "1.1" : "1");
Radek Krejci8d81e002015-12-10 11:18:59 +01001364 }
Radek Krejci76b07902015-10-09 09:11:25 +02001365 ly_print(out, "%*sbelongs-to %s {\n", LEVEL, INDENT, ((struct lys_submodule *)module)->belongsto->name);
Michal Vasko116172e2015-07-07 11:54:37 +02001366 level++;
Radek Krejci8d81e002015-12-10 11:18:59 +01001367 ly_print(out, "%*sprefix %s;\n", LEVEL, INDENT, module->prefix);
Michal Vasko116172e2015-07-07 11:54:37 +02001368 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001369 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko116172e2015-07-07 11:54:37 +02001370 } else {
Michal Vasko89563fc2016-07-28 16:19:35 +02001371 ly_print(out, "module %s {%s\n", module->name, (module->deviated == 1 ? " // DEVIATED" : ""));
Michal Vasko116172e2015-07-07 11:54:37 +02001372 level++;
Radek Krejci8d81e002015-12-10 11:18:59 +01001373 if (module->version) {
Radek Krejcic071c542016-01-27 14:57:51 +01001374 ly_print(out, "%*syang-version %s;\n", LEVEL, INDENT, module->version == 2 ? "1.1" : "1");
Radek Krejci8d81e002015-12-10 11:18:59 +01001375 }
Radek Krejci76b07902015-10-09 09:11:25 +02001376 ly_print(out, "%*snamespace \"%s\";\n", LEVEL, INDENT, module->ns);
Radek Krejci8d81e002015-12-10 11:18:59 +01001377 ly_print(out, "%*sprefix %s;\n", LEVEL, INDENT, module->prefix);
Michal Vasko116172e2015-07-07 11:54:37 +02001378 }
Radek Krejcib0594bf2015-05-21 23:51:27 +02001379
Radek Krejci8d81e002015-12-10 11:18:59 +01001380 /* linkage-stmts */
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001381 for (i = 0; i < module->imp_size; i++) {
Radek Krejcie534c132016-11-23 13:32:31 +01001382 ly_print(out, "\n%*simport %s {\n", LEVEL, INDENT, module->imp[i].module->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001383 level++;
Radek Krejci8d81e002015-12-10 11:18:59 +01001384 ly_print(out, "%*sprefix %s;\n", LEVEL, INDENT, module->imp[i].prefix);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001385 if (module->imp[i].rev[0]) {
Michal Vasko86dfd262016-02-15 14:26:31 +01001386 ly_print(out, "%*srevision-date %s;\n", LEVEL, INDENT, module->imp[i].rev);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001387 }
Michal Vasko8bfe3812016-07-27 13:37:52 +02001388 if (module->imp[i].dsc) {
1389 yang_print_text(out, level, "description", module->imp[i].dsc, 0);
1390 }
1391 if (module->imp[i].ref) {
1392 yang_print_text(out, level, "reference", module->imp[i].ref, 0);
1393 }
Radek Krejcie534c132016-11-23 13:32:31 +01001394 if (module->imp[i].ext_size) {
1395 yang_print_extension_instances(out, level, module, module->imp[i].ext, module->imp[i].ext_size);
1396 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001397 level--;
Michal Vaskoc8e3ce02016-02-12 14:28:35 +01001398 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001399 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001400 for (i = 0; i < module->inc_size; i++) {
Radek Krejcic071c542016-01-27 14:57:51 +01001401 if (module->inc[i].external) {
1402 continue;
1403 }
Radek Krejcie534c132016-11-23 13:32:31 +01001404 if (module->inc[i].rev[0] || module->inc[i].dsc || module->inc[i].ref || module->inc[i].ext_size) {
1405 ly_print(out, "\n%*sinclude %s {\n", LEVEL, INDENT, module->inc[i].submodule->name);
Radek Krejci8d81e002015-12-10 11:18:59 +01001406 level++;
Radek Krejcie534c132016-11-23 13:32:31 +01001407 if (module->inc[i].rev[0]) {
1408 ly_print(out, "%*srevision-date %s;\n", LEVEL, INDENT, module->inc[i].rev);
1409 }
Michal Vasko8bfe3812016-07-27 13:37:52 +02001410 if (module->inc[i].dsc) {
1411 yang_print_text(out, level, "description", module->inc[i].dsc, 0);
1412 }
1413 if (module->inc[i].ref) {
1414 yang_print_text(out, level, "reference", module->inc[i].ref, 0);
1415 }
Radek Krejcie534c132016-11-23 13:32:31 +01001416 if (module->inc[i].ext_size) {
1417 yang_print_extension_instances(out, level, module, module->inc[i].ext, module->inc[i].ext_size);
1418 }
Radek Krejci8d81e002015-12-10 11:18:59 +01001419 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001420 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001421 } else {
Radek Krejcie534c132016-11-23 13:32:31 +01001422 ly_print(out, "\n%*sinclude \"%s\";\n", LEVEL, INDENT, module->inc[i].submodule->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001423 }
1424 }
Radek Krejciefaeba32015-05-27 14:30:57 +02001425
Radek Krejci8d81e002015-12-10 11:18:59 +01001426 /* meta-stmts */
1427 if (module->org || module->contact || module->dsc || module->ref) {
1428 ly_print(out, "\n");
1429 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001430 if (module->org) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001431 yang_print_text(out, level, "organization", module->org, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001432 }
1433 if (module->contact) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001434 yang_print_text(out, level, "contact", module->contact, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001435 }
1436 if (module->dsc) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001437 yang_print_text(out, level, "description", module->dsc, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001438 }
1439 if (module->ref) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001440 yang_print_text(out, level, "reference", module->ref, 0);
1441 }
1442
1443 /* revision-stmts */
1444 if (module->rev_size) {
1445 ly_print(out, "\n");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001446 }
1447 for (i = 0; i < module->rev_size; i++) {
1448 if (module->rev[i].dsc || module->rev[i].ref) {
Radek Krejci76b07902015-10-09 09:11:25 +02001449 ly_print(out, "%*srevision \"%s\" {\n", LEVEL, INDENT, module->rev[i].date);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001450 level++;
1451 if (module->rev[i].dsc) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001452 yang_print_text(out, level, "description", module->rev[i].dsc, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001453 }
1454 if (module->rev[i].ref) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001455 yang_print_text(out, level, "reference", module->rev[i].ref, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001456 }
1457 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001458 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001459 } else {
Michal Vasko86dfd262016-02-15 14:26:31 +01001460 ly_print(out, "%*srevision %s;\n", LEVEL, INDENT, module->rev[i].date);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001461 }
1462 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001463
Radek Krejci8d81e002015-12-10 11:18:59 +01001464 /* body-stmts */
Radek Krejcie534c132016-11-23 13:32:31 +01001465 for (i = 0; i < module->extensions_size; i++) {
1466 ly_print(out, "\n");
1467 yang_print_extension(out, level, &module->extensions[i]);
1468 }
1469
Michal Vasko30f6e912015-07-07 12:24:27 +02001470 for (i = 0; i < module->features_size; i++) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001471 ly_print(out, "\n");
Radek Krejci76b07902015-10-09 09:11:25 +02001472 yang_print_feature(out, level, &module->features[i]);
Michal Vasko30f6e912015-07-07 12:24:27 +02001473 }
1474
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001475 for (i = 0; i < module->ident_size; i++) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001476 ly_print(out, "\n");
Radek Krejci76b07902015-10-09 09:11:25 +02001477 yang_print_identity(out, level, &module->ident[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001478 }
Radek Krejci6793db02015-05-22 17:49:54 +02001479
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001480 for (i = 0; i < module->tpdf_size; i++) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001481 ly_print(out, "\n");
Radek Krejci76b07902015-10-09 09:11:25 +02001482 yang_print_typedef(out, level, module, &module->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001483 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001484
Radek Krejcie534c132016-11-23 13:32:31 +01001485 if (module->ext_size) {
Radek Krejcia1a6b762016-11-14 09:53:38 +09001486 ly_print(out, "\n");
Radek Krejcie534c132016-11-23 13:32:31 +01001487 yang_print_extension_instances(out, level, module, module->ext, module->ext_size);
Radek Krejcia1a6b762016-11-14 09:53:38 +09001488 }
1489
Radek Krejci8d81e002015-12-10 11:18:59 +01001490 for (i = 0; i < module->deviation_size; ++i) {
1491 ly_print(out, "\n");
1492 yang_print_deviation(out, level, module, &module->deviation[i]);
1493 }
1494
Radek Krejcic4283442016-04-22 09:19:27 +02001495 LY_TREE_FOR(lys_main_module(module)->data, node) {
Radek Krejcic071c542016-01-27 14:57:51 +01001496 if (node->module != module) {
Michal Vasko0c5e9282016-02-15 13:11:57 +01001497 /* data from submodules */
Radek Krejcic071c542016-01-27 14:57:51 +01001498 continue;
1499 }
Radek Krejci8d81e002015-12-10 11:18:59 +01001500 ly_print(out, "\n");
Radek Krejci92720552015-10-05 15:28:27 +02001501 switch(node->nodetype) {
1502 case LYS_RPC:
Michal Vaskoca7cbc42016-07-01 11:36:53 +02001503 yang_print_rpc_action(out, level, node);
Radek Krejci92720552015-10-05 15:28:27 +02001504 break;
1505 case LYS_NOTIF:
Radek Krejcibac81762015-10-09 10:19:52 +02001506 yang_print_notif(out, level, node);
Radek Krejci92720552015-10-05 15:28:27 +02001507 break;
1508 default:
Radek Krejcibac81762015-10-09 10:19:52 +02001509 yang_print_snode(out, level, node,
Radek Krejci92720552015-10-05 15:28:27 +02001510 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
Radek Krejcibf2abff2016-08-23 15:51:52 +02001511 LYS_USES | LYS_GROUPING | LYS_ANYDATA);
Radek Krejci92720552015-10-05 15:28:27 +02001512 break;
1513 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001514 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001515
Michal Vasko6f25f212015-07-07 15:42:07 +02001516 for (i = 0; i < module->augment_size; i++) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001517 ly_print(out, "\n");
Radek Krejci76b07902015-10-09 09:11:25 +02001518 yang_print_augment(out, level, module, &module->augment[i]);
Michal Vasko6f25f212015-07-07 15:42:07 +02001519 }
1520
Radek Krejci76b07902015-10-09 09:11:25 +02001521 ly_print(out, "}\n");
Michal Vasko95068c42016-03-24 14:58:11 +01001522 ly_print_flush(out);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001523
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001524 return EXIT_SUCCESS;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001525#undef LEVEL
1526}