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