Add supoprt for D release use-case.
[sim/o1-interface.git] / ntsimulator / ntsim-ng / core / app / manager.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 "manager.h"
21 #include "utils/log_utils.h"
22 #include "utils/sys_utils.h"
23 #include <stdio.h>
24 #include <assert.h>
25
26 #include <sysrepo.h>
27 #include <sysrepo/values.h>
28
29 #include "core/framework.h"
30 #include "core/session.h"
31 #include "core/context.h"
32
33 #include "app_common.h"
34
35 static int manager_change_cb(sr_session_ctx_t *session, const char *module_name, const char *xpath, sr_event_t event, uint32_t request_id, void *private_data);
36 static int manager_instances_get_items_cb(sr_session_ctx_t *session, const char *module_name, const char *xpath, const char *request_xpath, uint32_t request_id, struct lyd_node **parent, void *private_data);
37
38 int manager_run(void) {
39     assert_session();
40
41     log_add_verbose(1, LOG_COLOR_BOLD_YELLOW"starting MANAGER...\n"LOG_COLOR_RESET);
42
43     int rc = app_common_init();
44     if(rc != NTS_ERR_OK) {
45         log_error("app_common_init failed\n");
46         return NTS_ERR_FAILED;
47     }
48     
49     //init manager context
50     rc = manager_context_init();
51     if(rc != NTS_ERR_OK) {
52         log_error("manager_context_init failed\n");
53         return NTS_ERR_FAILED;
54     }
55
56     //init operations
57     rc = manager_operations_init();
58     if(rc != NTS_ERR_OK) {
59         log_error("manager_operations_init failed\n");
60         return NTS_ERR_FAILED;
61     }
62
63     //print everything on the manager's screen
64     log_add_verbose(1, LOG_COLOR_BOLD_CYAN"Available images: \n"LOG_COLOR_RESET);
65     for(int i = 0; i < docker_context_count; i++) {
66         log_add_verbose(1, LOG_COLOR_BOLD_CYAN"- %s\n"LOG_COLOR_RESET, docker_context[i].image);
67         for(int j = 0; j < docker_context[i].available_images_count; j++) {
68             log_add_verbose(1, "   - "LOG_COLOR_RED"%s/"LOG_COLOR_CYAN"%s"LOG_COLOR_RESET":"LOG_COLOR_YELLOW"%s\n"LOG_COLOR_RESET, docker_context[i].available_images[j].repo, docker_context[i].image, docker_context[i].available_images[j].tag);
69         }
70     }
71     
72     // subscribe to any changes on the list
73     rc = sr_module_change_subscribe(session_running, NTS_MANAGER_MODULE, NTS_SIMULATION_SCHEMA_XPATH, manager_change_cb, NULL, 0, SR_SUBSCR_CTX_REUSE | SR_SUBSCR_UPDATE, &session_subscription);
74     if(rc != SR_ERR_OK) {
75         log_error("could not subscribe to simulation changes\n");
76         return NTS_ERR_FAILED;
77     }
78
79     //subscribe to stats
80     rc = sr_oper_get_items_subscribe(session_running, NTS_MANAGER_MODULE, NTS_SIMULATION_SCHEMA_XPATH, manager_sr_stats_get_items_cb, NULL, SR_SUBSCR_CTX_REUSE | SR_SUBSCR_OPER_MERGE, &session_subscription);
81     if(rc != SR_ERR_OK) {
82         log_error("could not subscribe to oper faults\n");
83         return NTS_ERR_FAILED;
84     }
85
86     //subscribe to instances oper change
87     rc = sr_oper_get_items_subscribe(session_running, NTS_MANAGER_MODULE, NTS_FUNCTION_LIST_SCHEMA_XPATH, manager_instances_get_items_cb, NULL, SR_SUBSCR_CTX_REUSE, &session_subscription);
88     if(rc != SR_ERR_OK) {
89         log_error("could not subscribe to oper faults\n");
90         return 0;
91     }
92
93     rc = manager_sr_update_static_stats();
94     if(rc != NTS_ERR_OK) {
95         log_error("manager_sr_update_static_stats failed\n");
96         return NTS_ERR_FAILED;
97     }
98
99     log_add_verbose(1, LOG_COLOR_BOLD_YELLOW"nts-ng manager"LOG_COLOR_RESET" v%s build %s\n", framework_environment.nts.version, framework_environment.nts.build_time);
100     log_add_verbose(1, LOG_COLOR_BOLD_YELLOW"Host IP:"LOG_COLOR_RESET" %s\n", framework_environment.host.ip);
101     log_add_verbose(1, LOG_COLOR_BOLD_YELLOW"Host ports"LOG_COLOR_RESET": ");
102     if(framework_environment.settings.ssh_connections) {
103         log_add(1, "NETCONF SSH: %d (%d)", framework_environment.host.ssh_base_port, framework_environment.settings.ssh_connections);
104     }
105     else {
106         log_add(1, "NETCONF SSH: disabled");
107     }
108     if(framework_environment.settings.tls_connections) {
109         log_add(1, " | NETCONF TLS: %d (%d)", framework_environment.host.tls_base_port, framework_environment.settings.tls_connections);
110     }
111     else {
112         log_add(1, " | NETCONF TLS: disabled");
113     }
114     if(framework_environment.settings.ftp_connections) {
115         log_add(1, " | FTP: %d (%d)", framework_environment.host.ftp_base_port, framework_environment.settings.ftp_connections);
116     }
117     else {
118         log_add(1, " | FTP: disabled");
119     }
120     if(framework_environment.settings.sftp_connections) {
121         log_add(1, " | SFTP: %d (%d)", framework_environment.host.sftp_base_port, framework_environment.settings.sftp_connections);
122     }
123     else {
124         log_add(1, " | SFTP: disabled");
125     }
126     log_add(1,"\n");
127     log_add_verbose(1, LOG_COLOR_BOLD_GREEN"started!\n"LOG_COLOR_RESET);
128
129     //daemonize
130     while(!framework_sigint) {
131         manager_operations_loop();  //caution - this function time-waits (1sec) on manager_operation_sem
132     }
133
134     manager_operations_free();
135     manager_context_free();
136
137     return NTS_ERR_OK;
138 }
139
140 static int manager_change_cb(sr_session_ctx_t *session, const char *module_name, const char *xpath, sr_event_t event, uint32_t request_id, void *private_data) {
141     sr_change_iter_t *it = 0;
142     sr_change_oper_t oper;
143     sr_val_t *old_value = 0;
144     sr_val_t *new_value = 0;
145     int rc = SR_ERR_OK;
146
147     if(manager_sr_get_context_sync()) {
148         return SR_ERR_OK;
149     }
150
151     if(event == SR_EV_UPDATE) {
152         rc = sr_get_changes_iter(session, NTS_FUNCTION_LIST_SCHEMA_XPATH"//.", &it);
153         if(rc != SR_ERR_OK) {
154             log_error("sr_get_changes_iter failed\n");
155             return SR_ERR_VALIDATION_FAILED;
156         }
157
158         manager_operation_t *new_oper = 0;
159         while((rc = sr_get_change_next(session, it, &oper, &old_value, &new_value)) == SR_ERR_OK) {
160             if(new_value) {
161                 //get function type and index
162                 char *nv = sr_val_to_str(new_value);
163
164                 char function_type[512];
165                 strncpy(function_type, strstr(new_value->xpath, "function-type='") + 15, 510);
166                 *strchr(function_type, '\'') = 0;
167                 
168                 //if context is new
169                 if((new_oper == 0) || (strcmp(new_oper->function_type, function_type) != 0)) {
170
171                     if(new_oper == 0) {
172                         manager_operations_begin();
173                     }
174                     else {
175                         //validate and add the operation
176                         if(manager_operations_validate(new_oper) != NTS_ERR_OK) {
177                             manager_operations_free_oper(new_oper);
178                             manager_operations_finish_with_error();
179                             return SR_ERR_VALIDATION_FAILED;
180                         }
181                         
182                         manager_operations_add(new_oper);
183                     }
184
185                     new_oper = manager_operations_new_oper(MANAGER_OPERATION_EDIT);
186                     new_oper->function_type = strdup(function_type);
187
188                     //get ft_idnex
189                     for(int i = 0; i < docker_context_count; i++) {
190                         if(strcmp(new_oper->function_type, manager_context[i].function_type) == 0) {
191                             new_oper->ft_index = i;
192                             break;
193                         }
194                     }
195
196                     if(new_oper->ft_index == -1) {
197                         log_error("function-type not found: %s\n", new_oper->function_type);
198                         return SR_ERR_VALIDATION_FAILED;
199                     }
200                 }
201
202                 char *leaf_path  = strdup(strstr(new_value->xpath, "']/") + 3);
203                 if(strcmp(leaf_path, "started-instances") == 0) {
204                     new_oper->started_instances = new_value->data.uint16_val;
205                     rc = sr_set_item(session, old_value->xpath, old_value, 0);
206                     if(rc != SR_ERR_OK) {
207                         log_error("sr_set_item failed\n");
208                         return SR_ERR_VALIDATION_FAILED;
209                     }
210                 }
211                 else if(strcmp(leaf_path, "mounted-instances") == 0) {
212                     new_oper->mounted_instances = new_value->data.uint16_val;
213                     rc = sr_set_item(session, old_value->xpath, old_value, 0);
214                     if(rc != SR_ERR_OK) {
215                         log_error("sr_set_item failed\n");
216                         return SR_ERR_VALIDATION_FAILED;
217                     }
218                 }
219                 else if(strcmp(leaf_path, "docker-instance-name") == 0) {
220                     new_oper->docker_instance_name = strdup(nv);
221                     free(manager_context[new_oper->ft_index].docker_instance_name);
222                     manager_context[new_oper->ft_index].docker_instance_name = strdup(nv);
223                 }
224                 else if(strcmp(leaf_path, "docker-version-tag") == 0) {
225                     new_oper->docker_version_tag = strdup(nv);
226                     free(manager_context[new_oper->ft_index].docker_version_tag);
227                     manager_context[new_oper->ft_index].docker_version_tag = strdup(nv);
228                 }
229                 else if(strcmp(leaf_path, "docker-repository") == 0) {
230                     new_oper->docker_repository = strdup(nv);
231                     free(manager_context[new_oper->ft_index].docker_repository);
232                     manager_context[new_oper->ft_index].docker_repository = strdup(nv);
233                 }
234                 else if(strcmp(leaf_path, "mount-point-addressing-method") == 0) {
235                     //update conetxt
236                     free(manager_context[new_oper->ft_index].mount_point_addressing_method);
237                     manager_context[new_oper->ft_index].mount_point_addressing_method = strdup(nv);
238                 }
239                 else {
240                     //mark each instance for reconfiguration
241                     for(int i = 0; i < manager_context[new_oper->ft_index].started_instances; i++) {
242                         manager_context[new_oper->ft_index].instance[i].is_configured = false;
243                     }
244                 }
245
246                 free(leaf_path);
247                 free(nv);
248             }
249
250             sr_free_val(old_value);
251             sr_free_val(new_value);
252         }
253
254         sr_free_change_iter(it);
255
256         //validate and add the operation, if any; can be 0 if no modifications to NF list
257         if(new_oper) {
258             if(manager_operations_validate(new_oper) != NTS_ERR_OK) {
259                 manager_operations_free_oper(new_oper);
260                 manager_operations_finish_with_error();
261                 return SR_ERR_VALIDATION_FAILED;
262             }
263
264             manager_operations_add(new_oper);
265         }
266     }
267     else if(event == SR_EV_CHANGE) {
268     }
269     else if(event == SR_EV_DONE) {
270         bool global_change = false;
271
272         // go throughout all the changes, not just NF list
273         rc = sr_get_changes_iter(session, NTS_SIMULATION_SCHEMA_XPATH"//.", &it);
274         if(rc != SR_ERR_OK) {
275             log_error("sr_get_changes_iter failed\n");
276             return SR_ERR_VALIDATION_FAILED;
277         }
278
279         while((rc = sr_get_change_next(session, it, &oper, &old_value, &new_value)) == SR_ERR_OK) {
280             if(new_value) {
281                 if(strstr(new_value->xpath, NTS_FUNCTION_LIST_SCHEMA_XPATH) != new_value->xpath) {
282                     global_change = true;
283                     sr_free_val(old_value);
284                     sr_free_val(new_value);
285                     break;
286                 }
287             }
288
289             sr_free_val(old_value);
290             sr_free_val(new_value);
291         }
292
293         sr_free_change_iter(it);
294
295         if(global_change) {
296             //mark each instance for reconfiguration
297             for(int i = 0; i < docker_context_count; i++) {
298                 for(int j = 0; j < manager_context[i].started_instances; j++) {
299                     manager_context[i].instance[j].is_configured = false;
300                 }
301             }
302         }
303
304         manager_operations_finish_and_execute();   //from this point on, manager_operations_loop will take over
305     }
306
307     return SR_ERR_OK;
308 }
309
310 static int manager_instances_get_items_cb(sr_session_ctx_t *session, const char *module_name, const char *xpath, const char *request_xpath, uint32_t request_id, struct lyd_node **parent, void *private_data) {
311
312     *parent = lyd_new_path(NULL, sr_get_context(sr_session_get_connection(session)), NTS_FUNCTION_LIST_SCHEMA_XPATH, 0, 0, 0);
313     if(*parent == 0) {
314         log_error("lyd_new_path failed\n");
315         return SR_ERR_OPERATION_FAILED;
316     }
317
318     for(int i = 0; i < docker_context_count; i++) {
319         char ftype_path[512];
320         sprintf(ftype_path, "%s[function-type='%s']/instances/instance", NTS_FUNCTION_LIST_SCHEMA_XPATH, manager_context[i].function_type);
321         for(int j = 0; j < manager_context[i].started_instances; j++) {
322             char instance_path[1024];
323             sprintf(instance_path, "%s[name='%s']", ftype_path, manager_context[i].instance[j].container.name);
324
325             char full_path[2048];
326
327             sprintf(full_path, "%s/mount-point-addressing-method", instance_path);
328             if(lyd_new_path(*parent, NULL, full_path, manager_context[i].instance[j].mount_point_addressing_method, 0, 0) == 0) {
329                 log_error("lyd_new_path failed\n");
330                 return SR_ERR_OPERATION_FAILED;
331             }
332
333             sprintf(full_path, "%s/is-mounted", instance_path);
334             struct lyd_node *is_mounted = lyd_new_path(*parent, NULL, full_path, manager_context[i].instance[j].is_mounted ? "true" : "false", 0, LYD_PATH_OPT_NOPARENTRET);
335             if(is_mounted == 0) {
336                 log_error("lyd_new_path failed\n");
337                 return SR_ERR_OPERATION_FAILED;
338             }
339
340             if(manager_sr_populate_networking(is_mounted->parent, &manager_context[i].instance[j]) != NTS_ERR_OK) {
341                 log_error("manager_sr_populate_networking failed\n");
342                 return SR_ERR_OPERATION_FAILED;
343             }
344         }
345     }
346
347     return SR_ERR_OK;
348 }