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 ***************************************************************************/
25 void set_curl_common_info_ves(CURL *curl)
27 struct curl_slist *chunk = NULL;
28 chunk = curl_slist_append(chunk, "Content-Type: application/json");
29 chunk = curl_slist_append(chunk, "Accept: application/json");
31 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
33 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 2L); // seconds timeout for a connection
34 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5L); //seconds timeout for an operation
36 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
39 void getCurrentDateAndTime(char *date_and_time)
41 time_t t = time(NULL);
42 struct tm tm = *localtime(&t);
46 gettimeofday(&tv, NULL);
47 millisec = lrint(tv.tv_usec/1000.0); // Round to nearest millisec
49 { // Allow for rounding up to nearest second
54 sprintf(date_and_time, "%04d-%02d-%02dT%02d:%02d:%02d.%01dZ",
55 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
56 tm.tm_hour, tm.tm_min, tm.tm_sec, millisec/100);
61 void generateRandomMacAddress(char *mac_address)
63 long rand1, rand2, rand3, rand4, rand5, rand6;
64 rand1 = random_at_most(255);
65 rand2 = random_at_most(255);
66 rand3 = random_at_most(255);
67 rand4 = random_at_most(255);
68 rand5 = random_at_most(255);
69 rand6 = random_at_most(255);
71 sprintf(mac_address, "%02X:%02X:%02X:%02X:%02X:%02X", rand1, rand2, rand3, rand4, rand5, rand6);
76 long random_at_most(long max) {
78 // max <= RAND_MAX < ULONG_MAX, so this is okay.
79 num_bins = (unsigned long) max + 1,
80 num_rand = (unsigned long) RAND_MAX + 1,
81 bin_size = num_rand / num_bins,
82 defect = num_rand % num_bins;
88 // This is carefully written not to overflow
89 while (num_rand - defect <= (unsigned long)x);
91 // Truncated division is intentional
95 int getSecondsFromLastQuarterInterval(void)
97 time_t t = time(NULL);
98 time_t t_past = time(NULL);
99 struct tm tm = *localtime(&t);
100 struct tm tm_15_min_ago = tm;
102 //round to the last quarter hour
103 tm_15_min_ago.tm_min -= (tm_15_min_ago.tm_min % 15);
104 tm_15_min_ago.tm_sec = 0;
106 t=mktime(&tm_15_min_ago);
109 double seconds = difftime(t_past, t);
114 int getSecondsFromLastDayInterval(void)
116 time_t t = time(NULL);
117 time_t t_past = time(NULL);
118 struct tm tm = *localtime(&t);
119 struct tm tm_day_ago = tm;
121 //round to the last quarter hour
122 tm_day_ago.tm_hour = 0;
123 tm_day_ago.tm_min = 0;
124 tm_day_ago.tm_sec = 0;
126 t=mktime(&tm_day_ago);
129 double seconds = difftime(t_past, t);
134 void getPreviousQuarterInterval(int number_of_intervals, char *date_and_time)
136 time_t t = time(NULL);
137 t -= 15 * 60 * number_of_intervals;
138 struct tm tm = *localtime(&t);
140 tm.tm_min -= (tm.tm_min % 15);
143 sprintf(date_and_time, "%04d-%02d-%02dT%02d:%02d:%02d.0Z",
144 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
145 tm.tm_hour, tm.tm_min, tm.tm_sec);
150 void getPreviousDayPmTimestamp(int number_of_intervals, char *date_and_time)
152 time_t t = time(NULL);
153 t -= 24 * 60 * 60 * number_of_intervals;
154 struct tm tm = *localtime(&t);
160 sprintf(date_and_time, "%04d-%02d-%02dT%02d:%02d:%02d.0Z",
161 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
162 tm.tm_hour, tm.tm_min, tm.tm_sec);
167 long int getMicrosecondsSinceEpoch(void)
169 time_t t = time(NULL);
170 struct tm tm = *localtime(&t);
174 gettimeofday(&tv, NULL);
175 useconds = t*1000 + tv.tv_usec; //add the microseconds to the seconds
180 //TODO need to implement other authentication methods as well, not only no-auth
181 void prepare_ves_message_curl(CURL *curl)
183 curl_easy_reset(curl);
184 set_curl_common_info_ves(curl);
186 char *ves_ip = getVesIpFromConfigJson();
187 int ves_port = getVesPortFromConfigJson();
190 sprintf(url, "http://%s:%d/eventListener/v7", ves_ip, ves_port);
191 curl_easy_setopt(curl, CURLOPT_URL, url);
195 // curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
200 cJSON* vesCreateCommonEventHeader(char *domain, char *event_type, char *source_name, int seq_id)
202 char dateAndTime[50];
203 getCurrentDateAndTime(dateAndTime);
205 long int useconds = getMicrosecondsSinceEpoch;
207 cJSON *commonEventHeader = cJSON_CreateObject();
208 if (commonEventHeader == NULL)
210 printf("Could not create JSON object: commonEventHeader\n");
214 if (cJSON_AddStringToObject(commonEventHeader, "domain", domain) == NULL)
216 printf("Could not create JSON object: domain\n");
221 sprintf(eventId, "%s_%s", source_name, dateAndTime);
223 if (cJSON_AddStringToObject(commonEventHeader, "eventId", eventId) == NULL)
225 printf("Could not create JSON object: eventId\n");
229 char event_name[200];
230 sprintf(event_name, "%s_%s", domain, event_type);
232 if (cJSON_AddStringToObject(commonEventHeader, "eventName", event_name) == NULL)
234 printf("Could not create JSON object: eventName\n");
238 if (cJSON_AddStringToObject(commonEventHeader, "eventType", event_type) == NULL)
240 printf("Could not create JSON object: eventType\n");
244 if (cJSON_AddNumberToObject(commonEventHeader, "sequence", (double)(seq_id)) == NULL)
246 printf("Could not create JSON object: sequence\n");
250 if (cJSON_AddStringToObject(commonEventHeader, "priority", "Low") == NULL)
252 printf("Could not create JSON object: priority\n");
256 if (cJSON_AddStringToObject(commonEventHeader, "reportingEntityId", "") == NULL)
258 printf("Could not create JSON object: reportingEntityId\n");
262 if (cJSON_AddStringToObject(commonEventHeader, "reportingEntityName", source_name) == NULL)
264 printf("Could not create JSON object: reportingEntityName\n");
268 if (cJSON_AddStringToObject(commonEventHeader, "sourceId", "") == NULL)
270 printf("Could not create JSON object: sourceId\n");
274 if (cJSON_AddStringToObject(commonEventHeader, "sourceName", source_name) == NULL)
276 printf("Could not create JSON object: sourceName\n");
280 if (cJSON_AddNumberToObject(commonEventHeader, "startEpochMicrosec", (double)(useconds)) == NULL)
282 printf("Could not create JSON object: startEpochMicrosec\n");
286 if (cJSON_AddNumberToObject(commonEventHeader, "lastEpochMicrosec", (double)(useconds)) == NULL)
288 printf("Could not create JSON object: lastEpochMicrosec\n");
292 if (cJSON_AddStringToObject(commonEventHeader, "nfNamingCode", "sdn controller") == NULL)
294 printf("Could not create JSON object: nfNamingCode\n");
298 if (cJSON_AddStringToObject(commonEventHeader, "nfVendorName", "sdn") == NULL)
300 printf("Could not create JSON object: nfVendorName\n");
304 if (cJSON_AddStringToObject(commonEventHeader, "timeZoneOffset", "+00:00") == NULL)
306 printf("Could not create JSON object: timeZoneOffset\n");
310 if (cJSON_AddStringToObject(commonEventHeader, "version", "4.0.1") == NULL)
312 printf("Could not create JSON object: version\n");
316 if (cJSON_AddStringToObject(commonEventHeader, "vesEventListenerVersion", "7.0.1") == NULL)
318 printf("Could not create JSON object: vesEventListenerVersion\n");
322 return commonEventHeader;
325 cJSON* vesCreateHeartbeatFields(int heartbeat_interval)
327 char dateAndTime[50];
328 getCurrentDateAndTime(dateAndTime);
330 cJSON *heartbeatFields = cJSON_CreateObject();
331 if (heartbeatFields == NULL)
333 printf("Could not create JSON object: heartbeatFields\n");
337 if (cJSON_AddStringToObject(heartbeatFields, "heartbeatFieldsVersion", "3.0") == NULL)
339 printf("Could not create JSON object: heartbeatFieldsVersion\n");
343 if (cJSON_AddNumberToObject(heartbeatFields, "heartbeatInterval", (double)(heartbeat_interval)) == NULL)
345 printf("Could not create JSON object: heartbeatInterval\n");
349 cJSON *additionalFields = cJSON_CreateObject();
350 if (additionalFields == NULL)
352 printf("Could not create JSON object: additionalFields\n");
355 cJSON_AddItemToObject(heartbeatFields, "additionalFields", additionalFields);
357 if (cJSON_AddStringToObject(additionalFields, "eventTime", dateAndTime) == NULL)
359 printf("Could not create JSON object: eventTime\n");
363 return heartbeatFields;
366 char* readConfigFileInString(void)
370 char config_file[200];
371 sprintf(config_file, "%s/configuration.json", getenv("SCRIPTS_DIR"));
372 FILE * f = fopen (config_file, "rb");
376 fseek (f, 0, SEEK_END);
378 fseek (f, 0, SEEK_SET);
379 buffer = malloc (length + 1);
382 fread (buffer, 1, length, f);
385 buffer[length] = '\0';
396 void writeConfigFile(char *config)
400 char config_file[200];
401 sprintf(config_file, "%s/configuration.json", getenv("SCRIPTS_DIR"));
402 FILE * f = fopen (config_file, "w");
411 printf("Could not write configuration file");
415 int getFaultNotificationDelayPeriodFromConfigJson(void)
417 char *stringConfig = readConfigFileInString();
418 int notificationDelay = 0;
420 if (stringConfig == NULL)
422 printf("Could not read JSON configuration file in string.");
426 cJSON *jsonConfig = cJSON_Parse(stringConfig);
427 if (jsonConfig == NULL)
430 const char *error_ptr = cJSON_GetErrorPtr();
431 if (error_ptr != NULL)
433 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
435 return SR_ERR_OPERATION_FAILED;
437 //we don't need the string anymore
440 cJSON *notifConfig = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
441 if (!cJSON_IsObject(notifConfig))
443 printf("Configuration JSON is not as expected: notification-config is not an object");
445 return SR_ERR_OPERATION_FAILED;
448 cJSON *faultNotifDelay = cJSON_GetObjectItemCaseSensitive(notifConfig, "fault-notification-delay-period");
449 if (!cJSON_IsNumber(faultNotifDelay))
451 printf("Configuration JSON is not as expected: fault-notification-delay-period is not a number");
453 return SR_ERR_OPERATION_FAILED;
456 notificationDelay = (int)(faultNotifDelay->valuedouble);
460 return notificationDelay;
463 int getVesHeartbeatPeriodFromConfigJson(void)
465 char *stringConfig = readConfigFileInString();
466 int vesHeartbeat = 0;
468 if (stringConfig == NULL)
470 printf("Could not read JSON configuration file in string.");
474 cJSON *jsonConfig = cJSON_Parse(stringConfig);
475 if (jsonConfig == NULL)
478 const char *error_ptr = cJSON_GetErrorPtr();
479 if (error_ptr != NULL)
481 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
483 return SR_ERR_OPERATION_FAILED;
485 //we don't need the string anymore
488 cJSON *notifConfig = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
489 if (!cJSON_IsObject(notifConfig))
491 printf("Configuration JSON is not as expected: notification-config is not an object");
493 return SR_ERR_OPERATION_FAILED;
496 cJSON *vesHeartbeatPeriod = cJSON_GetObjectItemCaseSensitive(notifConfig, "ves-heartbeat-period");
497 if (!cJSON_IsNumber(vesHeartbeatPeriod))
499 printf("Configuration JSON is not as expected: ves-heartbeat-period is not a number");
501 return SR_ERR_OPERATION_FAILED;
504 vesHeartbeat = (int)(vesHeartbeatPeriod->valuedouble);
513 * Dynamically allocated memory;
514 * Caller needs to free the memory after it uses the value.
517 char* getVesAuthMethodFromConfigJson(void)
519 char *stringConfig = readConfigFileInString();
521 if (stringConfig == NULL)
523 printf("Could not read JSON configuration file in string.");
527 cJSON *jsonConfig = cJSON_Parse(stringConfig);
528 if (jsonConfig == NULL)
531 const char *error_ptr = cJSON_GetErrorPtr();
532 if (error_ptr != NULL)
534 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
536 return SR_ERR_OPERATION_FAILED;
538 //we don't need the string anymore
541 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
542 if (!cJSON_IsObject(vesDetails))
544 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
546 return SR_ERR_OPERATION_FAILED;
549 cJSON *vesAuthMethod = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-auth-method");
550 if (!cJSON_IsString(vesAuthMethod))
552 printf("Configuration JSON is not as expected: ves-endpoint-auth-method is not an object");
554 return SR_ERR_OPERATION_FAILED;
557 char *auth_method_string = strdup(cJSON_GetStringValue(vesAuthMethod));
561 return auth_method_string;
565 * Dynamically allocated memory;
566 * Caller needs to free the memory after it uses the value.
569 char* getVesIpFromConfigJson(void)
571 char *stringConfig = readConfigFileInString();
573 if (stringConfig == NULL)
575 printf("Could not read JSON configuration file in string.");
579 cJSON *jsonConfig = cJSON_Parse(stringConfig);
580 if (jsonConfig == NULL)
583 const char *error_ptr = cJSON_GetErrorPtr();
584 if (error_ptr != NULL)
586 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
588 return SR_ERR_OPERATION_FAILED;
590 //we don't need the string anymore
593 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
594 if (!cJSON_IsObject(vesDetails))
596 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
598 return SR_ERR_OPERATION_FAILED;
601 cJSON *vesIp = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-ip");
602 if (!cJSON_IsString(vesIp))
604 printf("Configuration JSON is not as expected: ves-endpoint-ip is not an object");
606 return SR_ERR_OPERATION_FAILED;
609 char *ves_ip = strdup(cJSON_GetStringValue(vesIp));
616 int getVesPortFromConfigJson(void)
618 char *stringConfig = readConfigFileInString();
620 if (stringConfig == NULL)
622 printf("Could not read JSON configuration file in string.");
626 cJSON *jsonConfig = cJSON_Parse(stringConfig);
627 if (jsonConfig == NULL)
630 const char *error_ptr = cJSON_GetErrorPtr();
631 if (error_ptr != NULL)
633 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
635 return SR_ERR_OPERATION_FAILED;
637 //we don't need the string anymore
640 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
641 if (!cJSON_IsObject(vesDetails))
643 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
645 return SR_ERR_OPERATION_FAILED;
648 cJSON *vesPort = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-port");
649 if (!cJSON_IsNumber(vesPort))
651 printf("Configuration JSON is not as expected: ves-endpoint-port is not an object");
653 return SR_ERR_OPERATION_FAILED;
656 int port = (int)(vesPort->valuedouble);
663 int getVesRegistrationFromConfigJson(void)
665 char *stringConfig = readConfigFileInString();
667 if (stringConfig == NULL)
669 printf("Could not read JSON configuration file in string.");
673 cJSON *jsonConfig = cJSON_Parse(stringConfig);
674 if (jsonConfig == NULL)
677 const char *error_ptr = cJSON_GetErrorPtr();
678 if (error_ptr != NULL)
680 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
682 return SR_ERR_OPERATION_FAILED;
684 //we don't need the string anymore
687 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
688 if (!cJSON_IsObject(vesDetails))
690 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
692 return SR_ERR_OPERATION_FAILED;
695 cJSON *vesReg = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-registration");
696 if (!cJSON_IsBool(vesReg))
698 printf("Configuration JSON is not as expected: ves-registration is not an object");
700 return SR_ERR_OPERATION_FAILED;
703 int is_ves_reg = (cJSON_IsTrue(vesReg)) ? TRUE : FALSE;
710 cJSON* vesCreatePnfRegistrationFields(int port, bool is_tls)
712 cJSON *pnfRegistrationFields = cJSON_CreateObject();
713 if (pnfRegistrationFields == NULL)
715 printf("Could not create JSON object: pnfRegistrationFields\n");
719 if (cJSON_AddStringToObject(pnfRegistrationFields, "pnfRegistrationFieldsVersion", "2.0") == NULL)
721 printf("Could not create JSON object: pnfRegistrationFieldsVersion\n");
725 if (cJSON_AddStringToObject(pnfRegistrationFields, "lastServiceDate", "2019-08-16") == NULL)
727 printf("Could not create JSON object: lastServiceDate\n");
732 generateRandomMacAddress(mac_addr);
734 if (cJSON_AddStringToObject(pnfRegistrationFields, "macAddress", mac_addr) == NULL)
736 printf("Could not create JSON object: macAddress\n");
740 if (cJSON_AddStringToObject(pnfRegistrationFields, "manufactureDate", "2019-08-16") == NULL)
742 printf("Could not create JSON object: manufactureDate\n");
746 if (cJSON_AddStringToObject(pnfRegistrationFields, "modelNumber", "Simulated Device Melacon") == NULL)
748 printf("Could not create JSON object: manufactureDate\n");
752 if (cJSON_AddStringToObject(pnfRegistrationFields, "oamV4IpAddress", getenv("NTS_IP")) == NULL)
754 printf("Could not create JSON object: oamV4IpAddress\n");
758 if (cJSON_AddStringToObject(pnfRegistrationFields, "oamV6IpAddress", "0:0:0:0:0:ffff:a0a:011") == NULL)
760 printf("Could not create JSON object: oamV6IpAddress\n");
764 char serial_number[100];
765 sprintf(serial_number, "%s-%s-%d-Simulated Device Melacon", getenv("HOSTNAME"), getenv("NTS_IP"), port);
767 if (cJSON_AddStringToObject(pnfRegistrationFields, "serialNumber", serial_number) == NULL)
769 printf("Could not create JSON object: serialNumber\n");
773 if (cJSON_AddStringToObject(pnfRegistrationFields, "softwareVersion", "2.3.5") == NULL)
775 printf("Could not create JSON object: softwareVersion\n");
779 if (cJSON_AddStringToObject(pnfRegistrationFields, "unitFamily", "Simulated Device") == NULL)
781 printf("Could not create JSON object: unitFamily\n");
785 if (cJSON_AddStringToObject(pnfRegistrationFields, "unitType", "O-RAN-sim") == NULL)
787 printf("Could not create JSON object: unitType\n");
791 if (cJSON_AddStringToObject(pnfRegistrationFields, "vendorName", "Melacon") == NULL)
793 printf("Could not create JSON object: vendorName\n");
797 cJSON *additionalFields = cJSON_CreateObject();
798 if (additionalFields == NULL)
800 printf("Could not create JSON object: additionalFields\n");
803 cJSON_AddItemToObject(pnfRegistrationFields, "additionalFields", additionalFields);
806 sprintf(portString, "%d", port);
808 if (cJSON_AddStringToObject(additionalFields, "oamPort", portString) == NULL)
810 printf("Could not create JSON object: oamPort\n");
816 //TLS specific configuration
817 if (cJSON_AddStringToObject(additionalFields, "protocol", "TLS") == NULL)
819 printf("Could not create JSON object: protocol\n");
823 //TODO here we have the username from the docker container hardcoded: netconf
824 if (cJSON_AddStringToObject(additionalFields, "username", "netconf") == NULL)
826 printf("Could not create JSON object: username\n");
830 if (cJSON_AddStringToObject(additionalFields, "keyId", "device-key") == NULL)
832 printf("Could not create JSON object: keyId\n");
838 //SSH specific configuration
839 if (cJSON_AddStringToObject(additionalFields, "protocol", "SSH") == NULL)
841 printf("Could not create JSON object: protocol\n");
845 //TODO here we have the username from the docker container hardcoded: netconf
846 if (cJSON_AddStringToObject(additionalFields, "username", "netconf") == NULL)
848 printf("Could not create JSON object: username\n");
852 //TODO here we have the password from the docker container hardcoded: netconf
853 if (cJSON_AddStringToObject(additionalFields, "password", "netconf") == NULL)
855 printf("Could not create JSON object: password\n");
860 if (cJSON_AddStringToObject(additionalFields, "reconnectOnChangedSchema", "false") == NULL)
862 printf("Could not create JSON object: reconnectOnChangedSchema\n");
866 if (cJSON_AddStringToObject(additionalFields, "sleep-factor", "1.5") == NULL)
868 printf("Could not create JSON object: sleep-factor\n");
872 if (cJSON_AddStringToObject(additionalFields, "tcpOnly", "false") == NULL)
874 printf("Could not create JSON object: tcpOnly\n");
878 if (cJSON_AddStringToObject(additionalFields, "connectionTimeout", "20000") == NULL)
880 printf("Could not create JSON object: connectionTimeout\n");
884 if (cJSON_AddStringToObject(additionalFields, "maxConnectionAttempts", "100") == NULL)
886 printf("Could not create JSON object: maxConnectionAttempts\n");
890 if (cJSON_AddStringToObject(additionalFields, "betweenAttemptsTimeout", "2000") == NULL)
892 printf("Could not create JSON object: betweenAttemptsTimeout\n");
896 if (cJSON_AddStringToObject(additionalFields, "keepaliveDelay", "120") == NULL)
898 printf("Could not create JSON object: keepaliveDelay\n");
902 return pnfRegistrationFields;
905 int getNetconfAvailableFromConfigJson(void)
907 char *stringConfig = readConfigFileInString();
909 if (stringConfig == NULL)
911 printf("Could not read JSON configuration file in string.");
915 cJSON *jsonConfig = cJSON_Parse(stringConfig);
916 if (jsonConfig == NULL)
919 const char *error_ptr = cJSON_GetErrorPtr();
920 if (error_ptr != NULL)
922 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
924 return SR_ERR_OPERATION_FAILED;
926 //we don't need the string anymore
929 cJSON *notifDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
930 if (!cJSON_IsObject(notifDetails))
932 printf("Configuration JSON is not as expected: notification-config is not an object");
934 return SR_ERR_OPERATION_FAILED;
937 cJSON *isNetconfAvailable = cJSON_GetObjectItemCaseSensitive(notifDetails, "is-netconf-available");
938 if (!cJSON_IsBool(isNetconfAvailable))
940 printf("Configuration JSON is not as expected: is-netconf-available is not an object");
942 return SR_ERR_OPERATION_FAILED;
945 int is_netconf_available = (cJSON_IsTrue(isNetconfAvailable)) ? TRUE : FALSE;
949 return is_netconf_available;
952 int getVesAvailableFromConfigJson(void)
954 char *stringConfig = readConfigFileInString();
956 if (stringConfig == NULL)
958 printf("Could not read JSON configuration file in string.");
962 cJSON *jsonConfig = cJSON_Parse(stringConfig);
963 if (jsonConfig == NULL)
966 const char *error_ptr = cJSON_GetErrorPtr();
967 if (error_ptr != NULL)
969 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
971 return SR_ERR_OPERATION_FAILED;
973 //we don't need the string anymore
976 cJSON *notifDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
977 if (!cJSON_IsObject(notifDetails))
979 printf("Configuration JSON is not as expected: notification-config is not an object");
981 return SR_ERR_OPERATION_FAILED;
984 cJSON *isVesAvailable = cJSON_GetObjectItemCaseSensitive(notifDetails, "is-ves-available");
985 if (!cJSON_IsBool(isVesAvailable))
987 printf("Configuration JSON is not as expected: is-ves-available is not an object");
989 return SR_ERR_OPERATION_FAILED;
992 int is_netconf_available = (cJSON_IsTrue(isVesAvailable)) ? TRUE : FALSE;
996 return is_netconf_available;
999 cJSON* vesCreateFaultFields(char *alarm_condition, char *alarm_object, char *severity, char *date_time, char *specific_problem)
1001 cJSON *faultFields = cJSON_CreateObject();
1002 if (faultFields == NULL)
1004 printf("Could not create JSON object: faultFields\n");
1008 if (cJSON_AddStringToObject(faultFields, "faultFieldsVersion", "4.0") == NULL)
1010 printf("Could not create JSON object: faultFieldsVersion\n");
1014 if (cJSON_AddStringToObject(faultFields, "alarmCondition", alarm_condition) == NULL)
1016 printf("Could not create JSON object: alarmCondition\n");
1020 if (cJSON_AddStringToObject(faultFields, "alarmInterfaceA", alarm_object) == NULL)
1022 printf("Could not create JSON object: alarmInterfaceA\n");
1026 if (cJSON_AddStringToObject(faultFields, "eventSourceType", "O_RAN_COMPONENT") == NULL)
1028 printf("Could not create JSON object: eventSourceType\n");
1032 if (cJSON_AddStringToObject(faultFields, "specificProblem", specific_problem) == NULL)
1034 printf("Could not create JSON object: specificProblem\n");
1038 if (cJSON_AddStringToObject(faultFields, "eventSeverity", severity) == NULL)
1040 printf("Could not create JSON object: eventSeverity\n");
1044 if (cJSON_AddStringToObject(faultFields, "vfStatus", "Active") == NULL)
1046 printf("Could not create JSON object: vfStatus\n");
1050 cJSON *alarmAdditionalInformation = cJSON_CreateObject();
1051 if (alarmAdditionalInformation == NULL)
1053 printf("Could not create JSON object: alarmAdditionalInformation\n");
1056 cJSON_AddItemToObject(faultFields, "alarmAdditionalInformation", alarmAdditionalInformation);
1058 if (cJSON_AddStringToObject(alarmAdditionalInformation, "eventTime", date_time) == NULL)
1060 printf("Could not create JSON object: eventTime\n");
1064 if (cJSON_AddStringToObject(alarmAdditionalInformation, "equipType", "O-RAN-sim") == NULL)
1066 printf("Could not create JSON object: equipType\n");
1070 if (cJSON_AddStringToObject(alarmAdditionalInformation, "vendor", "Melacon") == NULL)
1072 printf("Could not create JSON object: vendor\n");
1076 if (cJSON_AddStringToObject(alarmAdditionalInformation, "model", "Simulated Device") == NULL)
1078 printf("Could not create JSON object: model\n");