+
+ log_add_verbose(1, "populate finished...\n");
+ return NTS_ERR_OK;
+}
+
+int datastore_populate_update_operational(const char **xpath, int xpath_len) {
+
+ if(xpath_len == 0) {
+ return NTS_ERR_OK;
+ }
+
+ populate_instance_t *instance = 0;
+ int instance_count = 0;
+
+ for(int i = 0; i < xpath_len; i++) {
+ log_add_verbose(1, "generating "LOG_COLOR_BOLD_YELLOW"%s"LOG_COLOR_RESET" operational data...\n", xpath[i]);
+
+ struct lyd_node *node_dev = datastore_operations_get_lyd_node(populate_job.dev, xpath[i]);
+ if(node_dev == 0) {
+ log_error("datastore_operations_get_lyd_node failed on dev\n");
+ return NTS_ERR_FAILED;
+ }
+
+ struct lyd_node *node_running = datastore_operations_get_lyd_node(populate_job.running, xpath[i]);
+ if(node_running == 0) {
+ log_error("datastore_operations_get_lyd_node failed on running\n");
+ return NTS_ERR_FAILED;
+ }
+
+ //operational node (container/list) does not exist yet
+ struct lyd_node *node_operational = lyd_new_path(populate_job.operational, 0, xpath[i], 0, 0, LYD_PATH_OPT_NOPARENTRET | LYD_PATH_OPT_UPDATE);
+ if(node_operational == 0) {
+ log_error("lyd_new_path failed on operational\n");
+ return NTS_ERR_FAILED;
+ }
+
+ struct lys_node *schema_node = node_dev->schema;
+
+ int cinst = instance_count;
+ instance_count++;
+ instance = (populate_instance_t *)realloc(instance, sizeof(populate_instance_t) * instance_count);
+ instance[cinst].init = true;
+ instance[cinst].xpath = strdup(xpath[i]);
+ instance[cinst].modules = 0;
+ instance[cinst].mod_count = 0;
+ instance[cinst].dev = node_dev;
+ instance[cinst].operational = node_operational;
+ instance[cinst].running = node_running;
+
+ int rc = populate_instance_add_module(&instance[cinst], schema_node->module);
+ if(rc != NTS_ERR_OK) {
+ log_error("instance_add_module failed\n");
+ return rc;
+ }
+
+ //populate-recursive pe toti childrenii, cu param only_operational == 1
+ struct lys_node *elem;
+ LY_TREE_FOR(schema_node->child, elem) {
+ int rc = populate_recursive(&populate_job, &instance[cinst], elem, node_dev, node_operational, node_running, 1);
+ if(rc != NTS_ERR_OK) {
+ log_error("populate_recursive failed with xpath %s\n", instance[cinst].xpath);
+ return rc;
+ }
+ }
+ }
+
+ //late resolve
+ log_add_verbose(1, LOG_COLOR_BOLD_YELLOW"datastore_populate_update_operational() starting late-resolve process...\n"LOG_COLOR_RESET);
+ if(populate_job.late_resolve_count) {
+ int rc = populate_late_resolve(&populate_job);
+ if(rc != NTS_ERR_OK) {
+ log_error("populate_late_resolve failed\n");
+ return rc;
+ }
+ }
+
+ // //validate
+ // log_add_verbose(1, LOG_COLOR_BOLD_YELLOW"datastore_populate_update_operational() validating\n"LOG_COLOR_RESET);
+
+ // //build validate_instance' populate_validate must have root-path instances
+ // populate_instance_t *validate_instance = 0;
+ // int validate_instance_count = 0;
+
+ // for(int i = 0; i < instance_count; i++) {
+ // //get root path from instance[i].xpath
+ // char *root_path = strdup(instance[i].xpath);
+ // *strstr(root_path + 1, "/") = 0;
+
+ // int found = validate_instance_count;
+ // for(int j = 0; j < validate_instance_count; j++) {
+ // if(strcmp(root_path, validate_instance[j].xpath) == 0) {
+ // found = j;
+ // break;
+ // }
+ // }
+
+ // //if not found
+ // if(found == validate_instance_count) {
+ // //add root path UNIQUE to validate_instance
+ // validate_instance_count++;
+ // validate_instance = (populate_instance_t *)realloc(validate_instance, sizeof(populate_instance_t) * validate_instance_count);
+
+ // validate_instance[found].init = true;
+ // validate_instance[found].xpath = strdup(root_path);
+ // validate_instance[found].modules = 0;
+ // validate_instance[found].mod_count = 0;
+ // validate_instance[found].dev = datastore_operations_get_lyd_node(populate_job.dev, root_path);
+ // validate_instance[found].operational = datastore_operations_get_lyd_node(populate_job.operational, root_path);
+ // validate_instance[found].running = datastore_operations_get_lyd_node(populate_job.running, root_path);
+ // }
+ // free(root_path);
+
+ // //add each instance[i].modules to validate_instance[].modules
+ // for(int j = 0; j < instance[i].mod_count; j++) {
+ // int rc = populate_instance_add_module(&validate_instance[found], instance[i].modules[j]);
+ // if(rc != NTS_ERR_OK) {
+ // log_error("instance_add_module failed\n");
+ // return rc;
+ // }
+ // }
+ // }
+
+ // int rc = populate_validate(validate_instance, validate_instance_count);
+ // if(rc != NTS_ERR_OK) {
+ // log_error("populate_validate failed\n");
+ // return rc;
+ // }
+
+ //cleanup
+ log_add_verbose(1, LOG_COLOR_BOLD_YELLOW"datastore_populate_update_operational() cleanup\n"LOG_COLOR_RESET);
+ // for(int i = 0; i < validate_instance_count; i++) {
+ // free(validate_instance[i].modules);
+ // free(validate_instance[i].xpath);
+ // }
+ // free(validate_instance);
+
+ for(int i = 0; i < instance_count; i++) {
+ free(instance[i].modules);
+ free(instance[i].xpath);
+ }
+ free(instance);
+
+ free(populate_job.late_resolve_instance);
+ free(populate_job.late_resolve_schema);
+ free(populate_job.late_resolve_parent_d);
+ free(populate_job.late_resolve_parent_o);
+ free(populate_job.late_resolve_parent_r);
+
+ populate_job.late_resolving = false;
+ populate_job.late_resolve_instance = 0;
+ populate_job.late_resolve_schema = 0;
+ populate_job.late_resolve_parent_d = 0;
+ populate_job.late_resolve_parent_o = 0;
+ populate_job.late_resolve_parent_r = 0;
+ populate_job.late_resolve_count = 0;
+
+ //edit batch and apply pe operational
+ if(populate_job.operational) {
+ log_add_verbose(1, "editing batch for OPERATIONAL... ");
+ int rc = sr_edit_batch(session_operational, populate_job.operational, "replace");
+ // lyd_free_withsiblings(populate_job.running); //checkAL
+ if (rc != SR_ERR_OK) {
+ log_add(1, LOG_COLOR_BOLD_RED"failed\n"LOG_COLOR_RESET);
+ return NTS_ERR_FAILED;
+ }
+ else {
+ log_add(1, LOG_COLOR_BOLD_GREEN"done\n"LOG_COLOR_RESET);
+ }
+
+
+ //apply pe operational
+ log_add_verbose(1, "appling changes to OPERATIONAL... ");
+ rc = sr_apply_changes(session_operational, 0, 0);
+ if (rc != SR_ERR_OK) {
+ log_add(1, LOG_COLOR_BOLD_RED"failed\n"LOG_COLOR_RESET);
+ return NTS_ERR_FAILED;
+ }
+ else {
+ log_add(1, LOG_COLOR_BOLD_GREEN"done\n"LOG_COLOR_RESET);
+ }
+ }
+
+ log_add_verbose(1, "datastore_populate_update_operational() finished...\n");