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)
80 // max <= RAND_MAX < ULONG_MAX, so this is okay.
81 num_bins = (unsigned long) max + 1,
82 num_rand = (unsigned long) RAND_MAX + 1,
83 bin_size = num_rand / num_bins,
84 defect = num_rand % num_bins;
87 FILE* urandom = fopen("/dev/urandom", "r");
88 fread(&seed, sizeof(int), 1, urandom);
97 // This is carefully written not to overflow
98 while (num_rand - defect <= (unsigned long)x);
100 // Truncated division is intentional
104 int getSecondsFromLastQuarterInterval(void)
106 time_t t = time(NULL);
107 time_t t_past = time(NULL);
108 struct tm tm = *localtime(&t);
109 struct tm tm_15_min_ago = tm;
111 //round to the last quarter hour
112 tm_15_min_ago.tm_min -= (tm_15_min_ago.tm_min % 15);
113 tm_15_min_ago.tm_sec = 0;
115 t=mktime(&tm_15_min_ago);
118 double seconds = difftime(t_past, t);
123 int getSecondsFromLastDayInterval(void)
125 time_t t = time(NULL);
126 time_t t_past = time(NULL);
127 struct tm tm = *localtime(&t);
128 struct tm tm_day_ago = tm;
130 //round to the last quarter hour
131 tm_day_ago.tm_hour = 0;
132 tm_day_ago.tm_min = 0;
133 tm_day_ago.tm_sec = 0;
135 t=mktime(&tm_day_ago);
138 double seconds = difftime(t_past, t);
143 void getPreviousQuarterInterval(int number_of_intervals, char *date_and_time)
145 time_t t = time(NULL);
146 t -= 15 * 60 * number_of_intervals;
147 struct tm tm = *localtime(&t);
149 tm.tm_min -= (tm.tm_min % 15);
152 sprintf(date_and_time, "%04d-%02d-%02dT%02d:%02d:%02d.0Z",
153 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
154 tm.tm_hour, tm.tm_min, tm.tm_sec);
159 void getPreviousDayPmTimestamp(int number_of_intervals, char *date_and_time)
161 time_t t = time(NULL);
162 t -= 24 * 60 * 60 * number_of_intervals;
163 struct tm tm = *localtime(&t);
169 sprintf(date_and_time, "%04d-%02d-%02dT%02d:%02d:%02d.0Z",
170 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
171 tm.tm_hour, tm.tm_min, tm.tm_sec);
176 long int getMicrosecondsSinceEpoch(void)
178 time_t t = time(NULL);
182 gettimeofday(&tv, NULL);
183 useconds = t*1000 + tv.tv_usec; //add the microseconds to the seconds
188 //TODO need to implement other authentication methods as well, not only no-auth
189 void prepare_ves_message_curl(CURL *curl)
191 curl_easy_reset(curl);
192 set_curl_common_info_ves(curl);
194 char *ves_ip = getVesIpFromConfigJson();
195 int ves_port = getVesPortFromConfigJson();
198 sprintf(url, "http://%s:%d/eventListener/v7", ves_ip, ves_port);
199 curl_easy_setopt(curl, CURLOPT_URL, url);
207 * Dynamically allocated memory;
208 * Caller needs to free the memory after it uses the value.
211 cJSON* vesCreateCommonEventHeader(char *domain, char *event_type, char *source_name, int seq_id)
213 char dateAndTime[50];
214 getCurrentDateAndTime(dateAndTime);
216 long useconds = getMicrosecondsSinceEpoch();
218 cJSON *commonEventHeader = cJSON_CreateObject();
219 if (commonEventHeader == NULL)
221 printf("Could not create JSON object: commonEventHeader\n");
225 if (cJSON_AddStringToObject(commonEventHeader, "domain", domain) == NULL)
227 printf("Could not create JSON object: domain\n");
228 cJSON_Delete(commonEventHeader);
233 sprintf(eventId, "%s_%s", source_name, dateAndTime);
235 if (cJSON_AddStringToObject(commonEventHeader, "eventId", eventId) == NULL)
237 printf("Could not create JSON object: eventId\n");
238 cJSON_Delete(commonEventHeader);
242 char event_name[200];
243 sprintf(event_name, "%s_%s", domain, event_type);
245 if (cJSON_AddStringToObject(commonEventHeader, "eventName", event_name) == NULL)
247 printf("Could not create JSON object: eventName\n");
248 cJSON_Delete(commonEventHeader);
252 if (cJSON_AddStringToObject(commonEventHeader, "eventType", event_type) == NULL)
254 printf("Could not create JSON object: eventType\n");
255 cJSON_Delete(commonEventHeader);
259 if (cJSON_AddNumberToObject(commonEventHeader, "sequence", (double)(seq_id)) == NULL)
261 printf("Could not create JSON object: sequence\n");
262 cJSON_Delete(commonEventHeader);
266 if (cJSON_AddStringToObject(commonEventHeader, "priority", "Low") == NULL)
268 printf("Could not create JSON object: priority\n");
269 cJSON_Delete(commonEventHeader);
273 if (cJSON_AddStringToObject(commonEventHeader, "reportingEntityId", "") == NULL)
275 printf("Could not create JSON object: reportingEntityId\n");
276 cJSON_Delete(commonEventHeader);
280 if (cJSON_AddStringToObject(commonEventHeader, "reportingEntityName", source_name) == NULL)
282 printf("Could not create JSON object: reportingEntityName\n");
283 cJSON_Delete(commonEventHeader);
287 if (cJSON_AddStringToObject(commonEventHeader, "sourceId", "") == NULL)
289 printf("Could not create JSON object: sourceId\n");
290 cJSON_Delete(commonEventHeader);
294 if (cJSON_AddStringToObject(commonEventHeader, "sourceName", source_name) == NULL)
296 printf("Could not create JSON object: sourceName\n");
297 cJSON_Delete(commonEventHeader);
301 if (cJSON_AddNumberToObject(commonEventHeader, "startEpochMicrosec", (double)(useconds)) == NULL)
303 printf("Could not create JSON object: startEpochMicrosec\n");
304 cJSON_Delete(commonEventHeader);
308 if (cJSON_AddNumberToObject(commonEventHeader, "lastEpochMicrosec", (double)(useconds)) == NULL)
310 printf("Could not create JSON object: lastEpochMicrosec\n");
311 cJSON_Delete(commonEventHeader);
315 if (cJSON_AddStringToObject(commonEventHeader, "nfNamingCode", "sdn controller") == NULL)
317 printf("Could not create JSON object: nfNamingCode\n");
318 cJSON_Delete(commonEventHeader);
322 if (cJSON_AddStringToObject(commonEventHeader, "nfVendorName", "sdn") == NULL)
324 printf("Could not create JSON object: nfVendorName\n");
325 cJSON_Delete(commonEventHeader);
329 if (cJSON_AddStringToObject(commonEventHeader, "timeZoneOffset", "+00:00") == NULL)
331 printf("Could not create JSON object: timeZoneOffset\n");
332 cJSON_Delete(commonEventHeader);
336 if (cJSON_AddStringToObject(commonEventHeader, "version", "4.0.1") == NULL)
338 printf("Could not create JSON object: version\n");
339 cJSON_Delete(commonEventHeader);
343 if (cJSON_AddStringToObject(commonEventHeader, "vesEventListenerVersion", "7.0.1") == NULL)
345 printf("Could not create JSON object: vesEventListenerVersion\n");
346 cJSON_Delete(commonEventHeader);
350 return commonEventHeader;
354 * Dynamically allocated memory;
355 * Caller needs to free the memory after it uses the value.
358 cJSON* vesCreateHeartbeatFields(int heartbeat_interval)
360 char dateAndTime[50];
361 getCurrentDateAndTime(dateAndTime);
363 cJSON *heartbeatFields = cJSON_CreateObject();
364 if (heartbeatFields == NULL)
366 printf("Could not create JSON object: heartbeatFields\n");
370 if (cJSON_AddStringToObject(heartbeatFields, "heartbeatFieldsVersion", "3.0") == NULL)
372 printf("Could not create JSON object: heartbeatFieldsVersion\n");
373 cJSON_Delete(heartbeatFields);
377 if (cJSON_AddNumberToObject(heartbeatFields, "heartbeatInterval", (double)(heartbeat_interval)) == NULL)
379 printf("Could not create JSON object: heartbeatInterval\n");
380 cJSON_Delete(heartbeatFields);
384 cJSON *additionalFields = cJSON_CreateObject();
385 if (additionalFields == NULL)
387 printf("Could not create JSON object: additionalFields\n");
388 cJSON_Delete(heartbeatFields);
391 cJSON_AddItemToObject(heartbeatFields, "additionalFields", additionalFields);
393 if (cJSON_AddStringToObject(additionalFields, "eventTime", dateAndTime) == NULL)
395 printf("Could not create JSON object: eventTime\n");
396 cJSON_Delete(heartbeatFields);
400 return heartbeatFields;
404 * Dynamically allocated memory;
405 * Caller needs to free the memory after it uses the value.
408 char* readConfigFileInString(void)
412 char config_file[200];
413 sprintf(config_file, "%s/configuration.json", getenv("SCRIPTS_DIR"));
414 FILE * f = fopen (config_file, "rb");
418 fseek (f, 0, SEEK_END);
420 fseek (f, 0, SEEK_SET);
421 buffer = malloc (length + 1);
424 fread (buffer, 1, length, f);
427 buffer[length] = '\0';
438 void writeConfigFile(char *config)
440 char config_file[200];
441 sprintf(config_file, "%s/configuration.json", getenv("SCRIPTS_DIR"));
442 FILE * f = fopen (config_file, "w");
451 printf("Could not write configuration file");
455 int getFaultNotificationDelayPeriodFromConfigJson(int *period_array, int *count)
457 char *stringConfig = readConfigFileInString();
459 if (stringConfig == NULL)
461 printf("Could not read JSON configuration file in string.");
465 cJSON *jsonConfig = cJSON_Parse(stringConfig);
466 if (jsonConfig == NULL)
469 const char *error_ptr = cJSON_GetErrorPtr();
470 if (error_ptr != NULL)
472 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
474 return SR_ERR_OPERATION_FAILED;
476 //we don't need the string anymore
480 cJSON *notifConfig = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
481 if (!cJSON_IsObject(notifConfig))
483 printf("Configuration JSON is not as expected: notification-config is not an object");
484 cJSON_Delete(jsonConfig);
485 return SR_ERR_OPERATION_FAILED;
488 cJSON *faultNotifDelay = cJSON_GetObjectItemCaseSensitive(notifConfig, "fault-notification-delay-period");
489 if (!cJSON_IsArray(faultNotifDelay))
491 printf("Configuration JSON is not as expected: fault-notification-delay-period is not an array.");
492 cJSON_Delete(jsonConfig);
493 return SR_ERR_OPERATION_FAILED;
496 cJSON *iterator = NULL;
500 cJSON_ArrayForEach(iterator, faultNotifDelay)
502 if (cJSON_IsNumber(iterator))
504 period_array[i++] = iterator->valueint;
508 printf("Invalid number in array!");
514 cJSON_Delete(jsonConfig);
519 int getVesHeartbeatPeriodFromConfigJson(void)
521 char *stringConfig = readConfigFileInString();
522 int vesHeartbeat = 0;
524 if (stringConfig == NULL)
526 printf("Could not read JSON configuration file in string.");
530 cJSON *jsonConfig = cJSON_Parse(stringConfig);
531 if (jsonConfig == NULL)
534 const char *error_ptr = cJSON_GetErrorPtr();
535 if (error_ptr != NULL)
537 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
539 return SR_ERR_OPERATION_FAILED;
541 //we don't need the string anymore
545 cJSON *notifConfig = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
546 if (!cJSON_IsObject(notifConfig))
548 printf("Configuration JSON is not as expected: notification-config is not an object");
549 cJSON_Delete(jsonConfig);
550 return SR_ERR_OPERATION_FAILED;
553 cJSON *vesHeartbeatPeriod = cJSON_GetObjectItemCaseSensitive(notifConfig, "ves-heartbeat-period");
554 if (!cJSON_IsNumber(vesHeartbeatPeriod))
556 printf("Configuration JSON is not as expected: ves-heartbeat-period is not a number");
557 cJSON_Delete(jsonConfig);
558 return SR_ERR_OPERATION_FAILED;
561 vesHeartbeat = (int)(vesHeartbeatPeriod->valuedouble);
563 cJSON_Delete(jsonConfig);
570 * Dynamically allocated memory;
571 * Caller needs to free the memory after it uses the value.
574 char* getVesAuthMethodFromConfigJson(void)
576 char *stringConfig = readConfigFileInString();
578 if (stringConfig == NULL)
580 printf("Could not read JSON configuration file in string.");
584 cJSON *jsonConfig = cJSON_Parse(stringConfig);
585 if (jsonConfig == NULL)
588 const char *error_ptr = cJSON_GetErrorPtr();
589 if (error_ptr != NULL)
591 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
595 //we don't need the string anymore
599 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
600 if (!cJSON_IsObject(vesDetails))
602 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
603 cJSON_Delete(jsonConfig);
607 cJSON *vesAuthMethod = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-auth-method");
608 if (!cJSON_IsString(vesAuthMethod))
610 printf("Configuration JSON is not as expected: ves-endpoint-auth-method is not an object");
611 cJSON_Delete(jsonConfig);
615 char *auth_method_string = strdup(cJSON_GetStringValue(vesAuthMethod));
617 cJSON_Delete(jsonConfig);
619 return auth_method_string;
623 * Dynamically allocated memory;
624 * Caller needs to free the memory after it uses the value.
627 char* getVesIpFromConfigJson(void)
629 char *stringConfig = readConfigFileInString();
631 if (stringConfig == NULL)
633 printf("Could not read JSON configuration file in string.");
637 cJSON *jsonConfig = cJSON_Parse(stringConfig);
638 if (jsonConfig == NULL)
641 const char *error_ptr = cJSON_GetErrorPtr();
642 if (error_ptr != NULL)
644 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
648 //we don't need the string anymore
652 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
653 if (!cJSON_IsObject(vesDetails))
655 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
656 cJSON_Delete(jsonConfig);
660 cJSON *vesIp = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-ip");
661 if (!cJSON_IsString(vesIp))
663 printf("Configuration JSON is not as expected: ves-endpoint-ip is not an object");
664 cJSON_Delete(jsonConfig);
668 char *ves_ip = strdup(cJSON_GetStringValue(vesIp));
670 cJSON_Delete(jsonConfig);
675 int getVesPortFromConfigJson(void)
677 char *stringConfig = readConfigFileInString();
679 if (stringConfig == NULL)
681 printf("Could not read JSON configuration file in string.");
685 cJSON *jsonConfig = cJSON_Parse(stringConfig);
686 if (jsonConfig == NULL)
689 const char *error_ptr = cJSON_GetErrorPtr();
690 if (error_ptr != NULL)
692 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
694 return SR_ERR_OPERATION_FAILED;
696 //we don't need the string anymore
700 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
701 if (!cJSON_IsObject(vesDetails))
703 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
704 cJSON_Delete(jsonConfig);
705 return SR_ERR_OPERATION_FAILED;
708 cJSON *vesPort = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-port");
709 if (!cJSON_IsNumber(vesPort))
711 printf("Configuration JSON is not as expected: ves-endpoint-port is not an object");
712 cJSON_Delete(jsonConfig);
713 return SR_ERR_OPERATION_FAILED;
716 int port = (int)(vesPort->valuedouble);
718 cJSON_Delete(jsonConfig);
723 int getVesRegistrationFromConfigJson(void)
725 char *stringConfig = readConfigFileInString();
727 if (stringConfig == NULL)
729 printf("Could not read JSON configuration file in string.");
733 cJSON *jsonConfig = cJSON_Parse(stringConfig);
734 if (jsonConfig == NULL)
737 const char *error_ptr = cJSON_GetErrorPtr();
738 if (error_ptr != NULL)
740 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
742 return SR_ERR_OPERATION_FAILED;
744 //we don't need the string anymore
748 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
749 if (!cJSON_IsObject(vesDetails))
751 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
752 cJSON_Delete(jsonConfig);
753 return SR_ERR_OPERATION_FAILED;
756 cJSON *vesReg = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-registration");
757 if (!cJSON_IsBool(vesReg))
759 printf("Configuration JSON is not as expected: ves-registration is not an object");
760 cJSON_Delete(jsonConfig);
761 return SR_ERR_OPERATION_FAILED;
764 int is_ves_reg = (cJSON_IsTrue(vesReg)) ? TRUE : FALSE;
766 cJSON_Delete(jsonConfig);
772 * Dynamically allocated memory;
773 * Caller needs to free the memory after it uses the value.
776 cJSON* vesCreatePnfRegistrationFields(int port, bool is_tls)
778 cJSON *pnfRegistrationFields = cJSON_CreateObject();
779 if (pnfRegistrationFields == NULL)
781 printf("Could not create JSON object: pnfRegistrationFields\n");
785 if (cJSON_AddStringToObject(pnfRegistrationFields, "pnfRegistrationFieldsVersion", "2.0") == NULL)
787 printf("Could not create JSON object: pnfRegistrationFieldsVersion\n");
788 cJSON_Delete(pnfRegistrationFields);
792 if (cJSON_AddStringToObject(pnfRegistrationFields, "lastServiceDate", "2019-08-16") == NULL)
794 printf("Could not create JSON object: lastServiceDate\n");
795 cJSON_Delete(pnfRegistrationFields);
800 generateRandomMacAddress(mac_addr);
802 if (cJSON_AddStringToObject(pnfRegistrationFields, "macAddress", mac_addr) == NULL)
804 printf("Could not create JSON object: macAddress\n");
805 cJSON_Delete(pnfRegistrationFields);
809 if (cJSON_AddStringToObject(pnfRegistrationFields, "manufactureDate", "2019-08-16") == NULL)
811 printf("Could not create JSON object: manufactureDate\n");
812 cJSON_Delete(pnfRegistrationFields);
816 if (cJSON_AddStringToObject(pnfRegistrationFields, "modelNumber", "Simulated Device Melacon") == NULL)
818 printf("Could not create JSON object: manufactureDate\n");
819 cJSON_Delete(pnfRegistrationFields);
823 if (cJSON_AddStringToObject(pnfRegistrationFields, "oamV4IpAddress", getenv("NTS_IP")) == NULL)
825 printf("Could not create JSON object: oamV4IpAddress\n");
826 cJSON_Delete(pnfRegistrationFields);
830 if (cJSON_AddStringToObject(pnfRegistrationFields, "oamV6IpAddress", "0:0:0:0:0:ffff:a0a:011") == NULL)
832 printf("Could not create JSON object: oamV6IpAddress\n");
833 cJSON_Delete(pnfRegistrationFields);
837 char serial_number[100];
838 sprintf(serial_number, "%s-%s-%d-Simulated Device Melacon", getenv("HOSTNAME"), getenv("NTS_IP"), port);
840 if (cJSON_AddStringToObject(pnfRegistrationFields, "serialNumber", serial_number) == NULL)
842 printf("Could not create JSON object: serialNumber\n");
843 cJSON_Delete(pnfRegistrationFields);
847 if (cJSON_AddStringToObject(pnfRegistrationFields, "softwareVersion", "2.3.5") == NULL)
849 printf("Could not create JSON object: softwareVersion\n");
850 cJSON_Delete(pnfRegistrationFields);
854 if (cJSON_AddStringToObject(pnfRegistrationFields, "unitFamily", "Simulated Device") == NULL)
856 printf("Could not create JSON object: unitFamily\n");
857 cJSON_Delete(pnfRegistrationFields);
861 if (cJSON_AddStringToObject(pnfRegistrationFields, "unitType", "O-RAN-sim") == NULL)
863 printf("Could not create JSON object: unitType\n");
864 cJSON_Delete(pnfRegistrationFields);
868 if (cJSON_AddStringToObject(pnfRegistrationFields, "vendorName", "Melacon") == NULL)
870 printf("Could not create JSON object: vendorName\n");
871 cJSON_Delete(pnfRegistrationFields);
875 cJSON *additionalFields = cJSON_CreateObject();
876 if (additionalFields == NULL)
878 printf("Could not create JSON object: additionalFields\n");
879 cJSON_Delete(pnfRegistrationFields);
882 cJSON_AddItemToObject(pnfRegistrationFields, "additionalFields", additionalFields);
885 sprintf(portString, "%d", port);
887 if (cJSON_AddStringToObject(additionalFields, "oamPort", portString) == NULL)
889 printf("Could not create JSON object: oamPort\n");
890 cJSON_Delete(pnfRegistrationFields);
896 //TLS specific configuration
897 if (cJSON_AddStringToObject(additionalFields, "protocol", "TLS") == NULL)
899 printf("Could not create JSON object: protocol\n");
900 cJSON_Delete(pnfRegistrationFields);
904 //TODO here we have the username from the docker container hardcoded: netconf
905 if (cJSON_AddStringToObject(additionalFields, "username", "netconf") == NULL)
907 printf("Could not create JSON object: username\n");
908 cJSON_Delete(pnfRegistrationFields);
912 if (cJSON_AddStringToObject(additionalFields, "keyId", "device-key") == NULL)
914 printf("Could not create JSON object: keyId\n");
915 cJSON_Delete(pnfRegistrationFields);
921 //SSH specific configuration
922 if (cJSON_AddStringToObject(additionalFields, "protocol", "SSH") == NULL)
924 printf("Could not create JSON object: protocol\n");
925 cJSON_Delete(pnfRegistrationFields);
929 //TODO here we have the username from the docker container hardcoded: netconf
930 if (cJSON_AddStringToObject(additionalFields, "username", "netconf") == NULL)
932 printf("Could not create JSON object: username\n");
933 cJSON_Delete(pnfRegistrationFields);
937 //TODO here we have the password from the docker container hardcoded: netconf
938 if (cJSON_AddStringToObject(additionalFields, "password", "netconf") == NULL)
940 printf("Could not create JSON object: password\n");
941 cJSON_Delete(pnfRegistrationFields);
946 if (cJSON_AddStringToObject(additionalFields, "reconnectOnChangedSchema", "false") == NULL)
948 printf("Could not create JSON object: reconnectOnChangedSchema\n");
949 cJSON_Delete(pnfRegistrationFields);
953 if (cJSON_AddStringToObject(additionalFields, "sleep-factor", "1.5") == NULL)
955 printf("Could not create JSON object: sleep-factor\n");
956 cJSON_Delete(pnfRegistrationFields);
960 if (cJSON_AddStringToObject(additionalFields, "tcpOnly", "false") == NULL)
962 printf("Could not create JSON object: tcpOnly\n");
963 cJSON_Delete(pnfRegistrationFields);
967 if (cJSON_AddStringToObject(additionalFields, "connectionTimeout", "20000") == NULL)
969 printf("Could not create JSON object: connectionTimeout\n");
970 cJSON_Delete(pnfRegistrationFields);
974 if (cJSON_AddStringToObject(additionalFields, "maxConnectionAttempts", "100") == NULL)
976 printf("Could not create JSON object: maxConnectionAttempts\n");
977 cJSON_Delete(pnfRegistrationFields);
981 if (cJSON_AddStringToObject(additionalFields, "betweenAttemptsTimeout", "2000") == NULL)
983 printf("Could not create JSON object: betweenAttemptsTimeout\n");
984 cJSON_Delete(pnfRegistrationFields);
988 if (cJSON_AddStringToObject(additionalFields, "keepaliveDelay", "120") == NULL)
990 printf("Could not create JSON object: keepaliveDelay\n");
991 cJSON_Delete(pnfRegistrationFields);
995 return pnfRegistrationFields;
998 int getNetconfAvailableFromConfigJson(void)
1000 char *stringConfig = readConfigFileInString();
1002 if (stringConfig == NULL)
1004 printf("Could not read JSON configuration file in string.");
1008 cJSON *jsonConfig = cJSON_Parse(stringConfig);
1009 if (jsonConfig == NULL)
1012 const char *error_ptr = cJSON_GetErrorPtr();
1013 if (error_ptr != NULL)
1015 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
1017 return SR_ERR_OPERATION_FAILED;
1019 //we don't need the string anymore
1021 stringConfig = NULL;
1023 cJSON *notifDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
1024 if (!cJSON_IsObject(notifDetails))
1026 printf("Configuration JSON is not as expected: notification-config is not an object");
1027 cJSON_Delete(jsonConfig);
1028 return SR_ERR_OPERATION_FAILED;
1031 cJSON *isNetconfAvailable = cJSON_GetObjectItemCaseSensitive(notifDetails, "is-netconf-available");
1032 if (!cJSON_IsBool(isNetconfAvailable))
1034 printf("Configuration JSON is not as expected: is-netconf-available is not an object");
1035 cJSON_Delete(jsonConfig);
1036 return SR_ERR_OPERATION_FAILED;
1039 int is_netconf_available = (cJSON_IsTrue(isNetconfAvailable)) ? TRUE : FALSE;
1041 cJSON_Delete(jsonConfig);
1043 return is_netconf_available;
1046 int getVesAvailableFromConfigJson(void)
1048 char *stringConfig = readConfigFileInString();
1050 if (stringConfig == NULL)
1052 printf("Could not read JSON configuration file in string.");
1056 cJSON *jsonConfig = cJSON_Parse(stringConfig);
1057 if (jsonConfig == NULL)
1060 const char *error_ptr = cJSON_GetErrorPtr();
1061 if (error_ptr != NULL)
1063 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
1065 return SR_ERR_OPERATION_FAILED;
1067 //we don't need the string anymore
1069 stringConfig = NULL;
1071 cJSON *notifDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
1072 if (!cJSON_IsObject(notifDetails))
1074 printf("Configuration JSON is not as expected: notification-config is not an object");
1075 cJSON_Delete(jsonConfig);
1076 return SR_ERR_OPERATION_FAILED;
1079 cJSON *isVesAvailable = cJSON_GetObjectItemCaseSensitive(notifDetails, "is-ves-available");
1080 if (!cJSON_IsBool(isVesAvailable))
1082 printf("Configuration JSON is not as expected: is-ves-available is not an object");
1083 cJSON_Delete(jsonConfig);
1084 return SR_ERR_OPERATION_FAILED;
1087 int is_netconf_available = (cJSON_IsTrue(isVesAvailable)) ? TRUE : FALSE;
1089 cJSON_Delete(jsonConfig);
1091 return is_netconf_available;
1095 * Dynamically allocated memory;
1096 * Caller needs to free the memory after it uses the value.
1099 cJSON* vesCreateFaultFields(char *alarm_condition, char *alarm_object, char *severity, char *date_time, char *specific_problem)
1101 cJSON *faultFields = cJSON_CreateObject();
1102 if (faultFields == NULL)
1104 printf("Could not create JSON object: faultFields\n");
1108 if (cJSON_AddStringToObject(faultFields, "faultFieldsVersion", "4.0") == NULL)
1110 printf("Could not create JSON object: faultFieldsVersion\n");
1111 cJSON_Delete(faultFields);
1115 if (cJSON_AddStringToObject(faultFields, "alarmCondition", alarm_condition) == NULL)
1117 printf("Could not create JSON object: alarmCondition\n");
1118 cJSON_Delete(faultFields);
1122 if (cJSON_AddStringToObject(faultFields, "alarmInterfaceA", alarm_object) == NULL)
1124 printf("Could not create JSON object: alarmInterfaceA\n");
1125 cJSON_Delete(faultFields);
1129 if (cJSON_AddStringToObject(faultFields, "eventSourceType", "O_RAN_COMPONENT") == NULL)
1131 printf("Could not create JSON object: eventSourceType\n");
1132 cJSON_Delete(faultFields);
1136 if (cJSON_AddStringToObject(faultFields, "specificProblem", specific_problem) == NULL)
1138 printf("Could not create JSON object: specificProblem\n");
1139 cJSON_Delete(faultFields);
1143 if (cJSON_AddStringToObject(faultFields, "eventSeverity", severity) == NULL)
1145 printf("Could not create JSON object: eventSeverity\n");
1146 cJSON_Delete(faultFields);
1150 if (cJSON_AddStringToObject(faultFields, "vfStatus", "Active") == NULL)
1152 printf("Could not create JSON object: vfStatus\n");
1153 cJSON_Delete(faultFields);
1157 cJSON *alarmAdditionalInformation = cJSON_CreateObject();
1158 if (alarmAdditionalInformation == NULL)
1160 printf("Could not create JSON object: alarmAdditionalInformation\n");
1161 cJSON_Delete(faultFields);
1164 cJSON_AddItemToObject(faultFields, "alarmAdditionalInformation", alarmAdditionalInformation);
1166 if (cJSON_AddStringToObject(alarmAdditionalInformation, "eventTime", date_time) == NULL)
1168 printf("Could not create JSON object: eventTime\n");
1169 cJSON_Delete(faultFields);
1173 if (cJSON_AddStringToObject(alarmAdditionalInformation, "equipType", "O-RAN-sim") == NULL)
1175 printf("Could not create JSON object: equipType\n");
1176 cJSON_Delete(faultFields);
1180 if (cJSON_AddStringToObject(alarmAdditionalInformation, "vendor", "Melacon") == NULL)
1182 printf("Could not create JSON object: vendor\n");
1183 cJSON_Delete(faultFields);
1187 if (cJSON_AddStringToObject(alarmAdditionalInformation, "model", "Simulated Device") == NULL)
1189 printf("Could not create JSON object: model\n");
1190 cJSON_Delete(faultFields);
1197 static cJSON* createSeverityCounters(counterAlarms count)
1199 cJSON *severityCounters = cJSON_CreateObject();
1200 if (severityCounters == NULL)
1202 printf("Could not create JSON object: severityCounters\n");
1206 if (cJSON_AddNumberToObject(severityCounters, "severity-normal", count.normal) == NULL)
1208 printf("Could not create JSON object: severity-normal\n");
1212 if (cJSON_AddNumberToObject(severityCounters, "severity-warning", count.warning) == NULL)
1214 printf("Could not create JSON object: severity-warning\n");
1218 if (cJSON_AddNumberToObject(severityCounters, "severity-minor", count.minor) == NULL)
1220 printf("Could not create JSON object: severity-minor\n");
1224 if (cJSON_AddNumberToObject(severityCounters, "severity-major", count.major) == NULL)
1226 printf("Could not create JSON object: severity-major\n");
1230 if (cJSON_AddNumberToObject(severityCounters, "severity-critical", count.critical) == NULL)
1232 printf("Could not create JSON object: severity-critical\n");
1236 return severityCounters;
1239 void writeStatusFile(char *status)
1241 char status_file[200];
1242 sprintf(status_file, "%s/status.json", getenv("SCRIPTS_DIR"));
1243 FILE * f = fopen (status_file, "w");
1252 printf("Could not write status file!\n");
1256 int writeSkeletonStatusFile()
1258 cJSON *statusObject = cJSON_CreateObject();
1259 if (statusObject == NULL)
1261 printf("Could not create JSON object: statusObject\n");
1262 return SR_ERR_OPERATION_FAILED;
1265 // counterAlarms counter = {
1273 // cJSON *totalVesNotifications = createSeverityCounters(counter);
1274 // if (totalVesNotifications == NULL)
1276 // printf("Could not create JSON object: totalVesNotifications\n");
1277 // cJSON_Delete(statusObject);
1278 // return SR_ERR_OPERATION_FAILED;
1280 // cJSON_AddItemToObject(statusObject, "total-ves-notifications-sent", totalVesNotifications);
1282 // cJSON *totalNetconfNotifications = createSeverityCounters(counter);
1283 // if (totalNetconfNotifications == NULL)
1285 // printf("Could not create JSON object: totalNetconfNotifications\n");
1286 // cJSON_Delete(statusObject);
1287 // return SR_ERR_OPERATION_FAILED;
1289 // cJSON_AddItemToObject(statusObject, "total-netconf-notifications-sent", totalNetconfNotifications);
1291 cJSON *deviceList = cJSON_CreateArray();
1292 if (deviceList == NULL)
1294 printf("Could not create JSON object: deviceList\n");
1295 cJSON_Delete(statusObject);
1296 return SR_ERR_OPERATION_FAILED;
1298 cJSON_AddItemToObject(statusObject, "device-list", deviceList);
1300 char *status_string = NULL;
1302 status_string = cJSON_PrintUnformatted(statusObject);
1304 writeStatusFile(status_string);
1306 if (status_string != NULL)
1308 free(status_string);
1309 status_string = NULL;
1312 if (statusObject != NULL)
1314 cJSON_Delete(statusObject);
1321 * Dynamically allocated memory;
1322 * Caller needs to free the memory after it uses the value.
1325 char* readStatusFileInString(void)
1329 char config_file[200];
1330 sprintf(config_file, "%s/status.json", getenv("SCRIPTS_DIR"));
1331 FILE * f = fopen (config_file, "rb");
1335 fseek (f, 0, SEEK_END);
1337 fseek (f, 0, SEEK_SET);
1338 buffer = malloc (length + 1);
1341 fread (buffer, 1, length, f);
1344 buffer[length] = '\0';
1356 * Dynamically allocated memory;
1357 * Caller needs to free the memory after it uses the value.
1360 cJSON* getDeviceListFromStatusFile(void)
1362 char *stringStatus = readStatusFileInString();
1364 if (stringStatus == NULL)
1366 printf("Could not read status file!\n");
1370 cJSON *jsonStatus = cJSON_Parse(stringStatus);
1371 if (jsonStatus == NULL)
1374 const char *error_ptr = cJSON_GetErrorPtr();
1375 if (error_ptr != NULL)
1377 fprintf(stderr, "Could not parse JSON status! Error before: %s\n", error_ptr);
1381 //we don't need the string anymore
1383 stringStatus = NULL;
1388 cJSON* createDeviceListEntry(counterAlarms ves_counter, counterAlarms netconf_counter)
1390 cJSON *deviceListEntry = cJSON_CreateObject();
1391 if (deviceListEntry == NULL)
1393 printf("Could not create JSON object: deviceListEntry\n");
1398 sprintf(hostname, "%s", getenv("HOSTNAME"));
1400 if (cJSON_AddStringToObject(deviceListEntry, "device-name", hostname) == NULL)
1402 printf("Could not create JSON object: device-name\n");
1403 cJSON_Delete(deviceListEntry);
1407 cJSON *vesNotificationsSent = createSeverityCounters(ves_counter);
1408 if (vesNotificationsSent == NULL)
1410 printf("Could not create JSON object: vesNotificationsSent\n");
1411 cJSON_Delete(deviceListEntry);
1414 cJSON_AddItemToObject(deviceListEntry, "ves-notifications-sent", vesNotificationsSent);
1416 cJSON *netconfNotificationsSent = createSeverityCounters(netconf_counter);
1417 if (netconfNotificationsSent == NULL)
1419 printf("Could not create JSON object: netconfNotificationsSent\n");
1420 cJSON_Delete(deviceListEntry);
1423 cJSON_AddItemToObject(deviceListEntry, "netconf-notifications-sent", netconfNotificationsSent);
1425 return deviceListEntry;
1428 static void modifySeverityCounters(cJSON **severityCounters, counterAlarms count)
1430 cJSON *severity= cJSON_GetObjectItemCaseSensitive(*severityCounters, "severity-normal");
1431 if (!cJSON_IsNumber(severity))
1433 printf("Status JSON is not as expected: severity-normal is not an number");
1436 //we set the value of the severity-normal object
1437 cJSON_SetNumberValue(severity, count.normal);
1439 severity= cJSON_GetObjectItemCaseSensitive(*severityCounters, "severity-warning");
1440 if (!cJSON_IsNumber(severity))
1442 printf("Status JSON is not as expected: severity-warning is not an number");
1445 //we set the value of the severity-warning object
1446 cJSON_SetNumberValue(severity, count.warning);
1448 severity= cJSON_GetObjectItemCaseSensitive(*severityCounters, "severity-minor");
1449 if (!cJSON_IsNumber(severity))
1451 printf("Status JSON is not as expected: severity-minor is not an number");
1454 //we set the value of the severity-minor object
1455 cJSON_SetNumberValue(severity, count.minor);
1457 severity= cJSON_GetObjectItemCaseSensitive(*severityCounters, "severity-major");
1458 if (!cJSON_IsNumber(severity))
1460 printf("Status JSON is not as expected: severity-major is not an number");
1463 //we set the value of the severity-major object
1464 cJSON_SetNumberValue(severity, count.major);
1466 severity= cJSON_GetObjectItemCaseSensitive(*severityCounters, "severity-critical");
1467 if (!cJSON_IsNumber(severity))
1469 printf("Status JSON is not as expected: severity-critical is not an number");
1472 //we set the value of the severity-critical object
1473 cJSON_SetNumberValue(severity, count.critical);
1478 static void modifyDeviceListEntry(cJSON **deviceListEntry, counterAlarms ves_counter, counterAlarms netconf_counter)
1480 cJSON *vesNotificationsSent= cJSON_GetObjectItemCaseSensitive(*deviceListEntry, "ves-notifications-sent");
1481 if (!cJSON_IsObject(vesNotificationsSent))
1483 printf("Status JSON is not as expected: ves-notifications-sent is not a object");
1487 modifySeverityCounters(&vesNotificationsSent, ves_counter);
1489 cJSON *netconfNotificationsSent= cJSON_GetObjectItemCaseSensitive(*deviceListEntry, "netconf-notifications-sent");
1490 if (!cJSON_IsObject(netconfNotificationsSent))
1492 printf("Status JSON is not as expected: netconf-notifications-sent is not a object");
1496 modifySeverityCounters(&netconfNotificationsSent, netconf_counter);
1499 int writeStatusNotificationCounters(counterAlarms ves_counter, counterAlarms netconf_counter)
1501 cJSON *jsonStatus = getDeviceListFromStatusFile();
1503 cJSON *deviceList = cJSON_GetObjectItemCaseSensitive(jsonStatus, "device-list");
1504 if (!cJSON_IsArray(deviceList))
1506 printf("Status JSON is not as expected: device-list is not an object");
1507 cJSON_Delete(jsonStatus);
1508 return SR_ERR_OPERATION_FAILED;
1511 int array_size = cJSON_GetArraySize(deviceList);
1514 for (int i=0; i<array_size; ++i)
1516 cJSON *deviceListEntry = cJSON_GetArrayItem(deviceList, i);
1518 sprintf(hostname, "%s", getenv("HOSTNAME"));
1520 cJSON *deviceName = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "device-name");
1521 if (!cJSON_IsString(deviceName))
1523 printf("Status JSON is not as expected: device-name is not a string.");
1524 cJSON_Delete(jsonStatus);
1525 return SR_ERR_OPERATION_FAILED;
1527 char *deviceNameString = cJSON_GetStringValue(deviceName);
1529 if (strcmp(hostname, deviceNameString) == 0)
1531 modifyDeviceListEntry(&deviceListEntry, ves_counter, netconf_counter);
1538 cJSON* deviceListEntry = createDeviceListEntry(ves_counter, netconf_counter);
1540 cJSON_AddItemToArray(deviceList, deviceListEntry);
1543 //writing the new JSON to the configuration file
1544 char *stringStatus = cJSON_PrintUnformatted(jsonStatus);
1545 writeStatusFile(stringStatus);
1547 if (stringStatus != NULL)
1550 stringStatus = NULL;
1553 if (jsonStatus != NULL)
1555 cJSON_Delete(jsonStatus);
1562 int removeDeviceEntryFromStatusFile(char *containerId)
1564 cJSON *jsonStatus = getDeviceListFromStatusFile();
1566 cJSON *deviceList = cJSON_GetObjectItemCaseSensitive(jsonStatus, "device-list");
1567 if (!cJSON_IsArray(deviceList))
1569 printf("Status JSON is not as expected: device-list is not an object");
1570 cJSON_Delete(jsonStatus);
1571 return SR_ERR_OPERATION_FAILED;
1574 int array_size = cJSON_GetArraySize(deviceList);
1575 int found = array_size;
1577 for (int i=0; i<array_size; ++i)
1579 cJSON *deviceListEntry = cJSON_GetArrayItem(deviceList, i);
1581 sprintf(hostname, "%s", getenv("HOSTNAME"));
1583 cJSON *deviceName = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "device-name");
1584 if (!cJSON_IsString(deviceName))
1586 printf("Status JSON is not as expected: device-name is not a string.");
1587 cJSON_Delete(jsonStatus);
1588 return SR_ERR_OPERATION_FAILED;
1590 char *deviceNameString = cJSON_GetStringValue(deviceName);
1592 if (strcmp(containerId, deviceNameString) == 0)
1599 if (found < array_size)
1601 cJSON_DeleteItemFromArray(deviceList, found);
1605 printf("Could not find status file entry for device with id=\"%s\"", containerId);
1608 //writing the new JSON to the configuration file
1609 char *stringStatus = cJSON_PrintUnformatted(jsonStatus);
1610 writeStatusFile(stringStatus);
1612 if (stringStatus != NULL)
1615 stringStatus = NULL;
1618 if (jsonStatus != NULL)
1620 cJSON_Delete(jsonStatus);
1626 int compute_notifications_count(counterAlarms *ves_counter, counterAlarms *netconf_counter)
1628 ves_counter->normal = ves_counter->warning = \
1629 ves_counter->minor = ves_counter->major = \
1630 ves_counter->critical = 0;
1631 netconf_counter->normal = netconf_counter->warning = \
1632 netconf_counter->minor = netconf_counter->major = \
1633 netconf_counter->critical = 0;
1635 cJSON *jsonStatus = getDeviceListFromStatusFile();
1637 cJSON *deviceList = cJSON_GetObjectItemCaseSensitive(jsonStatus, "device-list");
1638 if (!cJSON_IsArray(deviceList))
1640 printf("Status JSON is not as expected: device-list is not an object");
1641 cJSON_Delete(jsonStatus);
1642 return SR_ERR_OPERATION_FAILED;
1645 int array_size = cJSON_GetArraySize(deviceList);
1647 for (int i=0; i<array_size; ++i)
1649 cJSON *deviceListEntry = cJSON_GetArrayItem(deviceList, i);
1651 cJSON *vesNotifications = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "ves-notifications-sent");
1652 if (!cJSON_IsObject(vesNotifications))
1654 printf("Status JSON is not as expected: ves-notifications-sent is not an object.");
1655 cJSON_Delete(jsonStatus);
1656 return SR_ERR_OPERATION_FAILED;
1659 cJSON *severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-normal");
1660 if (!cJSON_IsNumber(severity))
1662 printf("Status JSON is not as expected: severity-normal is not a string.");
1663 cJSON_Delete(jsonStatus);
1664 return SR_ERR_OPERATION_FAILED;
1666 int counter = (int)(severity->valuedouble);
1667 ves_counter->normal += counter;
1669 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-warning");
1670 if (!cJSON_IsNumber(severity))
1672 printf("Status JSON is not as expected: severity-warning is not a string.");
1673 cJSON_Delete(jsonStatus);
1674 return SR_ERR_OPERATION_FAILED;
1676 counter = (int)(severity->valuedouble);
1677 ves_counter->warning += counter;
1679 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-minor");
1680 if (!cJSON_IsNumber(severity))
1682 printf("Status JSON is not as expected: severity-minor is not a string.");
1683 cJSON_Delete(jsonStatus);
1684 return SR_ERR_OPERATION_FAILED;
1686 counter = (int)(severity->valuedouble);
1687 ves_counter->minor += counter;
1689 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-major");
1690 if (!cJSON_IsNumber(severity))
1692 printf("Status JSON is not as expected: severity-major is not a string.");
1693 cJSON_Delete(jsonStatus);
1694 return SR_ERR_OPERATION_FAILED;
1696 counter = (int)(severity->valuedouble);
1697 ves_counter->major += counter;
1699 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-critical");
1700 if (!cJSON_IsNumber(severity))
1702 printf("Status JSON is not as expected: severity-critical is not a string.");
1703 cJSON_Delete(jsonStatus);
1704 return SR_ERR_OPERATION_FAILED;
1706 counter = (int)(severity->valuedouble);
1707 ves_counter->critical += counter;
1709 cJSON *netconfNotifications = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "netconf-notifications-sent");
1710 if (!cJSON_IsObject(netconfNotifications))
1712 printf("Status JSON is not as expected: netconf-notifications-sent is not an object.");
1713 cJSON_Delete(jsonStatus);
1714 return SR_ERR_OPERATION_FAILED;
1717 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-normal");
1718 if (!cJSON_IsNumber(severity))
1720 printf("Status JSON is not as expected: severity-normal is not a string.");
1721 cJSON_Delete(jsonStatus);
1722 return SR_ERR_OPERATION_FAILED;
1725 counter = (int)(severity->valuedouble);
1726 netconf_counter->normal += (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1728 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-warning");
1729 if (!cJSON_IsNumber(severity))
1731 printf("Status JSON is not as expected: severity-warning is not a string.");
1732 cJSON_Delete(jsonStatus);
1733 return SR_ERR_OPERATION_FAILED;
1735 counter = (int)(severity->valuedouble);
1736 netconf_counter->warning += (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1738 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-minor");
1739 if (!cJSON_IsNumber(severity))
1741 printf("Status JSON is not as expected: severity-minor is not a string.");
1742 cJSON_Delete(jsonStatus);
1743 return SR_ERR_OPERATION_FAILED;
1745 counter = (int)(severity->valuedouble);
1746 netconf_counter->minor += (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1748 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-major");
1749 if (!cJSON_IsNumber(severity))
1751 printf("Status JSON is not as expected: severity-major is not a string.");
1752 cJSON_Delete(jsonStatus);
1753 return SR_ERR_OPERATION_FAILED;
1755 counter = (int)(severity->valuedouble);
1756 netconf_counter->major += (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1758 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-critical");
1759 if (!cJSON_IsNumber(severity))
1761 printf("Status JSON is not as expected: severity-critical is not a string.");
1762 cJSON_Delete(jsonStatus);
1763 return SR_ERR_OPERATION_FAILED;
1765 counter = (int)(severity->valuedouble);
1766 netconf_counter->critical += (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1769 cJSON_Delete(jsonStatus);
1774 int getDeviceCounters(char *containerId, counterAlarms *ves_counter, counterAlarms *netconf_counter)
1776 cJSON *jsonStatus = getDeviceListFromStatusFile();
1778 cJSON *deviceList = cJSON_GetObjectItemCaseSensitive(jsonStatus, "device-list");
1779 if (!cJSON_IsArray(deviceList))
1781 printf("Status JSON is not as expected: device-list is not an object");
1782 cJSON_Delete(jsonStatus);
1783 return SR_ERR_OPERATION_FAILED;
1786 int array_size = cJSON_GetArraySize(deviceList);
1788 ves_counter->critical = ves_counter->major = ves_counter->minor = ves_counter->warning = ves_counter->normal = 0;
1789 netconf_counter->critical = netconf_counter->major = netconf_counter->minor = netconf_counter->warning = netconf_counter->normal = 0;
1791 for (int i=0; i<array_size; ++i)
1793 cJSON *deviceListEntry = cJSON_GetArrayItem(deviceList, i);
1795 cJSON *deviceName = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "device-name");
1796 if (!cJSON_IsString(deviceName))
1798 printf("Status JSON is not as expected: device-name is not a string.");
1799 cJSON_Delete(jsonStatus);
1800 return SR_ERR_OPERATION_FAILED;
1802 char *deviceNameString = cJSON_GetStringValue(deviceName);
1804 if (strcmp(deviceNameString, containerId) != 0)
1809 cJSON *vesNotifications = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "ves-notifications-sent");
1810 if (!cJSON_IsObject(vesNotifications))
1812 printf("Status JSON is not as expected: ves-notifications-sent is not an object.");
1813 cJSON_Delete(jsonStatus);
1814 return SR_ERR_OPERATION_FAILED;
1817 cJSON *severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-normal");
1818 if (!cJSON_IsNumber(severity))
1820 printf("Status JSON is not as expected: severity-normal is not a string.");
1821 cJSON_Delete(jsonStatus);
1822 return SR_ERR_OPERATION_FAILED;
1824 int counter = (int)(severity->valuedouble);
1825 ves_counter->normal = counter;
1827 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-warning");
1828 if (!cJSON_IsNumber(severity))
1830 printf("Status JSON is not as expected: severity-warning is not a string.");
1831 cJSON_Delete(jsonStatus);
1832 return SR_ERR_OPERATION_FAILED;
1834 counter = (int)(severity->valuedouble);
1835 ves_counter->warning = counter;
1837 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-minor");
1838 if (!cJSON_IsNumber(severity))
1840 printf("Status JSON is not as expected: severity-minor is not a string.");
1841 cJSON_Delete(jsonStatus);
1842 return SR_ERR_OPERATION_FAILED;
1844 counter = (int)(severity->valuedouble);
1845 ves_counter->minor = counter;
1847 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-major");
1848 if (!cJSON_IsNumber(severity))
1850 printf("Status JSON is not as expected: severity-major is not a string.");
1851 cJSON_Delete(jsonStatus);
1852 return SR_ERR_OPERATION_FAILED;
1854 counter = (int)(severity->valuedouble);
1855 ves_counter->major = counter;
1857 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-critical");
1858 if (!cJSON_IsNumber(severity))
1860 printf("Status JSON is not as expected: severity-critical is not a string.");
1861 cJSON_Delete(jsonStatus);
1862 return SR_ERR_OPERATION_FAILED;
1864 counter = (int)(severity->valuedouble);
1865 ves_counter->critical = counter;
1867 cJSON *netconfNotifications = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "netconf-notifications-sent");
1868 if (!cJSON_IsObject(netconfNotifications))
1870 printf("Status JSON is not as expected: netconf-notifications-sent is not an object.");
1871 cJSON_Delete(jsonStatus);
1872 return SR_ERR_OPERATION_FAILED;
1875 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-normal");
1876 if (!cJSON_IsNumber(severity))
1878 printf("Status JSON is not as expected: severity-normal is not a string.");
1879 cJSON_Delete(jsonStatus);
1880 return SR_ERR_OPERATION_FAILED;
1883 counter = (int)(severity->valuedouble);
1884 netconf_counter->normal = (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1886 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-warning");
1887 if (!cJSON_IsNumber(severity))
1889 printf("Status JSON is not as expected: severity-warning is not a string.");
1890 cJSON_Delete(jsonStatus);
1891 return SR_ERR_OPERATION_FAILED;
1893 counter = (int)(severity->valuedouble);
1894 netconf_counter->warning = (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1896 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-minor");
1897 if (!cJSON_IsNumber(severity))
1899 printf("Status JSON is not as expected: severity-minor is not a string.");
1900 cJSON_Delete(jsonStatus);
1901 return SR_ERR_OPERATION_FAILED;
1903 counter = (int)(severity->valuedouble);
1904 netconf_counter->minor = (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1906 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-major");
1907 if (!cJSON_IsNumber(severity))
1909 printf("Status JSON is not as expected: severity-major is not a string.");
1910 cJSON_Delete(jsonStatus);
1911 return SR_ERR_OPERATION_FAILED;
1913 counter = (int)(severity->valuedouble);
1914 netconf_counter->major = (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1916 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-critical");
1917 if (!cJSON_IsNumber(severity))
1919 printf("Status JSON is not as expected: severity-critical is not a string.");
1920 cJSON_Delete(jsonStatus);
1921 return SR_ERR_OPERATION_FAILED;
1923 counter = (int)(severity->valuedouble);
1924 netconf_counter->critical = (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1927 cJSON_Delete(jsonStatus);
1932 int writeSkeletonConfigFile()
1934 cJSON *configObject = cJSON_CreateObject();
1935 if (configObject == NULL)
1937 printf("Could not create JSON object: configObject\n");
1938 return SR_ERR_OPERATION_FAILED;
1941 cJSON *notificationConfig = cJSON_CreateObject();
1942 if (notificationConfig == NULL)
1944 printf("Could not create JSON object: notificationConfig\n");
1945 cJSON_Delete(configObject);
1946 return SR_ERR_OPERATION_FAILED;
1948 cJSON_AddItemToObject(configObject, "notification-config", notificationConfig);
1950 if (cJSON_AddNumberToObject(notificationConfig, "ves-heartbeat-period", 0) == NULL)
1952 printf("Could not create JSON object: ves-heartbeat-period\n");
1953 cJSON_Delete(configObject);
1954 return SR_ERR_OPERATION_FAILED;
1957 if (cJSON_AddTrueToObject(notificationConfig, "is-netconf-available") == NULL)
1959 printf("Could not create JSON object: is-netconf-available\n");
1960 cJSON_Delete(configObject);
1961 return SR_ERR_OPERATION_FAILED;
1964 if (cJSON_AddTrueToObject(notificationConfig, "is-ves-available") == NULL)
1966 printf("Could not create JSON object: is-ves-available\n");
1967 cJSON_Delete(configObject);
1968 return SR_ERR_OPERATION_FAILED;
1971 cJSON *faultNotificationDelayPeriod = cJSON_CreateArray();
1972 if (faultNotificationDelayPeriod == NULL)
1974 printf("Could not create JSON object: faultNotificationDelayPeriod\n");
1975 cJSON_Delete(configObject);
1976 return SR_ERR_OPERATION_FAILED;
1978 cJSON_AddItemToObject(notificationConfig, "fault-notification-delay-period", faultNotificationDelayPeriod);
1980 cJSON *arrayItem = cJSON_CreateNumber(0);
1981 if (arrayItem == NULL)
1983 printf("Could not create JSON object: arrayItem\n");
1984 cJSON_Delete(configObject);
1985 return SR_ERR_OPERATION_FAILED;
1987 cJSON_AddItemToArray(faultNotificationDelayPeriod, arrayItem);
1989 cJSON *vesEndPointDetails = cJSON_CreateObject();
1990 if (vesEndPointDetails == NULL)
1992 printf("Could not create JSON object: vesEndPointDetails\n");
1993 cJSON_Delete(configObject);
1994 return SR_ERR_OPERATION_FAILED;
1996 cJSON_AddItemToObject(configObject, "ves-endpoint-details", vesEndPointDetails);
1998 if (cJSON_AddStringToObject(vesEndPointDetails, "ves-endpoint-ip", "172.17.0.1") == NULL)
2000 printf("Could not create JSON object: ves-endpoint-ip\n");
2001 cJSON_Delete(configObject);
2002 return SR_ERR_OPERATION_FAILED;
2005 if (cJSON_AddNumberToObject(vesEndPointDetails, "ves-endpoint-port", 30007) == NULL)
2007 printf("Could not create JSON object: ves-endpoint-port\n");
2008 cJSON_Delete(configObject);
2009 return SR_ERR_OPERATION_FAILED;
2012 if (cJSON_AddStringToObject(vesEndPointDetails, "ves-endpoint-auth-method", "no-auth") == NULL)
2014 printf("Could not create JSON object: ves-endpoint-auth-method\n");
2015 cJSON_Delete(configObject);
2016 return SR_ERR_OPERATION_FAILED;
2019 if (cJSON_AddStringToObject(vesEndPointDetails, "ves-endpoint-username", "") == NULL)
2021 printf("Could not create JSON object: ves-endpoint-username\n");
2022 cJSON_Delete(configObject);
2023 return SR_ERR_OPERATION_FAILED;
2026 if (cJSON_AddStringToObject(vesEndPointDetails, "ves-endpoint-password", "") == NULL)
2028 printf("Could not create JSON object: ves-endpoint-password\n");
2029 cJSON_Delete(configObject);
2030 return SR_ERR_OPERATION_FAILED;
2033 if (cJSON_AddStringToObject(vesEndPointDetails, "ves-endpoint-certificate", "") == NULL)
2035 printf("Could not create JSON object: ves-endpoint-certificate\n");
2036 cJSON_Delete(configObject);
2037 return SR_ERR_OPERATION_FAILED;
2040 if (cJSON_AddFalseToObject(vesEndPointDetails, "ves-registration") == NULL)
2042 printf("Could not create JSON object: ves-registration\n");
2043 cJSON_Delete(configObject);
2044 return SR_ERR_OPERATION_FAILED;
2047 if (cJSON_AddNumberToObject(configObject, "ssh-connections", 1) == NULL)
2049 printf("Could not create JSON object: ssh-connections\n");
2050 cJSON_Delete(configObject);
2051 return SR_ERR_OPERATION_FAILED;
2054 if (cJSON_AddNumberToObject(configObject, "tls-connections", 0) == NULL)
2056 printf("Could not create JSON object: tls-connections\n");
2057 cJSON_Delete(configObject);
2058 return SR_ERR_OPERATION_FAILED;
2061 char *config_string = NULL;
2063 config_string = cJSON_PrintUnformatted(configObject);
2065 writeConfigFile(config_string);
2067 if (config_string != NULL)
2069 free(config_string);
2070 config_string = NULL;
2073 if (configObject != NULL)
2075 cJSON_Delete(configObject);
2081 int getIntFromString(char *string, int def_value)
2083 int rc, value = def_value;
2086 rc = sscanf(string, "%d", &value);
2089 printf("Could not get the %s! Using the default 0...\n", string);
2096 int getSshConnectionsFromConfigJson(void)
2098 char *stringConfig = readConfigFileInString();
2100 if (stringConfig == NULL)
2102 printf("Could not read JSON configuration file in string.");
2106 cJSON *jsonConfig = cJSON_Parse(stringConfig);
2107 if (jsonConfig == NULL)
2110 const char *error_ptr = cJSON_GetErrorPtr();
2111 if (error_ptr != NULL)
2113 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
2115 return SR_ERR_OPERATION_FAILED;
2117 //we don't need the string anymore
2119 stringConfig = NULL;
2121 cJSON *sshConnections = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ssh-connections");
2122 if (!cJSON_IsNumber(sshConnections))
2124 printf("Configuration JSON is not as expected: ssh-connections is not an object");
2125 cJSON_Delete(jsonConfig);
2126 return SR_ERR_OPERATION_FAILED;
2129 int num_of_ssh = (int)(sshConnections->valuedouble);
2131 cJSON_Delete(jsonConfig);
2136 int getTlsConnectionsFromConfigJson(void)
2138 char *stringConfig = readConfigFileInString();
2140 if (stringConfig == NULL)
2142 printf("Could not read JSON configuration file in string.");
2146 cJSON *jsonConfig = cJSON_Parse(stringConfig);
2147 if (jsonConfig == NULL)
2150 const char *error_ptr = cJSON_GetErrorPtr();
2151 if (error_ptr != NULL)
2153 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
2155 return SR_ERR_OPERATION_FAILED;
2157 //we don't need the string anymore
2159 stringConfig = NULL;
2161 cJSON *tlsConnections = cJSON_GetObjectItemCaseSensitive(jsonConfig, "tls-connections");
2162 if (!cJSON_IsNumber(tlsConnections))
2164 printf("Configuration JSON is not as expected: ssh-connections is not an object");
2165 cJSON_Delete(jsonConfig);
2166 return SR_ERR_OPERATION_FAILED;
2169 int num_of_tls = (int)(tlsConnections->valuedouble);
2171 cJSON_Delete(jsonConfig);