1 /*************************************************************************
3 * Copyright 2019 highstreet technologies GmbH and others
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 ***************************************************************************/
28 #include "sysrepo/values.h"
32 #define LINE_BUFSIZE 128
33 #define ORAN_FAULT_ALARMS_NUMBER 10
34 #define AFFECTED_OBJECTS_MAX_NUMBER 100
36 volatile int exit_application = 0;
45 char* affectedObjects[AFFECTED_OBJECTS_MAX_NUMBER];
47 struct faultAlarms oran_fault_alarms[ORAN_FAULT_ALARMS_NUMBER] = {
48 {.faultId = 1, .faultSource = "jknsdfnui", .affectedObjects = {"akddconoj", "asodmnjvf", "roiemfkmods"}, .cleared = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, .faultSeverity = "MAJOR", .faultText = "sdnjosopnojnsd"},
49 {.faultId = 2, .faultSource = "onascokjnasc", .affectedObjects = {"sdouvncsjdfv13", "asjdn13ejlncd4"}, .cleared = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, .faultSeverity = "WARNING", .faultText = "4pionfcsofn42on"},
50 {.faultId = 3, .faultSource = "asonxpkn", .affectedObjects = {"0j4fiwef320fd", "sdlvkmsdv-9023"}, .cleared = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, .faultSeverity = "CRITICAL", .faultText = "sdjnonj32onjsa23"},
51 {.faultId = 4, .faultSource = "asnjcpkd", .affectedObjects = {"0j4fiwef320fd", "sdlvkmsdv-9023", "laksmdklmdas21"}, .cleared = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, .faultSeverity = "MINOR", .faultText = "asdjln12osa453"},
52 {.faultId = 5, .faultSource = "dskmfl", .affectedObjects = {"sdkm31wdlk"}, .cleared = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, .faultSeverity = "MAJOR", .faultText = "dknovrf34ekl"},
53 {.faultId = 6, .faultSource = "dsllkje232kl", .affectedObjects = {"sFKOM24KLMerw"}, .cleared = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, .faultSeverity = "MAJOR", .faultText = "frpkm24k lsd kmewfpm"},
54 {.faultId = 7, .faultSource = "fvkdlsfjnwej23kloe", .affectedObjects = {"fvkm24km", "sdfk23d", "kmdfkmo32", "wekl2332"}, .cleared = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, .faultSeverity = "WARNING", .faultText = "dsm 2d 32j sdfmr32"},
55 {.faultId = 8, .faultSource = "dkom32", .affectedObjects = {"kmsdfkpm23ds", "sdmkp32"}, .cleared = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, .faultSeverity = "CRITICAL", .faultText = "dsonj32 don32 mdson32pk654"},
56 {.faultId = 9, .faultSource = "weflm3", .affectedObjects = {"klklm32kl3", "dsfln234poewj23-", "spmd32k"}, .cleared = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, .faultSeverity = "MINOR", .faultText = "dsflknjwej32"},
57 {.faultId = 10, .faultSource = "fweiunvfrem32", .affectedObjects = {"sfkm23klsdf2343"}, .cleared = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, .faultSeverity = "MAJOR", .faultText = "dfskjnl4j dsfknl2 fodn54 65k"}
62 static int _init_curl()
64 curl = curl_easy_init();
67 printf("cURL initialization error! Aborting call!\n");
68 return SR_ERR_OPERATION_FAILED;
74 static int cleanup_curl()
78 curl_easy_cleanup(curl);
84 static int send_fault_ves_message(char *alarm_condition, char *alarm_object, char *severity, char *date_time, char *specific_problem, int port)
88 static int sequence_id = 0;
89 int netconf_port_base = 0;
91 prepare_ves_message_curl(curl);
93 cJSON *postDataJson = cJSON_CreateObject();
95 cJSON *event = cJSON_CreateObject();
98 printf("Could not create JSON object: event\n");
101 cJSON_AddItemToObject(postDataJson, "event", event);
103 char *hostname = getenv("HOSTNAME");
104 char *netconf_base_string = getenv("NETCONF_BASE");
106 if (netconf_base_string != NULL)
108 rc = sscanf(netconf_base_string, "%d", &netconf_port_base);
111 printf("Could not find the NETCONF base port, aborting the PNF registration...\n");
114 netconf_port_base += port;
117 char source_name[100];
118 sprintf(source_name, "%s_%d", hostname, netconf_port_base);
120 cJSON *commonEventHeader = vesCreateCommonEventHeader("fault", "O_RAN_COMPONENT_Alarms", source_name, sequence_id++);
121 if (commonEventHeader == NULL)
123 printf("Could not create JSON object: commonEventHeader\n");
126 cJSON_AddItemToObject(event, "commonEventHeader", commonEventHeader);
128 cJSON *faultFields = vesCreateFaultFields(alarm_condition, alarm_object, severity, date_time, specific_problem);
129 if (faultFields == NULL)
131 printf("Could not create JSON object: faultFields\n");
132 if (postDataJson != NULL)
134 cJSON_Delete(postDataJson);
138 cJSON_AddItemToObject(event, "faultFields", faultFields);
140 char *post_data_string = NULL;
142 post_data_string = cJSON_PrintUnformatted(postDataJson);
144 printf("Post data JSON:\n%s\n", post_data_string);
146 if (postDataJson != NULL)
148 cJSON_Delete(postDataJson);
151 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data_string);
153 res = curl_easy_perform(curl);
157 printf("Failed to send cURL...\n");
158 return SR_ERR_OPERATION_FAILED;
164 static int send_dummy_notif(sr_session_ctx_t *sess)
168 char dateAndTime[256];
169 time_t t = time(NULL);
170 struct tm tm = *localtime(&t);
174 gettimeofday(&tv, NULL);
175 millisec = lrint(tv.tv_usec/1000.0); // Round to nearest millisec
177 { // Allow for rounding up to nearest second
182 sprintf(dateAndTime, "%04d-%02d-%02dT%02d:%02d:%02d.%01dZ",
183 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
184 tm.tm_hour, tm.tm_min, tm.tm_sec, millisec/100);
186 int ran = (int) random_at_most(ORAN_FAULT_ALARMS_NUMBER - 1);
188 //TODO we hardcode here the number of ports for each device, 10
189 int random_port = (int) random_at_most(9);
191 if (oran_fault_alarms[ran].cleared[random_port] == 1)
193 oran_fault_alarms[ran].cleared[random_port] = 0;
197 oran_fault_alarms[ran].cleared[random_port] = 1;
201 size_t current_num_of_values= 0;
203 CREATE_NEW_VALUE(rc, vnotif, current_num_of_values);
205 sr_val_build_xpath(&vnotif[current_num_of_values - 1], "%s", "/o-ran-fm:alarm-notif/fault-id");
206 vnotif[current_num_of_values - 1].type = SR_UINT16_T;
207 vnotif[current_num_of_values - 1].data.uint16_val = oran_fault_alarms[ran].faultId;
209 CREATE_NEW_VALUE(rc, vnotif, current_num_of_values);
211 sr_val_build_xpath(&vnotif[current_num_of_values - 1], "%s", "/o-ran-fm:alarm-notif/fault-source");
212 sr_val_set_str_data(&vnotif[current_num_of_values - 1], SR_STRING_T, oran_fault_alarms[ran].faultSource);
214 CREATE_NEW_VALUE(rc, vnotif, current_num_of_values);
216 sr_val_build_xpath(&vnotif[current_num_of_values - 1], "%s", "/o-ran-fm:alarm-notif/fault-severity");
217 sr_val_set_str_data(&vnotif[current_num_of_values - 1], SR_ENUM_T, oran_fault_alarms[ran].faultSeverity);
219 CREATE_NEW_VALUE(rc, vnotif, current_num_of_values);
221 sr_val_build_xpath(&vnotif[current_num_of_values - 1], "%s", "/o-ran-fm:alarm-notif/is-cleared");
222 vnotif[current_num_of_values - 1].type = SR_BOOL_T;
223 vnotif[current_num_of_values - 1].data.bool_val = oran_fault_alarms[ran].cleared[random_port];
225 CREATE_NEW_VALUE(rc, vnotif, current_num_of_values);
227 sr_val_build_xpath(&vnotif[current_num_of_values - 1], "%s", "/o-ran-fm:alarm-notif/fault-text");
228 sr_val_set_str_data(&vnotif[current_num_of_values - 1], SR_STRING_T, oran_fault_alarms[ran].faultText);
230 CREATE_NEW_VALUE(rc, vnotif, current_num_of_values);
232 sr_val_build_xpath(&vnotif[current_num_of_values - 1], "%s", "/o-ran-fm:alarm-notif/event-time");
233 sr_val_build_str_data(&vnotif[current_num_of_values - 1], SR_STRING_T, "%s", dateAndTime);
235 for (int i = 0; i < AFFECTED_OBJECTS_MAX_NUMBER; ++i)
238 if (oran_fault_alarms[ran].affectedObjects[i] == NULL)
243 sprintf(path, "/o-ran-fm:alarm-notif/affected-objects[name='%s']", oran_fault_alarms[ran].affectedObjects[i]);
245 CREATE_NEW_VALUE(rc, vnotif, current_num_of_values);
247 sr_val_build_xpath(&vnotif[current_num_of_values - 1], "%s", path);
248 vnotif[current_num_of_values - 1].type = SR_LIST_T;
251 int isNetconfAvailable = getNetconfAvailableFromConfigJson();
252 int isVesAvailable = getVesAvailableFromConfigJson();
254 if (isNetconfAvailable)
256 rc = sr_event_notif_send(sess, "/o-ran-fm:alarm-notif", vnotif, current_num_of_values, SR_EV_NOTIF_DEFAULT);
259 printf("Failed to send notification send_dummy_notif\n");
260 return SR_ERR_OPERATION_FAILED;
262 printf("Successfully sent notification with timestamp=\"%s\"\n", dateAndTime);
267 sprintf(faultId, "%d", oran_fault_alarms[ran].faultId);
268 rc = send_fault_ves_message(faultId, oran_fault_alarms[ran].faultSource,
269 (oran_fault_alarms[ran].cleared[random_port]) ? "NORMAL" : oran_fault_alarms[ran].faultSeverity, dateAndTime, oran_fault_alarms[ran].faultText, random_port);
272 printf("Could not send Fault VES message\n");
276 sr_free_values(vnotif, current_num_of_values);
282 sigint_handler(int signum)
284 exit_application = 1;
288 main(int argc, char **argv)
290 sr_conn_ctx_t *connection = NULL;
291 sr_session_ctx_t *session = NULL;
292 sr_subscription_ctx_t *subscription = NULL;
294 int notification_delay_period = 0; //seconds
296 setbuf(stdout, NULL);
298 /* connect to sysrepo */
299 rc = sr_connect("oran_notifications", SR_CONN_DEFAULT, &connection);
300 if (SR_ERR_OK != rc) {
301 fprintf(stderr, "Error by sr_connect: %s\n", sr_strerror(rc));
306 rc = sr_session_start(connection, SR_DS_RUNNING, SR_SESS_DEFAULT, &session);
307 if (SR_ERR_OK != rc) {
308 fprintf(stderr, "Error by sr_session_start: %s\n", sr_strerror(rc));
315 fprintf(stderr, "Could not initialize cURL: %s\n", sr_strerror(rc));
319 /* loop until ctrl-c is pressed / SIGINT is received */
320 signal(SIGINT, sigint_handler);
321 signal(SIGPIPE, SIG_IGN);
324 while (!exit_application) {
325 notification_delay_period = getFaultNotificationDelayPeriodFromConfigJson();
327 if (notification_delay_period > 0)
329 send_dummy_notif(session);
331 sleep(notification_delay_period);
340 printf("Application exit requested, exiting.\n");
343 if (NULL != subscription) {
344 sr_unsubscribe(session, subscription);
346 if (NULL != session) {
347 sr_session_stop(session);
349 if (NULL != connection) {
350 sr_disconnect(connection);
353 printf("Error encountered. Exiting...");