blob: 15f5ba4b59fd01c96aea9603eb802de44d9a7ac8 [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
dhirajverma55e755b2021-06-29 02:01:47 +053063
64static int enable_log_change_notify(const char* fileName)
65{
66 int ret = -1;
67 struct stat fileInfo;
68 if ( lstat(fileName,&fileInfo) == 0 )
69 {
70 ret = register_log_change_notify(fileName);
71 }
72 return ret;
73}
74
75
76static int register_log_change_notify(const char *fileName)
77{
78 pthread_attr_t cb_attr;
79 pthread_t tid;
80 pthread_attr_init(&cb_attr);
81 pthread_attr_setdetachstate(&cb_attr,PTHREAD_CREATE_DETACHED);
82 return pthread_create(&tid, &cb_attr,&monitor_loglevel_change_handler,(void *)strdup(fileName));
83}
84
85
86static void * monitor_loglevel_change_handler(void* arg)
87{
88 char *fileName = (char*) arg;
89 int ifd; // the inotify file des
90 int wfd; // the watched file des
91 ssize_t n = 0;
92 char rbuf[4096]; // large read buffer as the event is var len
93 fd_set fds;
94 int res = 0;
95 struct timeval timeout;
96 char* dname=NULL; // directory name
97 char* bname = NULL; // basename
98 char* tok=NULL;
99 char* log_level=NULL;
100
101 dname = strdup( fileName); // defrock the file name into dir and basename
102 if( (tok = strrchr( dname, '/' )) != NULL ) {
103 *tok = '\0';
104 bname = strdup( tok+1 );
105 }
106
107
108 ifd = inotify_init1( 0 ); // initialise watcher setting blocking read (no option)
109 if( ifd < 0 ) {
110 fprintf( stderr, "### ERR ### unable to initialise file watch %s\n", strerror( errno ) );
111 } else {
112 wfd = inotify_add_watch( ifd, dname, IN_MOVED_TO | IN_CLOSE_WRITE ); // we only care about close write changes
113
114 if( wfd < 0 ) {
115 fprintf( stderr, "### ERR ### unable to add watch on config file %s: %s\n", fileName, strerror( errno ) );
116 } else {
117
118
119 memset( &timeout, 0, sizeof(timeout) );
120 while( 1 ) {
121 FD_ZERO (&fds);
122 FD_SET (ifd, &fds);
123 timeout.tv_sec=1;
124 res = select (ifd + 1, &fds, NULL, NULL, &timeout);
125 if(res)
126 {
127 n = read( ifd, rbuf, sizeof( rbuf ) ); // read the event
128 if( n < 0 ) {
129 if( errno == EAGAIN ) {
130 } else {
131 fprintf( stderr, "### CRIT ### config listener read err: %s\n", strerror( errno ) );
132 }
133 continue;
134 }
135
136 //Retrieving Log Level from configmap by parsing configmap file
137 log_level = parse_file(fileName);
138 update_mdc_log_level_severity(log_level); //setting log level
139 free(log_level);
140 }
141 }
142 inotify_rm_watch(ifd,wfd);
143 }
144 close(ifd);
145 }
146 free(bname);
147 free(dname);
148
149 pthread_exit(NULL);
150}
151
152void update_mdc_log_level_severity(char* log_level)
153{
154 mdclog_severity_t level = MDCLOG_ERR;
155
156 if(log_level == NULL)
157 {
158 printf("### ERR ### Invalid Log-Level Configuration in ConfigMap, Default Log-Level Applied: %d\n",level);
159 }
160 else if(strcasecmp(log_level,"1")==0)
161 {
162 level = MDCLOG_ERR;
163 }
164 else if(strcasecmp(log_level,"2")==0)
165 {
166 level = MDCLOG_WARN;
167 }
168 else if(strcasecmp(log_level,"3")==0)
169 {
170 level = MDCLOG_INFO;
171 }
172 else if(strcasecmp(log_level,"4")==0)
173 {
174 level = MDCLOG_DEBUG;
175 }
176
177 mdclog_level_set(level);
178}
179static char* parse_file(char* filename)
180{
181 char *token=NULL;
182 char *search = ": ";
183 char *string_match = "log-level";
184 bool found = false;
185 FILE *file = fopen ( filename, "r" );
186 if ( file != NULL )
187 {
188 char line [ 128 ];
189 while ( fgets ( line, sizeof line, file ) != NULL )
190 {
191 token = strtok(line, search);
192 if(strcmp(token,string_match)==0)
193 {
194 found = true;
195 token = strtok(NULL, search);
196 token = strtok(token, "\n");//removing newline if any
197 break;
198 }
199 }
200 fclose ( file );
201 }
202 if(found)
203 return(strdup(token));
204 else
205 return(NULL);
206}
207
208char *read_env_param(const char*envkey)
209{
210 if(envkey)
211 {
212 char *value = getenv(envkey);
213 if(value)
214 return strdup(value);
215 }
216 return NULL;
217}
218
219void dynamic_log_level_change()
220{
221 char *logFile_Name = read_env_param(LOG_FILE_CONFIG_MAP);
222 char* log_level_init=NULL;
223 if(logFile_Name)
224 {
225 log_level_init = parse_file(logFile_Name);
226 update_mdc_log_level_severity(log_level_init); //setting log level
227 free(log_level_init);
228
229 }
230 enable_log_change_notify(logFile_Name);
231 free(logFile_Name);
232
233}
234
ss412g1a79bdf2019-10-24 12:03:05 +0300235void init_log() {
dhirajverma55e755b2021-06-29 02:01:47 +0530236 int log_change_monitor = 0;
ss412g1a79bdf2019-10-24 12:03:05 +0300237 mdclog_attr_t *attr;
238 mdclog_attr_init(&attr);
239 mdclog_attr_set_ident(attr, "E2Terminator");
240 mdclog_init(attr);
dhirajverma5f132832021-06-17 17:27:54 +0530241 if(mdclog_format_initialize(log_change_monitor)!=0)
242 mdclog_write(MDCLOG_ERR, "Failed to intialize MDC log format !!!");
dhirajverma55e755b2021-06-29 02:01:47 +0530243 dynamic_log_level_change();
ss412g1a79bdf2019-10-24 12:03:05 +0300244 mdclog_attr_destroy(attr);
245}
ss412g3bac2da2020-01-05 11:52:19 +0200246auto start_time = std::chrono::high_resolution_clock::now();
247typedef std::chrono::duration<double, std::ratio<1,1>> seconds_t;
ss412g1a79bdf2019-10-24 12:03:05 +0300248
ss412g3bac2da2020-01-05 11:52:19 +0200249double age() {
250 return seconds_t(std::chrono::high_resolution_clock::now() - start_time).count();
251}
252
aa7133@att.com409f2232020-10-29 13:06:45 +0300253double approx_CPU_MHz(unsigned sleepTime) {
ss412g3bac2da2020-01-05 11:52:19 +0200254 using namespace std::chrono_literals;
255 uint32_t aux = 0;
256 uint64_t cycles_start = rdtscp(aux);
257 double time_start = age();
aa7133@att.com409f2232020-10-29 13:06:45 +0300258 std::this_thread::sleep_for(sleepTime * 1ms);
ss412g3bac2da2020-01-05 11:52:19 +0200259 uint64_t elapsed_cycles = rdtscp(aux) - cycles_start;
260 double elapsed_time = age() - time_start;
261 return elapsed_cycles / elapsed_time;
262}
ss412g1a79bdf2019-10-24 12:03:05 +0300263
264//std::atomic<int64_t> rmrCounter{0};
265std::atomic<int64_t> num_of_messages{0};
ss412g3bac2da2020-01-05 11:52:19 +0200266std::atomic<int64_t> num_of_XAPP_messages{0};
ss412g1a79bdf2019-10-24 12:03:05 +0300267static long transactionCounter = 0;
268
aa7133@att.com84bd3342020-03-16 18:04:57 +0200269int buildListeningPort(sctp_params_t &sctpParams) {
aa7133@att.comf211f192020-06-23 12:50:10 +0300270 sctpParams.listenFD = socket(AF_INET6, SOCK_STREAM, IPPROTO_SCTP);
271 if (sctpParams.listenFD <= 0) {
272 mdclog_write(MDCLOG_ERR, "Error Opening socket, %s", strerror(errno));
273 return -1;
274 }
275
aa7133@att.com409f2232020-10-29 13:06:45 +0300276 struct sockaddr_in6 serverAddress {};
277 serverAddress.sin6_family = AF_INET6;
278 serverAddress.sin6_addr = in6addr_any;
279 serverAddress.sin6_port = htons(sctpParams.sctpPort);
280 if (bind(sctpParams.listenFD, (SA *)&serverAddress, sizeof(serverAddress)) < 0 ) {
aa7133@att.comf211f192020-06-23 12:50:10 +0300281 mdclog_write(MDCLOG_ERR, "Error binding port %d. %s", sctpParams.sctpPort, strerror(errno));
aa7133@att.com84bd3342020-03-16 18:04:57 +0200282 return -1;
283 }
284 if (setSocketNoBlocking(sctpParams.listenFD) == -1) {
285 //mdclog_write(MDCLOG_ERR, "Error binding. %s", strerror(errno));
286 return -1;
287 }
288 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300289 struct sockaddr_in6 clientAddress {};
290 socklen_t len = sizeof(clientAddress);
291 getsockname(sctpParams.listenFD, (SA *)&clientAddress, &len);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200292 char buff[1024] {};
aa7133@att.com409f2232020-10-29 13:06:45 +0300293 inet_ntop(AF_INET6, &clientAddress.sin6_addr, buff, sizeof(buff));
294 mdclog_write(MDCLOG_DEBUG, "My address: %s, port %d\n", buff, htons(clientAddress.sin6_port));
ss412g1a79bdf2019-10-24 12:03:05 +0300295 }
296
aa7133@att.com84bd3342020-03-16 18:04:57 +0200297 if (listen(sctpParams.listenFD, SOMAXCONN) < 0) {
298 mdclog_write(MDCLOG_ERR, "Error listening. %s\n", strerror(errno));
299 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200300 }
aa7133@att.com84bd3342020-03-16 18:04:57 +0200301 struct epoll_event event {};
302 event.events = EPOLLIN | EPOLLET;
303 event.data.fd = sctpParams.listenFD;
304
305 // add listening port to epoll
306 if (epoll_ctl(sctpParams.epoll_fd, EPOLL_CTL_ADD, sctpParams.listenFD, &event)) {
307 printf("Failed to add descriptor to epoll\n");
308 mdclog_write(MDCLOG_ERR, "Failed to add descriptor to epoll. %s\n", strerror(errno));
309 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200310 }
311
aa7133@att.com84bd3342020-03-16 18:04:57 +0200312 return 0;
313}
ss412g3bac2da2020-01-05 11:52:19 +0200314
aa7133@att.com84bd3342020-03-16 18:04:57 +0200315int buildConfiguration(sctp_params_t &sctpParams) {
ss412g3bac2da2020-01-05 11:52:19 +0200316 path p = (sctpParams.configFilePath + "/" + sctpParams.configFileName).c_str();
317 if (exists(p)) {
318 const int size = 2048;
319 auto fileSize = file_size(p);
320 if (fileSize > size) {
321 mdclog_write(MDCLOG_ERR, "File %s larger than %d", p.string().c_str(), size);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200322 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200323 }
324 } else {
325 mdclog_write(MDCLOG_ERR, "Configuration File %s not exists", p.string().c_str());
aa7133@att.com84bd3342020-03-16 18:04:57 +0200326 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200327 }
328
ss412g3bac2da2020-01-05 11:52:19 +0200329 ReadConfigFile conf;
330 if (conf.openConfigFile(p.string()) == -1) {
331 mdclog_write(MDCLOG_ERR, "Filed to open config file %s, %s",
332 p.string().c_str(), strerror(errno));
aa7133@att.com84bd3342020-03-16 18:04:57 +0200333 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200334 }
335 int rmrPort = conf.getIntValue("nano");
336 if (rmrPort == -1) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300337 mdclog_write(MDCLOG_ERR, "illegal RMR port ");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200338 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200339 }
340 sctpParams.rmrPort = (uint16_t)rmrPort;
341 snprintf(sctpParams.rmrAddress, sizeof(sctpParams.rmrAddress), "%d", (int) (sctpParams.rmrPort));
dhirajverma5f132832021-06-17 17:27:54 +0530342 auto tmpStr = conf.getStringValue("volume");
ss412g3bac2da2020-01-05 11:52:19 +0200343 if (tmpStr.length() == 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300344 mdclog_write(MDCLOG_ERR, "illegal volume.");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200345 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200346 }
347
ss412g1a79bdf2019-10-24 12:03:05 +0300348 char tmpLogFilespec[VOLUME_URL_SIZE];
349 tmpLogFilespec[0] = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200350 sctpParams.volume[0] = 0;
351 snprintf(sctpParams.volume, VOLUME_URL_SIZE, "%s", tmpStr.c_str());
352 // copy the name to temp file as well
353 snprintf(tmpLogFilespec, VOLUME_URL_SIZE, "%s", tmpStr.c_str());
354
355
356 // define the file name in the tmp directory under the volume
357 strcat(tmpLogFilespec,"/tmp/E2Term_%Y-%m-%d_%H-%M-%S.%N.tmpStr");
358
359 sctpParams.myIP = conf.getStringValue("local-ip");
360 if (sctpParams.myIP.length() == 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300361 mdclog_write(MDCLOG_ERR, "illegal local-ip.");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200362 return -1;
ss412g1a79bdf2019-10-24 12:03:05 +0300363 }
364
aa7133@att.combe92d712020-03-18 17:30:39 +0200365 int sctpPort = conf.getIntValue("sctp-port");
366 if (sctpPort == -1) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300367 mdclog_write(MDCLOG_ERR, "illegal SCTP port ");
aa7133@att.combe92d712020-03-18 17:30:39 +0200368 return -1;
369 }
370 sctpParams.sctpPort = (uint16_t)sctpPort;
371
aa7133@att.com84bd3342020-03-16 18:04:57 +0200372 sctpParams.fqdn = conf.getStringValue("external-fqdn");
373 if (sctpParams.fqdn.length() == 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300374 mdclog_write(MDCLOG_ERR, "illegal external-fqdn");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200375 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200376 }
ss412g1a79bdf2019-10-24 12:03:05 +0300377
aa7133@att.com342843e2020-01-26 12:53:34 +0200378 std::string pod = conf.getStringValue("pod_name");
dhirajverma6cf28612021-06-23 04:53:34 -0400379#ifndef UNIT_TEST
aa7133@att.com342843e2020-01-26 12:53:34 +0200380 if (pod.length() == 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300381 mdclog_write(MDCLOG_ERR, "illegal pod_name in config file");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200382 return -1;
aa7133@att.com342843e2020-01-26 12:53:34 +0200383 }
384 auto *podName = getenv(pod.c_str());
385 if (podName == nullptr) {
aa7133@att.com54885fe2020-10-29 17:24:49 +0300386 mdclog_write(MDCLOG_ERR, "illegal pod_name or environment variable not exists : %s", pod.c_str());
aa7133@att.com84bd3342020-03-16 18:04:57 +0200387 return -1;
aa7133@att.com342843e2020-01-26 12:53:34 +0200388
389 } else {
390 sctpParams.podName.assign(podName);
391 if (sctpParams.podName.length() == 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300392 mdclog_write(MDCLOG_ERR, "illegal pod_name");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200393 return -1;
aa7133@att.com342843e2020-01-26 12:53:34 +0200394 }
395 }
dhirajverma6cf28612021-06-23 04:53:34 -0400396#endif
ss412g3bac2da2020-01-05 11:52:19 +0200397 tmpStr = conf.getStringValue("trace");
398 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
399 if ((tmpStr.compare("start")) == 0) {
400 mdclog_write(MDCLOG_INFO, "Trace set to: start");
401 sctpParams.trace = true;
402 } else if ((tmpStr.compare("stop")) == 0) {
403 mdclog_write(MDCLOG_INFO, "Trace set to: stop");
404 sctpParams.trace = false;
aa7133@att.com41fcb3f2020-11-29 13:48:25 +0200405 } else {
406 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 +0200407 sctpParams.trace = false;
ss412g3bac2da2020-01-05 11:52:19 +0200408 }
409 jsonTrace = sctpParams.trace;
ss412g1a79bdf2019-10-24 12:03:05 +0300410
ss412g4aeeda22020-06-16 14:12:41 +0300411 sctpParams.epollTimeOut = -1;
ss412g4aeeda22020-06-16 14:12:41 +0300412
413 tmpStr = conf.getStringValue("prometheusPort");
414 if (tmpStr.length() != 0) {
415 sctpParams.prometheusPort = tmpStr;
416 }
417
aa7133@att.combe92d712020-03-18 17:30:39 +0200418 sctpParams.ka_message_length = snprintf(sctpParams.ka_message, KA_MESSAGE_SIZE, "{\"address\": \"%s:%d\","
aa7133@att.com96c9b882020-03-25 14:50:40 +0200419 "\"fqdn\": \"%s\","
420 "\"pod_name\": \"%s\"}",
aa7133@att.com342843e2020-01-26 12:53:34 +0200421 (const char *)sctpParams.myIP.c_str(),
422 sctpParams.rmrPort,
423 sctpParams.fqdn.c_str(),
424 sctpParams.podName.c_str());
425
ss412g1a79bdf2019-10-24 12:03:05 +0300426 if (mdclog_level_get() >= MDCLOG_INFO) {
dhirajverma5f132832021-06-17 17:27:54 +0530427 mdclog_write(MDCLOG_DEBUG,"RMR Port: %s", to_string(sctpParams.rmrPort).c_str());
428 mdclog_write(MDCLOG_DEBUG,"LogLevel: %s", to_string(sctpParams.logLevel).c_str());
429 mdclog_write(MDCLOG_DEBUG,"volume: %s", sctpParams.volume);
430 mdclog_write(MDCLOG_DEBUG,"tmpLogFilespec: %s", tmpLogFilespec);
431 mdclog_write(MDCLOG_DEBUG,"my ip: %s", sctpParams.myIP.c_str());
432 mdclog_write(MDCLOG_DEBUG,"pod name: %s", sctpParams.podName.c_str());
ss412g1a79bdf2019-10-24 12:03:05 +0300433
aa7133@att.com342843e2020-01-26 12:53:34 +0200434 mdclog_write(MDCLOG_INFO, "running parameters for instance : %s", sctpParams.ka_message);
ss412g1a79bdf2019-10-24 12:03:05 +0300435 }
ss412g1a79bdf2019-10-24 12:03:05 +0300436
437 // Files written to the current working directory
438 boostLogger = logging::add_file_log(
ss412g3bac2da2020-01-05 11:52:19 +0200439 keywords::file_name = tmpLogFilespec, // to temp directory
ss412g1a79bdf2019-10-24 12:03:05 +0300440 keywords::rotation_size = 10 * 1024 * 1024,
441 keywords::time_based_rotation = sinks::file::rotation_at_time_interval(posix_time::hours(1)),
442 keywords::format = "%Message%"
ss412g3bac2da2020-01-05 11:52:19 +0200443 //keywords::format = "[%TimeStamp%]: %Message%" // use each tmpStr with time stamp
ss412g1a79bdf2019-10-24 12:03:05 +0300444 );
445
aa7133@att.com54885fe2020-10-29 17:24:49 +0300446 // Setup a destination folder for collecting rotated (closed) files --since the same volume can use rename()
ss412g1a79bdf2019-10-24 12:03:05 +0300447 boostLogger->locked_backend()->set_file_collector(sinks::file::make_collector(
ss412g3bac2da2020-01-05 11:52:19 +0200448 keywords::target = sctpParams.volume
ss412g1a79bdf2019-10-24 12:03:05 +0300449 ));
450
451 // Upon restart, scan the directory for files matching the file_name pattern
452 boostLogger->locked_backend()->scan_for_files();
453
ss412g3bac2da2020-01-05 11:52:19 +0200454 // Enable auto-flushing after each tmpStr record written
ss412g1a79bdf2019-10-24 12:03:05 +0300455 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com84bd3342020-03-16 18:04:57 +0200456 boostLogger->locked_backend()->auto_flush(true);
457 }
458
459 return 0;
460}
461
aa7133@att.coma4c80012020-06-24 16:36:47 +0300462void startPrometheus(sctp_params_t &sctpParams) {
463 sctpParams.prometheusFamily = &BuildCounter()
464 .Name("E2T")
465 .Help("E2T message counter")
466 .Labels({{"POD_NAME", sctpParams.podName}})
467 .Register(*sctpParams.prometheusRegistry);
aa7133@att.com16c799b2020-03-24 18:01:51 +0200468
aa7133@att.com54885fe2020-10-29 17:24:49 +0300469 string prometheusPath = sctpParams.prometheusPort + "," + "[::]:" + sctpParams.prometheusPort;
aa7133@att.comb92748d2020-06-29 11:52:30 +0300470 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com54885fe2020-10-29 17:24:49 +0300471 mdclog_write(MDCLOG_DEBUG, "Start Prometheus Pull mode on %s", prometheusPath.c_str());
aa7133@att.coma4c80012020-06-24 16:36:47 +0300472 }
aa7133@att.com54885fe2020-10-29 17:24:49 +0300473 sctpParams.prometheusExposer = new Exposer(prometheusPath, 1);
aa7133@att.comb92748d2020-06-29 11:52:30 +0300474 sctpParams.prometheusExposer->RegisterCollectable(sctpParams.prometheusRegistry);
aa7133@att.coma4c80012020-06-24 16:36:47 +0300475}
dhirajverma31768a92021-05-06 12:38:49 +0530476#ifndef UNIT_TEST
dhirajverma5f132832021-06-17 17:27:54 +0530477
aa7133@att.com84bd3342020-03-16 18:04:57 +0200478int main(const int argc, char **argv) {
479 sctp_params_t sctpParams;
aa7133@att.com84bd3342020-03-16 18:04:57 +0200480 {
481 std::random_device device{};
482 std::mt19937 generator(device());
483 std::uniform_int_distribution<long> distribution(1, (long) 1e12);
484 transactionCounter = distribution(generator);
485 }
486
487// uint64_t st = 0;
488// uint32_t aux1 = 0;
489// st = rdtscp(aux1);
490
491 unsigned num_cpus = std::thread::hardware_concurrency();
492 init_log();
aa7133@att.com84bd3342020-03-16 18:04:57 +0200493 if (std::signal(SIGINT, catch_function) == SIG_ERR) {
494 mdclog_write(MDCLOG_ERR, "Error initializing SIGINT");
495 exit(1);
496 }
497 if (std::signal(SIGABRT, catch_function)== SIG_ERR) {
498 mdclog_write(MDCLOG_ERR, "Error initializing SIGABRT");
499 exit(1);
500 }
501 if (std::signal(SIGTERM, catch_function)== SIG_ERR) {
502 mdclog_write(MDCLOG_ERR, "Error initializing SIGTERM");
503 exit(1);
504 }
505
506 cpuClock = approx_CPU_MHz(100);
507
508 mdclog_write(MDCLOG_DEBUG, "CPU speed %11.11f", cpuClock);
509
510 auto result = parse(argc, argv, sctpParams);
511
512 if (buildConfiguration(sctpParams) != 0) {
513 exit(-1);
ss412g1a79bdf2019-10-24 12:03:05 +0300514 }
515
ss412g4aeeda22020-06-16 14:12:41 +0300516 //auto registry = std::make_shared<Registry>();
517 sctpParams.prometheusRegistry = std::make_shared<Registry>();
518
aa7133@att.com409f2232020-10-29 13:06:45 +0300519 //sctpParams.prometheusFamily = new Family<Counter>("E2T", "E2T message counter", {{"E", sctpParams.podName}});
ss412g4aeeda22020-06-16 14:12:41 +0300520
aa7133@att.coma4c80012020-06-24 16:36:47 +0300521 startPrometheus(sctpParams);
ss412g4aeeda22020-06-16 14:12:41 +0300522
ss412g1a79bdf2019-10-24 12:03:05 +0300523 // start epoll
ss412g3bac2da2020-01-05 11:52:19 +0200524 sctpParams.epoll_fd = epoll_create1(0);
525 if (sctpParams.epoll_fd == -1) {
ss412g1a79bdf2019-10-24 12:03:05 +0300526 mdclog_write(MDCLOG_ERR, "failed to open epoll descriptor");
527 exit(-1);
528 }
aa7133@att.combe92d712020-03-18 17:30:39 +0200529 getRmrContext(sctpParams);
ss412g3bac2da2020-01-05 11:52:19 +0200530 if (sctpParams.rmrCtx == nullptr) {
531 close(sctpParams.epoll_fd);
ss412g1a79bdf2019-10-24 12:03:05 +0300532 exit(-1);
533 }
534
ss412g3bac2da2020-01-05 11:52:19 +0200535 if (buildInotify(sctpParams) == -1) {
536 close(sctpParams.rmrListenFd);
537 rmr_close(sctpParams.rmrCtx);
538 close(sctpParams.epoll_fd);
539 exit(-1);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200540 }
541
542 if (buildListeningPort(sctpParams) != 0) {
543 close(sctpParams.rmrListenFd);
544 rmr_close(sctpParams.rmrCtx);
545 close(sctpParams.epoll_fd);
546 exit(-1);
547 }
ss412g3bac2da2020-01-05 11:52:19 +0200548
549 sctpParams.sctpMap = new mapWrapper();
ss412g1a79bdf2019-10-24 12:03:05 +0300550
551 std::vector<std::thread> threads(num_cpus);
552// std::vector<std::thread> threads;
553
aa7133@att.com24e9fa42020-11-13 20:32:53 +0200554 num_cpus = 3;
ss412g1a79bdf2019-10-24 12:03:05 +0300555 for (unsigned int i = 0; i < num_cpus; i++) {
ss412g3bac2da2020-01-05 11:52:19 +0200556 threads[i] = std::thread(listener, &sctpParams);
ss412g1a79bdf2019-10-24 12:03:05 +0300557
558 cpu_set_t cpuset;
559 CPU_ZERO(&cpuset);
560 CPU_SET(i, &cpuset);
561 int rc = pthread_setaffinity_np(threads[i].native_handle(), sizeof(cpu_set_t), &cpuset);
562 if (rc != 0) {
563 mdclog_write(MDCLOG_ERR, "Error calling pthread_setaffinity_np: %d", rc);
564 }
ss412g1a79bdf2019-10-24 12:03:05 +0300565 }
566
aa7133@att.com16c799b2020-03-24 18:01:51 +0200567
ss412g3bac2da2020-01-05 11:52:19 +0200568 //loop over term_init until first message from xApp
569 handleTermInit(sctpParams);
570
571 for (auto &t : threads) {
572 t.join();
573 }
574
ss412g3bac2da2020-01-05 11:52:19 +0200575 return 0;
576}
dhirajverma60f8bef2021-06-29 07:22:58 -0400577#endif
ss412g3bac2da2020-01-05 11:52:19 +0200578void handleTermInit(sctp_params_t &sctpParams) {
579 sendTermInit(sctpParams);
ss412g1a79bdf2019-10-24 12:03:05 +0300580 //send to e2 manager init of e2 term
581 //E2_TERM_INIT
ss412g1a79bdf2019-10-24 12:03:05 +0300582
ss412g3bac2da2020-01-05 11:52:19 +0200583 int count = 0;
584 while (true) {
585 auto xappMessages = num_of_XAPP_messages.load(std::memory_order_acquire);
586 if (xappMessages > 0) {
587 if (mdclog_level_get() >= MDCLOG_INFO) {
aa7133@att.com54885fe2020-10-29 17:24:49 +0300588 mdclog_write(MDCLOG_INFO, "Got a message from some application, stop sending E2_TERM_INIT");
ss412g3bac2da2020-01-05 11:52:19 +0200589 }
590 return;
591 }
592 usleep(100000);
593 count++;
594 if (count % 1000 == 0) {
595 mdclog_write(MDCLOG_ERR, "GOT No messages from any xApp");
596 sendTermInit(sctpParams);
597 }
598 }
599}
600
601void sendTermInit(sctp_params_t &sctpParams) {
602 rmr_mbuf_t *msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.ka_message_length);
ss412g1a79bdf2019-10-24 12:03:05 +0300603 auto count = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200604 while (true) {
ss412g1a79bdf2019-10-24 12:03:05 +0300605 msg->mtype = E2_TERM_INIT;
606 msg->state = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200607 rmr_bytes2payload(msg, (unsigned char *)sctpParams.ka_message, sctpParams.ka_message_length);
ss412g1a79bdf2019-10-24 12:03:05 +0300608 static unsigned char tx[32];
609 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
610 rmr_bytes2xact(msg, tx, txLen);
ss412g3bac2da2020-01-05 11:52:19 +0200611 msg = rmr_send_msg(sctpParams.rmrCtx, msg);
ss412g1a79bdf2019-10-24 12:03:05 +0300612 if (msg == nullptr) {
aa7133@att.comc9575aa2020-04-02 11:40:09 +0300613 msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.ka_message_length);
ss412g1a79bdf2019-10-24 12:03:05 +0300614 } else if (msg->state == 0) {
ss412g1a79bdf2019-10-24 12:03:05 +0300615 rmr_free_msg(msg);
ss412g3bac2da2020-01-05 11:52:19 +0200616 if (mdclog_level_get() >= MDCLOG_INFO) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300617 mdclog_write(MDCLOG_INFO, "E2_TERM_INIT successfully sent ");
ss412g3bac2da2020-01-05 11:52:19 +0200618 }
619 return;
ss412g1a79bdf2019-10-24 12:03:05 +0300620 } else {
621 if (count % 100 == 0) {
aa7133@att.com11b17d42020-03-22 11:35:16 +0200622 mdclog_write(MDCLOG_ERR, "Error sending E2_TERM_INIT cause : %s ", translateRmrErrorMessages(msg->state).c_str());
ss412g1a79bdf2019-10-24 12:03:05 +0300623 }
624 sleep(1);
625 }
626 count++;
627 }
ss412g3bac2da2020-01-05 11:52:19 +0200628}
629
630/**
631 *
632 * @param argc
633 * @param argv
634 * @param sctpParams
635 * @return
636 */
637cxxopts::ParseResult parse(int argc, char *argv[], sctp_params_t &sctpParams) {
638 cxxopts::Options options(argv[0], "e2 term help");
639 options.positional_help("[optional args]").show_positional_help();
640 options.allow_unrecognised_options().add_options()
641 ("p,path", "config file path", cxxopts::value<std::string>(sctpParams.configFilePath)->default_value("config"))
642 ("f,file", "config file name", cxxopts::value<std::string>(sctpParams.configFileName)->default_value("config.conf"))
643 ("h,help", "Print help");
644
aa7133@att.comde40d1c2020-08-26 16:19:20 +0300645 auto result = options.parse(argc, (const char **&)argv);
ss412g3bac2da2020-01-05 11:52:19 +0200646
647 if (result.count("help")) {
648 std::cout << options.help({""}) << std::endl;
649 exit(0);
650 }
651 return result;
652}
653
654/**
655 *
656 * @param sctpParams
657 * @return -1 failed 0 success
658 */
659int buildInotify(sctp_params_t &sctpParams) {
660 sctpParams.inotifyFD = inotify_init1(IN_NONBLOCK);
661 if (sctpParams.inotifyFD == -1) {
662 mdclog_write(MDCLOG_ERR, "Failed to init inotify (inotify_init1) %s", strerror(errno));
ss412g3bac2da2020-01-05 11:52:19 +0200663 return -1;
ss412g1a79bdf2019-10-24 12:03:05 +0300664 }
665
ss412g3bac2da2020-01-05 11:52:19 +0200666 sctpParams.inotifyWD = inotify_add_watch(sctpParams.inotifyFD,
aa7133@att.com96c9b882020-03-25 14:50:40 +0200667 (const char *)sctpParams.configFilePath.c_str(),
aa7133@att.com84bd3342020-03-16 18:04:57 +0200668 (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 +0200669 if (sctpParams.inotifyWD == -1) {
670 mdclog_write(MDCLOG_ERR, "Failed to add directory : %s to inotify (inotify_add_watch) %s",
aa7133@att.com96c9b882020-03-25 14:50:40 +0200671 sctpParams.configFilePath.c_str(),
672 strerror(errno));
ss412g3bac2da2020-01-05 11:52:19 +0200673 close(sctpParams.inotifyFD);
674 return -1;
675 }
676
677 struct epoll_event event{};
678 event.events = (EPOLLIN);
679 event.data.fd = sctpParams.inotifyFD;
680 // add listening RMR FD to epoll
681 if (epoll_ctl(sctpParams.epoll_fd, EPOLL_CTL_ADD, sctpParams.inotifyFD, &event)) {
682 mdclog_write(MDCLOG_ERR, "Failed to add inotify FD to epoll");
683 close(sctpParams.inotifyFD);
684 return -1;
685 }
ss412g1a79bdf2019-10-24 12:03:05 +0300686 return 0;
687}
688
689/**
690 *
691 * @param args
692 * @return
693 */
694void listener(sctp_params_t *params) {
ss412g1a79bdf2019-10-24 12:03:05 +0300695 int num_of_SCTP_messages = 0;
ss412g1a79bdf2019-10-24 12:03:05 +0300696 auto totalTime = 0.0;
ss412g1a79bdf2019-10-24 12:03:05 +0300697 std::thread::id this_id = std::this_thread::get_id();
698 //save cout
dhirajverma5f132832021-06-17 17:27:54 +0530699 auto pod_name = std::getenv("POD_NAME");
700 auto container_name = std::getenv("CONTAINER_NAME");
701 auto service_name = std::getenv("SERVICE_NAME");
702 auto host_name = std::getenv("HOST_NAME");
703 auto system_name = std::getenv("SYSTEM_NAME");
704 auto pid = std::to_string(getpid()).c_str();
ss412g1a79bdf2019-10-24 12:03:05 +0300705 streambuf *oldCout = cout.rdbuf();
706 ostringstream memCout;
707 // create new cout
708 cout.rdbuf(memCout.rdbuf());
709 cout << this_id;
710 //return to the normal cout
711 cout.rdbuf(oldCout);
712
713 char tid[32];
714 memcpy(tid, memCout.str().c_str(), memCout.str().length() < 32 ? memCout.str().length() : 31);
715 tid[memCout.str().length()] = 0;
dhirajverma5f132832021-06-17 17:27:54 +0530716 mdclog_mdc_add("SYSTEM_NAME", system_name);
717 mdclog_mdc_add("HOST_NAME", host_name);
718 mdclog_mdc_add("SERVICE_NAME", service_name);
719 mdclog_mdc_add("CONTAINER_NAME", container_name);
720 mdclog_mdc_add("POD_NAME", pod_name);
721 mdclog_mdc_add("PID", pid);
ss412g1a79bdf2019-10-24 12:03:05 +0300722
723 if (mdclog_level_get() >= MDCLOG_DEBUG) {
724 mdclog_write(MDCLOG_DEBUG, "started thread number %s", tid);
725 }
726
727 RmrMessagesBuffer_t rmrMessageBuffer{};
728 //create and init RMR
729 rmrMessageBuffer.rmrCtx = params->rmrCtx;
730
731 auto *events = (struct epoll_event *) calloc(MAXEVENTS, sizeof(struct epoll_event));
732 struct timespec end{0, 0};
733 struct timespec start{0, 0};
734
735 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
736 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
737
ss412g3bac2da2020-01-05 11:52:19 +0200738 memcpy(rmrMessageBuffer.ka_message, params->ka_message, params->ka_message_length);
739 rmrMessageBuffer.ka_message_len = params->ka_message_length;
740 rmrMessageBuffer.ka_message[rmrMessageBuffer.ka_message_len] = 0;
741
742 if (mdclog_level_get() >= MDCLOG_DEBUG) {
743 mdclog_write(MDCLOG_DEBUG, "keep alive message is : %s", rmrMessageBuffer.ka_message);
744 }
745
ss412g1a79bdf2019-10-24 12:03:05 +0300746 ReportingMessages_t message {};
747
aa7133@att.com409f2232020-10-29 13:06:45 +0300748// for (int i = 0; i < MAX_RMR_BUFF_ARRAY; i++) {
aa7133@att.com7c3e4c02020-03-25 16:37:19 +0200749// rmrMessageBuffer.rcvBufferedMessages[i] = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
750// rmrMessageBuffer.sendBufferedMessages[i] = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
751// }
ss412g1a79bdf2019-10-24 12:03:05 +0300752
753 while (true) {
754 if (mdclog_level_get() >= MDCLOG_DEBUG) {
ss412g4aeeda22020-06-16 14:12:41 +0300755 mdclog_write(MDCLOG_DEBUG, "Start EPOLL Wait. Timeout = %d", params->epollTimeOut);
ss412g1a79bdf2019-10-24 12:03:05 +0300756 }
dhirajverma6cf28612021-06-23 04:53:34 -0400757#ifndef UNIT_TEST
ss412g4aeeda22020-06-16 14:12:41 +0300758 auto numOfEvents = epoll_wait(params->epoll_fd, events, MAXEVENTS, params->epollTimeOut);
dhirajverma6cf28612021-06-23 04:53:34 -0400759#else
760 auto numOfEvents = 1;
761#endif
aa7133@att.comb92748d2020-06-29 11:52:30 +0300762 if (numOfEvents == 0) { // time out
763 if (mdclog_level_get() >= MDCLOG_DEBUG) {
764 mdclog_write(MDCLOG_DEBUG, "got epoll timeout");
ss412g1a79bdf2019-10-24 12:03:05 +0300765 }
766 continue;
ss412g4aeeda22020-06-16 14:12:41 +0300767 } else if (numOfEvents < 0) {
768 if (errno == EINTR) {
769 if (mdclog_level_get() >= MDCLOG_DEBUG) {
770 mdclog_write(MDCLOG_DEBUG, "got EINTR : %s", strerror(errno));
771 }
772 continue;
773 }
ss412g1a79bdf2019-10-24 12:03:05 +0300774 mdclog_write(MDCLOG_ERR, "Epoll wait failed, errno = %s", strerror(errno));
775 return;
776 }
777 for (auto i = 0; i < numOfEvents; i++) {
778 if (mdclog_level_get() >= MDCLOG_DEBUG) {
779 mdclog_write(MDCLOG_DEBUG, "handling epoll event %d out of %d", i + 1, numOfEvents);
780 }
781 clock_gettime(CLOCK_MONOTONIC, &message.message.time);
782 start.tv_sec = message.message.time.tv_sec;
783 start.tv_nsec = message.message.time.tv_nsec;
ss412g3bac2da2020-01-05 11:52:19 +0200784
785
ss412g1a79bdf2019-10-24 12:03:05 +0300786 if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP)) {
aa7133@att.combe92d712020-03-18 17:30:39 +0200787 handlepoll_error(events[i], message, rmrMessageBuffer, params);
ss412g1a79bdf2019-10-24 12:03:05 +0300788 } else if (events[i].events & EPOLLOUT) {
aa7133@att.combe92d712020-03-18 17:30:39 +0200789 handleEinprogressMessages(events[i], message, rmrMessageBuffer, params);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200790 } else if (params->listenFD == events[i].data.fd) {
aa7133@att.com6680c3b2020-03-22 12:06:58 +0200791 if (mdclog_level_get() >= MDCLOG_INFO) {
792 mdclog_write(MDCLOG_INFO, "New connection request from sctp network\n");
793 }
aa7133@att.com84bd3342020-03-16 18:04:57 +0200794 // new connection is requested from RAN start build connection
795 while (true) {
796 struct sockaddr in_addr {};
797 socklen_t in_len;
798 char hostBuff[NI_MAXHOST];
799 char portBuff[NI_MAXSERV];
800
801 in_len = sizeof(in_addr);
802 auto *peerInfo = (ConnectedCU_t *)calloc(1, sizeof(ConnectedCU_t));
aa7133@att.com409f2232020-10-29 13:06:45 +0300803 if(peerInfo == nullptr){
swc58d4f5d2020-10-20 15:08:38 +0900804 mdclog_write(MDCLOG_ERR, "calloc failed");
805 break;
806 }
aa7133@att.com84bd3342020-03-16 18:04:57 +0200807 peerInfo->sctpParams = params;
808 peerInfo->fileDescriptor = accept(params->listenFD, &in_addr, &in_len);
809 if (peerInfo->fileDescriptor == -1) {
810 if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
811 /* We have processed all incoming connections. */
812 break;
813 } else {
814 mdclog_write(MDCLOG_ERR, "Accept error, errno = %s", strerror(errno));
815 break;
816 }
817 }
818 if (setSocketNoBlocking(peerInfo->fileDescriptor) == -1) {
819 mdclog_write(MDCLOG_ERR, "setSocketNoBlocking failed to set new connection %s on port %s\n", hostBuff, portBuff);
820 close(peerInfo->fileDescriptor);
821 break;
822 }
823 auto ans = getnameinfo(&in_addr, in_len,
aa7133@att.com96c9b882020-03-25 14:50:40 +0200824 peerInfo->hostName, NI_MAXHOST,
825 peerInfo->portNumber, NI_MAXSERV, (unsigned )((unsigned int)NI_NUMERICHOST | (unsigned int)NI_NUMERICSERV));
aa7133@att.com84bd3342020-03-16 18:04:57 +0200826 if (ans < 0) {
827 mdclog_write(MDCLOG_ERR, "Failed to get info on connection request. %s\n", strerror(errno));
828 close(peerInfo->fileDescriptor);
829 break;
830 }
831 if (mdclog_level_get() >= MDCLOG_DEBUG) {
832 mdclog_write(MDCLOG_DEBUG, "Accepted connection on descriptor %d (host=%s, port=%s)\n", peerInfo->fileDescriptor, peerInfo->hostName, peerInfo->portNumber);
833 }
834 peerInfo->isConnected = false;
835 peerInfo->gotSetup = false;
836 if (addToEpoll(params->epoll_fd,
837 peerInfo,
838 (EPOLLIN | EPOLLET),
839 params->sctpMap, nullptr,
aa7133@att.combe92d712020-03-18 17:30:39 +0200840 0) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +0200841 break;
842 }
aa7133@att.com6680c3b2020-03-22 12:06:58 +0200843 break;
aa7133@att.com84bd3342020-03-16 18:04:57 +0200844 }
ss412g1a79bdf2019-10-24 12:03:05 +0300845 } else if (params->rmrListenFd == events[i].data.fd) {
846 // got message from XAPP
aa7133@att.comde40d1c2020-08-26 16:19:20 +0300847 //num_of_XAPP_messages.fetch_add(1, std::memory_order_release);
ss412g1a79bdf2019-10-24 12:03:05 +0300848 num_of_messages.fetch_add(1, std::memory_order_release);
849 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.comf13529a2020-08-27 11:21:16 +0300850 mdclog_write(MDCLOG_DEBUG, "new RMR message");
ss412g1a79bdf2019-10-24 12:03:05 +0300851 }
aa7133@att.com6680c3b2020-03-22 12:06:58 +0200852 if (receiveXappMessages(params->sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +0300853 rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200854 message.message.time) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +0300855 mdclog_write(MDCLOG_ERR, "Error handling Xapp message");
856 }
ss412g3bac2da2020-01-05 11:52:19 +0200857 } else if (params->inotifyFD == events[i].data.fd) {
858 mdclog_write(MDCLOG_INFO, "Got event from inotify (configuration update)");
859 handleConfigChange(params);
ss412g1a79bdf2019-10-24 12:03:05 +0300860 } else {
861 /* We RMR_ERR_RETRY have data on the fd waiting to be read. Read and display it.
862 * We must read whatever data is available completely, as we are running
863 * in edge-triggered mode and won't get a notification again for the same data. */
864 num_of_messages.fetch_add(1, std::memory_order_release);
865 if (mdclog_level_get() >= MDCLOG_DEBUG) {
866 mdclog_write(MDCLOG_DEBUG, "new message from SCTP, epoll flags are : %0x", events[i].events);
867 }
868 receiveDataFromSctp(&events[i],
869 params->sctpMap,
870 num_of_SCTP_messages,
871 rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200872 message.message.time);
ss412g1a79bdf2019-10-24 12:03:05 +0300873 }
874
875 clock_gettime(CLOCK_MONOTONIC, &end);
876 if (mdclog_level_get() >= MDCLOG_INFO) {
877 totalTime += ((end.tv_sec + 1.0e-9 * end.tv_nsec) -
878 ((double) start.tv_sec + 1.0e-9 * start.tv_nsec));
879 }
880 if (mdclog_level_get() >= MDCLOG_DEBUG) {
881 mdclog_write(MDCLOG_DEBUG, "message handling is %ld seconds %ld nanoseconds",
882 end.tv_sec - start.tv_sec,
883 end.tv_nsec - start.tv_nsec);
884 }
885 }
dhirajverma6cf28612021-06-23 04:53:34 -0400886#ifdef UNIT_TEST
dhirajverma99990292021-07-05 10:03:59 -0400887 break;
dhirajverma6cf28612021-06-23 04:53:34 -0400888#endif
ss412g1a79bdf2019-10-24 12:03:05 +0300889 }
ss412g1a79bdf2019-10-24 12:03:05 +0300890}
891
892/**
893 *
ss412g3bac2da2020-01-05 11:52:19 +0200894 * @param sctpParams
895 */
896void handleConfigChange(sctp_params_t *sctpParams) {
897 char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event))));
898 const struct inotify_event *event;
899 char *ptr;
dhirajverma60f8bef2021-06-29 07:22:58 -0400900#ifdef UNIT_TEST
901 struct inotify_event tmpEvent;
902#endif
ss412g3bac2da2020-01-05 11:52:19 +0200903 path p = (sctpParams->configFilePath + "/" + sctpParams->configFileName).c_str();
904 auto endlessLoop = true;
905 while (endlessLoop) {
dhirajverma60f8bef2021-06-29 07:22:58 -0400906#ifndef UNIT_TEST
907 auto len = read(sctpParams->inotifyFD, buf, sizeof buf);
908#else
909 auto len=10;
910#endif
ss412g3bac2da2020-01-05 11:52:19 +0200911 if (len == -1) {
912 if (errno != EAGAIN) {
913 mdclog_write(MDCLOG_ERR, "read %s ", strerror(errno));
914 endlessLoop = false;
915 continue;
916 }
917 else {
918 endlessLoop = false;
919 continue;
920 }
921 }
922
923 for (ptr = buf; ptr < buf + len; ptr += sizeof(struct inotify_event) + event->len) {
dhirajverma60f8bef2021-06-29 07:22:58 -0400924#ifndef UNIT_TEST
925 event = (const struct inotify_event *)ptr;
926#else
927 tmpEvent.mask = (uint32_t)IN_CLOSE_WRITE;
928 event = &tmpEvent;
929#endif
ss412g3bac2da2020-01-05 11:52:19 +0200930 if (event->mask & (uint32_t)IN_ISDIR) {
931 continue;
932 }
933
934 // the directory name
935 if (sctpParams->inotifyWD == event->wd) {
936 // not the directory
937 }
938 if (event->len) {
aa7133@att.comdfc1db92020-03-31 17:32:04 +0300939 auto retVal = strcmp(sctpParams->configFileName.c_str(), event->name);
940 if (retVal != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200941 continue;
942 }
943 }
944 // only the file we want
945 if (event->mask & (uint32_t)IN_CLOSE_WRITE) {
aa7133@att.comdfc1db92020-03-31 17:32:04 +0300946 if (mdclog_level_get() >= MDCLOG_INFO) {
947 mdclog_write(MDCLOG_INFO, "Configuration file changed");
948 }
ss412g3bac2da2020-01-05 11:52:19 +0200949 if (exists(p)) {
950 const int size = 2048;
951 auto fileSize = file_size(p);
952 if (fileSize > size) {
953 mdclog_write(MDCLOG_ERR, "File %s larger than %d", p.string().c_str(), size);
954 return;
955 }
956 } else {
957 mdclog_write(MDCLOG_ERR, "Configuration File %s not exists", p.string().c_str());
958 return;
959 }
960
961 ReadConfigFile conf;
962 if (conf.openConfigFile(p.string()) == -1) {
963 mdclog_write(MDCLOG_ERR, "Filed to open config file %s, %s",
964 p.string().c_str(), strerror(errno));
965 return;
966 }
ss412g3bac2da2020-01-05 11:52:19 +0200967 auto tmpStr = conf.getStringValue("loglevel");
968 if (tmpStr.length() == 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300969 mdclog_write(MDCLOG_ERR, "illegal loglevel. Set loglevel to MDCLOG_INFO");
ss412g3bac2da2020-01-05 11:52:19 +0200970 tmpStr = "info";
971 }
972 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
973
974 if ((tmpStr.compare("debug")) == 0) {
975 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_DEBUG");
976 sctpParams->logLevel = MDCLOG_DEBUG;
977 } else if ((tmpStr.compare("info")) == 0) {
978 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_INFO");
979 sctpParams->logLevel = MDCLOG_INFO;
980 } else if ((tmpStr.compare("warning")) == 0) {
981 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_WARN");
982 sctpParams->logLevel = MDCLOG_WARN;
983 } else if ((tmpStr.compare("error")) == 0) {
984 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_ERR");
985 sctpParams->logLevel = MDCLOG_ERR;
986 } else {
aa7133@att.com409f2232020-10-29 13:06:45 +0300987 mdclog_write(MDCLOG_ERR, "illegal loglevel = %s. Set loglevel to MDCLOG_INFO", tmpStr.c_str());
ss412g3bac2da2020-01-05 11:52:19 +0200988 sctpParams->logLevel = MDCLOG_INFO;
989 }
990 mdclog_level_set(sctpParams->logLevel);
ss412g3bac2da2020-01-05 11:52:19 +0200991 tmpStr = conf.getStringValue("trace");
992 if (tmpStr.length() == 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300993 mdclog_write(MDCLOG_ERR, "illegal trace. Set trace to stop");
ss412g3bac2da2020-01-05 11:52:19 +0200994 tmpStr = "stop";
995 }
996
997 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
998 if ((tmpStr.compare("start")) == 0) {
999 mdclog_write(MDCLOG_INFO, "Trace set to: start");
1000 sctpParams->trace = true;
1001 } else if ((tmpStr.compare("stop")) == 0) {
1002 mdclog_write(MDCLOG_INFO, "Trace set to: stop");
1003 sctpParams->trace = false;
1004 } else {
1005 mdclog_write(MDCLOG_ERR, "Trace was set to wrong value %s, set to stop", tmpStr.c_str());
1006 sctpParams->trace = false;
1007 }
1008 jsonTrace = sctpParams->trace;
ss412g4aeeda22020-06-16 14:12:41 +03001009
ss412g4aeeda22020-06-16 14:12:41 +03001010
ss412g3bac2da2020-01-05 11:52:19 +02001011 endlessLoop = false;
1012 }
dhirajverma60f8bef2021-06-29 07:22:58 -04001013#ifdef UNIT_TEST
1014 break;
1015#endif
ss412g3bac2da2020-01-05 11:52:19 +02001016 }
1017 }
1018}
1019
1020/**
1021 *
1022 * @param event
1023 * @param message
1024 * @param rmrMessageBuffer
1025 * @param params
ss412g3bac2da2020-01-05 11:52:19 +02001026 */
1027void handleEinprogressMessages(struct epoll_event &event,
1028 ReportingMessages_t &message,
1029 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +02001030 sctp_params_t *params) {
ss412g3bac2da2020-01-05 11:52:19 +02001031 auto *peerInfo = (ConnectedCU_t *)event.data.ptr;
1032 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
1033
1034 mdclog_write(MDCLOG_INFO, "file descriptor %d got EPOLLOUT", peerInfo->fileDescriptor);
1035 auto retVal = 0;
1036 socklen_t retValLen = 0;
1037 auto rc = getsockopt(peerInfo->fileDescriptor, SOL_SOCKET, SO_ERROR, &retVal, &retValLen);
1038 if (rc != 0 || retVal != 0) {
dhirajverma31768a92021-05-06 12:38:49 +05301039#ifndef UNIT_TEST
ss412g3bac2da2020-01-05 11:52:19 +02001040 if (rc != 0) {
1041 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
1042 "%s|Failed SCTP Connection, after EINPROGRESS the getsockopt%s",
1043 peerInfo->enodbName, strerror(errno));
1044 } else if (retVal != 0) {
1045 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
1046 "%s|Failed SCTP Connection after EINPROGRESS, SO_ERROR",
1047 peerInfo->enodbName);
1048 }
1049
1050 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1051 message.message.asnLength = rmrMessageBuffer.sendMessage->len;
1052 mdclog_write(MDCLOG_ERR, "%s", rmrMessageBuffer.sendMessage->payload);
1053 message.message.direction = 'N';
aa7133@att.combe92d712020-03-18 17:30:39 +02001054 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +02001055 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
1056 }
dhirajverma31768a92021-05-06 12:38:49 +05301057#endif
ss412g3bac2da2020-01-05 11:52:19 +02001058 memset(peerInfo->asnData, 0, peerInfo->asnLength);
1059 peerInfo->asnLength = 0;
1060 peerInfo->mtype = 0;
ss412g3bac2da2020-01-05 11:52:19 +02001061 return;
1062 }
1063
1064 peerInfo->isConnected = true;
1065
1066 if (modifyToEpoll(params->epoll_fd, peerInfo, (EPOLLIN | EPOLLET), params->sctpMap, peerInfo->enodbName,
aa7133@att.combe92d712020-03-18 17:30:39 +02001067 peerInfo->mtype) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +02001068 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_MOD");
ss412g3bac2da2020-01-05 11:52:19 +02001069 return;
1070 }
1071
1072 message.message.asndata = (unsigned char *)peerInfo->asnData;
1073 message.message.asnLength = peerInfo->asnLength;
1074 message.message.messageType = peerInfo->mtype;
1075 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
1076 num_of_messages.fetch_add(1, std::memory_order_release);
1077 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1078 mdclog_write(MDCLOG_DEBUG, "send the delayed SETUP/ENDC SETUP to sctp for %s",
1079 message.message.enodbName);
1080 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001081 if (sendSctpMsg(peerInfo, message, params->sctpMap) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +02001082 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1083 mdclog_write(MDCLOG_DEBUG, "Error write to SCTP %s %d", __func__, __LINE__);
1084 }
ss412g3bac2da2020-01-05 11:52:19 +02001085 return;
1086 }
1087
1088 memset(peerInfo->asnData, 0, peerInfo->asnLength);
1089 peerInfo->asnLength = 0;
1090 peerInfo->mtype = 0;
ss412g3bac2da2020-01-05 11:52:19 +02001091}
1092
1093
1094void handlepoll_error(struct epoll_event &event,
1095 ReportingMessages_t &message,
1096 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +02001097 sctp_params_t *params) {
ss412g3bac2da2020-01-05 11:52:19 +02001098 if (event.data.fd != params->rmrListenFd) {
1099 auto *peerInfo = (ConnectedCU_t *)event.data.ptr;
1100 mdclog_write(MDCLOG_ERR, "epoll error, events %0x on fd %d, RAN NAME : %s",
1101 event.events, peerInfo->fileDescriptor, peerInfo->enodbName);
dhirajverma31768a92021-05-06 12:38:49 +05301102#ifndef UNIT_TEST
ss412g3bac2da2020-01-05 11:52:19 +02001103
1104 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
1105 "%s|Failed SCTP Connection",
1106 peerInfo->enodbName);
1107 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1108 message.message.asnLength = rmrMessageBuffer.sendMessage->len;
1109
1110 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
1111 message.message.direction = 'N';
aa7133@att.combe92d712020-03-18 17:30:39 +02001112 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +02001113 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
1114 }
dhirajverma31768a92021-05-06 12:38:49 +05301115#endif
ss412g3bac2da2020-01-05 11:52:19 +02001116 close(peerInfo->fileDescriptor);
aa7133@att.com7b437f72020-03-30 13:25:54 +03001117 params->sctpMap->erase(peerInfo->enodbName);
aa7133@att.combe92d712020-03-18 17:30:39 +02001118 cleanHashEntry((ConnectedCU_t *) event.data.ptr, params->sctpMap);
ss412g3bac2da2020-01-05 11:52:19 +02001119 } else {
1120 mdclog_write(MDCLOG_ERR, "epoll error, events %0x on RMR FD", event.events);
1121 }
ss412g3bac2da2020-01-05 11:52:19 +02001122}
1123/**
1124 *
ss412g1a79bdf2019-10-24 12:03:05 +03001125 * @param socket
1126 * @return
1127 */
1128int setSocketNoBlocking(int socket) {
1129 auto flags = fcntl(socket, F_GETFL, 0);
1130
1131 if (flags == -1) {
ss412g1a79bdf2019-10-24 12:03:05 +03001132 mdclog_write(MDCLOG_ERR, "%s, %s", __FUNCTION__, strerror(errno));
ss412g1a79bdf2019-10-24 12:03:05 +03001133 return -1;
1134 }
1135
1136 flags = (unsigned) flags | (unsigned) O_NONBLOCK;
1137 if (fcntl(socket, F_SETFL, flags) == -1) {
ss412g1a79bdf2019-10-24 12:03:05 +03001138 mdclog_write(MDCLOG_ERR, "%s, %s", __FUNCTION__, strerror(errno));
ss412g1a79bdf2019-10-24 12:03:05 +03001139 return -1;
1140 }
1141
1142 return 0;
1143}
1144
1145/**
1146 *
1147 * @param val
1148 * @param m
ss412g1a79bdf2019-10-24 12:03:05 +03001149 */
aa7133@att.combe92d712020-03-18 17:30:39 +02001150void cleanHashEntry(ConnectedCU_t *val, Sctp_Map_t *m) {
ss412g1a79bdf2019-10-24 12:03:05 +03001151 char *dummy;
1152 auto port = (uint16_t) strtol(val->portNumber, &dummy, 10);
aa7133@att.com84bd3342020-03-16 18:04:57 +02001153 char searchBuff[2048]{};
ss412g1a79bdf2019-10-24 12:03:05 +03001154
1155 snprintf(searchBuff, sizeof searchBuff, "host:%s:%d", val->hostName, port);
1156 m->erase(searchBuff);
1157
1158 m->erase(val->enodbName);
dhirajverma31768a92021-05-06 12:38:49 +05301159#ifndef UNIT_TEST
ss412g1a79bdf2019-10-24 12:03:05 +03001160 free(val);
dhirajverma31768a92021-05-06 12:38:49 +05301161#endif
ss412g1a79bdf2019-10-24 12:03:05 +03001162}
1163
1164/**
1165 *
aa7133@att.com409f2232020-10-29 13:06:45 +03001166 * @param fd file descriptor
ss412g1a79bdf2019-10-24 12:03:05 +03001167 * @param data the asn data to send
1168 * @param len length of the data
1169 * @param enodbName the enodbName as in the map for printing purpose
1170 * @param m map host information
1171 * @param mtype message number
aa7133@att.com409f2232020-10-29 13:06:45 +03001172 * @return 0 success, a negative number on fail
ss412g1a79bdf2019-10-24 12:03:05 +03001173 */
aa7133@att.combe92d712020-03-18 17:30:39 +02001174int sendSctpMsg(ConnectedCU_t *peerInfo, ReportingMessages_t &message, Sctp_Map_t *m) {
ss412g1a79bdf2019-10-24 12:03:05 +03001175 auto loglevel = mdclog_level_get();
dhirajverma99990292021-07-05 10:03:59 -04001176#ifndef UNIT_TEST
ss412g1a79bdf2019-10-24 12:03:05 +03001177 int fd = peerInfo->fileDescriptor;
dhirajverma99990292021-07-05 10:03:59 -04001178#else
1179 int fd = FILE_DESCRIPTOR;
1180#endif
ss412g1a79bdf2019-10-24 12:03:05 +03001181 if (loglevel >= MDCLOG_DEBUG) {
1182 mdclog_write(MDCLOG_DEBUG, "Send SCTP message for CU %s, %s",
1183 message.message.enodbName, __FUNCTION__);
1184 }
1185
1186 while (true) {
ss412g1a79bdf2019-10-24 12:03:05 +03001187 if (send(fd,message.message.asndata, message.message.asnLength,MSG_NOSIGNAL) < 0) {
1188 if (errno == EINTR) {
1189 continue;
1190 }
1191 mdclog_write(MDCLOG_ERR, "error writing to CU a message, %s ", strerror(errno));
dhirajverma99990292021-07-05 10:03:59 -04001192#ifndef UNIT_TEST
aa7133@att.com84bd3342020-03-16 18:04:57 +02001193 if (!peerInfo->isConnected) {
1194 mdclog_write(MDCLOG_ERR, "connection to CU %s is still in progress.", message.message.enodbName);
aa7133@att.com84bd3342020-03-16 18:04:57 +02001195 return -1;
1196 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001197 cleanHashEntry(peerInfo, m);
ss412g1a79bdf2019-10-24 12:03:05 +03001198 close(fd);
dhirajverma99990292021-07-05 10:03:59 -04001199#endif
ss412g1a79bdf2019-10-24 12:03:05 +03001200 char key[MAX_ENODB_NAME_SIZE * 2];
1201 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", message.message.enodbName,
1202 message.message.messageType);
1203 if (loglevel >= MDCLOG_DEBUG) {
1204 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
1205 }
aa7133@att.com96c9b882020-03-25 14:50:40 +02001206 auto tmp = m->find(key);
ss412g1a79bdf2019-10-24 12:03:05 +03001207 if (tmp) {
1208 free(tmp);
1209 }
1210 m->erase(key);
dhirajverma6cf28612021-06-23 04:53:34 -04001211#ifndef UNIT_TEST
ss412g1a79bdf2019-10-24 12:03:05 +03001212 return -1;
dhirajverma6cf28612021-06-23 04:53:34 -04001213#endif
ss412g1a79bdf2019-10-24 12:03:05 +03001214 }
1215 message.message.direction = 'D';
1216 // send report.buffer of size
1217 buildJsonMessage(message);
1218
1219 if (loglevel >= MDCLOG_DEBUG) {
1220 mdclog_write(MDCLOG_DEBUG,
1221 "SCTP message for CU %s sent from %s",
1222 message.message.enodbName,
1223 __FUNCTION__);
1224 }
ss412g1a79bdf2019-10-24 12:03:05 +03001225 return 0;
1226 }
1227}
1228
1229/**
1230 *
1231 * @param message
1232 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001233 */
aa7133@att.combe92d712020-03-18 17:30:39 +02001234void getRequestMetaData(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03001235 message.message.asndata = rmrMessageBuffer.rcvMessage->payload;
1236 message.message.asnLength = rmrMessageBuffer.rcvMessage->len;
1237
1238 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1239 mdclog_write(MDCLOG_DEBUG, "Message from Xapp RAN name = %s message length = %ld",
1240 message.message.enodbName, (unsigned long) message.message.asnLength);
1241 }
ss412g1a79bdf2019-10-24 12:03:05 +03001242}
1243
1244
ss412g1a79bdf2019-10-24 12:03:05 +03001245
1246/**
1247 *
1248 * @param events
1249 * @param sctpMap
1250 * @param numOfMessages
1251 * @param rmrMessageBuffer
1252 * @param ts
ss412g1a79bdf2019-10-24 12:03:05 +03001253 * @return
1254 */
1255int receiveDataFromSctp(struct epoll_event *events,
1256 Sctp_Map_t *sctpMap,
1257 int &numOfMessages,
1258 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +02001259 struct timespec &ts) {
ss412g1a79bdf2019-10-24 12:03:05 +03001260 /* We have data on the fd waiting to be read. Read and display it.
1261 * We must read whatever data is available completely, as we are running
1262 * in edge-triggered mode and won't get a notification again for the same data. */
aa7133@att.com84bd3342020-03-16 18:04:57 +02001263 ReportingMessages_t message {};
1264 auto done = 0;
ss412g1a79bdf2019-10-24 12:03:05 +03001265 auto loglevel = mdclog_level_get();
aa7133@att.com84bd3342020-03-16 18:04:57 +02001266
ss412g1a79bdf2019-10-24 12:03:05 +03001267 // get the identity of the interface
aa7133@att.com84bd3342020-03-16 18:04:57 +02001268 message.peerInfo = (ConnectedCU_t *)events->data.ptr;
1269
ss412g1a79bdf2019-10-24 12:03:05 +03001270 struct timespec start{0, 0};
aa7133@att.com409f2232020-10-29 13:06:45 +03001271 struct timespec decodeStart{0, 0};
ss412g1a79bdf2019-10-24 12:03:05 +03001272 struct timespec end{0, 0};
1273
1274 E2AP_PDU_t *pdu = nullptr;
1275
ss412g1a79bdf2019-10-24 12:03:05 +03001276 while (true) {
1277 if (loglevel >= MDCLOG_DEBUG) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001278 mdclog_write(MDCLOG_DEBUG, "Start Read from SCTP %d fd", message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001279 clock_gettime(CLOCK_MONOTONIC, &start);
1280 }
1281 // read the buffer directly to rmr payload
1282 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
dhirajverma99990292021-07-05 10:03:59 -04001283#ifndef UNIT_TEST
ss412g1a79bdf2019-10-24 12:03:05 +03001284 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
aa7133@att.com84bd3342020-03-16 18:04:57 +02001285 read(message.peerInfo->fileDescriptor, rmrMessageBuffer.sendMessage->payload, RECEIVE_SCTP_BUFFER_SIZE);
dhirajverma99990292021-07-05 10:03:59 -04001286#else
1287 message.message.asnLength = rmrMessageBuffer.sendMessage->len;
1288#endif
aa7133@att.com84bd3342020-03-16 18:04:57 +02001289
ss412g1a79bdf2019-10-24 12:03:05 +03001290 if (loglevel >= MDCLOG_DEBUG) {
1291 mdclog_write(MDCLOG_DEBUG, "Finish Read from SCTP %d fd message length = %ld",
aa7133@att.com96c9b882020-03-25 14:50:40 +02001292 message.peerInfo->fileDescriptor, message.message.asnLength);
ss412g1a79bdf2019-10-24 12:03:05 +03001293 }
aa7133@att.com16c799b2020-03-24 18:01:51 +02001294
aa7133@att.com84bd3342020-03-16 18:04:57 +02001295 memcpy(message.message.enodbName, message.peerInfo->enodbName, sizeof(message.peerInfo->enodbName));
ss412g1a79bdf2019-10-24 12:03:05 +03001296 message.message.direction = 'U';
1297 message.message.time.tv_nsec = ts.tv_nsec;
1298 message.message.time.tv_sec = ts.tv_sec;
1299
1300 if (message.message.asnLength < 0) {
1301 if (errno == EINTR) {
1302 continue;
1303 }
1304 /* If errno == EAGAIN, that means we have read all
aa7133@att.com84bd3342020-03-16 18:04:57 +02001305 data. So goReportingMessages_t back to the main loop. */
ss412g1a79bdf2019-10-24 12:03:05 +03001306 if (errno != EAGAIN) {
1307 mdclog_write(MDCLOG_ERR, "Read error, %s ", strerror(errno));
1308 done = 1;
1309 } else if (loglevel >= MDCLOG_DEBUG) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001310 mdclog_write(MDCLOG_DEBUG, "EAGAIN - descriptor = %d", message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001311 }
1312 break;
1313 } else if (message.message.asnLength == 0) {
1314 /* End of file. The remote has closed the connection. */
1315 if (loglevel >= MDCLOG_INFO) {
1316 mdclog_write(MDCLOG_INFO, "END of File Closed connection - descriptor = %d",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001317 message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001318 }
1319 done = 1;
1320 break;
1321 }
1322
ss412g1a79bdf2019-10-24 12:03:05 +03001323 if (loglevel >= MDCLOG_DEBUG) {
swc58d4f5d2020-10-20 15:08:38 +09001324 char printBuffer[RECEIVE_SCTP_BUFFER_SIZE]{};
ss412g1a79bdf2019-10-24 12:03:05 +03001325 char *tmp = printBuffer;
1326 for (size_t i = 0; i < (size_t)message.message.asnLength; ++i) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001327 snprintf(tmp, 3, "%02x", message.message.asndata[i]);
ss412g1a79bdf2019-10-24 12:03:05 +03001328 tmp += 2;
1329 }
1330 printBuffer[message.message.asnLength] = 0;
1331 clock_gettime(CLOCK_MONOTONIC, &end);
1332 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 +02001333 message.peerInfo->enodbName, end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec);
ss412g1a79bdf2019-10-24 12:03:05 +03001334 mdclog_write(MDCLOG_DEBUG, "PDU buffer length = %ld, data = : %s", message.message.asnLength,
1335 printBuffer);
aa7133@att.com409f2232020-10-29 13:06:45 +03001336 clock_gettime(CLOCK_MONOTONIC, &decodeStart);
ss412g1a79bdf2019-10-24 12:03:05 +03001337 }
dhirajverma99990292021-07-05 10:03:59 -04001338#ifndef UNIT_TEST
aa7133@att.com7b437f72020-03-30 13:25:54 +03001339 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, (void **) &pdu,
dhirajverma99990292021-07-05 10:03:59 -04001340 message.message.asndata, message.message.asnLength);
1341#else
1342 asn_dec_rval_t rval = {RC_OK, 0};
1343 pdu = (E2AP_PDU_t*)rmrMessageBuffer.sendMessage->tp_buf;
1344#endif
ss412g1a79bdf2019-10-24 12:03:05 +03001345 if (rval.code != RC_OK) {
1346 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2AP PDU from RAN : %s", rval.code,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001347 message.peerInfo->enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001348 break;
1349 }
1350
1351 if (loglevel >= MDCLOG_DEBUG) {
1352 clock_gettime(CLOCK_MONOTONIC, &end);
1353 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 +03001354 message.peerInfo->enodbName, end.tv_sec - decodeStart.tv_sec, end.tv_nsec - decodeStart.tv_nsec);
ss412g1a79bdf2019-10-24 12:03:05 +03001355 char *printBuffer;
1356 size_t size;
1357 FILE *stream = open_memstream(&printBuffer, &size);
1358 asn_fprint(stream, &asn_DEF_E2AP_PDU, pdu);
1359 mdclog_write(MDCLOG_DEBUG, "Encoding E2AP PDU past : %s", printBuffer);
aa7133@att.com409f2232020-10-29 13:06:45 +03001360 clock_gettime(CLOCK_MONOTONIC, &decodeStart);
Byonggon Chuncae9bd52020-11-18 13:37:29 +09001361
1362 fclose(stream);
1363 free(printBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001364 }
1365
1366 switch (pdu->present) {
1367 case E2AP_PDU_PR_initiatingMessage: {//initiating message
aa7133@att.com7b437f72020-03-30 13:25:54 +03001368 asnInitiatingRequest(pdu, sctpMap,message, rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001369 break;
1370 }
1371 case E2AP_PDU_PR_successfulOutcome: { //successful outcome
aa7133@att.com409f2232020-10-29 13:06:45 +03001372 asnSuccessfulMsg(pdu, sctpMap, message, rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001373 break;
1374 }
1375 case E2AP_PDU_PR_unsuccessfulOutcome: { //Unsuccessful Outcome
aa7133@att.com7b437f72020-03-30 13:25:54 +03001376 asnUnSuccsesfulMsg(pdu, sctpMap, message, rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001377 break;
1378 }
1379 default:
1380 mdclog_write(MDCLOG_ERR, "Unknown index %d in E2AP PDU", pdu->present);
1381 break;
1382 }
1383 if (loglevel >= MDCLOG_DEBUG) {
1384 clock_gettime(CLOCK_MONOTONIC, &end);
1385 mdclog_write(MDCLOG_DEBUG,
1386 "After processing message and sent to rmr for : %s, Read time is : %ld seconds, %ld nanoseconds",
aa7133@att.com409f2232020-10-29 13:06:45 +03001387 message.peerInfo->enodbName, end.tv_sec - decodeStart.tv_sec, end.tv_nsec - decodeStart.tv_nsec);
ss412g1a79bdf2019-10-24 12:03:05 +03001388 }
1389 numOfMessages++;
ss412g1a79bdf2019-10-24 12:03:05 +03001390 if (pdu != nullptr) {
aa7133@att.com1fecb052020-03-29 09:44:09 +03001391 ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, pdu);
1392 //ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
aa7133@att.com9dff4f02020-03-29 09:55:22 +03001393 //pdu = nullptr;
ss412g1a79bdf2019-10-24 12:03:05 +03001394 }
dhirajverma99990292021-07-05 10:03:59 -04001395#ifdef UNIT_TEST
1396 done = 1;
1397 break;
1398#endif
ss412g1a79bdf2019-10-24 12:03:05 +03001399 }
ss412g1a79bdf2019-10-24 12:03:05 +03001400
1401 if (done) {
1402 if (loglevel >= MDCLOG_INFO) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001403 mdclog_write(MDCLOG_INFO, "Closed connection - descriptor = %d", message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001404 }
1405 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
1406 snprintf((char *)rmrMessageBuffer.sendMessage->payload,
aa7133@att.com96c9b882020-03-25 14:50:40 +02001407 256,
1408 "%s|CU disconnected unexpectedly",
1409 message.peerInfo->enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001410 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
dhirajverma99990292021-07-05 10:03:59 -04001411#ifndef UNIT_TEST
ss412g1a79bdf2019-10-24 12:03:05 +03001412 if (sendRequestToXapp(message,
1413 RIC_SCTP_CONNECTION_FAILURE,
aa7133@att.combe92d712020-03-18 17:30:39 +02001414 rmrMessageBuffer) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03001415 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
1416 }
dhirajverma99990292021-07-05 10:03:59 -04001417#endif
ss412g1a79bdf2019-10-24 12:03:05 +03001418
1419 /* Closing descriptor make epoll remove it from the set of descriptors which are monitored. */
aa7133@att.com84bd3342020-03-16 18:04:57 +02001420 close(message.peerInfo->fileDescriptor);
aa7133@att.combe92d712020-03-18 17:30:39 +02001421 cleanHashEntry((ConnectedCU_t *) events->data.ptr, sctpMap);
ss412g1a79bdf2019-10-24 12:03:05 +03001422 }
1423 if (loglevel >= MDCLOG_DEBUG) {
1424 clock_gettime(CLOCK_MONOTONIC, &end);
1425 mdclog_write(MDCLOG_DEBUG, "from receive SCTP to send RMR time is %ld seconds and %ld nanoseconds",
1426 end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec);
1427
1428 }
ss412g1a79bdf2019-10-24 12:03:05 +03001429 return 0;
1430}
1431
aa7133@att.com409f2232020-10-29 13:06:45 +03001432static void buildAndSendSetupRequest(ReportingMessages_t &message,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001433 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001434 E2AP_PDU_t *pdu/*,
ss412g4aeeda22020-06-16 14:12:41 +03001435 string const &messageName,
1436 string const &ieName,
1437 vector<string> &functionsToAdd_v,
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001438 vector<string> &functionsToModified_v*/) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001439 auto logLevel = mdclog_level_get();
aa7133@att.com84bd3342020-03-16 18:04:57 +02001440 // now we can send the data to e2Mgr
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001441
ss412g4aeeda22020-06-16 14:12:41 +03001442 asn_enc_rval_t er;
1443 auto buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
Byonggon Chun51885f32020-11-18 15:31:04 +09001444 unsigned char *buffer = nullptr;
1445 buffer = (unsigned char *) calloc(buffer_size, sizeof(unsigned char));
1446 if(!buffer)
1447 {
1448 mdclog_write(MDCLOG_ERR, "Allocating buffer for %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1449 return;
1450 }
ss412g4aeeda22020-06-16 14:12:41 +03001451 while (true) {
ss412g4aeeda22020-06-16 14:12:41 +03001452 er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu, buffer, buffer_size);
1453 if (er.encoded == -1) {
1454 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1455 return;
1456 } else if (er.encoded > (ssize_t) buffer_size) {
1457 buffer_size = er.encoded + 128;
1458 mdclog_write(MDCLOG_WARN, "Buffer of size %d is to small for %s. Reallocate buffer of size %d",
1459 (int) buffer_size,
1460 asn_DEF_E2AP_PDU.name, buffer_size);
1461 buffer_size = er.encoded + 128;
Byonggon Chun51885f32020-11-18 15:31:04 +09001462
1463 unsigned char *newBuffer = nullptr;
1464 newBuffer = (unsigned char *) realloc(buffer, buffer_size);
1465 if (!newBuffer)
1466 {
1467 // out of memory
1468 mdclog_write(MDCLOG_ERR, "Reallocating buffer for %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1469 free(buffer);
1470 return;
1471 }
1472 buffer = newBuffer;
ss412g4aeeda22020-06-16 14:12:41 +03001473 continue;
1474 }
1475 buffer[er.encoded] = '\0';
1476 break;
1477 }
1478 // encode to xml
1479
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001480 string res((char *)buffer);
1481 res.erase(std::remove(res.begin(), res.end(), '\n'), res.end());
1482 res.erase(std::remove(res.begin(), res.end(), '\t'), res.end());
1483 res.erase(std::remove(res.begin(), res.end(), ' '), res.end());
1484
1485// string res {};
1486// if (!functionsToAdd_v.empty() || !functionsToModified_v.empty()) {
1487// res = buildXmlData(messageName, ieName, functionsToAdd_v, functionsToModified_v, buffer, (size_t) er.encoded);
1488// }
ss412g4aeeda22020-06-16 14:12:41 +03001489 rmr_mbuf_t *rmrMsg;
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001490// if (res.length() == 0) {
1491// rmrMsg = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, buffer_size + 256);
1492// rmrMsg->len = snprintf((char *) rmrMsg->payload, RECEIVE_SCTP_BUFFER_SIZE * 2, "%s:%d|%s",
1493// message.peerInfo->sctpParams->myIP.c_str(),
1494// message.peerInfo->sctpParams->rmrPort,
1495// buffer);
1496// } else {
ss412g4aeeda22020-06-16 14:12:41 +03001497 rmrMsg = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, (int)res.length() + 256);
1498 rmrMsg->len = snprintf((char *) rmrMsg->payload, res.length() + 256, "%s:%d|%s",
1499 message.peerInfo->sctpParams->myIP.c_str(),
1500 message.peerInfo->sctpParams->rmrPort,
1501 res.c_str());
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001502// }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001503
ss412g4aeeda22020-06-16 14:12:41 +03001504 if (logLevel >= MDCLOG_DEBUG) {
1505 mdclog_write(MDCLOG_DEBUG, "Setup request of size %d :\n %s\n", rmrMsg->len, rmrMsg->payload);
1506 }
1507 // send to RMR
1508 rmrMsg->mtype = message.message.messageType;
1509 rmrMsg->state = 0;
1510 rmr_bytes2meid(rmrMsg, (unsigned char *) message.message.enodbName, strlen(message.message.enodbName));
aa7133@att.com84bd3342020-03-16 18:04:57 +02001511
ss412g4aeeda22020-06-16 14:12:41 +03001512 static unsigned char tx[32];
1513 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1514 rmr_bytes2xact(rmrMsg, tx, strlen((const char *) tx));
dhirajverma60f8bef2021-06-29 07:22:58 -04001515#ifndef UNIT_TEST
ss412g4aeeda22020-06-16 14:12:41 +03001516 rmrMsg = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMsg);
dhirajverma60f8bef2021-06-29 07:22:58 -04001517#endif
ss412g4aeeda22020-06-16 14:12:41 +03001518 if (rmrMsg == nullptr) {
1519 mdclog_write(MDCLOG_ERR, "RMR failed to send returned nullptr");
1520 } else if (rmrMsg->state != 0) {
1521 char meid[RMR_MAX_MEID]{};
1522 if (rmrMsg->state == RMR_ERR_RETRY) {
1523 usleep(5);
1524 rmrMsg->state = 0;
1525 mdclog_write(MDCLOG_INFO, "RETRY sending Message %d to Xapp from %s",
1526 rmrMsg->mtype, rmr_get_meid(rmrMsg, (unsigned char *) meid));
dhirajverma60f8bef2021-06-29 07:22:58 -04001527#ifndef UNIT_TEST
ss412g4aeeda22020-06-16 14:12:41 +03001528 rmrMsg = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMsg);
dhirajverma60f8bef2021-06-29 07:22:58 -04001529#endif
ss412g4aeeda22020-06-16 14:12:41 +03001530 if (rmrMsg == nullptr) {
1531 mdclog_write(MDCLOG_ERR, "RMR failed send returned nullptr");
1532 } else if (rmrMsg->state != 0) {
1533 mdclog_write(MDCLOG_ERR,
1534 "RMR Retry failed %s sending request %d to Xapp from %s",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001535 translateRmrErrorMessages(rmrMsg->state).c_str(),
1536 rmrMsg->mtype,
1537 rmr_get_meid(rmrMsg, (unsigned char *) meid));
1538 }
ss412g4aeeda22020-06-16 14:12:41 +03001539 } else {
1540 mdclog_write(MDCLOG_ERR, "RMR failed: %s. sending request %d to Xapp from %s",
1541 translateRmrErrorMessages(rmrMsg->state).c_str(),
1542 rmrMsg->mtype,
1543 rmr_get_meid(rmrMsg, (unsigned char *) meid));
aa7133@att.combc110772020-03-25 09:45:14 +02001544 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001545 }
ss412g4aeeda22020-06-16 14:12:41 +03001546 message.peerInfo->gotSetup = true;
1547 buildJsonMessage(message);
Byonggon Chun51885f32020-11-18 15:31:04 +09001548
ss412g4aeeda22020-06-16 14:12:41 +03001549 if (rmrMsg != nullptr) {
1550 rmr_free_msg(rmrMsg);
1551 }
Byonggon Chun51885f32020-11-18 15:31:04 +09001552 free(buffer);
1553
1554 return;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001555}
aa7133@att.comc5a15202020-04-05 19:57:01 +03001556
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001557#if 0
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001558int RAN_Function_list_To_Vector(RANfunctions_List_t& list, vector <string> &runFunXML_v) {
1559 auto index = 0;
1560 runFunXML_v.clear();
1561 for (auto j = 0; j < list.list.count; j++) {
1562 auto *raNfunctionItemIEs = (RANfunction_ItemIEs_t *)list.list.array[j];
1563 if (raNfunctionItemIEs->id == ProtocolIE_ID_id_RANfunction_Item &&
1564 (raNfunctionItemIEs->value.present == RANfunction_ItemIEs__value_PR_RANfunction_Item)) {
1565 // encode to xml
1566 E2SM_gNB_NRT_RANfunction_Definition_t *ranFunDef = nullptr;
1567 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER,
1568 &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition,
1569 (void **)&ranFunDef,
1570 raNfunctionItemIEs->value.choice.RANfunction_Item.ranFunctionDefinition.buf,
1571 raNfunctionItemIEs->value.choice.RANfunction_Item.ranFunctionDefinition.size);
1572 if (rval.code != RC_OK) {
1573 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2SM message from : %s",
1574 rval.code,
1575 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name);
1576 return -1;
1577 }
1578
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001579 auto xml_buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
1580 unsigned char xml_buffer[RECEIVE_SCTP_BUFFER_SIZE * 2];
ss412g4aeeda22020-06-16 14:12:41 +03001581 memset(xml_buffer, 0, RECEIVE_SCTP_BUFFER_SIZE * 2);
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001582 // encode to xml
1583 auto er = asn_encode_to_buffer(nullptr,
1584 ATS_BASIC_XER,
1585 &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition,
1586 ranFunDef,
1587 xml_buffer,
1588 xml_buffer_size);
1589 if (er.encoded == -1) {
1590 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s",
1591 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name,
1592 strerror(errno));
1593 } else if (er.encoded > (ssize_t)xml_buffer_size) {
1594 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
1595 (int) xml_buffer_size,
1596 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name, __func__, __LINE__);
1597 } else {
1598 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1599 mdclog_write(MDCLOG_DEBUG, "Encoding E2SM %s PDU number %d : %s",
1600 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name,
1601 index++,
1602 xml_buffer);
1603 }
ss412g4aeeda22020-06-16 14:12:41 +03001604
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001605 string runFuncs = (char *)(xml_buffer);
1606 runFunXML_v.emplace_back(runFuncs);
1607 }
1608 }
1609 }
1610 return 0;
1611}
aa7133@att.comc5a15202020-04-05 19:57:01 +03001612
ss412g4aeeda22020-06-16 14:12:41 +03001613int collectServiceUpdate_RequestData(E2AP_PDU_t *pdu,
1614 Sctp_Map_t *sctpMap,
1615 ReportingMessages_t &message,
1616 vector <string> &RANfunctionsAdded_v,
1617 vector <string> &RANfunctionsModified_v) {
aa7133@att.comc5a15202020-04-05 19:57:01 +03001618 memset(message.peerInfo->enodbName, 0 , MAX_ENODB_NAME_SIZE);
ss412g4aeeda22020-06-16 14:12:41 +03001619 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICserviceUpdate.protocolIEs.list.count; i++) {
1620 auto *ie = pdu->choice.initiatingMessage->value.choice.RICserviceUpdate.protocolIEs.list.array[i];
1621 if (ie->id == ProtocolIE_ID_id_RANfunctionsAdded) {
1622 if (ie->value.present == RICserviceUpdate_IEs__value_PR_RANfunctionsID_List) {
aa7133@att.comc5a15202020-04-05 19:57:01 +03001623 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001624 mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1625 ie->value.choice.RANfunctions_List.list.count);
aa7133@att.comc5a15202020-04-05 19:57:01 +03001626 }
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001627 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsAdded_v) != 0 ) {
1628 return -1;
1629 }
1630 }
1631 } else if (ie->id == ProtocolIE_ID_id_RANfunctionsModified) {
ss412g4aeeda22020-06-16 14:12:41 +03001632 if (ie->value.present == RICserviceUpdate_IEs__value_PR_RANfunctions_List) {
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001633 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1634 mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1635 ie->value.choice.RANfunctions_List.list.count);
1636 }
1637 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsModified_v) != 0 ) {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001638 return -1;
aa7133@att.comc5a15202020-04-05 19:57:01 +03001639 }
1640 }
1641 }
1642 }
1643 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1644 mdclog_write(MDCLOG_DEBUG, "Run function vector have %ld entries",
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001645 RANfunctionsAdded_v.size());
aa7133@att.comc5a15202020-04-05 19:57:01 +03001646 }
1647 return 0;
1648}
ss412g4aeeda22020-06-16 14:12:41 +03001649
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001650#endif
ss412g4aeeda22020-06-16 14:12:41 +03001651
1652
aa7133@att.com409f2232020-10-29 13:06:45 +03001653void buildPrometheusList(ConnectedCU_t *peerInfo, Family<Counter> *prometheusFamily) {
aa7133@att.coma4c80012020-06-24 16:36:47 +03001654 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"SetupRequest", "Messages"}});
1655 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"SetupRequest", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001656
aa7133@att.coma4c80012020-06-24 16:36:47 +03001657 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ErrorIndication", "Messages"}});
1658 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ErrorIndication", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001659
aa7133@att.coma4c80012020-06-24 16:36:47 +03001660 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_RICindication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICindication", "Messages"}});
1661 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_RICindication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICindication", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001662
aa7133@att.coma4c80012020-06-24 16:36:47 +03001663 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetRequest", "Messages"}});
1664 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetRequest", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001665
aa7133@att.coma4c80012020-06-24 16:36:47 +03001666 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICserviceUpdate", "Messages"}});
1667 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICserviceUpdate", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001668 // ---------------------------------------------
aa7133@att.coma4c80012020-06-24 16:36:47 +03001669 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetACK", "Messages"}});
1670 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001671
aa7133@att.coma4c80012020-06-24 16:36:47 +03001672 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolACK", "Messages"}});
1673 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001674
aa7133@att.coma4c80012020-06-24 16:36:47 +03001675 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionACK", "Messages"}});
1676 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001677
aa7133@att.coma4c80012020-06-24 16:36:47 +03001678 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteACK", "Messages"}});
1679 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001680 //-------------------------------------------------------------
1681
aa7133@att.coma4c80012020-06-24 16:36:47 +03001682 peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolFailure", "Messages"}});
1683 peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001684
aa7133@att.coma4c80012020-06-24 16:36:47 +03001685 peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionFailure", "Messages"}});
1686 peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001687
aa7133@att.coma4c80012020-06-24 16:36:47 +03001688 peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteFailure", "Messages"}});
1689 peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001690
1691 //====================================================================================
aa7133@att.coma4c80012020-06-24 16:36:47 +03001692 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ErrorIndication", "Messages"}});
1693 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ErrorIndication", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001694
aa7133@att.coma4c80012020-06-24 16:36:47 +03001695 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetRequest", "Messages"}});
1696 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetRequest", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001697
aa7133@att.coma4c80012020-06-24 16:36:47 +03001698 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICcontrol", "Messages"}});
1699 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICcontrol", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001700
aa7133@att.coma4c80012020-06-24 16:36:47 +03001701 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICserviceQuery)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceQuery", "Messages"}});
1702 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICserviceQuery)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceQuery", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001703
aa7133@att.coma4c80012020-06-24 16:36:47 +03001704 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscription", "Messages"}});
1705 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscription", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001706
aa7133@att.coma4c80012020-06-24 16:36:47 +03001707 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscriptionDelete", "Messages"}});
1708 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscriptionDelete", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001709 //---------------------------------------------------------------------------------------------------------
aa7133@att.coma4c80012020-06-24 16:36:47 +03001710 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupResponse", "Messages"}});
1711 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupResponse", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001712
aa7133@att.coma4c80012020-06-24 16:36:47 +03001713 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetACK", "Messages"}});
1714 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001715
aa7133@att.coma4c80012020-06-24 16:36:47 +03001716 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateResponse", "Messages"}});
1717 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateResponse", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001718 //----------------------------------------------------------------------------------------------------------------
aa7133@att.coma4c80012020-06-24 16:36:47 +03001719 peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupRequestFailure", "Messages"}});
1720 peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupRequestFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001721
aa7133@att.coma4c80012020-06-24 16:36:47 +03001722 peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateFailure", "Messages"}});
1723 peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001724}
1725/**
1726 *
1727 * @param pdu
1728 * @param sctpMap
1729 * @param message
1730 * @param RANfunctionsAdded_v
1731 * @return
1732 */
1733int collectSetupRequestData(E2AP_PDU_t *pdu,
1734 Sctp_Map_t *sctpMap,
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001735 ReportingMessages_t &message /*, vector <string> &RANfunctionsAdded_v*/) {
ss412g4aeeda22020-06-16 14:12:41 +03001736 memset(message.peerInfo->enodbName, 0 , MAX_ENODB_NAME_SIZE);
1737 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.count; i++) {
1738 auto *ie = pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.array[i];
1739 if (ie->id == ProtocolIE_ID_id_GlobalE2node_ID) {
1740 // get the ran name for meid
1741 if (ie->value.present == E2setupRequestIEs__value_PR_GlobalE2node_ID) {
1742 if (buildRanName(message.peerInfo->enodbName, ie) < 0) {
1743 mdclog_write(MDCLOG_ERR, "Bad param in E2setupRequestIEs GlobalE2node_ID.\n");
aa7133@att.com409f2232020-10-29 13:06:45 +03001744 // no message will be sent
ss412g4aeeda22020-06-16 14:12:41 +03001745 return -1;
1746 }
1747
1748 memcpy(message.message.enodbName, message.peerInfo->enodbName, strlen(message.peerInfo->enodbName));
1749 sctpMap->setkey(message.message.enodbName, message.peerInfo);
1750 }
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001751 } /*else if (ie->id == ProtocolIE_ID_id_RANfunctionsAdded) {
ss412g4aeeda22020-06-16 14:12:41 +03001752 if (ie->value.present == E2setupRequestIEs__value_PR_RANfunctions_List) {
1753 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1754 mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1755 ie->value.choice.RANfunctions_List.list.count);
1756 }
1757 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsAdded_v) != 0 ) {
1758 return -1;
1759 }
1760 }
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001761 } */
ss412g4aeeda22020-06-16 14:12:41 +03001762 }
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001763// if (mdclog_level_get() >= MDCLOG_DEBUG) {
1764// mdclog_write(MDCLOG_DEBUG, "Run function vector have %ld entries",
1765// RANfunctionsAdded_v.size());
1766// }
ss412g4aeeda22020-06-16 14:12:41 +03001767 return 0;
1768}
1769
1770int XML_From_PER(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
1771 E2AP_PDU_t *pdu = nullptr;
1772
1773 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1774 mdclog_write(MDCLOG_DEBUG, "got PER message of size %d is:%s",
1775 rmrMessageBuffer.sendMessage->len, rmrMessageBuffer.sendMessage->payload);
1776 }
1777 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, (void **) &pdu,
1778 rmrMessageBuffer.sendMessage->payload, rmrMessageBuffer.sendMessage->len);
1779 if (rval.code != RC_OK) {
1780 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) setup response from E2MGR : %s",
1781 rval.code,
1782 message.message.enodbName);
1783 return -1;
1784 }
1785
1786 int buff_size = RECEIVE_XAPP_BUFFER_SIZE;
1787 auto er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu,
1788 rmrMessageBuffer.sendMessage->payload, buff_size);
1789 if (er.encoded == -1) {
1790 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1791 return -1;
1792 } else if (er.encoded > (ssize_t)buff_size) {
1793 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
1794 (int)rmrMessageBuffer.sendMessage->len,
1795 asn_DEF_E2AP_PDU.name,
1796 __func__,
1797 __LINE__);
1798 return -1;
1799 }
1800 rmrMessageBuffer.sendMessage->len = er.encoded;
1801 return 0;
1802
1803}
1804
ss412g1a79bdf2019-10-24 12:03:05 +03001805/**
1806 *
1807 * @param pdu
1808 * @param message
1809 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001810 */
1811void asnInitiatingRequest(E2AP_PDU_t *pdu,
aa7133@att.com7b437f72020-03-30 13:25:54 +03001812 Sctp_Map_t *sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +03001813 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +02001814 RmrMessagesBuffer_t &rmrMessageBuffer) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001815 auto logLevel = mdclog_level_get();
ss412g1a79bdf2019-10-24 12:03:05 +03001816 auto procedureCode = ((InitiatingMessage_t *) pdu->choice.initiatingMessage)->procedureCode;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001817 if (logLevel >= MDCLOG_DEBUG) {
1818 mdclog_write(MDCLOG_DEBUG, "Initiating message %ld\n", procedureCode);
ss412g1a79bdf2019-10-24 12:03:05 +03001819 }
1820 switch (procedureCode) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001821 case ProcedureCode_id_E2setup: {
1822 if (logLevel >= MDCLOG_DEBUG) {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001823 mdclog_write(MDCLOG_DEBUG, "Got E2setup");
aa7133@att.com84bd3342020-03-16 18:04:57 +02001824 }
1825
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001826// vector <string> RANfunctionsAdded_v;
1827// vector <string> RANfunctionsModified_v;
1828// RANfunctionsAdded_v.clear();
1829// RANfunctionsModified_v.clear();
1830 if (collectSetupRequestData(pdu, sctpMap, message) != 0) {
aa7133@att.com7b437f72020-03-30 13:25:54 +03001831 break;
1832 }
1833
aa7133@att.com409f2232020-10-29 13:06:45 +03001834 buildPrometheusList(message.peerInfo, message.peerInfo->sctpParams->prometheusFamily);
ss412g4aeeda22020-06-16 14:12:41 +03001835
1836 string messageName("E2setupRequest");
1837 string ieName("E2setupRequestIEs");
1838 message.message.messageType = RIC_E2_SETUP_REQ;
aa7133@att.coma4c80012020-06-24 16:36:47 +03001839 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
1840 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment((double)message.message.asnLength);
aa7133@att.com409f2232020-10-29 13:06:45 +03001841 buildAndSendSetupRequest(message, rmrMessageBuffer, pdu);
ss412g1a79bdf2019-10-24 12:03:05 +03001842 break;
1843 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001844 case ProcedureCode_id_RICserviceUpdate: {
1845 if (logLevel >= MDCLOG_DEBUG) {
1846 mdclog_write(MDCLOG_DEBUG, "Got RICserviceUpdate %s", message.message.enodbName);
1847 }
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001848// vector <string> RANfunctionsAdded_v;
1849// vector <string> RANfunctionsModified_v;
1850// RANfunctionsAdded_v.clear();
1851// RANfunctionsModified_v.clear();
1852// if (collectServiceUpdate_RequestData(pdu, sctpMap, message,
1853// RANfunctionsAdded_v, RANfunctionsModified_v) != 0) {
1854// break;
1855// }
ss412g4aeeda22020-06-16 14:12:41 +03001856
1857 string messageName("RICserviceUpdate");
1858 string ieName("RICserviceUpdateIEs");
1859 message.message.messageType = RIC_SERVICE_UPDATE;
dhirajverma6cf28612021-06-23 04:53:34 -04001860#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03001861 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
1862 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04001863#endif
aa7133@att.com409f2232020-10-29 13:06:45 +03001864 buildAndSendSetupRequest(message, rmrMessageBuffer, pdu);
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001865 break;
1866 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001867 case ProcedureCode_id_ErrorIndication: {
1868 if (logLevel >= MDCLOG_DEBUG) {
1869 mdclog_write(MDCLOG_DEBUG, "Got ErrorIndication %s", message.message.enodbName);
1870 }
dhirajverma6cf28612021-06-23 04:53:34 -04001871#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03001872 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_ErrorIndication]->Increment();
1873 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_ErrorIndication]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04001874#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02001875 if (sendRequestToXapp(message, RIC_ERROR_INDICATION, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001876 mdclog_write(MDCLOG_ERR, "RIC_ERROR_INDICATION failed to send to xAPP");
ss412g1a79bdf2019-10-24 12:03:05 +03001877 }
1878 break;
1879 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001880 case ProcedureCode_id_Reset: {
1881 if (logLevel >= MDCLOG_DEBUG) {
1882 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001883 }
dhirajverma6cf28612021-06-23 04:53:34 -04001884#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03001885 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
1886 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04001887#endif
ss412g4aeeda22020-06-16 14:12:41 +03001888 if (XML_From_PER(message, rmrMessageBuffer) < 0) {
1889 break;
ss412g1a79bdf2019-10-24 12:03:05 +03001890 }
ss412g4aeeda22020-06-16 14:12:41 +03001891
1892 if (sendRequestToXapp(message, RIC_E2_RESET_REQ, rmrMessageBuffer) != 0) {
1893 mdclog_write(MDCLOG_ERR, "RIC_E2_RESET_REQ message failed to send to xAPP");
aa7133@att.com84bd3342020-03-16 18:04:57 +02001894 }
1895 break;
1896 }
1897 case ProcedureCode_id_RICindication: {
1898 if (logLevel >= MDCLOG_DEBUG) {
1899 mdclog_write(MDCLOG_DEBUG, "Got RICindication %s", message.message.enodbName);
1900 }
1901 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.count; i++) {
ss412g1a79bdf2019-10-24 12:03:05 +03001902 auto messageSent = false;
1903 RICindication_IEs_t *ie = pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.array[i];
aa7133@att.com84bd3342020-03-16 18:04:57 +02001904 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03001905 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1906 }
1907 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001908 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03001909 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1910 }
1911 if (ie->value.present == RICindication_IEs__value_PR_RICrequestID) {
1912 static unsigned char tx[32];
1913 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_INDICATION;
1914 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1915 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
ss412g3bac2da2020-01-05 11:52:19 +02001916 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001917 (unsigned char *)message.message.enodbName,
1918 strlen(message.message.enodbName));
ss412g1a79bdf2019-10-24 12:03:05 +03001919 rmrMessageBuffer.sendMessage->state = 0;
aa7133@att.comc5a15202020-04-05 19:57:01 +03001920 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
1921
1922 //ie->value.choice.RICrequestID.ricInstanceID;
ss412g3bac2da2020-01-05 11:52:19 +02001923 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.comc5a15202020-04-05 19:57:01 +03001924 mdclog_write(MDCLOG_DEBUG, "sub id = %d, mtype = %d, ric instance id %ld, requestor id = %ld",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001925 rmrMessageBuffer.sendMessage->sub_id,
aa7133@att.comc5a15202020-04-05 19:57:01 +03001926 rmrMessageBuffer.sendMessage->mtype,
1927 ie->value.choice.RICrequestID.ricInstanceID,
1928 ie->value.choice.RICrequestID.ricRequestorID);
ss412g3bac2da2020-01-05 11:52:19 +02001929 }
dhirajverma6cf28612021-06-23 04:53:34 -04001930#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03001931 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICindication]->Increment();
1932 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICindication]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04001933#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02001934 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03001935 messageSent = true;
1936 } else {
aa7133@att.com409f2232020-10-29 13:06:45 +03001937 mdclog_write(MDCLOG_ERR, "RIC request id missing illegal request");
ss412g1a79bdf2019-10-24 12:03:05 +03001938 }
1939 }
1940 if (messageSent) {
1941 break;
1942 }
1943 }
1944 break;
1945 }
ss412g1a79bdf2019-10-24 12:03:05 +03001946 default: {
1947 mdclog_write(MDCLOG_ERR, "Undefined or not supported message = %ld", procedureCode);
1948 message.message.messageType = 0; // no RMR message type yet
1949
1950 buildJsonMessage(message);
1951
1952 break;
1953 }
1954 }
ss412g1a79bdf2019-10-24 12:03:05 +03001955}
1956
1957/**
1958 *
1959 * @param pdu
1960 * @param message
ss412g1a79bdf2019-10-24 12:03:05 +03001961 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001962 */
aa7133@att.com409f2232020-10-29 13:06:45 +03001963void asnSuccessfulMsg(E2AP_PDU_t *pdu,
aa7133@att.com7b437f72020-03-30 13:25:54 +03001964 Sctp_Map_t *sctpMap,
1965 ReportingMessages_t &message,
1966 RmrMessagesBuffer_t &rmrMessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03001967 auto procedureCode = pdu->choice.successfulOutcome->procedureCode;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001968 auto logLevel = mdclog_level_get();
1969 if (logLevel >= MDCLOG_INFO) {
ss412g1a79bdf2019-10-24 12:03:05 +03001970 mdclog_write(MDCLOG_INFO, "Successful Outcome %ld", procedureCode);
1971 }
1972 switch (procedureCode) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001973 case ProcedureCode_id_Reset: {
1974 if (logLevel >= MDCLOG_DEBUG) {
1975 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001976 }
dhirajverma6cf28612021-06-23 04:53:34 -04001977#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03001978 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
1979 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04001980#endif
ss412g4aeeda22020-06-16 14:12:41 +03001981 if (XML_From_PER(message, rmrMessageBuffer) < 0) {
1982 break;
1983 }
1984 if (sendRequestToXapp(message, RIC_E2_RESET_RESP, rmrMessageBuffer) != 0) {
1985 mdclog_write(MDCLOG_ERR, "RIC_E2_RESET_RESP message failed to send to xAPP");
ss412g1a79bdf2019-10-24 12:03:05 +03001986 }
1987 break;
1988 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001989 case ProcedureCode_id_RICcontrol: {
1990 if (logLevel >= MDCLOG_DEBUG) {
1991 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001992 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001993 for (auto i = 0;
ss412g1a79bdf2019-10-24 12:03:05 +03001994 i < pdu->choice.successfulOutcome->value.choice.RICcontrolAcknowledge.protocolIEs.list.count; i++) {
1995 auto messageSent = false;
1996 RICcontrolAcknowledge_IEs_t *ie = pdu->choice.successfulOutcome->value.choice.RICcontrolAcknowledge.protocolIEs.list.array[i];
1997 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1998 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1999 }
2000 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
2001 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2002 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
2003 }
2004 if (ie->value.present == RICcontrolAcknowledge_IEs__value_PR_RICrequestID) {
2005 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_CONTROL_ACK;
2006 rmrMessageBuffer.sendMessage->state = 0;
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002007// rmrMessageBuffer.sendMessage->sub_id = (int) ie->value.choice.RICrequestID.ricRequestorID;
2008 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
2009
ss412g1a79bdf2019-10-24 12:03:05 +03002010 static unsigned char tx[32];
2011 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2012 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
ss412g3bac2da2020-01-05 11:52:19 +02002013 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
aa7133@att.com84bd3342020-03-16 18:04:57 +02002014 (unsigned char *)message.message.enodbName,
2015 strlen(message.message.enodbName));
dhirajverma6cf28612021-06-23 04:53:34 -04002016#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03002017 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
2018 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04002019#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002020 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03002021 messageSent = true;
2022 } else {
aa7133@att.com409f2232020-10-29 13:06:45 +03002023 mdclog_write(MDCLOG_ERR, "RIC request id missing illegal request");
ss412g1a79bdf2019-10-24 12:03:05 +03002024 }
2025 }
2026 if (messageSent) {
2027 break;
2028 }
2029 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02002030
2031 break;
2032 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02002033 case ProcedureCode_id_RICsubscription: {
2034 if (logLevel >= MDCLOG_DEBUG) {
2035 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
2036 }
dhirajverma6cf28612021-06-23 04:53:34 -04002037#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03002038 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
2039 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04002040#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002041 if (sendRequestToXapp(message, RIC_SUB_RESP, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02002042 mdclog_write(MDCLOG_ERR, "Subscription successful message failed to send to xAPP");
2043 }
2044 break;
2045 }
2046 case ProcedureCode_id_RICsubscriptionDelete: {
2047 if (logLevel >= MDCLOG_DEBUG) {
2048 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
2049 }
dhirajverma6cf28612021-06-23 04:53:34 -04002050#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03002051 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
2052 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04002053#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002054 if (sendRequestToXapp(message, RIC_SUB_DEL_RESP, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02002055 mdclog_write(MDCLOG_ERR, "Subscription delete successful message failed to send to xAPP");
2056 }
ss412g1a79bdf2019-10-24 12:03:05 +03002057 break;
2058 }
2059 default: {
2060 mdclog_write(MDCLOG_WARN, "Undefined or not supported message = %ld", procedureCode);
2061 message.message.messageType = 0; // no RMR message type yet
2062 buildJsonMessage(message);
2063
2064 break;
2065 }
2066 }
ss412g1a79bdf2019-10-24 12:03:05 +03002067}
2068
2069/**
2070 *
2071 * @param pdu
2072 * @param message
ss412g1a79bdf2019-10-24 12:03:05 +03002073 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03002074 */
2075void asnUnSuccsesfulMsg(E2AP_PDU_t *pdu,
aa7133@att.com7b437f72020-03-30 13:25:54 +03002076 Sctp_Map_t *sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +03002077 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +02002078 RmrMessagesBuffer_t &rmrMessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03002079 auto procedureCode = pdu->choice.unsuccessfulOutcome->procedureCode;
aa7133@att.com84bd3342020-03-16 18:04:57 +02002080 auto logLevel = mdclog_level_get();
2081 if (logLevel >= MDCLOG_INFO) {
ss412g1a79bdf2019-10-24 12:03:05 +03002082 mdclog_write(MDCLOG_INFO, "Unsuccessful Outcome %ld", procedureCode);
2083 }
2084 switch (procedureCode) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02002085 case ProcedureCode_id_RICcontrol: {
2086 if (logLevel >= MDCLOG_DEBUG) {
2087 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03002088 }
2089 for (int i = 0;
2090 i < pdu->choice.unsuccessfulOutcome->value.choice.RICcontrolFailure.protocolIEs.list.count; i++) {
2091 auto messageSent = false;
2092 RICcontrolFailure_IEs_t *ie = pdu->choice.unsuccessfulOutcome->value.choice.RICcontrolFailure.protocolIEs.list.array[i];
aa7133@att.com84bd3342020-03-16 18:04:57 +02002093 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03002094 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
2095 }
2096 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02002097 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03002098 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
2099 }
2100 if (ie->value.present == RICcontrolFailure_IEs__value_PR_RICrequestID) {
2101 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_CONTROL_FAILURE;
2102 rmrMessageBuffer.sendMessage->state = 0;
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002103// rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricRequestorID;
2104 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
ss412g1a79bdf2019-10-24 12:03:05 +03002105 static unsigned char tx[32];
2106 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2107 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
aa7133@att.com84bd3342020-03-16 18:04:57 +02002108 rmr_bytes2meid(rmrMessageBuffer.sendMessage, (unsigned char *) message.message.enodbName,
2109 strlen(message.message.enodbName));
dhirajverma6cf28612021-06-23 04:53:34 -04002110#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03002111 message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
2112 message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment((double)message.message.asnLength);
dhirajverma99990292021-07-05 10:03:59 -04002113#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002114 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03002115 messageSent = true;
2116 } else {
aa7133@att.com409f2232020-10-29 13:06:45 +03002117 mdclog_write(MDCLOG_ERR, "RIC request id missing illegal request");
ss412g1a79bdf2019-10-24 12:03:05 +03002118 }
2119 }
2120 if (messageSent) {
2121 break;
2122 }
2123 }
2124 break;
2125 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02002126 case ProcedureCode_id_RICsubscription: {
2127 if (logLevel >= MDCLOG_DEBUG) {
2128 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
2129 }
dhirajverma6cf28612021-06-23 04:53:34 -04002130#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03002131 message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
2132 message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04002133#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002134 if (sendRequestToXapp(message, RIC_SUB_FAILURE, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02002135 mdclog_write(MDCLOG_ERR, "Subscription unsuccessful message failed to send to xAPP");
2136 }
2137 break;
2138 }
2139 case ProcedureCode_id_RICsubscriptionDelete: {
2140 if (logLevel >= MDCLOG_DEBUG) {
2141 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
2142 }
dhirajverma6cf28612021-06-23 04:53:34 -04002143#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03002144 message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
2145 message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04002146#endif
ss412g4aeeda22020-06-16 14:12:41 +03002147 if (sendRequestToXapp(message, RIC_SUB_FAILURE, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02002148 mdclog_write(MDCLOG_ERR, "Subscription Delete unsuccessful message failed to send to xAPP");
2149 }
2150 break;
2151 }
ss412g1a79bdf2019-10-24 12:03:05 +03002152 default: {
2153 mdclog_write(MDCLOG_WARN, "Undefined or not supported message = %ld", procedureCode);
2154 message.message.messageType = 0; // no RMR message type yet
dhirajverma99990292021-07-05 10:03:59 -04002155#ifndef UNIT_TEST
ss412g1a79bdf2019-10-24 12:03:05 +03002156 buildJsonMessage(message);
dhirajverma99990292021-07-05 10:03:59 -04002157#endif
ss412g1a79bdf2019-10-24 12:03:05 +03002158 break;
2159 }
2160 }
ss412g1a79bdf2019-10-24 12:03:05 +03002161}
2162
2163/**
2164 *
2165 * @param message
2166 * @param requestId
2167 * @param rmrMmessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03002168 * @return
2169 */
2170int sendRequestToXapp(ReportingMessages_t &message,
2171 int requestId,
aa7133@att.combe92d712020-03-18 17:30:39 +02002172 RmrMessagesBuffer_t &rmrMmessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03002173 rmr_bytes2meid(rmrMmessageBuffer.sendMessage,
2174 (unsigned char *)message.message.enodbName,
2175 strlen(message.message.enodbName));
2176 message.message.messageType = rmrMmessageBuffer.sendMessage->mtype = requestId;
2177 rmrMmessageBuffer.sendMessage->state = 0;
2178 static unsigned char tx[32];
2179 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2180 rmr_bytes2xact(rmrMmessageBuffer.sendMessage, tx, strlen((const char *) tx));
2181
aa7133@att.combe92d712020-03-18 17:30:39 +02002182 auto rc = sendRmrMessage(rmrMmessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03002183 return rc;
2184}
2185
ss412g4aeeda22020-06-16 14:12:41 +03002186/**
2187 *
2188 * @param pSctpParams
2189 */
aa7133@att.combe92d712020-03-18 17:30:39 +02002190void getRmrContext(sctp_params_t &pSctpParams) {
ss412g3bac2da2020-01-05 11:52:19 +02002191 pSctpParams.rmrCtx = nullptr;
aa7133@att.comab0e0f42020-04-06 17:13:58 +03002192 pSctpParams.rmrCtx = rmr_init(pSctpParams.rmrAddress, RECEIVE_XAPP_BUFFER_SIZE, RMRFL_NONE);
ss412g3bac2da2020-01-05 11:52:19 +02002193 if (pSctpParams.rmrCtx == nullptr) {
2194 mdclog_write(MDCLOG_ERR, "Failed to initialize RMR");
ss412g3bac2da2020-01-05 11:52:19 +02002195 return;
ss412g1a79bdf2019-10-24 12:03:05 +03002196 }
2197
ss412g3bac2da2020-01-05 11:52:19 +02002198 rmr_set_stimeout(pSctpParams.rmrCtx, 0); // disable retries for any send operation
ss412g1a79bdf2019-10-24 12:03:05 +03002199 // we need to find that routing table exist and we can run
2200 if (mdclog_level_get() >= MDCLOG_INFO) {
2201 mdclog_write(MDCLOG_INFO, "We are after RMR INIT wait for RMR_Ready");
2202 }
2203 int rmrReady = 0;
2204 int count = 0;
2205 while (!rmrReady) {
ss412g3bac2da2020-01-05 11:52:19 +02002206 if ((rmrReady = rmr_ready(pSctpParams.rmrCtx)) == 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002207 sleep(1);
2208 }
2209 count++;
2210 if (count % 60 == 0) {
2211 mdclog_write(MDCLOG_INFO, "waiting to RMR ready state for %d seconds", count);
2212 }
2213 }
2214 if (mdclog_level_get() >= MDCLOG_INFO) {
2215 mdclog_write(MDCLOG_INFO, "RMR running");
2216 }
ss412g3bac2da2020-01-05 11:52:19 +02002217 rmr_init_trace(pSctpParams.rmrCtx, 200);
2218 // get the RMR fd for the epoll
2219 pSctpParams.rmrListenFd = rmr_get_rcvfd(pSctpParams.rmrCtx);
2220 struct epoll_event event{};
2221 // add RMR fd to epoll
2222 event.events = (EPOLLIN);
2223 event.data.fd = pSctpParams.rmrListenFd;
2224 // add listening RMR FD to epoll
2225 if (epoll_ctl(pSctpParams.epoll_fd, EPOLL_CTL_ADD, pSctpParams.rmrListenFd, &event)) {
2226 mdclog_write(MDCLOG_ERR, "Failed to add RMR descriptor to epoll");
2227 close(pSctpParams.rmrListenFd);
2228 rmr_close(pSctpParams.rmrCtx);
2229 pSctpParams.rmrCtx = nullptr;
2230 }
ss412g1a79bdf2019-10-24 12:03:05 +03002231}
2232
ss412g4aeeda22020-06-16 14:12:41 +03002233/**
2234 *
2235 * @param message
2236 * @param rmrMessageBuffer
2237 * @return
2238 */
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002239int PER_FromXML(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
aa7133@att.com07e256f2020-04-20 15:59:51 +03002240 E2AP_PDU_t *pdu = nullptr;
aa7133@att.com1c4f50f2020-03-30 14:30:52 +03002241
2242 if (mdclog_level_get() >= MDCLOG_DEBUG) {
ss412g4aeeda22020-06-16 14:12:41 +03002243 mdclog_write(MDCLOG_DEBUG, "got xml Format data from xApp of size %d is:%s",
aa7133@att.com07e256f2020-04-20 15:59:51 +03002244 rmrMessageBuffer.rcvMessage->len, rmrMessageBuffer.rcvMessage->payload);
aa7133@att.com1c4f50f2020-03-30 14:30:52 +03002245 }
aa7133@att.com1fecb052020-03-29 09:44:09 +03002246 auto rval = asn_decode(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, (void **) &pdu,
2247 rmrMessageBuffer.rcvMessage->payload, rmrMessageBuffer.rcvMessage->len);
aa7133@att.com409f2232020-10-29 13:06:45 +03002248 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2249 mdclog_write(MDCLOG_DEBUG, "%s After decoding the XML to PDU", __func__ );
2250 }
aa7133@att.com1fecb052020-03-29 09:44:09 +03002251 if (rval.code != RC_OK) {
aa7133@att.com1c4f50f2020-03-30 14:30:52 +03002252 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) setup response from E2MGR : %s",
aa7133@att.com1fecb052020-03-29 09:44:09 +03002253 rval.code,
2254 message.message.enodbName);
2255 return -1;
2256 }
2257
aa7133@att.coma0f0db62020-04-01 16:52:50 +03002258 int buff_size = RECEIVE_XAPP_BUFFER_SIZE;
2259 auto er = asn_encode_to_buffer(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, pdu,
2260 rmrMessageBuffer.rcvMessage->payload, buff_size);
aa7133@att.com409f2232020-10-29 13:06:45 +03002261 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2262 mdclog_write(MDCLOG_DEBUG, "%s After encoding PDU to PER", __func__ );
2263 }
aa7133@att.com1fecb052020-03-29 09:44:09 +03002264 if (er.encoded == -1) {
2265 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
2266 return -1;
aa7133@att.coma0f0db62020-04-01 16:52:50 +03002267 } else if (er.encoded > (ssize_t)buff_size) {
2268 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 +03002269 (int)rmrMessageBuffer.rcvMessage->len,
aa7133@att.coma0f0db62020-04-01 16:52:50 +03002270 asn_DEF_E2AP_PDU.name,
2271 __func__,
2272 __LINE__);
aa7133@att.com1fecb052020-03-29 09:44:09 +03002273 return -1;
2274 }
aa7133@att.coma0f0db62020-04-01 16:52:50 +03002275 rmrMessageBuffer.rcvMessage->len = er.encoded;
aa7133@att.com1fecb052020-03-29 09:44:09 +03002276 return 0;
2277}
2278
ss412g1a79bdf2019-10-24 12:03:05 +03002279/**
2280 *
ss412g1a79bdf2019-10-24 12:03:05 +03002281 * @param sctpMap
2282 * @param rmrMessageBuffer
2283 * @param ts
ss412g1a79bdf2019-10-24 12:03:05 +03002284 * @return
2285 */
aa7133@att.com6680c3b2020-03-22 12:06:58 +02002286int receiveXappMessages(Sctp_Map_t *sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +03002287 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +02002288 struct timespec &ts) {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002289 int loglevel = mdclog_level_get();
ss412g1a79bdf2019-10-24 12:03:05 +03002290 if (rmrMessageBuffer.rcvMessage == nullptr) {
2291 //we have error
2292 mdclog_write(MDCLOG_ERR, "RMR Allocation message, %s", strerror(errno));
ss412g1a79bdf2019-10-24 12:03:05 +03002293 return -1;
2294 }
2295
aa7133@att.comf13529a2020-08-27 11:21:16 +03002296// if (loglevel >= MDCLOG_DEBUG) {
2297// mdclog_write(MDCLOG_DEBUG, "Call to rmr_rcv_msg");
2298// }
ss412g1a79bdf2019-10-24 12:03:05 +03002299 rmrMessageBuffer.rcvMessage = rmr_rcv_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.rcvMessage);
2300 if (rmrMessageBuffer.rcvMessage == nullptr) {
aa7133@att.com409f2232020-10-29 13:06:45 +03002301 mdclog_write(MDCLOG_ERR, "RMR Receiving message with null pointer, Reallocated rmr message buffer");
ss412g1a79bdf2019-10-24 12:03:05 +03002302 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
ss412g1a79bdf2019-10-24 12:03:05 +03002303 return -2;
2304 }
2305 ReportingMessages_t message;
2306 message.message.direction = 'D';
2307 message.message.time.tv_nsec = ts.tv_nsec;
2308 message.message.time.tv_sec = ts.tv_sec;
2309
2310 // get message payload
2311 //auto msgData = msg->payload;
dhirajverma60f8bef2021-06-29 07:22:58 -04002312#ifdef UNIT_TEST
dhirajverma99990292021-07-05 10:03:59 -04002313 rmrMessageBuffer.rcvMessage->state = 0;
dhirajverma60f8bef2021-06-29 07:22:58 -04002314#endif
ss412g1a79bdf2019-10-24 12:03:05 +03002315 if (rmrMessageBuffer.rcvMessage->state != 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +03002316 mdclog_write(MDCLOG_ERR, "RMR Receiving message with stat = %d", rmrMessageBuffer.rcvMessage->state);
ss412g1a79bdf2019-10-24 12:03:05 +03002317 return -1;
2318 }
aa7133@att.com1fecb052020-03-29 09:44:09 +03002319 rmr_get_meid(rmrMessageBuffer.rcvMessage, (unsigned char *)message.message.enodbName);
ss412g4aeeda22020-06-16 14:12:41 +03002320 message.peerInfo = (ConnectedCU_t *) sctpMap->find(message.message.enodbName);
2321 if (message.peerInfo == nullptr) {
2322 auto type = rmrMessageBuffer.rcvMessage->mtype;
2323 switch (type) {
2324 case RIC_SCTP_CLEAR_ALL:
2325 case E2_TERM_KEEP_ALIVE_REQ:
2326 case RIC_HEALTH_CHECK_REQ:
2327 break;
2328 default:
dhirajverma99990292021-07-05 10:03:59 -04002329#ifdef UNIT_TEST
2330 break;
2331#endif
ss412g4aeeda22020-06-16 14:12:41 +03002332 mdclog_write(MDCLOG_ERR, "Failed to send message no CU entry %s", message.message.enodbName);
2333 return -1;
2334 }
2335 }
2336
aa7133@att.comde40d1c2020-08-26 16:19:20 +03002337 if (rmrMessageBuffer.rcvMessage->mtype != RIC_HEALTH_CHECK_REQ) {
2338 num_of_XAPP_messages.fetch_add(1, std::memory_order_release);
2339
2340 }
ss412g1a79bdf2019-10-24 12:03:05 +03002341 switch (rmrMessageBuffer.rcvMessage->mtype) {
aa7133@att.combe92d712020-03-18 17:30:39 +02002342 case RIC_E2_SETUP_RESP : {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002343 if (loglevel >= MDCLOG_DEBUG) {
2344 mdclog_write(MDCLOG_DEBUG, "RIC_E2_SETUP_RESP");
2345 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002346 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
aa7133@att.com1fecb052020-03-29 09:44:09 +03002347 break;
2348 }
dhirajverma99990292021-07-05 10:03:59 -04002349#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03002350 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2351 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment(rmrMessageBuffer.rcvMessage->len);
dhirajverma99990292021-07-05 10:03:59 -04002352#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002353 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2354 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_SETUP_RESP");
2355 return -6;
ss412g1a79bdf2019-10-24 12:03:05 +03002356 }
2357 break;
2358 }
aa7133@att.combe92d712020-03-18 17:30:39 +02002359 case RIC_E2_SETUP_FAILURE : {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002360 if (loglevel >= MDCLOG_DEBUG) {
2361 mdclog_write(MDCLOG_DEBUG, "RIC_E2_SETUP_FAILURE");
2362 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002363 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
aa7133@att.com1fecb052020-03-29 09:44:09 +03002364 break;
2365 }
dhirajverma99990292021-07-05 10:03:59 -04002366#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03002367 message.peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2368 message.peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment(rmrMessageBuffer.rcvMessage->len);
dhirajverma99990292021-07-05 10:03:59 -04002369#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002370 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2371 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_SETUP_FAILURE");
2372 return -6;
ss412g1a79bdf2019-10-24 12:03:05 +03002373 }
2374 break;
2375 }
aa7133@att.combe92d712020-03-18 17:30:39 +02002376 case RIC_ERROR_INDICATION: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002377 if (loglevel >= MDCLOG_DEBUG) {
2378 mdclog_write(MDCLOG_DEBUG, "RIC_ERROR_INDICATION");
2379 }
dhirajverma99990292021-07-05 10:03:59 -04002380#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03002381 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_ErrorIndication]->Increment();
2382 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_ErrorIndication]->Increment(rmrMessageBuffer.rcvMessage->len);
dhirajverma99990292021-07-05 10:03:59 -04002383#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002384 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2385 mdclog_write(MDCLOG_ERR, "Failed to send RIC_ERROR_INDICATION");
ss412g1a79bdf2019-10-24 12:03:05 +03002386 return -6;
2387 }
2388 break;
2389 }
2390 case RIC_SUB_REQ: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002391 if (loglevel >= MDCLOG_DEBUG) {
2392 mdclog_write(MDCLOG_DEBUG, "RIC_SUB_REQ");
2393 }
dhirajverma99990292021-07-05 10:03:59 -04002394#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03002395 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
2396 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment(rmrMessageBuffer.rcvMessage->len);
dhirajverma99990292021-07-05 10:03:59 -04002397#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002398 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002399 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SUB_REQ");
ss412g1a79bdf2019-10-24 12:03:05 +03002400 return -6;
2401 }
2402 break;
2403 }
2404 case RIC_SUB_DEL_REQ: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002405 if (loglevel >= MDCLOG_DEBUG) {
2406 mdclog_write(MDCLOG_DEBUG, "RIC_SUB_DEL_REQ");
2407 }
dhirajverma99990292021-07-05 10:03:59 -04002408#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03002409 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
2410 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment(rmrMessageBuffer.rcvMessage->len);
dhirajverma99990292021-07-05 10:03:59 -04002411#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002412 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002413 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SUB_DEL_REQ");
ss412g1a79bdf2019-10-24 12:03:05 +03002414 return -6;
2415 }
2416 break;
2417 }
2418 case RIC_CONTROL_REQ: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002419 if (loglevel >= MDCLOG_DEBUG) {
2420 mdclog_write(MDCLOG_DEBUG, "RIC_CONTROL_REQ");
2421 }
dhirajverma99990292021-07-05 10:03:59 -04002422#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03002423 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
2424 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment(rmrMessageBuffer.rcvMessage->len);
dhirajverma99990292021-07-05 10:03:59 -04002425#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002426 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002427 mdclog_write(MDCLOG_ERR, "Failed to send RIC_CONTROL_REQ");
ss412g1a79bdf2019-10-24 12:03:05 +03002428 return -6;
2429 }
2430 break;
2431 }
2432 case RIC_SERVICE_QUERY: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002433 if (loglevel >= MDCLOG_DEBUG) {
2434 mdclog_write(MDCLOG_DEBUG, "RIC_SERVICE_QUERY");
2435 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002436 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2437 break;
2438 }
dhirajverma99990292021-07-05 10:03:59 -04002439#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03002440 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceQuery]->Increment();
2441 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceQuery]->Increment(rmrMessageBuffer.rcvMessage->len);
dhirajverma99990292021-07-05 10:03:59 -04002442#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002443 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002444 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_QUERY");
ss412g1a79bdf2019-10-24 12:03:05 +03002445 return -6;
2446 }
2447 break;
2448 }
2449 case RIC_SERVICE_UPDATE_ACK: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002450 if (loglevel >= MDCLOG_DEBUG) {
2451 mdclog_write(MDCLOG_DEBUG, "RIC_SERVICE_UPDATE_ACK");
2452 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002453 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
aa7133@att.com54885fe2020-10-29 17:24:49 +03002454 mdclog_write(MDCLOG_ERR, "error in PER_FromXML");
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002455 break;
2456 }
dhirajverma99990292021-07-05 10:03:59 -04002457#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03002458 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
aa7133@att.com52771912020-10-29 17:45:01 +03002459 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
dhirajverma99990292021-07-05 10:03:59 -04002460#endif
aa7133@att.com54885fe2020-10-29 17:24:49 +03002461 if (loglevel >= MDCLOG_DEBUG) {
2462 mdclog_write(MDCLOG_DEBUG, "Before sending to CU");
2463 }
aa7133@att.combe92d712020-03-18 17:30:39 +02002464 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002465 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_UPDATE_ACK");
ss412g1a79bdf2019-10-24 12:03:05 +03002466 return -6;
2467 }
2468 break;
2469 }
2470 case RIC_SERVICE_UPDATE_FAILURE: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002471 if (loglevel >= MDCLOG_DEBUG) {
2472 mdclog_write(MDCLOG_DEBUG, "RIC_SERVICE_UPDATE_FAILURE");
2473 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002474 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2475 break;
2476 }
dhirajverma99990292021-07-05 10:03:59 -04002477#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03002478 message.peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
aa7133@att.com7c07fee2020-10-29 17:54:38 +03002479 message.peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
dhirajverma99990292021-07-05 10:03:59 -04002480#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002481 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002482 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_UPDATE_FAILURE");
ss412g1a79bdf2019-10-24 12:03:05 +03002483 return -6;
2484 }
2485 break;
2486 }
ss412g4aeeda22020-06-16 14:12:41 +03002487 case RIC_E2_RESET_REQ: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002488 if (loglevel >= MDCLOG_DEBUG) {
2489 mdclog_write(MDCLOG_DEBUG, "RIC_E2_RESET_REQ");
2490 }
ss412g4aeeda22020-06-16 14:12:41 +03002491 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2492 break;
2493 }
dhirajverma99990292021-07-05 10:03:59 -04002494#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03002495 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2496 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment(rmrMessageBuffer.rcvMessage->len);
dhirajverma99990292021-07-05 10:03:59 -04002497#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002498 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g4aeeda22020-06-16 14:12:41 +03002499 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_RESET");
ss412g1a79bdf2019-10-24 12:03:05 +03002500 return -6;
2501 }
2502 break;
2503 }
ss412g4aeeda22020-06-16 14:12:41 +03002504 case RIC_E2_RESET_RESP: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002505 if (loglevel >= MDCLOG_DEBUG) {
2506 mdclog_write(MDCLOG_DEBUG, "RIC_E2_RESET_RESP");
2507 }
ss412g4aeeda22020-06-16 14:12:41 +03002508 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2509 break;
2510 }
dhirajverma99990292021-07-05 10:03:59 -04002511#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03002512 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2513 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment(rmrMessageBuffer.rcvMessage->len);
dhirajverma99990292021-07-05 10:03:59 -04002514#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002515 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g4aeeda22020-06-16 14:12:41 +03002516 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_RESET_RESP");
ss412g1a79bdf2019-10-24 12:03:05 +03002517 return -6;
2518 }
2519 break;
2520 }
2521 case RIC_SCTP_CLEAR_ALL: {
2522 mdclog_write(MDCLOG_INFO, "RIC_SCTP_CLEAR_ALL");
2523 // loop on all keys and close socket and then erase all map.
2524 vector<char *> v;
2525 sctpMap->getKeys(v);
2526 for (auto const &iter : v) { //}; iter != sctpMap.end(); iter++) {
2527 if (!boost::starts_with((string) (iter), "host:") && !boost::starts_with((string) (iter), "msg:")) {
2528 auto *peerInfo = (ConnectedCU_t *) sctpMap->find(iter);
2529 if (peerInfo == nullptr) {
2530 continue;
2531 }
2532 close(peerInfo->fileDescriptor);
2533 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
2534 message.message.direction = 'D';
2535 message.message.time.tv_nsec = ts.tv_nsec;
2536 message.message.time.tv_sec = ts.tv_sec;
2537
2538 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
2539 snprintf((char *)rmrMessageBuffer.sendMessage->payload,
aa7133@att.com96c9b882020-03-25 14:50:40 +02002540 256,
2541 "%s|RIC_SCTP_CLEAR_ALL",
2542 peerInfo->enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03002543 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
2544 mdclog_write(MDCLOG_INFO, "%s", message.message.asndata);
aa7133@att.combe92d712020-03-18 17:30:39 +02002545 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002546 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
2547 }
2548 free(peerInfo);
2549 }
2550 }
2551
2552 sleep(1);
2553 sctpMap->clear();
2554 break;
2555 }
ss412g3bac2da2020-01-05 11:52:19 +02002556 case E2_TERM_KEEP_ALIVE_REQ: {
2557 // send message back
ss412g3bac2da2020-01-05 11:52:19 +02002558 rmr_bytes2payload(rmrMessageBuffer.sendMessage,
aa7133@att.com96c9b882020-03-25 14:50:40 +02002559 (unsigned char *)rmrMessageBuffer.ka_message,
2560 rmrMessageBuffer.ka_message_len);
ss412g3bac2da2020-01-05 11:52:19 +02002561 rmrMessageBuffer.sendMessage->mtype = E2_TERM_KEEP_ALIVE_RESP;
2562 rmrMessageBuffer.sendMessage->state = 0;
2563 static unsigned char tx[32];
2564 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2565 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, txLen);
dhirajverma60f8bef2021-06-29 07:22:58 -04002566#ifndef UNIT_TEST
ss412g3bac2da2020-01-05 11:52:19 +02002567 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
dhirajverma60f8bef2021-06-29 07:22:58 -04002568#endif
ss412g3bac2da2020-01-05 11:52:19 +02002569 if (rmrMessageBuffer.sendMessage == nullptr) {
2570 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
aa7133@att.com77fff432020-01-27 12:01:20 +02002571 mdclog_write(MDCLOG_ERR, "Failed to send E2_TERM_KEEP_ALIVE_RESP RMR message returned NULL");
ss412g3bac2da2020-01-05 11:52:19 +02002572 } else if (rmrMessageBuffer.sendMessage->state != 0) {
aa7133@att.com77fff432020-01-27 12:01:20 +02002573 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 +02002574 rmrMessageBuffer.sendMessage->state, translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str());
aa7133@att.comf13529a2020-08-27 11:21:16 +03002575 } else if (loglevel >= MDCLOG_DEBUG) {
aa7133@att.combc110772020-03-25 09:45:14 +02002576 mdclog_write(MDCLOG_DEBUG, "Got Keep Alive Request send : %s", rmrMessageBuffer.ka_message);
ss412g3bac2da2020-01-05 11:52:19 +02002577 }
aa7133@att.com342843e2020-01-26 12:53:34 +02002578
ss412g3bac2da2020-01-05 11:52:19 +02002579 break;
2580 }
ss412g4aeeda22020-06-16 14:12:41 +03002581 case RIC_HEALTH_CHECK_REQ: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002582 static int counter = 0;
ss412g4aeeda22020-06-16 14:12:41 +03002583 // send message back
aa7133@att.comc18fe532020-06-30 12:40:18 +03002584 rmr_bytes2payload(rmrMessageBuffer.rcvMessage,
ss412g4aeeda22020-06-16 14:12:41 +03002585 (unsigned char *)"OK",
2586 2);
aa7133@att.comc18fe532020-06-30 12:40:18 +03002587 rmrMessageBuffer.rcvMessage->mtype = RIC_HEALTH_CHECK_RESP;
2588 rmrMessageBuffer.rcvMessage->state = 0;
ss412g4aeeda22020-06-16 14:12:41 +03002589 static unsigned char tx[32];
2590 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
aa7133@att.comc18fe532020-06-30 12:40:18 +03002591 rmr_bytes2xact(rmrMessageBuffer.rcvMessage, tx, txLen);
2592 rmrMessageBuffer.rcvMessage = rmr_rts_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.rcvMessage);
2593 //rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2594 if (rmrMessageBuffer.rcvMessage == nullptr) {
2595 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
ss412g4aeeda22020-06-16 14:12:41 +03002596 mdclog_write(MDCLOG_ERR, "Failed to send RIC_HEALTH_CHECK_RESP RMR message returned NULL");
aa7133@att.comc18fe532020-06-30 12:40:18 +03002597 } else if (rmrMessageBuffer.rcvMessage->state != 0) {
ss412g4aeeda22020-06-16 14:12:41 +03002598 mdclog_write(MDCLOG_ERR, "Failed to send RIC_HEALTH_CHECK_RESP, on RMR state = %d ( %s)",
aa7133@att.comc18fe532020-06-30 12:40:18 +03002599 rmrMessageBuffer.rcvMessage->state, translateRmrErrorMessages(rmrMessageBuffer.rcvMessage->state).c_str());
aa7133@att.comf13529a2020-08-27 11:21:16 +03002600 } else if (loglevel >= MDCLOG_DEBUG && ++counter % 100 == 0) {
2601 mdclog_write(MDCLOG_DEBUG, "Got %d RIC_HEALTH_CHECK_REQ Request send : OK", counter);
ss412g4aeeda22020-06-16 14:12:41 +03002602 }
2603
2604 break;
2605 }
2606
ss412g1a79bdf2019-10-24 12:03:05 +03002607 default:
aa7133@att.com409f2232020-10-29 13:06:45 +03002608 mdclog_write(MDCLOG_WARN, "Message Type : %d is not supported", rmrMessageBuffer.rcvMessage->mtype);
ss412g1a79bdf2019-10-24 12:03:05 +03002609 message.message.asndata = rmrMessageBuffer.rcvMessage->payload;
2610 message.message.asnLength = rmrMessageBuffer.rcvMessage->len;
2611 message.message.time.tv_nsec = ts.tv_nsec;
2612 message.message.time.tv_sec = ts.tv_sec;
2613 message.message.messageType = rmrMessageBuffer.rcvMessage->mtype;
2614
2615 buildJsonMessage(message);
2616
2617
ss412g1a79bdf2019-10-24 12:03:05 +03002618 return -7;
2619 }
2620 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2621 mdclog_write(MDCLOG_DEBUG, "EXIT OK from %s", __FUNCTION__);
2622 }
ss412g1a79bdf2019-10-24 12:03:05 +03002623 return 0;
2624}
2625
2626/**
2627 * Send message to the CU that is not expecting for successful or unsuccessful results
2628 * @param messageBuffer
2629 * @param message
2630 * @param failedMsgId
2631 * @param sctpMap
ss412g1a79bdf2019-10-24 12:03:05 +03002632 * @return
2633 */
2634int sendDirectionalSctpMsg(RmrMessagesBuffer_t &messageBuffer,
2635 ReportingMessages_t &message,
2636 int failedMsgId,
aa7133@att.combe92d712020-03-18 17:30:39 +02002637 Sctp_Map_t *sctpMap) {
aa7133@att.com409f2232020-10-29 13:06:45 +03002638 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2639 mdclog_write(MDCLOG_DEBUG, "send message: %d to %s address", message.message.messageType, message.message.enodbName);
2640 }
ss412g1a79bdf2019-10-24 12:03:05 +03002641
aa7133@att.combe92d712020-03-18 17:30:39 +02002642 getRequestMetaData(message, messageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03002643 if (mdclog_level_get() >= MDCLOG_INFO) {
2644 mdclog_write(MDCLOG_INFO, "send message to %s address", message.message.enodbName);
2645 }
2646
aa7133@att.combe92d712020-03-18 17:30:39 +02002647 auto rc = sendMessagetoCu(sctpMap, messageBuffer, message, failedMsgId);
ss412g1a79bdf2019-10-24 12:03:05 +03002648 return rc;
2649}
2650
2651/**
2652 *
2653 * @param sctpMap
2654 * @param messageBuffer
2655 * @param message
2656 * @param failedMesgId
ss412g1a79bdf2019-10-24 12:03:05 +03002657 * @return
2658 */
2659int sendMessagetoCu(Sctp_Map_t *sctpMap,
2660 RmrMessagesBuffer_t &messageBuffer,
2661 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +02002662 int failedMesgId) {
ss412g1a79bdf2019-10-24 12:03:05 +03002663 // get the FD
2664 message.message.messageType = messageBuffer.rcvMessage->mtype;
ss412g4aeeda22020-06-16 14:12:41 +03002665 auto rc = sendSctpMsg(message.peerInfo, message, sctpMap);
ss412g1a79bdf2019-10-24 12:03:05 +03002666 return rc;
2667}
2668
ss412g1a79bdf2019-10-24 12:03:05 +03002669
2670/**
2671 *
2672 * @param epoll_fd
2673 * @param peerInfo
2674 * @param events
2675 * @param sctpMap
2676 * @param enodbName
2677 * @param msgType
ss412g1a79bdf2019-10-24 12:03:05 +03002678 * @return
2679 */
2680int addToEpoll(int epoll_fd,
2681 ConnectedCU_t *peerInfo,
2682 uint32_t events,
2683 Sctp_Map_t *sctpMap,
2684 char *enodbName,
aa7133@att.combe92d712020-03-18 17:30:39 +02002685 int msgType) {
ss412g1a79bdf2019-10-24 12:03:05 +03002686 // Add to Epol
2687 struct epoll_event event{};
2688 event.data.ptr = peerInfo;
2689 event.events = events;
2690 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, peerInfo->fileDescriptor, &event) < 0) {
2691 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com409f2232020-10-29 13:06:45 +03002692 mdclog_write(MDCLOG_DEBUG, "epoll_ctl EPOLL_CTL_ADD (may check not to quit here), %s, %s %d",
ss412g1a79bdf2019-10-24 12:03:05 +03002693 strerror(errno), __func__, __LINE__);
2694 }
2695 close(peerInfo->fileDescriptor);
aa7133@att.com84bd3342020-03-16 18:04:57 +02002696 if (enodbName != nullptr) {
aa7133@att.combe92d712020-03-18 17:30:39 +02002697 cleanHashEntry(peerInfo, sctpMap);
aa7133@att.com84bd3342020-03-16 18:04:57 +02002698 char key[MAX_ENODB_NAME_SIZE * 2];
2699 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", enodbName, msgType);
2700 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2701 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
2702 }
2703 auto tmp = sctpMap->find(key);
2704 if (tmp) {
2705 free(tmp);
2706 sctpMap->erase(key);
2707 }
2708 } else {
2709 peerInfo->enodbName[0] = 0;
ss412g1a79bdf2019-10-24 12:03:05 +03002710 }
aa7133@att.com409f2232020-10-29 13:06:45 +03002711 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may check not to quit here)");
ss412g1a79bdf2019-10-24 12:03:05 +03002712 return -1;
2713 }
ss412g1a79bdf2019-10-24 12:03:05 +03002714 return 0;
2715}
2716
2717/**
2718 *
2719 * @param epoll_fd
2720 * @param peerInfo
2721 * @param events
2722 * @param sctpMap
2723 * @param enodbName
2724 * @param msgType
ss412g1a79bdf2019-10-24 12:03:05 +03002725 * @return
2726 */
2727int modifyToEpoll(int epoll_fd,
2728 ConnectedCU_t *peerInfo,
2729 uint32_t events,
2730 Sctp_Map_t *sctpMap,
2731 char *enodbName,
aa7133@att.combe92d712020-03-18 17:30:39 +02002732 int msgType) {
ss412g1a79bdf2019-10-24 12:03:05 +03002733 // Add to Epol
2734 struct epoll_event event{};
2735 event.data.ptr = peerInfo;
2736 event.events = events;
2737 if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, peerInfo->fileDescriptor, &event) < 0) {
2738 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com409f2232020-10-29 13:06:45 +03002739 mdclog_write(MDCLOG_DEBUG, "epoll_ctl EPOLL_CTL_MOD (may check not to quit here), %s, %s %d",
ss412g1a79bdf2019-10-24 12:03:05 +03002740 strerror(errno), __func__, __LINE__);
2741 }
2742 close(peerInfo->fileDescriptor);
aa7133@att.combe92d712020-03-18 17:30:39 +02002743 cleanHashEntry(peerInfo, sctpMap);
ss412g1a79bdf2019-10-24 12:03:05 +03002744 char key[MAX_ENODB_NAME_SIZE * 2];
2745 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", enodbName, msgType);
2746 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2747 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
2748 }
aa7133@att.com96c9b882020-03-25 14:50:40 +02002749 auto tmp = sctpMap->find(key);
ss412g1a79bdf2019-10-24 12:03:05 +03002750 if (tmp) {
2751 free(tmp);
2752 }
2753 sctpMap->erase(key);
aa7133@att.com409f2232020-10-29 13:06:45 +03002754 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may check not to quit here)");
ss412g1a79bdf2019-10-24 12:03:05 +03002755 return -1;
2756 }
ss412g1a79bdf2019-10-24 12:03:05 +03002757 return 0;
2758}
2759
2760
aa7133@att.combe92d712020-03-18 17:30:39 +02002761int sendRmrMessage(RmrMessagesBuffer_t &rmrMessageBuffer, ReportingMessages_t &message) {
ss412g1a79bdf2019-10-24 12:03:05 +03002762 buildJsonMessage(message);
dhirajverma60f8bef2021-06-29 07:22:58 -04002763#ifndef UNIT_TEST
ss412g1a79bdf2019-10-24 12:03:05 +03002764 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
dhirajverma60f8bef2021-06-29 07:22:58 -04002765#else
dhirajverma99990292021-07-05 10:03:59 -04002766 rmrMessageBuffer.sendMessage->state = RMR_ERR_RETRY;
dhirajverma60f8bef2021-06-29 07:22:58 -04002767#endif
ss412g1a79bdf2019-10-24 12:03:05 +03002768 if (rmrMessageBuffer.sendMessage == nullptr) {
2769 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
2770 mdclog_write(MDCLOG_ERR, "RMR failed send message returned with NULL pointer");
ss412g1a79bdf2019-10-24 12:03:05 +03002771 return -1;
2772 }
2773
2774 if (rmrMessageBuffer.sendMessage->state != 0) {
2775 char meid[RMR_MAX_MEID]{};
2776 if (rmrMessageBuffer.sendMessage->state == RMR_ERR_RETRY) {
2777 usleep(5);
2778 rmrMessageBuffer.sendMessage->state = 0;
2779 mdclog_write(MDCLOG_INFO, "RETRY sending Message type %d to Xapp from %s",
2780 rmrMessageBuffer.sendMessage->mtype,
2781 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
dhirajverma60f8bef2021-06-29 07:22:58 -04002782#ifndef UNIT_TEST
ss412g1a79bdf2019-10-24 12:03:05 +03002783 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
dhirajverma60f8bef2021-06-29 07:22:58 -04002784#endif
ss412g1a79bdf2019-10-24 12:03:05 +03002785 if (rmrMessageBuffer.sendMessage == nullptr) {
2786 mdclog_write(MDCLOG_ERR, "RMR failed send message returned with NULL pointer");
2787 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
ss412g1a79bdf2019-10-24 12:03:05 +03002788 return -1;
2789 } else if (rmrMessageBuffer.sendMessage->state != 0) {
2790 mdclog_write(MDCLOG_ERR,
2791 "Message state %s while sending request %d to Xapp from %s after retry of 10 microseconds",
2792 translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str(),
2793 rmrMessageBuffer.sendMessage->mtype,
2794 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
2795 auto rc = rmrMessageBuffer.sendMessage->state;
ss412g1a79bdf2019-10-24 12:03:05 +03002796 return rc;
2797 }
2798 } else {
2799 mdclog_write(MDCLOG_ERR, "Message state %s while sending request %d to Xapp from %s",
2800 translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str(),
2801 rmrMessageBuffer.sendMessage->mtype,
2802 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
ss412g1a79bdf2019-10-24 12:03:05 +03002803 return rmrMessageBuffer.sendMessage->state;
2804 }
2805 }
2806 return 0;
2807}
2808
2809void buildJsonMessage(ReportingMessages_t &message) {
dhirajverma60f8bef2021-06-29 07:22:58 -04002810#ifdef UNIT_TEST
dhirajverma99990292021-07-05 10:03:59 -04002811 jsonTrace = true;
dhirajverma60f8bef2021-06-29 07:22:58 -04002812#endif
ss412g3bac2da2020-01-05 11:52:19 +02002813 if (jsonTrace) {
2814 message.outLen = sizeof(message.base64Data);
2815 base64::encode((const unsigned char *) message.message.asndata,
2816 (const int) message.message.asnLength,
2817 message.base64Data,
2818 message.outLen);
2819 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.comc5a15202020-04-05 19:57:01 +03002820 mdclog_write(MDCLOG_DEBUG, "Tracing: ASN length = %d, base64 message length = %d ",
ss412g3bac2da2020-01-05 11:52:19 +02002821 (int) message.message.asnLength,
2822 (int) message.outLen);
2823 }
2824
2825 snprintf(message.buffer, sizeof(message.buffer),
aa7133@att.com96c9b882020-03-25 14:50:40 +02002826 "{\"header\": {\"ts\": \"%ld.%09ld\","
2827 "\"ranName\": \"%s\","
2828 "\"messageType\": %d,"
2829 "\"direction\": \"%c\"},"
2830 "\"base64Length\": %d,"
2831 "\"asnBase64\": \"%s\"}",
2832 message.message.time.tv_sec,
2833 message.message.time.tv_nsec,
2834 message.message.enodbName,
2835 message.message.messageType,
2836 message.message.direction,
2837 (int) message.outLen,
2838 message.base64Data);
ss412g3bac2da2020-01-05 11:52:19 +02002839 static src::logger_mt &lg = my_logger::get();
2840
2841 BOOST_LOG(lg) << message.buffer;
ss412g1a79bdf2019-10-24 12:03:05 +03002842 }
ss412g1a79bdf2019-10-24 12:03:05 +03002843}
2844
2845
2846/**
2847 * take RMR error code to string
2848 * @param state
2849 * @return
2850 */
2851string translateRmrErrorMessages(int state) {
2852 string str = {};
2853 switch (state) {
2854 case RMR_OK:
2855 str = "RMR_OK - state is good";
2856 break;
2857 case RMR_ERR_BADARG:
aa7133@att.com409f2232020-10-29 13:06:45 +03002858 str = "RMR_ERR_BADARG - argument passed to function was unusable";
ss412g1a79bdf2019-10-24 12:03:05 +03002859 break;
2860 case RMR_ERR_NOENDPT:
2861 str = "RMR_ERR_NOENDPT - send//call could not find an endpoint based on msg type";
2862 break;
2863 case RMR_ERR_EMPTY:
2864 str = "RMR_ERR_EMPTY - msg received had no payload; attempt to send an empty message";
2865 break;
2866 case RMR_ERR_NOHDR:
2867 str = "RMR_ERR_NOHDR - message didn't contain a valid header";
2868 break;
2869 case RMR_ERR_SENDFAILED:
2870 str = "RMR_ERR_SENDFAILED - send failed; errno has nano reason";
2871 break;
2872 case RMR_ERR_CALLFAILED:
2873 str = "RMR_ERR_CALLFAILED - unable to send call() message";
2874 break;
2875 case RMR_ERR_NOWHOPEN:
2876 str = "RMR_ERR_NOWHOPEN - no wormholes are open";
2877 break;
2878 case RMR_ERR_WHID:
2879 str = "RMR_ERR_WHID - wormhole id was invalid";
2880 break;
2881 case RMR_ERR_OVERFLOW:
2882 str = "RMR_ERR_OVERFLOW - operation would have busted through a buffer/field size";
2883 break;
2884 case RMR_ERR_RETRY:
2885 str = "RMR_ERR_RETRY - request (send/call/rts) failed, but caller should retry (EAGAIN for wrappers)";
2886 break;
2887 case RMR_ERR_RCVFAILED:
2888 str = "RMR_ERR_RCVFAILED - receive failed (hard error)";
2889 break;
2890 case RMR_ERR_TIMEOUT:
2891 str = "RMR_ERR_TIMEOUT - message processing call timed out";
2892 break;
2893 case RMR_ERR_UNSET:
2894 str = "RMR_ERR_UNSET - the message hasn't been populated with a transport buffer";
2895 break;
2896 case RMR_ERR_TRUNC:
2897 str = "RMR_ERR_TRUNC - received message likely truncated";
2898 break;
2899 case RMR_ERR_INITFAILED:
2900 str = "RMR_ERR_INITFAILED - initialisation of something (probably message) failed";
2901 break;
2902 case RMR_ERR_NOTSUPP:
2903 str = "RMR_ERR_NOTSUPP - the request is not supported, or RMr was not initialised for the request";
2904 break;
2905 default:
2906 char buf[128]{};
2907 snprintf(buf, sizeof buf, "UNDOCUMENTED RMR_ERR : %d", state);
2908 str = buf;
2909 break;
2910 }
2911 return str;
2912}