VES EVEL Library VES 5.4.1 enhancements
[demo.git] / vnfs / VES5.0 / evel / evel-library / code / VESreporting_HB / ves_heartbeat_reporter.c
1 /*************************************************************************//**
2  *
3  * Copyright © 2018 AT&T Intellectual Property. All rights reserved.
4  *
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
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and 
14  * limitations under the License.
15  *
16  ****************************************************************************/
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <pthread.h>
22 #include <string.h>
23 #include <netdb.h>
24 #include <sys/time.h>
25 #include <sys/stat.h>
26 #include "jsmn.h"
27 #include "evel.h"
28
29 #define BUFSIZE 128
30 #define MAX_BUFFER_SIZE 4096
31 #define MAX_TOKENS 1000
32
33 void *HeartbeatThread(void *threadarg);
34
35 unsigned long long epoch_start = 0;
36
37 int main(int argc, char** argv)
38 {
39   char* fqdn = argv[1];
40   int port = atoi(argv[2]);
41   int i=0;
42   int rc;
43   pthread_attr_t attr;
44   pthread_t hb_thread;
45   char* fqdn2 = NULL;
46   int port2 = 0;
47
48   if(argc == 5)
49   {
50      fqdn2 = argv[3];
51      port2 = atoi(argv[4]);
52   }
53
54   if (!((argc == 3) || (argc == 5)))
55   {
56     fprintf(stderr, "Usage: %s <FQDN>|<IP address> <port> <FQDN>|<IP address> <port>  \n", argv[0]);
57     fprintf(stderr, "OR\n");
58     fprintf(stderr, "Usage: %s <FQDN>|<IP address> <port> \n", argv[0]);
59     exit(-1);
60   }
61
62   /**************************************************************************/
63   /* Initialize                                                             */
64   /**************************************************************************/
65   if(evel_initialize(fqdn,                         /* FQDN                  */
66                      port,                         /* Port                  */
67                      fqdn2,                        /* Backup FQDN           */
68                      port2,                        /* Backup port           */
69                      NULL,                         /* optional path         */
70                      NULL,                         /* optional topic        */
71                      100,                          /* Ring Buffer size      */
72                      0,                            /* HTTPS?                */
73                      NULL,                         /* cert file             */
74                      NULL,                         /* key  file             */
75                      NULL,                         /* ca   info             */
76                      NULL,                         /* ca   file             */
77                      0,                            /* verify peer           */
78                      0,                            /* verify host           */
79                      "sample1",                    /* Username              */
80                      "sample1",                    /* Password              */
81                      "sample1",                    /* Username2             */
82                      "sample1",                    /* Password2             */
83                      NULL,                         /* Source ip             */
84                      NULL,                         /* Source ip2            */
85                      EVEL_SOURCE_VIRTUAL_MACHINE,  /* Source type           */
86                      "vHeartbeat",                 /* Role                  */
87                      1))                           /* Verbosity             */
88   {
89     fprintf(stderr, "\nFailed to initialize the EVEL library!!!\n");
90     exit(-1);
91   }
92   else
93   {
94     printf("\nInitialization completed\n");
95   }
96
97   /* Initialize and set thread detached attribute */
98   pthread_attr_init(&attr);
99   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
100
101   printf("Main:Creating thread \n");
102   rc = pthread_create(&hb_thread, NULL, HeartbeatThread, &i);
103   if (rc)
104   {
105     printf("ERROR; return code from pthread_create() is %d\n", rc);
106     exit(-1);
107   }
108   printf("Main:Created HB thread \n");
109
110   pthread_join(hb_thread, NULL);
111
112   evel_terminate();
113   printf("Terminated\n");
114   return 0;
115 }
116
117 void *HeartbeatThread(void *threadarg)
118 {
119
120   EVENT_HEARTBEAT_FIELD * event = NULL;
121   EVENT_HEADER* hb_header = NULL;
122   EVEL_ERR_CODES evel_rc = EVEL_SUCCESS;
123
124   int numToken;
125   jsmntok_t tokens[MAX_TOKENS];
126   char js[MAX_BUFFER_SIZE]; 
127
128   jsmn_parser p;
129   char ch[BUFSIZE];
130   int ret = 0;
131   char stringVal[BUFSIZE];
132   char eName[BUFSIZE];
133   int hb_interval;
134
135   char event_id1[10] = "heartbeat";
136   char event_id2[15] = {0};
137   int event_id3 = 0;
138   char event_id[BUFSIZE] = {0};
139
140   sleep(1);
141   printf("Running HB thread \n");
142   fflush(stdout);
143
144   while(1)
145   {
146
147      FILE * file = fopen("hb_config.json", "r");
148
149      memset(js, 0, MAX_BUFFER_SIZE);
150      memset(ch, 0, BUFSIZE);
151
152      while((fgets(ch, (BUFSIZE-1), file)) !=NULL)
153      {
154         strcat(js, ch);
155         memset(ch, 0, BUFSIZE);
156      }
157 //   printf("the file content is \n %s \n", js);
158
159      jsmn_init(&p);
160      numToken = jsmn_parse(&p, js, strlen(js), tokens, MAX_TOKENS);
161      printf("Token count-%d\n", numToken);
162
163 //   printToken(js,tokens, numToken);
164
165      ret = getStringToken(js, tokens, numToken, "eventName", "tmp_directParameters", eName, BUFSIZE);
166      if (ret != 0)
167      {
168         printf("Missing mandatory parameters - eventName is not there in tmp_directParameters. Exiting...\n");
169         exit(1);
170      }
171
172      ret = getIntToken(js, tokens, numToken, "heartbeatInterval", "tmp_directParameters", &hb_interval);
173      if (ret != 0)
174      {
175         printf("The parameter heartbeatInterval is not defined, defaulted to 60 seconds\n");
176         hb_interval = 60;
177      }
178
179      /***************************************************************************/
180      /* Heartbeat                                                               */
181      /***************************************************************************/
182      memset(event_id, 0, BUFSIZE);
183      memset(event_id2, 0, 15);
184      sprintf(event_id2, "%09d", event_id3++);
185      strcat(event_id, event_id1);
186      strcat(event_id, event_id2);
187
188      event = evel_new_heartbeat_field(hb_interval, eName, event_id);
189      if (event != NULL)
190      {
191        hb_header = (EVENT_HEADER *)event;
192
193        ret = getStringToken(js, tokens, numToken, "eventType", "tmp_directParameters", stringVal, BUFSIZE);
194        if (ret == 0)
195        {
196          evel_header_type_set(&event->header, stringVal);
197        }
198
199        struct timeval tv_now;
200        gettimeofday(&tv_now, NULL);
201        unsigned long long epoch_now = tv_now.tv_usec + 1000000 * tv_now.tv_sec;
202
203        evel_start_epoch_set(&event->header, epoch_start);
204        evel_last_epoch_set(&event->header, epoch_now);
205        epoch_start = epoch_now;
206
207        ret = getStringToken(js, tokens, numToken, "nfcNamingCode", "tmp_directParameters", stringVal, BUFSIZE);
208        if (ret == 0)
209          evel_nfcnamingcode_set(&event->header, stringVal);
210  
211        ret = getStringToken(js, tokens, numToken, "nfNamingCode", "tmp_directParameters", stringVal, BUFSIZE);
212        if (ret == 0)
213          evel_nfnamingcode_set(&event->header, stringVal);
214
215        ret = getStringToken(js, tokens, numToken, "reportingEntityName", "tmp_directParameters", stringVal, BUFSIZE);
216        if (ret == 0)
217          evel_reporting_entity_name_set(&event->header, stringVal);
218        else 
219        {
220           printf("Missing mandatory parameters - reportingEntityName is not there in tmp_directParameters\n");
221           printf("Defaulting reportingEntityName to hostname\n");
222        }
223
224        ret = getStringToken(js, tokens, numToken, "reportingEntityId", "tmp_directParameters", stringVal, BUFSIZE);
225        if (ret == 0)
226          evel_reporting_entity_id_set(&event->header, stringVal);
227
228        ret = getStringToken(js, tokens, numToken, "sourceId", "tmp_directParameters", stringVal, BUFSIZE);
229        if (ret == 0)
230          evel_source_id_set(&event->header, stringVal);
231
232        ret = getStringToken(js, tokens, numToken, "sourceName", "tmp_directParameters", stringVal, BUFSIZE);
233        if (ret == 0)
234          evel_source_name_set(&event->header, stringVal);
235        else 
236        {
237           printf("Missing mandatory parameters - sourceName is not there in tmp_directParameters\n");
238           printf("Defaulting sourceName to hostname\n");
239        }
240
241        evel_rc = evel_post_event(hb_header);
242        if (evel_rc != EVEL_SUCCESS)
243        {
244          EVEL_ERROR("Post failed %d (%s)", evel_rc, evel_error_string());
245        }
246     }
247     else
248     {
249       EVEL_ERROR("New Heartbeat failed");
250     }
251     printf("   Processed Heartbeat\n");
252
253     sleep(hb_interval);
254   }
255 }