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/sys_utils.h"
27 #include <sysrepo/values.h>
29 #include "core/framework.h"
30 #include "core/session.h"
31 #include "core/context.h"
33 #include "app_common.h"
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);
38 int manager_run(void) {
41 log_add_verbose(1, LOG_COLOR_BOLD_YELLOW"starting MANAGER...\n"LOG_COLOR_RESET);
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;
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;
57 rc = manager_operations_init();
58 if(rc != NTS_ERR_OK) {
59 log_error("manager_operations_init failed\n");
60 return NTS_ERR_FAILED;
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);
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);
75 log_error("could not subscribe to simulation changes\n");
76 return NTS_ERR_FAILED;
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);
82 log_error("could not subscribe to oper faults\n");
83 return NTS_ERR_FAILED;
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);
89 log_error("could not subscribe to oper faults\n");
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;
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);
106 log_add(1, "NETCONF SSH: disabled");
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);
112 log_add(1, " | NETCONF TLS: disabled");
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);
118 log_add(1, " | FTP: disabled");
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);
124 log_add(1, " | SFTP: disabled");
127 log_add_verbose(1, LOG_COLOR_BOLD_GREEN"started!\n"LOG_COLOR_RESET);
130 while(!framework_sigint) {
131 manager_operations_loop(); //caution - this function time-waits (1sec) on manager_operation_sem
134 manager_operations_free();
135 manager_context_free();
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;
147 if(manager_sr_get_context_sync()) {
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;
158 manager_operation_t *new_oper = 0;
159 while((rc = sr_get_change_next(session, it, &oper, &old_value, &new_value)) == SR_ERR_OK) {
161 //get function type and index
162 char *nv = sr_val_to_str(new_value);
164 char function_type[512];
165 strncpy(function_type, strstr(new_value->xpath, "function-type='") + 15, 510);
166 *strchr(function_type, '\'') = 0;
169 if((new_oper == 0) || (strcmp(new_oper->function_type, function_type) != 0)) {
172 manager_operations_begin();
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;
182 manager_operations_add(new_oper);
185 new_oper = manager_operations_new_oper(MANAGER_OPERATION_EDIT);
186 new_oper->function_type = strdup(function_type);
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;
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;
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;
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;
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);
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);
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);
234 else if(strcmp(leaf_path, "mount-point-addressing-method") == 0) {
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);
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;
250 sr_free_val(old_value);
251 sr_free_val(new_value);
254 sr_free_change_iter(it);
256 //validate and add the operation, if any; can be 0 if no modifications to NF list
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;
264 manager_operations_add(new_oper);
267 else if(event == SR_EV_CHANGE) {
269 else if(event == SR_EV_DONE) {
270 bool global_change = false;
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;
279 while((rc = sr_get_change_next(session, it, &oper, &old_value, &new_value)) == SR_ERR_OK) {
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);
289 sr_free_val(old_value);
290 sr_free_val(new_value);
293 sr_free_change_iter(it);
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;
304 manager_operations_finish_and_execute(); //from this point on, manager_operations_loop will take over
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) {
312 *parent = lyd_new_path(NULL, sr_get_context(sr_session_get_connection(session)), NTS_FUNCTION_LIST_SCHEMA_XPATH, 0, 0, 0);
314 log_error("lyd_new_path failed\n");
315 return SR_ERR_OPERATION_FAILED;
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);
325 char full_path[2048];
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;
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;
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;