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 *source_name, 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 if(cJSON_AddStringToObject(common_event_header, "reportingEntityName", source_name) == 0) {
104 log_error("cJSON AddStringToObject error\n");
105 cJSON_Delete(common_event_header);
109 if(cJSON_AddStringToObject(common_event_header, "sourceId", "") == 0) {
110 log_error("cJSON AddStringToObject error\n");
111 cJSON_Delete(common_event_header);
115 if(cJSON_AddStringToObject(common_event_header, "sourceName", source_name) == 0) {
116 log_error("cJSON AddStringToObject error\n");
117 cJSON_Delete(common_event_header);
121 if(cJSON_AddNumberToObject(common_event_header, "startEpochMicrosec", (double)(useconds)) == 0) {
122 log_error("cJSON AddNumberToObject error\n");
123 cJSON_Delete(common_event_header);
127 if(cJSON_AddNumberToObject(common_event_header, "lastEpochMicrosec", (double)(useconds)) == 0) {
128 log_error("cJSON AddNumberToObject error\n");
129 cJSON_Delete(common_event_header);
133 if(cJSON_AddStringToObject(common_event_header, "nfNamingCode", "sdn controller") == 0) {
134 log_error("cJSON AddStringToObject error\n");
135 cJSON_Delete(common_event_header);
139 if(cJSON_AddStringToObject(common_event_header, "nfVendorName", "sdn") == 0) {
140 log_error("cJSON AddStringToObject error\n");
141 cJSON_Delete(common_event_header);
145 if(cJSON_AddStringToObject(common_event_header, "timeZoneOffset", "+00:00") == 0) {
146 log_error("cJSON AddStringToObject error\n");
147 cJSON_Delete(common_event_header);
151 if(cJSON_AddStringToObject(common_event_header, "version", "4.1") == 0) {
152 log_error("cJSON AddStringToObject error\n");
153 cJSON_Delete(common_event_header);
157 if(cJSON_AddStringToObject(common_event_header, "vesEventListenerVersion", framework_environment.ves_endpoint.common_header_version) == 0) {
158 log_error("cJSON AddStringToObject error\n");
159 cJSON_Delete(common_event_header);
163 return common_event_header;
166 nts_mount_point_addressing_method_t nts_mount_point_addressing_method_get(sr_session_ctx_t *current_session) {
169 nts_mount_point_addressing_method_t ret = UNKNOWN_MAPPING;
172 bool session_started = false;
173 if(current_session == 0) {
174 rc = sr_session_start(session_connection, SR_DS_RUNNING, ¤t_session);
175 if(rc != SR_ERR_OK) {
176 log_error("could not start sysrepo session\n");
179 session_started = true;
183 rc = sr_get_item(session_running, NTS_NETWORK_FUNCTION_MPAM_SCHEMA_XPATH, 0, &value);
184 if(rc == SR_ERR_OK) {
185 if(strcmp(value->data.enum_val, "host-mapping") == 0) {
189 ret = DOCKER_MAPPING;
194 if(session_started) {
195 rc = sr_session_stop(current_session);
196 if(rc != SR_ERR_OK) {
197 log_error("could not stop sysrepo session\n");
205 // checkAS authentication via certificate not supported yet
206 ves_details_t *ves_endpoint_details_get(sr_session_ctx_t *current_session) {
210 bool session_started = false;
211 if(current_session == 0) {
212 rc = sr_session_start(session_connection, SR_DS_RUNNING, ¤t_session);
213 if(rc != SR_ERR_OK) {
214 log_error("could not start sysrepo session\n");
217 session_started = true;
220 struct lyd_node *data = 0;
223 if(framework_arguments.nts_mode == NTS_MODE_MANAGER) {
224 xpath_to_get = "/nts-manager:simulation/ves-endpoint";
227 xpath_to_get = "/nts-network-function:simulation/ves-endpoint";
230 rc = sr_get_subtree(current_session, xpath_to_get, 0, &data);
231 if(rc != SR_ERR_OK) {
232 log_error("could not get value for xPath=%s from the running datastore\n", xpath_to_get);
233 if(session_started) {
234 sr_session_stop(current_session);
239 if(session_started) {
240 rc = sr_session_stop(current_session);
241 if(rc != SR_ERR_OK) {
242 log_error("could not stop sysrepo session\n");
248 if(data->child == 0) {
249 log_error("ves-endpoint probably not set yet\n", xpath_to_get);
254 ves_details_t *ret = (ves_details_t *)malloc(sizeof(ves_details_t));
256 log_error("malloc failed\n");
264 ret->auth_method = 0;
268 struct lyd_node *chd = 0;
269 LY_TREE_FOR(data->child, chd) {
270 const char *val = ((const struct lyd_node_leaf_list *)chd)->value_str;
272 if(strcmp(chd->schema->name, "ves-endpoint-protocol") == 0) {
273 ret->protocol = strdup(val);
275 else if(strcmp(chd->schema->name, "ves-endpoint-ip") == 0) {
276 ret->ip = strdup(val);
278 else if(strcmp(chd->schema->name, "ves-endpoint-port") == 0) {
279 ret->port = ((const struct lyd_node_leaf_list *)chd)->value.uint16;
281 else if(strcmp(chd->schema->name, "ves-endpoint-auth-method") == 0) {
282 ret->auth_method = strdup(val);
284 else if(strcmp(chd->schema->name, "ves-endpoint-username") == 0) {
285 ret->username = strdup(val);
287 else if(strcmp(chd->schema->name, "ves-endpoint-password") == 0) {
288 ret->password = strdup(val);
293 if (strstr(ret->ip, ":")) {
295 asprintf(&ret->url, "%s://[%s]:%d/eventListener/v7", ret->protocol, ret->ip, ret->port);
298 asprintf(&ret->url, "%s://%s:%d/eventListener/v7", ret->protocol, ret->ip, ret->port);
301 if((ret->protocol == 0) || (ret->ip == 0) || (ret->auth_method == 0) || (ret->username == 0) || (ret->password == 0) || (ret->url == 0)) {
304 free(ret->auth_method);
315 void ves_details_free(ves_details_t *instance) {
318 free(instance->protocol);
321 free(instance->auth_method);
322 free(instance->username);
323 free(instance->password);
328 // checkAS authentication via certificate not supported yet
329 controller_details_t *controller_details_get(sr_session_ctx_t *current_session) {
333 bool session_started = false;
334 if(current_session == 0) {
335 rc = sr_session_start(session_connection, SR_DS_RUNNING, ¤t_session);
336 if(rc != SR_ERR_OK) {
337 log_error("could not start sysrepo session\n");
340 session_started = true;
343 struct lyd_node *data = 0;
346 if(framework_arguments.nts_mode == NTS_MODE_MANAGER) {
347 xpath_to_get = "/nts-manager:simulation/sdn-controller";
350 xpath_to_get = "/nts-network-function:simulation/sdn-controller";
353 rc = sr_get_subtree(current_session, xpath_to_get, 0, &data);
354 if(rc != SR_ERR_OK) {
355 log_error("could not get value for xPath=%s from the running datastore\n", xpath_to_get);
356 if(session_started) {
357 sr_session_stop(current_session);
362 if(session_started) {
363 rc = sr_session_stop(current_session);
364 if(rc != SR_ERR_OK) {
365 log_error("could not stop sysrepo session\n");
371 if(data->child == 0) {
372 log_error("sdn-controller probably not set yet\n");
377 controller_details_t *ret = (controller_details_t *)malloc(sizeof(controller_details_t));
379 log_error("malloc failed\n");
387 ret->nc_callhome_port = 0;
388 ret->auth_method = 0;
392 ret->auth_method = strdup("basic");
394 struct lyd_node *chd = 0;
395 LY_TREE_FOR(data->child, chd) {
396 const char *val = ((const struct lyd_node_leaf_list *)chd)->value_str;
398 if(strcmp(chd->schema->name, "controller-protocol") == 0) {
399 ret->protocol = strdup(val);
401 else if(strcmp(chd->schema->name, "controller-ip") == 0) {
402 ret->ip = strdup(val);
404 else if(strcmp(chd->schema->name, "controller-port") == 0) {
405 ret->port = ((const struct lyd_node_leaf_list *)chd)->value.uint16;
407 else if(strcmp(chd->schema->name, "controller-netconf-call-home-port") == 0) {
408 ret->nc_callhome_port = ((const struct lyd_node_leaf_list *)chd)->value.uint16;
410 else if(strcmp(chd->schema->name, "controller-username") == 0) {
411 ret->username = strdup(val);
413 else if(strcmp(chd->schema->name, "controller-password") == 0) {
414 ret->password = strdup(val);
419 if (strstr(ret->ip, ":")) {
421 asprintf(&ret->base_url, "%s://[%s]:%d", ret->protocol, ret->ip, ret->port);
424 asprintf(&ret->base_url, "%s://%s:%d", ret->protocol, ret->ip, ret->port);
427 if((ret->protocol == 0) || (ret->ip == 0) || (ret->auth_method == 0) || (ret->username == 0) || (ret->password == 0) || (ret->base_url == 0)) {
430 free(ret->auth_method);
441 void controller_details_free(controller_details_t *instance) {
444 free(instance->protocol);
446 free(instance->base_url);
447 free(instance->auth_method);
448 free(instance->username);
449 free(instance->password);
453 int nts_vercmp(const char *ver1, const char *ver2) {
458 int v1 = 0, v2 = 0, v3 = 0;
459 while(ver1[i] && (ver1[i] != '.')) {
467 while(ver1[i] && (ver1[i] != '.')) {
475 while(ver1[i] && (ver1[i] != '.')) {
484 int V1 = 0, V2 = 0, V3 = 0;
486 while(ver2[i] && (ver2[i] != '.')) {
494 while(ver2[i] && (ver2[i] != '.')) {
502 while(ver2[i] && (ver2[i] != '.')) {
534 int nts_utils_populate_info(sr_session_ctx_t *current_session, const char *function_type) {
535 assert(current_session);
536 assert(function_type);
538 bool manager = (strcmp(function_type, "NTS_FUNCTION_TYPE_MANAGER") == 0);
542 //setup sdn-controller defaults
543 if(strlen(framework_environment.sdn_controller.protocol)) {
545 rc = sr_set_item_str(current_session, NTS_MANAGER_SDN_CONTROLLER_CONFIG_XPATH"/controller-protocol", (const char*)framework_environment.sdn_controller.protocol, 0, 0);
548 rc = sr_set_item_str(current_session, NTS_NF_SDN_CONTROLLER_CONFIG_XPATH"/controller-protocol", (const char*)framework_environment.sdn_controller.protocol, 0, 0);
550 if(rc != SR_ERR_OK) {
551 log_error("sr_set_item_str failed\n");
552 return NTS_ERR_FAILED;
556 if(strlen(framework_environment.sdn_controller.ip)) {
558 rc = sr_set_item_str(current_session, NTS_MANAGER_SDN_CONTROLLER_CONFIG_XPATH"/controller-ip", (const char*)framework_environment.sdn_controller.ip, 0, 0);
561 rc = sr_set_item_str(current_session, NTS_NF_SDN_CONTROLLER_CONFIG_XPATH"/controller-ip", (const char*)framework_environment.sdn_controller.ip, 0, 0);
563 if(rc != SR_ERR_OK) {
564 log_error("sr_set_item_str failed\n");
565 return NTS_ERR_FAILED;
569 sprintf(int_to_str, "%d", framework_environment.sdn_controller.port);
571 rc = sr_set_item_str(current_session, NTS_MANAGER_SDN_CONTROLLER_CONFIG_XPATH"/controller-port", (const char*)int_to_str, 0, 0);
574 rc = sr_set_item_str(current_session, NTS_NF_SDN_CONTROLLER_CONFIG_XPATH"/controller-port", (const char*)int_to_str, 0, 0);
576 if(rc != SR_ERR_OK) {
577 log_error("sr_set_item_str failed\n");
578 return NTS_ERR_FAILED;
581 sprintf(int_to_str, "%d", framework_environment.sdn_controller.callhome_port);
583 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);
586 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);
588 if(rc != SR_ERR_OK) {
589 log_error("sr_set_item_str failed\n");
590 return NTS_ERR_FAILED;
593 if(strlen(framework_environment.sdn_controller.username)) {
595 rc = sr_set_item_str(current_session, NTS_MANAGER_SDN_CONTROLLER_CONFIG_XPATH"/controller-username", (const char*)framework_environment.sdn_controller.username, 0, 0);
598 rc = sr_set_item_str(current_session, NTS_NF_SDN_CONTROLLER_CONFIG_XPATH"/controller-username", (const char*)framework_environment.sdn_controller.username, 0, 0);
600 if(rc != SR_ERR_OK) {
601 log_error("sr_set_item_str failed\n");
602 return NTS_ERR_FAILED;
606 if(strlen(framework_environment.sdn_controller.password)) {
608 rc = sr_set_item_str(current_session, NTS_MANAGER_SDN_CONTROLLER_CONFIG_XPATH"/controller-password", (const char*)framework_environment.sdn_controller.password, 0, 0);
611 rc = sr_set_item_str(current_session, NTS_NF_SDN_CONTROLLER_CONFIG_XPATH"/controller-password", (const char*)framework_environment.sdn_controller.password, 0, 0);
613 if(rc != SR_ERR_OK) {
614 log_error("sr_set_item_str failed\n");
615 return NTS_ERR_FAILED;
619 //setup ves-endpoint details
620 if(strlen(framework_environment.ves_endpoint.protocol)) {
622 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);
625 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);
627 if(rc != SR_ERR_OK) {
628 log_error("sr_set_item_str failed\n");
629 return NTS_ERR_FAILED;
633 if(strlen(framework_environment.ves_endpoint.ip)) {
635 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);
638 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);
640 if(rc != SR_ERR_OK) {
641 log_error("sr_set_item_str failed\n");
642 return NTS_ERR_FAILED;
646 sprintf(int_to_str, "%d", framework_environment.ves_endpoint.port);
648 rc = sr_set_item_str(current_session, NTS_MANAGER_VES_ENDPOINT_CONFIG_XPATH"/ves-endpoint-port", (const char*)int_to_str, 0, 0);
651 rc = sr_set_item_str(current_session, NTS_NF_VES_ENDPOINT_CONFIG_XPATH"/ves-endpoint-port", (const char*)int_to_str, 0, 0);
653 if(rc != SR_ERR_OK) {
654 log_error("sr_set_item_str failed\n");
655 return NTS_ERR_FAILED;
658 if(strlen(framework_environment.ves_endpoint.auth_method)) {
660 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);
663 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);
665 if(rc != SR_ERR_OK) {
666 log_error("sr_set_item_str failed\n");
667 return NTS_ERR_FAILED;
671 if(strlen(framework_environment.ves_endpoint.username)) {
673 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);
676 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);
678 if(rc != SR_ERR_OK) {
679 log_error("sr_set_item_str failed\n");
680 return NTS_ERR_FAILED;
684 if(strlen(framework_environment.ves_endpoint.password)) {
686 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);
689 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);
691 if(rc != SR_ERR_OK) {
692 log_error("sr_set_item_str failed\n");
693 return NTS_ERR_FAILED;
697 if(strlen(framework_environment.ves_endpoint.certificate)) {
699 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);
702 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);
704 if(rc != SR_ERR_OK) {
705 log_error("sr_set_item_str failed\n");
706 return NTS_ERR_FAILED;
710 if(manager == false) {
711 //presence containers
712 rc = sr_set_item_str(current_session, NTS_NETWORK_FUNCTION_FAULT_GENERATION_SCHEMA_XPATH, 0, 0, 0);
713 if(rc != SR_ERR_OK) {
714 log_error("sr_set_item_str failed\n");
715 return NTS_ERR_FAILED;
718 rc = sr_set_item_str(current_session, NTS_NETWORK_FUNCTION_NETCONF_SCHEMA_XPATH, 0, 0, 0);
719 if(rc != SR_ERR_OK) {
720 log_error("sr_set_item_str failed\n");
721 return NTS_ERR_FAILED;
724 rc = sr_set_item_str(current_session, NTS_NETWORK_FUNCTION_VES_SCHEMA_XPATH, 0, 0, 0);
725 if(rc != SR_ERR_OK) {
726 log_error("sr_set_item_str failed\n");
727 return NTS_ERR_FAILED;
731 //also set the network-function module for easy identifying the function type
732 rc = sr_set_item_str(current_session, NTS_NETWORK_FUNCTION_FTYPE_SCHEMA_XPATH, function_type, 0, 0);
733 if(rc != SR_ERR_OK) {
734 log_error("sr_set_item_str failed\n");
735 return NTS_ERR_FAILED;
738 //mount-point-addressing-method
739 rc = sr_set_item_str(current_session, NTS_NETWORK_FUNCTION_MPAM_SCHEMA_XPATH, "docker-mapping", 0, 0);
740 if(rc != SR_ERR_OK) {
741 log_error("sr_set_item_str failed\n");
742 return NTS_ERR_FAILED;
746 rc = sr_apply_changes(current_session, 0, 0);
747 if(rc != SR_ERR_OK) {
748 log_error("sr_apply_changes failed: %s\n", sr_strerror(rc));
749 return NTS_ERR_FAILED;