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();
196 char *ves_auth_method = getVesAuthMethodFromConfigJson();
197 if (strcmp(ves_auth_method, "basic-auth") == 0)
199 char *ves_username = getVesUsernameFromConfigJson();
200 char *ves_password = getVesPasswordFromConfigJson();
202 char credentials[200];
203 sprintf(credentials, "%s:%s", ves_username, ves_password);
208 curl_easy_setopt(curl, CURLOPT_USERPWD, credentials);
210 free(ves_auth_method);
213 sprintf(url, "https://%s:%d/eventListener/v7", ves_ip, ves_port);
214 curl_easy_setopt(curl, CURLOPT_URL, url);
216 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
217 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
218 curl_easy_setopt(curl, CURLOPT_PROXY_SSL_VERIFYPEER, 0L);
219 curl_easy_setopt(curl, CURLOPT_PROXY_SSL_VERIFYHOST, 0L);
227 * Dynamically allocated memory;
228 * Caller needs to free the memory after it uses the value.
231 cJSON* vesCreateCommonEventHeader(char *domain, char *event_type, char *source_name, int seq_id)
233 char dateAndTime[50];
234 getCurrentDateAndTime(dateAndTime);
236 long useconds = getMicrosecondsSinceEpoch();
238 cJSON *commonEventHeader = cJSON_CreateObject();
239 if (commonEventHeader == NULL)
241 printf("Could not create JSON object: commonEventHeader\n");
245 if (cJSON_AddStringToObject(commonEventHeader, "domain", domain) == NULL)
247 printf("Could not create JSON object: domain\n");
248 cJSON_Delete(commonEventHeader);
253 sprintf(eventId, "%s_%s", source_name, dateAndTime);
255 if (cJSON_AddStringToObject(commonEventHeader, "eventId", eventId) == NULL)
257 printf("Could not create JSON object: eventId\n");
258 cJSON_Delete(commonEventHeader);
262 char event_name[200];
263 sprintf(event_name, "%s_%s", domain, event_type);
265 if (cJSON_AddStringToObject(commonEventHeader, "eventName", event_name) == NULL)
267 printf("Could not create JSON object: eventName\n");
268 cJSON_Delete(commonEventHeader);
272 if (cJSON_AddStringToObject(commonEventHeader, "eventType", event_type) == NULL)
274 printf("Could not create JSON object: eventType\n");
275 cJSON_Delete(commonEventHeader);
279 if (cJSON_AddNumberToObject(commonEventHeader, "sequence", (double)(seq_id)) == NULL)
281 printf("Could not create JSON object: sequence\n");
282 cJSON_Delete(commonEventHeader);
286 if (cJSON_AddStringToObject(commonEventHeader, "priority", "Low") == NULL)
288 printf("Could not create JSON object: priority\n");
289 cJSON_Delete(commonEventHeader);
293 if (cJSON_AddStringToObject(commonEventHeader, "reportingEntityId", "") == NULL)
295 printf("Could not create JSON object: reportingEntityId\n");
296 cJSON_Delete(commonEventHeader);
300 if (cJSON_AddStringToObject(commonEventHeader, "reportingEntityName", source_name) == NULL)
302 printf("Could not create JSON object: reportingEntityName\n");
303 cJSON_Delete(commonEventHeader);
307 if (cJSON_AddStringToObject(commonEventHeader, "sourceId", "") == NULL)
309 printf("Could not create JSON object: sourceId\n");
310 cJSON_Delete(commonEventHeader);
314 if (cJSON_AddStringToObject(commonEventHeader, "sourceName", source_name) == NULL)
316 printf("Could not create JSON object: sourceName\n");
317 cJSON_Delete(commonEventHeader);
321 if (cJSON_AddNumberToObject(commonEventHeader, "startEpochMicrosec", (double)(useconds)) == NULL)
323 printf("Could not create JSON object: startEpochMicrosec\n");
324 cJSON_Delete(commonEventHeader);
328 if (cJSON_AddNumberToObject(commonEventHeader, "lastEpochMicrosec", (double)(useconds)) == NULL)
330 printf("Could not create JSON object: lastEpochMicrosec\n");
331 cJSON_Delete(commonEventHeader);
335 if (cJSON_AddStringToObject(commonEventHeader, "nfNamingCode", "sdn controller") == NULL)
337 printf("Could not create JSON object: nfNamingCode\n");
338 cJSON_Delete(commonEventHeader);
342 if (cJSON_AddStringToObject(commonEventHeader, "nfVendorName", "sdn") == NULL)
344 printf("Could not create JSON object: nfVendorName\n");
345 cJSON_Delete(commonEventHeader);
349 if (cJSON_AddStringToObject(commonEventHeader, "timeZoneOffset", "+00:00") == NULL)
351 printf("Could not create JSON object: timeZoneOffset\n");
352 cJSON_Delete(commonEventHeader);
356 if (cJSON_AddStringToObject(commonEventHeader, "version", "4.0.1") == NULL)
358 printf("Could not create JSON object: version\n");
359 cJSON_Delete(commonEventHeader);
363 if (cJSON_AddStringToObject(commonEventHeader, "vesEventListenerVersion", "7.0.1") == NULL)
365 printf("Could not create JSON object: vesEventListenerVersion\n");
366 cJSON_Delete(commonEventHeader);
370 return commonEventHeader;
374 * Dynamically allocated memory;
375 * Caller needs to free the memory after it uses the value.
378 cJSON* vesCreateHeartbeatFields(int heartbeat_interval)
380 char dateAndTime[50];
381 getCurrentDateAndTime(dateAndTime);
383 cJSON *heartbeatFields = cJSON_CreateObject();
384 if (heartbeatFields == NULL)
386 printf("Could not create JSON object: heartbeatFields\n");
390 if (cJSON_AddStringToObject(heartbeatFields, "heartbeatFieldsVersion", "3.0") == NULL)
392 printf("Could not create JSON object: heartbeatFieldsVersion\n");
393 cJSON_Delete(heartbeatFields);
397 if (cJSON_AddNumberToObject(heartbeatFields, "heartbeatInterval", (double)(heartbeat_interval)) == NULL)
399 printf("Could not create JSON object: heartbeatInterval\n");
400 cJSON_Delete(heartbeatFields);
404 cJSON *additionalFields = cJSON_CreateObject();
405 if (additionalFields == NULL)
407 printf("Could not create JSON object: additionalFields\n");
408 cJSON_Delete(heartbeatFields);
411 cJSON_AddItemToObject(heartbeatFields, "additionalFields", additionalFields);
413 if (cJSON_AddStringToObject(additionalFields, "eventTime", dateAndTime) == NULL)
415 printf("Could not create JSON object: eventTime\n");
416 cJSON_Delete(heartbeatFields);
420 return heartbeatFields;
424 * Dynamically allocated memory;
425 * Caller needs to free the memory after it uses the value.
428 char* readConfigFileInString(void)
432 char config_file[200];
434 //sprintf(config_file, "%s/configuration.json", getenv("SCRIPTS_DIR"));
436 // --> recommended fix, Karl Koch, Deutsche Telekom AG, 22. 5. 2020.
437 // Path to config_file contains NULL when env variable is unset.
438 char *scripts_dir = getenv("SCRIPTS_DIR");
439 char *scripts_dir_default = "/opt/dev/ntsimulator/scripts";
441 if(NULL != scripts_dir)
443 sprintf(config_file, "%s/configuration.json", scripts_dir);
447 sprintf(config_file, "%s/configuration.json", scripts_dir_default);
448 printf("warning: opening config file in default path: <%s>\n",
453 FILE * f = fopen (config_file, "rb");
457 fseek (f, 0, SEEK_END);
459 fseek (f, 0, SEEK_SET);
460 buffer = malloc (length + 1);
463 fread (buffer, 1, length, f);
466 buffer[length] = '\0';
477 void writeConfigFile(char *config)
479 char config_file[200];
481 //sprintf(config_file, "%s/configuration.json", getenv("SCRIPTS_DIR"));
483 // --> recommended fix, Karl Koch, Deutsche Telekom AG, 22. 5. 2020.
484 // Path to config_file contains NULL when env variable is unset.
485 char *scripts_dir = getenv("SCRIPTS_DIR");
486 char *scripts_dir_default = "/opt/dev/ntsimulator/scripts";
488 if(NULL != scripts_dir)
490 sprintf(config_file, "%s/configuration.json", scripts_dir);
494 sprintf(config_file, "%s/configuration.json", scripts_dir_default);
495 printf("warning: opening config file in default path: <%s>\n",
500 FILE * f = fopen (config_file, "w");
509 printf("Could not write configuration file");
513 int getFaultNotificationDelayPeriodFromConfigJson(int *period_array, int *count)
515 char *stringConfig = readConfigFileInString();
517 if (stringConfig == NULL)
519 printf("Could not read JSON configuration file in string.");
523 cJSON *jsonConfig = cJSON_Parse(stringConfig);
524 if (jsonConfig == NULL)
527 const char *error_ptr = cJSON_GetErrorPtr();
528 if (error_ptr != NULL)
530 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
532 return SR_ERR_OPERATION_FAILED;
534 //we don't need the string anymore
538 cJSON *notifConfig = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
539 if (!cJSON_IsObject(notifConfig))
541 printf("Configuration JSON is not as expected: notification-config is not an object");
542 cJSON_Delete(jsonConfig);
543 return SR_ERR_OPERATION_FAILED;
546 cJSON *faultNotifDelay = cJSON_GetObjectItemCaseSensitive(notifConfig, "fault-notification-delay-period");
547 if (!cJSON_IsArray(faultNotifDelay))
549 printf("Configuration JSON is not as expected: fault-notification-delay-period is not an array.");
550 cJSON_Delete(jsonConfig);
551 return SR_ERR_OPERATION_FAILED;
554 cJSON *iterator = NULL;
558 cJSON_ArrayForEach(iterator, faultNotifDelay)
560 if (cJSON_IsNumber(iterator))
562 period_array[i++] = iterator->valueint;
566 printf("Invalid number in array!");
572 cJSON_Delete(jsonConfig);
577 int getVesHeartbeatPeriodFromConfigJson(void)
579 char *stringConfig = readConfigFileInString();
580 int vesHeartbeat = 0;
582 if (stringConfig == NULL)
584 printf("Could not read JSON configuration file in string.");
588 cJSON *jsonConfig = cJSON_Parse(stringConfig);
589 if (jsonConfig == NULL)
592 const char *error_ptr = cJSON_GetErrorPtr();
593 if (error_ptr != NULL)
595 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
597 return SR_ERR_OPERATION_FAILED;
599 //we don't need the string anymore
603 cJSON *notifConfig = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
604 if (!cJSON_IsObject(notifConfig))
606 printf("Configuration JSON is not as expected: notification-config is not an object");
607 cJSON_Delete(jsonConfig);
608 return SR_ERR_OPERATION_FAILED;
611 cJSON *vesHeartbeatPeriod = cJSON_GetObjectItemCaseSensitive(notifConfig, "ves-heartbeat-period");
612 if (!cJSON_IsNumber(vesHeartbeatPeriod))
614 printf("Configuration JSON is not as expected: ves-heartbeat-period is not a number");
615 cJSON_Delete(jsonConfig);
616 return SR_ERR_OPERATION_FAILED;
619 vesHeartbeat = (int)(vesHeartbeatPeriod->valuedouble);
621 cJSON_Delete(jsonConfig);
628 * Dynamically allocated memory;
629 * Caller needs to free the memory after it uses the value.
632 char* getVesAuthMethodFromConfigJson(void)
634 char *stringConfig = readConfigFileInString();
636 if (stringConfig == NULL)
638 printf("Could not read JSON configuration file in string.");
642 cJSON *jsonConfig = cJSON_Parse(stringConfig);
643 if (jsonConfig == NULL)
646 const char *error_ptr = cJSON_GetErrorPtr();
647 if (error_ptr != NULL)
649 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
653 //we don't need the string anymore
657 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
658 if (!cJSON_IsObject(vesDetails))
660 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
661 cJSON_Delete(jsonConfig);
665 cJSON *vesAuthMethod = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-auth-method");
666 if (!cJSON_IsString(vesAuthMethod))
668 printf("Configuration JSON is not as expected: ves-endpoint-auth-method is not an object");
669 cJSON_Delete(jsonConfig);
673 char *auth_method_string = strdup(cJSON_GetStringValue(vesAuthMethod));
675 cJSON_Delete(jsonConfig);
677 return auth_method_string;
681 * Dynamically allocated memory;
682 * Caller needs to free the memory after it uses the value.
685 char* getVesIpFromConfigJson(void)
687 char *stringConfig = readConfigFileInString();
689 if (stringConfig == NULL)
691 printf("Could not read JSON configuration file in string.");
695 cJSON *jsonConfig = cJSON_Parse(stringConfig);
696 if (jsonConfig == NULL)
699 const char *error_ptr = cJSON_GetErrorPtr();
700 if (error_ptr != NULL)
702 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
706 //we don't need the string anymore
710 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
711 if (!cJSON_IsObject(vesDetails))
713 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
714 cJSON_Delete(jsonConfig);
718 cJSON *vesIp = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-ip");
719 if (!cJSON_IsString(vesIp))
721 printf("Configuration JSON is not as expected: ves-endpoint-ip is not an object");
722 cJSON_Delete(jsonConfig);
726 char *ves_ip = strdup(cJSON_GetStringValue(vesIp));
728 cJSON_Delete(jsonConfig);
734 * Dynamically allocated memory;
735 * Caller needs to free the memory after it uses the value.
738 char* getVesUsernameFromConfigJson(void)
740 char *stringConfig = readConfigFileInString();
742 if (stringConfig == NULL)
744 printf("Could not read JSON configuration file in string.");
748 cJSON *jsonConfig = cJSON_Parse(stringConfig);
749 if (jsonConfig == NULL)
752 const char *error_ptr = cJSON_GetErrorPtr();
753 if (error_ptr != NULL)
755 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
759 //we don't need the string anymore
763 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
764 if (!cJSON_IsObject(vesDetails))
766 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
767 cJSON_Delete(jsonConfig);
771 cJSON *vesUsername = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-username");
772 if (!cJSON_IsString(vesUsername))
774 printf("Configuration JSON is not as expected: ves-endpoint-username is not an object");
775 cJSON_Delete(jsonConfig);
779 char *ves_username = strdup(cJSON_GetStringValue(vesUsername));
781 cJSON_Delete(jsonConfig);
787 * Dynamically allocated memory;
788 * Caller needs to free the memory after it uses the value.
791 char* getVesPasswordFromConfigJson(void)
793 char *stringConfig = readConfigFileInString();
795 if (stringConfig == NULL)
797 printf("Could not read JSON configuration file in string.");
801 cJSON *jsonConfig = cJSON_Parse(stringConfig);
802 if (jsonConfig == NULL)
805 const char *error_ptr = cJSON_GetErrorPtr();
806 if (error_ptr != NULL)
808 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
812 //we don't need the string anymore
816 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
817 if (!cJSON_IsObject(vesDetails))
819 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
820 cJSON_Delete(jsonConfig);
824 cJSON *vesPassword = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-password");
825 if (!cJSON_IsString(vesPassword))
827 printf("Configuration JSON is not as expected: ves-endpoint-password is not an object");
828 cJSON_Delete(jsonConfig);
832 char *ves_password = strdup(cJSON_GetStringValue(vesPassword));
834 cJSON_Delete(jsonConfig);
839 int getVesPortFromConfigJson(void)
841 char *stringConfig = readConfigFileInString();
843 if (stringConfig == NULL)
845 printf("Could not read JSON configuration file in string.");
849 cJSON *jsonConfig = cJSON_Parse(stringConfig);
850 if (jsonConfig == NULL)
853 const char *error_ptr = cJSON_GetErrorPtr();
854 if (error_ptr != NULL)
856 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
858 return SR_ERR_OPERATION_FAILED;
860 //we don't need the string anymore
864 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
865 if (!cJSON_IsObject(vesDetails))
867 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
868 cJSON_Delete(jsonConfig);
869 return SR_ERR_OPERATION_FAILED;
872 cJSON *vesPort = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-port");
873 if (!cJSON_IsNumber(vesPort))
875 printf("Configuration JSON is not as expected: ves-endpoint-port is not an object");
876 cJSON_Delete(jsonConfig);
877 return SR_ERR_OPERATION_FAILED;
880 int port = (int)(vesPort->valuedouble);
882 cJSON_Delete(jsonConfig);
887 int getVesRegistrationFromConfigJson(void)
889 char *stringConfig = readConfigFileInString();
891 if (stringConfig == NULL)
893 printf("Could not read JSON configuration file in string.");
897 cJSON *jsonConfig = cJSON_Parse(stringConfig);
898 if (jsonConfig == NULL)
901 const char *error_ptr = cJSON_GetErrorPtr();
902 if (error_ptr != NULL)
904 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
906 return SR_ERR_OPERATION_FAILED;
908 //we don't need the string anymore
912 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
913 if (!cJSON_IsObject(vesDetails))
915 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
916 cJSON_Delete(jsonConfig);
917 return SR_ERR_OPERATION_FAILED;
920 cJSON *vesReg = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-registration");
921 if (!cJSON_IsBool(vesReg))
923 printf("Configuration JSON is not as expected: ves-registration is not an object");
924 cJSON_Delete(jsonConfig);
925 return SR_ERR_OPERATION_FAILED;
928 int is_ves_reg = (cJSON_IsTrue(vesReg)) ? TRUE : FALSE;
930 cJSON_Delete(jsonConfig);
936 * Dynamically allocated memory;
937 * Caller needs to free the memory after it uses the value.
940 cJSON* vesCreatePnfRegistrationFields(int port, bool is_tls)
942 cJSON *pnfRegistrationFields = cJSON_CreateObject();
943 if (pnfRegistrationFields == NULL)
945 printf("Could not create JSON object: pnfRegistrationFields\n");
949 if (cJSON_AddStringToObject(pnfRegistrationFields, "pnfRegistrationFieldsVersion", "2.0") == NULL)
951 printf("Could not create JSON object: pnfRegistrationFieldsVersion\n");
952 cJSON_Delete(pnfRegistrationFields);
956 if (cJSON_AddStringToObject(pnfRegistrationFields, "lastServiceDate", "2019-08-16") == NULL)
958 printf("Could not create JSON object: lastServiceDate\n");
959 cJSON_Delete(pnfRegistrationFields);
964 generateRandomMacAddress(mac_addr);
966 if (cJSON_AddStringToObject(pnfRegistrationFields, "macAddress", mac_addr) == NULL)
968 printf("Could not create JSON object: macAddress\n");
969 cJSON_Delete(pnfRegistrationFields);
973 if (cJSON_AddStringToObject(pnfRegistrationFields, "manufactureDate", "2019-08-16") == NULL)
975 printf("Could not create JSON object: manufactureDate\n");
976 cJSON_Delete(pnfRegistrationFields);
980 if (cJSON_AddStringToObject(pnfRegistrationFields, "modelNumber", "Simulated Device Melacon") == NULL)
982 printf("Could not create JSON object: manufactureDate\n");
983 cJSON_Delete(pnfRegistrationFields);
987 if (cJSON_AddStringToObject(pnfRegistrationFields, "oamV4IpAddress", getenv("EXTERNAL_NTS_IP")) == NULL)
989 printf("Could not create JSON object: oamV4IpAddress\n");
990 cJSON_Delete(pnfRegistrationFields);
994 if (cJSON_AddStringToObject(pnfRegistrationFields, "oamV6IpAddress", "0:0:0:0:0:ffff:a0a:011") == NULL)
996 printf("Could not create JSON object: oamV6IpAddress\n");
997 cJSON_Delete(pnfRegistrationFields);
1001 char serial_number[100];
1002 sprintf(serial_number, "%s-%s-%d-Simulated Device Melacon", getenv("HOSTNAME"), getenv("EXTERNAL_NTS_IP"), port);
1004 if (cJSON_AddStringToObject(pnfRegistrationFields, "serialNumber", serial_number) == NULL)
1006 printf("Could not create JSON object: serialNumber\n");
1007 cJSON_Delete(pnfRegistrationFields);
1011 if (cJSON_AddStringToObject(pnfRegistrationFields, "softwareVersion", "2.3.5") == NULL)
1013 printf("Could not create JSON object: softwareVersion\n");
1014 cJSON_Delete(pnfRegistrationFields);
1018 if (cJSON_AddStringToObject(pnfRegistrationFields, "unitFamily", "Simulated Device") == NULL)
1020 printf("Could not create JSON object: unitFamily\n");
1021 cJSON_Delete(pnfRegistrationFields);
1025 if (cJSON_AddStringToObject(pnfRegistrationFields, "unitType", "O-RAN-sim") == NULL)
1027 printf("Could not create JSON object: unitType\n");
1028 cJSON_Delete(pnfRegistrationFields);
1032 if (cJSON_AddStringToObject(pnfRegistrationFields, "vendorName", "Melacon") == NULL)
1034 printf("Could not create JSON object: vendorName\n");
1035 cJSON_Delete(pnfRegistrationFields);
1039 cJSON *additionalFields = cJSON_CreateObject();
1040 if (additionalFields == NULL)
1042 printf("Could not create JSON object: additionalFields\n");
1043 cJSON_Delete(pnfRegistrationFields);
1046 cJSON_AddItemToObject(pnfRegistrationFields, "additionalFields", additionalFields);
1048 char portString[10];
1049 sprintf(portString, "%d", port);
1051 if (cJSON_AddStringToObject(additionalFields, "oamPort", portString) == NULL)
1053 printf("Could not create JSON object: oamPort\n");
1054 cJSON_Delete(pnfRegistrationFields);
1060 //TLS specific configuration
1061 if (cJSON_AddStringToObject(additionalFields, "protocol", "TLS") == NULL)
1063 printf("Could not create JSON object: protocol\n");
1064 cJSON_Delete(pnfRegistrationFields);
1068 //TODO here we have the username from the docker container hardcoded: netconf
1069 if (cJSON_AddStringToObject(additionalFields, "username", "netconf") == NULL)
1071 printf("Could not create JSON object: username\n");
1072 cJSON_Delete(pnfRegistrationFields);
1076 if (cJSON_AddStringToObject(additionalFields, "keyId", "device-key") == NULL)
1078 printf("Could not create JSON object: keyId\n");
1079 cJSON_Delete(pnfRegistrationFields);
1085 //SSH specific configuration
1086 if (cJSON_AddStringToObject(additionalFields, "protocol", "SSH") == NULL)
1088 printf("Could not create JSON object: protocol\n");
1089 cJSON_Delete(pnfRegistrationFields);
1093 //TODO here we have the username from the docker container hardcoded: netconf
1094 if (cJSON_AddStringToObject(additionalFields, "username", "netconf") == NULL)
1096 printf("Could not create JSON object: username\n");
1097 cJSON_Delete(pnfRegistrationFields);
1101 //TODO here we have the password from the docker container hardcoded: netconf
1102 if (cJSON_AddStringToObject(additionalFields, "password", "netconf") == NULL)
1104 printf("Could not create JSON object: password\n");
1105 cJSON_Delete(pnfRegistrationFields);
1110 if (cJSON_AddStringToObject(additionalFields, "reconnectOnChangedSchema", "false") == NULL)
1112 printf("Could not create JSON object: reconnectOnChangedSchema\n");
1113 cJSON_Delete(pnfRegistrationFields);
1117 if (cJSON_AddStringToObject(additionalFields, "sleep-factor", "1.5") == NULL)
1119 printf("Could not create JSON object: sleep-factor\n");
1120 cJSON_Delete(pnfRegistrationFields);
1124 if (cJSON_AddStringToObject(additionalFields, "tcpOnly", "false") == NULL)
1126 printf("Could not create JSON object: tcpOnly\n");
1127 cJSON_Delete(pnfRegistrationFields);
1131 if (cJSON_AddStringToObject(additionalFields, "connectionTimeout", "20000") == NULL)
1133 printf("Could not create JSON object: connectionTimeout\n");
1134 cJSON_Delete(pnfRegistrationFields);
1138 if (cJSON_AddStringToObject(additionalFields, "maxConnectionAttempts", "100") == NULL)
1140 printf("Could not create JSON object: maxConnectionAttempts\n");
1141 cJSON_Delete(pnfRegistrationFields);
1145 if (cJSON_AddStringToObject(additionalFields, "betweenAttemptsTimeout", "2000") == NULL)
1147 printf("Could not create JSON object: betweenAttemptsTimeout\n");
1148 cJSON_Delete(pnfRegistrationFields);
1152 if (cJSON_AddStringToObject(additionalFields, "keepaliveDelay", "120") == NULL)
1154 printf("Could not create JSON object: keepaliveDelay\n");
1155 cJSON_Delete(pnfRegistrationFields);
1159 return pnfRegistrationFields;
1162 int getNetconfAvailableFromConfigJson(void)
1164 char *stringConfig = readConfigFileInString();
1166 if (stringConfig == NULL)
1168 printf("Could not read JSON configuration file in string.");
1172 cJSON *jsonConfig = cJSON_Parse(stringConfig);
1173 if (jsonConfig == NULL)
1176 const char *error_ptr = cJSON_GetErrorPtr();
1177 if (error_ptr != NULL)
1179 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
1181 return SR_ERR_OPERATION_FAILED;
1183 //we don't need the string anymore
1185 stringConfig = NULL;
1187 cJSON *notifDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
1188 if (!cJSON_IsObject(notifDetails))
1190 printf("Configuration JSON is not as expected: notification-config is not an object");
1191 cJSON_Delete(jsonConfig);
1192 return SR_ERR_OPERATION_FAILED;
1195 cJSON *isNetconfAvailable = cJSON_GetObjectItemCaseSensitive(notifDetails, "is-netconf-available");
1196 if (!cJSON_IsBool(isNetconfAvailable))
1198 printf("Configuration JSON is not as expected: is-netconf-available is not an object");
1199 cJSON_Delete(jsonConfig);
1200 return SR_ERR_OPERATION_FAILED;
1203 int is_netconf_available = (cJSON_IsTrue(isNetconfAvailable)) ? TRUE : FALSE;
1205 cJSON_Delete(jsonConfig);
1207 return is_netconf_available;
1210 int getVesAvailableFromConfigJson(void)
1212 char *stringConfig = readConfigFileInString();
1214 if (stringConfig == NULL)
1216 printf("Could not read JSON configuration file in string.");
1220 cJSON *jsonConfig = cJSON_Parse(stringConfig);
1221 if (jsonConfig == NULL)
1224 const char *error_ptr = cJSON_GetErrorPtr();
1225 if (error_ptr != NULL)
1227 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
1229 return SR_ERR_OPERATION_FAILED;
1231 //we don't need the string anymore
1233 stringConfig = NULL;
1235 cJSON *notifDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
1236 if (!cJSON_IsObject(notifDetails))
1238 printf("Configuration JSON is not as expected: notification-config is not an object");
1239 cJSON_Delete(jsonConfig);
1240 return SR_ERR_OPERATION_FAILED;
1243 cJSON *isVesAvailable = cJSON_GetObjectItemCaseSensitive(notifDetails, "is-ves-available");
1244 if (!cJSON_IsBool(isVesAvailable))
1246 printf("Configuration JSON is not as expected: is-ves-available is not an object");
1247 cJSON_Delete(jsonConfig);
1248 return SR_ERR_OPERATION_FAILED;
1251 int is_netconf_available = (cJSON_IsTrue(isVesAvailable)) ? TRUE : FALSE;
1253 cJSON_Delete(jsonConfig);
1255 return is_netconf_available;
1259 * Dynamically allocated memory;
1260 * Caller needs to free the memory after it uses the value.
1263 cJSON* vesCreateFaultFields(char *alarm_condition, char *alarm_object, char *severity, char *date_time, char *specific_problem)
1265 cJSON *faultFields = cJSON_CreateObject();
1266 if (faultFields == NULL)
1268 printf("Could not create JSON object: faultFields\n");
1272 if (cJSON_AddStringToObject(faultFields, "faultFieldsVersion", "4.0") == NULL)
1274 printf("Could not create JSON object: faultFieldsVersion\n");
1275 cJSON_Delete(faultFields);
1279 if (cJSON_AddStringToObject(faultFields, "alarmCondition", alarm_condition) == NULL)
1281 printf("Could not create JSON object: alarmCondition\n");
1282 cJSON_Delete(faultFields);
1286 if (cJSON_AddStringToObject(faultFields, "alarmInterfaceA", alarm_object) == NULL)
1288 printf("Could not create JSON object: alarmInterfaceA\n");
1289 cJSON_Delete(faultFields);
1293 if (cJSON_AddStringToObject(faultFields, "eventSourceType", "O_RAN_COMPONENT") == NULL)
1295 printf("Could not create JSON object: eventSourceType\n");
1296 cJSON_Delete(faultFields);
1300 if (cJSON_AddStringToObject(faultFields, "specificProblem", specific_problem) == NULL)
1302 printf("Could not create JSON object: specificProblem\n");
1303 cJSON_Delete(faultFields);
1307 if (cJSON_AddStringToObject(faultFields, "eventSeverity", severity) == NULL)
1309 printf("Could not create JSON object: eventSeverity\n");
1310 cJSON_Delete(faultFields);
1314 if (cJSON_AddStringToObject(faultFields, "vfStatus", "Active") == NULL)
1316 printf("Could not create JSON object: vfStatus\n");
1317 cJSON_Delete(faultFields);
1321 cJSON *alarmAdditionalInformation = cJSON_CreateObject();
1322 if (alarmAdditionalInformation == NULL)
1324 printf("Could not create JSON object: alarmAdditionalInformation\n");
1325 cJSON_Delete(faultFields);
1328 cJSON_AddItemToObject(faultFields, "alarmAdditionalInformation", alarmAdditionalInformation);
1330 if (cJSON_AddStringToObject(alarmAdditionalInformation, "eventTime", date_time) == NULL)
1332 printf("Could not create JSON object: eventTime\n");
1333 cJSON_Delete(faultFields);
1337 if (cJSON_AddStringToObject(alarmAdditionalInformation, "equipType", "O-RAN-sim") == NULL)
1339 printf("Could not create JSON object: equipType\n");
1340 cJSON_Delete(faultFields);
1344 if (cJSON_AddStringToObject(alarmAdditionalInformation, "vendor", "Melacon") == NULL)
1346 printf("Could not create JSON object: vendor\n");
1347 cJSON_Delete(faultFields);
1351 if (cJSON_AddStringToObject(alarmAdditionalInformation, "model", "Simulated Device") == NULL)
1353 printf("Could not create JSON object: model\n");
1354 cJSON_Delete(faultFields);
1361 static cJSON* createSeverityCounters(counterAlarms count)
1363 cJSON *severityCounters = cJSON_CreateObject();
1364 if (severityCounters == NULL)
1366 printf("Could not create JSON object: severityCounters\n");
1370 if (cJSON_AddNumberToObject(severityCounters, "severity-normal", count.normal) == NULL)
1372 printf("Could not create JSON object: severity-normal\n");
1376 if (cJSON_AddNumberToObject(severityCounters, "severity-warning", count.warning) == NULL)
1378 printf("Could not create JSON object: severity-warning\n");
1382 if (cJSON_AddNumberToObject(severityCounters, "severity-minor", count.minor) == NULL)
1384 printf("Could not create JSON object: severity-minor\n");
1388 if (cJSON_AddNumberToObject(severityCounters, "severity-major", count.major) == NULL)
1390 printf("Could not create JSON object: severity-major\n");
1394 if (cJSON_AddNumberToObject(severityCounters, "severity-critical", count.critical) == NULL)
1396 printf("Could not create JSON object: severity-critical\n");
1400 return severityCounters;
1403 void writeStatusFile(char *status)
1405 char status_file[200];
1406 sprintf(status_file, "%s/status.json", getenv("SCRIPTS_DIR"));
1407 FILE * f = fopen (status_file, "w");
1416 printf("Could not write status file!\n");
1420 int writeSkeletonStatusFile()
1422 cJSON *statusObject = cJSON_CreateObject();
1423 if (statusObject == NULL)
1425 printf("Could not create JSON object: statusObject\n");
1426 return SR_ERR_OPERATION_FAILED;
1429 // counterAlarms counter = {
1437 // cJSON *totalVesNotifications = createSeverityCounters(counter);
1438 // if (totalVesNotifications == NULL)
1440 // printf("Could not create JSON object: totalVesNotifications\n");
1441 // cJSON_Delete(statusObject);
1442 // return SR_ERR_OPERATION_FAILED;
1444 // cJSON_AddItemToObject(statusObject, "total-ves-notifications-sent", totalVesNotifications);
1446 // cJSON *totalNetconfNotifications = createSeverityCounters(counter);
1447 // if (totalNetconfNotifications == NULL)
1449 // printf("Could not create JSON object: totalNetconfNotifications\n");
1450 // cJSON_Delete(statusObject);
1451 // return SR_ERR_OPERATION_FAILED;
1453 // cJSON_AddItemToObject(statusObject, "total-netconf-notifications-sent", totalNetconfNotifications);
1455 cJSON *deviceList = cJSON_CreateArray();
1456 if (deviceList == NULL)
1458 printf("Could not create JSON object: deviceList\n");
1459 cJSON_Delete(statusObject);
1460 return SR_ERR_OPERATION_FAILED;
1462 cJSON_AddItemToObject(statusObject, "device-list", deviceList);
1464 char *status_string = NULL;
1466 status_string = cJSON_PrintUnformatted(statusObject);
1468 writeStatusFile(status_string);
1470 if (status_string != NULL)
1472 free(status_string);
1473 status_string = NULL;
1476 if (statusObject != NULL)
1478 cJSON_Delete(statusObject);
1485 * Dynamically allocated memory;
1486 * Caller needs to free the memory after it uses the value.
1489 char* readStatusFileInString(void)
1493 char config_file[200];
1494 sprintf(config_file, "%s/status.json", getenv("SCRIPTS_DIR"));
1495 FILE * f = fopen (config_file, "rb");
1499 fseek (f, 0, SEEK_END);
1501 fseek (f, 0, SEEK_SET);
1502 buffer = malloc (length + 1);
1505 fread (buffer, 1, length, f);
1508 buffer[length] = '\0';
1520 * Dynamically allocated memory;
1521 * Caller needs to free the memory after it uses the value.
1524 cJSON* getDeviceListFromStatusFile(void)
1526 char *stringStatus = readStatusFileInString();
1528 if (stringStatus == NULL)
1530 printf("Could not read status file!\n");
1534 cJSON *jsonStatus = cJSON_Parse(stringStatus);
1535 if (jsonStatus == NULL)
1538 const char *error_ptr = cJSON_GetErrorPtr();
1539 if (error_ptr != NULL)
1541 fprintf(stderr, "Could not parse JSON status! Error before: %s\n", error_ptr);
1545 //we don't need the string anymore
1547 stringStatus = NULL;
1552 cJSON* createDeviceListEntry(counterAlarms ves_counter, counterAlarms netconf_counter)
1554 cJSON *deviceListEntry = cJSON_CreateObject();
1555 if (deviceListEntry == NULL)
1557 printf("Could not create JSON object: deviceListEntry\n");
1562 sprintf(hostname, "%s", getenv("HOSTNAME"));
1564 if (cJSON_AddStringToObject(deviceListEntry, "device-name", hostname) == NULL)
1566 printf("Could not create JSON object: device-name\n");
1567 cJSON_Delete(deviceListEntry);
1571 cJSON *vesNotificationsSent = createSeverityCounters(ves_counter);
1572 if (vesNotificationsSent == NULL)
1574 printf("Could not create JSON object: vesNotificationsSent\n");
1575 cJSON_Delete(deviceListEntry);
1578 cJSON_AddItemToObject(deviceListEntry, "ves-notifications-sent", vesNotificationsSent);
1580 cJSON *netconfNotificationsSent = createSeverityCounters(netconf_counter);
1581 if (netconfNotificationsSent == NULL)
1583 printf("Could not create JSON object: netconfNotificationsSent\n");
1584 cJSON_Delete(deviceListEntry);
1587 cJSON_AddItemToObject(deviceListEntry, "netconf-notifications-sent", netconfNotificationsSent);
1589 return deviceListEntry;
1592 static void modifySeverityCounters(cJSON **severityCounters, counterAlarms count)
1594 cJSON *severity= cJSON_GetObjectItemCaseSensitive(*severityCounters, "severity-normal");
1595 if (!cJSON_IsNumber(severity))
1597 printf("Status JSON is not as expected: severity-normal is not an number");
1600 //we set the value of the severity-normal object
1601 cJSON_SetNumberValue(severity, count.normal);
1603 severity= cJSON_GetObjectItemCaseSensitive(*severityCounters, "severity-warning");
1604 if (!cJSON_IsNumber(severity))
1606 printf("Status JSON is not as expected: severity-warning is not an number");
1609 //we set the value of the severity-warning object
1610 cJSON_SetNumberValue(severity, count.warning);
1612 severity= cJSON_GetObjectItemCaseSensitive(*severityCounters, "severity-minor");
1613 if (!cJSON_IsNumber(severity))
1615 printf("Status JSON is not as expected: severity-minor is not an number");
1618 //we set the value of the severity-minor object
1619 cJSON_SetNumberValue(severity, count.minor);
1621 severity= cJSON_GetObjectItemCaseSensitive(*severityCounters, "severity-major");
1622 if (!cJSON_IsNumber(severity))
1624 printf("Status JSON is not as expected: severity-major is not an number");
1627 //we set the value of the severity-major object
1628 cJSON_SetNumberValue(severity, count.major);
1630 severity= cJSON_GetObjectItemCaseSensitive(*severityCounters, "severity-critical");
1631 if (!cJSON_IsNumber(severity))
1633 printf("Status JSON is not as expected: severity-critical is not an number");
1636 //we set the value of the severity-critical object
1637 cJSON_SetNumberValue(severity, count.critical);
1642 static void modifyDeviceListEntry(cJSON **deviceListEntry, counterAlarms ves_counter, counterAlarms netconf_counter)
1644 cJSON *vesNotificationsSent= cJSON_GetObjectItemCaseSensitive(*deviceListEntry, "ves-notifications-sent");
1645 if (!cJSON_IsObject(vesNotificationsSent))
1647 printf("Status JSON is not as expected: ves-notifications-sent is not a object");
1651 modifySeverityCounters(&vesNotificationsSent, ves_counter);
1653 cJSON *netconfNotificationsSent= cJSON_GetObjectItemCaseSensitive(*deviceListEntry, "netconf-notifications-sent");
1654 if (!cJSON_IsObject(netconfNotificationsSent))
1656 printf("Status JSON is not as expected: netconf-notifications-sent is not a object");
1660 modifySeverityCounters(&netconfNotificationsSent, netconf_counter);
1663 int writeStatusNotificationCounters(counterAlarms ves_counter, counterAlarms netconf_counter)
1665 cJSON *jsonStatus = getDeviceListFromStatusFile();
1667 cJSON *deviceList = cJSON_GetObjectItemCaseSensitive(jsonStatus, "device-list");
1668 if (!cJSON_IsArray(deviceList))
1670 printf("Status JSON is not as expected: device-list is not an object");
1671 cJSON_Delete(jsonStatus);
1672 return SR_ERR_OPERATION_FAILED;
1675 int array_size = cJSON_GetArraySize(deviceList);
1678 for (int i=0; i<array_size; ++i)
1680 cJSON *deviceListEntry = cJSON_GetArrayItem(deviceList, i);
1682 sprintf(hostname, "%s", getenv("HOSTNAME"));
1684 cJSON *deviceName = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "device-name");
1685 if (!cJSON_IsString(deviceName))
1687 printf("Status JSON is not as expected: device-name is not a string.");
1688 cJSON_Delete(jsonStatus);
1689 return SR_ERR_OPERATION_FAILED;
1691 char *deviceNameString = cJSON_GetStringValue(deviceName);
1693 if (strcmp(hostname, deviceNameString) == 0)
1695 modifyDeviceListEntry(&deviceListEntry, ves_counter, netconf_counter);
1702 cJSON* deviceListEntry = createDeviceListEntry(ves_counter, netconf_counter);
1704 cJSON_AddItemToArray(deviceList, deviceListEntry);
1707 //writing the new JSON to the configuration file
1708 char *stringStatus = cJSON_PrintUnformatted(jsonStatus);
1709 writeStatusFile(stringStatus);
1711 if (stringStatus != NULL)
1714 stringStatus = NULL;
1717 if (jsonStatus != NULL)
1719 cJSON_Delete(jsonStatus);
1726 int removeDeviceEntryFromStatusFile(char *containerId)
1728 cJSON *jsonStatus = getDeviceListFromStatusFile();
1730 cJSON *deviceList = cJSON_GetObjectItemCaseSensitive(jsonStatus, "device-list");
1731 if (!cJSON_IsArray(deviceList))
1733 printf("Status JSON is not as expected: device-list is not an object");
1734 cJSON_Delete(jsonStatus);
1735 return SR_ERR_OPERATION_FAILED;
1738 int array_size = cJSON_GetArraySize(deviceList);
1739 int found = array_size;
1741 for (int i=0; i<array_size; ++i)
1743 cJSON *deviceListEntry = cJSON_GetArrayItem(deviceList, i);
1745 sprintf(hostname, "%s", getenv("HOSTNAME"));
1747 cJSON *deviceName = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "device-name");
1748 if (!cJSON_IsString(deviceName))
1750 printf("Status JSON is not as expected: device-name is not a string.");
1751 cJSON_Delete(jsonStatus);
1752 return SR_ERR_OPERATION_FAILED;
1754 char *deviceNameString = cJSON_GetStringValue(deviceName);
1756 if (strcmp(containerId, deviceNameString) == 0)
1763 if (found < array_size)
1765 cJSON_DeleteItemFromArray(deviceList, found);
1769 printf("Could not find status file entry for device with id=\"%s\"", containerId);
1772 //writing the new JSON to the configuration file
1773 char *stringStatus = cJSON_PrintUnformatted(jsonStatus);
1774 writeStatusFile(stringStatus);
1776 if (stringStatus != NULL)
1779 stringStatus = NULL;
1782 if (jsonStatus != NULL)
1784 cJSON_Delete(jsonStatus);
1790 int compute_notifications_count(counterAlarms *ves_counter, counterAlarms *netconf_counter)
1792 ves_counter->normal = ves_counter->warning = \
1793 ves_counter->minor = ves_counter->major = \
1794 ves_counter->critical = 0;
1795 netconf_counter->normal = netconf_counter->warning = \
1796 netconf_counter->minor = netconf_counter->major = \
1797 netconf_counter->critical = 0;
1799 cJSON *jsonStatus = getDeviceListFromStatusFile();
1801 cJSON *deviceList = cJSON_GetObjectItemCaseSensitive(jsonStatus, "device-list");
1802 if (!cJSON_IsArray(deviceList))
1804 printf("Status JSON is not as expected: device-list is not an object");
1805 cJSON_Delete(jsonStatus);
1806 return SR_ERR_OPERATION_FAILED;
1809 int array_size = cJSON_GetArraySize(deviceList);
1811 for (int i=0; i<array_size; ++i)
1813 cJSON *deviceListEntry = cJSON_GetArrayItem(deviceList, i);
1815 cJSON *vesNotifications = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "ves-notifications-sent");
1816 if (!cJSON_IsObject(vesNotifications))
1818 printf("Status JSON is not as expected: ves-notifications-sent is not an object.");
1819 cJSON_Delete(jsonStatus);
1820 return SR_ERR_OPERATION_FAILED;
1823 cJSON *severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-normal");
1824 if (!cJSON_IsNumber(severity))
1826 printf("Status JSON is not as expected: severity-normal is not a string.");
1827 cJSON_Delete(jsonStatus);
1828 return SR_ERR_OPERATION_FAILED;
1830 int counter = (int)(severity->valuedouble);
1831 ves_counter->normal += counter;
1833 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-warning");
1834 if (!cJSON_IsNumber(severity))
1836 printf("Status JSON is not as expected: severity-warning is not a string.");
1837 cJSON_Delete(jsonStatus);
1838 return SR_ERR_OPERATION_FAILED;
1840 counter = (int)(severity->valuedouble);
1841 ves_counter->warning += counter;
1843 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-minor");
1844 if (!cJSON_IsNumber(severity))
1846 printf("Status JSON is not as expected: severity-minor is not a string.");
1847 cJSON_Delete(jsonStatus);
1848 return SR_ERR_OPERATION_FAILED;
1850 counter = (int)(severity->valuedouble);
1851 ves_counter->minor += counter;
1853 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-major");
1854 if (!cJSON_IsNumber(severity))
1856 printf("Status JSON is not as expected: severity-major is not a string.");
1857 cJSON_Delete(jsonStatus);
1858 return SR_ERR_OPERATION_FAILED;
1860 counter = (int)(severity->valuedouble);
1861 ves_counter->major += counter;
1863 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-critical");
1864 if (!cJSON_IsNumber(severity))
1866 printf("Status JSON is not as expected: severity-critical is not a string.");
1867 cJSON_Delete(jsonStatus);
1868 return SR_ERR_OPERATION_FAILED;
1870 counter = (int)(severity->valuedouble);
1871 ves_counter->critical += counter;
1873 cJSON *netconfNotifications = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "netconf-notifications-sent");
1874 if (!cJSON_IsObject(netconfNotifications))
1876 printf("Status JSON is not as expected: netconf-notifications-sent is not an object.");
1877 cJSON_Delete(jsonStatus);
1878 return SR_ERR_OPERATION_FAILED;
1881 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-normal");
1882 if (!cJSON_IsNumber(severity))
1884 printf("Status JSON is not as expected: severity-normal is not a string.");
1885 cJSON_Delete(jsonStatus);
1886 return SR_ERR_OPERATION_FAILED;
1889 counter = (int)(severity->valuedouble);
1890 netconf_counter->normal += (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1892 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-warning");
1893 if (!cJSON_IsNumber(severity))
1895 printf("Status JSON is not as expected: severity-warning is not a string.");
1896 cJSON_Delete(jsonStatus);
1897 return SR_ERR_OPERATION_FAILED;
1899 counter = (int)(severity->valuedouble);
1900 netconf_counter->warning += (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1902 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-minor");
1903 if (!cJSON_IsNumber(severity))
1905 printf("Status JSON is not as expected: severity-minor is not a string.");
1906 cJSON_Delete(jsonStatus);
1907 return SR_ERR_OPERATION_FAILED;
1909 counter = (int)(severity->valuedouble);
1910 netconf_counter->minor += (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1912 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-major");
1913 if (!cJSON_IsNumber(severity))
1915 printf("Status JSON is not as expected: severity-major is not a string.");
1916 cJSON_Delete(jsonStatus);
1917 return SR_ERR_OPERATION_FAILED;
1919 counter = (int)(severity->valuedouble);
1920 netconf_counter->major += (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1922 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-critical");
1923 if (!cJSON_IsNumber(severity))
1925 printf("Status JSON is not as expected: severity-critical is not a string.");
1926 cJSON_Delete(jsonStatus);
1927 return SR_ERR_OPERATION_FAILED;
1929 counter = (int)(severity->valuedouble);
1930 netconf_counter->critical += (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1933 cJSON_Delete(jsonStatus);
1938 int getDeviceCounters(char *containerId, counterAlarms *ves_counter, counterAlarms *netconf_counter)
1940 cJSON *jsonStatus = getDeviceListFromStatusFile();
1942 cJSON *deviceList = cJSON_GetObjectItemCaseSensitive(jsonStatus, "device-list");
1943 if (!cJSON_IsArray(deviceList))
1945 printf("Status JSON is not as expected: device-list is not an object");
1946 cJSON_Delete(jsonStatus);
1947 return SR_ERR_OPERATION_FAILED;
1950 int array_size = cJSON_GetArraySize(deviceList);
1952 ves_counter->critical = ves_counter->major = ves_counter->minor = ves_counter->warning = ves_counter->normal = 0;
1953 netconf_counter->critical = netconf_counter->major = netconf_counter->minor = netconf_counter->warning = netconf_counter->normal = 0;
1955 for (int i=0; i<array_size; ++i)
1957 cJSON *deviceListEntry = cJSON_GetArrayItem(deviceList, i);
1959 cJSON *deviceName = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "device-name");
1960 if (!cJSON_IsString(deviceName))
1962 printf("Status JSON is not as expected: device-name is not a string.");
1963 cJSON_Delete(jsonStatus);
1964 return SR_ERR_OPERATION_FAILED;
1966 char *deviceNameString = cJSON_GetStringValue(deviceName);
1968 if (strcmp(deviceNameString, containerId) != 0)
1973 cJSON *vesNotifications = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "ves-notifications-sent");
1974 if (!cJSON_IsObject(vesNotifications))
1976 printf("Status JSON is not as expected: ves-notifications-sent is not an object.");
1977 cJSON_Delete(jsonStatus);
1978 return SR_ERR_OPERATION_FAILED;
1981 cJSON *severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-normal");
1982 if (!cJSON_IsNumber(severity))
1984 printf("Status JSON is not as expected: severity-normal is not a string.");
1985 cJSON_Delete(jsonStatus);
1986 return SR_ERR_OPERATION_FAILED;
1988 int counter = (int)(severity->valuedouble);
1989 ves_counter->normal = counter;
1991 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-warning");
1992 if (!cJSON_IsNumber(severity))
1994 printf("Status JSON is not as expected: severity-warning is not a string.");
1995 cJSON_Delete(jsonStatus);
1996 return SR_ERR_OPERATION_FAILED;
1998 counter = (int)(severity->valuedouble);
1999 ves_counter->warning = counter;
2001 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-minor");
2002 if (!cJSON_IsNumber(severity))
2004 printf("Status JSON is not as expected: severity-minor is not a string.");
2005 cJSON_Delete(jsonStatus);
2006 return SR_ERR_OPERATION_FAILED;
2008 counter = (int)(severity->valuedouble);
2009 ves_counter->minor = counter;
2011 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-major");
2012 if (!cJSON_IsNumber(severity))
2014 printf("Status JSON is not as expected: severity-major is not a string.");
2015 cJSON_Delete(jsonStatus);
2016 return SR_ERR_OPERATION_FAILED;
2018 counter = (int)(severity->valuedouble);
2019 ves_counter->major = counter;
2021 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-critical");
2022 if (!cJSON_IsNumber(severity))
2024 printf("Status JSON is not as expected: severity-critical is not a string.");
2025 cJSON_Delete(jsonStatus);
2026 return SR_ERR_OPERATION_FAILED;
2028 counter = (int)(severity->valuedouble);
2029 ves_counter->critical = counter;
2031 cJSON *netconfNotifications = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "netconf-notifications-sent");
2032 if (!cJSON_IsObject(netconfNotifications))
2034 printf("Status JSON is not as expected: netconf-notifications-sent is not an object.");
2035 cJSON_Delete(jsonStatus);
2036 return SR_ERR_OPERATION_FAILED;
2039 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-normal");
2040 if (!cJSON_IsNumber(severity))
2042 printf("Status JSON is not as expected: severity-normal is not a string.");
2043 cJSON_Delete(jsonStatus);
2044 return SR_ERR_OPERATION_FAILED;
2047 counter = (int)(severity->valuedouble);
2048 netconf_counter->normal = (counter * NETCONF_CONNECTIONS_PER_DEVICE);
2050 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-warning");
2051 if (!cJSON_IsNumber(severity))
2053 printf("Status JSON is not as expected: severity-warning is not a string.");
2054 cJSON_Delete(jsonStatus);
2055 return SR_ERR_OPERATION_FAILED;
2057 counter = (int)(severity->valuedouble);
2058 netconf_counter->warning = (counter * NETCONF_CONNECTIONS_PER_DEVICE);
2060 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-minor");
2061 if (!cJSON_IsNumber(severity))
2063 printf("Status JSON is not as expected: severity-minor is not a string.");
2064 cJSON_Delete(jsonStatus);
2065 return SR_ERR_OPERATION_FAILED;
2067 counter = (int)(severity->valuedouble);
2068 netconf_counter->minor = (counter * NETCONF_CONNECTIONS_PER_DEVICE);
2070 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-major");
2071 if (!cJSON_IsNumber(severity))
2073 printf("Status JSON is not as expected: severity-major is not a string.");
2074 cJSON_Delete(jsonStatus);
2075 return SR_ERR_OPERATION_FAILED;
2077 counter = (int)(severity->valuedouble);
2078 netconf_counter->major = (counter * NETCONF_CONNECTIONS_PER_DEVICE);
2080 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-critical");
2081 if (!cJSON_IsNumber(severity))
2083 printf("Status JSON is not as expected: severity-critical is not a string.");
2084 cJSON_Delete(jsonStatus);
2085 return SR_ERR_OPERATION_FAILED;
2087 counter = (int)(severity->valuedouble);
2088 netconf_counter->critical = (counter * NETCONF_CONNECTIONS_PER_DEVICE);
2091 cJSON_Delete(jsonStatus);
2096 int writeSkeletonConfigFile()
2098 cJSON *configObject = cJSON_CreateObject();
2099 if (configObject == NULL)
2101 printf("Could not create JSON object: configObject\n");
2102 return SR_ERR_OPERATION_FAILED;
2105 cJSON *notificationConfig = cJSON_CreateObject();
2106 if (notificationConfig == NULL)
2108 printf("Could not create JSON object: notificationConfig\n");
2109 cJSON_Delete(configObject);
2110 return SR_ERR_OPERATION_FAILED;
2112 cJSON_AddItemToObject(configObject, "notification-config", notificationConfig);
2114 if (cJSON_AddNumberToObject(notificationConfig, "ves-heartbeat-period", 0) == NULL)
2116 printf("Could not create JSON object: ves-heartbeat-period\n");
2117 cJSON_Delete(configObject);
2118 return SR_ERR_OPERATION_FAILED;
2121 if (cJSON_AddTrueToObject(notificationConfig, "is-netconf-available") == NULL)
2123 printf("Could not create JSON object: is-netconf-available\n");
2124 cJSON_Delete(configObject);
2125 return SR_ERR_OPERATION_FAILED;
2128 if (cJSON_AddTrueToObject(notificationConfig, "is-ves-available") == NULL)
2130 printf("Could not create JSON object: is-ves-available\n");
2131 cJSON_Delete(configObject);
2132 return SR_ERR_OPERATION_FAILED;
2135 cJSON *faultNotificationDelayPeriod = cJSON_CreateArray();
2136 if (faultNotificationDelayPeriod == NULL)
2138 printf("Could not create JSON object: faultNotificationDelayPeriod\n");
2139 cJSON_Delete(configObject);
2140 return SR_ERR_OPERATION_FAILED;
2142 cJSON_AddItemToObject(notificationConfig, "fault-notification-delay-period", faultNotificationDelayPeriod);
2144 cJSON *arrayItem = cJSON_CreateNumber(0);
2145 if (arrayItem == NULL)
2147 printf("Could not create JSON object: arrayItem\n");
2148 cJSON_Delete(configObject);
2149 return SR_ERR_OPERATION_FAILED;
2151 cJSON_AddItemToArray(faultNotificationDelayPeriod, arrayItem);
2153 cJSON *vesEndPointDetails = cJSON_CreateObject();
2154 if (vesEndPointDetails == NULL)
2156 printf("Could not create JSON object: vesEndPointDetails\n");
2157 cJSON_Delete(configObject);
2158 return SR_ERR_OPERATION_FAILED;
2160 cJSON_AddItemToObject(configObject, "ves-endpoint-details", vesEndPointDetails);
2162 if (cJSON_AddStringToObject(vesEndPointDetails, "ves-endpoint-ip", "172.17.0.1") == NULL)
2164 printf("Could not create JSON object: ves-endpoint-ip\n");
2165 cJSON_Delete(configObject);
2166 return SR_ERR_OPERATION_FAILED;
2169 if (cJSON_AddNumberToObject(vesEndPointDetails, "ves-endpoint-port", 30007) == NULL)
2171 printf("Could not create JSON object: ves-endpoint-port\n");
2172 cJSON_Delete(configObject);
2173 return SR_ERR_OPERATION_FAILED;
2176 if (cJSON_AddStringToObject(vesEndPointDetails, "ves-endpoint-auth-method", "no-auth") == NULL)
2178 printf("Could not create JSON object: ves-endpoint-auth-method\n");
2179 cJSON_Delete(configObject);
2180 return SR_ERR_OPERATION_FAILED;
2183 if (cJSON_AddStringToObject(vesEndPointDetails, "ves-endpoint-username", "") == NULL)
2185 printf("Could not create JSON object: ves-endpoint-username\n");
2186 cJSON_Delete(configObject);
2187 return SR_ERR_OPERATION_FAILED;
2190 if (cJSON_AddStringToObject(vesEndPointDetails, "ves-endpoint-password", "") == NULL)
2192 printf("Could not create JSON object: ves-endpoint-password\n");
2193 cJSON_Delete(configObject);
2194 return SR_ERR_OPERATION_FAILED;
2197 if (cJSON_AddStringToObject(vesEndPointDetails, "ves-endpoint-certificate", "") == NULL)
2199 printf("Could not create JSON object: ves-endpoint-certificate\n");
2200 cJSON_Delete(configObject);
2201 return SR_ERR_OPERATION_FAILED;
2204 if (cJSON_AddFalseToObject(vesEndPointDetails, "ves-registration") == NULL)
2206 printf("Could not create JSON object: ves-registration\n");
2207 cJSON_Delete(configObject);
2208 return SR_ERR_OPERATION_FAILED;
2211 cJSON *controllerDetails = cJSON_CreateObject();
2212 if (controllerDetails == NULL)
2214 printf("Could not create JSON object: controllerDetails\n");
2215 cJSON_Delete(configObject);
2216 return SR_ERR_OPERATION_FAILED;
2218 cJSON_AddItemToObject(configObject, "controller-details", controllerDetails);
2220 if (cJSON_AddStringToObject(controllerDetails, "controller-ip", "172.17.0.1") == NULL)
2222 printf("Could not create JSON object: controller-ip\n");
2223 cJSON_Delete(configObject);
2224 return SR_ERR_OPERATION_FAILED;
2227 if (cJSON_AddNumberToObject(controllerDetails, "controller-port", 8181) == NULL)
2229 printf("Could not create JSON object: controller-port\n");
2230 cJSON_Delete(configObject);
2231 return SR_ERR_OPERATION_FAILED;
2234 if (cJSON_AddNumberToObject(controllerDetails, "netconf-call-home-port", 6666) == NULL)
2236 printf("Could not create JSON object: netconf-call-home-port\n");
2237 cJSON_Delete(configObject);
2238 return SR_ERR_OPERATION_FAILED;
2241 if (cJSON_AddStringToObject(controllerDetails, "controller-username", "admin") == NULL)
2243 printf("Could not create JSON object: controller-username\n");
2244 cJSON_Delete(configObject);
2245 return SR_ERR_OPERATION_FAILED;
2248 if (cJSON_AddStringToObject(controllerDetails, "controller-password", "admin") == NULL)
2250 printf("Could not create JSON object: controller-password\n");
2251 cJSON_Delete(configObject);
2252 return SR_ERR_OPERATION_FAILED;
2255 if (cJSON_AddNumberToObject(configObject, "ssh-connections", 1) == NULL)
2257 printf("Could not create JSON object: ssh-connections\n");
2258 cJSON_Delete(configObject);
2259 return SR_ERR_OPERATION_FAILED;
2262 if (cJSON_AddNumberToObject(configObject, "tls-connections", 0) == NULL)
2264 printf("Could not create JSON object: tls-connections\n");
2265 cJSON_Delete(configObject);
2266 return SR_ERR_OPERATION_FAILED;
2269 if (cJSON_AddFalseToObject(configObject, "netconf-call-home") == NULL)
2271 printf("Could not create JSON object: netconf-call-home\n");
2272 cJSON_Delete(configObject);
2273 return SR_ERR_OPERATION_FAILED;
2276 char *config_string = NULL;
2278 config_string = cJSON_PrintUnformatted(configObject);
2280 writeConfigFile(config_string);
2282 if (config_string != NULL)
2284 free(config_string);
2285 config_string = NULL;
2288 if (configObject != NULL)
2290 cJSON_Delete(configObject);
2296 int getIntFromString(char *string, int def_value)
2298 int rc, value = def_value;
2301 rc = sscanf(string, "%d", &value);
2304 printf("Could not get the %s! Using the default 0...\n", string);
2311 int getSshConnectionsFromConfigJson(void)
2313 char *stringConfig = readConfigFileInString();
2315 if (stringConfig == NULL)
2317 printf("Could not read JSON configuration file in string.");
2321 cJSON *jsonConfig = cJSON_Parse(stringConfig);
2322 if (jsonConfig == NULL)
2325 const char *error_ptr = cJSON_GetErrorPtr();
2326 if (error_ptr != NULL)
2328 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
2330 return SR_ERR_OPERATION_FAILED;
2332 //we don't need the string anymore
2334 stringConfig = NULL;
2336 cJSON *sshConnections = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ssh-connections");
2337 if (!cJSON_IsNumber(sshConnections))
2339 printf("Configuration JSON is not as expected: ssh-connections is not an object");
2340 cJSON_Delete(jsonConfig);
2341 return SR_ERR_OPERATION_FAILED;
2344 int num_of_ssh = (int)(sshConnections->valuedouble);
2346 cJSON_Delete(jsonConfig);
2351 int getTlsConnectionsFromConfigJson(void)
2353 char *stringConfig = readConfigFileInString();
2355 if (stringConfig == NULL)
2357 printf("Could not read JSON configuration file in string.");
2361 cJSON *jsonConfig = cJSON_Parse(stringConfig);
2362 if (jsonConfig == NULL)
2365 const char *error_ptr = cJSON_GetErrorPtr();
2366 if (error_ptr != NULL)
2368 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
2370 return SR_ERR_OPERATION_FAILED;
2372 //we don't need the string anymore
2374 stringConfig = NULL;
2376 cJSON *tlsConnections = cJSON_GetObjectItemCaseSensitive(jsonConfig, "tls-connections");
2377 if (!cJSON_IsNumber(tlsConnections))
2379 printf("Configuration JSON is not as expected: ssh-connections is not an object");
2380 cJSON_Delete(jsonConfig);
2381 return SR_ERR_OPERATION_FAILED;
2384 int num_of_tls = (int)(tlsConnections->valuedouble);
2386 cJSON_Delete(jsonConfig);