4 * Created on: Oct 24, 2019
21 #include "heartbeat.h"
23 #include "sysrepo/values.h"
27 #define LINE_BUFSIZE 128
28 #define SLEEP_BEFORE_PNF_AUTOREG 60
30 volatile int exit_application = 0;
38 curl = curl_easy_init();
41 printf("cURL initialization error! Aborting call!\n");
42 return SR_ERR_OPERATION_FAILED;
52 curl_easy_cleanup(curl);
58 //static void prepare_ves_message_curl(void)
60 // curl_easy_reset(curl);
61 // set_curl_common_info();
63 // char *ves_ip = getVesIpFromConfigJson();
64 // int ves_port = getVesPortFromConfigJson();
67 // sprintf(url, "http://%s:%d/eventListener/v7", ves_ip, ves_port);
68 // curl_easy_setopt(curl, CURLOPT_URL, url);
72 //// curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
77 * Heartbeat payload example
81 "commonEventHeader": {
82 "domain": "heartbeat",
83 "eventId": "parallels-Parallels-Virtual-Platform_2019-10-24T10:25:25.514Z",
84 "eventName": "heartbeat_Controller",
85 "eventType": "Controller",
88 "reportingEntityId": "",
89 "reportingEntityName": "parallels-Parallels-Virtual-Platform",
91 "sourceName": "parallels-Parallels-Virtual-Platform",
92 "startEpochMicrosec": 1571912725514,
93 "lastEpochMicrosec": 1571912725514,
94 "nfNamingCode": "sdn controller",
95 "nfVendorName": "sdn",
96 "timeZoneOffset": "+00:00",
98 "vesEventListenerVersion":"7.0.1"
101 "heartbeatFieldsVersion": "3.0",
102 "heartbeatInterval": 20,
103 "additionalFields": {
104 "eventTime": "2019-10-24T10:25:25.514Z"
112 static int send_heartbeat(int heartbeat_interval)
115 static int sequence_number = 0;
117 prepare_ves_message_curl(curl);
119 cJSON *postDataJson = cJSON_CreateObject();
121 cJSON *event = cJSON_CreateObject();
124 printf("Could not create JSON object: event\n");
127 cJSON_AddItemToObject(postDataJson, "event", event);
130 sprintf(hostname, "%s", getenv("HOSTNAME"));
132 cJSON *commonEventHeader = vesCreateCommonEventHeader("heartbeat", "Controller", hostname, sequence_number++);
133 if (commonEventHeader == NULL)
135 printf("Could not create JSON object: commonEventHeader\n");
138 cJSON_AddItemToObject(event, "commonEventHeader", commonEventHeader);
140 cJSON *heartbeatFields = vesCreateHeartbeatFields(heartbeat_interval);
141 if (heartbeatFields == NULL)
143 printf("Could not create JSON object: heartbeatFields\n");
146 cJSON_AddItemToObject(event, "heartbeatFields", heartbeatFields);
148 char *post_data_string = NULL;
150 post_data_string = cJSON_PrintUnformatted(postDataJson);
152 printf("Post data JSON:\n%s\n", post_data_string);
154 if (postDataJson != NULL)
156 cJSON_Delete(postDataJson);
159 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data_string);
161 res = curl_easy_perform(curl);
165 printf("Failed to send cURL...\n");
166 return SR_ERR_OPERATION_FAILED;
173 sigint_handler(int signum)
175 exit_application = 1;
178 static int send_pnf_registration_instance(char *hostname, int port, bool is_tls)
181 static int sequence_number = 0;
183 prepare_ves_message_curl(curl);
185 cJSON *postDataJson = cJSON_CreateObject();
187 cJSON *event = cJSON_CreateObject();
190 printf("Could not create JSON object: event\n");
193 cJSON_AddItemToObject(postDataJson, "event", event);
195 char source_name[100];
196 sprintf(source_name, "%s_%d", hostname, port);
198 cJSON *commonEventHeader = vesCreateCommonEventHeader("pnfRegistration", "EventType5G", source_name, sequence_number++);
199 if (commonEventHeader == NULL)
201 printf("Could not create JSON object: commonEventHeader\n");
204 cJSON_AddItemToObject(event, "commonEventHeader", commonEventHeader);
206 cJSON *pnfRegistrationFields = vesCreatePnfRegistrationFields(port, is_tls);
207 if (pnfRegistrationFields == NULL)
209 printf("Could not create JSON object: pnfRegistrationFields\n");
212 cJSON_AddItemToObject(event, "pnfRegistrationFields", pnfRegistrationFields);
214 char *post_data_string = NULL;
216 post_data_string = cJSON_PrintUnformatted(postDataJson);
218 printf("Post data JSON:\n%s\n", post_data_string);
220 if (postDataJson != NULL)
222 cJSON_Delete(postDataJson);
225 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data_string);
227 res = curl_easy_perform(curl);
231 printf("Failed to send cURL...\n");
232 return SR_ERR_OPERATION_FAILED;
238 static void pnf_registration(void)
240 // delay the PNF Registration VES message, until anything else is initialized
241 printf("delay the PNF Registration VES message, until anything else is initialized");
242 sleep(SLEEP_BEFORE_PNF_AUTOREG);
244 int is_reg = getVesRegistrationFromConfigJson();
248 //ves-registration object is set to False, we do not make an automatic PNF registration
249 printf("ves-registration object is set to False, we do not make an automatic PNF registration");
253 int rc = SR_ERR_OK, netconf_port_base = 0;
254 char *netconf_base_string = getenv("NETCONF_BASE");
255 char *hostname_string = getenv("HOSTNAME");
257 if (netconf_base_string != NULL)
259 rc = sscanf(netconf_base_string, "%d", &netconf_port_base);
262 printf("Could not find the NETCONF base port, aborting the PNF registration...\n");
267 //TODO This is where we hardcoded: 7 devices will have SSH connections and 3 devices will have TLS connections
268 for (int port = 0; port < NETCONF_CONNECTIONS_PER_DEVICE - 3; ++port)
270 pthread_mutex_lock(&lock);
271 rc = send_pnf_registration_instance(hostname_string, netconf_port_base + port, FALSE);
274 printf("Could not send PNF Registration SSH message...\n");
276 pthread_mutex_unlock(&lock);
278 for (int port = NETCONF_CONNECTIONS_PER_DEVICE - 3; port < NETCONF_CONNECTIONS_PER_DEVICE; ++port)
280 pthread_mutex_lock(&lock);
281 rc = send_pnf_registration_instance(hostname_string, netconf_port_base + port, TRUE);
282 pthread_mutex_unlock(&lock);
285 printf("Could not send PNF Registration TLS message...\n");
293 main(int argc, char **argv)
297 int heartbeat_interval = 120; //seconds
299 setbuf(stdout, NULL);
301 if (pthread_mutex_init(&lock, NULL) != 0)
303 printf("Mutex init failed...\n");
307 pthread_t pnf_autoregistration_thread;
308 if(pthread_create(&pnf_autoregistration_thread, NULL, pnf_registration, NULL))
310 fprintf(stderr, "Could not create thread for pnf auto registration\n");
317 fprintf(stderr, "Could not initialize cURL: %s\n", sr_strerror(rc));
321 /* loop until ctrl-c is pressed / SIGINT is received */
322 signal(SIGINT, sigint_handler);
323 signal(SIGTERM, sigint_handler);
324 signal(SIGPIPE, SIG_IGN);
326 while (!exit_application)
328 heartbeat_interval = getVesHeartbeatPeriodFromConfigJson();
330 if (heartbeat_interval > 0)
332 pthread_mutex_lock(&lock);
333 send_heartbeat(heartbeat_interval);
334 pthread_mutex_unlock(&lock);
335 sleep(heartbeat_interval);
343 printf("Application exit requested, exiting.\n");