1 /*************************************************************************
3 * Copyright 2020 highstreet technologies GmbH and others
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 ***************************************************************************/
20 #include "utils/log_utils.h"
21 #include "utils/type_utils.h"
27 #include <libyang/libyang.h>
28 #include "core/session.h"
29 #include "core/framework.h"
31 static int schema_print_recursive(struct lys_node *root);
33 int schema_get_xpaths(char ***root_xpath) {
37 const struct lys_module *module;
38 const struct lys_node *root;
43 while((module = ly_ctx_get_module_iter(session_context, &idx)) != 0) {
44 if(!framework_is_populate_excluded_module(module->name) && (module->implemented)) {
45 LY_TREE_FOR(module->data, root) {
46 if(((root->nodetype == LYS_CONTAINER) || (root->nodetype == LYS_LIST)) && ((root->flags & LYS_STATUS_DEPRC) == 0)) {
47 list = (char **)realloc(list, sizeof(char *) * (total + 1));
49 log_error("bad realloc");
50 return NTS_ERR_FAILED;
52 asprintf(&list[total], "/%s:%s", module->name, root->name);
54 log_error("bad asprintf");
55 return NTS_ERR_FAILED;
59 else if(root->nodetype == LYS_USES) {
61 LY_TREE_FOR(root->child, chd) {
62 if(((chd->nodetype == LYS_CONTAINER) || (chd->nodetype == LYS_LIST)) && ((chd->flags & LYS_STATUS_DEPRC) == 0)) {
63 list = (char **)realloc(list, sizeof(char *) * (total + 1));
65 log_error("bad realloc");
66 return NTS_ERR_FAILED;
68 asprintf(&list[total], "/%s:%s", module->name, chd->name);
70 log_error("bad asprintf");
71 return NTS_ERR_FAILED;
85 int schema_print_root_paths(void) {
88 struct lys_module *module;
89 struct lys_node *root;
92 while((module = (struct lys_module *)ly_ctx_get_module_iter(session_context, &idx)) != 0) {
93 log_message(2, "looking into module "LOG_COLOR_BOLD_MAGENTA"%s"LOG_COLOR_RESET"\n", module->name);
97 flags[1] = (module->implemented == 0) ? 'i' : ' ';
98 flags[3] = framework_is_populate_excluded_module(module->name) ? 'E' : ' ';
100 LY_TREE_FOR(module->data, root) {
101 log_message(2, " found "LOG_COLOR_CYAN"%s"LOG_COLOR_RESET" with name "LOG_COLOR_BOLD_YELLOW"%s"LOG_COLOR_RESET"\n", typeutils_yang_nodetype_to_str(root->nodetype), root->name);
102 if((root->nodetype == LYS_CONTAINER) || (root->nodetype == LYS_LIST)) {
103 flags[2] = ((root->flags & LYS_STATUS_DEPRC) != 0) ? 'D' : ' ';
104 flags[4] = ((root->flags & LYS_CONFIG_W) == 0) ? 'O' : 'R';
105 flags[5] = (root->nodetype == LYS_CONTAINER) ? 'C' : 'L';
106 log_message(1, LOG_COLOR_BOLD_YELLOW"%s"LOG_COLOR_CYAN" /%s:%s\n"LOG_COLOR_RESET, flags, module->name, root->name);
108 else if(root->nodetype == LYS_USES) {
109 struct lys_node *chd;
110 LY_TREE_FOR(root->child, chd) {
111 log_message(2, " - found "LOG_COLOR_CYAN"%s"LOG_COLOR_RESET" with name "LOG_COLOR_BOLD_YELLOW"%s"LOG_COLOR_RESET"\n", typeutils_yang_nodetype_to_str(chd->nodetype), chd->name);
112 if((chd->nodetype == LYS_CONTAINER) || (chd->nodetype == LYS_LIST)) {
113 flags[2] = ((chd->flags & LYS_STATUS_DEPRC) != 0) ? 'D' : ' ';
114 flags[4] = ((chd->flags & LYS_CONFIG_W) == 0) ? 'O' : 'R';
115 flags[5] = (chd->nodetype == LYS_CONTAINER) ? 'C' : 'L';
116 log_message(1, LOG_COLOR_BOLD_YELLOW"%s"LOG_COLOR_CYAN" /%s:%s\n"LOG_COLOR_RESET, flags, module->name, chd->name);
123 log_message(1, "\n "LOG_COLOR_BOLD_YELLOW"i"LOG_COLOR_RESET" - not implemented | "LOG_COLOR_BOLD_YELLOW"D"LOG_COLOR_RESET" - deprecated | "LOG_COLOR_BOLD_YELLOW"E"LOG_COLOR_RESET" - excluded by config | "LOG_COLOR_BOLD_YELLOW"O"LOG_COLOR_RESET" - operational datastore | "LOG_COLOR_BOLD_YELLOW"R"LOG_COLOR_RESET" - running datastore | "LOG_COLOR_BOLD_YELLOW"C"LOG_COLOR_RESET" - container | "LOG_COLOR_BOLD_YELLOW"L"LOG_COLOR_RESET" - list\n\n");
127 int schema_print_xpath(const char *xpath) {
132 log_error("xpath is null");
133 return NTS_ERR_FAILED;
135 log_message(1, "printing out data structure for xpath: "LOG_COLOR_BOLD_YELLOW"%s"LOG_COLOR_RESET"\n", xpath);
137 struct lys_node *elem = (struct lys_node *)ly_ctx_get_node(session_context, 0, xpath, 0);
139 log_error("ly_ctx_get_node failed for xpath: %s", xpath);
140 return NTS_ERR_FAILED;
143 struct lys_module *module = lys_node_module(elem);
145 log_error("lys_node_module failed for xpath: %s", xpath);
146 return NTS_ERR_FAILED;
150 log_message(2, "module is %s @ revision %s\n", module->name, module->rev[0].date);
152 int rc = schema_print_recursive(elem);
153 if(rc != NTS_ERR_OK) {
154 log_error("schema_print_recursive failed for xpath: %s", xpath);
155 return NTS_ERR_FAILED;
158 log_message(1, "\n "LOG_COLOR_BOLD_YELLOW"R"LOG_COLOR_RESET" - read only | "LOG_COLOR_BOLD_YELLOW"W"LOG_COLOR_RESET" - writeable | "LOG_COLOR_BOLD_YELLOW"*"LOG_COLOR_RESET" - key | "LOG_COLOR_BOLD_YELLOW"M"LOG_COLOR_RESET" - mandatory | "LOG_COLOR_BOLD_YELLOW"D"LOG_COLOR_RESET" - deprecated | "LOG_COLOR_BOLD_YELLOW"O"LOG_COLOR_RESET" - obsolete\n\n");
159 log_message(2, "schema_print() finished\n");
165 static int schema_print_recursive(struct lys_node *root) {
168 char my_status[] = "[ ]";
169 my_status[1] = ((root->flags & LYS_CONFIG_W) == 0) ? 'R' : 'W';
170 my_status[2] = ((root->flags & LYS_MAND_TRUE) != 0) ? 'M' : ' ';
171 my_status[3] = ((root->flags & LYS_STATUS_DEPRC) != 0) ? 'D' : ' ';
172 my_status[4] = ((root->flags & LYS_STATUS_OBSLT) != 0) ? 'O' : ' ';
174 if(((root->parent) && (root->parent->nodetype == LYS_CASE)) && ((root->parent->flags & LYS_MAND_TRUE) != 0)) {
178 char *path = lys_data_path(root);
179 if((root->nodetype != LYS_CHOICE) && (root->nodetype != LYS_CASE)) {
180 log_message(1, LOG_COLOR_BOLD_YELLOW"%s"LOG_COLOR_RESET" %-100s ", my_status, path);
182 if(root->nodetype == LYS_LIST) {
183 struct lys_node_list *list = (struct lys_node_list *)root;
184 log_message(1, LOG_COLOR_CYAN"%s "LOG_COLOR_MAGENTA"(m: %d M: %d)"LOG_COLOR_RESET, 4 + typeutils_yang_nodetype_to_str(root->nodetype), list->min, list->max); //+4 to skip "LYS_"
186 else if(root->nodetype == LYS_LEAFLIST) {
187 struct lys_node_leaflist *list = (struct lys_node_leaflist *)root;
188 log_message(1, LOG_COLOR_CYAN"%s "LOG_COLOR_MAGENTA"(m: %d M: %d)"LOG_COLOR_RESET, 4 + typeutils_yang_nodetype_to_str(root->nodetype), list->min, list->max); //+4 to skip "LYS_"
191 log_message(1, LOG_COLOR_CYAN"%-20s"LOG_COLOR_RESET, 4 + typeutils_yang_nodetype_to_str(root->nodetype)); //+4 to skip "LYS_"
196 switch(root->nodetype) {
199 struct lys_type *type = &((struct lys_node_leaf *)root)->type;
200 if(lys_is_key((const struct lys_node_leaf *)root, 0) != 0) {
201 log_message(1, LOG_COLOR_BOLD_YELLOW"[*]"LOG_COLOR_RESET);
207 char *typestr = typeutils_type_to_str(type);
208 log_message(1, "[%s]", typestr);
212 if(root->parent->nodetype == LYS_CASE) {
213 log_message(1, " is a "LOG_COLOR_BLUE"CASE"LOG_COLOR_RESET" of "LOG_COLOR_CYAN"%s"LOG_COLOR_RESET, root->parent->parent->name);
217 if(type->base == LY_TYPE_LEAFREF) {
218 log_message(1, " path: "LOG_COLOR_GREEN"%s"LOG_COLOR_RESET" ", type->info.lref.path);
220 else if(type->base == LY_TYPE_UNION) {
221 if((type->info.uni.count == 0) && (type->der != 0)) {
222 type = &type->der->type;
225 log_message(1, " available union types (%d):"LOG_COLOR_GREEN, type->info.uni.count);
226 for(int i = 0; i < type->info.uni.count; i++) {
227 char *typestr = typeutils_type_to_str(&type->info.uni.types[i]);
228 log_message(1, " %s", typestr);
231 log_message(1, LOG_COLOR_RESET);
239 if((root->nodetype != LYS_CHOICE) && (root->nodetype != LYS_CASE)) {
240 log_message(1, "\n");
243 struct lys_node *child = 0;
244 LY_TREE_FOR(root->child, child) {
245 int rc = schema_print_recursive(child);
246 if(rc != NTS_ERR_OK) {