X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=ntsimulator%2Fsrc%2Futils%2Futils.c;h=5242f6d777882dadfb264a14027961247cd99625;hb=22d42e2cb4dced153aa8ee785ea2a102962a31b4;hp=15bb4260cd3d095049546ea52a2e6928515e0f15;hpb=610985b805c2b5c730bbc247ccbf76dd624792d0;p=sim%2Fo1-interface.git diff --git a/ntsimulator/src/utils/utils.c b/ntsimulator/src/utils/utils.c index 15bb426..5242f6d 100644 --- a/ntsimulator/src/utils/utils.c +++ b/ntsimulator/src/utils/utils.c @@ -410,7 +410,26 @@ char* readConfigFileInString(void) char * buffer = 0; long length; char config_file[200]; - sprintf(config_file, "%s/configuration.json", getenv("SCRIPTS_DIR")); + + //sprintf(config_file, "%s/configuration.json", getenv("SCRIPTS_DIR")); + + // --> recommended fix, Karl Koch, Deutsche Telekom AG, 22. 5. 2020. + // Path to config_file contains NULL when env variable is unset. + char *scripts_dir = getenv("SCRIPTS_DIR"); + char *scripts_dir_default = "/opt/dev/ntsimulator/scripts"; + + if(NULL != scripts_dir) + { + sprintf(config_file, "%s/configuration.json", scripts_dir); + } + else + { + sprintf(config_file, "%s/configuration.json", scripts_dir_default); + printf("warning: opening config file in default path: <%s>\n", + config_file); + } + // end of fix <-- + FILE * f = fopen (config_file, "rb"); if (f) @@ -438,7 +457,26 @@ char* readConfigFileInString(void) void writeConfigFile(char *config) { char config_file[200]; - sprintf(config_file, "%s/configuration.json", getenv("SCRIPTS_DIR")); + + //sprintf(config_file, "%s/configuration.json", getenv("SCRIPTS_DIR")); + + // --> recommended fix, Karl Koch, Deutsche Telekom AG, 22. 5. 2020. + // Path to config_file contains NULL when env variable is unset. + char *scripts_dir = getenv("SCRIPTS_DIR"); + char *scripts_dir_default = "/opt/dev/ntsimulator/scripts"; + + if(NULL != scripts_dir) + { + sprintf(config_file, "%s/configuration.json", scripts_dir); + } + else + { + sprintf(config_file, "%s/configuration.json", scripts_dir_default); + printf("warning: opening config file in default path: <%s>\n", + config_file); + } + // end of fix <-- + FILE * f = fopen (config_file, "w"); if (f) @@ -455,7 +493,6 @@ void writeConfigFile(char *config) int getFaultNotificationDelayPeriodFromConfigJson(int *period_array, int *count) { char *stringConfig = readConfigFileInString(); - int notificationDelay = 0; if (stringConfig == NULL) { @@ -1304,6 +1341,17 @@ int writeSkeletonStatusFile() writeStatusFile(status_string); + if (status_string != NULL) + { + free(status_string); + status_string = NULL; + } + + if (statusObject != NULL) + { + cJSON_Delete(statusObject); + } + return SR_ERR_OK; } @@ -1534,7 +1582,16 @@ int writeStatusNotificationCounters(counterAlarms ves_counter, counterAlarms net char *stringStatus = cJSON_PrintUnformatted(jsonStatus); writeStatusFile(stringStatus); - cJSON_Delete(jsonStatus); + if (stringStatus != NULL) + { + free(stringStatus); + stringStatus = NULL; + } + + if (jsonStatus != NULL) + { + cJSON_Delete(jsonStatus); + } return SR_ERR_OK; } @@ -1590,7 +1647,16 @@ int removeDeviceEntryFromStatusFile(char *containerId) char *stringStatus = cJSON_PrintUnformatted(jsonStatus); writeStatusFile(stringStatus); - cJSON_Delete(jsonStatus); + if (stringStatus != NULL) + { + free(stringStatus); + stringStatus = NULL; + } + + if (jsonStatus != NULL) + { + cJSON_Delete(jsonStatus); + } return SR_ERR_OK; } @@ -1757,6 +1823,9 @@ int getDeviceCounters(char *containerId, counterAlarms *ves_counter, counterAlar int array_size = cJSON_GetArraySize(deviceList); + ves_counter->critical = ves_counter->major = ves_counter->minor = ves_counter->warning = ves_counter->normal = 0; + netconf_counter->critical = netconf_counter->major = netconf_counter->minor = netconf_counter->warning = netconf_counter->normal = 0; + for (int i=0; ivaluedouble); + + cJSON_Delete(jsonConfig); + + return num_of_ssh; +} + +int getTlsConnectionsFromConfigJson(void) +{ + char *stringConfig = readConfigFileInString(); + + if (stringConfig == NULL) + { + printf("Could not read JSON configuration file in string."); + return 0; + } + + cJSON *jsonConfig = cJSON_Parse(stringConfig); + if (jsonConfig == NULL) + { + free(stringConfig); + const char *error_ptr = cJSON_GetErrorPtr(); + if (error_ptr != NULL) + { + fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr); + } + return SR_ERR_OPERATION_FAILED; + } + //we don't need the string anymore + free(stringConfig); + stringConfig = NULL; + + cJSON *tlsConnections = cJSON_GetObjectItemCaseSensitive(jsonConfig, "tls-connections"); + if (!cJSON_IsNumber(tlsConnections)) + { + printf("Configuration JSON is not as expected: ssh-connections is not an object"); + cJSON_Delete(jsonConfig); + return SR_ERR_OPERATION_FAILED; + } + + int num_of_tls = (int)(tlsConnections->valuedouble); + + cJSON_Delete(jsonConfig); + + return num_of_tls; +}