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"
28 #include "core/session.h"
29 #include "core/framework.h"
31 static int datastore_populate_from_store(const char *running_filename, const char *operational_filename);
32 static int datastore_populate_commit(void);
34 int datastore_populate(int retries) {
40 rc = datastore_generate_external();
41 if(rc != NTS_ERR_OK) {
42 log_error("datastore_generate_external() failed\n");
43 return NTS_ERR_FAILED;
46 rc = datastore_populate_from_store(DATASTORE_RUNNING_PATH, DATASTORE_OPERATIONAL_PATH);
47 if(rc != NTS_ERR_OK) {
49 log_error("datastore_populate_from_store() failed\n");
53 sr_discard_changes(session_running);
54 sr_discard_changes(session_operational);
55 log_error("datastore_populate() failed, discarding changes\n");
58 rc = datastore_populate_commit();
59 if(rc != NTS_ERR_OK) {
60 log_error("datastore_populate_commit() failed\n");
72 log_error("datastore_populate() failed to populate\n");
73 return NTS_ERR_FAILED;
76 log_add_verbose(1, LOG_COLOR_BOLD_GREEN"datastore_populate() success\n"LOG_COLOR_RESET);
81 static int datastore_populate_from_store(const char *running_filename, const char *operational_filename) {
85 struct lyd_node *data;
87 data = datastore_load_external(running_filename, false);
89 log_add_verbose(1, "editing batch for RUNNING... ");
90 rc = sr_edit_batch(session_running, data, "replace");
91 lyd_free_withsiblings(data);
92 if (rc != SR_ERR_OK) {
93 log_add(1, LOG_COLOR_BOLD_RED"failed\n"LOG_COLOR_RESET);
94 return NTS_ERR_FAILED;
97 log_add(1, LOG_COLOR_BOLD_GREEN"done\n"LOG_COLOR_RESET);
101 if(running_filename) {
102 log_add_verbose(2, "datastore_populate_from_store(): %s could not be loaded, skipping\n", running_filename);
106 data = datastore_load_external(operational_filename, true);
108 log_add_verbose(1, "editing batch for OPERATIONAL... ");
109 rc = sr_edit_batch(session_operational, data, "replace");
110 lyd_free_withsiblings(data);
111 if (rc != SR_ERR_OK) {
112 log_add(1, LOG_COLOR_BOLD_RED"failed\n"LOG_COLOR_RESET);
113 return NTS_ERR_FAILED;
116 log_add(1, LOG_COLOR_BOLD_GREEN"done\n"LOG_COLOR_RESET);
120 if(running_filename) {
121 log_add_verbose(2, "datastore_populate_from_store(): %s could not be loaded, skipping\n", operational_filename);
128 static int datastore_populate_commit(void) {
131 log_add_verbose(1, "appling changes to RUNNING... ");
132 int rc = sr_apply_changes(session_running, 0, 0);
133 if (rc != SR_ERR_OK) {
134 sr_discard_changes(session_running);
135 log_add(1, LOG_COLOR_BOLD_RED"failed\n"LOG_COLOR_RESET);
136 return NTS_ERR_FAILED;
139 log_add(1, LOG_COLOR_BOLD_GREEN"done\n"LOG_COLOR_RESET);
142 log_add_verbose(1, "appling changes to OPERATIONAL... ");
143 rc = sr_apply_changes(session_operational, 0, 0);
144 if (rc != SR_ERR_OK) {
145 log_add(1, LOG_COLOR_BOLD_RED"failed\n"LOG_COLOR_RESET);
146 return NTS_ERR_FAILED;
149 log_add(1, LOG_COLOR_BOLD_GREEN"done\n"LOG_COLOR_RESET);