1 // Copyright 2019 AT&T Intellectual Property
2 // Copyright 2019 Nokia
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
16 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
17 // platform project (RICP).
19 // TODO: High-level file comment.
23 #include <3rdparty/oranE2/RANfunctions-List.h>
24 #include "sctpThread.h"
25 #include "BuildRunName.h"
27 //#include "3rdparty/oranE2SM/E2SM-gNB-NRT-RANfunction-Definition.h"
28 //#include "BuildXml.h"
29 //#include "pugixml/src/pugixml.hpp"
32 #include <sys/inotify.h>
37 //using namespace std::placeholders;
38 using namespace boost::filesystem;
39 using namespace prometheus;
47 // need to expose without the include of gcov
48 extern "C" void __gcov_flush(void);
49 #define LOG_FILE_CONFIG_MAP "CONFIG_MAP_NAME"
51 static void catch_function(int signal) {
57 BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger, src::logger_mt)
59 boost::shared_ptr<sinks::synchronous_sink<sinks::text_file_backend>> boostLogger;
60 double cpuClock = 0.0;
61 bool jsonTrace = false;
63 char* getinterfaceip()
67 struct hostent *host_entry;
69 retVal = gethostname(hostname, sizeof(hostname));
72 host_entry = gethostbyname(hostname);
73 if ( host_entry == NULL )
75 IP = inet_ntoa(*((struct in_addr*) host_entry->h_addr_list[0]));
80 static int enable_log_change_notify(const char* fileName)
84 if ( lstat(fileName,&fileInfo) == 0 )
86 ret = register_log_change_notify(fileName);
92 static int register_log_change_notify(const char *fileName)
94 pthread_attr_t cb_attr;
96 pthread_attr_init(&cb_attr);
97 pthread_attr_setdetachstate(&cb_attr,PTHREAD_CREATE_DETACHED);
98 return pthread_create(&tid, &cb_attr,&monitor_loglevel_change_handler,(void *)fileName);
102 static void * monitor_loglevel_change_handler(void* arg)
104 char *fileName = (char*) arg;
105 int ifd; // the inotify file des
106 int wfd; // the watched file des
108 char rbuf[4096]; // large read buffer as the event is var len
111 struct timeval timeout;
112 char* dname=NULL; // directory name
113 char* bname = NULL; // basename
115 char* log_level=NULL;
117 dname = strdup( fileName); // defrock the file name into dir and basename
118 if( (tok = strrchr( dname, '/' )) != NULL ) {
120 bname = strdup( tok+1 );
124 ifd = inotify_init1( 0 ); // initialise watcher setting blocking read (no option)
126 fprintf( stderr, "### ERR ### unable to initialise file watch %s\n", strerror( errno ) );
128 wfd = inotify_add_watch( ifd, dname, IN_MOVED_TO | IN_CLOSE_WRITE ); // we only care about close write changes
131 fprintf( stderr, "### ERR ### unable to add watch on config file %s: %s\n", fileName, strerror( errno ) );
135 memset( &timeout, 0, sizeof(timeout) );
140 res = select (ifd + 1, &fds, NULL, NULL, &timeout);
143 n = read( ifd, rbuf, sizeof( rbuf ) ); // read the event
145 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
146 if( errno == EAGAIN ) {
148 printf( "### CRIT ### config listener read err: %s\n", strerror( errno ) );
154 //Retrieving Log Level from configmap by parsing configmap file
155 log_level = parse_file(fileName);
156 update_mdc_log_level_severity(log_level); //setting log level
160 inotify_rm_watch(ifd,wfd);
170 void update_mdc_log_level_severity(char* log_level)
172 mdclog_severity_t level = MDCLOG_ERR;
174 if(log_level == NULL)
176 printf("### ERR ### Invalid Log-Level Configuration in ConfigMap, Default Log-Level Applied: %d\n",level);
178 else if(strcasecmp(log_level,"1")==0)
182 else if(strcasecmp(log_level,"2")==0)
186 else if(strcasecmp(log_level,"3")==0)
190 else if(strcasecmp(log_level,"4")==0)
192 level = MDCLOG_DEBUG;
195 mdclog_level_set(level);
197 static char* parse_file(char* filename)
201 char *string_match = "log-level";
203 FILE *file = fopen ( filename, "r" );
207 while ( fgets ( line, sizeof line, file ) != NULL )
209 token = strtok(line, search);
210 if(strcmp(token,string_match)==0)
213 token = strtok(NULL, search);
214 token = strtok(token, "\n");//removing newline if any
221 return(strdup(token));
226 char *read_env_param(const char*envkey)
230 char *value = getenv(envkey);
232 return strdup(value);
237 void dynamic_log_level_change()
239 char *logFile_Name = read_env_param(LOG_FILE_CONFIG_MAP);
240 char* log_level_init=NULL;
243 log_level_init = parse_file(logFile_Name);
244 update_mdc_log_level_severity(log_level_init); //setting log level
245 free(log_level_init);
248 enable_log_change_notify(logFile_Name);
254 int log_change_monitor = 0;
256 mdclog_attr_init(&attr);
257 mdclog_attr_set_ident(attr, "E2Terminator");
259 if(mdclog_format_initialize(log_change_monitor)!=0)
260 mdclog_write(MDCLOG_ERR, "Failed to intialize MDC log format !!!");
261 dynamic_log_level_change();
262 mdclog_attr_destroy(attr);
264 auto start_time = std::chrono::high_resolution_clock::now();
265 typedef std::chrono::duration<double, std::ratio<1,1>> seconds_t;
268 return seconds_t(std::chrono::high_resolution_clock::now() - start_time).count();
271 double approx_CPU_MHz(unsigned sleepTime) {
272 using namespace std::chrono_literals;
274 uint64_t cycles_start = rdtscp(aux);
275 double time_start = age();
276 std::this_thread::sleep_for(sleepTime * 1ms);
277 uint64_t elapsed_cycles = rdtscp(aux) - cycles_start;
278 double elapsed_time = age() - time_start;
279 return elapsed_cycles / elapsed_time;
282 //std::atomic<int64_t> rmrCounter{0};
283 std::atomic<int64_t> num_of_messages{0};
284 std::atomic<int64_t> num_of_XAPP_messages{0};
285 static long transactionCounter = 0;
287 int buildListeningPort(sctp_params_t &sctpParams) {
288 sctpParams.listenFD = socket(AF_INET6, SOCK_STREAM, IPPROTO_SCTP);
289 if (sctpParams.listenFD <= 0) {
290 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
291 mdclog_write(MDCLOG_ERR, "Error Opening socket, %s", strerror(errno));
295 struct sctp_initmsg initmsg;
296 memset (&initmsg, 0, sizeof (initmsg));
297 initmsg.sinit_num_ostreams = 2;
298 initmsg.sinit_max_instreams = 2;
299 initmsg.sinit_max_attempts = 4;
300 setsockopt (sctpParams.listenFD, IPPROTO_SCTP, SCTP_INITMSG, &initmsg, sizeof (initmsg));
302 struct sockaddr_in6 serverAddress {};
303 serverAddress.sin6_family = AF_INET6;
304 serverAddress.sin6_addr = in6addr_any;
305 serverAddress.sin6_port = htons(sctpParams.sctpPort);
306 if (bind(sctpParams.listenFD, (SA *)&serverAddress, sizeof(serverAddress)) < 0 ) {
307 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
308 mdclog_write(MDCLOG_ERR, "Error binding port %d. %s", sctpParams.sctpPort, strerror(errno));
312 if (setSocketNoBlocking(sctpParams.listenFD) == -1) {
313 //mdclog_write(MDCLOG_ERR, "Error binding. %s", strerror(errno));
316 if (mdclog_level_get() >= MDCLOG_DEBUG) {
317 struct sockaddr_in6 clientAddress {};
318 socklen_t len = sizeof(clientAddress);
319 getsockname(sctpParams.listenFD, (SA *)&clientAddress, &len);
321 inet_ntop(AF_INET6, &clientAddress.sin6_addr, buff, sizeof(buff));
322 mdclog_write(MDCLOG_DEBUG, "My address: %s, port %d\n", buff, htons(clientAddress.sin6_port));
325 if (listen(sctpParams.listenFD, SOMAXCONN) < 0) {
326 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
327 mdclog_write(MDCLOG_ERR, "Error listening. %s\n", strerror(errno));
331 struct epoll_event event {};
332 event.events = EPOLLIN | EPOLLET;
333 event.data.fd = sctpParams.listenFD;
335 // add listening port to epoll
336 if (epoll_ctl(sctpParams.epoll_fd, EPOLL_CTL_ADD, sctpParams.listenFD, &event)) {
337 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
338 printf("Failed to add descriptor to epoll\n");
339 mdclog_write(MDCLOG_ERR, "Failed to add descriptor to epoll. %s\n", strerror(errno));
347 int buildConfiguration(sctp_params_t &sctpParams) {
348 path p = (sctpParams.configFilePath + "/" + sctpParams.configFileName).c_str();
350 const int size = 2048;
351 auto fileSize = file_size(p);
352 if (fileSize > size) {
353 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
354 mdclog_write(MDCLOG_ERR, "File %s larger than %d", p.string().c_str(), size);
359 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
360 mdclog_write(MDCLOG_ERR, "Configuration File %s not exists", p.string().c_str());
366 if (conf.openConfigFile(p.string()) == -1) {
367 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
368 mdclog_write(MDCLOG_ERR, "Filed to open config file %s, %s",
369 p.string().c_str(), strerror(errno));
373 int rmrPort = conf.getIntValue("nano");
375 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
376 mdclog_write(MDCLOG_ERR, "illegal RMR port ");
380 sctpParams.rmrPort = (uint16_t)rmrPort;
381 snprintf(sctpParams.rmrAddress, sizeof(sctpParams.rmrAddress), "%d", (int) (sctpParams.rmrPort));
382 auto tmpStr = conf.getStringValue("volume");
383 if (tmpStr.length() == 0) {
384 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
385 mdclog_write(MDCLOG_ERR, "illegal volume.");
390 char tmpLogFilespec[VOLUME_URL_SIZE];
391 tmpLogFilespec[0] = 0;
392 sctpParams.volume[0] = 0;
393 snprintf(sctpParams.volume, VOLUME_URL_SIZE, "%s", tmpStr.c_str());
394 // copy the name to temp file as well
395 snprintf(tmpLogFilespec, VOLUME_URL_SIZE, "%s", tmpStr.c_str());
398 // define the file name in the tmp directory under the volume
399 strcat(tmpLogFilespec,"/tmp/E2Term_%Y-%m-%d_%H-%M-%S.%N.tmpStr");
401 sctpParams.myIP = conf.getStringValue("local-ip");
402 if (sctpParams.myIP.length() == 0) {
403 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
404 mdclog_write(MDCLOG_ERR, "illegal local-ip.");
409 int sctpPort = conf.getIntValue("sctp-port");
410 if (sctpPort == -1) {
411 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
412 mdclog_write(MDCLOG_ERR, "illegal SCTP port ");
416 sctpParams.sctpPort = (uint16_t)sctpPort;
418 sctpParams.fqdn = conf.getStringValue("external-fqdn");
419 if (sctpParams.fqdn.length() == 0) {
420 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
421 mdclog_write(MDCLOG_ERR, "illegal external-fqdn");
426 std::string pod = conf.getStringValue("pod_name");
428 if (pod.length() == 0) {
429 mdclog_write(MDCLOG_ERR, "illegal pod_name in config file");
432 auto *podName = getenv(pod.c_str());
433 if (podName == nullptr) {
434 mdclog_write(MDCLOG_ERR, "illegal pod_name or environment variable not exists : %s", pod.c_str());
438 sctpParams.podName.assign(podName);
439 if (sctpParams.podName.length() == 0) {
440 mdclog_write(MDCLOG_ERR, "illegal pod_name");
445 tmpStr = conf.getStringValue("trace");
446 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
447 if ((tmpStr.compare("start")) == 0) {
448 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
449 mdclog_write(MDCLOG_INFO, "Trace set to: start");
450 sctpParams.trace = true;
452 } else if ((tmpStr.compare("stop")) == 0) {
453 mdclog_write(MDCLOG_INFO, "Trace set to: stop");
454 sctpParams.trace = false;
456 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
457 mdclog_write(MDCLOG_ERR, "Trace was set to wrong value %s, set to stop", tmpStr.c_str());
458 sctpParams.trace = false;
461 jsonTrace = sctpParams.trace;
463 sctpParams.epollTimeOut = -1;
465 tmpStr = conf.getStringValue("prometheusPort");
466 if (tmpStr.length() != 0) {
467 sctpParams.prometheusPort = tmpStr;
470 sctpParams.ka_message_length = snprintf(sctpParams.ka_message, KA_MESSAGE_SIZE, "{\"address\": \"%s:%d\","
472 "\"pod_name\": \"%s\"}",
473 (const char *)sctpParams.myIP.c_str(),
475 sctpParams.fqdn.c_str(),
476 sctpParams.podName.c_str());
478 if (mdclog_level_get() >= MDCLOG_INFO) {
479 mdclog_write(MDCLOG_DEBUG,"RMR Port: %s", to_string(sctpParams.rmrPort).c_str());
480 mdclog_write(MDCLOG_DEBUG,"LogLevel: %s", to_string(sctpParams.logLevel).c_str());
481 mdclog_write(MDCLOG_DEBUG,"volume: %s", sctpParams.volume);
482 mdclog_write(MDCLOG_DEBUG,"tmpLogFilespec: %s", tmpLogFilespec);
483 mdclog_write(MDCLOG_DEBUG,"my ip: %s", sctpParams.myIP.c_str());
484 mdclog_write(MDCLOG_DEBUG,"pod name: %s", sctpParams.podName.c_str());
486 mdclog_write(MDCLOG_INFO, "running parameters for instance : %s", sctpParams.ka_message);
489 // Files written to the current working directory
490 boostLogger = logging::add_file_log(
491 keywords::file_name = tmpLogFilespec, // to temp directory
492 keywords::rotation_size = 10 * 1024 * 1024,
493 keywords::time_based_rotation = sinks::file::rotation_at_time_interval(posix_time::hours(1)),
494 keywords::format = "%Message%"
495 //keywords::format = "[%TimeStamp%]: %Message%" // use each tmpStr with time stamp
498 // Setup a destination folder for collecting rotated (closed) files --since the same volume can use rename()
499 boostLogger->locked_backend()->set_file_collector(sinks::file::make_collector(
500 keywords::target = sctpParams.volume
503 // Upon restart, scan the directory for files matching the file_name pattern
504 boostLogger->locked_backend()->scan_for_files();
506 // Enable auto-flushing after each tmpStr record written
507 if (mdclog_level_get() >= MDCLOG_DEBUG) {
508 boostLogger->locked_backend()->auto_flush(true);
514 void startPrometheus(sctp_params_t &sctpParams) {
515 auto podName = std::getenv("POD_NAME");
516 string metric = "E2TBeta";
517 if (strstr(podName, "alpha") != NULL) {
520 //Get eth0 interface IP
521 char* host = getinterfaceip();
522 string hostip = host;
524 sctpParams.prometheusFamily = &BuildCounter()
525 .Name(metric.c_str())
526 .Help("E2T instance metrics")
527 .Labels({{"POD_NAME", sctpParams.podName}})
528 .Register(*sctpParams.prometheusRegistry);
530 // Build E2T instance level metrics
531 buildE2TPrometheusCounters(sctpParams);
533 string prometheusPath;
535 prometheusPath = sctpParams.prometheusPort + "," + "[::]:" + sctpParams.prometheusPort;
537 prometheusPath = hostip + ":" + sctpParams.prometheusPort;
539 if (mdclog_level_get() >= MDCLOG_DEBUG) {
540 mdclog_write(MDCLOG_DEBUG, "Start Prometheus Pull mode on %s", prometheusPath.c_str());
542 sctpParams.prometheusExposer = new Exposer(prometheusPath, 1);
543 sctpParams.prometheusExposer->RegisterCollectable(sctpParams.prometheusRegistry);
547 int main(const int argc, char **argv) {
548 sctp_params_t sctpParams;
550 std::random_device device{};
551 std::mt19937 generator(device());
552 std::uniform_int_distribution<long> distribution(1, (long) 1e12);
553 transactionCounter = distribution(generator);
557 // uint32_t aux1 = 0;
558 // st = rdtscp(aux1);
560 unsigned num_cpus = std::thread::hardware_concurrency();
562 if (std::signal(SIGINT, catch_function) == SIG_ERR) {
563 mdclog_write(MDCLOG_ERR, "Error initializing SIGINT");
566 if (std::signal(SIGABRT, catch_function)== SIG_ERR) {
567 mdclog_write(MDCLOG_ERR, "Error initializing SIGABRT");
570 if (std::signal(SIGTERM, catch_function)== SIG_ERR) {
571 mdclog_write(MDCLOG_ERR, "Error initializing SIGTERM");
575 cpuClock = approx_CPU_MHz(100);
577 mdclog_write(MDCLOG_DEBUG, "CPU speed %11.11f", cpuClock);
579 auto result = parse(argc, argv, sctpParams);
581 if (buildConfiguration(sctpParams) != 0) {
585 //auto registry = std::make_shared<Registry>();
586 sctpParams.prometheusRegistry = std::make_shared<Registry>();
588 //sctpParams.prometheusFamily = new Family<Counter>("E2T", "E2T message counter", {{"E", sctpParams.podName}});
590 startPrometheus(sctpParams);
593 sctpParams.epoll_fd = epoll_create1(0);
594 if (sctpParams.epoll_fd == -1) {
595 mdclog_write(MDCLOG_ERR, "failed to open epoll descriptor");
598 getRmrContext(sctpParams);
599 if (sctpParams.rmrCtx == nullptr) {
600 close(sctpParams.epoll_fd);
604 if (buildInotify(sctpParams) == -1) {
605 close(sctpParams.rmrListenFd);
606 rmr_close(sctpParams.rmrCtx);
607 close(sctpParams.epoll_fd);
611 if (buildListeningPort(sctpParams) != 0) {
612 close(sctpParams.rmrListenFd);
613 rmr_close(sctpParams.rmrCtx);
614 close(sctpParams.epoll_fd);
618 sctpParams.sctpMap = new mapWrapper();
620 std::vector<std::thread> threads(num_cpus);
621 // std::vector<std::thread> threads;
624 for (unsigned int i = 0; i < num_cpus; i++) {
625 threads[i] = std::thread(listener, &sctpParams);
630 int rc = pthread_setaffinity_np(threads[i].native_handle(), sizeof(cpu_set_t), &cpuset);
632 mdclog_write(MDCLOG_ERR, "Error calling pthread_setaffinity_np: %d", rc);
637 //loop over term_init until first message from xApp
638 handleTermInit(sctpParams);
640 for (auto &t : threads) {
647 void handleTermInit(sctp_params_t &sctpParams) {
648 sendTermInit(sctpParams);
649 //send to e2 manager init of e2 term
654 auto xappMessages = num_of_XAPP_messages.load(std::memory_order_acquire);
655 if (xappMessages > 0) {
656 if (mdclog_level_get() >= MDCLOG_INFO) {
657 mdclog_write(MDCLOG_INFO, "Got a message from some application, stop sending E2_TERM_INIT");
663 if (count % 1000 == 0) {
664 mdclog_write(MDCLOG_ERR, "GOT No messages from any xApp");
665 sendTermInit(sctpParams);
670 void sendTermInit(sctp_params_t &sctpParams) {
671 rmr_mbuf_t *msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.ka_message_length);
674 msg->mtype = E2_TERM_INIT;
676 rmr_bytes2payload(msg, (unsigned char *)sctpParams.ka_message, sctpParams.ka_message_length);
677 static unsigned char tx[32];
678 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
679 rmr_bytes2xact(msg, tx, txLen);
680 msg = rmr_send_msg(sctpParams.rmrCtx, msg);
681 if (msg == nullptr) {
682 msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.ka_message_length);
683 } else if (msg->state == 0) {
685 if (mdclog_level_get() >= MDCLOG_INFO) {
686 mdclog_write(MDCLOG_INFO, "E2_TERM_INIT successfully sent ");
690 if (count % 100 == 0) {
691 mdclog_write(MDCLOG_ERR, "Error sending E2_TERM_INIT cause : %s ", translateRmrErrorMessages(msg->state).c_str());
706 cxxopts::ParseResult parse(int argc, char *argv[], sctp_params_t &sctpParams) {
707 cxxopts::Options options(argv[0], "e2 term help");
708 options.positional_help("[optional args]").show_positional_help();
709 options.allow_unrecognised_options().add_options()
710 ("p,path", "config file path", cxxopts::value<std::string>(sctpParams.configFilePath)->default_value("config"))
711 ("f,file", "config file name", cxxopts::value<std::string>(sctpParams.configFileName)->default_value("config.conf"))
712 ("h,help", "Print help");
714 auto result = options.parse(argc, (const char **&)argv);
716 if (result.count("help")) {
717 std::cout << options.help({""}) << std::endl;
726 * @return -1 failed 0 success
728 int buildInotify(sctp_params_t &sctpParams) {
729 sctpParams.inotifyFD = inotify_init1(IN_NONBLOCK);
730 if (sctpParams.inotifyFD == -1) {
731 mdclog_write(MDCLOG_ERR, "Failed to init inotify (inotify_init1) %s", strerror(errno));
735 sctpParams.inotifyWD = inotify_add_watch(sctpParams.inotifyFD,
736 (const char *)sctpParams.configFilePath.c_str(),
737 (unsigned)IN_OPEN | (unsigned)IN_CLOSE_WRITE | (unsigned)IN_CLOSE_NOWRITE); //IN_CLOSE = (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)
738 if (sctpParams.inotifyWD == -1) {
739 mdclog_write(MDCLOG_ERR, "Failed to add directory : %s to inotify (inotify_add_watch) %s",
740 sctpParams.configFilePath.c_str(),
742 close(sctpParams.inotifyFD);
746 struct epoll_event event{};
747 event.events = (EPOLLIN);
748 event.data.fd = sctpParams.inotifyFD;
749 // add listening RMR FD to epoll
750 if (epoll_ctl(sctpParams.epoll_fd, EPOLL_CTL_ADD, sctpParams.inotifyFD, &event)) {
751 mdclog_write(MDCLOG_ERR, "Failed to add inotify FD to epoll");
752 close(sctpParams.inotifyFD);
763 void listener(sctp_params_t *params) {
764 int num_of_SCTP_messages = 0;
765 auto totalTime = 0.0;
766 std::thread::id this_id = std::this_thread::get_id();
768 auto pod_name = std::getenv("POD_NAME");
769 auto container_name = std::getenv("CONTAINER_NAME");
770 auto service_name = std::getenv("SERVICE_NAME");
771 auto host_name = std::getenv("HOST_NAME");
772 auto system_name = std::getenv("SYSTEM_NAME");
773 auto pid = std::to_string(getpid()).c_str();
774 streambuf *oldCout = cout.rdbuf();
775 ostringstream memCout;
777 cout.rdbuf(memCout.rdbuf());
779 //return to the normal cout
783 memcpy(tid, memCout.str().c_str(), memCout.str().length() < 32 ? memCout.str().length() : 31);
784 tid[memCout.str().length()] = 0;
785 mdclog_mdc_add("SYSTEM_NAME", system_name);
786 mdclog_mdc_add("HOST_NAME", host_name);
787 mdclog_mdc_add("SERVICE_NAME", service_name);
788 mdclog_mdc_add("CONTAINER_NAME", container_name);
789 mdclog_mdc_add("POD_NAME", pod_name);
790 mdclog_mdc_add("PID", pid);
792 if (mdclog_level_get() >= MDCLOG_DEBUG) {
793 mdclog_write(MDCLOG_DEBUG, "started thread number %s", tid);
796 RmrMessagesBuffer_t rmrMessageBuffer{};
797 //create and init RMR
798 rmrMessageBuffer.rmrCtx = params->rmrCtx;
800 auto *events = (struct epoll_event *) calloc(MAXEVENTS, sizeof(struct epoll_event));
801 struct timespec end{0, 0};
802 struct timespec start{0, 0};
804 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
805 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
807 memcpy(rmrMessageBuffer.ka_message, params->ka_message, params->ka_message_length);
808 rmrMessageBuffer.ka_message_len = params->ka_message_length;
809 rmrMessageBuffer.ka_message[rmrMessageBuffer.ka_message_len] = 0;
811 if (mdclog_level_get() >= MDCLOG_DEBUG) {
812 mdclog_write(MDCLOG_DEBUG, "keep alive message is : %s", rmrMessageBuffer.ka_message);
815 ReportingMessages_t message {};
817 // for (int i = 0; i < MAX_RMR_BUFF_ARRAY; i++) {
818 // rmrMessageBuffer.rcvBufferedMessages[i] = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
819 // rmrMessageBuffer.sendBufferedMessages[i] = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
823 if (mdclog_level_get() >= MDCLOG_DEBUG) {
824 mdclog_write(MDCLOG_DEBUG, "Start EPOLL Wait. Timeout = %d", params->epollTimeOut);
827 auto numOfEvents = epoll_wait(params->epoll_fd, events, MAXEVENTS, params->epollTimeOut);
829 auto numOfEvents = 1;
831 if (numOfEvents == 0) { // time out
832 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
833 if (mdclog_level_get() >= MDCLOG_DEBUG) {
834 mdclog_write(MDCLOG_DEBUG, "got epoll timeout");
837 } else if (numOfEvents < 0) {
838 if (errno == EINTR) {
839 if (mdclog_level_get() >= MDCLOG_DEBUG) {
840 mdclog_write(MDCLOG_DEBUG, "got EINTR : %s", strerror(errno));
844 mdclog_write(MDCLOG_ERR, "Epoll wait failed, errno = %s", strerror(errno));
852 for (auto i = 0; i < numOfEvents; i++) {
853 if (mdclog_level_get() >= MDCLOG_DEBUG) {
854 mdclog_write(MDCLOG_DEBUG, "handling epoll event %d out of %d", i + 1, numOfEvents);
856 clock_gettime(CLOCK_MONOTONIC, &message.message.time);
857 start.tv_sec = message.message.time.tv_sec;
858 start.tv_nsec = message.message.time.tv_nsec;
861 if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP)) {
862 handlepoll_error(events[i], message, rmrMessageBuffer, params);
863 } else if (events[i].events & EPOLLOUT) {
864 handleEinprogressMessages(events[i], message, rmrMessageBuffer, params);
865 } else if (params->listenFD == events[i].data.fd) {
866 if (mdclog_level_get() >= MDCLOG_INFO) {
867 mdclog_write(MDCLOG_INFO, "New connection request from sctp network\n");
869 // new connection is requested from RAN start build connection
871 struct sockaddr in_addr {};
873 char hostBuff[NI_MAXHOST];
874 char portBuff[NI_MAXSERV];
876 in_len = sizeof(in_addr);
877 auto *peerInfo = (ConnectedCU_t *)calloc(1, sizeof(ConnectedCU_t));
878 if(peerInfo == nullptr){
879 mdclog_write(MDCLOG_ERR, "calloc failed");
882 peerInfo->sctpParams = params;
883 peerInfo->fileDescriptor = accept(params->listenFD, &in_addr, &in_len);
884 if (peerInfo->fileDescriptor == -1) {
885 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
886 if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
887 /* We have processed all incoming connections. */
894 mdclog_write(MDCLOG_ERR, "Accept error, errno = %s", strerror(errno));
898 if (setSocketNoBlocking(peerInfo->fileDescriptor) == -1) {
899 mdclog_write(MDCLOG_ERR, "setSocketNoBlocking failed to set new connection %s on port %s\n", hostBuff, portBuff);
900 close(peerInfo->fileDescriptor);
906 struct sctp_event_subscribe sctpevents;
907 memset( (void *)&sctpevents, 0, sizeof(sctpevents) );
908 sctpevents.sctp_data_io_event = 1;
909 setsockopt(peerInfo->fileDescriptor, SOL_SCTP, SCTP_EVENTS,(const void *)&sctpevents, sizeof(sctpevents) );
911 auto ans = getnameinfo(&in_addr, in_len,
912 peerInfo->hostName, NI_MAXHOST,
913 peerInfo->portNumber, NI_MAXSERV, (unsigned )((unsigned int)NI_NUMERICHOST | (unsigned int)NI_NUMERICSERV));
915 mdclog_write(MDCLOG_ERR, "Failed to get info on connection request. %s\n", strerror(errno));
916 close(peerInfo->fileDescriptor);
921 if (mdclog_level_get() >= MDCLOG_DEBUG) {
922 mdclog_write(MDCLOG_DEBUG, "Accepted connection on descriptor %d (host=%s, port=%s)\n", peerInfo->fileDescriptor, peerInfo->hostName, peerInfo->portNumber);
924 peerInfo->isConnected = false;
925 peerInfo->gotSetup = false;
926 if (addToEpoll(params->epoll_fd,
929 params->sctpMap, nullptr,
937 } else if (params->rmrListenFd == events[i].data.fd) {
938 // got message from XAPP
939 //num_of_XAPP_messages.fetch_add(1, std::memory_order_release);
940 num_of_messages.fetch_add(1, std::memory_order_release);
941 if (mdclog_level_get() >= MDCLOG_DEBUG) {
942 mdclog_write(MDCLOG_DEBUG, "new RMR message");
944 if (receiveXappMessages(params->sctpMap,
946 message.message.time) != 0) {
947 mdclog_write(MDCLOG_ERR, "Error handling Xapp message");
949 } else if (params->inotifyFD == events[i].data.fd) {
950 mdclog_write(MDCLOG_INFO, "Got event from inotify (configuration update)");
951 handleConfigChange(params);
953 /* We RMR_ERR_RETRY have data on the fd waiting to be read. Read and display it.
954 * We must read whatever data is available completely, as we are running
955 * in edge-triggered mode and won't get a notification again for the same data. */
956 num_of_messages.fetch_add(1, std::memory_order_release);
957 if (mdclog_level_get() >= MDCLOG_DEBUG) {
958 mdclog_write(MDCLOG_DEBUG, "new message from SCTP, epoll flags are : %0x", events[i].events);
960 receiveDataFromSctp(&events[i],
962 num_of_SCTP_messages,
964 message.message.time);
967 clock_gettime(CLOCK_MONOTONIC, &end);
968 if (mdclog_level_get() >= MDCLOG_INFO) {
969 totalTime += ((end.tv_sec + 1.0e-9 * end.tv_nsec) -
970 ((double) start.tv_sec + 1.0e-9 * start.tv_nsec));
972 if (mdclog_level_get() >= MDCLOG_DEBUG) {
973 mdclog_write(MDCLOG_DEBUG, "message handling is %ld seconds %ld nanoseconds",
974 end.tv_sec - start.tv_sec,
975 end.tv_nsec - start.tv_nsec);
988 void handleConfigChange(sctp_params_t *sctpParams) {
989 char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event))));
990 const struct inotify_event *event;
993 struct inotify_event tmpEvent;
995 path p = (sctpParams->configFilePath + "/" + sctpParams->configFileName).c_str();
996 auto endlessLoop = true;
997 while (endlessLoop) {
998 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
999 auto len = read(sctpParams->inotifyFD, buf, sizeof buf);
1004 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
1005 if (errno != EAGAIN) {
1006 mdclog_write(MDCLOG_ERR, "read %s ", strerror(errno));
1007 endlessLoop = false;
1011 endlessLoop = false;
1017 for (ptr = buf; ptr < buf + len; ptr += sizeof(struct inotify_event) + event->len) {
1019 event = (const struct inotify_event *)ptr;
1021 tmpEvent.mask = (uint32_t)IN_CLOSE_WRITE;
1024 if (event->mask & (uint32_t)IN_ISDIR) {
1028 // the directory name
1029 if (sctpParams->inotifyWD == event->wd) {
1030 // not the directory
1033 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
1034 auto retVal = strcmp(sctpParams->configFileName.c_str(), event->name);
1040 // only the file we want
1041 if (event->mask & (uint32_t)IN_CLOSE_WRITE) {
1042 if (mdclog_level_get() >= MDCLOG_INFO) {
1043 mdclog_write(MDCLOG_INFO, "Configuration file changed");
1046 const int size = 2048;
1047 auto fileSize = file_size(p);
1048 if (fileSize > size) {
1049 mdclog_write(MDCLOG_ERR, "File %s larger than %d", p.string().c_str(), size);
1053 mdclog_write(MDCLOG_ERR, "Configuration File %s not exists", p.string().c_str());
1057 ReadConfigFile conf;
1058 if (conf.openConfigFile(p.string()) == -1) {
1059 mdclog_write(MDCLOG_ERR, "Filed to open config file %s, %s",
1060 p.string().c_str(), strerror(errno));
1063 auto tmpStr = conf.getStringValue("loglevel");
1064 if (tmpStr.length() == 0) {
1065 mdclog_write(MDCLOG_ERR, "illegal loglevel. Set loglevel to MDCLOG_INFO");
1068 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
1070 if ((tmpStr.compare("debug")) == 0) {
1071 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_DEBUG");
1072 sctpParams->logLevel = MDCLOG_DEBUG;
1074 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
1075 else if ((tmpStr.compare("info")) == 0) {
1076 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_INFO");
1077 sctpParams->logLevel = MDCLOG_INFO;
1078 } else if ((tmpStr.compare("warning")) == 0) {
1079 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_WARN");
1080 sctpParams->logLevel = MDCLOG_WARN;
1081 } else if ((tmpStr.compare("error")) == 0) {
1082 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_ERR");
1083 sctpParams->logLevel = MDCLOG_ERR;
1085 mdclog_write(MDCLOG_ERR, "illegal loglevel = %s. Set loglevel to MDCLOG_INFO", tmpStr.c_str());
1086 sctpParams->logLevel = MDCLOG_INFO;
1089 mdclog_level_set(sctpParams->logLevel);
1090 tmpStr = conf.getStringValue("trace");
1091 if (tmpStr.length() == 0) {
1092 mdclog_write(MDCLOG_ERR, "illegal trace. Set trace to stop");
1096 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
1097 if ((tmpStr.compare("start")) == 0) {
1098 mdclog_write(MDCLOG_INFO, "Trace set to: start");
1099 sctpParams->trace = true;
1100 } else if ((tmpStr.compare("stop")) == 0) {
1101 mdclog_write(MDCLOG_INFO, "Trace set to: stop");
1102 sctpParams->trace = false;
1104 mdclog_write(MDCLOG_ERR, "Trace was set to wrong value %s, set to stop", tmpStr.c_str());
1105 sctpParams->trace = false;
1107 jsonTrace = sctpParams->trace;
1110 endlessLoop = false;
1123 * @param rmrMessageBuffer
1126 void handleEinprogressMessages(struct epoll_event &event,
1127 ReportingMessages_t &message,
1128 RmrMessagesBuffer_t &rmrMessageBuffer,
1129 sctp_params_t *params) {
1130 auto *peerInfo = (ConnectedCU_t *)event.data.ptr;
1131 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
1133 mdclog_write(MDCLOG_INFO, "file descriptor %d got EPOLLOUT", peerInfo->fileDescriptor);
1135 socklen_t retValLen = 0;
1136 auto rc = getsockopt(peerInfo->fileDescriptor, SOL_SOCKET, SO_ERROR, &retVal, &retValLen);
1137 if (rc != 0 || retVal != 0) {
1138 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
1140 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
1141 "%s|Failed SCTP Connection, after EINPROGRESS the getsockopt%s",
1142 peerInfo->enodbName, strerror(errno));
1143 } else if (retVal != 0) {
1144 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
1145 "%s|Failed SCTP Connection after EINPROGRESS, SO_ERROR",
1146 peerInfo->enodbName);
1149 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1150 message.message.asnLength = rmrMessageBuffer.sendMessage->len;
1151 mdclog_write(MDCLOG_ERR, "%s", rmrMessageBuffer.sendMessage->payload);
1152 message.message.direction = 'N';
1153 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
1154 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
1157 memset(peerInfo->asnData, 0, peerInfo->asnLength);
1158 peerInfo->asnLength = 0;
1159 peerInfo->mtype = 0;
1162 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
1163 peerInfo->isConnected = true;
1165 if (modifyToEpoll(params->epoll_fd, peerInfo, (EPOLLIN | EPOLLET), params->sctpMap, peerInfo->enodbName,
1166 peerInfo->mtype) != 0) {
1167 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_MOD");
1171 message.message.asndata = (unsigned char *)peerInfo->asnData;
1172 message.message.asnLength = peerInfo->asnLength;
1173 message.message.messageType = peerInfo->mtype;
1174 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
1175 num_of_messages.fetch_add(1, std::memory_order_release);
1176 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1177 mdclog_write(MDCLOG_DEBUG, "send the delayed SETUP/ENDC SETUP to sctp for %s",
1178 message.message.enodbName);
1180 if (sendSctpMsg(peerInfo, message, params->sctpMap) != 0) {
1181 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1182 mdclog_write(MDCLOG_DEBUG, "Error write to SCTP %s %d", __func__, __LINE__);
1187 memset(peerInfo->asnData, 0, peerInfo->asnLength);
1188 peerInfo->asnLength = 0;
1189 peerInfo->mtype = 0;
1194 void handlepoll_error(struct epoll_event &event,
1195 ReportingMessages_t &message,
1196 RmrMessagesBuffer_t &rmrMessageBuffer,
1197 sctp_params_t *params) {
1198 if (event.data.fd != params->rmrListenFd) {
1199 auto *peerInfo = (ConnectedCU_t *)event.data.ptr;
1200 mdclog_write(MDCLOG_ERR, "epoll error, events %0x on fd %d, RAN NAME : %s",
1201 event.events, peerInfo->fileDescriptor, peerInfo->enodbName);
1202 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
1203 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
1204 "%s|Failed SCTP Connection",
1205 peerInfo->enodbName);
1206 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1207 message.message.asnLength = rmrMessageBuffer.sendMessage->len;
1209 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
1210 message.message.direction = 'N';
1211 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
1212 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
1215 close(peerInfo->fileDescriptor);
1216 params->sctpMap->erase(peerInfo->enodbName);
1217 cleanHashEntry((ConnectedCU_t *) event.data.ptr, params->sctpMap);
1219 mdclog_write(MDCLOG_ERR, "epoll error, events %0x on RMR FD", event.events);
1227 int setSocketNoBlocking(int socket) {
1228 auto flags = fcntl(socket, F_GETFL, 0);
1231 mdclog_write(MDCLOG_ERR, "%s, %s", __FUNCTION__, strerror(errno));
1235 flags = (unsigned) flags | (unsigned) O_NONBLOCK;
1236 if (fcntl(socket, F_SETFL, flags) == -1) {
1237 mdclog_write(MDCLOG_ERR, "%s, %s", __FUNCTION__, strerror(errno));
1249 void cleanHashEntry(ConnectedCU_t *val, Sctp_Map_t *m) {
1251 auto port = (uint16_t) strtol(val->portNumber, &dummy, 10);
1252 char searchBuff[2048]{};
1254 snprintf(searchBuff, sizeof searchBuff, "host:%s:%d", val->hostName, port);
1255 m->erase(searchBuff);
1257 m->erase(val->enodbName);
1265 * @param fd file descriptor
1266 * @param data the asn data to send
1267 * @param len length of the data
1268 * @param enodbName the enodbName as in the map for printing purpose
1269 * @param m map host information
1270 * @param mtype message number
1271 * @return 0 success, a negative number on fail
1273 int sendSctpMsg(ConnectedCU_t *peerInfo, ReportingMessages_t &message, Sctp_Map_t *m) {
1274 auto loglevel = mdclog_level_get();
1276 int fd = peerInfo->fileDescriptor;
1277 int streamId = fetchStreamId(peerInfo,message);
1279 int fd = FILE_DESCRIPTOR;
1282 if (loglevel >= MDCLOG_DEBUG) {
1283 mdclog_write(MDCLOG_DEBUG, "Send SCTP message for CU %s, %s",
1284 message.message.enodbName, __FUNCTION__);
1288 if (sctp_sendmsg(fd,message.message.asndata, message.message.asnLength,(struct sockaddr *) NULL, 0, 0, 0,streamId,0,0) < 0) {
1289 if (errno == EINTR) {
1292 mdclog_write(MDCLOG_ERR, "error writing to CU a message, %s ", strerror(errno));
1293 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
1294 if (!peerInfo->isConnected) {
1295 mdclog_write(MDCLOG_ERR, "connection to CU %s is still in progress.", message.message.enodbName);
1300 cleanHashEntry(peerInfo, m);
1303 char key[MAX_ENODB_NAME_SIZE * 2];
1304 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", message.message.enodbName,
1305 message.message.messageType);
1306 if (loglevel >= MDCLOG_DEBUG) {
1307 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
1309 auto tmp = m->find(key);
1318 message.message.direction = 'D';
1319 // send report.buffer of size
1320 buildJsonMessage(message);
1322 if (loglevel >= MDCLOG_DEBUG) {
1323 mdclog_write(MDCLOG_DEBUG,
1324 "SCTP message for CU %s sent from %s",
1325 message.message.enodbName,
1335 * @param rmrMessageBuffer
1337 void getRequestMetaData(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
1338 message.message.asndata = rmrMessageBuffer.rcvMessage->payload;
1339 message.message.asnLength = rmrMessageBuffer.rcvMessage->len;
1341 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1342 mdclog_write(MDCLOG_DEBUG, "Message from Xapp RAN name = %s message length = %ld",
1343 message.message.enodbName, (unsigned long) message.message.asnLength);
1353 * @param numOfMessages
1354 * @param rmrMessageBuffer
1358 int receiveDataFromSctp(struct epoll_event *events,
1359 Sctp_Map_t *sctpMap,
1361 RmrMessagesBuffer_t &rmrMessageBuffer,
1362 struct timespec &ts) {
1363 /* We have data on the fd waiting to be read. Read and display it.
1364 * We must read whatever data is available completely, as we are running
1365 * in edge-triggered mode and won't get a notification again for the same data. */
1366 ReportingMessages_t message {};
1368 auto loglevel = mdclog_level_get();
1369 struct sctp_sndrcvinfo sndrcvinfo;
1373 // get the identity of the interface
1374 message.peerInfo = (ConnectedCU_t *)events->data.ptr;
1376 struct timespec start{0, 0};
1377 struct timespec decodeStart{0, 0};
1378 struct timespec end{0, 0};
1380 E2AP_PDU_t *pdu = nullptr;
1383 if (loglevel >= MDCLOG_DEBUG) {
1384 mdclog_write(MDCLOG_DEBUG, "Start Read from SCTP %d fd", message.peerInfo->fileDescriptor);
1385 clock_gettime(CLOCK_MONOTONIC, &start);
1387 // read the buffer directly to rmr payload
1388 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1390 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
1391 sctp_recvmsg(message.peerInfo->fileDescriptor, rmrMessageBuffer.sendMessage->payload, RECEIVE_SCTP_BUFFER_SIZE,(struct sockaddr *) NULL, 0, &sndrcvinfo, &flags);
1392 mdclog_write(MDCLOG_DEBUG, "Start Read from SCTP fd %d stream %d ", message.peerInfo->fileDescriptor, sndrcvinfo.sinfo_stream);
1393 streamId = sndrcvinfo.sinfo_stream;
1395 message.message.asnLength = rmrMessageBuffer.sendMessage->len;
1399 if (loglevel >= MDCLOG_DEBUG) {
1400 mdclog_write(MDCLOG_DEBUG, "Finish Read from SCTP %d fd message length = %ld",
1401 message.peerInfo->fileDescriptor, message.message.asnLength);
1404 memcpy(message.message.enodbName, message.peerInfo->enodbName, sizeof(message.peerInfo->enodbName));
1405 message.message.direction = 'U';
1406 message.message.time.tv_nsec = ts.tv_nsec;
1407 message.message.time.tv_sec = ts.tv_sec;
1409 if (message.message.asnLength < 0) {
1410 if (errno == EINTR) {
1413 /* If errno == EAGAIN, that means we have read all
1414 data. So goReportingMessages_t back to the main loop. */
1415 if (errno != EAGAIN) {
1416 mdclog_write(MDCLOG_ERR, "Read error, %s ", strerror(errno));
1418 } else if (loglevel >= MDCLOG_DEBUG) {
1419 mdclog_write(MDCLOG_DEBUG, "EAGAIN - descriptor = %d", message.peerInfo->fileDescriptor);
1422 } else if (message.message.asnLength == 0) {
1423 /* End of file. The remote has closed the connection. */
1424 if (loglevel >= MDCLOG_INFO) {
1425 mdclog_write(MDCLOG_INFO, "END of File Closed connection - descriptor = %d",
1426 message.peerInfo->fileDescriptor);
1432 if (loglevel >= MDCLOG_DEBUG) {
1433 char printBuffer[RECEIVE_SCTP_BUFFER_SIZE]{};
1434 char *tmp = printBuffer;
1435 for (size_t i = 0; i < (size_t)message.message.asnLength; ++i) {
1436 snprintf(tmp, 3, "%02x", message.message.asndata[i]);
1439 printBuffer[message.message.asnLength] = 0;
1440 clock_gettime(CLOCK_MONOTONIC, &end);
1441 mdclog_write(MDCLOG_DEBUG, "Before Encoding E2AP PDU for : %s, Read time is : %ld seconds, %ld nanoseconds",
1442 message.peerInfo->enodbName, end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec);
1443 mdclog_write(MDCLOG_DEBUG, "PDU buffer length = %ld, data = : %s", message.message.asnLength,
1445 clock_gettime(CLOCK_MONOTONIC, &decodeStart);
1448 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, (void **) &pdu,
1449 message.message.asndata, message.message.asnLength);
1451 asn_dec_rval_t rval = {RC_OK, 0};
1452 pdu = (E2AP_PDU_t*)rmrMessageBuffer.sendMessage->tp_buf;
1454 if (rval.code != RC_OK) {
1455 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2AP PDU from RAN : %s", rval.code,
1456 message.peerInfo->enodbName);
1457 if (pdu != nullptr) {
1458 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
1464 if (loglevel >= MDCLOG_DEBUG) {
1465 clock_gettime(CLOCK_MONOTONIC, &end);
1466 mdclog_write(MDCLOG_DEBUG, "After Encoding E2AP PDU for : %s, Read time is : %ld seconds, %ld nanoseconds",
1467 message.peerInfo->enodbName, end.tv_sec - decodeStart.tv_sec, end.tv_nsec - decodeStart.tv_nsec);
1470 FILE *stream = open_memstream(&printBuffer, &size);
1471 asn_fprint(stream, &asn_DEF_E2AP_PDU, pdu);
1472 mdclog_write(MDCLOG_DEBUG, "Encoding E2AP PDU past : %s", printBuffer);
1473 clock_gettime(CLOCK_MONOTONIC, &decodeStart);
1479 switch (pdu->present) {
1480 case E2AP_PDU_PR_initiatingMessage: {//initiating message
1481 asnInitiatingRequest(pdu, sctpMap,message, rmrMessageBuffer, streamId);
1484 case E2AP_PDU_PR_successfulOutcome: { //successful outcome
1485 asnSuccessfulMsg(pdu, sctpMap, message, rmrMessageBuffer);
1488 case E2AP_PDU_PR_unsuccessfulOutcome: { //Unsuccessful Outcome
1489 asnUnSuccsesfulMsg(pdu, sctpMap, message, rmrMessageBuffer);
1493 mdclog_write(MDCLOG_ERR, "Unknown index %d in E2AP PDU", pdu->present);
1496 if (loglevel >= MDCLOG_DEBUG) {
1497 clock_gettime(CLOCK_MONOTONIC, &end);
1498 mdclog_write(MDCLOG_DEBUG,
1499 "After processing message and sent to rmr for : %s, Read time is : %ld seconds, %ld nanoseconds",
1500 message.peerInfo->enodbName, end.tv_sec - decodeStart.tv_sec, end.tv_nsec - decodeStart.tv_nsec);
1504 if (pdu != nullptr) {
1505 // ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, pdu); /* With reset we were not freeing the memory and was causing the leak here. */
1506 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
1516 if (loglevel >= MDCLOG_INFO) {
1517 mdclog_write(MDCLOG_INFO, "Closed connection - descriptor = %d", message.peerInfo->fileDescriptor);
1519 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
1520 snprintf((char *)rmrMessageBuffer.sendMessage->payload,
1522 "%s|CU disconnected unexpectedly",
1523 message.peerInfo->enodbName);
1524 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1525 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
1526 if (sendRequestToXapp(message,
1527 RIC_SCTP_CONNECTION_FAILURE,
1528 rmrMessageBuffer) != 0) {
1529 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
1533 /* Closing descriptor make epoll remove it from the set of descriptors which are monitored. */
1534 close(message.peerInfo->fileDescriptor);
1535 cleanHashEntry((ConnectedCU_t *) events->data.ptr, sctpMap);
1537 if (loglevel >= MDCLOG_DEBUG) {
1538 clock_gettime(CLOCK_MONOTONIC, &end);
1539 mdclog_write(MDCLOG_DEBUG, "from receive SCTP to send RMR time is %ld seconds and %ld nanoseconds",
1540 end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec);
1546 static void buildAndSendSetupRequest(ReportingMessages_t &message,
1547 RmrMessagesBuffer_t &rmrMessageBuffer,
1549 string const &messageName,
1550 string const &ieName,
1551 vector<string> &functionsToAdd_v,
1552 vector<string> &functionsToModified_v*/) {
1553 auto logLevel = mdclog_level_get();
1554 // now we can send the data to e2Mgr
1557 auto buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
1558 unsigned char *buffer = nullptr;
1559 buffer = (unsigned char *) calloc(buffer_size, sizeof(unsigned char));
1562 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
1563 mdclog_write(MDCLOG_ERR, "Allocating buffer for %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1568 er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu, buffer, buffer_size);
1569 if (er.encoded == -1) {
1570 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
1571 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1574 } else if (er.encoded > (ssize_t) buffer_size) {
1575 buffer_size = er.encoded + 128;
1576 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
1577 mdclog_write(MDCLOG_WARN, "Buffer of size %d is to small for %s. Reallocate buffer of size %d",
1579 asn_DEF_E2AP_PDU.name, buffer_size);
1580 buffer_size = er.encoded + 128;
1582 unsigned char *newBuffer = nullptr;
1583 newBuffer = (unsigned char *) realloc(buffer, buffer_size);
1587 mdclog_write(MDCLOG_ERR, "Reallocating buffer for %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1595 buffer[er.encoded] = '\0';
1600 string res((char *)buffer);
1601 res.erase(std::remove(res.begin(), res.end(), '\n'), res.end());
1602 res.erase(std::remove(res.begin(), res.end(), '\t'), res.end());
1603 res.erase(std::remove(res.begin(), res.end(), ' '), res.end());
1606 // if (!functionsToAdd_v.empty() || !functionsToModified_v.empty()) {
1607 // res = buildXmlData(messageName, ieName, functionsToAdd_v, functionsToModified_v, buffer, (size_t) er.encoded);
1610 // if (res.length() == 0) {
1611 // rmrMsg = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, buffer_size + 256);
1612 // rmrMsg->len = snprintf((char *) rmrMsg->payload, RECEIVE_SCTP_BUFFER_SIZE * 2, "%s:%d|%s",
1613 // message.peerInfo->sctpParams->myIP.c_str(),
1614 // message.peerInfo->sctpParams->rmrPort,
1617 rmrMsg = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, (int)res.length() + 256);
1618 rmrMsg->len = snprintf((char *) rmrMsg->payload, res.length() + 256, "%s:%d|%s",
1619 message.peerInfo->sctpParams->myIP.c_str(),
1620 message.peerInfo->sctpParams->rmrPort,
1624 if (logLevel >= MDCLOG_DEBUG) {
1625 mdclog_write(MDCLOG_DEBUG, "Setup request of size %d :\n %s\n", rmrMsg->len, rmrMsg->payload);
1628 rmrMsg->mtype = message.message.messageType;
1630 rmr_bytes2meid(rmrMsg, (unsigned char *) message.message.enodbName, strlen(message.message.enodbName));
1632 static unsigned char tx[32];
1633 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1634 rmr_bytes2xact(rmrMsg, tx, strlen((const char *) tx));
1636 rmrMsg = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMsg);
1638 if (rmrMsg == nullptr) {
1639 mdclog_write(MDCLOG_ERR, "RMR failed to send returned nullptr");
1640 } else if (rmrMsg->state != 0) {
1641 char meid[RMR_MAX_MEID]{};
1642 if (rmrMsg->state == RMR_ERR_RETRY) {
1645 mdclog_write(MDCLOG_INFO, "RETRY sending Message %d to Xapp from %s",
1646 rmrMsg->mtype, rmr_get_meid(rmrMsg, (unsigned char *) meid));
1648 rmrMsg = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMsg);
1650 if (rmrMsg == nullptr) {
1651 mdclog_write(MDCLOG_ERR, "RMR failed send returned nullptr");
1652 } else if (rmrMsg->state != 0) {
1653 mdclog_write(MDCLOG_ERR,
1654 "RMR Retry failed %s sending request %d to Xapp from %s",
1655 translateRmrErrorMessages(rmrMsg->state).c_str(),
1657 rmr_get_meid(rmrMsg, (unsigned char *) meid));
1660 mdclog_write(MDCLOG_ERR, "RMR failed: %s. sending request %d to Xapp from %s",
1661 translateRmrErrorMessages(rmrMsg->state).c_str(),
1663 rmr_get_meid(rmrMsg, (unsigned char *) meid));
1666 message.peerInfo->gotSetup = true;
1667 buildJsonMessage(message);
1669 if (rmrMsg != nullptr) {
1670 rmr_free_msg(rmrMsg);
1678 int RAN_Function_list_To_Vector(RANfunctions_List_t& list, vector <string> &runFunXML_v) {
1680 runFunXML_v.clear();
1681 for (auto j = 0; j < list.list.count; j++) {
1682 auto *raNfunctionItemIEs = (RANfunction_ItemIEs_t *)list.list.array[j];
1683 if (raNfunctionItemIEs->id == ProtocolIE_ID_id_RANfunction_Item &&
1684 (raNfunctionItemIEs->value.present == RANfunction_ItemIEs__value_PR_RANfunction_Item)) {
1686 E2SM_gNB_NRT_RANfunction_Definition_t *ranFunDef = nullptr;
1687 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER,
1688 &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition,
1689 (void **)&ranFunDef,
1690 raNfunctionItemIEs->value.choice.RANfunction_Item.ranFunctionDefinition.buf,
1691 raNfunctionItemIEs->value.choice.RANfunction_Item.ranFunctionDefinition.size);
1692 if (rval.code != RC_OK) {
1693 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2SM message from : %s",
1695 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name);
1699 auto xml_buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
1700 unsigned char xml_buffer[RECEIVE_SCTP_BUFFER_SIZE * 2];
1701 memset(xml_buffer, 0, RECEIVE_SCTP_BUFFER_SIZE * 2);
1703 auto er = asn_encode_to_buffer(nullptr,
1705 &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition,
1709 if (er.encoded == -1) {
1710 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s",
1711 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name,
1713 } else if (er.encoded > (ssize_t)xml_buffer_size) {
1714 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
1715 (int) xml_buffer_size,
1716 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name, __func__, __LINE__);
1718 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1719 mdclog_write(MDCLOG_DEBUG, "Encoding E2SM %s PDU number %d : %s",
1720 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name,
1725 string runFuncs = (char *)(xml_buffer);
1726 runFunXML_v.emplace_back(runFuncs);
1733 int collectServiceUpdate_RequestData(E2AP_PDU_t *pdu,
1734 Sctp_Map_t *sctpMap,
1735 ReportingMessages_t &message,
1736 vector <string> &RANfunctionsAdded_v,
1737 vector <string> &RANfunctionsModified_v) {
1738 memset(message.peerInfo->enodbName, 0 , MAX_ENODB_NAME_SIZE);
1739 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICserviceUpdate.protocolIEs.list.count; i++) {
1740 auto *ie = pdu->choice.initiatingMessage->value.choice.RICserviceUpdate.protocolIEs.list.array[i];
1741 if (ie->id == ProtocolIE_ID_id_RANfunctionsAdded) {
1742 if (ie->value.present == RICserviceUpdate_IEs__value_PR_RANfunctionsID_List) {
1743 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1744 mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1745 ie->value.choice.RANfunctions_List.list.count);
1747 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsAdded_v) != 0 ) {
1751 } else if (ie->id == ProtocolIE_ID_id_RANfunctionsModified) {
1752 if (ie->value.present == RICserviceUpdate_IEs__value_PR_RANfunctions_List) {
1753 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1754 mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1755 ie->value.choice.RANfunctions_List.list.count);
1757 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsModified_v) != 0 ) {
1763 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1764 mdclog_write(MDCLOG_DEBUG, "Run function vector have %ld entries",
1765 RANfunctionsAdded_v.size());
1773 void buildE2TPrometheusCounters(sctp_params_t &sctpParams) {
1774 sctpParams.e2tCounters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &sctpParams.prometheusFamily->Add({{"counter", "SetupRequestMsgs"}});
1775 sctpParams.e2tCounters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &sctpParams.prometheusFamily->Add({{"counter", "SetupRequestBytes"}});
1777 sctpParams.e2tCounters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &sctpParams.prometheusFamily->Add({{"counter", "SetupResponseMsgs"}});
1778 sctpParams.e2tCounters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &sctpParams.prometheusFamily->Add({{"counter", "SetupResponseBytes"}});
1780 sctpParams.e2tCounters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_E2setup] = &sctpParams.prometheusFamily->Add({{"counter", "SetupRequestFailureMsgs"}});
1781 sctpParams.e2tCounters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_E2setup] = &sctpParams.prometheusFamily->Add({{"counter", "SetupRequestFailureBytes"}});
1783 sctpParams.e2tCounters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_E2nodeConfigurationUpdate)] = &sctpParams.prometheusFamily->Add({{"counter", "E2NodeConfigUpdateMsgs"}});
1784 sctpParams.e2tCounters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_E2nodeConfigurationUpdate)] = &sctpParams.prometheusFamily->Add({{"counter", "E2NodeConfigUpdateBytes"}});
1786 sctpParams.e2tCounters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_E2nodeConfigurationUpdate)] = &sctpParams.prometheusFamily->Add({{"counter", "E2NodeConfigUpdateResponseMsgs"}});
1787 sctpParams.e2tCounters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2nodeConfigurationUpdate)] = &sctpParams.prometheusFamily->Add({{"counter", "E2NodeConfigUpdateResponseBytes"}});
1789 sctpParams.e2tCounters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate] = &sctpParams.prometheusFamily->Add({{"counter", "E2NodeConfigUpdateFailureMsgs"}});
1790 sctpParams.e2tCounters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate] = &sctpParams.prometheusFamily->Add({{"counter", "E2NodeConfigUpdateFailureBytes"}});
1792 sctpParams.e2tCounters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_ErrorIndication)] = &sctpParams.prometheusFamily->Add({{"counter", "ErrorIndicationMsgs"}});
1793 sctpParams.e2tCounters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_ErrorIndication)] = &sctpParams.prometheusFamily->Add({{"counter", "ErrorIndicationBytes"}});
1795 sctpParams.e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_Reset] = &sctpParams.prometheusFamily->Add({{"counter", "ResetRequestMsgs"}});
1796 sctpParams.e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_Reset] = &sctpParams.prometheusFamily->Add({{"counter", "ResetRequestBytes"}});
1798 sctpParams.e2tCounters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_Reset] = &sctpParams.prometheusFamily->Add({{"counter", "ResetAckMsgs"}});
1799 sctpParams.e2tCounters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_Reset] = &sctpParams.prometheusFamily->Add({{"counter", "ResetAckBytes"}});
1801 sctpParams.e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate] = &sctpParams.prometheusFamily->Add({{"counter", "RICServiceUpdateMsgs"}});
1802 sctpParams.e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate] = &sctpParams.prometheusFamily->Add({{"counter", "RICServiceUpdateBytes"}});
1804 sctpParams.e2tCounters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate] = &sctpParams.prometheusFamily->Add({{"counter", "RICServiceUpdateRespMsgs"}});
1805 sctpParams.e2tCounters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate] = &sctpParams.prometheusFamily->Add({{"counter", "RICServiceUpdateRespBytes"}});
1807 sctpParams.e2tCounters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate] = &sctpParams.prometheusFamily->Add({{"counter", "RICServiceUpdateFailureMsgs"}});
1808 sctpParams.e2tCounters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate] = &sctpParams.prometheusFamily->Add({{"counter", "RICServiceUpdateFailureBytes"}});
1810 sctpParams.e2tCounters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICcontrol] = &sctpParams.prometheusFamily->Add({{"counter", "RICControlMsgs"}});
1811 sctpParams.e2tCounters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICcontrol] = &sctpParams.prometheusFamily->Add({{"counter", "RICControlBytes"}});
1813 sctpParams.e2tCounters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol] = &sctpParams.prometheusFamily->Add({{"counter", "RICControlAckMsgs"}});
1814 sctpParams.e2tCounters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol] = &sctpParams.prometheusFamily->Add({{"counter", "RICControlAckBytes"}});
1816 sctpParams.e2tCounters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol] = &sctpParams.prometheusFamily->Add({{"counter", "RICControlFailureMsgs"}});
1817 sctpParams.e2tCounters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol] = &sctpParams.prometheusFamily->Add({{"counter", "RICControlFailureBytes"}});
1819 sctpParams.e2tCounters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscription] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionMsgs"}});
1820 sctpParams.e2tCounters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscription] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionBytes"}});
1822 sctpParams.e2tCounters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionAckMsgs"}});
1823 sctpParams.e2tCounters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionAckBytes"}});
1825 sctpParams.e2tCounters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionFailureMsgs"}});
1826 sctpParams.e2tCounters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionFailureBytes"}});
1828 sctpParams.e2tCounters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionDeleteMsgs"}});
1829 sctpParams.e2tCounters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionDeleteBytes"}});
1831 sctpParams.e2tCounters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionDeleteAckMsgs"}});
1832 sctpParams.e2tCounters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionDeleteAckBytes"}});
1834 sctpParams.e2tCounters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionDeleteFailMsgs"}});
1835 sctpParams.e2tCounters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionDeleteFailBytes"}});
1837 sctpParams.e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICindication] = &sctpParams.prometheusFamily->Add({{"counter", "RICIndicationMsgs"}});
1838 sctpParams.e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICindication] = &sctpParams.prometheusFamily->Add({{"counter", "RICIndicationBytes"}});
1840 sctpParams.e2tCounters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceQuery] = &sctpParams.prometheusFamily->Add({{"counter", "RICServiceQueryMsgs"}});
1841 sctpParams.e2tCounters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceQuery] = &sctpParams.prometheusFamily->Add({{"counter", "RICServiceQueryBytes"}});
1844 void buildPrometheusList(ConnectedCU_t *peerInfo, Family<Counter> *prometheusFamily) {
1845 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"SetupRequest", "Messages"}});
1846 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"SetupRequest", "Bytes"}});
1848 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_E2nodeConfigurationUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"E2NodeConfigUpdate", "Messages"}});
1849 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_E2nodeConfigurationUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"E2NodeConfigUpdate", "Bytes"}});
1851 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ErrorIndication", "Messages"}});
1852 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ErrorIndication", "Bytes"}});
1854 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_RICindication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICindication", "Messages"}});
1855 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_RICindication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICindication", "Bytes"}});
1857 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetRequest", "Messages"}});
1858 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetRequest", "Bytes"}});
1860 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICserviceUpdate", "Messages"}});
1861 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICserviceUpdate", "Bytes"}});
1862 // ---------------------------------------------
1863 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetACK", "Messages"}});
1864 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetACK", "Bytes"}});
1866 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolACK", "Messages"}});
1867 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolACK", "Bytes"}});
1869 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionACK", "Messages"}});
1870 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionACK", "Bytes"}});
1872 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteACK", "Messages"}});
1873 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteACK", "Bytes"}});
1874 //-------------------------------------------------------------
1876 peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolFailure", "Messages"}});
1877 peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolFailure", "Bytes"}});
1879 peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionFailure", "Messages"}});
1880 peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionFailure", "Bytes"}});
1882 peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteFailure", "Messages"}});
1883 peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteFailure", "Bytes"}});
1885 //====================================================================================
1886 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ErrorIndication", "Messages"}});
1887 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ErrorIndication", "Bytes"}});
1889 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetRequest", "Messages"}});
1890 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetRequest", "Bytes"}});
1892 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICcontrol", "Messages"}});
1893 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICcontrol", "Bytes"}});
1895 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICserviceQuery)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceQuery", "Messages"}});
1896 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICserviceQuery)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceQuery", "Bytes"}});
1898 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscription", "Messages"}});
1899 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscription", "Bytes"}});
1901 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscriptionDelete", "Messages"}});
1902 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscriptionDelete", "Bytes"}});
1903 //---------------------------------------------------------------------------------------------------------
1904 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupResponse", "Messages"}});
1905 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupResponse", "Bytes"}});
1907 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_E2nodeConfigurationUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"E2NodeConfigUpdateSuccess", "Messages"}});
1908 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2nodeConfigurationUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"E2NodeConfigUpdateSuccess", "Bytes"}});
1910 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetACK", "Messages"}});
1911 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetACK", "Bytes"}});
1913 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateResponse", "Messages"}});
1914 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateResponse", "Bytes"}});
1915 //----------------------------------------------------------------------------------------------------------------
1916 peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupRequestFailure", "Messages"}});
1917 peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupRequestFailure", "Bytes"}});
1919 peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_E2nodeConfigurationUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"E2NodeConfigUpdateFailure", "Messages"}});
1920 peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2nodeConfigurationUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"E2NodeConfigUpdateFailure", "Bytes"}});
1922 peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateFailure", "Messages"}});
1923 peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateFailure", "Bytes"}});
1931 * @param RANfunctionsAdded_v
1934 int collectSetupRequestData(E2AP_PDU_t *pdu,
1935 Sctp_Map_t *sctpMap,
1936 ReportingMessages_t &message /*, vector <string> &RANfunctionsAdded_v*/) {
1937 memset(message.peerInfo->enodbName, 0 , MAX_ENODB_NAME_SIZE);
1938 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.count; i++) {
1939 auto *ie = pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.array[i];
1940 if (ie->id == ProtocolIE_ID_id_GlobalE2node_ID) {
1941 // get the ran name for meid
1942 if (ie->value.present == E2setupRequestIEs__value_PR_GlobalE2node_ID) {
1943 if (buildRanName(message.peerInfo->enodbName, ie) < 0) {
1944 mdclog_write(MDCLOG_ERR, "Bad param in E2setupRequestIEs GlobalE2node_ID.\n");
1945 // no message will be sent
1949 memcpy(message.message.enodbName, message.peerInfo->enodbName, strlen(message.peerInfo->enodbName));
1950 sctpMap->setkey(message.message.enodbName, message.peerInfo);
1952 } /*else if (ie->id == ProtocolIE_ID_id_RANfunctionsAdded) {
1953 if (ie->value.present == E2setupRequestIEs__value_PR_RANfunctions_List) {
1954 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1955 mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1956 ie->value.choice.RANfunctions_List.list.count);
1958 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsAdded_v) != 0 ) {
1964 // if (mdclog_level_get() >= MDCLOG_DEBUG) {
1965 // mdclog_write(MDCLOG_DEBUG, "Run function vector have %ld entries",
1966 // RANfunctionsAdded_v.size());
1971 int XML_From_PER(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
1972 E2AP_PDU_t *pdu = nullptr;
1974 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1975 mdclog_write(MDCLOG_DEBUG, "got PER message of size %d is:%s",
1976 rmrMessageBuffer.sendMessage->len, rmrMessageBuffer.sendMessage->payload);
1978 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, (void **) &pdu,
1979 rmrMessageBuffer.sendMessage->payload, rmrMessageBuffer.sendMessage->len);
1980 if (rval.code != RC_OK) {
1981 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) setup response from E2MGR : %s",
1983 message.message.enodbName);
1984 if (pdu != nullptr) {
1985 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
1991 int buff_size = RECEIVE_XAPP_BUFFER_SIZE;
1992 auto er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu,
1993 rmrMessageBuffer.sendMessage->payload, buff_size);
1994 if (er.encoded == -1) {
1995 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1996 if (pdu != nullptr) {
1997 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
2001 } else if (er.encoded > (ssize_t)buff_size) {
2002 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
2003 (int)rmrMessageBuffer.sendMessage->len,
2004 asn_DEF_E2AP_PDU.name,
2007 if (pdu != nullptr) {
2008 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
2013 rmrMessageBuffer.sendMessage->len = er.encoded;
2014 if (pdu != nullptr) {
2015 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
2026 * @param rmrMessageBuffer
2028 void asnInitiatingRequest(E2AP_PDU_t *pdu,
2029 Sctp_Map_t *sctpMap,
2030 ReportingMessages_t &message,
2031 RmrMessagesBuffer_t &rmrMessageBuffer, int streamId) {
2032 auto logLevel = mdclog_level_get();
2033 auto procedureCode = ((InitiatingMessage_t *) pdu->choice.initiatingMessage)->procedureCode;
2034 if (logLevel >= MDCLOG_DEBUG) {
2035 mdclog_write(MDCLOG_DEBUG, "Initiating message %ld\n", procedureCode);
2037 switch (procedureCode) {
2038 case ProcedureCode_id_E2setup: {
2039 if (logLevel >= MDCLOG_DEBUG) {
2040 mdclog_write(MDCLOG_DEBUG, "Got E2setup");
2043 // vector <string> RANfunctionsAdded_v;
2044 // vector <string> RANfunctionsModified_v;
2045 // RANfunctionsAdded_v.clear();
2046 // RANfunctionsModified_v.clear();
2047 if (collectSetupRequestData(pdu, sctpMap, message) != 0) {
2050 struct sctp_status status;
2051 int stat_size = sizeof(status);
2052 getsockopt( message.peerInfo->fileDescriptor, SOL_SCTP, SCTP_STATUS,(void *)&status, (socklen_t *)&stat_size );
2053 if (logLevel >= MDCLOG_DEBUG) {
2054 mdclog_write(MDCLOG_DEBUG, "Start from SCTP %d fd", message.peerInfo->fileDescriptor);
2055 mdclog_write(MDCLOG_DEBUG, "SCTP status assoc id %d instrms %d outstrms %d", status.sstat_assoc_id,
2056 status.sstat_instrms, status.sstat_outstrms);
2058 if(status.sstat_outstrms == 1 || status.sstat_instrms == 1)
2060 message.peerInfo->isSingleStream = true;
2061 message.peerInfo->singleStreamId = streamId;
2062 if (status.sstat_outstrms == 1 && status.sstat_instrms == 1){
2063 if (logLevel >= MDCLOG_DEBUG) {
2064 mdclog_write(MDCLOG_DEBUG, "Single SCTP stream is used for sending from now on, assoc id %d streamId %d #instrms %d #outstrms %d, %s",status.sstat_assoc_id, streamId, status.sstat_instrms, status.sstat_outstrms, __FUNCTION__);
2068 mdclog_write(MDCLOG_ERR, "Single SCTP stream used for sending messages even if there is a mismatch in number of in & out streams, assoc id %d instrms %d outstrms %d", status.sstat_assoc_id,
2069 status.sstat_instrms, status.sstat_outstrms);
2073 buildPrometheusList(message.peerInfo, message.peerInfo->sctpParams->prometheusFamily);
2075 string messageName("E2setupRequest");
2076 string ieName("E2setupRequestIEs");
2077 message.message.messageType = RIC_E2_SETUP_REQ;
2078 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2079 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment((double)message.message.asnLength);
2081 // Update E2T instance level metrics
2082 message.peerInfo->sctpParams->e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2083 message.peerInfo->sctpParams->e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment((double)message.message.asnLength);
2085 buildAndSendSetupRequest(message, rmrMessageBuffer, pdu);
2088 case ProcedureCode_id_RICserviceUpdate: {
2089 if (logLevel >= MDCLOG_DEBUG) {
2090 mdclog_write(MDCLOG_DEBUG, "Got RICserviceUpdate %s", message.message.enodbName);
2092 // vector <string> RANfunctionsAdded_v;
2093 // vector <string> RANfunctionsModified_v;
2094 // RANfunctionsAdded_v.clear();
2095 // RANfunctionsModified_v.clear();
2096 // if (collectServiceUpdate_RequestData(pdu, sctpMap, message,
2097 // RANfunctionsAdded_v, RANfunctionsModified_v) != 0) {
2101 string messageName("RICserviceUpdate");
2102 string ieName("RICserviceUpdateIEs");
2103 message.message.messageType = RIC_SERVICE_UPDATE;
2104 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2105 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
2106 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment((double)message.message.asnLength);
2108 // Update E2T instance level metrics
2109 message.peerInfo->sctpParams->e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
2110 message.peerInfo->sctpParams->e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment((double)message.message.asnLength);
2112 buildAndSendSetupRequest(message, rmrMessageBuffer, pdu);
2116 case ProcedureCode_id_E2nodeConfigurationUpdate: {
2117 if (logLevel >= MDCLOG_DEBUG) {
2118 mdclog_write(MDCLOG_DEBUG, "Got E2nodeConfigurationUpdate %s", message.message.enodbName);
2121 string messageName("RICE2nodeConfigurationUpdate");
2122 string ieName("RICE2nodeConfigurationUpdateIEs");
2123 message.message.messageType = RIC_E2NODE_CONFIG_UPDATE;
2124 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2125 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment();
2126 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment((double)message.message.asnLength);
2128 // Update E2T instance level metrics
2129 message.peerInfo->sctpParams->e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment();
2130 message.peerInfo->sctpParams->e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment((double)message.message.asnLength);
2132 buildAndSendSetupRequest(message, rmrMessageBuffer, pdu);
2136 case ProcedureCode_id_ErrorIndication: {
2137 if (logLevel >= MDCLOG_DEBUG) {
2138 mdclog_write(MDCLOG_DEBUG, "Got ErrorIndication %s", message.message.enodbName);
2140 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2141 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_ErrorIndication]->Increment();
2142 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_ErrorIndication]->Increment((double)message.message.asnLength);
2144 // Update E2T instance level metrics
2145 message.peerInfo->sctpParams->e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_ErrorIndication]->Increment();
2146 message.peerInfo->sctpParams->e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_ErrorIndication]->Increment((double)message.message.asnLength);
2148 if (sendRequestToXapp(message, RIC_ERROR_INDICATION, rmrMessageBuffer) != 0) {
2149 mdclog_write(MDCLOG_ERR, "RIC_ERROR_INDICATION failed to send to xAPP");
2153 case ProcedureCode_id_Reset: {
2154 if (logLevel >= MDCLOG_DEBUG) {
2155 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
2157 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2158 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2159 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment((double)message.message.asnLength);
2161 // Update E2T instance level metrics
2162 message.peerInfo->sctpParams->e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2163 message.peerInfo->sctpParams->e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment((double)message.message.asnLength);
2165 if (XML_From_PER(message, rmrMessageBuffer) < 0) {
2169 if (sendRequestToXapp(message, RIC_E2_RESET_REQ, rmrMessageBuffer) != 0) {
2170 mdclog_write(MDCLOG_ERR, "RIC_E2_RESET_REQ message failed to send to xAPP");
2174 case ProcedureCode_id_RICindication: {
2175 if (logLevel >= MDCLOG_DEBUG) {
2176 mdclog_write(MDCLOG_DEBUG, "Got RICindication %s", message.message.enodbName);
2178 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.count; i++) {
2179 auto messageSent = false;
2180 RICindication_IEs_t *ie = pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.array[i];
2181 if (logLevel >= MDCLOG_DEBUG) {
2182 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
2184 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
2185 if (logLevel >= MDCLOG_DEBUG) {
2186 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
2188 if (ie->value.present == RICindication_IEs__value_PR_RICrequestID) {
2189 static unsigned char tx[32];
2190 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_INDICATION;
2191 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2192 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
2193 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
2194 (unsigned char *)message.message.enodbName,
2195 strlen(message.message.enodbName));
2196 rmrMessageBuffer.sendMessage->state = 0;
2197 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
2199 //ie->value.choice.RICrequestID.ricInstanceID;
2200 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2201 mdclog_write(MDCLOG_DEBUG, "sub id = %d, mtype = %d, ric instance id %ld, requestor id = %ld",
2202 rmrMessageBuffer.sendMessage->sub_id,
2203 rmrMessageBuffer.sendMessage->mtype,
2204 ie->value.choice.RICrequestID.ricInstanceID,
2205 ie->value.choice.RICrequestID.ricRequestorID);
2207 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2208 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICindication]->Increment();
2209 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICindication]->Increment((double)message.message.asnLength);
2211 // Update E2T instance level metrics
2212 message.peerInfo->sctpParams->e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICindication]->Increment();
2213 message.peerInfo->sctpParams->e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICindication]->Increment((double)message.message.asnLength);
2215 sendRmrMessage(rmrMessageBuffer, message);
2218 mdclog_write(MDCLOG_ERR, "RIC request id missing illegal request");
2228 mdclog_write(MDCLOG_ERR, "Undefined or not supported message = %ld", procedureCode);
2229 message.message.messageType = 0; // no RMR message type yet
2231 buildJsonMessage(message);
2242 * @param rmrMessageBuffer
2244 void asnSuccessfulMsg(E2AP_PDU_t *pdu,
2245 Sctp_Map_t *sctpMap,
2246 ReportingMessages_t &message,
2247 RmrMessagesBuffer_t &rmrMessageBuffer) {
2248 auto procedureCode = pdu->choice.successfulOutcome->procedureCode;
2249 auto logLevel = mdclog_level_get();
2250 if (logLevel >= MDCLOG_INFO) {
2251 mdclog_write(MDCLOG_INFO, "Successful Outcome %ld", procedureCode);
2253 switch (procedureCode) {
2254 case ProcedureCode_id_Reset: {
2255 if (logLevel >= MDCLOG_DEBUG) {
2256 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
2258 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2259 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2260 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment((double)message.message.asnLength);
2262 // Update E2T instance level metrics
2263 message.peerInfo->sctpParams->e2tCounters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2264 message.peerInfo->sctpParams->e2tCounters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment((double)message.message.asnLength);
2266 if (XML_From_PER(message, rmrMessageBuffer) < 0) {
2269 if (sendRequestToXapp(message, RIC_E2_RESET_RESP, rmrMessageBuffer) != 0) {
2270 mdclog_write(MDCLOG_ERR, "RIC_E2_RESET_RESP message failed to send to xAPP");
2274 case ProcedureCode_id_RICcontrol: {
2275 if (logLevel >= MDCLOG_DEBUG) {
2276 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
2279 i < pdu->choice.successfulOutcome->value.choice.RICcontrolAcknowledge.protocolIEs.list.count; i++) {
2280 auto messageSent = false;
2281 RICcontrolAcknowledge_IEs_t *ie = pdu->choice.successfulOutcome->value.choice.RICcontrolAcknowledge.protocolIEs.list.array[i];
2282 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2283 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
2285 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
2286 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2287 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
2289 if (ie->value.present == RICcontrolAcknowledge_IEs__value_PR_RICrequestID) {
2290 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_CONTROL_ACK;
2291 rmrMessageBuffer.sendMessage->state = 0;
2292 // rmrMessageBuffer.sendMessage->sub_id = (int) ie->value.choice.RICrequestID.ricRequestorID;
2293 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
2295 static unsigned char tx[32];
2296 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2297 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
2298 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
2299 (unsigned char *)message.message.enodbName,
2300 strlen(message.message.enodbName));
2301 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2302 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
2303 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment((double)message.message.asnLength);
2305 // Update E2T instance level metrics
2306 message.peerInfo->sctpParams->e2tCounters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
2307 message.peerInfo->sctpParams->e2tCounters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment((double)message.message.asnLength);
2309 sendRmrMessage(rmrMessageBuffer, message);
2312 mdclog_write(MDCLOG_ERR, "RIC request id missing illegal request");
2322 case ProcedureCode_id_RICsubscription: {
2323 if (logLevel >= MDCLOG_DEBUG) {
2324 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
2326 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2327 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
2328 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment((double)message.message.asnLength);
2330 // Update E2T instance level metrics
2331 message.peerInfo->sctpParams->e2tCounters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
2332 message.peerInfo->sctpParams->e2tCounters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment((double)message.message.asnLength);
2334 if (sendRequestToXapp(message, RIC_SUB_RESP, rmrMessageBuffer) != 0) {
2335 mdclog_write(MDCLOG_ERR, "Subscription successful message failed to send to xAPP");
2339 case ProcedureCode_id_RICsubscriptionDelete: {
2340 if (logLevel >= MDCLOG_DEBUG) {
2341 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
2343 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2344 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
2345 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment((double)message.message.asnLength);
2347 // Update E2T instance level metrics
2348 message.peerInfo->sctpParams->e2tCounters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
2349 message.peerInfo->sctpParams->e2tCounters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment((double)message.message.asnLength);
2351 if (sendRequestToXapp(message, RIC_SUB_DEL_RESP, rmrMessageBuffer) != 0) {
2352 mdclog_write(MDCLOG_ERR, "Subscription delete successful message failed to send to xAPP");
2357 mdclog_write(MDCLOG_WARN, "Undefined or not supported message = %ld", procedureCode);
2358 message.message.messageType = 0; // no RMR message type yet
2359 buildJsonMessage(message);
2370 * @param rmrMessageBuffer
2372 void asnUnSuccsesfulMsg(E2AP_PDU_t *pdu,
2373 Sctp_Map_t *sctpMap,
2374 ReportingMessages_t &message,
2375 RmrMessagesBuffer_t &rmrMessageBuffer) {
2376 auto procedureCode = pdu->choice.unsuccessfulOutcome->procedureCode;
2377 auto logLevel = mdclog_level_get();
2378 if (logLevel >= MDCLOG_INFO) {
2379 mdclog_write(MDCLOG_INFO, "Unsuccessful Outcome %ld", procedureCode);
2381 switch (procedureCode) {
2382 case ProcedureCode_id_RICcontrol: {
2383 if (logLevel >= MDCLOG_DEBUG) {
2384 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
2387 i < pdu->choice.unsuccessfulOutcome->value.choice.RICcontrolFailure.protocolIEs.list.count; i++) {
2388 auto messageSent = false;
2389 RICcontrolFailure_IEs_t *ie = pdu->choice.unsuccessfulOutcome->value.choice.RICcontrolFailure.protocolIEs.list.array[i];
2390 if (logLevel >= MDCLOG_DEBUG) {
2391 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
2393 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
2394 if (logLevel >= MDCLOG_DEBUG) {
2395 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
2397 if (ie->value.present == RICcontrolFailure_IEs__value_PR_RICrequestID) {
2398 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_CONTROL_FAILURE;
2399 rmrMessageBuffer.sendMessage->state = 0;
2400 // rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricRequestorID;
2401 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
2402 static unsigned char tx[32];
2403 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2404 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
2405 rmr_bytes2meid(rmrMessageBuffer.sendMessage, (unsigned char *) message.message.enodbName,
2406 strlen(message.message.enodbName));
2407 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2408 message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
2409 message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment((double)message.message.asnLength);
2411 // Update E2T instance level metrics
2412 message.peerInfo->sctpParams->e2tCounters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
2413 message.peerInfo->sctpParams->e2tCounters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment((double)message.message.asnLength);
2415 sendRmrMessage(rmrMessageBuffer, message);
2418 mdclog_write(MDCLOG_ERR, "RIC request id missing illegal request");
2427 case ProcedureCode_id_RICsubscription: {
2428 if (logLevel >= MDCLOG_DEBUG) {
2429 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
2431 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2432 message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
2433 message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment((double)message.message.asnLength);
2435 // Update E2T instance level metrics
2436 message.peerInfo->sctpParams->e2tCounters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
2437 message.peerInfo->sctpParams->e2tCounters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment((double)message.message.asnLength);
2439 if (sendRequestToXapp(message, RIC_SUB_FAILURE, rmrMessageBuffer) != 0) {
2440 mdclog_write(MDCLOG_ERR, "Subscription unsuccessful message failed to send to xAPP");
2444 case ProcedureCode_id_RICsubscriptionDelete: {
2445 if (logLevel >= MDCLOG_DEBUG) {
2446 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
2448 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2449 message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
2450 message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment((double)message.message.asnLength);
2452 // Update E2T instance level metrics
2453 message.peerInfo->sctpParams->e2tCounters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
2454 message.peerInfo->sctpParams->e2tCounters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment((double)message.message.asnLength);
2456 if (sendRequestToXapp(message, RIC_SUB_FAILURE, rmrMessageBuffer) != 0) {
2457 mdclog_write(MDCLOG_ERR, "Subscription Delete unsuccessful message failed to send to xAPP");
2462 mdclog_write(MDCLOG_WARN, "Undefined or not supported message = %ld", procedureCode);
2463 message.message.messageType = 0; // no RMR message type yet
2464 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2465 buildJsonMessage(message);
2476 * @param rmrMmessageBuffer
2479 int sendRequestToXapp(ReportingMessages_t &message,
2481 RmrMessagesBuffer_t &rmrMmessageBuffer) {
2482 rmr_bytes2meid(rmrMmessageBuffer.sendMessage,
2483 (unsigned char *)message.message.enodbName,
2484 strlen(message.message.enodbName));
2485 message.message.messageType = rmrMmessageBuffer.sendMessage->mtype = requestId;
2486 rmrMmessageBuffer.sendMessage->state = 0;
2487 static unsigned char tx[32];
2488 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2489 rmr_bytes2xact(rmrMmessageBuffer.sendMessage, tx, strlen((const char *) tx));
2491 auto rc = sendRmrMessage(rmrMmessageBuffer, message);
2497 * @param pSctpParams
2499 void getRmrContext(sctp_params_t &pSctpParams) {
2500 pSctpParams.rmrCtx = nullptr;
2501 pSctpParams.rmrCtx = rmr_init(pSctpParams.rmrAddress, RECEIVE_XAPP_BUFFER_SIZE, RMRFL_NONE);
2502 if (pSctpParams.rmrCtx == nullptr) {
2503 mdclog_write(MDCLOG_ERR, "Failed to initialize RMR");
2507 rmr_set_stimeout(pSctpParams.rmrCtx, 0); // disable retries for any send operation
2508 // we need to find that routing table exist and we can run
2509 if (mdclog_level_get() >= MDCLOG_INFO) {
2510 mdclog_write(MDCLOG_INFO, "We are after RMR INIT wait for RMR_Ready");
2515 if ((rmrReady = rmr_ready(pSctpParams.rmrCtx)) == 0) {
2519 if (count % 60 == 0) {
2520 mdclog_write(MDCLOG_INFO, "waiting to RMR ready state for %d seconds", count);
2523 if (mdclog_level_get() >= MDCLOG_INFO) {
2524 mdclog_write(MDCLOG_INFO, "RMR running");
2526 rmr_init_trace(pSctpParams.rmrCtx, 200);
2527 // get the RMR fd for the epoll
2528 pSctpParams.rmrListenFd = rmr_get_rcvfd(pSctpParams.rmrCtx);
2529 struct epoll_event event{};
2530 // add RMR fd to epoll
2531 event.events = (EPOLLIN);
2532 event.data.fd = pSctpParams.rmrListenFd;
2533 // add listening RMR FD to epoll
2534 if (epoll_ctl(pSctpParams.epoll_fd, EPOLL_CTL_ADD, pSctpParams.rmrListenFd, &event)) {
2535 mdclog_write(MDCLOG_ERR, "Failed to add RMR descriptor to epoll");
2536 close(pSctpParams.rmrListenFd);
2537 rmr_close(pSctpParams.rmrCtx);
2538 pSctpParams.rmrCtx = nullptr;
2545 * @param rmrMessageBuffer
2548 int PER_FromXML(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
2549 E2AP_PDU_t *pdu = nullptr;
2551 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2552 mdclog_write(MDCLOG_DEBUG, "got xml Format data from xApp of size %d is:%s",
2553 rmrMessageBuffer.rcvMessage->len, rmrMessageBuffer.rcvMessage->payload);
2555 auto rval = asn_decode(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, (void **) &pdu,
2556 rmrMessageBuffer.rcvMessage->payload, rmrMessageBuffer.rcvMessage->len);
2557 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2558 mdclog_write(MDCLOG_DEBUG, "%s After decoding the XML to PDU", __func__ );
2560 if (rval.code != RC_OK) {
2564 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) setup response from E2MGR : %s",
2566 message.message.enodbName);
2567 if (pdu != nullptr) {
2568 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
2574 int buff_size = RECEIVE_XAPP_BUFFER_SIZE;
2575 auto er = asn_encode_to_buffer(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, pdu,
2576 rmrMessageBuffer.rcvMessage->payload, buff_size);
2577 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2578 mdclog_write(MDCLOG_DEBUG, "%s After encoding PDU to PER", __func__ );
2580 if (er.encoded == -1) {
2581 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
2582 if (pdu != nullptr) {
2583 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
2587 } else if (er.encoded > (ssize_t)buff_size) {
2588 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
2589 (int)rmrMessageBuffer.rcvMessage->len,
2590 asn_DEF_E2AP_PDU.name,
2593 if (pdu != nullptr) {
2594 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
2599 rmrMessageBuffer.rcvMessage->len = er.encoded;
2600 if (pdu != nullptr) {
2601 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
2610 * @param rmrMessageBuffer
2614 int receiveXappMessages(Sctp_Map_t *sctpMap,
2615 RmrMessagesBuffer_t &rmrMessageBuffer,
2616 struct timespec &ts) {
2617 int loglevel = mdclog_level_get();
2618 if (rmrMessageBuffer.rcvMessage == nullptr) {
2620 mdclog_write(MDCLOG_ERR, "RMR Allocation message, %s", strerror(errno));
2624 // if (loglevel >= MDCLOG_DEBUG) {
2625 // mdclog_write(MDCLOG_DEBUG, "Call to rmr_rcv_msg");
2627 rmrMessageBuffer.rcvMessage = rmr_rcv_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.rcvMessage);
2628 if (rmrMessageBuffer.rcvMessage == nullptr) {
2629 mdclog_write(MDCLOG_ERR, "RMR Receiving message with null pointer, Reallocated rmr message buffer");
2630 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
2633 ReportingMessages_t message;
2634 message.message.direction = 'D';
2635 message.message.time.tv_nsec = ts.tv_nsec;
2636 message.message.time.tv_sec = ts.tv_sec;
2638 // get message payload
2639 //auto msgData = msg->payload;
2641 rmrMessageBuffer.rcvMessage->state = 0;
2643 if (rmrMessageBuffer.rcvMessage->state != 0) {
2644 mdclog_write(MDCLOG_ERR, "RMR Receiving message with stat = %d", rmrMessageBuffer.rcvMessage->state);
2647 rmr_get_meid(rmrMessageBuffer.rcvMessage, (unsigned char *)message.message.enodbName);
2648 message.peerInfo = (ConnectedCU_t *) sctpMap->find(message.message.enodbName);
2649 if (message.peerInfo == nullptr) {
2650 auto type = rmrMessageBuffer.rcvMessage->mtype;
2652 case RIC_SCTP_CLEAR_ALL:
2653 case E2_TERM_KEEP_ALIVE_REQ:
2654 case RIC_HEALTH_CHECK_REQ:
2660 mdclog_write(MDCLOG_ERR, "Failed to send message no CU entry %s", message.message.enodbName);
2665 if (rmrMessageBuffer.rcvMessage->mtype != RIC_HEALTH_CHECK_REQ) {
2666 num_of_XAPP_messages.fetch_add(1, std::memory_order_release);
2669 switch (rmrMessageBuffer.rcvMessage->mtype) {
2670 case RIC_E2_SETUP_RESP : {
2671 if (loglevel >= MDCLOG_DEBUG) {
2672 mdclog_write(MDCLOG_DEBUG, "RIC_E2_SETUP_RESP");
2674 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2677 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2678 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2679 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment(rmrMessageBuffer.rcvMessage->len);
2681 // Update E2T instance level metrics
2682 message.peerInfo->sctpParams->e2tCounters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2683 message.peerInfo->sctpParams->e2tCounters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment(rmrMessageBuffer.rcvMessage->len);
2685 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2686 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_SETUP_RESP");
2691 case RIC_E2_SETUP_FAILURE : {
2692 if (loglevel >= MDCLOG_DEBUG) {
2693 mdclog_write(MDCLOG_DEBUG, "RIC_E2_SETUP_FAILURE");
2695 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2698 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2699 message.peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2700 message.peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment(rmrMessageBuffer.rcvMessage->len);
2702 // Update E2T instance level metrics
2703 message.peerInfo->sctpParams->e2tCounters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2704 message.peerInfo->sctpParams->e2tCounters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment(rmrMessageBuffer.rcvMessage->len);
2706 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2707 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_SETUP_FAILURE");
2713 case RIC_E2NODE_CONFIG_UPDATE_ACK: {
2714 if (loglevel >= MDCLOG_DEBUG) {
2715 mdclog_write(MDCLOG_DEBUG, "RIC_E2NODE_CONFIG_UPDATE_ACK");
2717 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2720 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2721 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment();
2722 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
2724 // Update E2T instance level metrics
2725 message.peerInfo->sctpParams->e2tCounters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment();
2726 message.peerInfo->sctpParams->e2tCounters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
2728 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2729 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2NODE_CONFIG_UPDATE_ACK");
2735 case RIC_E2NODE_CONFIG_UPDATE_FAILURE: {
2736 if (loglevel >= MDCLOG_DEBUG) {
2737 mdclog_write(MDCLOG_DEBUG, "RIC_E2NODE_CONFIG_UPDATE_FAILURE");
2739 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2742 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2743 message.peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment();
2744 message.peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
2746 // Update E2T instance level metrics
2747 message.peerInfo->sctpParams->e2tCounters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment();
2748 message.peerInfo->sctpParams->e2tCounters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
2750 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2751 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2NODE_CONFIG_UPDATE_FAILURE");
2757 case RIC_ERROR_INDICATION: {
2758 if (loglevel >= MDCLOG_DEBUG) {
2759 mdclog_write(MDCLOG_DEBUG, "RIC_ERROR_INDICATION");
2761 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2762 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_ErrorIndication]->Increment();
2763 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_ErrorIndication]->Increment(rmrMessageBuffer.rcvMessage->len);
2765 // Update E2T instance level metrics
2766 message.peerInfo->sctpParams->e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_ErrorIndication]->Increment();
2767 message.peerInfo->sctpParams->e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_ErrorIndication]->Increment(rmrMessageBuffer.rcvMessage->len);
2769 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2770 mdclog_write(MDCLOG_ERR, "Failed to send RIC_ERROR_INDICATION");
2776 if (loglevel >= MDCLOG_DEBUG) {
2777 mdclog_write(MDCLOG_DEBUG, "RIC_SUB_REQ");
2779 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2780 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
2781 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment(rmrMessageBuffer.rcvMessage->len);
2783 // Update E2T instance level metrics
2784 message.peerInfo->sctpParams->e2tCounters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
2785 message.peerInfo->sctpParams->e2tCounters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment(rmrMessageBuffer.rcvMessage->len);
2787 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2788 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SUB_REQ");
2793 case RIC_SUB_DEL_REQ: {
2794 if (loglevel >= MDCLOG_DEBUG) {
2795 mdclog_write(MDCLOG_DEBUG, "RIC_SUB_DEL_REQ");
2797 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2798 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
2799 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment(rmrMessageBuffer.rcvMessage->len);
2801 // Update E2T instance level metrics
2802 message.peerInfo->sctpParams->e2tCounters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
2803 message.peerInfo->sctpParams->e2tCounters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment(rmrMessageBuffer.rcvMessage->len);
2805 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2806 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SUB_DEL_REQ");
2811 case RIC_CONTROL_REQ: {
2812 if (loglevel >= MDCLOG_DEBUG) {
2813 mdclog_write(MDCLOG_DEBUG, "RIC_CONTROL_REQ");
2815 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2816 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
2817 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment(rmrMessageBuffer.rcvMessage->len);
2819 // Update E2T instance level metrics
2820 message.peerInfo->sctpParams->e2tCounters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
2821 message.peerInfo->sctpParams->e2tCounters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment(rmrMessageBuffer.rcvMessage->len);
2823 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2824 mdclog_write(MDCLOG_ERR, "Failed to send RIC_CONTROL_REQ");
2829 case RIC_SERVICE_QUERY: {
2830 if (loglevel >= MDCLOG_DEBUG) {
2831 mdclog_write(MDCLOG_DEBUG, "RIC_SERVICE_QUERY");
2833 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2836 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2837 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceQuery]->Increment();
2838 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceQuery]->Increment(rmrMessageBuffer.rcvMessage->len);
2840 // Update E2T instance level metrics
2841 message.peerInfo->sctpParams->e2tCounters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceQuery]->Increment();
2842 message.peerInfo->sctpParams->e2tCounters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceQuery]->Increment(rmrMessageBuffer.rcvMessage->len);
2844 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2845 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_QUERY");
2850 case RIC_SERVICE_UPDATE_ACK: {
2851 if (loglevel >= MDCLOG_DEBUG) {
2852 mdclog_write(MDCLOG_DEBUG, "RIC_SERVICE_UPDATE_ACK");
2854 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2855 mdclog_write(MDCLOG_ERR, "error in PER_FromXML");
2858 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2859 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
2860 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
2862 // Update E2T instance level metrics
2863 message.peerInfo->sctpParams->e2tCounters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
2864 message.peerInfo->sctpParams->e2tCounters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
2866 if (loglevel >= MDCLOG_DEBUG) {
2867 mdclog_write(MDCLOG_DEBUG, "Before sending to CU");
2869 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2870 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_UPDATE_ACK");
2875 case RIC_SERVICE_UPDATE_FAILURE: {
2876 if (loglevel >= MDCLOG_DEBUG) {
2877 mdclog_write(MDCLOG_DEBUG, "RIC_SERVICE_UPDATE_FAILURE");
2879 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2882 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2883 message.peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
2884 message.peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
2886 // Update E2T instance level metrics
2887 message.peerInfo->sctpParams->e2tCounters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
2888 message.peerInfo->sctpParams->e2tCounters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
2890 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2891 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_UPDATE_FAILURE");
2896 case RIC_E2_RESET_REQ: {
2897 if (loglevel >= MDCLOG_DEBUG) {
2898 mdclog_write(MDCLOG_DEBUG, "RIC_E2_RESET_REQ");
2900 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2903 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2904 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2905 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment(rmrMessageBuffer.rcvMessage->len);
2907 // Update E2T instance level metrics
2908 message.peerInfo->sctpParams->e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2909 message.peerInfo->sctpParams->e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment(rmrMessageBuffer.rcvMessage->len);
2911 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2912 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_RESET");
2917 case RIC_E2_RESET_RESP: {
2918 if (loglevel >= MDCLOG_DEBUG) {
2919 mdclog_write(MDCLOG_DEBUG, "RIC_E2_RESET_RESP");
2921 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2924 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2925 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2926 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment(rmrMessageBuffer.rcvMessage->len);
2928 // Update E2T instance level metrics
2929 message.peerInfo->sctpParams->e2tCounters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2930 message.peerInfo->sctpParams->e2tCounters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment(rmrMessageBuffer.rcvMessage->len);
2932 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2933 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_RESET_RESP");
2938 case RIC_SCTP_CLEAR_ALL: {
2939 mdclog_write(MDCLOG_INFO, "RIC_SCTP_CLEAR_ALL");
2940 // loop on all keys and close socket and then erase all map.
2942 sctpMap->getKeys(v);
2943 for (auto const &iter : v) { //}; iter != sctpMap.end(); iter++) {
2944 if (!boost::starts_with((string) (iter), "host:") && !boost::starts_with((string) (iter), "msg:")) {
2945 auto *peerInfo = (ConnectedCU_t *) sctpMap->find(iter);
2946 if (peerInfo == nullptr) {
2949 close(peerInfo->fileDescriptor);
2950 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
2951 message.message.direction = 'D';
2952 message.message.time.tv_nsec = ts.tv_nsec;
2953 message.message.time.tv_sec = ts.tv_sec;
2955 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
2956 snprintf((char *)rmrMessageBuffer.sendMessage->payload,
2958 "%s|RIC_SCTP_CLEAR_ALL",
2959 peerInfo->enodbName);
2960 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
2961 mdclog_write(MDCLOG_INFO, "%s", message.message.asndata);
2962 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
2963 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
2973 case E2_TERM_KEEP_ALIVE_REQ: {
2974 // send message back
2975 rmr_bytes2payload(rmrMessageBuffer.sendMessage,
2976 (unsigned char *)rmrMessageBuffer.ka_message,
2977 rmrMessageBuffer.ka_message_len);
2978 rmrMessageBuffer.sendMessage->mtype = E2_TERM_KEEP_ALIVE_RESP;
2979 rmrMessageBuffer.sendMessage->state = 0;
2980 static unsigned char tx[32];
2981 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2982 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, txLen);
2983 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2984 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2986 if (rmrMessageBuffer.sendMessage == nullptr) {
2987 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
2988 mdclog_write(MDCLOG_ERR, "Failed to send E2_TERM_KEEP_ALIVE_RESP RMR message returned NULL");
2989 } else if (rmrMessageBuffer.sendMessage->state != 0) {
2990 mdclog_write(MDCLOG_ERR, "Failed to send E2_TERM_KEEP_ALIVE_RESP, on RMR state = %d ( %s)",
2991 rmrMessageBuffer.sendMessage->state, translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str());
2992 } else if (loglevel >= MDCLOG_DEBUG) {
2993 mdclog_write(MDCLOG_DEBUG, "Got Keep Alive Request send : %s", rmrMessageBuffer.ka_message);
2998 case RIC_HEALTH_CHECK_REQ: {
2999 static int counter = 0;
3000 // send message back
3001 rmr_bytes2payload(rmrMessageBuffer.rcvMessage,
3002 (unsigned char *)"OK",
3004 rmrMessageBuffer.rcvMessage->mtype = RIC_HEALTH_CHECK_RESP;
3005 rmrMessageBuffer.rcvMessage->state = 0;
3006 static unsigned char tx[32];
3007 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
3008 rmr_bytes2xact(rmrMessageBuffer.rcvMessage, tx, txLen);
3009 rmrMessageBuffer.rcvMessage = rmr_rts_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.rcvMessage);
3010 //rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
3011 if (rmrMessageBuffer.rcvMessage == nullptr) {
3012 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
3013 mdclog_write(MDCLOG_ERR, "Failed to send RIC_HEALTH_CHECK_RESP RMR message returned NULL");
3014 } else if (rmrMessageBuffer.rcvMessage->state != 0) {
3015 mdclog_write(MDCLOG_ERR, "Failed to send RIC_HEALTH_CHECK_RESP, on RMR state = %d ( %s)",
3016 rmrMessageBuffer.rcvMessage->state, translateRmrErrorMessages(rmrMessageBuffer.rcvMessage->state).c_str());
3017 } else if (loglevel >= MDCLOG_DEBUG && (++counter % 100 == 0)) {
3018 mdclog_write(MDCLOG_DEBUG, "Got %d RIC_HEALTH_CHECK_REQ Request send : OK", counter);
3025 mdclog_write(MDCLOG_WARN, "Message Type : %d is not supported", rmrMessageBuffer.rcvMessage->mtype);
3026 message.message.asndata = rmrMessageBuffer.rcvMessage->payload;
3027 message.message.asnLength = rmrMessageBuffer.rcvMessage->len;
3028 message.message.time.tv_nsec = ts.tv_nsec;
3029 message.message.time.tv_sec = ts.tv_sec;
3030 message.message.messageType = rmrMessageBuffer.rcvMessage->mtype;
3032 buildJsonMessage(message);
3037 if (mdclog_level_get() >= MDCLOG_DEBUG) {
3038 mdclog_write(MDCLOG_DEBUG, "EXIT OK from %s", __FUNCTION__);
3044 * Send message to the CU that is not expecting for successful or unsuccessful results
3045 * @param messageBuffer
3047 * @param failedMsgId
3051 int sendDirectionalSctpMsg(RmrMessagesBuffer_t &messageBuffer,
3052 ReportingMessages_t &message,
3054 Sctp_Map_t *sctpMap) {
3055 if (mdclog_level_get() >= MDCLOG_DEBUG) {
3056 mdclog_write(MDCLOG_DEBUG, "send message: %d to %s address", message.message.messageType, message.message.enodbName);
3059 getRequestMetaData(message, messageBuffer);
3060 if (mdclog_level_get() >= MDCLOG_INFO) {
3061 mdclog_write(MDCLOG_INFO, "send message to %s address", message.message.enodbName);
3064 auto rc = sendMessagetoCu(sctpMap, messageBuffer, message, failedMsgId);
3071 * @param messageBuffer
3073 * @param failedMesgId
3076 int sendMessagetoCu(Sctp_Map_t *sctpMap,
3077 RmrMessagesBuffer_t &messageBuffer,
3078 ReportingMessages_t &message,
3081 message.message.messageType = messageBuffer.rcvMessage->mtype;
3082 auto rc = sendSctpMsg(message.peerInfo, message, sctpMap);
3097 int addToEpoll(int epoll_fd,
3098 ConnectedCU_t *peerInfo,
3100 Sctp_Map_t *sctpMap,
3104 struct epoll_event event{};
3105 event.data.ptr = peerInfo;
3106 event.events = events;
3107 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, peerInfo->fileDescriptor, &event) < 0) {
3108 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
3109 if (mdclog_level_get() >= MDCLOG_DEBUG) {
3110 mdclog_write(MDCLOG_DEBUG, "epoll_ctl EPOLL_CTL_ADD (may check not to quit here), %s, %s %d",
3111 strerror(errno), __func__, __LINE__);
3113 close(peerInfo->fileDescriptor);
3114 if (enodbName != nullptr) {
3115 cleanHashEntry(peerInfo, sctpMap);
3116 char key[MAX_ENODB_NAME_SIZE * 2];
3117 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", enodbName, msgType);
3118 if (mdclog_level_get() >= MDCLOG_DEBUG) {
3119 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
3121 auto tmp = sctpMap->find(key);
3124 sctpMap->erase(key);
3127 peerInfo->enodbName[0] = 0;
3129 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may check not to quit here)");
3146 int modifyToEpoll(int epoll_fd,
3147 ConnectedCU_t *peerInfo,
3149 Sctp_Map_t *sctpMap,
3153 struct epoll_event event{};
3154 event.data.ptr = peerInfo;
3155 event.events = events;
3156 if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, peerInfo->fileDescriptor, &event) < 0) {
3157 if (mdclog_level_get() >= MDCLOG_DEBUG) {
3158 mdclog_write(MDCLOG_DEBUG, "epoll_ctl EPOLL_CTL_MOD (may check not to quit here), %s, %s %d",
3159 strerror(errno), __func__, __LINE__);
3161 close(peerInfo->fileDescriptor);
3162 cleanHashEntry(peerInfo, sctpMap);
3163 char key[MAX_ENODB_NAME_SIZE * 2];
3164 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", enodbName, msgType);
3165 if (mdclog_level_get() >= MDCLOG_DEBUG) {
3166 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
3168 auto tmp = sctpMap->find(key);
3172 sctpMap->erase(key);
3173 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may check not to quit here)");
3180 int sendRmrMessage(RmrMessagesBuffer_t &rmrMessageBuffer, ReportingMessages_t &message) {
3181 buildJsonMessage(message);
3183 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
3185 rmrMessageBuffer.sendMessage->state = RMR_ERR_RETRY;
3187 if (rmrMessageBuffer.sendMessage == nullptr) {
3188 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
3189 mdclog_write(MDCLOG_ERR, "RMR failed send message returned with NULL pointer");
3193 if (rmrMessageBuffer.sendMessage->state != 0) {
3194 char meid[RMR_MAX_MEID]{};
3195 if (rmrMessageBuffer.sendMessage->state == RMR_ERR_RETRY) {
3197 rmrMessageBuffer.sendMessage->state = 0;
3198 mdclog_write(MDCLOG_INFO, "RETRY sending Message type %d to Xapp from %s",
3199 rmrMessageBuffer.sendMessage->mtype,
3200 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
3202 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
3204 if (rmrMessageBuffer.sendMessage == nullptr) {
3205 mdclog_write(MDCLOG_ERR, "RMR failed send message returned with NULL pointer");
3206 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
3208 } else if (rmrMessageBuffer.sendMessage->state != 0) {
3209 mdclog_write(MDCLOG_ERR,
3210 "Message state %s while sending request %d to Xapp from %s after retry of 10 microseconds",
3211 translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str(),
3212 rmrMessageBuffer.sendMessage->mtype,
3213 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
3214 auto rc = rmrMessageBuffer.sendMessage->state;
3218 mdclog_write(MDCLOG_ERR, "Message state %s while sending request %d to Xapp from %s",
3219 translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str(),
3220 rmrMessageBuffer.sendMessage->mtype,
3221 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
3222 return rmrMessageBuffer.sendMessage->state;
3228 void buildJsonMessage(ReportingMessages_t &message) {
3233 message.outLen = sizeof(message.base64Data);
3234 base64::encode((const unsigned char *) message.message.asndata,
3235 (const int) message.message.asnLength,
3238 if (mdclog_level_get() >= MDCLOG_DEBUG) {
3239 mdclog_write(MDCLOG_DEBUG, "Tracing: ASN length = %d, base64 message length = %d ",
3240 (int) message.message.asnLength,
3241 (int) message.outLen);
3244 snprintf(message.buffer, sizeof(message.buffer),
3245 "{\"header\": {\"ts\": \"%ld.%09ld\","
3246 "\"ranName\": \"%s\","
3247 "\"messageType\": %d,"
3248 "\"direction\": \"%c\"},"
3249 "\"base64Length\": %d,"
3250 "\"asnBase64\": \"%s\"}",
3251 message.message.time.tv_sec,
3252 message.message.time.tv_nsec,
3253 message.message.enodbName,
3254 message.message.messageType,
3255 message.message.direction,
3256 (int) message.outLen,
3257 message.base64Data);
3258 static src::logger_mt &lg = my_logger::get();
3260 BOOST_LOG(lg) << message.buffer;
3266 * take RMR error code to string
3270 string translateRmrErrorMessages(int state) {
3274 str = "RMR_OK - state is good";
3276 case RMR_ERR_BADARG:
3277 str = "RMR_ERR_BADARG - argument passed to function was unusable";
3279 case RMR_ERR_NOENDPT:
3280 str = "RMR_ERR_NOENDPT - send//call could not find an endpoint based on msg type";
3283 str = "RMR_ERR_EMPTY - msg received had no payload; attempt to send an empty message";
3286 str = "RMR_ERR_NOHDR - message didn't contain a valid header";
3288 case RMR_ERR_SENDFAILED:
3289 str = "RMR_ERR_SENDFAILED - send failed; errno has nano reason";
3291 case RMR_ERR_CALLFAILED:
3292 str = "RMR_ERR_CALLFAILED - unable to send call() message";
3294 case RMR_ERR_NOWHOPEN:
3295 str = "RMR_ERR_NOWHOPEN - no wormholes are open";
3298 str = "RMR_ERR_WHID - wormhole id was invalid";
3300 case RMR_ERR_OVERFLOW:
3301 str = "RMR_ERR_OVERFLOW - operation would have busted through a buffer/field size";
3304 str = "RMR_ERR_RETRY - request (send/call/rts) failed, but caller should retry (EAGAIN for wrappers)";
3306 case RMR_ERR_RCVFAILED:
3307 str = "RMR_ERR_RCVFAILED - receive failed (hard error)";
3309 case RMR_ERR_TIMEOUT:
3310 str = "RMR_ERR_TIMEOUT - message processing call timed out";
3313 str = "RMR_ERR_UNSET - the message hasn't been populated with a transport buffer";
3316 str = "RMR_ERR_TRUNC - received message likely truncated";
3318 case RMR_ERR_INITFAILED:
3319 str = "RMR_ERR_INITFAILED - initialisation of something (probably message) failed";
3321 case RMR_ERR_NOTSUPP:
3322 str = "RMR_ERR_NOTSUPP - the request is not supported, or RMr was not initialised for the request";
3326 snprintf(buf, sizeof buf, "UNDOCUMENTED RMR_ERR : %d", state);
3332 int fetchStreamId(ConnectedCU_t *peerInfo, ReportingMessages_t &message)
3334 auto loglevel = mdclog_level_get();
3335 int streamId = INVALID_STREAM_ID;
3336 if(message.peerInfo->isSingleStream != false)
3338 streamId = message.peerInfo->singleStreamId;
3339 if (loglevel >= MDCLOG_DEBUG) {
3340 mdclog_write(MDCLOG_DEBUG, "Send SCTP message for SINGLE_STREAM streamId %d , Messeage Type %d ,%s",
3341 streamId,message.message.messageType, __FUNCTION__);
3345 int msgType = message.message.messageType;
3347 case RIC_E2_RESET_REQ:
3348 case RIC_E2_RESET_RESP:
3349 case RIC_E2_SETUP_RESP:
3350 case RIC_E2_SETUP_FAILURE:
3351 case RIC_ERROR_INDICATION:
3352 case RIC_SERVICE_QUERY:
3353 case RIC_SERVICE_UPDATE_ACK:
3354 case RIC_SERVICE_UPDATE_FAILURE:
3358 case RIC_SUB_DEL_REQ:
3359 case RIC_CONTROL_REQ:
3366 if (loglevel >= MDCLOG_DEBUG) {
3367 mdclog_write(MDCLOG_DEBUG, "Send SCTP message for streamId %d Messeage Type %d, %s",
3368 streamId, message.message.messageType, __FUNCTION__);