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 ***************************************************************************/
21 #include "utils/log_utils.h"
22 #include "utils/rand_utils.h"
23 #include "utils/type_utils.h"
24 #include "utils/sys_utils.h"
30 #include "core/session.h"
31 #include "core/framework.h"
35 #define LEAFREF_TOTAL_TEST_ENTRIES 11
42 const struct lys_module **modules;
45 struct lyd_node *operational;
46 struct lyd_node *running;
47 } generate_instance_t;
50 struct lyd_node *operational;
51 struct lyd_node *running;
54 int late_resolve_count;
55 struct lys_node **late_resolve_schema;
56 struct lyd_node **late_resolve_parent_o;
57 struct lyd_node **late_resolve_parent_r;
58 generate_instance_t **late_resolve_instance;
61 static int generate_recursive(generate_job_t *job, generate_instance_t *instance, struct lys_node *schema, struct lyd_node *parent_o, struct lyd_node *parent_r);
62 static int generate_add_leaf(generate_job_t *job, generate_instance_t *instance, struct lys_node *schema, struct lyd_node *parent_o, struct lyd_node *parent_r);
64 static int generate_late_resolve(generate_job_t *job);
65 static int generate_validate(generate_instance_t *instance, int count);
66 static int generate_export_data(generate_job_t *job, const char *running_filename, const char *operational_filename);
68 static int instance_add_module(generate_instance_t *instance, const struct lys_module *module);
69 static int generate_late_resolve_add_leaf(generate_job_t *job, generate_instance_t *instance, struct lys_node *schema, struct lyd_node *parent_o, struct lyd_node *parent_r);
70 static const char* leafref_test_val(int index);
72 static int generate_get_instance_count(const char *path);
73 static char *generate_get_restrict_schema(const char *path);
75 struct lyd_node *datastore_load_external(const char *filename, bool operational) {
77 struct lyd_node *data_tree = 0;
80 if(file_exists(filename)) {
81 LYD_FORMAT format = LYD_JSON;
82 if(strstr(filename, ".xml") != 0) {
86 int flags = LYD_OPT_TRUSTED | LYD_OPT_NOSIBLINGS;
88 flags |= LYD_OPT_DATA;
91 flags |= LYD_OPT_CONFIG;
94 data_tree = lyd_parse_path(session_context, filename, format, flags);
96 log_error("lyd_parse_path failed\n");
104 int datastore_generate_data(const char *running_filename, const char *operational_filename) {
107 log_add_verbose(1, LOG_COLOR_BOLD_YELLOW"datastore_generate_data() begin\n"LOG_COLOR_RESET);
112 job.late_resolve_count = 0;
113 job.late_resolve_instance = 0;
114 job.late_resolve_schema = 0;
115 job.late_resolve_parent_o = 0;
116 job.late_resolve_parent_r = 0;
117 job.late_resolving = false;
120 //load pre-populated data
121 for(int i = 0; i < framework_config.datastore_populate.preg_running_count; i++) {
122 char *filename = framework_config.datastore_populate.preg_running[i];
123 struct lyd_node *data = datastore_load_external(filename, false);
125 log_add_verbose(2, "datastore_load_external() could not load %s\n", filename);
128 log_add_verbose(1, "loaded into running %s (%s)\n", filename, data->schema->module->name);
130 int rc = lyd_merge(job.running, data, 0);
132 log_error("lyd_merge failed\n");
135 lyd_free_withsiblings(data);
142 //also load as operational
143 data = datastore_load_external(filename, true);
145 log_add_verbose(2, "datastore_load_external() could not load %s\n", filename);
148 log_add_verbose(1, "loaded into operational %s (%s)\n", filename, data->schema->module->name);
149 if(job.operational) {
150 int rc = lyd_merge(job.operational, data, 0);
152 log_error("lyd_merge failed\n");
155 lyd_free_withsiblings(data);
158 job.operational = data;
163 for(int i = 0; i < framework_config.datastore_populate.preg_operational_count; i++) {
164 char *filename = framework_config.datastore_populate.preg_operational[i];
165 struct lyd_node *data = datastore_load_external(filename, true);
167 log_add_verbose(2, "datastore_load_external() could not load %s\n", filename);
170 log_add_verbose(1, "loaded into operational %s (%s)\n", filename, data->schema->module->name);
171 if(job.operational) {
172 int rc = lyd_merge(job.operational, data, 0);
174 log_error("lyd_merge failed\n");
177 lyd_free_withsiblings(data);
180 job.operational = data;
186 int instance_count = datastore_schema_get_xpaths(&xpaths);
187 if(instance_count < 0) {
188 log_error("datastore_schema_get_xpaths failed\n");
189 return NTS_ERR_FAILED;
192 //exclude pre-populated modules
193 struct lyd_node *elem;
194 LY_TREE_FOR(job.operational, elem) {
195 for(int i = 0; i < instance_count; i++) {
196 if(strstr(xpaths[i], elem->schema->module->name) == (xpaths[i] + 1)) { //xpaths[i] is "/module:container"
200 for(int j = i; j < instance_count; j++) {
201 xpaths[j] = xpaths[j + 1];
209 generate_instance_t *instance = (generate_instance_t *)malloc(sizeof(generate_instance_t) * instance_count);
211 log_error("bad malloc\n");
212 for(int i = 0; i < instance_count; i++) {
216 return NTS_ERR_FAILED;
219 if(framework_config.datastore_populate.random_generation_enabled) {
220 //RANDOM generate everything
221 for(int i = 0; i < instance_count; i++) {
222 log_add_verbose(1, "generating "LOG_COLOR_BOLD_YELLOW"%s"LOG_COLOR_RESET" data...\n", xpaths[i]);
224 struct lys_node *schema_node = (struct lys_node *)ly_ctx_get_node(session_context, 0, xpaths[i], 0);
225 if(schema_node == 0) {
226 log_error("ly_ctx_get_node failed for %s\n", xpaths[i]);
227 return NTS_ERR_FAILED;
230 if(schema_node == 0) {
231 log_add_verbose(1, LOG_COLOR_BOLD_RED"failed\n"LOG_COLOR_RESET);
232 log_error("ly_ctx_get_node failed for %s\n", xpaths[i]);
233 return NTS_ERR_FAILED;
236 if(!schema_node->module->implemented) {
237 log_add_verbose(1, LOG_COLOR_BOLD_RED"failed\n"LOG_COLOR_RESET);
238 log_error("module is not implemented for %s\n", xpaths[i]);
239 return NTS_ERR_FAILED;
242 if((schema_node->flags & LYS_STATUS_DEPRC) != 0) {
243 log_add_verbose(1, LOG_COLOR_BOLD_RED"failed\n"LOG_COLOR_RESET);
244 log_error("module is deprecated for %s\n", xpaths[i]);
245 return NTS_ERR_FAILED;
248 //populate current instance vals
249 instance[i].init = 0;
250 instance[i].xpath = strdup(xpaths[i]);
251 instance[i].modules = 0;
252 instance[i].mod_count = 0;
253 instance[i].operational = 0;
254 instance[i].running = 0;
256 //do the actual population
257 int rc = generate_recursive(&job, &instance[i], schema_node, 0, 0);
258 if(rc != NTS_ERR_OK) {
259 log_error("generate_recursive failed instance %d with xpath %s\n", i, instance[i].xpath);
264 //link everything so we would be able to find everything in late-resolve
265 log_add_verbose(1, LOG_COLOR_BOLD_YELLOW"datastore_generate_data() done generating, now linking... (%d root nodes)\n"LOG_COLOR_RESET, instance_count);
266 for(int i = 0; i < instance_count; i++) {
268 if(instance[i].operational) {
269 if(job.operational) {
270 int rc = lyd_insert_sibling(&job.operational, instance[i].operational);
272 log_error("lyd_insert_sibling\n");
273 return NTS_ERR_FAILED;
277 job.operational = instance[i].operational;
281 if(instance[i].running) {
283 int rc = lyd_insert_sibling(&job.running, instance[i].running);
285 log_error("lyd_insert_sibling\n");
286 return NTS_ERR_FAILED;
290 job.running = instance[i].running;
296 log_add_verbose(1, LOG_COLOR_BOLD_YELLOW"datastore_generate_data() starting late-resolve process...\n"LOG_COLOR_RESET);
297 if(job.late_resolve_count) {
298 int rc = generate_late_resolve(&job);
299 if(rc != NTS_ERR_OK) {
300 log_error("generate_late_resolve failed\n");
305 //validate data and remove invalid nodes
306 log_add_verbose(1, LOG_COLOR_BOLD_YELLOW"datastore_generate_data() validating\n"LOG_COLOR_RESET);
307 int rc = generate_validate(instance, instance_count);
308 if(rc != NTS_ERR_OK) {
309 log_error("generate_validate failed\n");
314 //export generated data
315 log_add_verbose(1, LOG_COLOR_BOLD_YELLOW"datastore_generate_data() exporting data\n"LOG_COLOR_RESET);
316 int rc = generate_export_data(&job, running_filename, operational_filename);
317 if(rc != NTS_ERR_OK) {
318 log_error("generate_export_data failed\n");
323 log_add_verbose(1, LOG_COLOR_BOLD_YELLOW"datastore_generate_data() cleaning up... "LOG_COLOR_RESET);
324 for(int i = 0; i < instance_count; i++) {
325 log_add(1, "%d ", i);
327 free(instance[i].modules);
328 free(instance[i].xpath);
333 free(job.late_resolve_instance);
334 free(job.late_resolve_schema);
335 free(job.late_resolve_parent_o);
336 free(job.late_resolve_parent_r);
338 lyd_free_withsiblings(job.operational);
339 lyd_free_withsiblings(job.running);
342 log_add_verbose(1, LOG_COLOR_BOLD_GREEN"datastore_generate_data() finished\n"LOG_COLOR_RESET);
347 int datastore_generate_external(void) {
349 sprintf(cmd, "%s --generate", framework_arguments.argv[0]);
355 static int generate_recursive(generate_job_t *job, generate_instance_t *instance, struct lys_node *schema, struct lyd_node *parent_o, struct lyd_node *parent_r) {
360 char *resolved_schema_path = lys_path(schema, LYS_PATH_FIRST_PREFIX);
361 bool schema_operational = ((schema->flags & LYS_CONFIG_W) == 0);
363 generate_recursive_rerun_switch:
364 switch(schema->nodetype) {
365 //for container, just add it to the xpath, and iterate it's childeren to further traverse the tree
366 case LYS_CONTAINER: {
369 struct lyd_node *new_parent_o = parent_o;
370 struct lyd_node *new_parent_r = parent_r;
372 new_parent_o = lyd_new(parent_o, schema->module, schema->name);
374 log_error("error creating container operational -> %s\n", schema->name);
375 log_error("ly_error: %s\n", ly_errmsg(session_context));
376 return NTS_ERR_FAILED;
379 if(!schema_operational) {
380 new_parent_r = lyd_new(parent_r, schema->module, schema->name);
382 log_error("error creating container running -> %s\n", schema->name);
383 log_error("ly_error: %s\n", ly_errmsg(session_context));
384 return NTS_ERR_FAILED;
388 if(!instance->init) {
389 instance->init = true;
390 instance->operational = new_parent_o;
391 instance->running = new_parent_r;
394 char mandatory = ' ';
395 if((schema->flags & LYS_MAND_TRUE) != 0) {
398 if((schema->parent) && (schema->parent->nodetype == LYS_CASE)) {
399 if((schema->parent->flags & LYS_MAND_TRUE) != 0) {
403 bool node_operational = ((schema->flags & LYS_CONFIG_W) == 0);
404 char *xpath = lyd_path(new_parent_o);
405 log_add_verbose(1, LOG_COLOR_BOLD_MAGENTA"[%15s] "LOG_COLOR_BOLD_YELLOW"[%c%c]"LOG_COLOR_RESET" %s\n", "CONTAINER", node_operational ? 'O' : 'R', mandatory, xpath);
408 int rc = instance_add_module(instance, schema->module);
409 if(rc != NTS_ERR_OK) {
410 log_error("instance_add_module failed\n");
414 struct lys_node *child = 0;
415 LY_TREE_FOR(schema->child, child) {
416 int rc = generate_recursive(job, instance, child, new_parent_o, new_parent_r);
417 if(rc != NTS_ERR_OK) {
418 log_error("generate_recursive failed\n");
424 //choice does not appear into the data path. get all the avalable choices, and choose a random one
427 struct lys_node_case *choice = (struct lys_node_case *)schema->child;
430 choice = (struct lys_node_case *)choice->next;
433 //select a random choice
434 choice_no = rand_uint16() % choice_no;
437 choice = (struct lys_node_case *)schema->child;
438 while(i < choice_no) {
440 choice = (struct lys_node_case *)choice->next;
443 //after the choice was made, rerun the adding without other tree-searching (will run into a CASE)
444 schema = (struct lys_node *)choice;
445 goto generate_recursive_rerun_switch;
448 //the actual "case" is this node's child, so we skip directly to that
450 //case contains mandatory
452 schema = schema->child;
453 goto generate_recursive_rerun_switch;
463 //get min-max for current list
464 struct lys_node_list *list = (struct lys_node_list *)schema;
465 int min_added = list->min ? list->min : 1;
466 int max_added = list->max ? list->max : 65536;
468 int populating_times = generate_get_instance_count(resolved_schema_path);
469 if(populating_times != 0) {
470 if(min_added < populating_times) {
471 min_added = populating_times;
473 if(min_added > max_added) {
474 min_added = max_added;
475 log_error("min-elements exceeds max-elements for path %s. truncated to %d\n", resolved_schema_path, max_added);
477 log_add_verbose(2, "populating %d times list '%s'\n", min_added, resolved_schema_path);
479 //populate node with the intended number of values
483 struct lyd_node *new_parent_o = parent_o;
484 struct lyd_node *new_parent_r = parent_r;
486 new_parent_o = lyd_new(parent_o, schema->module, schema->name);
488 log_error("error creating list operational -> %s\n", schema->name);
489 log_error("ly_error: %s\n", ly_errmsg(session_context));
490 return NTS_ERR_FAILED;
493 if(!schema_operational) {
494 new_parent_r = lyd_new(parent_r, schema->module, schema->name);
496 log_error("error creating container running -> %s\n", schema->name);
497 log_error("ly_error: %s\n", ly_errmsg(session_context));
498 return NTS_ERR_FAILED;
502 if(!instance->init) {
503 instance->init = true;
504 instance->operational = new_parent_o;
505 instance->running = new_parent_r;
508 char mandatory = ' ';
509 if((schema->flags & LYS_MAND_TRUE) != 0) {
512 if((schema->parent) && (schema->parent->nodetype == LYS_CASE)) {
513 if((schema->parent->flags & LYS_MAND_TRUE) != 0) {
517 bool node_operational = ((schema->flags & LYS_CONFIG_W) == 0);
518 char *xpath = lyd_path(new_parent_o);
519 log_add_verbose(1, LOG_COLOR_BOLD_MAGENTA"[%15s] "LOG_COLOR_BOLD_YELLOW"[%c%c]"LOG_COLOR_RESET" %s\n", "LIST", node_operational ? 'O' : 'R', mandatory, xpath);
522 int rc = instance_add_module(instance, schema->module);
523 if(rc != NTS_ERR_OK) {
524 log_error("instance_add_module failed\n");
528 //populate all list elements below in the tree
529 struct lys_node *child = 0;
530 LY_TREE_FOR(schema->child, child) {
531 int rc = generate_recursive(job, instance, child, new_parent_o, new_parent_r);
532 if(rc != NTS_ERR_OK) {
533 log_error("generate_recursive failed\n");
542 log_add_verbose(2, "not populating list '%s'\n", resolved_schema_path);
548 if(generate_add_leaf(job, instance, schema, parent_o, parent_r) != NTS_ERR_OK) {
549 return NTS_ERR_FAILED;
553 //leaflist is treated the same as a LEAF, but with min/max characteristics of a LIST
555 //get min-max for the current leaflist
556 struct lys_node_leaflist *list = (struct lys_node_leaflist *)schema;
557 int min_added = list->min ? list->min : 1;
558 int max_added = list->max ? list->max : 65536;
560 int populating_times = generate_get_instance_count(resolved_schema_path);
561 if(populating_times != 0) {
562 if(min_added < populating_times) {
563 min_added = populating_times;
565 if(min_added > max_added) {
566 min_added = max_added;
567 log_error("min-elements exceeds max-elements for path %s truncated to %d\n", resolved_schema_path, max_added);
569 log_add_verbose(2, "populating %d times leaflist '%s'\n", min_added, resolved_schema_path);
573 if(generate_add_leaf(job, instance, schema, parent_o, parent_r) != NTS_ERR_OK) {
574 return NTS_ERR_FAILED;
580 log_add_verbose(2, "not populating leaflist '%s'\n", resolved_schema_path);
588 //don't do anything, since we don't want to add this or go further down the tree when we meet them
591 //other node types (grouping, uses, augment, etc just traverse)
593 log_add_verbose(1, "[%15s] %s\n", typeutils_yang_nodetype_to_str(schema->nodetype), resolved_schema_path);
595 //traverse the tree down for any other node types, without adding anything to the path
596 struct lys_node *child = 0;
597 LY_TREE_FOR(schema->child, child) {
598 int rc = generate_recursive(job, instance, child, parent_o, parent_r);
599 if(rc != NTS_ERR_OK) {
606 free(resolved_schema_path);
611 static int generate_add_leaf(generate_job_t *job, generate_instance_t *instance, struct lys_node *schema, struct lyd_node *parent_o, struct lyd_node *parent_r) {
617 int rc = instance_add_module(instance, schema->module);
618 if(rc != NTS_ERR_OK) {
619 log_error("bad schema_instance_add module\n");
623 struct lys_type *type = &((struct lys_node_leaf *)schema)->type;
625 char *data_xpath = lyd_path(parent_o);
626 data_xpath = (char *)realloc(data_xpath, sizeof(char) * (strlen(data_xpath) + 1 + strlen(schema->name) + 1));
628 log_error("lyd_path failed\n");
629 return NTS_ERR_FAILED;
631 strcat(data_xpath, "/");
632 strcat(data_xpath, schema->name);
634 //check whether the value is MANDATORY or not (for logging purposes)
635 char mandatory = ' ';
636 if((schema->flags & LYS_MAND_TRUE) != 0) {
640 if((schema->parent) && (schema->parent->nodetype == LYS_CASE)) {
641 if((schema->parent->flags & LYS_MAND_TRUE) != 0) {
646 bool node_operational = ((schema->flags & LYS_CONFIG_W) == 0);
647 log_add_verbose(1, LOG_COLOR_BOLD_MAGENTA"[%15s] "LOG_COLOR_BOLD_YELLOW"[%c%c]"LOG_COLOR_RESET" %s <-- ", typeutils_yang_type_to_str(type->base), node_operational ? 'O' : 'R', mandatory, data_xpath);
651 char *resolved_schema_path = lys_path(schema, LYS_PATH_FIRST_PREFIX);
652 char *value = generate_get_restrict_schema(resolved_schema_path);
653 free(resolved_schema_path);
655 generate_add_leaf_rerun_switch:
658 if((type->info.uni.count == 0) && (type->der != 0)) {
659 type = &type->der->type;
662 type = &type->info.uni.types[0];
663 goto generate_add_leaf_rerun_switch;
667 struct lyd_node *parent = parent_o;
668 while(parent->parent) {
669 parent = parent->parent;
673 value = lyd_path(parent);
676 goto generate_add_leaf_actual_add;
680 if(rand_bool()) { //if present, add it
681 log_add(1, LOG_COLOR_CYAN"present"LOG_COLOR_RESET"\n");
682 goto generate_add_leaf_actual_add;
685 log_add(1, LOG_COLOR_CYAN"empty"LOG_COLOR_RESET"\n");
690 case LY_TYPE_LEAFREF: {
693 struct lyd_node *new_node = 0;
694 while((new_node == 0) && (index < LEAFREF_TOTAL_TEST_ENTRIES)) {
695 new_node = lyd_new_leaf(parent_o, schema->module, schema->name, leafref_test_val(index));
700 log_error("error on lyd_new_leaf schema %s. didn't work with any temp val\n", schema->name);
701 return NTS_ERR_FAILED;
704 //based on the new_node's path, try to find elements of relative path for the leafref
705 struct ly_set *set = lyd_find_path(new_node, type->info.lref.path);
708 if(set && set->number) {
709 //choose a random schema and get its value
710 static int set_number = 0; //checkAL aici trebuia oare random ?
712 if(set_number >= set->number) {
715 asprintf(&value, "%s", ((struct lyd_node_leaf_list *)set->set.d[set_number])->value_str);
717 log_error("bad asprintf\n");
718 return NTS_ERR_FAILED;
721 int rc = instance_add_module(instance, set->set.d[set_number]->schema->module);
722 if(rc != NTS_ERR_OK) {
723 log_error("bad schema_instance_add module\n");
729 goto generate_add_leaf_actual_add;
732 //adding to late-resolve list, as we don't have any nodes in the leafref path
733 int rc = generate_late_resolve_add_leaf(job, instance, schema, parent_o, parent_r);
734 if(rc != NTS_ERR_OK) {
738 if(!job->late_resolving) {
739 log_add(1, LOG_COLOR_BOLD_YELLOW"added to late-resolve list...\n"LOG_COLOR_RESET);
742 log_add(1, LOG_COLOR_BOLD_YELLOW"REadded to late-resolve list...\n"LOG_COLOR_RESET);
752 value = rand_get_populate_value(type);
754 goto generate_add_leaf_actual_add;
758 generate_add_leaf_actual_add: {
759 //add schema to operational
760 struct lyd_node *new_node = lyd_new_leaf(parent_o, schema->module, schema->name, value);
762 log_error("error on lyd_new_leaf operational: %s\n", ly_errmsg(session_context));
763 return NTS_ERR_FAILED;
766 //print out the value
768 log_add(1, LOG_COLOR_CYAN"'%s'"LOG_COLOR_RESET"\n", value);
774 //if it fits the case, add it also to running
775 if(!node_operational) {
776 struct lyd_node *new_node = lyd_new_leaf(parent_r, schema->module, schema->name, value);
778 log_error("error on lyd_new_leaf running: %s\n", ly_errmsg(session_context));
779 return NTS_ERR_FAILED;
791 static int generate_late_resolve(generate_job_t *job) {
794 job->late_resolving = true;
796 int prev_count = job->late_resolve_count + 1;
798 while(prev_count > job->late_resolve_count) {
799 int late_resolve_count = job->late_resolve_count;
800 struct lys_node **late_resolve_schema = job->late_resolve_schema;
801 struct lyd_node **late_resolve_parent_o = job->late_resolve_parent_o;
802 struct lyd_node **late_resolve_parent_r = job->late_resolve_parent_r;
803 generate_instance_t **late_resolve_instance = job->late_resolve_instance;
805 job->late_resolve_count = 0;
806 job->late_resolve_schema = 0;
807 job->late_resolve_parent_o = 0;
808 job->late_resolve_parent_r = 0;
809 job->late_resolve_instance = 0;
811 prev_count = late_resolve_count;
813 for(int i = 0; i < late_resolve_count; i++) {
814 log_add_verbose(1, LOG_COLOR_BOLD_YELLOW"late-populating "LOG_COLOR_RESET": ");
815 int rc = generate_add_leaf(job, late_resolve_instance[i], late_resolve_schema[i], late_resolve_parent_o[i], late_resolve_parent_r[i]);
816 if(rc != NTS_ERR_OK) {
817 log_error("generate_add_leaf failed on late-resolve\n");
822 free(late_resolve_schema);
823 free(late_resolve_parent_o);
824 free(late_resolve_parent_r);
825 free(late_resolve_instance);
827 job->late_resolving = false;
829 if(prev_count != 0) {
830 log_error("generate_late_resolve detected circular loop!\n");
836 static int generate_validate(generate_instance_t *instance, int count) {
840 int commit_ok = NTS_ERR_OK;
842 for(int i = 0; i < count; i++) {
843 if(instance[i].operational) {
844 log_add_verbose(2, "available modules:");
845 for(int j = 0; j < instance[i].mod_count; j++) {
846 log_add(2, " %s", instance[i].modules[j]->name);
849 log_add_verbose(1, "validating OPERATIONAL for [%d] : %s... ", i, instance[i].xpath);
851 int solved_instance_errors = 1;
852 int solved_errors = 0;
853 bool success = false;
854 while(instance[i].operational && solved_instance_errors) {
855 solved_instance_errors = 0;
856 rc = lyd_validate_modules(&instance[i].operational, instance[i].modules, instance[i].mod_count, LYD_OPT_DATA, 0);
858 log_add(1, LOG_COLOR_BOLD_GREEN"success (%d)\n"LOG_COLOR_RESET, solved_errors);
865 struct ly_err_item *err = ly_err_first(session_context);
867 if((err->vecode == LYVE_NOWHEN) || (err->vecode == LYVE_NOMUST) || (err->vecode == LYVE_NOCONSTR) || (err->vecode == LYVE_NOLEAFREF) || (err->vecode == LYVE_NOMIN) || (err->vecode == LYVE_INVAL)) {
868 struct ly_set *set = lyd_find_path(instance[i].operational, err->path);
869 if(set && set->number) {
870 log_add_verbose(2, "operational error code %d on path %s with msg %s\n", err->vecode, err->path, err->msg);
871 log_add_verbose(2, LOG_COLOR_BOLD_RED" [WHEN-DELETE O]"LOG_COLOR_RESET" %s ... ", err->path);
873 bool mandatory = false;
874 if((set->set.d[0]->schema->flags & LYS_MAND_TRUE) != 0) {
878 if((set->set.d[0]->schema->parent) && (set->set.d[0]->schema->parent->nodetype == LYS_CASE)) {
879 if((set->set.d[0]->schema->parent->flags & LYS_MAND_TRUE) != 0) {
884 if((set->set.d[0]->dflt != 0) || (lys_is_key((const struct lys_node_leaf *)set->set.d[0]->schema, 0)) || (mandatory) || (err->vecode == LYVE_NOMIN)) {
885 //delete whole parent
886 log_add(2, "deleted parent : %s\n", lyd_path(set->set.d[0]->parent));
887 struct lyd_node *p = set->set.d[0]->parent;
888 lyd_free_withsiblings(set->set.d[0]);
890 if(p == instance[i].operational) {
891 log_add_verbose(1, "instance became empty "LOG_COLOR_BOLD_GREEN"success\n"LOG_COLOR_RESET);
893 instance[i].operational = 0;
898 //delete THIS node only
899 lyd_free(set->set.d[0]);
900 log_add(2, "deleted\n");
901 if(set->set.d[0] == instance[i].operational) {
902 log_add_verbose(1, "instance became empty "LOG_COLOR_BOLD_GREEN"success\n"LOG_COLOR_RESET);
904 instance[i].operational = 0;
908 solved_instance_errors++;
913 else if((err->vecode != 0) && (err->vecode != 29)) {
914 log_add_verbose(2, "operational error code %d on path %s with msg %s\n", err->vecode, err->path, err->msg);
919 ly_err_clean(session_context, 0);
922 solved_errors += solved_instance_errors;
927 log_add(1, LOG_COLOR_BOLD_YELLOW"failed"LOG_COLOR_RESET"\n%s\n", ly_errmsg(session_context));
930 log_add(1, LOG_COLOR_BOLD_YELLOW"partially solved (%d)"LOG_COLOR_RESET"\n", solved_errors);
935 if(instance[i].running) {
936 log_add_verbose(1, "validating RUNNING... for [%d] : %s... ", i, instance[i].xpath);
938 int solved_instance_errors = 1;
939 int solved_errors = 0;
940 bool success = false;
941 while(instance[i].running && solved_instance_errors) {
942 solved_instance_errors = 0;
943 rc = lyd_validate_modules(&instance[i].running, instance[i].modules, instance[i].mod_count, LYD_OPT_CONFIG, 0);
945 log_add(1, LOG_COLOR_BOLD_GREEN"success (%d)\n"LOG_COLOR_RESET, solved_errors);
952 struct ly_err_item *err = ly_err_first(session_context);
954 if((err->vecode == LYVE_NOWHEN) || (err->vecode == LYVE_NOMUST) || (err->vecode == LYVE_NOCONSTR) || (err->vecode == LYVE_NOLEAFREF) || (err->vecode == LYVE_NOMIN) || (err->vecode == LYVE_INVAL)) {
955 struct ly_set *set = lyd_find_path(instance[i].running, err->path);
956 if(set && set->number) {
957 log_add_verbose(2, "running error code %d on path %s with msg %s\n", err->vecode, err->path, err->msg);
958 log_add_verbose(2, LOG_COLOR_BOLD_RED" [WHEN-DELETE R]"LOG_COLOR_RESET" %s ... ", err->path);
960 bool mandatory = false;
961 if((set->set.d[0]->schema->flags & LYS_MAND_TRUE) != 0) {
965 if((set->set.d[0]->schema->parent) && (set->set.d[0]->schema->parent->nodetype == LYS_CASE)) {
966 if((set->set.d[0]->schema->parent->flags & LYS_MAND_TRUE) != 0) {
971 if((set->set.d[0]->dflt != 0) || (lys_is_key((const struct lys_node_leaf *)set->set.d[0]->schema, 0)) || (mandatory) || (err->vecode == LYVE_NOMIN)) {
972 //delete whole parent
973 log_add(2, "deleted parent : %s\n", lyd_path(set->set.d[0]->parent));
974 struct lyd_node *p = set->set.d[0]->parent;
975 lyd_free_withsiblings(set->set.d[0]);
978 if(p == instance[i].running) {
979 log_add_verbose(1, "instance became empty "LOG_COLOR_BOLD_GREEN"success\n"LOG_COLOR_RESET);
981 instance[i].running = 0;
986 //delete THIS node only
987 lyd_free(set->set.d[0]);
988 log_add(2, "deleted\n");
989 if(set->set.d[0] == instance[i].running) {
990 log_add_verbose(1, "instance became empty "LOG_COLOR_BOLD_GREEN"success\n"LOG_COLOR_RESET);
992 instance[i].running = 0;
996 solved_instance_errors++;
1001 else if((err->vecode != 0) && (err->vecode != 29)) {
1002 log_add_verbose(2, "running error code %d on path %s with msg %s\n", err->vecode, err->path, err->msg);
1007 ly_err_clean(session_context, 0);
1010 solved_errors += solved_instance_errors;
1014 if(!solved_errors) {
1015 log_add(1, LOG_COLOR_BOLD_YELLOW"failed"LOG_COLOR_RESET"\n%s\n", ly_errmsg(session_context));
1018 log_add(1, LOG_COLOR_BOLD_YELLOW"partially solved (%d)"LOG_COLOR_RESET"\n", solved_errors);
1027 static int generate_export_data(generate_job_t *job, const char *running_filename, const char *operational_filename) {
1030 if(job->operational) {
1031 if(lyd_print_path(operational_filename, job->operational, LYD_JSON, LYP_FORMAT | LYP_WITHSIBLINGS) != 0) {
1032 log_error("lyd_print_path failed for operational\n");
1033 return NTS_ERR_FAILED;
1038 if(lyd_print_path(running_filename, job->running, LYD_JSON, LYP_FORMAT | LYP_WITHSIBLINGS) != 0) {
1039 log_error("lyd_print_path failed for running\n");
1040 return NTS_ERR_FAILED;
1047 static int instance_add_module(generate_instance_t *instance, const struct lys_module *module) {
1051 for(int i = 0; i < instance->mod_count; i++) {
1052 if(instance->modules[i] == module) {
1057 instance->modules = (const struct lys_module **)realloc(instance->modules, sizeof(const struct lys_module *) * (instance->mod_count + 1));
1058 if(!instance->modules) {
1059 log_error("bad realloc\n");
1060 return NTS_ERR_FAILED;
1062 instance->modules[instance->mod_count] = module;
1063 instance->mod_count++;
1068 static int generate_late_resolve_add_leaf(generate_job_t *job, generate_instance_t *instance, struct lys_node *schema, struct lyd_node *parent_o, struct lyd_node *parent_r) {
1072 job->late_resolve_schema = (struct lys_node **)realloc(job->late_resolve_schema, (job->late_resolve_count + 1) * sizeof(struct lys_node *));
1073 if(!job->late_resolve_schema) {
1074 log_error("bad realloc\n");
1075 return NTS_ERR_FAILED;
1077 job->late_resolve_schema[job->late_resolve_count] = schema;
1079 job->late_resolve_parent_o = (struct lyd_node **)realloc(job->late_resolve_parent_o, (job->late_resolve_count + 1) * sizeof(struct lyd_node *));
1080 if(!job->late_resolve_parent_o) {
1081 log_error("bad realloc\n");
1082 return NTS_ERR_FAILED;
1084 job->late_resolve_parent_o[job->late_resolve_count] = parent_o;
1086 job->late_resolve_parent_r = (struct lyd_node **)realloc(job->late_resolve_parent_r, (job->late_resolve_count + 1) * sizeof(struct lyd_node *));
1087 if(!job->late_resolve_parent_r) {
1088 log_error("bad realloc\n");
1089 return NTS_ERR_FAILED;
1091 job->late_resolve_parent_r[job->late_resolve_count] = parent_r;
1093 job->late_resolve_instance = (generate_instance_t **)realloc(job->late_resolve_instance, (job->late_resolve_count + 1) * sizeof(generate_instance_t *));
1094 if(!job->late_resolve_instance) {
1095 log_error("bad realloc\n");
1096 return NTS_ERR_FAILED;
1098 job->late_resolve_instance[job->late_resolve_count] = instance;
1100 job->late_resolve_count++;
1105 static const char* leafref_test_val(int index) {
1116 return "Fd:4D:63:A5:21:C5";
1140 return "best-effort";
1144 return "yes-fault:o-ran-sc-alarm-type";
1152 log_error("index out of bounds\n");
1158 static int generate_get_instance_count(const char *path) {
1161 for(int i = 0; i < framework_config.datastore_generate.custom_list_instances_count; i++) {
1162 if(strcmp(path, framework_config.datastore_generate.custom_list_instances[i].path) == 0) {
1163 return framework_config.datastore_generate.custom_list_instances[i].count;
1166 return framework_config.datastore_generate.default_list_instances;
1169 static char *generate_get_restrict_schema(const char *path) {
1173 for(int i = 0; i < framework_config.datastore_generate.restrict_schema_count; i++) {
1174 if(strcmp(path, framework_config.datastore_generate.restrict_schema[i].path) == 0) {
1175 ret = strdup(framework_config.datastore_generate.restrict_schema[i].values[framework_config.datastore_generate.restrict_schema[i].index]);
1176 framework_config.datastore_generate.restrict_schema[i].index++;
1177 if(framework_config.datastore_generate.restrict_schema[i].index >= framework_config.datastore_generate.restrict_schema[i].values_count) {
1178 framework_config.datastore_generate.restrict_schema[i].index = 0;