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);
32 static bool generate_is_excluded_module(const char *module);
34 int datastore_schema_get_xpaths(char ***root_xpath) {
38 const struct lys_module *module;
39 const struct lys_node *root;
44 while((module = ly_ctx_get_module_iter(session_context, &idx)) != 0) {
45 if(!generate_is_excluded_module(module->name) && (module->implemented)) {
46 LY_TREE_FOR(module->data, root) {
47 if(((root->nodetype == LYS_CONTAINER) || (root->nodetype == LYS_LIST)) && ((root->flags & LYS_STATUS_DEPRC) == 0)) {
48 list = (char **)realloc(list, sizeof(char *) * (total + 1));
50 log_error("bad realloc\n");
51 return NTS_ERR_FAILED;
53 asprintf(&list[total], "/%s:%s", module->name, root->name);
55 log_error("bad asprintf\n");
56 return NTS_ERR_FAILED;
60 else if(root->nodetype == LYS_USES) {
62 LY_TREE_FOR(root->child, chd) {
63 if(((chd->nodetype == LYS_CONTAINER) || (chd->nodetype == LYS_LIST)) && ((chd->flags & LYS_STATUS_DEPRC) == 0)) {
64 list = (char **)realloc(list, sizeof(char *) * (total + 1));
66 log_error("bad realloc\n");
67 return NTS_ERR_FAILED;
69 asprintf(&list[total], "/%s:%s", module->name, chd->name);
71 log_error("bad asprintf\n");
72 return NTS_ERR_FAILED;
86 int datastore_schema_print_root_paths(void) {
89 struct lys_module *module;
90 struct lys_node *root;
93 while((module = (struct lys_module *)ly_ctx_get_module_iter(session_context, &idx)) != 0) {
94 log_add_verbose(2, "looking into module "LOG_COLOR_BOLD_MAGENTA"%s"LOG_COLOR_RESET"\n", module->name);
98 flags[1] = (module->implemented == 0) ? 'i' : ' ';
99 flags[3] = generate_is_excluded_module(module->name) ? 'E' : ' ';
101 LY_TREE_FOR(module->data, root) {
102 log_add_verbose(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);
103 if((root->nodetype == LYS_CONTAINER) || (root->nodetype == LYS_LIST)) {
104 flags[2] = ((root->flags & LYS_STATUS_DEPRC) != 0) ? 'D' : ' ';
105 flags[4] = ((root->flags & LYS_CONFIG_W) == 0) ? 'O' : 'R';
106 flags[5] = (root->nodetype == LYS_CONTAINER) ? 'C' : 'L';
107 log_add_verbose(1, LOG_COLOR_BOLD_YELLOW"%s"LOG_COLOR_CYAN" /%s:%s\n"LOG_COLOR_RESET, flags, module->name, root->name);
109 else if(root->nodetype == LYS_USES) {
110 struct lys_node *chd;
111 LY_TREE_FOR(root->child, chd) {
112 log_add_verbose(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);
113 if((chd->nodetype == LYS_CONTAINER) || (chd->nodetype == LYS_LIST)) {
114 flags[2] = ((chd->flags & LYS_STATUS_DEPRC) != 0) ? 'D' : ' ';
115 flags[4] = ((chd->flags & LYS_CONFIG_W) == 0) ? 'O' : 'R';
116 flags[5] = (chd->nodetype == LYS_CONTAINER) ? 'C' : 'L';
117 log_add_verbose(1, LOG_COLOR_BOLD_YELLOW"%s"LOG_COLOR_CYAN" /%s:%s\n"LOG_COLOR_RESET, flags, module->name, chd->name);
124 log_add_verbose(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");
128 int datastore_schema_print_xpath(const char *xpath) {
133 log_error("xpath is null\n");
134 return NTS_ERR_FAILED;
136 log_add_verbose(1, "printing out data structure for xpath: "LOG_COLOR_BOLD_YELLOW"%s"LOG_COLOR_RESET"\n", xpath);
138 struct lys_node *elem = (struct lys_node *)ly_ctx_get_node(session_context, 0, xpath, 0);
140 log_error("ly_ctx_get_node failed for xpath: %s\n", xpath);
141 return NTS_ERR_FAILED;
144 struct lys_module *module = lys_node_module(elem);
146 log_error("lys_node_module failed for xpath: %s\n", xpath);
147 return NTS_ERR_FAILED;
151 log_add_verbose(2, "module is %s @ revision %s\n", module->name, module->rev[0].date);
153 int rc = schema_print_recursive(elem);
154 if(rc != NTS_ERR_OK) {
155 log_error("schema_print_recursive failed for xpath: %s\n", xpath);
156 return NTS_ERR_FAILED;
159 log_add_verbose(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");
160 log_add_verbose(2, "schema_print() finished\n");
166 static int schema_print_recursive(struct lys_node *root) {
169 char my_status[] = "[ ]";
170 my_status[1] = ((root->flags & LYS_CONFIG_W) == 0) ? 'R' : 'W';
171 my_status[2] = ((root->flags & LYS_MAND_TRUE) != 0) ? 'M' : ' ';
172 my_status[3] = ((root->flags & LYS_STATUS_DEPRC) != 0) ? 'D' : ' ';
173 my_status[4] = ((root->flags & LYS_STATUS_OBSLT) != 0) ? 'O' : ' ';
175 if(((root->parent) && (root->parent->nodetype == LYS_CASE)) && ((root->parent->flags & LYS_MAND_TRUE) != 0)) {
179 char *path = lys_data_path(root);
180 if((root->nodetype != LYS_CHOICE) && (root->nodetype != LYS_CASE)) {
181 log_add_verbose(1, LOG_COLOR_BOLD_YELLOW"%s"LOG_COLOR_RESET" %-100s ", my_status, path);
183 if(root->nodetype == LYS_LIST) {
184 struct lys_node_list *list = (struct lys_node_list *)root;
185 log_add(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_"
187 else if(root->nodetype == LYS_LEAFLIST) {
188 struct lys_node_leaflist *list = (struct lys_node_leaflist *)root;
189 log_add(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_"
192 log_add(1, LOG_COLOR_CYAN"%-20s"LOG_COLOR_RESET, 4 + typeutils_yang_nodetype_to_str(root->nodetype)); //+4 to skip "LYS_"
197 switch(root->nodetype) {
200 struct lys_type *type = &((struct lys_node_leaf *)root)->type;
201 if(lys_is_key((const struct lys_node_leaf *)root, 0) != 0) {
202 log_add(1, LOG_COLOR_BOLD_YELLOW"[*]"LOG_COLOR_RESET);
208 char *typestr = typeutils_type_to_str(type);
209 log_add(1, "[%s]", typestr);
213 if(root->parent->nodetype == LYS_CASE) {
214 log_add(1, " is a "LOG_COLOR_BLUE"CASE"LOG_COLOR_RESET" of "LOG_COLOR_CYAN"%s"LOG_COLOR_RESET, root->parent->parent->name);
218 if(type->base == LY_TYPE_LEAFREF) {
219 log_add(1, " path: "LOG_COLOR_GREEN"%s"LOG_COLOR_RESET" ", type->info.lref.path);
221 else if(type->base == LY_TYPE_UNION) {
222 if((type->info.uni.count == 0) && (type->der != 0)) {
223 type = &type->der->type;
226 log_add(1, " available union types (%d):"LOG_COLOR_GREEN, type->info.uni.count);
227 for(int i = 0; i < type->info.uni.count; i++) {
228 char *typestr = typeutils_type_to_str(&type->info.uni.types[i]);
229 log_add(1, " %s", typestr);
232 log_add(1, LOG_COLOR_RESET);
240 if((root->nodetype != LYS_CHOICE) && (root->nodetype != LYS_CASE)) {
244 struct lys_node *child = 0;
245 LY_TREE_FOR(root->child, child) {
246 int rc = schema_print_recursive(child);
247 if(rc != NTS_ERR_OK) {
255 static bool generate_is_excluded_module(const char *module) {
258 for(int i = 0; i < framework_config.datastore_generate.excluded_modules_count; i++) {
259 if(strstr(module, framework_config.datastore_generate.excluded_modules[i]) != 0) {