When env variable SCRIPTS_DIR is unset the code reads or writes from a path
[sim/o1-interface.git] / ntsimulator / src / utils / utils.c
index 7d1d2a0..5242f6d 100644 (file)
@@ -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)
@@ -452,10 +490,9 @@ void       writeConfigFile(char *config)
        }
 }
 
-int    getFaultNotificationDelayPeriodFromConfigJson(void)
+int getFaultNotificationDelayPeriodFromConfigJson(int *period_array, int *count)
 {
        char *stringConfig = readConfigFileInString();
-       int notificationDelay = 0;
 
        if (stringConfig == NULL)
        {
@@ -487,18 +524,34 @@ int       getFaultNotificationDelayPeriodFromConfigJson(void)
        }
 
        cJSON *faultNotifDelay = cJSON_GetObjectItemCaseSensitive(notifConfig, "fault-notification-delay-period");
-       if (!cJSON_IsNumber(faultNotifDelay))
+       if (!cJSON_IsArray(faultNotifDelay))
        {
-               printf("Configuration JSON is not as expected: fault-notification-delay-period is not a number");
+               printf("Configuration JSON is not as expected: fault-notification-delay-period is not an array.");
                cJSON_Delete(jsonConfig);
                return SR_ERR_OPERATION_FAILED;
        }
 
-       notificationDelay = (int)(faultNotifDelay->valuedouble);
+    cJSON *iterator = NULL;
+    *count = 0;
+    int i = 0;
+
+    cJSON_ArrayForEach(iterator, faultNotifDelay) 
+    {
+        if (cJSON_IsNumber(iterator)) 
+        {
+            period_array[i++] = iterator->valueint;
+        } 
+        else 
+        {
+            printf("Invalid number in array!");
+        }
+    }
+
+    *count = i;
 
        cJSON_Delete(jsonConfig);
 
-       return notificationDelay;
+       return SR_ERR_OK;
 }
 
 int    getVesHeartbeatPeriodFromConfigJson(void)
@@ -1288,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;
 }
 
@@ -1518,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;
 }
@@ -1574,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;
 }
