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);
69 * Heartbeat payload example
73 "commonEventHeader": {
74 "domain": "heartbeat",
75 "eventId": "parallels-Parallels-Virtual-Platform_2019-10-24T10:25:25.514Z",
76 "eventName": "heartbeat_Controller",
77 "eventType": "Controller",
80 "reportingEntityId": "",
81 "reportingEntityName": "parallels-Parallels-Virtual-Platform",
83 "sourceName": "parallels-Parallels-Virtual-Platform",
84 "startEpochMicrosec": 1571912725514,
85 "lastEpochMicrosec": 1571912725514,
86 "nfNamingCode": "sdn controller",
87 "nfVendorName": "sdn",
88 "timeZoneOffset": "+00:00",
90 "vesEventListenerVersion":"7.0.1"
93 "heartbeatFieldsVersion": "3.0",
94 "heartbeatInterval": 20,
96 "eventTime": "2019-10-24T10:25:25.514Z"
104 static int send_heartbeat(int heartbeat_interval)
107 static int sequence_number = 0;
109 prepare_ves_message_curl(curl);
111 cJSON *postDataJson = cJSON_CreateObject();
112 if (postDataJson == NULL)
114 printf("Could not create JSON object: postDataJson\n");
118 cJSON *event = cJSON_CreateObject();
121 printf("Could not create JSON object: event\n");
124 cJSON_AddItemToObject(postDataJson, "event", event);
127 sprintf(hostname, "%s", getenv("HOSTNAME"));
129 cJSON *commonEventHeader = vesCreateCommonEventHeader("heartbeat", "Controller", hostname, sequence_number++);
130 if (commonEventHeader == NULL)
132 printf("Could not create JSON object: commonEventHeader\n");
133 cJSON_Delete(postDataJson);
136 cJSON_AddItemToObject(event, "commonEventHeader", commonEventHeader);
138 cJSON *heartbeatFields = vesCreateHeartbeatFields(heartbeat_interval);
139 if (heartbeatFields == NULL)
141 printf("Could not create JSON object: heartbeatFields\n");
142 cJSON_Delete(postDataJson);
145 cJSON_AddItemToObject(event, "heartbeatFields", heartbeatFields);
147 char *post_data_string = NULL;
149 post_data_string = cJSON_PrintUnformatted(postDataJson);
151 printf("Post data JSON:\n%s\n", post_data_string);
153 if (postDataJson != NULL)
155 cJSON_Delete(postDataJson);
158 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data_string);
160 res = curl_easy_perform(curl);
164 return SR_ERR_OPERATION_FAILED;
171 sigint_handler(int signum)
173 exit_application = 1;
176 static int send_pnf_registration_instance(char *hostname, int port, bool is_tls)
179 static int sequence_number = 0;
181 prepare_ves_message_curl(curl);
183 cJSON *postDataJson = cJSON_CreateObject();
184 if (postDataJson == NULL)
186 printf("Could not create JSON object: postDataJson\n");
190 cJSON *event = cJSON_CreateObject();
193 printf("Could not create JSON object: event\n");
194 cJSON_Delete(postDataJson);
197 cJSON_AddItemToObject(postDataJson, "event", event);
199 char source_name[100];
200 sprintf(source_name, "%s_%d", hostname, port);
202 cJSON *commonEventHeader = vesCreateCommonEventHeader("pnfRegistration", "EventType5G", source_name, sequence_number++);
203 if (commonEventHeader == NULL)
205 printf("Could not create JSON object: commonEventHeader\n");
206 cJSON_Delete(postDataJson);
209 cJSON_AddItemToObject(event, "commonEventHeader", commonEventHeader);
211 cJSON *pnfRegistrationFields = vesCreatePnfRegistrationFields(port, is_tls);
212 if (pnfRegistrationFields == NULL)
214 printf("Could not create JSON object: pnfRegistrationFields\n");
215 cJSON_Delete(postDataJson);
218 cJSON_AddItemToObject(event, "pnfRegistrationFields", pnfRegistrationFields);
220 char *post_data_string = NULL;
222 post_data_string = cJSON_PrintUnformatted(postDataJson);
224 printf("Post data JSON:\n%s\n", post_data_string);
226 if (postDataJson != NULL)
228 cJSON_Delete(postDataJson);
231 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data_string);
233 res = curl_easy_perform(curl);
237 printf("Failed to send cURL...\n");
238 return SR_ERR_OPERATION_FAILED;
244 static void *pnf_registration(void *arg)
246 // delay the PNF Registration VES message, until anything else is initialized
247 printf("delay the PNF Registration VES message, until anything else is initialized");
248 sleep(SLEEP_BEFORE_PNF_AUTOREG);
250 int is_reg = getVesRegistrationFromConfigJson();
254 //ves-registration object is set to False, we do not make an automatic PNF registration
255 printf("ves-registration object is set to False, we do not make an automatic PNF registration");
259 int rc = SR_ERR_OK, netconf_port_base = 0;
260 char *hostname_string = getenv("HOSTNAME");
263 netconf_port_base = getIntFromString(getenv("NETCONF_BASE"), 0);
265 //TODO This is where we hardcoded: 7 devices will have SSH connections and 3 devices will have TLS connections
266 for (int i = 0; i < SSH_CONNECTIONS_PER_DEVICE; ++port, ++i)
268 pthread_mutex_lock(&lock);
269 rc = send_pnf_registration_instance(hostname_string, netconf_port_base + port, FALSE);
272 printf("Could not send PNF Registration SSH message...\n");
274 pthread_mutex_unlock(&lock);
276 for (int i = 0; i < TLS_CONNECTIONS_PER_DEVICE; ++port, ++i)
278 pthread_mutex_lock(&lock);
279 rc = send_pnf_registration_instance(hostname_string, netconf_port_base + port, TRUE);
280 pthread_mutex_unlock(&lock);
283 printf("Could not send PNF Registration TLS message...\n");
291 main(int argc, char **argv)
295 int heartbeat_interval = 120; //seconds
297 setbuf(stdout, NULL);
299 if (pthread_mutex_init(&lock, NULL) != 0)
301 printf("Mutex init failed...\n");
305 pthread_t pnf_autoregistration_thread;
306 if(pthread_create(&pnf_autoregistration_thread, NULL, pnf_registration, NULL))
308 fprintf(stderr, "Could not create thread for pnf auto registration\n");
315 fprintf(stderr, "Could not initialize cURL: %s\n", sr_strerror(rc));
319 /* loop until ctrl-c is pressed / SIGINT is received */
320 signal(SIGINT, sigint_handler);
321 signal(SIGTERM, sigint_handler);
322 signal(SIGPIPE, SIG_IGN);
324 while (!exit_application)
326 heartbeat_interval = getVesHeartbeatPeriodFromConfigJson();
328 if (heartbeat_interval > 0)
330 pthread_mutex_lock(&lock);
331 send_heartbeat(heartbeat_interval);
332 pthread_mutex_unlock(&lock);
333 sleep(heartbeat_interval);
341 printf("Application exit requested, exiting.\n");