Configurable HTTP(S) support for VES messaging
[sim/o1-interface.git] / ntsimulator / src / ntsimulator-manager / simulator-operations.c
index 6d02c01..87b1d68 100644 (file)
@@ -370,7 +370,7 @@ static char* create_docker_container_curl(int base_netconf_port, cJSON* managerB
 
     cJSON_AddItemToObject(postDataJson, "Env", env_variables_array);
 
-    char environment_var[50];
+    char environment_var[100];
     sprintf(environment_var, "NTS_IP=%s", getenv("NTS_IP"));
 
     cJSON *env_var_obj = cJSON_CreateString(environment_var);
@@ -381,6 +381,24 @@ static char* create_docker_container_curl(int base_netconf_port, cJSON* managerB
        }
     cJSON_AddItemToArray(env_variables_array, env_var_obj);
 
+    char *external_nts_ip = getenv("EXTERNAL_NTS_IP");
+    if (external_nts_ip == NULL)
+    {
+        sprintf(environment_var, "EXTERNAL_NTS_IP=%s", getenv("NTS_IP"));
+    }
+    else
+    {
+        sprintf(environment_var, "EXTERNAL_NTS_IP=%s", external_nts_ip);
+    }
+
+    cJSON *env_var_obj_0 = cJSON_CreateString(environment_var);
+    if (env_var_obj_0 == NULL)
+       {
+        printf("Could not create JSON object: Env array object EXTERNAL_NTS_IP\n");
+               return NULL;
+       }
+    cJSON_AddItemToArray(env_variables_array, env_var_obj_0);
+
     sprintf(environment_var, "NETCONF_BASE=%d", base_netconf_port);
     cJSON *env_var_obj_2 = cJSON_CreateString(environment_var);
     if (env_var_obj_2 == NULL)
@@ -619,7 +637,8 @@ static int send_mount_device_instance_ssh(char *url, char *credentials, char *de
             "<max-connection-attempts xmlns=\"urn:opendaylight:netconf-node-topology\">100</max-connection-attempts>"
             "<between-attempts-timeout-millis xmlns=\"urn:opendaylight:netconf-node-topology\">2000</between-attempts-timeout-millis>"
             "</node>",
-                       device_name, device_port, getenv("NTS_IP"), device_port, "netconf", "netconf");
+                       device_name, device_port, (getenv("EXTERNAL_NTS_IP") == NULL) ? getenv("NTS_IP") : getenv("EXTERNAL_NTS_IP"),
+            device_port, "netconf", "netconf");
 
        printf("Post data:\n%s\n", post_data_xml);
 
@@ -682,7 +701,7 @@ static int send_mount_device_instance_tls(char *url, char *credentials, char *de
                        "<max-connection-attempts xmlns=\"urn:opendaylight:netconf-node-topology\">100</max-connection-attempts>"
                        "<between-attempts-timeout-millis xmlns=\"urn:opendaylight:netconf-node-topology\">2000</between-attempts-timeout-millis>"
                        "</node>",
-                       device_name, device_port, getenv("NTS_IP"), "netconf", device_port);
+                       device_name, device_port, (getenv("EXTERNAL_NTS_IP") == NULL) ? getenv("NTS_IP") : getenv("EXTERNAL_NTS_IP"), "netconf", device_port);
 
        printf("Post data:\n%s\n", post_data_xml);
 
