blob: 89feebb4cd8ae3e4efe9b4dde8015a654b202865 [file] [log] [blame]
ss412g1a79bdf2019-10-24 12:03:05 +03001// Copyright 2019 AT&T Intellectual Property
2// Copyright 2019 Nokia
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
nm755n2e268142019-11-28 16:40:23 +000016// This source code is part of the near-RT RIC (RAN Intelligent Controller)
17// platform project (RICP).
18
ss412g1a79bdf2019-10-24 12:03:05 +030019// TODO: High-level file comment.
20
21
aa7133@att.com84bd3342020-03-16 18:04:57 +020022
aa7133@att.com7b437f72020-03-30 13:25:54 +030023#include <3rdparty/oranE2/RANfunctions-List.h>
ss412g1a79bdf2019-10-24 12:03:05 +030024#include "sctpThread.h"
aa7133@att.com84bd3342020-03-16 18:04:57 +020025#include "BuildRunName.h"
dhirajverma5f132832021-06-17 17:27:54 +053026#include <unistd.h>
aa7133@att.comdf4f1822020-06-16 16:23:53 +030027//#include "3rdparty/oranE2SM/E2SM-gNB-NRT-RANfunction-Definition.h"
28//#include "BuildXml.h"
29//#include "pugixml/src/pugixml.hpp"
dhirajverma55e755b2021-06-29 02:01:47 +053030#include <pthread.h>
31#include <sys/time.h>
32#include <sys/inotify.h>
33#include <errno.h>
34#include <sys/stat.h>
35
aa7133@att.com84bd3342020-03-16 18:04:57 +020036using namespace std;
37//using namespace std::placeholders;
ss412g3bac2da2020-01-05 11:52:19 +020038using namespace boost::filesystem;
ss412g4aeeda22020-06-16 14:12:41 +030039using namespace prometheus;
40
ss412g3bac2da2020-01-05 11:52:19 +020041
ss412g1a79bdf2019-10-24 12:03:05 +030042//#ifdef __cplusplus
43//extern "C"
44//{
45//#endif
46
ss412g3bac2da2020-01-05 11:52:19 +020047// need to expose without the include of gcov
48extern "C" void __gcov_flush(void);
dhirajverma55e755b2021-06-29 02:01:47 +053049#define LOG_FILE_CONFIG_MAP "CONFIG_MAP_NAME"
ss412g3bac2da2020-01-05 11:52:19 +020050
51static void catch_function(int signal) {
52 __gcov_flush();
53 exit(signal);
54}
55
56
ss412g1a79bdf2019-10-24 12:03:05 +030057BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger, src::logger_mt)
58
59boost::shared_ptr<sinks::synchronous_sink<sinks::text_file_backend>> boostLogger;
ss412g3bac2da2020-01-05 11:52:19 +020060double cpuClock = 0.0;
aa7133@att.com41fcb3f2020-11-29 13:48:25 +020061bool jsonTrace = false;
ss412g1a79bdf2019-10-24 12:03:05 +030062
wahidw1d1a9b72021-09-24 17:35:28 +000063char* getinterfaceip()
64{
65 char hostname[256];
66 char *IP;
67 struct hostent *host_entry;
68 int retVal;
69 retVal = gethostname(hostname, sizeof(hostname));
70 if ( retVal == -1 )
71 return NULL;
72 host_entry = gethostbyname(hostname);
73 if ( host_entry == NULL )
74 return NULL;
75 IP = inet_ntoa(*((struct in_addr*) host_entry->h_addr_list[0]));
76 return IP;
77}
78
dhirajverma55e755b2021-06-29 02:01:47 +053079
80static int enable_log_change_notify(const char* fileName)
81{
82 int ret = -1;
83 struct stat fileInfo;
84 if ( lstat(fileName,&fileInfo) == 0 )
85 {
86 ret = register_log_change_notify(fileName);
87 }
88 return ret;
89}
90
91
92static int register_log_change_notify(const char *fileName)
93{
94 pthread_attr_t cb_attr;
95 pthread_t tid;
96 pthread_attr_init(&cb_attr);
97 pthread_attr_setdetachstate(&cb_attr,PTHREAD_CREATE_DETACHED);
dhirajverma9f59b572021-07-23 02:36:13 -040098 return pthread_create(&tid, &cb_attr,&monitor_loglevel_change_handler,(void *)fileName);
dhirajverma55e755b2021-06-29 02:01:47 +053099}
100
101
102static void * monitor_loglevel_change_handler(void* arg)
103{
104 char *fileName = (char*) arg;
105 int ifd; // the inotify file des
106 int wfd; // the watched file des
107 ssize_t n = 0;
108 char rbuf[4096]; // large read buffer as the event is var len
109 fd_set fds;
110 int res = 0;
111 struct timeval timeout;
112 char* dname=NULL; // directory name
113 char* bname = NULL; // basename
114 char* tok=NULL;
115 char* log_level=NULL;
116
117 dname = strdup( fileName); // defrock the file name into dir and basename
118 if( (tok = strrchr( dname, '/' )) != NULL ) {
119 *tok = '\0';
120 bname = strdup( tok+1 );
121 }
122
123
124 ifd = inotify_init1( 0 ); // initialise watcher setting blocking read (no option)
125 if( ifd < 0 ) {
126 fprintf( stderr, "### ERR ### unable to initialise file watch %s\n", strerror( errno ) );
127 } else {
128 wfd = inotify_add_watch( ifd, dname, IN_MOVED_TO | IN_CLOSE_WRITE ); // we only care about close write changes
129
130 if( wfd < 0 ) {
131 fprintf( stderr, "### ERR ### unable to add watch on config file %s: %s\n", fileName, strerror( errno ) );
132 } else {
133
134
135 memset( &timeout, 0, sizeof(timeout) );
136 while( 1 ) {
137 FD_ZERO (&fds);
138 FD_SET (ifd, &fds);
139 timeout.tv_sec=1;
140 res = select (ifd + 1, &fds, NULL, NULL, &timeout);
141 if(res)
142 {
143 n = read( ifd, rbuf, sizeof( rbuf ) ); // read the event
144 if( n < 0 ) {
dhirajverma93b6d9c2021-07-10 00:49:13 -0400145#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
dhirajverma55e755b2021-06-29 02:01:47 +0530146 if( errno == EAGAIN ) {
147 } else {
wahidw1d1a9b72021-09-24 17:35:28 +0000148 printf( "### CRIT ### config listener read err: %s\n", strerror( errno ) );
dhirajverma55e755b2021-06-29 02:01:47 +0530149 }
150 continue;
dhirajverma93b6d9c2021-07-10 00:49:13 -0400151#endif
dhirajverma55e755b2021-06-29 02:01:47 +0530152 }
153
154 //Retrieving Log Level from configmap by parsing configmap file
155 log_level = parse_file(fileName);
156 update_mdc_log_level_severity(log_level); //setting log level
157 free(log_level);
158 }
159 }
160 inotify_rm_watch(ifd,wfd);
161 }
162 close(ifd);
163 }
164 free(bname);
165 free(dname);
166
167 pthread_exit(NULL);
168}
169
170void update_mdc_log_level_severity(char* log_level)
171{
172 mdclog_severity_t level = MDCLOG_ERR;
173
174 if(log_level == NULL)
175 {
176 printf("### ERR ### Invalid Log-Level Configuration in ConfigMap, Default Log-Level Applied: %d\n",level);
177 }
178 else if(strcasecmp(log_level,"1")==0)
179 {
180 level = MDCLOG_ERR;
181 }
182 else if(strcasecmp(log_level,"2")==0)
183 {
184 level = MDCLOG_WARN;
185 }
186 else if(strcasecmp(log_level,"3")==0)
187 {
188 level = MDCLOG_INFO;
189 }
190 else if(strcasecmp(log_level,"4")==0)
191 {
192 level = MDCLOG_DEBUG;
193 }
194
195 mdclog_level_set(level);
196}
197static char* parse_file(char* filename)
198{
199 char *token=NULL;
200 char *search = ": ";
201 char *string_match = "log-level";
202 bool found = false;
203 FILE *file = fopen ( filename, "r" );
204 if ( file != NULL )
205 {
206 char line [ 128 ];
207 while ( fgets ( line, sizeof line, file ) != NULL )
208 {
209 token = strtok(line, search);
210 if(strcmp(token,string_match)==0)
211 {
212 found = true;
213 token = strtok(NULL, search);
214 token = strtok(token, "\n");//removing newline if any
215 break;
216 }
217 }
218 fclose ( file );
219 }
220 if(found)
221 return(strdup(token));
222 else
223 return(NULL);
224}
225
226char *read_env_param(const char*envkey)
227{
228 if(envkey)
229 {
230 char *value = getenv(envkey);
231 if(value)
232 return strdup(value);
233 }
234 return NULL;
235}
236
237void dynamic_log_level_change()
238{
239 char *logFile_Name = read_env_param(LOG_FILE_CONFIG_MAP);
240 char* log_level_init=NULL;
241 if(logFile_Name)
242 {
243 log_level_init = parse_file(logFile_Name);
244 update_mdc_log_level_severity(log_level_init); //setting log level
245 free(log_level_init);
246
247 }
248 enable_log_change_notify(logFile_Name);
249 free(logFile_Name);
250
251}
252
ss412g1a79bdf2019-10-24 12:03:05 +0300253void init_log() {
dhirajverma55e755b2021-06-29 02:01:47 +0530254 int log_change_monitor = 0;
ss412g1a79bdf2019-10-24 12:03:05 +0300255 mdclog_attr_t *attr;
256 mdclog_attr_init(&attr);
257 mdclog_attr_set_ident(attr, "E2Terminator");
258 mdclog_init(attr);
dhirajverma5f132832021-06-17 17:27:54 +0530259 if(mdclog_format_initialize(log_change_monitor)!=0)
260 mdclog_write(MDCLOG_ERR, "Failed to intialize MDC log format !!!");
dhirajverma55e755b2021-06-29 02:01:47 +0530261 dynamic_log_level_change();
ss412g1a79bdf2019-10-24 12:03:05 +0300262 mdclog_attr_destroy(attr);
263}
ss412g3bac2da2020-01-05 11:52:19 +0200264auto start_time = std::chrono::high_resolution_clock::now();
265typedef std::chrono::duration<double, std::ratio<1,1>> seconds_t;
ss412g1a79bdf2019-10-24 12:03:05 +0300266
ss412g3bac2da2020-01-05 11:52:19 +0200267double age() {
268 return seconds_t(std::chrono::high_resolution_clock::now() - start_time).count();
269}
270
aa7133@att.com409f2232020-10-29 13:06:45 +0300271double approx_CPU_MHz(unsigned sleepTime) {
ss412g3bac2da2020-01-05 11:52:19 +0200272 using namespace std::chrono_literals;
273 uint32_t aux = 0;
274 uint64_t cycles_start = rdtscp(aux);
275 double time_start = age();
aa7133@att.com409f2232020-10-29 13:06:45 +0300276 std::this_thread::sleep_for(sleepTime * 1ms);
ss412g3bac2da2020-01-05 11:52:19 +0200277 uint64_t elapsed_cycles = rdtscp(aux) - cycles_start;
278 double elapsed_time = age() - time_start;
279 return elapsed_cycles / elapsed_time;
280}
ss412g1a79bdf2019-10-24 12:03:05 +0300281
282//std::atomic<int64_t> rmrCounter{0};
283std::atomic<int64_t> num_of_messages{0};
ss412g3bac2da2020-01-05 11:52:19 +0200284std::atomic<int64_t> num_of_XAPP_messages{0};
ss412g1a79bdf2019-10-24 12:03:05 +0300285static long transactionCounter = 0;
sandeekuea662102022-11-08 18:51:20 +0530286pthread_mutex_t thread_lock;
ss412g1a79bdf2019-10-24 12:03:05 +0300287
aa7133@att.com84bd3342020-03-16 18:04:57 +0200288int buildListeningPort(sctp_params_t &sctpParams) {
aa7133@att.comf211f192020-06-23 12:50:10 +0300289 sctpParams.listenFD = socket(AF_INET6, SOCK_STREAM, IPPROTO_SCTP);
290 if (sctpParams.listenFD <= 0) {
dhirajverma93b6d9c2021-07-10 00:49:13 -0400291#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.comf211f192020-06-23 12:50:10 +0300292 mdclog_write(MDCLOG_ERR, "Error Opening socket, %s", strerror(errno));
293 return -1;
dhirajverma93b6d9c2021-07-10 00:49:13 -0400294#endif
aa7133@att.comf211f192020-06-23 12:50:10 +0300295 }
czichy2175dd32022-05-17 15:24:51 +0300296 struct sctp_initmsg initmsg;
297 memset (&initmsg, 0, sizeof (initmsg));
298 initmsg.sinit_num_ostreams = 2;
299 initmsg.sinit_max_instreams = 2;
300 initmsg.sinit_max_attempts = 4;
301 setsockopt (sctpParams.listenFD, IPPROTO_SCTP, SCTP_INITMSG, &initmsg, sizeof (initmsg));
aa7133@att.comf211f192020-06-23 12:50:10 +0300302
aa7133@att.com409f2232020-10-29 13:06:45 +0300303 struct sockaddr_in6 serverAddress {};
304 serverAddress.sin6_family = AF_INET6;
305 serverAddress.sin6_addr = in6addr_any;
306 serverAddress.sin6_port = htons(sctpParams.sctpPort);
307 if (bind(sctpParams.listenFD, (SA *)&serverAddress, sizeof(serverAddress)) < 0 ) {
dhirajverma93b6d9c2021-07-10 00:49:13 -0400308#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.comf211f192020-06-23 12:50:10 +0300309 mdclog_write(MDCLOG_ERR, "Error binding port %d. %s", sctpParams.sctpPort, strerror(errno));
aa7133@att.com84bd3342020-03-16 18:04:57 +0200310 return -1;
dhirajverma93b6d9c2021-07-10 00:49:13 -0400311#endif
aa7133@att.com84bd3342020-03-16 18:04:57 +0200312 }
313 if (setSocketNoBlocking(sctpParams.listenFD) == -1) {
314 //mdclog_write(MDCLOG_ERR, "Error binding. %s", strerror(errno));
315 return -1;
316 }
317 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300318 struct sockaddr_in6 clientAddress {};
319 socklen_t len = sizeof(clientAddress);
320 getsockname(sctpParams.listenFD, (SA *)&clientAddress, &len);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200321 char buff[1024] {};
aa7133@att.com409f2232020-10-29 13:06:45 +0300322 inet_ntop(AF_INET6, &clientAddress.sin6_addr, buff, sizeof(buff));
323 mdclog_write(MDCLOG_DEBUG, "My address: %s, port %d\n", buff, htons(clientAddress.sin6_port));
ss412g1a79bdf2019-10-24 12:03:05 +0300324 }
325
aa7133@att.com84bd3342020-03-16 18:04:57 +0200326 if (listen(sctpParams.listenFD, SOMAXCONN) < 0) {
dhirajverma93b6d9c2021-07-10 00:49:13 -0400327#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.com84bd3342020-03-16 18:04:57 +0200328 mdclog_write(MDCLOG_ERR, "Error listening. %s\n", strerror(errno));
329 return -1;
dhirajverma93b6d9c2021-07-10 00:49:13 -0400330#endif
ss412g3bac2da2020-01-05 11:52:19 +0200331 }
aa7133@att.com84bd3342020-03-16 18:04:57 +0200332 struct epoll_event event {};
333 event.events = EPOLLIN | EPOLLET;
334 event.data.fd = sctpParams.listenFD;
335
336 // add listening port to epoll
337 if (epoll_ctl(sctpParams.epoll_fd, EPOLL_CTL_ADD, sctpParams.listenFD, &event)) {
dhirajverma93b6d9c2021-07-10 00:49:13 -0400338#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.com84bd3342020-03-16 18:04:57 +0200339 printf("Failed to add descriptor to epoll\n");
340 mdclog_write(MDCLOG_ERR, "Failed to add descriptor to epoll. %s\n", strerror(errno));
341 return -1;
dhirajverma93b6d9c2021-07-10 00:49:13 -0400342#endif
ss412g3bac2da2020-01-05 11:52:19 +0200343 }
344
aa7133@att.com84bd3342020-03-16 18:04:57 +0200345 return 0;
346}
ss412g3bac2da2020-01-05 11:52:19 +0200347
aa7133@att.com84bd3342020-03-16 18:04:57 +0200348int buildConfiguration(sctp_params_t &sctpParams) {
ss412g3bac2da2020-01-05 11:52:19 +0200349 path p = (sctpParams.configFilePath + "/" + sctpParams.configFileName).c_str();
350 if (exists(p)) {
351 const int size = 2048;
352 auto fileSize = file_size(p);
353 if (fileSize > size) {
dhirajverma93b6d9c2021-07-10 00:49:13 -0400354#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
ss412g3bac2da2020-01-05 11:52:19 +0200355 mdclog_write(MDCLOG_ERR, "File %s larger than %d", p.string().c_str(), size);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200356 return -1;
dhirajverma93b6d9c2021-07-10 00:49:13 -0400357#endif
ss412g3bac2da2020-01-05 11:52:19 +0200358 }
359 } else {
dhirajverma93b6d9c2021-07-10 00:49:13 -0400360#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
ss412g3bac2da2020-01-05 11:52:19 +0200361 mdclog_write(MDCLOG_ERR, "Configuration File %s not exists", p.string().c_str());
aa7133@att.com84bd3342020-03-16 18:04:57 +0200362 return -1;
dhirajverma93b6d9c2021-07-10 00:49:13 -0400363#endif
ss412g3bac2da2020-01-05 11:52:19 +0200364 }
365
ss412g3bac2da2020-01-05 11:52:19 +0200366 ReadConfigFile conf;
367 if (conf.openConfigFile(p.string()) == -1) {
dhirajverma93b6d9c2021-07-10 00:49:13 -0400368#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
ss412g3bac2da2020-01-05 11:52:19 +0200369 mdclog_write(MDCLOG_ERR, "Filed to open config file %s, %s",
370 p.string().c_str(), strerror(errno));
aa7133@att.com84bd3342020-03-16 18:04:57 +0200371 return -1;
dhirajverma93b6d9c2021-07-10 00:49:13 -0400372#endif
ss412g3bac2da2020-01-05 11:52:19 +0200373 }
374 int rmrPort = conf.getIntValue("nano");
375 if (rmrPort == -1) {
dhirajverma93b6d9c2021-07-10 00:49:13 -0400376#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.com409f2232020-10-29 13:06:45 +0300377 mdclog_write(MDCLOG_ERR, "illegal RMR port ");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200378 return -1;
dhirajverma93b6d9c2021-07-10 00:49:13 -0400379#endif
ss412g3bac2da2020-01-05 11:52:19 +0200380 }
381 sctpParams.rmrPort = (uint16_t)rmrPort;
382 snprintf(sctpParams.rmrAddress, sizeof(sctpParams.rmrAddress), "%d", (int) (sctpParams.rmrPort));
dhirajverma5f132832021-06-17 17:27:54 +0530383 auto tmpStr = conf.getStringValue("volume");
ss412g3bac2da2020-01-05 11:52:19 +0200384 if (tmpStr.length() == 0) {
dhirajverma93b6d9c2021-07-10 00:49:13 -0400385#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.com409f2232020-10-29 13:06:45 +0300386 mdclog_write(MDCLOG_ERR, "illegal volume.");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200387 return -1;
dhirajverma93b6d9c2021-07-10 00:49:13 -0400388#endif
ss412g3bac2da2020-01-05 11:52:19 +0200389 }
390
ss412g1a79bdf2019-10-24 12:03:05 +0300391 char tmpLogFilespec[VOLUME_URL_SIZE];
392 tmpLogFilespec[0] = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200393 sctpParams.volume[0] = 0;
394 snprintf(sctpParams.volume, VOLUME_URL_SIZE, "%s", tmpStr.c_str());
395 // copy the name to temp file as well
396 snprintf(tmpLogFilespec, VOLUME_URL_SIZE, "%s", tmpStr.c_str());
397
398
399 // define the file name in the tmp directory under the volume
400 strcat(tmpLogFilespec,"/tmp/E2Term_%Y-%m-%d_%H-%M-%S.%N.tmpStr");
401
402 sctpParams.myIP = conf.getStringValue("local-ip");
403 if (sctpParams.myIP.length() == 0) {
dhirajverma93b6d9c2021-07-10 00:49:13 -0400404#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.com409f2232020-10-29 13:06:45 +0300405 mdclog_write(MDCLOG_ERR, "illegal local-ip.");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200406 return -1;
dhirajverma93b6d9c2021-07-10 00:49:13 -0400407#endif
ss412g1a79bdf2019-10-24 12:03:05 +0300408 }
409
aa7133@att.combe92d712020-03-18 17:30:39 +0200410 int sctpPort = conf.getIntValue("sctp-port");
411 if (sctpPort == -1) {
dhirajverma93b6d9c2021-07-10 00:49:13 -0400412#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.com409f2232020-10-29 13:06:45 +0300413 mdclog_write(MDCLOG_ERR, "illegal SCTP port ");
aa7133@att.combe92d712020-03-18 17:30:39 +0200414 return -1;
dhirajverma93b6d9c2021-07-10 00:49:13 -0400415#endif
aa7133@att.combe92d712020-03-18 17:30:39 +0200416 }
417 sctpParams.sctpPort = (uint16_t)sctpPort;
418
aa7133@att.com84bd3342020-03-16 18:04:57 +0200419 sctpParams.fqdn = conf.getStringValue("external-fqdn");
420 if (sctpParams.fqdn.length() == 0) {
dhirajverma93b6d9c2021-07-10 00:49:13 -0400421#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.com409f2232020-10-29 13:06:45 +0300422 mdclog_write(MDCLOG_ERR, "illegal external-fqdn");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200423 return -1;
dhirajverma93b6d9c2021-07-10 00:49:13 -0400424#endif
ss412g3bac2da2020-01-05 11:52:19 +0200425 }
ss412g1a79bdf2019-10-24 12:03:05 +0300426
aa7133@att.com342843e2020-01-26 12:53:34 +0200427 std::string pod = conf.getStringValue("pod_name");
dhirajverma6cf28612021-06-23 04:53:34 -0400428#ifndef UNIT_TEST
aa7133@att.com342843e2020-01-26 12:53:34 +0200429 if (pod.length() == 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300430 mdclog_write(MDCLOG_ERR, "illegal pod_name in config file");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200431 return -1;
aa7133@att.com342843e2020-01-26 12:53:34 +0200432 }
433 auto *podName = getenv(pod.c_str());
434 if (podName == nullptr) {
aa7133@att.com54885fe2020-10-29 17:24:49 +0300435 mdclog_write(MDCLOG_ERR, "illegal pod_name or environment variable not exists : %s", pod.c_str());
aa7133@att.com84bd3342020-03-16 18:04:57 +0200436 return -1;
aa7133@att.com342843e2020-01-26 12:53:34 +0200437
438 } else {
439 sctpParams.podName.assign(podName);
440 if (sctpParams.podName.length() == 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300441 mdclog_write(MDCLOG_ERR, "illegal pod_name");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200442 return -1;
aa7133@att.com342843e2020-01-26 12:53:34 +0200443 }
444 }
dhirajverma6cf28612021-06-23 04:53:34 -0400445#endif
ss412g3bac2da2020-01-05 11:52:19 +0200446 tmpStr = conf.getStringValue("trace");
447 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
448 if ((tmpStr.compare("start")) == 0) {
dhirajverma93b6d9c2021-07-10 00:49:13 -0400449#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
ss412g3bac2da2020-01-05 11:52:19 +0200450 mdclog_write(MDCLOG_INFO, "Trace set to: start");
451 sctpParams.trace = true;
dhirajverma93b6d9c2021-07-10 00:49:13 -0400452#endif
ss412g3bac2da2020-01-05 11:52:19 +0200453 } else if ((tmpStr.compare("stop")) == 0) {
454 mdclog_write(MDCLOG_INFO, "Trace set to: stop");
455 sctpParams.trace = false;
aa7133@att.com41fcb3f2020-11-29 13:48:25 +0200456 } else {
dhirajverma93b6d9c2021-07-10 00:49:13 -0400457#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.com41fcb3f2020-11-29 13:48:25 +0200458 mdclog_write(MDCLOG_ERR, "Trace was set to wrong value %s, set to stop", tmpStr.c_str());
aa7133@att.com92aeebe2020-11-29 14:22:09 +0200459 sctpParams.trace = false;
dhirajverma93b6d9c2021-07-10 00:49:13 -0400460#endif
ss412g3bac2da2020-01-05 11:52:19 +0200461 }
462 jsonTrace = sctpParams.trace;
ss412g1a79bdf2019-10-24 12:03:05 +0300463
ss412g4aeeda22020-06-16 14:12:41 +0300464 sctpParams.epollTimeOut = -1;
ss412g4aeeda22020-06-16 14:12:41 +0300465
466 tmpStr = conf.getStringValue("prometheusPort");
467 if (tmpStr.length() != 0) {
468 sctpParams.prometheusPort = tmpStr;
469 }
470
aa7133@att.combe92d712020-03-18 17:30:39 +0200471 sctpParams.ka_message_length = snprintf(sctpParams.ka_message, KA_MESSAGE_SIZE, "{\"address\": \"%s:%d\","
aa7133@att.com96c9b882020-03-25 14:50:40 +0200472 "\"fqdn\": \"%s\","
473 "\"pod_name\": \"%s\"}",
aa7133@att.com342843e2020-01-26 12:53:34 +0200474 (const char *)sctpParams.myIP.c_str(),
475 sctpParams.rmrPort,
476 sctpParams.fqdn.c_str(),
477 sctpParams.podName.c_str());
478
ss412g1a79bdf2019-10-24 12:03:05 +0300479 if (mdclog_level_get() >= MDCLOG_INFO) {
dhirajverma5f132832021-06-17 17:27:54 +0530480 mdclog_write(MDCLOG_DEBUG,"RMR Port: %s", to_string(sctpParams.rmrPort).c_str());
481 mdclog_write(MDCLOG_DEBUG,"LogLevel: %s", to_string(sctpParams.logLevel).c_str());
482 mdclog_write(MDCLOG_DEBUG,"volume: %s", sctpParams.volume);
483 mdclog_write(MDCLOG_DEBUG,"tmpLogFilespec: %s", tmpLogFilespec);
484 mdclog_write(MDCLOG_DEBUG,"my ip: %s", sctpParams.myIP.c_str());
485 mdclog_write(MDCLOG_DEBUG,"pod name: %s", sctpParams.podName.c_str());
ss412g1a79bdf2019-10-24 12:03:05 +0300486
aa7133@att.com342843e2020-01-26 12:53:34 +0200487 mdclog_write(MDCLOG_INFO, "running parameters for instance : %s", sctpParams.ka_message);
ss412g1a79bdf2019-10-24 12:03:05 +0300488 }
ss412g1a79bdf2019-10-24 12:03:05 +0300489
490 // Files written to the current working directory
491 boostLogger = logging::add_file_log(
ss412g3bac2da2020-01-05 11:52:19 +0200492 keywords::file_name = tmpLogFilespec, // to temp directory
ss412g1a79bdf2019-10-24 12:03:05 +0300493 keywords::rotation_size = 10 * 1024 * 1024,
494 keywords::time_based_rotation = sinks::file::rotation_at_time_interval(posix_time::hours(1)),
495 keywords::format = "%Message%"
ss412g3bac2da2020-01-05 11:52:19 +0200496 //keywords::format = "[%TimeStamp%]: %Message%" // use each tmpStr with time stamp
ss412g1a79bdf2019-10-24 12:03:05 +0300497 );
498
aa7133@att.com54885fe2020-10-29 17:24:49 +0300499 // Setup a destination folder for collecting rotated (closed) files --since the same volume can use rename()
ss412g1a79bdf2019-10-24 12:03:05 +0300500 boostLogger->locked_backend()->set_file_collector(sinks::file::make_collector(
ss412g3bac2da2020-01-05 11:52:19 +0200501 keywords::target = sctpParams.volume
ss412g1a79bdf2019-10-24 12:03:05 +0300502 ));
503
504 // Upon restart, scan the directory for files matching the file_name pattern
505 boostLogger->locked_backend()->scan_for_files();
506
ss412g3bac2da2020-01-05 11:52:19 +0200507 // Enable auto-flushing after each tmpStr record written
ss412g1a79bdf2019-10-24 12:03:05 +0300508 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com84bd3342020-03-16 18:04:57 +0200509 boostLogger->locked_backend()->auto_flush(true);
510 }
511
512 return 0;
513}
514
aa7133@att.coma4c80012020-06-24 16:36:47 +0300515void startPrometheus(sctp_params_t &sctpParams) {
Mohamed Abukar64f37242021-08-15 12:39:56 +0000516 auto podName = std::getenv("POD_NAME");
517 string metric = "E2TBeta";
518 if (strstr(podName, "alpha") != NULL) {
519 metric = "E2TAlpha";
520 }
wahidw1d1a9b72021-09-24 17:35:28 +0000521 //Get eth0 interface IP
522 char* host = getinterfaceip();
523 string hostip = host;
Mohamed Abukar64f37242021-08-15 12:39:56 +0000524
aa7133@att.coma4c80012020-06-24 16:36:47 +0300525 sctpParams.prometheusFamily = &BuildCounter()
Mohamed Abukar64f37242021-08-15 12:39:56 +0000526 .Name(metric.c_str())
527 .Help("E2T instance metrics")
aa7133@att.coma4c80012020-06-24 16:36:47 +0300528 .Labels({{"POD_NAME", sctpParams.podName}})
529 .Register(*sctpParams.prometheusRegistry);
aa7133@att.com16c799b2020-03-24 18:01:51 +0200530
Mohamed Abukar64f37242021-08-15 12:39:56 +0000531 // Build E2T instance level metrics
532 buildE2TPrometheusCounters(sctpParams);
533
wahidw1d1a9b72021-09-24 17:35:28 +0000534 string prometheusPath;
535 if (hostip.empty())
536 prometheusPath = sctpParams.prometheusPort + "," + "[::]:" + sctpParams.prometheusPort;
537 else
538 prometheusPath = hostip + ":" + sctpParams.prometheusPort;
539
aa7133@att.comb92748d2020-06-29 11:52:30 +0300540 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com54885fe2020-10-29 17:24:49 +0300541 mdclog_write(MDCLOG_DEBUG, "Start Prometheus Pull mode on %s", prometheusPath.c_str());
aa7133@att.coma4c80012020-06-24 16:36:47 +0300542 }
aa7133@att.com54885fe2020-10-29 17:24:49 +0300543 sctpParams.prometheusExposer = new Exposer(prometheusPath, 1);
aa7133@att.comb92748d2020-06-29 11:52:30 +0300544 sctpParams.prometheusExposer->RegisterCollectable(sctpParams.prometheusRegistry);
aa7133@att.coma4c80012020-06-24 16:36:47 +0300545}
dhirajverma31768a92021-05-06 12:38:49 +0530546#ifndef UNIT_TEST
dhirajverma5f132832021-06-17 17:27:54 +0530547
aa7133@att.com84bd3342020-03-16 18:04:57 +0200548int main(const int argc, char **argv) {
549 sctp_params_t sctpParams;
aa7133@att.com84bd3342020-03-16 18:04:57 +0200550 {
551 std::random_device device{};
552 std::mt19937 generator(device());
553 std::uniform_int_distribution<long> distribution(1, (long) 1e12);
554 transactionCounter = distribution(generator);
555 }
556
557// uint64_t st = 0;
558// uint32_t aux1 = 0;
559// st = rdtscp(aux1);
560
561 unsigned num_cpus = std::thread::hardware_concurrency();
562 init_log();
aa7133@att.com84bd3342020-03-16 18:04:57 +0200563 if (std::signal(SIGINT, catch_function) == SIG_ERR) {
564 mdclog_write(MDCLOG_ERR, "Error initializing SIGINT");
565 exit(1);
566 }
567 if (std::signal(SIGABRT, catch_function)== SIG_ERR) {
568 mdclog_write(MDCLOG_ERR, "Error initializing SIGABRT");
569 exit(1);
570 }
571 if (std::signal(SIGTERM, catch_function)== SIG_ERR) {
572 mdclog_write(MDCLOG_ERR, "Error initializing SIGTERM");
573 exit(1);
574 }
575
576 cpuClock = approx_CPU_MHz(100);
577
578 mdclog_write(MDCLOG_DEBUG, "CPU speed %11.11f", cpuClock);
579
580 auto result = parse(argc, argv, sctpParams);
581
582 if (buildConfiguration(sctpParams) != 0) {
583 exit(-1);
ss412g1a79bdf2019-10-24 12:03:05 +0300584 }
585
ss412g4aeeda22020-06-16 14:12:41 +0300586 //auto registry = std::make_shared<Registry>();
587 sctpParams.prometheusRegistry = std::make_shared<Registry>();
588
aa7133@att.com409f2232020-10-29 13:06:45 +0300589 //sctpParams.prometheusFamily = new Family<Counter>("E2T", "E2T message counter", {{"E", sctpParams.podName}});
ss412g4aeeda22020-06-16 14:12:41 +0300590
aa7133@att.coma4c80012020-06-24 16:36:47 +0300591 startPrometheus(sctpParams);
ss412g4aeeda22020-06-16 14:12:41 +0300592
ss412g1a79bdf2019-10-24 12:03:05 +0300593 // start epoll
ss412g3bac2da2020-01-05 11:52:19 +0200594 sctpParams.epoll_fd = epoll_create1(0);
595 if (sctpParams.epoll_fd == -1) {
ss412g1a79bdf2019-10-24 12:03:05 +0300596 mdclog_write(MDCLOG_ERR, "failed to open epoll descriptor");
597 exit(-1);
598 }
aa7133@att.combe92d712020-03-18 17:30:39 +0200599 getRmrContext(sctpParams);
ss412g3bac2da2020-01-05 11:52:19 +0200600 if (sctpParams.rmrCtx == nullptr) {
601 close(sctpParams.epoll_fd);
ss412g1a79bdf2019-10-24 12:03:05 +0300602 exit(-1);
603 }
604
ss412g3bac2da2020-01-05 11:52:19 +0200605 if (buildInotify(sctpParams) == -1) {
606 close(sctpParams.rmrListenFd);
607 rmr_close(sctpParams.rmrCtx);
608 close(sctpParams.epoll_fd);
609 exit(-1);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200610 }
611
612 if (buildListeningPort(sctpParams) != 0) {
613 close(sctpParams.rmrListenFd);
614 rmr_close(sctpParams.rmrCtx);
615 close(sctpParams.epoll_fd);
616 exit(-1);
617 }
ss412g3bac2da2020-01-05 11:52:19 +0200618
619 sctpParams.sctpMap = new mapWrapper();
ss412g1a79bdf2019-10-24 12:03:05 +0300620
sandeekuea662102022-11-08 18:51:20 +0530621 if (pthread_mutex_init(&thread_lock, NULL) != 0) {
622 mdclog_write(MDCLOG_ERR, "failed to init thread lock");
623 exit(-1);
624 }
ss412g1a79bdf2019-10-24 12:03:05 +0300625 std::vector<std::thread> threads(num_cpus);
626// std::vector<std::thread> threads;
627
aa7133@att.com24e9fa42020-11-13 20:32:53 +0200628 num_cpus = 3;
ss412g1a79bdf2019-10-24 12:03:05 +0300629 for (unsigned int i = 0; i < num_cpus; i++) {
ss412g3bac2da2020-01-05 11:52:19 +0200630 threads[i] = std::thread(listener, &sctpParams);
ss412g1a79bdf2019-10-24 12:03:05 +0300631
632 cpu_set_t cpuset;
633 CPU_ZERO(&cpuset);
634 CPU_SET(i, &cpuset);
635 int rc = pthread_setaffinity_np(threads[i].native_handle(), sizeof(cpu_set_t), &cpuset);
636 if (rc != 0) {
637 mdclog_write(MDCLOG_ERR, "Error calling pthread_setaffinity_np: %d", rc);
638 }
ss412g1a79bdf2019-10-24 12:03:05 +0300639 }
640
aa7133@att.com16c799b2020-03-24 18:01:51 +0200641
ss412g3bac2da2020-01-05 11:52:19 +0200642 //loop over term_init until first message from xApp
643 handleTermInit(sctpParams);
644
645 for (auto &t : threads) {
646 t.join();
647 }
sandeekuea662102022-11-08 18:51:20 +0530648 pthread_mutex_destroy(&thread_lock);
ss412g3bac2da2020-01-05 11:52:19 +0200649 return 0;
650}
dhirajverma60f8bef2021-06-29 07:22:58 -0400651#endif
ss412g3bac2da2020-01-05 11:52:19 +0200652void handleTermInit(sctp_params_t &sctpParams) {
653 sendTermInit(sctpParams);
ss412g1a79bdf2019-10-24 12:03:05 +0300654 //send to e2 manager init of e2 term
655 //E2_TERM_INIT
ss412g1a79bdf2019-10-24 12:03:05 +0300656
ss412g3bac2da2020-01-05 11:52:19 +0200657 int count = 0;
658 while (true) {
659 auto xappMessages = num_of_XAPP_messages.load(std::memory_order_acquire);
660 if (xappMessages > 0) {
661 if (mdclog_level_get() >= MDCLOG_INFO) {
aa7133@att.com54885fe2020-10-29 17:24:49 +0300662 mdclog_write(MDCLOG_INFO, "Got a message from some application, stop sending E2_TERM_INIT");
ss412g3bac2da2020-01-05 11:52:19 +0200663 }
664 return;
665 }
666 usleep(100000);
667 count++;
668 if (count % 1000 == 0) {
669 mdclog_write(MDCLOG_ERR, "GOT No messages from any xApp");
670 sendTermInit(sctpParams);
671 }
672 }
673}
674
675void sendTermInit(sctp_params_t &sctpParams) {
676 rmr_mbuf_t *msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.ka_message_length);
ss412g1a79bdf2019-10-24 12:03:05 +0300677 auto count = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200678 while (true) {
ss412g1a79bdf2019-10-24 12:03:05 +0300679 msg->mtype = E2_TERM_INIT;
680 msg->state = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200681 rmr_bytes2payload(msg, (unsigned char *)sctpParams.ka_message, sctpParams.ka_message_length);
ss412g1a79bdf2019-10-24 12:03:05 +0300682 static unsigned char tx[32];
683 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
684 rmr_bytes2xact(msg, tx, txLen);
ss412g3bac2da2020-01-05 11:52:19 +0200685 msg = rmr_send_msg(sctpParams.rmrCtx, msg);
ss412g1a79bdf2019-10-24 12:03:05 +0300686 if (msg == nullptr) {
aa7133@att.comc9575aa2020-04-02 11:40:09 +0300687 msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.ka_message_length);
ss412g1a79bdf2019-10-24 12:03:05 +0300688 } else if (msg->state == 0) {
ss412g1a79bdf2019-10-24 12:03:05 +0300689 rmr_free_msg(msg);
ss412g3bac2da2020-01-05 11:52:19 +0200690 if (mdclog_level_get() >= MDCLOG_INFO) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300691 mdclog_write(MDCLOG_INFO, "E2_TERM_INIT successfully sent ");
ss412g3bac2da2020-01-05 11:52:19 +0200692 }
693 return;
ss412g1a79bdf2019-10-24 12:03:05 +0300694 } else {
695 if (count % 100 == 0) {
aa7133@att.com11b17d42020-03-22 11:35:16 +0200696 mdclog_write(MDCLOG_ERR, "Error sending E2_TERM_INIT cause : %s ", translateRmrErrorMessages(msg->state).c_str());
ss412g1a79bdf2019-10-24 12:03:05 +0300697 }
698 sleep(1);
699 }
700 count++;
701 }
ss412g3bac2da2020-01-05 11:52:19 +0200702}
703
704/**
705 *
706 * @param argc
707 * @param argv
708 * @param sctpParams
709 * @return
710 */
711cxxopts::ParseResult parse(int argc, char *argv[], sctp_params_t &sctpParams) {
712 cxxopts::Options options(argv[0], "e2 term help");
713 options.positional_help("[optional args]").show_positional_help();
714 options.allow_unrecognised_options().add_options()
715 ("p,path", "config file path", cxxopts::value<std::string>(sctpParams.configFilePath)->default_value("config"))
716 ("f,file", "config file name", cxxopts::value<std::string>(sctpParams.configFileName)->default_value("config.conf"))
717 ("h,help", "Print help");
718
aa7133@att.comde40d1c2020-08-26 16:19:20 +0300719 auto result = options.parse(argc, (const char **&)argv);
ss412g3bac2da2020-01-05 11:52:19 +0200720
721 if (result.count("help")) {
722 std::cout << options.help({""}) << std::endl;
723 exit(0);
724 }
725 return result;
726}
727
728/**
729 *
730 * @param sctpParams
731 * @return -1 failed 0 success
732 */
733int buildInotify(sctp_params_t &sctpParams) {
734 sctpParams.inotifyFD = inotify_init1(IN_NONBLOCK);
735 if (sctpParams.inotifyFD == -1) {
736 mdclog_write(MDCLOG_ERR, "Failed to init inotify (inotify_init1) %s", strerror(errno));
ss412g3bac2da2020-01-05 11:52:19 +0200737 return -1;
ss412g1a79bdf2019-10-24 12:03:05 +0300738 }
739
ss412g3bac2da2020-01-05 11:52:19 +0200740 sctpParams.inotifyWD = inotify_add_watch(sctpParams.inotifyFD,
aa7133@att.com96c9b882020-03-25 14:50:40 +0200741 (const char *)sctpParams.configFilePath.c_str(),
aa7133@att.com84bd3342020-03-16 18:04:57 +0200742 (unsigned)IN_OPEN | (unsigned)IN_CLOSE_WRITE | (unsigned)IN_CLOSE_NOWRITE); //IN_CLOSE = (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)
ss412g3bac2da2020-01-05 11:52:19 +0200743 if (sctpParams.inotifyWD == -1) {
744 mdclog_write(MDCLOG_ERR, "Failed to add directory : %s to inotify (inotify_add_watch) %s",
aa7133@att.com96c9b882020-03-25 14:50:40 +0200745 sctpParams.configFilePath.c_str(),
746 strerror(errno));
ss412g3bac2da2020-01-05 11:52:19 +0200747 close(sctpParams.inotifyFD);
748 return -1;
749 }
750
751 struct epoll_event event{};
752 event.events = (EPOLLIN);
753 event.data.fd = sctpParams.inotifyFD;
754 // add listening RMR FD to epoll
755 if (epoll_ctl(sctpParams.epoll_fd, EPOLL_CTL_ADD, sctpParams.inotifyFD, &event)) {
756 mdclog_write(MDCLOG_ERR, "Failed to add inotify FD to epoll");
757 close(sctpParams.inotifyFD);
758 return -1;
759 }
ss412g1a79bdf2019-10-24 12:03:05 +0300760 return 0;
761}
762
763/**
764 *
765 * @param args
766 * @return
767 */
768void listener(sctp_params_t *params) {
ss412g1a79bdf2019-10-24 12:03:05 +0300769 int num_of_SCTP_messages = 0;
ss412g1a79bdf2019-10-24 12:03:05 +0300770 auto totalTime = 0.0;
ss412g1a79bdf2019-10-24 12:03:05 +0300771 std::thread::id this_id = std::this_thread::get_id();
772 //save cout
dhirajverma5f132832021-06-17 17:27:54 +0530773 auto pod_name = std::getenv("POD_NAME");
774 auto container_name = std::getenv("CONTAINER_NAME");
775 auto service_name = std::getenv("SERVICE_NAME");
776 auto host_name = std::getenv("HOST_NAME");
777 auto system_name = std::getenv("SYSTEM_NAME");
778 auto pid = std::to_string(getpid()).c_str();
ss412g1a79bdf2019-10-24 12:03:05 +0300779 streambuf *oldCout = cout.rdbuf();
780 ostringstream memCout;
781 // create new cout
782 cout.rdbuf(memCout.rdbuf());
783 cout << this_id;
784 //return to the normal cout
785 cout.rdbuf(oldCout);
786
787 char tid[32];
788 memcpy(tid, memCout.str().c_str(), memCout.str().length() < 32 ? memCout.str().length() : 31);
789 tid[memCout.str().length()] = 0;
dhirajverma5f132832021-06-17 17:27:54 +0530790 mdclog_mdc_add("SYSTEM_NAME", system_name);
791 mdclog_mdc_add("HOST_NAME", host_name);
792 mdclog_mdc_add("SERVICE_NAME", service_name);
793 mdclog_mdc_add("CONTAINER_NAME", container_name);
794 mdclog_mdc_add("POD_NAME", pod_name);
795 mdclog_mdc_add("PID", pid);
ss412g1a79bdf2019-10-24 12:03:05 +0300796
797 if (mdclog_level_get() >= MDCLOG_DEBUG) {
798 mdclog_write(MDCLOG_DEBUG, "started thread number %s", tid);
799 }
800
801 RmrMessagesBuffer_t rmrMessageBuffer{};
802 //create and init RMR
803 rmrMessageBuffer.rmrCtx = params->rmrCtx;
804
805 auto *events = (struct epoll_event *) calloc(MAXEVENTS, sizeof(struct epoll_event));
806 struct timespec end{0, 0};
807 struct timespec start{0, 0};
808
809 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
810 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
811
ss412g3bac2da2020-01-05 11:52:19 +0200812 memcpy(rmrMessageBuffer.ka_message, params->ka_message, params->ka_message_length);
813 rmrMessageBuffer.ka_message_len = params->ka_message_length;
814 rmrMessageBuffer.ka_message[rmrMessageBuffer.ka_message_len] = 0;
815
816 if (mdclog_level_get() >= MDCLOG_DEBUG) {
817 mdclog_write(MDCLOG_DEBUG, "keep alive message is : %s", rmrMessageBuffer.ka_message);
818 }
819
ss412g1a79bdf2019-10-24 12:03:05 +0300820 ReportingMessages_t message {};
821
aa7133@att.com409f2232020-10-29 13:06:45 +0300822// for (int i = 0; i < MAX_RMR_BUFF_ARRAY; i++) {
aa7133@att.com7c3e4c02020-03-25 16:37:19 +0200823// rmrMessageBuffer.rcvBufferedMessages[i] = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
824// rmrMessageBuffer.sendBufferedMessages[i] = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
825// }
ss412g1a79bdf2019-10-24 12:03:05 +0300826
827 while (true) {
828 if (mdclog_level_get() >= MDCLOG_DEBUG) {
ss412g4aeeda22020-06-16 14:12:41 +0300829 mdclog_write(MDCLOG_DEBUG, "Start EPOLL Wait. Timeout = %d", params->epollTimeOut);
ss412g1a79bdf2019-10-24 12:03:05 +0300830 }
dhirajverma6cf28612021-06-23 04:53:34 -0400831#ifndef UNIT_TEST
ss412g4aeeda22020-06-16 14:12:41 +0300832 auto numOfEvents = epoll_wait(params->epoll_fd, events, MAXEVENTS, params->epollTimeOut);
dhirajverma6cf28612021-06-23 04:53:34 -0400833#else
834 auto numOfEvents = 1;
835#endif
aa7133@att.comb92748d2020-06-29 11:52:30 +0300836 if (numOfEvents == 0) { // time out
dhirajverma93b6d9c2021-07-10 00:49:13 -0400837#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.comb92748d2020-06-29 11:52:30 +0300838 if (mdclog_level_get() >= MDCLOG_DEBUG) {
839 mdclog_write(MDCLOG_DEBUG, "got epoll timeout");
ss412g1a79bdf2019-10-24 12:03:05 +0300840 }
841 continue;
ss412g4aeeda22020-06-16 14:12:41 +0300842 } else if (numOfEvents < 0) {
843 if (errno == EINTR) {
844 if (mdclog_level_get() >= MDCLOG_DEBUG) {
845 mdclog_write(MDCLOG_DEBUG, "got EINTR : %s", strerror(errno));
846 }
847 continue;
848 }
ss412g1a79bdf2019-10-24 12:03:05 +0300849 mdclog_write(MDCLOG_ERR, "Epoll wait failed, errno = %s", strerror(errno));
dhirajvermab55bc172021-11-09 02:15:08 -0500850 if(events)
851 {
852 free(events);
sandeekuea662102022-11-08 18:51:20 +0530853 events = nullptr;
dhirajvermab55bc172021-11-09 02:15:08 -0500854 }
ss412g1a79bdf2019-10-24 12:03:05 +0300855 return;
dhirajverma93b6d9c2021-07-10 00:49:13 -0400856#endif
ss412g1a79bdf2019-10-24 12:03:05 +0300857 }
858 for (auto i = 0; i < numOfEvents; i++) {
859 if (mdclog_level_get() >= MDCLOG_DEBUG) {
860 mdclog_write(MDCLOG_DEBUG, "handling epoll event %d out of %d", i + 1, numOfEvents);
861 }
862 clock_gettime(CLOCK_MONOTONIC, &message.message.time);
863 start.tv_sec = message.message.time.tv_sec;
864 start.tv_nsec = message.message.time.tv_nsec;
ss412g3bac2da2020-01-05 11:52:19 +0200865
866
ss412g1a79bdf2019-10-24 12:03:05 +0300867 if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP)) {
aa7133@att.combe92d712020-03-18 17:30:39 +0200868 handlepoll_error(events[i], message, rmrMessageBuffer, params);
ss412g1a79bdf2019-10-24 12:03:05 +0300869 } else if (events[i].events & EPOLLOUT) {
aa7133@att.combe92d712020-03-18 17:30:39 +0200870 handleEinprogressMessages(events[i], message, rmrMessageBuffer, params);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200871 } else if (params->listenFD == events[i].data.fd) {
aa7133@att.com6680c3b2020-03-22 12:06:58 +0200872 if (mdclog_level_get() >= MDCLOG_INFO) {
873 mdclog_write(MDCLOG_INFO, "New connection request from sctp network\n");
874 }
aa7133@att.com84bd3342020-03-16 18:04:57 +0200875 // new connection is requested from RAN start build connection
876 while (true) {
877 struct sockaddr in_addr {};
878 socklen_t in_len;
879 char hostBuff[NI_MAXHOST];
880 char portBuff[NI_MAXSERV];
881
882 in_len = sizeof(in_addr);
883 auto *peerInfo = (ConnectedCU_t *)calloc(1, sizeof(ConnectedCU_t));
aa7133@att.com409f2232020-10-29 13:06:45 +0300884 if(peerInfo == nullptr){
swc58d4f5d2020-10-20 15:08:38 +0900885 mdclog_write(MDCLOG_ERR, "calloc failed");
886 break;
887 }
aa7133@att.com84bd3342020-03-16 18:04:57 +0200888 peerInfo->sctpParams = params;
889 peerInfo->fileDescriptor = accept(params->listenFD, &in_addr, &in_len);
890 if (peerInfo->fileDescriptor == -1) {
dhirajverma93b6d9c2021-07-10 00:49:13 -0400891#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.com84bd3342020-03-16 18:04:57 +0200892 if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
893 /* We have processed all incoming connections. */
dhirajverma9f59b572021-07-23 02:36:13 -0400894 if(peerInfo)
895 free(peerInfo);
sandeekuea662102022-11-08 18:51:20 +0530896 peerInfo = nullptr;
aa7133@att.com84bd3342020-03-16 18:04:57 +0200897 break;
898 } else {
dhirajverma9f59b572021-07-23 02:36:13 -0400899 if(peerInfo)
900 free(peerInfo);
sandeekuea662102022-11-08 18:51:20 +0530901 peerInfo = nullptr;
aa7133@att.com84bd3342020-03-16 18:04:57 +0200902 mdclog_write(MDCLOG_ERR, "Accept error, errno = %s", strerror(errno));
903 break;
904 }
905 }
906 if (setSocketNoBlocking(peerInfo->fileDescriptor) == -1) {
907 mdclog_write(MDCLOG_ERR, "setSocketNoBlocking failed to set new connection %s on port %s\n", hostBuff, portBuff);
908 close(peerInfo->fileDescriptor);
dhirajverma9f59b572021-07-23 02:36:13 -0400909 if(peerInfo)
910 free(peerInfo);
sandeekuea662102022-11-08 18:51:20 +0530911 peerInfo = nullptr;
aa7133@att.com84bd3342020-03-16 18:04:57 +0200912 break;
dhirajverma93b6d9c2021-07-10 00:49:13 -0400913#endif
aa7133@att.com84bd3342020-03-16 18:04:57 +0200914 }
czichy2175dd32022-05-17 15:24:51 +0300915 struct sctp_event_subscribe sctpevents;
916 memset( (void *)&sctpevents, 0, sizeof(sctpevents) );
917 sctpevents.sctp_data_io_event = 1;
918 setsockopt(peerInfo->fileDescriptor, SOL_SCTP, SCTP_EVENTS,(const void *)&sctpevents, sizeof(sctpevents) );
919
aa7133@att.com84bd3342020-03-16 18:04:57 +0200920 auto ans = getnameinfo(&in_addr, in_len,
aa7133@att.com96c9b882020-03-25 14:50:40 +0200921 peerInfo->hostName, NI_MAXHOST,
922 peerInfo->portNumber, NI_MAXSERV, (unsigned )((unsigned int)NI_NUMERICHOST | (unsigned int)NI_NUMERICSERV));
aa7133@att.com84bd3342020-03-16 18:04:57 +0200923 if (ans < 0) {
924 mdclog_write(MDCLOG_ERR, "Failed to get info on connection request. %s\n", strerror(errno));
925 close(peerInfo->fileDescriptor);
dhirajverma9f59b572021-07-23 02:36:13 -0400926 if(peerInfo)
927 free(peerInfo);
sandeekuea662102022-11-08 18:51:20 +0530928 peerInfo = nullptr;
aa7133@att.com84bd3342020-03-16 18:04:57 +0200929 break;
930 }
931 if (mdclog_level_get() >= MDCLOG_DEBUG) {
932 mdclog_write(MDCLOG_DEBUG, "Accepted connection on descriptor %d (host=%s, port=%s)\n", peerInfo->fileDescriptor, peerInfo->hostName, peerInfo->portNumber);
933 }
934 peerInfo->isConnected = false;
935 peerInfo->gotSetup = false;
936 if (addToEpoll(params->epoll_fd,
937 peerInfo,
938 (EPOLLIN | EPOLLET),
939 params->sctpMap, nullptr,
aa7133@att.combe92d712020-03-18 17:30:39 +0200940 0) != 0) {
dhirajverma9f59b572021-07-23 02:36:13 -0400941 if(peerInfo)
942 free(peerInfo);
sandeekuea662102022-11-08 18:51:20 +0530943 peerInfo = nullptr;
aa7133@att.com84bd3342020-03-16 18:04:57 +0200944 break;
945 }
aa7133@att.com6680c3b2020-03-22 12:06:58 +0200946 break;
aa7133@att.com84bd3342020-03-16 18:04:57 +0200947 }
ss412g1a79bdf2019-10-24 12:03:05 +0300948 } else if (params->rmrListenFd == events[i].data.fd) {
949 // got message from XAPP
aa7133@att.comde40d1c2020-08-26 16:19:20 +0300950 //num_of_XAPP_messages.fetch_add(1, std::memory_order_release);
ss412g1a79bdf2019-10-24 12:03:05 +0300951 num_of_messages.fetch_add(1, std::memory_order_release);
952 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.comf13529a2020-08-27 11:21:16 +0300953 mdclog_write(MDCLOG_DEBUG, "new RMR message");
ss412g1a79bdf2019-10-24 12:03:05 +0300954 }
aa7133@att.com6680c3b2020-03-22 12:06:58 +0200955 if (receiveXappMessages(params->sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +0300956 rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200957 message.message.time) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +0300958 mdclog_write(MDCLOG_ERR, "Error handling Xapp message");
959 }
ss412g3bac2da2020-01-05 11:52:19 +0200960 } else if (params->inotifyFD == events[i].data.fd) {
961 mdclog_write(MDCLOG_INFO, "Got event from inotify (configuration update)");
962 handleConfigChange(params);
ss412g1a79bdf2019-10-24 12:03:05 +0300963 } else {
964 /* We RMR_ERR_RETRY have data on the fd waiting to be read. Read and display it.
965 * We must read whatever data is available completely, as we are running
966 * in edge-triggered mode and won't get a notification again for the same data. */
967 num_of_messages.fetch_add(1, std::memory_order_release);
968 if (mdclog_level_get() >= MDCLOG_DEBUG) {
969 mdclog_write(MDCLOG_DEBUG, "new message from SCTP, epoll flags are : %0x", events[i].events);
970 }
971 receiveDataFromSctp(&events[i],
972 params->sctpMap,
973 num_of_SCTP_messages,
974 rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200975 message.message.time);
ss412g1a79bdf2019-10-24 12:03:05 +0300976 }
977
978 clock_gettime(CLOCK_MONOTONIC, &end);
979 if (mdclog_level_get() >= MDCLOG_INFO) {
980 totalTime += ((end.tv_sec + 1.0e-9 * end.tv_nsec) -
981 ((double) start.tv_sec + 1.0e-9 * start.tv_nsec));
982 }
983 if (mdclog_level_get() >= MDCLOG_DEBUG) {
984 mdclog_write(MDCLOG_DEBUG, "message handling is %ld seconds %ld nanoseconds",
985 end.tv_sec - start.tv_sec,
986 end.tv_nsec - start.tv_nsec);
987 }
988 }
dhirajverma6cf28612021-06-23 04:53:34 -0400989#ifdef UNIT_TEST
dhirajverma99990292021-07-05 10:03:59 -0400990 break;
dhirajverma6cf28612021-06-23 04:53:34 -0400991#endif
ss412g1a79bdf2019-10-24 12:03:05 +0300992 }
ss412g1a79bdf2019-10-24 12:03:05 +0300993}
994
995/**
996 *
ss412g3bac2da2020-01-05 11:52:19 +0200997 * @param sctpParams
998 */
999void handleConfigChange(sctp_params_t *sctpParams) {
1000 char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event))));
1001 const struct inotify_event *event;
1002 char *ptr;
dhirajverma60f8bef2021-06-29 07:22:58 -04001003#ifdef UNIT_TEST
1004 struct inotify_event tmpEvent;
1005#endif
ss412g3bac2da2020-01-05 11:52:19 +02001006 path p = (sctpParams->configFilePath + "/" + sctpParams->configFileName).c_str();
1007 auto endlessLoop = true;
1008 while (endlessLoop) {
dhirajverma93b6d9c2021-07-10 00:49:13 -04001009#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
1010 auto len = read(sctpParams->inotifyFD, buf, sizeof buf);
dhirajverma60f8bef2021-06-29 07:22:58 -04001011#else
1012 auto len=10;
1013#endif
ss412g3bac2da2020-01-05 11:52:19 +02001014 if (len == -1) {
dhirajverma93b6d9c2021-07-10 00:49:13 -04001015#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
ss412g3bac2da2020-01-05 11:52:19 +02001016 if (errno != EAGAIN) {
1017 mdclog_write(MDCLOG_ERR, "read %s ", strerror(errno));
1018 endlessLoop = false;
1019 continue;
1020 }
1021 else {
1022 endlessLoop = false;
1023 continue;
1024 }
dhirajverma93b6d9c2021-07-10 00:49:13 -04001025#endif
ss412g3bac2da2020-01-05 11:52:19 +02001026 }
1027
1028 for (ptr = buf; ptr < buf + len; ptr += sizeof(struct inotify_event) + event->len) {
dhirajverma60f8bef2021-06-29 07:22:58 -04001029#ifndef UNIT_TEST
1030 event = (const struct inotify_event *)ptr;
1031#else
1032 tmpEvent.mask = (uint32_t)IN_CLOSE_WRITE;
1033 event = &tmpEvent;
1034#endif
ss412g3bac2da2020-01-05 11:52:19 +02001035 if (event->mask & (uint32_t)IN_ISDIR) {
1036 continue;
1037 }
1038
1039 // the directory name
1040 if (sctpParams->inotifyWD == event->wd) {
1041 // not the directory
1042 }
1043 if (event->len) {
dhirajverma93b6d9c2021-07-10 00:49:13 -04001044#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.comdfc1db92020-03-31 17:32:04 +03001045 auto retVal = strcmp(sctpParams->configFileName.c_str(), event->name);
1046 if (retVal != 0) {
ss412g3bac2da2020-01-05 11:52:19 +02001047 continue;
1048 }
dhirajverma93b6d9c2021-07-10 00:49:13 -04001049#endif
ss412g3bac2da2020-01-05 11:52:19 +02001050 }
1051 // only the file we want
1052 if (event->mask & (uint32_t)IN_CLOSE_WRITE) {
aa7133@att.comdfc1db92020-03-31 17:32:04 +03001053 if (mdclog_level_get() >= MDCLOG_INFO) {
1054 mdclog_write(MDCLOG_INFO, "Configuration file changed");
1055 }
ss412g3bac2da2020-01-05 11:52:19 +02001056 if (exists(p)) {
1057 const int size = 2048;
1058 auto fileSize = file_size(p);
1059 if (fileSize > size) {
1060 mdclog_write(MDCLOG_ERR, "File %s larger than %d", p.string().c_str(), size);
1061 return;
1062 }
1063 } else {
1064 mdclog_write(MDCLOG_ERR, "Configuration File %s not exists", p.string().c_str());
1065 return;
1066 }
1067
1068 ReadConfigFile conf;
1069 if (conf.openConfigFile(p.string()) == -1) {
1070 mdclog_write(MDCLOG_ERR, "Filed to open config file %s, %s",
1071 p.string().c_str(), strerror(errno));
1072 return;
1073 }
ss412g3bac2da2020-01-05 11:52:19 +02001074 auto tmpStr = conf.getStringValue("loglevel");
1075 if (tmpStr.length() == 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +03001076 mdclog_write(MDCLOG_ERR, "illegal loglevel. Set loglevel to MDCLOG_INFO");
ss412g3bac2da2020-01-05 11:52:19 +02001077 tmpStr = "info";
1078 }
1079 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
1080
1081 if ((tmpStr.compare("debug")) == 0) {
1082 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_DEBUG");
1083 sctpParams->logLevel = MDCLOG_DEBUG;
dhirajverma93b6d9c2021-07-10 00:49:13 -04001084 }
1085#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
1086 else if ((tmpStr.compare("info")) == 0) {
ss412g3bac2da2020-01-05 11:52:19 +02001087 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_INFO");
1088 sctpParams->logLevel = MDCLOG_INFO;
1089 } else if ((tmpStr.compare("warning")) == 0) {
1090 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_WARN");
1091 sctpParams->logLevel = MDCLOG_WARN;
1092 } else if ((tmpStr.compare("error")) == 0) {
1093 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_ERR");
1094 sctpParams->logLevel = MDCLOG_ERR;
1095 } else {
aa7133@att.com409f2232020-10-29 13:06:45 +03001096 mdclog_write(MDCLOG_ERR, "illegal loglevel = %s. Set loglevel to MDCLOG_INFO", tmpStr.c_str());
ss412g3bac2da2020-01-05 11:52:19 +02001097 sctpParams->logLevel = MDCLOG_INFO;
1098 }
dhirajverma93b6d9c2021-07-10 00:49:13 -04001099#endif
ss412g3bac2da2020-01-05 11:52:19 +02001100 mdclog_level_set(sctpParams->logLevel);
ss412g3bac2da2020-01-05 11:52:19 +02001101 tmpStr = conf.getStringValue("trace");
1102 if (tmpStr.length() == 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +03001103 mdclog_write(MDCLOG_ERR, "illegal trace. Set trace to stop");
ss412g3bac2da2020-01-05 11:52:19 +02001104 tmpStr = "stop";
1105 }
1106
1107 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
1108 if ((tmpStr.compare("start")) == 0) {
1109 mdclog_write(MDCLOG_INFO, "Trace set to: start");
1110 sctpParams->trace = true;
1111 } else if ((tmpStr.compare("stop")) == 0) {
1112 mdclog_write(MDCLOG_INFO, "Trace set to: stop");
1113 sctpParams->trace = false;
1114 } else {
1115 mdclog_write(MDCLOG_ERR, "Trace was set to wrong value %s, set to stop", tmpStr.c_str());
1116 sctpParams->trace = false;
1117 }
1118 jsonTrace = sctpParams->trace;
ss412g4aeeda22020-06-16 14:12:41 +03001119
ss412g4aeeda22020-06-16 14:12:41 +03001120
ss412g3bac2da2020-01-05 11:52:19 +02001121 endlessLoop = false;
1122 }
dhirajverma60f8bef2021-06-29 07:22:58 -04001123#ifdef UNIT_TEST
1124 break;
1125#endif
ss412g3bac2da2020-01-05 11:52:19 +02001126 }
1127 }
1128}
1129
1130/**
1131 *
1132 * @param event
1133 * @param message
1134 * @param rmrMessageBuffer
1135 * @param params
ss412g3bac2da2020-01-05 11:52:19 +02001136 */
1137void handleEinprogressMessages(struct epoll_event &event,
1138 ReportingMessages_t &message,
1139 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +02001140 sctp_params_t *params) {
ss412g3bac2da2020-01-05 11:52:19 +02001141 auto *peerInfo = (ConnectedCU_t *)event.data.ptr;
1142 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
1143
1144 mdclog_write(MDCLOG_INFO, "file descriptor %d got EPOLLOUT", peerInfo->fileDescriptor);
1145 auto retVal = 0;
1146 socklen_t retValLen = 0;
1147 auto rc = getsockopt(peerInfo->fileDescriptor, SOL_SOCKET, SO_ERROR, &retVal, &retValLen);
1148 if (rc != 0 || retVal != 0) {
dhirajverma93b6d9c2021-07-10 00:49:13 -04001149#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
ss412g3bac2da2020-01-05 11:52:19 +02001150 if (rc != 0) {
1151 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
1152 "%s|Failed SCTP Connection, after EINPROGRESS the getsockopt%s",
1153 peerInfo->enodbName, strerror(errno));
1154 } else if (retVal != 0) {
1155 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
1156 "%s|Failed SCTP Connection after EINPROGRESS, SO_ERROR",
1157 peerInfo->enodbName);
1158 }
1159
1160 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1161 message.message.asnLength = rmrMessageBuffer.sendMessage->len;
1162 mdclog_write(MDCLOG_ERR, "%s", rmrMessageBuffer.sendMessage->payload);
1163 message.message.direction = 'N';
aa7133@att.combe92d712020-03-18 17:30:39 +02001164 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +02001165 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
1166 }
dhirajverma31768a92021-05-06 12:38:49 +05301167#endif
ss412g3bac2da2020-01-05 11:52:19 +02001168 memset(peerInfo->asnData, 0, peerInfo->asnLength);
1169 peerInfo->asnLength = 0;
1170 peerInfo->mtype = 0;
ss412g3bac2da2020-01-05 11:52:19 +02001171 return;
1172 }
dhirajverma93b6d9c2021-07-10 00:49:13 -04001173#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
ss412g3bac2da2020-01-05 11:52:19 +02001174 peerInfo->isConnected = true;
1175
1176 if (modifyToEpoll(params->epoll_fd, peerInfo, (EPOLLIN | EPOLLET), params->sctpMap, peerInfo->enodbName,
aa7133@att.combe92d712020-03-18 17:30:39 +02001177 peerInfo->mtype) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +02001178 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_MOD");
ss412g3bac2da2020-01-05 11:52:19 +02001179 return;
1180 }
1181
1182 message.message.asndata = (unsigned char *)peerInfo->asnData;
1183 message.message.asnLength = peerInfo->asnLength;
1184 message.message.messageType = peerInfo->mtype;
1185 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
1186 num_of_messages.fetch_add(1, std::memory_order_release);
1187 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1188 mdclog_write(MDCLOG_DEBUG, "send the delayed SETUP/ENDC SETUP to sctp for %s",
1189 message.message.enodbName);
1190 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001191 if (sendSctpMsg(peerInfo, message, params->sctpMap) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +02001192 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1193 mdclog_write(MDCLOG_DEBUG, "Error write to SCTP %s %d", __func__, __LINE__);
1194 }
ss412g3bac2da2020-01-05 11:52:19 +02001195 return;
1196 }
1197
1198 memset(peerInfo->asnData, 0, peerInfo->asnLength);
1199 peerInfo->asnLength = 0;
1200 peerInfo->mtype = 0;
dhirajverma93b6d9c2021-07-10 00:49:13 -04001201#endif
ss412g3bac2da2020-01-05 11:52:19 +02001202}
1203
1204
1205void handlepoll_error(struct epoll_event &event,
1206 ReportingMessages_t &message,
1207 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +02001208 sctp_params_t *params) {
sandeekuea662102022-11-08 18:51:20 +05301209 if ((event.data.fd != params->rmrListenFd) && (event.data.ptr != nullptr)) {
ss412g3bac2da2020-01-05 11:52:19 +02001210 auto *peerInfo = (ConnectedCU_t *)event.data.ptr;
1211 mdclog_write(MDCLOG_ERR, "epoll error, events %0x on fd %d, RAN NAME : %s",
1212 event.events, peerInfo->fileDescriptor, peerInfo->enodbName);
dhirajverma93b6d9c2021-07-10 00:49:13 -04001213#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
ss412g3bac2da2020-01-05 11:52:19 +02001214 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
1215 "%s|Failed SCTP Connection",
1216 peerInfo->enodbName);
1217 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1218 message.message.asnLength = rmrMessageBuffer.sendMessage->len;
1219
1220 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
1221 message.message.direction = 'N';
aa7133@att.combe92d712020-03-18 17:30:39 +02001222 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +02001223 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
1224 }
dhirajverma31768a92021-05-06 12:38:49 +05301225#endif
ss412g3bac2da2020-01-05 11:52:19 +02001226 close(peerInfo->fileDescriptor);
sandeekuea662102022-11-08 18:51:20 +05301227 //params->sctpMap->erase(peerInfo->enodbName);
aa7133@att.combe92d712020-03-18 17:30:39 +02001228 cleanHashEntry((ConnectedCU_t *) event.data.ptr, params->sctpMap);
ss412g3bac2da2020-01-05 11:52:19 +02001229 } else {
1230 mdclog_write(MDCLOG_ERR, "epoll error, events %0x on RMR FD", event.events);
1231 }
ss412g3bac2da2020-01-05 11:52:19 +02001232}
1233/**
1234 *
ss412g1a79bdf2019-10-24 12:03:05 +03001235 * @param socket
1236 * @return
1237 */
1238int setSocketNoBlocking(int socket) {
1239 auto flags = fcntl(socket, F_GETFL, 0);
1240
1241 if (flags == -1) {
ss412g1a79bdf2019-10-24 12:03:05 +03001242 mdclog_write(MDCLOG_ERR, "%s, %s", __FUNCTION__, strerror(errno));
ss412g1a79bdf2019-10-24 12:03:05 +03001243 return -1;
1244 }
1245
1246 flags = (unsigned) flags | (unsigned) O_NONBLOCK;
1247 if (fcntl(socket, F_SETFL, flags) == -1) {
ss412g1a79bdf2019-10-24 12:03:05 +03001248 mdclog_write(MDCLOG_ERR, "%s, %s", __FUNCTION__, strerror(errno));
ss412g1a79bdf2019-10-24 12:03:05 +03001249 return -1;
1250 }
1251
1252 return 0;
1253}
1254
1255/**
1256 *
1257 * @param val
1258 * @param m
ss412g1a79bdf2019-10-24 12:03:05 +03001259 */
aa7133@att.combe92d712020-03-18 17:30:39 +02001260void cleanHashEntry(ConnectedCU_t *val, Sctp_Map_t *m) {
sandeekuea662102022-11-08 18:51:20 +05301261 if(val != nullptr)
1262 {
ss412g1a79bdf2019-10-24 12:03:05 +03001263 char *dummy;
1264 auto port = (uint16_t) strtol(val->portNumber, &dummy, 10);
aa7133@att.com84bd3342020-03-16 18:04:57 +02001265 char searchBuff[2048]{};
ss412g1a79bdf2019-10-24 12:03:05 +03001266
1267 snprintf(searchBuff, sizeof searchBuff, "host:%s:%d", val->hostName, port);
sandeekuea662102022-11-08 18:51:20 +05301268 if(m->find(searchBuff))
1269 {
ss412g1a79bdf2019-10-24 12:03:05 +03001270 m->erase(searchBuff);
sandeekuea662102022-11-08 18:51:20 +05301271 }
ss412g1a79bdf2019-10-24 12:03:05 +03001272
sandeekuea662102022-11-08 18:51:20 +05301273 if(m->find(val->enodbName))
1274 {
1275 mdclog_write(MDCLOG_DEBUG, "remove key enodbName = %s from %s at line %d", val->enodbName, __FUNCTION__, __LINE__);
ss412g1a79bdf2019-10-24 12:03:05 +03001276 m->erase(val->enodbName);
sandeekuea662102022-11-08 18:51:20 +05301277 }
dhirajverma31768a92021-05-06 12:38:49 +05301278#ifndef UNIT_TEST
sandeekuea662102022-11-08 18:51:20 +05301279 if(val) {
1280 free(val);
1281 val = nullptr;
1282 }
dhirajverma31768a92021-05-06 12:38:49 +05301283#endif
sandeekuea662102022-11-08 18:51:20 +05301284 }
ss412g1a79bdf2019-10-24 12:03:05 +03001285}
1286
1287/**
1288 *
aa7133@att.com409f2232020-10-29 13:06:45 +03001289 * @param fd file descriptor
ss412g1a79bdf2019-10-24 12:03:05 +03001290 * @param data the asn data to send
1291 * @param len length of the data
1292 * @param enodbName the enodbName as in the map for printing purpose
1293 * @param m map host information
1294 * @param mtype message number
aa7133@att.com409f2232020-10-29 13:06:45 +03001295 * @return 0 success, a negative number on fail
ss412g1a79bdf2019-10-24 12:03:05 +03001296 */
aa7133@att.combe92d712020-03-18 17:30:39 +02001297int sendSctpMsg(ConnectedCU_t *peerInfo, ReportingMessages_t &message, Sctp_Map_t *m) {
ss412g1a79bdf2019-10-24 12:03:05 +03001298 auto loglevel = mdclog_level_get();
dhirajverma99990292021-07-05 10:03:59 -04001299#ifndef UNIT_TEST
ss412g1a79bdf2019-10-24 12:03:05 +03001300 int fd = peerInfo->fileDescriptor;
czichy2175dd32022-05-17 15:24:51 +03001301 int streamId = fetchStreamId(peerInfo,message);
dhirajverma99990292021-07-05 10:03:59 -04001302#else
1303 int fd = FILE_DESCRIPTOR;
czichy2175dd32022-05-17 15:24:51 +03001304 int streamId = 0;
dhirajverma99990292021-07-05 10:03:59 -04001305#endif
ss412g1a79bdf2019-10-24 12:03:05 +03001306 if (loglevel >= MDCLOG_DEBUG) {
1307 mdclog_write(MDCLOG_DEBUG, "Send SCTP message for CU %s, %s",
1308 message.message.enodbName, __FUNCTION__);
1309 }
1310
1311 while (true) {
czichy2175dd32022-05-17 15:24:51 +03001312 if (sctp_sendmsg(fd,message.message.asndata, message.message.asnLength,(struct sockaddr *) NULL, 0, 0, 0,streamId,0,0) < 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03001313 if (errno == EINTR) {
1314 continue;
1315 }
1316 mdclog_write(MDCLOG_ERR, "error writing to CU a message, %s ", strerror(errno));
dhirajverma93b6d9c2021-07-10 00:49:13 -04001317#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.com84bd3342020-03-16 18:04:57 +02001318 if (!peerInfo->isConnected) {
1319 mdclog_write(MDCLOG_ERR, "connection to CU %s is still in progress.", message.message.enodbName);
aa7133@att.com84bd3342020-03-16 18:04:57 +02001320 return -1;
1321 }
dhirajverma93b6d9c2021-07-10 00:49:13 -04001322#endif
1323#ifndef UNIT_TEST
aa7133@att.combe92d712020-03-18 17:30:39 +02001324 cleanHashEntry(peerInfo, m);
dhirajvermac40ea1f2021-07-08 12:52:30 -04001325 close(fd);
dhirajverma93b6d9c2021-07-10 00:49:13 -04001326#endif
ss412g1a79bdf2019-10-24 12:03:05 +03001327 char key[MAX_ENODB_NAME_SIZE * 2];
1328 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", message.message.enodbName,
1329 message.message.messageType);
1330 if (loglevel >= MDCLOG_DEBUG) {
1331 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
1332 }
aa7133@att.com96c9b882020-03-25 14:50:40 +02001333 auto tmp = m->find(key);
ss412g1a79bdf2019-10-24 12:03:05 +03001334 if (tmp) {
1335 free(tmp);
sandeekuea662102022-11-08 18:51:20 +05301336 tmp = nullptr;
ss412g1a79bdf2019-10-24 12:03:05 +03001337 }
1338 m->erase(key);
dhirajverma6cf28612021-06-23 04:53:34 -04001339#ifndef UNIT_TEST
ss412g1a79bdf2019-10-24 12:03:05 +03001340 return -1;
dhirajverma6cf28612021-06-23 04:53:34 -04001341#endif
ss412g1a79bdf2019-10-24 12:03:05 +03001342 }
1343 message.message.direction = 'D';
1344 // send report.buffer of size
1345 buildJsonMessage(message);
1346
1347 if (loglevel >= MDCLOG_DEBUG) {
1348 mdclog_write(MDCLOG_DEBUG,
1349 "SCTP message for CU %s sent from %s",
1350 message.message.enodbName,
1351 __FUNCTION__);
1352 }
ss412g1a79bdf2019-10-24 12:03:05 +03001353 return 0;
1354 }
1355}
1356
1357/**
1358 *
1359 * @param message
1360 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001361 */
aa7133@att.combe92d712020-03-18 17:30:39 +02001362void getRequestMetaData(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03001363 message.message.asndata = rmrMessageBuffer.rcvMessage->payload;
1364 message.message.asnLength = rmrMessageBuffer.rcvMessage->len;
1365
1366 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1367 mdclog_write(MDCLOG_DEBUG, "Message from Xapp RAN name = %s message length = %ld",
1368 message.message.enodbName, (unsigned long) message.message.asnLength);
1369 }
ss412g1a79bdf2019-10-24 12:03:05 +03001370}
1371
1372
ss412g1a79bdf2019-10-24 12:03:05 +03001373
1374/**
1375 *
1376 * @param events
1377 * @param sctpMap
1378 * @param numOfMessages
1379 * @param rmrMessageBuffer
1380 * @param ts
ss412g1a79bdf2019-10-24 12:03:05 +03001381 * @return
1382 */
1383int receiveDataFromSctp(struct epoll_event *events,
1384 Sctp_Map_t *sctpMap,
1385 int &numOfMessages,
1386 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +02001387 struct timespec &ts) {
ss412g1a79bdf2019-10-24 12:03:05 +03001388 /* We have data on the fd waiting to be read. Read and display it.
1389 * We must read whatever data is available completely, as we are running
1390 * in edge-triggered mode and won't get a notification again for the same data. */
aa7133@att.com84bd3342020-03-16 18:04:57 +02001391 ReportingMessages_t message {};
1392 auto done = 0;
ss412g1a79bdf2019-10-24 12:03:05 +03001393 auto loglevel = mdclog_level_get();
czichy2175dd32022-05-17 15:24:51 +03001394 struct sctp_sndrcvinfo sndrcvinfo;
1395 int flags;
1396 int streamId;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001397
ss412g1a79bdf2019-10-24 12:03:05 +03001398 // get the identity of the interface
sandeekuea662102022-11-08 18:51:20 +05301399 if (events->data.ptr != nullptr){
aa7133@att.com84bd3342020-03-16 18:04:57 +02001400 message.peerInfo = (ConnectedCU_t *)events->data.ptr;
sandeekuea662102022-11-08 18:51:20 +05301401 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001402
ss412g1a79bdf2019-10-24 12:03:05 +03001403 struct timespec start{0, 0};
aa7133@att.com409f2232020-10-29 13:06:45 +03001404 struct timespec decodeStart{0, 0};
ss412g1a79bdf2019-10-24 12:03:05 +03001405 struct timespec end{0, 0};
1406
1407 E2AP_PDU_t *pdu = nullptr;
1408
ss412g1a79bdf2019-10-24 12:03:05 +03001409 while (true) {
1410 if (loglevel >= MDCLOG_DEBUG) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001411 mdclog_write(MDCLOG_DEBUG, "Start Read from SCTP %d fd", message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001412 clock_gettime(CLOCK_MONOTONIC, &start);
1413 }
1414 // read the buffer directly to rmr payload
1415 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
dhirajverma99990292021-07-05 10:03:59 -04001416#ifndef UNIT_TEST
ss412g1a79bdf2019-10-24 12:03:05 +03001417 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
czichy2175dd32022-05-17 15:24:51 +03001418 sctp_recvmsg(message.peerInfo->fileDescriptor, rmrMessageBuffer.sendMessage->payload, RECEIVE_SCTP_BUFFER_SIZE,(struct sockaddr *) NULL, 0, &sndrcvinfo, &flags);
1419 mdclog_write(MDCLOG_DEBUG, "Start Read from SCTP fd %d stream %d ", message.peerInfo->fileDescriptor, sndrcvinfo.sinfo_stream);
1420 streamId = sndrcvinfo.sinfo_stream;
dhirajverma99990292021-07-05 10:03:59 -04001421#else
1422 message.message.asnLength = rmrMessageBuffer.sendMessage->len;
czichy2175dd32022-05-17 15:24:51 +03001423 streamId = 0;
dhirajverma99990292021-07-05 10:03:59 -04001424#endif
aa7133@att.com84bd3342020-03-16 18:04:57 +02001425
ss412g1a79bdf2019-10-24 12:03:05 +03001426 if (loglevel >= MDCLOG_DEBUG) {
1427 mdclog_write(MDCLOG_DEBUG, "Finish Read from SCTP %d fd message length = %ld",
aa7133@att.com96c9b882020-03-25 14:50:40 +02001428 message.peerInfo->fileDescriptor, message.message.asnLength);
ss412g1a79bdf2019-10-24 12:03:05 +03001429 }
aa7133@att.com16c799b2020-03-24 18:01:51 +02001430
aa7133@att.com84bd3342020-03-16 18:04:57 +02001431 memcpy(message.message.enodbName, message.peerInfo->enodbName, sizeof(message.peerInfo->enodbName));
ss412g1a79bdf2019-10-24 12:03:05 +03001432 message.message.direction = 'U';
1433 message.message.time.tv_nsec = ts.tv_nsec;
1434 message.message.time.tv_sec = ts.tv_sec;
1435
1436 if (message.message.asnLength < 0) {
1437 if (errno == EINTR) {
1438 continue;
1439 }
1440 /* If errno == EAGAIN, that means we have read all
aa7133@att.com84bd3342020-03-16 18:04:57 +02001441 data. So goReportingMessages_t back to the main loop. */
ss412g1a79bdf2019-10-24 12:03:05 +03001442 if (errno != EAGAIN) {
1443 mdclog_write(MDCLOG_ERR, "Read error, %s ", strerror(errno));
1444 done = 1;
1445 } else if (loglevel >= MDCLOG_DEBUG) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001446 mdclog_write(MDCLOG_DEBUG, "EAGAIN - descriptor = %d", message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001447 }
1448 break;
1449 } else if (message.message.asnLength == 0) {
1450 /* End of file. The remote has closed the connection. */
1451 if (loglevel >= MDCLOG_INFO) {
1452 mdclog_write(MDCLOG_INFO, "END of File Closed connection - descriptor = %d",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001453 message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001454 }
1455 done = 1;
1456 break;
1457 }
1458
ss412g1a79bdf2019-10-24 12:03:05 +03001459 if (loglevel >= MDCLOG_DEBUG) {
swc58d4f5d2020-10-20 15:08:38 +09001460 char printBuffer[RECEIVE_SCTP_BUFFER_SIZE]{};
ss412g1a79bdf2019-10-24 12:03:05 +03001461 char *tmp = printBuffer;
1462 for (size_t i = 0; i < (size_t)message.message.asnLength; ++i) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001463 snprintf(tmp, 3, "%02x", message.message.asndata[i]);
ss412g1a79bdf2019-10-24 12:03:05 +03001464 tmp += 2;
1465 }
1466 printBuffer[message.message.asnLength] = 0;
1467 clock_gettime(CLOCK_MONOTONIC, &end);
1468 mdclog_write(MDCLOG_DEBUG, "Before Encoding E2AP PDU for : %s, Read time is : %ld seconds, %ld nanoseconds",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001469 message.peerInfo->enodbName, end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec);
ss412g1a79bdf2019-10-24 12:03:05 +03001470 mdclog_write(MDCLOG_DEBUG, "PDU buffer length = %ld, data = : %s", message.message.asnLength,
1471 printBuffer);
aa7133@att.com409f2232020-10-29 13:06:45 +03001472 clock_gettime(CLOCK_MONOTONIC, &decodeStart);
ss412g1a79bdf2019-10-24 12:03:05 +03001473 }
dhirajverma99990292021-07-05 10:03:59 -04001474#ifndef UNIT_TEST
aa7133@att.com7b437f72020-03-30 13:25:54 +03001475 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, (void **) &pdu,
dhirajverma99990292021-07-05 10:03:59 -04001476 message.message.asndata, message.message.asnLength);
1477#else
1478 asn_dec_rval_t rval = {RC_OK, 0};
1479 pdu = (E2AP_PDU_t*)rmrMessageBuffer.sendMessage->tp_buf;
1480#endif
ss412g1a79bdf2019-10-24 12:03:05 +03001481 if (rval.code != RC_OK) {
1482 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2AP PDU from RAN : %s", rval.code,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001483 message.peerInfo->enodbName);
dhirajverma9f59b572021-07-23 02:36:13 -04001484 if (pdu != nullptr) {
1485 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
1486 pdu = nullptr;
1487 }
ss412g1a79bdf2019-10-24 12:03:05 +03001488 break;
1489 }
1490
1491 if (loglevel >= MDCLOG_DEBUG) {
1492 clock_gettime(CLOCK_MONOTONIC, &end);
1493 mdclog_write(MDCLOG_DEBUG, "After Encoding E2AP PDU for : %s, Read time is : %ld seconds, %ld nanoseconds",
aa7133@att.com409f2232020-10-29 13:06:45 +03001494 message.peerInfo->enodbName, end.tv_sec - decodeStart.tv_sec, end.tv_nsec - decodeStart.tv_nsec);
ss412g1a79bdf2019-10-24 12:03:05 +03001495 char *printBuffer;
1496 size_t size;
1497 FILE *stream = open_memstream(&printBuffer, &size);
1498 asn_fprint(stream, &asn_DEF_E2AP_PDU, pdu);
1499 mdclog_write(MDCLOG_DEBUG, "Encoding E2AP PDU past : %s", printBuffer);
aa7133@att.com409f2232020-10-29 13:06:45 +03001500 clock_gettime(CLOCK_MONOTONIC, &decodeStart);
Byonggon Chuncae9bd52020-11-18 13:37:29 +09001501
1502 fclose(stream);
1503 free(printBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001504 }
1505
1506 switch (pdu->present) {
1507 case E2AP_PDU_PR_initiatingMessage: {//initiating message
czichy2175dd32022-05-17 15:24:51 +03001508 asnInitiatingRequest(pdu, sctpMap,message, rmrMessageBuffer, streamId);
ss412g1a79bdf2019-10-24 12:03:05 +03001509 break;
1510 }
1511 case E2AP_PDU_PR_successfulOutcome: { //successful outcome
aa7133@att.com409f2232020-10-29 13:06:45 +03001512 asnSuccessfulMsg(pdu, sctpMap, message, rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001513 break;
1514 }
1515 case E2AP_PDU_PR_unsuccessfulOutcome: { //Unsuccessful Outcome
aa7133@att.com7b437f72020-03-30 13:25:54 +03001516 asnUnSuccsesfulMsg(pdu, sctpMap, message, rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001517 break;
1518 }
1519 default:
1520 mdclog_write(MDCLOG_ERR, "Unknown index %d in E2AP PDU", pdu->present);
1521 break;
1522 }
1523 if (loglevel >= MDCLOG_DEBUG) {
1524 clock_gettime(CLOCK_MONOTONIC, &end);
1525 mdclog_write(MDCLOG_DEBUG,
1526 "After processing message and sent to rmr for : %s, Read time is : %ld seconds, %ld nanoseconds",
aa7133@att.com409f2232020-10-29 13:06:45 +03001527 message.peerInfo->enodbName, end.tv_sec - decodeStart.tv_sec, end.tv_nsec - decodeStart.tv_nsec);
ss412g1a79bdf2019-10-24 12:03:05 +03001528 }
1529 numOfMessages++;
dhirajverma9f59b572021-07-23 02:36:13 -04001530#ifndef UNIT_TEST
ss412g1a79bdf2019-10-24 12:03:05 +03001531 if (pdu != nullptr) {
dhirajverma9f59b572021-07-23 02:36:13 -04001532 // ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, pdu); /* With reset we were not freeing the memory and was causing the leak here. */
1533 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
1534 pdu = nullptr;
ss412g1a79bdf2019-10-24 12:03:05 +03001535 }
dhirajverma9f59b572021-07-23 02:36:13 -04001536#else
dhirajverma99990292021-07-05 10:03:59 -04001537 done = 1;
1538 break;
1539#endif
ss412g1a79bdf2019-10-24 12:03:05 +03001540 }
ss412g1a79bdf2019-10-24 12:03:05 +03001541
1542 if (done) {
1543 if (loglevel >= MDCLOG_INFO) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001544 mdclog_write(MDCLOG_INFO, "Closed connection - descriptor = %d", message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001545 }
1546 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
1547 snprintf((char *)rmrMessageBuffer.sendMessage->payload,
aa7133@att.com96c9b882020-03-25 14:50:40 +02001548 256,
1549 "%s|CU disconnected unexpectedly",
1550 message.peerInfo->enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001551 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
dhirajverma93b6d9c2021-07-10 00:49:13 -04001552#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
ss412g1a79bdf2019-10-24 12:03:05 +03001553 if (sendRequestToXapp(message,
1554 RIC_SCTP_CONNECTION_FAILURE,
aa7133@att.combe92d712020-03-18 17:30:39 +02001555 rmrMessageBuffer) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03001556 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
1557 }
dhirajverma99990292021-07-05 10:03:59 -04001558#endif
ss412g1a79bdf2019-10-24 12:03:05 +03001559
1560 /* Closing descriptor make epoll remove it from the set of descriptors which are monitored. */
sandeekuea662102022-11-08 18:51:20 +05301561#ifndef UNIT_TEST
1562 pthread_mutex_lock(&thread_lock);
1563 if (fcntl(message.peerInfo->fileDescriptor, F_GETFD) != -1) {
1564 mdclog_write(MDCLOG_DEBUG, "Closing connection - descriptor = %d", message.peerInfo->fileDescriptor);
1565 close(message.peerInfo->fileDescriptor);
1566 cleanHashEntry((ConnectedCU_t *) events->data.ptr, sctpMap);
1567 }
1568 pthread_mutex_unlock(&thread_lock);
1569#else
aa7133@att.com84bd3342020-03-16 18:04:57 +02001570 close(message.peerInfo->fileDescriptor);
aa7133@att.combe92d712020-03-18 17:30:39 +02001571 cleanHashEntry((ConnectedCU_t *) events->data.ptr, sctpMap);
sandeekuea662102022-11-08 18:51:20 +05301572#endif
ss412g1a79bdf2019-10-24 12:03:05 +03001573 }
1574 if (loglevel >= MDCLOG_DEBUG) {
1575 clock_gettime(CLOCK_MONOTONIC, &end);
1576 mdclog_write(MDCLOG_DEBUG, "from receive SCTP to send RMR time is %ld seconds and %ld nanoseconds",
1577 end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec);
1578
1579 }
ss412g1a79bdf2019-10-24 12:03:05 +03001580 return 0;
1581}
1582
aa7133@att.com409f2232020-10-29 13:06:45 +03001583static void buildAndSendSetupRequest(ReportingMessages_t &message,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001584 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001585 E2AP_PDU_t *pdu/*,
ss412g4aeeda22020-06-16 14:12:41 +03001586 string const &messageName,
1587 string const &ieName,
1588 vector<string> &functionsToAdd_v,
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001589 vector<string> &functionsToModified_v*/) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001590 auto logLevel = mdclog_level_get();
aa7133@att.com84bd3342020-03-16 18:04:57 +02001591 // now we can send the data to e2Mgr
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001592
ss412g4aeeda22020-06-16 14:12:41 +03001593 asn_enc_rval_t er;
1594 auto buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
Byonggon Chun51885f32020-11-18 15:31:04 +09001595 unsigned char *buffer = nullptr;
1596 buffer = (unsigned char *) calloc(buffer_size, sizeof(unsigned char));
1597 if(!buffer)
1598 {
dhirajverma93b6d9c2021-07-10 00:49:13 -04001599#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
Byonggon Chun51885f32020-11-18 15:31:04 +09001600 mdclog_write(MDCLOG_ERR, "Allocating buffer for %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1601 return;
dhirajverma93b6d9c2021-07-10 00:49:13 -04001602#endif
Byonggon Chun51885f32020-11-18 15:31:04 +09001603 }
ss412g4aeeda22020-06-16 14:12:41 +03001604 while (true) {
ss412g4aeeda22020-06-16 14:12:41 +03001605 er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu, buffer, buffer_size);
1606 if (er.encoded == -1) {
dhirajverma93b6d9c2021-07-10 00:49:13 -04001607#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
ss412g4aeeda22020-06-16 14:12:41 +03001608 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1609 return;
dhirajverma93b6d9c2021-07-10 00:49:13 -04001610#endif
ss412g4aeeda22020-06-16 14:12:41 +03001611 } else if (er.encoded > (ssize_t) buffer_size) {
1612 buffer_size = er.encoded + 128;
dhirajverma93b6d9c2021-07-10 00:49:13 -04001613#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
ss412g4aeeda22020-06-16 14:12:41 +03001614 mdclog_write(MDCLOG_WARN, "Buffer of size %d is to small for %s. Reallocate buffer of size %d",
1615 (int) buffer_size,
1616 asn_DEF_E2AP_PDU.name, buffer_size);
1617 buffer_size = er.encoded + 128;
Byonggon Chun51885f32020-11-18 15:31:04 +09001618
1619 unsigned char *newBuffer = nullptr;
1620 newBuffer = (unsigned char *) realloc(buffer, buffer_size);
1621 if (!newBuffer)
1622 {
1623 // out of memory
1624 mdclog_write(MDCLOG_ERR, "Reallocating buffer for %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1625 free(buffer);
sandeekuea662102022-11-08 18:51:20 +05301626 buffer = nullptr;
Byonggon Chun51885f32020-11-18 15:31:04 +09001627 return;
1628 }
1629 buffer = newBuffer;
ss412g4aeeda22020-06-16 14:12:41 +03001630 continue;
dhirajverma93b6d9c2021-07-10 00:49:13 -04001631#endif
ss412g4aeeda22020-06-16 14:12:41 +03001632 }
1633 buffer[er.encoded] = '\0';
1634 break;
1635 }
1636 // encode to xml
1637
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001638 string res((char *)buffer);
1639 res.erase(std::remove(res.begin(), res.end(), '\n'), res.end());
1640 res.erase(std::remove(res.begin(), res.end(), '\t'), res.end());
1641 res.erase(std::remove(res.begin(), res.end(), ' '), res.end());
1642
1643// string res {};
1644// if (!functionsToAdd_v.empty() || !functionsToModified_v.empty()) {
1645// res = buildXmlData(messageName, ieName, functionsToAdd_v, functionsToModified_v, buffer, (size_t) er.encoded);
1646// }
ss412g4aeeda22020-06-16 14:12:41 +03001647 rmr_mbuf_t *rmrMsg;
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001648// if (res.length() == 0) {
1649// rmrMsg = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, buffer_size + 256);
1650// rmrMsg->len = snprintf((char *) rmrMsg->payload, RECEIVE_SCTP_BUFFER_SIZE * 2, "%s:%d|%s",
1651// message.peerInfo->sctpParams->myIP.c_str(),
1652// message.peerInfo->sctpParams->rmrPort,
1653// buffer);
1654// } else {
ss412g4aeeda22020-06-16 14:12:41 +03001655 rmrMsg = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, (int)res.length() + 256);
1656 rmrMsg->len = snprintf((char *) rmrMsg->payload, res.length() + 256, "%s:%d|%s",
1657 message.peerInfo->sctpParams->myIP.c_str(),
1658 message.peerInfo->sctpParams->rmrPort,
1659 res.c_str());
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001660// }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001661
ss412g4aeeda22020-06-16 14:12:41 +03001662 if (logLevel >= MDCLOG_DEBUG) {
1663 mdclog_write(MDCLOG_DEBUG, "Setup request of size %d :\n %s\n", rmrMsg->len, rmrMsg->payload);
1664 }
1665 // send to RMR
1666 rmrMsg->mtype = message.message.messageType;
1667 rmrMsg->state = 0;
1668 rmr_bytes2meid(rmrMsg, (unsigned char *) message.message.enodbName, strlen(message.message.enodbName));
aa7133@att.com84bd3342020-03-16 18:04:57 +02001669
ss412g4aeeda22020-06-16 14:12:41 +03001670 static unsigned char tx[32];
1671 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1672 rmr_bytes2xact(rmrMsg, tx, strlen((const char *) tx));
dhirajverma60f8bef2021-06-29 07:22:58 -04001673#ifndef UNIT_TEST
ss412g4aeeda22020-06-16 14:12:41 +03001674 rmrMsg = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMsg);
dhirajverma60f8bef2021-06-29 07:22:58 -04001675#endif
ss412g4aeeda22020-06-16 14:12:41 +03001676 if (rmrMsg == nullptr) {
1677 mdclog_write(MDCLOG_ERR, "RMR failed to send returned nullptr");
1678 } else if (rmrMsg->state != 0) {
1679 char meid[RMR_MAX_MEID]{};
1680 if (rmrMsg->state == RMR_ERR_RETRY) {
1681 usleep(5);
1682 rmrMsg->state = 0;
1683 mdclog_write(MDCLOG_INFO, "RETRY sending Message %d to Xapp from %s",
1684 rmrMsg->mtype, rmr_get_meid(rmrMsg, (unsigned char *) meid));
dhirajverma60f8bef2021-06-29 07:22:58 -04001685#ifndef UNIT_TEST
ss412g4aeeda22020-06-16 14:12:41 +03001686 rmrMsg = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMsg);
dhirajverma60f8bef2021-06-29 07:22:58 -04001687#endif
ss412g4aeeda22020-06-16 14:12:41 +03001688 if (rmrMsg == nullptr) {
1689 mdclog_write(MDCLOG_ERR, "RMR failed send returned nullptr");
1690 } else if (rmrMsg->state != 0) {
1691 mdclog_write(MDCLOG_ERR,
1692 "RMR Retry failed %s sending request %d to Xapp from %s",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001693 translateRmrErrorMessages(rmrMsg->state).c_str(),
1694 rmrMsg->mtype,
1695 rmr_get_meid(rmrMsg, (unsigned char *) meid));
1696 }
ss412g4aeeda22020-06-16 14:12:41 +03001697 } else {
1698 mdclog_write(MDCLOG_ERR, "RMR failed: %s. sending request %d to Xapp from %s",
1699 translateRmrErrorMessages(rmrMsg->state).c_str(),
1700 rmrMsg->mtype,
1701 rmr_get_meid(rmrMsg, (unsigned char *) meid));
aa7133@att.combc110772020-03-25 09:45:14 +02001702 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001703 }
ss412g4aeeda22020-06-16 14:12:41 +03001704 message.peerInfo->gotSetup = true;
1705 buildJsonMessage(message);
Byonggon Chun51885f32020-11-18 15:31:04 +09001706
ss412g4aeeda22020-06-16 14:12:41 +03001707 if (rmrMsg != nullptr) {
1708 rmr_free_msg(rmrMsg);
1709 }
Byonggon Chun51885f32020-11-18 15:31:04 +09001710 free(buffer);
sandeekuea662102022-11-08 18:51:20 +05301711 buffer = nullptr;
Byonggon Chun51885f32020-11-18 15:31:04 +09001712
1713 return;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001714}
aa7133@att.comc5a15202020-04-05 19:57:01 +03001715
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001716#if 0
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001717int RAN_Function_list_To_Vector(RANfunctions_List_t& list, vector <string> &runFunXML_v) {
1718 auto index = 0;
1719 runFunXML_v.clear();
1720 for (auto j = 0; j < list.list.count; j++) {
1721 auto *raNfunctionItemIEs = (RANfunction_ItemIEs_t *)list.list.array[j];
1722 if (raNfunctionItemIEs->id == ProtocolIE_ID_id_RANfunction_Item &&
1723 (raNfunctionItemIEs->value.present == RANfunction_ItemIEs__value_PR_RANfunction_Item)) {
1724 // encode to xml
1725 E2SM_gNB_NRT_RANfunction_Definition_t *ranFunDef = nullptr;
1726 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER,
1727 &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition,
1728 (void **)&ranFunDef,
1729 raNfunctionItemIEs->value.choice.RANfunction_Item.ranFunctionDefinition.buf,
1730 raNfunctionItemIEs->value.choice.RANfunction_Item.ranFunctionDefinition.size);
1731 if (rval.code != RC_OK) {
1732 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2SM message from : %s",
1733 rval.code,
1734 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name);
1735 return -1;
1736 }
1737
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001738 auto xml_buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
1739 unsigned char xml_buffer[RECEIVE_SCTP_BUFFER_SIZE * 2];
ss412g4aeeda22020-06-16 14:12:41 +03001740 memset(xml_buffer, 0, RECEIVE_SCTP_BUFFER_SIZE * 2);
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001741 // encode to xml
1742 auto er = asn_encode_to_buffer(nullptr,
1743 ATS_BASIC_XER,
1744 &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition,
1745 ranFunDef,
1746 xml_buffer,
1747 xml_buffer_size);
1748 if (er.encoded == -1) {
1749 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s",
1750 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name,
1751 strerror(errno));
1752 } else if (er.encoded > (ssize_t)xml_buffer_size) {
1753 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
1754 (int) xml_buffer_size,
1755 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name, __func__, __LINE__);
1756 } else {
1757 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1758 mdclog_write(MDCLOG_DEBUG, "Encoding E2SM %s PDU number %d : %s",
1759 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name,
1760 index++,
1761 xml_buffer);
1762 }
ss412g4aeeda22020-06-16 14:12:41 +03001763
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001764 string runFuncs = (char *)(xml_buffer);
1765 runFunXML_v.emplace_back(runFuncs);
1766 }
1767 }
1768 }
1769 return 0;
1770}
aa7133@att.comc5a15202020-04-05 19:57:01 +03001771
ss412g4aeeda22020-06-16 14:12:41 +03001772int collectServiceUpdate_RequestData(E2AP_PDU_t *pdu,
1773 Sctp_Map_t *sctpMap,
1774 ReportingMessages_t &message,
1775 vector <string> &RANfunctionsAdded_v,
1776 vector <string> &RANfunctionsModified_v) {
aa7133@att.comc5a15202020-04-05 19:57:01 +03001777 memset(message.peerInfo->enodbName, 0 , MAX_ENODB_NAME_SIZE);
ss412g4aeeda22020-06-16 14:12:41 +03001778 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICserviceUpdate.protocolIEs.list.count; i++) {
1779 auto *ie = pdu->choice.initiatingMessage->value.choice.RICserviceUpdate.protocolIEs.list.array[i];
1780 if (ie->id == ProtocolIE_ID_id_RANfunctionsAdded) {
1781 if (ie->value.present == RICserviceUpdate_IEs__value_PR_RANfunctionsID_List) {
aa7133@att.comc5a15202020-04-05 19:57:01 +03001782 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001783 mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1784 ie->value.choice.RANfunctions_List.list.count);
aa7133@att.comc5a15202020-04-05 19:57:01 +03001785 }
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001786 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsAdded_v) != 0 ) {
1787 return -1;
1788 }
1789 }
1790 } else if (ie->id == ProtocolIE_ID_id_RANfunctionsModified) {
ss412g4aeeda22020-06-16 14:12:41 +03001791 if (ie->value.present == RICserviceUpdate_IEs__value_PR_RANfunctions_List) {
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001792 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1793 mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1794 ie->value.choice.RANfunctions_List.list.count);
1795 }
1796 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsModified_v) != 0 ) {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001797 return -1;
aa7133@att.comc5a15202020-04-05 19:57:01 +03001798 }
1799 }
1800 }
1801 }
1802 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1803 mdclog_write(MDCLOG_DEBUG, "Run function vector have %ld entries",
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001804 RANfunctionsAdded_v.size());
aa7133@att.comc5a15202020-04-05 19:57:01 +03001805 }
1806 return 0;
1807}
ss412g4aeeda22020-06-16 14:12:41 +03001808
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001809#endif
ss412g4aeeda22020-06-16 14:12:41 +03001810
1811
Mohamed Abukar64f37242021-08-15 12:39:56 +00001812void buildE2TPrometheusCounters(sctp_params_t &sctpParams) {
1813 sctpParams.e2tCounters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &sctpParams.prometheusFamily->Add({{"counter", "SetupRequestMsgs"}});
1814 sctpParams.e2tCounters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &sctpParams.prometheusFamily->Add({{"counter", "SetupRequestBytes"}});
1815
1816 sctpParams.e2tCounters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &sctpParams.prometheusFamily->Add({{"counter", "SetupResponseMsgs"}});
1817 sctpParams.e2tCounters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &sctpParams.prometheusFamily->Add({{"counter", "SetupResponseBytes"}});
1818
1819 sctpParams.e2tCounters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_E2setup] = &sctpParams.prometheusFamily->Add({{"counter", "SetupRequestFailureMsgs"}});
1820 sctpParams.e2tCounters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_E2setup] = &sctpParams.prometheusFamily->Add({{"counter", "SetupRequestFailureBytes"}});
1821
czichy2175dd32022-05-17 15:24:51 +03001822 sctpParams.e2tCounters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_E2nodeConfigurationUpdate)] = &sctpParams.prometheusFamily->Add({{"counter", "E2NodeConfigUpdateMsgs"}});
1823 sctpParams.e2tCounters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_E2nodeConfigurationUpdate)] = &sctpParams.prometheusFamily->Add({{"counter", "E2NodeConfigUpdateBytes"}});
1824
1825 sctpParams.e2tCounters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_E2nodeConfigurationUpdate)] = &sctpParams.prometheusFamily->Add({{"counter", "E2NodeConfigUpdateResponseMsgs"}});
1826 sctpParams.e2tCounters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2nodeConfigurationUpdate)] = &sctpParams.prometheusFamily->Add({{"counter", "E2NodeConfigUpdateResponseBytes"}});
1827
1828 sctpParams.e2tCounters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate] = &sctpParams.prometheusFamily->Add({{"counter", "E2NodeConfigUpdateFailureMsgs"}});
1829 sctpParams.e2tCounters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate] = &sctpParams.prometheusFamily->Add({{"counter", "E2NodeConfigUpdateFailureBytes"}});
1830
Mohamed Abukar64f37242021-08-15 12:39:56 +00001831 sctpParams.e2tCounters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_ErrorIndication)] = &sctpParams.prometheusFamily->Add({{"counter", "ErrorIndicationMsgs"}});
1832 sctpParams.e2tCounters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_ErrorIndication)] = &sctpParams.prometheusFamily->Add({{"counter", "ErrorIndicationBytes"}});
1833
1834 sctpParams.e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_Reset] = &sctpParams.prometheusFamily->Add({{"counter", "ResetRequestMsgs"}});
1835 sctpParams.e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_Reset] = &sctpParams.prometheusFamily->Add({{"counter", "ResetRequestBytes"}});
1836
1837 sctpParams.e2tCounters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_Reset] = &sctpParams.prometheusFamily->Add({{"counter", "ResetAckMsgs"}});
1838 sctpParams.e2tCounters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_Reset] = &sctpParams.prometheusFamily->Add({{"counter", "ResetAckBytes"}});
1839
1840 sctpParams.e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate] = &sctpParams.prometheusFamily->Add({{"counter", "RICServiceUpdateMsgs"}});
1841 sctpParams.e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate] = &sctpParams.prometheusFamily->Add({{"counter", "RICServiceUpdateBytes"}});
1842
1843 sctpParams.e2tCounters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate] = &sctpParams.prometheusFamily->Add({{"counter", "RICServiceUpdateRespMsgs"}});
1844 sctpParams.e2tCounters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate] = &sctpParams.prometheusFamily->Add({{"counter", "RICServiceUpdateRespBytes"}});
1845
1846 sctpParams.e2tCounters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate] = &sctpParams.prometheusFamily->Add({{"counter", "RICServiceUpdateFailureMsgs"}});
1847 sctpParams.e2tCounters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate] = &sctpParams.prometheusFamily->Add({{"counter", "RICServiceUpdateFailureBytes"}});
1848
1849 sctpParams.e2tCounters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICcontrol] = &sctpParams.prometheusFamily->Add({{"counter", "RICControlMsgs"}});
1850 sctpParams.e2tCounters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICcontrol] = &sctpParams.prometheusFamily->Add({{"counter", "RICControlBytes"}});
1851
1852 sctpParams.e2tCounters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol] = &sctpParams.prometheusFamily->Add({{"counter", "RICControlAckMsgs"}});
1853 sctpParams.e2tCounters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol] = &sctpParams.prometheusFamily->Add({{"counter", "RICControlAckBytes"}});
1854
1855 sctpParams.e2tCounters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol] = &sctpParams.prometheusFamily->Add({{"counter", "RICControlFailureMsgs"}});
1856 sctpParams.e2tCounters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol] = &sctpParams.prometheusFamily->Add({{"counter", "RICControlFailureBytes"}});
1857
1858 sctpParams.e2tCounters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscription] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionMsgs"}});
1859 sctpParams.e2tCounters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscription] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionBytes"}});
1860
1861 sctpParams.e2tCounters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionAckMsgs"}});
1862 sctpParams.e2tCounters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionAckBytes"}});
1863
1864 sctpParams.e2tCounters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionFailureMsgs"}});
1865 sctpParams.e2tCounters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionFailureBytes"}});
1866
1867 sctpParams.e2tCounters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionDeleteMsgs"}});
1868 sctpParams.e2tCounters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionDeleteBytes"}});
1869
1870 sctpParams.e2tCounters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionDeleteAckMsgs"}});
1871 sctpParams.e2tCounters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionDeleteAckBytes"}});
1872
1873 sctpParams.e2tCounters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionDeleteFailMsgs"}});
1874 sctpParams.e2tCounters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete] = &sctpParams.prometheusFamily->Add({{"counter", "RICSubscriptionDeleteFailBytes"}});
1875
1876 sctpParams.e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICindication] = &sctpParams.prometheusFamily->Add({{"counter", "RICIndicationMsgs"}});
1877 sctpParams.e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICindication] = &sctpParams.prometheusFamily->Add({{"counter", "RICIndicationBytes"}});
1878
1879 sctpParams.e2tCounters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceQuery] = &sctpParams.prometheusFamily->Add({{"counter", "RICServiceQueryMsgs"}});
1880 sctpParams.e2tCounters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceQuery] = &sctpParams.prometheusFamily->Add({{"counter", "RICServiceQueryBytes"}});
1881}
1882
aa7133@att.com409f2232020-10-29 13:06:45 +03001883void buildPrometheusList(ConnectedCU_t *peerInfo, Family<Counter> *prometheusFamily) {
aa7133@att.coma4c80012020-06-24 16:36:47 +03001884 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"SetupRequest", "Messages"}});
1885 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"SetupRequest", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001886
czichy2175dd32022-05-17 15:24:51 +03001887 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_E2nodeConfigurationUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"E2NodeConfigUpdate", "Messages"}});
1888 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_E2nodeConfigurationUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"E2NodeConfigUpdate", "Bytes"}});
1889
aa7133@att.coma4c80012020-06-24 16:36:47 +03001890 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ErrorIndication", "Messages"}});
1891 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ErrorIndication", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001892
aa7133@att.coma4c80012020-06-24 16:36:47 +03001893 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_RICindication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICindication", "Messages"}});
1894 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_RICindication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICindication", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001895
aa7133@att.coma4c80012020-06-24 16:36:47 +03001896 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetRequest", "Messages"}});
1897 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetRequest", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001898
aa7133@att.coma4c80012020-06-24 16:36:47 +03001899 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICserviceUpdate", "Messages"}});
1900 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICserviceUpdate", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001901 // ---------------------------------------------
aa7133@att.coma4c80012020-06-24 16:36:47 +03001902 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetACK", "Messages"}});
1903 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001904
aa7133@att.coma4c80012020-06-24 16:36:47 +03001905 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolACK", "Messages"}});
1906 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001907
aa7133@att.coma4c80012020-06-24 16:36:47 +03001908 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionACK", "Messages"}});
1909 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001910
aa7133@att.coma4c80012020-06-24 16:36:47 +03001911 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteACK", "Messages"}});
1912 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001913 //-------------------------------------------------------------
1914
aa7133@att.coma4c80012020-06-24 16:36:47 +03001915 peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolFailure", "Messages"}});
1916 peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001917
aa7133@att.coma4c80012020-06-24 16:36:47 +03001918 peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionFailure", "Messages"}});
1919 peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001920
aa7133@att.coma4c80012020-06-24 16:36:47 +03001921 peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteFailure", "Messages"}});
1922 peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001923
1924 //====================================================================================
aa7133@att.coma4c80012020-06-24 16:36:47 +03001925 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ErrorIndication", "Messages"}});
1926 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ErrorIndication", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001927
aa7133@att.coma4c80012020-06-24 16:36:47 +03001928 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetRequest", "Messages"}});
1929 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetRequest", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001930
aa7133@att.coma4c80012020-06-24 16:36:47 +03001931 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICcontrol", "Messages"}});
1932 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICcontrol", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001933
aa7133@att.coma4c80012020-06-24 16:36:47 +03001934 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICserviceQuery)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceQuery", "Messages"}});
1935 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICserviceQuery)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceQuery", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001936
aa7133@att.coma4c80012020-06-24 16:36:47 +03001937 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscription", "Messages"}});
1938 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscription", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001939
aa7133@att.coma4c80012020-06-24 16:36:47 +03001940 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscriptionDelete", "Messages"}});
1941 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscriptionDelete", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001942 //---------------------------------------------------------------------------------------------------------
aa7133@att.coma4c80012020-06-24 16:36:47 +03001943 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupResponse", "Messages"}});
1944 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupResponse", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001945
czichy2175dd32022-05-17 15:24:51 +03001946 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_E2nodeConfigurationUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"E2NodeConfigUpdateSuccess", "Messages"}});
1947 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2nodeConfigurationUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"E2NodeConfigUpdateSuccess", "Bytes"}});
1948
aa7133@att.coma4c80012020-06-24 16:36:47 +03001949 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetACK", "Messages"}});
1950 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001951
aa7133@att.coma4c80012020-06-24 16:36:47 +03001952 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateResponse", "Messages"}});
1953 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateResponse", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001954 //----------------------------------------------------------------------------------------------------------------
aa7133@att.coma4c80012020-06-24 16:36:47 +03001955 peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupRequestFailure", "Messages"}});
1956 peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupRequestFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001957
czichy2175dd32022-05-17 15:24:51 +03001958 peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_E2nodeConfigurationUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"E2NodeConfigUpdateFailure", "Messages"}});
1959 peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2nodeConfigurationUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"E2NodeConfigUpdateFailure", "Bytes"}});
1960
aa7133@att.coma4c80012020-06-24 16:36:47 +03001961 peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateFailure", "Messages"}});
1962 peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001963}
Mohamed Abukar64f37242021-08-15 12:39:56 +00001964
ss412g4aeeda22020-06-16 14:12:41 +03001965/**
1966 *
1967 * @param pdu
1968 * @param sctpMap
1969 * @param message
1970 * @param RANfunctionsAdded_v
1971 * @return
1972 */
1973int collectSetupRequestData(E2AP_PDU_t *pdu,
1974 Sctp_Map_t *sctpMap,
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001975 ReportingMessages_t &message /*, vector <string> &RANfunctionsAdded_v*/) {
ss412g4aeeda22020-06-16 14:12:41 +03001976 memset(message.peerInfo->enodbName, 0 , MAX_ENODB_NAME_SIZE);
1977 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.count; i++) {
1978 auto *ie = pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.array[i];
1979 if (ie->id == ProtocolIE_ID_id_GlobalE2node_ID) {
1980 // get the ran name for meid
1981 if (ie->value.present == E2setupRequestIEs__value_PR_GlobalE2node_ID) {
1982 if (buildRanName(message.peerInfo->enodbName, ie) < 0) {
1983 mdclog_write(MDCLOG_ERR, "Bad param in E2setupRequestIEs GlobalE2node_ID.\n");
aa7133@att.com409f2232020-10-29 13:06:45 +03001984 // no message will be sent
ss412g4aeeda22020-06-16 14:12:41 +03001985 return -1;
1986 }
1987
1988 memcpy(message.message.enodbName, message.peerInfo->enodbName, strlen(message.peerInfo->enodbName));
1989 sctpMap->setkey(message.message.enodbName, message.peerInfo);
1990 }
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001991 } /*else if (ie->id == ProtocolIE_ID_id_RANfunctionsAdded) {
ss412g4aeeda22020-06-16 14:12:41 +03001992 if (ie->value.present == E2setupRequestIEs__value_PR_RANfunctions_List) {
1993 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1994 mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1995 ie->value.choice.RANfunctions_List.list.count);
1996 }
1997 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsAdded_v) != 0 ) {
1998 return -1;
1999 }
2000 }
aa7133@att.comdf4f1822020-06-16 16:23:53 +03002001 } */
ss412g4aeeda22020-06-16 14:12:41 +03002002 }
aa7133@att.comdf4f1822020-06-16 16:23:53 +03002003// if (mdclog_level_get() >= MDCLOG_DEBUG) {
2004// mdclog_write(MDCLOG_DEBUG, "Run function vector have %ld entries",
2005// RANfunctionsAdded_v.size());
2006// }
ss412g4aeeda22020-06-16 14:12:41 +03002007 return 0;
2008}
2009
2010int XML_From_PER(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
2011 E2AP_PDU_t *pdu = nullptr;
2012
2013 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2014 mdclog_write(MDCLOG_DEBUG, "got PER message of size %d is:%s",
2015 rmrMessageBuffer.sendMessage->len, rmrMessageBuffer.sendMessage->payload);
2016 }
2017 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, (void **) &pdu,
2018 rmrMessageBuffer.sendMessage->payload, rmrMessageBuffer.sendMessage->len);
2019 if (rval.code != RC_OK) {
2020 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) setup response from E2MGR : %s",
2021 rval.code,
2022 message.message.enodbName);
dhirajverma9f59b572021-07-23 02:36:13 -04002023 if (pdu != nullptr) {
2024 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
2025 pdu = nullptr;
2026 }
ss412g4aeeda22020-06-16 14:12:41 +03002027 return -1;
2028 }
2029
2030 int buff_size = RECEIVE_XAPP_BUFFER_SIZE;
2031 auto er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu,
2032 rmrMessageBuffer.sendMessage->payload, buff_size);
2033 if (er.encoded == -1) {
2034 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
dhirajverma9f59b572021-07-23 02:36:13 -04002035 if (pdu != nullptr) {
2036 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
2037 pdu = nullptr;
2038 }
ss412g4aeeda22020-06-16 14:12:41 +03002039 return -1;
2040 } else if (er.encoded > (ssize_t)buff_size) {
2041 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
2042 (int)rmrMessageBuffer.sendMessage->len,
2043 asn_DEF_E2AP_PDU.name,
2044 __func__,
2045 __LINE__);
dhirajverma9f59b572021-07-23 02:36:13 -04002046 if (pdu != nullptr) {
2047 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
2048 pdu = nullptr;
2049 }
ss412g4aeeda22020-06-16 14:12:41 +03002050 return -1;
2051 }
2052 rmrMessageBuffer.sendMessage->len = er.encoded;
dhirajverma9f59b572021-07-23 02:36:13 -04002053 if (pdu != nullptr) {
2054 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
2055 pdu = nullptr;
2056 }
ss412g4aeeda22020-06-16 14:12:41 +03002057 return 0;
2058
2059}
2060
ss412g1a79bdf2019-10-24 12:03:05 +03002061/**
2062 *
2063 * @param pdu
2064 * @param message
2065 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03002066 */
2067void asnInitiatingRequest(E2AP_PDU_t *pdu,
aa7133@att.com7b437f72020-03-30 13:25:54 +03002068 Sctp_Map_t *sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +03002069 ReportingMessages_t &message,
czichy2175dd32022-05-17 15:24:51 +03002070 RmrMessagesBuffer_t &rmrMessageBuffer, int streamId) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02002071 auto logLevel = mdclog_level_get();
ss412g1a79bdf2019-10-24 12:03:05 +03002072 auto procedureCode = ((InitiatingMessage_t *) pdu->choice.initiatingMessage)->procedureCode;
aa7133@att.com84bd3342020-03-16 18:04:57 +02002073 if (logLevel >= MDCLOG_DEBUG) {
2074 mdclog_write(MDCLOG_DEBUG, "Initiating message %ld\n", procedureCode);
ss412g1a79bdf2019-10-24 12:03:05 +03002075 }
2076 switch (procedureCode) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02002077 case ProcedureCode_id_E2setup: {
2078 if (logLevel >= MDCLOG_DEBUG) {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002079 mdclog_write(MDCLOG_DEBUG, "Got E2setup");
aa7133@att.com84bd3342020-03-16 18:04:57 +02002080 }
2081
aa7133@att.comdf4f1822020-06-16 16:23:53 +03002082// vector <string> RANfunctionsAdded_v;
2083// vector <string> RANfunctionsModified_v;
2084// RANfunctionsAdded_v.clear();
2085// RANfunctionsModified_v.clear();
2086 if (collectSetupRequestData(pdu, sctpMap, message) != 0) {
aa7133@att.com7b437f72020-03-30 13:25:54 +03002087 break;
2088 }
czichy2175dd32022-05-17 15:24:51 +03002089 struct sctp_status status;
2090 int stat_size = sizeof(status);
2091 getsockopt( message.peerInfo->fileDescriptor, SOL_SCTP, SCTP_STATUS,(void *)&status, (socklen_t *)&stat_size );
2092 if (logLevel >= MDCLOG_DEBUG) {
2093 mdclog_write(MDCLOG_DEBUG, "Start from SCTP %d fd", message.peerInfo->fileDescriptor);
2094 mdclog_write(MDCLOG_DEBUG, "SCTP status assoc id %d instrms %d outstrms %d", status.sstat_assoc_id,
2095 status.sstat_instrms, status.sstat_outstrms);
2096 }
2097 if(status.sstat_outstrms == 1 || status.sstat_instrms == 1)
2098 {
2099 message.peerInfo->isSingleStream = true;
2100 message.peerInfo->singleStreamId = streamId;
2101 if (status.sstat_outstrms == 1 && status.sstat_instrms == 1){
2102 if (logLevel >= MDCLOG_DEBUG) {
2103 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__);
2104 }
2105 }
2106 else {
2107 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,
2108 status.sstat_instrms, status.sstat_outstrms);
2109 }
2110 }
aa7133@att.com7b437f72020-03-30 13:25:54 +03002111
aa7133@att.com409f2232020-10-29 13:06:45 +03002112 buildPrometheusList(message.peerInfo, message.peerInfo->sctpParams->prometheusFamily);
ss412g4aeeda22020-06-16 14:12:41 +03002113
2114 string messageName("E2setupRequest");
2115 string ieName("E2setupRequestIEs");
2116 message.message.messageType = RIC_E2_SETUP_REQ;
aa7133@att.coma4c80012020-06-24 16:36:47 +03002117 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2118 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment((double)message.message.asnLength);
Mohamed Abukar64f37242021-08-15 12:39:56 +00002119
2120 // Update E2T instance level metrics
2121 message.peerInfo->sctpParams->e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2122 message.peerInfo->sctpParams->e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment((double)message.message.asnLength);
2123
aa7133@att.com409f2232020-10-29 13:06:45 +03002124 buildAndSendSetupRequest(message, rmrMessageBuffer, pdu);
ss412g1a79bdf2019-10-24 12:03:05 +03002125 break;
2126 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002127 case ProcedureCode_id_RICserviceUpdate: {
2128 if (logLevel >= MDCLOG_DEBUG) {
2129 mdclog_write(MDCLOG_DEBUG, "Got RICserviceUpdate %s", message.message.enodbName);
2130 }
aa7133@att.comdf4f1822020-06-16 16:23:53 +03002131// vector <string> RANfunctionsAdded_v;
2132// vector <string> RANfunctionsModified_v;
2133// RANfunctionsAdded_v.clear();
2134// RANfunctionsModified_v.clear();
2135// if (collectServiceUpdate_RequestData(pdu, sctpMap, message,
2136// RANfunctionsAdded_v, RANfunctionsModified_v) != 0) {
2137// break;
2138// }
ss412g4aeeda22020-06-16 14:12:41 +03002139
2140 string messageName("RICserviceUpdate");
2141 string ieName("RICserviceUpdateIEs");
2142 message.message.messageType = RIC_SERVICE_UPDATE;
dhirajverma93b6d9c2021-07-10 00:49:13 -04002143#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.coma4c80012020-06-24 16:36:47 +03002144 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
2145 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment((double)message.message.asnLength);
Mohamed Abukar64f37242021-08-15 12:39:56 +00002146
2147 // Update E2T instance level metrics
2148 message.peerInfo->sctpParams->e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
2149 message.peerInfo->sctpParams->e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04002150#endif
aa7133@att.com409f2232020-10-29 13:06:45 +03002151 buildAndSendSetupRequest(message, rmrMessageBuffer, pdu);
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002152 break;
2153 }
czichy2175dd32022-05-17 15:24:51 +03002154
2155case ProcedureCode_id_E2nodeConfigurationUpdate: {
2156 if (logLevel >= MDCLOG_DEBUG) {
2157 mdclog_write(MDCLOG_DEBUG, "Got E2nodeConfigurationUpdate %s", message.message.enodbName);
2158 }
2159
2160 string messageName("RICE2nodeConfigurationUpdate");
2161 string ieName("RICE2nodeConfigurationUpdateIEs");
2162 message.message.messageType = RIC_E2NODE_CONFIG_UPDATE;
2163#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2164 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment();
2165 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment((double)message.message.asnLength);
2166
2167 // Update E2T instance level metrics
2168 message.peerInfo->sctpParams->e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment();
2169 message.peerInfo->sctpParams->e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment((double)message.message.asnLength);
2170#endif
2171 buildAndSendSetupRequest(message, rmrMessageBuffer, pdu);
2172 break;
2173 }
2174
aa7133@att.com84bd3342020-03-16 18:04:57 +02002175 case ProcedureCode_id_ErrorIndication: {
2176 if (logLevel >= MDCLOG_DEBUG) {
2177 mdclog_write(MDCLOG_DEBUG, "Got ErrorIndication %s", message.message.enodbName);
2178 }
dhirajverma93b6d9c2021-07-10 00:49:13 -04002179#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.coma4c80012020-06-24 16:36:47 +03002180 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_ErrorIndication]->Increment();
2181 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_ErrorIndication]->Increment((double)message.message.asnLength);
Mohamed Abukar64f37242021-08-15 12:39:56 +00002182
2183 // Update E2T instance level metrics
2184 message.peerInfo->sctpParams->e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_ErrorIndication]->Increment();
2185 message.peerInfo->sctpParams->e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_ErrorIndication]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04002186#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002187 if (sendRequestToXapp(message, RIC_ERROR_INDICATION, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02002188 mdclog_write(MDCLOG_ERR, "RIC_ERROR_INDICATION failed to send to xAPP");
ss412g1a79bdf2019-10-24 12:03:05 +03002189 }
2190 break;
2191 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02002192 case ProcedureCode_id_Reset: {
2193 if (logLevel >= MDCLOG_DEBUG) {
2194 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03002195 }
dhirajverma93b6d9c2021-07-10 00:49:13 -04002196#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.coma4c80012020-06-24 16:36:47 +03002197 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2198 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment((double)message.message.asnLength);
Mohamed Abukar64f37242021-08-15 12:39:56 +00002199
2200 // Update E2T instance level metrics
2201 message.peerInfo->sctpParams->e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2202 message.peerInfo->sctpParams->e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04002203#endif
ss412g4aeeda22020-06-16 14:12:41 +03002204 if (XML_From_PER(message, rmrMessageBuffer) < 0) {
2205 break;
ss412g1a79bdf2019-10-24 12:03:05 +03002206 }
ss412g4aeeda22020-06-16 14:12:41 +03002207
2208 if (sendRequestToXapp(message, RIC_E2_RESET_REQ, rmrMessageBuffer) != 0) {
2209 mdclog_write(MDCLOG_ERR, "RIC_E2_RESET_REQ message failed to send to xAPP");
aa7133@att.com84bd3342020-03-16 18:04:57 +02002210 }
2211 break;
2212 }
2213 case ProcedureCode_id_RICindication: {
2214 if (logLevel >= MDCLOG_DEBUG) {
2215 mdclog_write(MDCLOG_DEBUG, "Got RICindication %s", message.message.enodbName);
2216 }
2217 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.count; i++) {
ss412g1a79bdf2019-10-24 12:03:05 +03002218 auto messageSent = false;
2219 RICindication_IEs_t *ie = pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.array[i];
aa7133@att.com84bd3342020-03-16 18:04:57 +02002220 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03002221 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
2222 }
2223 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02002224 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03002225 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
2226 }
2227 if (ie->value.present == RICindication_IEs__value_PR_RICrequestID) {
2228 static unsigned char tx[32];
2229 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_INDICATION;
2230 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2231 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
ss412g3bac2da2020-01-05 11:52:19 +02002232 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
aa7133@att.com84bd3342020-03-16 18:04:57 +02002233 (unsigned char *)message.message.enodbName,
2234 strlen(message.message.enodbName));
ss412g1a79bdf2019-10-24 12:03:05 +03002235 rmrMessageBuffer.sendMessage->state = 0;
aa7133@att.comc5a15202020-04-05 19:57:01 +03002236 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
2237
2238 //ie->value.choice.RICrequestID.ricInstanceID;
ss412g3bac2da2020-01-05 11:52:19 +02002239 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.comc5a15202020-04-05 19:57:01 +03002240 mdclog_write(MDCLOG_DEBUG, "sub id = %d, mtype = %d, ric instance id %ld, requestor id = %ld",
aa7133@att.com84bd3342020-03-16 18:04:57 +02002241 rmrMessageBuffer.sendMessage->sub_id,
aa7133@att.comc5a15202020-04-05 19:57:01 +03002242 rmrMessageBuffer.sendMessage->mtype,
2243 ie->value.choice.RICrequestID.ricInstanceID,
2244 ie->value.choice.RICrequestID.ricRequestorID);
ss412g3bac2da2020-01-05 11:52:19 +02002245 }
dhirajverma93b6d9c2021-07-10 00:49:13 -04002246#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.coma4c80012020-06-24 16:36:47 +03002247 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICindication]->Increment();
2248 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICindication]->Increment((double)message.message.asnLength);
Mohamed Abukar64f37242021-08-15 12:39:56 +00002249
2250 // Update E2T instance level metrics
2251 message.peerInfo->sctpParams->e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICindication]->Increment();
2252 message.peerInfo->sctpParams->e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICindication]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04002253#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002254 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03002255 messageSent = true;
2256 } else {
aa7133@att.com409f2232020-10-29 13:06:45 +03002257 mdclog_write(MDCLOG_ERR, "RIC request id missing illegal request");
ss412g1a79bdf2019-10-24 12:03:05 +03002258 }
2259 }
2260 if (messageSent) {
2261 break;
2262 }
2263 }
2264 break;
2265 }
ss412g1a79bdf2019-10-24 12:03:05 +03002266 default: {
2267 mdclog_write(MDCLOG_ERR, "Undefined or not supported message = %ld", procedureCode);
2268 message.message.messageType = 0; // no RMR message type yet
2269
2270 buildJsonMessage(message);
2271
2272 break;
2273 }
2274 }
ss412g1a79bdf2019-10-24 12:03:05 +03002275}
2276
2277/**
2278 *
2279 * @param pdu
2280 * @param message
ss412g1a79bdf2019-10-24 12:03:05 +03002281 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03002282 */
aa7133@att.com409f2232020-10-29 13:06:45 +03002283void asnSuccessfulMsg(E2AP_PDU_t *pdu,
aa7133@att.com7b437f72020-03-30 13:25:54 +03002284 Sctp_Map_t *sctpMap,
2285 ReportingMessages_t &message,
2286 RmrMessagesBuffer_t &rmrMessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03002287 auto procedureCode = pdu->choice.successfulOutcome->procedureCode;
aa7133@att.com84bd3342020-03-16 18:04:57 +02002288 auto logLevel = mdclog_level_get();
2289 if (logLevel >= MDCLOG_INFO) {
ss412g1a79bdf2019-10-24 12:03:05 +03002290 mdclog_write(MDCLOG_INFO, "Successful Outcome %ld", procedureCode);
2291 }
2292 switch (procedureCode) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02002293 case ProcedureCode_id_Reset: {
2294 if (logLevel >= MDCLOG_DEBUG) {
2295 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03002296 }
dhirajverma93b6d9c2021-07-10 00:49:13 -04002297#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.coma4c80012020-06-24 16:36:47 +03002298 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2299 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment((double)message.message.asnLength);
Mohamed Abukar64f37242021-08-15 12:39:56 +00002300
2301 // Update E2T instance level metrics
2302 message.peerInfo->sctpParams->e2tCounters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2303 message.peerInfo->sctpParams->e2tCounters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04002304#endif
ss412g4aeeda22020-06-16 14:12:41 +03002305 if (XML_From_PER(message, rmrMessageBuffer) < 0) {
2306 break;
2307 }
2308 if (sendRequestToXapp(message, RIC_E2_RESET_RESP, rmrMessageBuffer) != 0) {
2309 mdclog_write(MDCLOG_ERR, "RIC_E2_RESET_RESP message failed to send to xAPP");
ss412g1a79bdf2019-10-24 12:03:05 +03002310 }
2311 break;
2312 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02002313 case ProcedureCode_id_RICcontrol: {
2314 if (logLevel >= MDCLOG_DEBUG) {
2315 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03002316 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02002317 for (auto i = 0;
ss412g1a79bdf2019-10-24 12:03:05 +03002318 i < pdu->choice.successfulOutcome->value.choice.RICcontrolAcknowledge.protocolIEs.list.count; i++) {
2319 auto messageSent = false;
2320 RICcontrolAcknowledge_IEs_t *ie = pdu->choice.successfulOutcome->value.choice.RICcontrolAcknowledge.protocolIEs.list.array[i];
2321 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2322 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
2323 }
2324 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
2325 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2326 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
2327 }
2328 if (ie->value.present == RICcontrolAcknowledge_IEs__value_PR_RICrequestID) {
2329 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_CONTROL_ACK;
2330 rmrMessageBuffer.sendMessage->state = 0;
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002331// rmrMessageBuffer.sendMessage->sub_id = (int) ie->value.choice.RICrequestID.ricRequestorID;
2332 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
2333
ss412g1a79bdf2019-10-24 12:03:05 +03002334 static unsigned char tx[32];
2335 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2336 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
ss412g3bac2da2020-01-05 11:52:19 +02002337 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
aa7133@att.com84bd3342020-03-16 18:04:57 +02002338 (unsigned char *)message.message.enodbName,
2339 strlen(message.message.enodbName));
dhirajverma93b6d9c2021-07-10 00:49:13 -04002340#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.coma4c80012020-06-24 16:36:47 +03002341 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
2342 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment((double)message.message.asnLength);
Mohamed Abukar64f37242021-08-15 12:39:56 +00002343
2344 // Update E2T instance level metrics
2345 message.peerInfo->sctpParams->e2tCounters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
2346 message.peerInfo->sctpParams->e2tCounters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04002347#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002348 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03002349 messageSent = true;
2350 } else {
aa7133@att.com409f2232020-10-29 13:06:45 +03002351 mdclog_write(MDCLOG_ERR, "RIC request id missing illegal request");
ss412g1a79bdf2019-10-24 12:03:05 +03002352 }
2353 }
2354 if (messageSent) {
2355 break;
2356 }
2357 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02002358
2359 break;
2360 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02002361 case ProcedureCode_id_RICsubscription: {
2362 if (logLevel >= MDCLOG_DEBUG) {
2363 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
2364 }
dhirajverma93b6d9c2021-07-10 00:49:13 -04002365#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.coma4c80012020-06-24 16:36:47 +03002366 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
2367 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment((double)message.message.asnLength);
Mohamed Abukar64f37242021-08-15 12:39:56 +00002368
2369 // Update E2T instance level metrics
2370 message.peerInfo->sctpParams->e2tCounters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
2371 message.peerInfo->sctpParams->e2tCounters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04002372#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002373 if (sendRequestToXapp(message, RIC_SUB_RESP, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02002374 mdclog_write(MDCLOG_ERR, "Subscription successful message failed to send to xAPP");
2375 }
2376 break;
2377 }
2378 case ProcedureCode_id_RICsubscriptionDelete: {
2379 if (logLevel >= MDCLOG_DEBUG) {
2380 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
2381 }
dhirajverma93b6d9c2021-07-10 00:49:13 -04002382#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.coma4c80012020-06-24 16:36:47 +03002383 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
2384 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment((double)message.message.asnLength);
Mohamed Abukar64f37242021-08-15 12:39:56 +00002385
2386 // Update E2T instance level metrics
2387 message.peerInfo->sctpParams->e2tCounters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
2388 message.peerInfo->sctpParams->e2tCounters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04002389#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002390 if (sendRequestToXapp(message, RIC_SUB_DEL_RESP, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02002391 mdclog_write(MDCLOG_ERR, "Subscription delete successful message failed to send to xAPP");
2392 }
ss412g1a79bdf2019-10-24 12:03:05 +03002393 break;
2394 }
2395 default: {
2396 mdclog_write(MDCLOG_WARN, "Undefined or not supported message = %ld", procedureCode);
2397 message.message.messageType = 0; // no RMR message type yet
2398 buildJsonMessage(message);
2399
2400 break;
2401 }
2402 }
ss412g1a79bdf2019-10-24 12:03:05 +03002403}
2404
2405/**
2406 *
2407 * @param pdu
2408 * @param message
ss412g1a79bdf2019-10-24 12:03:05 +03002409 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03002410 */
2411void asnUnSuccsesfulMsg(E2AP_PDU_t *pdu,
aa7133@att.com7b437f72020-03-30 13:25:54 +03002412 Sctp_Map_t *sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +03002413 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +02002414 RmrMessagesBuffer_t &rmrMessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03002415 auto procedureCode = pdu->choice.unsuccessfulOutcome->procedureCode;
aa7133@att.com84bd3342020-03-16 18:04:57 +02002416 auto logLevel = mdclog_level_get();
2417 if (logLevel >= MDCLOG_INFO) {
ss412g1a79bdf2019-10-24 12:03:05 +03002418 mdclog_write(MDCLOG_INFO, "Unsuccessful Outcome %ld", procedureCode);
2419 }
2420 switch (procedureCode) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02002421 case ProcedureCode_id_RICcontrol: {
2422 if (logLevel >= MDCLOG_DEBUG) {
2423 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03002424 }
2425 for (int i = 0;
2426 i < pdu->choice.unsuccessfulOutcome->value.choice.RICcontrolFailure.protocolIEs.list.count; i++) {
2427 auto messageSent = false;
2428 RICcontrolFailure_IEs_t *ie = pdu->choice.unsuccessfulOutcome->value.choice.RICcontrolFailure.protocolIEs.list.array[i];
aa7133@att.com84bd3342020-03-16 18:04:57 +02002429 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03002430 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
2431 }
2432 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02002433 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03002434 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
2435 }
2436 if (ie->value.present == RICcontrolFailure_IEs__value_PR_RICrequestID) {
2437 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_CONTROL_FAILURE;
2438 rmrMessageBuffer.sendMessage->state = 0;
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002439// rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricRequestorID;
2440 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
ss412g1a79bdf2019-10-24 12:03:05 +03002441 static unsigned char tx[32];
2442 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2443 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
aa7133@att.com84bd3342020-03-16 18:04:57 +02002444 rmr_bytes2meid(rmrMessageBuffer.sendMessage, (unsigned char *) message.message.enodbName,
2445 strlen(message.message.enodbName));
dhirajverma93b6d9c2021-07-10 00:49:13 -04002446#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.coma4c80012020-06-24 16:36:47 +03002447 message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
2448 message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment((double)message.message.asnLength);
Mohamed Abukar64f37242021-08-15 12:39:56 +00002449
2450 // Update E2T instance level metrics
2451 message.peerInfo->sctpParams->e2tCounters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
2452 message.peerInfo->sctpParams->e2tCounters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment((double)message.message.asnLength);
dhirajverma99990292021-07-05 10:03:59 -04002453#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002454 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03002455 messageSent = true;
2456 } else {
aa7133@att.com409f2232020-10-29 13:06:45 +03002457 mdclog_write(MDCLOG_ERR, "RIC request id missing illegal request");
ss412g1a79bdf2019-10-24 12:03:05 +03002458 }
2459 }
2460 if (messageSent) {
2461 break;
2462 }
2463 }
2464 break;
2465 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02002466 case ProcedureCode_id_RICsubscription: {
2467 if (logLevel >= MDCLOG_DEBUG) {
2468 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
2469 }
dhirajverma93b6d9c2021-07-10 00:49:13 -04002470#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.coma4c80012020-06-24 16:36:47 +03002471 message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
2472 message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment((double)message.message.asnLength);
Mohamed Abukar64f37242021-08-15 12:39:56 +00002473
2474 // Update E2T instance level metrics
2475 message.peerInfo->sctpParams->e2tCounters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
2476 message.peerInfo->sctpParams->e2tCounters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04002477#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002478 if (sendRequestToXapp(message, RIC_SUB_FAILURE, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02002479 mdclog_write(MDCLOG_ERR, "Subscription unsuccessful message failed to send to xAPP");
2480 }
2481 break;
2482 }
2483 case ProcedureCode_id_RICsubscriptionDelete: {
2484 if (logLevel >= MDCLOG_DEBUG) {
2485 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
2486 }
dhirajverma93b6d9c2021-07-10 00:49:13 -04002487#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.coma4c80012020-06-24 16:36:47 +03002488 message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
2489 message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment((double)message.message.asnLength);
Mohamed Abukar64f37242021-08-15 12:39:56 +00002490
2491 // Update E2T instance level metrics
2492 message.peerInfo->sctpParams->e2tCounters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
2493 message.peerInfo->sctpParams->e2tCounters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04002494#endif
ss412g4aeeda22020-06-16 14:12:41 +03002495 if (sendRequestToXapp(message, RIC_SUB_FAILURE, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02002496 mdclog_write(MDCLOG_ERR, "Subscription Delete unsuccessful message failed to send to xAPP");
2497 }
2498 break;
2499 }
ss412g1a79bdf2019-10-24 12:03:05 +03002500 default: {
2501 mdclog_write(MDCLOG_WARN, "Undefined or not supported message = %ld", procedureCode);
2502 message.message.messageType = 0; // no RMR message type yet
dhirajverma93b6d9c2021-07-10 00:49:13 -04002503#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
ss412g1a79bdf2019-10-24 12:03:05 +03002504 buildJsonMessage(message);
dhirajverma99990292021-07-05 10:03:59 -04002505#endif
ss412g1a79bdf2019-10-24 12:03:05 +03002506 break;
2507 }
2508 }
ss412g1a79bdf2019-10-24 12:03:05 +03002509}
2510
2511/**
2512 *
2513 * @param message
2514 * @param requestId
2515 * @param rmrMmessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03002516 * @return
2517 */
2518int sendRequestToXapp(ReportingMessages_t &message,
2519 int requestId,
aa7133@att.combe92d712020-03-18 17:30:39 +02002520 RmrMessagesBuffer_t &rmrMmessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03002521 rmr_bytes2meid(rmrMmessageBuffer.sendMessage,
2522 (unsigned char *)message.message.enodbName,
2523 strlen(message.message.enodbName));
2524 message.message.messageType = rmrMmessageBuffer.sendMessage->mtype = requestId;
2525 rmrMmessageBuffer.sendMessage->state = 0;
2526 static unsigned char tx[32];
2527 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2528 rmr_bytes2xact(rmrMmessageBuffer.sendMessage, tx, strlen((const char *) tx));
2529
aa7133@att.combe92d712020-03-18 17:30:39 +02002530 auto rc = sendRmrMessage(rmrMmessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03002531 return rc;
2532}
2533
ss412g4aeeda22020-06-16 14:12:41 +03002534/**
2535 *
2536 * @param pSctpParams
2537 */
aa7133@att.combe92d712020-03-18 17:30:39 +02002538void getRmrContext(sctp_params_t &pSctpParams) {
ss412g3bac2da2020-01-05 11:52:19 +02002539 pSctpParams.rmrCtx = nullptr;
aa7133@att.comab0e0f42020-04-06 17:13:58 +03002540 pSctpParams.rmrCtx = rmr_init(pSctpParams.rmrAddress, RECEIVE_XAPP_BUFFER_SIZE, RMRFL_NONE);
ss412g3bac2da2020-01-05 11:52:19 +02002541 if (pSctpParams.rmrCtx == nullptr) {
2542 mdclog_write(MDCLOG_ERR, "Failed to initialize RMR");
ss412g3bac2da2020-01-05 11:52:19 +02002543 return;
ss412g1a79bdf2019-10-24 12:03:05 +03002544 }
2545
ss412g3bac2da2020-01-05 11:52:19 +02002546 rmr_set_stimeout(pSctpParams.rmrCtx, 0); // disable retries for any send operation
ss412g1a79bdf2019-10-24 12:03:05 +03002547 // we need to find that routing table exist and we can run
2548 if (mdclog_level_get() >= MDCLOG_INFO) {
2549 mdclog_write(MDCLOG_INFO, "We are after RMR INIT wait for RMR_Ready");
2550 }
2551 int rmrReady = 0;
2552 int count = 0;
2553 while (!rmrReady) {
ss412g3bac2da2020-01-05 11:52:19 +02002554 if ((rmrReady = rmr_ready(pSctpParams.rmrCtx)) == 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002555 sleep(1);
2556 }
2557 count++;
2558 if (count % 60 == 0) {
2559 mdclog_write(MDCLOG_INFO, "waiting to RMR ready state for %d seconds", count);
2560 }
2561 }
2562 if (mdclog_level_get() >= MDCLOG_INFO) {
2563 mdclog_write(MDCLOG_INFO, "RMR running");
2564 }
ss412g3bac2da2020-01-05 11:52:19 +02002565 rmr_init_trace(pSctpParams.rmrCtx, 200);
2566 // get the RMR fd for the epoll
2567 pSctpParams.rmrListenFd = rmr_get_rcvfd(pSctpParams.rmrCtx);
2568 struct epoll_event event{};
2569 // add RMR fd to epoll
2570 event.events = (EPOLLIN);
2571 event.data.fd = pSctpParams.rmrListenFd;
2572 // add listening RMR FD to epoll
2573 if (epoll_ctl(pSctpParams.epoll_fd, EPOLL_CTL_ADD, pSctpParams.rmrListenFd, &event)) {
2574 mdclog_write(MDCLOG_ERR, "Failed to add RMR descriptor to epoll");
2575 close(pSctpParams.rmrListenFd);
2576 rmr_close(pSctpParams.rmrCtx);
2577 pSctpParams.rmrCtx = nullptr;
2578 }
ss412g1a79bdf2019-10-24 12:03:05 +03002579}
2580
ss412g4aeeda22020-06-16 14:12:41 +03002581/**
2582 *
2583 * @param message
2584 * @param rmrMessageBuffer
2585 * @return
2586 */
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002587int PER_FromXML(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
aa7133@att.com07e256f2020-04-20 15:59:51 +03002588 E2AP_PDU_t *pdu = nullptr;
aa7133@att.com1c4f50f2020-03-30 14:30:52 +03002589
2590 if (mdclog_level_get() >= MDCLOG_DEBUG) {
ss412g4aeeda22020-06-16 14:12:41 +03002591 mdclog_write(MDCLOG_DEBUG, "got xml Format data from xApp of size %d is:%s",
aa7133@att.com07e256f2020-04-20 15:59:51 +03002592 rmrMessageBuffer.rcvMessage->len, rmrMessageBuffer.rcvMessage->payload);
aa7133@att.com1c4f50f2020-03-30 14:30:52 +03002593 }
aa7133@att.com1fecb052020-03-29 09:44:09 +03002594 auto rval = asn_decode(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, (void **) &pdu,
2595 rmrMessageBuffer.rcvMessage->payload, rmrMessageBuffer.rcvMessage->len);
aa7133@att.com409f2232020-10-29 13:06:45 +03002596 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2597 mdclog_write(MDCLOG_DEBUG, "%s After decoding the XML to PDU", __func__ );
2598 }
aa7133@att.com1fecb052020-03-29 09:44:09 +03002599 if (rval.code != RC_OK) {
dhirajvermac40ea1f2021-07-08 12:52:30 -04002600#ifdef UNIT_TEST
2601 return 0;
2602#endif
aa7133@att.com1c4f50f2020-03-30 14:30:52 +03002603 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) setup response from E2MGR : %s",
aa7133@att.com1fecb052020-03-29 09:44:09 +03002604 rval.code,
2605 message.message.enodbName);
dhirajverma9f59b572021-07-23 02:36:13 -04002606 if (pdu != nullptr) {
2607 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
2608 pdu = nullptr;
2609 }
aa7133@att.com1fecb052020-03-29 09:44:09 +03002610 return -1;
2611 }
2612
aa7133@att.coma0f0db62020-04-01 16:52:50 +03002613 int buff_size = RECEIVE_XAPP_BUFFER_SIZE;
2614 auto er = asn_encode_to_buffer(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, pdu,
2615 rmrMessageBuffer.rcvMessage->payload, buff_size);
aa7133@att.com409f2232020-10-29 13:06:45 +03002616 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2617 mdclog_write(MDCLOG_DEBUG, "%s After encoding PDU to PER", __func__ );
2618 }
aa7133@att.com1fecb052020-03-29 09:44:09 +03002619 if (er.encoded == -1) {
2620 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
dhirajverma9f59b572021-07-23 02:36:13 -04002621 if (pdu != nullptr) {
2622 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
2623 pdu = nullptr;
2624 }
aa7133@att.com1fecb052020-03-29 09:44:09 +03002625 return -1;
aa7133@att.coma0f0db62020-04-01 16:52:50 +03002626 } else if (er.encoded > (ssize_t)buff_size) {
2627 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
aa7133@att.com1fecb052020-03-29 09:44:09 +03002628 (int)rmrMessageBuffer.rcvMessage->len,
aa7133@att.coma0f0db62020-04-01 16:52:50 +03002629 asn_DEF_E2AP_PDU.name,
2630 __func__,
2631 __LINE__);
dhirajverma9f59b572021-07-23 02:36:13 -04002632 if (pdu != nullptr) {
2633 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
2634 pdu = nullptr;
2635 }
aa7133@att.com1fecb052020-03-29 09:44:09 +03002636 return -1;
2637 }
aa7133@att.coma0f0db62020-04-01 16:52:50 +03002638 rmrMessageBuffer.rcvMessage->len = er.encoded;
dhirajverma9f59b572021-07-23 02:36:13 -04002639 if (pdu != nullptr) {
2640 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
2641 pdu = nullptr;
2642 }
aa7133@att.com1fecb052020-03-29 09:44:09 +03002643 return 0;
2644}
2645
ss412g1a79bdf2019-10-24 12:03:05 +03002646/**
2647 *
ss412g1a79bdf2019-10-24 12:03:05 +03002648 * @param sctpMap
2649 * @param rmrMessageBuffer
2650 * @param ts
ss412g1a79bdf2019-10-24 12:03:05 +03002651 * @return
2652 */
aa7133@att.com6680c3b2020-03-22 12:06:58 +02002653int receiveXappMessages(Sctp_Map_t *sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +03002654 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +02002655 struct timespec &ts) {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002656 int loglevel = mdclog_level_get();
ss412g1a79bdf2019-10-24 12:03:05 +03002657 if (rmrMessageBuffer.rcvMessage == nullptr) {
2658 //we have error
2659 mdclog_write(MDCLOG_ERR, "RMR Allocation message, %s", strerror(errno));
ss412g1a79bdf2019-10-24 12:03:05 +03002660 return -1;
2661 }
2662
aa7133@att.comf13529a2020-08-27 11:21:16 +03002663// if (loglevel >= MDCLOG_DEBUG) {
2664// mdclog_write(MDCLOG_DEBUG, "Call to rmr_rcv_msg");
2665// }
ss412g1a79bdf2019-10-24 12:03:05 +03002666 rmrMessageBuffer.rcvMessage = rmr_rcv_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.rcvMessage);
2667 if (rmrMessageBuffer.rcvMessage == nullptr) {
aa7133@att.com409f2232020-10-29 13:06:45 +03002668 mdclog_write(MDCLOG_ERR, "RMR Receiving message with null pointer, Reallocated rmr message buffer");
ss412g1a79bdf2019-10-24 12:03:05 +03002669 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
ss412g1a79bdf2019-10-24 12:03:05 +03002670 return -2;
2671 }
2672 ReportingMessages_t message;
2673 message.message.direction = 'D';
2674 message.message.time.tv_nsec = ts.tv_nsec;
2675 message.message.time.tv_sec = ts.tv_sec;
2676
2677 // get message payload
2678 //auto msgData = msg->payload;
dhirajverma60f8bef2021-06-29 07:22:58 -04002679#ifdef UNIT_TEST
dhirajverma99990292021-07-05 10:03:59 -04002680 rmrMessageBuffer.rcvMessage->state = 0;
dhirajverma60f8bef2021-06-29 07:22:58 -04002681#endif
ss412g1a79bdf2019-10-24 12:03:05 +03002682 if (rmrMessageBuffer.rcvMessage->state != 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +03002683 mdclog_write(MDCLOG_ERR, "RMR Receiving message with stat = %d", rmrMessageBuffer.rcvMessage->state);
ss412g1a79bdf2019-10-24 12:03:05 +03002684 return -1;
2685 }
aa7133@att.com1fecb052020-03-29 09:44:09 +03002686 rmr_get_meid(rmrMessageBuffer.rcvMessage, (unsigned char *)message.message.enodbName);
ss412g4aeeda22020-06-16 14:12:41 +03002687 message.peerInfo = (ConnectedCU_t *) sctpMap->find(message.message.enodbName);
2688 if (message.peerInfo == nullptr) {
2689 auto type = rmrMessageBuffer.rcvMessage->mtype;
2690 switch (type) {
2691 case RIC_SCTP_CLEAR_ALL:
2692 case E2_TERM_KEEP_ALIVE_REQ:
2693 case RIC_HEALTH_CHECK_REQ:
2694 break;
2695 default:
dhirajverma99990292021-07-05 10:03:59 -04002696#ifdef UNIT_TEST
2697 break;
2698#endif
ss412g4aeeda22020-06-16 14:12:41 +03002699 mdclog_write(MDCLOG_ERR, "Failed to send message no CU entry %s", message.message.enodbName);
2700 return -1;
2701 }
2702 }
2703
aa7133@att.comde40d1c2020-08-26 16:19:20 +03002704 if (rmrMessageBuffer.rcvMessage->mtype != RIC_HEALTH_CHECK_REQ) {
2705 num_of_XAPP_messages.fetch_add(1, std::memory_order_release);
2706
2707 }
ss412g1a79bdf2019-10-24 12:03:05 +03002708 switch (rmrMessageBuffer.rcvMessage->mtype) {
aa7133@att.combe92d712020-03-18 17:30:39 +02002709 case RIC_E2_SETUP_RESP : {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002710 if (loglevel >= MDCLOG_DEBUG) {
2711 mdclog_write(MDCLOG_DEBUG, "RIC_E2_SETUP_RESP");
2712 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002713 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
aa7133@att.com1fecb052020-03-29 09:44:09 +03002714 break;
2715 }
dhirajverma93b6d9c2021-07-10 00:49:13 -04002716#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.coma4c80012020-06-24 16:36:47 +03002717 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2718 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment(rmrMessageBuffer.rcvMessage->len);
Mohamed Abukar64f37242021-08-15 12:39:56 +00002719
2720 // Update E2T instance level metrics
2721 message.peerInfo->sctpParams->e2tCounters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2722 message.peerInfo->sctpParams->e2tCounters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment(rmrMessageBuffer.rcvMessage->len);
dhirajverma99990292021-07-05 10:03:59 -04002723#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002724 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2725 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_SETUP_RESP");
2726 return -6;
ss412g1a79bdf2019-10-24 12:03:05 +03002727 }
2728 break;
2729 }
aa7133@att.combe92d712020-03-18 17:30:39 +02002730 case RIC_E2_SETUP_FAILURE : {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002731 if (loglevel >= MDCLOG_DEBUG) {
2732 mdclog_write(MDCLOG_DEBUG, "RIC_E2_SETUP_FAILURE");
2733 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002734 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
aa7133@att.com1fecb052020-03-29 09:44:09 +03002735 break;
2736 }
dhirajverma93b6d9c2021-07-10 00:49:13 -04002737#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.coma4c80012020-06-24 16:36:47 +03002738 message.peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2739 message.peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment(rmrMessageBuffer.rcvMessage->len);
Mohamed Abukar64f37242021-08-15 12:39:56 +00002740
2741 // Update E2T instance level metrics
2742 message.peerInfo->sctpParams->e2tCounters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2743 message.peerInfo->sctpParams->e2tCounters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment(rmrMessageBuffer.rcvMessage->len);
dhirajverma99990292021-07-05 10:03:59 -04002744#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002745 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2746 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_SETUP_FAILURE");
2747 return -6;
ss412g1a79bdf2019-10-24 12:03:05 +03002748 }
2749 break;
2750 }
czichy2175dd32022-05-17 15:24:51 +03002751
2752 case RIC_E2NODE_CONFIG_UPDATE_ACK: {
2753 if (loglevel >= MDCLOG_DEBUG) {
2754 mdclog_write(MDCLOG_DEBUG, "RIC_E2NODE_CONFIG_UPDATE_ACK");
2755 }
2756 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2757 break;
2758 }
2759#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2760 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment();
2761 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
2762
2763 // Update E2T instance level metrics
2764 message.peerInfo->sctpParams->e2tCounters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment();
2765 message.peerInfo->sctpParams->e2tCounters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
2766#endif
2767 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2768 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2NODE_CONFIG_UPDATE_ACK");
2769 return -6;
2770 }
2771 break;
2772 }
2773
2774 case RIC_E2NODE_CONFIG_UPDATE_FAILURE: {
2775 if (loglevel >= MDCLOG_DEBUG) {
2776 mdclog_write(MDCLOG_DEBUG, "RIC_E2NODE_CONFIG_UPDATE_FAILURE");
2777 }
2778 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2779 break;
2780 }
2781#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
2782 message.peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment();
2783 message.peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
2784
2785 // Update E2T instance level metrics
2786 message.peerInfo->sctpParams->e2tCounters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment();
2787 message.peerInfo->sctpParams->e2tCounters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_E2nodeConfigurationUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
2788#endif
2789 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2790 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2NODE_CONFIG_UPDATE_FAILURE");
2791 return -6;
2792 }
2793 break;
2794 }
2795
aa7133@att.combe92d712020-03-18 17:30:39 +02002796 case RIC_ERROR_INDICATION: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002797 if (loglevel >= MDCLOG_DEBUG) {
2798 mdclog_write(MDCLOG_DEBUG, "RIC_ERROR_INDICATION");
2799 }
dhirajverma93b6d9c2021-07-10 00:49:13 -04002800#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.coma4c80012020-06-24 16:36:47 +03002801 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_ErrorIndication]->Increment();
2802 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_ErrorIndication]->Increment(rmrMessageBuffer.rcvMessage->len);
Mohamed Abukar64f37242021-08-15 12:39:56 +00002803
2804 // Update E2T instance level metrics
2805 message.peerInfo->sctpParams->e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_ErrorIndication]->Increment();
2806 message.peerInfo->sctpParams->e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_ErrorIndication]->Increment(rmrMessageBuffer.rcvMessage->len);
dhirajverma99990292021-07-05 10:03:59 -04002807#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002808 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2809 mdclog_write(MDCLOG_ERR, "Failed to send RIC_ERROR_INDICATION");
ss412g1a79bdf2019-10-24 12:03:05 +03002810 return -6;
2811 }
2812 break;
2813 }
2814 case RIC_SUB_REQ: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002815 if (loglevel >= MDCLOG_DEBUG) {
2816 mdclog_write(MDCLOG_DEBUG, "RIC_SUB_REQ");
2817 }
dhirajverma93b6d9c2021-07-10 00:49:13 -04002818#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.coma4c80012020-06-24 16:36:47 +03002819 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
2820 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment(rmrMessageBuffer.rcvMessage->len);
Mohamed Abukar64f37242021-08-15 12:39:56 +00002821
2822 // Update E2T instance level metrics
2823 message.peerInfo->sctpParams->e2tCounters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
2824 message.peerInfo->sctpParams->e2tCounters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment(rmrMessageBuffer.rcvMessage->len);
dhirajverma99990292021-07-05 10:03:59 -04002825#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002826 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002827 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SUB_REQ");
ss412g1a79bdf2019-10-24 12:03:05 +03002828 return -6;
2829 }
2830 break;
2831 }
2832 case RIC_SUB_DEL_REQ: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002833 if (loglevel >= MDCLOG_DEBUG) {
2834 mdclog_write(MDCLOG_DEBUG, "RIC_SUB_DEL_REQ");
2835 }
dhirajverma93b6d9c2021-07-10 00:49:13 -04002836#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.coma4c80012020-06-24 16:36:47 +03002837 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
2838 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment(rmrMessageBuffer.rcvMessage->len);
Mohamed Abukar64f37242021-08-15 12:39:56 +00002839
2840 // Update E2T instance level metrics
2841 message.peerInfo->sctpParams->e2tCounters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
2842 message.peerInfo->sctpParams->e2tCounters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment(rmrMessageBuffer.rcvMessage->len);
dhirajverma99990292021-07-05 10:03:59 -04002843#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002844 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002845 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SUB_DEL_REQ");
ss412g1a79bdf2019-10-24 12:03:05 +03002846 return -6;
2847 }
2848 break;
2849 }
2850 case RIC_CONTROL_REQ: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002851 if (loglevel >= MDCLOG_DEBUG) {
2852 mdclog_write(MDCLOG_DEBUG, "RIC_CONTROL_REQ");
2853 }
dhirajverma93b6d9c2021-07-10 00:49:13 -04002854#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.coma4c80012020-06-24 16:36:47 +03002855 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
2856 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment(rmrMessageBuffer.rcvMessage->len);
Mohamed Abukar64f37242021-08-15 12:39:56 +00002857
2858 // Update E2T instance level metrics
2859 message.peerInfo->sctpParams->e2tCounters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
2860 message.peerInfo->sctpParams->e2tCounters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment(rmrMessageBuffer.rcvMessage->len);
dhirajverma99990292021-07-05 10:03:59 -04002861#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002862 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002863 mdclog_write(MDCLOG_ERR, "Failed to send RIC_CONTROL_REQ");
ss412g1a79bdf2019-10-24 12:03:05 +03002864 return -6;
2865 }
2866 break;
2867 }
2868 case RIC_SERVICE_QUERY: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002869 if (loglevel >= MDCLOG_DEBUG) {
2870 mdclog_write(MDCLOG_DEBUG, "RIC_SERVICE_QUERY");
2871 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002872 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2873 break;
2874 }
dhirajverma93b6d9c2021-07-10 00:49:13 -04002875#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.coma4c80012020-06-24 16:36:47 +03002876 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceQuery]->Increment();
2877 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceQuery]->Increment(rmrMessageBuffer.rcvMessage->len);
Mohamed Abukar64f37242021-08-15 12:39:56 +00002878
2879 // Update E2T instance level metrics
2880 message.peerInfo->sctpParams->e2tCounters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceQuery]->Increment();
2881 message.peerInfo->sctpParams->e2tCounters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceQuery]->Increment(rmrMessageBuffer.rcvMessage->len);
dhirajverma99990292021-07-05 10:03:59 -04002882#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002883 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002884 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_QUERY");
ss412g1a79bdf2019-10-24 12:03:05 +03002885 return -6;
2886 }
2887 break;
2888 }
2889 case RIC_SERVICE_UPDATE_ACK: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002890 if (loglevel >= MDCLOG_DEBUG) {
2891 mdclog_write(MDCLOG_DEBUG, "RIC_SERVICE_UPDATE_ACK");
2892 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002893 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
aa7133@att.com54885fe2020-10-29 17:24:49 +03002894 mdclog_write(MDCLOG_ERR, "error in PER_FromXML");
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002895 break;
2896 }
dhirajverma93b6d9c2021-07-10 00:49:13 -04002897#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.coma4c80012020-06-24 16:36:47 +03002898 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
aa7133@att.com52771912020-10-29 17:45:01 +03002899 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
Mohamed Abukar64f37242021-08-15 12:39:56 +00002900
2901 // Update E2T instance level metrics
2902 message.peerInfo->sctpParams->e2tCounters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
2903 message.peerInfo->sctpParams->e2tCounters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
dhirajverma99990292021-07-05 10:03:59 -04002904#endif
aa7133@att.com54885fe2020-10-29 17:24:49 +03002905 if (loglevel >= MDCLOG_DEBUG) {
2906 mdclog_write(MDCLOG_DEBUG, "Before sending to CU");
2907 }
aa7133@att.combe92d712020-03-18 17:30:39 +02002908 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002909 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_UPDATE_ACK");
ss412g1a79bdf2019-10-24 12:03:05 +03002910 return -6;
2911 }
2912 break;
2913 }
2914 case RIC_SERVICE_UPDATE_FAILURE: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002915 if (loglevel >= MDCLOG_DEBUG) {
2916 mdclog_write(MDCLOG_DEBUG, "RIC_SERVICE_UPDATE_FAILURE");
2917 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002918 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2919 break;
2920 }
dhirajverma93b6d9c2021-07-10 00:49:13 -04002921#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.coma4c80012020-06-24 16:36:47 +03002922 message.peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
aa7133@att.com7c07fee2020-10-29 17:54:38 +03002923 message.peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
Mohamed Abukar64f37242021-08-15 12:39:56 +00002924
2925 // Update E2T instance level metrics
2926 message.peerInfo->sctpParams->e2tCounters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
2927 message.peerInfo->sctpParams->e2tCounters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
dhirajverma99990292021-07-05 10:03:59 -04002928#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002929 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002930 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_UPDATE_FAILURE");
ss412g1a79bdf2019-10-24 12:03:05 +03002931 return -6;
2932 }
2933 break;
2934 }
ss412g4aeeda22020-06-16 14:12:41 +03002935 case RIC_E2_RESET_REQ: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002936 if (loglevel >= MDCLOG_DEBUG) {
2937 mdclog_write(MDCLOG_DEBUG, "RIC_E2_RESET_REQ");
2938 }
ss412g4aeeda22020-06-16 14:12:41 +03002939 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2940 break;
2941 }
dhirajverma93b6d9c2021-07-10 00:49:13 -04002942#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.coma4c80012020-06-24 16:36:47 +03002943 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2944 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment(rmrMessageBuffer.rcvMessage->len);
Mohamed Abukar64f37242021-08-15 12:39:56 +00002945
2946 // Update E2T instance level metrics
2947 message.peerInfo->sctpParams->e2tCounters[IN_INITI][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2948 message.peerInfo->sctpParams->e2tCounters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment(rmrMessageBuffer.rcvMessage->len);
dhirajverma99990292021-07-05 10:03:59 -04002949#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002950 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g4aeeda22020-06-16 14:12:41 +03002951 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_RESET");
ss412g1a79bdf2019-10-24 12:03:05 +03002952 return -6;
2953 }
2954 break;
2955 }
ss412g4aeeda22020-06-16 14:12:41 +03002956 case RIC_E2_RESET_RESP: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002957 if (loglevel >= MDCLOG_DEBUG) {
2958 mdclog_write(MDCLOG_DEBUG, "RIC_E2_RESET_RESP");
2959 }
ss412g4aeeda22020-06-16 14:12:41 +03002960 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2961 break;
2962 }
dhirajverma93b6d9c2021-07-10 00:49:13 -04002963#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
aa7133@att.coma4c80012020-06-24 16:36:47 +03002964 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2965 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment(rmrMessageBuffer.rcvMessage->len);
Mohamed Abukar64f37242021-08-15 12:39:56 +00002966
2967 // Update E2T instance level metrics
2968 message.peerInfo->sctpParams->e2tCounters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2969 message.peerInfo->sctpParams->e2tCounters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment(rmrMessageBuffer.rcvMessage->len);
dhirajverma99990292021-07-05 10:03:59 -04002970#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002971 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g4aeeda22020-06-16 14:12:41 +03002972 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_RESET_RESP");
ss412g1a79bdf2019-10-24 12:03:05 +03002973 return -6;
2974 }
2975 break;
2976 }
2977 case RIC_SCTP_CLEAR_ALL: {
2978 mdclog_write(MDCLOG_INFO, "RIC_SCTP_CLEAR_ALL");
2979 // loop on all keys and close socket and then erase all map.
2980 vector<char *> v;
2981 sctpMap->getKeys(v);
2982 for (auto const &iter : v) { //}; iter != sctpMap.end(); iter++) {
2983 if (!boost::starts_with((string) (iter), "host:") && !boost::starts_with((string) (iter), "msg:")) {
2984 auto *peerInfo = (ConnectedCU_t *) sctpMap->find(iter);
2985 if (peerInfo == nullptr) {
2986 continue;
2987 }
2988 close(peerInfo->fileDescriptor);
2989 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
2990 message.message.direction = 'D';
2991 message.message.time.tv_nsec = ts.tv_nsec;
2992 message.message.time.tv_sec = ts.tv_sec;
2993
2994 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
2995 snprintf((char *)rmrMessageBuffer.sendMessage->payload,
aa7133@att.com96c9b882020-03-25 14:50:40 +02002996 256,
2997 "%s|RIC_SCTP_CLEAR_ALL",
2998 peerInfo->enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03002999 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
3000 mdclog_write(MDCLOG_INFO, "%s", message.message.asndata);
aa7133@att.combe92d712020-03-18 17:30:39 +02003001 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03003002 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
3003 }
3004 free(peerInfo);
sandeekuea662102022-11-08 18:51:20 +05303005 peerInfo = nullptr;
ss412g1a79bdf2019-10-24 12:03:05 +03003006 }
3007 }
3008
3009 sleep(1);
3010 sctpMap->clear();
3011 break;
3012 }
ss412g3bac2da2020-01-05 11:52:19 +02003013 case E2_TERM_KEEP_ALIVE_REQ: {
3014 // send message back
ss412g3bac2da2020-01-05 11:52:19 +02003015 rmr_bytes2payload(rmrMessageBuffer.sendMessage,
aa7133@att.com96c9b882020-03-25 14:50:40 +02003016 (unsigned char *)rmrMessageBuffer.ka_message,
3017 rmrMessageBuffer.ka_message_len);
ss412g3bac2da2020-01-05 11:52:19 +02003018 rmrMessageBuffer.sendMessage->mtype = E2_TERM_KEEP_ALIVE_RESP;
3019 rmrMessageBuffer.sendMessage->state = 0;
3020 static unsigned char tx[32];
3021 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
3022 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, txLen);
dhirajverma93b6d9c2021-07-10 00:49:13 -04003023#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
ss412g3bac2da2020-01-05 11:52:19 +02003024 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
dhirajverma60f8bef2021-06-29 07:22:58 -04003025#endif
ss412g3bac2da2020-01-05 11:52:19 +02003026 if (rmrMessageBuffer.sendMessage == nullptr) {
3027 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
aa7133@att.com77fff432020-01-27 12:01:20 +02003028 mdclog_write(MDCLOG_ERR, "Failed to send E2_TERM_KEEP_ALIVE_RESP RMR message returned NULL");
ss412g3bac2da2020-01-05 11:52:19 +02003029 } else if (rmrMessageBuffer.sendMessage->state != 0) {
aa7133@att.com77fff432020-01-27 12:01:20 +02003030 mdclog_write(MDCLOG_ERR, "Failed to send E2_TERM_KEEP_ALIVE_RESP, on RMR state = %d ( %s)",
aa7133@att.com96c9b882020-03-25 14:50:40 +02003031 rmrMessageBuffer.sendMessage->state, translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str());
aa7133@att.comf13529a2020-08-27 11:21:16 +03003032 } else if (loglevel >= MDCLOG_DEBUG) {
aa7133@att.combc110772020-03-25 09:45:14 +02003033 mdclog_write(MDCLOG_DEBUG, "Got Keep Alive Request send : %s", rmrMessageBuffer.ka_message);
ss412g3bac2da2020-01-05 11:52:19 +02003034 }
aa7133@att.com342843e2020-01-26 12:53:34 +02003035
ss412g3bac2da2020-01-05 11:52:19 +02003036 break;
3037 }
ss412g4aeeda22020-06-16 14:12:41 +03003038 case RIC_HEALTH_CHECK_REQ: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03003039 static int counter = 0;
ss412g4aeeda22020-06-16 14:12:41 +03003040 // send message back
aa7133@att.comc18fe532020-06-30 12:40:18 +03003041 rmr_bytes2payload(rmrMessageBuffer.rcvMessage,
ss412g4aeeda22020-06-16 14:12:41 +03003042 (unsigned char *)"OK",
3043 2);
aa7133@att.comc18fe532020-06-30 12:40:18 +03003044 rmrMessageBuffer.rcvMessage->mtype = RIC_HEALTH_CHECK_RESP;
3045 rmrMessageBuffer.rcvMessage->state = 0;
ss412g4aeeda22020-06-16 14:12:41 +03003046 static unsigned char tx[32];
3047 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
aa7133@att.comc18fe532020-06-30 12:40:18 +03003048 rmr_bytes2xact(rmrMessageBuffer.rcvMessage, tx, txLen);
3049 rmrMessageBuffer.rcvMessage = rmr_rts_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.rcvMessage);
3050 //rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
3051 if (rmrMessageBuffer.rcvMessage == nullptr) {
3052 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
ss412g4aeeda22020-06-16 14:12:41 +03003053 mdclog_write(MDCLOG_ERR, "Failed to send RIC_HEALTH_CHECK_RESP RMR message returned NULL");
aa7133@att.comc18fe532020-06-30 12:40:18 +03003054 } else if (rmrMessageBuffer.rcvMessage->state != 0) {
ss412g4aeeda22020-06-16 14:12:41 +03003055 mdclog_write(MDCLOG_ERR, "Failed to send RIC_HEALTH_CHECK_RESP, on RMR state = %d ( %s)",
aa7133@att.comc18fe532020-06-30 12:40:18 +03003056 rmrMessageBuffer.rcvMessage->state, translateRmrErrorMessages(rmrMessageBuffer.rcvMessage->state).c_str());
dhirajvermab55bc172021-11-09 02:15:08 -05003057 } else if (loglevel >= MDCLOG_DEBUG && (++counter % 100 == 0)) {
aa7133@att.comf13529a2020-08-27 11:21:16 +03003058 mdclog_write(MDCLOG_DEBUG, "Got %d RIC_HEALTH_CHECK_REQ Request send : OK", counter);
ss412g4aeeda22020-06-16 14:12:41 +03003059 }
3060
3061 break;
3062 }
3063
ss412g1a79bdf2019-10-24 12:03:05 +03003064 default:
aa7133@att.com409f2232020-10-29 13:06:45 +03003065 mdclog_write(MDCLOG_WARN, "Message Type : %d is not supported", rmrMessageBuffer.rcvMessage->mtype);
ss412g1a79bdf2019-10-24 12:03:05 +03003066 message.message.asndata = rmrMessageBuffer.rcvMessage->payload;
3067 message.message.asnLength = rmrMessageBuffer.rcvMessage->len;
3068 message.message.time.tv_nsec = ts.tv_nsec;
3069 message.message.time.tv_sec = ts.tv_sec;
3070 message.message.messageType = rmrMessageBuffer.rcvMessage->mtype;
3071
3072 buildJsonMessage(message);
3073
3074
ss412g1a79bdf2019-10-24 12:03:05 +03003075 return -7;
3076 }
3077 if (mdclog_level_get() >= MDCLOG_DEBUG) {
3078 mdclog_write(MDCLOG_DEBUG, "EXIT OK from %s", __FUNCTION__);
3079 }
ss412g1a79bdf2019-10-24 12:03:05 +03003080 return 0;
3081}
3082
3083/**
3084 * Send message to the CU that is not expecting for successful or unsuccessful results
3085 * @param messageBuffer
3086 * @param message
3087 * @param failedMsgId
3088 * @param sctpMap
ss412g1a79bdf2019-10-24 12:03:05 +03003089 * @return
3090 */
3091int sendDirectionalSctpMsg(RmrMessagesBuffer_t &messageBuffer,
3092 ReportingMessages_t &message,
3093 int failedMsgId,
aa7133@att.combe92d712020-03-18 17:30:39 +02003094 Sctp_Map_t *sctpMap) {
aa7133@att.com409f2232020-10-29 13:06:45 +03003095 if (mdclog_level_get() >= MDCLOG_DEBUG) {
3096 mdclog_write(MDCLOG_DEBUG, "send message: %d to %s address", message.message.messageType, message.message.enodbName);
3097 }
ss412g1a79bdf2019-10-24 12:03:05 +03003098
aa7133@att.combe92d712020-03-18 17:30:39 +02003099 getRequestMetaData(message, messageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03003100 if (mdclog_level_get() >= MDCLOG_INFO) {
3101 mdclog_write(MDCLOG_INFO, "send message to %s address", message.message.enodbName);
3102 }
3103
aa7133@att.combe92d712020-03-18 17:30:39 +02003104 auto rc = sendMessagetoCu(sctpMap, messageBuffer, message, failedMsgId);
ss412g1a79bdf2019-10-24 12:03:05 +03003105 return rc;
3106}
3107
3108/**
3109 *
3110 * @param sctpMap
3111 * @param messageBuffer
3112 * @param message
3113 * @param failedMesgId
ss412g1a79bdf2019-10-24 12:03:05 +03003114 * @return
3115 */
3116int sendMessagetoCu(Sctp_Map_t *sctpMap,
3117 RmrMessagesBuffer_t &messageBuffer,
3118 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +02003119 int failedMesgId) {
ss412g1a79bdf2019-10-24 12:03:05 +03003120 // get the FD
3121 message.message.messageType = messageBuffer.rcvMessage->mtype;
ss412g4aeeda22020-06-16 14:12:41 +03003122 auto rc = sendSctpMsg(message.peerInfo, message, sctpMap);
ss412g1a79bdf2019-10-24 12:03:05 +03003123 return rc;
3124}
3125
ss412g1a79bdf2019-10-24 12:03:05 +03003126
3127/**
3128 *
3129 * @param epoll_fd
3130 * @param peerInfo
3131 * @param events
3132 * @param sctpMap
3133 * @param enodbName
3134 * @param msgType
ss412g1a79bdf2019-10-24 12:03:05 +03003135 * @return
3136 */
3137int addToEpoll(int epoll_fd,
3138 ConnectedCU_t *peerInfo,
3139 uint32_t events,
3140 Sctp_Map_t *sctpMap,
3141 char *enodbName,
aa7133@att.combe92d712020-03-18 17:30:39 +02003142 int msgType) {
ss412g1a79bdf2019-10-24 12:03:05 +03003143 // Add to Epol
3144 struct epoll_event event{};
3145 event.data.ptr = peerInfo;
3146 event.events = events;
3147 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, peerInfo->fileDescriptor, &event) < 0) {
dhirajverma93b6d9c2021-07-10 00:49:13 -04003148#if !(defined(UNIT_TEST) || defined(MODULE_TEST))
ss412g1a79bdf2019-10-24 12:03:05 +03003149 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com409f2232020-10-29 13:06:45 +03003150 mdclog_write(MDCLOG_DEBUG, "epoll_ctl EPOLL_CTL_ADD (may check not to quit here), %s, %s %d",
ss412g1a79bdf2019-10-24 12:03:05 +03003151 strerror(errno), __func__, __LINE__);
3152 }
3153 close(peerInfo->fileDescriptor);
aa7133@att.com84bd3342020-03-16 18:04:57 +02003154 if (enodbName != nullptr) {
aa7133@att.combe92d712020-03-18 17:30:39 +02003155 cleanHashEntry(peerInfo, sctpMap);
aa7133@att.com84bd3342020-03-16 18:04:57 +02003156 char key[MAX_ENODB_NAME_SIZE * 2];
3157 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", enodbName, msgType);
3158 if (mdclog_level_get() >= MDCLOG_DEBUG) {
3159 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
3160 }
3161 auto tmp = sctpMap->find(key);
3162 if (tmp) {
3163 free(tmp);
sandeekuea662102022-11-08 18:51:20 +05303164 tmp = nullptr;
aa7133@att.com84bd3342020-03-16 18:04:57 +02003165 sctpMap->erase(key);
3166 }
3167 } else {
3168 peerInfo->enodbName[0] = 0;
ss412g1a79bdf2019-10-24 12:03:05 +03003169 }
aa7133@att.com409f2232020-10-29 13:06:45 +03003170 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may check not to quit here)");
ss412g1a79bdf2019-10-24 12:03:05 +03003171 return -1;
dhirajverma93b6d9c2021-07-10 00:49:13 -04003172#endif
ss412g1a79bdf2019-10-24 12:03:05 +03003173 }
ss412g1a79bdf2019-10-24 12:03:05 +03003174 return 0;
3175}
3176
3177/**
3178 *
3179 * @param epoll_fd
3180 * @param peerInfo
3181 * @param events
3182 * @param sctpMap
3183 * @param enodbName
3184 * @param msgType
ss412g1a79bdf2019-10-24 12:03:05 +03003185 * @return
3186 */
3187int modifyToEpoll(int epoll_fd,
3188 ConnectedCU_t *peerInfo,
3189 uint32_t events,
3190 Sctp_Map_t *sctpMap,
3191 char *enodbName,
aa7133@att.combe92d712020-03-18 17:30:39 +02003192 int msgType) {
ss412g1a79bdf2019-10-24 12:03:05 +03003193 // Add to Epol
3194 struct epoll_event event{};
3195 event.data.ptr = peerInfo;
3196 event.events = events;
3197 if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, peerInfo->fileDescriptor, &event) < 0) {
3198 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com409f2232020-10-29 13:06:45 +03003199 mdclog_write(MDCLOG_DEBUG, "epoll_ctl EPOLL_CTL_MOD (may check not to quit here), %s, %s %d",
ss412g1a79bdf2019-10-24 12:03:05 +03003200 strerror(errno), __func__, __LINE__);
3201 }
3202 close(peerInfo->fileDescriptor);
aa7133@att.combe92d712020-03-18 17:30:39 +02003203 cleanHashEntry(peerInfo, sctpMap);
ss412g1a79bdf2019-10-24 12:03:05 +03003204 char key[MAX_ENODB_NAME_SIZE * 2];
3205 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", enodbName, msgType);
3206 if (mdclog_level_get() >= MDCLOG_DEBUG) {
3207 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
3208 }
aa7133@att.com96c9b882020-03-25 14:50:40 +02003209 auto tmp = sctpMap->find(key);
ss412g1a79bdf2019-10-24 12:03:05 +03003210 if (tmp) {
3211 free(tmp);
sandeekuea662102022-11-08 18:51:20 +05303212 tmp = nullptr;
ss412g1a79bdf2019-10-24 12:03:05 +03003213 sctpMap->erase(key);
sandeekuea662102022-11-08 18:51:20 +05303214 }
aa7133@att.com409f2232020-10-29 13:06:45 +03003215 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may check not to quit here)");
ss412g1a79bdf2019-10-24 12:03:05 +03003216 return -1;
3217 }
ss412g1a79bdf2019-10-24 12:03:05 +03003218 return 0;
3219}
3220
3221
aa7133@att.combe92d712020-03-18 17:30:39 +02003222int sendRmrMessage(RmrMessagesBuffer_t &rmrMessageBuffer, ReportingMessages_t &message) {
ss412g1a79bdf2019-10-24 12:03:05 +03003223 buildJsonMessage(message);
dhirajverma60f8bef2021-06-29 07:22:58 -04003224#ifndef UNIT_TEST
ss412g1a79bdf2019-10-24 12:03:05 +03003225 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
dhirajverma60f8bef2021-06-29 07:22:58 -04003226#else
dhirajverma99990292021-07-05 10:03:59 -04003227 rmrMessageBuffer.sendMessage->state = RMR_ERR_RETRY;
dhirajverma60f8bef2021-06-29 07:22:58 -04003228#endif
ss412g1a79bdf2019-10-24 12:03:05 +03003229 if (rmrMessageBuffer.sendMessage == nullptr) {
3230 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
3231 mdclog_write(MDCLOG_ERR, "RMR failed send message returned with NULL pointer");
ss412g1a79bdf2019-10-24 12:03:05 +03003232 return -1;
3233 }
3234
3235 if (rmrMessageBuffer.sendMessage->state != 0) {
3236 char meid[RMR_MAX_MEID]{};
3237 if (rmrMessageBuffer.sendMessage->state == RMR_ERR_RETRY) {
3238 usleep(5);
3239 rmrMessageBuffer.sendMessage->state = 0;
3240 mdclog_write(MDCLOG_INFO, "RETRY sending Message type %d to Xapp from %s",
3241 rmrMessageBuffer.sendMessage->mtype,
3242 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
dhirajverma60f8bef2021-06-29 07:22:58 -04003243#ifndef UNIT_TEST
ss412g1a79bdf2019-10-24 12:03:05 +03003244 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
dhirajverma60f8bef2021-06-29 07:22:58 -04003245#endif
ss412g1a79bdf2019-10-24 12:03:05 +03003246 if (rmrMessageBuffer.sendMessage == nullptr) {
3247 mdclog_write(MDCLOG_ERR, "RMR failed send message returned with NULL pointer");
3248 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
ss412g1a79bdf2019-10-24 12:03:05 +03003249 return -1;
3250 } else if (rmrMessageBuffer.sendMessage->state != 0) {
3251 mdclog_write(MDCLOG_ERR,
3252 "Message state %s while sending request %d to Xapp from %s after retry of 10 microseconds",
3253 translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str(),
3254 rmrMessageBuffer.sendMessage->mtype,
3255 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
3256 auto rc = rmrMessageBuffer.sendMessage->state;
ss412g1a79bdf2019-10-24 12:03:05 +03003257 return rc;
3258 }
3259 } else {
3260 mdclog_write(MDCLOG_ERR, "Message state %s while sending request %d to Xapp from %s",
3261 translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str(),
3262 rmrMessageBuffer.sendMessage->mtype,
3263 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
ss412g1a79bdf2019-10-24 12:03:05 +03003264 return rmrMessageBuffer.sendMessage->state;
3265 }
3266 }
3267 return 0;
3268}
3269
3270void buildJsonMessage(ReportingMessages_t &message) {
dhirajverma60f8bef2021-06-29 07:22:58 -04003271#ifdef UNIT_TEST
dhirajverma99990292021-07-05 10:03:59 -04003272 jsonTrace = true;
dhirajverma60f8bef2021-06-29 07:22:58 -04003273#endif
ss412g3bac2da2020-01-05 11:52:19 +02003274 if (jsonTrace) {
3275 message.outLen = sizeof(message.base64Data);
3276 base64::encode((const unsigned char *) message.message.asndata,
3277 (const int) message.message.asnLength,
3278 message.base64Data,
3279 message.outLen);
3280 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.comc5a15202020-04-05 19:57:01 +03003281 mdclog_write(MDCLOG_DEBUG, "Tracing: ASN length = %d, base64 message length = %d ",
ss412g3bac2da2020-01-05 11:52:19 +02003282 (int) message.message.asnLength,
3283 (int) message.outLen);
3284 }
3285
3286 snprintf(message.buffer, sizeof(message.buffer),
aa7133@att.com96c9b882020-03-25 14:50:40 +02003287 "{\"header\": {\"ts\": \"%ld.%09ld\","
3288 "\"ranName\": \"%s\","
3289 "\"messageType\": %d,"
3290 "\"direction\": \"%c\"},"
3291 "\"base64Length\": %d,"
3292 "\"asnBase64\": \"%s\"}",
3293 message.message.time.tv_sec,
3294 message.message.time.tv_nsec,
3295 message.message.enodbName,
3296 message.message.messageType,
3297 message.message.direction,
3298 (int) message.outLen,
3299 message.base64Data);
ss412g3bac2da2020-01-05 11:52:19 +02003300 static src::logger_mt &lg = my_logger::get();
3301
3302 BOOST_LOG(lg) << message.buffer;
ss412g1a79bdf2019-10-24 12:03:05 +03003303 }
ss412g1a79bdf2019-10-24 12:03:05 +03003304}
3305
3306
3307/**
3308 * take RMR error code to string
3309 * @param state
3310 * @return
3311 */
3312string translateRmrErrorMessages(int state) {
3313 string str = {};
3314 switch (state) {
3315 case RMR_OK:
3316 str = "RMR_OK - state is good";
3317 break;
3318 case RMR_ERR_BADARG:
aa7133@att.com409f2232020-10-29 13:06:45 +03003319 str = "RMR_ERR_BADARG - argument passed to function was unusable";
ss412g1a79bdf2019-10-24 12:03:05 +03003320 break;
3321 case RMR_ERR_NOENDPT:
3322 str = "RMR_ERR_NOENDPT - send//call could not find an endpoint based on msg type";
3323 break;
3324 case RMR_ERR_EMPTY:
3325 str = "RMR_ERR_EMPTY - msg received had no payload; attempt to send an empty message";
3326 break;
3327 case RMR_ERR_NOHDR:
3328 str = "RMR_ERR_NOHDR - message didn't contain a valid header";
3329 break;
3330 case RMR_ERR_SENDFAILED:
3331 str = "RMR_ERR_SENDFAILED - send failed; errno has nano reason";
3332 break;
3333 case RMR_ERR_CALLFAILED:
3334 str = "RMR_ERR_CALLFAILED - unable to send call() message";
3335 break;
3336 case RMR_ERR_NOWHOPEN:
3337 str = "RMR_ERR_NOWHOPEN - no wormholes are open";
3338 break;
3339 case RMR_ERR_WHID:
3340 str = "RMR_ERR_WHID - wormhole id was invalid";
3341 break;
3342 case RMR_ERR_OVERFLOW:
3343 str = "RMR_ERR_OVERFLOW - operation would have busted through a buffer/field size";
3344 break;
3345 case RMR_ERR_RETRY:
3346 str = "RMR_ERR_RETRY - request (send/call/rts) failed, but caller should retry (EAGAIN for wrappers)";
3347 break;
3348 case RMR_ERR_RCVFAILED:
3349 str = "RMR_ERR_RCVFAILED - receive failed (hard error)";
3350 break;
3351 case RMR_ERR_TIMEOUT:
3352 str = "RMR_ERR_TIMEOUT - message processing call timed out";
3353 break;
3354 case RMR_ERR_UNSET:
3355 str = "RMR_ERR_UNSET - the message hasn't been populated with a transport buffer";
3356 break;
3357 case RMR_ERR_TRUNC:
3358 str = "RMR_ERR_TRUNC - received message likely truncated";
3359 break;
3360 case RMR_ERR_INITFAILED:
3361 str = "RMR_ERR_INITFAILED - initialisation of something (probably message) failed";
3362 break;
3363 case RMR_ERR_NOTSUPP:
3364 str = "RMR_ERR_NOTSUPP - the request is not supported, or RMr was not initialised for the request";
3365 break;
3366 default:
3367 char buf[128]{};
3368 snprintf(buf, sizeof buf, "UNDOCUMENTED RMR_ERR : %d", state);
3369 str = buf;
3370 break;
3371 }
3372 return str;
3373}
czichy2175dd32022-05-17 15:24:51 +03003374int fetchStreamId(ConnectedCU_t *peerInfo, ReportingMessages_t &message)
3375{
3376 auto loglevel = mdclog_level_get();
3377 int streamId = INVALID_STREAM_ID;
3378 if(message.peerInfo->isSingleStream != false)
3379 {
3380 streamId = message.peerInfo->singleStreamId;
3381 if (loglevel >= MDCLOG_DEBUG) {
3382 mdclog_write(MDCLOG_DEBUG, "Send SCTP message for SINGLE_STREAM streamId %d , Messeage Type %d ,%s",
3383 streamId,message.message.messageType, __FUNCTION__);
3384 }
3385 return streamId;
3386 }
3387 int msgType = message.message.messageType;
3388 switch (msgType){
3389 case RIC_E2_RESET_REQ:
3390 case RIC_E2_RESET_RESP:
3391 case RIC_E2_SETUP_RESP:
3392 case RIC_E2_SETUP_FAILURE:
3393 case RIC_ERROR_INDICATION:
3394 case RIC_SERVICE_QUERY:
3395 case RIC_SERVICE_UPDATE_ACK:
3396 case RIC_SERVICE_UPDATE_FAILURE:
3397 streamId = 0;
3398 break;
3399 case RIC_SUB_REQ:
3400 case RIC_SUB_DEL_REQ:
3401 case RIC_CONTROL_REQ:
3402 streamId = 1;
3403 break;
3404 default:
3405 streamId = 0;
3406 break;
3407 }
3408 if (loglevel >= MDCLOG_DEBUG) {
3409 mdclog_write(MDCLOG_DEBUG, "Send SCTP message for streamId %d Messeage Type %d, %s",
3410 streamId, message.message.messageType, __FUNCTION__);
3411 }
3412 return streamId;
3413}