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 ***************************************************************************/
31 #include "heartbeat.h"
33 #include "sysrepo/values.h"
37 #define LINE_BUFSIZE 128
38 #define SLEEP_BEFORE_PNF_AUTOREG 60
40 volatile int exit_application = 0;
48 curl = curl_easy_init();
51 printf("cURL initialization error! Aborting call!\n");
52 return SR_ERR_OPERATION_FAILED;
62 curl_easy_cleanup(curl);
68 //static void prepare_ves_message_curl(void)
70 // curl_easy_reset(curl);
71 // set_curl_common_info();
73 // char *ves_ip = getVesIpFromConfigJson();
74 // int ves_port = getVesPortFromConfigJson();
77 // sprintf(url, "http://%s:%d/eventListener/v7", ves_ip, ves_port);
78 // curl_easy_setopt(curl, CURLOPT_URL, url);
82 //// curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
87 * Heartbeat payload example
91 "commonEventHeader": {
92 "domain": "heartbeat",
93 "eventId": "parallels-Parallels-Virtual-Platform_2019-10-24T10:25:25.514Z",
94 "eventName": "heartbeat_Controller",
95 "eventType": "Controller",
98 "reportingEntityId": "",
99 "reportingEntityName": "parallels-Parallels-Virtual-Platform",
101 "sourceName": "parallels-Parallels-Virtual-Platform",
102 "startEpochMicrosec": 1571912725514,
103 "lastEpochMicrosec": 1571912725514,
104 "nfNamingCode": "sdn controller",
105 "nfVendorName": "sdn",
106 "timeZoneOffset": "+00:00",
108 "vesEventListenerVersion":"7.0.1"
111 "heartbeatFieldsVersion": "3.0",
112 "heartbeatInterval": 20,
113 "additionalFields": {
114 "eventTime": "2019-10-24T10:25:25.514Z"
122 static int send_heartbeat(int heartbeat_interval)
125 static int sequence_number = 0;
127 prepare_ves_message_curl(curl);
129 cJSON *postDataJson = cJSON_CreateObject();
131 cJSON *event = cJSON_CreateObject();
134 printf("Could not create JSON object: event\n");
137 cJSON_AddItemToObject(postDataJson, "event", event);
140 sprintf(hostname, "%s", getenv("HOSTNAME"));
142 cJSON *commonEventHeader = vesCreateCommonEventHeader("heartbeat", "Controller", hostname, sequence_number++);
143 if (commonEventHeader == NULL)
145 printf("Could not create JSON object: commonEventHeader\n");
148 cJSON_AddItemToObject(event, "commonEventHeader", commonEventHeader);
150 cJSON *heartbeatFields = vesCreateHeartbeatFields(heartbeat_interval);
151 if (heartbeatFields == NULL)
153 printf("Could not create JSON object: heartbeatFields\n");
156 cJSON_AddItemToObject(event, "heartbeatFields", heartbeatFields);
158 char *post_data_string = NULL;
160 post_data_string = cJSON_PrintUnformatted(postDataJson);
162 printf("Post data JSON:\n%s\n", post_data_string);
164 if (postDataJson != NULL)
166 cJSON_Delete(postDataJson);
169 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data_string);
171 res = curl_easy_perform(curl);
175 printf("Failed to send cURL...\n");
176 return SR_ERR_OPERATION_FAILED;
183 sigint_handler(int signum)
185 exit_application = 1;
188 static int send_pnf_registration_instance(char *hostname, int port, bool is_tls)
191 static int sequence_number = 0;
193 prepare_ves_message_curl(curl);
195 cJSON *postDataJson = cJSON_CreateObject();
197 cJSON *event = cJSON_CreateObject();
200 printf("Could not create JSON object: event\n");
203 cJSON_AddItemToObject(postDataJson, "event", event);
205 char source_name[100];
206 sprintf(source_name, "%s_%d", hostname, port);
208 cJSON *commonEventHeader = vesCreateCommonEventHeader("pnfRegistration", "EventType5G", source_name, sequence_number++);
209 if (commonEventHeader == NULL)
211 printf("Could not create JSON object: commonEventHeader\n");
214 cJSON_AddItemToObject(event, "commonEventHeader", commonEventHeader);
216 cJSON *pnfRegistrationFields = vesCreatePnfRegistrationFields(port, is_tls);
217 if (pnfRegistrationFields == NULL)
219 printf("Could not create JSON object: pnfRegistrationFields\n");
222 cJSON_AddItemToObject(event, "pnfRegistrationFields", pnfRegistrationFields);
224 char *post_data_string = NULL;
226 post_data_string = cJSON_PrintUnformatted(postDataJson);
228 printf("Post data JSON:\n%s\n", post_data_string);
230 if (postDataJson != NULL)
232 cJSON_Delete(postDataJson);
235 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data_string);
237 res = curl_easy_perform(curl);
241 printf("Failed to send cURL...\n");
242 return SR_ERR_OPERATION_FAILED;
248 static void pnf_registration(void)
250 // delay the PNF Registration VES message, until anything else is initialized
251 printf("delay the PNF Registration VES message, until anything else is initialized");
252 sleep(SLEEP_BEFORE_PNF_AUTOREG);
254 int is_reg = getVesRegistrationFromConfigJson();
258 //ves-registration object is set to False, we do not make an automatic PNF registration
259 printf("ves-registration object is set to False, we do not make an automatic PNF registration");
263 int rc = SR_ERR_OK, netconf_port_base = 0;
264 char *netconf_base_string = getenv("NETCONF_BASE");
265 char *hostname_string = getenv("HOSTNAME");
267 if (netconf_base_string != NULL)
269 rc = sscanf(netconf_base_string, "%d", &netconf_port_base);
272 printf("Could not find the NETCONF base port, aborting the PNF registration...\n");
277 //TODO This is where we hardcoded: 7 devices will have SSH connections and 3 devices will have TLS connections
278 for (int port = 0; port < NETCONF_CONNECTIONS_PER_DEVICE - 3; ++port)
280 pthread_mutex_lock(&lock);
281 rc = send_pnf_registration_instance(hostname_string, netconf_port_base + port, FALSE);
284 printf("Could not send PNF Registration SSH message...\n");
286 pthread_mutex_unlock(&lock);
288 for (int port = NETCONF_CONNECTIONS_PER_DEVICE - 3; port < NETCONF_CONNECTIONS_PER_DEVICE; ++port)
290 pthread_mutex_lock(&lock);
291 rc = send_pnf_registration_instance(hostname_string, netconf_port_base + port, TRUE);
292 pthread_mutex_unlock(&lock);
295 printf("Could not send PNF Registration TLS message...\n");
303 main(int argc, char **argv)
307 int heartbeat_interval = 120; //seconds
309 setbuf(stdout, NULL);
311 if (pthread_mutex_init(&lock, NULL) != 0)
313 printf("Mutex init failed...\n");
317 pthread_t pnf_autoregistration_thread;
318 if(pthread_create(&pnf_autoregistration_thread, NULL, pnf_registration, NULL))
320 fprintf(stderr, "Could not create thread for pnf auto registration\n");
327 fprintf(stderr, "Could not initialize cURL: %s\n", sr_strerror(rc));
331 /* loop until ctrl-c is pressed / SIGINT is received */
332 signal(SIGINT, sigint_handler);
333 signal(SIGTERM, sigint_handler);
334 signal(SIGPIPE, SIG_IGN);
336 while (!exit_application)
338 heartbeat_interval = getVesHeartbeatPeriodFromConfigJson();
340 if (heartbeat_interval > 0)
342 pthread_mutex_lock(&lock);
343 send_heartbeat(heartbeat_interval);
344 pthread_mutex_unlock(&lock);
345 sleep(heartbeat_interval);
353 printf("Application exit requested, exiting.\n");