Configurable HTTP(S) support for VES messaging
[sim/o1-interface.git] / ntsimulator / src / ves-messages / heartbeat.c
index fce544c..be0093e 100644 (file)
@@ -1,9 +1,19 @@
-/*
- * heartbeat.c
- *
- *  Created on: Oct 24, 2019
- *      Author: parallels
- */
+/*************************************************************************
+*
+* Copyright 2019 highstreet technologies GmbH and others
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+***************************************************************************/
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -55,24 +65,6 @@ int cleanup_curl()
        return SR_ERR_OK;
 }
 
-//static void prepare_ves_message_curl(void)
-//{
-//     curl_easy_reset(curl);
-//     set_curl_common_info();
-//
-//     char *ves_ip = getVesIpFromConfigJson();
-//     int ves_port = getVesPortFromConfigJson();
-//
-//     char url[100];
-//     sprintf(url, "http://%s:%d/eventListener/v7", ves_ip, ves_port);
-//     curl_easy_setopt(curl, CURLOPT_URL, url);
-//
-//     free(ves_ip);
-//
-////   curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
-//
-//     return;
-//}
 /*
  * Heartbeat payload example
  *
@@ -117,6 +109,11 @@ static int send_heartbeat(int heartbeat_interval)
        prepare_ves_message_curl(curl);
 
        cJSON *postDataJson = cJSON_CreateObject();
+       if (postDataJson == NULL)
+       {
+               printf("Could not create JSON object: postDataJson\n");
+               return 1;
+       }
 
        cJSON *event = cJSON_CreateObject();
        if (event == NULL)
@@ -133,6 +130,7 @@ static int send_heartbeat(int heartbeat_interval)
        if (commonEventHeader == NULL)
        {
                printf("Could not create JSON object: commonEventHeader\n");
+               cJSON_Delete(postDataJson);
                return 1;
        }
        cJSON_AddItemToObject(event, "commonEventHeader", commonEventHeader);
@@ -141,6 +139,7 @@ static int send_heartbeat(int heartbeat_interval)
        if (heartbeatFields == NULL)
        {
                printf("Could not create JSON object: heartbeatFields\n");
+               cJSON_Delete(postDataJson);
                return 1;
        }
        cJSON_AddItemToObject(event, "heartbeatFields", heartbeatFields);
@@ -162,7 +161,6 @@ static int send_heartbeat(int heartbeat_interval)
 
        if (res != CURLE_OK)
        {
-               printf("Failed to send cURL...\n");
                return SR_ERR_OPERATION_FAILED;
        }
 
@@ -183,11 +181,17 @@ static int send_pnf_registration_instance(char *hostname, int port, bool is_tls)
        prepare_ves_message_curl(curl);
 
        cJSON *postDataJson = cJSON_CreateObject();
+       if (postDataJson == NULL)
+       {
+               printf("Could not create JSON object: postDataJson\n");
+               return 1;
+       }
 
        cJSON *event = cJSON_CreateObject();
        if (event == NULL)
        {
                printf("Could not create JSON object: event\n");
+               cJSON_Delete(postDataJson);
                return 1;
        }
        cJSON_AddItemToObject(postDataJson, "event", event);
@@ -199,6 +203,7 @@ static int send_pnf_registration_instance(char *hostname, int port, bool is_tls)
        if (commonEventHeader == NULL)
        {
                printf("Could not create JSON object: commonEventHeader\n");
+               cJSON_Delete(postDataJson);
                return 1;
        }
        cJSON_AddItemToObject(event, "commonEventHeader", commonEventHeader);
@@ -207,6 +212,7 @@ static int send_pnf_registration_instance(char *hostname, int port, bool is_tls)
        if (pnfRegistrationFields == NULL)
        {
                printf("Could not create JSON object: pnfRegistrationFields\n");
+               cJSON_Delete(postDataJson);
                return 1;
        }
        cJSON_AddItemToObject(event, "pnfRegistrationFields", pnfRegistrationFields);
@@ -235,7 +241,7 @@ static int send_pnf_registration_instance(char *hostname, int port, bool is_tls)
        return SR_ERR_OK;
 }
 
-static void pnf_registration(void)
+static void *pnf_registration(void *arg)
 {
        // delay the PNF Registration VES message, until anything else is initialized
        printf("delay the PNF Registration VES message, until anything else is initialized");
@@ -247,25 +253,17 @@ static void pnf_registration(void)
        {
                //ves-registration object is set to False, we do not make an automatic PNF registration
                printf("ves-registration object is set to False, we do not make an automatic PNF registration");
-               return;
+               return NULL;
        }
 
        int rc = SR_ERR_OK, netconf_port_base = 0;
-       char *netconf_base_string = getenv("NETCONF_BASE");
        char *hostname_string = getenv("HOSTNAME");
+    int port = 0;
 
-       if (netconf_base_string != NULL)
-       {
-               rc = sscanf(netconf_base_string, "%d", &netconf_port_base);
-               if (rc != 1)
-               {
-                       printf("Could not find the NETCONF base port, aborting the PNF registration...\n");
-                       return;
-               }
-       }
+    netconf_port_base = getIntFromString(getenv("NETCONF_BASE"), 0);
 
        //TODO This is where we hardcoded: 7 devices will have SSH connections and 3 devices will have TLS connections
-       for (int port = 0; port < NETCONF_CONNECTIONS_PER_DEVICE - 3; ++port)
+       for (int i = 0; i < SSH_CONNECTIONS_PER_DEVICE; ++port, ++i)
        {
                pthread_mutex_lock(&lock);
                rc = send_pnf_registration_instance(hostname_string, netconf_port_base + port, FALSE);
@@ -275,7 +273,7 @@ static void pnf_registration(void)
                }
                pthread_mutex_unlock(&lock);
        }
-       for (int port = NETCONF_CONNECTIONS_PER_DEVICE - 3; port < NETCONF_CONNECTIONS_PER_DEVICE; ++port)
+       for (int i = 0; i < TLS_CONNECTIONS_PER_DEVICE; ++port, ++i)
        {
                pthread_mutex_lock(&lock);
                rc = send_pnf_registration_instance(hostname_string, netconf_port_base + port, TRUE);
@@ -286,7 +284,7 @@ static void pnf_registration(void)
                }
        }
 
-       return;
+       return NULL;
 }
 
 int