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 ***************************************************************************/
26 void set_curl_common_info_ves(CURL *curl)
28 struct curl_slist *chunk = NULL;
29 chunk = curl_slist_append(chunk, "Content-Type: application/json");
30 chunk = curl_slist_append(chunk, "Accept: application/json");
32 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
34 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 2L); // seconds timeout for a connection
35 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5L); //seconds timeout for an operation
37 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
40 void getCurrentDateAndTime(char *date_and_time)
42 time_t t = time(NULL);
43 struct tm tm = *localtime(&t);
47 gettimeofday(&tv, NULL);
48 millisec = lrint(tv.tv_usec/1000.0); // Round to nearest millisec
50 { // Allow for rounding up to nearest second
55 sprintf(date_and_time, "%04d-%02d-%02dT%02d:%02d:%02d.%01dZ",
56 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
57 tm.tm_hour, tm.tm_min, tm.tm_sec, millisec/100);
62 void generateRandomMacAddress(char *mac_address)
64 long rand1, rand2, rand3, rand4, rand5, rand6;
65 rand1 = random_at_most(255);
66 rand2 = random_at_most(255);
67 rand3 = random_at_most(255);
68 rand4 = random_at_most(255);
69 rand5 = random_at_most(255);
70 rand6 = random_at_most(255);
72 sprintf(mac_address, "%02X:%02X:%02X:%02X:%02X:%02X", rand1, rand2, rand3, rand4, rand5, rand6);
77 long random_at_most(long max) {
79 // max <= RAND_MAX < ULONG_MAX, so this is okay.
80 num_bins = (unsigned long) max + 1,
81 num_rand = (unsigned long) RAND_MAX + 1,
82 bin_size = num_rand / num_bins,
83 defect = num_rand % num_bins;
89 // This is carefully written not to overflow
90 while (num_rand - defect <= (unsigned long)x);
92 // Truncated division is intentional
96 int getSecondsFromLastQuarterInterval(void)
98 time_t t = time(NULL);
99 time_t t_past = time(NULL);
100 struct tm tm = *localtime(&t);
101 struct tm tm_15_min_ago = tm;
103 //round to the last quarter hour
104 tm_15_min_ago.tm_min -= (tm_15_min_ago.tm_min % 15);
105 tm_15_min_ago.tm_sec = 0;
107 t=mktime(&tm_15_min_ago);
110 double seconds = difftime(t_past, t);
115 int getSecondsFromLastDayInterval(void)
117 time_t t = time(NULL);
118 time_t t_past = time(NULL);
119 struct tm tm = *localtime(&t);
120 struct tm tm_day_ago = tm;
122 //round to the last quarter hour
123 tm_day_ago.tm_hour = 0;
124 tm_day_ago.tm_min = 0;
125 tm_day_ago.tm_sec = 0;
127 t=mktime(&tm_day_ago);
130 double seconds = difftime(t_past, t);
135 void getPreviousQuarterInterval(int number_of_intervals, char *date_and_time)
137 time_t t = time(NULL);
138 t -= 15 * 60 * number_of_intervals;
139 struct tm tm = *localtime(&t);
141 tm.tm_min -= (tm.tm_min % 15);
144 sprintf(date_and_time, "%04d-%02d-%02dT%02d:%02d:%02d.0Z",
145 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
146 tm.tm_hour, tm.tm_min, tm.tm_sec);
151 void getPreviousDayPmTimestamp(int number_of_intervals, char *date_and_time)
153 time_t t = time(NULL);
154 t -= 24 * 60 * 60 * number_of_intervals;
155 struct tm tm = *localtime(&t);
161 sprintf(date_and_time, "%04d-%02d-%02dT%02d:%02d:%02d.0Z",
162 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
163 tm.tm_hour, tm.tm_min, tm.tm_sec);
168 long int getMicrosecondsSinceEpoch(void)
170 time_t t = time(NULL);
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);
199 * Dynamically allocated memory;
200 * Caller needs to free the memory after it uses the value.
203 cJSON* vesCreateCommonEventHeader(char *domain, char *event_type, char *source_name, int seq_id)
205 char dateAndTime[50];
206 getCurrentDateAndTime(dateAndTime);
208 long useconds = getMicrosecondsSinceEpoch();
210 cJSON *commonEventHeader = cJSON_CreateObject();
211 if (commonEventHeader == NULL)
213 printf("Could not create JSON object: commonEventHeader\n");
217 if (cJSON_AddStringToObject(commonEventHeader, "domain", domain) == NULL)
219 printf("Could not create JSON object: domain\n");
220 cJSON_Delete(commonEventHeader);
225 sprintf(eventId, "%s_%s", source_name, dateAndTime);
227 if (cJSON_AddStringToObject(commonEventHeader, "eventId", eventId) == NULL)
229 printf("Could not create JSON object: eventId\n");
230 cJSON_Delete(commonEventHeader);
234 char event_name[200];
235 sprintf(event_name, "%s_%s", domain, event_type);
237 if (cJSON_AddStringToObject(commonEventHeader, "eventName", event_name) == NULL)
239 printf("Could not create JSON object: eventName\n");
240 cJSON_Delete(commonEventHeader);
244 if (cJSON_AddStringToObject(commonEventHeader, "eventType", event_type) == NULL)
246 printf("Could not create JSON object: eventType\n");
247 cJSON_Delete(commonEventHeader);
251 if (cJSON_AddNumberToObject(commonEventHeader, "sequence", (double)(seq_id)) == NULL)
253 printf("Could not create JSON object: sequence\n");
254 cJSON_Delete(commonEventHeader);
258 if (cJSON_AddStringToObject(commonEventHeader, "priority", "Low") == NULL)
260 printf("Could not create JSON object: priority\n");
261 cJSON_Delete(commonEventHeader);
265 if (cJSON_AddStringToObject(commonEventHeader, "reportingEntityId", "") == NULL)
267 printf("Could not create JSON object: reportingEntityId\n");
268 cJSON_Delete(commonEventHeader);
272 if (cJSON_AddStringToObject(commonEventHeader, "reportingEntityName", source_name) == NULL)
274 printf("Could not create JSON object: reportingEntityName\n");
275 cJSON_Delete(commonEventHeader);
279 if (cJSON_AddStringToObject(commonEventHeader, "sourceId", "") == NULL)
281 printf("Could not create JSON object: sourceId\n");
282 cJSON_Delete(commonEventHeader);
286 if (cJSON_AddStringToObject(commonEventHeader, "sourceName", source_name) == NULL)
288 printf("Could not create JSON object: sourceName\n");
289 cJSON_Delete(commonEventHeader);
293 if (cJSON_AddNumberToObject(commonEventHeader, "startEpochMicrosec", (double)(useconds)) == NULL)
295 printf("Could not create JSON object: startEpochMicrosec\n");
296 cJSON_Delete(commonEventHeader);
300 if (cJSON_AddNumberToObject(commonEventHeader, "lastEpochMicrosec", (double)(useconds)) == NULL)
302 printf("Could not create JSON object: lastEpochMicrosec\n");
303 cJSON_Delete(commonEventHeader);
307 if (cJSON_AddStringToObject(commonEventHeader, "nfNamingCode", "sdn controller") == NULL)
309 printf("Could not create JSON object: nfNamingCode\n");
310 cJSON_Delete(commonEventHeader);
314 if (cJSON_AddStringToObject(commonEventHeader, "nfVendorName", "sdn") == NULL)
316 printf("Could not create JSON object: nfVendorName\n");
317 cJSON_Delete(commonEventHeader);
321 if (cJSON_AddStringToObject(commonEventHeader, "timeZoneOffset", "+00:00") == NULL)
323 printf("Could not create JSON object: timeZoneOffset\n");
324 cJSON_Delete(commonEventHeader);
328 if (cJSON_AddStringToObject(commonEventHeader, "version", "4.0.1") == NULL)
330 printf("Could not create JSON object: version\n");
331 cJSON_Delete(commonEventHeader);
335 if (cJSON_AddStringToObject(commonEventHeader, "vesEventListenerVersion", "7.0.1") == NULL)
337 printf("Could not create JSON object: vesEventListenerVersion\n");
338 cJSON_Delete(commonEventHeader);
342 return commonEventHeader;
346 * Dynamically allocated memory;
347 * Caller needs to free the memory after it uses the value.
350 cJSON* vesCreateHeartbeatFields(int heartbeat_interval)
352 char dateAndTime[50];
353 getCurrentDateAndTime(dateAndTime);
355 cJSON *heartbeatFields = cJSON_CreateObject();
356 if (heartbeatFields == NULL)
358 printf("Could not create JSON object: heartbeatFields\n");
362 if (cJSON_AddStringToObject(heartbeatFields, "heartbeatFieldsVersion", "3.0") == NULL)
364 printf("Could not create JSON object: heartbeatFieldsVersion\n");
365 cJSON_Delete(heartbeatFields);
369 if (cJSON_AddNumberToObject(heartbeatFields, "heartbeatInterval", (double)(heartbeat_interval)) == NULL)
371 printf("Could not create JSON object: heartbeatInterval\n");
372 cJSON_Delete(heartbeatFields);
376 cJSON *additionalFields = cJSON_CreateObject();
377 if (additionalFields == NULL)
379 printf("Could not create JSON object: additionalFields\n");
380 cJSON_Delete(heartbeatFields);
383 cJSON_AddItemToObject(heartbeatFields, "additionalFields", additionalFields);
385 if (cJSON_AddStringToObject(additionalFields, "eventTime", dateAndTime) == NULL)
387 printf("Could not create JSON object: eventTime\n");
388 cJSON_Delete(heartbeatFields);
392 return heartbeatFields;
396 * Dynamically allocated memory;
397 * Caller needs to free the memory after it uses the value.
400 char* readConfigFileInString(void)
404 char config_file[200];
405 sprintf(config_file, "%s/configuration.json", getenv("SCRIPTS_DIR"));
406 FILE * f = fopen (config_file, "rb");
410 fseek (f, 0, SEEK_END);
412 fseek (f, 0, SEEK_SET);
413 buffer = malloc (length + 1);
416 fread (buffer, 1, length, f);
419 buffer[length] = '\0';
430 void writeConfigFile(char *config)
432 char config_file[200];
433 sprintf(config_file, "%s/configuration.json", getenv("SCRIPTS_DIR"));
434 FILE * f = fopen (config_file, "w");
443 printf("Could not write configuration file");
447 int getFaultNotificationDelayPeriodFromConfigJson(void)
449 char *stringConfig = readConfigFileInString();
450 int notificationDelay = 0;
452 if (stringConfig == NULL)
454 printf("Could not read JSON configuration file in string.");
458 cJSON *jsonConfig = cJSON_Parse(stringConfig);
459 if (jsonConfig == NULL)
462 const char *error_ptr = cJSON_GetErrorPtr();
463 if (error_ptr != NULL)
465 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
467 return SR_ERR_OPERATION_FAILED;
469 //we don't need the string anymore
473 cJSON *notifConfig = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
474 if (!cJSON_IsObject(notifConfig))
476 printf("Configuration JSON is not as expected: notification-config is not an object");
477 cJSON_Delete(jsonConfig);
478 return SR_ERR_OPERATION_FAILED;
481 cJSON *faultNotifDelay = cJSON_GetObjectItemCaseSensitive(notifConfig, "fault-notification-delay-period");
482 if (!cJSON_IsNumber(faultNotifDelay))
484 printf("Configuration JSON is not as expected: fault-notification-delay-period is not a number");
485 cJSON_Delete(jsonConfig);
486 return SR_ERR_OPERATION_FAILED;
489 notificationDelay = (int)(faultNotifDelay->valuedouble);
491 cJSON_Delete(jsonConfig);
493 return notificationDelay;
496 int getVesHeartbeatPeriodFromConfigJson(void)
498 char *stringConfig = readConfigFileInString();
499 int vesHeartbeat = 0;
501 if (stringConfig == NULL)
503 printf("Could not read JSON configuration file in string.");
507 cJSON *jsonConfig = cJSON_Parse(stringConfig);
508 if (jsonConfig == NULL)
511 const char *error_ptr = cJSON_GetErrorPtr();
512 if (error_ptr != NULL)
514 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
516 return SR_ERR_OPERATION_FAILED;
518 //we don't need the string anymore
522 cJSON *notifConfig = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
523 if (!cJSON_IsObject(notifConfig))
525 printf("Configuration JSON is not as expected: notification-config is not an object");
526 cJSON_Delete(jsonConfig);
527 return SR_ERR_OPERATION_FAILED;
530 cJSON *vesHeartbeatPeriod = cJSON_GetObjectItemCaseSensitive(notifConfig, "ves-heartbeat-period");
531 if (!cJSON_IsNumber(vesHeartbeatPeriod))
533 printf("Configuration JSON is not as expected: ves-heartbeat-period is not a number");
534 cJSON_Delete(jsonConfig);
535 return SR_ERR_OPERATION_FAILED;
538 vesHeartbeat = (int)(vesHeartbeatPeriod->valuedouble);
540 cJSON_Delete(jsonConfig);
547 * Dynamically allocated memory;
548 * Caller needs to free the memory after it uses the value.
551 char* getVesAuthMethodFromConfigJson(void)
553 char *stringConfig = readConfigFileInString();
555 if (stringConfig == NULL)
557 printf("Could not read JSON configuration file in string.");
561 cJSON *jsonConfig = cJSON_Parse(stringConfig);
562 if (jsonConfig == NULL)
565 const char *error_ptr = cJSON_GetErrorPtr();
566 if (error_ptr != NULL)
568 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
572 //we don't need the string anymore
576 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
577 if (!cJSON_IsObject(vesDetails))
579 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
580 cJSON_Delete(jsonConfig);
584 cJSON *vesAuthMethod = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-auth-method");
585 if (!cJSON_IsString(vesAuthMethod))
587 printf("Configuration JSON is not as expected: ves-endpoint-auth-method is not an object");
588 cJSON_Delete(jsonConfig);
592 char *auth_method_string = strdup(cJSON_GetStringValue(vesAuthMethod));
594 cJSON_Delete(jsonConfig);
596 return auth_method_string;
600 * Dynamically allocated memory;
601 * Caller needs to free the memory after it uses the value.
604 char* getVesIpFromConfigJson(void)
606 char *stringConfig = readConfigFileInString();
608 if (stringConfig == NULL)
610 printf("Could not read JSON configuration file in string.");
614 cJSON *jsonConfig = cJSON_Parse(stringConfig);
615 if (jsonConfig == NULL)
618 const char *error_ptr = cJSON_GetErrorPtr();
619 if (error_ptr != NULL)
621 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
625 //we don't need the string anymore
629 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
630 if (!cJSON_IsObject(vesDetails))
632 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
633 cJSON_Delete(jsonConfig);
637 cJSON *vesIp = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-ip");
638 if (!cJSON_IsString(vesIp))
640 printf("Configuration JSON is not as expected: ves-endpoint-ip is not an object");
641 cJSON_Delete(jsonConfig);
645 char *ves_ip = strdup(cJSON_GetStringValue(vesIp));
647 cJSON_Delete(jsonConfig);
652 int getVesPortFromConfigJson(void)
654 char *stringConfig = readConfigFileInString();
656 if (stringConfig == NULL)
658 printf("Could not read JSON configuration file in string.");
662 cJSON *jsonConfig = cJSON_Parse(stringConfig);
663 if (jsonConfig == NULL)
666 const char *error_ptr = cJSON_GetErrorPtr();
667 if (error_ptr != NULL)
669 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
671 return SR_ERR_OPERATION_FAILED;
673 //we don't need the string anymore
677 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
678 if (!cJSON_IsObject(vesDetails))
680 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
681 cJSON_Delete(jsonConfig);
682 return SR_ERR_OPERATION_FAILED;
685 cJSON *vesPort = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-port");
686 if (!cJSON_IsNumber(vesPort))
688 printf("Configuration JSON is not as expected: ves-endpoint-port is not an object");
689 cJSON_Delete(jsonConfig);
690 return SR_ERR_OPERATION_FAILED;
693 int port = (int)(vesPort->valuedouble);
695 cJSON_Delete(jsonConfig);
700 int getVesRegistrationFromConfigJson(void)
702 char *stringConfig = readConfigFileInString();
704 if (stringConfig == NULL)
706 printf("Could not read JSON configuration file in string.");
710 cJSON *jsonConfig = cJSON_Parse(stringConfig);
711 if (jsonConfig == NULL)
714 const char *error_ptr = cJSON_GetErrorPtr();
715 if (error_ptr != NULL)
717 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
719 return SR_ERR_OPERATION_FAILED;
721 //we don't need the string anymore
725 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
726 if (!cJSON_IsObject(vesDetails))
728 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
729 cJSON_Delete(jsonConfig);
730 return SR_ERR_OPERATION_FAILED;
733 cJSON *vesReg = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-registration");
734 if (!cJSON_IsBool(vesReg))
736 printf("Configuration JSON is not as expected: ves-registration is not an object");
737 cJSON_Delete(jsonConfig);
738 return SR_ERR_OPERATION_FAILED;
741 int is_ves_reg = (cJSON_IsTrue(vesReg)) ? TRUE : FALSE;
743 cJSON_Delete(jsonConfig);
749 * Dynamically allocated memory;
750 * Caller needs to free the memory after it uses the value.
753 cJSON* vesCreatePnfRegistrationFields(int port, bool is_tls)
755 cJSON *pnfRegistrationFields = cJSON_CreateObject();
756 if (pnfRegistrationFields == NULL)
758 printf("Could not create JSON object: pnfRegistrationFields\n");
762 if (cJSON_AddStringToObject(pnfRegistrationFields, "pnfRegistrationFieldsVersion", "2.0") == NULL)
764 printf("Could not create JSON object: pnfRegistrationFieldsVersion\n");
765 cJSON_Delete(pnfRegistrationFields);
769 if (cJSON_AddStringToObject(pnfRegistrationFields, "lastServiceDate", "2019-08-16") == NULL)
771 printf("Could not create JSON object: lastServiceDate\n");
772 cJSON_Delete(pnfRegistrationFields);
777 generateRandomMacAddress(mac_addr);
779 if (cJSON_AddStringToObject(pnfRegistrationFields, "macAddress", mac_addr) == NULL)
781 printf("Could not create JSON object: macAddress\n");
782 cJSON_Delete(pnfRegistrationFields);
786 if (cJSON_AddStringToObject(pnfRegistrationFields, "manufactureDate", "2019-08-16") == NULL)
788 printf("Could not create JSON object: manufactureDate\n");
789 cJSON_Delete(pnfRegistrationFields);
793 if (cJSON_AddStringToObject(pnfRegistrationFields, "modelNumber", "Simulated Device Melacon") == NULL)
795 printf("Could not create JSON object: manufactureDate\n");
796 cJSON_Delete(pnfRegistrationFields);
800 if (cJSON_AddStringToObject(pnfRegistrationFields, "oamV4IpAddress", getenv("NTS_IP")) == NULL)
802 printf("Could not create JSON object: oamV4IpAddress\n");
803 cJSON_Delete(pnfRegistrationFields);
807 if (cJSON_AddStringToObject(pnfRegistrationFields, "oamV6IpAddress", "0:0:0:0:0:ffff:a0a:011") == NULL)
809 printf("Could not create JSON object: oamV6IpAddress\n");
810 cJSON_Delete(pnfRegistrationFields);
814 char serial_number[100];
815 sprintf(serial_number, "%s-%s-%d-Simulated Device Melacon", getenv("HOSTNAME"), getenv("NTS_IP"), port);
817 if (cJSON_AddStringToObject(pnfRegistrationFields, "serialNumber", serial_number) == NULL)
819 printf("Could not create JSON object: serialNumber\n");
820 cJSON_Delete(pnfRegistrationFields);
824 if (cJSON_AddStringToObject(pnfRegistrationFields, "softwareVersion", "2.3.5") == NULL)
826 printf("Could not create JSON object: softwareVersion\n");
827 cJSON_Delete(pnfRegistrationFields);
831 if (cJSON_AddStringToObject(pnfRegistrationFields, "unitFamily", "Simulated Device") == NULL)
833 printf("Could not create JSON object: unitFamily\n");
834 cJSON_Delete(pnfRegistrationFields);
838 if (cJSON_AddStringToObject(pnfRegistrationFields, "unitType", "O-RAN-sim") == NULL)
840 printf("Could not create JSON object: unitType\n");
841 cJSON_Delete(pnfRegistrationFields);
845 if (cJSON_AddStringToObject(pnfRegistrationFields, "vendorName", "Melacon") == NULL)
847 printf("Could not create JSON object: vendorName\n");
848 cJSON_Delete(pnfRegistrationFields);
852 cJSON *additionalFields = cJSON_CreateObject();
853 if (additionalFields == NULL)
855 printf("Could not create JSON object: additionalFields\n");
856 cJSON_Delete(pnfRegistrationFields);
859 cJSON_AddItemToObject(pnfRegistrationFields, "additionalFields", additionalFields);
862 sprintf(portString, "%d", port);
864 if (cJSON_AddStringToObject(additionalFields, "oamPort", portString) == NULL)
866 printf("Could not create JSON object: oamPort\n");
867 cJSON_Delete(pnfRegistrationFields);
873 //TLS specific configuration
874 if (cJSON_AddStringToObject(additionalFields, "protocol", "TLS") == NULL)
876 printf("Could not create JSON object: protocol\n");
877 cJSON_Delete(pnfRegistrationFields);
881 //TODO here we have the username from the docker container hardcoded: netconf
882 if (cJSON_AddStringToObject(additionalFields, "username", "netconf") == NULL)
884 printf("Could not create JSON object: username\n");
885 cJSON_Delete(pnfRegistrationFields);
889 if (cJSON_AddStringToObject(additionalFields, "keyId", "device-key") == NULL)
891 printf("Could not create JSON object: keyId\n");
892 cJSON_Delete(pnfRegistrationFields);
898 //SSH specific configuration
899 if (cJSON_AddStringToObject(additionalFields, "protocol", "SSH") == NULL)
901 printf("Could not create JSON object: protocol\n");
902 cJSON_Delete(pnfRegistrationFields);
906 //TODO here we have the username from the docker container hardcoded: netconf
907 if (cJSON_AddStringToObject(additionalFields, "username", "netconf") == NULL)
909 printf("Could not create JSON object: username\n");
910 cJSON_Delete(pnfRegistrationFields);
914 //TODO here we have the password from the docker container hardcoded: netconf
915 if (cJSON_AddStringToObject(additionalFields, "password", "netconf") == NULL)
917 printf("Could not create JSON object: password\n");
918 cJSON_Delete(pnfRegistrationFields);
923 if (cJSON_AddStringToObject(additionalFields, "reconnectOnChangedSchema", "false") == NULL)
925 printf("Could not create JSON object: reconnectOnChangedSchema\n");
926 cJSON_Delete(pnfRegistrationFields);
930 if (cJSON_AddStringToObject(additionalFields, "sleep-factor", "1.5") == NULL)
932 printf("Could not create JSON object: sleep-factor\n");
933 cJSON_Delete(pnfRegistrationFields);
937 if (cJSON_AddStringToObject(additionalFields, "tcpOnly", "false") == NULL)
939 printf("Could not create JSON object: tcpOnly\n");
940 cJSON_Delete(pnfRegistrationFields);
944 if (cJSON_AddStringToObject(additionalFields, "connectionTimeout", "20000") == NULL)
946 printf("Could not create JSON object: connectionTimeout\n");
947 cJSON_Delete(pnfRegistrationFields);
951 if (cJSON_AddStringToObject(additionalFields, "maxConnectionAttempts", "100") == NULL)
953 printf("Could not create JSON object: maxConnectionAttempts\n");
954 cJSON_Delete(pnfRegistrationFields);
958 if (cJSON_AddStringToObject(additionalFields, "betweenAttemptsTimeout", "2000") == NULL)
960 printf("Could not create JSON object: betweenAttemptsTimeout\n");
961 cJSON_Delete(pnfRegistrationFields);
965 if (cJSON_AddStringToObject(additionalFields, "keepaliveDelay", "120") == NULL)
967 printf("Could not create JSON object: keepaliveDelay\n");
968 cJSON_Delete(pnfRegistrationFields);
972 return pnfRegistrationFields;
975 int getNetconfAvailableFromConfigJson(void)
977 char *stringConfig = readConfigFileInString();
979 if (stringConfig == NULL)
981 printf("Could not read JSON configuration file in string.");
985 cJSON *jsonConfig = cJSON_Parse(stringConfig);
986 if (jsonConfig == NULL)
989 const char *error_ptr = cJSON_GetErrorPtr();
990 if (error_ptr != NULL)
992 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
994 return SR_ERR_OPERATION_FAILED;
996 //we don't need the string anymore
1000 cJSON *notifDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
1001 if (!cJSON_IsObject(notifDetails))
1003 printf("Configuration JSON is not as expected: notification-config is not an object");
1004 cJSON_Delete(jsonConfig);
1005 return SR_ERR_OPERATION_FAILED;
1008 cJSON *isNetconfAvailable = cJSON_GetObjectItemCaseSensitive(notifDetails, "is-netconf-available");
1009 if (!cJSON_IsBool(isNetconfAvailable))
1011 printf("Configuration JSON is not as expected: is-netconf-available is not an object");
1012 cJSON_Delete(jsonConfig);
1013 return SR_ERR_OPERATION_FAILED;
1016 int is_netconf_available = (cJSON_IsTrue(isNetconfAvailable)) ? TRUE : FALSE;
1018 cJSON_Delete(jsonConfig);
1020 return is_netconf_available;
1023 int getVesAvailableFromConfigJson(void)
1025 char *stringConfig = readConfigFileInString();
1027 if (stringConfig == NULL)
1029 printf("Could not read JSON configuration file in string.");
1033 cJSON *jsonConfig = cJSON_Parse(stringConfig);
1034 if (jsonConfig == NULL)
1037 const char *error_ptr = cJSON_GetErrorPtr();
1038 if (error_ptr != NULL)
1040 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
1042 return SR_ERR_OPERATION_FAILED;
1044 //we don't need the string anymore
1046 stringConfig = NULL;
1048 cJSON *notifDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
1049 if (!cJSON_IsObject(notifDetails))
1051 printf("Configuration JSON is not as expected: notification-config is not an object");
1052 cJSON_Delete(jsonConfig);
1053 return SR_ERR_OPERATION_FAILED;
1056 cJSON *isVesAvailable = cJSON_GetObjectItemCaseSensitive(notifDetails, "is-ves-available");
1057 if (!cJSON_IsBool(isVesAvailable))
1059 printf("Configuration JSON is not as expected: is-ves-available is not an object");
1060 cJSON_Delete(jsonConfig);
1061 return SR_ERR_OPERATION_FAILED;
1064 int is_netconf_available = (cJSON_IsTrue(isVesAvailable)) ? TRUE : FALSE;
1066 cJSON_Delete(jsonConfig);
1068 return is_netconf_available;
1072 * Dynamically allocated memory;
1073 * Caller needs to free the memory after it uses the value.
1076 cJSON* vesCreateFaultFields(char *alarm_condition, char *alarm_object, char *severity, char *date_time, char *specific_problem)
1078 cJSON *faultFields = cJSON_CreateObject();
1079 if (faultFields == NULL)
1081 printf("Could not create JSON object: faultFields\n");
1085 if (cJSON_AddStringToObject(faultFields, "faultFieldsVersion", "4.0") == NULL)
1087 printf("Could not create JSON object: faultFieldsVersion\n");
1088 cJSON_Delete(faultFields);
1092 if (cJSON_AddStringToObject(faultFields, "alarmCondition", alarm_condition) == NULL)
1094 printf("Could not create JSON object: alarmCondition\n");
1095 cJSON_Delete(faultFields);
1099 if (cJSON_AddStringToObject(faultFields, "alarmInterfaceA", alarm_object) == NULL)
1101 printf("Could not create JSON object: alarmInterfaceA\n");
1102 cJSON_Delete(faultFields);
1106 if (cJSON_AddStringToObject(faultFields, "eventSourceType", "O_RAN_COMPONENT") == NULL)
1108 printf("Could not create JSON object: eventSourceType\n");
1109 cJSON_Delete(faultFields);
1113 if (cJSON_AddStringToObject(faultFields, "specificProblem", specific_problem) == NULL)
1115 printf("Could not create JSON object: specificProblem\n");
1116 cJSON_Delete(faultFields);
1120 if (cJSON_AddStringToObject(faultFields, "eventSeverity", severity) == NULL)
1122 printf("Could not create JSON object: eventSeverity\n");
1123 cJSON_Delete(faultFields);
1127 if (cJSON_AddStringToObject(faultFields, "vfStatus", "Active") == NULL)
1129 printf("Could not create JSON object: vfStatus\n");
1130 cJSON_Delete(faultFields);
1134 cJSON *alarmAdditionalInformation = cJSON_CreateObject();
1135 if (alarmAdditionalInformation == NULL)
1137 printf("Could not create JSON object: alarmAdditionalInformation\n");
1138 cJSON_Delete(faultFields);
1141 cJSON_AddItemToObject(faultFields, "alarmAdditionalInformation", alarmAdditionalInformation);
1143 if (cJSON_AddStringToObject(alarmAdditionalInformation, "eventTime", date_time) == NULL)
1145 printf("Could not create JSON object: eventTime\n");
1146 cJSON_Delete(faultFields);
1150 if (cJSON_AddStringToObject(alarmAdditionalInformation, "equipType", "O-RAN-sim") == NULL)
1152 printf("Could not create JSON object: equipType\n");
1153 cJSON_Delete(faultFields);
1157 if (cJSON_AddStringToObject(alarmAdditionalInformation, "vendor", "Melacon") == NULL)
1159 printf("Could not create JSON object: vendor\n");
1160 cJSON_Delete(faultFields);
1164 if (cJSON_AddStringToObject(alarmAdditionalInformation, "model", "Simulated Device") == NULL)
1166 printf("Could not create JSON object: model\n");
1167 cJSON_Delete(faultFields);