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(void)
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_IsNumber(faultNotifDelay))
492 printf("Configuration JSON is not as expected: fault-notification-delay-period is not a number");
493 cJSON_Delete(jsonConfig);
494 return SR_ERR_OPERATION_FAILED;
497 notificationDelay = (int)(faultNotifDelay->valuedouble);
499 cJSON_Delete(jsonConfig);
501 return notificationDelay;
504 int getVesHeartbeatPeriodFromConfigJson(void)
506 char *stringConfig = readConfigFileInString();
507 int vesHeartbeat = 0;
509 if (stringConfig == NULL)
511 printf("Could not read JSON configuration file in string.");
515 cJSON *jsonConfig = cJSON_Parse(stringConfig);
516 if (jsonConfig == NULL)
519 const char *error_ptr = cJSON_GetErrorPtr();
520 if (error_ptr != NULL)
522 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
524 return SR_ERR_OPERATION_FAILED;
526 //we don't need the string anymore
530 cJSON *notifConfig = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
531 if (!cJSON_IsObject(notifConfig))
533 printf("Configuration JSON is not as expected: notification-config is not an object");
534 cJSON_Delete(jsonConfig);
535 return SR_ERR_OPERATION_FAILED;
538 cJSON *vesHeartbeatPeriod = cJSON_GetObjectItemCaseSensitive(notifConfig, "ves-heartbeat-period");
539 if (!cJSON_IsNumber(vesHeartbeatPeriod))
541 printf("Configuration JSON is not as expected: ves-heartbeat-period is not a number");
542 cJSON_Delete(jsonConfig);
543 return SR_ERR_OPERATION_FAILED;
546 vesHeartbeat = (int)(vesHeartbeatPeriod->valuedouble);
548 cJSON_Delete(jsonConfig);
555 * Dynamically allocated memory;
556 * Caller needs to free the memory after it uses the value.
559 char* getVesAuthMethodFromConfigJson(void)
561 char *stringConfig = readConfigFileInString();
563 if (stringConfig == NULL)
565 printf("Could not read JSON configuration file in string.");
569 cJSON *jsonConfig = cJSON_Parse(stringConfig);
570 if (jsonConfig == NULL)
573 const char *error_ptr = cJSON_GetErrorPtr();
574 if (error_ptr != NULL)
576 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
580 //we don't need the string anymore
584 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
585 if (!cJSON_IsObject(vesDetails))
587 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
588 cJSON_Delete(jsonConfig);
592 cJSON *vesAuthMethod = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-auth-method");
593 if (!cJSON_IsString(vesAuthMethod))
595 printf("Configuration JSON is not as expected: ves-endpoint-auth-method is not an object");
596 cJSON_Delete(jsonConfig);
600 char *auth_method_string = strdup(cJSON_GetStringValue(vesAuthMethod));
602 cJSON_Delete(jsonConfig);
604 return auth_method_string;
608 * Dynamically allocated memory;
609 * Caller needs to free the memory after it uses the value.
612 char* getVesIpFromConfigJson(void)
614 char *stringConfig = readConfigFileInString();
616 if (stringConfig == NULL)
618 printf("Could not read JSON configuration file in string.");
622 cJSON *jsonConfig = cJSON_Parse(stringConfig);
623 if (jsonConfig == NULL)
626 const char *error_ptr = cJSON_GetErrorPtr();
627 if (error_ptr != NULL)
629 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
633 //we don't need the string anymore
637 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
638 if (!cJSON_IsObject(vesDetails))
640 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
641 cJSON_Delete(jsonConfig);
645 cJSON *vesIp = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-ip");
646 if (!cJSON_IsString(vesIp))
648 printf("Configuration JSON is not as expected: ves-endpoint-ip is not an object");
649 cJSON_Delete(jsonConfig);
653 char *ves_ip = strdup(cJSON_GetStringValue(vesIp));
655 cJSON_Delete(jsonConfig);
660 int getVesPortFromConfigJson(void)
662 char *stringConfig = readConfigFileInString();
664 if (stringConfig == NULL)
666 printf("Could not read JSON configuration file in string.");
670 cJSON *jsonConfig = cJSON_Parse(stringConfig);
671 if (jsonConfig == NULL)
674 const char *error_ptr = cJSON_GetErrorPtr();
675 if (error_ptr != NULL)
677 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
679 return SR_ERR_OPERATION_FAILED;
681 //we don't need the string anymore
685 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
686 if (!cJSON_IsObject(vesDetails))
688 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
689 cJSON_Delete(jsonConfig);
690 return SR_ERR_OPERATION_FAILED;
693 cJSON *vesPort = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-port");
694 if (!cJSON_IsNumber(vesPort))
696 printf("Configuration JSON is not as expected: ves-endpoint-port is not an object");
697 cJSON_Delete(jsonConfig);
698 return SR_ERR_OPERATION_FAILED;
701 int port = (int)(vesPort->valuedouble);
703 cJSON_Delete(jsonConfig);
708 int getVesRegistrationFromConfigJson(void)
710 char *stringConfig = readConfigFileInString();
712 if (stringConfig == NULL)
714 printf("Could not read JSON configuration file in string.");
718 cJSON *jsonConfig = cJSON_Parse(stringConfig);
719 if (jsonConfig == NULL)
722 const char *error_ptr = cJSON_GetErrorPtr();
723 if (error_ptr != NULL)
725 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
727 return SR_ERR_OPERATION_FAILED;
729 //we don't need the string anymore
733 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
734 if (!cJSON_IsObject(vesDetails))
736 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
737 cJSON_Delete(jsonConfig);
738 return SR_ERR_OPERATION_FAILED;
741 cJSON *vesReg = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-registration");
742 if (!cJSON_IsBool(vesReg))
744 printf("Configuration JSON is not as expected: ves-registration is not an object");
745 cJSON_Delete(jsonConfig);
746 return SR_ERR_OPERATION_FAILED;
749 int is_ves_reg = (cJSON_IsTrue(vesReg)) ? TRUE : FALSE;
751 cJSON_Delete(jsonConfig);
757 * Dynamically allocated memory;
758 * Caller needs to free the memory after it uses the value.
761 cJSON* vesCreatePnfRegistrationFields(int port, bool is_tls)
763 cJSON *pnfRegistrationFields = cJSON_CreateObject();
764 if (pnfRegistrationFields == NULL)
766 printf("Could not create JSON object: pnfRegistrationFields\n");
770 if (cJSON_AddStringToObject(pnfRegistrationFields, "pnfRegistrationFieldsVersion", "2.0") == NULL)
772 printf("Could not create JSON object: pnfRegistrationFieldsVersion\n");
773 cJSON_Delete(pnfRegistrationFields);
777 if (cJSON_AddStringToObject(pnfRegistrationFields, "lastServiceDate", "2019-08-16") == NULL)
779 printf("Could not create JSON object: lastServiceDate\n");
780 cJSON_Delete(pnfRegistrationFields);
785 generateRandomMacAddress(mac_addr);
787 if (cJSON_AddStringToObject(pnfRegistrationFields, "macAddress", mac_addr) == NULL)
789 printf("Could not create JSON object: macAddress\n");
790 cJSON_Delete(pnfRegistrationFields);
794 if (cJSON_AddStringToObject(pnfRegistrationFields, "manufactureDate", "2019-08-16") == NULL)
796 printf("Could not create JSON object: manufactureDate\n");
797 cJSON_Delete(pnfRegistrationFields);
801 if (cJSON_AddStringToObject(pnfRegistrationFields, "modelNumber", "Simulated Device Melacon") == NULL)
803 printf("Could not create JSON object: manufactureDate\n");
804 cJSON_Delete(pnfRegistrationFields);
808 if (cJSON_AddStringToObject(pnfRegistrationFields, "oamV4IpAddress", getenv("NTS_IP")) == NULL)
810 printf("Could not create JSON object: oamV4IpAddress\n");
811 cJSON_Delete(pnfRegistrationFields);
815 if (cJSON_AddStringToObject(pnfRegistrationFields, "oamV6IpAddress", "0:0:0:0:0:ffff:a0a:011") == NULL)
817 printf("Could not create JSON object: oamV6IpAddress\n");
818 cJSON_Delete(pnfRegistrationFields);
822 char serial_number[100];
823 sprintf(serial_number, "%s-%s-%d-Simulated Device Melacon", getenv("HOSTNAME"), getenv("NTS_IP"), port);
825 if (cJSON_AddStringToObject(pnfRegistrationFields, "serialNumber", serial_number) == NULL)
827 printf("Could not create JSON object: serialNumber\n");
828 cJSON_Delete(pnfRegistrationFields);
832 if (cJSON_AddStringToObject(pnfRegistrationFields, "softwareVersion", "2.3.5") == NULL)
834 printf("Could not create JSON object: softwareVersion\n");
835 cJSON_Delete(pnfRegistrationFields);
839 if (cJSON_AddStringToObject(pnfRegistrationFields, "unitFamily", "Simulated Device") == NULL)
841 printf("Could not create JSON object: unitFamily\n");
842 cJSON_Delete(pnfRegistrationFields);
846 if (cJSON_AddStringToObject(pnfRegistrationFields, "unitType", "O-RAN-sim") == NULL)
848 printf("Could not create JSON object: unitType\n");
849 cJSON_Delete(pnfRegistrationFields);
853 if (cJSON_AddStringToObject(pnfRegistrationFields, "vendorName", "Melacon") == NULL)
855 printf("Could not create JSON object: vendorName\n");
856 cJSON_Delete(pnfRegistrationFields);
860 cJSON *additionalFields = cJSON_CreateObject();
861 if (additionalFields == NULL)
863 printf("Could not create JSON object: additionalFields\n");
864 cJSON_Delete(pnfRegistrationFields);
867 cJSON_AddItemToObject(pnfRegistrationFields, "additionalFields", additionalFields);
870 sprintf(portString, "%d", port);
872 if (cJSON_AddStringToObject(additionalFields, "oamPort", portString) == NULL)
874 printf("Could not create JSON object: oamPort\n");
875 cJSON_Delete(pnfRegistrationFields);
881 //TLS specific configuration
882 if (cJSON_AddStringToObject(additionalFields, "protocol", "TLS") == NULL)
884 printf("Could not create JSON object: protocol\n");
885 cJSON_Delete(pnfRegistrationFields);
889 //TODO here we have the username from the docker container hardcoded: netconf
890 if (cJSON_AddStringToObject(additionalFields, "username", "netconf") == NULL)
892 printf("Could not create JSON object: username\n");
893 cJSON_Delete(pnfRegistrationFields);
897 if (cJSON_AddStringToObject(additionalFields, "keyId", "device-key") == NULL)
899 printf("Could not create JSON object: keyId\n");
900 cJSON_Delete(pnfRegistrationFields);
906 //SSH specific configuration
907 if (cJSON_AddStringToObject(additionalFields, "protocol", "SSH") == NULL)
909 printf("Could not create JSON object: protocol\n");
910 cJSON_Delete(pnfRegistrationFields);
914 //TODO here we have the username from the docker container hardcoded: netconf
915 if (cJSON_AddStringToObject(additionalFields, "username", "netconf") == NULL)
917 printf("Could not create JSON object: username\n");
918 cJSON_Delete(pnfRegistrationFields);
922 //TODO here we have the password from the docker container hardcoded: netconf
923 if (cJSON_AddStringToObject(additionalFields, "password", "netconf") == NULL)
925 printf("Could not create JSON object: password\n");
926 cJSON_Delete(pnfRegistrationFields);
931 if (cJSON_AddStringToObject(additionalFields, "reconnectOnChangedSchema", "false") == NULL)
933 printf("Could not create JSON object: reconnectOnChangedSchema\n");
934 cJSON_Delete(pnfRegistrationFields);
938 if (cJSON_AddStringToObject(additionalFields, "sleep-factor", "1.5") == NULL)
940 printf("Could not create JSON object: sleep-factor\n");
941 cJSON_Delete(pnfRegistrationFields);
945 if (cJSON_AddStringToObject(additionalFields, "tcpOnly", "false") == NULL)
947 printf("Could not create JSON object: tcpOnly\n");
948 cJSON_Delete(pnfRegistrationFields);
952 if (cJSON_AddStringToObject(additionalFields, "connectionTimeout", "20000") == NULL)
954 printf("Could not create JSON object: connectionTimeout\n");
955 cJSON_Delete(pnfRegistrationFields);
959 if (cJSON_AddStringToObject(additionalFields, "maxConnectionAttempts", "100") == NULL)
961 printf("Could not create JSON object: maxConnectionAttempts\n");
962 cJSON_Delete(pnfRegistrationFields);
966 if (cJSON_AddStringToObject(additionalFields, "betweenAttemptsTimeout", "2000") == NULL)
968 printf("Could not create JSON object: betweenAttemptsTimeout\n");
969 cJSON_Delete(pnfRegistrationFields);
973 if (cJSON_AddStringToObject(additionalFields, "keepaliveDelay", "120") == NULL)
975 printf("Could not create JSON object: keepaliveDelay\n");
976 cJSON_Delete(pnfRegistrationFields);
980 return pnfRegistrationFields;
983 int getNetconfAvailableFromConfigJson(void)
985 char *stringConfig = readConfigFileInString();
987 if (stringConfig == NULL)
989 printf("Could not read JSON configuration file in string.");
993 cJSON *jsonConfig = cJSON_Parse(stringConfig);
994 if (jsonConfig == NULL)
997 const char *error_ptr = cJSON_GetErrorPtr();
998 if (error_ptr != NULL)
1000 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
1002 return SR_ERR_OPERATION_FAILED;
1004 //we don't need the string anymore
1006 stringConfig = NULL;
1008 cJSON *notifDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
1009 if (!cJSON_IsObject(notifDetails))
1011 printf("Configuration JSON is not as expected: notification-config is not an object");
1012 cJSON_Delete(jsonConfig);
1013 return SR_ERR_OPERATION_FAILED;
1016 cJSON *isNetconfAvailable = cJSON_GetObjectItemCaseSensitive(notifDetails, "is-netconf-available");
1017 if (!cJSON_IsBool(isNetconfAvailable))
1019 printf("Configuration JSON is not as expected: is-netconf-available is not an object");
1020 cJSON_Delete(jsonConfig);
1021 return SR_ERR_OPERATION_FAILED;
1024 int is_netconf_available = (cJSON_IsTrue(isNetconfAvailable)) ? TRUE : FALSE;
1026 cJSON_Delete(jsonConfig);
1028 return is_netconf_available;
1031 int getVesAvailableFromConfigJson(void)
1033 char *stringConfig = readConfigFileInString();
1035 if (stringConfig == NULL)
1037 printf("Could not read JSON configuration file in string.");
1041 cJSON *jsonConfig = cJSON_Parse(stringConfig);
1042 if (jsonConfig == NULL)
1045 const char *error_ptr = cJSON_GetErrorPtr();
1046 if (error_ptr != NULL)
1048 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
1050 return SR_ERR_OPERATION_FAILED;
1052 //we don't need the string anymore
1054 stringConfig = NULL;
1056 cJSON *notifDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
1057 if (!cJSON_IsObject(notifDetails))
1059 printf("Configuration JSON is not as expected: notification-config is not an object");
1060 cJSON_Delete(jsonConfig);
1061 return SR_ERR_OPERATION_FAILED;
1064 cJSON *isVesAvailable = cJSON_GetObjectItemCaseSensitive(notifDetails, "is-ves-available");
1065 if (!cJSON_IsBool(isVesAvailable))
1067 printf("Configuration JSON is not as expected: is-ves-available is not an object");
1068 cJSON_Delete(jsonConfig);
1069 return SR_ERR_OPERATION_FAILED;
1072 int is_netconf_available = (cJSON_IsTrue(isVesAvailable)) ? TRUE : FALSE;
1074 cJSON_Delete(jsonConfig);
1076 return is_netconf_available;
1080 * Dynamically allocated memory;
1081 * Caller needs to free the memory after it uses the value.
1084 cJSON* vesCreateFaultFields(char *alarm_condition, char *alarm_object, char *severity, char *date_time, char *specific_problem)
1086 cJSON *faultFields = cJSON_CreateObject();
1087 if (faultFields == NULL)
1089 printf("Could not create JSON object: faultFields\n");
1093 if (cJSON_AddStringToObject(faultFields, "faultFieldsVersion", "4.0") == NULL)
1095 printf("Could not create JSON object: faultFieldsVersion\n");
1096 cJSON_Delete(faultFields);
1100 if (cJSON_AddStringToObject(faultFields, "alarmCondition", alarm_condition) == NULL)
1102 printf("Could not create JSON object: alarmCondition\n");
1103 cJSON_Delete(faultFields);
1107 if (cJSON_AddStringToObject(faultFields, "alarmInterfaceA", alarm_object) == NULL)
1109 printf("Could not create JSON object: alarmInterfaceA\n");
1110 cJSON_Delete(faultFields);
1114 if (cJSON_AddStringToObject(faultFields, "eventSourceType", "O_RAN_COMPONENT") == NULL)
1116 printf("Could not create JSON object: eventSourceType\n");
1117 cJSON_Delete(faultFields);
1121 if (cJSON_AddStringToObject(faultFields, "specificProblem", specific_problem) == NULL)
1123 printf("Could not create JSON object: specificProblem\n");
1124 cJSON_Delete(faultFields);
1128 if (cJSON_AddStringToObject(faultFields, "eventSeverity", severity) == NULL)
1130 printf("Could not create JSON object: eventSeverity\n");
1131 cJSON_Delete(faultFields);
1135 if (cJSON_AddStringToObject(faultFields, "vfStatus", "Active") == NULL)
1137 printf("Could not create JSON object: vfStatus\n");
1138 cJSON_Delete(faultFields);
1142 cJSON *alarmAdditionalInformation = cJSON_CreateObject();
1143 if (alarmAdditionalInformation == NULL)
1145 printf("Could not create JSON object: alarmAdditionalInformation\n");
1146 cJSON_Delete(faultFields);
1149 cJSON_AddItemToObject(faultFields, "alarmAdditionalInformation", alarmAdditionalInformation);
1151 if (cJSON_AddStringToObject(alarmAdditionalInformation, "eventTime", date_time) == NULL)
1153 printf("Could not create JSON object: eventTime\n");
1154 cJSON_Delete(faultFields);
1158 if (cJSON_AddStringToObject(alarmAdditionalInformation, "equipType", "O-RAN-sim") == NULL)
1160 printf("Could not create JSON object: equipType\n");
1161 cJSON_Delete(faultFields);
1165 if (cJSON_AddStringToObject(alarmAdditionalInformation, "vendor", "Melacon") == NULL)
1167 printf("Could not create JSON object: vendor\n");
1168 cJSON_Delete(faultFields);
1172 if (cJSON_AddStringToObject(alarmAdditionalInformation, "model", "Simulated Device") == NULL)
1174 printf("Could not create JSON object: model\n");
1175 cJSON_Delete(faultFields);
1182 static cJSON* createSeverityCounters(counterAlarms count)
1184 cJSON *severityCounters = cJSON_CreateObject();
1185 if (severityCounters == NULL)
1187 printf("Could not create JSON object: severityCounters\n");
1191 if (cJSON_AddNumberToObject(severityCounters, "severity-normal", count.normal) == NULL)
1193 printf("Could not create JSON object: severity-normal\n");
1197 if (cJSON_AddNumberToObject(severityCounters, "severity-warning", count.warning) == NULL)
1199 printf("Could not create JSON object: severity-warning\n");
1203 if (cJSON_AddNumberToObject(severityCounters, "severity-minor", count.minor) == NULL)
1205 printf("Could not create JSON object: severity-minor\n");
1209 if (cJSON_AddNumberToObject(severityCounters, "severity-major", count.major) == NULL)
1211 printf("Could not create JSON object: severity-major\n");
1215 if (cJSON_AddNumberToObject(severityCounters, "severity-critical", count.critical) == NULL)
1217 printf("Could not create JSON object: severity-critical\n");
1221 return severityCounters;
1224 void writeStatusFile(char *status)
1226 char status_file[200];
1227 sprintf(status_file, "%s/status.json", getenv("SCRIPTS_DIR"));
1228 FILE * f = fopen (status_file, "w");
1237 printf("Could not write status file!\n");
1241 int writeSkeletonStatusFile()
1243 cJSON *statusObject = cJSON_CreateObject();
1244 if (statusObject == NULL)
1246 printf("Could not create JSON object: statusObject\n");
1247 return SR_ERR_OPERATION_FAILED;
1250 // counterAlarms counter = {
1258 // cJSON *totalVesNotifications = createSeverityCounters(counter);
1259 // if (totalVesNotifications == NULL)
1261 // printf("Could not create JSON object: totalVesNotifications\n");
1262 // cJSON_Delete(statusObject);
1263 // return SR_ERR_OPERATION_FAILED;
1265 // cJSON_AddItemToObject(statusObject, "total-ves-notifications-sent", totalVesNotifications);
1267 // cJSON *totalNetconfNotifications = createSeverityCounters(counter);
1268 // if (totalNetconfNotifications == NULL)
1270 // printf("Could not create JSON object: totalNetconfNotifications\n");
1271 // cJSON_Delete(statusObject);
1272 // return SR_ERR_OPERATION_FAILED;
1274 // cJSON_AddItemToObject(statusObject, "total-netconf-notifications-sent", totalNetconfNotifications);
1276 cJSON *deviceList = cJSON_CreateArray();
1277 if (deviceList == NULL)
1279 printf("Could not create JSON object: deviceList\n");
1280 cJSON_Delete(statusObject);
1281 return SR_ERR_OPERATION_FAILED;
1283 cJSON_AddItemToObject(statusObject, "device-list", deviceList);
1285 char *status_string = NULL;
1287 status_string = cJSON_PrintUnformatted(statusObject);
1289 writeStatusFile(status_string);
1295 * Dynamically allocated memory;
1296 * Caller needs to free the memory after it uses the value.
1299 char* readStatusFileInString(void)
1303 char config_file[200];
1304 sprintf(config_file, "%s/status.json", getenv("SCRIPTS_DIR"));
1305 FILE * f = fopen (config_file, "rb");
1309 fseek (f, 0, SEEK_END);
1311 fseek (f, 0, SEEK_SET);
1312 buffer = malloc (length + 1);
1315 fread (buffer, 1, length, f);
1318 buffer[length] = '\0';
1330 * Dynamically allocated memory;
1331 * Caller needs to free the memory after it uses the value.
1334 cJSON* getDeviceListFromStatusFile(void)
1336 char *stringStatus = readStatusFileInString();
1338 if (stringStatus == NULL)
1340 printf("Could not read status file!\n");
1344 cJSON *jsonStatus = cJSON_Parse(stringStatus);
1345 if (jsonStatus == NULL)
1348 const char *error_ptr = cJSON_GetErrorPtr();
1349 if (error_ptr != NULL)
1351 fprintf(stderr, "Could not parse JSON status! Error before: %s\n", error_ptr);
1355 //we don't need the string anymore
1357 stringStatus = NULL;
1362 cJSON* createDeviceListEntry(counterAlarms ves_counter, counterAlarms netconf_counter)
1364 cJSON *deviceListEntry = cJSON_CreateObject();
1365 if (deviceListEntry == NULL)
1367 printf("Could not create JSON object: deviceListEntry\n");
1372 sprintf(hostname, "%s", getenv("HOSTNAME"));
1374 if (cJSON_AddStringToObject(deviceListEntry, "device-name", hostname) == NULL)
1376 printf("Could not create JSON object: device-name\n");
1377 cJSON_Delete(deviceListEntry);
1381 cJSON *vesNotificationsSent = createSeverityCounters(ves_counter);
1382 if (vesNotificationsSent == NULL)
1384 printf("Could not create JSON object: vesNotificationsSent\n");
1385 cJSON_Delete(deviceListEntry);
1388 cJSON_AddItemToObject(deviceListEntry, "ves-notifications-sent", vesNotificationsSent);
1390 cJSON *netconfNotificationsSent = createSeverityCounters(netconf_counter);
1391 if (netconfNotificationsSent == NULL)
1393 printf("Could not create JSON object: netconfNotificationsSent\n");
1394 cJSON_Delete(deviceListEntry);
1397 cJSON_AddItemToObject(deviceListEntry, "netconf-notifications-sent", netconfNotificationsSent);
1399 return deviceListEntry;
1402 static void modifySeverityCounters(cJSON **severityCounters, counterAlarms count)
1404 cJSON *severity= cJSON_GetObjectItemCaseSensitive(*severityCounters, "severity-normal");
1405 if (!cJSON_IsNumber(severity))
1407 printf("Status JSON is not as expected: severity-normal is not an number");
1410 //we set the value of the severity-normal object
1411 cJSON_SetNumberValue(severity, count.normal);
1413 severity= cJSON_GetObjectItemCaseSensitive(*severityCounters, "severity-warning");
1414 if (!cJSON_IsNumber(severity))
1416 printf("Status JSON is not as expected: severity-warning is not an number");
1419 //we set the value of the severity-warning object
1420 cJSON_SetNumberValue(severity, count.warning);
1422 severity= cJSON_GetObjectItemCaseSensitive(*severityCounters, "severity-minor");
1423 if (!cJSON_IsNumber(severity))
1425 printf("Status JSON is not as expected: severity-minor is not an number");
1428 //we set the value of the severity-minor object
1429 cJSON_SetNumberValue(severity, count.minor);
1431 severity= cJSON_GetObjectItemCaseSensitive(*severityCounters, "severity-major");
1432 if (!cJSON_IsNumber(severity))
1434 printf("Status JSON is not as expected: severity-major is not an number");
1437 //we set the value of the severity-major object
1438 cJSON_SetNumberValue(severity, count.major);
1440 severity= cJSON_GetObjectItemCaseSensitive(*severityCounters, "severity-critical");
1441 if (!cJSON_IsNumber(severity))
1443 printf("Status JSON is not as expected: severity-critical is not an number");
1446 //we set the value of the severity-critical object
1447 cJSON_SetNumberValue(severity, count.critical);
1452 static void modifyDeviceListEntry(cJSON **deviceListEntry, counterAlarms ves_counter, counterAlarms netconf_counter)
1454 cJSON *vesNotificationsSent= cJSON_GetObjectItemCaseSensitive(*deviceListEntry, "ves-notifications-sent");
1455 if (!cJSON_IsObject(vesNotificationsSent))
1457 printf("Status JSON is not as expected: ves-notifications-sent is not a object");
1461 modifySeverityCounters(&vesNotificationsSent, ves_counter);
1463 cJSON *netconfNotificationsSent= cJSON_GetObjectItemCaseSensitive(*deviceListEntry, "netconf-notifications-sent");
1464 if (!cJSON_IsObject(netconfNotificationsSent))
1466 printf("Status JSON is not as expected: netconf-notifications-sent is not a object");
1470 modifySeverityCounters(&netconfNotificationsSent, netconf_counter);
1473 int writeStatusNotificationCounters(counterAlarms ves_counter, counterAlarms netconf_counter)
1475 cJSON *jsonStatus = getDeviceListFromStatusFile();
1477 cJSON *deviceList = cJSON_GetObjectItemCaseSensitive(jsonStatus, "device-list");
1478 if (!cJSON_IsArray(deviceList))
1480 printf("Status JSON is not as expected: device-list is not an object");
1481 cJSON_Delete(jsonStatus);
1482 return SR_ERR_OPERATION_FAILED;
1485 int array_size = cJSON_GetArraySize(deviceList);
1488 for (int i=0; i<array_size; ++i)
1490 cJSON *deviceListEntry = cJSON_GetArrayItem(deviceList, i);
1492 sprintf(hostname, "%s", getenv("HOSTNAME"));
1494 cJSON *deviceName = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "device-name");
1495 if (!cJSON_IsString(deviceName))
1497 printf("Status JSON is not as expected: device-name is not a string.");
1498 cJSON_Delete(jsonStatus);
1499 return SR_ERR_OPERATION_FAILED;
1501 char *deviceNameString = cJSON_GetStringValue(deviceName);
1503 if (strcmp(hostname, deviceNameString) == 0)
1505 modifyDeviceListEntry(&deviceListEntry, ves_counter, netconf_counter);
1512 cJSON* deviceListEntry = createDeviceListEntry(ves_counter, netconf_counter);
1514 cJSON_AddItemToArray(deviceList, deviceListEntry);
1517 //writing the new JSON to the configuration file
1518 char *stringStatus = cJSON_PrintUnformatted(jsonStatus);
1519 writeStatusFile(stringStatus);
1521 cJSON_Delete(jsonStatus);
1527 int removeDeviceEntryFromStatusFile(char *containerId)
1529 cJSON *jsonStatus = getDeviceListFromStatusFile();
1531 cJSON *deviceList = cJSON_GetObjectItemCaseSensitive(jsonStatus, "device-list");
1532 if (!cJSON_IsArray(deviceList))
1534 printf("Status JSON is not as expected: device-list is not an object");
1535 cJSON_Delete(jsonStatus);
1536 return SR_ERR_OPERATION_FAILED;
1539 int array_size = cJSON_GetArraySize(deviceList);
1540 int found = array_size;
1542 for (int i=0; i<array_size; ++i)
1544 cJSON *deviceListEntry = cJSON_GetArrayItem(deviceList, i);
1546 sprintf(hostname, "%s", getenv("HOSTNAME"));
1548 cJSON *deviceName = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "device-name");
1549 if (!cJSON_IsString(deviceName))
1551 printf("Status JSON is not as expected: device-name is not a string.");
1552 cJSON_Delete(jsonStatus);
1553 return SR_ERR_OPERATION_FAILED;
1555 char *deviceNameString = cJSON_GetStringValue(deviceName);
1557 if (strcmp(containerId, deviceNameString) == 0)
1564 if (found < array_size)
1566 cJSON_DeleteItemFromArray(deviceList, found);
1570 printf("Could not find status file entry for device with id=\"%s\"", containerId);
1573 //writing the new JSON to the configuration file
1574 char *stringStatus = cJSON_PrintUnformatted(jsonStatus);
1575 writeStatusFile(stringStatus);
1577 cJSON_Delete(jsonStatus);
1582 int compute_notifications_count(counterAlarms *ves_counter, counterAlarms *netconf_counter)
1584 ves_counter->normal = ves_counter->warning = \
1585 ves_counter->minor = ves_counter->major = \
1586 ves_counter->critical = 0;
1587 netconf_counter->normal = netconf_counter->warning = \
1588 netconf_counter->minor = netconf_counter->major = \
1589 netconf_counter->critical = 0;
1591 cJSON *jsonStatus = getDeviceListFromStatusFile();
1593 cJSON *deviceList = cJSON_GetObjectItemCaseSensitive(jsonStatus, "device-list");
1594 if (!cJSON_IsArray(deviceList))
1596 printf("Status JSON is not as expected: device-list is not an object");
1597 cJSON_Delete(jsonStatus);
1598 return SR_ERR_OPERATION_FAILED;
1601 int array_size = cJSON_GetArraySize(deviceList);
1603 for (int i=0; i<array_size; ++i)
1605 cJSON *deviceListEntry = cJSON_GetArrayItem(deviceList, i);
1607 cJSON *vesNotifications = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "ves-notifications-sent");
1608 if (!cJSON_IsObject(vesNotifications))
1610 printf("Status JSON is not as expected: ves-notifications-sent is not an object.");
1611 cJSON_Delete(jsonStatus);
1612 return SR_ERR_OPERATION_FAILED;
1615 cJSON *severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-normal");
1616 if (!cJSON_IsNumber(severity))
1618 printf("Status JSON is not as expected: severity-normal is not a string.");
1619 cJSON_Delete(jsonStatus);
1620 return SR_ERR_OPERATION_FAILED;
1622 int counter = (int)(severity->valuedouble);
1623 ves_counter->normal += counter;
1625 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-warning");
1626 if (!cJSON_IsNumber(severity))
1628 printf("Status JSON is not as expected: severity-warning is not a string.");
1629 cJSON_Delete(jsonStatus);
1630 return SR_ERR_OPERATION_FAILED;
1632 counter = (int)(severity->valuedouble);
1633 ves_counter->warning += counter;
1635 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-minor");
1636 if (!cJSON_IsNumber(severity))
1638 printf("Status JSON is not as expected: severity-minor is not a string.");
1639 cJSON_Delete(jsonStatus);
1640 return SR_ERR_OPERATION_FAILED;
1642 counter = (int)(severity->valuedouble);
1643 ves_counter->minor += counter;
1645 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-major");
1646 if (!cJSON_IsNumber(severity))
1648 printf("Status JSON is not as expected: severity-major is not a string.");
1649 cJSON_Delete(jsonStatus);
1650 return SR_ERR_OPERATION_FAILED;
1652 counter = (int)(severity->valuedouble);
1653 ves_counter->major += counter;
1655 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-critical");
1656 if (!cJSON_IsNumber(severity))
1658 printf("Status JSON is not as expected: severity-critical is not a string.");
1659 cJSON_Delete(jsonStatus);
1660 return SR_ERR_OPERATION_FAILED;
1662 counter = (int)(severity->valuedouble);
1663 ves_counter->critical += counter;
1665 cJSON *netconfNotifications = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "netconf-notifications-sent");
1666 if (!cJSON_IsObject(netconfNotifications))
1668 printf("Status JSON is not as expected: netconf-notifications-sent is not an object.");
1669 cJSON_Delete(jsonStatus);
1670 return SR_ERR_OPERATION_FAILED;
1673 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-normal");
1674 if (!cJSON_IsNumber(severity))
1676 printf("Status JSON is not as expected: severity-normal is not a string.");
1677 cJSON_Delete(jsonStatus);
1678 return SR_ERR_OPERATION_FAILED;
1681 counter = (int)(severity->valuedouble);
1682 netconf_counter->normal += (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1684 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-warning");
1685 if (!cJSON_IsNumber(severity))
1687 printf("Status JSON is not as expected: severity-warning is not a string.");
1688 cJSON_Delete(jsonStatus);
1689 return SR_ERR_OPERATION_FAILED;
1691 counter = (int)(severity->valuedouble);
1692 netconf_counter->warning += (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1694 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-minor");
1695 if (!cJSON_IsNumber(severity))
1697 printf("Status JSON is not as expected: severity-minor is not a string.");
1698 cJSON_Delete(jsonStatus);
1699 return SR_ERR_OPERATION_FAILED;
1701 counter = (int)(severity->valuedouble);
1702 netconf_counter->minor += (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1704 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-major");
1705 if (!cJSON_IsNumber(severity))
1707 printf("Status JSON is not as expected: severity-major is not a string.");
1708 cJSON_Delete(jsonStatus);
1709 return SR_ERR_OPERATION_FAILED;
1711 counter = (int)(severity->valuedouble);
1712 netconf_counter->major += (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1714 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-critical");
1715 if (!cJSON_IsNumber(severity))
1717 printf("Status JSON is not as expected: severity-critical is not a string.");
1718 cJSON_Delete(jsonStatus);
1719 return SR_ERR_OPERATION_FAILED;
1721 counter = (int)(severity->valuedouble);
1722 netconf_counter->critical += (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1725 cJSON_Delete(jsonStatus);
1730 int getDeviceCounters(char *containerId, counterAlarms *ves_counter, counterAlarms *netconf_counter)
1732 cJSON *jsonStatus = getDeviceListFromStatusFile();
1734 cJSON *deviceList = cJSON_GetObjectItemCaseSensitive(jsonStatus, "device-list");
1735 if (!cJSON_IsArray(deviceList))
1737 printf("Status JSON is not as expected: device-list is not an object");
1738 cJSON_Delete(jsonStatus);
1739 return SR_ERR_OPERATION_FAILED;
1742 int array_size = cJSON_GetArraySize(deviceList);
1744 for (int i=0; i<array_size; ++i)
1746 cJSON *deviceListEntry = cJSON_GetArrayItem(deviceList, i);
1748 cJSON *deviceName = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "device-name");
1749 if (!cJSON_IsString(deviceName))
1751 printf("Status JSON is not as expected: device-name is not a string.");
1752 cJSON_Delete(jsonStatus);
1753 return SR_ERR_OPERATION_FAILED;
1755 char *deviceNameString = cJSON_GetStringValue(deviceName);
1757 if (strcmp(deviceNameString, containerId) != 0)
1762 cJSON *vesNotifications = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "ves-notifications-sent");
1763 if (!cJSON_IsObject(vesNotifications))
1765 printf("Status JSON is not as expected: ves-notifications-sent is not an object.");
1766 cJSON_Delete(jsonStatus);
1767 return SR_ERR_OPERATION_FAILED;
1770 cJSON *severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-normal");
1771 if (!cJSON_IsNumber(severity))
1773 printf("Status JSON is not as expected: severity-normal is not a string.");
1774 cJSON_Delete(jsonStatus);
1775 return SR_ERR_OPERATION_FAILED;
1777 int counter = (int)(severity->valuedouble);
1778 ves_counter->normal = counter;
1780 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-warning");
1781 if (!cJSON_IsNumber(severity))
1783 printf("Status JSON is not as expected: severity-warning is not a string.");
1784 cJSON_Delete(jsonStatus);
1785 return SR_ERR_OPERATION_FAILED;
1787 counter = (int)(severity->valuedouble);
1788 ves_counter->warning = counter;
1790 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-minor");
1791 if (!cJSON_IsNumber(severity))
1793 printf("Status JSON is not as expected: severity-minor is not a string.");
1794 cJSON_Delete(jsonStatus);
1795 return SR_ERR_OPERATION_FAILED;
1797 counter = (int)(severity->valuedouble);
1798 ves_counter->minor = counter;
1800 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-major");
1801 if (!cJSON_IsNumber(severity))
1803 printf("Status JSON is not as expected: severity-major is not a string.");
1804 cJSON_Delete(jsonStatus);
1805 return SR_ERR_OPERATION_FAILED;
1807 counter = (int)(severity->valuedouble);
1808 ves_counter->major = counter;
1810 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-critical");
1811 if (!cJSON_IsNumber(severity))
1813 printf("Status JSON is not as expected: severity-critical is not a string.");
1814 cJSON_Delete(jsonStatus);
1815 return SR_ERR_OPERATION_FAILED;
1817 counter = (int)(severity->valuedouble);
1818 ves_counter->critical = counter;
1820 cJSON *netconfNotifications = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "netconf-notifications-sent");
1821 if (!cJSON_IsObject(netconfNotifications))
1823 printf("Status JSON is not as expected: netconf-notifications-sent is not an object.");
1824 cJSON_Delete(jsonStatus);
1825 return SR_ERR_OPERATION_FAILED;
1828 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-normal");
1829 if (!cJSON_IsNumber(severity))
1831 printf("Status JSON is not as expected: severity-normal is not a string.");
1832 cJSON_Delete(jsonStatus);
1833 return SR_ERR_OPERATION_FAILED;
1836 counter = (int)(severity->valuedouble);
1837 netconf_counter->normal = (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1839 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-warning");
1840 if (!cJSON_IsNumber(severity))
1842 printf("Status JSON is not as expected: severity-warning is not a string.");
1843 cJSON_Delete(jsonStatus);
1844 return SR_ERR_OPERATION_FAILED;
1846 counter = (int)(severity->valuedouble);
1847 netconf_counter->warning = (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1849 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-minor");
1850 if (!cJSON_IsNumber(severity))
1852 printf("Status JSON is not as expected: severity-minor is not a string.");
1853 cJSON_Delete(jsonStatus);
1854 return SR_ERR_OPERATION_FAILED;
1856 counter = (int)(severity->valuedouble);
1857 netconf_counter->minor = (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1859 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-major");
1860 if (!cJSON_IsNumber(severity))
1862 printf("Status JSON is not as expected: severity-major is not a string.");
1863 cJSON_Delete(jsonStatus);
1864 return SR_ERR_OPERATION_FAILED;
1866 counter = (int)(severity->valuedouble);
1867 netconf_counter->major = (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1869 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-critical");
1870 if (!cJSON_IsNumber(severity))
1872 printf("Status JSON is not as expected: severity-critical is not a string.");
1873 cJSON_Delete(jsonStatus);
1874 return SR_ERR_OPERATION_FAILED;
1876 counter = (int)(severity->valuedouble);
1877 netconf_counter->critical = (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1880 cJSON_Delete(jsonStatus);