1 /*************************************************************************
3 * Copyright 2019 highstreet technologies GmbH and others
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 ***************************************************************************/
26 void set_curl_common_info_ves(CURL *curl)
28 struct curl_slist *chunk = NULL;
29 chunk = curl_slist_append(chunk, "Content-Type: application/json");
30 chunk = curl_slist_append(chunk, "Accept: application/json");
32 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
34 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 2L); // seconds timeout for a connection
35 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5L); //seconds timeout for an operation
37 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
40 void getCurrentDateAndTime(char *date_and_time)
42 time_t t = time(NULL);
43 struct tm tm = *localtime(&t);
47 gettimeofday(&tv, NULL);
48 millisec = lrint(tv.tv_usec/1000.0); // Round to nearest millisec
50 { // Allow for rounding up to nearest second
55 sprintf(date_and_time, "%04d-%02d-%02dT%02d:%02d:%02d.%01dZ",
56 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
57 tm.tm_hour, tm.tm_min, tm.tm_sec, millisec/100);
62 void generateRandomMacAddress(char *mac_address)
64 long rand1, rand2, rand3, rand4, rand5, rand6;
65 rand1 = random_at_most(255);
66 rand2 = random_at_most(255);
67 rand3 = random_at_most(255);
68 rand4 = random_at_most(255);
69 rand5 = random_at_most(255);
70 rand6 = random_at_most(255);
72 sprintf(mac_address, "%02X:%02X:%02X:%02X:%02X:%02X", rand1, rand2, rand3, rand4, rand5, rand6);
77 long random_at_most(long max)
80 // max <= RAND_MAX < ULONG_MAX, so this is okay.
81 num_bins = (unsigned long) max + 1,
82 num_rand = (unsigned long) RAND_MAX + 1,
83 bin_size = num_rand / num_bins,
84 defect = num_rand % num_bins;
87 FILE* urandom = fopen("/dev/urandom", "r");
88 fread(&seed, sizeof(int), 1, urandom);
97 // This is carefully written not to overflow
98 while (num_rand - defect <= (unsigned long)x);
100 // Truncated division is intentional
104 int getSecondsFromLastQuarterInterval(void)
106 time_t t = time(NULL);
107 time_t t_past = time(NULL);
108 struct tm tm = *localtime(&t);
109 struct tm tm_15_min_ago = tm;
111 //round to the last quarter hour
112 tm_15_min_ago.tm_min -= (tm_15_min_ago.tm_min % 15);
113 tm_15_min_ago.tm_sec = 0;
115 t=mktime(&tm_15_min_ago);
118 double seconds = difftime(t_past, t);
123 int getSecondsFromLastDayInterval(void)
125 time_t t = time(NULL);
126 time_t t_past = time(NULL);
127 struct tm tm = *localtime(&t);
128 struct tm tm_day_ago = tm;
130 //round to the last quarter hour
131 tm_day_ago.tm_hour = 0;
132 tm_day_ago.tm_min = 0;
133 tm_day_ago.tm_sec = 0;
135 t=mktime(&tm_day_ago);
138 double seconds = difftime(t_past, t);
143 void getPreviousQuarterInterval(int number_of_intervals, char *date_and_time)
145 time_t t = time(NULL);
146 t -= 15 * 60 * number_of_intervals;
147 struct tm tm = *localtime(&t);
149 tm.tm_min -= (tm.tm_min % 15);
152 sprintf(date_and_time, "%04d-%02d-%02dT%02d:%02d:%02d.0Z",
153 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
154 tm.tm_hour, tm.tm_min, tm.tm_sec);
159 void getPreviousDayPmTimestamp(int number_of_intervals, char *date_and_time)
161 time_t t = time(NULL);
162 t -= 24 * 60 * 60 * number_of_intervals;
163 struct tm tm = *localtime(&t);
169 sprintf(date_and_time, "%04d-%02d-%02dT%02d:%02d:%02d.0Z",
170 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
171 tm.tm_hour, tm.tm_min, tm.tm_sec);
176 long int getMicrosecondsSinceEpoch(void)
178 time_t t = time(NULL);
182 gettimeofday(&tv, NULL);
183 useconds = t*1000 + tv.tv_usec; //add the microseconds to the seconds
188 //TODO need to implement other authentication methods as well, not only no-auth
189 void prepare_ves_message_curl(CURL *curl)
191 curl_easy_reset(curl);
192 set_curl_common_info_ves(curl);
194 char *ves_ip = getVesIpFromConfigJson();
195 int ves_port = getVesPortFromConfigJson();
198 sprintf(url, "http://%s:%d/eventListener/v7", ves_ip, ves_port);
199 curl_easy_setopt(curl, CURLOPT_URL, url);
207 * Dynamically allocated memory;
208 * Caller needs to free the memory after it uses the value.
211 cJSON* vesCreateCommonEventHeader(char *domain, char *event_type, char *source_name, int seq_id)
213 char dateAndTime[50];
214 getCurrentDateAndTime(dateAndTime);
216 long useconds = getMicrosecondsSinceEpoch();
218 cJSON *commonEventHeader = cJSON_CreateObject();
219 if (commonEventHeader == NULL)
221 printf("Could not create JSON object: commonEventHeader\n");
225 if (cJSON_AddStringToObject(commonEventHeader, "domain", domain) == NULL)
227 printf("Could not create JSON object: domain\n");
228 cJSON_Delete(commonEventHeader);
233 sprintf(eventId, "%s_%s", source_name, dateAndTime);
235 if (cJSON_AddStringToObject(commonEventHeader, "eventId", eventId) == NULL)
237 printf("Could not create JSON object: eventId\n");
238 cJSON_Delete(commonEventHeader);
242 char event_name[200];
243 sprintf(event_name, "%s_%s", domain, event_type);
245 if (cJSON_AddStringToObject(commonEventHeader, "eventName", event_name) == NULL)
247 printf("Could not create JSON object: eventName\n");
248 cJSON_Delete(commonEventHeader);
252 if (cJSON_AddStringToObject(commonEventHeader, "eventType", event_type) == NULL)
254 printf("Could not create JSON object: eventType\n");
255 cJSON_Delete(commonEventHeader);
259 if (cJSON_AddNumberToObject(commonEventHeader, "sequence", (double)(seq_id)) == NULL)
261 printf("Could not create JSON object: sequence\n");
262 cJSON_Delete(commonEventHeader);
266 if (cJSON_AddStringToObject(commonEventHeader, "priority", "Low") == NULL)
268 printf("Could not create JSON object: priority\n");
269 cJSON_Delete(commonEventHeader);
273 if (cJSON_AddStringToObject(commonEventHeader, "reportingEntityId", "") == NULL)
275 printf("Could not create JSON object: reportingEntityId\n");
276 cJSON_Delete(commonEventHeader);
280 if (cJSON_AddStringToObject(commonEventHeader, "reportingEntityName", source_name) == NULL)
282 printf("Could not create JSON object: reportingEntityName\n");
283 cJSON_Delete(commonEventHeader);
287 if (cJSON_AddStringToObject(commonEventHeader, "sourceId", "") == NULL)
289 printf("Could not create JSON object: sourceId\n");
290 cJSON_Delete(commonEventHeader);
294 if (cJSON_AddStringToObject(commonEventHeader, "sourceName", source_name) == NULL)
296 printf("Could not create JSON object: sourceName\n");
297 cJSON_Delete(commonEventHeader);
301 if (cJSON_AddNumberToObject(commonEventHeader, "startEpochMicrosec", (double)(useconds)) == NULL)
303 printf("Could not create JSON object: startEpochMicrosec\n");
304 cJSON_Delete(commonEventHeader);
308 if (cJSON_AddNumberToObject(commonEventHeader, "lastEpochMicrosec", (double)(useconds)) == NULL)
310 printf("Could not create JSON object: lastEpochMicrosec\n");
311 cJSON_Delete(commonEventHeader);
315 if (cJSON_AddStringToObject(commonEventHeader, "nfNamingCode", "sdn controller") == NULL)
317 printf("Could not create JSON object: nfNamingCode\n");
318 cJSON_Delete(commonEventHeader);
322 if (cJSON_AddStringToObject(commonEventHeader, "nfVendorName", "sdn") == NULL)
324 printf("Could not create JSON object: nfVendorName\n");
325 cJSON_Delete(commonEventHeader);
329 if (cJSON_AddStringToObject(commonEventHeader, "timeZoneOffset", "+00:00") == NULL)
331 printf("Could not create JSON object: timeZoneOffset\n");
332 cJSON_Delete(commonEventHeader);
336 if (cJSON_AddStringToObject(commonEventHeader, "version", "4.0.1") == NULL)
338 printf("Could not create JSON object: version\n");
339 cJSON_Delete(commonEventHeader);
343 if (cJSON_AddStringToObject(commonEventHeader, "vesEventListenerVersion", "7.0.1") == NULL)
345 printf("Could not create JSON object: vesEventListenerVersion\n");
346 cJSON_Delete(commonEventHeader);
350 return commonEventHeader;
354 * Dynamically allocated memory;
355 * Caller needs to free the memory after it uses the value.
358 cJSON* vesCreateHeartbeatFields(int heartbeat_interval)
360 char dateAndTime[50];
361 getCurrentDateAndTime(dateAndTime);
363 cJSON *heartbeatFields = cJSON_CreateObject();
364 if (heartbeatFields == NULL)
366 printf("Could not create JSON object: heartbeatFields\n");
370 if (cJSON_AddStringToObject(heartbeatFields, "heartbeatFieldsVersion", "3.0") == NULL)
372 printf("Could not create JSON object: heartbeatFieldsVersion\n");
373 cJSON_Delete(heartbeatFields);
377 if (cJSON_AddNumberToObject(heartbeatFields, "heartbeatInterval", (double)(heartbeat_interval)) == NULL)
379 printf("Could not create JSON object: heartbeatInterval\n");
380 cJSON_Delete(heartbeatFields);
384 cJSON *additionalFields = cJSON_CreateObject();
385 if (additionalFields == NULL)
387 printf("Could not create JSON object: additionalFields\n");
388 cJSON_Delete(heartbeatFields);
391 cJSON_AddItemToObject(heartbeatFields, "additionalFields", additionalFields);
393 if (cJSON_AddStringToObject(additionalFields, "eventTime", dateAndTime) == NULL)
395 printf("Could not create JSON object: eventTime\n");
396 cJSON_Delete(heartbeatFields);
400 return heartbeatFields;
404 * Dynamically allocated memory;
405 * Caller needs to free the memory after it uses the value.
408 char* readConfigFileInString(void)
412 char config_file[200];
414 //sprintf(config_file, "%s/configuration.json", getenv("SCRIPTS_DIR"));
416 // --> recommended fix, Karl Koch, Deutsche Telekom AG, 22. 5. 2020.
417 // Path to config_file contains NULL when env variable is unset.
418 char *scripts_dir = getenv("SCRIPTS_DIR");
419 char *scripts_dir_default = "/opt/dev/ntsimulator/scripts";
421 if(NULL != scripts_dir)
423 sprintf(config_file, "%s/configuration.json", scripts_dir);
427 sprintf(config_file, "%s/configuration.json", scripts_dir_default);
428 printf("warning: opening config file in default path: <%s>\n",
433 FILE * f = fopen (config_file, "rb");
437 fseek (f, 0, SEEK_END);
439 fseek (f, 0, SEEK_SET);
440 buffer = malloc (length + 1);
443 fread (buffer, 1, length, f);
446 buffer[length] = '\0';
457 void writeConfigFile(char *config)
459 char config_file[200];
461 //sprintf(config_file, "%s/configuration.json", getenv("SCRIPTS_DIR"));
463 // --> recommended fix, Karl Koch, Deutsche Telekom AG, 22. 5. 2020.
464 // Path to config_file contains NULL when env variable is unset.
465 char *scripts_dir = getenv("SCRIPTS_DIR");
466 char *scripts_dir_default = "/opt/dev/ntsimulator/scripts";
468 if(NULL != scripts_dir)
470 sprintf(config_file, "%s/configuration.json", scripts_dir);
474 sprintf(config_file, "%s/configuration.json", scripts_dir_default);
475 printf("warning: opening config file in default path: <%s>\n",
480 FILE * f = fopen (config_file, "w");
489 printf("Could not write configuration file");
493 int getFaultNotificationDelayPeriodFromConfigJson(int *period_array, int *count)
495 char *stringConfig = readConfigFileInString();
497 if (stringConfig == NULL)
499 printf("Could not read JSON configuration file in string.");
503 cJSON *jsonConfig = cJSON_Parse(stringConfig);
504 if (jsonConfig == NULL)
507 const char *error_ptr = cJSON_GetErrorPtr();
508 if (error_ptr != NULL)
510 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
512 return SR_ERR_OPERATION_FAILED;
514 //we don't need the string anymore
518 cJSON *notifConfig = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
519 if (!cJSON_IsObject(notifConfig))
521 printf("Configuration JSON is not as expected: notification-config is not an object");
522 cJSON_Delete(jsonConfig);
523 return SR_ERR_OPERATION_FAILED;
526 cJSON *faultNotifDelay = cJSON_GetObjectItemCaseSensitive(notifConfig, "fault-notification-delay-period");
527 if (!cJSON_IsArray(faultNotifDelay))
529 printf("Configuration JSON is not as expected: fault-notification-delay-period is not an array.");
530 cJSON_Delete(jsonConfig);
531 return SR_ERR_OPERATION_FAILED;
534 cJSON *iterator = NULL;
538 cJSON_ArrayForEach(iterator, faultNotifDelay)
540 if (cJSON_IsNumber(iterator))
542 period_array[i++] = iterator->valueint;
546 printf("Invalid number in array!");
552 cJSON_Delete(jsonConfig);
557 int getVesHeartbeatPeriodFromConfigJson(void)
559 char *stringConfig = readConfigFileInString();
560 int vesHeartbeat = 0;
562 if (stringConfig == NULL)
564 printf("Could not read JSON configuration file in string.");
568 cJSON *jsonConfig = cJSON_Parse(stringConfig);
569 if (jsonConfig == NULL)
572 const char *error_ptr = cJSON_GetErrorPtr();
573 if (error_ptr != NULL)
575 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
577 return SR_ERR_OPERATION_FAILED;
579 //we don't need the string anymore
583 cJSON *notifConfig = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
584 if (!cJSON_IsObject(notifConfig))
586 printf("Configuration JSON is not as expected: notification-config is not an object");
587 cJSON_Delete(jsonConfig);
588 return SR_ERR_OPERATION_FAILED;
591 cJSON *vesHeartbeatPeriod = cJSON_GetObjectItemCaseSensitive(notifConfig, "ves-heartbeat-period");
592 if (!cJSON_IsNumber(vesHeartbeatPeriod))
594 printf("Configuration JSON is not as expected: ves-heartbeat-period is not a number");
595 cJSON_Delete(jsonConfig);
596 return SR_ERR_OPERATION_FAILED;
599 vesHeartbeat = (int)(vesHeartbeatPeriod->valuedouble);
601 cJSON_Delete(jsonConfig);
608 * Dynamically allocated memory;
609 * Caller needs to free the memory after it uses the value.
612 char* getVesAuthMethodFromConfigJson(void)
614 char *stringConfig = readConfigFileInString();
616 if (stringConfig == NULL)
618 printf("Could not read JSON configuration file in string.");
622 cJSON *jsonConfig = cJSON_Parse(stringConfig);
623 if (jsonConfig == NULL)
626 const char *error_ptr = cJSON_GetErrorPtr();
627 if (error_ptr != NULL)
629 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
633 //we don't need the string anymore
637 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
638 if (!cJSON_IsObject(vesDetails))
640 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
641 cJSON_Delete(jsonConfig);
645 cJSON *vesAuthMethod = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-auth-method");
646 if (!cJSON_IsString(vesAuthMethod))
648 printf("Configuration JSON is not as expected: ves-endpoint-auth-method is not an object");
649 cJSON_Delete(jsonConfig);
653 char *auth_method_string = strdup(cJSON_GetStringValue(vesAuthMethod));
655 cJSON_Delete(jsonConfig);
657 return auth_method_string;
661 * Dynamically allocated memory;
662 * Caller needs to free the memory after it uses the value.
665 char* getVesIpFromConfigJson(void)
667 char *stringConfig = readConfigFileInString();
669 if (stringConfig == NULL)
671 printf("Could not read JSON configuration file in string.");
675 cJSON *jsonConfig = cJSON_Parse(stringConfig);
676 if (jsonConfig == NULL)
679 const char *error_ptr = cJSON_GetErrorPtr();
680 if (error_ptr != NULL)
682 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
686 //we don't need the string anymore
690 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
691 if (!cJSON_IsObject(vesDetails))
693 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
694 cJSON_Delete(jsonConfig);
698 cJSON *vesIp = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-ip");
699 if (!cJSON_IsString(vesIp))
701 printf("Configuration JSON is not as expected: ves-endpoint-ip is not an object");
702 cJSON_Delete(jsonConfig);
706 char *ves_ip = strdup(cJSON_GetStringValue(vesIp));
708 cJSON_Delete(jsonConfig);
713 int getVesPortFromConfigJson(void)
715 char *stringConfig = readConfigFileInString();
717 if (stringConfig == NULL)
719 printf("Could not read JSON configuration file in string.");
723 cJSON *jsonConfig = cJSON_Parse(stringConfig);
724 if (jsonConfig == NULL)
727 const char *error_ptr = cJSON_GetErrorPtr();
728 if (error_ptr != NULL)
730 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
732 return SR_ERR_OPERATION_FAILED;
734 //we don't need the string anymore
738 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
739 if (!cJSON_IsObject(vesDetails))
741 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
742 cJSON_Delete(jsonConfig);
743 return SR_ERR_OPERATION_FAILED;
746 cJSON *vesPort = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-endpoint-port");
747 if (!cJSON_IsNumber(vesPort))
749 printf("Configuration JSON is not as expected: ves-endpoint-port is not an object");
750 cJSON_Delete(jsonConfig);
751 return SR_ERR_OPERATION_FAILED;
754 int port = (int)(vesPort->valuedouble);
756 cJSON_Delete(jsonConfig);
761 int getVesRegistrationFromConfigJson(void)
763 char *stringConfig = readConfigFileInString();
765 if (stringConfig == NULL)
767 printf("Could not read JSON configuration file in string.");
771 cJSON *jsonConfig = cJSON_Parse(stringConfig);
772 if (jsonConfig == NULL)
775 const char *error_ptr = cJSON_GetErrorPtr();
776 if (error_ptr != NULL)
778 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
780 return SR_ERR_OPERATION_FAILED;
782 //we don't need the string anymore
786 cJSON *vesDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ves-endpoint-details");
787 if (!cJSON_IsObject(vesDetails))
789 printf("Configuration JSON is not as expected: ves-endpoint-details is not an object");
790 cJSON_Delete(jsonConfig);
791 return SR_ERR_OPERATION_FAILED;
794 cJSON *vesReg = cJSON_GetObjectItemCaseSensitive(vesDetails, "ves-registration");
795 if (!cJSON_IsBool(vesReg))
797 printf("Configuration JSON is not as expected: ves-registration is not an object");
798 cJSON_Delete(jsonConfig);
799 return SR_ERR_OPERATION_FAILED;
802 int is_ves_reg = (cJSON_IsTrue(vesReg)) ? TRUE : FALSE;
804 cJSON_Delete(jsonConfig);
810 * Dynamically allocated memory;
811 * Caller needs to free the memory after it uses the value.
814 cJSON* vesCreatePnfRegistrationFields(int port, bool is_tls)
816 cJSON *pnfRegistrationFields = cJSON_CreateObject();
817 if (pnfRegistrationFields == NULL)
819 printf("Could not create JSON object: pnfRegistrationFields\n");
823 if (cJSON_AddStringToObject(pnfRegistrationFields, "pnfRegistrationFieldsVersion", "2.0") == NULL)
825 printf("Could not create JSON object: pnfRegistrationFieldsVersion\n");
826 cJSON_Delete(pnfRegistrationFields);
830 if (cJSON_AddStringToObject(pnfRegistrationFields, "lastServiceDate", "2019-08-16") == NULL)
832 printf("Could not create JSON object: lastServiceDate\n");
833 cJSON_Delete(pnfRegistrationFields);
838 generateRandomMacAddress(mac_addr);
840 if (cJSON_AddStringToObject(pnfRegistrationFields, "macAddress", mac_addr) == NULL)
842 printf("Could not create JSON object: macAddress\n");
843 cJSON_Delete(pnfRegistrationFields);
847 if (cJSON_AddStringToObject(pnfRegistrationFields, "manufactureDate", "2019-08-16") == NULL)
849 printf("Could not create JSON object: manufactureDate\n");
850 cJSON_Delete(pnfRegistrationFields);
854 if (cJSON_AddStringToObject(pnfRegistrationFields, "modelNumber", "Simulated Device Melacon") == NULL)
856 printf("Could not create JSON object: manufactureDate\n");
857 cJSON_Delete(pnfRegistrationFields);
861 if (cJSON_AddStringToObject(pnfRegistrationFields, "oamV4IpAddress", getenv("NTS_IP")) == NULL)
863 printf("Could not create JSON object: oamV4IpAddress\n");
864 cJSON_Delete(pnfRegistrationFields);
868 if (cJSON_AddStringToObject(pnfRegistrationFields, "oamV6IpAddress", "0:0:0:0:0:ffff:a0a:011") == NULL)
870 printf("Could not create JSON object: oamV6IpAddress\n");
871 cJSON_Delete(pnfRegistrationFields);
875 char serial_number[100];
876 sprintf(serial_number, "%s-%s-%d-Simulated Device Melacon", getenv("HOSTNAME"), getenv("NTS_IP"), port);
878 if (cJSON_AddStringToObject(pnfRegistrationFields, "serialNumber", serial_number) == NULL)
880 printf("Could not create JSON object: serialNumber\n");
881 cJSON_Delete(pnfRegistrationFields);
885 if (cJSON_AddStringToObject(pnfRegistrationFields, "softwareVersion", "2.3.5") == NULL)
887 printf("Could not create JSON object: softwareVersion\n");
888 cJSON_Delete(pnfRegistrationFields);
892 if (cJSON_AddStringToObject(pnfRegistrationFields, "unitFamily", "Simulated Device") == NULL)
894 printf("Could not create JSON object: unitFamily\n");
895 cJSON_Delete(pnfRegistrationFields);
899 if (cJSON_AddStringToObject(pnfRegistrationFields, "unitType", "O-RAN-sim") == NULL)
901 printf("Could not create JSON object: unitType\n");
902 cJSON_Delete(pnfRegistrationFields);
906 if (cJSON_AddStringToObject(pnfRegistrationFields, "vendorName", "Melacon") == NULL)
908 printf("Could not create JSON object: vendorName\n");
909 cJSON_Delete(pnfRegistrationFields);
913 cJSON *additionalFields = cJSON_CreateObject();
914 if (additionalFields == NULL)
916 printf("Could not create JSON object: additionalFields\n");
917 cJSON_Delete(pnfRegistrationFields);
920 cJSON_AddItemToObject(pnfRegistrationFields, "additionalFields", additionalFields);
923 sprintf(portString, "%d", port);
925 if (cJSON_AddStringToObject(additionalFields, "oamPort", portString) == NULL)
927 printf("Could not create JSON object: oamPort\n");
928 cJSON_Delete(pnfRegistrationFields);
934 //TLS specific configuration
935 if (cJSON_AddStringToObject(additionalFields, "protocol", "TLS") == NULL)
937 printf("Could not create JSON object: protocol\n");
938 cJSON_Delete(pnfRegistrationFields);
942 //TODO here we have the username from the docker container hardcoded: netconf
943 if (cJSON_AddStringToObject(additionalFields, "username", "netconf") == NULL)
945 printf("Could not create JSON object: username\n");
946 cJSON_Delete(pnfRegistrationFields);
950 if (cJSON_AddStringToObject(additionalFields, "keyId", "device-key") == NULL)
952 printf("Could not create JSON object: keyId\n");
953 cJSON_Delete(pnfRegistrationFields);
959 //SSH specific configuration
960 if (cJSON_AddStringToObject(additionalFields, "protocol", "SSH") == NULL)
962 printf("Could not create JSON object: protocol\n");
963 cJSON_Delete(pnfRegistrationFields);
967 //TODO here we have the username from the docker container hardcoded: netconf
968 if (cJSON_AddStringToObject(additionalFields, "username", "netconf") == NULL)
970 printf("Could not create JSON object: username\n");
971 cJSON_Delete(pnfRegistrationFields);
975 //TODO here we have the password from the docker container hardcoded: netconf
976 if (cJSON_AddStringToObject(additionalFields, "password", "netconf") == NULL)
978 printf("Could not create JSON object: password\n");
979 cJSON_Delete(pnfRegistrationFields);
984 if (cJSON_AddStringToObject(additionalFields, "reconnectOnChangedSchema", "false") == NULL)
986 printf("Could not create JSON object: reconnectOnChangedSchema\n");
987 cJSON_Delete(pnfRegistrationFields);
991 if (cJSON_AddStringToObject(additionalFields, "sleep-factor", "1.5") == NULL)
993 printf("Could not create JSON object: sleep-factor\n");
994 cJSON_Delete(pnfRegistrationFields);
998 if (cJSON_AddStringToObject(additionalFields, "tcpOnly", "false") == NULL)
1000 printf("Could not create JSON object: tcpOnly\n");
1001 cJSON_Delete(pnfRegistrationFields);
1005 if (cJSON_AddStringToObject(additionalFields, "connectionTimeout", "20000") == NULL)
1007 printf("Could not create JSON object: connectionTimeout\n");
1008 cJSON_Delete(pnfRegistrationFields);
1012 if (cJSON_AddStringToObject(additionalFields, "maxConnectionAttempts", "100") == NULL)
1014 printf("Could not create JSON object: maxConnectionAttempts\n");
1015 cJSON_Delete(pnfRegistrationFields);
1019 if (cJSON_AddStringToObject(additionalFields, "betweenAttemptsTimeout", "2000") == NULL)
1021 printf("Could not create JSON object: betweenAttemptsTimeout\n");
1022 cJSON_Delete(pnfRegistrationFields);
1026 if (cJSON_AddStringToObject(additionalFields, "keepaliveDelay", "120") == NULL)
1028 printf("Could not create JSON object: keepaliveDelay\n");
1029 cJSON_Delete(pnfRegistrationFields);
1033 return pnfRegistrationFields;
1036 int getNetconfAvailableFromConfigJson(void)
1038 char *stringConfig = readConfigFileInString();
1040 if (stringConfig == NULL)
1042 printf("Could not read JSON configuration file in string.");
1046 cJSON *jsonConfig = cJSON_Parse(stringConfig);
1047 if (jsonConfig == NULL)
1050 const char *error_ptr = cJSON_GetErrorPtr();
1051 if (error_ptr != NULL)
1053 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
1055 return SR_ERR_OPERATION_FAILED;
1057 //we don't need the string anymore
1059 stringConfig = NULL;
1061 cJSON *notifDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
1062 if (!cJSON_IsObject(notifDetails))
1064 printf("Configuration JSON is not as expected: notification-config is not an object");
1065 cJSON_Delete(jsonConfig);
1066 return SR_ERR_OPERATION_FAILED;
1069 cJSON *isNetconfAvailable = cJSON_GetObjectItemCaseSensitive(notifDetails, "is-netconf-available");
1070 if (!cJSON_IsBool(isNetconfAvailable))
1072 printf("Configuration JSON is not as expected: is-netconf-available is not an object");
1073 cJSON_Delete(jsonConfig);
1074 return SR_ERR_OPERATION_FAILED;
1077 int is_netconf_available = (cJSON_IsTrue(isNetconfAvailable)) ? TRUE : FALSE;
1079 cJSON_Delete(jsonConfig);
1081 return is_netconf_available;
1084 int getVesAvailableFromConfigJson(void)
1086 char *stringConfig = readConfigFileInString();
1088 if (stringConfig == NULL)
1090 printf("Could not read JSON configuration file in string.");
1094 cJSON *jsonConfig = cJSON_Parse(stringConfig);
1095 if (jsonConfig == NULL)
1098 const char *error_ptr = cJSON_GetErrorPtr();
1099 if (error_ptr != NULL)
1101 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
1103 return SR_ERR_OPERATION_FAILED;
1105 //we don't need the string anymore
1107 stringConfig = NULL;
1109 cJSON *notifDetails = cJSON_GetObjectItemCaseSensitive(jsonConfig, "notification-config");
1110 if (!cJSON_IsObject(notifDetails))
1112 printf("Configuration JSON is not as expected: notification-config is not an object");
1113 cJSON_Delete(jsonConfig);
1114 return SR_ERR_OPERATION_FAILED;
1117 cJSON *isVesAvailable = cJSON_GetObjectItemCaseSensitive(notifDetails, "is-ves-available");
1118 if (!cJSON_IsBool(isVesAvailable))
1120 printf("Configuration JSON is not as expected: is-ves-available is not an object");
1121 cJSON_Delete(jsonConfig);
1122 return SR_ERR_OPERATION_FAILED;
1125 int is_netconf_available = (cJSON_IsTrue(isVesAvailable)) ? TRUE : FALSE;
1127 cJSON_Delete(jsonConfig);
1129 return is_netconf_available;
1133 * Dynamically allocated memory;
1134 * Caller needs to free the memory after it uses the value.
1137 cJSON* vesCreateFaultFields(char *alarm_condition, char *alarm_object, char *severity, char *date_time, char *specific_problem)
1139 cJSON *faultFields = cJSON_CreateObject();
1140 if (faultFields == NULL)
1142 printf("Could not create JSON object: faultFields\n");
1146 if (cJSON_AddStringToObject(faultFields, "faultFieldsVersion", "4.0") == NULL)
1148 printf("Could not create JSON object: faultFieldsVersion\n");
1149 cJSON_Delete(faultFields);
1153 if (cJSON_AddStringToObject(faultFields, "alarmCondition", alarm_condition) == NULL)
1155 printf("Could not create JSON object: alarmCondition\n");
1156 cJSON_Delete(faultFields);
1160 if (cJSON_AddStringToObject(faultFields, "alarmInterfaceA", alarm_object) == NULL)
1162 printf("Could not create JSON object: alarmInterfaceA\n");
1163 cJSON_Delete(faultFields);
1167 if (cJSON_AddStringToObject(faultFields, "eventSourceType", "O_RAN_COMPONENT") == NULL)
1169 printf("Could not create JSON object: eventSourceType\n");
1170 cJSON_Delete(faultFields);
1174 if (cJSON_AddStringToObject(faultFields, "specificProblem", specific_problem) == NULL)
1176 printf("Could not create JSON object: specificProblem\n");
1177 cJSON_Delete(faultFields);
1181 if (cJSON_AddStringToObject(faultFields, "eventSeverity", severity) == NULL)
1183 printf("Could not create JSON object: eventSeverity\n");
1184 cJSON_Delete(faultFields);
1188 if (cJSON_AddStringToObject(faultFields, "vfStatus", "Active") == NULL)
1190 printf("Could not create JSON object: vfStatus\n");
1191 cJSON_Delete(faultFields);
1195 cJSON *alarmAdditionalInformation = cJSON_CreateObject();
1196 if (alarmAdditionalInformation == NULL)
1198 printf("Could not create JSON object: alarmAdditionalInformation\n");
1199 cJSON_Delete(faultFields);
1202 cJSON_AddItemToObject(faultFields, "alarmAdditionalInformation", alarmAdditionalInformation);
1204 if (cJSON_AddStringToObject(alarmAdditionalInformation, "eventTime", date_time) == NULL)
1206 printf("Could not create JSON object: eventTime\n");
1207 cJSON_Delete(faultFields);
1211 if (cJSON_AddStringToObject(alarmAdditionalInformation, "equipType", "O-RAN-sim") == NULL)
1213 printf("Could not create JSON object: equipType\n");
1214 cJSON_Delete(faultFields);
1218 if (cJSON_AddStringToObject(alarmAdditionalInformation, "vendor", "Melacon") == NULL)
1220 printf("Could not create JSON object: vendor\n");
1221 cJSON_Delete(faultFields);
1225 if (cJSON_AddStringToObject(alarmAdditionalInformation, "model", "Simulated Device") == NULL)
1227 printf("Could not create JSON object: model\n");
1228 cJSON_Delete(faultFields);
1235 static cJSON* createSeverityCounters(counterAlarms count)
1237 cJSON *severityCounters = cJSON_CreateObject();
1238 if (severityCounters == NULL)
1240 printf("Could not create JSON object: severityCounters\n");
1244 if (cJSON_AddNumberToObject(severityCounters, "severity-normal", count.normal) == NULL)
1246 printf("Could not create JSON object: severity-normal\n");
1250 if (cJSON_AddNumberToObject(severityCounters, "severity-warning", count.warning) == NULL)
1252 printf("Could not create JSON object: severity-warning\n");
1256 if (cJSON_AddNumberToObject(severityCounters, "severity-minor", count.minor) == NULL)
1258 printf("Could not create JSON object: severity-minor\n");
1262 if (cJSON_AddNumberToObject(severityCounters, "severity-major", count.major) == NULL)
1264 printf("Could not create JSON object: severity-major\n");
1268 if (cJSON_AddNumberToObject(severityCounters, "severity-critical", count.critical) == NULL)
1270 printf("Could not create JSON object: severity-critical\n");
1274 return severityCounters;
1277 void writeStatusFile(char *status)
1279 char status_file[200];
1280 sprintf(status_file, "%s/status.json", getenv("SCRIPTS_DIR"));
1281 FILE * f = fopen (status_file, "w");
1290 printf("Could not write status file!\n");
1294 int writeSkeletonStatusFile()
1296 cJSON *statusObject = cJSON_CreateObject();
1297 if (statusObject == NULL)
1299 printf("Could not create JSON object: statusObject\n");
1300 return SR_ERR_OPERATION_FAILED;
1303 // counterAlarms counter = {
1311 // cJSON *totalVesNotifications = createSeverityCounters(counter);
1312 // if (totalVesNotifications == NULL)
1314 // printf("Could not create JSON object: totalVesNotifications\n");
1315 // cJSON_Delete(statusObject);
1316 // return SR_ERR_OPERATION_FAILED;
1318 // cJSON_AddItemToObject(statusObject, "total-ves-notifications-sent", totalVesNotifications);
1320 // cJSON *totalNetconfNotifications = createSeverityCounters(counter);
1321 // if (totalNetconfNotifications == NULL)
1323 // printf("Could not create JSON object: totalNetconfNotifications\n");
1324 // cJSON_Delete(statusObject);
1325 // return SR_ERR_OPERATION_FAILED;
1327 // cJSON_AddItemToObject(statusObject, "total-netconf-notifications-sent", totalNetconfNotifications);
1329 cJSON *deviceList = cJSON_CreateArray();
1330 if (deviceList == NULL)
1332 printf("Could not create JSON object: deviceList\n");
1333 cJSON_Delete(statusObject);
1334 return SR_ERR_OPERATION_FAILED;
1336 cJSON_AddItemToObject(statusObject, "device-list", deviceList);
1338 char *status_string = NULL;
1340 status_string = cJSON_PrintUnformatted(statusObject);
1342 writeStatusFile(status_string);
1344 if (status_string != NULL)
1346 free(status_string);
1347 status_string = NULL;
1350 if (statusObject != NULL)
1352 cJSON_Delete(statusObject);
1359 * Dynamically allocated memory;
1360 * Caller needs to free the memory after it uses the value.
1363 char* readStatusFileInString(void)
1367 char config_file[200];
1368 sprintf(config_file, "%s/status.json", getenv("SCRIPTS_DIR"));
1369 FILE * f = fopen (config_file, "rb");
1373 fseek (f, 0, SEEK_END);
1375 fseek (f, 0, SEEK_SET);
1376 buffer = malloc (length + 1);
1379 fread (buffer, 1, length, f);
1382 buffer[length] = '\0';
1394 * Dynamically allocated memory;
1395 * Caller needs to free the memory after it uses the value.
1398 cJSON* getDeviceListFromStatusFile(void)
1400 char *stringStatus = readStatusFileInString();
1402 if (stringStatus == NULL)
1404 printf("Could not read status file!\n");
1408 cJSON *jsonStatus = cJSON_Parse(stringStatus);
1409 if (jsonStatus == NULL)
1412 const char *error_ptr = cJSON_GetErrorPtr();
1413 if (error_ptr != NULL)
1415 fprintf(stderr, "Could not parse JSON status! Error before: %s\n", error_ptr);
1419 //we don't need the string anymore
1421 stringStatus = NULL;
1426 cJSON* createDeviceListEntry(counterAlarms ves_counter, counterAlarms netconf_counter)
1428 cJSON *deviceListEntry = cJSON_CreateObject();
1429 if (deviceListEntry == NULL)
1431 printf("Could not create JSON object: deviceListEntry\n");
1436 sprintf(hostname, "%s", getenv("HOSTNAME"));
1438 if (cJSON_AddStringToObject(deviceListEntry, "device-name", hostname) == NULL)
1440 printf("Could not create JSON object: device-name\n");
1441 cJSON_Delete(deviceListEntry);
1445 cJSON *vesNotificationsSent = createSeverityCounters(ves_counter);
1446 if (vesNotificationsSent == NULL)
1448 printf("Could not create JSON object: vesNotificationsSent\n");
1449 cJSON_Delete(deviceListEntry);
1452 cJSON_AddItemToObject(deviceListEntry, "ves-notifications-sent", vesNotificationsSent);
1454 cJSON *netconfNotificationsSent = createSeverityCounters(netconf_counter);
1455 if (netconfNotificationsSent == NULL)
1457 printf("Could not create JSON object: netconfNotificationsSent\n");
1458 cJSON_Delete(deviceListEntry);
1461 cJSON_AddItemToObject(deviceListEntry, "netconf-notifications-sent", netconfNotificationsSent);
1463 return deviceListEntry;
1466 static void modifySeverityCounters(cJSON **severityCounters, counterAlarms count)
1468 cJSON *severity= cJSON_GetObjectItemCaseSensitive(*severityCounters, "severity-normal");
1469 if (!cJSON_IsNumber(severity))
1471 printf("Status JSON is not as expected: severity-normal is not an number");
1474 //we set the value of the severity-normal object
1475 cJSON_SetNumberValue(severity, count.normal);
1477 severity= cJSON_GetObjectItemCaseSensitive(*severityCounters, "severity-warning");
1478 if (!cJSON_IsNumber(severity))
1480 printf("Status JSON is not as expected: severity-warning is not an number");
1483 //we set the value of the severity-warning object
1484 cJSON_SetNumberValue(severity, count.warning);
1486 severity= cJSON_GetObjectItemCaseSensitive(*severityCounters, "severity-minor");
1487 if (!cJSON_IsNumber(severity))
1489 printf("Status JSON is not as expected: severity-minor is not an number");
1492 //we set the value of the severity-minor object
1493 cJSON_SetNumberValue(severity, count.minor);
1495 severity= cJSON_GetObjectItemCaseSensitive(*severityCounters, "severity-major");
1496 if (!cJSON_IsNumber(severity))
1498 printf("Status JSON is not as expected: severity-major is not an number");
1501 //we set the value of the severity-major object
1502 cJSON_SetNumberValue(severity, count.major);
1504 severity= cJSON_GetObjectItemCaseSensitive(*severityCounters, "severity-critical");
1505 if (!cJSON_IsNumber(severity))
1507 printf("Status JSON is not as expected: severity-critical is not an number");
1510 //we set the value of the severity-critical object
1511 cJSON_SetNumberValue(severity, count.critical);
1516 static void modifyDeviceListEntry(cJSON **deviceListEntry, counterAlarms ves_counter, counterAlarms netconf_counter)
1518 cJSON *vesNotificationsSent= cJSON_GetObjectItemCaseSensitive(*deviceListEntry, "ves-notifications-sent");
1519 if (!cJSON_IsObject(vesNotificationsSent))
1521 printf("Status JSON is not as expected: ves-notifications-sent is not a object");
1525 modifySeverityCounters(&vesNotificationsSent, ves_counter);
1527 cJSON *netconfNotificationsSent= cJSON_GetObjectItemCaseSensitive(*deviceListEntry, "netconf-notifications-sent");
1528 if (!cJSON_IsObject(netconfNotificationsSent))
1530 printf("Status JSON is not as expected: netconf-notifications-sent is not a object");
1534 modifySeverityCounters(&netconfNotificationsSent, netconf_counter);
1537 int writeStatusNotificationCounters(counterAlarms ves_counter, counterAlarms netconf_counter)
1539 cJSON *jsonStatus = getDeviceListFromStatusFile();
1541 cJSON *deviceList = cJSON_GetObjectItemCaseSensitive(jsonStatus, "device-list");
1542 if (!cJSON_IsArray(deviceList))
1544 printf("Status JSON is not as expected: device-list is not an object");
1545 cJSON_Delete(jsonStatus);
1546 return SR_ERR_OPERATION_FAILED;
1549 int array_size = cJSON_GetArraySize(deviceList);
1552 for (int i=0; i<array_size; ++i)
1554 cJSON *deviceListEntry = cJSON_GetArrayItem(deviceList, i);
1556 sprintf(hostname, "%s", getenv("HOSTNAME"));
1558 cJSON *deviceName = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "device-name");
1559 if (!cJSON_IsString(deviceName))
1561 printf("Status JSON is not as expected: device-name is not a string.");
1562 cJSON_Delete(jsonStatus);
1563 return SR_ERR_OPERATION_FAILED;
1565 char *deviceNameString = cJSON_GetStringValue(deviceName);
1567 if (strcmp(hostname, deviceNameString) == 0)
1569 modifyDeviceListEntry(&deviceListEntry, ves_counter, netconf_counter);
1576 cJSON* deviceListEntry = createDeviceListEntry(ves_counter, netconf_counter);
1578 cJSON_AddItemToArray(deviceList, deviceListEntry);
1581 //writing the new JSON to the configuration file
1582 char *stringStatus = cJSON_PrintUnformatted(jsonStatus);
1583 writeStatusFile(stringStatus);
1585 if (stringStatus != NULL)
1588 stringStatus = NULL;
1591 if (jsonStatus != NULL)
1593 cJSON_Delete(jsonStatus);
1600 int removeDeviceEntryFromStatusFile(char *containerId)
1602 cJSON *jsonStatus = getDeviceListFromStatusFile();
1604 cJSON *deviceList = cJSON_GetObjectItemCaseSensitive(jsonStatus, "device-list");
1605 if (!cJSON_IsArray(deviceList))
1607 printf("Status JSON is not as expected: device-list is not an object");
1608 cJSON_Delete(jsonStatus);
1609 return SR_ERR_OPERATION_FAILED;
1612 int array_size = cJSON_GetArraySize(deviceList);
1613 int found = array_size;
1615 for (int i=0; i<array_size; ++i)
1617 cJSON *deviceListEntry = cJSON_GetArrayItem(deviceList, i);
1619 sprintf(hostname, "%s", getenv("HOSTNAME"));
1621 cJSON *deviceName = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "device-name");
1622 if (!cJSON_IsString(deviceName))
1624 printf("Status JSON is not as expected: device-name is not a string.");
1625 cJSON_Delete(jsonStatus);
1626 return SR_ERR_OPERATION_FAILED;
1628 char *deviceNameString = cJSON_GetStringValue(deviceName);
1630 if (strcmp(containerId, deviceNameString) == 0)
1637 if (found < array_size)
1639 cJSON_DeleteItemFromArray(deviceList, found);
1643 printf("Could not find status file entry for device with id=\"%s\"", containerId);
1646 //writing the new JSON to the configuration file
1647 char *stringStatus = cJSON_PrintUnformatted(jsonStatus);
1648 writeStatusFile(stringStatus);
1650 if (stringStatus != NULL)
1653 stringStatus = NULL;
1656 if (jsonStatus != NULL)
1658 cJSON_Delete(jsonStatus);
1664 int compute_notifications_count(counterAlarms *ves_counter, counterAlarms *netconf_counter)
1666 ves_counter->normal = ves_counter->warning = \
1667 ves_counter->minor = ves_counter->major = \
1668 ves_counter->critical = 0;
1669 netconf_counter->normal = netconf_counter->warning = \
1670 netconf_counter->minor = netconf_counter->major = \
1671 netconf_counter->critical = 0;
1673 cJSON *jsonStatus = getDeviceListFromStatusFile();
1675 cJSON *deviceList = cJSON_GetObjectItemCaseSensitive(jsonStatus, "device-list");
1676 if (!cJSON_IsArray(deviceList))
1678 printf("Status JSON is not as expected: device-list is not an object");
1679 cJSON_Delete(jsonStatus);
1680 return SR_ERR_OPERATION_FAILED;
1683 int array_size = cJSON_GetArraySize(deviceList);
1685 for (int i=0; i<array_size; ++i)
1687 cJSON *deviceListEntry = cJSON_GetArrayItem(deviceList, i);
1689 cJSON *vesNotifications = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "ves-notifications-sent");
1690 if (!cJSON_IsObject(vesNotifications))
1692 printf("Status JSON is not as expected: ves-notifications-sent is not an object.");
1693 cJSON_Delete(jsonStatus);
1694 return SR_ERR_OPERATION_FAILED;
1697 cJSON *severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-normal");
1698 if (!cJSON_IsNumber(severity))
1700 printf("Status JSON is not as expected: severity-normal is not a string.");
1701 cJSON_Delete(jsonStatus);
1702 return SR_ERR_OPERATION_FAILED;
1704 int counter = (int)(severity->valuedouble);
1705 ves_counter->normal += counter;
1707 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-warning");
1708 if (!cJSON_IsNumber(severity))
1710 printf("Status JSON is not as expected: severity-warning is not a string.");
1711 cJSON_Delete(jsonStatus);
1712 return SR_ERR_OPERATION_FAILED;
1714 counter = (int)(severity->valuedouble);
1715 ves_counter->warning += counter;
1717 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-minor");
1718 if (!cJSON_IsNumber(severity))
1720 printf("Status JSON is not as expected: severity-minor is not a string.");
1721 cJSON_Delete(jsonStatus);
1722 return SR_ERR_OPERATION_FAILED;
1724 counter = (int)(severity->valuedouble);
1725 ves_counter->minor += counter;
1727 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-major");
1728 if (!cJSON_IsNumber(severity))
1730 printf("Status JSON is not as expected: severity-major is not a string.");
1731 cJSON_Delete(jsonStatus);
1732 return SR_ERR_OPERATION_FAILED;
1734 counter = (int)(severity->valuedouble);
1735 ves_counter->major += counter;
1737 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-critical");
1738 if (!cJSON_IsNumber(severity))
1740 printf("Status JSON is not as expected: severity-critical is not a string.");
1741 cJSON_Delete(jsonStatus);
1742 return SR_ERR_OPERATION_FAILED;
1744 counter = (int)(severity->valuedouble);
1745 ves_counter->critical += counter;
1747 cJSON *netconfNotifications = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "netconf-notifications-sent");
1748 if (!cJSON_IsObject(netconfNotifications))
1750 printf("Status JSON is not as expected: netconf-notifications-sent is not an object.");
1751 cJSON_Delete(jsonStatus);
1752 return SR_ERR_OPERATION_FAILED;
1755 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-normal");
1756 if (!cJSON_IsNumber(severity))
1758 printf("Status JSON is not as expected: severity-normal is not a string.");
1759 cJSON_Delete(jsonStatus);
1760 return SR_ERR_OPERATION_FAILED;
1763 counter = (int)(severity->valuedouble);
1764 netconf_counter->normal += (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1766 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-warning");
1767 if (!cJSON_IsNumber(severity))
1769 printf("Status JSON is not as expected: severity-warning is not a string.");
1770 cJSON_Delete(jsonStatus);
1771 return SR_ERR_OPERATION_FAILED;
1773 counter = (int)(severity->valuedouble);
1774 netconf_counter->warning += (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1776 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-minor");
1777 if (!cJSON_IsNumber(severity))
1779 printf("Status JSON is not as expected: severity-minor is not a string.");
1780 cJSON_Delete(jsonStatus);
1781 return SR_ERR_OPERATION_FAILED;
1783 counter = (int)(severity->valuedouble);
1784 netconf_counter->minor += (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1786 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-major");
1787 if (!cJSON_IsNumber(severity))
1789 printf("Status JSON is not as expected: severity-major is not a string.");
1790 cJSON_Delete(jsonStatus);
1791 return SR_ERR_OPERATION_FAILED;
1793 counter = (int)(severity->valuedouble);
1794 netconf_counter->major += (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1796 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-critical");
1797 if (!cJSON_IsNumber(severity))
1799 printf("Status JSON is not as expected: severity-critical is not a string.");
1800 cJSON_Delete(jsonStatus);
1801 return SR_ERR_OPERATION_FAILED;
1803 counter = (int)(severity->valuedouble);
1804 netconf_counter->critical += (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1807 cJSON_Delete(jsonStatus);
1812 int getDeviceCounters(char *containerId, counterAlarms *ves_counter, counterAlarms *netconf_counter)
1814 cJSON *jsonStatus = getDeviceListFromStatusFile();
1816 cJSON *deviceList = cJSON_GetObjectItemCaseSensitive(jsonStatus, "device-list");
1817 if (!cJSON_IsArray(deviceList))
1819 printf("Status JSON is not as expected: device-list is not an object");
1820 cJSON_Delete(jsonStatus);
1821 return SR_ERR_OPERATION_FAILED;
1824 int array_size = cJSON_GetArraySize(deviceList);
1826 ves_counter->critical = ves_counter->major = ves_counter->minor = ves_counter->warning = ves_counter->normal = 0;
1827 netconf_counter->critical = netconf_counter->major = netconf_counter->minor = netconf_counter->warning = netconf_counter->normal = 0;
1829 for (int i=0; i<array_size; ++i)
1831 cJSON *deviceListEntry = cJSON_GetArrayItem(deviceList, i);
1833 cJSON *deviceName = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "device-name");
1834 if (!cJSON_IsString(deviceName))
1836 printf("Status JSON is not as expected: device-name is not a string.");
1837 cJSON_Delete(jsonStatus);
1838 return SR_ERR_OPERATION_FAILED;
1840 char *deviceNameString = cJSON_GetStringValue(deviceName);
1842 if (strcmp(deviceNameString, containerId) != 0)
1847 cJSON *vesNotifications = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "ves-notifications-sent");
1848 if (!cJSON_IsObject(vesNotifications))
1850 printf("Status JSON is not as expected: ves-notifications-sent is not an object.");
1851 cJSON_Delete(jsonStatus);
1852 return SR_ERR_OPERATION_FAILED;
1855 cJSON *severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-normal");
1856 if (!cJSON_IsNumber(severity))
1858 printf("Status JSON is not as expected: severity-normal is not a string.");
1859 cJSON_Delete(jsonStatus);
1860 return SR_ERR_OPERATION_FAILED;
1862 int counter = (int)(severity->valuedouble);
1863 ves_counter->normal = counter;
1865 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-warning");
1866 if (!cJSON_IsNumber(severity))
1868 printf("Status JSON is not as expected: severity-warning is not a string.");
1869 cJSON_Delete(jsonStatus);
1870 return SR_ERR_OPERATION_FAILED;
1872 counter = (int)(severity->valuedouble);
1873 ves_counter->warning = counter;
1875 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-minor");
1876 if (!cJSON_IsNumber(severity))
1878 printf("Status JSON is not as expected: severity-minor is not a string.");
1879 cJSON_Delete(jsonStatus);
1880 return SR_ERR_OPERATION_FAILED;
1882 counter = (int)(severity->valuedouble);
1883 ves_counter->minor = counter;
1885 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-major");
1886 if (!cJSON_IsNumber(severity))
1888 printf("Status JSON is not as expected: severity-major is not a string.");
1889 cJSON_Delete(jsonStatus);
1890 return SR_ERR_OPERATION_FAILED;
1892 counter = (int)(severity->valuedouble);
1893 ves_counter->major = counter;
1895 severity = cJSON_GetObjectItemCaseSensitive(vesNotifications, "severity-critical");
1896 if (!cJSON_IsNumber(severity))
1898 printf("Status JSON is not as expected: severity-critical is not a string.");
1899 cJSON_Delete(jsonStatus);
1900 return SR_ERR_OPERATION_FAILED;
1902 counter = (int)(severity->valuedouble);
1903 ves_counter->critical = counter;
1905 cJSON *netconfNotifications = cJSON_GetObjectItemCaseSensitive(deviceListEntry, "netconf-notifications-sent");
1906 if (!cJSON_IsObject(netconfNotifications))
1908 printf("Status JSON is not as expected: netconf-notifications-sent is not an object.");
1909 cJSON_Delete(jsonStatus);
1910 return SR_ERR_OPERATION_FAILED;
1913 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-normal");
1914 if (!cJSON_IsNumber(severity))
1916 printf("Status JSON is not as expected: severity-normal is not a string.");
1917 cJSON_Delete(jsonStatus);
1918 return SR_ERR_OPERATION_FAILED;
1921 counter = (int)(severity->valuedouble);
1922 netconf_counter->normal = (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1924 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-warning");
1925 if (!cJSON_IsNumber(severity))
1927 printf("Status JSON is not as expected: severity-warning is not a string.");
1928 cJSON_Delete(jsonStatus);
1929 return SR_ERR_OPERATION_FAILED;
1931 counter = (int)(severity->valuedouble);
1932 netconf_counter->warning = (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1934 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-minor");
1935 if (!cJSON_IsNumber(severity))
1937 printf("Status JSON is not as expected: severity-minor is not a string.");
1938 cJSON_Delete(jsonStatus);
1939 return SR_ERR_OPERATION_FAILED;
1941 counter = (int)(severity->valuedouble);
1942 netconf_counter->minor = (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1944 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-major");
1945 if (!cJSON_IsNumber(severity))
1947 printf("Status JSON is not as expected: severity-major is not a string.");
1948 cJSON_Delete(jsonStatus);
1949 return SR_ERR_OPERATION_FAILED;
1951 counter = (int)(severity->valuedouble);
1952 netconf_counter->major = (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1954 severity = cJSON_GetObjectItemCaseSensitive(netconfNotifications, "severity-critical");
1955 if (!cJSON_IsNumber(severity))
1957 printf("Status JSON is not as expected: severity-critical is not a string.");
1958 cJSON_Delete(jsonStatus);
1959 return SR_ERR_OPERATION_FAILED;
1961 counter = (int)(severity->valuedouble);
1962 netconf_counter->critical = (counter * NETCONF_CONNECTIONS_PER_DEVICE);
1965 cJSON_Delete(jsonStatus);
1970 int writeSkeletonConfigFile()
1972 cJSON *configObject = cJSON_CreateObject();
1973 if (configObject == NULL)
1975 printf("Could not create JSON object: configObject\n");
1976 return SR_ERR_OPERATION_FAILED;
1979 cJSON *notificationConfig = cJSON_CreateObject();
1980 if (notificationConfig == NULL)
1982 printf("Could not create JSON object: notificationConfig\n");
1983 cJSON_Delete(configObject);
1984 return SR_ERR_OPERATION_FAILED;
1986 cJSON_AddItemToObject(configObject, "notification-config", notificationConfig);
1988 if (cJSON_AddNumberToObject(notificationConfig, "ves-heartbeat-period", 0) == NULL)
1990 printf("Could not create JSON object: ves-heartbeat-period\n");
1991 cJSON_Delete(configObject);
1992 return SR_ERR_OPERATION_FAILED;
1995 if (cJSON_AddTrueToObject(notificationConfig, "is-netconf-available") == NULL)
1997 printf("Could not create JSON object: is-netconf-available\n");
1998 cJSON_Delete(configObject);
1999 return SR_ERR_OPERATION_FAILED;
2002 if (cJSON_AddTrueToObject(notificationConfig, "is-ves-available") == NULL)
2004 printf("Could not create JSON object: is-ves-available\n");
2005 cJSON_Delete(configObject);
2006 return SR_ERR_OPERATION_FAILED;
2009 cJSON *faultNotificationDelayPeriod = cJSON_CreateArray();
2010 if (faultNotificationDelayPeriod == NULL)
2012 printf("Could not create JSON object: faultNotificationDelayPeriod\n");
2013 cJSON_Delete(configObject);
2014 return SR_ERR_OPERATION_FAILED;
2016 cJSON_AddItemToObject(notificationConfig, "fault-notification-delay-period", faultNotificationDelayPeriod);
2018 cJSON *arrayItem = cJSON_CreateNumber(0);
2019 if (arrayItem == NULL)
2021 printf("Could not create JSON object: arrayItem\n");
2022 cJSON_Delete(configObject);
2023 return SR_ERR_OPERATION_FAILED;
2025 cJSON_AddItemToArray(faultNotificationDelayPeriod, arrayItem);
2027 cJSON *vesEndPointDetails = cJSON_CreateObject();
2028 if (vesEndPointDetails == NULL)
2030 printf("Could not create JSON object: vesEndPointDetails\n");
2031 cJSON_Delete(configObject);
2032 return SR_ERR_OPERATION_FAILED;
2034 cJSON_AddItemToObject(configObject, "ves-endpoint-details", vesEndPointDetails);
2036 if (cJSON_AddStringToObject(vesEndPointDetails, "ves-endpoint-ip", "172.17.0.1") == NULL)
2038 printf("Could not create JSON object: ves-endpoint-ip\n");
2039 cJSON_Delete(configObject);
2040 return SR_ERR_OPERATION_FAILED;
2043 if (cJSON_AddNumberToObject(vesEndPointDetails, "ves-endpoint-port", 30007) == NULL)
2045 printf("Could not create JSON object: ves-endpoint-port\n");
2046 cJSON_Delete(configObject);
2047 return SR_ERR_OPERATION_FAILED;
2050 if (cJSON_AddStringToObject(vesEndPointDetails, "ves-endpoint-auth-method", "no-auth") == NULL)
2052 printf("Could not create JSON object: ves-endpoint-auth-method\n");
2053 cJSON_Delete(configObject);
2054 return SR_ERR_OPERATION_FAILED;
2057 if (cJSON_AddStringToObject(vesEndPointDetails, "ves-endpoint-username", "") == NULL)
2059 printf("Could not create JSON object: ves-endpoint-username\n");
2060 cJSON_Delete(configObject);
2061 return SR_ERR_OPERATION_FAILED;
2064 if (cJSON_AddStringToObject(vesEndPointDetails, "ves-endpoint-password", "") == NULL)
2066 printf("Could not create JSON object: ves-endpoint-password\n");
2067 cJSON_Delete(configObject);
2068 return SR_ERR_OPERATION_FAILED;
2071 if (cJSON_AddStringToObject(vesEndPointDetails, "ves-endpoint-certificate", "") == NULL)
2073 printf("Could not create JSON object: ves-endpoint-certificate\n");
2074 cJSON_Delete(configObject);
2075 return SR_ERR_OPERATION_FAILED;
2078 if (cJSON_AddFalseToObject(vesEndPointDetails, "ves-registration") == NULL)
2080 printf("Could not create JSON object: ves-registration\n");
2081 cJSON_Delete(configObject);
2082 return SR_ERR_OPERATION_FAILED;
2085 cJSON *controllerDetails = cJSON_CreateObject();
2086 if (controllerDetails == NULL)
2088 printf("Could not create JSON object: controllerDetails\n");
2089 cJSON_Delete(configObject);
2090 return SR_ERR_OPERATION_FAILED;
2092 cJSON_AddItemToObject(configObject, "controller-details", controllerDetails);
2094 if (cJSON_AddStringToObject(controllerDetails, "controller-ip", "172.17.0.1") == NULL)
2096 printf("Could not create JSON object: controller-ip\n");
2097 cJSON_Delete(configObject);
2098 return SR_ERR_OPERATION_FAILED;
2101 if (cJSON_AddNumberToObject(controllerDetails, "controller-port", 8181) == NULL)
2103 printf("Could not create JSON object: controller-port\n");
2104 cJSON_Delete(configObject);
2105 return SR_ERR_OPERATION_FAILED;
2108 if (cJSON_AddNumberToObject(controllerDetails, "netconf-call-home-port", 6666) == NULL)
2110 printf("Could not create JSON object: netconf-call-home-port\n");
2111 cJSON_Delete(configObject);
2112 return SR_ERR_OPERATION_FAILED;
2115 if (cJSON_AddStringToObject(controllerDetails, "controller-username", "admin") == NULL)
2117 printf("Could not create JSON object: controller-username\n");
2118 cJSON_Delete(configObject);
2119 return SR_ERR_OPERATION_FAILED;
2122 if (cJSON_AddStringToObject(controllerDetails, "controller-password", "admin") == NULL)
2124 printf("Could not create JSON object: controller-password\n");
2125 cJSON_Delete(configObject);
2126 return SR_ERR_OPERATION_FAILED;
2129 if (cJSON_AddNumberToObject(configObject, "ssh-connections", 1) == NULL)
2131 printf("Could not create JSON object: ssh-connections\n");
2132 cJSON_Delete(configObject);
2133 return SR_ERR_OPERATION_FAILED;
2136 if (cJSON_AddNumberToObject(configObject, "tls-connections", 0) == NULL)
2138 printf("Could not create JSON object: tls-connections\n");
2139 cJSON_Delete(configObject);
2140 return SR_ERR_OPERATION_FAILED;
2143 if (cJSON_AddFalseToObject(configObject, "netconf-call-home") == NULL)
2145 printf("Could not create JSON object: netconf-call-home\n");
2146 cJSON_Delete(configObject);
2147 return SR_ERR_OPERATION_FAILED;
2150 char *config_string = NULL;
2152 config_string = cJSON_PrintUnformatted(configObject);
2154 writeConfigFile(config_string);
2156 if (config_string != NULL)
2158 free(config_string);
2159 config_string = NULL;
2162 if (configObject != NULL)
2164 cJSON_Delete(configObject);
2170 int getIntFromString(char *string, int def_value)
2172 int rc, value = def_value;
2175 rc = sscanf(string, "%d", &value);
2178 printf("Could not get the %s! Using the default 0...\n", string);
2185 int getSshConnectionsFromConfigJson(void)
2187 char *stringConfig = readConfigFileInString();
2189 if (stringConfig == NULL)
2191 printf("Could not read JSON configuration file in string.");
2195 cJSON *jsonConfig = cJSON_Parse(stringConfig);
2196 if (jsonConfig == NULL)
2199 const char *error_ptr = cJSON_GetErrorPtr();
2200 if (error_ptr != NULL)
2202 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
2204 return SR_ERR_OPERATION_FAILED;
2206 //we don't need the string anymore
2208 stringConfig = NULL;
2210 cJSON *sshConnections = cJSON_GetObjectItemCaseSensitive(jsonConfig, "ssh-connections");
2211 if (!cJSON_IsNumber(sshConnections))
2213 printf("Configuration JSON is not as expected: ssh-connections is not an object");
2214 cJSON_Delete(jsonConfig);
2215 return SR_ERR_OPERATION_FAILED;
2218 int num_of_ssh = (int)(sshConnections->valuedouble);
2220 cJSON_Delete(jsonConfig);
2225 int getTlsConnectionsFromConfigJson(void)
2227 char *stringConfig = readConfigFileInString();
2229 if (stringConfig == NULL)
2231 printf("Could not read JSON configuration file in string.");
2235 cJSON *jsonConfig = cJSON_Parse(stringConfig);
2236 if (jsonConfig == NULL)
2239 const char *error_ptr = cJSON_GetErrorPtr();
2240 if (error_ptr != NULL)
2242 fprintf(stderr, "Could not parse JSON configuration! Error before: %s\n", error_ptr);
2244 return SR_ERR_OPERATION_FAILED;
2246 //we don't need the string anymore
2248 stringConfig = NULL;
2250 cJSON *tlsConnections = cJSON_GetObjectItemCaseSensitive(jsonConfig, "tls-connections");
2251 if (!cJSON_IsNumber(tlsConnections))
2253 printf("Configuration JSON is not as expected: ssh-connections is not an object");
2254 cJSON_Delete(jsonConfig);
2255 return SR_ERR_OPERATION_FAILED;
2258 int num_of_tls = (int)(tlsConnections->valuedouble);
2260 cJSON_Delete(jsonConfig);