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 char ves_protocol[15] = "https";
214 if( !strcmp(getenv("VES_HTTPS"), "false") )
216 strcpy(ves_protocol ,"http" );
217 printf("VES connection protocol %s enabled\n", ves_protocol);
220 sprintf(url, "%s://%s:%d/eventListener/v7", ves_protocol, ves_ip, ves_port);
221 curl_easy_setopt(curl, CURLOPT_URL, url);
223 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
224 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
225 curl_easy_setopt(curl, CURLOPT_PROXY_SSL_VERIFYPEER, 0L);
226 curl_easy_setopt(curl, CURLOPT_PROXY_SSL_VERIFYHOST, 0L);
234 * Dynamically allocated memory;
235 * Caller needs to free the memory after it uses the value.
238 cJSON* vesCreateCommonEventHeader(char *domain, char *event_type, char *source_name, int seq_id)
240 char dateAndTime[50];
241 getCurrentDateAndTime(dateAndTime);
243 long useconds = getMicrosecondsSinceEpoch();
245 cJSON *commonEventHeader = cJSON_CreateObject();
246 if (commonEventHeader == NULL)
248 printf("Could not create JSON object: commonEventHeader\n");
252 if (cJSON_AddStringToObject(commonEventHeader, "domain", domain) == NULL)
254 printf("Could not create JSON object: domain\n");
255 cJSON_Delete(commonEventHeader);
260 sprintf(eventId, "%s_%s", source_name, dateAndTime);
262 if (cJSON_AddStringToObject(commonEventHeader, "eventId", eventId) == NULL)
264 printf("Could not create JSON object: eventId\n");
265 cJSON_Delete(commonEventHeader);
269 char event_name[200];
270 sprintf(event_name, "%s_%s", domain, event_type);
272 if (cJSON_AddStringToObject(commonEventHeader, "eventName", event_name) == NULL)
274 printf("Could not create JSON object: eventName\n");
275 cJSON_Delete(commonEventHeader);
279 if (cJSON_AddStringToObject(commonEventHeader, "eventType", event_type) == NULL)
281 printf("Could not create JSON object: eventType\n");
282 cJSON_Delete(commonEventHeader);
286 if (cJSON_AddNumberToObject(commonEventHeader, "sequence", (double)(seq_id)) == NULL)
288 printf("Could not create JSON object: sequence\n");
289 cJSON_Delete(commonEventHeader);
293 if (cJSON_AddStringToObject(commonEventHeader, "priority", "Low") == NULL)
295 printf("Could not create JSON object: priority\n");
296 cJSON_Delete(commonEventHeader);
300 if (cJSON_AddStringToObject(commonEventHeader, "reportingEntityId", "") == NULL)
302 printf("Could not create JSON object: reportingEntityId\n");
303 cJSON_Delete(commonEventHeader);
307 if (cJSON_AddStringToObject(commonEventHeader, "reportingEntityName", source_name) == NULL)
309 printf("Could not create JSON object: reportingEntityName\n");
310 cJSON_Delete(commonEventHeader);
314 if (cJSON_AddStringToObject(commonEventHeader, "sourceId", "") == NULL)
316 printf("Could not create JSON object: sourceId\n");
317 cJSON_Delete(commonEventHeader);
321 if (cJSON_AddStringToObject(commonEventHeader, "sourceName", source_name) == NULL)
323 printf("Could not create JSON object: sourceName\n");
324 cJSON_Delete(commonEventHeader);
328 if (cJSON_AddNumberToObject(commonEventHeader, "startEpochMicrosec", (double)(useconds)) == NULL)
330 printf("Could not create JSON object: startEpochMicrosec\n");
331 cJSON_Delete(commonEventHeader);
335 if (cJSON_AddNumberToObject(commonEventHeader, "lastEpochMicrosec", (double)(useconds)) == NULL)
337 printf("Could not create JSON object: lastEpochMicrosec\n");
338 cJSON_Delete(commonEventHeader);
342 if (cJSON_AddStringToObject(commonEventHeader, "nfNamingCode", "sdn controller") == NULL)
344 printf("Could not create JSON object: nfNamingCode\n");
345 cJSON_Delete(commonEventHeader);
349 if (cJSON_AddStringToObject(commonEventHeader, "nfVendorName", "sdn") == NULL)
351 printf("Could not create JSON object: nfVendorName\n");
352 cJSON_Delete(commonEventHeader);
356 if (cJSON_AddStringToObject(commonEventHeader, "timeZoneOffset", "+00:00") == NULL)
358 printf("Could not create JSON object: timeZoneOffset\n");
359 cJSON_Delete(commonEventHeader);
363 if (cJSON_AddStringToObject(commonEventHeader, "version", "4.0.1") == NULL)
365 printf("Could not create JSON object: version\n");
366 cJSON_Delete(commonEventHeader);
370 if (cJSON_AddStringToObject(commonEventHeader, "vesEventListenerVersion", "7.0.1") == NULL)
372 printf("Could not create JSON object: vesEventListenerVersion\n");
373 cJSON_Delete(commonEventHeader);
377 return commonEventHeader;
381 * Dynamically allocated memory;
382 * Caller needs to free the memory after it uses the value.
385 cJSON* vesCreateHeartbeatFields(int heartbeat_interval)
387 char dateAndTime[50];
388 getCurrentDateAndTime(dateAndTime);
390 cJSON *heartbeatFields = cJSON_CreateObject();
391 if (heartbeatFields == NULL)
393 printf("Could not create JSON object: heartbeatFields\n");
397 if (cJSON_AddStringToObject(heartbeatFields, "heartbeatFieldsVersion", "3.0") == NULL)
399 printf("Could not create JSON object: heartbeatFieldsVersion\n");
400 cJSON_Delete(heartbeatFields);
404 if (cJSON_AddNumberToObject(heartbeatFields, "heartbeatInterval", (double)(heartbeat_interval)) == NULL)
406 printf("Could not create JSON object: heartbeatInterval\n");
407 cJSON_Delete(heartbeatFields);
411 cJSON *additionalFields = cJSON_CreateObject();
412 if (additionalFields == NULL)
414 printf("Could not create JSON object: additionalFields\n");
415 cJSON_Delete(heartbeatFields);
418 cJSON_AddItemToObject(heartbeatFields, "additionalFields", additionalFields);
420 if (cJSON_AddStringToObject(additionalFields, "eventTime", dateAndTime) == NULL)
422 printf("Could not create JSON object: eventTime\n");
423 cJSON_Delete(heartbeatFields);
427 return heartbeatFields;
431 * Dynamically allocated memory;
432 * Caller needs to free the memory after it uses the value.
435 char* readConfigFileInString(void)
439 char config_file[200];
441 //sprintf(config_file, "%s/configuration.json", getenv("SCRIPTS_DIR"));
443 // --> recommended fix, Karl Koch, Deutsche Telekom AG, 22. 5. 2020.
444 // Path to config_file contains NULL when env variable is unset.
445 char *scripts_dir = getenv("SCRIPTS_DIR");
446 char *scripts_dir_default = "/opt/dev/ntsimulator/scripts";
448 if(NULL != scripts_dir)
450 sprintf(config_file, "%s/configuration.json", scripts_dir);
454 sprintf(config_file, "%s/configuration.json", scripts_dir_default);
455 printf("warning: opening config file in default path: <%s>\n",
460 FILE * f = fopen (config_file, "rb");
464 fseek (f, 0, SEEK_END);
466 fseek (f, 0, SEEK_SET);
467 buffer = malloc (length + 1);
470 fread (buffer, 1, length, f);
473 buffer[length] = '\0';
484 void writeConfigFile(char *config)
486 char config_file[200];
488 //sprintf(config_file, "%s/configuration.json", getenv("SCRIPTS_DIR"));
490 // --> recommended fix, Karl Koch, Deutsche Telekom AG, 22. 5. 2020.
491 // Path to config_file contains NULL when env variable is unset.
492 char *scripts_dir = getenv("SCRIPTS_DIR");
493 char *scripts_dir_default = "/opt/dev/ntsimulator/scripts";
495 if(NULL != scripts_dir)
497 sprintf(config_file, "%s/configuration.json", scripts_dir);
501 sprintf(config_file, "%s/configuration.json", scripts_dir_default);
502 printf("warning: opening config file in default path: <%s>\n",
507 FILE * f = fopen (config_file, "w");
516 printf("Could not write configuration file");
520 int getFaultNotificationDelayPeriodFromConfigJson(int *period_array, int *count)
522 char *stringConfig = readConfigFileInString();
524 if (stringConfig == NULL)
526 printf("Could not read JSON configuration file in string.");
530 cJSON *jsonConfig = cJSON_Parse(stringConfig);
531 if (jsonConfig == NULL)
534 const char *error_ptr = cJSON_GetErrorPtr();
535 if (error_ptr != NULL)
537 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
539 return SR_ERR_OPERATION_FAILED;
541 //we don't need the string anymore
545 cJSON *notifConfig = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
546 if (!cJSON_IsObject(notifConfig))
548 printf("Configuration JSON is not as expected: notification-config is not an object");
549 cJSON_Delete(jsonConfig);
550 return SR_ERR_OPERATION_FAILED;
553 cJSON *faultNotifDelay = cJSON_GetObjectItemCaseSensitive(notifConfig, "fault-notification-delay-period");
554 if (!cJSON_IsArray(faultNotifDelay))
556 printf("Configuration JSON is not as expected: fault-notification-delay-period is not an array.");
557 cJSON_Delete(jsonConfig);
558 return SR_ERR_OPERATION_FAILED;
561 cJSON *iterator = NULL;
565 cJSON_ArrayForEach(iterator, faultNotifDelay)
567 if (cJSON_IsNumber(iterator))
569 period_array[i++] = iterator->valueint;
573 printf("Invalid number in array!");
579 cJSON_Delete(jsonConfig);
584 int getVesHeartbeatPeriodFromConfigJson(void)
586 char *stringConfig = readConfigFileInString();
587 int vesHeartbeat = 0;
589 if (stringConfig == NULL)
591 printf("Could not read JSON configuration file in string.");
595 cJSON *jsonConfig = cJSON_Parse(stringConfig);
596 if (jsonConfig == NULL)
599 const char *error_ptr = cJSON_GetErrorPtr();
600 if (error_ptr != NULL)
602 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
604 return SR_ERR_OPERATION_FAILED;
606 //we don't need the string anymore
610 cJSON *notifConfig = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
611 if (!cJSON_IsObject(notifConfig))
613 printf("Configuration JSON is not as expected: notification-config is not an object");
614 cJSON_Delete(jsonConfig);
615 return SR_ERR_OPERATION_FAILED;
618 cJSON *vesHeartbeatPeriod = cJSON_GetObjectItemCaseSensitive(notifConfig, "ves-heartbeat-period");
619 if (!cJSON_IsNumber(vesHeartbeatPeriod))
621 printf("Configuration JSON is not as expected: ves-heartbeat-period is not a number");
622 cJSON_Delete(jsonConfig);
623 return SR_ERR_OPERATION_FAILED;
626 vesHeartbeat = (int)(vesHeartbeatPeriod->valuedouble);
628 cJSON_Delete(jsonConfig);
635 * Dynamically allocated memory;
636 * Caller needs to free the memory after it uses the value.
639 char* getVesAuthMethodFromConfigJson(void)
641 char *stringConfig = readConfigFileInString();
643 if (stringConfig == NULL)
645 printf("Could not read JSON configuration file in string.");
649 cJSON *jsonConfig = cJSON_Parse(stringConfig);
650 if (jsonConfig == NULL)
653 const char *error_ptr = cJSON_GetErrorPtr();
654 if (error_ptr != NULL)
656 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
660 //we don't need the string anymore
664 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
665 if (!cJSON_IsObject(vesDetails))
667 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
668 cJSON_Delete(jsonConfig);
672 cJSON *vesAuthMethod = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-auth-method");
673 if (!cJSON_IsString(vesAuthMethod))
675 printf("Configuration JSON is not as expected: ves-endpoint-auth-method is not an object");
676 cJSON_Delete(jsonConfig);
680 char *auth_method_string = strdup(cJSON_GetStringValue(vesAuthMethod));
682 cJSON_Delete(jsonConfig);
684 return auth_method_string;
688 * Dynamically allocated memory;
689 * Caller needs to free the memory after it uses the value.
692 char* getVesIpFromConfigJson(void)
694 char *stringConfig = readConfigFileInString();
696 if (stringConfig == NULL)
698 printf("Could not read JSON configuration file in string.");
702 cJSON *jsonConfig = cJSON_Parse(stringConfig);
703 if (jsonConfig == NULL)
706 const char *error_ptr = cJSON_GetErrorPtr();
707 if (error_ptr != NULL)
709 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
713 //we don't need the string anymore
717 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
718 if (!cJSON_IsObject(vesDetails))
720 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
721 cJSON_Delete(jsonConfig);
725 cJSON *vesIp = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-ip");
726 if (!cJSON_IsString(vesIp))
728 printf("Configuration JSON is not as expected: ves-endpoint-ip is not an object");
729 cJSON_Delete(jsonConfig);
733 char *ves_ip = strdup(cJSON_GetStringValue(vesIp));
735 cJSON_Delete(jsonConfig);
741 * Dynamically allocated memory;
742 * Caller needs to free the memory after it uses the value.
745 char* getVesUsernameFromConfigJson(void)
747 char *stringConfig = readConfigFileInString();
749 if (stringConfig == NULL)
751 printf("Could not read JSON configuration file in string.");
755 cJSON *jsonConfig = cJSON_Parse(stringConfig);
756 if (jsonConfig == NULL)
759 const char *error_ptr = cJSON_GetErrorPtr();
760 if (error_ptr != NULL)
762 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
766 //we don't need the string anymore
770 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
771 if (!cJSON_IsObject(vesDetails))
773 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
774 cJSON_Delete(jsonConfig);
778 cJSON *vesUsername = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-username");
779 if (!cJSON_IsString(vesUsername))
781 printf("Configuration JSON is not as expected: ves-endpoint-username is not an object");
782 cJSON_Delete(jsonConfig);
786 char *ves_username = strdup(cJSON_GetStringValue(vesUsername));
788 cJSON_Delete(jsonConfig);
794 * Dynamically allocated memory;
795 * Caller needs to free the memory after it uses the value.
798 char* getVesPasswordFromConfigJson(void)
800 char *stringConfig = readConfigFileInString();
802 if (stringConfig == NULL)
804 printf("Could not read JSON configuration file in string.");
808 cJSON *jsonConfig = cJSON_Parse(stringConfig);
809 if (jsonConfig == NULL)
812 const char *error_ptr = cJSON_GetErrorPtr();
813 if (error_ptr != NULL)
815 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
819 //we don't need the string anymore
823 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
824 if (!cJSON_IsObject(vesDetails))
826 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
827 cJSON_Delete(jsonConfig);
831 cJSON *vesPassword = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-password");
832 if (!cJSON_IsString(vesPassword))
834 printf("Configuration JSON is not as expected: ves-endpoint-password is not an object");
835 cJSON_Delete(jsonConfig);
839 char *ves_password = strdup(cJSON_GetStringValue(vesPassword));
841 cJSON_Delete(jsonConfig);
846 int getVesPortFromConfigJson(void)
848 char *stringConfig = readConfigFileInString();
850 if (stringConfig == NULL)
852 printf("Could not read JSON configuration file in string.");
856 cJSON *jsonConfig = cJSON_Parse(stringConfig);
857 if (jsonConfig == NULL)
860 const char *error_ptr = cJSON_GetErrorPtr();
861 if (error_ptr != NULL)
863 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
865 return SR_ERR_OPERATION_FAILED;
867 //we don't need the string anymore
871 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
872 if (!cJSON_IsObject(vesDetails))
874 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
875 cJSON_Delete(jsonConfig);
876 return SR_ERR_OPERATION_FAILED;
879 cJSON *vesPort = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-port");
880 if (!cJSON_IsNumber(vesPort))
882 printf("Configuration JSON is not as expected: ves-endpoint-port is not an object");
883 cJSON_Delete(jsonConfig);
884 return SR_ERR_OPERATION_FAILED;
887 int port = (int)(vesPort->valuedouble);
889 cJSON_Delete(jsonConfig);
894 int getVesRegistrationFromConfigJson(void)
896 char *stringConfig = readConfigFileInString();
898 if (stringConfig == NULL)
900 printf("Could not read JSON configuration file in string.");
904 cJSON *jsonConfig = cJSON_Parse(stringConfig);
905 if (jsonConfig == NULL)
908 const char *error_ptr = cJSON_GetErrorPtr();
909 if (error_ptr != NULL)
911 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
913 return SR_ERR_OPERATION_FAILED;
915 //we don't need the string anymore
919 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
920 if (!cJSON_IsObject(vesDetails))
922 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
923 cJSON_Delete(jsonConfig);
924 return SR_ERR_OPERATION_FAILED;
927 cJSON *vesReg = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-registration");
928 if (!cJSON_IsBool(vesReg))
930 printf("Configuration JSON is not as expected: ves-registration is not an object");
931 cJSON_Delete(jsonConfig);
932 return SR_ERR_OPERATION_FAILED;
935 int is_ves_reg = (cJSON_IsTrue(vesReg)) ? TRUE : FALSE;
937 cJSON_Delete(jsonConfig);
943 * Dynamically allocated memory;
944 * Caller needs to free the memory after it uses the value.
947 cJSON* vesCreatePnfRegistrationFields(int port, bool is_tls)
949 cJSON *pnfRegistrationFields = cJSON_CreateObject();
950 if (pnfRegistrationFields == NULL)
952 printf("Could not create JSON object: pnfRegistrationFields\n");
956 if (cJSON_AddStringToObject(pnfRegistrationFields, "pnfRegistrationFieldsVersion", "2.0") == NULL)
958 printf("Could not create JSON object: pnfRegistrationFieldsVersion\n");
959 cJSON_Delete(pnfRegistrationFields);
963 if (cJSON_AddStringToObject(pnfRegistrationFields, "lastServiceDate", "2019-08-16") == NULL)
965 printf("Could not create JSON object: lastServiceDate\n");
966 cJSON_Delete(pnfRegistrationFields);
971 generateRandomMacAddress(mac_addr);
973 if (cJSON_AddStringToObject(pnfRegistrationFields, "macAddress", mac_addr) == NULL)
975 printf("Could not create JSON object: macAddress\n");
976 cJSON_Delete(pnfRegistrationFields);
980 if (cJSON_AddStringToObject(pnfRegistrationFields, "manufactureDate", "2019-08-16") == NULL)
982 printf("Could not create JSON object: manufactureDate\n");
983 cJSON_Delete(pnfRegistrationFields);
987 if (cJSON_AddStringToObject(pnfRegistrationFields, "modelNumber", "Simulated Device Melacon") == NULL)
989 printf("Could not create JSON object: manufactureDate\n");
990 cJSON_Delete(pnfRegistrationFields);
994 if (cJSON_AddStringToObject(pnfRegistrationFields, "oamV4IpAddress", getenv("EXTERNAL_NTS_IP")) == NULL)
996 printf("Could not create JSON object: oamV4IpAddress\n");
997 cJSON_Delete(pnfRegistrationFields);
1001 if (cJSON_AddStringToObject(pnfRegistrationFields, "oamV6IpAddress", "0:0:0:0:0:ffff:a0a:011") == NULL)
1003 printf("Could not create JSON object: oamV6IpAddress\n");
1004 cJSON_Delete(pnfRegistrationFields);
1008 char serial_number[100];
1009 sprintf(serial_number, "%s-%s-%d-Simulated Device Melacon", getenv("HOSTNAME"), getenv("EXTERNAL_NTS_IP"), port);
1011 if (cJSON_AddStringToObject(pnfRegistrationFields, "serialNumber", serial_number) == NULL)
1013 printf("Could not create JSON object: serialNumber\n");
1014 cJSON_Delete(pnfRegistrationFields);
1018 if (cJSON_AddStringToObject(pnfRegistrationFields, "softwareVersion", "2.3.5") == NULL)
1020 printf("Could not create JSON object: softwareVersion\n");
1021 cJSON_Delete(pnfRegistrationFields);
1025 if (cJSON_AddStringToObject(pnfRegistrationFields, "unitFamily", "Simulated Device") == NULL)
1027 printf("Could not create JSON object: unitFamily\n");
1028 cJSON_Delete(pnfRegistrationFields);
1032 if (cJSON_AddStringToObject(pnfRegistrationFields, "unitType", "O-RAN-sim") == NULL)
1034 printf("Could not create JSON object: unitType\n");
1035 cJSON_Delete(pnfRegistrationFields);
1039 if (cJSON_AddStringToObject(pnfRegistrationFields, "vendorName", "Melacon") == NULL)
1041 printf("Could not create JSON object: vendorName\n");
1042 cJSON_Delete(pnfRegistrationFields);
1046 cJSON *additionalFields = cJSON_CreateObject();
1047 if (additionalFields == NULL)
1049 printf("Could not create JSON object: additionalFields\n");
1050 cJSON_Delete(pnfRegistrationFields);
1053 cJSON_AddItemToObject(pnfRegistrationFields, "additionalFields", additionalFields);
1055 char portString[10];
1056 sprintf(portString, "%d", port);
1058 if (cJSON_AddStringToObject(additionalFields, "oamPort", portString) == NULL)
1060 printf("Could not create JSON object: oamPort\n");
1061 cJSON_Delete(pnfRegistrationFields);
1067 //TLS specific configuration
1068 if (cJSON_AddStringToObject(additionalFields, "protocol", "TLS") == NULL)
1070 printf("Could not create JSON object: protocol\n");
1071 cJSON_Delete(pnfRegistrationFields);
1075 //TODO here we have the username from the docker container hardcoded: netconf
1076 if (cJSON_AddStringToObject(additionalFields, "username", "netconf") == NULL)
1078 printf("Could not create JSON object: username\n");
1079 cJSON_Delete(pnfRegistrationFields);
1083 if (cJSON_AddStringToObject(additionalFields, "keyId", "device-key") == NULL)
1085 printf("Could not create JSON object: keyId\n");
1086 cJSON_Delete(pnfRegistrationFields);
1092 //SSH specific configuration
1093 if (cJSON_AddStringToObject(additionalFields, "protocol", "SSH") == NULL)
1095 printf("Could not create JSON object: protocol\n");
1096 cJSON_Delete(pnfRegistrationFields);
1100 //TODO here we have the username from the docker container hardcoded: netconf
1101 if (cJSON_AddStringToObject(additionalFields, "username", "netconf") == NULL)
1103 printf("Could not create JSON object: username\n");
1104 cJSON_Delete(pnfRegistrationFields);
1108 //TODO here we have the password from the docker container hardcoded: netconf
1109 if (cJSON_AddStringToObject(additionalFields, "password", "netconf") == NULL)
1111 printf("Could not create JSON object: password\n");
1112 cJSON_Delete(pnfRegistrationFields);
1117 if (cJSON_AddStringToObject(additionalFields, "reconnectOnChangedSchema", "false") == NULL)
1119 printf("Could not create JSON object: reconnectOnChangedSchema\n");
1120 cJSON_Delete(pnfRegistrationFields);
1124 if (cJSON_AddStringToObject(additionalFields, "sleep-factor", "1.5") == NULL)
1126 printf("Could not create JSON object: sleep-factor\n");
1127 cJSON_Delete(pnfRegistrationFields);
1131 if (cJSON_AddStringToObject(additionalFields, "tcpOnly", "false") == NULL)
1133 printf("Could not create JSON object: tcpOnly\n");
1134 cJSON_Delete(pnfRegistrationFields);
1138 if (cJSON_AddStringToObject(additionalFields, "connectionTimeout", "20000") == NULL)
1140 printf("Could not create JSON object: connectionTimeout\n");
1141 cJSON_Delete(pnfRegistrationFields);
1145 if (cJSON_AddStringToObject(additionalFields, "maxConnectionAttempts", "100") == NULL)
1147 printf("Could not create JSON object: maxConnectionAttempts\n");
1148 cJSON_Delete(pnfRegistrationFields);
1152 if (cJSON_AddStringToObject(additionalFields, "betweenAttemptsTimeout", "2000") == NULL)
1154 printf("Could not create JSON object: betweenAttemptsTimeout\n");
1155 cJSON_Delete(pnfRegistrationFields);
1159 if (cJSON_AddStringToObject(additionalFields, "keepaliveDelay", "120") == NULL)
1161 printf("Could not create JSON object: keepaliveDelay\n");
1162 cJSON_Delete(pnfRegistrationFields);
1166 return pnfRegistrationFields;
1169 int getNetconfAvailableFromConfigJson(void)
1171 char *stringConfig = readConfigFileInString();
1173 if (stringConfig == NULL)
1175 printf("Could not read JSON configuration file in string.");
1179 cJSON *jsonConfig = cJSON_Parse(stringConfig);
1180 if (jsonConfig == NULL)
1183 const char *error_ptr = cJSON_GetErrorPtr();
1184 if (error_ptr != NULL)
1186 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
1188 return SR_ERR_OPERATION_FAILED;
1190 //we don't need the string anymore
1192 stringConfig = NULL;
1194 cJSON *notifDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
1195 if (!cJSON_IsObject(notifDetails))
1197 printf("Configuration JSON is not as expected: notification-config is not an object");
1198 cJSON_Delete(jsonConfig);
1199 return SR_ERR_OPERATION_FAILED;
1202 cJSON *isNetconfAvailable = cJSON_GetObjectItemCaseSensitive(notifDetails, "is-netconf-available");
1203 if (!cJSON_IsBool(isNetconfAvailable))
1205 printf("Configuration JSON is not as expected: is-netconf-available is not an object");
1206 cJSON_Delete(jsonConfig);
1207 return SR_ERR_OPERATION_FAILED;
1210 int is_netconf_available = (cJSON_IsTrue(isNetconfAvailable)) ? TRUE : FALSE;
1212 cJSON_Delete(jsonConfig);
1214 return is_netconf_available;
1217 int getVesAvailableFromConfigJson(void)
1219 char *stringConfig = readConfigFileInString();
1221 if (stringConfig == NULL)
1223 printf("Could not read JSON configuration file in string.");
1227 cJSON *jsonConfig = cJSON_Parse(stringConfig);
1228 if (jsonConfig == NULL)
1231 const char *error_ptr = cJSON_GetErrorPtr();
1232 if (error_ptr != NULL)
1234 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
1236 return SR_ERR_OPERATION_FAILED;
1238 //we don't need the string anymore
1240 stringConfig = NULL;
1242 cJSON *notifDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
1243 if (!cJSON_IsObject(notifDetails))
1245 printf("Configuration JSON is not as expected: notification-config is not an object");
1246 cJSON_Delete(jsonConfig);
1247 return SR_ERR_OPERATION_FAILED;
1250 cJSON *isVesAvailable = cJSON_GetObjectItemCaseSensitive(notifDetails, "is-ves-available");
1251 if (!cJSON_IsBool(isVesAvailable))
1253 printf("Configuration JSON is not as expected: is-ves-available is not an object");
1254 cJSON_Delete(jsonConfig);
1255 return SR_ERR_OPERATION_FAILED;
1258 int is_netconf_available = (cJSON_IsTrue(isVesAvailable)) ? TRUE : FALSE;
1260 cJSON_Delete(jsonConfig);
1262 return is_netconf_available;
1266 * Dynamically allocated memory;
1267 * Caller needs to free the memory after it uses the value.
1270 cJSON* vesCreateFaultFields(char *alarm_condition, char *alarm_object, char *severity, char *date_time, char *specific_problem)
1272 cJSON *faultFields = cJSON_CreateObject();
1273 if (faultFields == NULL)
1275 printf("Could not create JSON object: faultFields\n");
1279 if (cJSON_AddStringToObject(faultFields, "faultFieldsVersion", "4.0") == NULL)
1281 printf("Could not create JSON object: faultFieldsVersion\n");
1282 cJSON_Delete(faultFields);
1286 if (cJSON_AddStringToObject(faultFields, "alarmCondition", alarm_condition) == NULL)
1288 printf("Could not create JSON object: alarmCondition\n");
1289 cJSON_Delete(faultFields);
1293 if (cJSON_AddStringToObject(faultFields, "alarmInterfaceA", alarm_object) == NULL)
1295 printf("Could not create JSON object: alarmInterfaceA\n");
1296 cJSON_Delete(faultFields);
1300 if (cJSON_AddStringToObject(faultFields, "eventSourceType", "O_RAN_COMPONENT") == NULL)
1302 printf("Could not create JSON object: eventSourceType\n");
1303 cJSON_Delete(faultFields);
1307 if (cJSON_AddStringToObject(faultFields, "specificProblem", specific_problem) == NULL)
1309 printf("Could not create JSON object: specificProblem\n");
1310 cJSON_Delete(faultFields);
1314 if (cJSON_AddStringToObject(faultFields, "eventSeverity", severity) == NULL)
1316 printf("Could not create JSON object: eventSeverity\n");
1317 cJSON_Delete(faultFields);
1321 if (cJSON_AddStringToObject(faultFields, "vfStatus", "Active") == NULL)
1323 printf("Could not create JSON object: vfStatus\n");
1324 cJSON_Delete(faultFields);
1328 cJSON *alarmAdditionalInformation = cJSON_CreateObject();
1329 if (alarmAdditionalInformation == NULL)
1331 printf("Could not create JSON object: alarmAdditionalInformation\n");
1332 cJSON_Delete(faultFields);
1335 cJSON_AddItemToObject(faultFields, "alarmAdditionalInformation", alarmAdditionalInformation);
1337 if (cJSON_AddStringToObject(alarmAdditionalInformation, "eventTime", date_time) == NULL)
1339 printf("Could not create JSON object: eventTime\n");
1340 cJSON_Delete(faultFields);
1344 if (cJSON_AddStringToObject(alarmAdditionalInformation, "equipType", "O-RAN-sim") == NULL)
1346 printf("Could not create JSON object: equipType\n");
1347 cJSON_Delete(faultFields);
1351 if (cJSON_AddStringToObject(alarmAdditionalInformation, "vendor", "Melacon") == NULL)
1353 printf("Could not create JSON object: vendor\n");
1354 cJSON_Delete(faultFields);
1358 if (cJSON_AddStringToObject(alarmAdditionalInformation, "model", "Simulated Device") == NULL)
1360 printf("Could not create JSON object: model\n");
1361 cJSON_Delete(faultFields);
1368 static cJSON* createSeverityCounters(counterAlarms count)
1370 cJSON *severityCounters = cJSON_CreateObject();
1371 if (severityCounters == NULL)
1373 printf("Could not create JSON object: severityCounters\n");
1377 if (cJSON_AddNumberToObject(severityCounters, "severity-normal", count.normal) == NULL)
1379 printf("Could not create JSON object: severity-normal\n");
1383 if (cJSON_AddNumberToObject(severityCounters, "severity-warning", count.warning) == NULL)
1385 printf("Could not create JSON object: severity-warning\n");
1389 if (cJSON_AddNumberToObject(severityCounters, "severity-minor", count.minor) == NULL)
1391 printf("Could not create JSON object: severity-minor\n");
1395 if (cJSON_AddNumberToObject(severityCounters, "severity-major", count.major) == NULL)
1397 printf("Could not create JSON object: severity-major\n");
1401 if (cJSON_AddNumberToObject(severityCounters, "severity-critical", count.critical) == NULL)
1403 printf("Could not create JSON object: severity-critical\n");
1407 return severityCounters;
1410 void writeStatusFile(char *status)
1412 char status_file[200];
1413 sprintf(status_file, "%s/status.json", getenv("SCRIPTS_DIR"));
1414 FILE * f = fopen (status_file, "w");
1423 printf("Could not write status file!\n");
1427 int writeSkeletonStatusFile()
1429 cJSON *statusObject = cJSON_CreateObject();
1430 if (statusObject == NULL)
1432 printf("Could not create JSON object: statusObject\n");
1433 return SR_ERR_OPERATION_FAILED;
1436 // counterAlarms counter = {
1444 // cJSON *totalVesNotifications = createSeverityCounters(counter);
1445 // if (totalVesNotifications == NULL)
1447 // printf("Could not create JSON object: totalVesNotifications\n");
1448 // cJSON_Delete(statusObject);
1449 // return SR_ERR_OPERATION_FAILED;
1451 // cJSON_AddItemToObject(statusObject, "total-ves-notifications-sent", totalVesNotifications);
1453 // cJSON *totalNetconfNotifications = createSeverityCounters(counter);
1454 // if (totalNetconfNotifications == NULL)
1456 // printf("Could not create JSON object: totalNetconfNotifications\n");
1457 // cJSON_Delete(statusObject);
1458 // return SR_ERR_OPERATION_FAILED;
1460 // cJSON_AddItemToObject(statusObject, "total-netconf-notifications-sent", totalNetconfNotifications);
1462 cJSON *deviceList = cJSON_CreateArray();
1463 if (deviceList == NULL)
1465 printf("Could not create JSON object: deviceList\n");
1466 cJSON_Delete(statusObject);
1467 return SR_ERR_OPERATION_FAILED;
1469 cJSON_AddItemToObject(statusObject, "device-list", deviceList);
1471 char *status_string = NULL;
1473 status_string = cJSON_PrintUnformatted(statusObject);
1475 writeStatusFile(status_string);
1477 if (status_string != NULL)
1479 free(status_string);
1480 status_string = NULL;
1483 if (statusObject != NULL)
1485 cJSON_Delete(statusObject);
1492 * Dynamically allocated memory;
1493 * Caller needs to free the memory after it uses the value.
1496 char* readStatusFileInString(void)
1500 char config_file[200];
1501 sprintf(config_file, "%s/status.json", getenv("SCRIPTS_DIR"));
1502 FILE * f = fopen (config_file, "rb");
1506 fseek (f, 0, SEEK_END);
1508 fseek (f, 0, SEEK_SET);
1509 buffer = malloc (length + 1);
1512 fread (buffer, 1, length, f);
1515 buffer[length] = '\0';
1527 * Dynamically allocated memory;
1528 * Caller needs to free the memory after it uses the value.
1531 cJSON* getDeviceListFromStatusFile(void)
1533 char *stringStatus = readStatusFileInString();
1535 if (stringStatus == NULL)
1537 printf("Could not read status file!\n");
1541 cJSON *jsonStatus = cJSON_Parse(stringStatus);
1542 if (jsonStatus == NULL)
1545 const char *error_ptr = cJSON_GetErrorPtr();
1546 if (error_ptr != NULL)
1548 fprintf(stderr, "Could not parse JSON status! Error before: %s\n", error_ptr);
1552 //we don't need the string anymore
1554 stringStatus = NULL;
1559 cJSON* createDeviceListEntry(counterAlarms ves_counter, counterAlarms netconf_counter)
1561 cJSON *deviceListEntry = cJSON_CreateObject();
1562 if (deviceListEntry == NULL)
1564 printf("Could not create JSON object: deviceListEntry\n");
1569 sprintf(hostname, "%s", getenv("HOSTNAME"));
1571 if (cJSON_AddStringToObject(deviceListEntry, "device-name", hostname) == NULL)
1573 printf("Could not create JSON object: device-name\n");
1574 cJSON_Delete(deviceListEntry);
1578 cJSON *vesNotificationsSent = createSeverityCounters(ves_counter);
1579 if (vesNotificationsSent == NULL)
1581 printf("Could not create JSON object: vesNotificationsSent\n");
1582 cJSON_Delete(deviceListEntry);
1585 cJSON_AddItemToObject(deviceListEntry, "ves-notifications-sent", vesNotificationsSent);
1587 cJSON *netconfNotificationsSent = createSeverityCounters(netconf_counter);
1588 if (netconfNotificationsSent == NULL)
1590 printf("Could not create JSON object: netconfNotificationsSent\n");
1591 cJSON_Delete(deviceListEntry);
1594 cJSON_AddItemToObject(deviceListEntry, "netconf-notifications-sent", netconfNotificationsSent);
1596 return deviceListEntry;
1599 static void modifySeverityCounters(cJSON **severityCounters, counterAlarms count)
1601 cJSON *severity= cJSON_GetObjectItemCaseSensitive(*severityCounters, "severity-normal");
1602 if (!cJSON_IsNumber(severity))
1604 printf("Status JSON is not as expected: severity-normal is not an number");
1607 //we set the value of the severity-normal object
1608 cJSON_SetNumberValue(severity, count.normal);
1610 severity= cJSON_GetObjectItemCaseSensitive(*severityCounters, "severity-warning");
1611 if (!cJSON_IsNumber(severity))
1613 printf("Status JSON is not as expected: severity-warning is not an number");
1616 //we set the value of the severity-warning object
1617 cJSON_SetNumberValue(severity, count.warning);
1619 severity= cJSON_GetObjectItemCaseSensitive(*severityCounters, "severity-minor");
1620 if (!cJSON_IsNumber(severity))
1622 printf("Status JSON is not as expected: severity-minor is not an number");
1625 //we set the value of the severity-minor object
1626 cJSON_SetNumberValue(severity, count.minor);
1628 severity= cJSON_GetObjectItemCaseSensitive(*severityCounters, "severity-major");
1629 if (!cJSON_IsNumber(severity))
1631 printf("Status JSON is not as expected: severity-major is not an number");
1634 //we set the value of the severity-major object
1635 cJSON_SetNumberValue(severity, count.major);
1637 severity= cJSON_GetObjectItemCaseSensitive(*severityCounters, "severity-critical");
1638 if (!cJSON_IsNumber(severity))
1640 printf("Status JSON is not as expected: severity-critical is not an number");
1643 //we set the value of the severity-critical object
1644 cJSON_SetNumberValue(severity, count.critical);
1649 static void modifyDeviceListEntry(cJSON **deviceListEntry, counterAlarms ves_counter, counterAlarms netconf_counter)
1651 cJSON *vesNotificationsSent= cJSON_GetObjectItemCaseSensitive(*deviceListEntry, "ves-notifications-sent");
1652 if (!cJSON_IsObject(vesNotificationsSent))
1654 printf("Status JSON is not as expected: ves-notifications-sent is not a object");
1658 modifySeverityCounters(&vesNotificationsSent, ves_counter);
1660 cJSON *netconfNotificationsSent= cJSON_GetObjectItemCaseSensitive(*deviceListEntry, "netconf-notifications-sent");
1661 if (!cJSON_IsObject(netconfNotificationsSent))
1663 printf("Status JSON is not as expected: netconf-notifications-sent is not a object");
1667 modifySeverityCounters(&netconfNotificationsSent, netconf_counter);
1670 int writeStatusNotificationCounters(counterAlarms ves_counter, counterAlarms netconf_counter)
1672 cJSON *jsonStatus = getDeviceListFromStatusFile();
1674 cJSON *deviceList = cJSON_GetObjectItemCaseSensitive(jsonStatus, "device-list");
1675 if (!cJSON_IsArray(deviceList))
1677 printf("Status JSON is not as expected: device-list is not an object");
1678 cJSON_Delete(jsonStatus);
1679 return SR_ERR_OPERATION_FAILED;
1682 int array_size = cJSON_GetArraySize(deviceList);
1685 for (int i=0; i<array_size; ++i)
1687 cJSON *deviceListEntry = cJSON_GetArrayItem(deviceList, i);
1689 sprintf(hostname, "%s", getenv("HOSTNAME"));
1691 cJSON *deviceName = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "device-name");
1692 if (!cJSON_IsString(deviceName))
1694 printf("Status JSON is not as expected: device-name is not a string.");
1695 cJSON_Delete(jsonStatus);
1696 return SR_ERR_OPERATION_FAILED;
1698 char *deviceNameString = cJSON_GetStringValue(deviceName);
1700 if (strcmp(hostname, deviceNameString) == 0)
1702 modifyDeviceListEntry(&deviceListEntry, ves_counter, netconf_counter);
1709 cJSON* deviceListEntry = createDeviceListEntry(ves_counter, netconf_counter);
1711 cJSON_AddItemToArray(deviceList, deviceListEntry);
1714 //writing the new JSON to the configuration file
1715 char *stringStatus = cJSON_PrintUnformatted(jsonStatus);
1716 writeStatusFile(stringStatus);
1718 if (stringStatus != NULL)
1721 stringStatus = NULL;
1724 if (jsonStatus != NULL)
1726 cJSON_Delete(jsonStatus);
1733 int removeDeviceEntryFromStatusFile(char *containerId)
1735 cJSON *jsonStatus = getDeviceListFromStatusFile();
1737 cJSON *deviceList = cJSON_GetObjectItemCaseSensitive(jsonStatus, "device-list");
1738 if (!cJSON_IsArray(deviceList))
1740 printf("Status JSON is not as expected: device-list is not an object");
1741 cJSON_Delete(jsonStatus);
1742 return SR_ERR_OPERATION_FAILED;
1745 int array_size = cJSON_GetArraySize(deviceList);
1746 int found = array_size;
1748 for (int i=0; i<array_size; ++i)
1750 cJSON *deviceListEntry = cJSON_GetArrayItem(deviceList, i);
1752 sprintf(hostname, "%s", getenv("HOSTNAME"));
1754 cJSON *deviceName = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "device-name");
1755 if (!cJSON_IsString(deviceName))
1757 printf("Status JSON is not as expected: device-name is not a string.");
1758 cJSON_Delete(jsonStatus);
1759 return SR_ERR_OPERATION_FAILED;
1761 char *deviceNameString = cJSON_GetStringValue(deviceName);
1763 if (strcmp(containerId, deviceNameString) == 0)
1770 if (found < array_size)
1772 cJSON_DeleteItemFromArray(deviceList, found);
1776 printf("Could not find status file entry for device with id=\"%s\"", containerId);
1779 //writing the new JSON to the configuration file
1780 char *stringStatus = cJSON_PrintUnformatted(jsonStatus);
1781 writeStatusFile(stringStatus);
1783 if (stringStatus != NULL)
1786 stringStatus = NULL;
1789 if (jsonStatus != NULL)
1791 cJSON_Delete(jsonStatus);
1797 int compute_notifications_count(counterAlarms *ves_counter, counterAlarms *netconf_counter)
1799 ves_counter->normal = ves_counter->warning = \
1800 ves_counter->minor = ves_counter->major = \
1801 ves_counter->critical = 0;
1802 netconf_counter->normal = netconf_counter->warning = \
1803 netconf_counter->minor = netconf_counter->major = \
1804 netconf_counter->critical = 0;
1806 cJSON *jsonStatus = getDeviceListFromStatusFile();
1808 cJSON *deviceList = cJSON_GetObjectItemCaseSensitive(jsonStatus, "device-list");
1809 if (!cJSON_IsArray(deviceList))
1811 printf("Status JSON is not as expected: device-list is not an object");
1812 cJSON_Delete(jsonStatus);
1813 return SR_ERR_OPERATION_FAILED;
1816 int array_size = cJSON_GetArraySize(deviceList);
1818 for (int i=0; i<array_size; ++i)
1820 cJSON *deviceListEntry = cJSON_GetArrayItem(deviceList, i);
1822 cJSON *vesNotifications = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "ves-notifications-sent");
1823 if (!cJSON_IsObject(vesNotifications))
1825 printf("Status JSON is not as expected: ves-notifications-sent is not an object.");
1826 cJSON_Delete(jsonStatus);
1827 return SR_ERR_OPERATION_FAILED;
1830 cJSON *severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-normal");
1831 if (!cJSON_IsNumber(severity))
1833 printf("Status JSON is not as expected: severity-normal is not a string.");
1834 cJSON_Delete(jsonStatus);
1835 return SR_ERR_OPERATION_FAILED;
1837 int counter = (int)(severity->valuedouble);
1838 ves_counter->normal += counter;
1840 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-warning");
1841 if (!cJSON_IsNumber(severity))
1843 printf("Status JSON is not as expected: severity-warning is not a string.");
1844 cJSON_Delete(jsonStatus);
1845 return SR_ERR_OPERATION_FAILED;
1847 counter = (int)(severity->valuedouble);
1848 ves_counter->warning += counter;
1850 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-minor");
1851 if (!cJSON_IsNumber(severity))
1853 printf("Status JSON is not as expected: severity-minor is not a string.");
1854 cJSON_Delete(jsonStatus);
1855 return SR_ERR_OPERATION_FAILED;
1857 counter = (int)(severity->valuedouble);
1858 ves_counter->minor += counter;
1860 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-major");
1861 if (!cJSON_IsNumber(severity))
1863 printf("Status JSON is not as expected: severity-major is not a string.");
1864 cJSON_Delete(jsonStatus);
1865 return SR_ERR_OPERATION_FAILED;
1867 counter = (int)(severity->valuedouble);
1868 ves_counter->major += counter;
1870 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-critical");
1871 if (!cJSON_IsNumber(severity))
1873 printf("Status JSON is not as expected: severity-critical is not a string.");
1874 cJSON_Delete(jsonStatus);
1875 return SR_ERR_OPERATION_FAILED;
1877 counter = (int)(severity->valuedouble);
1878 ves_counter->critical += counter;
1880 cJSON *netconfNotifications = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "netconf-notifications-sent");
1881 if (!cJSON_IsObject(netconfNotifications))
1883 printf("Status JSON is not as expected: netconf-notifications-sent is not an object.");
1884 cJSON_Delete(jsonStatus);
1885 return SR_ERR_OPERATION_FAILED;
1888 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-normal");
1889 if (!cJSON_IsNumber(severity))
1891 printf("Status JSON is not as expected: severity-normal is not a string.");
1892 cJSON_Delete(jsonStatus);
1893 return SR_ERR_OPERATION_FAILED;
1896 counter = (int)(severity->valuedouble);
1897 netconf_counter->normal += (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1899 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-warning");
1900 if (!cJSON_IsNumber(severity))
1902 printf("Status JSON is not as expected: severity-warning is not a string.");
1903 cJSON_Delete(jsonStatus);
1904 return SR_ERR_OPERATION_FAILED;
1906 counter = (int)(severity->valuedouble);
1907 netconf_counter->warning += (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1909 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-minor");
1910 if (!cJSON_IsNumber(severity))
1912 printf("Status JSON is not as expected: severity-minor is not a string.");
1913 cJSON_Delete(jsonStatus);
1914 return SR_ERR_OPERATION_FAILED;
1916 counter = (int)(severity->valuedouble);
1917 netconf_counter->minor += (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1919 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-major");
1920 if (!cJSON_IsNumber(severity))
1922 printf("Status JSON is not as expected: severity-major is not a string.");
1923 cJSON_Delete(jsonStatus);
1924 return SR_ERR_OPERATION_FAILED;
1926 counter = (int)(severity->valuedouble);
1927 netconf_counter->major += (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1929 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-critical");
1930 if (!cJSON_IsNumber(severity))
1932 printf("Status JSON is not as expected: severity-critical is not a string.");
1933 cJSON_Delete(jsonStatus);
1934 return SR_ERR_OPERATION_FAILED;
1936 counter = (int)(severity->valuedouble);
1937 netconf_counter->critical += (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1940 cJSON_Delete(jsonStatus);
1945 int getDeviceCounters(char *containerId, counterAlarms *ves_counter, counterAlarms *netconf_counter)
1947 cJSON *jsonStatus = getDeviceListFromStatusFile();
1949 cJSON *deviceList = cJSON_GetObjectItemCaseSensitive(jsonStatus, "device-list");
1950 if (!cJSON_IsArray(deviceList))
1952 printf("Status JSON is not as expected: device-list is not an object");
1953 cJSON_Delete(jsonStatus);
1954 return SR_ERR_OPERATION_FAILED;
1957 int array_size = cJSON_GetArraySize(deviceList);
1959 ves_counter->critical = ves_counter->major = ves_counter->minor = ves_counter->warning = ves_counter->normal = 0;
1960 netconf_counter->critical = netconf_counter->major = netconf_counter->minor = netconf_counter->warning = netconf_counter->normal = 0;
1962 for (int i=0; i<array_size; ++i)
1964 cJSON *deviceListEntry = cJSON_GetArrayItem(deviceList, i);
1966 cJSON *deviceName = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "device-name");
1967 if (!cJSON_IsString(deviceName))
1969 printf("Status JSON is not as expected: device-name is not a string.");
1970 cJSON_Delete(jsonStatus);
1971 return SR_ERR_OPERATION_FAILED;
1973 char *deviceNameString = cJSON_GetStringValue(deviceName);
1975 if (strcmp(deviceNameString, containerId) != 0)
1980 cJSON *vesNotifications = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "ves-notifications-sent");
1981 if (!cJSON_IsObject(vesNotifications))
1983 printf("Status JSON is not as expected: ves-notifications-sent is not an object.");
1984 cJSON_Delete(jsonStatus);
1985 return SR_ERR_OPERATION_FAILED;
1988 cJSON *severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-normal");
1989 if (!cJSON_IsNumber(severity))
1991 printf("Status JSON is not as expected: severity-normal is not a string.");
1992 cJSON_Delete(jsonStatus);
1993 return SR_ERR_OPERATION_FAILED;
1995 int counter = (int)(severity->valuedouble);
1996 ves_counter->normal = counter;
1998 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-warning");
1999 if (!cJSON_IsNumber(severity))
2001 printf("Status JSON is not as expected: severity-warning is not a string.");
2002 cJSON_Delete(jsonStatus);
2003 return SR_ERR_OPERATION_FAILED;
2005 counter = (int)(severity->valuedouble);
2006 ves_counter->warning = counter;
2008 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-minor");
2009 if (!cJSON_IsNumber(severity))
2011 printf("Status JSON is not as expected: severity-minor is not a string.");
2012 cJSON_Delete(jsonStatus);
2013 return SR_ERR_OPERATION_FAILED;
2015 counter = (int)(severity->valuedouble);
2016 ves_counter->minor = counter;
2018 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-major");
2019 if (!cJSON_IsNumber(severity))
2021 printf("Status JSON is not as expected: severity-major is not a string.");
2022 cJSON_Delete(jsonStatus);
2023 return SR_ERR_OPERATION_FAILED;
2025 counter = (int)(severity->valuedouble);
2026 ves_counter->major = counter;
2028 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-critical");
2029 if (!cJSON_IsNumber(severity))
2031 printf("Status JSON is not as expected: severity-critical is not a string.");
2032 cJSON_Delete(jsonStatus);
2033 return SR_ERR_OPERATION_FAILED;
2035 counter = (int)(severity->valuedouble);
2036 ves_counter->critical = counter;
2038 cJSON *netconfNotifications = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "netconf-notifications-sent");
2039 if (!cJSON_IsObject(netconfNotifications))
2041 printf("Status JSON is not as expected: netconf-notifications-sent is not an object.");
2042 cJSON_Delete(jsonStatus);
2043 return SR_ERR_OPERATION_FAILED;
2046 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-normal");
2047 if (!cJSON_IsNumber(severity))
2049 printf("Status JSON is not as expected: severity-normal is not a string.");
2050 cJSON_Delete(jsonStatus);
2051 return SR_ERR_OPERATION_FAILED;
2054 counter = (int)(severity->valuedouble);
2055 netconf_counter->normal = (counter * NETCONF_CONNECTIONS_PER_DEVICE);
2057 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-warning");
2058 if (!cJSON_IsNumber(severity))
2060 printf("Status JSON is not as expected: severity-warning is not a string.");
2061 cJSON_Delete(jsonStatus);
2062 return SR_ERR_OPERATION_FAILED;
2064 counter = (int)(severity->valuedouble);
2065 netconf_counter->warning = (counter * NETCONF_CONNECTIONS_PER_DEVICE);
2067 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-minor");
2068 if (!cJSON_IsNumber(severity))
2070 printf("Status JSON is not as expected: severity-minor is not a string.");
2071 cJSON_Delete(jsonStatus);
2072 return SR_ERR_OPERATION_FAILED;
2074 counter = (int)(severity->valuedouble);
2075 netconf_counter->minor = (counter * NETCONF_CONNECTIONS_PER_DEVICE);
2077 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-major");
2078 if (!cJSON_IsNumber(severity))
2080 printf("Status JSON is not as expected: severity-major is not a string.");
2081 cJSON_Delete(jsonStatus);
2082 return SR_ERR_OPERATION_FAILED;
2084 counter = (int)(severity->valuedouble);
2085 netconf_counter->major = (counter * NETCONF_CONNECTIONS_PER_DEVICE);
2087 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-critical");
2088 if (!cJSON_IsNumber(severity))
2090 printf("Status JSON is not as expected: severity-critical is not a string.");
2091 cJSON_Delete(jsonStatus);
2092 return SR_ERR_OPERATION_FAILED;
2094 counter = (int)(severity->valuedouble);
2095 netconf_counter->critical = (counter * NETCONF_CONNECTIONS_PER_DEVICE);
2098 cJSON_Delete(jsonStatus);
2103 int writeSkeletonConfigFile()
2105 cJSON *configObject = cJSON_CreateObject();
2106 if (configObject == NULL)
2108 printf("Could not create JSON object: configObject\n");
2109 return SR_ERR_OPERATION_FAILED;
2112 cJSON *notificationConfig = cJSON_CreateObject();
2113 if (notificationConfig == NULL)
2115 printf("Could not create JSON object: notificationConfig\n");
2116 cJSON_Delete(configObject);
2117 return SR_ERR_OPERATION_FAILED;
2119 cJSON_AddItemToObject(configObject, "notification-config", notificationConfig);
2121 if (cJSON_AddNumberToObject(notificationConfig, "ves-heartbeat-period", 0) == NULL)
2123 printf("Could not create JSON object: ves-heartbeat-period\n");
2124 cJSON_Delete(configObject);
2125 return SR_ERR_OPERATION_FAILED;
2128 if (cJSON_AddTrueToObject(notificationConfig, "is-netconf-available") == NULL)
2130 printf("Could not create JSON object: is-netconf-available\n");
2131 cJSON_Delete(configObject);
2132 return SR_ERR_OPERATION_FAILED;
2135 if (cJSON_AddTrueToObject(notificationConfig, "is-ves-available") == NULL)
2137 printf("Could not create JSON object: is-ves-available\n");
2138 cJSON_Delete(configObject);
2139 return SR_ERR_OPERATION_FAILED;
2142 cJSON *faultNotificationDelayPeriod = cJSON_CreateArray();
2143 if (faultNotificationDelayPeriod == NULL)
2145 printf("Could not create JSON object: faultNotificationDelayPeriod\n");
2146 cJSON_Delete(configObject);
2147 return SR_ERR_OPERATION_FAILED;
2149 cJSON_AddItemToObject(notificationConfig, "fault-notification-delay-period", faultNotificationDelayPeriod);
2151 cJSON *arrayItem = cJSON_CreateNumber(0);
2152 if (arrayItem == NULL)
2154 printf("Could not create JSON object: arrayItem\n");
2155 cJSON_Delete(configObject);
2156 return SR_ERR_OPERATION_FAILED;
2158 cJSON_AddItemToArray(faultNotificationDelayPeriod, arrayItem);
2160 cJSON *vesEndPointDetails = cJSON_CreateObject();
2161 if (vesEndPointDetails == NULL)
2163 printf("Could not create JSON object: vesEndPointDetails\n");
2164 cJSON_Delete(configObject);
2165 return SR_ERR_OPERATION_FAILED;
2167 cJSON_AddItemToObject(configObject, "ves-endpoint-details", vesEndPointDetails);
2169 if (cJSON_AddStringToObject(vesEndPointDetails, "ves-endpoint-ip", "172.17.0.1") == NULL)
2171 printf("Could not create JSON object: ves-endpoint-ip\n");
2172 cJSON_Delete(configObject);
2173 return SR_ERR_OPERATION_FAILED;
2176 if (cJSON_AddNumberToObject(vesEndPointDetails, "ves-endpoint-port", 30007) == NULL)
2178 printf("Could not create JSON object: ves-endpoint-port\n");
2179 cJSON_Delete(configObject);
2180 return SR_ERR_OPERATION_FAILED;
2183 if (cJSON_AddStringToObject(vesEndPointDetails, "ves-endpoint-auth-method", "no-auth") == NULL)
2185 printf("Could not create JSON object: ves-endpoint-auth-method\n");
2186 cJSON_Delete(configObject);
2187 return SR_ERR_OPERATION_FAILED;
2190 if (cJSON_AddStringToObject(vesEndPointDetails, "ves-endpoint-username", "") == NULL)
2192 printf("Could not create JSON object: ves-endpoint-username\n");
2193 cJSON_Delete(configObject);
2194 return SR_ERR_OPERATION_FAILED;
2197 if (cJSON_AddStringToObject(vesEndPointDetails, "ves-endpoint-password", "") == NULL)
2199 printf("Could not create JSON object: ves-endpoint-password\n");
2200 cJSON_Delete(configObject);
2201 return SR_ERR_OPERATION_FAILED;
2204 if (cJSON_AddStringToObject(vesEndPointDetails, "ves-endpoint-certificate", "") == NULL)
2206 printf("Could not create JSON object: ves-endpoint-certificate\n");
2207 cJSON_Delete(configObject);
2208 return SR_ERR_OPERATION_FAILED;
2211 if (cJSON_AddFalseToObject(vesEndPointDetails, "ves-registration") == NULL)
2213 printf("Could not create JSON object: ves-registration\n");
2214 cJSON_Delete(configObject);
2215 return SR_ERR_OPERATION_FAILED;
2218 cJSON *controllerDetails = cJSON_CreateObject();
2219 if (controllerDetails == NULL)
2221 printf("Could not create JSON object: controllerDetails\n");
2222 cJSON_Delete(configObject);
2223 return SR_ERR_OPERATION_FAILED;
2225 cJSON_AddItemToObject(configObject, "controller-details", controllerDetails);
2227 if (cJSON_AddStringToObject(controllerDetails, "controller-ip", "172.17.0.1") == NULL)
2229 printf("Could not create JSON object: controller-ip\n");
2230 cJSON_Delete(configObject);
2231 return SR_ERR_OPERATION_FAILED;
2234 if (cJSON_AddNumberToObject(controllerDetails, "controller-port", 8181) == NULL)
2236 printf("Could not create JSON object: controller-port\n");
2237 cJSON_Delete(configObject);
2238 return SR_ERR_OPERATION_FAILED;
2241 if (cJSON_AddNumberToObject(controllerDetails, "netconf-call-home-port", 6666) == NULL)
2243 printf("Could not create JSON object: netconf-call-home-port\n");
2244 cJSON_Delete(configObject);
2245 return SR_ERR_OPERATION_FAILED;
2248 if (cJSON_AddStringToObject(controllerDetails, "controller-username", "admin") == NULL)
2250 printf("Could not create JSON object: controller-username\n");
2251 cJSON_Delete(configObject);
2252 return SR_ERR_OPERATION_FAILED;
2255 if (cJSON_AddStringToObject(controllerDetails, "controller-password", "admin") == NULL)
2257 printf("Could not create JSON object: controller-password\n");
2258 cJSON_Delete(configObject);
2259 return SR_ERR_OPERATION_FAILED;
2262 if (cJSON_AddNumberToObject(configObject, "ssh-connections", 1) == NULL)
2264 printf("Could not create JSON object: ssh-connections\n");
2265 cJSON_Delete(configObject);
2266 return SR_ERR_OPERATION_FAILED;
2269 if (cJSON_AddNumberToObject(configObject, "tls-connections", 0) == NULL)
2271 printf("Could not create JSON object: tls-connections\n");
2272 cJSON_Delete(configObject);
2273 return SR_ERR_OPERATION_FAILED;
2276 if (cJSON_AddFalseToObject(configObject, "netconf-call-home") == NULL)
2278 printf("Could not create JSON object: netconf-call-home\n");
2279 cJSON_Delete(configObject);
2280 return SR_ERR_OPERATION_FAILED;
2283 char *config_string = NULL;
2285 config_string = cJSON_PrintUnformatted(configObject);
2287 writeConfigFile(config_string);
2289 if (config_string != NULL)
2291 free(config_string);
2292 config_string = NULL;
2295 if (configObject != NULL)
2297 cJSON_Delete(configObject);
2303 int getIntFromString(char *string, int def_value)
2305 int rc, value = def_value;
2308 rc = sscanf(string, "%d", &value);
2311 printf("Could not get the %s! Using the default 0...\n", string);
2318 int getSshConnectionsFromConfigJson(void)
2320 char *stringConfig = readConfigFileInString();
2322 if (stringConfig == NULL)
2324 printf("Could not read JSON configuration file in string.");
2328 cJSON *jsonConfig = cJSON_Parse(stringConfig);
2329 if (jsonConfig == NULL)
2332 const char *error_ptr = cJSON_GetErrorPtr();
2333 if (error_ptr != NULL)
2335 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
2337 return SR_ERR_OPERATION_FAILED;
2339 //we don't need the string anymore
2341 stringConfig = NULL;
2343 cJSON *sshConnections = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ssh-connections");
2344 if (!cJSON_IsNumber(sshConnections))
2346 printf("Configuration JSON is not as expected: ssh-connections is not an object");
2347 cJSON_Delete(jsonConfig);
2348 return SR_ERR_OPERATION_FAILED;
2351 int num_of_ssh = (int)(sshConnections->valuedouble);
2353 cJSON_Delete(jsonConfig);
2358 int getTlsConnectionsFromConfigJson(void)
2360 char *stringConfig = readConfigFileInString();
2362 if (stringConfig == NULL)
2364 printf("Could not read JSON configuration file in string.");
2368 cJSON *jsonConfig = cJSON_Parse(stringConfig);
2369 if (jsonConfig == NULL)
2372 const char *error_ptr = cJSON_GetErrorPtr();
2373 if (error_ptr != NULL)
2375 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
2377 return SR_ERR_OPERATION_FAILED;
2379 //we don't need the string anymore
2381 stringConfig = NULL;
2383 cJSON *tlsConnections = cJSON_GetObjectItemCaseSensitive(jsonConfig, "tls-connections");
2384 if (!cJSON_IsNumber(tlsConnections))
2386 printf("Configuration JSON is not as expected: ssh-connections is not an object");
2387 cJSON_Delete(jsonConfig);
2388 return SR_ERR_OPERATION_FAILED;
2391 int num_of_tls = (int)(tlsConnections->valuedouble);
2393 cJSON_Delete(jsonConfig);