843539757c487594b743bfb889b03fabc7313385
[sim/o1-interface.git] / ntsimulator / ntsim-ng / core / datastore / populate.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 "populate.h"
21 #include "utils/log_utils.h"
22 #include "utils/rand_utils.h"
23 #include "utils/type_utils.h"
24 #include <stdio.h>
25 #include <assert.h>
26
27 #include "generate.h"
28 #include "core/session.h"
29 #include "core/framework.h"
30
31 static int datastore_populate_from_store(const char *running_filename, const char *operational_filename);
32 static int datastore_populate_commit(void);
33
34 int datastore_populate(int retries) {
35     int rc;
36
37     while(retries) {
38         int failed = 0;
39
40         rc = datastore_generate_external();
41         if(rc != NTS_ERR_OK) {
42             log_error("datastore_generate_external() failed\n");
43             return NTS_ERR_FAILED;
44         }
45
46         rc = datastore_populate_from_store(DATASTORE_RUNNING_PATH, DATASTORE_OPERATIONAL_PATH);
47         if(rc != NTS_ERR_OK) {
48             failed = 1;
49             log_error("datastore_populate_from_store() failed\n");
50         }
51
52         if(failed) {
53             sr_discard_changes(session_running);
54             sr_discard_changes(session_operational);
55             log_error("datastore_populate() failed, discarding changes\n");
56         }
57         else {
58             rc = datastore_populate_commit();
59             if(rc != NTS_ERR_OK) {
60                 log_error("datastore_populate_commit() failed\n");
61                 failed = 1;
62             }
63         }
64
65         if(!failed) {
66             break;
67         }
68         retries--;
69     }
70
71     if(retries == 0) {
72         log_error("datastore_populate() failed to populate\n");
73         return NTS_ERR_FAILED;
74     }
75
76     log_add_verbose(1, LOG_COLOR_BOLD_GREEN"datastore_populate() success\n"LOG_COLOR_RESET);
77     return NTS_ERR_OK;
78 }
79
80
81 static int datastore_populate_from_store(const char *running_filename, const char *operational_filename) {
82     assert_session();
83     
84     int rc = 0;
85     struct lyd_node *data;
86
87     data = datastore_load_external(running_filename, false);
88     if(data) {
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;
95         }
96         else {
97             log_add(1, LOG_COLOR_BOLD_GREEN"done\n"LOG_COLOR_RESET);
98         }
99     }
100     else {
101         if(running_filename) {
102             log_add_verbose(2, "datastore_populate_from_store(): %s could not be loaded, skipping\n", running_filename);
103         }
104     }
105
106     data = datastore_load_external(operational_filename, true);
107     if(data) {
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;
114         }
115         else {
116             log_add(1, LOG_COLOR_BOLD_GREEN"done\n"LOG_COLOR_RESET);
117         }
118     }
119     else {
120         if(running_filename) {
121             log_add_verbose(2, "datastore_populate_from_store(): %s could not be loaded, skipping\n", operational_filename);
122         }
123     }
124
125     return NTS_ERR_OK;
126 }
127
128 static int datastore_populate_commit(void) {
129     assert_session();
130
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;
137     }
138     else {
139         log_add(1, LOG_COLOR_BOLD_GREEN"done\n"LOG_COLOR_RESET);
140     }
141
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;
147     }
148     else {
149         log_add(1, LOG_COLOR_BOLD_GREEN"done\n"LOG_COLOR_RESET);
150     }
151
152     return NTS_ERR_OK;
153 }