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