Add support for notification generation pattern
[sim/o1-interface.git] / ntsimulator / src / ntsimulator-manager / simulator-operations.c
index 8520d2b..f734307 100644 (file)
@@ -92,8 +92,8 @@ static cJSON* get_docker_container_bindings(void)
        curl_easy_reset(curl);
        set_curl_common_info();
 
-       char url[100];
-       sprintf(url, "http:/v%s/containers/NTS_Manager/json", getenv("DOCKER_ENGINE_VERSION"));
+       char url[200];
+       sprintf(url, "http:/v%s/containers/%s/json", getenv("DOCKER_ENGINE_VERSION"), getenv("HOSTNAME"));
 
        curl_easy_setopt(curl, CURLOPT_URL, url);
 
@@ -253,6 +253,12 @@ static char* create_docker_container_curl(int base_netconf_port, cJSON* managerB
        return NULL;
     }
 
+       if (cJSON_AddStringToObject(labels, "NTS_Manager", getenv("HOSTNAME")) == NULL)
+    {
+       printf("Could not create JSON object: NTS Manager\n");
+       return NULL;
+    }
+
     cJSON *env_variables_array = cJSON_CreateArray();
     if (env_variables_array == NULL)
        {
@@ -902,6 +908,12 @@ int stop_device(device_stack_t *theStack)
                printf("Could not kill and remove docker container with uuid=\"%s\"\n", last_id);
        }
 
+    rc = removeDeviceEntryFromStatusFile(last_id);
+    if (rc != SR_ERR_OK)
+    {
+        printf("Could not remove entry from status file for uuid=\"%s\"\n", last_id);
+    }
+
        pop_device(theStack);
 
        return SR_ERR_OK;
@@ -1021,6 +1033,7 @@ int get_docker_containers_operational_state_curl(device_stack_t *theStack)
                                        if (rc != SR_ERR_OK)
                                        {
                                                printf("Could not set the operational state for the device with uuid=\"%s\"\n", container_id_short);
+                                               return SR_ERR_OPERATION_FAILED;
                                        }
                                }
                        }
@@ -1063,7 +1076,7 @@ char* get_docker_container_resource_stats(device_stack_t *theStack)
        return NULL;     /* return with exit code indicating success. */
 }
 
-int notification_delay_period_changed(int period)
+int notification_delay_period_changed(sr_val_t *val, size_t count)
 {
        char *stringConfiguration = readConfigFileInString();
 
@@ -1092,26 +1105,60 @@ int notification_delay_period_changed(int period)
        if (!cJSON_IsObject(notifConfig))
        {
                printf("Configuration JSON is not as expected: notification-config is not an object");
-               free(jsonConfig);
+               cJSON_Delete(jsonConfig);
                return SR_ERR_OPERATION_FAILED;
        }
 
        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 an object");
-               free(jsonConfig);
+               printf("Configuration JSON is not as expected: fault-notification-delay-period is not an array.");
+               cJSON_Delete(jsonConfig);
                return SR_ERR_OPERATION_FAILED;
        }
 
-       //we set the value of the fault-notification-delay-period object
-       cJSON_SetNumberValue(faultNotifDelay, period);
+    cJSON_DeleteItemFromObject(notifConfig, "fault-notification-delay-period");
+
+    faultNotifDelay = NULL;
+
+    faultNotifDelay = cJSON_CreateArray();
+    if (faultNotifDelay == NULL) 
+    {
+        cJSON_Delete(jsonConfig);
+               return SR_ERR_OPERATION_FAILED;
+    }
+    cJSON_AddItemToObject(notifConfig, "fault-notification-delay-period", faultNotifDelay);
+
+    if (val != NULL && count > 0)
+    {
+        cJSON *arrayEntry = NULL;
+        for (size_t i=0; i<count; ++i)
+        {
+            arrayEntry = cJSON_CreateNumber(val[i].data.uint32_val);
+            if (arrayEntry == NULL) 
+            {
+                cJSON_Delete(jsonConfig);
+                return SR_ERR_OPERATION_FAILED;
+            }
+            cJSON_AddItemToArray(faultNotifDelay, arrayEntry);
+        }
+    }
+    else
+    {
+        cJSON *arrayEntry =  cJSON_CreateNumber(0);
+        if (arrayEntry == NULL) 
+        {
+            cJSON_Delete(jsonConfig);
+            return SR_ERR_OPERATION_FAILED;
+        }
+        cJSON_AddItemToArray(faultNotifDelay, arrayEntry);
+    }
 
        //writing the new JSON to the configuration file
        stringConfiguration = cJSON_Print(jsonConfig);
        writeConfigFile(stringConfiguration);
 
-       free(jsonConfig);
+       cJSON_Delete(jsonConfig);
 
        return SR_ERR_OK;
 }
@@ -1145,7 +1192,7 @@ int ves_heartbeat_period_changed(int period)
        if (!cJSON_IsObject(notifConfig))
        {
                printf("Configuration JSON is not as expected: notification-config is not an object");
-               free(jsonConfig);
+               cJSON_Delete(jsonConfig);
                return SR_ERR_OPERATION_FAILED;
        }
 
@@ -1153,7 +1200,7 @@ int ves_heartbeat_period_changed(int period)
        if (!cJSON_IsNumber(vesHeartbeatPeriod))
        {
                printf("Configuration JSON is not as expected: ves-heartbeat-period is not an object");
-               free(jsonConfig);
+               cJSON_Delete(jsonConfig);
                return SR_ERR_OPERATION_FAILED;
        }
 
