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();
458 int notificationDelay = 0;
460 if (stringConfig == NULL)
462 printf("Could not read JSON configuration file in string.");
466 cJSON *jsonConfig = cJSON_Parse(stringConfig);
467 if (jsonConfig == NULL)
470 const char *error_ptr = cJSON_GetErrorPtr();
471 if (error_ptr != NULL)
473 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
475 return SR_ERR_OPERATION_FAILED;
477 //we don't need the string anymore
481 cJSON *notifConfig = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
482 if (!cJSON_IsObject(notifConfig))
484 printf("Configuration JSON is not as expected: notification-config is not an object");
485 cJSON_Delete(jsonConfig);
486 return SR_ERR_OPERATION_FAILED;
489 cJSON *faultNotifDelay = cJSON_GetObjectItemCaseSensitive(notifConfig, "fault-notification-delay-period");
490 if (!cJSON_IsArray(faultNotifDelay))
492 printf("Configuration JSON is not as expected: fault-notification-delay-period is not an array.");
493 cJSON_Delete(jsonConfig);
494 return SR_ERR_OPERATION_FAILED;
497 cJSON *iterator = NULL;
501 cJSON_ArrayForEach(iterator, faultNotifDelay)
503 if (cJSON_IsNumber(iterator))
505 period_array[i++] = iterator->valueint;
509 printf("Invalid number in array!");
515 cJSON_Delete(jsonConfig);
520 int getVesHeartbeatPeriodFromConfigJson(void)
522 char *stringConfig = readConfigFileInString();
523 int vesHeartbeat = 0;
525 if (stringConfig == NULL)
527 printf("Could not read JSON configuration file in string.");
531 cJSON *jsonConfig = cJSON_Parse(stringConfig);
532 if (jsonConfig == NULL)
535 const char *error_ptr = cJSON_GetErrorPtr();
536 if (error_ptr != NULL)
538 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
540 return SR_ERR_OPERATION_FAILED;
542 //we don't need the string anymore
546 cJSON *notifConfig = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
547 if (!cJSON_IsObject(notifConfig))
549 printf("Configuration JSON is not as expected: notification-config is not an object");
550 cJSON_Delete(jsonConfig);
551 return SR_ERR_OPERATION_FAILED;
554 cJSON *vesHeartbeatPeriod = cJSON_GetObjectItemCaseSensitive(notifConfig, "ves-heartbeat-period");
555 if (!cJSON_IsNumber(vesHeartbeatPeriod))
557 printf("Configuration JSON is not as expected: ves-heartbeat-period is not a number");
558 cJSON_Delete(jsonConfig);
559 return SR_ERR_OPERATION_FAILED;
562 vesHeartbeat = (int)(vesHeartbeatPeriod->valuedouble);
564 cJSON_Delete(jsonConfig);
571 * Dynamically allocated memory;
572 * Caller needs to free the memory after it uses the value.
575 char* getVesAuthMethodFromConfigJson(void)
577 char *stringConfig = readConfigFileInString();
579 if (stringConfig == NULL)
581 printf("Could not read JSON configuration file in string.");
585 cJSON *jsonConfig = cJSON_Parse(stringConfig);
586 if (jsonConfig == NULL)
589 const char *error_ptr = cJSON_GetErrorPtr();
590 if (error_ptr != NULL)
592 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
596 //we don't need the string anymore
600 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
601 if (!cJSON_IsObject(vesDetails))
603 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
604 cJSON_Delete(jsonConfig);
608 cJSON *vesAuthMethod = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-auth-method");
609 if (!cJSON_IsString(vesAuthMethod))
611 printf("Configuration JSON is not as expected: ves-endpoint-auth-method is not an object");
612 cJSON_Delete(jsonConfig);
616 char *auth_method_string = strdup(cJSON_GetStringValue(vesAuthMethod));
618 cJSON_Delete(jsonConfig);
620 return auth_method_string;
624 * Dynamically allocated memory;
625 * Caller needs to free the memory after it uses the value.
628 char* getVesIpFromConfigJson(void)
630 char *stringConfig = readConfigFileInString();
632 if (stringConfig == NULL)
634 printf("Could not read JSON configuration file in string.");
638 cJSON *jsonConfig = cJSON_Parse(stringConfig);
639 if (jsonConfig == NULL)
642 const char *error_ptr = cJSON_GetErrorPtr();
643 if (error_ptr != NULL)
645 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
649 //we don't need the string anymore
653 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
654 if (!cJSON_IsObject(vesDetails))
656 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
657 cJSON_Delete(jsonConfig);
661 cJSON *vesIp = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-ip");
662 if (!cJSON_IsString(vesIp))
664 printf("Configuration JSON is not as expected: ves-endpoint-ip is not an object");
665 cJSON_Delete(jsonConfig);
669 char *ves_ip = strdup(cJSON_GetStringValue(vesIp));
671 cJSON_Delete(jsonConfig);
676 int getVesPortFromConfigJson(void)
678 char *stringConfig = readConfigFileInString();
680 if (stringConfig == NULL)
682 printf("Could not read JSON configuration file in string.");
686 cJSON *jsonConfig = cJSON_Parse(stringConfig);
687 if (jsonConfig == NULL)
690 const char *error_ptr = cJSON_GetErrorPtr();
691 if (error_ptr != NULL)
693 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
695 return SR_ERR_OPERATION_FAILED;
697 //we don't need the string anymore
701 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
702 if (!cJSON_IsObject(vesDetails))
704 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
705 cJSON_Delete(jsonConfig);
706 return SR_ERR_OPERATION_FAILED;
709 cJSON *vesPort = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-port");
710 if (!cJSON_IsNumber(vesPort))
712 printf("Configuration JSON is not as expected: ves-endpoint-port is not an object");
713 cJSON_Delete(jsonConfig);
714 return SR_ERR_OPERATION_FAILED;
717 int port = (int)(vesPort->valuedouble);
719 cJSON_Delete(jsonConfig);
724 int getVesRegistrationFromConfigJson(void)
726 char *stringConfig = readConfigFileInString();
728 if (stringConfig == NULL)
730 printf("Could not read JSON configuration file in string.");
734 cJSON *jsonConfig = cJSON_Parse(stringConfig);
735 if (jsonConfig == NULL)
738 const char *error_ptr = cJSON_GetErrorPtr();
739 if (error_ptr != NULL)
741 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
743 return SR_ERR_OPERATION_FAILED;
745 //we don't need the string anymore
749 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
750 if (!cJSON_IsObject(vesDetails))
752 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
753 cJSON_Delete(jsonConfig);
754 return SR_ERR_OPERATION_FAILED;
757 cJSON *vesReg = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-registration");
758 if (!cJSON_IsBool(vesReg))
760 printf("Configuration JSON is not as expected: ves-registration is not an object");
761 cJSON_Delete(jsonConfig);
762 return SR_ERR_OPERATION_FAILED;
765 int is_ves_reg = (cJSON_IsTrue(vesReg)) ? TRUE : FALSE;
767 cJSON_Delete(jsonConfig);
773 * Dynamically allocated memory;
774 * Caller needs to free the memory after it uses the value.
777 cJSON* vesCreatePnfRegistrationFields(int port, bool is_tls)
779 cJSON *pnfRegistrationFields = cJSON_CreateObject();
780 if (pnfRegistrationFields == NULL)
782 printf("Could not create JSON object: pnfRegistrationFields\n");
786 if (cJSON_AddStringToObject(pnfRegistrationFields, "pnfRegistrationFieldsVersion", "2.0") == NULL)
788 printf("Could not create JSON object: pnfRegistrationFieldsVersion\n");
789 cJSON_Delete(pnfRegistrationFields);
793 if (cJSON_AddStringToObject(pnfRegistrationFields, "lastServiceDate", "2019-08-16") == NULL)
795 printf("Could not create JSON object: lastServiceDate\n");
796 cJSON_Delete(pnfRegistrationFields);
801 generateRandomMacAddress(mac_addr);
803 if (cJSON_AddStringToObject(pnfRegistrationFields, "macAddress", mac_addr) == NULL)
805 printf("Could not create JSON object: macAddress\n");
806 cJSON_Delete(pnfRegistrationFields);
810 if (cJSON_AddStringToObject(pnfRegistrationFields, "manufactureDate", "2019-08-16") == NULL)
812 printf("Could not create JSON object: manufactureDate\n");
813 cJSON_Delete(pnfRegistrationFields);
817 if (cJSON_AddStringToObject(pnfRegistrationFields, "modelNumber", "Simulated Device Melacon") == NULL)
819 printf("Could not create JSON object: manufactureDate\n");
820 cJSON_Delete(pnfRegistrationFields);
824 if (cJSON_AddStringToObject(pnfRegistrationFields, "oamV4IpAddress", getenv("NTS_IP")) == NULL)
826 printf("Could not create JSON object: oamV4IpAddress\n");
827 cJSON_Delete(pnfRegistrationFields);
831 if (cJSON_AddStringToObject(pnfRegistrationFields, "oamV6IpAddress", "0:0:0:0:0:ffff:a0a:011") == NULL)
833 printf("Could not create JSON object: oamV6IpAddress\n");
834 cJSON_Delete(pnfRegistrationFields);
838 char serial_number[100];
839 sprintf(serial_number, "%s-%s-%d-Simulated Device Melacon", getenv("HOSTNAME"), getenv("NTS_IP"), port);
841 if (cJSON_AddStringToObject(pnfRegistrationFields, "serialNumber", serial_number) == NULL)
843 printf("Could not create JSON object: serialNumber\n");
844 cJSON_Delete(pnfRegistrationFields);
848 if (cJSON_AddStringToObject(pnfRegistrationFields, "softwareVersion", "2.3.5") == NULL)
850 printf("Could not create JSON object: softwareVersion\n");
851 cJSON_Delete(pnfRegistrationFields);
855 if (cJSON_AddStringToObject(pnfRegistrationFields, "unitFamily", "Simulated Device") == NULL)
857 printf("Could not create JSON object: unitFamily\n");
858 cJSON_Delete(pnfRegistrationFields);
862 if (cJSON_AddStringToObject(pnfRegistrationFields, "unitType", "O-RAN-sim") == NULL)
864 printf("Could not create JSON object: unitType\n");
865 cJSON_Delete(pnfRegistrationFields);
869 if (cJSON_AddStringToObject(pnfRegistrationFields, "vendorName", "Melacon") == NULL)
871 printf("Could not create JSON object: vendorName\n");
872 cJSON_Delete(pnfRegistrationFields);
876 cJSON *additionalFields = cJSON_CreateObject();
877 if (additionalFields == NULL)
879 printf("Could not create JSON object: additionalFields\n");
880 cJSON_Delete(pnfRegistrationFields);
883 cJSON_AddItemToObject(pnfRegistrationFields, "additionalFields", additionalFields);
886 sprintf(portString, "%d", port);
888 if (cJSON_AddStringToObject(additionalFields, "oamPort", portString) == NULL)
890 printf("Could not create JSON object: oamPort\n");
891 cJSON_Delete(pnfRegistrationFields);
897 //TLS specific configuration
898 if (cJSON_AddStringToObject(additionalFields, "protocol", "TLS") == NULL)
900 printf("Could not create JSON object: protocol\n");
901 cJSON_Delete(pnfRegistrationFields);
905 //TODO here we have the username from the docker container hardcoded: netconf
906 if (cJSON_AddStringToObject(additionalFields, "username", "netconf") == NULL)
908 printf("Could not create JSON object: username\n");
909 cJSON_Delete(pnfRegistrationFields);
913 if (cJSON_AddStringToObject(additionalFields, "keyId", "device-key") == NULL)
915 printf("Could not create JSON object: keyId\n");
916 cJSON_Delete(pnfRegistrationFields);
922 //SSH specific configuration
923 if (cJSON_AddStringToObject(additionalFields, "protocol", "SSH") == NULL)
925 printf("Could not create JSON object: protocol\n");
926 cJSON_Delete(pnfRegistrationFields);
930 //TODO here we have the username from the docker container hardcoded: netconf
931 if (cJSON_AddStringToObject(additionalFields, "username", "netconf") == NULL)
933 printf("Could not create JSON object: username\n");
934 cJSON_Delete(pnfRegistrationFields);
938 //TODO here we have the password from the docker container hardcoded: netconf
939 if (cJSON_AddStringToObject(additionalFields, "password", "netconf") == NULL)
941 printf("Could not create JSON object: password\n");
942 cJSON_Delete(pnfRegistrationFields);
947 if (cJSON_AddStringToObject(additionalFields, "reconnectOnChangedSchema", "false") == NULL)
949 printf("Could not create JSON object: reconnectOnChangedSchema\n");
950 cJSON_Delete(pnfRegistrationFields);
954 if (cJSON_AddStringToObject(additionalFields, "sleep-factor", "1.5") == NULL)
956 printf("Could not create JSON object: sleep-factor\n");
957 cJSON_Delete(pnfRegistrationFields);
961 if (cJSON_AddStringToObject(additionalFields, "tcpOnly", "false") == NULL)
963 printf("Could not create JSON object: tcpOnly\n");
964 cJSON_Delete(pnfRegistrationFields);
968 if (cJSON_AddStringToObject(additionalFields, "connectionTimeout", "20000") == NULL)
970 printf("Could not create JSON object: connectionTimeout\n");
971 cJSON_Delete(pnfRegistrationFields);
975 if (cJSON_AddStringToObject(additionalFields, "maxConnectionAttempts", "100") == NULL)
977 printf("Could not create JSON object: maxConnectionAttempts\n");
978 cJSON_Delete(pnfRegistrationFields);
982 if (cJSON_AddStringToObject(additionalFields, "betweenAttemptsTimeout", "2000") == NULL)
984 printf("Could not create JSON object: betweenAttemptsTimeout\n");
985 cJSON_Delete(pnfRegistrationFields);
989 if (cJSON_AddStringToObject(additionalFields, "keepaliveDelay", "120") == NULL)
991 printf("Could not create JSON object: keepaliveDelay\n");
992 cJSON_Delete(pnfRegistrationFields);
996 return pnfRegistrationFields;
999 int getNetconfAvailableFromConfigJson(void)
1001 char *stringConfig = readConfigFileInString();
1003 if (stringConfig == NULL)
1005 printf("Could not read JSON configuration file in string.");
1009 cJSON *jsonConfig = cJSON_Parse(stringConfig);
1010 if (jsonConfig == NULL)
1013 const char *error_ptr = cJSON_GetErrorPtr();
1014 if (error_ptr != NULL)
1016 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
1018 return SR_ERR_OPERATION_FAILED;
1020 //we don't need the string anymore
1022 stringConfig = NULL;
1024 cJSON *notifDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
1025 if (!cJSON_IsObject(notifDetails))
1027 printf("Configuration JSON is not as expected: notification-config is not an object");
1028 cJSON_Delete(jsonConfig);
1029 return SR_ERR_OPERATION_FAILED;
1032 cJSON *isNetconfAvailable = cJSON_GetObjectItemCaseSensitive(notifDetails, "is-netconf-available");
1033 if (!cJSON_IsBool(isNetconfAvailable))
1035 printf("Configuration JSON is not as expected: is-netconf-available is not an object");
1036 cJSON_Delete(jsonConfig);
1037 return SR_ERR_OPERATION_FAILED;
1040 int is_netconf_available = (cJSON_IsTrue(isNetconfAvailable)) ? TRUE : FALSE;
1042 cJSON_Delete(jsonConfig);
1044 return is_netconf_available;
1047 int getVesAvailableFromConfigJson(void)
1049 char *stringConfig = readConfigFileInString();
1051 if (stringConfig == NULL)
1053 printf("Could not read JSON configuration file in string.");
1057 cJSON *jsonConfig = cJSON_Parse(stringConfig);
1058 if (jsonConfig == NULL)
1061 const char *error_ptr = cJSON_GetErrorPtr();
1062 if (error_ptr != NULL)
1064 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
1066 return SR_ERR_OPERATION_FAILED;
1068 //we don't need the string anymore
1070 stringConfig = NULL;
1072 cJSON *notifDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
1073 if (!cJSON_IsObject(notifDetails))
1075 printf("Configuration JSON is not as expected: notification-config is not an object");
1076 cJSON_Delete(jsonConfig);
1077 return SR_ERR_OPERATION_FAILED;
1080 cJSON *isVesAvailable = cJSON_GetObjectItemCaseSensitive(notifDetails, "is-ves-available");
1081 if (!cJSON_IsBool(isVesAvailable))
1083 printf("Configuration JSON is not as expected: is-ves-available is not an object");
1084 cJSON_Delete(jsonConfig);
1085 return SR_ERR_OPERATION_FAILED;
1088 int is_netconf_available = (cJSON_IsTrue(isVesAvailable)) ? TRUE : FALSE;
1090 cJSON_Delete(jsonConfig);
1092 return is_netconf_available;
1096 * Dynamically allocated memory;
1097 * Caller needs to free the memory after it uses the value.
1100 cJSON* vesCreateFaultFields(char *alarm_condition, char *alarm_object, char *severity, char *date_time, char *specific_problem)
1102 cJSON *faultFields = cJSON_CreateObject();
1103 if (faultFields == NULL)
1105 printf("Could not create JSON object: faultFields\n");
1109 if (cJSON_AddStringToObject(faultFields, "faultFieldsVersion", "4.0") == NULL)
1111 printf("Could not create JSON object: faultFieldsVersion\n");
1112 cJSON_Delete(faultFields);
1116 if (cJSON_AddStringToObject(faultFields, "alarmCondition", alarm_condition) == NULL)
1118 printf("Could not create JSON object: alarmCondition\n");
1119 cJSON_Delete(faultFields);
1123 if (cJSON_AddStringToObject(faultFields, "alarmInterfaceA", alarm_object) == NULL)
1125 printf("Could not create JSON object: alarmInterfaceA\n");
1126 cJSON_Delete(faultFields);
1130 if (cJSON_AddStringToObject(faultFields, "eventSourceType", "O_RAN_COMPONENT") == NULL)
1132 printf("Could not create JSON object: eventSourceType\n");
1133 cJSON_Delete(faultFields);
1137 if (cJSON_AddStringToObject(faultFields, "specificProblem", specific_problem) == NULL)
1139 printf("Could not create JSON object: specificProblem\n");
1140 cJSON_Delete(faultFields);
1144 if (cJSON_AddStringToObject(faultFields, "eventSeverity", severity) == NULL)
1146 printf("Could not create JSON object: eventSeverity\n");
1147 cJSON_Delete(faultFields);
1151 if (cJSON_AddStringToObject(faultFields, "vfStatus", "Active") == NULL)
1153 printf("Could not create JSON object: vfStatus\n");
1154 cJSON_Delete(faultFields);
1158 cJSON *alarmAdditionalInformation = cJSON_CreateObject();
1159 if (alarmAdditionalInformation == NULL)
1161 printf("Could not create JSON object: alarmAdditionalInformation\n");
1162 cJSON_Delete(faultFields);
1165 cJSON_AddItemToObject(faultFields, "alarmAdditionalInformation", alarmAdditionalInformation);
1167 if (cJSON_AddStringToObject(alarmAdditionalInformation, "eventTime", date_time) == NULL)
1169 printf("Could not create JSON object: eventTime\n");
1170 cJSON_Delete(faultFields);
1174 if (cJSON_AddStringToObject(alarmAdditionalInformation, "equipType", "O-RAN-sim") == NULL)
1176 printf("Could not create JSON object: equipType\n");
1177 cJSON_Delete(faultFields);
1181 if (cJSON_AddStringToObject(alarmAdditionalInformation, "vendor", "Melacon") == NULL)
1183 printf("Could not create JSON object: vendor\n");
1184 cJSON_Delete(faultFields);
1188 if (cJSON_AddStringToObject(alarmAdditionalInformation, "model", "Simulated Device") == NULL)
1190 printf("Could not create JSON object: model\n");
1191 cJSON_Delete(faultFields);
1198 static cJSON* createSeverityCounters(counterAlarms count)
1200 cJSON *severityCounters = cJSON_CreateObject();
1201 if (severityCounters == NULL)
1203 printf("Could not create JSON object: severityCounters\n");
1207 if (cJSON_AddNumberToObject(severityCounters, "severity-normal", count.normal) == NULL)
1209 printf("Could not create JSON object: severity-normal\n");
1213 if (cJSON_AddNumberToObject(severityCounters, "severity-warning", count.warning) == NULL)
1215 printf("Could not create JSON object: severity-warning\n");
1219 if (cJSON_AddNumberToObject(severityCounters, "severity-minor", count.minor) == NULL)
1221 printf("Could not create JSON object: severity-minor\n");
1225 if (cJSON_AddNumberToObject(severityCounters, "severity-major", count.major) == NULL)
1227 printf("Could not create JSON object: severity-major\n");
1231 if (cJSON_AddNumberToObject(severityCounters, "severity-critical", count.critical) == NULL)
1233 printf("Could not create JSON object: severity-critical\n");
1237 return severityCounters;
1240 void writeStatusFile(char *status)
1242 char status_file[200];
1243 sprintf(status_file, "%s/status.json", getenv("SCRIPTS_DIR"));
1244 FILE * f = fopen (status_file, "w");
1253 printf("Could not write status file!\n");
1257 int writeSkeletonStatusFile()
1259 cJSON *statusObject = cJSON_CreateObject();
1260 if (statusObject == NULL)
1262 printf("Could not create JSON object: statusObject\n");
1263 return SR_ERR_OPERATION_FAILED;
1266 // counterAlarms counter = {
1274 // cJSON *totalVesNotifications = createSeverityCounters(counter);
1275 // if (totalVesNotifications == NULL)
1277 // printf("Could not create JSON object: totalVesNotifications\n");
1278 // cJSON_Delete(statusObject);
1279 // return SR_ERR_OPERATION_FAILED;
1281 // cJSON_AddItemToObject(statusObject, "total-ves-notifications-sent", totalVesNotifications);
1283 // cJSON *totalNetconfNotifications = createSeverityCounters(counter);
1284 // if (totalNetconfNotifications == NULL)
1286 // printf("Could not create JSON object: totalNetconfNotifications\n");
1287 // cJSON_Delete(statusObject);
1288 // return SR_ERR_OPERATION_FAILED;
1290 // cJSON_AddItemToObject(statusObject, "total-netconf-notifications-sent", totalNetconfNotifications);
1292 cJSON *deviceList = cJSON_CreateArray();
1293 if (deviceList == NULL)
1295 printf("Could not create JSON object: deviceList\n");
1296 cJSON_Delete(statusObject);
1297 return SR_ERR_OPERATION_FAILED;
1299 cJSON_AddItemToObject(statusObject, "device-list", deviceList);
1301 char *status_string = NULL;
1303 status_string = cJSON_PrintUnformatted(statusObject);
1305 writeStatusFile(status_string);
1311 * Dynamically allocated memory;
1312 * Caller needs to free the memory after it uses the value.
1315 char* readStatusFileInString(void)
1319 char config_file[200];
1320 sprintf(config_file, "%s/status.json", getenv("SCRIPTS_DIR"));
1321 FILE * f = fopen (config_file, "rb");
1325 fseek (f, 0, SEEK_END);
1327 fseek (f, 0, SEEK_SET);
1328 buffer = malloc (length + 1);
1331 fread (buffer, 1, length, f);
1334 buffer[length] = '\0';
1346 * Dynamically allocated memory;
1347 * Caller needs to free the memory after it uses the value.
1350 cJSON* getDeviceListFromStatusFile(void)
1352 char *stringStatus = readStatusFileInString();
1354 if (stringStatus == NULL)
1356 printf("Could not read status file!\n");
1360 cJSON *jsonStatus = cJSON_Parse(stringStatus);
1361 if (jsonStatus == NULL)
1364 const char *error_ptr = cJSON_GetErrorPtr();
1365 if (error_ptr != NULL)
1367 fprintf(stderr, "Could not parse JSON status! Error before: %s\n", error_ptr);
1371 //we don't need the string anymore
1373 stringStatus = NULL;
1378 cJSON* createDeviceListEntry(counterAlarms ves_counter, counterAlarms netconf_counter)
1380 cJSON *deviceListEntry = cJSON_CreateObject();
1381 if (deviceListEntry == NULL)
1383 printf("Could not create JSON object: deviceListEntry\n");
1388 sprintf(hostname, "%s", getenv("HOSTNAME"));
1390 if (cJSON_AddStringToObject(deviceListEntry, "device-name", hostname) == NULL)
1392 printf("Could not create JSON object: device-name\n");
1393 cJSON_Delete(deviceListEntry);
1397 cJSON *vesNotificationsSent = createSeverityCounters(ves_counter);
1398 if (vesNotificationsSent == NULL)
1400 printf("Could not create JSON object: vesNotificationsSent\n");
1401 cJSON_Delete(deviceListEntry);
1404 cJSON_AddItemToObject(deviceListEntry, "ves-notifications-sent", vesNotificationsSent);
1406 cJSON *netconfNotificationsSent = createSeverityCounters(netconf_counter);
1407 if (netconfNotificationsSent == NULL)
1409 printf("Could not create JSON object: netconfNotificationsSent\n");
1410 cJSON_Delete(deviceListEntry);
1413 cJSON_AddItemToObject(deviceListEntry, "netconf-notifications-sent", netconfNotificationsSent);
1415 return deviceListEntry;
1418 static void modifySeverityCounters(cJSON **severityCounters, counterAlarms count)
1420 cJSON *severity= cJSON_GetObjectItemCaseSensitive(*severityCounters, "severity-normal");
1421 if (!cJSON_IsNumber(severity))
1423 printf("Status JSON is not as expected: severity-normal is not an number");
1426 //we set the value of the severity-normal object
1427 cJSON_SetNumberValue(severity, count.normal);
1429 severity= cJSON_GetObjectItemCaseSensitive(*severityCounters, "severity-warning");
1430 if (!cJSON_IsNumber(severity))
1432 printf("Status JSON is not as expected: severity-warning is not an number");
1435 //we set the value of the severity-warning object
1436 cJSON_SetNumberValue(severity, count.warning);
1438 severity= cJSON_GetObjectItemCaseSensitive(*severityCounters, "severity-minor");
1439 if (!cJSON_IsNumber(severity))
1441 printf("Status JSON is not as expected: severity-minor is not an number");
1444 //we set the value of the severity-minor object
1445 cJSON_SetNumberValue(severity, count.minor);
1447 severity= cJSON_GetObjectItemCaseSensitive(*severityCounters, "severity-major");
1448 if (!cJSON_IsNumber(severity))
1450 printf("Status JSON is not as expected: severity-major is not an number");
1453 //we set the value of the severity-major object
1454 cJSON_SetNumberValue(severity, count.major);
1456 severity= cJSON_GetObjectItemCaseSensitive(*severityCounters, "severity-critical");
1457 if (!cJSON_IsNumber(severity))
1459 printf("Status JSON is not as expected: severity-critical is not an number");
1462 //we set the value of the severity-critical object
1463 cJSON_SetNumberValue(severity, count.critical);
1468 static void modifyDeviceListEntry(cJSON **deviceListEntry, counterAlarms ves_counter, counterAlarms netconf_counter)
1470 cJSON *vesNotificationsSent= cJSON_GetObjectItemCaseSensitive(*deviceListEntry, "ves-notifications-sent");
1471 if (!cJSON_IsObject(vesNotificationsSent))
1473 printf("Status JSON is not as expected: ves-notifications-sent is not a object");
1477 modifySeverityCounters(&vesNotificationsSent, ves_counter);
1479 cJSON *netconfNotificationsSent= cJSON_GetObjectItemCaseSensitive(*deviceListEntry, "netconf-notifications-sent");
1480 if (!cJSON_IsObject(netconfNotificationsSent))
1482 printf("Status JSON is not as expected: netconf-notifications-sent is not a object");
1486 modifySeverityCounters(&netconfNotificationsSent, netconf_counter);
1489 int writeStatusNotificationCounters(counterAlarms ves_counter, counterAlarms netconf_counter)
1491 cJSON *jsonStatus = getDeviceListFromStatusFile();
1493 cJSON *deviceList = cJSON_GetObjectItemCaseSensitive(jsonStatus, "device-list");
1494 if (!cJSON_IsArray(deviceList))
1496 printf("Status JSON is not as expected: device-list is not an object");
1497 cJSON_Delete(jsonStatus);
1498 return SR_ERR_OPERATION_FAILED;
1501 int array_size = cJSON_GetArraySize(deviceList);
1504 for (int i=0; i<array_size; ++i)
1506 cJSON *deviceListEntry = cJSON_GetArrayItem(deviceList, i);
1508 sprintf(hostname, "%s", getenv("HOSTNAME"));
1510 cJSON *deviceName = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "device-name");
1511 if (!cJSON_IsString(deviceName))
1513 printf("Status JSON is not as expected: device-name is not a string.");
1514 cJSON_Delete(jsonStatus);
1515 return SR_ERR_OPERATION_FAILED;
1517 char *deviceNameString = cJSON_GetStringValue(deviceName);
1519 if (strcmp(hostname, deviceNameString) == 0)
1521 modifyDeviceListEntry(&deviceListEntry, ves_counter, netconf_counter);
1528 cJSON* deviceListEntry = createDeviceListEntry(ves_counter, netconf_counter);
1530 cJSON_AddItemToArray(deviceList, deviceListEntry);
1533 //writing the new JSON to the configuration file
1534 char *stringStatus = cJSON_PrintUnformatted(jsonStatus);
1535 writeStatusFile(stringStatus);
1537 cJSON_Delete(jsonStatus);
1543 int removeDeviceEntryFromStatusFile(char *containerId)
1545 cJSON *jsonStatus = getDeviceListFromStatusFile();
1547 cJSON *deviceList = cJSON_GetObjectItemCaseSensitive(jsonStatus, "device-list");
1548 if (!cJSON_IsArray(deviceList))
1550 printf("Status JSON is not as expected: device-list is not an object");
1551 cJSON_Delete(jsonStatus);
1552 return SR_ERR_OPERATION_FAILED;
1555 int array_size = cJSON_GetArraySize(deviceList);
1556 int found = array_size;
1558 for (int i=0; i<array_size; ++i)
1560 cJSON *deviceListEntry = cJSON_GetArrayItem(deviceList, i);
1562 sprintf(hostname, "%s", getenv("HOSTNAME"));
1564 cJSON *deviceName = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "device-name");
1565 if (!cJSON_IsString(deviceName))
1567 printf("Status JSON is not as expected: device-name is not a string.");
1568 cJSON_Delete(jsonStatus);
1569 return SR_ERR_OPERATION_FAILED;
1571 char *deviceNameString = cJSON_GetStringValue(deviceName);
1573 if (strcmp(containerId, deviceNameString) == 0)
1580 if (found < array_size)
1582 cJSON_DeleteItemFromArray(deviceList, found);
1586 printf("Could not find status file entry for device with id=\"%s\"", containerId);
1589 //writing the new JSON to the configuration file
1590 char *stringStatus = cJSON_PrintUnformatted(jsonStatus);
1591 writeStatusFile(stringStatus);
1593 cJSON_Delete(jsonStatus);
1598 int compute_notifications_count(counterAlarms *ves_counter, counterAlarms *netconf_counter)
1600 ves_counter->normal = ves_counter->warning = \
1601 ves_counter->minor = ves_counter->major = \
1602 ves_counter->critical = 0;
1603 netconf_counter->normal = netconf_counter->warning = \
1604 netconf_counter->minor = netconf_counter->major = \
1605 netconf_counter->critical = 0;
1607 cJSON *jsonStatus = getDeviceListFromStatusFile();
1609 cJSON *deviceList = cJSON_GetObjectItemCaseSensitive(jsonStatus, "device-list");
1610 if (!cJSON_IsArray(deviceList))
1612 printf("Status JSON is not as expected: device-list is not an object");
1613 cJSON_Delete(jsonStatus);
1614 return SR_ERR_OPERATION_FAILED;
1617 int array_size = cJSON_GetArraySize(deviceList);
1619 for (int i=0; i<array_size; ++i)
1621 cJSON *deviceListEntry = cJSON_GetArrayItem(deviceList, i);
1623 cJSON *vesNotifications = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "ves-notifications-sent");
1624 if (!cJSON_IsObject(vesNotifications))
1626 printf("Status JSON is not as expected: ves-notifications-sent is not an object.");
1627 cJSON_Delete(jsonStatus);
1628 return SR_ERR_OPERATION_FAILED;
1631 cJSON *severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-normal");
1632 if (!cJSON_IsNumber(severity))
1634 printf("Status JSON is not as expected: severity-normal is not a string.");
1635 cJSON_Delete(jsonStatus);
1636 return SR_ERR_OPERATION_FAILED;
1638 int counter = (int)(severity->valuedouble);
1639 ves_counter->normal += counter;
1641 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-warning");
1642 if (!cJSON_IsNumber(severity))
1644 printf("Status JSON is not as expected: severity-warning is not a string.");
1645 cJSON_Delete(jsonStatus);
1646 return SR_ERR_OPERATION_FAILED;
1648 counter = (int)(severity->valuedouble);
1649 ves_counter->warning += counter;
1651 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-minor");
1652 if (!cJSON_IsNumber(severity))
1654 printf("Status JSON is not as expected: severity-minor is not a string.");
1655 cJSON_Delete(jsonStatus);
1656 return SR_ERR_OPERATION_FAILED;
1658 counter = (int)(severity->valuedouble);
1659 ves_counter->minor += counter;
1661 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-major");
1662 if (!cJSON_IsNumber(severity))
1664 printf("Status JSON is not as expected: severity-major is not a string.");
1665 cJSON_Delete(jsonStatus);
1666 return SR_ERR_OPERATION_FAILED;
1668 counter = (int)(severity->valuedouble);
1669 ves_counter->major += counter;
1671 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-critical");
1672 if (!cJSON_IsNumber(severity))
1674 printf("Status JSON is not as expected: severity-critical is not a string.");
1675 cJSON_Delete(jsonStatus);
1676 return SR_ERR_OPERATION_FAILED;
1678 counter = (int)(severity->valuedouble);
1679 ves_counter->critical += counter;
1681 cJSON *netconfNotifications = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "netconf-notifications-sent");
1682 if (!cJSON_IsObject(netconfNotifications))
1684 printf("Status JSON is not as expected: netconf-notifications-sent is not an object.");
1685 cJSON_Delete(jsonStatus);
1686 return SR_ERR_OPERATION_FAILED;
1689 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-normal");
1690 if (!cJSON_IsNumber(severity))
1692 printf("Status JSON is not as expected: severity-normal is not a string.");
1693 cJSON_Delete(jsonStatus);
1694 return SR_ERR_OPERATION_FAILED;
1697 counter = (int)(severity->valuedouble);
1698 netconf_counter->normal += (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1700 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-warning");
1701 if (!cJSON_IsNumber(severity))
1703 printf("Status JSON is not as expected: severity-warning is not a string.");
1704 cJSON_Delete(jsonStatus);
1705 return SR_ERR_OPERATION_FAILED;
1707 counter = (int)(severity->valuedouble);
1708 netconf_counter->warning += (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1710 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-minor");
1711 if (!cJSON_IsNumber(severity))
1713 printf("Status JSON is not as expected: severity-minor is not a string.");
1714 cJSON_Delete(jsonStatus);
1715 return SR_ERR_OPERATION_FAILED;
1717 counter = (int)(severity->valuedouble);
1718 netconf_counter->minor += (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1720 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-major");
1721 if (!cJSON_IsNumber(severity))
1723 printf("Status JSON is not as expected: severity-major is not a string.");
1724 cJSON_Delete(jsonStatus);
1725 return SR_ERR_OPERATION_FAILED;
1727 counter = (int)(severity->valuedouble);
1728 netconf_counter->major += (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1730 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-critical");
1731 if (!cJSON_IsNumber(severity))
1733 printf("Status JSON is not as expected: severity-critical is not a string.");
1734 cJSON_Delete(jsonStatus);
1735 return SR_ERR_OPERATION_FAILED;
1737 counter = (int)(severity->valuedouble);
1738 netconf_counter->critical += (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1741 cJSON_Delete(jsonStatus);
1746 int getDeviceCounters(char *containerId, counterAlarms *ves_counter, counterAlarms *netconf_counter)
1748 cJSON *jsonStatus = getDeviceListFromStatusFile();
1750 cJSON *deviceList = cJSON_GetObjectItemCaseSensitive(jsonStatus, "device-list");
1751 if (!cJSON_IsArray(deviceList))
1753 printf("Status JSON is not as expected: device-list is not an object");
1754 cJSON_Delete(jsonStatus);
1755 return SR_ERR_OPERATION_FAILED;
1758 int array_size = cJSON_GetArraySize(deviceList);
1760 for (int i=0; i<array_size; ++i)
1762 cJSON *deviceListEntry = cJSON_GetArrayItem(deviceList, i);
1764 cJSON *deviceName = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "device-name");
1765 if (!cJSON_IsString(deviceName))
1767 printf("Status JSON is not as expected: device-name is not a string.");
1768 cJSON_Delete(jsonStatus);
1769 return SR_ERR_OPERATION_FAILED;
1771 char *deviceNameString = cJSON_GetStringValue(deviceName);
1773 if (strcmp(deviceNameString, containerId) != 0)
1778 cJSON *vesNotifications = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "ves-notifications-sent");
1779 if (!cJSON_IsObject(vesNotifications))
1781 printf("Status JSON is not as expected: ves-notifications-sent is not an object.");
1782 cJSON_Delete(jsonStatus);
1783 return SR_ERR_OPERATION_FAILED;
1786 cJSON *severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-normal");
1787 if (!cJSON_IsNumber(severity))
1789 printf("Status JSON is not as expected: severity-normal is not a string.");
1790 cJSON_Delete(jsonStatus);
1791 return SR_ERR_OPERATION_FAILED;
1793 int counter = (int)(severity->valuedouble);
1794 ves_counter->normal = counter;
1796 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-warning");
1797 if (!cJSON_IsNumber(severity))
1799 printf("Status JSON is not as expected: severity-warning is not a string.");
1800 cJSON_Delete(jsonStatus);
1801 return SR_ERR_OPERATION_FAILED;
1803 counter = (int)(severity->valuedouble);
1804 ves_counter->warning = counter;
1806 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-minor");
1807 if (!cJSON_IsNumber(severity))
1809 printf("Status JSON is not as expected: severity-minor is not a string.");
1810 cJSON_Delete(jsonStatus);
1811 return SR_ERR_OPERATION_FAILED;
1813 counter = (int)(severity->valuedouble);
1814 ves_counter->minor = counter;
1816 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-major");
1817 if (!cJSON_IsNumber(severity))
1819 printf("Status JSON is not as expected: severity-major is not a string.");
1820 cJSON_Delete(jsonStatus);
1821 return SR_ERR_OPERATION_FAILED;
1823 counter = (int)(severity->valuedouble);
1824 ves_counter->major = counter;
1826 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-critical");
1827 if (!cJSON_IsNumber(severity))
1829 printf("Status JSON is not as expected: severity-critical is not a string.");
1830 cJSON_Delete(jsonStatus);
1831 return SR_ERR_OPERATION_FAILED;
1833 counter = (int)(severity->valuedouble);
1834 ves_counter->critical = counter;
1836 cJSON *netconfNotifications = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "netconf-notifications-sent");
1837 if (!cJSON_IsObject(netconfNotifications))
1839 printf("Status JSON is not as expected: netconf-notifications-sent is not an object.");
1840 cJSON_Delete(jsonStatus);
1841 return SR_ERR_OPERATION_FAILED;
1844 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-normal");
1845 if (!cJSON_IsNumber(severity))
1847 printf("Status JSON is not as expected: severity-normal is not a string.");
1848 cJSON_Delete(jsonStatus);
1849 return SR_ERR_OPERATION_FAILED;
1852 counter = (int)(severity->valuedouble);
1853 netconf_counter->normal = (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1855 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-warning");
1856 if (!cJSON_IsNumber(severity))
1858 printf("Status JSON is not as expected: severity-warning is not a string.");
1859 cJSON_Delete(jsonStatus);
1860 return SR_ERR_OPERATION_FAILED;
1862 counter = (int)(severity->valuedouble);
1863 netconf_counter->warning = (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1865 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-minor");
1866 if (!cJSON_IsNumber(severity))
1868 printf("Status JSON is not as expected: severity-minor is not a string.");
1869 cJSON_Delete(jsonStatus);
1870 return SR_ERR_OPERATION_FAILED;
1872 counter = (int)(severity->valuedouble);
1873 netconf_counter->minor = (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1875 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-major");
1876 if (!cJSON_IsNumber(severity))
1878 printf("Status JSON is not as expected: severity-major is not a string.");
1879 cJSON_Delete(jsonStatus);
1880 return SR_ERR_OPERATION_FAILED;
1882 counter = (int)(severity->valuedouble);
1883 netconf_counter->major = (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1885 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-critical");
1886 if (!cJSON_IsNumber(severity))
1888 printf("Status JSON is not as expected: severity-critical is not a string.");
1889 cJSON_Delete(jsonStatus);
1890 return SR_ERR_OPERATION_FAILED;
1892 counter = (int)(severity->valuedouble);
1893 netconf_counter->critical = (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1896 cJSON_Delete(jsonStatus);