Add simulator enhancements.
[sim/o1-interface.git] / ntsimulator / src / o-ran-notifications / o-ran-notifications.c
index 747bec9..3699e28 100644 (file)
@@ -1,9 +1,19 @@
-/*
- * o-ran-notifications.c
- *
- *  Created on: Oct 23, 2019
- *      Author: parallels
- */
+/*************************************************************************
+*
+* Copyright 2019 highstreet technologies GmbH and others
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+***************************************************************************/
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -13,6 +23,8 @@
 #include <time.h>
 #include <math.h>
 #include <sys/time.h>
+#include <cjson/cJSON.h>
+#include <string.h>
 
 #include "sysrepo.h"
 #include "sysrepo/values.h"
 
 volatile int exit_application = 0;
 
+static counterAlarms netconf_alarm_counter = {
+    .normal = 0,
+    .warning = 0,
+    .minor = 0,
+    .major = 0,
+    .critical = 0
+};
+static counterAlarms ves_alarm_counter= {
+    .normal = 0,
+    .warning = 0,
+    .minor = 0,
+    .major = 0,
+    .critical = 0
+};
+
 struct faultAlarms
 {
        int             faultId;
@@ -73,9 +100,8 @@ static int cleanup_curl()
 
 static int send_fault_ves_message(char *alarm_condition, char *alarm_object, char *severity, char *date_time, char *specific_problem, int port)
 {
-       int rc = SR_ERR_OK;
        CURLcode res;
-       static sequence_id = 0;
+       static int sequence_id = 0;
        int netconf_port_base = 0;
 
        prepare_ves_message_curl(curl);
@@ -86,23 +112,14 @@ static int send_fault_ves_message(char *alarm_condition, char *alarm_object, cha
        if (event == NULL)
        {
                printf("Could not create JSON object: event\n");
-               return 1;
+               return SR_ERR_OPERATION_FAILED;
        }
        cJSON_AddItemToObject(postDataJson, "event", event);
 
        char *hostname = getenv("HOSTNAME");
-       char *netconf_base_string = getenv("NETCONF_BASE");
 
-       if (netconf_base_string != NULL)
-       {
-               rc = sscanf(netconf_base_string, "%d", &netconf_port_base);
-               if (rc != 1)
-               {
-                       printf("Could not find the NETCONF base port, aborting the PNF registration...\n");
-                       return 1;
-               }
-               netconf_port_base += port;
-       }
+    netconf_port_base = getIntFromString(getenv("NETCONF_BASE"), 1);
+    netconf_port_base += port;
 
        char source_name[100];
        sprintf(source_name, "%s_%d", hostname, netconf_port_base);
@@ -111,7 +128,7 @@ static int send_fault_ves_message(char *alarm_condition, char *alarm_object, cha
        if (commonEventHeader == NULL)
        {
                printf("Could not create JSON object: commonEventHeader\n");
-               return 1;
+               return SR_ERR_OPERATION_FAILED;
        }
        cJSON_AddItemToObject(event, "commonEventHeader", commonEventHeader);
 
@@ -119,7 +136,11 @@ static int send_fault_ves_message(char *alarm_condition, char *alarm_object, cha
        if (faultFields == NULL)
        {
                printf("Could not create JSON object: faultFields\n");
-               return 1;
+               if (postDataJson != NULL)
+               {
+                       cJSON_Delete(postDataJson);
+               }
+               return SR_ERR_OPERATION_FAILED;
        }
        cJSON_AddItemToObject(event, "faultFields", faultFields);
 
@@ -147,41 +168,6 @@ static int send_fault_ves_message(char *alarm_condition, char *alarm_object, cha
        return SR_ERR_OK;
 }
 
-static int send_dummy_notif_file_mgmt(sr_session_ctx_t *sess)
-{
-       int rc;
-
-       sr_val_t *vnotif;
-       size_t current_num_of_values= 0;
-
-       CREATE_NEW_VALUE(rc, vnotif, current_num_of_values);
-
-       sr_val_build_xpath(&vnotif[current_num_of_values - 1], "%s", "/o-ran-file-management:file-upload-notification/local-logical-file-path");
-       sr_val_set_str_data(&vnotif[current_num_of_values - 1], SR_STRING_T, "odsanzucjsdoj");
-
-       CREATE_NEW_VALUE(rc, vnotif, current_num_of_values);
-
-       sr_val_build_xpath(&vnotif[current_num_of_values - 1], "%s", "/o-ran-file-management:file-upload-notification/remote-file-path");
-       sr_val_set_str_data(&vnotif[current_num_of_values - 1], SR_STRING_T, "jsdknvjnkfd");
-
-       CREATE_NEW_VALUE(rc, vnotif, current_num_of_values);
-
-       sr_val_build_xpath(&vnotif[current_num_of_values - 1], "%s", "/o-ran-file-management:file-upload-notification/status");
-       sr_val_set_str_data(&vnotif[current_num_of_values - 1], SR_ENUM_T, "SUCCESS");
-
-       rc = sr_event_notif_send(sess, "/o-ran-file-management:file-upload-notification", vnotif, current_num_of_values, SR_EV_NOTIF_DEFAULT);
-       if (rc != SR_ERR_OK) {
-               printf("Failed to send notification send_dummy_notif_file_mgmt\n");
-               return SR_ERR_OPERATION_FAILED;
-       }
-
-       printf("Successfully sent notification...\n");
-
-       sr_free_values(vnotif, current_num_of_values);
-
-       return SR_ERR_OK;
-}
-
 static int send_dummy_notif(sr_session_ctx_t *sess)
 {
        int rc;
@@ -280,6 +266,30 @@ static int send_dummy_notif(sr_session_ctx_t *sess)
                        printf("Failed to send notification send_dummy_notif\n");
                        return SR_ERR_OPERATION_FAILED;
                }
+               if (oran_fault_alarms[ran].cleared[random_port])
+               {
+                       netconf_alarm_counter.normal++;
+               }
+               else
+               {
+            if (strcmp(oran_fault_alarms[ran].faultSeverity, "WARNING") == 0)
+            {
+                netconf_alarm_counter.warning++;
+            }
+            else if (strcmp(oran_fault_alarms[ran].faultSeverity, "MINOR") == 0)
+            {
+                netconf_alarm_counter.minor++;
+            }
+            else if (strcmp(oran_fault_alarms[ran].faultSeverity, "MAJOR") == 0)
+            {
+                netconf_alarm_counter.major++;
+            }
+            else if (strcmp(oran_fault_alarms[ran].faultSeverity, "CRITICAL") == 0)
+            {
+                netconf_alarm_counter.critical++;
+            }
+               }
+               
                printf("Successfully sent notification with timestamp=\"%s\"\n", dateAndTime);
        }
        if (isVesAvailable)
@@ -292,7 +302,36 @@ static int send_dummy_notif(sr_session_ctx_t *sess)
                {
                        printf("Could not send Fault VES message\n");
                }
+        if (oran_fault_alarms[ran].cleared[random_port])
+               {
+                       ves_alarm_counter.normal++;
+               }
+               else
+               {
+            if (strcmp(oran_fault_alarms[ran].faultSeverity, "WARNING") == 0)
+            {
+                ves_alarm_counter.warning++;
+            }
+            else if (strcmp(oran_fault_alarms[ran].faultSeverity, "MINOR") == 0)
+            {
+                ves_alarm_counter.minor++;
+            }
+            else if (strcmp(oran_fault_alarms[ran].faultSeverity, "MAJOR") == 0)
+            {
+                ves_alarm_counter.major++;
+            }
+            else if (strcmp(oran_fault_alarms[ran].faultSeverity, "CRITICAL") == 0)
+            {
+                ves_alarm_counter.critical++;
+            }
+               }
        }
+    printf("Writing counters to file...\n");
+    rc = writeStatusNotificationCounters(ves_alarm_counter, netconf_alarm_counter);
+    if (rc != SR_ERR_OK)
+    {
+        printf("Could not write status to file...\n");
+    }
 
        sr_free_values(vnotif, current_num_of_values);
 
@@ -312,7 +351,6 @@ main(int argc, char **argv)
     sr_session_ctx_t *session = NULL;
     sr_subscription_ctx_t *subscription = NULL;
     int rc = SR_ERR_OK;
-    int notification_delay_period = 0; //seconds
 
     setbuf(stdout, NULL);
 
@@ -341,20 +379,44 @@ main(int argc, char **argv)
     signal(SIGINT, sigint_handler);
     signal(SIGPIPE, SIG_IGN);
 
-
     while (!exit_application) {
-        notification_delay_period = getFaultNotificationDelayPeriodFromConfigJson();
+        int notif_delay[100], count = 0;
 
-        if (notification_delay_period > 0)
+        rc = getFaultNotificationDelayPeriodFromConfigJson(notif_delay, &count);
+        if (rc != SR_ERR_OK)
         {
-               send_dummy_notif(session);
-//             send_dummy_notif_file_mgmt(session);
+            printf("Could not get fault notification delay period.");
+            sleep(1);
+            continue;
+        }
 
-            sleep(notification_delay_period);
+        if (count > 1)
+        {
+            for (int i = 0; i < count; ++i)
+            {
+                sleep(notif_delay[i]);
+                send_dummy_notif(session);                
+            }
         }
-        else
+        else if (count == 1)
         {
-               sleep(1);
+            if (notif_delay[0] > 0)
+            {
+                sleep(notif_delay[0]);
+                send_dummy_notif(session);
+            }
+            else 
+            {
+                sleep(1);
+                // reset the counters when the notifciation delay period is switched back to 0
+                netconf_alarm_counter.normal = netconf_alarm_counter.warning = \
+                netconf_alarm_counter.minor = netconf_alarm_counter.major = \
+                netconf_alarm_counter.critical = 0;
+                
+                ves_alarm_counter.normal = ves_alarm_counter.warning = \
+                ves_alarm_counter.minor = ves_alarm_counter.major = \
+                ves_alarm_counter.critical = 0;
+            }
         }
 
     }