Add counters for generated notifications.
[sim/o1-interface.git] / ntsimulator / src / ntsimulator-manager / ntsimulator-manager.c
index b5555c4..836fc6f 100644 (file)
@@ -117,7 +117,7 @@ print_current_config(sr_session_ctx_t *session, const char *module_name)
    sr_free_val(odl_username);
    sr_free_val(odl_password);
 
-    sr_free_values(values, count);
+   sr_free_values(values, count);
 }
 
 static void clean_current_docker_configuration(void);
@@ -184,7 +184,7 @@ simulator_config_change_cb(sr_session_ctx_t *session, const char *module_name, s
     printf("\n\n ========== CONFIG HAS CHANGED, CURRENT RUNNING CONFIG %s: ==========\n\n", module_name);
     print_current_config(session, module_name);
 
-    sr_val_t *val;
+    sr_val_t *val = NULL;
 
     /* get the value from sysrepo, we do not care if the value did not change in our case */
     rc = sr_get_item(session, "/network-topology-simulator:simulator-config/simulated-devices", &val);
@@ -198,6 +198,7 @@ simulator_config_change_cb(sr_session_ctx_t *session, const char *module_name, s
     }
 
     sr_free_val(val);
+       val = NULL;
 
     /* get the value from sysrepo, we do not care if the value did not change in our case */
     rc = sr_get_item(session, "/network-topology-simulator:simulator-config/mounted-devices", &val);
@@ -211,6 +212,7 @@ simulator_config_change_cb(sr_session_ctx_t *session, const char *module_name, s
        {
                printf("Cannot set mount value greater than number of simulated devices.\n");
                sr_free_val(val);
+                       val = NULL;
                return SR_ERR_OK;
        }
 
@@ -221,6 +223,7 @@ simulator_config_change_cb(sr_session_ctx_t *session, const char *module_name, s
     }
 
     sr_free_val(val);
+       val = NULL;
 
     /* get the value from sysrepo, we do not care if the value did not change in our case */
     rc = sr_get_item(session, "/network-topology-simulator:simulator-config/notification-config/fault-notification-delay-period", &val);
@@ -234,6 +237,7 @@ simulator_config_change_cb(sr_session_ctx_t *session, const char *module_name, s
     }
 
     sr_free_val(val);
+       val = NULL;
 
     /* get the value from sysrepo, we do not care if the value did not change in our case */
        rc = sr_get_item(session, "/network-topology-simulator:simulator-config/notification-config/ves-heartbeat-period", &val);
@@ -247,6 +251,7 @@ simulator_config_change_cb(sr_session_ctx_t *session, const char *module_name, s
        }
 
        sr_free_val(val);
+       val = NULL;
 
        /* get the value from sysrepo, we do not care if the value did not change in our case */
        rc = sr_get_item(session, "/network-topology-simulator:simulator-config/ves-endpoint-details/ves-endpoint-ip", &val);
@@ -260,6 +265,7 @@ simulator_config_change_cb(sr_session_ctx_t *session, const char *module_name, s
        }
 
        sr_free_val(val);
+       val = NULL;
 
        /* get the value from sysrepo, we do not care if the value did not change in our case */
        rc = sr_get_item(session, "/network-topology-simulator:simulator-config/ves-endpoint-details/ves-endpoint-port", &val);
@@ -273,6 +279,7 @@ simulator_config_change_cb(sr_session_ctx_t *session, const char *module_name, s
        }
 
        sr_free_val(val);
+       val = NULL;
 
        /* get the value from sysrepo, we do not care if the value did not change in our case */
        rc = sr_get_item(session, "/network-topology-simulator:simulator-config/ves-endpoint-details/ves-registration", &val);
@@ -286,6 +293,7 @@ simulator_config_change_cb(sr_session_ctx_t *session, const char *module_name, s
        }
 
        sr_free_val(val);
+       val = NULL;
 
        /* get the value from sysrepo, we do not care if the value did not change in our case */
        rc = sr_get_item(session, "/network-topology-simulator:simulator-config/notification-config/is-netconf-available", &val);
@@ -299,6 +307,7 @@ simulator_config_change_cb(sr_session_ctx_t *session, const char *module_name, s
        }
 
        sr_free_val(val);
+       val = NULL;
 
        /* get the value from sysrepo, we do not care if the value did not change in our case */
        rc = sr_get_item(session, "/network-topology-simulator:simulator-config/notification-config/is-ves-available", &val);
@@ -312,11 +321,17 @@ simulator_config_change_cb(sr_session_ctx_t *session, const char *module_name, s
        }
 
        sr_free_val(val);
+       val = NULL;
 
     return SR_ERR_OK;
 
 sr_error:
        printf("NTSimulator config change callback failed: %s.", sr_strerror(rc));
+       if (val != NULL)
+       {
+               sr_free_val(val);
+               val = NULL;
+       }
        return rc;
 }
 
@@ -326,7 +341,14 @@ simulator_status_cb(const char *xpath, sr_val_t **values, size_t *values_cnt,
 {
        int rc;
 
-       printf("\n\n ========== Called simulator_status_cb for xpath: %s ==========\n\n", xpath);
+       // printf("\n\n ========== Called simulator_status_cb for xpath: %s ==========\n\n", xpath);
+
+    counterAlarms ves_counter, netconf_counter;
+    rc = compute_notifications_count(&ves_counter, &netconf_counter);
+    if (rc != SR_ERR_OK)
+    {
+        printf("Could not compute the total number of notification count.\n");
+    }
 
        if (sr_xpath_node_name_eq(xpath, "simulated-devices-list")) {
                sr_val_t *v;
@@ -344,12 +366,20 @@ simulator_status_cb(const char *xpath, sr_val_t **values, size_t *values_cnt,
                if (rc != SR_ERR_OK)
                {
                        printf("Could not get the operational state for the devices simulated.\n");
+                       return SR_ERR_OPERATION_FAILED;
                }
 
                device_t *current_device = device_list->head;
 
                while (current_device != NULL)
                {
+            counterAlarms vesCount, netconfCount;
+            rc = getDeviceCounters(current_device->device_id, &vesCount, &netconfCount);
+            if (rc != SR_ERR_OK)
+            {
+                printf("Could not get Notification Counters for device with uuid=\"%s\"", current_device->device_id);
+            }            
+
                        CREATE_NEW_VALUE(rc, v, current_num_of_values);
 
                        sr_val_build_xpath(&v[current_num_of_values - 1], "%s[uuid='%s']/%s", xpath, current_device->device_id, "device-ip");
@@ -378,6 +408,66 @@ simulator_status_cb(const char *xpath, sr_val_t **values, size_t *values_cnt,
                        sr_val_build_xpath(&v[current_num_of_values - 1], "%s[uuid='%s']/%s", xpath, current_device->device_id, "operational-state");
                        sr_val_build_str_data(&v[current_num_of_values - 1], SR_ENUM_T, "%s", operational_state);
 
+            CREATE_NEW_VALUE(rc, v, current_num_of_values);
+
+            sr_val_build_xpath(&v[current_num_of_values - 1], "%s[uuid='%s']/notification-count/ves-notifications/%s", xpath, current_device->device_id, "normal");
+            v[current_num_of_values - 1].type = SR_UINT32_T;
+            v[current_num_of_values - 1].data.uint32_val = vesCount.normal;
+
+            CREATE_NEW_VALUE(rc, v, current_num_of_values);
+
+            sr_val_build_xpath(&v[current_num_of_values - 1], "%s[uuid='%s']/notification-count/ves-notifications/%s", xpath, current_device->device_id, "warning");
+            v[current_num_of_values - 1].type = SR_UINT32_T;
+            v[current_num_of_values - 1].data.uint32_val = vesCount.warning;
+
+            CREATE_NEW_VALUE(rc, v, current_num_of_values);
+
+            sr_val_build_xpath(&v[current_num_of_values - 1], "%s[uuid='%s']/notification-count/ves-notifications/%s", xpath, current_device->device_id, "minor");
+            v[current_num_of_values - 1].type = SR_UINT32_T;
+            v[current_num_of_values - 1].data.uint32_val = vesCount.minor;
+
+            CREATE_NEW_VALUE(rc, v, current_num_of_values);
+
+            sr_val_build_xpath(&v[current_num_of_values - 1], "%s[uuid='%s']/notification-count/ves-notifications/%s", xpath, current_device->device_id, "major");
+            v[current_num_of_values - 1].type = SR_UINT32_T;
+            v[current_num_of_values - 1].data.uint32_val = vesCount.major;
+
+            CREATE_NEW_VALUE(rc, v, current_num_of_values);
+
+            sr_val_build_xpath(&v[current_num_of_values - 1], "%s[uuid='%s']/notification-count/ves-notifications/%s", xpath, current_device->device_id, "critical");
+            v[current_num_of_values - 1].type = SR_UINT32_T;
+            v[current_num_of_values - 1].data.uint32_val = vesCount.critical;
+
+            CREATE_NEW_VALUE(rc, v, current_num_of_values);
+
+            sr_val_build_xpath(&v[current_num_of_values - 1], "%s[uuid='%s']/notification-count/netconf-notifications/%s", xpath, current_device->device_id, "normal");
+            v[current_num_of_values - 1].type = SR_UINT32_T;
+            v[current_num_of_values - 1].data.uint32_val = netconfCount.normal;
+
+            CREATE_NEW_VALUE(rc, v, current_num_of_values);
+
+            sr_val_build_xpath(&v[current_num_of_values - 1], "%s[uuid='%s']/notification-count/netconf-notifications/%s", xpath, current_device->device_id, "warning");
+            v[current_num_of_values - 1].type = SR_UINT32_T;
+            v[current_num_of_values - 1].data.uint32_val = netconfCount.warning;
+
+            CREATE_NEW_VALUE(rc, v, current_num_of_values);
+
+            sr_val_build_xpath(&v[current_num_of_values - 1], "%s[uuid='%s']/notification-count/netconf-notifications/%s", xpath, current_device->device_id, "minor");
+            v[current_num_of_values - 1].type = SR_UINT32_T;
+            v[current_num_of_values - 1].data.uint32_val = netconfCount.minor;
+
+            CREATE_NEW_VALUE(rc, v, current_num_of_values);
+
+            sr_val_build_xpath(&v[current_num_of_values - 1], "%s[uuid='%s']/notification-count/netconf-notifications/%s", xpath, current_device->device_id, "major");
+            v[current_num_of_values - 1].type = SR_UINT32_T;
+            v[current_num_of_values - 1].data.uint32_val = netconfCount.major;
+
+            CREATE_NEW_VALUE(rc, v, current_num_of_values);
+
+            sr_val_build_xpath(&v[current_num_of_values - 1], "%s[uuid='%s']/notification-count/netconf-notifications/%s", xpath, current_device->device_id, "critical");
+            v[current_num_of_values - 1].type = SR_UINT32_T;
+            v[current_num_of_values - 1].data.uint32_val = netconfCount.critical;
+
                        current_device = current_device->next;
                }
 
@@ -437,6 +527,86 @@ simulator_status_cb(const char *xpath, sr_val_t **values, size_t *values_cnt,
                *values = v;
                *values_cnt = current_num_of_values;
         }
+     else if (sr_xpath_node_name_eq(xpath, "total-ves-notifications"))
+     {
+        sr_val_t *v;
+        /* convenient functions such as this can be found in sysrepo/values.h */
+        size_t current_num_of_values= 0;
+
+        CREATE_NEW_VALUE(rc, v, current_num_of_values);
+
+        sr_val_build_xpath(&v[current_num_of_values - 1], "%s/%s", xpath, "normal");
+        v[current_num_of_values - 1].type = SR_UINT32_T;
+        v[current_num_of_values - 1].data.uint32_val = ves_counter.normal;
+
+        CREATE_NEW_VALUE(rc, v, current_num_of_values);
+
+        sr_val_build_xpath(&v[current_num_of_values - 1], "%s/%s", xpath, "warning");
+        v[current_num_of_values - 1].type = SR_UINT32_T;
+        v[current_num_of_values - 1].data.uint32_val = ves_counter.warning;
+
+        CREATE_NEW_VALUE(rc, v, current_num_of_values);
+
+        sr_val_build_xpath(&v[current_num_of_values - 1], "%s/%s", xpath, "minor");
+        v[current_num_of_values - 1].type = SR_UINT32_T;
+        v[current_num_of_values - 1].data.uint32_val = ves_counter.minor;
+
+        CREATE_NEW_VALUE(rc, v, current_num_of_values);
+
+        sr_val_build_xpath(&v[current_num_of_values - 1], "%s/%s", xpath, "major");
+        v[current_num_of_values - 1].type = SR_UINT32_T;
+        v[current_num_of_values - 1].data.uint32_val = ves_counter.major;
+
+        CREATE_NEW_VALUE(rc, v, current_num_of_values);
+
+        sr_val_build_xpath(&v[current_num_of_values - 1], "%s/%s", xpath, "critical");
+        v[current_num_of_values - 1].type = SR_UINT32_T;
+        v[current_num_of_values - 1].data.uint32_val = ves_counter.critical;
+
+        //return the values that we have just created
+        *values = v;
+        *values_cnt = current_num_of_values;
+     }
+     else if (sr_xpath_node_name_eq(xpath, "total-netconf-notifications"))
+     {
+        sr_val_t *v;
+        /* convenient functions such as this can be found in sysrepo/values.h */
+        size_t current_num_of_values= 0;
+
+        CREATE_NEW_VALUE(rc, v, current_num_of_values);
+
+        sr_val_build_xpath(&v[current_num_of_values - 1], "%s/%s", xpath, "normal");
+        v[current_num_of_values - 1].type = SR_UINT32_T;
+        v[current_num_of_values - 1].data.uint32_val = netconf_counter.normal;
+
+        CREATE_NEW_VALUE(rc, v, current_num_of_values);
+
+        sr_val_build_xpath(&v[current_num_of_values - 1], "%s/%s", xpath, "warning");
+        v[current_num_of_values - 1].type = SR_UINT32_T;
+        v[current_num_of_values - 1].data.uint32_val = netconf_counter.warning;
+
+        CREATE_NEW_VALUE(rc, v, current_num_of_values);
+
+        sr_val_build_xpath(&v[current_num_of_values - 1], "%s/%s", xpath, "minor");
+        v[current_num_of_values - 1].type = SR_UINT32_T;
+        v[current_num_of_values - 1].data.uint32_val = netconf_counter.minor;
+
+        CREATE_NEW_VALUE(rc, v, current_num_of_values);
+
+        sr_val_build_xpath(&v[current_num_of_values - 1], "%s/%s", xpath, "major");
+        v[current_num_of_values - 1].type = SR_UINT32_T;
+        v[current_num_of_values - 1].data.uint32_val = netconf_counter.major;
+
+        CREATE_NEW_VALUE(rc, v, current_num_of_values);
+
+        sr_val_build_xpath(&v[current_num_of_values - 1], "%s/%s", xpath, "critical");
+        v[current_num_of_values - 1].type = SR_UINT32_T;
+        v[current_num_of_values - 1].data.uint32_val = netconf_counter.critical;
+
+        //return the values that we have just created
+        *values = v;
+        *values_cnt = current_num_of_values;
+     }
 
     return SR_ERR_OK;
 }
@@ -445,7 +615,6 @@ int odl_add_key_pair_cb(const char *xpath, const sr_val_t *input, const size_t i
                sr_val_t **output, size_t *output_cnt, void *private_ctx)
 {
        int rc = SR_ERR_OK;
-    sr_session_ctx_t *session = (sr_session_ctx_t *)private_ctx;
        controller_t controller_list[CONTROLLER_LIST_MAX_LEN];
        int controller_list_size = 0;
 
@@ -461,6 +630,7 @@ int odl_add_key_pair_cb(const char *xpath, const sr_val_t *input, const size_t i
        if (rc != SR_ERR_OK)
        {
                printf("Failed to add key pair to ODL.\n");
+               return SR_ERR_OPERATION_FAILED;
        }
 
        return rc;
@@ -540,6 +710,12 @@ main(int argc, char **argv)
 
        printf("\n\n ========== STARTUP CONFIG network-topology-simulator APPLIED AS RUNNING ==========\n\n");
 
+    rc = writeSkeletonStatusFile();
+    if (rc != SR_ERR_OK)
+    {
+        fprintf(stderr, "Could not initialize status JSON file: %s\n", sr_strerror(rc));
+    }
+
     /* loop until ctrl-c is pressed / SIGINT is received */
     signal(SIGINT, sigint_handler);
     signal(SIGTERM, sigint_handler);