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 ***************************************************************************/
20 #include "nts_utils.h"
21 #include "utils/log_utils.h"
22 #include "utils/sys_utils.h"
23 #include "core/framework.h"
24 #include "core/session.h"
29 #define NTS_MANAGER_SDN_CONTROLLER_CONFIG_XPATH "/nts-manager:simulation/sdn-controller"
30 #define NTS_NF_SDN_CONTROLLER_CONFIG_XPATH "/nts-network-function:simulation/sdn-controller"
31 #define NTS_MANAGER_VES_ENDPOINT_CONFIG_XPATH "/nts-manager:simulation/ves-endpoint"
32 #define NTS_NF_VES_ENDPOINT_CONFIG_XPATH "/nts-network-function:simulation/ves-endpoint"
34 #define NTS_NETWORK_FUNCTION_FTYPE_SCHEMA_XPATH "/nts-network-function:simulation/network-function/function-type"
35 #define NTS_NETWORK_FUNCTION_MPAM_SCHEMA_XPATH "/nts-network-function:simulation/network-function/mount-point-addressing-method"
36 #define NTS_NETWORK_FUNCTION_FAULT_GENERATION_SCHEMA_XPATH "/nts-network-function:simulation/network-function/fault-generation"
37 #define NTS_NETWORK_FUNCTION_NETCONF_SCHEMA_XPATH "/nts-network-function:simulation/network-function/netconf"
38 #define NTS_NETWORK_FUNCTION_VES_SCHEMA_XPATH "/nts-network-function:simulation/network-function/ves"
41 cJSON* ves_create_common_event_header(const char *domain, const char *event_type, const char *hostname, int port, const char *priority, int seq_id) {
48 long useconds = get_microseconds_since_epoch();
50 asprintf(&eventId, "%s-%d", event_type, seq_id);
52 log_error("asprintf failed\n");
56 cJSON *common_event_header = cJSON_CreateObject();
57 if(common_event_header == 0) {
58 log_error("could not create JSON object\n");
63 if(cJSON_AddStringToObject(common_event_header, "domain", domain) == 0) {
64 log_error("cJSON AddStringToObject error\n");
66 cJSON_Delete(common_event_header);
70 if(cJSON_AddStringToObject(common_event_header, "eventId", eventId) == 0) {
71 log_error("cJSON AddStringToObject error\n");
73 cJSON_Delete(common_event_header);
79 if(cJSON_AddStringToObject(common_event_header, "eventName", event_type) == 0) {
80 log_error("cJSON AddStringToObject error\n");
81 cJSON_Delete(common_event_header);
85 if(cJSON_AddNumberToObject(common_event_header, "sequence", (double)(seq_id)) == 0) {
86 log_error("cJSON AddNumberToObject error\n");
87 cJSON_Delete(common_event_header);
91 if(cJSON_AddStringToObject(common_event_header, "priority", priority) == 0) {
92 log_error("cJSON AddStringToObject error\n");
93 cJSON_Delete(common_event_header);
97 if(cJSON_AddStringToObject(common_event_header, "reportingEntityId", "") == 0) {
98 log_error("cJSON AddStringToObject error\n");
99 cJSON_Delete(common_event_header);
103 char source_name[512];
105 sprintf(source_name, "%s-%d", hostname, port);
108 sprintf(source_name, "%s", hostname);
111 if(cJSON_AddStringToObject(common_event_header, "reportingEntityName", source_name) == 0) {
112 log_error("cJSON AddStringToObject error\n");
113 cJSON_Delete(common_event_header);
117 if(cJSON_AddStringToObject(common_event_header, "sourceId", "") == 0) {
118 log_error("cJSON AddStringToObject error\n");
119 cJSON_Delete(common_event_header);
123 if(cJSON_AddStringToObject(common_event_header, "sourceName", source_name) == 0) {
124 log_error("cJSON AddStringToObject error\n");
125 cJSON_Delete(common_event_header);
129 if(cJSON_AddNumberToObject(common_event_header, "startEpochMicrosec", (double)(useconds)) == 0) {
130 log_error("cJSON AddNumberToObject error\n");
131 cJSON_Delete(common_event_header);
135 if(cJSON_AddNumberToObject(common_event_header, "lastEpochMicrosec", (double)(useconds)) == 0) {
136 log_error("cJSON AddNumberToObject error\n");
137 cJSON_Delete(common_event_header);
141 if(cJSON_AddStringToObject(common_event_header, "nfNamingCode", "sdn controller") == 0) {
142 log_error("cJSON AddStringToObject error\n");
143 cJSON_Delete(common_event_header);
147 if(cJSON_AddStringToObject(common_event_header, "nfVendorName", "sdn") == 0) {
148 log_error("cJSON AddStringToObject error\n");
149 cJSON_Delete(common_event_header);
153 if(cJSON_AddStringToObject(common_event_header, "timeZoneOffset", "+00:00") == 0) {
154 log_error("cJSON AddStringToObject error\n");
155 cJSON_Delete(common_event_header);
159 if(cJSON_AddStringToObject(common_event_header, "version", "4.1") == 0) {
160 log_error("cJSON AddStringToObject error\n");
161 cJSON_Delete(common_event_header);
165 if(cJSON_AddStringToObject(common_event_header, "vesEventListenerVersion", framework_environment.ves_endpoint.common_header_version) == 0) {
166 log_error("cJSON AddStringToObject error\n");
167 cJSON_Delete(common_event_header);
171 return common_event_header;
174 nts_mount_point_addressing_method_t nts_mount_point_addressing_method_get(sr_session_ctx_t *current_session) {
177 nts_mount_point_addressing_method_t ret = UNKNOWN_MAPPING;
180 bool session_started = false;
181 if(current_session == 0) {
182 rc = sr_session_start(session_connection, SR_DS_RUNNING, ¤t_session);
183 if(rc != SR_ERR_OK) {
184 log_error("could not start sysrepo session\n");
187 session_started = true;
191 rc = sr_get_item(session_running, NTS_NETWORK_FUNCTION_MPAM_SCHEMA_XPATH, 0, &value);
192 if(rc == SR_ERR_OK) {
193 if(strcmp(value->data.enum_val, "host-mapping") == 0) {
197 ret = DOCKER_MAPPING;
202 if(session_started) {
203 rc = sr_session_stop(current_session);
204 if(rc != SR_ERR_OK) {
205 log_error("could not stop sysrepo session\n");
213 // checkAS authentication via certificate not supported yet
214 ves_details_t *ves_endpoint_details_get(sr_session_ctx_t *current_session) {
218 bool session_started = false;
219 if(current_session == 0) {
220 rc = sr_session_start(session_connection, SR_DS_RUNNING, ¤t_session);
221 if(rc != SR_ERR_OK) {
222 log_error("could not start sysrepo session\n");
225 session_started = true;
228 struct lyd_node *data = 0;
231 if(framework_arguments.nts_mode == NTS_MODE_MANAGER) {
232 xpath_to_get = "/nts-manager:simulation/ves-endpoint";
235 xpath_to_get = "/nts-network-function:simulation/ves-endpoint";
238 rc = sr_get_subtree(current_session, xpath_to_get, 0, &data);
239 if(rc != SR_ERR_OK) {
240 log_error("could not get value for xPath=%s from the running datastore\n", xpath_to_get);
241 if(session_started) {
242 sr_session_stop(current_session);
247 if(session_started) {
248 rc = sr_session_stop(current_session);
249 if(rc != SR_ERR_OK) {
250 log_error("could not stop sysrepo session\n");
256 if(data->child == 0) {
257 log_error("ves-endpoint probably not set yet\n", xpath_to_get);
262 ves_details_t *ret = (ves_details_t *)malloc(sizeof(ves_details_t));
264 log_error("malloc failed\n");
272 ret->auth_method = 0;
276 struct lyd_node *chd = 0;
277 LY_TREE_FOR(data->child, chd) {
278 const char *val = ((const struct lyd_node_leaf_list *)chd)->value_str;
280 if(strcmp(chd->schema->name, "ves-endpoint-protocol") == 0) {
281 ret->protocol = strdup(val);
283 else if(strcmp(chd->schema->name, "ves-endpoint-ip") == 0) {
284 ret->ip = strdup(val);
286 else if(strcmp(chd->schema->name, "ves-endpoint-port") == 0) {
287 ret->port = ((const struct lyd_node_leaf_list *)chd)->value.uint16;
289 else if(strcmp(chd->schema->name, "ves-endpoint-auth-method") == 0) {
290 ret->auth_method = strdup(val);
292 else if(strcmp(chd->schema->name, "ves-endpoint-username") == 0) {
293 ret->username = strdup(val);
295 else if(strcmp(chd->schema->name, "ves-endpoint-password") == 0) {
296 ret->password = strdup(val);
301 if (strstr(ret->ip, ":")) {
303 asprintf(&ret->url, "%s://[%s]:%d/eventListener/v7", ret->protocol, ret->ip, ret->port);
306 asprintf(&ret->url, "%s://%s:%d/eventListener/v7", ret->protocol, ret->ip, ret->port);
309 if((ret->protocol == 0) || (ret->ip == 0) || (ret->auth_method == 0) || (ret->username == 0) || (ret->password == 0) || (ret->url == 0)) {
312 free(ret->auth_method);
323 void ves_details_free(ves_details_t *instance) {
326 free(instance->protocol);
329 free(instance->auth_method);
330 free(instance->username);
331 free(instance->password);
336 // checkAS authentication via certificate not supported yet
337 controller_details_t *controller_details_get(sr_session_ctx_t *current_session) {
341 bool session_started = false;
342 if(current_session == 0) {
343 rc = sr_session_start(session_connection, SR_DS_RUNNING, ¤t_session);
344 if(rc != SR_ERR_OK) {
345 log_error("could not start sysrepo session\n");
348 session_started = true;
351 struct lyd_node *data = 0;
354 if(framework_arguments.nts_mode == NTS_MODE_MANAGER) {
355 xpath_to_get = "/nts-manager:simulation/sdn-controller";
358 xpath_to_get = "/nts-network-function:simulation/sdn-controller";
361 rc = sr_get_subtree(current_session, xpath_to_get, 0, &data);
362 if(rc != SR_ERR_OK) {
363 log_error("could not get value for xPath=%s from the running datastore\n", xpath_to_get);
364 if(session_started) {
365 sr_session_stop(current_session);
370 if(session_started) {
371 rc = sr_session_stop(current_session);
372 if(rc != SR_ERR_OK) {
373 log_error("could not stop sysrepo session\n");
379 if(data->child == 0) {
380 log_error("sdn-controller probably not set yet\n");
385 controller_details_t *ret = (controller_details_t *)malloc(sizeof(controller_details_t));
387 log_error("malloc failed\n");
395 ret->nc_callhome_port = 0;
396 ret->auth_method = 0;
400 ret->auth_method = strdup("basic");
402 struct lyd_node *chd = 0;
403 LY_TREE_FOR(data->child, chd) {
404 const char *val = ((const struct lyd_node_leaf_list *)chd)->value_str;
406 if(strcmp(chd->schema->name, "controller-protocol") == 0) {
407 ret->protocol = strdup(val);
409 else if(strcmp(chd->schema->name, "controller-ip") == 0) {
410 ret->ip = strdup(val);
412 else if(strcmp(chd->schema->name, "controller-port") == 0) {
413 ret->port = ((const struct lyd_node_leaf_list *)chd)->value.uint16;
415 else if(strcmp(chd->schema->name, "controller-netconf-call-home-port") == 0) {
416 ret->nc_callhome_port = ((const struct lyd_node_leaf_list *)chd)->value.uint16;
418 else if(strcmp(chd->schema->name, "controller-username") == 0) {
419 ret->username = strdup(val);
421 else if(strcmp(chd->schema->name, "controller-password") == 0) {
422 ret->password = strdup(val);
427 if (strstr(ret->ip, ":")) {
429 asprintf(&ret->base_url, "%s://[%s]:%d", ret->protocol, ret->ip, ret->port);
432 asprintf(&ret->base_url, "%s://%s:%d", ret->protocol, ret->ip, ret->port);
435 if((ret->protocol == 0) || (ret->ip == 0) || (ret->auth_method == 0) || (ret->username == 0) || (ret->password == 0) || (ret->base_url == 0)) {
438 free(ret->auth_method);
449 void controller_details_free(controller_details_t *instance) {
452 free(instance->protocol);
454 free(instance->base_url);
455 free(instance->auth_method);
456 free(instance->username);
457 free(instance->password);
461 int nts_vercmp(const char *ver1, const char *ver2) {
466 int v1 = 0, v2 = 0, v3 = 0;
467 while(ver1[i] && (ver1[i] != '.')) {
475 while(ver1[i] && (ver1[i] != '.')) {
483 while(ver1[i] && (ver1[i] != '.')) {
492 int V1 = 0, V2 = 0, V3 = 0;
494 while(ver2[i] && (ver2[i] != '.')) {
502 while(ver2[i] && (ver2[i] != '.')) {
510 while(ver2[i] && (ver2[i] != '.')) {
542 int nts_utils_populate_info(sr_session_ctx_t *current_session, const char *function_type) {
543 assert(current_session);
544 assert(function_type);
546 bool manager = (strcmp(function_type, "NTS_FUNCTION_TYPE_MANAGER") == 0);
550 //setup sdn-controller defaults
551 if(strlen(framework_environment.sdn_controller.protocol)) {
553 rc = sr_set_item_str(current_session, NTS_MANAGER_SDN_CONTROLLER_CONFIG_XPATH"/controller-protocol", (const char*)framework_environment.sdn_controller.protocol, 0, 0);
556 rc = sr_set_item_str(current_session, NTS_NF_SDN_CONTROLLER_CONFIG_XPATH"/controller-protocol", (const char*)framework_environment.sdn_controller.protocol, 0, 0);
558 if(rc != SR_ERR_OK) {
559 log_error("sr_set_item_str failed\n");
560 return NTS_ERR_FAILED;
564 if(strlen(framework_environment.sdn_controller.ip)) {
566 rc = sr_set_item_str(current_session, NTS_MANAGER_SDN_CONTROLLER_CONFIG_XPATH"/controller-ip", (const char*)framework_environment.sdn_controller.ip, 0, 0);
569 rc = sr_set_item_str(current_session, NTS_NF_SDN_CONTROLLER_CONFIG_XPATH"/controller-ip", (const char*)framework_environment.sdn_controller.ip, 0, 0);
571 if(rc != SR_ERR_OK) {
572 log_error("sr_set_item_str failed\n");
573 return NTS_ERR_FAILED;
577 sprintf(int_to_str, "%d", framework_environment.sdn_controller.port);
579 rc = sr_set_item_str(current_session, NTS_MANAGER_SDN_CONTROLLER_CONFIG_XPATH"/controller-port", (const char*)int_to_str, 0, 0);
582 rc = sr_set_item_str(current_session, NTS_NF_SDN_CONTROLLER_CONFIG_XPATH"/controller-port", (const char*)int_to_str, 0, 0);
584 if(rc != SR_ERR_OK) {
585 log_error("sr_set_item_str failed\n");
586 return NTS_ERR_FAILED;
589 sprintf(int_to_str, "%d", framework_environment.sdn_controller.callhome_port);
591 rc = sr_set_item_str(current_session, NTS_MANAGER_SDN_CONTROLLER_CONFIG_XPATH"/controller-netconf-call-home-port", (const char*)int_to_str, 0, 0);
594 rc = sr_set_item_str(current_session, NTS_NF_SDN_CONTROLLER_CONFIG_XPATH"/controller-netconf-call-home-port", (const char*)int_to_str, 0, 0);
596 if(rc != SR_ERR_OK) {
597 log_error("sr_set_item_str failed\n");
598 return NTS_ERR_FAILED;
601 if(strlen(framework_environment.sdn_controller.username)) {
603 rc = sr_set_item_str(current_session, NTS_MANAGER_SDN_CONTROLLER_CONFIG_XPATH"/controller-username", (const char*)framework_environment.sdn_controller.username, 0, 0);
606 rc = sr_set_item_str(current_session, NTS_NF_SDN_CONTROLLER_CONFIG_XPATH"/controller-username", (const char*)framework_environment.sdn_controller.username, 0, 0);
608 if(rc != SR_ERR_OK) {
609 log_error("sr_set_item_str failed\n");
610 return NTS_ERR_FAILED;
614 if(strlen(framework_environment.sdn_controller.password)) {
616 rc = sr_set_item_str(current_session, NTS_MANAGER_SDN_CONTROLLER_CONFIG_XPATH"/controller-password", (const char*)framework_environment.sdn_controller.password, 0, 0);
619 rc = sr_set_item_str(current_session, NTS_NF_SDN_CONTROLLER_CONFIG_XPATH"/controller-password", (const char*)framework_environment.sdn_controller.password, 0, 0);
621 if(rc != SR_ERR_OK) {
622 log_error("sr_set_item_str failed\n");
623 return NTS_ERR_FAILED;
627 //setup ves-endpoint details
628 if(strlen(framework_environment.ves_endpoint.protocol)) {
630 rc = sr_set_item_str(current_session, NTS_MANAGER_VES_ENDPOINT_CONFIG_XPATH"/ves-endpoint-protocol", (const char*)framework_environment.ves_endpoint.protocol, 0, 0);
633 rc = sr_set_item_str(current_session, NTS_NF_VES_ENDPOINT_CONFIG_XPATH"/ves-endpoint-protocol", (const char*)framework_environment.ves_endpoint.protocol, 0, 0);
635 if(rc != SR_ERR_OK) {
636 log_error("sr_set_item_str failed\n");
637 return NTS_ERR_FAILED;
641 if(strlen(framework_environment.ves_endpoint.ip)) {
643 rc = sr_set_item_str(current_session, NTS_MANAGER_VES_ENDPOINT_CONFIG_XPATH"/ves-endpoint-ip", (const char*)framework_environment.ves_endpoint.ip, 0, 0);
646 rc = sr_set_item_str(current_session, NTS_NF_VES_ENDPOINT_CONFIG_XPATH"/ves-endpoint-ip", (const char*)framework_environment.ves_endpoint.ip, 0, 0);
648 if(rc != SR_ERR_OK) {
649 log_error("sr_set_item_str failed\n");
650 return NTS_ERR_FAILED;
654 sprintf(int_to_str, "%d", framework_environment.ves_endpoint.port);
656 rc = sr_set_item_str(current_session, NTS_MANAGER_VES_ENDPOINT_CONFIG_XPATH"/ves-endpoint-port", (const char*)int_to_str, 0, 0);
659 rc = sr_set_item_str(current_session, NTS_NF_VES_ENDPOINT_CONFIG_XPATH"/ves-endpoint-port", (const char*)int_to_str, 0, 0);
661 if(rc != SR_ERR_OK) {
662 log_error("sr_set_item_str failed\n");
663 return NTS_ERR_FAILED;
666 if(strlen(framework_environment.ves_endpoint.auth_method)) {
668 rc = sr_set_item_str(current_session, NTS_MANAGER_VES_ENDPOINT_CONFIG_XPATH"/ves-endpoint-auth-method", (const char*)framework_environment.ves_endpoint.auth_method, 0, 0);
671 rc = sr_set_item_str(current_session, NTS_NF_VES_ENDPOINT_CONFIG_XPATH"/ves-endpoint-auth-method", (const char*)framework_environment.ves_endpoint.auth_method, 0, 0);
673 if(rc != SR_ERR_OK) {
674 log_error("sr_set_item_str failed\n");
675 return NTS_ERR_FAILED;
679 if(strlen(framework_environment.ves_endpoint.username)) {
681 rc = sr_set_item_str(current_session, NTS_MANAGER_VES_ENDPOINT_CONFIG_XPATH"/ves-endpoint-username", (const char*)framework_environment.ves_endpoint.username, 0, 0);
684 rc = sr_set_item_str(current_session, NTS_NF_VES_ENDPOINT_CONFIG_XPATH"/ves-endpoint-username", (const char*)framework_environment.ves_endpoint.username, 0, 0);
686 if(rc != SR_ERR_OK) {
687 log_error("sr_set_item_str failed\n");
688 return NTS_ERR_FAILED;
692 if(strlen(framework_environment.ves_endpoint.password)) {
694 rc = sr_set_item_str(current_session, NTS_MANAGER_VES_ENDPOINT_CONFIG_XPATH"/ves-endpoint-password", (const char*)framework_environment.ves_endpoint.password, 0, 0);
697 rc = sr_set_item_str(current_session, NTS_NF_VES_ENDPOINT_CONFIG_XPATH"/ves-endpoint-password", (const char*)framework_environment.ves_endpoint.password, 0, 0);
699 if(rc != SR_ERR_OK) {
700 log_error("sr_set_item_str failed\n");
701 return NTS_ERR_FAILED;
705 if(strlen(framework_environment.ves_endpoint.certificate)) {
707 rc = sr_set_item_str(current_session, NTS_MANAGER_VES_ENDPOINT_CONFIG_XPATH"/ves-endpoint-certificate", (const char*)framework_environment.ves_endpoint.certificate, 0, 0);
710 rc = sr_set_item_str(current_session, NTS_NF_VES_ENDPOINT_CONFIG_XPATH"/ves-endpoint-certificate", (const char*)framework_environment.ves_endpoint.certificate, 0, 0);
712 if(rc != SR_ERR_OK) {
713 log_error("sr_set_item_str failed\n");
714 return NTS_ERR_FAILED;
718 if(manager == false) {
719 //presence containers
720 rc = sr_set_item_str(current_session, NTS_NETWORK_FUNCTION_FAULT_GENERATION_SCHEMA_XPATH, 0, 0, 0);
721 if(rc != SR_ERR_OK) {
722 log_error("sr_set_item_str failed\n");
723 return NTS_ERR_FAILED;
726 rc = sr_set_item_str(current_session, NTS_NETWORK_FUNCTION_NETCONF_SCHEMA_XPATH, 0, 0, 0);
727 if(rc != SR_ERR_OK) {
728 log_error("sr_set_item_str failed\n");
729 return NTS_ERR_FAILED;
732 rc = sr_set_item_str(current_session, NTS_NETWORK_FUNCTION_VES_SCHEMA_XPATH, 0, 0, 0);
733 if(rc != SR_ERR_OK) {
734 log_error("sr_set_item_str failed\n");
735 return NTS_ERR_FAILED;
739 //also set the network-function module for easy identifying the function type
740 rc = sr_set_item_str(current_session, NTS_NETWORK_FUNCTION_FTYPE_SCHEMA_XPATH, function_type, 0, 0);
741 if(rc != SR_ERR_OK) {
742 log_error("sr_set_item_str failed\n");
743 return NTS_ERR_FAILED;
746 //mount-point-addressing-method
747 rc = sr_set_item_str(current_session, NTS_NETWORK_FUNCTION_MPAM_SCHEMA_XPATH, "docker-mapping", 0, 0);
748 if(rc != SR_ERR_OK) {
749 log_error("sr_set_item_str failed\n");
750 return NTS_ERR_FAILED;
754 rc = sr_apply_changes(current_session, 0, 0);
755 if(rc != SR_ERR_OK) {
756 log_error("sr_apply_changes failed: %s\n", sr_strerror(rc));
757 return NTS_ERR_FAILED;