d30cdb636c691d7ee5897c6c52bc751abbd1a006
[sim/o1-interface.git] / ntsimulator / ntsim-ng / core / datastore / schema.c
1 /*************************************************************************
2 *
3 * Copyright 2020 highstreet technologies GmbH and others
4 *
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
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
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 ***************************************************************************/
17
18 #define _GNU_SOURCE
19
20 #include "utils/log_utils.h"
21 #include "utils/type_utils.h"
22 #include <stdio.h>
23 #include <time.h>
24 #include <inttypes.h>
25 #include <assert.h>
26
27 #include <libyang/libyang.h>
28 #include "core/session.h"
29 #include "core/framework.h"
30
31 static int schema_print_recursive(struct lys_node *root);
32 static bool generate_is_excluded_module(const char *module);
33
34 int datastore_schema_get_xpaths(char ***root_xpath) {
35     assert_session();
36     assert(root_xpath);
37
38     const struct lys_module *module;
39     const struct lys_node *root;
40     uint32_t idx = 0;
41     char **list = 0;
42     int total = 0;
43
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));
49                     if(!list) {
50                         log_error("bad realloc\n");
51                         return NTS_ERR_FAILED;
52                     }
53                     asprintf(&list[total], "/%s:%s", module->name, root->name);
54                     if(!list[total]) {
55                         log_error("bad asprintf\n");
56                         return NTS_ERR_FAILED;
57                     }
58                     total++; 
59                 }
60                 else if(root->nodetype == LYS_USES) {
61                     struct lys_node *chd;
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));
65                             if(!list) {
66                                 log_error("bad realloc\n");
67                                 return NTS_ERR_FAILED;
68                             }
69                             asprintf(&list[total], "/%s:%s", module->name, chd->name);                            
70                             if(!list[total]) {
71                                 log_error("bad asprintf\n");
72                                 return NTS_ERR_FAILED;
73                             }
74                             total++;
75                         }
76                     }
77                 }
78             }
79         }
80     }
81
82     *root_xpath = list;
83     return total;
84 }
85
86 int datastore_schema_print_root_paths(void) {
87     assert_session();
88
89     struct lys_module *module;
90     struct lys_node *root;
91     uint32_t idx = 0;
92
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);
95
96         char flags[10];
97         strcpy(flags, "[     ]");
98         flags[1] = (module->implemented == 0) ? 'i' : ' ';
99         flags[3] = generate_is_excluded_module(module->name) ? 'E' : ' ';
100
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);
108             }
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);
118                     }
119                 }
120             }
121         }
122     }
123
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");
125     return NTS_ERR_OK;
126 }
127
128 int datastore_schema_print_xpath(const char *xpath) {
129     assert_session();
130     assert(xpath);
131
132     if(xpath == 0) {
133         log_error("xpath is null\n");
134         return NTS_ERR_FAILED;
135     }
136     log_add_verbose(1, "printing out data structure for xpath: "LOG_COLOR_BOLD_YELLOW"%s"LOG_COLOR_RESET"\n", xpath);
137     
138     struct lys_node *elem = (struct lys_node *)ly_ctx_get_node(session_context, 0, xpath, 0);
139     if(elem == 0) {
140         log_error("ly_ctx_get_node failed for xpath: %s\n", xpath);
141         return NTS_ERR_FAILED;
142     }
143
144     struct lys_module *module = lys_node_module(elem);
145     if(module == 0) {
146         log_error("lys_node_module failed for xpath: %s\n", xpath);
147         return NTS_ERR_FAILED;
148     }
149
150
151     log_add_verbose(2, "module is %s @ revision %s\n", module->name, module->rev[0].date);
152
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;
157     }
158
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");
161
162     return NTS_ERR_OK;
163
164 }
165
166 static int schema_print_recursive(struct lys_node *root) {
167     assert(root);
168
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' : ' ';
174
175     if(((root->parent) && (root->parent->nodetype == LYS_CASE)) && ((root->parent->flags & LYS_MAND_TRUE) != 0)) {
176         my_status[2] = 'M';
177     }
178
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);
182
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_"
186         }
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_"
190         }
191         else {
192             log_add(1, LOG_COLOR_CYAN"%-20s"LOG_COLOR_RESET, 4 + typeutils_yang_nodetype_to_str(root->nodetype)); //+4 to skip "LYS_"
193         }
194     }
195     free(path);
196
197     switch(root->nodetype) {
198         case LYS_LEAF:
199         case LYS_LEAFLIST: {            
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);
203             }
204             else {
205                 log_add(1, "   ");
206             }
207
208             char *typestr = typeutils_type_to_str(type);
209             log_add(1, "[%s]", typestr);
210             free(typestr);
211             
212             if(root->parent) {
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);
215                 }
216             }
217             
218             if(type->base == LY_TYPE_LEAFREF) {
219                 log_add(1, " path: "LOG_COLOR_GREEN"%s"LOG_COLOR_RESET" ", type->info.lref.path);
220             }
221             else if(type->base == LY_TYPE_UNION) {
222                 if((type->info.uni.count == 0) && (type->der != 0)) {
223                     type = &type->der->type;
224                 }
225
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);
230                     free(typestr);
231                 }
232                 log_add(1, LOG_COLOR_RESET);
233             }
234         } break;
235
236         default:
237             break;
238     }
239   
240     if((root->nodetype != LYS_CHOICE) && (root->nodetype != LYS_CASE)) {
241         log_add(1, "\n");
242     }
243
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) {
248             return rc;
249         }
250     }
251
252     return NTS_ERR_OK;
253 }
254
255 static bool generate_is_excluded_module(const char *module) {
256     assert(module);
257
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) {
260             return true;
261         }
262     }
263     
264     return false;
265 }