08f3f600850c66ea23e275eac732b67c3dfbca0a
[ric-plt/e2.git] / RIC-E2-TERMINATION / sctpThread.cpp
1 // Copyright 2019 AT&T Intellectual Property
2 // Copyright 2019 Nokia
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
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 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
17 //  platform project (RICP).
18
19 // TODO: High-level file comment.
20
21
22
23 #include <3rdparty/oranE2/RANfunctions-List.h>
24 #include "sctpThread.h"
25 #include "BuildRunName.h"
26 #include <unistd.h>
27 //#include "3rdparty/oranE2SM/E2SM-gNB-NRT-RANfunction-Definition.h"
28 //#include "BuildXml.h"
29 //#include "pugixml/src/pugixml.hpp"
30 #include <pthread.h>
31 #include <sys/time.h>
32 #include <sys/inotify.h>
33 #include <errno.h>
34 #include <sys/stat.h>
35
36 using namespace std;
37 //using namespace std::placeholders;
38 using namespace boost::filesystem;
39 using namespace prometheus;
40
41
42 //#ifdef __cplusplus
43 //extern "C"
44 //{
45 //#endif
46
47 // need to expose without the include of gcov
48 extern "C" void __gcov_flush(void);
49 #define LOG_FILE_CONFIG_MAP "CONFIG_MAP_NAME"
50
51 static void catch_function(int signal) {
52     __gcov_flush();
53     exit(signal);
54 }
55
56
57 BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger, src::logger_mt)
58
59 boost::shared_ptr<sinks::synchronous_sink<sinks::text_file_backend>> boostLogger;
60 double cpuClock = 0.0;
61 bool jsonTrace = false;
62
63
64 static int enable_log_change_notify(const char* fileName)
65 {
66     int ret = -1;
67     struct stat fileInfo;
68     if ( lstat(fileName,&fileInfo) == 0 )
69     {
70         ret = register_log_change_notify(fileName);
71     }
72     return ret;
73 }
74
75
76 static int register_log_change_notify(const char *fileName)
77 {
78     pthread_attr_t cb_attr;
79     pthread_t tid;
80     pthread_attr_init(&cb_attr);
81     pthread_attr_setdetachstate(&cb_attr,PTHREAD_CREATE_DETACHED);
82     return pthread_create(&tid, &cb_attr,&monitor_loglevel_change_handler,(void *)fileName);
83 }
84
85
86 static void * monitor_loglevel_change_handler(void* arg)
87 {
88     char *fileName = (char*) arg;
89     int ifd;                   // the inotify file des
90     int wfd;                   // the watched file des
91     ssize_t n = 0;
92     char rbuf[4096];           // large read buffer as the event is var len
93     fd_set fds;
94     int res = 0;
95     struct timeval timeout;
96     char* dname=NULL;          // directory name
97     char* bname = NULL;        // basename
98     char* tok=NULL;
99     char* log_level=NULL;
100
101     dname = strdup( fileName); // defrock the file name into dir and basename
102     if( (tok = strrchr( dname, '/' )) != NULL ) {
103         *tok = '\0';
104         bname = strdup( tok+1 );
105     }
106
107
108     ifd = inotify_init1( 0 ); // initialise watcher setting blocking read (no option)
109     if( ifd < 0 ) {
110         fprintf( stderr, "### ERR ### unable to initialise file watch %s\n", strerror( errno ) );
111     } else {
112         wfd = inotify_add_watch( ifd, dname, IN_MOVED_TO | IN_CLOSE_WRITE ); // we only care about close write changes
113
114         if( wfd < 0 ) {
115             fprintf( stderr, "### ERR ### unable to add watch on config file %s: %s\n", fileName, strerror( errno ) );
116         } else {
117            
118
119             memset( &timeout, 0, sizeof(timeout) );
120             while( 1 ) {
121                 FD_ZERO (&fds);
122                 FD_SET (ifd, &fds);
123                 timeout.tv_sec=1;
124                 res = select (ifd + 1, &fds, NULL, NULL, &timeout);
125                 if(res)
126                 {
127                     n = read( ifd, rbuf, sizeof( rbuf ) ); // read the event
128                     if( n < 0  ) {
129 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))                        
130                         if( errno == EAGAIN ) {
131                         } else {
132                             fprintf( stderr, "### CRIT ### config listener read err: %s\n", strerror( errno ) );
133                         }
134                         continue;
135 #endif                        
136                     }
137
138                     //Retrieving Log Level from configmap by parsing configmap file
139                     log_level = parse_file(fileName);
140                     update_mdc_log_level_severity(log_level); //setting log level
141                     free(log_level);
142                 }
143             }
144             inotify_rm_watch(ifd,wfd);
145         }
146         close(ifd);
147     }
148     free(bname);
149     free(dname);
150
151     pthread_exit(NULL);
152 }
153
154 void  update_mdc_log_level_severity(char* log_level)
155 {
156     mdclog_severity_t level = MDCLOG_ERR;
157
158     if(log_level == NULL)
159     {
160         printf("### ERR ### Invalid Log-Level Configuration in ConfigMap, Default Log-Level Applied:   %d\n",level);
161     }
162     else if(strcasecmp(log_level,"1")==0)
163     {
164         level = MDCLOG_ERR;
165     }
166     else if(strcasecmp(log_level,"2")==0)
167     {
168         level = MDCLOG_WARN;
169     }
170     else if(strcasecmp(log_level,"3")==0)
171     {
172         level = MDCLOG_INFO;
173     }
174     else if(strcasecmp(log_level,"4")==0)
175     {
176         level = MDCLOG_DEBUG;
177     }
178
179     mdclog_level_set(level);
180 }
181 static char* parse_file(char* filename)
182 {
183     char *token=NULL;
184     char *search = ": ";
185     char *string_match = "log-level";
186     bool found = false;
187     FILE *file = fopen ( filename, "r" );
188     if ( file != NULL )
189     {
190         char line [ 128 ];
191         while ( fgets ( line, sizeof line, file ) != NULL )
192         {
193             token = strtok(line, search);
194             if(strcmp(token,string_match)==0)
195             {
196                 found = true;
197                 token = strtok(NULL, search);
198                 token = strtok(token, "\n");//removing newline if any
199                 break;
200             }
201         }
202         fclose ( file );
203      }
204      if(found)
205          return(strdup(token));
206      else
207          return(NULL);
208 }
209
210 char *read_env_param(const char*envkey)
211 {
212     if(envkey)
213     {
214         char *value = getenv(envkey);
215         if(value)
216             return strdup(value);
217     }
218     return NULL;
219 }
220
221 void dynamic_log_level_change()
222 {
223     char *logFile_Name = read_env_param(LOG_FILE_CONFIG_MAP);
224     char* log_level_init=NULL;
225     if(logFile_Name)
226     {
227         log_level_init = parse_file(logFile_Name);
228         update_mdc_log_level_severity(log_level_init); //setting log level
229         free(log_level_init);
230
231     }
232     enable_log_change_notify(logFile_Name);
233     free(logFile_Name);
234
235 }
236
237 void init_log() {
238     int log_change_monitor = 0;
239     mdclog_attr_t *attr;
240     mdclog_attr_init(&attr);
241     mdclog_attr_set_ident(attr, "E2Terminator");
242     mdclog_init(attr);
243     if(mdclog_format_initialize(log_change_monitor)!=0)
244         mdclog_write(MDCLOG_ERR, "Failed to intialize MDC log format !!!");
245     dynamic_log_level_change();
246     mdclog_attr_destroy(attr);
247 }
248 auto start_time = std::chrono::high_resolution_clock::now();
249 typedef std::chrono::duration<double, std::ratio<1,1>> seconds_t;
250
251 double age() {
252     return seconds_t(std::chrono::high_resolution_clock::now() - start_time).count();
253 }
254
255 double approx_CPU_MHz(unsigned sleepTime) {
256     using namespace std::chrono_literals;
257     uint32_t aux = 0;
258     uint64_t cycles_start = rdtscp(aux);
259     double time_start = age();
260     std::this_thread::sleep_for(sleepTime * 1ms);
261     uint64_t elapsed_cycles = rdtscp(aux) - cycles_start;
262     double elapsed_time = age() - time_start;
263     return elapsed_cycles / elapsed_time;
264 }
265
266 //std::atomic<int64_t> rmrCounter{0};
267 std::atomic<int64_t> num_of_messages{0};
268 std::atomic<int64_t> num_of_XAPP_messages{0};
269 static long transactionCounter = 0;
270
271 int buildListeningPort(sctp_params_t &sctpParams) {
272     sctpParams.listenFD = socket(AF_INET6, SOCK_STREAM, IPPROTO_SCTP);
273     if (sctpParams.listenFD <= 0) {
274 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))        
275         mdclog_write(MDCLOG_ERR, "Error Opening socket, %s", strerror(errno));
276         return -1;
277 #endif        
278     }
279
280     struct sockaddr_in6 serverAddress {};
281     serverAddress.sin6_family = AF_INET6;
282     serverAddress.sin6_addr   = in6addr_any;
283     serverAddress.sin6_port = htons(sctpParams.sctpPort);
284     if (bind(sctpParams.listenFD, (SA *)&serverAddress, sizeof(serverAddress)) < 0 ) {
285 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))        
286         mdclog_write(MDCLOG_ERR, "Error binding port %d. %s", sctpParams.sctpPort, strerror(errno));
287         return -1;
288 #endif        
289     }
290     if (setSocketNoBlocking(sctpParams.listenFD) == -1) {
291         //mdclog_write(MDCLOG_ERR, "Error binding. %s", strerror(errno));
292         return -1;
293     }
294     if (mdclog_level_get() >= MDCLOG_DEBUG) {
295         struct sockaddr_in6 clientAddress {};
296         socklen_t len = sizeof(clientAddress);
297         getsockname(sctpParams.listenFD, (SA *)&clientAddress, &len);
298         char buff[1024] {};
299         inet_ntop(AF_INET6, &clientAddress.sin6_addr, buff, sizeof(buff));
300         mdclog_write(MDCLOG_DEBUG, "My address: %s, port %d\n", buff, htons(clientAddress.sin6_port));
301     }
302
303     if (listen(sctpParams.listenFD, SOMAXCONN) < 0) {
304 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))     
305         mdclog_write(MDCLOG_ERR, "Error listening. %s\n", strerror(errno));
306         return -1;
307 #endif        
308     }
309     struct epoll_event event {};
310     event.events = EPOLLIN | EPOLLET;
311     event.data.fd = sctpParams.listenFD;
312
313     // add listening port to epoll
314     if (epoll_ctl(sctpParams.epoll_fd, EPOLL_CTL_ADD, sctpParams.listenFD, &event)) {
315 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
316         printf("Failed to add descriptor to epoll\n");
317         mdclog_write(MDCLOG_ERR, "Failed to add descriptor to epoll. %s\n", strerror(errno));
318         return -1;
319 #endif        
320     }
321
322     return 0;
323 }
324
325 int buildConfiguration(sctp_params_t &sctpParams) {
326     path p = (sctpParams.configFilePath + "/" + sctpParams.configFileName).c_str();
327     if (exists(p)) {
328         const int size = 2048;
329         auto fileSize = file_size(p);
330         if (fileSize > size) {
331 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))            
332             mdclog_write(MDCLOG_ERR, "File %s larger than %d", p.string().c_str(), size);
333             return -1;
334 #endif            
335         }
336     } else {
337 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))        
338         mdclog_write(MDCLOG_ERR, "Configuration File %s not exists", p.string().c_str());
339         return -1;
340 #endif        
341     }
342
343     ReadConfigFile conf;
344     if (conf.openConfigFile(p.string()) == -1) {
345 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))        
346         mdclog_write(MDCLOG_ERR, "Filed to open config file %s, %s",
347                      p.string().c_str(), strerror(errno));
348         return -1;
349 #endif        
350     }
351     int rmrPort = conf.getIntValue("nano");
352     if (rmrPort == -1) {
353 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
354         mdclog_write(MDCLOG_ERR, "illegal RMR port ");
355         return -1;
356 #endif        
357     }
358     sctpParams.rmrPort = (uint16_t)rmrPort;
359     snprintf(sctpParams.rmrAddress, sizeof(sctpParams.rmrAddress), "%d", (int) (sctpParams.rmrPort));
360     auto tmpStr = conf.getStringValue("volume");
361     if (tmpStr.length() == 0) {
362 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))        
363         mdclog_write(MDCLOG_ERR, "illegal volume.");
364         return -1;
365 #endif        
366     }
367
368     char tmpLogFilespec[VOLUME_URL_SIZE];
369     tmpLogFilespec[0] = 0;
370     sctpParams.volume[0] = 0;
371     snprintf(sctpParams.volume, VOLUME_URL_SIZE, "%s", tmpStr.c_str());
372     // copy the name to temp file as well
373     snprintf(tmpLogFilespec, VOLUME_URL_SIZE, "%s", tmpStr.c_str());
374
375
376     // define the file name in the tmp directory under the volume
377     strcat(tmpLogFilespec,"/tmp/E2Term_%Y-%m-%d_%H-%M-%S.%N.tmpStr");
378
379     sctpParams.myIP = conf.getStringValue("local-ip");
380     if (sctpParams.myIP.length() == 0) {
381 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))        
382         mdclog_write(MDCLOG_ERR, "illegal local-ip.");
383         return -1;
384 #endif        
385     }
386
387     int sctpPort = conf.getIntValue("sctp-port");
388     if (sctpPort == -1) {
389 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))        
390         mdclog_write(MDCLOG_ERR, "illegal SCTP port ");
391         return -1;
392 #endif        
393     }
394     sctpParams.sctpPort = (uint16_t)sctpPort;
395
396     sctpParams.fqdn = conf.getStringValue("external-fqdn");
397     if (sctpParams.fqdn.length() == 0) {
398 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))        
399         mdclog_write(MDCLOG_ERR, "illegal external-fqdn");
400         return -1;
401 #endif        
402     }
403
404     std::string pod = conf.getStringValue("pod_name");
405 #ifndef UNIT_TEST
406     if (pod.length() == 0) {
407         mdclog_write(MDCLOG_ERR, "illegal pod_name in config file");
408         return -1;
409     }
410     auto *podName = getenv(pod.c_str());
411     if (podName == nullptr) {
412         mdclog_write(MDCLOG_ERR, "illegal pod_name or environment variable not exists : %s", pod.c_str());
413         return -1;
414
415     } else {
416         sctpParams.podName.assign(podName);
417         if (sctpParams.podName.length() == 0) {
418             mdclog_write(MDCLOG_ERR, "illegal pod_name");
419             return -1;
420         }
421     }
422 #endif
423     tmpStr = conf.getStringValue("trace");
424     transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
425     if ((tmpStr.compare("start")) == 0) {
426 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))        
427         mdclog_write(MDCLOG_INFO, "Trace set to: start");
428         sctpParams.trace = true;
429 #endif        
430     } else if ((tmpStr.compare("stop")) == 0) {
431         mdclog_write(MDCLOG_INFO, "Trace set to: stop");
432         sctpParams.trace = false;
433     } else {
434 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))      
435         mdclog_write(MDCLOG_ERR, "Trace was set to wrong value %s, set to stop", tmpStr.c_str());
436         sctpParams.trace = false;
437 #endif        
438     }
439     jsonTrace = sctpParams.trace;
440
441     sctpParams.epollTimeOut = -1;
442
443     tmpStr = conf.getStringValue("prometheusPort");
444     if (tmpStr.length() != 0) {
445         sctpParams.prometheusPort = tmpStr;
446     }
447
448     sctpParams.ka_message_length = snprintf(sctpParams.ka_message, KA_MESSAGE_SIZE, "{\"address\": \"%s:%d\","
449                                                                                     "\"fqdn\": \"%s\","
450                                                                                     "\"pod_name\": \"%s\"}",
451                                             (const char *)sctpParams.myIP.c_str(),
452                                             sctpParams.rmrPort,
453                                             sctpParams.fqdn.c_str(),
454                                             sctpParams.podName.c_str());
455
456     if (mdclog_level_get() >= MDCLOG_INFO) {
457         mdclog_write(MDCLOG_DEBUG,"RMR Port: %s", to_string(sctpParams.rmrPort).c_str());
458         mdclog_write(MDCLOG_DEBUG,"LogLevel: %s", to_string(sctpParams.logLevel).c_str());
459         mdclog_write(MDCLOG_DEBUG,"volume: %s", sctpParams.volume);
460         mdclog_write(MDCLOG_DEBUG,"tmpLogFilespec: %s", tmpLogFilespec);
461         mdclog_write(MDCLOG_DEBUG,"my ip: %s", sctpParams.myIP.c_str());
462         mdclog_write(MDCLOG_DEBUG,"pod name: %s", sctpParams.podName.c_str());
463
464         mdclog_write(MDCLOG_INFO, "running parameters for instance : %s", sctpParams.ka_message);
465     }
466
467     // Files written to the current working directory
468     boostLogger = logging::add_file_log(
469             keywords::file_name = tmpLogFilespec, // to temp directory
470             keywords::rotation_size = 10 * 1024 * 1024,
471             keywords::time_based_rotation = sinks::file::rotation_at_time_interval(posix_time::hours(1)),
472             keywords::format = "%Message%"
473             //keywords::format = "[%TimeStamp%]: %Message%" // use each tmpStr with time stamp
474     );
475
476     // Setup a destination folder for collecting rotated (closed) files --since the same volume can use rename()
477     boostLogger->locked_backend()->set_file_collector(sinks::file::make_collector(
478             keywords::target = sctpParams.volume
479     ));
480
481     // Upon restart, scan the directory for files matching the file_name pattern
482     boostLogger->locked_backend()->scan_for_files();
483
484     // Enable auto-flushing after each tmpStr record written
485     if (mdclog_level_get() >= MDCLOG_DEBUG) {
486         boostLogger->locked_backend()->auto_flush(true);
487     }
488
489     return 0;
490 }
491
492 void startPrometheus(sctp_params_t &sctpParams) {
493     sctpParams.prometheusFamily = &BuildCounter()
494             .Name("E2T")
495             .Help("E2T message counter")
496             .Labels({{"POD_NAME", sctpParams.podName}})
497             .Register(*sctpParams.prometheusRegistry);
498
499     string prometheusPath = sctpParams.prometheusPort + "," + "[::]:" + sctpParams.prometheusPort;
500     if (mdclog_level_get() >= MDCLOG_DEBUG) {
501         mdclog_write(MDCLOG_DEBUG, "Start Prometheus Pull mode on %s", prometheusPath.c_str());
502     }
503     sctpParams.prometheusExposer = new Exposer(prometheusPath, 1);
504     sctpParams.prometheusExposer->RegisterCollectable(sctpParams.prometheusRegistry);
505 }
506 #ifndef UNIT_TEST
507
508 int main(const int argc, char **argv) {
509     sctp_params_t sctpParams;
510     {
511         std::random_device device{};
512         std::mt19937 generator(device());
513         std::uniform_int_distribution<long> distribution(1, (long) 1e12);
514         transactionCounter = distribution(generator);
515     }
516
517 //    uint64_t st = 0;
518 //    uint32_t aux1 = 0;
519 //   st = rdtscp(aux1);
520
521     unsigned num_cpus = std::thread::hardware_concurrency();
522     init_log();
523     if (std::signal(SIGINT, catch_function) == SIG_ERR) {
524         mdclog_write(MDCLOG_ERR, "Error initializing SIGINT");
525         exit(1);
526     }
527     if (std::signal(SIGABRT, catch_function)== SIG_ERR) {
528         mdclog_write(MDCLOG_ERR, "Error initializing SIGABRT");
529         exit(1);
530     }
531     if (std::signal(SIGTERM, catch_function)== SIG_ERR) {
532         mdclog_write(MDCLOG_ERR, "Error initializing SIGTERM");
533         exit(1);
534     }
535
536     cpuClock = approx_CPU_MHz(100);
537
538     mdclog_write(MDCLOG_DEBUG, "CPU speed %11.11f", cpuClock);
539
540     auto result = parse(argc, argv, sctpParams);
541
542     if (buildConfiguration(sctpParams) != 0) {
543         exit(-1);
544     }
545
546     //auto registry = std::make_shared<Registry>();
547     sctpParams.prometheusRegistry = std::make_shared<Registry>();
548
549     //sctpParams.prometheusFamily = new Family<Counter>("E2T", "E2T message counter", {{"E", sctpParams.podName}});
550
551     startPrometheus(sctpParams);
552
553     // start epoll
554     sctpParams.epoll_fd = epoll_create1(0);
555     if (sctpParams.epoll_fd == -1) {
556         mdclog_write(MDCLOG_ERR, "failed to open epoll descriptor");
557         exit(-1);
558     }
559     getRmrContext(sctpParams);
560     if (sctpParams.rmrCtx == nullptr) {
561         close(sctpParams.epoll_fd);
562         exit(-1);
563     }
564
565     if (buildInotify(sctpParams) == -1) {
566         close(sctpParams.rmrListenFd);
567         rmr_close(sctpParams.rmrCtx);
568         close(sctpParams.epoll_fd);
569         exit(-1);
570     }
571
572     if (buildListeningPort(sctpParams) != 0) {
573         close(sctpParams.rmrListenFd);
574         rmr_close(sctpParams.rmrCtx);
575         close(sctpParams.epoll_fd);
576         exit(-1);
577     }
578
579     sctpParams.sctpMap = new mapWrapper();
580
581     std::vector<std::thread> threads(num_cpus);
582 //    std::vector<std::thread> threads;
583
584     num_cpus = 3;
585     for (unsigned int i = 0; i < num_cpus; i++) {
586         threads[i] = std::thread(listener, &sctpParams);
587
588         cpu_set_t cpuset;
589         CPU_ZERO(&cpuset);
590         CPU_SET(i, &cpuset);
591         int rc = pthread_setaffinity_np(threads[i].native_handle(), sizeof(cpu_set_t), &cpuset);
592         if (rc != 0) {
593             mdclog_write(MDCLOG_ERR, "Error calling pthread_setaffinity_np: %d", rc);
594         }
595     }
596
597
598     //loop over term_init until first message from xApp
599     handleTermInit(sctpParams);
600
601     for (auto &t : threads) {
602         t.join();
603     }
604
605     return 0;
606 }
607 #endif
608 void handleTermInit(sctp_params_t &sctpParams) {
609     sendTermInit(sctpParams);
610     //send to e2 manager init of e2 term
611     //E2_TERM_INIT
612
613     int count = 0;
614     while (true) {
615         auto xappMessages = num_of_XAPP_messages.load(std::memory_order_acquire);
616         if (xappMessages > 0) {
617             if (mdclog_level_get() >=  MDCLOG_INFO) {
618                 mdclog_write(MDCLOG_INFO, "Got a message from some application, stop sending E2_TERM_INIT");
619             }
620             return;
621         }
622         usleep(100000);
623         count++;
624         if (count % 1000 == 0) {
625             mdclog_write(MDCLOG_ERR, "GOT No messages from any xApp");
626             sendTermInit(sctpParams);
627         }
628     }
629 }
630
631 void sendTermInit(sctp_params_t &sctpParams) {
632     rmr_mbuf_t *msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.ka_message_length);
633     auto count = 0;
634     while (true) {
635         msg->mtype = E2_TERM_INIT;
636         msg->state = 0;
637         rmr_bytes2payload(msg, (unsigned char *)sctpParams.ka_message, sctpParams.ka_message_length);
638         static unsigned char tx[32];
639         auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
640         rmr_bytes2xact(msg, tx, txLen);
641         msg = rmr_send_msg(sctpParams.rmrCtx, msg);
642         if (msg == nullptr) {
643             msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.ka_message_length);
644         } else if (msg->state == 0) {
645             rmr_free_msg(msg);
646             if (mdclog_level_get() >=  MDCLOG_INFO) {
647                 mdclog_write(MDCLOG_INFO, "E2_TERM_INIT successfully sent ");
648             }
649             return;
650         } else {
651             if (count % 100 == 0) {
652                 mdclog_write(MDCLOG_ERR, "Error sending E2_TERM_INIT cause : %s ", translateRmrErrorMessages(msg->state).c_str());
653             }
654             sleep(1);
655         }
656         count++;
657     }
658 }
659
660 /**
661  *
662  * @param argc
663  * @param argv
664  * @param sctpParams
665  * @return
666  */
667 cxxopts::ParseResult parse(int argc, char *argv[], sctp_params_t &sctpParams) {
668     cxxopts::Options options(argv[0], "e2 term help");
669     options.positional_help("[optional args]").show_positional_help();
670     options.allow_unrecognised_options().add_options()
671             ("p,path", "config file path", cxxopts::value<std::string>(sctpParams.configFilePath)->default_value("config"))
672             ("f,file", "config file name", cxxopts::value<std::string>(sctpParams.configFileName)->default_value("config.conf"))
673             ("h,help", "Print help");
674
675     auto result = options.parse(argc, (const char **&)argv);
676
677     if (result.count("help")) {
678         std::cout << options.help({""}) << std::endl;
679         exit(0);
680     }
681     return result;
682 }
683
684 /**
685  *
686  * @param sctpParams
687  * @return -1 failed 0 success
688  */
689 int buildInotify(sctp_params_t &sctpParams) {
690     sctpParams.inotifyFD = inotify_init1(IN_NONBLOCK);
691     if (sctpParams.inotifyFD == -1) {
692         mdclog_write(MDCLOG_ERR, "Failed to init inotify (inotify_init1) %s", strerror(errno));
693         return -1;
694     }
695
696     sctpParams.inotifyWD = inotify_add_watch(sctpParams.inotifyFD,
697                                              (const char *)sctpParams.configFilePath.c_str(),
698                                              (unsigned)IN_OPEN | (unsigned)IN_CLOSE_WRITE | (unsigned)IN_CLOSE_NOWRITE); //IN_CLOSE = (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)
699     if (sctpParams.inotifyWD == -1) {
700         mdclog_write(MDCLOG_ERR, "Failed to add directory : %s to  inotify (inotify_add_watch) %s",
701                      sctpParams.configFilePath.c_str(),
702                      strerror(errno));
703         close(sctpParams.inotifyFD);
704         return -1;
705     }
706
707     struct epoll_event event{};
708     event.events = (EPOLLIN);
709     event.data.fd = sctpParams.inotifyFD;
710     // add listening RMR FD to epoll
711     if (epoll_ctl(sctpParams.epoll_fd, EPOLL_CTL_ADD, sctpParams.inotifyFD, &event)) {
712         mdclog_write(MDCLOG_ERR, "Failed to add inotify FD to epoll");
713         close(sctpParams.inotifyFD);
714         return -1;
715     }
716     return 0;
717 }
718
719 /**
720  *
721  * @param args
722  * @return
723  */
724 void listener(sctp_params_t *params) {
725     int num_of_SCTP_messages = 0;
726     auto totalTime = 0.0;
727     std::thread::id this_id = std::this_thread::get_id();
728     //save cout
729     auto pod_name = std::getenv("POD_NAME");
730     auto container_name = std::getenv("CONTAINER_NAME");
731     auto service_name = std::getenv("SERVICE_NAME");
732     auto host_name = std::getenv("HOST_NAME");
733     auto system_name = std::getenv("SYSTEM_NAME");
734     auto pid = std::to_string(getpid()).c_str();
735     streambuf *oldCout = cout.rdbuf();
736     ostringstream memCout;
737     // create new cout
738     cout.rdbuf(memCout.rdbuf());
739     cout << this_id;
740     //return to the normal cout
741     cout.rdbuf(oldCout);
742
743     char tid[32];
744     memcpy(tid, memCout.str().c_str(), memCout.str().length() < 32 ? memCout.str().length() : 31);
745     tid[memCout.str().length()] = 0;
746     mdclog_mdc_add("SYSTEM_NAME", system_name);
747     mdclog_mdc_add("HOST_NAME", host_name);
748     mdclog_mdc_add("SERVICE_NAME", service_name);
749     mdclog_mdc_add("CONTAINER_NAME", container_name);
750     mdclog_mdc_add("POD_NAME", pod_name);
751     mdclog_mdc_add("PID", pid);
752
753     if (mdclog_level_get() >= MDCLOG_DEBUG) {
754         mdclog_write(MDCLOG_DEBUG, "started thread number %s", tid);
755     }
756
757     RmrMessagesBuffer_t rmrMessageBuffer{};
758     //create and init RMR
759     rmrMessageBuffer.rmrCtx = params->rmrCtx;
760
761     auto *events = (struct epoll_event *) calloc(MAXEVENTS, sizeof(struct epoll_event));
762     struct timespec end{0, 0};
763     struct timespec start{0, 0};
764
765     rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
766     rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
767
768     memcpy(rmrMessageBuffer.ka_message, params->ka_message, params->ka_message_length);
769     rmrMessageBuffer.ka_message_len = params->ka_message_length;
770     rmrMessageBuffer.ka_message[rmrMessageBuffer.ka_message_len] = 0;
771
772     if (mdclog_level_get() >= MDCLOG_DEBUG) {
773         mdclog_write(MDCLOG_DEBUG, "keep alive message is : %s", rmrMessageBuffer.ka_message);
774     }
775
776     ReportingMessages_t message {};
777
778 //    for (int i = 0; i < MAX_RMR_BUFF_ARRAY; i++) {
779 //        rmrMessageBuffer.rcvBufferedMessages[i] = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
780 //        rmrMessageBuffer.sendBufferedMessages[i] = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
781 //    }
782
783     while (true) {
784         if (mdclog_level_get() >= MDCLOG_DEBUG) {
785             mdclog_write(MDCLOG_DEBUG, "Start EPOLL Wait. Timeout = %d", params->epollTimeOut);
786         }
787 #ifndef UNIT_TEST
788         auto numOfEvents = epoll_wait(params->epoll_fd, events, MAXEVENTS, params->epollTimeOut);
789 #else
790         auto numOfEvents = 1;
791 #endif
792         if (numOfEvents == 0) { // time out
793 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))            
794             if (mdclog_level_get() >= MDCLOG_DEBUG) {
795                 mdclog_write(MDCLOG_DEBUG, "got epoll timeout");
796             }
797             continue;
798         } else if (numOfEvents < 0) {
799             if (errno == EINTR) {
800                 if (mdclog_level_get() >= MDCLOG_DEBUG) {
801                     mdclog_write(MDCLOG_DEBUG, "got EINTR : %s", strerror(errno));
802                 }
803                 continue;
804             }
805             mdclog_write(MDCLOG_ERR, "Epoll wait failed, errno = %s", strerror(errno));
806             return;
807 #endif            
808         }
809         for (auto i = 0; i < numOfEvents; i++) {
810             if (mdclog_level_get() >= MDCLOG_DEBUG) {
811                 mdclog_write(MDCLOG_DEBUG, "handling epoll event %d out of %d", i + 1, numOfEvents);
812             }
813             clock_gettime(CLOCK_MONOTONIC, &message.message.time);
814             start.tv_sec = message.message.time.tv_sec;
815             start.tv_nsec = message.message.time.tv_nsec;
816
817
818             if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP)) {
819                 handlepoll_error(events[i], message, rmrMessageBuffer, params);
820             } else if (events[i].events & EPOLLOUT) {
821                 handleEinprogressMessages(events[i], message, rmrMessageBuffer, params);
822             } else if (params->listenFD == events[i].data.fd) {
823                 if (mdclog_level_get() >= MDCLOG_INFO) {
824                     mdclog_write(MDCLOG_INFO, "New connection request from sctp network\n");
825                 }
826                 // new connection is requested from RAN  start build connection
827                 while (true) {
828                     struct sockaddr in_addr {};
829                     socklen_t in_len;
830                     char hostBuff[NI_MAXHOST];
831                     char portBuff[NI_MAXSERV];
832
833                     in_len = sizeof(in_addr);
834                     auto *peerInfo = (ConnectedCU_t *)calloc(1, sizeof(ConnectedCU_t));
835                     if(peerInfo == nullptr){
836                         mdclog_write(MDCLOG_ERR, "calloc failed");
837                         break;
838                     }
839                     peerInfo->sctpParams = params;
840                     peerInfo->fileDescriptor = accept(params->listenFD, &in_addr, &in_len);
841                     if (peerInfo->fileDescriptor == -1) {
842 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))                        
843                         if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
844                             /* We have processed all incoming connections. */
845                             if(peerInfo)
846                                 free(peerInfo);
847                             break;
848                         } else {
849                             if(peerInfo)
850                                 free(peerInfo);
851                             mdclog_write(MDCLOG_ERR, "Accept error, errno = %s", strerror(errno));
852                             break;
853                         }
854                     }
855                     if (setSocketNoBlocking(peerInfo->fileDescriptor) == -1) {
856                         mdclog_write(MDCLOG_ERR, "setSocketNoBlocking failed to set new connection %s on port %s\n", hostBuff, portBuff);
857                         close(peerInfo->fileDescriptor);
858                         if(peerInfo)
859                             free(peerInfo);
860                         break;
861 #endif                        
862                     }
863                     auto  ans = getnameinfo(&in_addr, in_len,
864                                             peerInfo->hostName, NI_MAXHOST,
865                                             peerInfo->portNumber, NI_MAXSERV, (unsigned )((unsigned int)NI_NUMERICHOST | (unsigned int)NI_NUMERICSERV));
866                     if (ans < 0) {
867                         mdclog_write(MDCLOG_ERR, "Failed to get info on connection request. %s\n", strerror(errno));
868                         close(peerInfo->fileDescriptor);
869                         if(peerInfo)
870                             free(peerInfo);
871                         break;
872                     }
873                     if (mdclog_level_get() >= MDCLOG_DEBUG) {
874                         mdclog_write(MDCLOG_DEBUG, "Accepted connection on descriptor %d (host=%s, port=%s)\n", peerInfo->fileDescriptor, peerInfo->hostName, peerInfo->portNumber);
875                     }
876                     peerInfo->isConnected = false;
877                     peerInfo->gotSetup = false;
878                     if (addToEpoll(params->epoll_fd,
879                                    peerInfo,
880                                    (EPOLLIN | EPOLLET),
881                                    params->sctpMap, nullptr,
882                                    0) != 0) {
883                         if(peerInfo)
884                             free(peerInfo);
885                         break;
886                     }
887                     break;
888                 }
889             } else if (params->rmrListenFd == events[i].data.fd) {
890                 // got message from XAPP
891                 //num_of_XAPP_messages.fetch_add(1, std::memory_order_release);
892                 num_of_messages.fetch_add(1, std::memory_order_release);
893                 if (mdclog_level_get() >= MDCLOG_DEBUG) {
894                     mdclog_write(MDCLOG_DEBUG, "new RMR message");
895                 }
896                 if (receiveXappMessages(params->sctpMap,
897                                         rmrMessageBuffer,
898                                         message.message.time) != 0) {
899                     mdclog_write(MDCLOG_ERR, "Error handling Xapp message");
900                 }
901             } else if (params->inotifyFD == events[i].data.fd) {
902                 mdclog_write(MDCLOG_INFO, "Got event from inotify (configuration update)");
903                 handleConfigChange(params);
904             } else {
905                 /* We RMR_ERR_RETRY have data on the fd waiting to be read. Read and display it.
906                  * We must read whatever data is available completely, as we are running
907                  *  in edge-triggered mode and won't get a notification again for the same data. */
908                 num_of_messages.fetch_add(1, std::memory_order_release);
909                 if (mdclog_level_get() >= MDCLOG_DEBUG) {
910                     mdclog_write(MDCLOG_DEBUG, "new message from SCTP, epoll flags are : %0x", events[i].events);
911                 }
912                 receiveDataFromSctp(&events[i],
913                                     params->sctpMap,
914                                     num_of_SCTP_messages,
915                                     rmrMessageBuffer,
916                                     message.message.time);
917             }
918
919             clock_gettime(CLOCK_MONOTONIC, &end);
920             if (mdclog_level_get() >= MDCLOG_INFO) {
921                 totalTime += ((end.tv_sec + 1.0e-9 * end.tv_nsec) -
922                               ((double) start.tv_sec + 1.0e-9 * start.tv_nsec));
923             }
924             if (mdclog_level_get() >= MDCLOG_DEBUG) {
925                 mdclog_write(MDCLOG_DEBUG, "message handling is %ld seconds %ld nanoseconds",
926                              end.tv_sec - start.tv_sec,
927                              end.tv_nsec - start.tv_nsec);
928             }
929         }
930 #ifdef UNIT_TEST
931     break;
932 #endif
933     }
934 }
935
936 /**
937  *
938  * @param sctpParams
939  */
940 void handleConfigChange(sctp_params_t *sctpParams) {
941     char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event))));
942     const struct inotify_event *event;
943     char *ptr;
944 #ifdef UNIT_TEST
945     struct inotify_event tmpEvent;
946 #endif
947     path p = (sctpParams->configFilePath + "/" + sctpParams->configFileName).c_str();
948     auto endlessLoop = true;
949     while (endlessLoop) {
950 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))    
951         auto len = read(sctpParams->inotifyFD, buf, sizeof buf);
952 #else
953     auto len=10;
954 #endif
955         if (len == -1) {
956 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))        
957             if (errno != EAGAIN) {
958                 mdclog_write(MDCLOG_ERR, "read %s ", strerror(errno));
959                 endlessLoop = false;
960                 continue;
961             }
962             else {
963                 endlessLoop = false;
964                 continue;
965             }
966 #endif            
967         }
968
969         for (ptr = buf; ptr < buf + len; ptr += sizeof(struct inotify_event) + event->len) {
970 #ifndef UNIT_TEST
971     event = (const struct inotify_event *)ptr;
972 #else
973     tmpEvent.mask = (uint32_t)IN_CLOSE_WRITE;
974     event = &tmpEvent;
975 #endif
976             if (event->mask & (uint32_t)IN_ISDIR) {
977                 continue;
978             }
979
980             // the directory name
981             if (sctpParams->inotifyWD == event->wd) {
982                 // not the directory
983             }
984             if (event->len) {
985 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))                
986                 auto  retVal = strcmp(sctpParams->configFileName.c_str(), event->name);
987                 if (retVal != 0) {
988                     continue;
989                 }
990 #endif                
991             }
992             // only the file we want
993             if (event->mask & (uint32_t)IN_CLOSE_WRITE) {
994                 if (mdclog_level_get() >= MDCLOG_INFO) {
995                     mdclog_write(MDCLOG_INFO, "Configuration file changed");
996                 }
997                 if (exists(p)) {
998                     const int size = 2048;
999                     auto fileSize = file_size(p);
1000                     if (fileSize > size) {
1001                         mdclog_write(MDCLOG_ERR, "File %s larger than %d", p.string().c_str(), size);
1002                         return;
1003                     }
1004                 } else {
1005                     mdclog_write(MDCLOG_ERR, "Configuration File %s not exists", p.string().c_str());
1006                     return;
1007                 }
1008
1009                 ReadConfigFile conf;
1010                 if (conf.openConfigFile(p.string()) == -1) {
1011                     mdclog_write(MDCLOG_ERR, "Filed to open config file %s, %s",
1012                                  p.string().c_str(), strerror(errno));
1013                     return;
1014                 }
1015                 auto tmpStr = conf.getStringValue("loglevel");
1016                 if (tmpStr.length() == 0) {
1017                     mdclog_write(MDCLOG_ERR, "illegal loglevel. Set loglevel to MDCLOG_INFO");
1018                     tmpStr = "info";
1019                 }
1020                 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
1021
1022                 if ((tmpStr.compare("debug")) == 0) {
1023                     mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_DEBUG");
1024                     sctpParams->logLevel = MDCLOG_DEBUG;
1025                 } 
1026 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))                
1027                 else if ((tmpStr.compare("info")) == 0) {
1028                     mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_INFO");
1029                     sctpParams->logLevel = MDCLOG_INFO;
1030                 } else if ((tmpStr.compare("warning")) == 0) {
1031                     mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_WARN");
1032                     sctpParams->logLevel = MDCLOG_WARN;
1033                 } else if ((tmpStr.compare("error")) == 0) {
1034                     mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_ERR");
1035                     sctpParams->logLevel = MDCLOG_ERR;
1036                 } else {
1037                     mdclog_write(MDCLOG_ERR, "illegal loglevel = %s. Set loglevel to MDCLOG_INFO", tmpStr.c_str());
1038                     sctpParams->logLevel = MDCLOG_INFO;
1039                 }
1040 #endif                
1041                 mdclog_level_set(sctpParams->logLevel);
1042                 tmpStr = conf.getStringValue("trace");
1043                 if (tmpStr.length() == 0) {
1044                     mdclog_write(MDCLOG_ERR, "illegal trace. Set trace to stop");
1045                     tmpStr = "stop";
1046                 }
1047
1048                 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
1049                 if ((tmpStr.compare("start")) == 0) {
1050                     mdclog_write(MDCLOG_INFO, "Trace set to: start");
1051                     sctpParams->trace = true;
1052                 } else if ((tmpStr.compare("stop")) == 0) {
1053                     mdclog_write(MDCLOG_INFO, "Trace set to: stop");
1054                     sctpParams->trace = false;
1055                 } else {
1056                     mdclog_write(MDCLOG_ERR, "Trace was set to wrong value %s, set to stop", tmpStr.c_str());
1057                     sctpParams->trace = false;
1058                 }
1059                 jsonTrace = sctpParams->trace;
1060
1061
1062                 endlessLoop = false;
1063             }
1064 #ifdef UNIT_TEST
1065             break;
1066 #endif
1067         }
1068     }
1069 }
1070
1071 /**
1072  *
1073  * @param event
1074  * @param message
1075  * @param rmrMessageBuffer
1076  * @param params
1077  */
1078 void handleEinprogressMessages(struct epoll_event &event,
1079                                ReportingMessages_t &message,
1080                                RmrMessagesBuffer_t &rmrMessageBuffer,
1081                                sctp_params_t *params) {
1082     auto *peerInfo = (ConnectedCU_t *)event.data.ptr;
1083     memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
1084
1085     mdclog_write(MDCLOG_INFO, "file descriptor %d got EPOLLOUT", peerInfo->fileDescriptor);
1086     auto retVal = 0;
1087     socklen_t retValLen = 0;
1088     auto rc = getsockopt(peerInfo->fileDescriptor, SOL_SOCKET, SO_ERROR, &retVal, &retValLen);
1089     if (rc != 0 || retVal != 0) {
1090 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))        
1091         if (rc != 0) {
1092             rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
1093                                                          "%s|Failed SCTP Connection, after EINPROGRESS the getsockopt%s",
1094                                                          peerInfo->enodbName, strerror(errno));
1095         } else if (retVal != 0) {
1096             rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
1097                                                          "%s|Failed SCTP Connection after EINPROGRESS, SO_ERROR",
1098                                                          peerInfo->enodbName);
1099         }
1100
1101         message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1102         message.message.asnLength = rmrMessageBuffer.sendMessage->len;
1103         mdclog_write(MDCLOG_ERR, "%s", rmrMessageBuffer.sendMessage->payload);
1104         message.message.direction = 'N';
1105         if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
1106             mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
1107         }
1108 #endif
1109         memset(peerInfo->asnData, 0, peerInfo->asnLength);
1110         peerInfo->asnLength = 0;
1111         peerInfo->mtype = 0;
1112         return;
1113     }
1114 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
1115     peerInfo->isConnected = true;
1116
1117     if (modifyToEpoll(params->epoll_fd, peerInfo, (EPOLLIN | EPOLLET), params->sctpMap, peerInfo->enodbName,
1118                       peerInfo->mtype) != 0) {
1119         mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_MOD");
1120         return;
1121     }
1122
1123     message.message.asndata = (unsigned char *)peerInfo->asnData;
1124     message.message.asnLength = peerInfo->asnLength;
1125     message.message.messageType = peerInfo->mtype;
1126     memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
1127     num_of_messages.fetch_add(1, std::memory_order_release);
1128     if (mdclog_level_get() >= MDCLOG_DEBUG) {
1129         mdclog_write(MDCLOG_DEBUG, "send the delayed SETUP/ENDC SETUP to sctp for %s",
1130                      message.message.enodbName);
1131     }
1132     if (sendSctpMsg(peerInfo, message, params->sctpMap) != 0) {
1133         if (mdclog_level_get() >= MDCLOG_DEBUG) {
1134             mdclog_write(MDCLOG_DEBUG, "Error write to SCTP  %s %d", __func__, __LINE__);
1135         }
1136         return;
1137     }
1138
1139     memset(peerInfo->asnData, 0, peerInfo->asnLength);
1140     peerInfo->asnLength = 0;
1141     peerInfo->mtype = 0;
1142 #endif    
1143 }
1144
1145
1146 void handlepoll_error(struct epoll_event &event,
1147                       ReportingMessages_t &message,
1148                       RmrMessagesBuffer_t &rmrMessageBuffer,
1149                       sctp_params_t *params) {
1150     if (event.data.fd != params->rmrListenFd) {
1151         auto *peerInfo = (ConnectedCU_t *)event.data.ptr;
1152         mdclog_write(MDCLOG_ERR, "epoll error, events %0x on fd %d, RAN NAME : %s",
1153                      event.events, peerInfo->fileDescriptor, peerInfo->enodbName);
1154 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
1155         rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
1156                                                      "%s|Failed SCTP Connection",
1157                                                      peerInfo->enodbName);
1158         message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1159         message.message.asnLength = rmrMessageBuffer.sendMessage->len;
1160
1161         memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
1162         message.message.direction = 'N';
1163         if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
1164             mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
1165         }
1166 #endif
1167         close(peerInfo->fileDescriptor);
1168         params->sctpMap->erase(peerInfo->enodbName);
1169         cleanHashEntry((ConnectedCU_t *) event.data.ptr, params->sctpMap);
1170     } else {
1171         mdclog_write(MDCLOG_ERR, "epoll error, events %0x on RMR FD", event.events);
1172     }
1173 }
1174 /**
1175  *
1176  * @param socket
1177  * @return
1178  */
1179 int setSocketNoBlocking(int socket) {
1180     auto flags = fcntl(socket, F_GETFL, 0);
1181
1182     if (flags == -1) {
1183         mdclog_write(MDCLOG_ERR, "%s, %s", __FUNCTION__, strerror(errno));
1184         return -1;
1185     }
1186
1187     flags = (unsigned) flags | (unsigned) O_NONBLOCK;
1188     if (fcntl(socket, F_SETFL, flags) == -1) {
1189         mdclog_write(MDCLOG_ERR, "%s, %s", __FUNCTION__, strerror(errno));
1190         return -1;
1191     }
1192
1193     return 0;
1194 }
1195
1196 /**
1197  *
1198  * @param val
1199  * @param m
1200  */
1201 void cleanHashEntry(ConnectedCU_t *val, Sctp_Map_t *m) {
1202     char *dummy;
1203     auto port = (uint16_t) strtol(val->portNumber, &dummy, 10);
1204     char searchBuff[2048]{};
1205
1206     snprintf(searchBuff, sizeof searchBuff, "host:%s:%d", val->hostName, port);
1207     m->erase(searchBuff);
1208
1209     m->erase(val->enodbName);
1210 #ifndef UNIT_TEST
1211     free(val);
1212 #endif
1213 }
1214
1215 /**
1216  *
1217  * @param fd file descriptor
1218  * @param data the asn data to send
1219  * @param len  length of the data
1220  * @param enodbName the enodbName as in the map for printing purpose
1221  * @param m map host information
1222  * @param mtype message number
1223  * @return 0 success, a negative number on fail
1224  */
1225 int sendSctpMsg(ConnectedCU_t *peerInfo, ReportingMessages_t &message, Sctp_Map_t *m) {
1226     auto loglevel = mdclog_level_get();
1227 #ifndef UNIT_TEST    
1228     int fd = peerInfo->fileDescriptor;
1229 #else
1230     int fd = FILE_DESCRIPTOR;
1231 #endif    
1232     if (loglevel >= MDCLOG_DEBUG) {
1233         mdclog_write(MDCLOG_DEBUG, "Send SCTP message for CU %s, %s",
1234                      message.message.enodbName, __FUNCTION__);
1235     }
1236
1237     while (true) {
1238         if (send(fd,message.message.asndata, message.message.asnLength,MSG_NOSIGNAL) < 0) {
1239             if (errno == EINTR) {
1240                 continue;
1241             }
1242             mdclog_write(MDCLOG_ERR, "error writing to CU a message, %s ", strerror(errno));
1243 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))            
1244             if (!peerInfo->isConnected) {
1245                 mdclog_write(MDCLOG_ERR, "connection to CU %s is still in progress.", message.message.enodbName);
1246                 return -1;
1247             }
1248 #endif
1249 #ifndef UNIT_TEST            
1250             cleanHashEntry(peerInfo, m);
1251             close(fd);
1252 #endif            
1253             char key[MAX_ENODB_NAME_SIZE * 2];
1254             snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", message.message.enodbName,
1255                      message.message.messageType);
1256             if (loglevel >= MDCLOG_DEBUG) {
1257                 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
1258             }
1259             auto tmp = m->find(key);
1260             if (tmp) {
1261                 free(tmp);
1262             }
1263             m->erase(key);
1264 #ifndef UNIT_TEST
1265             return -1;
1266 #endif
1267         }
1268         message.message.direction = 'D';
1269         // send report.buffer of size
1270         buildJsonMessage(message);
1271
1272         if (loglevel >= MDCLOG_DEBUG) {
1273             mdclog_write(MDCLOG_DEBUG,
1274                          "SCTP message for CU %s sent from %s",
1275                          message.message.enodbName,
1276                          __FUNCTION__);
1277         }
1278         return 0;
1279     }
1280 }
1281
1282 /**
1283  *
1284  * @param message
1285  * @param rmrMessageBuffer
1286  */
1287 void getRequestMetaData(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
1288     message.message.asndata = rmrMessageBuffer.rcvMessage->payload;
1289     message.message.asnLength = rmrMessageBuffer.rcvMessage->len;
1290
1291     if (mdclog_level_get() >= MDCLOG_DEBUG) {
1292         mdclog_write(MDCLOG_DEBUG, "Message from Xapp RAN name = %s message length = %ld",
1293                      message.message.enodbName, (unsigned long) message.message.asnLength);
1294     }
1295 }
1296
1297
1298
1299 /**
1300  *
1301  * @param events
1302  * @param sctpMap
1303  * @param numOfMessages
1304  * @param rmrMessageBuffer
1305  * @param ts
1306  * @return
1307  */
1308 int receiveDataFromSctp(struct epoll_event *events,
1309                         Sctp_Map_t *sctpMap,
1310                         int &numOfMessages,
1311                         RmrMessagesBuffer_t &rmrMessageBuffer,
1312                         struct timespec &ts) {
1313     /* We have data on the fd waiting to be read. Read and display it.
1314  * We must read whatever data is available completely, as we are running
1315  *  in edge-triggered mode and won't get a notification again for the same data. */
1316     ReportingMessages_t message {};
1317     auto done = 0;
1318     auto loglevel = mdclog_level_get();
1319
1320     // get the identity of the interface
1321     message.peerInfo = (ConnectedCU_t *)events->data.ptr;
1322
1323     struct timespec start{0, 0};
1324     struct timespec decodeStart{0, 0};
1325     struct timespec end{0, 0};
1326
1327     E2AP_PDU_t *pdu = nullptr;
1328
1329     while (true) {
1330         if (loglevel >= MDCLOG_DEBUG) {
1331             mdclog_write(MDCLOG_DEBUG, "Start Read from SCTP %d fd", message.peerInfo->fileDescriptor);
1332             clock_gettime(CLOCK_MONOTONIC, &start);
1333         }
1334         // read the buffer directly to rmr payload
1335         message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1336 #ifndef UNIT_TEST        
1337         message.message.asnLength = rmrMessageBuffer.sendMessage->len =
1338                 read(message.peerInfo->fileDescriptor, rmrMessageBuffer.sendMessage->payload, RECEIVE_SCTP_BUFFER_SIZE);
1339 #else
1340         message.message.asnLength = rmrMessageBuffer.sendMessage->len;
1341 #endif
1342
1343         if (loglevel >= MDCLOG_DEBUG) {
1344             mdclog_write(MDCLOG_DEBUG, "Finish Read from SCTP %d fd message length = %ld",
1345                          message.peerInfo->fileDescriptor, message.message.asnLength);
1346         }
1347
1348         memcpy(message.message.enodbName, message.peerInfo->enodbName, sizeof(message.peerInfo->enodbName));
1349         message.message.direction = 'U';
1350         message.message.time.tv_nsec = ts.tv_nsec;
1351         message.message.time.tv_sec = ts.tv_sec;
1352
1353         if (message.message.asnLength < 0) {
1354             if (errno == EINTR) {
1355                 continue;
1356             }
1357             /* If errno == EAGAIN, that means we have read all
1358                data. So goReportingMessages_t back to the main loop. */
1359             if (errno != EAGAIN) {
1360                 mdclog_write(MDCLOG_ERR, "Read error, %s ", strerror(errno));
1361                 done = 1;
1362             } else if (loglevel >= MDCLOG_DEBUG) {
1363                 mdclog_write(MDCLOG_DEBUG, "EAGAIN - descriptor = %d", message.peerInfo->fileDescriptor);
1364             }
1365             break;
1366         } else if (message.message.asnLength == 0) {
1367             /* End of file. The remote has closed the connection. */
1368             if (loglevel >= MDCLOG_INFO) {
1369                 mdclog_write(MDCLOG_INFO, "END of File Closed connection - descriptor = %d",
1370                              message.peerInfo->fileDescriptor);
1371             }
1372             done = 1;
1373             break;
1374         }
1375
1376         if (loglevel >= MDCLOG_DEBUG) {
1377             char printBuffer[RECEIVE_SCTP_BUFFER_SIZE]{};
1378             char *tmp = printBuffer;
1379             for (size_t i = 0; i < (size_t)message.message.asnLength; ++i) {
1380                 snprintf(tmp, 3, "%02x", message.message.asndata[i]);
1381                 tmp += 2;
1382             }
1383             printBuffer[message.message.asnLength] = 0;
1384             clock_gettime(CLOCK_MONOTONIC, &end);
1385             mdclog_write(MDCLOG_DEBUG, "Before Encoding E2AP PDU for : %s, Read time is : %ld seconds, %ld nanoseconds",
1386                          message.peerInfo->enodbName, end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec);
1387             mdclog_write(MDCLOG_DEBUG, "PDU buffer length = %ld, data =  : %s", message.message.asnLength,
1388                          printBuffer);
1389             clock_gettime(CLOCK_MONOTONIC, &decodeStart);
1390         }
1391 #ifndef UNIT_TEST
1392         auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, (void **) &pdu,
1393                         message.message.asndata, message.message.asnLength);
1394 #else
1395         asn_dec_rval_t rval = {RC_OK, 0};
1396         pdu = (E2AP_PDU_t*)rmrMessageBuffer.sendMessage->tp_buf;
1397 #endif
1398         if (rval.code != RC_OK) {
1399             mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2AP PDU from RAN : %s", rval.code,
1400                          message.peerInfo->enodbName);
1401             if (pdu != nullptr) {
1402                 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
1403                 pdu = nullptr;
1404             }
1405             break;
1406         }
1407
1408         if (loglevel >= MDCLOG_DEBUG) {
1409             clock_gettime(CLOCK_MONOTONIC, &end);
1410             mdclog_write(MDCLOG_DEBUG, "After Encoding E2AP PDU for : %s, Read time is : %ld seconds, %ld nanoseconds",
1411                          message.peerInfo->enodbName, end.tv_sec - decodeStart.tv_sec, end.tv_nsec - decodeStart.tv_nsec);
1412             char *printBuffer;
1413             size_t size;
1414             FILE *stream = open_memstream(&printBuffer, &size);
1415             asn_fprint(stream, &asn_DEF_E2AP_PDU, pdu);
1416             mdclog_write(MDCLOG_DEBUG, "Encoding E2AP PDU past : %s", printBuffer);
1417             clock_gettime(CLOCK_MONOTONIC, &decodeStart);
1418
1419             fclose(stream);
1420             free(printBuffer);
1421         }
1422
1423         switch (pdu->present) {
1424             case E2AP_PDU_PR_initiatingMessage: {//initiating message
1425                 asnInitiatingRequest(pdu, sctpMap,message, rmrMessageBuffer);
1426                 break;
1427             }
1428             case E2AP_PDU_PR_successfulOutcome: { //successful outcome
1429                 asnSuccessfulMsg(pdu, sctpMap, message, rmrMessageBuffer);
1430                 break;
1431             }
1432             case E2AP_PDU_PR_unsuccessfulOutcome: { //Unsuccessful Outcome
1433                 asnUnSuccsesfulMsg(pdu, sctpMap, message, rmrMessageBuffer);
1434                 break;
1435             }
1436             default:
1437                 mdclog_write(MDCLOG_ERR, "Unknown index %d in E2AP PDU", pdu->present);
1438                 break;
1439         }
1440         if (loglevel >= MDCLOG_DEBUG) {
1441             clock_gettime(CLOCK_MONOTONIC, &end);
1442             mdclog_write(MDCLOG_DEBUG,
1443                          "After processing message and sent to rmr for : %s, Read time is : %ld seconds, %ld nanoseconds",
1444                          message.peerInfo->enodbName, end.tv_sec - decodeStart.tv_sec, end.tv_nsec - decodeStart.tv_nsec);
1445         }
1446         numOfMessages++;
1447 #ifndef UNIT_TEST
1448         if (pdu != nullptr) {
1449             // ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, pdu); /* With reset we were not freeing the memory and was causing the leak here. */
1450             ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
1451             pdu = nullptr;
1452         }
1453 #else
1454     done = 1;
1455     break;
1456 #endif
1457     }
1458
1459     if (done) {
1460         if (loglevel >= MDCLOG_INFO) {
1461             mdclog_write(MDCLOG_INFO, "Closed connection - descriptor = %d", message.peerInfo->fileDescriptor);
1462         }
1463         message.message.asnLength = rmrMessageBuffer.sendMessage->len =
1464                 snprintf((char *)rmrMessageBuffer.sendMessage->payload,
1465                          256,
1466                          "%s|CU disconnected unexpectedly",
1467                          message.peerInfo->enodbName);
1468         message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1469 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))        
1470         if (sendRequestToXapp(message,
1471                               RIC_SCTP_CONNECTION_FAILURE,
1472                               rmrMessageBuffer) != 0) {
1473             mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
1474         }
1475 #endif        
1476
1477         /* Closing descriptor make epoll remove it from the set of descriptors which are monitored. */
1478         close(message.peerInfo->fileDescriptor);
1479         cleanHashEntry((ConnectedCU_t *) events->data.ptr, sctpMap);
1480     }
1481     if (loglevel >= MDCLOG_DEBUG) {
1482         clock_gettime(CLOCK_MONOTONIC, &end);
1483         mdclog_write(MDCLOG_DEBUG, "from receive SCTP to send RMR time is %ld seconds and %ld nanoseconds",
1484                      end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec);
1485
1486     }
1487     return 0;
1488 }
1489
1490 static void buildAndSendSetupRequest(ReportingMessages_t &message,
1491                                      RmrMessagesBuffer_t &rmrMessageBuffer,
1492                                      E2AP_PDU_t *pdu/*,
1493                                      string const &messageName,
1494                                      string const &ieName,
1495                                      vector<string> &functionsToAdd_v,
1496                                      vector<string> &functionsToModified_v*/) {
1497     auto logLevel = mdclog_level_get();
1498     // now we can send the data to e2Mgr
1499
1500     asn_enc_rval_t er;
1501     auto buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
1502     unsigned char *buffer = nullptr;
1503     buffer = (unsigned char *) calloc(buffer_size, sizeof(unsigned char));
1504     if(!buffer)
1505     {
1506 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))        
1507         mdclog_write(MDCLOG_ERR, "Allocating buffer for %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1508         return;
1509 #endif        
1510     }
1511     while (true) {
1512         er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu, buffer, buffer_size);
1513         if (er.encoded == -1) {
1514 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))     
1515             mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1516             return;
1517 #endif            
1518         } else if (er.encoded > (ssize_t) buffer_size) {
1519             buffer_size = er.encoded + 128;
1520 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))            
1521             mdclog_write(MDCLOG_WARN, "Buffer of size %d is to small for %s. Reallocate buffer of size %d",
1522                          (int) buffer_size,
1523                          asn_DEF_E2AP_PDU.name, buffer_size);
1524             buffer_size = er.encoded + 128;
1525
1526             unsigned char *newBuffer = nullptr;
1527             newBuffer = (unsigned char *) realloc(buffer, buffer_size);
1528             if (!newBuffer)
1529             {
1530                 // out of memory
1531                 mdclog_write(MDCLOG_ERR, "Reallocating buffer for %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1532                 free(buffer);
1533                 return;
1534             }
1535             buffer = newBuffer;
1536             continue;
1537 #endif            
1538         }
1539         buffer[er.encoded] = '\0';
1540         break;
1541     }
1542     // encode to xml
1543
1544     string res((char *)buffer);
1545     res.erase(std::remove(res.begin(), res.end(), '\n'), res.end());
1546     res.erase(std::remove(res.begin(), res.end(), '\t'), res.end());
1547     res.erase(std::remove(res.begin(), res.end(), ' '), res.end());
1548
1549 //    string res {};
1550 //    if (!functionsToAdd_v.empty() || !functionsToModified_v.empty()) {
1551 //        res = buildXmlData(messageName, ieName, functionsToAdd_v, functionsToModified_v, buffer, (size_t) er.encoded);
1552 //    }
1553     rmr_mbuf_t *rmrMsg;
1554 //    if (res.length() == 0) {
1555 //        rmrMsg = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, buffer_size + 256);
1556 //        rmrMsg->len = snprintf((char *) rmrMsg->payload, RECEIVE_SCTP_BUFFER_SIZE * 2, "%s:%d|%s",
1557 //                               message.peerInfo->sctpParams->myIP.c_str(),
1558 //                               message.peerInfo->sctpParams->rmrPort,
1559 //                               buffer);
1560 //    } else {
1561         rmrMsg = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, (int)res.length() + 256);
1562         rmrMsg->len = snprintf((char *) rmrMsg->payload, res.length() + 256, "%s:%d|%s",
1563                                message.peerInfo->sctpParams->myIP.c_str(),
1564                                message.peerInfo->sctpParams->rmrPort,
1565                                res.c_str());
1566 //    }
1567
1568     if (logLevel >= MDCLOG_DEBUG) {
1569         mdclog_write(MDCLOG_DEBUG, "Setup request of size %d :\n %s\n", rmrMsg->len, rmrMsg->payload);
1570     }
1571     // send to RMR
1572     rmrMsg->mtype = message.message.messageType;
1573     rmrMsg->state = 0;
1574     rmr_bytes2meid(rmrMsg, (unsigned char *) message.message.enodbName, strlen(message.message.enodbName));
1575
1576     static unsigned char tx[32];
1577     snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1578     rmr_bytes2xact(rmrMsg, tx, strlen((const char *) tx));
1579 #ifndef UNIT_TEST
1580     rmrMsg = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMsg);
1581 #endif
1582     if (rmrMsg == nullptr) {
1583         mdclog_write(MDCLOG_ERR, "RMR failed to send returned nullptr");
1584     } else if (rmrMsg->state != 0) {
1585         char meid[RMR_MAX_MEID]{};
1586         if (rmrMsg->state == RMR_ERR_RETRY) {
1587             usleep(5);
1588             rmrMsg->state = 0;
1589             mdclog_write(MDCLOG_INFO, "RETRY sending Message %d to Xapp from %s",
1590                          rmrMsg->mtype, rmr_get_meid(rmrMsg, (unsigned char *) meid));
1591 #ifndef UNIT_TEST
1592             rmrMsg = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMsg);
1593 #endif
1594             if (rmrMsg == nullptr) {
1595                 mdclog_write(MDCLOG_ERR, "RMR failed send returned nullptr");
1596             } else if (rmrMsg->state != 0) {
1597                 mdclog_write(MDCLOG_ERR,
1598                              "RMR Retry failed %s sending request %d to Xapp from %s",
1599                              translateRmrErrorMessages(rmrMsg->state).c_str(),
1600                              rmrMsg->mtype,
1601                              rmr_get_meid(rmrMsg, (unsigned char *) meid));
1602             }
1603         } else {
1604             mdclog_write(MDCLOG_ERR, "RMR failed: %s. sending request %d to Xapp from %s",
1605                          translateRmrErrorMessages(rmrMsg->state).c_str(),
1606                          rmrMsg->mtype,
1607                          rmr_get_meid(rmrMsg, (unsigned char *) meid));
1608         }
1609     }
1610     message.peerInfo->gotSetup = true;
1611     buildJsonMessage(message);
1612
1613     if (rmrMsg != nullptr) {
1614         rmr_free_msg(rmrMsg);
1615     }
1616     free(buffer);
1617
1618     return;
1619 }
1620
1621 #if 0
1622 int RAN_Function_list_To_Vector(RANfunctions_List_t& list, vector <string> &runFunXML_v) {
1623     auto index = 0;
1624     runFunXML_v.clear();
1625     for (auto j = 0; j < list.list.count; j++) {
1626         auto *raNfunctionItemIEs = (RANfunction_ItemIEs_t *)list.list.array[j];
1627         if (raNfunctionItemIEs->id == ProtocolIE_ID_id_RANfunction_Item &&
1628             (raNfunctionItemIEs->value.present == RANfunction_ItemIEs__value_PR_RANfunction_Item)) {
1629             // encode to xml
1630             E2SM_gNB_NRT_RANfunction_Definition_t *ranFunDef = nullptr;
1631             auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER,
1632                                    &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition,
1633                                    (void **)&ranFunDef,
1634                                    raNfunctionItemIEs->value.choice.RANfunction_Item.ranFunctionDefinition.buf,
1635                                    raNfunctionItemIEs->value.choice.RANfunction_Item.ranFunctionDefinition.size);
1636             if (rval.code != RC_OK) {
1637                 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2SM message from : %s",
1638                              rval.code,
1639                              asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name);
1640                 return -1;
1641             }
1642
1643             auto xml_buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
1644             unsigned char xml_buffer[RECEIVE_SCTP_BUFFER_SIZE * 2];
1645             memset(xml_buffer, 0, RECEIVE_SCTP_BUFFER_SIZE * 2);
1646             // encode to xml
1647             auto er = asn_encode_to_buffer(nullptr,
1648                                            ATS_BASIC_XER,
1649                                            &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition,
1650                                            ranFunDef,
1651                                            xml_buffer,
1652                                            xml_buffer_size);
1653             if (er.encoded == -1) {
1654                 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s",
1655                              asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name,
1656                              strerror(errno));
1657             } else if (er.encoded > (ssize_t)xml_buffer_size) {
1658                 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
1659                              (int) xml_buffer_size,
1660                              asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name, __func__, __LINE__);
1661             } else {
1662                 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1663                     mdclog_write(MDCLOG_DEBUG, "Encoding E2SM %s PDU number %d : %s",
1664                                  asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name,
1665                                  index++,
1666                                  xml_buffer);
1667                 }
1668
1669                 string runFuncs = (char *)(xml_buffer);
1670                 runFunXML_v.emplace_back(runFuncs);
1671             }
1672         }
1673     }
1674     return 0;
1675 }
1676
1677 int collectServiceUpdate_RequestData(E2AP_PDU_t *pdu,
1678                                      Sctp_Map_t *sctpMap,
1679                                      ReportingMessages_t &message,
1680                                      vector <string> &RANfunctionsAdded_v,
1681                                      vector <string> &RANfunctionsModified_v) {
1682     memset(message.peerInfo->enodbName, 0 , MAX_ENODB_NAME_SIZE);
1683     for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICserviceUpdate.protocolIEs.list.count; i++) {
1684         auto *ie = pdu->choice.initiatingMessage->value.choice.RICserviceUpdate.protocolIEs.list.array[i];
1685         if (ie->id == ProtocolIE_ID_id_RANfunctionsAdded) {
1686             if (ie->value.present == RICserviceUpdate_IEs__value_PR_RANfunctionsID_List) {
1687                 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1688                     mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1689                                  ie->value.choice.RANfunctions_List.list.count);
1690                 }
1691                 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsAdded_v) != 0 ) {
1692                     return -1;
1693                 }
1694             }
1695         } else if (ie->id == ProtocolIE_ID_id_RANfunctionsModified) {
1696             if (ie->value.present == RICserviceUpdate_IEs__value_PR_RANfunctions_List) {
1697                 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1698                     mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1699                                  ie->value.choice.RANfunctions_List.list.count);
1700                 }
1701                 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsModified_v) != 0 ) {
1702                     return -1;
1703                 }
1704             }
1705         }
1706     }
1707     if (mdclog_level_get() >= MDCLOG_DEBUG) {
1708         mdclog_write(MDCLOG_DEBUG, "Run function vector have %ld entries",
1709                      RANfunctionsAdded_v.size());
1710     }
1711     return 0;
1712 }
1713
1714 #endif
1715
1716
1717 void buildPrometheusList(ConnectedCU_t *peerInfo, Family<Counter> *prometheusFamily) {
1718     peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"SetupRequest", "Messages"}});
1719     peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"SetupRequest", "Bytes"}});
1720
1721     peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ErrorIndication", "Messages"}});
1722     peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ErrorIndication", "Bytes"}});
1723
1724     peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_RICindication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICindication", "Messages"}});
1725     peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_RICindication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICindication", "Bytes"}});
1726
1727     peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetRequest", "Messages"}});
1728     peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetRequest", "Bytes"}});
1729
1730     peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICserviceUpdate", "Messages"}});
1731     peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICserviceUpdate", "Bytes"}});
1732     // ---------------------------------------------
1733     peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetACK", "Messages"}});
1734     peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetACK", "Bytes"}});
1735
1736     peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolACK", "Messages"}});
1737     peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolACK", "Bytes"}});
1738
1739     peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionACK", "Messages"}});
1740     peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionACK", "Bytes"}});
1741
1742     peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteACK", "Messages"}});
1743     peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteACK", "Bytes"}});
1744     //-------------------------------------------------------------
1745
1746     peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolFailure", "Messages"}});
1747     peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolFailure", "Bytes"}});
1748
1749     peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionFailure", "Messages"}});
1750     peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionFailure", "Bytes"}});
1751
1752     peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteFailure", "Messages"}});
1753     peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteFailure", "Bytes"}});
1754
1755     //====================================================================================
1756     peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ErrorIndication", "Messages"}});
1757     peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ErrorIndication", "Bytes"}});
1758
1759     peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetRequest", "Messages"}});
1760     peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetRequest", "Bytes"}});
1761
1762     peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICcontrol", "Messages"}});
1763     peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICcontrol", "Bytes"}});
1764
1765     peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICserviceQuery)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceQuery", "Messages"}});
1766     peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICserviceQuery)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceQuery", "Bytes"}});
1767
1768     peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscription", "Messages"}});
1769     peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscription", "Bytes"}});
1770
1771     peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscriptionDelete", "Messages"}});
1772     peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscriptionDelete", "Bytes"}});
1773     //---------------------------------------------------------------------------------------------------------
1774     peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupResponse", "Messages"}});
1775     peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupResponse", "Bytes"}});
1776
1777     peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetACK", "Messages"}});
1778     peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetACK", "Bytes"}});
1779
1780     peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateResponse", "Messages"}});
1781     peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateResponse", "Bytes"}});
1782     //----------------------------------------------------------------------------------------------------------------
1783     peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupRequestFailure", "Messages"}});
1784     peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupRequestFailure", "Bytes"}});
1785
1786     peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateFailure", "Messages"}});
1787     peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateFailure", "Bytes"}});
1788 }
1789 /**
1790  *
1791  * @param pdu
1792  * @param sctpMap
1793  * @param message
1794  * @param RANfunctionsAdded_v
1795  * @return
1796  */
1797 int collectSetupRequestData(E2AP_PDU_t *pdu,
1798                                      Sctp_Map_t *sctpMap,
1799                                      ReportingMessages_t &message /*, vector <string> &RANfunctionsAdded_v*/) {
1800     memset(message.peerInfo->enodbName, 0 , MAX_ENODB_NAME_SIZE);
1801     for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.count; i++) {
1802         auto *ie = pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.array[i];
1803         if (ie->id == ProtocolIE_ID_id_GlobalE2node_ID) {
1804             // get the ran name for meid
1805             if (ie->value.present == E2setupRequestIEs__value_PR_GlobalE2node_ID) {
1806                 if (buildRanName(message.peerInfo->enodbName, ie) < 0) {
1807                     mdclog_write(MDCLOG_ERR, "Bad param in E2setupRequestIEs GlobalE2node_ID.\n");
1808                     // no message will be sent
1809                     return -1;
1810                 }
1811
1812                 memcpy(message.message.enodbName, message.peerInfo->enodbName, strlen(message.peerInfo->enodbName));
1813                 sctpMap->setkey(message.message.enodbName, message.peerInfo);
1814             }
1815         } /*else if (ie->id == ProtocolIE_ID_id_RANfunctionsAdded) {
1816             if (ie->value.present == E2setupRequestIEs__value_PR_RANfunctions_List) {
1817                 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1818                     mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1819                                  ie->value.choice.RANfunctions_List.list.count);
1820                 }
1821                 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsAdded_v) != 0 ) {
1822                     return -1;
1823                 }
1824             }
1825         } */
1826     }
1827 //    if (mdclog_level_get() >= MDCLOG_DEBUG) {
1828 //        mdclog_write(MDCLOG_DEBUG, "Run function vector have %ld entries",
1829 //                     RANfunctionsAdded_v.size());
1830 //    }
1831     return 0;
1832 }
1833
1834 int XML_From_PER(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
1835     E2AP_PDU_t *pdu = nullptr;
1836
1837     if (mdclog_level_get() >= MDCLOG_DEBUG) {
1838         mdclog_write(MDCLOG_DEBUG, "got PER message of size %d is:%s",
1839                      rmrMessageBuffer.sendMessage->len, rmrMessageBuffer.sendMessage->payload);
1840     }
1841     auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, (void **) &pdu,
1842                            rmrMessageBuffer.sendMessage->payload, rmrMessageBuffer.sendMessage->len);
1843     if (rval.code != RC_OK) {
1844         mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) setup response  from E2MGR : %s",
1845                      rval.code,
1846                      message.message.enodbName);
1847         if (pdu != nullptr) {
1848             ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
1849             pdu = nullptr;
1850         }
1851         return -1;
1852     }
1853
1854     int buff_size = RECEIVE_XAPP_BUFFER_SIZE;
1855     auto er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu,
1856                                    rmrMessageBuffer.sendMessage->payload, buff_size);
1857     if (er.encoded == -1) {
1858         mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1859         if (pdu != nullptr) {
1860             ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
1861             pdu = nullptr;
1862         }
1863         return -1;
1864     } else if (er.encoded > (ssize_t)buff_size) {
1865         mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
1866                      (int)rmrMessageBuffer.sendMessage->len,
1867                      asn_DEF_E2AP_PDU.name,
1868                      __func__,
1869                      __LINE__);
1870         if (pdu != nullptr) {
1871             ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
1872             pdu = nullptr;
1873         }
1874         return -1;
1875     }
1876     rmrMessageBuffer.sendMessage->len = er.encoded;
1877     if (pdu != nullptr) {
1878         ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
1879         pdu = nullptr;
1880     }
1881     return 0;
1882
1883 }
1884
1885 /**
1886  *
1887  * @param pdu
1888  * @param message
1889  * @param rmrMessageBuffer
1890  */
1891 void asnInitiatingRequest(E2AP_PDU_t *pdu,
1892                           Sctp_Map_t *sctpMap,
1893                           ReportingMessages_t &message,
1894                           RmrMessagesBuffer_t &rmrMessageBuffer) {
1895     auto logLevel = mdclog_level_get();
1896     auto procedureCode = ((InitiatingMessage_t *) pdu->choice.initiatingMessage)->procedureCode;
1897     if (logLevel >= MDCLOG_DEBUG) {
1898         mdclog_write(MDCLOG_DEBUG, "Initiating message %ld\n", procedureCode);
1899     }
1900     switch (procedureCode) {
1901         case ProcedureCode_id_E2setup: {
1902             if (logLevel >= MDCLOG_DEBUG) {
1903                 mdclog_write(MDCLOG_DEBUG, "Got E2setup");
1904             }
1905
1906 //            vector <string> RANfunctionsAdded_v;
1907 //            vector <string> RANfunctionsModified_v;
1908 //            RANfunctionsAdded_v.clear();
1909 //            RANfunctionsModified_v.clear();
1910             if (collectSetupRequestData(pdu, sctpMap, message) != 0) {
1911                 break;
1912             }
1913
1914             buildPrometheusList(message.peerInfo, message.peerInfo->sctpParams->prometheusFamily);
1915
1916             string messageName("E2setupRequest");
1917             string ieName("E2setupRequestIEs");
1918             message.message.messageType = RIC_E2_SETUP_REQ;
1919             message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
1920             message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment((double)message.message.asnLength);
1921             buildAndSendSetupRequest(message, rmrMessageBuffer, pdu);
1922             break;
1923         }
1924         case ProcedureCode_id_RICserviceUpdate: {
1925             if (logLevel >= MDCLOG_DEBUG) {
1926                 mdclog_write(MDCLOG_DEBUG, "Got RICserviceUpdate %s", message.message.enodbName);
1927             }
1928 //            vector <string> RANfunctionsAdded_v;
1929 //            vector <string> RANfunctionsModified_v;
1930 //            RANfunctionsAdded_v.clear();
1931 //            RANfunctionsModified_v.clear();
1932 //            if (collectServiceUpdate_RequestData(pdu, sctpMap, message,
1933 //                                                 RANfunctionsAdded_v, RANfunctionsModified_v) != 0) {
1934 //                break;
1935 //            }
1936
1937             string messageName("RICserviceUpdate");
1938             string ieName("RICserviceUpdateIEs");
1939             message.message.messageType = RIC_SERVICE_UPDATE;
1940 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))            
1941             message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
1942             message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment((double)message.message.asnLength);
1943 #endif
1944             buildAndSendSetupRequest(message, rmrMessageBuffer, pdu);
1945             break;
1946         }
1947         case ProcedureCode_id_ErrorIndication: {
1948             if (logLevel >= MDCLOG_DEBUG) {
1949                 mdclog_write(MDCLOG_DEBUG, "Got ErrorIndication %s", message.message.enodbName);
1950             }
1951 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))            
1952             message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_ErrorIndication]->Increment();
1953             message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_ErrorIndication]->Increment((double)message.message.asnLength);
1954 #endif
1955             if (sendRequestToXapp(message, RIC_ERROR_INDICATION, rmrMessageBuffer) != 0) {
1956                 mdclog_write(MDCLOG_ERR, "RIC_ERROR_INDICATION failed to send to xAPP");
1957             }
1958             break;
1959         }
1960         case ProcedureCode_id_Reset: {
1961             if (logLevel >= MDCLOG_DEBUG) {
1962                 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
1963             }
1964 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))            
1965             message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
1966             message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment((double)message.message.asnLength);
1967 #endif
1968             if (XML_From_PER(message, rmrMessageBuffer) < 0) {
1969                 break;
1970             }
1971
1972             if (sendRequestToXapp(message, RIC_E2_RESET_REQ, rmrMessageBuffer) != 0) {
1973                 mdclog_write(MDCLOG_ERR, "RIC_E2_RESET_REQ message failed to send to xAPP");
1974             }
1975             break;
1976         }
1977         case ProcedureCode_id_RICindication: {
1978             if (logLevel >= MDCLOG_DEBUG) {
1979                 mdclog_write(MDCLOG_DEBUG, "Got RICindication %s", message.message.enodbName);
1980             }
1981             for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.count; i++) {
1982                 auto messageSent = false;
1983                 RICindication_IEs_t *ie = pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.array[i];
1984                 if (logLevel >= MDCLOG_DEBUG) {
1985                     mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1986                 }
1987                 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
1988                     if (logLevel >= MDCLOG_DEBUG) {
1989                         mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1990                     }
1991                     if (ie->value.present == RICindication_IEs__value_PR_RICrequestID) {
1992                         static unsigned char tx[32];
1993                         message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_INDICATION;
1994                         snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1995                         rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
1996                         rmr_bytes2meid(rmrMessageBuffer.sendMessage,
1997                                        (unsigned char *)message.message.enodbName,
1998                                        strlen(message.message.enodbName));
1999                         rmrMessageBuffer.sendMessage->state = 0;
2000                         rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
2001
2002                         //ie->value.choice.RICrequestID.ricInstanceID;
2003                         if (mdclog_level_get() >= MDCLOG_DEBUG) {
2004                             mdclog_write(MDCLOG_DEBUG, "sub id = %d, mtype = %d, ric instance id %ld, requestor id = %ld",
2005                                          rmrMessageBuffer.sendMessage->sub_id,
2006                                          rmrMessageBuffer.sendMessage->mtype,
2007                                          ie->value.choice.RICrequestID.ricInstanceID,
2008                                          ie->value.choice.RICrequestID.ricRequestorID);
2009                         }
2010 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))                        
2011                         message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICindication]->Increment();
2012                         message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICindication]->Increment((double)message.message.asnLength);
2013 #endif
2014                         sendRmrMessage(rmrMessageBuffer, message);
2015                         messageSent = true;
2016                     } else {
2017                         mdclog_write(MDCLOG_ERR, "RIC request id missing illegal request");
2018                     }
2019                 }
2020                 if (messageSent) {
2021                     break;
2022                 }
2023             }
2024             break;
2025         }
2026         default: {
2027             mdclog_write(MDCLOG_ERR, "Undefined or not supported message = %ld", procedureCode);
2028             message.message.messageType = 0; // no RMR message type yet
2029
2030             buildJsonMessage(message);
2031
2032             break;
2033         }
2034     }
2035 }
2036
2037 /**
2038  *
2039  * @param pdu
2040  * @param message
2041  * @param rmrMessageBuffer
2042  */
2043 void asnSuccessfulMsg(E2AP_PDU_t *pdu,
2044                       Sctp_Map_t *sctpMap,
2045                       ReportingMessages_t &message,
2046                       RmrMessagesBuffer_t &rmrMessageBuffer) {
2047     auto procedureCode = pdu->choice.successfulOutcome->procedureCode;
2048     auto logLevel = mdclog_level_get();
2049     if (logLevel >= MDCLOG_INFO) {
2050         mdclog_write(MDCLOG_INFO, "Successful Outcome %ld", procedureCode);
2051     }
2052     switch (procedureCode) {
2053         case ProcedureCode_id_Reset: {
2054             if (logLevel >= MDCLOG_DEBUG) {
2055                 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
2056             }
2057 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))            
2058             message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2059             message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment((double)message.message.asnLength);
2060 #endif
2061             if (XML_From_PER(message, rmrMessageBuffer) < 0) {
2062                 break;
2063             }
2064             if (sendRequestToXapp(message, RIC_E2_RESET_RESP, rmrMessageBuffer) != 0) {
2065                 mdclog_write(MDCLOG_ERR, "RIC_E2_RESET_RESP message failed to send to xAPP");
2066             }
2067             break;
2068         }
2069         case ProcedureCode_id_RICcontrol: {
2070             if (logLevel >= MDCLOG_DEBUG) {
2071                 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
2072             }
2073             for (auto i = 0;
2074                  i < pdu->choice.successfulOutcome->value.choice.RICcontrolAcknowledge.protocolIEs.list.count; i++) {
2075                 auto messageSent = false;
2076                 RICcontrolAcknowledge_IEs_t *ie = pdu->choice.successfulOutcome->value.choice.RICcontrolAcknowledge.protocolIEs.list.array[i];
2077                 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2078                     mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
2079                 }
2080                 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
2081                     if (mdclog_level_get() >= MDCLOG_DEBUG) {
2082                         mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
2083                     }
2084                     if (ie->value.present == RICcontrolAcknowledge_IEs__value_PR_RICrequestID) {
2085                         message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_CONTROL_ACK;
2086                         rmrMessageBuffer.sendMessage->state = 0;
2087 //                        rmrMessageBuffer.sendMessage->sub_id = (int) ie->value.choice.RICrequestID.ricRequestorID;
2088                         rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
2089
2090                         static unsigned char tx[32];
2091                         snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2092                         rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
2093                         rmr_bytes2meid(rmrMessageBuffer.sendMessage,
2094                                        (unsigned char *)message.message.enodbName,
2095                                        strlen(message.message.enodbName));
2096 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))                        
2097                         message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
2098                         message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment((double)message.message.asnLength);
2099 #endif
2100                         sendRmrMessage(rmrMessageBuffer, message);
2101                         messageSent = true;
2102                     } else {
2103                         mdclog_write(MDCLOG_ERR, "RIC request id missing illegal request");
2104                     }
2105                 }
2106                 if (messageSent) {
2107                     break;
2108                 }
2109             }
2110
2111             break;
2112         }
2113         case ProcedureCode_id_RICsubscription: {
2114             if (logLevel >= MDCLOG_DEBUG) {
2115                 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
2116             }
2117 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))            
2118             message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
2119             message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment((double)message.message.asnLength);
2120 #endif
2121             if (sendRequestToXapp(message, RIC_SUB_RESP, rmrMessageBuffer) != 0) {
2122                 mdclog_write(MDCLOG_ERR, "Subscription successful message failed to send to xAPP");
2123             }
2124             break;
2125         }
2126         case ProcedureCode_id_RICsubscriptionDelete: {
2127             if (logLevel >= MDCLOG_DEBUG) {
2128                 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
2129             }
2130 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))            
2131             message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
2132             message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment((double)message.message.asnLength);
2133 #endif
2134             if (sendRequestToXapp(message, RIC_SUB_DEL_RESP, rmrMessageBuffer) != 0) {
2135                 mdclog_write(MDCLOG_ERR, "Subscription delete successful message failed to send to xAPP");
2136             }
2137             break;
2138         }
2139         default: {
2140             mdclog_write(MDCLOG_WARN, "Undefined or not supported message = %ld", procedureCode);
2141             message.message.messageType = 0; // no RMR message type yet
2142             buildJsonMessage(message);
2143
2144             break;
2145         }
2146     }
2147 }
2148
2149 /**
2150  *
2151  * @param pdu
2152  * @param message
2153  * @param rmrMessageBuffer
2154  */
2155 void asnUnSuccsesfulMsg(E2AP_PDU_t *pdu,
2156                         Sctp_Map_t *sctpMap,
2157                         ReportingMessages_t &message,
2158                         RmrMessagesBuffer_t &rmrMessageBuffer) {
2159     auto procedureCode = pdu->choice.unsuccessfulOutcome->procedureCode;
2160     auto logLevel = mdclog_level_get();
2161     if (logLevel >= MDCLOG_INFO) {
2162         mdclog_write(MDCLOG_INFO, "Unsuccessful Outcome %ld", procedureCode);
2163     }
2164     switch (procedureCode) {
2165         case ProcedureCode_id_RICcontrol: {
2166             if (logLevel >= MDCLOG_DEBUG) {
2167                 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
2168             }
2169             for (int i = 0;
2170                  i < pdu->choice.unsuccessfulOutcome->value.choice.RICcontrolFailure.protocolIEs.list.count; i++) {
2171                 auto messageSent = false;
2172                 RICcontrolFailure_IEs_t *ie = pdu->choice.unsuccessfulOutcome->value.choice.RICcontrolFailure.protocolIEs.list.array[i];
2173                 if (logLevel >= MDCLOG_DEBUG) {
2174                     mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
2175                 }
2176                 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
2177                     if (logLevel >= MDCLOG_DEBUG) {
2178                         mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
2179                     }
2180                     if (ie->value.present == RICcontrolFailure_IEs__value_PR_RICrequestID) {
2181                         message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_CONTROL_FAILURE;
2182                         rmrMessageBuffer.sendMessage->state = 0;
2183 //                        rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricRequestorID;
2184                         rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
2185                         static unsigned char tx[32];
2186                         snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2187                         rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
2188                         rmr_bytes2meid(rmrMessageBuffer.sendMessage, (unsigned char *) message.message.enodbName,
2189                                        strlen(message.message.enodbName));
2190 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))                        
2191                         message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
2192                         message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment((double)message.message.asnLength);
2193 #endif
2194                         sendRmrMessage(rmrMessageBuffer, message);
2195                         messageSent = true;
2196                     } else {
2197                         mdclog_write(MDCLOG_ERR, "RIC request id missing illegal request");
2198                     }
2199                 }
2200                 if (messageSent) {
2201                     break;
2202                 }
2203             }
2204             break;
2205         }
2206         case ProcedureCode_id_RICsubscription: {
2207             if (logLevel >= MDCLOG_DEBUG) {
2208                 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
2209             }
2210 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))            
2211             message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
2212             message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment((double)message.message.asnLength);
2213 #endif
2214             if (sendRequestToXapp(message, RIC_SUB_FAILURE, rmrMessageBuffer) != 0) {
2215                 mdclog_write(MDCLOG_ERR, "Subscription unsuccessful message failed to send to xAPP");
2216             }
2217             break;
2218         }
2219         case ProcedureCode_id_RICsubscriptionDelete: {
2220             if (logLevel >= MDCLOG_DEBUG) {
2221                 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
2222             }
2223 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))            
2224             message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
2225             message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment((double)message.message.asnLength);
2226 #endif
2227             if (sendRequestToXapp(message, RIC_SUB_FAILURE, rmrMessageBuffer) != 0) {
2228                 mdclog_write(MDCLOG_ERR, "Subscription Delete unsuccessful message failed to send to xAPP");
2229             }
2230             break;
2231         }
2232         default: {
2233             mdclog_write(MDCLOG_WARN, "Undefined or not supported message = %ld", procedureCode);
2234             message.message.messageType = 0; // no RMR message type yet
2235 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))            
2236             buildJsonMessage(message);
2237 #endif            
2238             break;
2239         }
2240     }
2241 }
2242
2243 /**
2244  *
2245  * @param message
2246  * @param requestId
2247  * @param rmrMmessageBuffer
2248  * @return
2249  */
2250 int sendRequestToXapp(ReportingMessages_t &message,
2251                       int requestId,
2252                       RmrMessagesBuffer_t &rmrMmessageBuffer) {
2253     rmr_bytes2meid(rmrMmessageBuffer.sendMessage,
2254                    (unsigned char *)message.message.enodbName,
2255                    strlen(message.message.enodbName));
2256     message.message.messageType = rmrMmessageBuffer.sendMessage->mtype = requestId;
2257     rmrMmessageBuffer.sendMessage->state = 0;
2258     static unsigned char tx[32];
2259     snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2260     rmr_bytes2xact(rmrMmessageBuffer.sendMessage, tx, strlen((const char *) tx));
2261
2262     auto rc = sendRmrMessage(rmrMmessageBuffer, message);
2263     return rc;
2264 }
2265
2266 /**
2267  *
2268  * @param pSctpParams
2269  */
2270 void getRmrContext(sctp_params_t &pSctpParams) {
2271     pSctpParams.rmrCtx = nullptr;
2272     pSctpParams.rmrCtx = rmr_init(pSctpParams.rmrAddress, RECEIVE_XAPP_BUFFER_SIZE, RMRFL_NONE);
2273     if (pSctpParams.rmrCtx == nullptr) {
2274         mdclog_write(MDCLOG_ERR, "Failed to initialize RMR");
2275         return;
2276     }
2277
2278     rmr_set_stimeout(pSctpParams.rmrCtx, 0);    // disable retries for any send operation
2279     // we need to find that routing table exist and we can run
2280     if (mdclog_level_get() >= MDCLOG_INFO) {
2281         mdclog_write(MDCLOG_INFO, "We are after RMR INIT wait for RMR_Ready");
2282     }
2283     int rmrReady = 0;
2284     int count = 0;
2285     while (!rmrReady) {
2286         if ((rmrReady = rmr_ready(pSctpParams.rmrCtx)) == 0) {
2287             sleep(1);
2288         }
2289         count++;
2290         if (count % 60 == 0) {
2291             mdclog_write(MDCLOG_INFO, "waiting to RMR ready state for %d seconds", count);
2292         }
2293     }
2294     if (mdclog_level_get() >= MDCLOG_INFO) {
2295         mdclog_write(MDCLOG_INFO, "RMR running");
2296     }
2297     rmr_init_trace(pSctpParams.rmrCtx, 200);
2298     // get the RMR fd for the epoll
2299     pSctpParams.rmrListenFd = rmr_get_rcvfd(pSctpParams.rmrCtx);
2300     struct epoll_event event{};
2301     // add RMR fd to epoll
2302     event.events = (EPOLLIN);
2303     event.data.fd = pSctpParams.rmrListenFd;
2304     // add listening RMR FD to epoll
2305     if (epoll_ctl(pSctpParams.epoll_fd, EPOLL_CTL_ADD, pSctpParams.rmrListenFd, &event)) {
2306         mdclog_write(MDCLOG_ERR, "Failed to add RMR descriptor to epoll");
2307         close(pSctpParams.rmrListenFd);
2308         rmr_close(pSctpParams.rmrCtx);
2309         pSctpParams.rmrCtx = nullptr;
2310     }
2311 }
2312
2313 /**
2314  *
2315  * @param message
2316  * @param rmrMessageBuffer
2317  * @return
2318  */
2319 int PER_FromXML(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
2320     E2AP_PDU_t *pdu = nullptr;
2321
2322     if (mdclog_level_get() >= MDCLOG_DEBUG) {
2323         mdclog_write(MDCLOG_DEBUG, "got xml Format  data from xApp of size %d is:%s",
2324                 rmrMessageBuffer.rcvMessage->len, rmrMessageBuffer.rcvMessage->payload);
2325     }
2326     auto rval = asn_decode(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, (void **) &pdu,
2327                            rmrMessageBuffer.rcvMessage->payload, rmrMessageBuffer.rcvMessage->len);
2328     if (mdclog_level_get() >= MDCLOG_DEBUG) {
2329         mdclog_write(MDCLOG_DEBUG, "%s After  decoding the XML to PDU", __func__ );
2330     }
2331     if (rval.code != RC_OK) {
2332 #ifdef UNIT_TEST
2333     return 0;
2334 #endif    
2335         mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) setup response  from E2MGR : %s",
2336                      rval.code,
2337                      message.message.enodbName);
2338         if (pdu != nullptr) {
2339             ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
2340             pdu = nullptr;
2341         }
2342         return -1;
2343     }
2344
2345     int buff_size = RECEIVE_XAPP_BUFFER_SIZE;
2346     auto er = asn_encode_to_buffer(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, pdu,
2347                                    rmrMessageBuffer.rcvMessage->payload, buff_size);
2348     if (mdclog_level_get() >= MDCLOG_DEBUG) {
2349         mdclog_write(MDCLOG_DEBUG, "%s After encoding PDU to PER", __func__ );
2350     }
2351     if (er.encoded == -1) {
2352         mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
2353         if (pdu != nullptr) {
2354             ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
2355             pdu = nullptr;
2356         }
2357         return -1;
2358     } else if (er.encoded > (ssize_t)buff_size) {
2359         mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
2360                      (int)rmrMessageBuffer.rcvMessage->len,
2361                      asn_DEF_E2AP_PDU.name,
2362                      __func__,
2363                      __LINE__);
2364         if (pdu != nullptr) {
2365             ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
2366             pdu = nullptr;
2367         }
2368         return -1;
2369     }
2370     rmrMessageBuffer.rcvMessage->len = er.encoded;
2371     if (pdu != nullptr) {
2372         ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
2373         pdu = nullptr;
2374     }
2375     return 0;
2376 }
2377
2378 /**
2379  *
2380  * @param sctpMap
2381  * @param rmrMessageBuffer
2382  * @param ts
2383  * @return
2384  */
2385 int receiveXappMessages(Sctp_Map_t *sctpMap,
2386                         RmrMessagesBuffer_t &rmrMessageBuffer,
2387                         struct timespec &ts) {
2388     int loglevel = mdclog_level_get();
2389     if (rmrMessageBuffer.rcvMessage == nullptr) {
2390         //we have error
2391         mdclog_write(MDCLOG_ERR, "RMR Allocation message, %s", strerror(errno));
2392         return -1;
2393     }
2394
2395 //    if (loglevel >= MDCLOG_DEBUG) {
2396 //        mdclog_write(MDCLOG_DEBUG, "Call to rmr_rcv_msg");
2397 //    }
2398     rmrMessageBuffer.rcvMessage = rmr_rcv_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.rcvMessage);
2399     if (rmrMessageBuffer.rcvMessage == nullptr) {
2400         mdclog_write(MDCLOG_ERR, "RMR Receiving message with null pointer, Reallocated rmr message buffer");
2401         rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
2402         return -2;
2403     }
2404     ReportingMessages_t message;
2405     message.message.direction = 'D';
2406     message.message.time.tv_nsec = ts.tv_nsec;
2407     message.message.time.tv_sec = ts.tv_sec;
2408
2409     // get message payload
2410     //auto msgData = msg->payload;
2411 #ifdef UNIT_TEST
2412     rmrMessageBuffer.rcvMessage->state = 0;
2413 #endif
2414     if (rmrMessageBuffer.rcvMessage->state != 0) {
2415         mdclog_write(MDCLOG_ERR, "RMR Receiving message with stat = %d", rmrMessageBuffer.rcvMessage->state);
2416         return -1;
2417     }
2418     rmr_get_meid(rmrMessageBuffer.rcvMessage, (unsigned char *)message.message.enodbName);
2419     message.peerInfo = (ConnectedCU_t *) sctpMap->find(message.message.enodbName);
2420     if (message.peerInfo == nullptr) {
2421         auto type = rmrMessageBuffer.rcvMessage->mtype;
2422         switch (type) {
2423             case RIC_SCTP_CLEAR_ALL:
2424             case E2_TERM_KEEP_ALIVE_REQ:
2425             case RIC_HEALTH_CHECK_REQ:
2426                 break;
2427             default:
2428 #ifdef UNIT_TEST
2429     break;
2430 #endif    
2431                 mdclog_write(MDCLOG_ERR, "Failed to send message no CU entry %s", message.message.enodbName);
2432                 return -1;
2433         }
2434     }
2435
2436     if (rmrMessageBuffer.rcvMessage->mtype != RIC_HEALTH_CHECK_REQ) {
2437         num_of_XAPP_messages.fetch_add(1, std::memory_order_release);
2438
2439     }
2440     switch (rmrMessageBuffer.rcvMessage->mtype) {
2441         case RIC_E2_SETUP_RESP : {
2442             if (loglevel >= MDCLOG_DEBUG) {
2443                 mdclog_write(MDCLOG_DEBUG, "RIC_E2_SETUP_RESP");
2444             }
2445             if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2446                 break;
2447             }
2448 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))            
2449             message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2450             message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment(rmrMessageBuffer.rcvMessage->len);
2451 #endif            
2452             if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2453                 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_SETUP_RESP");
2454                 return -6;
2455             }
2456             break;
2457         }
2458         case RIC_E2_SETUP_FAILURE : {
2459             if (loglevel >= MDCLOG_DEBUG) {
2460                 mdclog_write(MDCLOG_DEBUG, "RIC_E2_SETUP_FAILURE");
2461             }
2462             if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2463                 break;
2464             }
2465 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))            
2466             message.peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2467             message.peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment(rmrMessageBuffer.rcvMessage->len);
2468 #endif            
2469             if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2470                 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_SETUP_FAILURE");
2471                 return -6;
2472             }
2473             break;
2474         }
2475         case RIC_ERROR_INDICATION: {
2476             if (loglevel >= MDCLOG_DEBUG) {
2477                 mdclog_write(MDCLOG_DEBUG, "RIC_ERROR_INDICATION");
2478             }
2479 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))            
2480             message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_ErrorIndication]->Increment();
2481             message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_ErrorIndication]->Increment(rmrMessageBuffer.rcvMessage->len);
2482 #endif            
2483             if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2484                 mdclog_write(MDCLOG_ERR, "Failed to send RIC_ERROR_INDICATION");
2485                 return -6;
2486             }
2487             break;
2488         }
2489         case RIC_SUB_REQ: {
2490             if (loglevel >= MDCLOG_DEBUG) {
2491                 mdclog_write(MDCLOG_DEBUG, "RIC_SUB_REQ");
2492             }
2493 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))            
2494             message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
2495             message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment(rmrMessageBuffer.rcvMessage->len);
2496 #endif            
2497             if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2498                 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SUB_REQ");
2499                 return -6;
2500             }
2501             break;
2502         }
2503         case RIC_SUB_DEL_REQ: {
2504             if (loglevel >= MDCLOG_DEBUG) {
2505                 mdclog_write(MDCLOG_DEBUG, "RIC_SUB_DEL_REQ");
2506             }
2507 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))            
2508             message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
2509             message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment(rmrMessageBuffer.rcvMessage->len);
2510 #endif            
2511             if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2512                 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SUB_DEL_REQ");
2513                 return -6;
2514             }
2515             break;
2516         }
2517         case RIC_CONTROL_REQ: {
2518             if (loglevel >= MDCLOG_DEBUG) {
2519                 mdclog_write(MDCLOG_DEBUG, "RIC_CONTROL_REQ");
2520             }
2521 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))            
2522             message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
2523             message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment(rmrMessageBuffer.rcvMessage->len);
2524 #endif            
2525             if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2526                 mdclog_write(MDCLOG_ERR, "Failed to send RIC_CONTROL_REQ");
2527                 return -6;
2528             }
2529             break;
2530         }
2531         case RIC_SERVICE_QUERY: {
2532             if (loglevel >= MDCLOG_DEBUG) {
2533                 mdclog_write(MDCLOG_DEBUG, "RIC_SERVICE_QUERY");
2534             }
2535             if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2536                 break;
2537             }
2538 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))            
2539             message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceQuery]->Increment();
2540             message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceQuery]->Increment(rmrMessageBuffer.rcvMessage->len);
2541 #endif            
2542             if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2543                 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_QUERY");
2544                 return -6;
2545             }
2546             break;
2547         }
2548         case RIC_SERVICE_UPDATE_ACK: {
2549             if (loglevel >= MDCLOG_DEBUG) {
2550                 mdclog_write(MDCLOG_DEBUG, "RIC_SERVICE_UPDATE_ACK");
2551             }
2552             if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2553                 mdclog_write(MDCLOG_ERR, "error in PER_FromXML");
2554                 break;
2555             }
2556 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))            
2557             message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
2558             message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
2559 #endif            
2560             if (loglevel >= MDCLOG_DEBUG) {
2561                 mdclog_write(MDCLOG_DEBUG, "Before sending to CU");
2562             }
2563             if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2564                 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_UPDATE_ACK");
2565                 return -6;
2566             }
2567             break;
2568         }
2569         case RIC_SERVICE_UPDATE_FAILURE: {
2570             if (loglevel >= MDCLOG_DEBUG) {
2571                 mdclog_write(MDCLOG_DEBUG, "RIC_SERVICE_UPDATE_FAILURE");
2572             }
2573             if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2574                 break;
2575             }
2576 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))            
2577             message.peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
2578             message.peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
2579 #endif            
2580             if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2581                 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_UPDATE_FAILURE");
2582                 return -6;
2583             }
2584             break;
2585         }
2586         case RIC_E2_RESET_REQ: {
2587             if (loglevel >= MDCLOG_DEBUG) {
2588                 mdclog_write(MDCLOG_DEBUG, "RIC_E2_RESET_REQ");
2589             }
2590             if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2591                 break;
2592             }
2593 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))            
2594             message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2595             message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment(rmrMessageBuffer.rcvMessage->len);
2596 #endif            
2597             if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2598                 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_RESET");
2599                 return -6;
2600             }
2601             break;
2602         }
2603         case RIC_E2_RESET_RESP: {
2604             if (loglevel >= MDCLOG_DEBUG) {
2605                 mdclog_write(MDCLOG_DEBUG, "RIC_E2_RESET_RESP");
2606             }
2607             if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2608                 break;
2609             }
2610 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))            
2611             message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2612             message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment(rmrMessageBuffer.rcvMessage->len);
2613 #endif            
2614             if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2615                 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_RESET_RESP");
2616                 return -6;
2617             }
2618             break;
2619         }
2620         case RIC_SCTP_CLEAR_ALL: {
2621             mdclog_write(MDCLOG_INFO, "RIC_SCTP_CLEAR_ALL");
2622             // loop on all keys and close socket and then erase all map.
2623             vector<char *> v;
2624             sctpMap->getKeys(v);
2625             for (auto const &iter : v) { //}; iter != sctpMap.end(); iter++) {
2626                 if (!boost::starts_with((string) (iter), "host:") && !boost::starts_with((string) (iter), "msg:")) {
2627                     auto *peerInfo = (ConnectedCU_t *) sctpMap->find(iter);
2628                     if (peerInfo == nullptr) {
2629                         continue;
2630                     }
2631                     close(peerInfo->fileDescriptor);
2632                     memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
2633                     message.message.direction = 'D';
2634                     message.message.time.tv_nsec = ts.tv_nsec;
2635                     message.message.time.tv_sec = ts.tv_sec;
2636
2637                     message.message.asnLength = rmrMessageBuffer.sendMessage->len =
2638                             snprintf((char *)rmrMessageBuffer.sendMessage->payload,
2639                                      256,
2640                                      "%s|RIC_SCTP_CLEAR_ALL",
2641                                      peerInfo->enodbName);
2642                     message.message.asndata = rmrMessageBuffer.sendMessage->payload;
2643                     mdclog_write(MDCLOG_INFO, "%s", message.message.asndata);
2644                     if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
2645                         mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
2646                     }
2647                     free(peerInfo);
2648                 }
2649             }
2650
2651             sleep(1);
2652             sctpMap->clear();
2653             break;
2654         }
2655         case E2_TERM_KEEP_ALIVE_REQ: {
2656             // send message back
2657             rmr_bytes2payload(rmrMessageBuffer.sendMessage,
2658                               (unsigned char *)rmrMessageBuffer.ka_message,
2659                               rmrMessageBuffer.ka_message_len);
2660             rmrMessageBuffer.sendMessage->mtype = E2_TERM_KEEP_ALIVE_RESP;
2661             rmrMessageBuffer.sendMessage->state = 0;
2662             static unsigned char tx[32];
2663             auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2664             rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, txLen);
2665 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))            
2666             rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2667 #endif
2668             if (rmrMessageBuffer.sendMessage == nullptr) {
2669                 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
2670                 mdclog_write(MDCLOG_ERR, "Failed to send E2_TERM_KEEP_ALIVE_RESP RMR message returned NULL");
2671             } else if (rmrMessageBuffer.sendMessage->state != 0)  {
2672                 mdclog_write(MDCLOG_ERR, "Failed to send E2_TERM_KEEP_ALIVE_RESP, on RMR state = %d ( %s)",
2673                              rmrMessageBuffer.sendMessage->state, translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str());
2674             } else if (loglevel >= MDCLOG_DEBUG) {
2675                 mdclog_write(MDCLOG_DEBUG, "Got Keep Alive Request send : %s", rmrMessageBuffer.ka_message);
2676             }
2677
2678             break;
2679         }
2680         case RIC_HEALTH_CHECK_REQ: {
2681             static int counter = 0;
2682             // send message back
2683             rmr_bytes2payload(rmrMessageBuffer.rcvMessage,
2684                               (unsigned char *)"OK",
2685                               2);
2686             rmrMessageBuffer.rcvMessage->mtype = RIC_HEALTH_CHECK_RESP;
2687             rmrMessageBuffer.rcvMessage->state = 0;
2688             static unsigned char tx[32];
2689             auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2690             rmr_bytes2xact(rmrMessageBuffer.rcvMessage, tx, txLen);
2691             rmrMessageBuffer.rcvMessage = rmr_rts_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.rcvMessage);
2692             //rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2693             if (rmrMessageBuffer.rcvMessage == nullptr) {
2694                 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
2695                 mdclog_write(MDCLOG_ERR, "Failed to send RIC_HEALTH_CHECK_RESP RMR message returned NULL");
2696             } else if (rmrMessageBuffer.rcvMessage->state != 0)  {
2697                 mdclog_write(MDCLOG_ERR, "Failed to send RIC_HEALTH_CHECK_RESP, on RMR state = %d ( %s)",
2698                              rmrMessageBuffer.rcvMessage->state, translateRmrErrorMessages(rmrMessageBuffer.rcvMessage->state).c_str());
2699             } else if (loglevel >= MDCLOG_DEBUG && ++counter % 100 == 0) {
2700                 mdclog_write(MDCLOG_DEBUG, "Got %d RIC_HEALTH_CHECK_REQ Request send : OK", counter);
2701             }
2702
2703             break;
2704         }
2705
2706         default:
2707             mdclog_write(MDCLOG_WARN, "Message Type : %d is not supported", rmrMessageBuffer.rcvMessage->mtype);
2708             message.message.asndata = rmrMessageBuffer.rcvMessage->payload;
2709             message.message.asnLength = rmrMessageBuffer.rcvMessage->len;
2710             message.message.time.tv_nsec = ts.tv_nsec;
2711             message.message.time.tv_sec = ts.tv_sec;
2712             message.message.messageType = rmrMessageBuffer.rcvMessage->mtype;
2713
2714             buildJsonMessage(message);
2715
2716
2717             return -7;
2718     }
2719     if (mdclog_level_get() >= MDCLOG_DEBUG) {
2720         mdclog_write(MDCLOG_DEBUG, "EXIT OK from %s", __FUNCTION__);
2721     }
2722     return 0;
2723 }
2724
2725 /**
2726  * Send message to the CU that is not expecting for successful or unsuccessful results
2727  * @param messageBuffer
2728  * @param message
2729  * @param failedMsgId
2730  * @param sctpMap
2731  * @return
2732  */
2733 int sendDirectionalSctpMsg(RmrMessagesBuffer_t &messageBuffer,
2734                            ReportingMessages_t &message,
2735                            int failedMsgId,
2736                            Sctp_Map_t *sctpMap) {
2737     if (mdclog_level_get() >= MDCLOG_DEBUG) {
2738         mdclog_write(MDCLOG_DEBUG, "send message: %d to %s address", message.message.messageType, message.message.enodbName);
2739     }
2740
2741     getRequestMetaData(message, messageBuffer);
2742     if (mdclog_level_get() >= MDCLOG_INFO) {
2743         mdclog_write(MDCLOG_INFO, "send message to %s address", message.message.enodbName);
2744     }
2745
2746     auto rc = sendMessagetoCu(sctpMap, messageBuffer, message, failedMsgId);
2747     return rc;
2748 }
2749
2750 /**
2751  *
2752  * @param sctpMap
2753  * @param messageBuffer
2754  * @param message
2755  * @param failedMesgId
2756  * @return
2757  */
2758 int sendMessagetoCu(Sctp_Map_t *sctpMap,
2759                     RmrMessagesBuffer_t &messageBuffer,
2760                     ReportingMessages_t &message,
2761                     int failedMesgId) {
2762     // get the FD
2763     message.message.messageType = messageBuffer.rcvMessage->mtype;
2764     auto rc = sendSctpMsg(message.peerInfo, message, sctpMap);
2765     return rc;
2766 }
2767
2768
2769 /**
2770  *
2771  * @param epoll_fd
2772  * @param peerInfo
2773  * @param events
2774  * @param sctpMap
2775  * @param enodbName
2776  * @param msgType
2777  * @return
2778  */
2779 int addToEpoll(int epoll_fd,
2780                ConnectedCU_t *peerInfo,
2781                uint32_t events,
2782                Sctp_Map_t *sctpMap,
2783                char *enodbName,
2784                int msgType) {
2785     // Add to Epol
2786     struct epoll_event event{};
2787     event.data.ptr = peerInfo;
2788     event.events = events;
2789     if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, peerInfo->fileDescriptor, &event) < 0) {
2790 #if !(defined(UNIT_TEST) || defined(MODULE_TEST)) 
2791         if (mdclog_level_get() >= MDCLOG_DEBUG) {
2792             mdclog_write(MDCLOG_DEBUG, "epoll_ctl EPOLL_CTL_ADD (may check not to quit here), %s, %s %d",
2793                          strerror(errno), __func__, __LINE__);
2794         }
2795         close(peerInfo->fileDescriptor);
2796         if (enodbName != nullptr) {
2797             cleanHashEntry(peerInfo, sctpMap);
2798             char key[MAX_ENODB_NAME_SIZE * 2];
2799             snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", enodbName, msgType);
2800             if (mdclog_level_get() >= MDCLOG_DEBUG) {
2801                 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
2802             }
2803             auto tmp = sctpMap->find(key);
2804             if (tmp) {
2805                 free(tmp);
2806                 sctpMap->erase(key);
2807             }
2808         } else {
2809             peerInfo->enodbName[0] = 0;
2810         }
2811         mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may check not to quit here)");
2812         return -1;
2813 #endif
2814     }
2815     return 0;
2816 }
2817
2818 /**
2819  *
2820  * @param epoll_fd
2821  * @param peerInfo
2822  * @param events
2823  * @param sctpMap
2824  * @param enodbName
2825  * @param msgType
2826  * @return
2827  */
2828 int modifyToEpoll(int epoll_fd,
2829                   ConnectedCU_t *peerInfo,
2830                   uint32_t events,
2831                   Sctp_Map_t *sctpMap,
2832                   char *enodbName,
2833                   int msgType) {
2834     // Add to Epol
2835     struct epoll_event event{};
2836     event.data.ptr = peerInfo;
2837     event.events = events;
2838     if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, peerInfo->fileDescriptor, &event) < 0) {
2839         if (mdclog_level_get() >= MDCLOG_DEBUG) {
2840             mdclog_write(MDCLOG_DEBUG, "epoll_ctl EPOLL_CTL_MOD (may check not to quit here), %s, %s %d",
2841                          strerror(errno), __func__, __LINE__);
2842         }
2843         close(peerInfo->fileDescriptor);
2844         cleanHashEntry(peerInfo, sctpMap);
2845         char key[MAX_ENODB_NAME_SIZE * 2];
2846         snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", enodbName, msgType);
2847         if (mdclog_level_get() >= MDCLOG_DEBUG) {
2848             mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
2849         }
2850         auto tmp = sctpMap->find(key);
2851         if (tmp) {
2852             free(tmp);
2853         }
2854         sctpMap->erase(key);
2855         mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may check not to quit here)");
2856         return -1;
2857     }
2858     return 0;
2859 }
2860
2861
2862 int sendRmrMessage(RmrMessagesBuffer_t &rmrMessageBuffer, ReportingMessages_t &message) {
2863     buildJsonMessage(message);
2864 #ifndef UNIT_TEST
2865     rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2866 #else
2867     rmrMessageBuffer.sendMessage->state = RMR_ERR_RETRY;
2868 #endif
2869     if (rmrMessageBuffer.sendMessage == nullptr) {
2870         rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
2871         mdclog_write(MDCLOG_ERR, "RMR failed send message returned with NULL pointer");
2872         return -1;
2873     }
2874
2875     if (rmrMessageBuffer.sendMessage->state != 0) {
2876         char meid[RMR_MAX_MEID]{};
2877         if (rmrMessageBuffer.sendMessage->state == RMR_ERR_RETRY) {
2878             usleep(5);
2879             rmrMessageBuffer.sendMessage->state = 0;
2880             mdclog_write(MDCLOG_INFO, "RETRY sending Message type %d to Xapp from %s",
2881                          rmrMessageBuffer.sendMessage->mtype,
2882                          rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
2883 #ifndef UNIT_TEST
2884             rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2885 #endif
2886             if (rmrMessageBuffer.sendMessage == nullptr) {
2887                 mdclog_write(MDCLOG_ERR, "RMR failed send message returned with NULL pointer");
2888                 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
2889                 return -1;
2890             } else if (rmrMessageBuffer.sendMessage->state != 0) {
2891                 mdclog_write(MDCLOG_ERR,
2892                              "Message state %s while sending request %d to Xapp from %s after retry of 10 microseconds",
2893                              translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str(),
2894                              rmrMessageBuffer.sendMessage->mtype,
2895                              rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
2896                 auto rc = rmrMessageBuffer.sendMessage->state;
2897                 return rc;
2898             }
2899         } else {
2900             mdclog_write(MDCLOG_ERR, "Message state %s while sending request %d to Xapp from %s",
2901                          translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str(),
2902                          rmrMessageBuffer.sendMessage->mtype,
2903                          rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
2904             return rmrMessageBuffer.sendMessage->state;
2905         }
2906     }
2907     return 0;
2908 }
2909
2910 void buildJsonMessage(ReportingMessages_t &message) {
2911 #ifdef UNIT_TEST
2912     jsonTrace = true;
2913 #endif
2914     if (jsonTrace) {
2915         message.outLen = sizeof(message.base64Data);
2916         base64::encode((const unsigned char *) message.message.asndata,
2917                        (const int) message.message.asnLength,
2918                        message.base64Data,
2919                        message.outLen);
2920         if (mdclog_level_get() >= MDCLOG_DEBUG) {
2921             mdclog_write(MDCLOG_DEBUG, "Tracing: ASN length = %d, base64 message length = %d ",
2922                          (int) message.message.asnLength,
2923                          (int) message.outLen);
2924         }
2925
2926         snprintf(message.buffer, sizeof(message.buffer),
2927                  "{\"header\": {\"ts\": \"%ld.%09ld\","
2928                  "\"ranName\": \"%s\","
2929                  "\"messageType\": %d,"
2930                  "\"direction\": \"%c\"},"
2931                  "\"base64Length\": %d,"
2932                  "\"asnBase64\": \"%s\"}",
2933                  message.message.time.tv_sec,
2934                  message.message.time.tv_nsec,
2935                  message.message.enodbName,
2936                  message.message.messageType,
2937                  message.message.direction,
2938                  (int) message.outLen,
2939                  message.base64Data);
2940         static src::logger_mt &lg = my_logger::get();
2941
2942         BOOST_LOG(lg) << message.buffer;
2943     }
2944 }
2945
2946
2947 /**
2948  * take RMR error code to string
2949  * @param state
2950  * @return
2951  */
2952 string translateRmrErrorMessages(int state) {
2953     string str = {};
2954     switch (state) {
2955         case RMR_OK:
2956             str = "RMR_OK - state is good";
2957             break;
2958         case RMR_ERR_BADARG:
2959             str = "RMR_ERR_BADARG - argument passed to function was unusable";
2960             break;
2961         case RMR_ERR_NOENDPT:
2962             str = "RMR_ERR_NOENDPT - send//call could not find an endpoint based on msg type";
2963             break;
2964         case RMR_ERR_EMPTY:
2965             str = "RMR_ERR_EMPTY - msg received had no payload; attempt to send an empty message";
2966             break;
2967         case RMR_ERR_NOHDR:
2968             str = "RMR_ERR_NOHDR - message didn't contain a valid header";
2969             break;
2970         case RMR_ERR_SENDFAILED:
2971             str = "RMR_ERR_SENDFAILED - send failed; errno has nano reason";
2972             break;
2973         case RMR_ERR_CALLFAILED:
2974             str = "RMR_ERR_CALLFAILED - unable to send call() message";
2975             break;
2976         case RMR_ERR_NOWHOPEN:
2977             str = "RMR_ERR_NOWHOPEN - no wormholes are open";
2978             break;
2979         case RMR_ERR_WHID:
2980             str = "RMR_ERR_WHID - wormhole id was invalid";
2981             break;
2982         case RMR_ERR_OVERFLOW:
2983             str = "RMR_ERR_OVERFLOW - operation would have busted through a buffer/field size";
2984             break;
2985         case RMR_ERR_RETRY:
2986             str = "RMR_ERR_RETRY - request (send/call/rts) failed, but caller should retry (EAGAIN for wrappers)";
2987             break;
2988         case RMR_ERR_RCVFAILED:
2989             str = "RMR_ERR_RCVFAILED - receive failed (hard error)";
2990             break;
2991         case RMR_ERR_TIMEOUT:
2992             str = "RMR_ERR_TIMEOUT - message processing call timed out";
2993             break;
2994         case RMR_ERR_UNSET:
2995             str = "RMR_ERR_UNSET - the message hasn't been populated with a transport buffer";
2996             break;
2997         case RMR_ERR_TRUNC:
2998             str = "RMR_ERR_TRUNC - received message likely truncated";
2999             break;
3000         case RMR_ERR_INITFAILED:
3001             str = "RMR_ERR_INITFAILED - initialisation of something (probably message) failed";
3002             break;
3003         case RMR_ERR_NOTSUPP:
3004             str = "RMR_ERR_NOTSUPP - the request is not supported, or RMr was not initialised for the request";
3005             break;
3006         default:
3007             char buf[128]{};
3008             snprintf(buf, sizeof buf, "UNDOCUMENTED RMR_ERR : %d", state);
3009             str = buf;
3010             break;
3011     }
3012     return str;
3013 }