@@ -755,7 +774,7 @@ static int send_mount_device(device_t *current_device, controller_t controller_d
        bool is_mounted = true;
     int port = 0;
 
-    char device_name[100];
+    char device_name[200];
     sprintf(device_name, "%s-%d", getenv("CONTAINER_NAME"), current_device->device_number);
 
        //This is where we hardcoded: 7 devices will have SSH connections and 3 devices will have TLS connections
@@ -1662,51 +1681,51 @@ int add_key_pair_to_odl(controller_t *controller_list, int controller_list_size)
 
 int ves_ip_changed(char *new_ip)
 {
-       char *stringConfiguration = readConfigFileInString();
+    char *stringConfiguration = readConfigFileInString();
 
-       if (stringConfiguration == NULL)
-       {
-               printf("Could not read configuration file!\n");
-               return SR_ERR_OPERATION_FAILED;
-       }
+    if (stringConfiguration == NULL)
+    {
+        printf("Could not read configuration file!\n");
+        return SR_ERR_OPERATION_FAILED;
+    }
 
-       cJSON *jsonConfig = cJSON_Parse(stringConfiguration);
-       if (jsonConfig == NULL)
-       {
-               free(stringConfiguration);
-               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(stringConfiguration);
-       stringConfiguration = NULL;
+    cJSON *jsonConfig = cJSON_Parse(stringConfiguration);
+    if (jsonConfig == NULL)
+    {
+        free(stringConfiguration);
+        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(stringConfiguration);
+    stringConfiguration = NULL;
 
-       cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
-       if (!cJSON_IsObject(vesDetails))
-       {
-               printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
-               cJSON_Delete(jsonConfig);
-               return SR_ERR_OPERATION_FAILED;
-       }
+    cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
+    if (!cJSON_IsObject(vesDetails))
+    {
+        printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
+        cJSON_Delete(jsonConfig);
+        return SR_ERR_OPERATION_FAILED;
+    }
 
-       cJSON *vesIp = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-ip");
-       if (!cJSON_IsString(vesIp))
-       {
-               printf("Configuration JSON is not as expected: ves-endpoint-ip is not a string");
-               cJSON_Delete(jsonConfig);
-               return SR_ERR_OPERATION_FAILED;
-       }
+    cJSON *vesIp = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-ip");
+    if (!cJSON_IsString(vesIp))
+    {
+        printf("Configuration JSON is not as expected: ves-endpoint-ip is not a string");
+        cJSON_Delete(jsonConfig);
+        return SR_ERR_OPERATION_FAILED;
+    }
 
-       //we set the value of the fault-notification-delay-period object
-       cJSON_ReplaceItemInObject(vesDetails, "ves-endpoint-ip", cJSON_CreateString(new_ip));
+    //we set the value of the fault-notification-delay-period object
+    cJSON_ReplaceItemInObject(vesDetails, "ves-endpoint-ip", cJSON_CreateString(new_ip));
 
-       //writing the new JSON to the configuration file
-       stringConfiguration = cJSON_Print(jsonConfig);
-       writeConfigFile(stringConfiguration);
+    //writing the new JSON to the configuration file
+    stringConfiguration = cJSON_Print(jsonConfig);
+    writeConfigFile(stringConfiguration);
 
     if (stringConfiguration != NULL)
     {
@@ -1714,9 +1733,9 @@ int ves_ip_changed(char *new_ip)
         stringConfiguration = NULL;
     }
 
-       cJSON_Delete(jsonConfig);
+    cJSON_Delete(jsonConfig);
 
-       return SR_ERR_OK;
+    return SR_ERR_OK;
 }
 
 int ves_port_changed(int new_port)
@@ -1778,6 +1797,183 @@ int ves_port_changed(int new_port)
        return SR_ERR_OK;
 }
 
+int ves_username_changed(char *new_username)
+{
+    char *stringConfiguration = readConfigFileInString();
+
+    if (stringConfiguration == NULL)
+    {
+        printf("Could not read configuration file!\n");
+        return SR_ERR_OPERATION_FAILED;
+    }
+
+    cJSON *jsonConfig = cJSON_Parse(stringConfiguration);
+    if (jsonConfig == NULL)
+    {
+        free(stringConfiguration);
+        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(stringConfiguration);
+    stringConfiguration = NULL;
+
+    cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
+    if (!cJSON_IsObject(vesDetails))
+    {
+        printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
+        cJSON_Delete(jsonConfig);
+        return SR_ERR_OPERATION_FAILED;
+    }
+
+    cJSON *vesUsername = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-username");
+    if (!cJSON_IsString(vesUsername))
+    {
+        printf("Configuration JSON is not as expected: ves-endpoint-username is not a string");
+        cJSON_Delete(jsonConfig);
+        return SR_ERR_OPERATION_FAILED;
+    }
+
+    //we set the value of the fault-notification-delay-period object
+    cJSON_ReplaceItemInObject(vesDetails, "ves-endpoint-username", cJSON_CreateString(new_username));
+
+    //writing the new JSON to the configuration file
+    stringConfiguration = cJSON_Print(jsonConfig);
+    writeConfigFile(stringConfiguration);
+
+    if (stringConfiguration != NULL)
+    {
+        free(stringConfiguration);
+        stringConfiguration = NULL;
+    }
+
+    cJSON_Delete(jsonConfig);
+
+    return SR_ERR_OK;
+}
+
+int ves_password_changed(char *new_password)
+{
+    char *stringConfiguration = readConfigFileInString();
+
+    if (stringConfiguration == NULL)
+    {
+        printf("Could not read configuration file!\n");
+        return SR_ERR_OPERATION_FAILED;
+    }
+
+    cJSON *jsonConfig = cJSON_Parse(stringConfiguration);
+    if (jsonConfig == NULL)
+    {
+        free(stringConfiguration);
+        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(stringConfiguration);
+    stringConfiguration = NULL;
+
+    cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
+    if (!cJSON_IsObject(vesDetails))
+    {
+        printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
+        cJSON_Delete(jsonConfig);
+        return SR_ERR_OPERATION_FAILED;
+    }
+
+    cJSON *vesPassword = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-password");
+    if (!cJSON_IsString(vesPassword))
+    {
+        printf("Configuration JSON is not as expected: ves-endpoint-password is not a string");
+        cJSON_Delete(jsonConfig);
+        return SR_ERR_OPERATION_FAILED;
+    }
+
+    //we set the value of the fault-notification-delay-period object
+    cJSON_ReplaceItemInObject(vesDetails, "ves-endpoint-password", cJSON_CreateString(new_password));
+
+    //writing the new JSON to the configuration file
+    stringConfiguration = cJSON_Print(jsonConfig);
+    writeConfigFile(stringConfiguration);
+
+    if (stringConfiguration != NULL)
+    {
+        free(stringConfiguration);
+        stringConfiguration = NULL;
+    }
+
+    cJSON_Delete(jsonConfig);
+
+    return SR_ERR_OK;
+}
+
+int ves_auth_method_changed(char *new_auth_method)
+{
+    char *stringConfiguration = readConfigFileInString();
+
+    if (stringConfiguration == NULL)
+    {
+        printf("Could not read configuration file!\n");
+        return SR_ERR_OPERATION_FAILED;
+    }
+
+    cJSON *jsonConfig = cJSON_Parse(stringConfiguration);
+    if (jsonConfig == NULL)
+    {
+        free(stringConfiguration);
+        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(stringConfiguration);
+    stringConfiguration = NULL;
+
+    cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
+    if (!cJSON_IsObject(vesDetails))
+    {
+        printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
+        cJSON_Delete(jsonConfig);
+        return SR_ERR_OPERATION_FAILED;
+    }
+
+    cJSON *vesAuthMethod = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-auth-method");
+    if (!cJSON_IsString(vesAuthMethod))
+    {
+        printf("Configuration JSON is not as expected: ves-endpoint-auth-method is not a string");
+        cJSON_Delete(jsonConfig);
+        return SR_ERR_OPERATION_FAILED;
+    }
+
+    //we set the value of the fault-notification-delay-period object
+    cJSON_ReplaceItemInObject(vesDetails, "ves-endpoint-auth-method", cJSON_CreateString(new_auth_method));
+
+    //writing the new JSON to the configuration file
+    stringConfiguration = cJSON_Print(jsonConfig);
+    writeConfigFile(stringConfiguration);
+
+    if (stringConfiguration != NULL)
+    {
+        free(stringConfiguration);
+        stringConfiguration = NULL;
+    }
+
+    cJSON_Delete(jsonConfig);
+
+    return SR_ERR_OK;
+}
+
 int ves_registration_changed(cJSON_bool new_bool)
 {
        char *stringConfiguration = readConfigFileInString();
@@ -2511,7 +2707,7 @@ static int start_device_notification(char *exec_id)
     curl_easy_reset(curl);
     set_curl_common_info();
 
-    char url[100];
+    char url[500];
     sprintf(url, "http:/v%s/exec/%s/start", getenv("DOCKER_ENGINE_VERSION"), exec_id);
 
     curl_easy_setopt(curl, CURLOPT_URL, url);
@@ -2590,7 +2786,7 @@ static int inspect_device_notification_execution(char *exec_id)
     curl_easy_reset(curl);
     set_curl_common_info();
 
-    char url[100];
+    char url[500];
     sprintf(url, "http:/v%s/exec/%s/json", getenv("DOCKER_ENGINE_VERSION"), exec_id);
 
     curl_easy_setopt(curl, CURLOPT_URL, url);
@@ -2645,7 +2841,7 @@ int invoke_device_notification(char *device_id, char *module_name, char *notific
     curl_easy_reset(curl);
     set_curl_common_info();
 
-    char url[100];
+    char url[300];
     sprintf(url, "http:/v%s/containers/%s/exec", getenv("DOCKER_ENGINE_VERSION"), device_id);
 
     curl_easy_setopt(curl, CURLOPT_URL, url);
@@ -2703,7 +2899,8 @@ int invoke_device_notification(char *device_id, char *module_name, char *notific
     cJSON *cmd_string_2 = cJSON_CreateString("-c");
     cJSON_AddItemToArray(cmd_array, cmd_string_2);
 
-    char string_command[500];
+    //some notifications require a really long notification object
+    char string_command[1000000];
     sprintf(string_command, "/usr/local/bin/generic-notifications %s '%s'", module_name, notification_string);
 
     cJSON *cmd_string_3 = cJSON_CreateString(string_command);
@@ -2799,6 +2996,8 @@ int pull_docker_image_of_simulated_device()
 
     curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&curl_response_mem);
 
+    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 300L);
+
     res = curl_easy_perform(curl);
 
     if (res != CURLE_OK)