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>
35 #include <arpa/inet.h>
38 //using namespace std::placeholders;
39 using namespace boost::filesystem;
40 using namespace prometheus;
48 // need to expose without the include of gcov
49 extern "C" void __gcov_flush(void);
50 #define LOG_FILE_CONFIG_MAP "CONFIG_MAP_NAME"
51 #define E2AP_PPID 70 // as per E2GAP chapter 6.1
53 static void catch_function(int signal) {
59 BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger, src::logger_mt)
61 boost::shared_ptr<sinks::synchronous_sink<sinks::text_file_backend>> boostLogger;
62 // double cpuClock = 0.0;
63 bool jsonTrace = false;
65 char* getinterfaceip()
69 struct hostent *host_entry;
71 retVal = gethostname(hostname, sizeof(hostname));
74 host_entry = gethostbyname(hostname);
75 if ( host_entry == NULL )
77 IP = inet_ntoa(*((struct in_addr*) host_entry->h_addr_list[0]));
82 static int enable_log_change_notify(const char* fileName)
86 if ( lstat(fileName,&fileInfo) == 0 )
88 ret = register_log_change_notify(fileName);
94 static int register_log_change_notify(const char *fileName)
96 pthread_attr_t cb_attr;
98 pthread_attr_init(&cb_attr);
99 pthread_attr_setdetachstate(&cb_attr,PTHREAD_CREATE_DETACHED);
100 return pthread_create(&tid, &cb_attr,&monitor_loglevel_change_handler,(void *)fileName);
104 static void * monitor_loglevel_change_handler(void* arg)
106 char *fileName = (char*) arg;
107 int ifd; // the inotify file des
108 int wfd; // the watched file des
110 char rbuf[4096]; // large read buffer as the event is var len
113 char* dname=NULL; // directory name
114 char* bname = NULL; // basename
116 char* log_level=NULL;
118 dname = strdup( fileName ); // defrock the file name into dir and basename
119 if( (tok = strrchr( dname, '/' )) != NULL ) {
121 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 ) );
137 res = select (ifd + 1, &fds, NULL, NULL, NULL);
140 n = read( ifd, rbuf, sizeof( rbuf ) ); // read the event
142 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
143 if( errno == EAGAIN ) {
145 fprintf( stderr, "### CRIT ### config listener read err: %s\n", strerror( errno ) );
151 //Retrieving Log Level from configmap by parsing configmap file
152 log_level = parse_file(fileName);
153 update_mdc_log_level_severity(log_level); //setting log level
154 if(log_level != NULL) {
155 mdclog_write(MDCLOG_INFO, "MDC log level updated to %s", log_level);
160 inotify_rm_watch(ifd,wfd);
171 void update_mdc_log_level_severity(char* log_level)
173 mdclog_severity_t level = MDCLOG_ERR;
175 if(log_level == NULL)
177 printf("### ERR ### Invalid Log-Level Configuration in ConfigMap, Default Log-Level Applied: %d\n",level);
179 else if(strcasecmp(log_level,"1")==0)
183 else if(strcasecmp(log_level,"2")==0)
187 else if(strcasecmp(log_level,"3")==0)
191 else if(strcasecmp(log_level,"4")==0)
193 level = MDCLOG_DEBUG;
196 mdclog_level_set(level);
200 * @brief Remove leading and trailing spaces from s.
202 * If the string was allocated dynamically, the caller cannot
203 * overwrite the returned pointer.
205 * @param s the string we want to remove spaces.
206 * @return Returns a null-terminated substring of "s".
208 static inline char *trim(char *s)
211 /* skip leading spaces */
212 while (isspace(*s)) s++;
215 if (*s == '\0') return s;
217 /* skip trailing spaces */
218 end = s + strlen(s) - 1;
219 while (end > s && isspace(*end)) end--;
221 /* write null character */
227 static char* parse_file(char* filename)
231 FILE *file = fopen ( filename, "r" );
236 while ( fgets ( line, sizeof(line), file ) != NULL )
238 token = strtok(line, ":");
240 if (strcmp(token,"log-level") == 0) {
242 token = strtok(NULL, "\n");
250 return(strdup(token));
255 char *read_env_param(const char*envkey)
259 char *value = getenv(envkey);
261 return strdup(value);
266 void dynamic_log_level_change()
268 char *logFile_Name = read_env_param(LOG_FILE_CONFIG_MAP);
269 char* log_level_init=NULL;
272 log_level_init = parse_file(logFile_Name);
273 update_mdc_log_level_severity(log_level_init); //setting log level
274 mdclog_write(MDCLOG_INFO, "MDC log level set to %s", log_level_init);
275 free(log_level_init);
278 enable_log_change_notify(logFile_Name);
282 int log_change_monitor = 0;
284 mdclog_attr_init(&attr);
285 mdclog_attr_set_ident(attr, "E2Terminator");
287 if(mdclog_format_initialize(log_change_monitor)!=0)
288 mdclog_write(MDCLOG_ERR, "Failed to intialize MDC log format !!!");
289 dynamic_log_level_change();
290 mdclog_attr_destroy(attr);
292 //auto start_time = std::chrono::high_resolution_clock::now();
293 //typedef std::chrono::duration<double, std::ratio<1,1>> seconds_t;
296 // return seconds_t(std::chrono::high_resolution_clock::now() - start_time).count();
299 // If model name in "/proc/cpuinfo" is happens to be "Common KVM processor" then
300 // approx_CPU_MHz() results in "SIGILL - Illegal Instruction" signal.
302 // double approx_CPU_MHz(unsigned sleepTime) {
303 // using namespace std::chrono_literals;
305 // uint64_t cycles_start = rdtscp(aux);
306 // double time_start = age();
307 // std::this_thread::sleep_for(sleepTime * 1ms);
308 // uint64_t elapsed_cycles = rdtscp(aux) - cycles_start;
309 // double elapsed_time = age() - time_start;
310 // return elapsed_cycles / elapsed_time;
313 //std::atomic<int64_t> rmrCounter{0};
314 std::atomic<int64_t> num_of_messages{0};
315 std::atomic<int64_t> num_of_XAPP_messages{0};
316 static long transactionCounter = 0;
317 pthread_mutex_t thread_lock;
319 int buildListeningPort(sctp_params_t &sctpParams) {
320 sctpParams.listenFD = socket(AF_INET6, SOCK_STREAM, IPPROTO_SCTP);
321 if (sctpParams.listenFD <= 0) {
322 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
323 mdclog_write(MDCLOG_ERR, "Error Opening socket, %s", strerror(errno));
327 struct sctp_initmsg initmsg;
328 memset (&initmsg, 0, sizeof (initmsg));
329 initmsg.sinit_num_ostreams = 2;
330 initmsg.sinit_max_instreams = 2;
331 initmsg.sinit_max_attempts = 4;
332 setsockopt (sctpParams.listenFD, IPPROTO_SCTP, SCTP_INITMSG, &initmsg, sizeof (initmsg));
334 struct sockaddr_in6 serverAddress {};
335 serverAddress.sin6_family = AF_INET6;
336 serverAddress.sin6_addr = in6addr_any;
337 serverAddress.sin6_port = htons(sctpParams.sctpPort);
338 if (bind(sctpParams.listenFD, (SA *)&serverAddress, sizeof(serverAddress)) < 0 ) {
339 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
340 mdclog_write(MDCLOG_ERR, "Error binding port %d. %s", sctpParams.sctpPort, strerror(errno));
344 if (setSocketNoBlocking(sctpParams.listenFD) == -1) {
345 //mdclog_write(MDCLOG_ERR, "Error binding. %s", strerror(errno));
348 if (mdclog_level_get() >= MDCLOG_DEBUG) {
349 struct sockaddr_in6 clientAddress {};
350 socklen_t len = sizeof(clientAddress);
351 getsockname(sctpParams.listenFD, (SA *)&clientAddress, &len);
353 inet_ntop(AF_INET6, &clientAddress.sin6_addr, buff, sizeof(buff));
354 mdclog_write(MDCLOG_DEBUG, "My address: %s, port %d\n", buff, htons(clientAddress.sin6_port));
357 if (listen(sctpParams.listenFD, SOMAXCONN) < 0) {
358 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
359 mdclog_write(MDCLOG_ERR, "Error listening. %s\n", strerror(errno));
363 struct epoll_event event {};
364 event.events = EPOLLIN | EPOLLET;
365 event.data.fd = sctpParams.listenFD;
367 // add listening port to epoll
368 if (epoll_ctl(sctpParams.epoll_fd, EPOLL_CTL_ADD, sctpParams.listenFD, &event)) {
369 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
370 printf("Failed to add descriptor to epoll\n");
371 mdclog_write(MDCLOG_ERR, "Failed to add descriptor to epoll. %s\n", strerror(errno));
379 int buildConfiguration(sctp_params_t &sctpParams) {
380 path p = (sctpParams.configFilePath + "/" + sctpParams.configFileName).c_str();
382 const int size = 2048;
383 auto fileSize = file_size(p);
384 if (fileSize > size) {
385 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
386 mdclog_write(MDCLOG_ERR, "File %s larger than %d", p.string().c_str(), size);
391 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
392 mdclog_write(MDCLOG_ERR, "Configuration File %s not exists", p.string().c_str());
398 if (conf.openConfigFile(p.string()) == -1) {
399 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
400 mdclog_write(MDCLOG_ERR, "Filed to open config file %s, %s",
401 p.string().c_str(), strerror(errno));
405 int rmrPort = conf.getIntValue("nano");
407 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
408 mdclog_write(MDCLOG_ERR, "illegal RMR port ");
412 sctpParams.rmrPort = (uint16_t)rmrPort;
413 snprintf(sctpParams.rmrAddress, sizeof(sctpParams.rmrAddress), "%d", (int) (sctpParams.rmrPort));
414 auto tmpStr = conf.getStringValue("volume");
415 if (tmpStr.length() == 0) {
416 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
417 mdclog_write(MDCLOG_ERR, "illegal volume.");
422 char tmpLogFilespec[VOLUME_URL_SIZE];
423 tmpLogFilespec[0] = 0;
424 sctpParams.volume[0] = 0;
425 snprintf(sctpParams.volume, VOLUME_URL_SIZE, "%s", tmpStr.c_str());
426 // copy the name to temp file as well
427 snprintf(tmpLogFilespec, VOLUME_URL_SIZE, "%s", tmpStr.c_str());
430 // define the file name in the tmp directory under the volume
431 strcat(tmpLogFilespec,"/tmp/E2Term_%Y-%m-%d_%H-%M-%S.%N.tmpStr");
433 sctpParams.myIP = conf.getStringValue("local-ip");
434 if (sctpParams.myIP.length() == 0) {
435 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
436 mdclog_write(MDCLOG_ERR, "illegal local-ip.");
441 int sctpPort = conf.getIntValue("sctp-port");
442 if (sctpPort == -1) {
443 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
444 mdclog_write(MDCLOG_ERR, "illegal SCTP port ");
448 sctpParams.sctpPort = (uint16_t)sctpPort;
450 sctpParams.fqdn = conf.getStringValue("external-fqdn");
451 if (sctpParams.fqdn.length() == 0) {
452 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
453 mdclog_write(MDCLOG_ERR, "illegal external-fqdn");
458 std::string pod = conf.getStringValue("pod_name");
460 if (pod.length() == 0) {
461 mdclog_write(MDCLOG_ERR, "illegal pod_name in config file");
464 auto *podName = getenv(pod.c_str());
465 if (podName == nullptr) {
466 mdclog_write(MDCLOG_ERR, "illegal pod_name or environment variable not exists : %s", pod.c_str());
470 sctpParams.podName.assign(podName);
471 if (sctpParams.podName.length() == 0) {
472 mdclog_write(MDCLOG_ERR, "illegal pod_name");
477 tmpStr = conf.getStringValue("trace");
478 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
479 if ((tmpStr.compare("start")) == 0) {
480 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
481 mdclog_write(MDCLOG_INFO, "Trace set to: start");
482 sctpParams.trace = true;
484 } else if ((tmpStr.compare("stop")) == 0) {
485 mdclog_write(MDCLOG_INFO, "Trace set to: stop");
486 sctpParams.trace = false;
488 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
489 mdclog_write(MDCLOG_ERR, "Trace was set to wrong value %s, set to stop", tmpStr.c_str());
490 sctpParams.trace = false;
493 jsonTrace = sctpParams.trace;
495 sctpParams.epollTimeOut = -1;
497 tmpStr = conf.getStringValue("prometheusPort");
498 if (tmpStr.length() != 0) {
499 sctpParams.prometheusPort = tmpStr;
502 sctpParams.ka_message_length = snprintf(sctpParams.ka_message, KA_MESSAGE_SIZE, "{\"address\": \"%s:%d\","
504 "\"pod_name\": \"%s\"}",
505 (const char *)sctpParams.myIP.c_str(),
507 sctpParams.fqdn.c_str(),
508 sctpParams.podName.c_str());
510 if (mdclog_level_get() >= MDCLOG_INFO) {
511 mdclog_write(MDCLOG_DEBUG,"RMR Port: %s", to_string(sctpParams.rmrPort).c_str());
512 mdclog_write(MDCLOG_DEBUG,"volume: %s", sctpParams.volume);
513 mdclog_write(MDCLOG_DEBUG,"tmpLogFilespec: %s", tmpLogFilespec);
514 mdclog_write(MDCLOG_DEBUG,"my ip: %s", sctpParams.myIP.c_str());
515 mdclog_write(MDCLOG_DEBUG,"pod name: %s", sctpParams.podName.c_str());
517 mdclog_write(MDCLOG_INFO, "running parameters for instance : %s", sctpParams.ka_message);
520 // Files written to the current working directory
521 boostLogger = logging::add_file_log(
522 keywords::file_name = tmpLogFilespec, // to temp directory
523 keywords::rotation_size = 10 * 1024 * 1024,
524 keywords::time_based_rotation = sinks::file::rotation_at_time_interval(posix_time::hours(1)),
525 keywords::format = "%Message%"
526 //keywords::format = "[%TimeStamp%]: %Message%" // use each tmpStr with time stamp
529 // Setup a destination folder for collecting rotated (closed) files --since the same volume can use rename()
530 boostLogger->locked_backend()->set_file_collector(sinks::file::make_collector(
531 keywords::target = sctpParams.volume
534 // Upon restart, scan the directory for files matching the file_name pattern
535 boostLogger->locked_backend()->scan_for_files();
537 // Enable auto-flushing after each tmpStr record written
538 if (mdclog_level_get() >= MDCLOG_DEBUG) {
539 boostLogger->locked_backend()->auto_flush(true);
545 void startPrometheus(sctp_params_t &sctpParams) {
546 auto podName = std::getenv("POD_NAME");
547 string metric = "E2TBeta";
548 if (strstr(podName, "alpha") != NULL) {
551 //Get eth0 interface IP
552 char* host = getinterfaceip();
553 string hostip = host;
555 sctpParams.prometheusFamily = &BuildCounter()
556 .Name(metric.c_str())
557 .Help("E2T instance metrics")
558 .Labels({{"POD_NAME", sctpParams.podName}})
559 .Register(*sctpParams.prometheusRegistry);
561 // Build E2T instance level metrics
562 buildE2TPrometheusCounters(sctpParams);
564 string prometheusPath;
566 prometheusPath = sctpParams.prometheusPort + "," + "[::]:" + sctpParams.prometheusPort;
568 prometheusPath = hostip + ":" + sctpParams.prometheusPort;
570 if (mdclog_level_get() >= MDCLOG_DEBUG) {
571 mdclog_write(MDCLOG_DEBUG, "Start Prometheus Pull mode on %s", prometheusPath.c_str());
573 sctpParams.prometheusExposer = new Exposer(prometheusPath, 1);
574 sctpParams.prometheusExposer->RegisterCollectable(sctpParams.prometheusRegistry);
578 int main(const int argc, char **argv) {
579 sctp_params_t sctpParams;
581 std::random_device device{};
582 std::mt19937 generator(device());
583 std::uniform_int_distribution<long> distribution(1, (long) 1e12);
584 transactionCounter = distribution(generator);
588 // uint32_t aux1 = 0;
589 // st = rdtscp(aux1);
591 unsigned num_cpus = std::thread::hardware_concurrency();
593 if (std::signal(SIGINT, catch_function) == SIG_ERR) {
594 mdclog_write(MDCLOG_ERR, "Error initializing SIGINT");
597 if (std::signal(SIGABRT, catch_function)== SIG_ERR) {
598 mdclog_write(MDCLOG_ERR, "Error initializing SIGABRT");
601 if (std::signal(SIGTERM, catch_function)== SIG_ERR) {
602 mdclog_write(MDCLOG_ERR, "Error initializing SIGTERM");
606 // cpuClock = approx_CPU_MHz(100);
608 // mdclog_write(MDCLOG_DEBUG, "CPU speed %11.11f", cpuClock);
610 auto result = parse(argc, argv, sctpParams);
612 if (buildConfiguration(sctpParams) != 0) {
616 //auto registry = std::make_shared<Registry>();
617 sctpParams.prometheusRegistry = std::make_shared<Registry>();
619 //sctpParams.prometheusFamily = new Family<Counter>("E2T", "E2T message counter", {{"E", sctpParams.podName}});
621 startPrometheus(sctpParams);
624 sctpParams.epoll_fd = epoll_create1(0);
625 if (sctpParams.epoll_fd == -1) {
626 mdclog_write(MDCLOG_ERR, "failed to open epoll descriptor");
629 getRmrContext(sctpParams);
630 if (sctpParams.rmrCtx == nullptr) {
631 close(sctpParams.epoll_fd);
635 if (buildInotify(sctpParams) == -1) {
636 close(sctpParams.rmrListenFd);
637 rmr_close(sctpParams.rmrCtx);
638 close(sctpParams.epoll_fd);
642 if (buildListeningPort(sctpParams) != 0) {
643 close(sctpParams.rmrListenFd);
644 rmr_close(sctpParams.rmrCtx);
645 close(sctpParams.epoll_fd);
649 sctpParams.sctpMap = new mapWrapper();
651 if (pthread_mutex_init(&thread_lock, NULL) != 0) {
652 mdclog_write(MDCLOG_ERR, "failed to init thread lock");
655 std::vector<std::thread> threads(num_cpus);
656 // std::vector<std::thread> threads;
659 for (unsigned int i = 0; i < num_cpus; i++) {
660 threads[i] = std::thread(listener, &sctpParams);
665 int rc = pthread_setaffinity_np(threads[i].native_handle(), sizeof(cpu_set_t), &cpuset);
667 mdclog_write(MDCLOG_ERR, "Error calling pthread_setaffinity_np: %d", rc);
672 //loop over term_init until first message from xApp
673 handleTermInit(sctpParams);
675 for (auto &t : threads) {
678 pthread_mutex_destroy(&thread_lock);
682 void handleTermInit(sctp_params_t &sctpParams) {
683 sendTermInit(sctpParams);
684 //send to e2 manager init of e2 term
689 auto xappMessages = num_of_XAPP_messages.load(std::memory_order_acquire);
690 if (xappMessages > 0) {
691 if (mdclog_level_get() >= MDCLOG_INFO) {
692 mdclog_write(MDCLOG_INFO, "Got a message from some application, stop sending E2_TERM_INIT");
698 if (count % 1000 == 0) {
699 mdclog_write(MDCLOG_ERR, "GOT No messages from any xApp");
700 sendTermInit(sctpParams);
705 void sendTermInit(sctp_params_t &sctpParams) {
706 rmr_mbuf_t *msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.ka_message_length);
709 msg->mtype = E2_TERM_INIT;
711 rmr_bytes2payload(msg, (unsigned char *)sctpParams.ka_message, sctpParams.ka_message_length);
712 static unsigned char tx[32];
713 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
714 rmr_bytes2xact(msg, tx, txLen);
715 msg = rmr_send_msg(sctpParams.rmrCtx, msg);
716 if (msg == nullptr) {
717 msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.ka_message_length);
718 } else if (msg->state == 0) {
720 if (mdclog_level_get() >= MDCLOG_INFO) {
721 mdclog_write(MDCLOG_INFO, "E2_TERM_INIT successfully sent ");
725 if (count % 100 == 0) {
726 mdclog_write(MDCLOG_ERR, "Error sending E2_TERM_INIT cause : %s ", translateRmrErrorMessages(msg->state).c_str());
741 cxxopts::ParseResult parse(int argc, char *argv[], sctp_params_t &sctpParams) {
742 cxxopts::Options options(argv[0], "e2 term help");
743 options.positional_help("[optional args]").show_positional_help();
744 options.allow_unrecognised_options().add_options()
745 ("p,path", "config file path", cxxopts::value<std::string>(sctpParams.configFilePath)->default_value("config"))
746 ("f,file", "config file name", cxxopts::value<std::string>(sctpParams.configFileName)->default_value("config.conf"))
747 ("h,help", "Print help");
749 auto result = options.parse(argc, (const char **&)argv);
751 if (result.count("help")) {
752 std::cout << options.help({""}) << std::endl;
761 * @return -1 failed 0 success
763 int buildInotify(sctp_params_t &sctpParams) {
764 sctpParams.inotifyFD = inotify_init1(IN_NONBLOCK);
765 if (sctpParams.inotifyFD == -1) {
766 mdclog_write(MDCLOG_ERR, "Failed to init inotify (inotify_init1) %s", strerror(errno));
770 sctpParams.inotifyWD = inotify_add_watch(sctpParams.inotifyFD,
771 (const char *)sctpParams.configFilePath.c_str(),
772 (unsigned)IN_OPEN | (unsigned)IN_CLOSE_WRITE | (unsigned)IN_CLOSE_NOWRITE); //IN_CLOSE = (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)
773 if (sctpParams.inotifyWD == -1) {
774 mdclog_write(MDCLOG_ERR, "Failed to add directory : %s to inotify (inotify_add_watch) %s",
775 sctpParams.configFilePath.c_str(),
777 close(sctpParams.inotifyFD);
781 struct epoll_event event{};
782 event.events = (EPOLLIN);
783 event.data.fd = sctpParams.inotifyFD;
784 // add listening RMR FD to epoll
785 if (epoll_ctl(sctpParams.epoll_fd, EPOLL_CTL_ADD, sctpParams.inotifyFD, &event)) {
786 mdclog_write(MDCLOG_ERR, "Failed to add inotify FD to epoll");
787 close(sctpParams.inotifyFD);
798 void listener(sctp_params_t *params) {
799 int num_of_SCTP_messages = 0;
800 auto totalTime = 0.0;
801 std::thread::id this_id = std::this_thread::get_id();
803 auto pod_name = std::getenv("POD_NAME");
804 auto container_name = std::getenv("CONTAINER_NAME");
805 auto service_name = std::getenv("SERVICE_NAME");
806 auto host_name = std::getenv("HOST_NAME");
807 auto system_name = std::getenv("SYSTEM_NAME");
808 auto pid = std::to_string(getpid()).c_str();
809 streambuf *oldCout = cout.rdbuf();
810 ostringstream memCout;
812 cout.rdbuf(memCout.rdbuf());
814 //return to the normal cout
818 memcpy(tid, memCout.str().c_str(), memCout.str().length() < 32 ? memCout.str().length() : 31);
819 tid[memCout.str().length()] = 0;
820 mdclog_mdc_add("SYSTEM_NAME", system_name);
821 mdclog_mdc_add("HOST_NAME", host_name);
822 mdclog_mdc_add("SERVICE_NAME", service_name);
823 mdclog_mdc_add("CONTAINER_NAME", container_name);
824 mdclog_mdc_add("POD_NAME", pod_name);
825 mdclog_mdc_add("PID", pid);
827 if (mdclog_level_get() >= MDCLOG_DEBUG) {
828 mdclog_write(MDCLOG_DEBUG, "started thread number %s", tid);
831 RmrMessagesBuffer_t rmrMessageBuffer{};
832 //create and init RMR
833 rmrMessageBuffer.rmrCtx = params->rmrCtx;
835 auto *events = (struct epoll_event *) calloc(MAXEVENTS, sizeof(struct epoll_event));
836 struct timespec end{0, 0};
837 struct timespec start{0, 0};
839 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
840 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
842 memcpy(rmrMessageBuffer.ka_message, params->ka_message, params->ka_message_length);
843 rmrMessageBuffer.ka_message_len = params->ka_message_length;
844 rmrMessageBuffer.ka_message[rmrMessageBuffer.ka_message_len] = 0;
846 if (mdclog_level_get() >= MDCLOG_DEBUG) {
847 mdclog_write(MDCLOG_DEBUG, "keep alive message is : %s", rmrMessageBuffer.ka_message);
850 ReportingMessages_t message {};
852 // for (int i = 0; i < MAX_RMR_BUFF_ARRAY; i++) {
853 // rmrMessageBuffer.rcvBufferedMessages[i] = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
854 // rmrMessageBuffer.sendBufferedMessages[i] = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
858 if (mdclog_level_get() >= MDCLOG_DEBUG) {
859 mdclog_write(MDCLOG_DEBUG, "Start EPOLL Wait. Timeout = %d", params->epollTimeOut);
862 auto numOfEvents = epoll_wait(params->epoll_fd, events, MAXEVENTS, params->epollTimeOut);
864 auto numOfEvents = 1;
866 if (numOfEvents == 0) { // time out
867 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
868 if (mdclog_level_get() >= MDCLOG_DEBUG) {
869 mdclog_write(MDCLOG_DEBUG, "got epoll timeout");
872 } else if (numOfEvents < 0) {
873 if (errno == EINTR) {
874 if (mdclog_level_get() >= MDCLOG_DEBUG) {
875 mdclog_write(MDCLOG_DEBUG, "got EINTR : %s", strerror(errno));
879 mdclog_write(MDCLOG_ERR, "Epoll wait failed, errno = %s", strerror(errno));
888 for (auto i = 0; i < numOfEvents; i++) {
889 if (mdclog_level_get() >= MDCLOG_DEBUG) {
890 mdclog_write(MDCLOG_DEBUG, "handling epoll event %d out of %d", i + 1, numOfEvents);
892 clock_gettime(CLOCK_MONOTONIC, &message.message.time);
893 start.tv_sec = message.message.time.tv_sec;
894 start.tv_nsec = message.message.time.tv_nsec;
897 if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP)) {
898 handlepoll_error(events[i], message, rmrMessageBuffer, params);
899 } else if (events[i].events & EPOLLOUT) {
900 handleEinprogressMessages(events[i], message, rmrMessageBuffer, params);
901 } else if (params->listenFD == events[i].data.fd) {
902 if (mdclog_level_get() >= MDCLOG_INFO) {
903 mdclog_write(MDCLOG_INFO, "New connection request from sctp network\n");
905 // new connection is requested from RAN start build connection
907 struct sockaddr in_addr {};
909 char hostBuff[NI_MAXHOST];
910 char portBuff[NI_MAXSERV];
912 in_len = sizeof(in_addr);
913 auto *peerInfo = (ConnectedCU_t *)calloc(1, sizeof(ConnectedCU_t));
914 if(peerInfo == nullptr){
915 mdclog_write(MDCLOG_ERR, "calloc failed");
918 peerInfo->sctpParams = params;
919 peerInfo->fileDescriptor = accept(params->listenFD, &in_addr, &in_len);
920 if (peerInfo->fileDescriptor == -1) {
921 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
922 if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
923 /* We have processed all incoming connections. */
934 mdclog_write(MDCLOG_ERR, "Accept error, errno = %s", strerror(errno));
938 if (setSocketNoBlocking(peerInfo->fileDescriptor) == -1) {
939 mdclog_write(MDCLOG_ERR, "setSocketNoBlocking failed to set new connection %s on port %s\n", hostBuff, portBuff);
940 close(peerInfo->fileDescriptor);
948 struct sctp_event_subscribe sctpevents;
949 memset( (void *)&sctpevents, 0, sizeof(sctpevents) );
950 sctpevents.sctp_data_io_event = 1;
951 setsockopt(peerInfo->fileDescriptor, SOL_SCTP, SCTP_EVENTS,(const void *)&sctpevents, sizeof(sctpevents) );
953 auto ans = getnameinfo(&in_addr, in_len,
954 peerInfo->hostName, NI_MAXHOST,
955 peerInfo->portNumber, NI_MAXSERV, (unsigned )((unsigned int)NI_NUMERICHOST | (unsigned int)NI_NUMERICSERV));
957 mdclog_write(MDCLOG_ERR, "Failed to get info on connection request. %s\n", strerror(errno));
958 close(peerInfo->fileDescriptor);
965 if (mdclog_level_get() >= MDCLOG_DEBUG) {
966 mdclog_write(MDCLOG_DEBUG, "Accepted connection on descriptor %d (host=%s, port=%s)\n", peerInfo->fileDescriptor, peerInfo->hostName, peerInfo->portNumber);
968 peerInfo->isConnected = false;
969 peerInfo->gotSetup = false;
970 if (addToEpoll(params->epoll_fd,
973 params->sctpMap, nullptr,
983 } else if (params->rmrListenFd == events[i].data.fd) {
984 // got message from XAPP
985 //num_of_XAPP_messages.fetch_add(1, std::memory_order_release);
986 num_of_messages.fetch_add(1, std::memory_order_release);
987 if (mdclog_level_get() >= MDCLOG_DEBUG) {
988 mdclog_write(MDCLOG_DEBUG, "new RMR message");
990 if (receiveXappMessages(params->sctpMap,
992 message.message.time) != 0) {
993 mdclog_write(MDCLOG_ERR, "Error handling Xapp message");
995 } else if (params->inotifyFD == events[i].data.fd) {
996 mdclog_write(MDCLOG_INFO, "Got event from inotify (configuration update)");
997 handleConfigChange(params);
999 /* We RMR_ERR_RETRY have data on the fd waiting to be read. Read and display it.
1000 * We must read whatever data is available completely, as we are running
1001 * in edge-triggered mode and won't get a notification again for the same data. */
1002 num_of_messages.fetch_add(1, std::memory_order_release);
1003 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1004 mdclog_write(MDCLOG_DEBUG, "new message from SCTP, epoll flags are : %0x", events[i].events);
1006 receiveDataFromSctp(&events[i],
1008 num_of_SCTP_messages,
1010 message.message.time);
1013 clock_gettime(CLOCK_MONOTONIC, &end);
1014 if (mdclog_level_get() >= MDCLOG_INFO) {
1015 totalTime += ((end.tv_sec + 1.0e-9 * end.tv_nsec) -
1016 ((double) start.tv_sec + 1.0e-9 * start.tv_nsec));
1018 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1019 mdclog_write(MDCLOG_DEBUG, "message handling is %ld seconds %ld nanoseconds",
1020 end.tv_sec - start.tv_sec,
1021 end.tv_nsec - start.tv_nsec);
1034 void handleConfigChange(sctp_params_t *sctpParams) {
1035 char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event))));
1036 const struct inotify_event *event;
1039 struct inotify_event tmpEvent;
1041 path p = (sctpParams->configFilePath + "/" + sctpParams->configFileName).c_str();
1042 auto endlessLoop = true;
1043 while (endlessLoop) {
1044 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
1045 auto len = read(sctpParams->inotifyFD, buf, sizeof buf);
1050 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
1051 if (errno != EAGAIN) {
1052 mdclog_write(MDCLOG_ERR, "read %s ", strerror(errno));
1053 endlessLoop = false;
1057 endlessLoop = false;
1063 for (ptr = buf; ptr < buf + len; ptr += sizeof(struct inotify_event) + event->len) {
1065 event = (const struct inotify_event *)ptr;
1067 tmpEvent.mask = (uint32_t)IN_CLOSE_WRITE;
1070 if (event->mask & (uint32_t)IN_ISDIR) {
1074 // the directory name
1075 if (sctpParams->inotifyWD == event->wd) {
1076 // not the directory
1079 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
1080 auto retVal = strcmp(sctpParams->configFileName.c_str(), event->name);
1086 // only the file we want
1087 if (event->mask & (uint32_t)IN_CLOSE_WRITE) {
1088 if (mdclog_level_get() >= MDCLOG_INFO) {
1089 mdclog_write(MDCLOG_INFO, "Configuration file changed");
1092 const int size = 2048;
1093 auto fileSize = file_size(p);
1094 if (fileSize > size) {
1095 mdclog_write(MDCLOG_ERR, "File %s larger than %d", p.string().c_str(), size);
1099 mdclog_write(MDCLOG_ERR, "Configuration File %s not exists", p.string().c_str());
1103 ReadConfigFile conf;
1104 if (conf.openConfigFile(p.string()) == -1) {
1105 mdclog_write(MDCLOG_ERR, "Filed to open config file %s, %s",
1106 p.string().c_str(), strerror(errno));
1109 auto tmpStr = conf.getStringValue("trace");
1110 if (tmpStr.length() == 0) {
1111 mdclog_write(MDCLOG_ERR, "illegal trace. Set trace to stop");
1115 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
1116 if ((tmpStr.compare("start")) == 0) {
1117 mdclog_write(MDCLOG_INFO, "Trace set to: start");
1118 sctpParams->trace = true;
1119 } else if ((tmpStr.compare("stop")) == 0) {
1120 mdclog_write(MDCLOG_INFO, "Trace set to: stop");
1121 sctpParams->trace = false;
1123 mdclog_write(MDCLOG_ERR, "Trace was set to wrong value %s, set to stop", tmpStr.c_str());
1124 sctpParams->trace = false;
1126 jsonTrace = sctpParams->trace;
1129 endlessLoop = false;
1142 * @param rmrMessageBuffer
1145 void handleEinprogressMessages(struct epoll_event &event,
1146 ReportingMessages_t &message,
1147 RmrMessagesBuffer_t &rmrMessageBuffer,
1148 sctp_params_t *params) {
1149 auto *peerInfo = (ConnectedCU_t *)event.data.ptr;
1150 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
1152 mdclog_write(MDCLOG_INFO, "file descriptor %d got EPOLLOUT", peerInfo->fileDescriptor);
1154 socklen_t retValLen = 0;
1155 auto rc = getsockopt(peerInfo->fileDescriptor, SOL_SOCKET, SO_ERROR, &retVal, &retValLen);
1156 if (rc != 0 || retVal != 0) {
1157 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
1159 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
1160 "%s|Failed SCTP Connection, after EINPROGRESS the getsockopt%s",
1161 peerInfo->enodbName, strerror(errno));
1162 } else if (retVal != 0) {
1163 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
1164 "%s|Failed SCTP Connection after EINPROGRESS, SO_ERROR",
1165 peerInfo->enodbName);
1168 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1169 message.message.asnLength = rmrMessageBuffer.sendMessage->len;
1170 mdclog_write(MDCLOG_ERR, "%s", rmrMessageBuffer.sendMessage->payload);
1171 message.message.direction = 'N';
1172 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
1173 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
1176 memset(peerInfo->asnData, 0, peerInfo->asnLength);
1177 peerInfo->asnLength = 0;
1178 peerInfo->mtype = 0;
1181 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
1182 peerInfo->isConnected = true;
1184 if (modifyToEpoll(params->epoll_fd, peerInfo, (EPOLLIN | EPOLLET), params->sctpMap, peerInfo->enodbName,
1185 peerInfo->mtype) != 0) {
1186 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_MOD");
1190 message.message.asndata = (unsigned char *)peerInfo->asnData;
1191 message.message.asnLength = peerInfo->asnLength;
1192 message.message.messageType = peerInfo->mtype;
1193 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
1194 num_of_messages.fetch_add(1, std::memory_order_release);
1195 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1196 mdclog_write(MDCLOG_DEBUG, "send the delayed SETUP/ENDC SETUP to sctp for %s",
1197 message.message.enodbName);
1199 if (sendSctpMsg(peerInfo, message, params->sctpMap) != 0) {
1200 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1201 mdclog_write(MDCLOG_DEBUG, "Error write to SCTP %s %d", __func__, __LINE__);
1206 memset(peerInfo->asnData, 0, peerInfo->asnLength);
1207 peerInfo->asnLength = 0;
1208 peerInfo->mtype = 0;
1213 void handlepoll_error(struct epoll_event &event,
1214 ReportingMessages_t &message,
1215 RmrMessagesBuffer_t &rmrMessageBuffer,
1216 sctp_params_t *params) {
1217 if ((event.data.fd != params->rmrListenFd) && (event.data.ptr != nullptr)) {
1218 auto *peerInfo = (ConnectedCU_t *)event.data.ptr;
1219 mdclog_write(MDCLOG_ERR, "epoll error, events %0x on fd %d, RAN NAME : %s",
1220 event.events, peerInfo->fileDescriptor, peerInfo->enodbName);
1221 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
1222 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
1223 "%s|Failed SCTP Connection",
1224 peerInfo->enodbName);
1225 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1226 message.message.asnLength = rmrMessageBuffer.sendMessage->len;
1228 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
1229 message.message.direction = 'N';
1230 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
1231 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
1234 close(peerInfo->fileDescriptor);
1235 //params->sctpMap->erase(peerInfo->enodbName);
1236 cleanHashEntry((ConnectedCU_t *) event.data.ptr, params->sctpMap);
1238 mdclog_write(MDCLOG_ERR, "epoll error, events %0x on RMR FD", event.events);
1246 int setSocketNoBlocking(int socket) {
1247 auto flags = fcntl(socket, F_GETFL, 0);
1250 mdclog_write(MDCLOG_ERR, "%s, %s", __FUNCTION__, strerror(errno));
1254 flags = (unsigned) flags | (unsigned) O_NONBLOCK;
1255 if (fcntl(socket, F_SETFL, flags) == -1) {
1256 mdclog_write(MDCLOG_ERR, "%s, %s", __FUNCTION__, strerror(errno));
1268 void cleanHashEntry(ConnectedCU_t *val, Sctp_Map_t *m) {
1269 if(val != nullptr) {
1271 auto port = (uint16_t) strtol(val->portNumber, &dummy, 10);
1272 char searchBuff[2048]{};
1274 snprintf(searchBuff, sizeof searchBuff, "host:%s:%d", val->hostName, port);
1275 if(m->find(searchBuff)) {
1276 m->erase(searchBuff);
1279 if(m->find(val->enodbName)) {
1280 mdclog_write(MDCLOG_DEBUG, "remove key enodbName = %s from %s at line %d", val->enodbName, __FUNCTION__, __LINE__);
1281 m->erase(val->enodbName);
1294 * @param fd file descriptor
1295 * @param data the asn data to send
1296 * @param len length of the data
1297 * @param enodbName the enodbName as in the map for printing purpose
1298 * @param m map host information
1299 * @param mtype message number
1300 * @return 0 success, a negative number on fail
1302 int sendSctpMsg(ConnectedCU_t *peerInfo, ReportingMessages_t &message, Sctp_Map_t *m) {
1303 auto loglevel = mdclog_level_get();
1305 int fd = peerInfo->fileDescriptor;
1306 int streamId = fetchStreamId(peerInfo,message);
1308 int fd = FILE_DESCRIPTOR;
1311 if (loglevel >= MDCLOG_DEBUG) {
1312 mdclog_write(MDCLOG_DEBUG, "Send SCTP message for CU %s, %s",
1313 message.message.enodbName, __FUNCTION__);
1317 if (sctp_sendmsg(fd,message.message.asndata, message.message.asnLength,(struct sockaddr *) NULL, 0, htonl(E2AP_PPID), 0,streamId,0,0) < 0) {
1318 if (errno == EINTR) {
1321 mdclog_write(MDCLOG_ERR, "error writing to CU a message, %s ", strerror(errno));
1322 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
1323 if (!peerInfo->isConnected) {
1324 mdclog_write(MDCLOG_ERR, "connection to CU %s is still in progress.", message.message.enodbName);
1329 cleanHashEntry(peerInfo, m);
1332 char key[MAX_ENODB_NAME_SIZE * 2];
1333 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", message.message.enodbName,
1334 message.message.messageType);
1335 if (loglevel >= MDCLOG_DEBUG) {
1336 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
1338 auto tmp = m->find(key);
1348 message.message.direction = 'D';
1349 // send report.buffer of size
1350 buildJsonMessage(message);
1352 if (loglevel >= MDCLOG_DEBUG) {
1353 mdclog_write(MDCLOG_DEBUG,
1354 "SCTP message for CU %s sent from %s",
1355 message.message.enodbName,
1365 * @param rmrMessageBuffer
1367 void getRequestMetaData(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
1368 message.message.asndata = rmrMessageBuffer.rcvMessage->payload;
1369 message.message.asnLength = rmrMessageBuffer.rcvMessage->len;
1371 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1372 mdclog_write(MDCLOG_DEBUG, "Message from Xapp RAN name = %s message length = %ld",
1373 message.message.enodbName, (unsigned long) message.message.asnLength);
1383 * @param numOfMessages
1384 * @param rmrMessageBuffer
1388 int receiveDataFromSctp(struct epoll_event *events,
1389 Sctp_Map_t *sctpMap,
1391 RmrMessagesBuffer_t &rmrMessageBuffer,
1392 struct timespec &ts) {
1393 /* We have data on the fd waiting to be read. Read and display it.
1394 * We must read whatever data is available completely, as we are running
1395 * in edge-triggered mode and won't get a notification again for the same data. */
1396 ReportingMessages_t message {};
1398 auto loglevel = mdclog_level_get();
1399 struct sctp_sndrcvinfo sndrcvinfo;
1403 // get the identity of the interface
1404 if (events->data.ptr != nullptr){
1405 message.peerInfo = (ConnectedCU_t *)events->data.ptr;
1408 struct timespec start{0, 0};
1409 struct timespec decodeStart{0, 0};
1410 struct timespec end{0, 0};
1412 E2AP_PDU_t *pdu = nullptr;
1415 if (loglevel >= MDCLOG_DEBUG) {
1416 mdclog_write(MDCLOG_DEBUG, "Start Read from SCTP %d fd", message.peerInfo->fileDescriptor);
1417 clock_gettime(CLOCK_MONOTONIC, &start);
1419 // read the buffer directly to rmr payload
1420 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1422 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
1423 sctp_recvmsg(message.peerInfo->fileDescriptor, rmrMessageBuffer.sendMessage->payload, RECEIVE_SCTP_BUFFER_SIZE,(struct sockaddr *) NULL, 0, &sndrcvinfo, &flags);
1424 mdclog_write(MDCLOG_DEBUG, "Start Read from SCTP fd %d stream %d ", message.peerInfo->fileDescriptor, sndrcvinfo.sinfo_stream);
1425 streamId = sndrcvinfo.sinfo_stream;
1427 message.message.asnLength = rmrMessageBuffer.sendMessage->len;
1431 if (loglevel >= MDCLOG_DEBUG) {
1432 mdclog_write(MDCLOG_DEBUG, "Finish Read from SCTP %d fd message length = %ld",
1433 message.peerInfo->fileDescriptor, message.message.asnLength);
1436 memcpy(message.message.enodbName, message.peerInfo->enodbName, sizeof(message.peerInfo->enodbName));
1437 message.message.direction = 'U';
1438 message.message.time.tv_nsec = ts.tv_nsec;
1439 message.message.time.tv_sec = ts.tv_sec;
1441 if (message.message.asnLength < 0) {
1442 if (errno == EINTR) {
1445 /* If errno == EAGAIN, that means we have read all
1446 data. So goReportingMessages_t back to the main loop. */
1447 if (errno != EAGAIN) {
1448 mdclog_write(MDCLOG_ERR, "Read error, %s ", strerror(errno));
1450 } else if (loglevel >= MDCLOG_DEBUG) {
1451 mdclog_write(MDCLOG_DEBUG, "EAGAIN - descriptor = %d", message.peerInfo->fileDescriptor);
1454 } else if (message.message.asnLength == 0) {
1455 /* End of file. The remote has closed the connection. */
1456 if (loglevel >= MDCLOG_INFO) {
1457 mdclog_write(MDCLOG_INFO, "END of File Closed connection - descriptor = %d",
1458 message.peerInfo->fileDescriptor);
1464 if (loglevel >= MDCLOG_DEBUG) {
1465 char printBuffer[RECEIVE_SCTP_BUFFER_SIZE]{};
1466 char *tmp = printBuffer;
1467 for (size_t i = 0; i < (size_t)message.message.asnLength; ++i) {
1468 snprintf(tmp, 3, "%02x", message.message.asndata[i]);
1471 printBuffer[message.message.asnLength] = 0;
1472 clock_gettime(CLOCK_MONOTONIC, &end);
1473 mdclog_write(MDCLOG_DEBUG, "Before Encoding E2AP PDU for : %s, Read time is : %ld seconds, %ld nanoseconds",
1474 message.peerInfo->enodbName, end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec);
1475 mdclog_write(MDCLOG_DEBUG, "PDU buffer length = %ld, data = : %s", message.message.asnLength,
1477 clock_gettime(CLOCK_MONOTONIC, &decodeStart);
1480 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, (void **) &pdu,
1481 message.message.asndata, message.message.asnLength);
1483 asn_dec_rval_t rval = {RC_OK, 0};
1484 pdu = (E2AP_PDU_t*)rmrMessageBuffer.sendMessage->tp_buf;
1486 if (rval.code != RC_OK) {
1487 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2AP PDU from RAN : %s", rval.code,
1488 message.peerInfo->enodbName);
1489 if (pdu != nullptr) {
1490 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
1496 if (loglevel >= MDCLOG_DEBUG) {
1497 clock_gettime(CLOCK_MONOTONIC, &end);
1498 mdclog_write(MDCLOG_DEBUG, "After Encoding E2AP PDU for : %s, Read time is : %ld seconds, %ld nanoseconds",
1499 message.peerInfo->enodbName, end.tv_sec - decodeStart.tv_sec, end.tv_nsec - decodeStart.tv_nsec);
1502 FILE *stream = open_memstream(&printBuffer, &size);
1503 asn_fprint(stream, &asn_DEF_E2AP_PDU, pdu);
1504 mdclog_write(MDCLOG_DEBUG, "Encoding E2AP PDU past : %s", printBuffer);
1505 clock_gettime(CLOCK_MONOTONIC, &decodeStart);
1511 switch (pdu->present) {
1512 case E2AP_PDU_PR_initiatingMessage: {//initiating message
1513 asnInitiatingRequest(pdu, sctpMap,message, rmrMessageBuffer, streamId);
1516 case E2AP_PDU_PR_successfulOutcome: { //successful outcome
1517 asnSuccessfulMsg(pdu, sctpMap, message, rmrMessageBuffer);
1520 case E2AP_PDU_PR_unsuccessfulOutcome: { //Unsuccessful Outcome
1521 asnUnSuccsesfulMsg(pdu, sctpMap, message, rmrMessageBuffer);
1525 mdclog_write(MDCLOG_ERR, "Unknown index %d in E2AP PDU", pdu->present);
1528 if (loglevel >= MDCLOG_DEBUG) {
1529 clock_gettime(CLOCK_MONOTONIC, &end);
1530 mdclog_write(MDCLOG_DEBUG,
1531 "After processing message and sent to rmr for : %s, Read time is : %ld seconds, %ld nanoseconds",
1532 message.peerInfo->enodbName, end.tv_sec - decodeStart.tv_sec, end.tv_nsec - decodeStart.tv_nsec);
1536 if (pdu != nullptr) {
1537 // ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, pdu); /* With reset we were not freeing the memory and was causing the leak here. */
1538 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
1548 if (loglevel >= MDCLOG_INFO) {
1549 mdclog_write(MDCLOG_INFO, "Closed connection - descriptor = %d", message.peerInfo->fileDescriptor);
1551 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
1552 snprintf((char *)rmrMessageBuffer.sendMessage->payload,
1554 "%s|CU disconnected unexpectedly",
1555 message.peerInfo->enodbName);
1556 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1557 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
1558 if (sendRequestToXapp(message,
1559 RIC_SCTP_CONNECTION_FAILURE,
1560 rmrMessageBuffer) != 0) {
1561 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
1565 /* Closing descriptor make epoll remove it from the set of descriptors which are monitored. */
1567 pthread_mutex_lock(&thread_lock);
1568 if (fcntl(message.peerInfo->fileDescriptor, F_GETFD) != -1) {
1569 mdclog_write(MDCLOG_DEBUG, "Closing connection - descriptor = %d", message.peerInfo->fileDescriptor);
1570 close(message.peerInfo->fileDescriptor);
1571 cleanHashEntry((ConnectedCU_t *) events->data.ptr, sctpMap);
1573 pthread_mutex_unlock(&thread_lock);
1575 close(message.peerInfo->fileDescriptor);
1576 cleanHashEntry((ConnectedCU_t *) events->data.ptr, sctpMap);
1579 if (loglevel >= MDCLOG_DEBUG) {
1580 clock_gettime(CLOCK_MONOTONIC, &end);
1581 mdclog_write(MDCLOG_DEBUG, "from receive SCTP to send RMR time is %ld seconds and %ld nanoseconds",
1582 end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec);
1588 static void buildAndSendSetupRequest(ReportingMessages_t &message,
1589 RmrMessagesBuffer_t &rmrMessageBuffer,
1591 string const &messageName,
1592 string const &ieName,
1593 vector<string> &functionsToAdd_v,
1594 vector<string> &functionsToModified_v*/) {
1595 auto logLevel = mdclog_level_get();
1596 // now we can send the data to e2Mgr
1599 auto buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
1600 unsigned char *buffer = nullptr;
1601 buffer = (unsigned char *) calloc(buffer_size, sizeof(unsigned char));
1604 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
1605 mdclog_write(MDCLOG_ERR, "Allocating buffer for %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1610 er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu, buffer, buffer_size);
1611 if (er.encoded == -1) {
1612 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
1613 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1616 } else if (er.encoded > (ssize_t) buffer_size) {
1617 buffer_size = er.encoded + 128;
1618 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
1619 mdclog_write(MDCLOG_WARN, "Buffer of size %d is to small for %s. Reallocate buffer of size %d",
1621 asn_DEF_E2AP_PDU.name, buffer_size);
1622 buffer_size = er.encoded + 128;
1624 unsigned char *newBuffer = nullptr;
1625 newBuffer = (unsigned char *) realloc(buffer, buffer_size);
1629 mdclog_write(MDCLOG_ERR, "Reallocating buffer for %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1638 buffer[er.encoded] = '\0';
1643 string res((char *)buffer);
1644 res.erase(std::remove(res.begin(), res.end(), '\n'), res.end());
1645 res.erase(std::remove(res.begin(), res.end(), '\t'), res.end());
1646 res.erase(std::remove(res.begin(), res.end(), ' '), res.end());
1649 // if (!functionsToAdd_v.empty() || !functionsToModified_v.empty()) {
1650 // res = buildXmlData(messageName, ieName, functionsToAdd_v, functionsToModified_v, buffer, (size_t) er.encoded);
1653 // if (res.length() == 0) {
1654 // rmrMsg = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, buffer_size + 256);
1655 // rmrMsg->len = snprintf((char *) rmrMsg->payload, RECEIVE_SCTP_BUFFER_SIZE * 2, "%s:%d|%s",
1656 // message.peerInfo->sctpParams->myIP.c_str(),
1657 // message.peerInfo->sctpParams->rmrPort,
1660 rmrMsg = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, (int)res.length() + 256);
1661 rmrMsg->len = snprintf((char *) rmrMsg->payload, res.length() + 256, "%s:%d|%s",
1662 message.peerInfo->sctpParams->myIP.c_str(),
1663 message.peerInfo->sctpParams->rmrPort,
1667 if (logLevel >= MDCLOG_DEBUG) {
1668 mdclog_write(MDCLOG_DEBUG, "Setup request of size %d :\n %s\n", rmrMsg->len, rmrMsg->payload);
1671 rmrMsg->mtype = message.message.messageType;
1673 rmr_bytes2meid(rmrMsg, (unsigned char *) message.message.enodbName, strlen(message.message.enodbName));
1675 static unsigned char tx[32];
1676 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1677 rmr_bytes2xact(rmrMsg, tx, strlen((const char *) tx));
1679 rmrMsg = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMsg);
1681 if (rmrMsg == nullptr) {
1682 mdclog_write(MDCLOG_ERR, "RMR failed to send returned nullptr");
1683 } else if (rmrMsg->state != 0) {
1684 char meid[RMR_MAX_MEID]{};
1685 if (rmrMsg->state == RMR_ERR_RETRY) {
1688 mdclog_write(MDCLOG_INFO, "RETRY sending Message %d to Xapp from %s",
1689 rmrMsg->mtype, rmr_get_meid(rmrMsg, (unsigned char *) meid));
1691 rmrMsg = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMsg);
1693 if (rmrMsg == nullptr) {
1694 mdclog_write(MDCLOG_ERR, "RMR failed send returned nullptr");
1695 } else if (rmrMsg->state != 0) {
1696 mdclog_write(MDCLOG_ERR,
1697 "RMR Retry failed %s sending request %d to Xapp from %s",
1698 translateRmrErrorMessages(rmrMsg->state).c_str(),
1700 rmr_get_meid(rmrMsg, (unsigned char *) meid));
1703 mdclog_write(MDCLOG_ERR, "RMR failed: %s. sending request %d to Xapp from %s",
1704 translateRmrErrorMessages(rmrMsg->state).c_str(),
1706 rmr_get_meid(rmrMsg, (unsigned char *) meid));
1709 message.peerInfo->gotSetup = true;
1710 buildJsonMessage(message);
1712 if (rmrMsg != nullptr) {
1713 rmr_free_msg(rmrMsg);
1722 int RAN_Function_list_To_Vector(RANfunctions_List_t& list, vector <string> &runFunXML_v) {
1724 runFunXML_v.clear();
1725 for (auto j = 0; j < list.list.count; j++) {
1726 auto *raNfunctionItemIEs = (RANfunction_ItemIEs_t *)list.list.array[j];
1727 if (raNfunctionItemIEs->id == ProtocolIE_ID_id_RANfunction_Item &&
1728 (raNfunctionItemIEs->value.present == RANfunction_ItemIEs__value_PR_RANfunction_Item)) {
1730 E2SM_gNB_NRT_RANfunction_Definition_t *ranFunDef = nullptr;
1731 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER,
1732 &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition,
1733 (void **)&ranFunDef,
1734 raNfunctionItemIEs->value.choice.RANfunction_Item.ranFunctionDefinition.buf,
1735 raNfunctionItemIEs->value.choice.RANfunction_Item.ranFunctionDefinition.size);
1736 if (rval.code != RC_OK) {
1737 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2SM message from : %s",
1739 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name);
1743 auto xml_buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
1744 unsigned char xml_buffer[RECEIVE_SCTP_BUFFER_SIZE * 2];
1745 memset(xml_buffer, 0, RECEIVE_SCTP_BUFFER_SIZE * 2);
1747 auto er = asn_encode_to_buffer(nullptr,
1749 &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition,
1753 if (er.encoded == -1) {
1754 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s",
1755 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name,
1757 } else if (er.encoded > (ssize_t)xml_buffer_size) {
1758 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
1759 (int) xml_buffer_size,
1760 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name, __func__, __LINE__);
1762 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1763 mdclog_write(MDCLOG_DEBUG, "Encoding E2SM %s PDU number %d : %s",
1764 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name,
1769 string runFuncs = (char *)(xml_buffer);
1770 runFunXML_v.emplace_back(runFuncs);
1777 int collectServiceUpdate_RequestData(E2AP_PDU_t *pdu,
1778 Sctp_Map_t *sctpMap,
1779 ReportingMessages_t &message,
1780 vector <string> &RANfunctionsAdded_v,
1781 vector <string> &RANfunctionsModified_v) {
1782 memset(message.peerInfo->enodbName, 0 , MAX_ENODB_NAME_SIZE);
1783 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICserviceUpdate.protocolIEs.list.count; i++) {
1784 auto *ie = pdu->choice.initiatingMessage->value.choice.RICserviceUpdate.protocolIEs.list.array[i];
1785 if (ie->id == ProtocolIE_ID_id_RANfunctionsAdded) {
1786 if (ie->value.present == RICserviceUpdate_IEs__value_PR_RANfunctionsID_List) {
1787 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1788 mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1789 ie->value.choice.RANfunctions_List.list.count);
1791 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsAdded_v) != 0 ) {
1795 } else if (ie->id == ProtocolIE_ID_id_RANfunctionsModified) {
1796 if (ie->value.present == RICserviceUpdate_IEs__value_PR_RANfunctions_List) {
1797 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1798 mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1799 ie->value.choice.RANfunctions_List.list.count);
1801 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsModified_v) != 0 ) {
1807 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1808 mdclog_write(MDCLOG_DEBUG, "Run function vector have %ld entries",
1809 RANfunctionsAdded_v.size());
1817 void buildE2TPrometheusCounters(sctp_params_t &sctpParams) {
1818 sctpParams.e2tCounters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &sctpParams.prometheusFamily->Add({{"counter", "SetupRequestMsgs"}});
1819 sctpParams.e2tCounters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &sctpParams.prometheusFamily->Add({{"counter", "SetupRequestBytes"}});
1821 sctpParams.e2tCounters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &sctpParams.prometheusFamily->Add({{"counter", "SetupResponseMsgs"}});
1822 sctpParams.e2tCounters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &sctpParams.prometheusFamily->Add({{"counter", "SetupResponseBytes"}});
1824 sctpParams.e2tCounters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_E2setup] = &sctpParams.prometheusFamily->Add({{"counter", "SetupRequestFailureMsgs"}});
1825 sctpParams.e2tCounters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_E2setup] = &sctpParams.prometheusFamily->Add({{"counter", "SetupRequestFailureBytes"}});
1827 sctpParams.e2tCounters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_E2nodeConfigurationUpdate)] = &sctpParams.prometheusFamily->Add({{"counter", "E2NodeConfigUpdateMsgs"}});
1828 sctpParams.e2tCounters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_E2nodeConfigurationUpdate)] = &sctpParams.prometheusFamily->Add({{"counter", "E2NodeConfigUpdateBytes"}});
1830 sctpParams.e2tCounters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_E2nodeConfigurationUpdate)] = &sctpParams.prometheusFamily->Add({{"counter", "E2NodeConfigUpdateResponseMsgs"}});
1831 sctpParams.e2tCounters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2nodeConfigurationUpdate)] = &sctpParams.prometheusFamily->Add({{"counter", "E2NodeConfigUpdateResponseBytes"}});
1833 sctpParams.e2tCounters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate] = &sctpParams.prometheusFamily->Add({{"counter", "E2NodeConfigUpdateFailureMsgs"}});
1834 sctpParams.e2tCounters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate] = &sctpParams.prometheusFamily->Add({{"counter", "E2NodeConfigUpdateFailureBytes"}});
1836 sctpParams.e2tCounters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_ErrorIndication)] = &sctpParams.prometheusFamily->Add({{"counter", "ErrorIndicationMsgs"}});
1837 sctpParams.e2tCounters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_ErrorIndication)] = &sctpParams.prometheusFamily->Add({{"counter", "ErrorIndicationBytes"}});
1839 sctpParams.e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_Reset] = &sctpParams.prometheusFamily->Add({{"counter", "ResetRequestMsgs"}});
1840 sctpParams.e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_Reset] = &sctpParams.prometheusFamily->Add({{"counter", "ResetRequestBytes"}});
1842 sctpParams.e2tCounters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_Reset] = &sctpParams.prometheusFamily->Add({{"counter", "ResetAckMsgs"}});
1843 sctpParams.e2tCounters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_Reset] = &sctpParams.prometheusFamily->Add({{"counter", "ResetAckBytes"}});
1845 sctpParams.e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate] = &sctpParams.prometheusFamily->Add({{"counter", "RICServiceUpdateMsgs"}});
1846 sctpParams.e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate] = &sctpParams.prometheusFamily->Add({{"counter", "RICServiceUpdateBytes"}});
1848 sctpParams.e2tCounters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate] = &sctpParams.prometheusFamily->Add({{"counter", "RICServiceUpdateRespMsgs"}});
1849 sctpParams.e2tCounters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate] = &sctpParams.prometheusFamily->Add({{"counter", "RICServiceUpdateRespBytes"}});
1851 sctpParams.e2tCounters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate] = &sctpParams.prometheusFamily->Add({{"counter", "RICServiceUpdateFailureMsgs"}});
1852 sctpParams.e2tCounters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate] = &sctpParams.prometheusFamily->Add({{"counter", "RICServiceUpdateFailureBytes"}});
1854 sctpParams.e2tCounters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICcontrol] = &sctpParams.prometheusFamily->Add({{"counter", "RICControlMsgs"}});
1855 sctpParams.e2tCounters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICcontrol] = &sctpParams.prometheusFamily->Add({{"counter", "RICControlBytes"}});
1857 sctpParams.e2tCounters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol] = &sctpParams.prometheusFamily->Add({{"counter", "RICControlAckMsgs"}});
1858 sctpParams.e2tCounters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol] = &sctpParams.prometheusFamily->Add({{"counter", "RICControlAckBytes"}});
1860 sctpParams.e2tCounters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol] = &sctpParams.prometheusFamily->Add({{"counter", "RICControlFailureMsgs"}});
1861 sctpParams.e2tCounters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol] = &sctpParams.prometheusFamily->Add({{"counter", "RICControlFailureBytes"}});
1863 sctpParams.e2tCounters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscription] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionMsgs"}});
1864 sctpParams.e2tCounters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscription] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionBytes"}});
1866 sctpParams.e2tCounters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionAckMsgs"}});
1867 sctpParams.e2tCounters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionAckBytes"}});
1869 sctpParams.e2tCounters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionFailureMsgs"}});
1870 sctpParams.e2tCounters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionFailureBytes"}});
1872 sctpParams.e2tCounters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionDeleteMsgs"}});
1873 sctpParams.e2tCounters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionDeleteBytes"}});
1875 sctpParams.e2tCounters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionDeleteAckMsgs"}});
1876 sctpParams.e2tCounters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionDeleteAckBytes"}});
1878 sctpParams.e2tCounters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionDeleteFailMsgs"}});
1879 sctpParams.e2tCounters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionDeleteFailBytes"}});
1881 sctpParams.e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICindication] = &sctpParams.prometheusFamily->Add({{"counter", "RICIndicationMsgs"}});
1882 sctpParams.e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICindication] = &sctpParams.prometheusFamily->Add({{"counter", "RICIndicationBytes"}});
1884 sctpParams.e2tCounters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceQuery] = &sctpParams.prometheusFamily->Add({{"counter", "RICServiceQueryMsgs"}});
1885 sctpParams.e2tCounters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceQuery] = &sctpParams.prometheusFamily->Add({{"counter", "RICServiceQueryBytes"}});
1887 sctpParams.e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDeleteRequired] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionDeleteRequiredMsgs"}});
1888 sctpParams.e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDeleteRequired] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionDeleteRequiredBytes"}});
1892 void buildPrometheusList(ConnectedCU_t *peerInfo, Family<Counter> *prometheusFamily) {
1893 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"SetupRequest", "Messages"}});
1894 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"SetupRequest", "Bytes"}});
1896 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_E2nodeConfigurationUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"E2NodeConfigUpdate", "Messages"}});
1897 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_E2nodeConfigurationUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"E2NodeConfigUpdate", "Bytes"}});
1899 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ErrorIndication", "Messages"}});
1900 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ErrorIndication", "Bytes"}});
1902 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_RICindication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICindication", "Messages"}});
1903 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_RICindication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICindication", "Bytes"}});
1905 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetRequest", "Messages"}});
1906 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetRequest", "Bytes"}});
1908 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICserviceUpdate", "Messages"}});
1909 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICserviceUpdate", "Bytes"}});
1910 // ---------------------------------------------
1911 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetACK", "Messages"}});
1912 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetACK", "Bytes"}});
1914 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolACK", "Messages"}});
1915 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolACK", "Bytes"}});
1917 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionACK", "Messages"}});
1918 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionACK", "Bytes"}});
1920 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteACK", "Messages"}});
1921 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteACK", "Bytes"}});
1922 //-------------------------------------------------------------
1924 peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolFailure", "Messages"}});
1925 peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolFailure", "Bytes"}});
1927 peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionFailure", "Messages"}});
1928 peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionFailure", "Bytes"}});
1930 peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteFailure", "Messages"}});
1931 peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteFailure", "Bytes"}});
1933 //====================================================================================
1934 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ErrorIndication", "Messages"}});
1935 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ErrorIndication", "Bytes"}});
1937 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetRequest", "Messages"}});
1938 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetRequest", "Bytes"}});
1940 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICcontrol", "Messages"}});
1941 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICcontrol", "Bytes"}});
1943 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICserviceQuery)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceQuery", "Messages"}});
1944 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICserviceQuery)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceQuery", "Bytes"}});
1946 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscription", "Messages"}});
1947 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscription", "Bytes"}});
1949 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscriptionDelete", "Messages"}});
1950 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscriptionDelete", "Bytes"}});
1951 //---------------------------------------------------------------------------------------------------------
1952 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupResponse", "Messages"}});
1953 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupResponse", "Bytes"}});
1955 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_E2nodeConfigurationUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"E2NodeConfigUpdateSuccess", "Messages"}});
1956 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2nodeConfigurationUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"E2NodeConfigUpdateSuccess", "Bytes"}});
1958 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetACK", "Messages"}});
1959 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetACK", "Bytes"}});
1961 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateResponse", "Messages"}});
1962 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateResponse", "Bytes"}});
1963 //----------------------------------------------------------------------------------------------------------------
1964 peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupRequestFailure", "Messages"}});
1965 peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupRequestFailure", "Bytes"}});
1967 peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_E2nodeConfigurationUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"E2NodeConfigUpdateFailure", "Messages"}});
1968 peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2nodeConfigurationUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"E2NodeConfigUpdateFailure", "Bytes"}});
1970 peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateFailure", "Messages"}});
1971 peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateFailure", "Bytes"}});
1974 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDeleteRequired)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteRequired", "Messages"}});
1975 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDeleteRequired)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteRequired", "Bytes"}});
1984 * @param RANfunctionsAdded_v
1987 int collectSetupRequestData(E2AP_PDU_t *pdu,
1988 Sctp_Map_t *sctpMap,
1989 ReportingMessages_t &message /*, vector <string> &RANfunctionsAdded_v*/) {
1990 memset(message.peerInfo->enodbName, 0 , MAX_ENODB_NAME_SIZE);
1991 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.count; i++) {
1992 auto *ie = pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.array[i];
1993 if (ie->id == ProtocolIE_ID_id_GlobalE2node_ID) {
1994 // get the ran name for meid
1995 if (ie->value.present == E2setupRequestIEs__value_PR_GlobalE2node_ID) {
1996 if (buildRanName(message.peerInfo->enodbName, ie) < 0) {
1997 mdclog_write(MDCLOG_ERR, "Bad param in E2setupRequestIEs GlobalE2node_ID.\n");
1998 // no message will be sent
2002 memcpy(message.message.enodbName, message.peerInfo->enodbName, strlen(message.peerInfo->enodbName));
2003 sctpMap->setkey(message.message.enodbName, message.peerInfo);
2005 } /*else if (ie->id == ProtocolIE_ID_id_RANfunctionsAdded) {
2006 if (ie->value.present == E2setupRequestIEs__value_PR_RANfunctions_List) {
2007 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2008 mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
2009 ie->value.choice.RANfunctions_List.list.count);
2011 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsAdded_v) != 0 ) {
2017 // if (mdclog_level_get() >= MDCLOG_DEBUG) {
2018 // mdclog_write(MDCLOG_DEBUG, "Run function vector have %ld entries",
2019 // RANfunctionsAdded_v.size());
2024 int XML_From_PER(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
2025 E2AP_PDU_t *pdu = nullptr;
2027 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2028 mdclog_write(MDCLOG_DEBUG, "got PER message of size %d is:%s",
2029 rmrMessageBuffer.sendMessage->len, rmrMessageBuffer.sendMessage->payload);
2031 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, (void **) &pdu,
2032 rmrMessageBuffer.sendMessage->payload, rmrMessageBuffer.sendMessage->len);
2033 if (rval.code != RC_OK) {
2034 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) setup response from E2MGR : %s",
2036 message.message.enodbName);
2037 if (pdu != nullptr) {
2038 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
2044 int buff_size = RECEIVE_XAPP_BUFFER_SIZE;
2045 auto er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu,
2046 rmrMessageBuffer.sendMessage->payload, buff_size);
2047 if (er.encoded == -1) {
2048 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
2049 if (pdu != nullptr) {
2050 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
2054 } else if (er.encoded > (ssize_t)buff_size) {
2055 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
2056 (int)rmrMessageBuffer.sendMessage->len,
2057 asn_DEF_E2AP_PDU.name,
2060 if (pdu != nullptr) {
2061 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
2066 rmrMessageBuffer.sendMessage->len = er.encoded;
2067 if (pdu != nullptr) {
2068 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
2079 * @param rmrMessageBuffer
2081 void asnInitiatingRequest(E2AP_PDU_t *pdu,
2082 Sctp_Map_t *sctpMap,
2083 ReportingMessages_t &message,
2084 RmrMessagesBuffer_t &rmrMessageBuffer, int streamId) {
2085 auto logLevel = mdclog_level_get();
2086 auto procedureCode = ((InitiatingMessage_t *) pdu->choice.initiatingMessage)->procedureCode;
2087 if (logLevel >= MDCLOG_DEBUG) {
2088 mdclog_write(MDCLOG_DEBUG, "Initiating message %ld\n", procedureCode);
2090 switch (procedureCode) {
2091 case ProcedureCode_id_E2setup: {
2092 if (logLevel >= MDCLOG_DEBUG) {
2093 mdclog_write(MDCLOG_DEBUG, "Got E2setup");
2096 // vector <string> RANfunctionsAdded_v;
2097 // vector <string> RANfunctionsModified_v;
2098 // RANfunctionsAdded_v.clear();
2099 // RANfunctionsModified_v.clear();
2100 if (collectSetupRequestData(pdu, sctpMap, message) != 0) {
2103 struct sctp_status status;
2104 int stat_size = sizeof(status);
2105 getsockopt( message.peerInfo->fileDescriptor, SOL_SCTP, SCTP_STATUS,(void *)&status, (socklen_t *)&stat_size );
2106 if (logLevel >= MDCLOG_DEBUG) {
2107 mdclog_write(MDCLOG_DEBUG, "Start from SCTP %d fd", message.peerInfo->fileDescriptor);
2108 mdclog_write(MDCLOG_DEBUG, "SCTP status assoc id %d instrms %d outstrms %d", status.sstat_assoc_id,
2109 status.sstat_instrms, status.sstat_outstrms);
2111 if(status.sstat_outstrms == 1 || status.sstat_instrms == 1)
2113 message.peerInfo->isSingleStream = true;
2114 message.peerInfo->singleStreamId = streamId;
2115 if (status.sstat_outstrms == 1 && status.sstat_instrms == 1){
2116 if (logLevel >= MDCLOG_DEBUG) {
2117 mdclog_write(MDCLOG_DEBUG, "Single SCTP stream is used for sending from now on, assoc id %d streamId %d #instrms %d #outstrms %d, %s",status.sstat_assoc_id, streamId, status.sstat_instrms, status.sstat_outstrms, __FUNCTION__);
2121 mdclog_write(MDCLOG_ERR, "Single SCTP stream used for sending messages even if there is a mismatch in number of in & out streams, assoc id %d instrms %d outstrms %d", status.sstat_assoc_id,
2122 status.sstat_instrms, status.sstat_outstrms);
2126 buildPrometheusList(message.peerInfo, message.peerInfo->sctpParams->prometheusFamily);
2128 string messageName("E2setupRequest");
2129 string ieName("E2setupRequestIEs");
2130 message.message.messageType = RIC_E2_SETUP_REQ;
2131 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2132 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment((double)message.message.asnLength);
2134 // Update E2T instance level metrics
2135 message.peerInfo->sctpParams->e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2136 message.peerInfo->sctpParams->e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment((double)message.message.asnLength);
2138 buildAndSendSetupRequest(message, rmrMessageBuffer, pdu);
2141 case ProcedureCode_id_RICserviceUpdate: {
2142 if (logLevel >= MDCLOG_DEBUG) {
2143 mdclog_write(MDCLOG_DEBUG, "Got RICserviceUpdate %s", message.message.enodbName);
2145 // vector <string> RANfunctionsAdded_v;
2146 // vector <string> RANfunctionsModified_v;
2147 // RANfunctionsAdded_v.clear();
2148 // RANfunctionsModified_v.clear();
2149 // if (collectServiceUpdate_RequestData(pdu, sctpMap, message,
2150 // RANfunctionsAdded_v, RANfunctionsModified_v) != 0) {
2154 string messageName("RICserviceUpdate");
2155 string ieName("RICserviceUpdateIEs");
2156 message.message.messageType = RIC_SERVICE_UPDATE;
2157 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2158 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
2159 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment((double)message.message.asnLength);
2161 // Update E2T instance level metrics
2162 message.peerInfo->sctpParams->e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
2163 message.peerInfo->sctpParams->e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment((double)message.message.asnLength);
2165 buildAndSendSetupRequest(message, rmrMessageBuffer, pdu);
2169 case ProcedureCode_id_E2nodeConfigurationUpdate: {
2170 if (logLevel >= MDCLOG_DEBUG) {
2171 mdclog_write(MDCLOG_DEBUG, "Got E2nodeConfigurationUpdate %s", message.message.enodbName);
2174 string messageName("RICE2nodeConfigurationUpdate");
2175 string ieName("RICE2nodeConfigurationUpdateIEs");
2176 message.message.messageType = RIC_E2NODE_CONFIG_UPDATE;
2177 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2178 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment();
2179 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment((double)message.message.asnLength);
2181 // Update E2T instance level metrics
2182 message.peerInfo->sctpParams->e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment();
2183 message.peerInfo->sctpParams->e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment((double)message.message.asnLength);
2185 buildAndSendSetupRequest(message, rmrMessageBuffer, pdu);
2189 case ProcedureCode_id_ErrorIndication: {
2190 if (logLevel >= MDCLOG_DEBUG) {
2191 mdclog_write(MDCLOG_DEBUG, "Got ErrorIndication %s", message.message.enodbName);
2193 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2194 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_ErrorIndication]->Increment();
2195 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_ErrorIndication]->Increment((double)message.message.asnLength);
2197 // Update E2T instance level metrics
2198 message.peerInfo->sctpParams->e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_ErrorIndication]->Increment();
2199 message.peerInfo->sctpParams->e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_ErrorIndication]->Increment((double)message.message.asnLength);
2201 if (sendRequestToXapp(message, RIC_ERROR_INDICATION, rmrMessageBuffer) != 0) {
2202 mdclog_write(MDCLOG_ERR, "RIC_ERROR_INDICATION failed to send to xAPP");
2206 case ProcedureCode_id_Reset: {
2207 if (logLevel >= MDCLOG_DEBUG) {
2208 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
2210 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2211 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2212 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment((double)message.message.asnLength);
2214 // Update E2T instance level metrics
2215 message.peerInfo->sctpParams->e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2216 message.peerInfo->sctpParams->e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment((double)message.message.asnLength);
2218 if (XML_From_PER(message, rmrMessageBuffer) < 0) {
2222 if (sendRequestToXapp(message, RIC_E2_RESET_REQ, rmrMessageBuffer) != 0) {
2223 mdclog_write(MDCLOG_ERR, "RIC_E2_RESET_REQ message failed to send to xAPP");
2227 case ProcedureCode_id_RICindication: {
2228 if (logLevel >= MDCLOG_DEBUG) {
2229 mdclog_write(MDCLOG_DEBUG, "Got RICindication %s", message.message.enodbName);
2231 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.count; i++) {
2232 auto messageSent = false;
2233 RICindication_IEs_t *ie = pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.array[i];
2234 if (logLevel >= MDCLOG_DEBUG) {
2235 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
2237 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
2238 if (logLevel >= MDCLOG_DEBUG) {
2239 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
2241 if (ie->value.present == RICindication_IEs__value_PR_RICrequestID) {
2242 static unsigned char tx[32];
2243 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_INDICATION;
2244 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2245 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
2246 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
2247 (unsigned char *)message.message.enodbName,
2248 strlen(message.message.enodbName));
2249 rmrMessageBuffer.sendMessage->state = 0;
2250 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
2252 //ie->value.choice.RICrequestID.ricInstanceID;
2253 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2254 mdclog_write(MDCLOG_DEBUG, "sub id = %d, mtype = %d, ric instance id %ld, requestor id = %ld",
2255 rmrMessageBuffer.sendMessage->sub_id,
2256 rmrMessageBuffer.sendMessage->mtype,
2257 ie->value.choice.RICrequestID.ricInstanceID,
2258 ie->value.choice.RICrequestID.ricRequestorID);
2260 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2261 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICindication]->Increment();
2262 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICindication]->Increment((double)message.message.asnLength);
2264 // Update E2T instance level metrics
2265 message.peerInfo->sctpParams->e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICindication]->Increment();
2266 message.peerInfo->sctpParams->e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICindication]->Increment((double)message.message.asnLength);
2268 sendRmrMessage(rmrMessageBuffer, message);
2271 mdclog_write(MDCLOG_ERR, "RIC request id missing illegal request");
2280 case ProcedureCode_id_RICsubscriptionDeleteRequired: {
2281 if (logLevel >= MDCLOG_DEBUG) {
2282 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDeleteRequired %s", message.message.enodbName);
2284 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2285 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDeleteRequired]->Increment();
2286 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDeleteRequired]->Increment((double)message.message.asnLength);
2288 // Update E2T instance level metrics
2289 message.peerInfo->sctpParams->e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDeleteRequired]->Increment();
2290 message.peerInfo->sctpParams->e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDeleteRequired]->Increment((double)message.message.asnLength);
2292 if (sendRequestToXapp(message, RIC_SUB_DEL_REQUIRED, rmrMessageBuffer) != 0) {
2293 mdclog_write(MDCLOG_ERR, "Subscription Delete Required message failed to send to xAPP");
2299 mdclog_write(MDCLOG_ERR, "Undefined or not supported message = %ld", procedureCode);
2300 message.message.messageType = 0; // no RMR message type yet
2302 buildJsonMessage(message);
2313 * @param rmrMessageBuffer
2315 void asnSuccessfulMsg(E2AP_PDU_t *pdu,
2316 Sctp_Map_t *sctpMap,
2317 ReportingMessages_t &message,
2318 RmrMessagesBuffer_t &rmrMessageBuffer) {
2319 auto procedureCode = pdu->choice.successfulOutcome->procedureCode;
2320 auto logLevel = mdclog_level_get();
2321 if (logLevel >= MDCLOG_INFO) {
2322 mdclog_write(MDCLOG_INFO, "Successful Outcome %ld", procedureCode);
2324 switch (procedureCode) {
2325 case ProcedureCode_id_Reset: {
2326 if (logLevel >= MDCLOG_DEBUG) {
2327 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
2329 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2330 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2331 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment((double)message.message.asnLength);
2333 // Update E2T instance level metrics
2334 message.peerInfo->sctpParams->e2tCounters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2335 message.peerInfo->sctpParams->e2tCounters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment((double)message.message.asnLength);
2337 if (XML_From_PER(message, rmrMessageBuffer) < 0) {
2340 if (sendRequestToXapp(message, RIC_E2_RESET_RESP, rmrMessageBuffer) != 0) {
2341 mdclog_write(MDCLOG_ERR, "RIC_E2_RESET_RESP message failed to send to xAPP");
2345 case ProcedureCode_id_RICcontrol: {
2346 if (logLevel >= MDCLOG_DEBUG) {
2347 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
2350 i < pdu->choice.successfulOutcome->value.choice.RICcontrolAcknowledge.protocolIEs.list.count; i++) {
2351 auto messageSent = false;
2352 RICcontrolAcknowledge_IEs_t *ie = pdu->choice.successfulOutcome->value.choice.RICcontrolAcknowledge.protocolIEs.list.array[i];
2353 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2354 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
2356 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
2357 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2358 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
2360 if (ie->value.present == RICcontrolAcknowledge_IEs__value_PR_RICrequestID) {
2361 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_CONTROL_ACK;
2362 rmrMessageBuffer.sendMessage->state = 0;
2363 // rmrMessageBuffer.sendMessage->sub_id = (int) ie->value.choice.RICrequestID.ricRequestorID;
2364 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
2366 static unsigned char tx[32];
2367 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2368 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
2369 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
2370 (unsigned char *)message.message.enodbName,
2371 strlen(message.message.enodbName));
2372 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2373 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
2374 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment((double)message.message.asnLength);
2376 // Update E2T instance level metrics
2377 message.peerInfo->sctpParams->e2tCounters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
2378 message.peerInfo->sctpParams->e2tCounters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment((double)message.message.asnLength);
2380 sendRmrMessage(rmrMessageBuffer, message);
2383 mdclog_write(MDCLOG_ERR, "RIC request id missing illegal request");
2393 case ProcedureCode_id_RICsubscription: {
2394 if (logLevel >= MDCLOG_DEBUG) {
2395 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
2397 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2398 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
2399 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment((double)message.message.asnLength);
2401 // Update E2T instance level metrics
2402 message.peerInfo->sctpParams->e2tCounters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
2403 message.peerInfo->sctpParams->e2tCounters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment((double)message.message.asnLength);
2405 if (sendRequestToXapp(message, RIC_SUB_RESP, rmrMessageBuffer) != 0) {
2406 mdclog_write(MDCLOG_ERR, "Subscription successful message failed to send to xAPP");
2410 case ProcedureCode_id_RICsubscriptionDelete: {
2411 if (logLevel >= MDCLOG_DEBUG) {
2412 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
2414 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2415 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
2416 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment((double)message.message.asnLength);
2418 // Update E2T instance level metrics
2419 message.peerInfo->sctpParams->e2tCounters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
2420 message.peerInfo->sctpParams->e2tCounters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment((double)message.message.asnLength);
2422 if (sendRequestToXapp(message, RIC_SUB_DEL_RESP, rmrMessageBuffer) != 0) {
2423 mdclog_write(MDCLOG_ERR, "Subscription delete successful message failed to send to xAPP");
2428 mdclog_write(MDCLOG_WARN, "Undefined or not supported message = %ld", procedureCode);
2429 message.message.messageType = 0; // no RMR message type yet
2430 buildJsonMessage(message);
2441 * @param rmrMessageBuffer
2443 void asnUnSuccsesfulMsg(E2AP_PDU_t *pdu,
2444 Sctp_Map_t *sctpMap,
2445 ReportingMessages_t &message,
2446 RmrMessagesBuffer_t &rmrMessageBuffer) {
2447 auto procedureCode = pdu->choice.unsuccessfulOutcome->procedureCode;
2448 auto logLevel = mdclog_level_get();
2449 if (logLevel >= MDCLOG_INFO) {
2450 mdclog_write(MDCLOG_INFO, "Unsuccessful Outcome %ld", procedureCode);
2452 switch (procedureCode) {
2453 case ProcedureCode_id_RICcontrol: {
2454 if (logLevel >= MDCLOG_DEBUG) {
2455 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
2458 i < pdu->choice.unsuccessfulOutcome->value.choice.RICcontrolFailure.protocolIEs.list.count; i++) {
2459 auto messageSent = false;
2460 RICcontrolFailure_IEs_t *ie = pdu->choice.unsuccessfulOutcome->value.choice.RICcontrolFailure.protocolIEs.list.array[i];
2461 if (logLevel >= MDCLOG_DEBUG) {
2462 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
2464 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
2465 if (logLevel >= MDCLOG_DEBUG) {
2466 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
2468 if (ie->value.present == RICcontrolFailure_IEs__value_PR_RICrequestID) {
2469 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_CONTROL_FAILURE;
2470 rmrMessageBuffer.sendMessage->state = 0;
2471 // rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricRequestorID;
2472 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
2473 static unsigned char tx[32];
2474 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2475 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
2476 rmr_bytes2meid(rmrMessageBuffer.sendMessage, (unsigned char *) message.message.enodbName,
2477 strlen(message.message.enodbName));
2478 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2479 message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
2480 message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment((double)message.message.asnLength);
2482 // Update E2T instance level metrics
2483 message.peerInfo->sctpParams->e2tCounters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
2484 message.peerInfo->sctpParams->e2tCounters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment((double)message.message.asnLength);
2486 sendRmrMessage(rmrMessageBuffer, message);
2489 mdclog_write(MDCLOG_ERR, "RIC request id missing illegal request");
2498 case ProcedureCode_id_RICsubscription: {
2499 if (logLevel >= MDCLOG_DEBUG) {
2500 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
2502 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2503 message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
2504 message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment((double)message.message.asnLength);
2506 // Update E2T instance level metrics
2507 message.peerInfo->sctpParams->e2tCounters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
2508 message.peerInfo->sctpParams->e2tCounters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment((double)message.message.asnLength);
2510 if (sendRequestToXapp(message, RIC_SUB_FAILURE, rmrMessageBuffer) != 0) {
2511 mdclog_write(MDCLOG_ERR, "Subscription unsuccessful message failed to send to xAPP");
2515 case ProcedureCode_id_RICsubscriptionDelete: {
2516 if (logLevel >= MDCLOG_DEBUG) {
2517 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
2519 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2520 message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
2521 message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment((double)message.message.asnLength);
2523 // Update E2T instance level metrics
2524 message.peerInfo->sctpParams->e2tCounters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
2525 message.peerInfo->sctpParams->e2tCounters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment((double)message.message.asnLength);
2527 if (sendRequestToXapp(message, RIC_SUB_FAILURE, rmrMessageBuffer) != 0) {
2528 mdclog_write(MDCLOG_ERR, "Subscription Delete unsuccessful message failed to send to xAPP");
2533 mdclog_write(MDCLOG_WARN, "Undefined or not supported message = %ld", procedureCode);
2534 message.message.messageType = 0; // no RMR message type yet
2535 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2536 buildJsonMessage(message);
2547 * @param rmrMmessageBuffer
2550 int sendRequestToXapp(ReportingMessages_t &message,
2552 RmrMessagesBuffer_t &rmrMmessageBuffer) {
2553 rmr_bytes2meid(rmrMmessageBuffer.sendMessage,
2554 (unsigned char *)message.message.enodbName,
2555 strlen(message.message.enodbName));
2556 message.message.messageType = rmrMmessageBuffer.sendMessage->mtype = requestId;
2557 rmrMmessageBuffer.sendMessage->state = 0;
2558 static unsigned char tx[32];
2559 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2560 rmr_bytes2xact(rmrMmessageBuffer.sendMessage, tx, strlen((const char *) tx));
2562 auto rc = sendRmrMessage(rmrMmessageBuffer, message);
2568 * @param pSctpParams
2570 void getRmrContext(sctp_params_t &pSctpParams) {
2571 pSctpParams.rmrCtx = nullptr;
2572 pSctpParams.rmrCtx = rmr_init(pSctpParams.rmrAddress, RECEIVE_XAPP_BUFFER_SIZE, RMRFL_NONE);
2573 if (pSctpParams.rmrCtx == nullptr) {
2574 mdclog_write(MDCLOG_ERR, "Failed to initialize RMR");
2578 rmr_set_stimeout(pSctpParams.rmrCtx, 0); // disable retries for any send operation
2579 // we need to find that routing table exist and we can run
2580 if (mdclog_level_get() >= MDCLOG_INFO) {
2581 mdclog_write(MDCLOG_INFO, "We are after RMR INIT wait for RMR_Ready");
2586 if ((rmrReady = rmr_ready(pSctpParams.rmrCtx)) == 0) {
2590 if (count % 60 == 0) {
2591 mdclog_write(MDCLOG_INFO, "waiting to RMR ready state for %d seconds", count);
2594 if (mdclog_level_get() >= MDCLOG_INFO) {
2595 mdclog_write(MDCLOG_INFO, "RMR running");
2597 rmr_init_trace(pSctpParams.rmrCtx, 200);
2598 // get the RMR fd for the epoll
2599 pSctpParams.rmrListenFd = rmr_get_rcvfd(pSctpParams.rmrCtx);
2600 struct epoll_event event{};
2601 // add RMR fd to epoll
2602 event.events = (EPOLLIN);
2603 event.data.fd = pSctpParams.rmrListenFd;
2604 // add listening RMR FD to epoll
2605 if (epoll_ctl(pSctpParams.epoll_fd, EPOLL_CTL_ADD, pSctpParams.rmrListenFd, &event)) {
2606 mdclog_write(MDCLOG_ERR, "Failed to add RMR descriptor to epoll");
2607 close(pSctpParams.rmrListenFd);
2608 rmr_close(pSctpParams.rmrCtx);
2609 pSctpParams.rmrCtx = nullptr;
2616 * @param rmrMessageBuffer
2619 int PER_FromXML(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
2620 E2AP_PDU_t *pdu = nullptr;
2622 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2623 mdclog_write(MDCLOG_DEBUG, "got xml Format data from xApp of size %d is:%s",
2624 rmrMessageBuffer.rcvMessage->len, rmrMessageBuffer.rcvMessage->payload);
2626 auto rval = asn_decode(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, (void **) &pdu,
2627 rmrMessageBuffer.rcvMessage->payload, rmrMessageBuffer.rcvMessage->len);
2628 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2629 mdclog_write(MDCLOG_DEBUG, "%s After decoding the XML to PDU", __func__ );
2631 if (rval.code != RC_OK) {
2635 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) setup response from E2MGR : %s",
2637 message.message.enodbName);
2638 if (pdu != nullptr) {
2639 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
2645 int buff_size = RECEIVE_XAPP_BUFFER_SIZE;
2646 auto er = asn_encode_to_buffer(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, pdu,
2647 rmrMessageBuffer.rcvMessage->payload, buff_size);
2648 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2649 mdclog_write(MDCLOG_DEBUG, "%s After encoding PDU to PER", __func__ );
2651 if (er.encoded == -1) {
2652 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
2653 if (pdu != nullptr) {
2654 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
2658 } else if (er.encoded > (ssize_t)buff_size) {
2659 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
2660 (int)rmrMessageBuffer.rcvMessage->len,
2661 asn_DEF_E2AP_PDU.name,
2664 if (pdu != nullptr) {
2665 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
2670 rmrMessageBuffer.rcvMessage->len = er.encoded;
2671 if (pdu != nullptr) {
2672 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
2681 * @param rmrMessageBuffer
2685 int receiveXappMessages(Sctp_Map_t *sctpMap,
2686 RmrMessagesBuffer_t &rmrMessageBuffer,
2687 struct timespec &ts) {
2688 int loglevel = mdclog_level_get();
2689 if (rmrMessageBuffer.rcvMessage == nullptr) {
2691 mdclog_write(MDCLOG_ERR, "RMR Allocation message, %s", strerror(errno));
2695 // if (loglevel >= MDCLOG_DEBUG) {
2696 // mdclog_write(MDCLOG_DEBUG, "Call to rmr_rcv_msg");
2698 rmrMessageBuffer.rcvMessage = rmr_rcv_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.rcvMessage);
2699 if (rmrMessageBuffer.rcvMessage == nullptr) {
2700 mdclog_write(MDCLOG_ERR, "RMR Receiving message with null pointer, Reallocated rmr message buffer");
2701 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
2704 ReportingMessages_t message;
2705 message.message.direction = 'D';
2706 message.message.time.tv_nsec = ts.tv_nsec;
2707 message.message.time.tv_sec = ts.tv_sec;
2709 // get message payload
2710 //auto msgData = msg->payload;
2712 rmrMessageBuffer.rcvMessage->state = 0;
2714 if (rmrMessageBuffer.rcvMessage->state != 0) {
2715 mdclog_write(MDCLOG_ERR, "RMR Receiving message with stat = %d", rmrMessageBuffer.rcvMessage->state);
2718 rmr_get_meid(rmrMessageBuffer.rcvMessage, (unsigned char *)message.message.enodbName);
2719 message.peerInfo = (ConnectedCU_t *) sctpMap->find(message.message.enodbName);
2720 if (message.peerInfo == nullptr) {
2721 auto type = rmrMessageBuffer.rcvMessage->mtype;
2723 case RIC_SCTP_CLEAR_ALL:
2724 case E2_TERM_KEEP_ALIVE_REQ:
2725 case RIC_HEALTH_CHECK_REQ:
2731 mdclog_write(MDCLOG_ERR, "Failed to send message no CU entry %s", message.message.enodbName);
2736 if (rmrMessageBuffer.rcvMessage->mtype != RIC_HEALTH_CHECK_REQ) {
2737 num_of_XAPP_messages.fetch_add(1, std::memory_order_release);
2740 switch (rmrMessageBuffer.rcvMessage->mtype) {
2741 case RIC_E2_SETUP_RESP : {
2742 if (loglevel >= MDCLOG_DEBUG) {
2743 mdclog_write(MDCLOG_DEBUG, "RIC_E2_SETUP_RESP");
2745 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2748 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2749 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2750 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment(rmrMessageBuffer.rcvMessage->len);
2752 // Update E2T instance level metrics
2753 message.peerInfo->sctpParams->e2tCounters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2754 message.peerInfo->sctpParams->e2tCounters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment(rmrMessageBuffer.rcvMessage->len);
2756 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2757 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_SETUP_RESP");
2762 case RIC_E2_SETUP_FAILURE : {
2763 if (loglevel >= MDCLOG_DEBUG) {
2764 mdclog_write(MDCLOG_DEBUG, "RIC_E2_SETUP_FAILURE");
2766 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2769 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2770 message.peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2771 message.peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment(rmrMessageBuffer.rcvMessage->len);
2773 // Update E2T instance level metrics
2774 message.peerInfo->sctpParams->e2tCounters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2775 message.peerInfo->sctpParams->e2tCounters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment(rmrMessageBuffer.rcvMessage->len);
2777 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2778 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_SETUP_FAILURE");
2784 case RIC_E2NODE_CONFIG_UPDATE_ACK: {
2785 if (loglevel >= MDCLOG_DEBUG) {
2786 mdclog_write(MDCLOG_DEBUG, "RIC_E2NODE_CONFIG_UPDATE_ACK");
2788 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2791 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2792 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment();
2793 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
2795 // Update E2T instance level metrics
2796 message.peerInfo->sctpParams->e2tCounters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment();
2797 message.peerInfo->sctpParams->e2tCounters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
2799 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2800 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2NODE_CONFIG_UPDATE_ACK");
2806 case RIC_E2NODE_CONFIG_UPDATE_FAILURE: {
2807 if (loglevel >= MDCLOG_DEBUG) {
2808 mdclog_write(MDCLOG_DEBUG, "RIC_E2NODE_CONFIG_UPDATE_FAILURE");
2810 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2813 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2814 message.peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment();
2815 message.peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
2817 // Update E2T instance level metrics
2818 message.peerInfo->sctpParams->e2tCounters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment();
2819 message.peerInfo->sctpParams->e2tCounters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
2821 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2822 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2NODE_CONFIG_UPDATE_FAILURE");
2828 case RIC_ERROR_INDICATION: {
2829 if (loglevel >= MDCLOG_DEBUG) {
2830 mdclog_write(MDCLOG_DEBUG, "RIC_ERROR_INDICATION");
2832 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2833 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_ErrorIndication]->Increment();
2834 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_ErrorIndication]->Increment(rmrMessageBuffer.rcvMessage->len);
2836 // Update E2T instance level metrics
2837 message.peerInfo->sctpParams->e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_ErrorIndication]->Increment();
2838 message.peerInfo->sctpParams->e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_ErrorIndication]->Increment(rmrMessageBuffer.rcvMessage->len);
2840 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2841 mdclog_write(MDCLOG_ERR, "Failed to send RIC_ERROR_INDICATION");
2847 if (loglevel >= MDCLOG_DEBUG) {
2848 mdclog_write(MDCLOG_DEBUG, "RIC_SUB_REQ");
2850 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2851 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
2852 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment(rmrMessageBuffer.rcvMessage->len);
2854 // Update E2T instance level metrics
2855 message.peerInfo->sctpParams->e2tCounters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
2856 message.peerInfo->sctpParams->e2tCounters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment(rmrMessageBuffer.rcvMessage->len);
2858 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2859 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SUB_REQ");
2864 case RIC_SUB_DEL_REQ: {
2865 if (loglevel >= MDCLOG_DEBUG) {
2866 mdclog_write(MDCLOG_DEBUG, "RIC_SUB_DEL_REQ");
2868 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2869 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
2870 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment(rmrMessageBuffer.rcvMessage->len);
2872 // Update E2T instance level metrics
2873 message.peerInfo->sctpParams->e2tCounters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
2874 message.peerInfo->sctpParams->e2tCounters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment(rmrMessageBuffer.rcvMessage->len);
2876 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2877 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SUB_DEL_REQ");
2882 case RIC_CONTROL_REQ: {
2883 if (loglevel >= MDCLOG_DEBUG) {
2884 mdclog_write(MDCLOG_DEBUG, "RIC_CONTROL_REQ");
2886 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2887 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
2888 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment(rmrMessageBuffer.rcvMessage->len);
2890 // Update E2T instance level metrics
2891 message.peerInfo->sctpParams->e2tCounters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
2892 message.peerInfo->sctpParams->e2tCounters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment(rmrMessageBuffer.rcvMessage->len);
2894 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2895 mdclog_write(MDCLOG_ERR, "Failed to send RIC_CONTROL_REQ");
2900 case RIC_SERVICE_QUERY: {
2901 if (loglevel >= MDCLOG_DEBUG) {
2902 mdclog_write(MDCLOG_DEBUG, "RIC_SERVICE_QUERY");
2904 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2907 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2908 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceQuery]->Increment();
2909 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceQuery]->Increment(rmrMessageBuffer.rcvMessage->len);
2911 // Update E2T instance level metrics
2912 message.peerInfo->sctpParams->e2tCounters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceQuery]->Increment();
2913 message.peerInfo->sctpParams->e2tCounters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceQuery]->Increment(rmrMessageBuffer.rcvMessage->len);
2915 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2916 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_QUERY");
2921 case RIC_SERVICE_UPDATE_ACK: {
2922 if (loglevel >= MDCLOG_DEBUG) {
2923 mdclog_write(MDCLOG_DEBUG, "RIC_SERVICE_UPDATE_ACK");
2925 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2926 mdclog_write(MDCLOG_ERR, "error in PER_FromXML");
2929 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2930 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
2931 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
2933 // Update E2T instance level metrics
2934 message.peerInfo->sctpParams->e2tCounters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
2935 message.peerInfo->sctpParams->e2tCounters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
2937 if (loglevel >= MDCLOG_DEBUG) {
2938 mdclog_write(MDCLOG_DEBUG, "Before sending to CU");
2940 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2941 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_UPDATE_ACK");
2946 case RIC_SERVICE_UPDATE_FAILURE: {
2947 if (loglevel >= MDCLOG_DEBUG) {
2948 mdclog_write(MDCLOG_DEBUG, "RIC_SERVICE_UPDATE_FAILURE");
2950 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2953 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2954 message.peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
2955 message.peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
2957 // Update E2T instance level metrics
2958 message.peerInfo->sctpParams->e2tCounters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
2959 message.peerInfo->sctpParams->e2tCounters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
2961 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2962 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_UPDATE_FAILURE");
2967 case RIC_E2_RESET_REQ: {
2968 if (loglevel >= MDCLOG_DEBUG) {
2969 mdclog_write(MDCLOG_DEBUG, "RIC_E2_RESET_REQ");
2971 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2974 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2975 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2976 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment(rmrMessageBuffer.rcvMessage->len);
2978 // Update E2T instance level metrics
2979 message.peerInfo->sctpParams->e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2980 message.peerInfo->sctpParams->e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment(rmrMessageBuffer.rcvMessage->len);
2982 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2983 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_RESET");
2988 case RIC_E2_RESET_RESP: {
2989 if (loglevel >= MDCLOG_DEBUG) {
2990 mdclog_write(MDCLOG_DEBUG, "RIC_E2_RESET_RESP");
2992 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2995 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2996 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2997 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment(rmrMessageBuffer.rcvMessage->len);
2999 // Update E2T instance level metrics
3000 message.peerInfo->sctpParams->e2tCounters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
3001 message.peerInfo->sctpParams->e2tCounters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment(rmrMessageBuffer.rcvMessage->len);
3003 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
3004 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_RESET_RESP");
3009 case RIC_SCTP_CLEAR_ALL: {
3010 mdclog_write(MDCLOG_INFO, "RIC_SCTP_CLEAR_ALL");
3011 // loop on all keys and close socket and then erase all map.
3013 sctpMap->getKeys(v);
3014 for (auto const &iter : v) { //}; iter != sctpMap.end(); iter++) {
3015 if (!boost::starts_with((string) (iter), "host:") && !boost::starts_with((string) (iter), "msg:")) {
3016 auto *peerInfo = (ConnectedCU_t *) sctpMap->find(iter);
3017 if (peerInfo == nullptr) {
3020 close(peerInfo->fileDescriptor);
3021 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
3022 message.message.direction = 'D';
3023 message.message.time.tv_nsec = ts.tv_nsec;
3024 message.message.time.tv_sec = ts.tv_sec;
3026 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
3027 snprintf((char *)rmrMessageBuffer.sendMessage->payload,
3029 "%s|RIC_SCTP_CLEAR_ALL",
3030 peerInfo->enodbName);
3031 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
3032 mdclog_write(MDCLOG_INFO, "%s", message.message.asndata);
3033 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
3034 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
3045 case E2_TERM_KEEP_ALIVE_REQ: {
3046 // send message back
3047 rmr_bytes2payload(rmrMessageBuffer.sendMessage,
3048 (unsigned char *)rmrMessageBuffer.ka_message,
3049 rmrMessageBuffer.ka_message_len);
3050 rmrMessageBuffer.sendMessage->mtype = E2_TERM_KEEP_ALIVE_RESP;
3051 rmrMessageBuffer.sendMessage->state = 0;
3052 static unsigned char tx[32];
3053 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
3054 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, txLen);
3055 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
3056 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
3058 if (rmrMessageBuffer.sendMessage == nullptr) {
3059 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
3060 mdclog_write(MDCLOG_ERR, "Failed to send E2_TERM_KEEP_ALIVE_RESP RMR message returned NULL");
3061 } else if (rmrMessageBuffer.sendMessage->state != 0) {
3062 mdclog_write(MDCLOG_ERR, "Failed to send E2_TERM_KEEP_ALIVE_RESP, on RMR state = %d ( %s)",
3063 rmrMessageBuffer.sendMessage->state, translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str());
3064 } else if (loglevel >= MDCLOG_DEBUG) {
3065 mdclog_write(MDCLOG_DEBUG, "Got Keep Alive Request send : %s", rmrMessageBuffer.ka_message);
3070 case RIC_HEALTH_CHECK_REQ: {
3071 static int counter = 0;
3072 // send message back
3073 rmr_bytes2payload(rmrMessageBuffer.rcvMessage,
3074 (unsigned char *)"OK",
3076 rmrMessageBuffer.rcvMessage->mtype = RIC_HEALTH_CHECK_RESP;
3077 rmrMessageBuffer.rcvMessage->state = 0;
3078 static unsigned char tx[32];
3079 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
3080 rmr_bytes2xact(rmrMessageBuffer.rcvMessage, tx, txLen);
3081 rmrMessageBuffer.rcvMessage = rmr_rts_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.rcvMessage);
3082 //rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
3083 if (rmrMessageBuffer.rcvMessage == nullptr) {
3084 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
3085 mdclog_write(MDCLOG_ERR, "Failed to send RIC_HEALTH_CHECK_RESP RMR message returned NULL");
3086 } else if (rmrMessageBuffer.rcvMessage->state != 0) {
3087 mdclog_write(MDCLOG_ERR, "Failed to send RIC_HEALTH_CHECK_RESP, on RMR state = %d ( %s)",
3088 rmrMessageBuffer.rcvMessage->state, translateRmrErrorMessages(rmrMessageBuffer.rcvMessage->state).c_str());
3089 } else if (loglevel >= MDCLOG_DEBUG && (++counter % 100 == 0)) {
3090 mdclog_write(MDCLOG_DEBUG, "Got %d RIC_HEALTH_CHECK_REQ Request send : OK", counter);
3097 mdclog_write(MDCLOG_WARN, "Message Type : %d is not supported", rmrMessageBuffer.rcvMessage->mtype);
3098 message.message.asndata = rmrMessageBuffer.rcvMessage->payload;
3099 message.message.asnLength = rmrMessageBuffer.rcvMessage->len;
3100 message.message.time.tv_nsec = ts.tv_nsec;
3101 message.message.time.tv_sec = ts.tv_sec;
3102 message.message.messageType = rmrMessageBuffer.rcvMessage->mtype;
3104 buildJsonMessage(message);
3109 if (mdclog_level_get() >= MDCLOG_DEBUG) {
3110 mdclog_write(MDCLOG_DEBUG, "EXIT OK from %s", __FUNCTION__);
3116 * Send message to the CU that is not expecting for successful or unsuccessful results
3117 * @param messageBuffer
3119 * @param failedMsgId
3123 int sendDirectionalSctpMsg(RmrMessagesBuffer_t &messageBuffer,
3124 ReportingMessages_t &message,
3126 Sctp_Map_t *sctpMap) {
3127 if (mdclog_level_get() >= MDCLOG_DEBUG) {
3128 mdclog_write(MDCLOG_DEBUG, "send message: %d to %s address", message.message.messageType, message.message.enodbName);
3131 getRequestMetaData(message, messageBuffer);
3132 if (mdclog_level_get() >= MDCLOG_INFO) {
3133 mdclog_write(MDCLOG_INFO, "send message to %s address", message.message.enodbName);
3136 auto rc = sendMessagetoCu(sctpMap, messageBuffer, message, failedMsgId);
3143 * @param messageBuffer
3145 * @param failedMesgId
3148 int sendMessagetoCu(Sctp_Map_t *sctpMap,
3149 RmrMessagesBuffer_t &messageBuffer,
3150 ReportingMessages_t &message,
3153 message.message.messageType = messageBuffer.rcvMessage->mtype;
3154 auto rc = sendSctpMsg(message.peerInfo, message, sctpMap);
3169 int addToEpoll(int epoll_fd,
3170 ConnectedCU_t *peerInfo,
3172 Sctp_Map_t *sctpMap,
3176 struct epoll_event event{};
3177 event.data.ptr = peerInfo;
3178 event.events = events;
3179 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, peerInfo->fileDescriptor, &event) < 0) {
3180 #if !(defined(UNIT_TEST) || defined(MODULE_TEST))
3181 if (mdclog_level_get() >= MDCLOG_DEBUG) {
3182 mdclog_write(MDCLOG_DEBUG, "epoll_ctl EPOLL_CTL_ADD (may check not to quit here), %s, %s %d",
3183 strerror(errno), __func__, __LINE__);
3185 close(peerInfo->fileDescriptor);
3186 if (enodbName != nullptr) {
3187 cleanHashEntry(peerInfo, sctpMap);
3188 char key[MAX_ENODB_NAME_SIZE * 2];
3189 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", enodbName, msgType);
3190 if (mdclog_level_get() >= MDCLOG_DEBUG) {
3191 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
3193 auto tmp = sctpMap->find(key);
3197 sctpMap->erase(key);
3200 peerInfo->enodbName[0] = 0;
3202 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may check not to quit here)");
3219 int modifyToEpoll(int epoll_fd,
3220 ConnectedCU_t *peerInfo,
3222 Sctp_Map_t *sctpMap,
3226 struct epoll_event event{};
3227 event.data.ptr = peerInfo;
3228 event.events = events;
3229 if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, peerInfo->fileDescriptor, &event) < 0) {
3230 if (mdclog_level_get() >= MDCLOG_DEBUG) {
3231 mdclog_write(MDCLOG_DEBUG, "epoll_ctl EPOLL_CTL_MOD (may check not to quit here), %s, %s %d",
3232 strerror(errno), __func__, __LINE__);
3234 close(peerInfo->fileDescriptor);
3235 cleanHashEntry(peerInfo, sctpMap);
3236 char key[MAX_ENODB_NAME_SIZE * 2];
3237 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", enodbName, msgType);
3238 if (mdclog_level_get() >= MDCLOG_DEBUG) {
3239 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
3241 auto tmp = sctpMap->find(key);
3245 sctpMap->erase(key);
3247 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may check not to quit here)");
3254 int sendRmrMessage(RmrMessagesBuffer_t &rmrMessageBuffer, ReportingMessages_t &message) {
3255 buildJsonMessage(message);
3257 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
3259 rmrMessageBuffer.sendMessage->state = RMR_ERR_RETRY;
3261 if (rmrMessageBuffer.sendMessage == nullptr) {
3262 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
3263 mdclog_write(MDCLOG_ERR, "RMR failed send message returned with NULL pointer");
3267 if (rmrMessageBuffer.sendMessage->state != 0) {
3268 char meid[RMR_MAX_MEID]{};
3269 if (rmrMessageBuffer.sendMessage->state == RMR_ERR_RETRY) {
3271 rmrMessageBuffer.sendMessage->state = 0;
3272 mdclog_write(MDCLOG_INFO, "RETRY sending Message type %d to Xapp from %s",
3273 rmrMessageBuffer.sendMessage->mtype,
3274 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
3276 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
3278 if (rmrMessageBuffer.sendMessage == nullptr) {
3279 mdclog_write(MDCLOG_ERR, "RMR failed send message returned with NULL pointer");
3280 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
3282 } else if (rmrMessageBuffer.sendMessage->state != 0) {
3283 mdclog_write(MDCLOG_ERR,
3284 "Message state %s while sending request %d to Xapp from %s after retry of 10 microseconds",
3285 translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str(),
3286 rmrMessageBuffer.sendMessage->mtype,
3287 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
3288 auto rc = rmrMessageBuffer.sendMessage->state;
3292 mdclog_write(MDCLOG_ERR, "Message state %s while sending request %d to Xapp from %s",
3293 translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str(),
3294 rmrMessageBuffer.sendMessage->mtype,
3295 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
3296 return rmrMessageBuffer.sendMessage->state;
3302 void buildJsonMessage(ReportingMessages_t &message) {
3307 message.outLen = sizeof(message.base64Data);
3308 base64::encode((const unsigned char *) message.message.asndata,
3309 (const int) message.message.asnLength,
3312 if (mdclog_level_get() >= MDCLOG_DEBUG) {
3313 mdclog_write(MDCLOG_DEBUG, "Tracing: ASN length = %d, base64 message length = %d ",
3314 (int) message.message.asnLength,
3315 (int) message.outLen);
3318 snprintf(message.buffer, sizeof(message.buffer),
3319 "{\"header\": {\"ts\": \"%ld.%09ld\","
3320 "\"ranName\": \"%s\","
3321 "\"messageType\": %d,"
3322 "\"direction\": \"%c\"},"
3323 "\"base64Length\": %d,"
3324 "\"asnBase64\": \"%s\"}",
3325 message.message.time.tv_sec,
3326 message.message.time.tv_nsec,
3327 message.message.enodbName,
3328 message.message.messageType,
3329 message.message.direction,
3330 (int) message.outLen,
3331 message.base64Data);
3332 static src::logger_mt &lg = my_logger::get();
3334 BOOST_LOG(lg) << message.buffer;
3340 * take RMR error code to string
3344 string translateRmrErrorMessages(int state) {
3348 str = "RMR_OK - state is good";
3350 case RMR_ERR_BADARG:
3351 str = "RMR_ERR_BADARG - argument passed to function was unusable";
3353 case RMR_ERR_NOENDPT:
3354 str = "RMR_ERR_NOENDPT - send//call could not find an endpoint based on msg type";
3357 str = "RMR_ERR_EMPTY - msg received had no payload; attempt to send an empty message";
3360 str = "RMR_ERR_NOHDR - message didn't contain a valid header";
3362 case RMR_ERR_SENDFAILED:
3363 str = "RMR_ERR_SENDFAILED - send failed; errno has nano reason";
3365 case RMR_ERR_CALLFAILED:
3366 str = "RMR_ERR_CALLFAILED - unable to send call() message";
3368 case RMR_ERR_NOWHOPEN:
3369 str = "RMR_ERR_NOWHOPEN - no wormholes are open";
3372 str = "RMR_ERR_WHID - wormhole id was invalid";
3374 case RMR_ERR_OVERFLOW:
3375 str = "RMR_ERR_OVERFLOW - operation would have busted through a buffer/field size";
3378 str = "RMR_ERR_RETRY - request (send/call/rts) failed, but caller should retry (EAGAIN for wrappers)";
3380 case RMR_ERR_RCVFAILED:
3381 str = "RMR_ERR_RCVFAILED - receive failed (hard error)";
3383 case RMR_ERR_TIMEOUT:
3384 str = "RMR_ERR_TIMEOUT - message processing call timed out";
3387 str = "RMR_ERR_UNSET - the message hasn't been populated with a transport buffer";
3390 str = "RMR_ERR_TRUNC - received message likely truncated";
3392 case RMR_ERR_INITFAILED:
3393 str = "RMR_ERR_INITFAILED - initialisation of something (probably message) failed";
3395 case RMR_ERR_NOTSUPP:
3396 str = "RMR_ERR_NOTSUPP - the request is not supported, or RMr was not initialised for the request";
3400 snprintf(buf, sizeof buf, "UNDOCUMENTED RMR_ERR : %d", state);
3406 int fetchStreamId(ConnectedCU_t *peerInfo, ReportingMessages_t &message)
3408 auto loglevel = mdclog_level_get();
3409 int streamId = INVALID_STREAM_ID;
3410 if(message.peerInfo->isSingleStream != false)
3412 streamId = message.peerInfo->singleStreamId;
3413 if (loglevel >= MDCLOG_DEBUG) {
3414 mdclog_write(MDCLOG_DEBUG, "Send SCTP message for SINGLE_STREAM streamId %d , Messeage Type %d ,%s",
3415 streamId,message.message.messageType, __FUNCTION__);
3419 int msgType = message.message.messageType;
3421 case RIC_E2_RESET_REQ:
3422 case RIC_E2_RESET_RESP:
3423 case RIC_E2_SETUP_RESP:
3424 case RIC_E2_SETUP_FAILURE:
3425 case RIC_ERROR_INDICATION:
3426 case RIC_SERVICE_QUERY:
3427 case RIC_SERVICE_UPDATE_ACK:
3428 case RIC_SERVICE_UPDATE_FAILURE:
3432 case RIC_SUB_DEL_REQ:
3433 case RIC_CONTROL_REQ:
3440 if (loglevel >= MDCLOG_DEBUG) {
3441 mdclog_write(MDCLOG_DEBUG, "Send SCTP message for streamId %d Messeage Type %d, %s",
3442 streamId, message.message.messageType, __FUNCTION__);