@@ -1164,7 +1211,7 @@ int ves_heartbeat_period_changed(int period)
        stringConfiguration = cJSON_Print(jsonConfig);
        writeConfigFile(stringConfiguration);
 
-       free(jsonConfig);
+       cJSON_Delete(jsonConfig);
 
        return SR_ERR_OK;
 }
@@ -1396,7 +1443,7 @@ int ves_ip_changed(char *new_ip)
        if (!cJSON_IsObject(vesDetails))
        {
                printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
-               free(jsonConfig);
+               cJSON_Delete(jsonConfig);
                return SR_ERR_OPERATION_FAILED;
        }
 
@@ -1404,7 +1451,7 @@ int ves_ip_changed(char *new_ip)
        if (!cJSON_IsString(vesIp))
        {
                printf("Configuration JSON is not as expected: ves-endpoint-ip is not a string");
-               free(jsonConfig);
+               cJSON_Delete(jsonConfig);
                return SR_ERR_OPERATION_FAILED;
        }
 
@@ -1415,7 +1462,7 @@ int ves_ip_changed(char *new_ip)
        stringConfiguration = cJSON_Print(jsonConfig);
        writeConfigFile(stringConfiguration);
 
-       free(jsonConfig);
+       cJSON_Delete(jsonConfig);
 
        return SR_ERR_OK;
 }
@@ -1449,7 +1496,7 @@ int ves_port_changed(int new_port)
        if (!cJSON_IsObject(vesDetails))
        {
                printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
-               free(jsonConfig);
+               cJSON_Delete(jsonConfig);
                return SR_ERR_OPERATION_FAILED;
        }
 
@@ -1457,7 +1504,7 @@ int ves_port_changed(int new_port)
        if (!cJSON_IsNumber(vesPort))
        {
                printf("Configuration JSON is not as expected: ves-endpoint-port is not a number.");
-               free(jsonConfig);
+               cJSON_Delete(jsonConfig);
                return SR_ERR_OPERATION_FAILED;
        }
 
@@ -1468,7 +1515,7 @@ int ves_port_changed(int new_port)
        stringConfiguration = cJSON_Print(jsonConfig);
        writeConfigFile(stringConfiguration);
 
-       free(jsonConfig);
+       cJSON_Delete(jsonConfig);
 
        return SR_ERR_OK;
 }
@@ -1502,7 +1549,7 @@ int ves_registration_changed(cJSON_bool new_bool)
        if (!cJSON_IsObject(vesDetails))
        {
                printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
-               free(jsonConfig);
+               cJSON_Delete(jsonConfig);
                return SR_ERR_OPERATION_FAILED;
        }
 
@@ -1510,7 +1557,7 @@ int ves_registration_changed(cJSON_bool new_bool)
        if (!cJSON_IsBool(vesRegistration))
        {
                printf("Configuration JSON is not as expected: ves-registration is not a bool.");
-               free(jsonConfig);
+               cJSON_Delete(jsonConfig);
                return SR_ERR_OPERATION_FAILED;
        }
 
@@ -1521,7 +1568,7 @@ int ves_registration_changed(cJSON_bool new_bool)
        stringConfiguration = cJSON_Print(jsonConfig);
        writeConfigFile(stringConfiguration);
 
-       free(jsonConfig);
+       cJSON_Delete(jsonConfig);
 
        return SR_ERR_OK;
 }
@@ -1555,7 +1602,7 @@ int is_netconf_available_changed(cJSON_bool new_bool)
        if (!cJSON_IsObject(notifConfig))
        {
                printf("Configuration JSON is not as expected: notification-config is not an object");
-               free(jsonConfig);
+               cJSON_Delete(jsonConfig);
                return SR_ERR_OPERATION_FAILED;
        }
 
@@ -1563,7 +1610,7 @@ int is_netconf_available_changed(cJSON_bool new_bool)
        if (!cJSON_IsBool(isNetconfAvailable))
        {
                printf("Configuration JSON is not as expected: is-netconf-available is not a bool.");
-               free(jsonConfig);
+               cJSON_Delete(jsonConfig);
                return SR_ERR_OPERATION_FAILED;
        }
 
@@ -1574,7 +1621,7 @@ int is_netconf_available_changed(cJSON_bool new_bool)
        stringConfiguration = cJSON_Print(jsonConfig);
        writeConfigFile(stringConfiguration);
 
-       free(jsonConfig);
+       cJSON_Delete(jsonConfig);
 
        return SR_ERR_OK;
 }
@@ -1608,7 +1655,7 @@ int is_ves_available_changed(cJSON_bool new_bool)
        if (!cJSON_IsObject(notifConfig))
        {
                printf("Configuration JSON is not as expected: notification-config is not an object");
-               free(jsonConfig);
+               cJSON_Delete(jsonConfig);
                return SR_ERR_OPERATION_FAILED;
        }
 
@@ -1616,7 +1663,7 @@ int is_ves_available_changed(cJSON_bool new_bool)
        if (!cJSON_IsBool(isVesAvailable))
        {
                printf("Configuration JSON is not as expected: is-ves-available is not a bool.");
-               free(jsonConfig);
+               cJSON_Delete(jsonConfig);
                return SR_ERR_OPERATION_FAILED;
        }
 
@@ -1627,7 +1674,7 @@ int is_ves_available_changed(cJSON_bool new_bool)
        stringConfiguration = cJSON_Print(jsonConfig);
        writeConfigFile(stringConfiguration);
 
-       free(jsonConfig);
+       cJSON_Delete(jsonConfig);
 
        return SR_ERR_OK;
 }