@@ -1741,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; i<array_size; ++i)
     {
         cJSON *deviceListEntry = cJSON_GetArrayItem(deviceList, i);
@@ -1880,4 +1965,248 @@ int getDeviceCounters(char *containerId, counterAlarms *ves_counter, counterAlar
     cJSON_Delete(jsonStatus);
 
     return SR_ERR_OK;
-}
\ No newline at end of file
+}
+
+int writeSkeletonConfigFile()
+{
+    cJSON *configObject = cJSON_CreateObject();
+    if (configObject == NULL)
+    {
+        printf("Could not create JSON object: configObject\n");
+        return SR_ERR_OPERATION_FAILED;
+    }
+
+    cJSON *notificationConfig = cJSON_CreateObject();
+    if (notificationConfig == NULL)
+    {
+        printf("Could not create JSON object: notificationConfig\n");
+        cJSON_Delete(configObject);
+        return SR_ERR_OPERATION_FAILED;
+    }
+    cJSON_AddItemToObject(configObject, "notification-config", notificationConfig);
+
+    if (cJSON_AddNumberToObject(notificationConfig, "ves-heartbeat-period", 0) == NULL)
+    {
+        printf("Could not create JSON object: ves-heartbeat-period\n");
+        cJSON_Delete(configObject);
+        return SR_ERR_OPERATION_FAILED;
+    }
+
+    if (cJSON_AddTrueToObject(notificationConfig, "is-netconf-available") == NULL)
+    {
+        printf("Could not create JSON object: is-netconf-available\n");
+        cJSON_Delete(configObject);
+        return SR_ERR_OPERATION_FAILED;
+    }
+
+    if (cJSON_AddTrueToObject(notificationConfig, "is-ves-available") == NULL)
+    {
+        printf("Could not create JSON object: is-ves-available\n");
+        cJSON_Delete(configObject);
+        return SR_ERR_OPERATION_FAILED;
+    }
+
+    cJSON *faultNotificationDelayPeriod = cJSON_CreateArray();
+    if (faultNotificationDelayPeriod == NULL)
+    {
+        printf("Could not create JSON object: faultNotificationDelayPeriod\n");
+        cJSON_Delete(configObject);
+        return SR_ERR_OPERATION_FAILED;
+       }
+    cJSON_AddItemToObject(notificationConfig, "fault-notification-delay-period", faultNotificationDelayPeriod);
+
+    cJSON *arrayItem = cJSON_CreateNumber(0);
+    if (arrayItem == NULL)
+    {
+        printf("Could not create JSON object: arrayItem\n");
+        cJSON_Delete(configObject);
+        return SR_ERR_OPERATION_FAILED;
+       }
+    cJSON_AddItemToArray(faultNotificationDelayPeriod, arrayItem);
+
+    cJSON *vesEndPointDetails = cJSON_CreateObject();
+    if (vesEndPointDetails == NULL)
+    {
+        printf("Could not create JSON object: vesEndPointDetails\n");
+        cJSON_Delete(configObject);
+        return SR_ERR_OPERATION_FAILED;
+    }
+    cJSON_AddItemToObject(configObject, "ves-endpoint-details", vesEndPointDetails);
+
+    if (cJSON_AddStringToObject(vesEndPointDetails, "ves-endpoint-ip", "172.17.0.1") == NULL)
+    {
+        printf("Could not create JSON object: ves-endpoint-ip\n");
+        cJSON_Delete(configObject);
+        return SR_ERR_OPERATION_FAILED;
+    }
+
+    if (cJSON_AddNumberToObject(vesEndPointDetails, "ves-endpoint-port", 30007) == NULL)
+    {
+        printf("Could not create JSON object: ves-endpoint-port\n");
+        cJSON_Delete(configObject);
+        return SR_ERR_OPERATION_FAILED;
+    }
+
+    if (cJSON_AddStringToObject(vesEndPointDetails, "ves-endpoint-auth-method", "no-auth") == NULL)
+    {
+        printf("Could not create JSON object: ves-endpoint-auth-method\n");
+        cJSON_Delete(configObject);
+        return SR_ERR_OPERATION_FAILED;
+    }
+
+    if (cJSON_AddStringToObject(vesEndPointDetails, "ves-endpoint-username", "") == NULL)
+    {
+        printf("Could not create JSON object: ves-endpoint-username\n");
+        cJSON_Delete(configObject);
+        return SR_ERR_OPERATION_FAILED;
+    }
+
+    if (cJSON_AddStringToObject(vesEndPointDetails, "ves-endpoint-password", "") == NULL)
+    {
+        printf("Could not create JSON object: ves-endpoint-password\n");
+        cJSON_Delete(configObject);
+        return SR_ERR_OPERATION_FAILED;
+    }
+
+    if (cJSON_AddStringToObject(vesEndPointDetails, "ves-endpoint-certificate", "") == NULL)
+    {
+        printf("Could not create JSON object: ves-endpoint-certificate\n");
+        cJSON_Delete(configObject);
+        return SR_ERR_OPERATION_FAILED;
+    }
+
+    if (cJSON_AddFalseToObject(vesEndPointDetails, "ves-registration") == NULL)
+    {
+        printf("Could not create JSON object: ves-registration\n");
+        cJSON_Delete(configObject);
+        return SR_ERR_OPERATION_FAILED;
+    }
+
+    if (cJSON_AddNumberToObject(configObject, "ssh-connections", 1) == NULL)
+    {
+        printf("Could not create JSON object: ssh-connections\n");
+        cJSON_Delete(configObject);
+        return SR_ERR_OPERATION_FAILED;
+    }
+
+    if (cJSON_AddNumberToObject(configObject, "tls-connections", 0) == NULL)
+    {
+        printf("Could not create JSON object: tls-connections\n");
+        cJSON_Delete(configObject);
+        return SR_ERR_OPERATION_FAILED;
+    }
+
+    char *config_string = NULL;
+
+    config_string = cJSON_PrintUnformatted(configObject);
+
+    writeConfigFile(config_string);
+
+    if (config_string != NULL)
+    {
+        free(config_string);
+        config_string = NULL;
+    }
+
+    if (configObject != NULL)
+    {
+        cJSON_Delete(configObject);
+    }
+
+    return SR_ERR_OK;
+}
+
+int getIntFromString(char *string, int def_value)
+{
+    int rc, value = def_value;
+    if (string != NULL)
+    {
+        rc = sscanf(string, "%d", &value);
+        if (rc != 1)
+        {
+            printf("Could not get the %s! Using the default 0...\n", string);
+            value = def_value;
+        }
+    }
+    return value;
+}
+
+int     getSshConnectionsFromConfigJson(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 *sshConnections = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ssh-connections");
+    if (!cJSON_IsNumber(sshConnections))
+    {
+        printf("Configuration JSON is not as expected: ssh-connections is not an object");
+        cJSON_Delete(jsonConfig);
+        return SR_ERR_OPERATION_FAILED;
+    }
+
+    int num_of_ssh = (int)(sshConnections->valuedouble);
+
+    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;
+}