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"
25 #include "core/xpath.h"
31 cJSON* ves_create_common_event_header(const char *domain, const char *event_type, const char *hostname, int port, const char *priority, int seq_id) {
38 long useconds = get_microseconds_since_epoch();
40 asprintf(&eventId, "%s-%d", event_type, seq_id);
42 log_error("asprintf failed\n");
46 cJSON *common_event_header = cJSON_CreateObject();
47 if(common_event_header == 0) {
48 log_error("could not create JSON object\n");
53 if(cJSON_AddStringToObject(common_event_header, "domain", domain) == 0) {
54 log_error("cJSON AddStringToObject error\n");
56 cJSON_Delete(common_event_header);
60 if(cJSON_AddStringToObject(common_event_header, "eventId", eventId) == 0) {
61 log_error("cJSON AddStringToObject error\n");
63 cJSON_Delete(common_event_header);
69 if(cJSON_AddStringToObject(common_event_header, "eventName", event_type) == 0) {
70 log_error("cJSON AddStringToObject error\n");
71 cJSON_Delete(common_event_header);
75 if(cJSON_AddNumberToObject(common_event_header, "sequence", (double)(seq_id)) == 0) {
76 log_error("cJSON AddNumberToObject error\n");
77 cJSON_Delete(common_event_header);
81 if(cJSON_AddStringToObject(common_event_header, "priority", priority) == 0) {
82 log_error("cJSON AddStringToObject error\n");
83 cJSON_Delete(common_event_header);
87 if(cJSON_AddStringToObject(common_event_header, "reportingEntityId", "") == 0) {
88 log_error("cJSON AddStringToObject error\n");
89 cJSON_Delete(common_event_header);
93 char source_name[512];
95 sprintf(source_name, "%s-%d", hostname, port);
98 sprintf(source_name, "%s", hostname);
101 if(cJSON_AddStringToObject(common_event_header, "reportingEntityName", source_name) == 0) {
102 log_error("cJSON AddStringToObject error\n");
103 cJSON_Delete(common_event_header);
107 if(cJSON_AddStringToObject(common_event_header, "sourceId", "") == 0) {
108 log_error("cJSON AddStringToObject error\n");
109 cJSON_Delete(common_event_header);
113 if(cJSON_AddStringToObject(common_event_header, "sourceName", source_name) == 0) {
114 log_error("cJSON AddStringToObject error\n");
115 cJSON_Delete(common_event_header);
119 if(cJSON_AddNumberToObject(common_event_header, "startEpochMicrosec", (double)(useconds)) == 0) {
120 log_error("cJSON AddNumberToObject error\n");
121 cJSON_Delete(common_event_header);
125 if(cJSON_AddNumberToObject(common_event_header, "lastEpochMicrosec", (double)(useconds)) == 0) {
126 log_error("cJSON AddNumberToObject error\n");
127 cJSON_Delete(common_event_header);
131 if(cJSON_AddStringToObject(common_event_header, "nfNamingCode", "sdn controller") == 0) {
132 log_error("cJSON AddStringToObject error\n");
133 cJSON_Delete(common_event_header);
137 if(cJSON_AddStringToObject(common_event_header, "nfVendorName", "sdn") == 0) {
138 log_error("cJSON AddStringToObject error\n");
139 cJSON_Delete(common_event_header);
143 if(cJSON_AddStringToObject(common_event_header, "timeZoneOffset", "+00:00") == 0) {
144 log_error("cJSON AddStringToObject error\n");
145 cJSON_Delete(common_event_header);
149 if(cJSON_AddStringToObject(common_event_header, "version", "4.1") == 0) {
150 log_error("cJSON AddStringToObject error\n");
151 cJSON_Delete(common_event_header);
155 if(cJSON_AddStringToObject(common_event_header, "vesEventListenerVersion", framework_environment.ves_endpoint.common_header_version) == 0) {
156 log_error("cJSON AddStringToObject error\n");
157 cJSON_Delete(common_event_header);
161 return common_event_header;
164 nts_mount_point_addressing_method_t nts_mount_point_addressing_method_get(sr_session_ctx_t *current_session) {
167 nts_mount_point_addressing_method_t ret = UNKNOWN_MAPPING;
170 bool session_started = false;
171 if(current_session == 0) {
172 rc = sr_session_start(session_connection, SR_DS_RUNNING, ¤t_session);
173 if(rc != SR_ERR_OK) {
174 log_error("could not start sysrepo session\n");
177 session_started = true;
181 rc = sr_get_item(current_session, NTS_NF_NETWORK_FUNCTION_MPAM_SCHEMA_XPATH, 0, &value);
182 if(rc == SR_ERR_OK) {
183 if(strcmp(value->data.enum_val, "host-mapping") == 0) {
187 ret = DOCKER_MAPPING;
192 if(session_started) {
193 rc = sr_session_stop(current_session);
194 if(rc != SR_ERR_OK) {
195 log_error("could not stop sysrepo session\n");
203 // checkAS authentication via certificate not supported yet
204 ves_details_t *ves_endpoint_details_get(sr_session_ctx_t *current_session, const char *custom_path) {
208 bool session_started = false;
209 if(current_session == 0) {
210 rc = sr_session_start(session_connection, SR_DS_RUNNING, ¤t_session);
211 if(rc != SR_ERR_OK) {
212 log_error("could not start sysrepo session\n");
215 session_started = true;
218 struct lyd_node *data = 0;
221 if(custom_path == 0) {
222 if(framework_arguments.nts_mode == NTS_MODE_MANAGER) {
223 xpath_to_get = NTS_MANAGER_VES_ENDPOINT_CONFIG_XPATH;
226 xpath_to_get = NTS_NF_VES_ENDPOINT_CONFIG_XPATH;
230 xpath_to_get = (char *)custom_path;
233 rc = sr_get_subtree(current_session, xpath_to_get, 0, &data);
234 if(rc != SR_ERR_OK) {
235 log_error("could not get value for xPath=%s from the running datastore\n", xpath_to_get);
236 if(session_started) {
237 sr_session_stop(current_session);
242 if(session_started) {
243 rc = sr_session_stop(current_session);
244 if(rc != SR_ERR_OK) {
245 log_error("could not stop sysrepo session\n");
251 if(data->child == 0) {
252 log_error("ves-endpoint probably not set yet\n", xpath_to_get);
257 ves_details_t *ret = (ves_details_t *)malloc(sizeof(ves_details_t));
259 log_error("malloc failed\n");
267 ret->auth_method = 0;
271 struct lyd_node *chd = 0;
272 LY_TREE_FOR(data->child, chd) {
273 const char *val = ((const struct lyd_node_leaf_list *)chd)->value_str;
275 if(strcmp(chd->schema->name, "ves-endpoint-protocol") == 0) {
276 ret->protocol = strdup(val);
278 else if(strcmp(chd->schema->name, "ves-endpoint-ip") == 0) {
279 ret->ip = strdup(val);
281 else if(strcmp(chd->schema->name, "ves-endpoint-port") == 0) {
282 ret->port = ((const struct lyd_node_leaf_list *)chd)->value.uint16;
284 else if(strcmp(chd->schema->name, "ves-endpoint-auth-method") == 0) {
285 ret->auth_method = strdup(val);
287 else if(strcmp(chd->schema->name, "ves-endpoint-username") == 0) {
288 ret->username = strdup(val);
290 else if(strcmp(chd->schema->name, "ves-endpoint-password") == 0) {
291 ret->password = strdup(val);
296 if (strstr(ret->ip, ":")) {
298 asprintf(&ret->url, "%s://[%s]:%d/eventListener/v7", ret->protocol, ret->ip, ret->port);
301 if (framework_environment.ves_endpoint.port_absent == true) {
302 // hostname addressing with port missing
303 asprintf(&ret->url, "%s://%s/eventListener/v7", ret->protocol, ret->ip);
306 // normal addressing with IP and Port
307 asprintf(&ret->url, "%s://%s:%d/eventListener/v7", ret->protocol, ret->ip, ret->port);
312 if((ret->protocol == 0) || (ret->ip == 0) || (ret->auth_method == 0) || (ret->username == 0) || (ret->password == 0) || (ret->url == 0)) {
315 free(ret->auth_method);
326 void ves_details_free(ves_details_t *instance) {
329 free(instance->protocol);
332 free(instance->auth_method);
333 free(instance->username);
334 free(instance->password);
339 // checkAS authentication via certificate not supported yet
340 controller_details_t *controller_details_get(sr_session_ctx_t *current_session) {
344 bool session_started = false;
345 if(current_session == 0) {
346 rc = sr_session_start(session_connection, SR_DS_RUNNING, ¤t_session);
347 if(rc != SR_ERR_OK) {
348 log_error("could not start sysrepo session\n");
351 session_started = true;
354 struct lyd_node *data = 0;
357 if(framework_arguments.nts_mode == NTS_MODE_MANAGER) {
358 xpath_to_get = NTS_MANAGER_SDN_CONTROLLER_CONFIG_XPATH;
361 xpath_to_get = NTS_NF_SDN_CONTROLLER_CONFIG_XPATH;
364 rc = sr_get_subtree(current_session, xpath_to_get, 0, &data);
365 if(rc != SR_ERR_OK) {
366 log_error("could not get value for xPath=%s from the running datastore\n", xpath_to_get);
367 if(session_started) {
368 sr_session_stop(current_session);
373 if(session_started) {
374 rc = sr_session_stop(current_session);
375 if(rc != SR_ERR_OK) {
376 log_error("could not stop sysrepo session\n");
382 if(data->child == 0) {
383 log_error("sdn-controller probably not set yet\n");
388 controller_details_t *ret = (controller_details_t *)malloc(sizeof(controller_details_t));
390 log_error("malloc failed\n");
398 ret->nc_callhome_ip = 0;
399 ret->nc_callhome_port = 0;
400 ret->auth_method = 0;
404 ret->auth_method = strdup("basic");
406 struct lyd_node *chd = 0;
407 LY_TREE_FOR(data->child, chd) {
408 const char *val = ((const struct lyd_node_leaf_list *)chd)->value_str;
410 if(strcmp(chd->schema->name, "controller-protocol") == 0) {
411 ret->protocol = strdup(val);
413 else if(strcmp(chd->schema->name, "controller-ip") == 0) {
414 ret->ip = strdup(val);
416 else if(strcmp(chd->schema->name, "controller-port") == 0) {
417 ret->port = ((const struct lyd_node_leaf_list *)chd)->value.uint16;
419 else if(strcmp(chd->schema->name, "controller-netconf-call-home-ip") == 0) {
420 ret->nc_callhome_ip = strdup(val);
422 else if(strcmp(chd->schema->name, "controller-netconf-call-home-port") == 0) {
423 ret->nc_callhome_port = ((const struct lyd_node_leaf_list *)chd)->value.uint16;
425 else if(strcmp(chd->schema->name, "controller-username") == 0) {
426 ret->username = strdup(val);
428 else if(strcmp(chd->schema->name, "controller-password") == 0) {
429 ret->password = strdup(val);
434 if (strstr(ret->ip, ":")) {
436 asprintf(&ret->base_url, "%s://[%s]:%d", ret->protocol, ret->ip, ret->port);
439 if (framework_environment.sdn_controller.port_absent == true) {
440 // hostname without port addressing
441 asprintf(&ret->base_url, "%s://%s", ret->protocol, ret->ip);
444 // normal IP and Port addressing
445 asprintf(&ret->base_url, "%s://%s:%d", ret->protocol, ret->ip, ret->port);
449 if((ret->protocol == 0) || (ret->ip == 0) || (ret->nc_callhome_ip == 0) || (ret->auth_method == 0) || (ret->username == 0) || (ret->password == 0) || (ret->base_url == 0)) {
452 free(ret->nc_callhome_ip);
453 free(ret->auth_method);
464 void controller_details_free(controller_details_t *instance) {
467 free(instance->protocol);
469 free(instance->nc_callhome_ip);
470 free(instance->base_url);
471 free(instance->auth_method);
472 free(instance->username);
473 free(instance->password);
477 int nts_vercmp(const char *ver1, const char *ver2) {
482 int v1 = 0, v2 = 0, v3 = 0;
483 while(ver1[i] && (ver1[i] != '.')) {
491 while(ver1[i] && (ver1[i] != '.')) {
499 while(ver1[i] && (ver1[i] != '.')) {
508 int V1 = 0, V2 = 0, V3 = 0;
510 while(ver2[i] && (ver2[i] != '.')) {
518 while(ver2[i] && (ver2[i] != '.')) {
526 while(ver2[i] && (ver2[i] != '.')) {
558 int nts_utils_populate_info(sr_session_ctx_t *current_session, const char *function_type) {
559 assert(current_session);
560 assert(function_type);
562 bool manager = (strcmp(function_type, "NTS_FUNCTION_TYPE_MANAGER") == 0);
566 //setup sdn-controller defaults
567 if(strlen(framework_environment.sdn_controller.protocol)) {
569 rc = sr_set_item_str(current_session, NTS_MANAGER_SDN_CONTROLLER_CONFIG_XPATH"/controller-protocol", (const char*)framework_environment.sdn_controller.protocol, 0, 0);
572 rc = sr_set_item_str(current_session, NTS_NF_SDN_CONTROLLER_CONFIG_XPATH"/controller-protocol", (const char*)framework_environment.sdn_controller.protocol, 0, 0);
574 if(rc != SR_ERR_OK) {
575 log_error("sr_set_item_str failed\n");
576 return NTS_ERR_FAILED;
580 if(strlen(framework_environment.sdn_controller.ip)) {
582 rc = sr_set_item_str(current_session, NTS_MANAGER_SDN_CONTROLLER_CONFIG_XPATH"/controller-ip", (const char*)framework_environment.sdn_controller.ip, 0, 0);
585 rc = sr_set_item_str(current_session, NTS_NF_SDN_CONTROLLER_CONFIG_XPATH"/controller-ip", (const char*)framework_environment.sdn_controller.ip, 0, 0);
587 if(rc != SR_ERR_OK) {
588 log_error("sr_set_item_str failed\n");
589 return NTS_ERR_FAILED;
593 sprintf(int_to_str, "%d", framework_environment.sdn_controller.port);
595 rc = sr_set_item_str(current_session, NTS_MANAGER_SDN_CONTROLLER_CONFIG_XPATH"/controller-port", (const char*)int_to_str, 0, 0);
598 rc = sr_set_item_str(current_session, NTS_NF_SDN_CONTROLLER_CONFIG_XPATH"/controller-port", (const char*)int_to_str, 0, 0);
600 if(rc != SR_ERR_OK) {
601 log_error("sr_set_item_str failed\n");
602 return NTS_ERR_FAILED;
605 if(strlen(framework_environment.sdn_controller.callhome_ip)) {
607 rc = sr_set_item_str(current_session, NTS_MANAGER_SDN_CONTROLLER_CONFIG_XPATH"/controller-netconf-call-home-ip", (const char*)framework_environment.sdn_controller.callhome_ip, 0, 0);
610 rc = sr_set_item_str(current_session, NTS_NF_SDN_CONTROLLER_CONFIG_XPATH"/controller-netconf-call-home-ip", (const char*)framework_environment.sdn_controller.callhome_ip, 0, 0);
612 if(rc != SR_ERR_OK) {
613 log_error("sr_set_item_str failed\n");
614 return NTS_ERR_FAILED;
618 sprintf(int_to_str, "%d", framework_environment.sdn_controller.callhome_port);
620 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);
623 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);
625 if(rc != SR_ERR_OK) {
626 log_error("sr_set_item_str failed\n");
627 return NTS_ERR_FAILED;
630 if(strlen(framework_environment.sdn_controller.username)) {
632 rc = sr_set_item_str(current_session, NTS_MANAGER_SDN_CONTROLLER_CONFIG_XPATH"/controller-username", (const char*)framework_environment.sdn_controller.username, 0, 0);
635 rc = sr_set_item_str(current_session, NTS_NF_SDN_CONTROLLER_CONFIG_XPATH"/controller-username", (const char*)framework_environment.sdn_controller.username, 0, 0);
637 if(rc != SR_ERR_OK) {
638 log_error("sr_set_item_str failed\n");
639 return NTS_ERR_FAILED;
643 if(strlen(framework_environment.sdn_controller.password)) {
645 rc = sr_set_item_str(current_session, NTS_MANAGER_SDN_CONTROLLER_CONFIG_XPATH"/controller-password", (const char*)framework_environment.sdn_controller.password, 0, 0);
648 rc = sr_set_item_str(current_session, NTS_NF_SDN_CONTROLLER_CONFIG_XPATH"/controller-password", (const char*)framework_environment.sdn_controller.password, 0, 0);
650 if(rc != SR_ERR_OK) {
651 log_error("sr_set_item_str failed\n");
652 return NTS_ERR_FAILED;
656 //setup ves-endpoint details
657 if(strlen(framework_environment.ves_endpoint.protocol)) {
659 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);
662 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);
664 if(rc != SR_ERR_OK) {
665 log_error("sr_set_item_str failed\n");
666 return NTS_ERR_FAILED;
670 if(strlen(framework_environment.ves_endpoint.ip)) {
672 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);
675 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);
677 if(rc != SR_ERR_OK) {
678 log_error("sr_set_item_str failed\n");
679 return NTS_ERR_FAILED;
683 sprintf(int_to_str, "%d", framework_environment.ves_endpoint.port);
685 rc = sr_set_item_str(current_session, NTS_MANAGER_VES_ENDPOINT_CONFIG_XPATH"/ves-endpoint-port", (const char*)int_to_str, 0, 0);
688 rc = sr_set_item_str(current_session, NTS_NF_VES_ENDPOINT_CONFIG_XPATH"/ves-endpoint-port", (const char*)int_to_str, 0, 0);
690 if(rc != SR_ERR_OK) {
691 log_error("sr_set_item_str failed\n");
692 return NTS_ERR_FAILED;
695 if(strlen(framework_environment.ves_endpoint.auth_method)) {
697 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);
700 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);
702 if(rc != SR_ERR_OK) {
703 log_error("sr_set_item_str failed\n");
704 return NTS_ERR_FAILED;
708 if(strlen(framework_environment.ves_endpoint.username)) {
710 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);
713 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);
715 if(rc != SR_ERR_OK) {
716 log_error("sr_set_item_str failed\n");
717 return NTS_ERR_FAILED;
721 if(strlen(framework_environment.ves_endpoint.password)) {
723 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);
726 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);
728 if(rc != SR_ERR_OK) {
729 log_error("sr_set_item_str failed\n");
730 return NTS_ERR_FAILED;
734 if(strlen(framework_environment.ves_endpoint.certificate)) {
736 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);
739 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);
741 if(rc != SR_ERR_OK) {
742 log_error("sr_set_item_str failed\n");
743 return NTS_ERR_FAILED;
747 if(manager == false) {
748 //presence containers
749 rc = sr_set_item_str(current_session, NTS_NF_FAULT_GENERATION_SCHEMA_XPATH, 0, 0, 0);
750 if(rc != SR_ERR_OK) {
751 log_error("sr_set_item_str failed\n");
752 return NTS_ERR_FAILED;
755 rc = sr_set_item_str(current_session, NTS_NF_NETCONF_SCHEMA_XPATH, 0, 0, 0);
756 if(rc != SR_ERR_OK) {
757 log_error("sr_set_item_str failed\n");
758 return NTS_ERR_FAILED;
761 rc = sr_set_item_str(current_session, NTS_NF_VES_SCHEMA_XPATH, 0, 0, 0);
762 if(rc != SR_ERR_OK) {
763 log_error("sr_set_item_str failed\n");
764 return NTS_ERR_FAILED;
768 //also set the network-function module for easy identifying the function type
769 rc = sr_set_item_str(current_session, NTS_NF_NETWORK_FUNCTION_FTYPE_SCHEMA_XPATH, function_type, 0, 0);
770 if(rc != SR_ERR_OK) {
771 log_error("sr_set_item_str failed\n");
772 return NTS_ERR_FAILED;
775 //mount-point-addressing-method
776 rc = sr_set_item_str(current_session, NTS_NF_NETWORK_FUNCTION_MPAM_SCHEMA_XPATH, framework_environment.nts.nf_mount_point_addressing_method, 0, 0);
777 if(rc != SR_ERR_OK) {
778 log_error("sr_set_item_str failed\n");
779 return NTS_ERR_FAILED;
783 rc = sr_apply_changes(current_session, 0, 0);
784 if(rc != SR_ERR_OK) {
785 log_error("sr_apply_changes failed: %s\n", sr_strerror(rc));
786 return NTS_ERR_FAILED;