blob: b341071645e38de0d68690f9b0a0fa249279996a [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 Krejci1d82ef62015-08-07 14:44:40 +0200226 if (node->flags & LYS_MAND_TRUE) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100227 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100228 ly_print(out, "%*smandatory true;\n", LEVEL, INDENT);
Radek Krejci1d82ef62015-08-07 14:44:40 +0200229 } else if (node->flags & LYS_MAND_FALSE) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100230 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100231 ly_print(out, "%*smandatory false;\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200232 }
Michal Vasko2b8faed2015-06-09 12:51:20 +0200233
Radek Krejci32cce7c2015-12-09 16:44:13 +0100234 yang_print_snode_common(out, level, node, flag);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200235}
236
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200237static void
Radek Krejci9ff0a922016-07-14 13:08:05 +0200238yang_print_iffeature(struct lyout *out, int level, const struct lys_module *module, struct lys_iffeature *iffeature)
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200239{
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200240 ly_print(out, "%*sif-feature \"", LEVEL, INDENT);
Radek Krejci9ff0a922016-07-14 13:08:05 +0200241 ly_print_iffeature(out, module, iffeature);
242 ly_print(out, "\";\n");
Michal Vaskoc39c4b12015-07-07 14:55:33 +0200243}
244
245static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100246yang_print_feature(struct lyout *out, int level, const struct lys_feature *feat)
Michal Vasko4773b762015-07-07 12:15:10 +0200247{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100248 int i, flag = 0;
Michal Vasko30f6e912015-07-07 12:24:27 +0200249
Radek Krejci32cce7c2015-12-09 16:44:13 +0100250 ly_print(out, "%*sfeature %s", LEVEL, INDENT, feat->name);
Michal Vasko4773b762015-07-07 12:15:10 +0200251 level++;
252
Radek Krejci32cce7c2015-12-09 16:44:13 +0100253 yang_print_snode_common(out, level, (struct lys_node *)feat, &flag);
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200254 for (i = 0; i < feat->iffeature_size; ++i) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100255 yang_print_open(out, &flag);
Radek Krejci9ff0a922016-07-14 13:08:05 +0200256 yang_print_iffeature(out, level, feat->module, &feat->iffeature[i]);
Michal Vasko30f6e912015-07-07 12:24:27 +0200257 }
Michal Vasko4773b762015-07-07 12:15:10 +0200258
259 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100260 yang_print_close(out, level, flag);
Michal Vasko4773b762015-07-07 12:15:10 +0200261}
262
263static void
Radek Krejci32cce7c2015-12-09 16:44:13 +0100264yang_print_restr(struct lyout *out, int level, const struct lys_restr *restr, int *flag)
Radek Krejci41726f92015-06-19 13:11:05 +0200265{
Radek Krejci0bd5db42015-06-19 13:30:07 +0200266 if (restr->dsc != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100267 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100268 yang_print_text(out, level, "description", restr->dsc, 0);
Radek Krejci41726f92015-06-19 13:11:05 +0200269 }
Radek Krejci0bd5db42015-06-19 13:30:07 +0200270 if (restr->ref != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100271 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100272 yang_print_text(out, level, "reference", restr->ref, 0);
Radek Krejci41726f92015-06-19 13:11:05 +0200273 }
Radek Krejci0bd5db42015-06-19 13:30:07 +0200274 if (restr->eapptag != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100275 yang_print_open(out, flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200276 ly_print(out, "%*serror-app-tag \"%s\";\n", LEVEL, INDENT, restr->eapptag);
Radek Krejci41726f92015-06-19 13:11:05 +0200277 }
Radek Krejci0bd5db42015-06-19 13:30:07 +0200278 if (restr->emsg != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100279 yang_print_open(out, flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100280 yang_print_text(out, level, "error-message", restr->emsg, 0);
Radek Krejci41726f92015-06-19 13:11:05 +0200281 }
282}
283
284static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100285yang_print_when(struct lyout *out, int level, const struct lys_module *module, const struct lys_when *when)
Michal Vasko1f0428a2015-07-07 14:55:04 +0200286{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100287 int flag = 0;
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100288 const char *str;
Michal Vaskof9893382015-10-09 14:03:04 +0200289
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100290 str = transform_json2schema(module, when->cond);
291 if (!str) {
Michal Vaskof9893382015-10-09 14:03:04 +0200292 ly_print(out, "(!error!)");
293 return;
294 }
295
Michal Vasko25cb6c62016-02-12 14:36:21 +0100296 ly_print(out, "%*swhen \"", LEVEL, INDENT);
297 yang_encode(out, str, -1);
298 ly_print(out, "\"");
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100299 lydict_remove(module->ctx, str);
Michal Vaskof9893382015-10-09 14:03:04 +0200300
Michal Vasko1f0428a2015-07-07 14:55:04 +0200301 level++;
302 if (when->dsc) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100303 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100304 yang_print_text(out, level, "description", when->dsc, 0);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200305 }
306 if (when->ref) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100307 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100308 yang_print_text(out, level, "reference", when->ref, 0);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200309 }
310 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100311 yang_print_close(out, level, flag);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200312}
313
314static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100315yang_print_type(struct lyout *out, int level, const struct lys_module *module, const struct lys_type *type)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200316{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200317 int i;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100318 int flag = 0, flag2;
Michal Vasko0fb82c62015-10-20 13:41:53 +0200319 const char *str;
Radek Krejcic071c542016-01-27 14:57:51 +0100320 struct lys_module *mod;
Radek Krejci25d782a2015-05-22 15:03:23 +0200321
Michal Vasko0fb82c62015-10-20 13:41:53 +0200322 if (type->module_name) {
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100323 ly_print(out, "%*stype %s:%s", LEVEL, INDENT,
324 transform_module_name2import_prefix(module, type->module_name), type->der->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200325 } else {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100326 ly_print(out, "%*stype %s", LEVEL, INDENT, type->der->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200327 }
328 level++;
329 switch (type->base) {
330 case LY_TYPE_BINARY:
Michal Vasko4634cda2016-02-16 09:22:09 +0100331 if (type->info.binary.length) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100332 yang_print_open(out, &flag);
Michal Vasko25cb6c62016-02-12 14:36:21 +0100333 ly_print(out, "%*slength \"", LEVEL, INDENT);
334 yang_encode(out, type->info.binary.length->expr, -1);
335 ly_print(out, "\"");
Radek Krejci32cce7c2015-12-09 16:44:13 +0100336 flag2 = 0;
337 yang_print_restr(out, level + 1, type->info.binary.length, &flag2);
338 yang_print_close(out, level, flag2);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200339 }
340 break;
341 case LY_TYPE_BITS:
342 for (i = 0; i < type->info.bits.count; ++i) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100343 yang_print_open(out, &flag);
Michal Vasko3f053ef2016-02-12 14:27:13 +0100344 ly_print(out, "%*sbit %s", LEVEL, INDENT, type->info.bits.bit[i].name);
345 flag2 = 0;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200346 level++;
Michal Vasko3f053ef2016-02-12 14:27:13 +0100347 yang_print_snode_common(out, level, (struct lys_node *)&type->info.bits.bit[i], &flag2);
348 if (!(type->info.bits.bit[i].flags & LYS_AUTOASSIGNED)) {
349 yang_print_open(out, &flag2);
350 ly_print(out, "%*sposition %u;\n", LEVEL, INDENT, type->info.bits.bit[i].pos);
351 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200352 level--;
Michal Vasko3f053ef2016-02-12 14:27:13 +0100353 yang_print_close(out, level, flag2);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200354 }
355 break;
356 case LY_TYPE_DEC64:
Radek Krejcib51d5932016-09-08 14:02:52 +0200357 if (!type->der->type.der) {
Michal Vasko4634cda2016-02-16 09:22:09 +0100358 yang_print_open(out, &flag);
359 ly_print(out, "%*sfraction-digits %d;\n", LEVEL, INDENT, type->info.dec64.dig);
360 }
Michal Vaskoea505ee2015-07-07 14:01:00 +0200361 if (type->info.dec64.range != NULL) {
Michal Vasko4634cda2016-02-16 09:22:09 +0100362 yang_print_open(out, &flag);
Michal Vasko25cb6c62016-02-12 14:36:21 +0100363 ly_print(out, "%*srange \"", LEVEL, INDENT);
364 yang_encode(out, type->info.dec64.range->expr, -1);
365 ly_print(out, "\"");
Radek Krejci32cce7c2015-12-09 16:44:13 +0100366 flag2 = 0;
367 yang_print_restr(out, level + 1, type->info.dec64.range, &flag2);
368 yang_print_close(out, level, flag2);
Michal Vaskoea505ee2015-07-07 14:01:00 +0200369 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200370 break;
371 case LY_TYPE_ENUM:
372 for (i = 0; i < type->info.enums.count; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100373 yang_print_open(out, &flag);
Michal Vasko3f053ef2016-02-12 14:27:13 +0100374 ly_print(out, "%*senum \"%s\"", LEVEL, INDENT, type->info.enums.enm[i].name);
375 flag2 = 0;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200376 level++;
Michal Vasko3f053ef2016-02-12 14:27:13 +0100377 yang_print_snode_common(out, level, (struct lys_node *)&type->info.enums.enm[i], &flag2);
378 if (!(type->info.enums.enm[i].flags & LYS_AUTOASSIGNED)) {
379 yang_print_open(out, &flag2);
380 ly_print(out, "%*svalue %d;\n", LEVEL, INDENT, type->info.enums.enm[i].value);
381 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200382 level--;
Michal Vasko3f053ef2016-02-12 14:27:13 +0100383 yang_print_close(out, level, flag2);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200384 }
385 break;
386 case LY_TYPE_IDENT:
Michal Vaskof2d43962016-09-02 11:10:16 +0200387 if (type->info.ident.count) {
Michal Vasko4634cda2016-02-16 09:22:09 +0100388 yang_print_open(out, &flag);
Michal Vaskof2d43962016-09-02 11:10:16 +0200389 for (i = 0; i < type->info.ident.count; ++i) {
390 mod = lys_main_module(type->info.ident.ref[i]->module);
391 if (lys_main_module(module) == mod) {
392 ly_print(out, "%*sbase %s;\n", LEVEL, INDENT, type->info.ident.ref[i]->name);
393 } else {
394 ly_print(out, "%*sbase %s:%s;\n", LEVEL, INDENT, transform_module_name2import_prefix(module, mod->name),
395 type->info.ident.ref[i]->name);
396 }
Michal Vasko4634cda2016-02-16 09:22:09 +0100397 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200398 }
399 break;
400 case LY_TYPE_INST:
Radek Krejciaf351422015-06-19 14:49:38 +0200401 if (type->info.inst.req == 1) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100402 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100403 ly_print(out, "%*srequire-instance true;\n", LEVEL, INDENT);
Radek Krejciaf351422015-06-19 14:49:38 +0200404 } else if (type->info.inst.req == -1) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100405 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100406 ly_print(out, "%*srequire-instance false;\n", LEVEL, INDENT);
Radek Krejciaf351422015-06-19 14:49:38 +0200407 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200408 break;
409 case LY_TYPE_INT8:
410 case LY_TYPE_INT16:
411 case LY_TYPE_INT32:
412 case LY_TYPE_INT64:
413 case LY_TYPE_UINT8:
414 case LY_TYPE_UINT16:
415 case LY_TYPE_UINT32:
416 case LY_TYPE_UINT64:
Michal Vasko4634cda2016-02-16 09:22:09 +0100417 if (type->info.num.range) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100418 yang_print_open(out, &flag);
Michal Vasko25cb6c62016-02-12 14:36:21 +0100419 ly_print(out, "%*srange \"", LEVEL, INDENT);
420 yang_encode(out, type->info.num.range->expr, -1);
421 ly_print(out, "\"");
Radek Krejci32cce7c2015-12-09 16:44:13 +0100422 flag2 = 0;
423 yang_print_restr(out, level + 1, type->info.num.range, &flag2);
424 yang_print_close(out, level, flag2);
Radek Krejcif2860132015-06-20 12:37:20 +0200425 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200426 break;
427 case LY_TYPE_LEAFREF:
Radek Krejci0dbff6a2016-07-17 12:40:18 +0200428 if (ly_strequal(type->der->name, "leafref", 0)) {
Michal Vasko4634cda2016-02-16 09:22:09 +0100429 yang_print_open(out, &flag);
430 str = transform_json2schema(module, type->info.lref.path);
431 ly_print(out, "%*spath \"%s\";\n", LEVEL, INDENT, str);
432 lydict_remove(module->ctx, str);
433 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200434 break;
435 case LY_TYPE_STRING:
Radek Krejci5fbc9162015-06-19 14:11:11 +0200436 if (type->info.str.length) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100437 yang_print_open(out, &flag);
Michal Vasko25cb6c62016-02-12 14:36:21 +0100438 ly_print(out, "%*slength \"", LEVEL, INDENT);
439 yang_encode(out, type->info.str.length->expr, -1);
440 ly_print(out, "\"");
Radek Krejci32cce7c2015-12-09 16:44:13 +0100441 flag2 = 0;
442 yang_print_restr(out, level + 1, type->info.str.length, &flag2);
443 yang_print_close(out, level, flag2);
Radek Krejci061bd522015-06-19 13:45:16 +0200444 }
Radek Krejci5fbc9162015-06-19 14:11:11 +0200445 for (i = 0; i < type->info.str.pat_count; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100446 yang_print_open(out, &flag);
Michal Vasko25cb6c62016-02-12 14:36:21 +0100447 ly_print(out, "%*spattern \"", LEVEL, INDENT);
Radek Krejci0d23e7a2016-08-04 12:46:17 +0200448 yang_encode(out, &type->info.str.patterns[i].expr[1], -1);
Michal Vasko25cb6c62016-02-12 14:36:21 +0100449 ly_print(out, "\"");
Radek Krejci32cce7c2015-12-09 16:44:13 +0100450 flag2 = 0;
Pavol Vican075fdf72016-09-21 09:34:28 +0200451 if (type->info.str.patterns[i].expr[0] == 0x15) {
Radek Krejci0d23e7a2016-08-04 12:46:17 +0200452 yang_print_open(out, &flag2);
Pavol Vican075fdf72016-09-21 09:34:28 +0200453 yang_print_text(out, level + 1, "modifier", "invert-match", 1);
Radek Krejci0d23e7a2016-08-04 12:46:17 +0200454 }
Radek Krejci32cce7c2015-12-09 16:44:13 +0100455 yang_print_restr(out, level + 1, &type->info.str.patterns[i], &flag2);
456 yang_print_close(out, level, flag2);
Radek Krejci5fbc9162015-06-19 14:11:11 +0200457 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200458 break;
459 case LY_TYPE_UNION:
460 for (i = 0; i < type->info.uni.count; ++i) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100461 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200462 yang_print_type(out, level, module, &type->info.uni.types[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200463 }
464 break;
465 default:
466 /* other types do not have substatements */
467 break;
468 }
469 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100470 yang_print_close(out, level, flag);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200471}
472
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200473static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100474yang_print_must(struct lyout *out, int level, const struct lys_module *module, const struct lys_restr *must)
Michal Vasko7f976ee2015-06-09 13:55:41 +0200475{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100476 int flag = 0;
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100477 const char *str;
Michal Vaskof9893382015-10-09 14:03:04 +0200478
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100479 str = transform_json2schema(module, must->expr);
480 if (!str) {
Michal Vaskof9893382015-10-09 14:03:04 +0200481 ly_print(out, "(!error!)");
482 return;
483 }
484
Michal Vasko25cb6c62016-02-12 14:36:21 +0100485 ly_print(out, "%*smust \"", LEVEL, INDENT);
486 yang_encode(out, str, -1);
487 ly_print(out, "\"");
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100488 lydict_remove(module->ctx, str);
Michal Vaskof9893382015-10-09 14:03:04 +0200489
Radek Krejci32cce7c2015-12-09 16:44:13 +0100490 yang_print_restr(out, level + 1, must, &flag);
491 yang_print_close(out, level, flag);
Michal Vasko7f976ee2015-06-09 13:55:41 +0200492}
493
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200494static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100495yang_print_unique(struct lyout *out, int level, const struct lys_unique *uniq)
Michal Vasko1ef07972015-07-07 14:01:35 +0200496{
497 int i;
498
Radek Krejci76b07902015-10-09 09:11:25 +0200499 ly_print(out, "%*sunique \"", LEVEL, INDENT);
Radek Krejci581ce772015-11-10 17:22:40 +0100500 for (i = 0; i < uniq->expr_size; i++) {
501 ly_print(out, "%s%s", uniq->expr[i], i + 1 < uniq->expr_size ? " " : "");
Michal Vasko1ef07972015-07-07 14:01:35 +0200502 }
Radek Krejci76b07902015-10-09 09:11:25 +0200503 ly_print(out, "\";\n");
Michal Vasko1ef07972015-07-07 14:01:35 +0200504}
505
506static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100507yang_print_refine(struct lyout *out, int level, const struct lys_module *module, const struct lys_refine *refine)
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200508{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200509 int i;
Michal Vaskoa8b25952015-10-20 15:30:25 +0200510 const char *str;
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200511
Michal Vasko5d112852016-02-12 16:47:13 +0100512 str = transform_json2schema(module, refine->target_name);
Michal Vaskoa8b25952015-10-20 15:30:25 +0200513 ly_print(out, "%*srefine \"%s\" {\n", LEVEL, INDENT, str);
514 lydict_remove(module->ctx, str);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200515 level++;
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200516
Radek Krejci1574a8d2015-08-03 14:16:52 +0200517 if (refine->flags & LYS_CONFIG_W) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100518 ly_print(out, "%*sconfig true;\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200519 } else if (refine->flags & LYS_CONFIG_R) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100520 ly_print(out, "%*sconfig false;\n", LEVEL, INDENT);
Radek Krejci6a113852015-07-03 16:04:20 +0200521 }
522
Radek Krejci1574a8d2015-08-03 14:16:52 +0200523 if (refine->flags & LYS_MAND_TRUE) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100524 ly_print(out, "%*smandatory true;\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200525 } else if (refine->flags & LYS_MAND_FALSE) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100526 ly_print(out, "%*smandatory false;\n", LEVEL, INDENT);
Radek Krejci6a113852015-07-03 16:04:20 +0200527 }
528
Radek Krejci32cce7c2015-12-09 16:44:13 +0100529 yang_print_snode_common(out, level, (struct lys_node *)refine, NULL);
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200530
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200531 for (i = 0; i < refine->must_size; ++i) {
Michal Vaskof9893382015-10-09 14:03:04 +0200532 yang_print_must(out, level, module, &refine->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200533 }
Radek Krejcibf06c4f2016-07-29 15:49:56 +0200534 for (i = 0; i < refine->iffeature_size; i++) {
535 yang_print_iffeature(out, level, module, &refine->iffeature[i]);
536 }
Pavol Vican3e7c73a2016-08-17 10:24:11 +0200537 for (i = 0; i < refine->dflt_size; ++i) {
538 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, refine->dflt[i]);
539 }
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200540
Pavol Vican3e7c73a2016-08-17 10:24:11 +0200541 if (refine->target_type == LYS_CONTAINER) {
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200542 if (refine->mod.presence != NULL) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100543 yang_print_text(out, level, "presence", refine->mod.presence, 1);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200544 }
Radek Krejci76512572015-08-04 09:47:08 +0200545 } else if (refine->target_type & (LYS_LIST | LYS_LEAFLIST)) {
Radek Krejci0f04a6c2016-04-14 16:16:36 +0200546 if (refine->flags & LYS_RFN_MINSET) {
Radek Krejci76b07902015-10-09 09:11:25 +0200547 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, refine->mod.list.min);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200548 }
Radek Krejci0f04a6c2016-04-14 16:16:36 +0200549 if (refine->flags & LYS_RFN_MAXSET) {
Radek Krejci0d7b2472016-02-12 11:11:03 +0100550 if (refine->mod.list.max) {
551 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, refine->mod.list.max);
552 } else {
553 ly_print(out, "%*smax-elements \"unbounded\";\n", LEVEL, INDENT);
554 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200555 }
556 }
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200557
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200558 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200559 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko00b7cfe2015-06-09 13:56:38 +0200560}
561
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200562static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100563yang_print_deviation(struct lyout *out, int level, const struct lys_module *module,
564 const struct lys_deviation *deviation)
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200565{
Pavol Vican38321d02016-08-16 14:56:02 +0200566 int i, j, k;
Michal Vaskoa8b25952015-10-20 15:30:25 +0200567 const char *str;
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200568
Michal Vasko5d112852016-02-12 16:47:13 +0100569 str = transform_json2schema(module, deviation->target_name);
Michal Vaskoa8b25952015-10-20 15:30:25 +0200570 ly_print(out, "%*sdeviation \"%s\" {\n", LEVEL, INDENT, str);
571 lydict_remove(module->ctx, str);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200572 level++;
573
574 if (deviation->dsc) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100575 yang_print_text(out, level, "description", deviation->dsc, 0);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200576 }
577 if (deviation->ref) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100578 yang_print_text(out, level, "reference", deviation->ref, 0);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200579 }
580
581 for (i = 0; i < deviation->deviate_size; ++i) {
Radek Krejci76b07902015-10-09 09:11:25 +0200582 ly_print(out, "%*sdeviate ", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200583 if (deviation->deviate[i].mod == LY_DEVIATE_NO) {
Michal Vaskod875e882016-05-19 10:57:30 +0200584 ly_print(out, "not-supported;\n");
585 continue;
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200586 } else if (deviation->deviate[i].mod == LY_DEVIATE_ADD) {
Radek Krejci76b07902015-10-09 09:11:25 +0200587 ly_print(out, "add {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200588 } else if (deviation->deviate[i].mod == LY_DEVIATE_RPL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200589 ly_print(out, "replace {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200590 } else if (deviation->deviate[i].mod == LY_DEVIATE_DEL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200591 ly_print(out, "delete {\n");
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200592 }
593 level++;
594
Radek Krejci1574a8d2015-08-03 14:16:52 +0200595 if (deviation->deviate[i].flags & LYS_CONFIG_W) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100596 ly_print(out, "%*sconfig true;\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200597 } else if (deviation->deviate[i].flags & LYS_CONFIG_R) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100598 ly_print(out, "%*sconfig false;\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200599 }
600
Radek Krejci1574a8d2015-08-03 14:16:52 +0200601 if (deviation->deviate[i].flags & LYS_MAND_TRUE) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100602 ly_print(out, "%*smandatory true;\n", LEVEL, INDENT);
Radek Krejci1574a8d2015-08-03 14:16:52 +0200603 } else if (deviation->deviate[i].flags & LYS_MAND_FALSE) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100604 ly_print(out, "%*smandatory false;\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200605 }
606
Pavol Vican38321d02016-08-16 14:56:02 +0200607 for (k = 0; k < deviation->deviate[i].dflt_size; ++k) {
608 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, deviation->deviate[i].dflt[k]);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200609 }
610
Radek Krejci0d7b2472016-02-12 11:11:03 +0100611 if (deviation->deviate[i].min_set) {
Radek Krejci76b07902015-10-09 09:11:25 +0200612 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, deviation->deviate[i].min);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200613 }
Radek Krejci0d7b2472016-02-12 11:11:03 +0100614 if (deviation->deviate[i].max_set) {
615 if (deviation->deviate[i].max) {
616 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, deviation->deviate[i].max);
617 } else {
618 ly_print(out, "%*smax-elements \"unbounded\";\n", LEVEL, INDENT);
619 }
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200620 }
621
622 for (j = 0; j < deviation->deviate[i].must_size; ++j) {
Michal Vaskof9893382015-10-09 14:03:04 +0200623 yang_print_must(out, level, module, &deviation->deviate[i].must[j]);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200624 }
625
626 for (j = 0; j < deviation->deviate[i].unique_size; ++j) {
Radek Krejci76b07902015-10-09 09:11:25 +0200627 yang_print_unique(out, level, &deviation->deviate[i].unique[j]);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200628 }
629
630 if (deviation->deviate[i].type) {
Radek Krejci76b07902015-10-09 09:11:25 +0200631 yang_print_type(out, level, module, deviation->deviate[i].type);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200632 }
633
634 if (deviation->deviate[i].units) {
Michal Vaskod875e882016-05-19 10:57:30 +0200635 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, deviation->deviate[i].units);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200636 }
637
638 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200639 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200640 }
641
642 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200643 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskoe0af1e22015-07-07 14:02:02 +0200644}
645
646static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100647yang_print_augment(struct lyout *out, int level, const struct lys_module *module,
648 const struct lys_node_augment *augment)
Michal Vasko6f25f212015-07-07 15:42:07 +0200649{
650 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200651 struct lys_node *sub;
Michal Vasko488c19e2015-10-20 15:21:00 +0200652 const char *str;
Michal Vasko6f25f212015-07-07 15:42:07 +0200653
Michal Vasko5d112852016-02-12 16:47:13 +0100654 str = transform_json2schema(module, augment->target_name);
Michal Vasko488c19e2015-10-20 15:21:00 +0200655 ly_print(out, "%*saugment \"%s\" {\n", LEVEL, INDENT, str);
656 lydict_remove(module->ctx, str);
Michal Vasko6f25f212015-07-07 15:42:07 +0200657 level++;
658
Radek Krejci32cce7c2015-12-09 16:44:13 +0100659 yang_print_nacmext(out, level, (struct lys_node *)augment, module, NULL);
660 yang_print_snode_common(out, level, (struct lys_node *)augment, NULL);
Michal Vasko6f25f212015-07-07 15:42:07 +0200661
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200662 for (i = 0; i < augment->iffeature_size; i++) {
Radek Krejci9ff0a922016-07-14 13:08:05 +0200663 yang_print_iffeature(out, level, module, &augment->iffeature[i]);
Michal Vasko6f25f212015-07-07 15:42:07 +0200664 }
665
666 if (augment->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200667 yang_print_when(out, level, module, augment->when);
Michal Vasko6f25f212015-07-07 15:42:07 +0200668 }
669
670 LY_TREE_FOR(augment->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100671 /* only our augment */
672 if (sub->parent != (struct lys_node *)augment) {
673 continue;
674 }
Radek Krejci76b07902015-10-09 09:11:25 +0200675 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200676 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
Radek Krejcibf2abff2016-08-23 15:51:52 +0200677 LYS_USES | LYS_ANYDATA | LYS_CASE | LYS_ACTION);
Michal Vasko6f25f212015-07-07 15:42:07 +0200678 }
679
680 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200681 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko6f25f212015-07-07 15:42:07 +0200682}
683
684static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100685yang_print_typedef(struct lyout *out, int level, const struct lys_module *module, const struct lys_tpdf *tpdf)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200686{
Radek Krejci76b07902015-10-09 09:11:25 +0200687 ly_print(out, "%*stypedef %s {\n", LEVEL, INDENT, tpdf->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200688 level++;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200689
Radek Krejci32cce7c2015-12-09 16:44:13 +0100690 yang_print_snode_common(out, level, (struct lys_node *)tpdf, NULL);
Radek Krejci76b07902015-10-09 09:11:25 +0200691 yang_print_type(out, level, module, &tpdf->type);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200692 if (tpdf->units != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200693 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, tpdf->units);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200694 }
695 if (tpdf->dflt != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200696 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, tpdf->dflt);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200697 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200698
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200699 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200700 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200701}
702
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200703static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100704yang_print_identity(struct lyout *out, int level, const struct lys_ident *ident)
Radek Krejci6793db02015-05-22 17:49:54 +0200705{
Radek Krejci018f1f52016-08-03 16:01:20 +0200706 int flag = 0, i;
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100707 struct lys_module *mod;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100708
709 ly_print(out, "%*sidentity %s", LEVEL, INDENT, ident->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200710 level++;
Radek Krejci6793db02015-05-22 17:49:54 +0200711
Radek Krejci32cce7c2015-12-09 16:44:13 +0100712 yang_print_snode_common(out, level, (struct lys_node *)ident, &flag);
Radek Krejci018f1f52016-08-03 16:01:20 +0200713 for (i = 0; i < ident->base_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100714 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200715 ly_print(out, "%*sbase ", LEVEL, INDENT);
Radek Krejci018f1f52016-08-03 16:01:20 +0200716 mod = lys_main_module(ident->base[i]->module);
Radek Krejcic4283442016-04-22 09:19:27 +0200717 if (lys_main_module(ident->module) != mod) {
Michal Vasko1bb7a5a2016-02-05 14:28:02 +0100718 ly_print(out, "%s:", transform_module_name2import_prefix(ident->module, mod->name));
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200719 }
Radek Krejci018f1f52016-08-03 16:01:20 +0200720 ly_print(out, "%s;\n", ident->base[i]->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200721 }
Radek Krejci6793db02015-05-22 17:49:54 +0200722
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200723 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100724 yang_print_close(out, level, flag);
Radek Krejci6793db02015-05-22 17:49:54 +0200725}
726
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200727static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100728yang_print_container(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200729{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100730 int i, flag = 0;
Radek Krejci76512572015-08-04 09:47:08 +0200731 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200732 struct lys_node_container *cont = (struct lys_node_container *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200733
Radek Krejci32cce7c2015-12-09 16:44:13 +0100734 ly_print(out, "%*scontainer %s", LEVEL, INDENT, node->name);
Michal Vasko7f976ee2015-06-09 13:55:41 +0200735
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200736 level++;
Radek Krejci6a113852015-07-03 16:04:20 +0200737
Radek Krejci32cce7c2015-12-09 16:44:13 +0100738 yang_print_nacmext(out, level, node, node->module, &flag);
Radek Krejci6a113852015-07-03 16:04:20 +0200739
Radek Krejci8d81e002015-12-10 11:18:59 +0100740 if (cont->when) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100741 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100742 yang_print_when(out, level, node->module, cont->when);
743 }
744
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200745 for (i = 0; i < cont->iffeature_size; i++) {
Radek Krejci8d81e002015-12-10 11:18:59 +0100746 yang_print_open(out, &flag);
Radek Krejci9ff0a922016-07-14 13:08:05 +0200747 yang_print_iffeature(out, level, node->module, &cont->iffeature[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200748 }
Michal Vasko7f976ee2015-06-09 13:55:41 +0200749
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200750 for (i = 0; i < cont->must_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100751 yang_print_open(out, &flag);
Michal Vaskof9893382015-10-09 14:03:04 +0200752 yang_print_must(out, level, node->module, &cont->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200753 }
Michal Vasko7f976ee2015-06-09 13:55:41 +0200754
Radek Krejci8d81e002015-12-10 11:18:59 +0100755 if (cont->presence != NULL) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100756 yang_print_open(out, &flag);
Radek Krejci8d81e002015-12-10 11:18:59 +0100757 yang_print_text(out, level, "presence", cont->presence, 1);
Michal Vasko4773b762015-07-07 12:15:10 +0200758 }
759
Radek Krejci8d81e002015-12-10 11:18:59 +0100760 yang_print_snode_common2(out, level, node, &flag);
761
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200762 for (i = 0; i < cont->tpdf_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100763 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200764 yang_print_typedef(out, level, node->module, &cont->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200765 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200766
Radek Krejci1d82ef62015-08-07 14:44:40 +0200767 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100768 /* augments */
769 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200770 continue;
771 }
Radek Krejci32cce7c2015-12-09 16:44:13 +0100772 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +0200773 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200774 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
Michal Vaskob15cae22016-09-15 09:40:56 +0200775 LYS_USES | LYS_GROUPING | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200776 }
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200777
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200778 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100779 yang_print_close(out, level, flag);
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200780}
781
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200782static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100783yang_print_case(struct lyout *out, int level, const struct lys_node *node)
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200784{
Michal Vasko4773b762015-07-07 12:15:10 +0200785 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200786 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200787 struct lys_node_case *cas = (struct lys_node_case *)node;
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200788
Radek Krejci76b07902015-10-09 09:11:25 +0200789 ly_print(out, "%*scase %s {\n", LEVEL, INDENT, cas->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200790 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100791 yang_print_nacmext(out, level, node, node->module, NULL);
792 yang_print_snode_common2(out, level, node, NULL);
Michal Vaskoc1329bc2015-06-09 13:58:18 +0200793
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200794 for (i = 0; i < cas->iffeature_size; i++) {
Radek Krejci9ff0a922016-07-14 13:08:05 +0200795 yang_print_iffeature(out, level, node->module, &cas->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200796 }
797
Michal Vasko1f0428a2015-07-07 14:55:04 +0200798 if (cas->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200799 yang_print_when(out, level, node->module, cas->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200800 }
801
Radek Krejci1d82ef62015-08-07 14:44:40 +0200802 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100803 /* augments */
804 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200805 continue;
806 }
Radek Krejci76b07902015-10-09 09:11:25 +0200807 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +0200808 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
Radek Krejcibf2abff2016-08-23 15:51:52 +0200809 LYS_USES | LYS_ANYDATA);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200810 }
Radek Krejcida04f4a2015-05-21 12:54:09 +0200811
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200812 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200813 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200814}
815
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200816static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100817yang_print_choice(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200818{
Michal Vasko4773b762015-07-07 12:15:10 +0200819 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200820 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200821 struct lys_node_choice *choice = (struct lys_node_choice *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200822
Radek Krejci76b07902015-10-09 09:11:25 +0200823 ly_print(out, "%*schoice %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +0200824
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200825 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100826 yang_print_nacmext(out, level, node, node->module, NULL);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200827 if (choice->dflt != NULL) {
Radek Krejci76b07902015-10-09 09:11:25 +0200828 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, choice->dflt->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200829 }
Michal Vasko16083662015-06-09 14:00:45 +0200830
Radek Krejci32cce7c2015-12-09 16:44:13 +0100831 yang_print_snode_common2(out, level, node, NULL);
Michal Vasko4773b762015-07-07 12:15:10 +0200832
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200833 for (i = 0; i < choice->iffeature_size; i++) {
Radek Krejci9ff0a922016-07-14 13:08:05 +0200834 yang_print_iffeature(out, level, node->module, &choice->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200835 }
836
Michal Vasko1f0428a2015-07-07 14:55:04 +0200837 if (choice->when) {
Michal Vaskof9893382015-10-09 14:03:04 +0200838 yang_print_when(out, level, node->module, choice->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200839 }
840
Radek Krejci1d82ef62015-08-07 14:44:40 +0200841 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100842 /* augments */
843 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200844 continue;
845 }
Radek Krejci76b07902015-10-09 09:11:25 +0200846 yang_print_snode(out, level, sub,
Radek Krejcibf2abff2016-08-23 15:51:52 +0200847 LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_CASE);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200848 }
849 level--;
Radek Krejci76b07902015-10-09 09:11:25 +0200850 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200851}
852
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200853static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100854yang_print_leaf(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200855{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200856 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200857 struct lys_node_leaf *leaf = (struct lys_node_leaf *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200858
Radek Krejci76b07902015-10-09 09:11:25 +0200859 ly_print(out, "%*sleaf %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +0200860
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200861 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100862 yang_print_nacmext(out, level, node, node->module, NULL);
Radek Krejci8d81e002015-12-10 11:18:59 +0100863 if (leaf->when) {
864 yang_print_when(out, level, node->module, leaf->when);
865 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200866 for (i = 0; i < leaf->iffeature_size; i++) {
Radek Krejci9ff0a922016-07-14 13:08:05 +0200867 yang_print_iffeature(out, level, node->module, &leaf->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200868 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200869 for (i = 0; i < leaf->must_size; i++) {
Michal Vaskof9893382015-10-09 14:03:04 +0200870 yang_print_must(out, level, node->module, &leaf->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200871 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100872 yang_print_snode_common2(out, level, node, NULL);
Radek Krejci76b07902015-10-09 09:11:25 +0200873 yang_print_type(out, level, node->module, &leaf->type);
Michal Vaskod875e882016-05-19 10:57:30 +0200874 if (leaf->units) {
Radek Krejci76b07902015-10-09 09:11:25 +0200875 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, leaf->units);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200876 }
Michal Vaskod875e882016-05-19 10:57:30 +0200877 if (leaf->dflt) {
878 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, leaf->dflt);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200879 }
880 level--;
Michal Vasko16083662015-06-09 14:00:45 +0200881
Radek Krejci76b07902015-10-09 09:11:25 +0200882 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko16083662015-06-09 14:00:45 +0200883}
884
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200885static void
Radek Krejcibf2abff2016-08-23 15:51:52 +0200886yang_print_anydata(struct lyout *out, int level, const struct lys_node *node)
Michal Vasko16083662015-06-09 14:00:45 +0200887{
Radek Krejci32cce7c2015-12-09 16:44:13 +0100888 int i, flag = 0;
Radek Krejcibf2abff2016-08-23 15:51:52 +0200889 struct lys_node_anydata *any = (struct lys_node_anydata *)node;
Michal Vasko16083662015-06-09 14:00:45 +0200890
Radek Krejcibf2abff2016-08-23 15:51:52 +0200891 ly_print(out, "%*s%s %s", LEVEL, INDENT, any->nodetype == LYS_ANYXML ? "anyxml" : "anydata", any->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200892 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100893 yang_print_nacmext(out, level, node, node->module, &flag);
894 yang_print_snode_common2(out, level, node, &flag);
Radek Krejcibf2abff2016-08-23 15:51:52 +0200895 for (i = 0; i < any->iffeature_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100896 yang_print_open(out, &flag);
Radek Krejcibf2abff2016-08-23 15:51:52 +0200897 yang_print_iffeature(out, level, node->module, &any->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200898 }
Radek Krejcibf2abff2016-08-23 15:51:52 +0200899 for (i = 0; i < any->must_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100900 yang_print_open(out, &flag);
Radek Krejcibf2abff2016-08-23 15:51:52 +0200901 yang_print_must(out, level, node->module, &any->must[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200902 }
Radek Krejcibf2abff2016-08-23 15:51:52 +0200903 if (any->when) {
Radek Krejci32cce7c2015-12-09 16:44:13 +0100904 yang_print_open(out, &flag);
Radek Krejcibf2abff2016-08-23 15:51:52 +0200905 yang_print_when(out, level, node->module, any->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200906 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200907 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100908 yang_print_close(out, level, flag);
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_leaflist(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200913{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200914 int i;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200915 struct lys_node_leaflist *llist = (struct lys_node_leaflist *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200916
Radek Krejci76b07902015-10-09 09:11:25 +0200917 ly_print(out, "%*sleaf-list %s {\n", LEVEL, INDENT, node->name);
Michal Vasko16083662015-06-09 14:00:45 +0200918
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200919 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100920 yang_print_nacmext(out, level, node, node->module, NULL);
Radek Krejci8d81e002015-12-10 11:18:59 +0100921 if (llist->when) {
922 yang_print_when(out, level, llist->module, llist->when);
923 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200924 for (i = 0; i < llist->iffeature_size; i++) {
Radek Krejci9ff0a922016-07-14 13:08:05 +0200925 yang_print_iffeature(out, level, node->module, &llist->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200926 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100927 for (i = 0; i < llist->must_size; i++) {
928 yang_print_must(out, level, node->module, &llist->must[i]);
929 }
930 yang_print_snode_common2(out, level, node, NULL);
931 yang_print_type(out, level, node->module, &llist->type);
Pavol Vican38321d02016-08-16 14:56:02 +0200932 for (i = 0; i < llist->dflt_size; ++i) {
933 ly_print(out, "%*sdefault \"%s\";\n", LEVEL, INDENT, llist->dflt[i]);
934 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100935 if (llist->units != NULL) {
936 ly_print(out, "%*sunits \"%s\";\n", LEVEL, INDENT, llist->units);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200937 }
938 if (llist->min > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200939 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, llist->min);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200940 }
941 if (llist->max > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200942 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, llist->max);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200943 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100944 if (llist->flags & LYS_USERORDERED) {
945 ly_print(out, "%*sordered-by user;\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200946 }
947 level--;
Michal Vasko16083662015-06-09 14:00:45 +0200948
Radek Krejci76b07902015-10-09 09:11:25 +0200949 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +0200950}
951
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200952static void
Michal Vasko1e62a092015-12-01 12:27:20 +0100953yang_print_list(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +0200954{
Michal Vasko1ef07972015-07-07 14:01:35 +0200955 int i;
Radek Krejci76512572015-08-04 09:47:08 +0200956 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +0200957 struct lys_node_list *list = (struct lys_node_list *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +0200958
Radek Krejci76b07902015-10-09 09:11:25 +0200959 ly_print(out, "%*slist %s {\n", LEVEL, INDENT, node->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200960 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +0100961 yang_print_nacmext(out, level, node, node->module, NULL);
Radek Krejci8d81e002015-12-10 11:18:59 +0100962 if (list->when) {
963 yang_print_when(out, level, list->module, list->when);
964 }
Michal Vaskoc5c26b02016-06-29 11:10:29 +0200965 for (i = 0; i < list->iffeature_size; i++) {
Radek Krejci9ff0a922016-07-14 13:08:05 +0200966 yang_print_iffeature(out, level, node->module, &list->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +0200967 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100968 for (i = 0; i < list->must_size; i++) {
969 yang_print_must(out, level, list->module, &list->must[i]);
970 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200971 if (list->keys_size) {
Radek Krejci76b07902015-10-09 09:11:25 +0200972 ly_print(out, "%*skey \"", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200973 for (i = 0; i < list->keys_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200974 ly_print(out, "%s%s", list->keys[i]->name, i + 1 < list->keys_size ? " " : "");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200975 }
Radek Krejci76b07902015-10-09 09:11:25 +0200976 ly_print(out, "\";\n");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200977 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200978 for (i = 0; i < list->unique_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200979 yang_print_unique(out, level, &list->unique[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200980 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100981 yang_print_snode_common2(out, level, node, NULL);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200982 if (list->min > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200983 ly_print(out, "%*smin-elements %u;\n", LEVEL, INDENT, list->min);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200984 }
985 if (list->max > 0) {
Radek Krejci76b07902015-10-09 09:11:25 +0200986 ly_print(out, "%*smax-elements %u;\n", LEVEL, INDENT, list->max);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200987 }
Radek Krejci8d81e002015-12-10 11:18:59 +0100988 if (list->flags & LYS_USERORDERED) {
989 ly_print(out, "%*sordered-by user;\n", LEVEL, INDENT);
Michal Vasko1f0428a2015-07-07 14:55:04 +0200990 }
Michal Vaskodff818b2015-06-09 14:01:12 +0200991
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200992 for (i = 0; i < list->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +0200993 yang_print_typedef(out, level, list->module, &list->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +0200994 }
Radek Krejci1d82ef62015-08-07 14:44:40 +0200995 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +0100996 /* augments */
997 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +0200998 continue;
999 }
Radek Krejci76b07902015-10-09 09:11:25 +02001000 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +02001001 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
Michal Vaskob15cae22016-09-15 09:40:56 +02001002 LYS_USES | LYS_GROUPING | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001003 }
1004 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001005 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001006}
1007
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001008static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001009yang_print_grouping(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001010{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001011 int i;
Michal Vasko0c5e9282016-02-15 13:11:57 +01001012 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001013 struct lys_node_grp *grp = (struct lys_node_grp *)node;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001014
Radek Krejci76b07902015-10-09 09:11:25 +02001015 ly_print(out, "%*sgrouping %s {\n", LEVEL, INDENT, node->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001016 level++;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001017
Radek Krejci32cce7c2015-12-09 16:44:13 +01001018 yang_print_snode_common(out, level, node, NULL);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001019
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001020 for (i = 0; i < grp->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +02001021 yang_print_typedef(out, level, node->module, &grp->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001022 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001023
Michal Vasko0c5e9282016-02-15 13:11:57 +01001024 LY_TREE_FOR(node->child, sub) {
1025 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +02001026 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
Radek Krejcibf2abff2016-08-23 15:51:52 +02001027 LYS_USES | LYS_GROUPING | LYS_ANYDATA | LYS_ACTION);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001028 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001029
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001030 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001031 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001032}
1033
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001034static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001035yang_print_uses(struct lyout *out, int level, const struct lys_node *node)
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001036{
Radek Krejci32cce7c2015-12-09 16:44:13 +01001037 int i, flag = 0;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001038 struct lys_node_uses *uses = (struct lys_node_uses *)node;
Michal Vasko1bb7a5a2016-02-05 14:28:02 +01001039 struct lys_module *mod;
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001040
Radek Krejci76b07902015-10-09 09:11:25 +02001041 ly_print(out, "%*suses ", LEVEL, INDENT);
Michal Vasko1bb7a5a2016-02-05 14:28:02 +01001042 if (node->child) {
Michal Vasko6c629ac2016-02-15 14:08:23 +01001043 mod = lys_node_module(node->child);
Michal Vasko1dae8ec2016-02-15 14:49:01 +01001044 if (lys_node_module(node) != mod) {
Michal Vasko1bb7a5a2016-02-05 14:28:02 +01001045 ly_print(out, "%s:", transform_module_name2import_prefix(node->module, mod->name));
1046 }
Michal Vaskoc39c4b12015-07-07 14:55:33 +02001047 }
Radek Krejci32cce7c2015-12-09 16:44:13 +01001048 ly_print(out, "%s", uses->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001049 level++;
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001050
Radek Krejci32cce7c2015-12-09 16:44:13 +01001051 yang_print_nacmext(out, level, node, node->module, &flag);
1052 yang_print_snode_common(out, level, node, &flag);
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001053 for (i = 0; i < uses->iffeature_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001054 yang_print_open(out, &flag);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001055 yang_print_iffeature(out, level, node->module, &uses->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +02001056 }
Michal Vasko1f0428a2015-07-07 14:55:04 +02001057 if (uses->when) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001058 yang_print_open(out, &flag);
Michal Vaskof9893382015-10-09 14:03:04 +02001059 yang_print_when(out, level, node->module, uses->when);
Michal Vasko1f0428a2015-07-07 14:55:04 +02001060 }
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001061
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001062 for (i = 0; i < uses->refine_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001063 yang_print_open(out, &flag);
Michal Vaskof9893382015-10-09 14:03:04 +02001064 yang_print_refine(out, level, node->module, &uses->refine[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001065 }
Michal Vasko00b7cfe2015-06-09 13:56:38 +02001066
Michal Vasko6f25f212015-07-07 15:42:07 +02001067 for (i = 0; i < uses->augment_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001068 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001069 yang_print_augment(out, level, node->module, &uses->augment[i]);
Michal Vasko6f25f212015-07-07 15:42:07 +02001070 }
1071
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001072 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001073 yang_print_close(out, level, flag);
Radek Krejcic7c9a6c2015-05-25 16:35:06 +02001074}
1075
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001076static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001077yang_print_input_output(struct lyout *out, int level, const struct lys_node *node)
Michal Vasko5bbae102015-06-16 12:16:44 +02001078{
Michal Vaskof4d3d742015-06-16 11:51:09 +02001079 int i;
Radek Krejci76512572015-08-04 09:47:08 +02001080 struct lys_node *sub;
Michal Vasko44fb6382016-06-29 11:12:27 +02001081 struct lys_node_inout *inout = (struct lys_node_inout *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +02001082
Radek Krejci41a349b2016-10-24 19:21:59 +02001083 if (node->flags & LYS_IMPLICIT) {
1084 /* implicit input/output which is not a part of the schema */
1085 return;
1086 }
1087
Radek Krejci76b07902015-10-09 09:11:25 +02001088 ly_print(out, "%*s%s {\n", LEVEL, INDENT, (inout->nodetype == LYS_INPUT ? "input" : "output"));
Michal Vaskof4d3d742015-06-16 11:51:09 +02001089
1090 level++;
1091 for (i = 0; i < inout->tpdf_size; i++) {
Radek Krejci76b07902015-10-09 09:11:25 +02001092 yang_print_typedef(out, level, node->module, &inout->tpdf[i]);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001093 }
Radek Krejci12032a52016-07-29 15:42:56 +02001094 for (i = 0; i < inout->must_size; i++) {
1095 yang_print_must(out, level, node->module, &inout->must[i]);
1096 }
Michal Vaskof4d3d742015-06-16 11:51:09 +02001097
Radek Krejci1d82ef62015-08-07 14:44:40 +02001098 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +01001099 /* augments */
1100 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +02001101 continue;
1102 }
Radek Krejci76b07902015-10-09 09:11:25 +02001103 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +02001104 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
Radek Krejcibf2abff2016-08-23 15:51:52 +02001105 LYS_USES | LYS_GROUPING | LYS_ANYDATA);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001106 }
1107
1108 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001109 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001110}
1111
1112static void
Michal Vaskoca7cbc42016-07-01 11:36:53 +02001113yang_print_rpc_action(struct lyout *out, int level, const struct lys_node *node)
Michal Vaskof4d3d742015-06-16 11:51:09 +02001114{
Radek Krejci32cce7c2015-12-09 16:44:13 +01001115 int i, flag = 0;
Radek Krejci76512572015-08-04 09:47:08 +02001116 struct lys_node *sub;
Michal Vasko44fb6382016-06-29 11:12:27 +02001117 struct lys_node_rpc_action *rpc = (struct lys_node_rpc_action *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +02001118
Michal Vaskoca7cbc42016-07-01 11:36:53 +02001119 ly_print(out, "%*s%s %s", LEVEL, INDENT, (node->nodetype == LYS_RPC ? "rpc" : "action"), node->name);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001120
1121 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001122 yang_print_snode_common(out, level, node, &flag);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001123
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001124 for (i = 0; i < rpc->iffeature_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001125 yang_print_open(out, &flag);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001126 yang_print_iffeature(out, level, node->module, &rpc->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +02001127 }
1128
Michal Vaskof4d3d742015-06-16 11:51:09 +02001129 for (i = 0; i < rpc->tpdf_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001130 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001131 yang_print_typedef(out, level, node->module, &rpc->tpdf[i]);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001132 }
1133
Radek Krejci1d82ef62015-08-07 14:44:40 +02001134 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +01001135 /* augments */
1136 if (sub->parent != node) {
Radek Krejcic071c542016-01-27 14:57:51 +01001137 continue;
1138 }
Radek Krejci32cce7c2015-12-09 16:44:13 +01001139 yang_print_open(out, &flag);
1140 yang_print_snode(out, level, sub, LYS_GROUPING | LYS_INPUT | LYS_OUTPUT);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001141 }
1142
1143 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001144 yang_print_close(out, level, flag);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001145}
1146
1147static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001148yang_print_notif(struct lyout *out, int level, const struct lys_node *node)
Michal Vasko7690bc12015-06-16 12:26:05 +02001149{
Radek Krejci32cce7c2015-12-09 16:44:13 +01001150 int i, flag = 0;
Radek Krejci76512572015-08-04 09:47:08 +02001151 struct lys_node *sub;
Radek Krejci1d82ef62015-08-07 14:44:40 +02001152 struct lys_node_notif *notif = (struct lys_node_notif *)node;
Michal Vaskof4d3d742015-06-16 11:51:09 +02001153
Radek Krejci32cce7c2015-12-09 16:44:13 +01001154 ly_print(out, "%*snotification %s", LEVEL, INDENT, node->name);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001155
1156 level++;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001157 yang_print_snode_common(out, level, node, &flag);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001158
Michal Vaskoc5c26b02016-06-29 11:10:29 +02001159 for (i = 0; i < notif->iffeature_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001160 yang_print_open(out, &flag);
Radek Krejci9ff0a922016-07-14 13:08:05 +02001161 yang_print_iffeature(out, level, node->module, &notif->iffeature[i]);
Michal Vasko4773b762015-07-07 12:15:10 +02001162 }
1163
Michal Vaskof4d3d742015-06-16 11:51:09 +02001164 for (i = 0; i < notif->tpdf_size; i++) {
Radek Krejci32cce7c2015-12-09 16:44:13 +01001165 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001166 yang_print_typedef(out, level, node->module, &notif->tpdf[i]);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001167 }
1168
Radek Krejci12032a52016-07-29 15:42:56 +02001169 for (i = 0; i < notif->must_size; i++) {
1170 yang_print_open(out, &flag);
1171 yang_print_must(out, level, node->module, &notif->must[i]);
1172 }
1173
Radek Krejci1d82ef62015-08-07 14:44:40 +02001174 LY_TREE_FOR(node->child, sub) {
Michal Vasko0c5e9282016-02-15 13:11:57 +01001175 /* augments */
1176 if (sub->parent != node) {
Michal Vasko15b48702015-07-07 15:49:34 +02001177 continue;
1178 }
Radek Krejci32cce7c2015-12-09 16:44:13 +01001179 yang_print_open(out, &flag);
Radek Krejci76b07902015-10-09 09:11:25 +02001180 yang_print_snode(out, level, sub,
Radek Krejci76512572015-08-04 09:47:08 +02001181 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
Radek Krejcibf2abff2016-08-23 15:51:52 +02001182 LYS_USES | LYS_GROUPING | LYS_ANYDATA);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001183 }
1184
1185 level--;
Radek Krejci32cce7c2015-12-09 16:44:13 +01001186 yang_print_close(out, level, flag);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001187}
1188
1189static void
Michal Vasko1e62a092015-12-01 12:27:20 +01001190yang_print_snode(struct lyout *out, int level, const struct lys_node *node, int mask)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001191{
Radek Krejci1d82ef62015-08-07 14:44:40 +02001192 switch (node->nodetype & mask) {
Radek Krejci76512572015-08-04 09:47:08 +02001193 case LYS_CONTAINER:
Radek Krejci76b07902015-10-09 09:11:25 +02001194 yang_print_container(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001195 break;
Radek Krejci76512572015-08-04 09:47:08 +02001196 case LYS_CHOICE:
Radek Krejci76b07902015-10-09 09:11:25 +02001197 yang_print_choice(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001198 break;
Radek Krejci76512572015-08-04 09:47:08 +02001199 case LYS_LEAF:
Radek Krejci76b07902015-10-09 09:11:25 +02001200 yang_print_leaf(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001201 break;
Radek Krejci76512572015-08-04 09:47:08 +02001202 case LYS_LEAFLIST:
Radek Krejci76b07902015-10-09 09:11:25 +02001203 yang_print_leaflist(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001204 break;
Radek Krejci76512572015-08-04 09:47:08 +02001205 case LYS_LIST:
Radek Krejci76b07902015-10-09 09:11:25 +02001206 yang_print_list(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001207 break;
Radek Krejci76512572015-08-04 09:47:08 +02001208 case LYS_USES:
Radek Krejci76b07902015-10-09 09:11:25 +02001209 yang_print_uses(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001210 break;
Radek Krejci76512572015-08-04 09:47:08 +02001211 case LYS_GROUPING:
Radek Krejci76b07902015-10-09 09:11:25 +02001212 yang_print_grouping(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001213 break;
Radek Krejci76512572015-08-04 09:47:08 +02001214 case LYS_ANYXML:
Radek Krejcibf2abff2016-08-23 15:51:52 +02001215 case LYS_ANYDATA:
1216 yang_print_anydata(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001217 break;
Radek Krejci76512572015-08-04 09:47:08 +02001218 case LYS_CASE:
Radek Krejci76b07902015-10-09 09:11:25 +02001219 yang_print_case(out, level, node);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001220 break;
Michal Vaskoca7cbc42016-07-01 11:36:53 +02001221 case LYS_ACTION:
1222 yang_print_rpc_action(out, level, node);
1223 break;
Radek Krejci76512572015-08-04 09:47:08 +02001224 case LYS_INPUT:
1225 case LYS_OUTPUT:
Radek Krejci76b07902015-10-09 09:11:25 +02001226 yang_print_input_output(out, level, node);
Michal Vaskof4d3d742015-06-16 11:51:09 +02001227 break;
Michal Vaskob15cae22016-09-15 09:40:56 +02001228 case LYS_NOTIF:
1229 yang_print_notif(out, level, node);
1230 break;
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001231 default:
1232 break;
1233 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001234}
1235
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001236int
Michal Vasko1e62a092015-12-01 12:27:20 +01001237yang_print_model(struct lyout *out, const struct lys_module *module)
Radek Krejcida04f4a2015-05-21 12:54:09 +02001238{
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001239 unsigned int i;
1240 int level = 0;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001241#define LEVEL (level*2)
1242
Radek Krejci1d82ef62015-08-07 14:44:40 +02001243 struct lys_node *node;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001244
Radek Krejci8d81e002015-12-10 11:18:59 +01001245 /* (sub)module-header-stmts */
Michal Vasko116172e2015-07-07 11:54:37 +02001246 if (module->type) {
Michal Vasko89563fc2016-07-28 16:19:35 +02001247 ly_print(out, "submodule %s {%s\n", module->name, (module->deviated == 1 ? " // DEVIATED" : ""));
Michal Vasko116172e2015-07-07 11:54:37 +02001248 level++;
Radek Krejci8d81e002015-12-10 11:18:59 +01001249 if (module->version) {
Radek Krejcic071c542016-01-27 14:57:51 +01001250 ly_print(out, "%*syang-version %s;\n", LEVEL, INDENT,
1251 ((struct lys_submodule *)module)->belongsto->version == 2 ? "1.1" : "1");
Radek Krejci8d81e002015-12-10 11:18:59 +01001252 }
Radek Krejci76b07902015-10-09 09:11:25 +02001253 ly_print(out, "%*sbelongs-to %s {\n", LEVEL, INDENT, ((struct lys_submodule *)module)->belongsto->name);
Michal Vasko116172e2015-07-07 11:54:37 +02001254 level++;
Radek Krejci8d81e002015-12-10 11:18:59 +01001255 ly_print(out, "%*sprefix %s;\n", LEVEL, INDENT, module->prefix);
Michal Vasko116172e2015-07-07 11:54:37 +02001256 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001257 ly_print(out, "%*s}\n", LEVEL, INDENT);
Michal Vasko116172e2015-07-07 11:54:37 +02001258 } else {
Michal Vasko89563fc2016-07-28 16:19:35 +02001259 ly_print(out, "module %s {%s\n", module->name, (module->deviated == 1 ? " // DEVIATED" : ""));
Michal Vasko116172e2015-07-07 11:54:37 +02001260 level++;
Radek Krejci8d81e002015-12-10 11:18:59 +01001261 if (module->version) {
Radek Krejcic071c542016-01-27 14:57:51 +01001262 ly_print(out, "%*syang-version %s;\n", LEVEL, INDENT, module->version == 2 ? "1.1" : "1");
Radek Krejci8d81e002015-12-10 11:18:59 +01001263 }
Radek Krejci76b07902015-10-09 09:11:25 +02001264 ly_print(out, "%*snamespace \"%s\";\n", LEVEL, INDENT, module->ns);
Radek Krejci8d81e002015-12-10 11:18:59 +01001265 ly_print(out, "%*sprefix %s;\n", LEVEL, INDENT, module->prefix);
Michal Vasko116172e2015-07-07 11:54:37 +02001266 }
Radek Krejcib0594bf2015-05-21 23:51:27 +02001267
Radek Krejci8d81e002015-12-10 11:18:59 +01001268 /* linkage-stmts */
1269 if (module->imp_size || module->inc_size) {
1270 ly_print(out, "\n");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001271 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001272 for (i = 0; i < module->imp_size; i++) {
Michal Vaskoc8e3ce02016-02-12 14:28:35 +01001273 ly_print(out, "%*simport %s {\n", LEVEL, INDENT, module->imp[i].module->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001274 level++;
Radek Krejci8d81e002015-12-10 11:18:59 +01001275 ly_print(out, "%*sprefix %s;\n", LEVEL, INDENT, module->imp[i].prefix);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001276 if (module->imp[i].rev[0]) {
Michal Vasko86dfd262016-02-15 14:26:31 +01001277 ly_print(out, "%*srevision-date %s;\n", LEVEL, INDENT, module->imp[i].rev);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001278 }
Michal Vasko8bfe3812016-07-27 13:37:52 +02001279 if (module->imp[i].dsc) {
1280 yang_print_text(out, level, "description", module->imp[i].dsc, 0);
1281 }
1282 if (module->imp[i].ref) {
1283 yang_print_text(out, level, "reference", module->imp[i].ref, 0);
1284 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001285 level--;
Michal Vaskoc8e3ce02016-02-12 14:28:35 +01001286 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001287 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001288 for (i = 0; i < module->inc_size; i++) {
Radek Krejcic071c542016-01-27 14:57:51 +01001289 if (module->inc[i].external) {
1290 continue;
1291 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001292 if (module->inc[i].rev[0]) {
Radek Krejci76b07902015-10-09 09:11:25 +02001293 ly_print(out, "%*sinclude \"%s\" {\n", LEVEL, INDENT, module->inc[i].submodule->name);
Radek Krejci8d81e002015-12-10 11:18:59 +01001294 level++;
Michal Vasko3a4e2d02016-02-15 15:12:46 +01001295 ly_print(out, "%*srevision-date %s;\n", LEVEL, INDENT, module->inc[i].rev);
Michal Vasko8bfe3812016-07-27 13:37:52 +02001296 if (module->inc[i].dsc) {
1297 yang_print_text(out, level, "description", module->inc[i].dsc, 0);
1298 }
1299 if (module->inc[i].ref) {
1300 yang_print_text(out, level, "reference", module->inc[i].ref, 0);
1301 }
Radek Krejci8d81e002015-12-10 11:18:59 +01001302 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001303 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001304 } else {
Radek Krejci76b07902015-10-09 09:11:25 +02001305 ly_print(out, "%*sinclude \"%s\";\n", LEVEL, INDENT, module->inc[i].submodule->name);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001306 }
1307 }
Radek Krejciefaeba32015-05-27 14:30:57 +02001308
Radek Krejci8d81e002015-12-10 11:18:59 +01001309 /* meta-stmts */
1310 if (module->org || module->contact || module->dsc || module->ref) {
1311 ly_print(out, "\n");
1312 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001313 if (module->org) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001314 yang_print_text(out, level, "organization", module->org, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001315 }
1316 if (module->contact) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001317 yang_print_text(out, level, "contact", module->contact, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001318 }
1319 if (module->dsc) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001320 yang_print_text(out, level, "description", module->dsc, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001321 }
1322 if (module->ref) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001323 yang_print_text(out, level, "reference", module->ref, 0);
1324 }
1325
1326 /* revision-stmts */
1327 if (module->rev_size) {
1328 ly_print(out, "\n");
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001329 }
1330 for (i = 0; i < module->rev_size; i++) {
1331 if (module->rev[i].dsc || module->rev[i].ref) {
Radek Krejci76b07902015-10-09 09:11:25 +02001332 ly_print(out, "%*srevision \"%s\" {\n", LEVEL, INDENT, module->rev[i].date);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001333 level++;
1334 if (module->rev[i].dsc) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001335 yang_print_text(out, level, "description", module->rev[i].dsc, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001336 }
1337 if (module->rev[i].ref) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001338 yang_print_text(out, level, "reference", module->rev[i].ref, 0);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001339 }
1340 level--;
Radek Krejci76b07902015-10-09 09:11:25 +02001341 ly_print(out, "%*s}\n", LEVEL, INDENT);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001342 } else {
Michal Vasko86dfd262016-02-15 14:26:31 +01001343 ly_print(out, "%*srevision %s;\n", LEVEL, INDENT, module->rev[i].date);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001344 }
1345 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001346
Radek Krejci8d81e002015-12-10 11:18:59 +01001347 /* body-stmts */
Michal Vasko30f6e912015-07-07 12:24:27 +02001348 for (i = 0; i < module->features_size; i++) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001349 ly_print(out, "\n");
Radek Krejci76b07902015-10-09 09:11:25 +02001350 yang_print_feature(out, level, &module->features[i]);
Michal Vasko30f6e912015-07-07 12:24:27 +02001351 }
1352
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001353 for (i = 0; i < module->ident_size; i++) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001354 ly_print(out, "\n");
Radek Krejci76b07902015-10-09 09:11:25 +02001355 yang_print_identity(out, level, &module->ident[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001356 }
Radek Krejci6793db02015-05-22 17:49:54 +02001357
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001358 for (i = 0; i < module->tpdf_size; i++) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001359 ly_print(out, "\n");
Radek Krejci76b07902015-10-09 09:11:25 +02001360 yang_print_typedef(out, level, module, &module->tpdf[i]);
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001361 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001362
Radek Krejci8d81e002015-12-10 11:18:59 +01001363 for (i = 0; i < module->deviation_size; ++i) {
1364 ly_print(out, "\n");
1365 yang_print_deviation(out, level, module, &module->deviation[i]);
1366 }
1367
Radek Krejcic4283442016-04-22 09:19:27 +02001368 LY_TREE_FOR(lys_main_module(module)->data, node) {
Radek Krejcic071c542016-01-27 14:57:51 +01001369 if (node->module != module) {
Michal Vasko0c5e9282016-02-15 13:11:57 +01001370 /* data from submodules */
Radek Krejcic071c542016-01-27 14:57:51 +01001371 continue;
1372 }
Radek Krejci8d81e002015-12-10 11:18:59 +01001373 ly_print(out, "\n");
Radek Krejci92720552015-10-05 15:28:27 +02001374 switch(node->nodetype) {
1375 case LYS_RPC:
Michal Vaskoca7cbc42016-07-01 11:36:53 +02001376 yang_print_rpc_action(out, level, node);
Radek Krejci92720552015-10-05 15:28:27 +02001377 break;
1378 case LYS_NOTIF:
Radek Krejcibac81762015-10-09 10:19:52 +02001379 yang_print_notif(out, level, node);
Radek Krejci92720552015-10-05 15:28:27 +02001380 break;
1381 default:
Radek Krejcibac81762015-10-09 10:19:52 +02001382 yang_print_snode(out, level, node,
Radek Krejci92720552015-10-05 15:28:27 +02001383 LYS_CHOICE | LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST |
Radek Krejcibf2abff2016-08-23 15:51:52 +02001384 LYS_USES | LYS_GROUPING | LYS_ANYDATA);
Radek Krejci92720552015-10-05 15:28:27 +02001385 break;
1386 }
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001387 }
Radek Krejcida04f4a2015-05-21 12:54:09 +02001388
Michal Vasko6f25f212015-07-07 15:42:07 +02001389 for (i = 0; i < module->augment_size; i++) {
Radek Krejci8d81e002015-12-10 11:18:59 +01001390 ly_print(out, "\n");
Radek Krejci76b07902015-10-09 09:11:25 +02001391 yang_print_augment(out, level, module, &module->augment[i]);
Michal Vasko6f25f212015-07-07 15:42:07 +02001392 }
1393
Radek Krejci76b07902015-10-09 09:11:25 +02001394 ly_print(out, "}\n");
Michal Vasko95068c42016-03-24 14:58:11 +01001395 ly_print_flush(out);
Radek Krejcida04f4a2015-05-21 12:54:09 +02001396
Radek Krejci6e4ffbb2015-06-16 10:34:41 +02001397 return EXIT_SUCCESS;
Radek Krejcida04f4a2015-05-21 12:54:09 +02001398#undef LEVEL
1399}