blob: 2d7e76b4b27627099f71f9fdc1b073311cfb934b [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
36
aa7133@att.com7b437f72020-03-30 13:25:54 +030037
aa7133@att.com84bd3342020-03-16 18:04:57 +020038using namespace std;
39//using namespace std::placeholders;
ss412g3bac2da2020-01-05 11:52:19 +020040using namespace boost::filesystem;
ss412g4aeeda22020-06-16 14:12:41 +030041using namespace prometheus;
42
ss412g3bac2da2020-01-05 11:52:19 +020043
ss412g1a79bdf2019-10-24 12:03:05 +030044//#ifdef __cplusplus
45//extern "C"
46//{
47//#endif
48
ss412g3bac2da2020-01-05 11:52:19 +020049// need to expose without the include of gcov
50extern "C" void __gcov_flush(void);
dhirajverma55e755b2021-06-29 02:01:47 +053051#define LOG_FILE_CONFIG_MAP "CONFIG_MAP_NAME"
ss412g3bac2da2020-01-05 11:52:19 +020052
53static void catch_function(int signal) {
54 __gcov_flush();
55 exit(signal);
56}
57
58
ss412g1a79bdf2019-10-24 12:03:05 +030059BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger, src::logger_mt)
60
61boost::shared_ptr<sinks::synchronous_sink<sinks::text_file_backend>> boostLogger;
ss412g3bac2da2020-01-05 11:52:19 +020062double cpuClock = 0.0;
aa7133@att.com41fcb3f2020-11-29 13:48:25 +020063bool jsonTrace = false;
ss412g1a79bdf2019-10-24 12:03:05 +030064
dhirajverma55e755b2021-06-29 02:01:47 +053065
66static int enable_log_change_notify(const char* fileName)
67{
68 int ret = -1;
69 struct stat fileInfo;
70 if ( lstat(fileName,&fileInfo) == 0 )
71 {
72 ret = register_log_change_notify(fileName);
73 }
74 return ret;
75}
76
77
78static int register_log_change_notify(const char *fileName)
79{
80 pthread_attr_t cb_attr;
81 pthread_t tid;
82 pthread_attr_init(&cb_attr);
83 pthread_attr_setdetachstate(&cb_attr,PTHREAD_CREATE_DETACHED);
84 return pthread_create(&tid, &cb_attr,&monitor_loglevel_change_handler,(void *)strdup(fileName));
85}
86
87
88static void * monitor_loglevel_change_handler(void* arg)
89{
90 char *fileName = (char*) arg;
91 int ifd; // the inotify file des
92 int wfd; // the watched file des
93 ssize_t n = 0;
94 char rbuf[4096]; // large read buffer as the event is var len
95 fd_set fds;
96 int res = 0;
97 struct timeval timeout;
98 char* dname=NULL; // directory name
99 char* bname = NULL; // basename
100 char* tok=NULL;
101 char* log_level=NULL;
102
103 dname = strdup( fileName); // defrock the file name into dir and basename
104 if( (tok = strrchr( dname, '/' )) != NULL ) {
105 *tok = '\0';
106 bname = strdup( tok+1 );
107 }
108
109
110 ifd = inotify_init1( 0 ); // initialise watcher setting blocking read (no option)
111 if( ifd < 0 ) {
112 fprintf( stderr, "### ERR ### unable to initialise file watch %s\n", strerror( errno ) );
113 } else {
114 wfd = inotify_add_watch( ifd, dname, IN_MOVED_TO | IN_CLOSE_WRITE ); // we only care about close write changes
115
116 if( wfd < 0 ) {
117 fprintf( stderr, "### ERR ### unable to add watch on config file %s: %s\n", fileName, strerror( errno ) );
118 } else {
119
120
121 memset( &timeout, 0, sizeof(timeout) );
122 while( 1 ) {
123 FD_ZERO (&fds);
124 FD_SET (ifd, &fds);
125 timeout.tv_sec=1;
126 res = select (ifd + 1, &fds, NULL, NULL, &timeout);
127 if(res)
128 {
129 n = read( ifd, rbuf, sizeof( rbuf ) ); // read the event
130 if( n < 0 ) {
131 if( errno == EAGAIN ) {
132 } else {
133 fprintf( stderr, "### CRIT ### config listener read err: %s\n", strerror( errno ) );
134 }
135 continue;
136 }
137
138 //Retrieving Log Level from configmap by parsing configmap file
139 log_level = parse_file(fileName);
140 update_mdc_log_level_severity(log_level); //setting log level
141 free(log_level);
142 }
143 }
144 inotify_rm_watch(ifd,wfd);
145 }
146 close(ifd);
147 }
148 free(bname);
149 free(dname);
150
151 pthread_exit(NULL);
152}
153
154void update_mdc_log_level_severity(char* log_level)
155{
156 mdclog_severity_t level = MDCLOG_ERR;
157
158 if(log_level == NULL)
159 {
160 printf("### ERR ### Invalid Log-Level Configuration in ConfigMap, Default Log-Level Applied: %d\n",level);
161 }
162 else if(strcasecmp(log_level,"1")==0)
163 {
164 level = MDCLOG_ERR;
165 }
166 else if(strcasecmp(log_level,"2")==0)
167 {
168 level = MDCLOG_WARN;
169 }
170 else if(strcasecmp(log_level,"3")==0)
171 {
172 level = MDCLOG_INFO;
173 }
174 else if(strcasecmp(log_level,"4")==0)
175 {
176 level = MDCLOG_DEBUG;
177 }
178
179 mdclog_level_set(level);
180}
181static char* parse_file(char* filename)
182{
183 char *token=NULL;
184 char *search = ": ";
185 char *string_match = "log-level";
186 bool found = false;
187 FILE *file = fopen ( filename, "r" );
188 if ( file != NULL )
189 {
190 char line [ 128 ];
191 while ( fgets ( line, sizeof line, file ) != NULL )
192 {
193 token = strtok(line, search);
194 if(strcmp(token,string_match)==0)
195 {
196 found = true;
197 token = strtok(NULL, search);
198 token = strtok(token, "\n");//removing newline if any
199 break;
200 }
201 }
202 fclose ( file );
203 }
204 if(found)
205 return(strdup(token));
206 else
207 return(NULL);
208}
209
210char *read_env_param(const char*envkey)
211{
212 if(envkey)
213 {
214 char *value = getenv(envkey);
215 if(value)
216 return strdup(value);
217 }
218 return NULL;
219}
220
221void dynamic_log_level_change()
222{
223 char *logFile_Name = read_env_param(LOG_FILE_CONFIG_MAP);
224 char* log_level_init=NULL;
225 if(logFile_Name)
226 {
227 log_level_init = parse_file(logFile_Name);
228 update_mdc_log_level_severity(log_level_init); //setting log level
229 free(log_level_init);
230
231 }
232 enable_log_change_notify(logFile_Name);
233 free(logFile_Name);
234
235}
236
ss412g1a79bdf2019-10-24 12:03:05 +0300237void init_log() {
dhirajverma55e755b2021-06-29 02:01:47 +0530238 int log_change_monitor = 0;
ss412g1a79bdf2019-10-24 12:03:05 +0300239 mdclog_attr_t *attr;
240 mdclog_attr_init(&attr);
241 mdclog_attr_set_ident(attr, "E2Terminator");
242 mdclog_init(attr);
dhirajverma5f132832021-06-17 17:27:54 +0530243 if(mdclog_format_initialize(log_change_monitor)!=0)
244 mdclog_write(MDCLOG_ERR, "Failed to intialize MDC log format !!!");
dhirajverma55e755b2021-06-29 02:01:47 +0530245 dynamic_log_level_change();
ss412g1a79bdf2019-10-24 12:03:05 +0300246 mdclog_attr_destroy(attr);
247}
ss412g3bac2da2020-01-05 11:52:19 +0200248auto start_time = std::chrono::high_resolution_clock::now();
249typedef std::chrono::duration<double, std::ratio<1,1>> seconds_t;
ss412g1a79bdf2019-10-24 12:03:05 +0300250
ss412g3bac2da2020-01-05 11:52:19 +0200251double age() {
252 return seconds_t(std::chrono::high_resolution_clock::now() - start_time).count();
253}
254
aa7133@att.com409f2232020-10-29 13:06:45 +0300255double approx_CPU_MHz(unsigned sleepTime) {
ss412g3bac2da2020-01-05 11:52:19 +0200256 using namespace std::chrono_literals;
257 uint32_t aux = 0;
258 uint64_t cycles_start = rdtscp(aux);
259 double time_start = age();
aa7133@att.com409f2232020-10-29 13:06:45 +0300260 std::this_thread::sleep_for(sleepTime * 1ms);
ss412g3bac2da2020-01-05 11:52:19 +0200261 uint64_t elapsed_cycles = rdtscp(aux) - cycles_start;
262 double elapsed_time = age() - time_start;
263 return elapsed_cycles / elapsed_time;
264}
ss412g1a79bdf2019-10-24 12:03:05 +0300265
266//std::atomic<int64_t> rmrCounter{0};
267std::atomic<int64_t> num_of_messages{0};
ss412g3bac2da2020-01-05 11:52:19 +0200268std::atomic<int64_t> num_of_XAPP_messages{0};
ss412g1a79bdf2019-10-24 12:03:05 +0300269static long transactionCounter = 0;
270
aa7133@att.com84bd3342020-03-16 18:04:57 +0200271int buildListeningPort(sctp_params_t &sctpParams) {
aa7133@att.comf211f192020-06-23 12:50:10 +0300272 sctpParams.listenFD = socket(AF_INET6, SOCK_STREAM, IPPROTO_SCTP);
273 if (sctpParams.listenFD <= 0) {
274 mdclog_write(MDCLOG_ERR, "Error Opening socket, %s", strerror(errno));
275 return -1;
276 }
277
aa7133@att.com409f2232020-10-29 13:06:45 +0300278 struct sockaddr_in6 serverAddress {};
279 serverAddress.sin6_family = AF_INET6;
280 serverAddress.sin6_addr = in6addr_any;
281 serverAddress.sin6_port = htons(sctpParams.sctpPort);
282 if (bind(sctpParams.listenFD, (SA *)&serverAddress, sizeof(serverAddress)) < 0 ) {
aa7133@att.comf211f192020-06-23 12:50:10 +0300283 mdclog_write(MDCLOG_ERR, "Error binding port %d. %s", sctpParams.sctpPort, strerror(errno));
aa7133@att.com84bd3342020-03-16 18:04:57 +0200284 return -1;
285 }
286 if (setSocketNoBlocking(sctpParams.listenFD) == -1) {
287 //mdclog_write(MDCLOG_ERR, "Error binding. %s", strerror(errno));
288 return -1;
289 }
290 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300291 struct sockaddr_in6 clientAddress {};
292 socklen_t len = sizeof(clientAddress);
293 getsockname(sctpParams.listenFD, (SA *)&clientAddress, &len);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200294 char buff[1024] {};
aa7133@att.com409f2232020-10-29 13:06:45 +0300295 inet_ntop(AF_INET6, &clientAddress.sin6_addr, buff, sizeof(buff));
296 mdclog_write(MDCLOG_DEBUG, "My address: %s, port %d\n", buff, htons(clientAddress.sin6_port));
ss412g1a79bdf2019-10-24 12:03:05 +0300297 }
298
aa7133@att.com84bd3342020-03-16 18:04:57 +0200299 if (listen(sctpParams.listenFD, SOMAXCONN) < 0) {
300 mdclog_write(MDCLOG_ERR, "Error listening. %s\n", strerror(errno));
301 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200302 }
aa7133@att.com84bd3342020-03-16 18:04:57 +0200303 struct epoll_event event {};
304 event.events = EPOLLIN | EPOLLET;
305 event.data.fd = sctpParams.listenFD;
306
307 // add listening port to epoll
308 if (epoll_ctl(sctpParams.epoll_fd, EPOLL_CTL_ADD, sctpParams.listenFD, &event)) {
309 printf("Failed to add descriptor to epoll\n");
310 mdclog_write(MDCLOG_ERR, "Failed to add descriptor to epoll. %s\n", strerror(errno));
311 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200312 }
313
aa7133@att.com84bd3342020-03-16 18:04:57 +0200314 return 0;
315}
ss412g3bac2da2020-01-05 11:52:19 +0200316
aa7133@att.com84bd3342020-03-16 18:04:57 +0200317int buildConfiguration(sctp_params_t &sctpParams) {
ss412g3bac2da2020-01-05 11:52:19 +0200318 path p = (sctpParams.configFilePath + "/" + sctpParams.configFileName).c_str();
319 if (exists(p)) {
320 const int size = 2048;
321 auto fileSize = file_size(p);
322 if (fileSize > size) {
323 mdclog_write(MDCLOG_ERR, "File %s larger than %d", p.string().c_str(), size);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200324 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200325 }
326 } else {
327 mdclog_write(MDCLOG_ERR, "Configuration File %s not exists", p.string().c_str());
aa7133@att.com84bd3342020-03-16 18:04:57 +0200328 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200329 }
330
ss412g3bac2da2020-01-05 11:52:19 +0200331 ReadConfigFile conf;
332 if (conf.openConfigFile(p.string()) == -1) {
333 mdclog_write(MDCLOG_ERR, "Filed to open config file %s, %s",
334 p.string().c_str(), strerror(errno));
aa7133@att.com84bd3342020-03-16 18:04:57 +0200335 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200336 }
337 int rmrPort = conf.getIntValue("nano");
338 if (rmrPort == -1) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300339 mdclog_write(MDCLOG_ERR, "illegal RMR port ");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200340 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200341 }
342 sctpParams.rmrPort = (uint16_t)rmrPort;
343 snprintf(sctpParams.rmrAddress, sizeof(sctpParams.rmrAddress), "%d", (int) (sctpParams.rmrPort));
dhirajverma5f132832021-06-17 17:27:54 +0530344 auto tmpStr = conf.getStringValue("volume");
ss412g3bac2da2020-01-05 11:52:19 +0200345 if (tmpStr.length() == 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300346 mdclog_write(MDCLOG_ERR, "illegal volume.");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200347 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200348 }
349
ss412g1a79bdf2019-10-24 12:03:05 +0300350 char tmpLogFilespec[VOLUME_URL_SIZE];
351 tmpLogFilespec[0] = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200352 sctpParams.volume[0] = 0;
353 snprintf(sctpParams.volume, VOLUME_URL_SIZE, "%s", tmpStr.c_str());
354 // copy the name to temp file as well
355 snprintf(tmpLogFilespec, VOLUME_URL_SIZE, "%s", tmpStr.c_str());
356
357
358 // define the file name in the tmp directory under the volume
359 strcat(tmpLogFilespec,"/tmp/E2Term_%Y-%m-%d_%H-%M-%S.%N.tmpStr");
360
361 sctpParams.myIP = conf.getStringValue("local-ip");
362 if (sctpParams.myIP.length() == 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300363 mdclog_write(MDCLOG_ERR, "illegal local-ip.");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200364 return -1;
ss412g1a79bdf2019-10-24 12:03:05 +0300365 }
366
aa7133@att.combe92d712020-03-18 17:30:39 +0200367 int sctpPort = conf.getIntValue("sctp-port");
368 if (sctpPort == -1) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300369 mdclog_write(MDCLOG_ERR, "illegal SCTP port ");
aa7133@att.combe92d712020-03-18 17:30:39 +0200370 return -1;
371 }
372 sctpParams.sctpPort = (uint16_t)sctpPort;
373
aa7133@att.com84bd3342020-03-16 18:04:57 +0200374 sctpParams.fqdn = conf.getStringValue("external-fqdn");
375 if (sctpParams.fqdn.length() == 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300376 mdclog_write(MDCLOG_ERR, "illegal external-fqdn");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200377 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200378 }
ss412g1a79bdf2019-10-24 12:03:05 +0300379
aa7133@att.com342843e2020-01-26 12:53:34 +0200380 std::string pod = conf.getStringValue("pod_name");
dhirajverma6cf28612021-06-23 04:53:34 -0400381#ifndef UNIT_TEST
aa7133@att.com342843e2020-01-26 12:53:34 +0200382 if (pod.length() == 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300383 mdclog_write(MDCLOG_ERR, "illegal pod_name in config file");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200384 return -1;
aa7133@att.com342843e2020-01-26 12:53:34 +0200385 }
386 auto *podName = getenv(pod.c_str());
387 if (podName == nullptr) {
aa7133@att.com54885fe2020-10-29 17:24:49 +0300388 mdclog_write(MDCLOG_ERR, "illegal pod_name or environment variable not exists : %s", pod.c_str());
aa7133@att.com84bd3342020-03-16 18:04:57 +0200389 return -1;
aa7133@att.com342843e2020-01-26 12:53:34 +0200390
391 } else {
392 sctpParams.podName.assign(podName);
393 if (sctpParams.podName.length() == 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300394 mdclog_write(MDCLOG_ERR, "illegal pod_name");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200395 return -1;
aa7133@att.com342843e2020-01-26 12:53:34 +0200396 }
397 }
dhirajverma6cf28612021-06-23 04:53:34 -0400398#endif
ss412g3bac2da2020-01-05 11:52:19 +0200399 tmpStr = conf.getStringValue("trace");
400 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
401 if ((tmpStr.compare("start")) == 0) {
402 mdclog_write(MDCLOG_INFO, "Trace set to: start");
403 sctpParams.trace = true;
404 } else if ((tmpStr.compare("stop")) == 0) {
405 mdclog_write(MDCLOG_INFO, "Trace set to: stop");
406 sctpParams.trace = false;
aa7133@att.com41fcb3f2020-11-29 13:48:25 +0200407 } else {
408 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 +0200409 sctpParams.trace = false;
ss412g3bac2da2020-01-05 11:52:19 +0200410 }
411 jsonTrace = sctpParams.trace;
ss412g1a79bdf2019-10-24 12:03:05 +0300412
ss412g4aeeda22020-06-16 14:12:41 +0300413 sctpParams.epollTimeOut = -1;
ss412g4aeeda22020-06-16 14:12:41 +0300414
415 tmpStr = conf.getStringValue("prometheusPort");
416 if (tmpStr.length() != 0) {
417 sctpParams.prometheusPort = tmpStr;
418 }
419
aa7133@att.combe92d712020-03-18 17:30:39 +0200420 sctpParams.ka_message_length = snprintf(sctpParams.ka_message, KA_MESSAGE_SIZE, "{\"address\": \"%s:%d\","
aa7133@att.com96c9b882020-03-25 14:50:40 +0200421 "\"fqdn\": \"%s\","
422 "\"pod_name\": \"%s\"}",
aa7133@att.com342843e2020-01-26 12:53:34 +0200423 (const char *)sctpParams.myIP.c_str(),
424 sctpParams.rmrPort,
425 sctpParams.fqdn.c_str(),
426 sctpParams.podName.c_str());
427
ss412g1a79bdf2019-10-24 12:03:05 +0300428 if (mdclog_level_get() >= MDCLOG_INFO) {
dhirajverma5f132832021-06-17 17:27:54 +0530429 mdclog_write(MDCLOG_DEBUG,"RMR Port: %s", to_string(sctpParams.rmrPort).c_str());
430 mdclog_write(MDCLOG_DEBUG,"LogLevel: %s", to_string(sctpParams.logLevel).c_str());
431 mdclog_write(MDCLOG_DEBUG,"volume: %s", sctpParams.volume);
432 mdclog_write(MDCLOG_DEBUG,"tmpLogFilespec: %s", tmpLogFilespec);
433 mdclog_write(MDCLOG_DEBUG,"my ip: %s", sctpParams.myIP.c_str());
434 mdclog_write(MDCLOG_DEBUG,"pod name: %s", sctpParams.podName.c_str());
ss412g1a79bdf2019-10-24 12:03:05 +0300435
aa7133@att.com342843e2020-01-26 12:53:34 +0200436 mdclog_write(MDCLOG_INFO, "running parameters for instance : %s", sctpParams.ka_message);
ss412g1a79bdf2019-10-24 12:03:05 +0300437 }
ss412g1a79bdf2019-10-24 12:03:05 +0300438
439 // Files written to the current working directory
440 boostLogger = logging::add_file_log(
ss412g3bac2da2020-01-05 11:52:19 +0200441 keywords::file_name = tmpLogFilespec, // to temp directory
ss412g1a79bdf2019-10-24 12:03:05 +0300442 keywords::rotation_size = 10 * 1024 * 1024,
443 keywords::time_based_rotation = sinks::file::rotation_at_time_interval(posix_time::hours(1)),
444 keywords::format = "%Message%"
ss412g3bac2da2020-01-05 11:52:19 +0200445 //keywords::format = "[%TimeStamp%]: %Message%" // use each tmpStr with time stamp
ss412g1a79bdf2019-10-24 12:03:05 +0300446 );
447
aa7133@att.com54885fe2020-10-29 17:24:49 +0300448 // Setup a destination folder for collecting rotated (closed) files --since the same volume can use rename()
ss412g1a79bdf2019-10-24 12:03:05 +0300449 boostLogger->locked_backend()->set_file_collector(sinks::file::make_collector(
ss412g3bac2da2020-01-05 11:52:19 +0200450 keywords::target = sctpParams.volume
ss412g1a79bdf2019-10-24 12:03:05 +0300451 ));
452
453 // Upon restart, scan the directory for files matching the file_name pattern
454 boostLogger->locked_backend()->scan_for_files();
455
ss412g3bac2da2020-01-05 11:52:19 +0200456 // Enable auto-flushing after each tmpStr record written
ss412g1a79bdf2019-10-24 12:03:05 +0300457 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com84bd3342020-03-16 18:04:57 +0200458 boostLogger->locked_backend()->auto_flush(true);
459 }
460
461 return 0;
462}
463
aa7133@att.coma4c80012020-06-24 16:36:47 +0300464void startPrometheus(sctp_params_t &sctpParams) {
465 sctpParams.prometheusFamily = &BuildCounter()
466 .Name("E2T")
467 .Help("E2T message counter")
468 .Labels({{"POD_NAME", sctpParams.podName}})
469 .Register(*sctpParams.prometheusRegistry);
aa7133@att.com16c799b2020-03-24 18:01:51 +0200470
aa7133@att.com54885fe2020-10-29 17:24:49 +0300471 string prometheusPath = sctpParams.prometheusPort + "," + "[::]:" + sctpParams.prometheusPort;
aa7133@att.comb92748d2020-06-29 11:52:30 +0300472 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com54885fe2020-10-29 17:24:49 +0300473 mdclog_write(MDCLOG_DEBUG, "Start Prometheus Pull mode on %s", prometheusPath.c_str());
aa7133@att.coma4c80012020-06-24 16:36:47 +0300474 }
aa7133@att.com54885fe2020-10-29 17:24:49 +0300475 sctpParams.prometheusExposer = new Exposer(prometheusPath, 1);
aa7133@att.comb92748d2020-06-29 11:52:30 +0300476 sctpParams.prometheusExposer->RegisterCollectable(sctpParams.prometheusRegistry);
aa7133@att.coma4c80012020-06-24 16:36:47 +0300477}
dhirajverma31768a92021-05-06 12:38:49 +0530478#ifndef UNIT_TEST
dhirajverma5f132832021-06-17 17:27:54 +0530479
aa7133@att.com84bd3342020-03-16 18:04:57 +0200480int main(const int argc, char **argv) {
481 sctp_params_t sctpParams;
aa7133@att.com84bd3342020-03-16 18:04:57 +0200482 {
483 std::random_device device{};
484 std::mt19937 generator(device());
485 std::uniform_int_distribution<long> distribution(1, (long) 1e12);
486 transactionCounter = distribution(generator);
487 }
488
489// uint64_t st = 0;
490// uint32_t aux1 = 0;
491// st = rdtscp(aux1);
492
493 unsigned num_cpus = std::thread::hardware_concurrency();
494 init_log();
aa7133@att.com84bd3342020-03-16 18:04:57 +0200495 if (std::signal(SIGINT, catch_function) == SIG_ERR) {
496 mdclog_write(MDCLOG_ERR, "Error initializing SIGINT");
497 exit(1);
498 }
499 if (std::signal(SIGABRT, catch_function)== SIG_ERR) {
500 mdclog_write(MDCLOG_ERR, "Error initializing SIGABRT");
501 exit(1);
502 }
503 if (std::signal(SIGTERM, catch_function)== SIG_ERR) {
504 mdclog_write(MDCLOG_ERR, "Error initializing SIGTERM");
505 exit(1);
506 }
507
508 cpuClock = approx_CPU_MHz(100);
509
510 mdclog_write(MDCLOG_DEBUG, "CPU speed %11.11f", cpuClock);
511
512 auto result = parse(argc, argv, sctpParams);
513
514 if (buildConfiguration(sctpParams) != 0) {
515 exit(-1);
ss412g1a79bdf2019-10-24 12:03:05 +0300516 }
517
ss412g4aeeda22020-06-16 14:12:41 +0300518 //auto registry = std::make_shared<Registry>();
519 sctpParams.prometheusRegistry = std::make_shared<Registry>();
520
aa7133@att.com409f2232020-10-29 13:06:45 +0300521 //sctpParams.prometheusFamily = new Family<Counter>("E2T", "E2T message counter", {{"E", sctpParams.podName}});
ss412g4aeeda22020-06-16 14:12:41 +0300522
aa7133@att.coma4c80012020-06-24 16:36:47 +0300523 startPrometheus(sctpParams);
ss412g4aeeda22020-06-16 14:12:41 +0300524
ss412g1a79bdf2019-10-24 12:03:05 +0300525 // start epoll
ss412g3bac2da2020-01-05 11:52:19 +0200526 sctpParams.epoll_fd = epoll_create1(0);
527 if (sctpParams.epoll_fd == -1) {
ss412g1a79bdf2019-10-24 12:03:05 +0300528 mdclog_write(MDCLOG_ERR, "failed to open epoll descriptor");
529 exit(-1);
530 }
aa7133@att.combe92d712020-03-18 17:30:39 +0200531 getRmrContext(sctpParams);
ss412g3bac2da2020-01-05 11:52:19 +0200532 if (sctpParams.rmrCtx == nullptr) {
533 close(sctpParams.epoll_fd);
ss412g1a79bdf2019-10-24 12:03:05 +0300534 exit(-1);
535 }
536
ss412g3bac2da2020-01-05 11:52:19 +0200537 if (buildInotify(sctpParams) == -1) {
538 close(sctpParams.rmrListenFd);
539 rmr_close(sctpParams.rmrCtx);
540 close(sctpParams.epoll_fd);
541 exit(-1);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200542 }
543
544 if (buildListeningPort(sctpParams) != 0) {
545 close(sctpParams.rmrListenFd);
546 rmr_close(sctpParams.rmrCtx);
547 close(sctpParams.epoll_fd);
548 exit(-1);
549 }
ss412g3bac2da2020-01-05 11:52:19 +0200550
551 sctpParams.sctpMap = new mapWrapper();
ss412g1a79bdf2019-10-24 12:03:05 +0300552
553 std::vector<std::thread> threads(num_cpus);
554// std::vector<std::thread> threads;
555
aa7133@att.com24e9fa42020-11-13 20:32:53 +0200556 num_cpus = 3;
ss412g1a79bdf2019-10-24 12:03:05 +0300557 for (unsigned int i = 0; i < num_cpus; i++) {
ss412g3bac2da2020-01-05 11:52:19 +0200558 threads[i] = std::thread(listener, &sctpParams);
ss412g1a79bdf2019-10-24 12:03:05 +0300559
560 cpu_set_t cpuset;
561 CPU_ZERO(&cpuset);
562 CPU_SET(i, &cpuset);
563 int rc = pthread_setaffinity_np(threads[i].native_handle(), sizeof(cpu_set_t), &cpuset);
564 if (rc != 0) {
565 mdclog_write(MDCLOG_ERR, "Error calling pthread_setaffinity_np: %d", rc);
566 }
ss412g1a79bdf2019-10-24 12:03:05 +0300567 }
568
aa7133@att.com16c799b2020-03-24 18:01:51 +0200569
ss412g3bac2da2020-01-05 11:52:19 +0200570 //loop over term_init until first message from xApp
571 handleTermInit(sctpParams);
572
573 for (auto &t : threads) {
574 t.join();
575 }
576
ss412g3bac2da2020-01-05 11:52:19 +0200577 return 0;
578}
dhirajverma60f8bef2021-06-29 07:22:58 -0400579#endif
ss412g3bac2da2020-01-05 11:52:19 +0200580void handleTermInit(sctp_params_t &sctpParams) {
581 sendTermInit(sctpParams);
ss412g1a79bdf2019-10-24 12:03:05 +0300582 //send to e2 manager init of e2 term
583 //E2_TERM_INIT
ss412g1a79bdf2019-10-24 12:03:05 +0300584
ss412g3bac2da2020-01-05 11:52:19 +0200585 int count = 0;
586 while (true) {
587 auto xappMessages = num_of_XAPP_messages.load(std::memory_order_acquire);
588 if (xappMessages > 0) {
589 if (mdclog_level_get() >= MDCLOG_INFO) {
aa7133@att.com54885fe2020-10-29 17:24:49 +0300590 mdclog_write(MDCLOG_INFO, "Got a message from some application, stop sending E2_TERM_INIT");
ss412g3bac2da2020-01-05 11:52:19 +0200591 }
592 return;
593 }
594 usleep(100000);
595 count++;
596 if (count % 1000 == 0) {
597 mdclog_write(MDCLOG_ERR, "GOT No messages from any xApp");
598 sendTermInit(sctpParams);
599 }
600 }
601}
602
603void sendTermInit(sctp_params_t &sctpParams) {
604 rmr_mbuf_t *msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.ka_message_length);
ss412g1a79bdf2019-10-24 12:03:05 +0300605 auto count = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200606 while (true) {
ss412g1a79bdf2019-10-24 12:03:05 +0300607 msg->mtype = E2_TERM_INIT;
608 msg->state = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200609 rmr_bytes2payload(msg, (unsigned char *)sctpParams.ka_message, sctpParams.ka_message_length);
ss412g1a79bdf2019-10-24 12:03:05 +0300610 static unsigned char tx[32];
611 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
612 rmr_bytes2xact(msg, tx, txLen);
ss412g3bac2da2020-01-05 11:52:19 +0200613 msg = rmr_send_msg(sctpParams.rmrCtx, msg);
ss412g1a79bdf2019-10-24 12:03:05 +0300614 if (msg == nullptr) {
aa7133@att.comc9575aa2020-04-02 11:40:09 +0300615 msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.ka_message_length);
ss412g1a79bdf2019-10-24 12:03:05 +0300616 } else if (msg->state == 0) {
ss412g1a79bdf2019-10-24 12:03:05 +0300617 rmr_free_msg(msg);
ss412g3bac2da2020-01-05 11:52:19 +0200618 if (mdclog_level_get() >= MDCLOG_INFO) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300619 mdclog_write(MDCLOG_INFO, "E2_TERM_INIT successfully sent ");
ss412g3bac2da2020-01-05 11:52:19 +0200620 }
621 return;
ss412g1a79bdf2019-10-24 12:03:05 +0300622 } else {
623 if (count % 100 == 0) {
aa7133@att.com11b17d42020-03-22 11:35:16 +0200624 mdclog_write(MDCLOG_ERR, "Error sending E2_TERM_INIT cause : %s ", translateRmrErrorMessages(msg->state).c_str());
ss412g1a79bdf2019-10-24 12:03:05 +0300625 }
626 sleep(1);
627 }
628 count++;
629 }
ss412g3bac2da2020-01-05 11:52:19 +0200630}
631
632/**
633 *
634 * @param argc
635 * @param argv
636 * @param sctpParams
637 * @return
638 */
639cxxopts::ParseResult parse(int argc, char *argv[], sctp_params_t &sctpParams) {
640 cxxopts::Options options(argv[0], "e2 term help");
641 options.positional_help("[optional args]").show_positional_help();
642 options.allow_unrecognised_options().add_options()
643 ("p,path", "config file path", cxxopts::value<std::string>(sctpParams.configFilePath)->default_value("config"))
644 ("f,file", "config file name", cxxopts::value<std::string>(sctpParams.configFileName)->default_value("config.conf"))
645 ("h,help", "Print help");
646
aa7133@att.comde40d1c2020-08-26 16:19:20 +0300647 auto result = options.parse(argc, (const char **&)argv);
ss412g3bac2da2020-01-05 11:52:19 +0200648
649 if (result.count("help")) {
650 std::cout << options.help({""}) << std::endl;
651 exit(0);
652 }
653 return result;
654}
655
656/**
657 *
658 * @param sctpParams
659 * @return -1 failed 0 success
660 */
661int buildInotify(sctp_params_t &sctpParams) {
662 sctpParams.inotifyFD = inotify_init1(IN_NONBLOCK);
663 if (sctpParams.inotifyFD == -1) {
664 mdclog_write(MDCLOG_ERR, "Failed to init inotify (inotify_init1) %s", strerror(errno));
ss412g3bac2da2020-01-05 11:52:19 +0200665 return -1;
ss412g1a79bdf2019-10-24 12:03:05 +0300666 }
667
ss412g3bac2da2020-01-05 11:52:19 +0200668 sctpParams.inotifyWD = inotify_add_watch(sctpParams.inotifyFD,
aa7133@att.com96c9b882020-03-25 14:50:40 +0200669 (const char *)sctpParams.configFilePath.c_str(),
aa7133@att.com84bd3342020-03-16 18:04:57 +0200670 (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 +0200671 if (sctpParams.inotifyWD == -1) {
672 mdclog_write(MDCLOG_ERR, "Failed to add directory : %s to inotify (inotify_add_watch) %s",
aa7133@att.com96c9b882020-03-25 14:50:40 +0200673 sctpParams.configFilePath.c_str(),
674 strerror(errno));
ss412g3bac2da2020-01-05 11:52:19 +0200675 close(sctpParams.inotifyFD);
676 return -1;
677 }
678
679 struct epoll_event event{};
680 event.events = (EPOLLIN);
681 event.data.fd = sctpParams.inotifyFD;
682 // add listening RMR FD to epoll
683 if (epoll_ctl(sctpParams.epoll_fd, EPOLL_CTL_ADD, sctpParams.inotifyFD, &event)) {
684 mdclog_write(MDCLOG_ERR, "Failed to add inotify FD to epoll");
685 close(sctpParams.inotifyFD);
686 return -1;
687 }
ss412g1a79bdf2019-10-24 12:03:05 +0300688 return 0;
689}
690
691/**
692 *
693 * @param args
694 * @return
695 */
696void listener(sctp_params_t *params) {
ss412g1a79bdf2019-10-24 12:03:05 +0300697 int num_of_SCTP_messages = 0;
ss412g1a79bdf2019-10-24 12:03:05 +0300698 auto totalTime = 0.0;
ss412g1a79bdf2019-10-24 12:03:05 +0300699 std::thread::id this_id = std::this_thread::get_id();
700 //save cout
dhirajverma5f132832021-06-17 17:27:54 +0530701 auto pod_name = std::getenv("POD_NAME");
702 auto container_name = std::getenv("CONTAINER_NAME");
703 auto service_name = std::getenv("SERVICE_NAME");
704 auto host_name = std::getenv("HOST_NAME");
705 auto system_name = std::getenv("SYSTEM_NAME");
706 auto pid = std::to_string(getpid()).c_str();
ss412g1a79bdf2019-10-24 12:03:05 +0300707 streambuf *oldCout = cout.rdbuf();
708 ostringstream memCout;
709 // create new cout
710 cout.rdbuf(memCout.rdbuf());
711 cout << this_id;
712 //return to the normal cout
713 cout.rdbuf(oldCout);
714
715 char tid[32];
716 memcpy(tid, memCout.str().c_str(), memCout.str().length() < 32 ? memCout.str().length() : 31);
717 tid[memCout.str().length()] = 0;
dhirajverma5f132832021-06-17 17:27:54 +0530718 mdclog_mdc_add("SYSTEM_NAME", system_name);
719 mdclog_mdc_add("HOST_NAME", host_name);
720 mdclog_mdc_add("SERVICE_NAME", service_name);
721 mdclog_mdc_add("CONTAINER_NAME", container_name);
722 mdclog_mdc_add("POD_NAME", pod_name);
723 mdclog_mdc_add("PID", pid);
ss412g1a79bdf2019-10-24 12:03:05 +0300724
725 if (mdclog_level_get() >= MDCLOG_DEBUG) {
726 mdclog_write(MDCLOG_DEBUG, "started thread number %s", tid);
727 }
728
729 RmrMessagesBuffer_t rmrMessageBuffer{};
730 //create and init RMR
731 rmrMessageBuffer.rmrCtx = params->rmrCtx;
732
733 auto *events = (struct epoll_event *) calloc(MAXEVENTS, sizeof(struct epoll_event));
734 struct timespec end{0, 0};
735 struct timespec start{0, 0};
736
737 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
738 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
739
ss412g3bac2da2020-01-05 11:52:19 +0200740 memcpy(rmrMessageBuffer.ka_message, params->ka_message, params->ka_message_length);
741 rmrMessageBuffer.ka_message_len = params->ka_message_length;
742 rmrMessageBuffer.ka_message[rmrMessageBuffer.ka_message_len] = 0;
743
744 if (mdclog_level_get() >= MDCLOG_DEBUG) {
745 mdclog_write(MDCLOG_DEBUG, "keep alive message is : %s", rmrMessageBuffer.ka_message);
746 }
747
ss412g1a79bdf2019-10-24 12:03:05 +0300748 ReportingMessages_t message {};
749
aa7133@att.com409f2232020-10-29 13:06:45 +0300750// for (int i = 0; i < MAX_RMR_BUFF_ARRAY; i++) {
aa7133@att.com7c3e4c02020-03-25 16:37:19 +0200751// rmrMessageBuffer.rcvBufferedMessages[i] = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
752// rmrMessageBuffer.sendBufferedMessages[i] = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
753// }
ss412g1a79bdf2019-10-24 12:03:05 +0300754
755 while (true) {
756 if (mdclog_level_get() >= MDCLOG_DEBUG) {
ss412g4aeeda22020-06-16 14:12:41 +0300757 mdclog_write(MDCLOG_DEBUG, "Start EPOLL Wait. Timeout = %d", params->epollTimeOut);
ss412g1a79bdf2019-10-24 12:03:05 +0300758 }
dhirajverma6cf28612021-06-23 04:53:34 -0400759#ifndef UNIT_TEST
ss412g4aeeda22020-06-16 14:12:41 +0300760 auto numOfEvents = epoll_wait(params->epoll_fd, events, MAXEVENTS, params->epollTimeOut);
dhirajverma6cf28612021-06-23 04:53:34 -0400761#else
762 auto numOfEvents = 1;
763#endif
aa7133@att.comb92748d2020-06-29 11:52:30 +0300764 if (numOfEvents == 0) { // time out
765 if (mdclog_level_get() >= MDCLOG_DEBUG) {
766 mdclog_write(MDCLOG_DEBUG, "got epoll timeout");
ss412g1a79bdf2019-10-24 12:03:05 +0300767 }
768 continue;
ss412g4aeeda22020-06-16 14:12:41 +0300769 } else if (numOfEvents < 0) {
770 if (errno == EINTR) {
771 if (mdclog_level_get() >= MDCLOG_DEBUG) {
772 mdclog_write(MDCLOG_DEBUG, "got EINTR : %s", strerror(errno));
773 }
774 continue;
775 }
ss412g1a79bdf2019-10-24 12:03:05 +0300776 mdclog_write(MDCLOG_ERR, "Epoll wait failed, errno = %s", strerror(errno));
777 return;
778 }
779 for (auto i = 0; i < numOfEvents; i++) {
780 if (mdclog_level_get() >= MDCLOG_DEBUG) {
781 mdclog_write(MDCLOG_DEBUG, "handling epoll event %d out of %d", i + 1, numOfEvents);
782 }
783 clock_gettime(CLOCK_MONOTONIC, &message.message.time);
784 start.tv_sec = message.message.time.tv_sec;
785 start.tv_nsec = message.message.time.tv_nsec;
ss412g3bac2da2020-01-05 11:52:19 +0200786
787
ss412g1a79bdf2019-10-24 12:03:05 +0300788 if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP)) {
aa7133@att.combe92d712020-03-18 17:30:39 +0200789 handlepoll_error(events[i], message, rmrMessageBuffer, params);
ss412g1a79bdf2019-10-24 12:03:05 +0300790 } else if (events[i].events & EPOLLOUT) {
aa7133@att.combe92d712020-03-18 17:30:39 +0200791 handleEinprogressMessages(events[i], message, rmrMessageBuffer, params);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200792 } else if (params->listenFD == events[i].data.fd) {
aa7133@att.com6680c3b2020-03-22 12:06:58 +0200793 if (mdclog_level_get() >= MDCLOG_INFO) {
794 mdclog_write(MDCLOG_INFO, "New connection request from sctp network\n");
795 }
aa7133@att.com84bd3342020-03-16 18:04:57 +0200796 // new connection is requested from RAN start build connection
797 while (true) {
798 struct sockaddr in_addr {};
799 socklen_t in_len;
800 char hostBuff[NI_MAXHOST];
801 char portBuff[NI_MAXSERV];
802
803 in_len = sizeof(in_addr);
804 auto *peerInfo = (ConnectedCU_t *)calloc(1, sizeof(ConnectedCU_t));
aa7133@att.com409f2232020-10-29 13:06:45 +0300805 if(peerInfo == nullptr){
swc58d4f5d2020-10-20 15:08:38 +0900806 mdclog_write(MDCLOG_ERR, "calloc failed");
807 break;
808 }
aa7133@att.com84bd3342020-03-16 18:04:57 +0200809 peerInfo->sctpParams = params;
810 peerInfo->fileDescriptor = accept(params->listenFD, &in_addr, &in_len);
811 if (peerInfo->fileDescriptor == -1) {
812 if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
813 /* We have processed all incoming connections. */
814 break;
815 } else {
816 mdclog_write(MDCLOG_ERR, "Accept error, errno = %s", strerror(errno));
817 break;
818 }
819 }
820 if (setSocketNoBlocking(peerInfo->fileDescriptor) == -1) {
821 mdclog_write(MDCLOG_ERR, "setSocketNoBlocking failed to set new connection %s on port %s\n", hostBuff, portBuff);
822 close(peerInfo->fileDescriptor);
823 break;
824 }
825 auto ans = getnameinfo(&in_addr, in_len,
aa7133@att.com96c9b882020-03-25 14:50:40 +0200826 peerInfo->hostName, NI_MAXHOST,
827 peerInfo->portNumber, NI_MAXSERV, (unsigned )((unsigned int)NI_NUMERICHOST | (unsigned int)NI_NUMERICSERV));
aa7133@att.com84bd3342020-03-16 18:04:57 +0200828 if (ans < 0) {
829 mdclog_write(MDCLOG_ERR, "Failed to get info on connection request. %s\n", strerror(errno));
830 close(peerInfo->fileDescriptor);
831 break;
832 }
833 if (mdclog_level_get() >= MDCLOG_DEBUG) {
834 mdclog_write(MDCLOG_DEBUG, "Accepted connection on descriptor %d (host=%s, port=%s)\n", peerInfo->fileDescriptor, peerInfo->hostName, peerInfo->portNumber);
835 }
836 peerInfo->isConnected = false;
837 peerInfo->gotSetup = false;
838 if (addToEpoll(params->epoll_fd,
839 peerInfo,
840 (EPOLLIN | EPOLLET),
841 params->sctpMap, nullptr,
aa7133@att.combe92d712020-03-18 17:30:39 +0200842 0) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +0200843 break;
844 }
aa7133@att.com6680c3b2020-03-22 12:06:58 +0200845 break;
aa7133@att.com84bd3342020-03-16 18:04:57 +0200846 }
ss412g1a79bdf2019-10-24 12:03:05 +0300847 } else if (params->rmrListenFd == events[i].data.fd) {
848 // got message from XAPP
aa7133@att.comde40d1c2020-08-26 16:19:20 +0300849 //num_of_XAPP_messages.fetch_add(1, std::memory_order_release);
ss412g1a79bdf2019-10-24 12:03:05 +0300850 num_of_messages.fetch_add(1, std::memory_order_release);
851 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.comf13529a2020-08-27 11:21:16 +0300852 mdclog_write(MDCLOG_DEBUG, "new RMR message");
ss412g1a79bdf2019-10-24 12:03:05 +0300853 }
aa7133@att.com6680c3b2020-03-22 12:06:58 +0200854 if (receiveXappMessages(params->sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +0300855 rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200856 message.message.time) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +0300857 mdclog_write(MDCLOG_ERR, "Error handling Xapp message");
858 }
ss412g3bac2da2020-01-05 11:52:19 +0200859 } else if (params->inotifyFD == events[i].data.fd) {
860 mdclog_write(MDCLOG_INFO, "Got event from inotify (configuration update)");
861 handleConfigChange(params);
ss412g1a79bdf2019-10-24 12:03:05 +0300862 } else {
863 /* We RMR_ERR_RETRY have data on the fd waiting to be read. Read and display it.
864 * We must read whatever data is available completely, as we are running
865 * in edge-triggered mode and won't get a notification again for the same data. */
866 num_of_messages.fetch_add(1, std::memory_order_release);
867 if (mdclog_level_get() >= MDCLOG_DEBUG) {
868 mdclog_write(MDCLOG_DEBUG, "new message from SCTP, epoll flags are : %0x", events[i].events);
869 }
870 receiveDataFromSctp(&events[i],
871 params->sctpMap,
872 num_of_SCTP_messages,
873 rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200874 message.message.time);
ss412g1a79bdf2019-10-24 12:03:05 +0300875 }
876
877 clock_gettime(CLOCK_MONOTONIC, &end);
878 if (mdclog_level_get() >= MDCLOG_INFO) {
879 totalTime += ((end.tv_sec + 1.0e-9 * end.tv_nsec) -
880 ((double) start.tv_sec + 1.0e-9 * start.tv_nsec));
881 }
882 if (mdclog_level_get() >= MDCLOG_DEBUG) {
883 mdclog_write(MDCLOG_DEBUG, "message handling is %ld seconds %ld nanoseconds",
884 end.tv_sec - start.tv_sec,
885 end.tv_nsec - start.tv_nsec);
886 }
887 }
dhirajverma6cf28612021-06-23 04:53:34 -0400888#ifdef UNIT_TEST
889 break;
890#endif
ss412g1a79bdf2019-10-24 12:03:05 +0300891 }
ss412g1a79bdf2019-10-24 12:03:05 +0300892}
893
894/**
895 *
ss412g3bac2da2020-01-05 11:52:19 +0200896 * @param sctpParams
897 */
898void handleConfigChange(sctp_params_t *sctpParams) {
899 char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event))));
900 const struct inotify_event *event;
901 char *ptr;
dhirajverma60f8bef2021-06-29 07:22:58 -0400902#ifdef UNIT_TEST
903 struct inotify_event tmpEvent;
904#endif
ss412g3bac2da2020-01-05 11:52:19 +0200905 path p = (sctpParams->configFilePath + "/" + sctpParams->configFileName).c_str();
906 auto endlessLoop = true;
907 while (endlessLoop) {
dhirajverma60f8bef2021-06-29 07:22:58 -0400908#ifndef UNIT_TEST
909 auto len = read(sctpParams->inotifyFD, buf, sizeof buf);
910#else
911 auto len=10;
912#endif
ss412g3bac2da2020-01-05 11:52:19 +0200913 if (len == -1) {
914 if (errno != EAGAIN) {
915 mdclog_write(MDCLOG_ERR, "read %s ", strerror(errno));
916 endlessLoop = false;
917 continue;
918 }
919 else {
920 endlessLoop = false;
921 continue;
922 }
923 }
924
925 for (ptr = buf; ptr < buf + len; ptr += sizeof(struct inotify_event) + event->len) {
dhirajverma60f8bef2021-06-29 07:22:58 -0400926#ifndef UNIT_TEST
927 event = (const struct inotify_event *)ptr;
928#else
929 tmpEvent.mask = (uint32_t)IN_CLOSE_WRITE;
930 event = &tmpEvent;
931#endif
ss412g3bac2da2020-01-05 11:52:19 +0200932 if (event->mask & (uint32_t)IN_ISDIR) {
933 continue;
934 }
935
936 // the directory name
937 if (sctpParams->inotifyWD == event->wd) {
938 // not the directory
939 }
940 if (event->len) {
aa7133@att.comdfc1db92020-03-31 17:32:04 +0300941 auto retVal = strcmp(sctpParams->configFileName.c_str(), event->name);
942 if (retVal != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200943 continue;
944 }
945 }
946 // only the file we want
947 if (event->mask & (uint32_t)IN_CLOSE_WRITE) {
aa7133@att.comdfc1db92020-03-31 17:32:04 +0300948 if (mdclog_level_get() >= MDCLOG_INFO) {
949 mdclog_write(MDCLOG_INFO, "Configuration file changed");
950 }
ss412g3bac2da2020-01-05 11:52:19 +0200951 if (exists(p)) {
952 const int size = 2048;
953 auto fileSize = file_size(p);
954 if (fileSize > size) {
955 mdclog_write(MDCLOG_ERR, "File %s larger than %d", p.string().c_str(), size);
956 return;
957 }
958 } else {
959 mdclog_write(MDCLOG_ERR, "Configuration File %s not exists", p.string().c_str());
960 return;
961 }
962
963 ReadConfigFile conf;
964 if (conf.openConfigFile(p.string()) == -1) {
965 mdclog_write(MDCLOG_ERR, "Filed to open config file %s, %s",
966 p.string().c_str(), strerror(errno));
967 return;
968 }
ss412g3bac2da2020-01-05 11:52:19 +0200969 auto tmpStr = conf.getStringValue("loglevel");
970 if (tmpStr.length() == 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300971 mdclog_write(MDCLOG_ERR, "illegal loglevel. Set loglevel to MDCLOG_INFO");
ss412g3bac2da2020-01-05 11:52:19 +0200972 tmpStr = "info";
973 }
974 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
975
976 if ((tmpStr.compare("debug")) == 0) {
977 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_DEBUG");
978 sctpParams->logLevel = MDCLOG_DEBUG;
979 } else if ((tmpStr.compare("info")) == 0) {
980 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_INFO");
981 sctpParams->logLevel = MDCLOG_INFO;
982 } else if ((tmpStr.compare("warning")) == 0) {
983 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_WARN");
984 sctpParams->logLevel = MDCLOG_WARN;
985 } else if ((tmpStr.compare("error")) == 0) {
986 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_ERR");
987 sctpParams->logLevel = MDCLOG_ERR;
988 } else {
aa7133@att.com409f2232020-10-29 13:06:45 +0300989 mdclog_write(MDCLOG_ERR, "illegal loglevel = %s. Set loglevel to MDCLOG_INFO", tmpStr.c_str());
ss412g3bac2da2020-01-05 11:52:19 +0200990 sctpParams->logLevel = MDCLOG_INFO;
991 }
992 mdclog_level_set(sctpParams->logLevel);
ss412g3bac2da2020-01-05 11:52:19 +0200993 tmpStr = conf.getStringValue("trace");
994 if (tmpStr.length() == 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300995 mdclog_write(MDCLOG_ERR, "illegal trace. Set trace to stop");
ss412g3bac2da2020-01-05 11:52:19 +0200996 tmpStr = "stop";
997 }
998
999 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
1000 if ((tmpStr.compare("start")) == 0) {
1001 mdclog_write(MDCLOG_INFO, "Trace set to: start");
1002 sctpParams->trace = true;
1003 } else if ((tmpStr.compare("stop")) == 0) {
1004 mdclog_write(MDCLOG_INFO, "Trace set to: stop");
1005 sctpParams->trace = false;
1006 } else {
1007 mdclog_write(MDCLOG_ERR, "Trace was set to wrong value %s, set to stop", tmpStr.c_str());
1008 sctpParams->trace = false;
1009 }
1010 jsonTrace = sctpParams->trace;
ss412g4aeeda22020-06-16 14:12:41 +03001011
ss412g4aeeda22020-06-16 14:12:41 +03001012
ss412g3bac2da2020-01-05 11:52:19 +02001013 endlessLoop = false;
1014 }
dhirajverma60f8bef2021-06-29 07:22:58 -04001015#ifdef UNIT_TEST
1016 break;
1017#endif
ss412g3bac2da2020-01-05 11:52:19 +02001018 }
1019 }
1020}
1021
1022/**
1023 *
1024 * @param event
1025 * @param message
1026 * @param rmrMessageBuffer
1027 * @param params
ss412g3bac2da2020-01-05 11:52:19 +02001028 */
1029void handleEinprogressMessages(struct epoll_event &event,
1030 ReportingMessages_t &message,
1031 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +02001032 sctp_params_t *params) {
ss412g3bac2da2020-01-05 11:52:19 +02001033 auto *peerInfo = (ConnectedCU_t *)event.data.ptr;
1034 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
1035
1036 mdclog_write(MDCLOG_INFO, "file descriptor %d got EPOLLOUT", peerInfo->fileDescriptor);
1037 auto retVal = 0;
1038 socklen_t retValLen = 0;
1039 auto rc = getsockopt(peerInfo->fileDescriptor, SOL_SOCKET, SO_ERROR, &retVal, &retValLen);
1040 if (rc != 0 || retVal != 0) {
dhirajverma31768a92021-05-06 12:38:49 +05301041#ifndef UNIT_TEST
ss412g3bac2da2020-01-05 11:52:19 +02001042 if (rc != 0) {
1043 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
1044 "%s|Failed SCTP Connection, after EINPROGRESS the getsockopt%s",
1045 peerInfo->enodbName, strerror(errno));
1046 } else if (retVal != 0) {
1047 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
1048 "%s|Failed SCTP Connection after EINPROGRESS, SO_ERROR",
1049 peerInfo->enodbName);
1050 }
1051
1052 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1053 message.message.asnLength = rmrMessageBuffer.sendMessage->len;
1054 mdclog_write(MDCLOG_ERR, "%s", rmrMessageBuffer.sendMessage->payload);
1055 message.message.direction = 'N';
aa7133@att.combe92d712020-03-18 17:30:39 +02001056 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +02001057 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
1058 }
dhirajverma31768a92021-05-06 12:38:49 +05301059#endif
ss412g3bac2da2020-01-05 11:52:19 +02001060 memset(peerInfo->asnData, 0, peerInfo->asnLength);
1061 peerInfo->asnLength = 0;
1062 peerInfo->mtype = 0;
ss412g3bac2da2020-01-05 11:52:19 +02001063 return;
1064 }
1065
1066 peerInfo->isConnected = true;
1067
1068 if (modifyToEpoll(params->epoll_fd, peerInfo, (EPOLLIN | EPOLLET), params->sctpMap, peerInfo->enodbName,
aa7133@att.combe92d712020-03-18 17:30:39 +02001069 peerInfo->mtype) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +02001070 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_MOD");
ss412g3bac2da2020-01-05 11:52:19 +02001071 return;
1072 }
1073
1074 message.message.asndata = (unsigned char *)peerInfo->asnData;
1075 message.message.asnLength = peerInfo->asnLength;
1076 message.message.messageType = peerInfo->mtype;
1077 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
1078 num_of_messages.fetch_add(1, std::memory_order_release);
1079 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1080 mdclog_write(MDCLOG_DEBUG, "send the delayed SETUP/ENDC SETUP to sctp for %s",
1081 message.message.enodbName);
1082 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001083 if (sendSctpMsg(peerInfo, message, params->sctpMap) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +02001084 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1085 mdclog_write(MDCLOG_DEBUG, "Error write to SCTP %s %d", __func__, __LINE__);
1086 }
ss412g3bac2da2020-01-05 11:52:19 +02001087 return;
1088 }
1089
1090 memset(peerInfo->asnData, 0, peerInfo->asnLength);
1091 peerInfo->asnLength = 0;
1092 peerInfo->mtype = 0;
ss412g3bac2da2020-01-05 11:52:19 +02001093}
1094
1095
1096void handlepoll_error(struct epoll_event &event,
1097 ReportingMessages_t &message,
1098 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +02001099 sctp_params_t *params) {
ss412g3bac2da2020-01-05 11:52:19 +02001100 if (event.data.fd != params->rmrListenFd) {
1101 auto *peerInfo = (ConnectedCU_t *)event.data.ptr;
1102 mdclog_write(MDCLOG_ERR, "epoll error, events %0x on fd %d, RAN NAME : %s",
1103 event.events, peerInfo->fileDescriptor, peerInfo->enodbName);
dhirajverma31768a92021-05-06 12:38:49 +05301104#ifndef UNIT_TEST
ss412g3bac2da2020-01-05 11:52:19 +02001105
1106 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
1107 "%s|Failed SCTP Connection",
1108 peerInfo->enodbName);
1109 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1110 message.message.asnLength = rmrMessageBuffer.sendMessage->len;
1111
1112 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
1113 message.message.direction = 'N';
aa7133@att.combe92d712020-03-18 17:30:39 +02001114 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +02001115 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
1116 }
dhirajverma31768a92021-05-06 12:38:49 +05301117#endif
ss412g3bac2da2020-01-05 11:52:19 +02001118 close(peerInfo->fileDescriptor);
aa7133@att.com7b437f72020-03-30 13:25:54 +03001119 params->sctpMap->erase(peerInfo->enodbName);
aa7133@att.combe92d712020-03-18 17:30:39 +02001120 cleanHashEntry((ConnectedCU_t *) event.data.ptr, params->sctpMap);
ss412g3bac2da2020-01-05 11:52:19 +02001121 } else {
1122 mdclog_write(MDCLOG_ERR, "epoll error, events %0x on RMR FD", event.events);
1123 }
ss412g3bac2da2020-01-05 11:52:19 +02001124}
1125/**
1126 *
ss412g1a79bdf2019-10-24 12:03:05 +03001127 * @param socket
1128 * @return
1129 */
1130int setSocketNoBlocking(int socket) {
1131 auto flags = fcntl(socket, F_GETFL, 0);
1132
1133 if (flags == -1) {
ss412g1a79bdf2019-10-24 12:03:05 +03001134 mdclog_write(MDCLOG_ERR, "%s, %s", __FUNCTION__, strerror(errno));
ss412g1a79bdf2019-10-24 12:03:05 +03001135 return -1;
1136 }
1137
1138 flags = (unsigned) flags | (unsigned) O_NONBLOCK;
1139 if (fcntl(socket, F_SETFL, flags) == -1) {
ss412g1a79bdf2019-10-24 12:03:05 +03001140 mdclog_write(MDCLOG_ERR, "%s, %s", __FUNCTION__, strerror(errno));
ss412g1a79bdf2019-10-24 12:03:05 +03001141 return -1;
1142 }
1143
1144 return 0;
1145}
1146
1147/**
1148 *
1149 * @param val
1150 * @param m
ss412g1a79bdf2019-10-24 12:03:05 +03001151 */
aa7133@att.combe92d712020-03-18 17:30:39 +02001152void cleanHashEntry(ConnectedCU_t *val, Sctp_Map_t *m) {
ss412g1a79bdf2019-10-24 12:03:05 +03001153 char *dummy;
1154 auto port = (uint16_t) strtol(val->portNumber, &dummy, 10);
aa7133@att.com84bd3342020-03-16 18:04:57 +02001155 char searchBuff[2048]{};
ss412g1a79bdf2019-10-24 12:03:05 +03001156
1157 snprintf(searchBuff, sizeof searchBuff, "host:%s:%d", val->hostName, port);
1158 m->erase(searchBuff);
1159
1160 m->erase(val->enodbName);
dhirajverma31768a92021-05-06 12:38:49 +05301161#ifndef UNIT_TEST
ss412g1a79bdf2019-10-24 12:03:05 +03001162 free(val);
dhirajverma31768a92021-05-06 12:38:49 +05301163#endif
ss412g1a79bdf2019-10-24 12:03:05 +03001164}
1165
1166/**
1167 *
aa7133@att.com409f2232020-10-29 13:06:45 +03001168 * @param fd file descriptor
ss412g1a79bdf2019-10-24 12:03:05 +03001169 * @param data the asn data to send
1170 * @param len length of the data
1171 * @param enodbName the enodbName as in the map for printing purpose
1172 * @param m map host information
1173 * @param mtype message number
aa7133@att.com409f2232020-10-29 13:06:45 +03001174 * @return 0 success, a negative number on fail
ss412g1a79bdf2019-10-24 12:03:05 +03001175 */
aa7133@att.combe92d712020-03-18 17:30:39 +02001176int sendSctpMsg(ConnectedCU_t *peerInfo, ReportingMessages_t &message, Sctp_Map_t *m) {
ss412g1a79bdf2019-10-24 12:03:05 +03001177 auto loglevel = mdclog_level_get();
1178 int fd = peerInfo->fileDescriptor;
1179 if (loglevel >= MDCLOG_DEBUG) {
1180 mdclog_write(MDCLOG_DEBUG, "Send SCTP message for CU %s, %s",
1181 message.message.enodbName, __FUNCTION__);
1182 }
1183
1184 while (true) {
ss412g1a79bdf2019-10-24 12:03:05 +03001185 if (send(fd,message.message.asndata, message.message.asnLength,MSG_NOSIGNAL) < 0) {
1186 if (errno == EINTR) {
1187 continue;
1188 }
1189 mdclog_write(MDCLOG_ERR, "error writing to CU a message, %s ", strerror(errno));
aa7133@att.com84bd3342020-03-16 18:04:57 +02001190 if (!peerInfo->isConnected) {
1191 mdclog_write(MDCLOG_ERR, "connection to CU %s is still in progress.", message.message.enodbName);
dhirajverma6cf28612021-06-23 04:53:34 -04001192#ifndef UNIT_TEST
aa7133@att.com84bd3342020-03-16 18:04:57 +02001193 return -1;
dhirajverma6cf28612021-06-23 04:53:34 -04001194#endif
aa7133@att.com84bd3342020-03-16 18:04:57 +02001195 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001196 cleanHashEntry(peerInfo, m);
ss412g1a79bdf2019-10-24 12:03:05 +03001197 close(fd);
1198 char key[MAX_ENODB_NAME_SIZE * 2];
1199 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", message.message.enodbName,
1200 message.message.messageType);
1201 if (loglevel >= MDCLOG_DEBUG) {
1202 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
1203 }
aa7133@att.com96c9b882020-03-25 14:50:40 +02001204 auto tmp = m->find(key);
ss412g1a79bdf2019-10-24 12:03:05 +03001205 if (tmp) {
1206 free(tmp);
1207 }
1208 m->erase(key);
dhirajverma6cf28612021-06-23 04:53:34 -04001209#ifndef UNIT_TEST
ss412g1a79bdf2019-10-24 12:03:05 +03001210 return -1;
dhirajverma6cf28612021-06-23 04:53:34 -04001211#endif
ss412g1a79bdf2019-10-24 12:03:05 +03001212 }
1213 message.message.direction = 'D';
1214 // send report.buffer of size
1215 buildJsonMessage(message);
1216
1217 if (loglevel >= MDCLOG_DEBUG) {
1218 mdclog_write(MDCLOG_DEBUG,
1219 "SCTP message for CU %s sent from %s",
1220 message.message.enodbName,
1221 __FUNCTION__);
1222 }
ss412g1a79bdf2019-10-24 12:03:05 +03001223 return 0;
1224 }
1225}
1226
1227/**
1228 *
1229 * @param message
1230 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001231 */
aa7133@att.combe92d712020-03-18 17:30:39 +02001232void getRequestMetaData(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03001233 message.message.asndata = rmrMessageBuffer.rcvMessage->payload;
1234 message.message.asnLength = rmrMessageBuffer.rcvMessage->len;
1235
1236 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1237 mdclog_write(MDCLOG_DEBUG, "Message from Xapp RAN name = %s message length = %ld",
1238 message.message.enodbName, (unsigned long) message.message.asnLength);
1239 }
ss412g1a79bdf2019-10-24 12:03:05 +03001240}
1241
1242
ss412g1a79bdf2019-10-24 12:03:05 +03001243
1244/**
1245 *
1246 * @param events
1247 * @param sctpMap
1248 * @param numOfMessages
1249 * @param rmrMessageBuffer
1250 * @param ts
ss412g1a79bdf2019-10-24 12:03:05 +03001251 * @return
1252 */
1253int receiveDataFromSctp(struct epoll_event *events,
1254 Sctp_Map_t *sctpMap,
1255 int &numOfMessages,
1256 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +02001257 struct timespec &ts) {
ss412g1a79bdf2019-10-24 12:03:05 +03001258 /* We have data on the fd waiting to be read. Read and display it.
1259 * We must read whatever data is available completely, as we are running
1260 * in edge-triggered mode and won't get a notification again for the same data. */
aa7133@att.com84bd3342020-03-16 18:04:57 +02001261 ReportingMessages_t message {};
1262 auto done = 0;
ss412g1a79bdf2019-10-24 12:03:05 +03001263 auto loglevel = mdclog_level_get();
aa7133@att.com84bd3342020-03-16 18:04:57 +02001264
ss412g1a79bdf2019-10-24 12:03:05 +03001265 // get the identity of the interface
aa7133@att.com84bd3342020-03-16 18:04:57 +02001266 message.peerInfo = (ConnectedCU_t *)events->data.ptr;
1267
ss412g1a79bdf2019-10-24 12:03:05 +03001268 struct timespec start{0, 0};
aa7133@att.com409f2232020-10-29 13:06:45 +03001269 struct timespec decodeStart{0, 0};
ss412g1a79bdf2019-10-24 12:03:05 +03001270 struct timespec end{0, 0};
1271
1272 E2AP_PDU_t *pdu = nullptr;
1273
ss412g1a79bdf2019-10-24 12:03:05 +03001274 while (true) {
1275 if (loglevel >= MDCLOG_DEBUG) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001276 mdclog_write(MDCLOG_DEBUG, "Start Read from SCTP %d fd", message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001277 clock_gettime(CLOCK_MONOTONIC, &start);
1278 }
1279 // read the buffer directly to rmr payload
1280 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1281 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
aa7133@att.com84bd3342020-03-16 18:04:57 +02001282 read(message.peerInfo->fileDescriptor, rmrMessageBuffer.sendMessage->payload, RECEIVE_SCTP_BUFFER_SIZE);
1283
ss412g1a79bdf2019-10-24 12:03:05 +03001284 if (loglevel >= MDCLOG_DEBUG) {
1285 mdclog_write(MDCLOG_DEBUG, "Finish Read from SCTP %d fd message length = %ld",
aa7133@att.com96c9b882020-03-25 14:50:40 +02001286 message.peerInfo->fileDescriptor, message.message.asnLength);
ss412g1a79bdf2019-10-24 12:03:05 +03001287 }
aa7133@att.com16c799b2020-03-24 18:01:51 +02001288
aa7133@att.com84bd3342020-03-16 18:04:57 +02001289 memcpy(message.message.enodbName, message.peerInfo->enodbName, sizeof(message.peerInfo->enodbName));
ss412g1a79bdf2019-10-24 12:03:05 +03001290 message.message.direction = 'U';
1291 message.message.time.tv_nsec = ts.tv_nsec;
1292 message.message.time.tv_sec = ts.tv_sec;
1293
1294 if (message.message.asnLength < 0) {
1295 if (errno == EINTR) {
1296 continue;
1297 }
1298 /* If errno == EAGAIN, that means we have read all
aa7133@att.com84bd3342020-03-16 18:04:57 +02001299 data. So goReportingMessages_t back to the main loop. */
ss412g1a79bdf2019-10-24 12:03:05 +03001300 if (errno != EAGAIN) {
1301 mdclog_write(MDCLOG_ERR, "Read error, %s ", strerror(errno));
1302 done = 1;
1303 } else if (loglevel >= MDCLOG_DEBUG) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001304 mdclog_write(MDCLOG_DEBUG, "EAGAIN - descriptor = %d", message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001305 }
1306 break;
1307 } else if (message.message.asnLength == 0) {
1308 /* End of file. The remote has closed the connection. */
1309 if (loglevel >= MDCLOG_INFO) {
1310 mdclog_write(MDCLOG_INFO, "END of File Closed connection - descriptor = %d",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001311 message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001312 }
1313 done = 1;
1314 break;
1315 }
1316
ss412g1a79bdf2019-10-24 12:03:05 +03001317 if (loglevel >= MDCLOG_DEBUG) {
swc58d4f5d2020-10-20 15:08:38 +09001318 char printBuffer[RECEIVE_SCTP_BUFFER_SIZE]{};
ss412g1a79bdf2019-10-24 12:03:05 +03001319 char *tmp = printBuffer;
1320 for (size_t i = 0; i < (size_t)message.message.asnLength; ++i) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001321 snprintf(tmp, 3, "%02x", message.message.asndata[i]);
ss412g1a79bdf2019-10-24 12:03:05 +03001322 tmp += 2;
1323 }
1324 printBuffer[message.message.asnLength] = 0;
1325 clock_gettime(CLOCK_MONOTONIC, &end);
1326 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 +02001327 message.peerInfo->enodbName, end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec);
ss412g1a79bdf2019-10-24 12:03:05 +03001328 mdclog_write(MDCLOG_DEBUG, "PDU buffer length = %ld, data = : %s", message.message.asnLength,
1329 printBuffer);
aa7133@att.com409f2232020-10-29 13:06:45 +03001330 clock_gettime(CLOCK_MONOTONIC, &decodeStart);
ss412g1a79bdf2019-10-24 12:03:05 +03001331 }
1332
aa7133@att.com7b437f72020-03-30 13:25:54 +03001333 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, (void **) &pdu,
ss412g1a79bdf2019-10-24 12:03:05 +03001334 message.message.asndata, message.message.asnLength);
1335 if (rval.code != RC_OK) {
1336 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2AP PDU from RAN : %s", rval.code,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001337 message.peerInfo->enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001338 break;
1339 }
1340
1341 if (loglevel >= MDCLOG_DEBUG) {
1342 clock_gettime(CLOCK_MONOTONIC, &end);
1343 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 +03001344 message.peerInfo->enodbName, end.tv_sec - decodeStart.tv_sec, end.tv_nsec - decodeStart.tv_nsec);
ss412g1a79bdf2019-10-24 12:03:05 +03001345 char *printBuffer;
1346 size_t size;
1347 FILE *stream = open_memstream(&printBuffer, &size);
1348 asn_fprint(stream, &asn_DEF_E2AP_PDU, pdu);
1349 mdclog_write(MDCLOG_DEBUG, "Encoding E2AP PDU past : %s", printBuffer);
aa7133@att.com409f2232020-10-29 13:06:45 +03001350 clock_gettime(CLOCK_MONOTONIC, &decodeStart);
Byonggon Chuncae9bd52020-11-18 13:37:29 +09001351
1352 fclose(stream);
1353 free(printBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001354 }
1355
1356 switch (pdu->present) {
1357 case E2AP_PDU_PR_initiatingMessage: {//initiating message
aa7133@att.com7b437f72020-03-30 13:25:54 +03001358 asnInitiatingRequest(pdu, sctpMap,message, rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001359 break;
1360 }
1361 case E2AP_PDU_PR_successfulOutcome: { //successful outcome
aa7133@att.com409f2232020-10-29 13:06:45 +03001362 asnSuccessfulMsg(pdu, sctpMap, message, rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001363 break;
1364 }
1365 case E2AP_PDU_PR_unsuccessfulOutcome: { //Unsuccessful Outcome
aa7133@att.com7b437f72020-03-30 13:25:54 +03001366 asnUnSuccsesfulMsg(pdu, sctpMap, message, rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001367 break;
1368 }
1369 default:
1370 mdclog_write(MDCLOG_ERR, "Unknown index %d in E2AP PDU", pdu->present);
1371 break;
1372 }
1373 if (loglevel >= MDCLOG_DEBUG) {
1374 clock_gettime(CLOCK_MONOTONIC, &end);
1375 mdclog_write(MDCLOG_DEBUG,
1376 "After processing message and sent to rmr for : %s, Read time is : %ld seconds, %ld nanoseconds",
aa7133@att.com409f2232020-10-29 13:06:45 +03001377 message.peerInfo->enodbName, end.tv_sec - decodeStart.tv_sec, end.tv_nsec - decodeStart.tv_nsec);
ss412g1a79bdf2019-10-24 12:03:05 +03001378 }
1379 numOfMessages++;
ss412g1a79bdf2019-10-24 12:03:05 +03001380 if (pdu != nullptr) {
aa7133@att.com1fecb052020-03-29 09:44:09 +03001381 ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, pdu);
1382 //ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
aa7133@att.com9dff4f02020-03-29 09:55:22 +03001383 //pdu = nullptr;
ss412g1a79bdf2019-10-24 12:03:05 +03001384 }
ss412g1a79bdf2019-10-24 12:03:05 +03001385 }
ss412g1a79bdf2019-10-24 12:03:05 +03001386
1387 if (done) {
1388 if (loglevel >= MDCLOG_INFO) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001389 mdclog_write(MDCLOG_INFO, "Closed connection - descriptor = %d", message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001390 }
1391 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
1392 snprintf((char *)rmrMessageBuffer.sendMessage->payload,
aa7133@att.com96c9b882020-03-25 14:50:40 +02001393 256,
1394 "%s|CU disconnected unexpectedly",
1395 message.peerInfo->enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001396 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1397
1398 if (sendRequestToXapp(message,
1399 RIC_SCTP_CONNECTION_FAILURE,
aa7133@att.combe92d712020-03-18 17:30:39 +02001400 rmrMessageBuffer) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03001401 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
1402 }
1403
1404 /* Closing descriptor make epoll remove it from the set of descriptors which are monitored. */
aa7133@att.com84bd3342020-03-16 18:04:57 +02001405 close(message.peerInfo->fileDescriptor);
aa7133@att.combe92d712020-03-18 17:30:39 +02001406 cleanHashEntry((ConnectedCU_t *) events->data.ptr, sctpMap);
ss412g1a79bdf2019-10-24 12:03:05 +03001407 }
1408 if (loglevel >= MDCLOG_DEBUG) {
1409 clock_gettime(CLOCK_MONOTONIC, &end);
1410 mdclog_write(MDCLOG_DEBUG, "from receive SCTP to send RMR time is %ld seconds and %ld nanoseconds",
1411 end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec);
1412
1413 }
ss412g1a79bdf2019-10-24 12:03:05 +03001414 return 0;
1415}
1416
aa7133@att.com409f2232020-10-29 13:06:45 +03001417static void buildAndSendSetupRequest(ReportingMessages_t &message,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001418 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001419 E2AP_PDU_t *pdu/*,
ss412g4aeeda22020-06-16 14:12:41 +03001420 string const &messageName,
1421 string const &ieName,
1422 vector<string> &functionsToAdd_v,
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001423 vector<string> &functionsToModified_v*/) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001424 auto logLevel = mdclog_level_get();
aa7133@att.com84bd3342020-03-16 18:04:57 +02001425 // now we can send the data to e2Mgr
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001426
ss412g4aeeda22020-06-16 14:12:41 +03001427 asn_enc_rval_t er;
1428 auto buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
Byonggon Chun51885f32020-11-18 15:31:04 +09001429 unsigned char *buffer = nullptr;
1430 buffer = (unsigned char *) calloc(buffer_size, sizeof(unsigned char));
1431 if(!buffer)
1432 {
1433 mdclog_write(MDCLOG_ERR, "Allocating buffer for %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1434 return;
1435 }
ss412g4aeeda22020-06-16 14:12:41 +03001436 while (true) {
ss412g4aeeda22020-06-16 14:12:41 +03001437 er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu, buffer, buffer_size);
1438 if (er.encoded == -1) {
1439 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1440 return;
1441 } else if (er.encoded > (ssize_t) buffer_size) {
1442 buffer_size = er.encoded + 128;
1443 mdclog_write(MDCLOG_WARN, "Buffer of size %d is to small for %s. Reallocate buffer of size %d",
1444 (int) buffer_size,
1445 asn_DEF_E2AP_PDU.name, buffer_size);
1446 buffer_size = er.encoded + 128;
Byonggon Chun51885f32020-11-18 15:31:04 +09001447
1448 unsigned char *newBuffer = nullptr;
1449 newBuffer = (unsigned char *) realloc(buffer, buffer_size);
1450 if (!newBuffer)
1451 {
1452 // out of memory
1453 mdclog_write(MDCLOG_ERR, "Reallocating buffer for %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1454 free(buffer);
1455 return;
1456 }
1457 buffer = newBuffer;
ss412g4aeeda22020-06-16 14:12:41 +03001458 continue;
1459 }
1460 buffer[er.encoded] = '\0';
1461 break;
1462 }
1463 // encode to xml
1464
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001465 string res((char *)buffer);
1466 res.erase(std::remove(res.begin(), res.end(), '\n'), res.end());
1467 res.erase(std::remove(res.begin(), res.end(), '\t'), res.end());
1468 res.erase(std::remove(res.begin(), res.end(), ' '), res.end());
1469
1470// string res {};
1471// if (!functionsToAdd_v.empty() || !functionsToModified_v.empty()) {
1472// res = buildXmlData(messageName, ieName, functionsToAdd_v, functionsToModified_v, buffer, (size_t) er.encoded);
1473// }
ss412g4aeeda22020-06-16 14:12:41 +03001474 rmr_mbuf_t *rmrMsg;
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001475// if (res.length() == 0) {
1476// rmrMsg = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, buffer_size + 256);
1477// rmrMsg->len = snprintf((char *) rmrMsg->payload, RECEIVE_SCTP_BUFFER_SIZE * 2, "%s:%d|%s",
1478// message.peerInfo->sctpParams->myIP.c_str(),
1479// message.peerInfo->sctpParams->rmrPort,
1480// buffer);
1481// } else {
ss412g4aeeda22020-06-16 14:12:41 +03001482 rmrMsg = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, (int)res.length() + 256);
1483 rmrMsg->len = snprintf((char *) rmrMsg->payload, res.length() + 256, "%s:%d|%s",
1484 message.peerInfo->sctpParams->myIP.c_str(),
1485 message.peerInfo->sctpParams->rmrPort,
1486 res.c_str());
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001487// }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001488
ss412g4aeeda22020-06-16 14:12:41 +03001489 if (logLevel >= MDCLOG_DEBUG) {
1490 mdclog_write(MDCLOG_DEBUG, "Setup request of size %d :\n %s\n", rmrMsg->len, rmrMsg->payload);
1491 }
1492 // send to RMR
1493 rmrMsg->mtype = message.message.messageType;
1494 rmrMsg->state = 0;
1495 rmr_bytes2meid(rmrMsg, (unsigned char *) message.message.enodbName, strlen(message.message.enodbName));
aa7133@att.com84bd3342020-03-16 18:04:57 +02001496
ss412g4aeeda22020-06-16 14:12:41 +03001497 static unsigned char tx[32];
1498 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1499 rmr_bytes2xact(rmrMsg, tx, strlen((const char *) tx));
dhirajverma60f8bef2021-06-29 07:22:58 -04001500#ifndef UNIT_TEST
ss412g4aeeda22020-06-16 14:12:41 +03001501 rmrMsg = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMsg);
dhirajverma60f8bef2021-06-29 07:22:58 -04001502#endif
ss412g4aeeda22020-06-16 14:12:41 +03001503 if (rmrMsg == nullptr) {
1504 mdclog_write(MDCLOG_ERR, "RMR failed to send returned nullptr");
1505 } else if (rmrMsg->state != 0) {
1506 char meid[RMR_MAX_MEID]{};
1507 if (rmrMsg->state == RMR_ERR_RETRY) {
1508 usleep(5);
1509 rmrMsg->state = 0;
1510 mdclog_write(MDCLOG_INFO, "RETRY sending Message %d to Xapp from %s",
1511 rmrMsg->mtype, rmr_get_meid(rmrMsg, (unsigned char *) meid));
dhirajverma60f8bef2021-06-29 07:22:58 -04001512#ifndef UNIT_TEST
ss412g4aeeda22020-06-16 14:12:41 +03001513 rmrMsg = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMsg);
dhirajverma60f8bef2021-06-29 07:22:58 -04001514#endif
ss412g4aeeda22020-06-16 14:12:41 +03001515 if (rmrMsg == nullptr) {
1516 mdclog_write(MDCLOG_ERR, "RMR failed send returned nullptr");
1517 } else if (rmrMsg->state != 0) {
1518 mdclog_write(MDCLOG_ERR,
1519 "RMR Retry failed %s sending request %d to Xapp from %s",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001520 translateRmrErrorMessages(rmrMsg->state).c_str(),
1521 rmrMsg->mtype,
1522 rmr_get_meid(rmrMsg, (unsigned char *) meid));
1523 }
ss412g4aeeda22020-06-16 14:12:41 +03001524 } else {
1525 mdclog_write(MDCLOG_ERR, "RMR failed: %s. sending request %d to Xapp from %s",
1526 translateRmrErrorMessages(rmrMsg->state).c_str(),
1527 rmrMsg->mtype,
1528 rmr_get_meid(rmrMsg, (unsigned char *) meid));
aa7133@att.combc110772020-03-25 09:45:14 +02001529 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001530 }
ss412g4aeeda22020-06-16 14:12:41 +03001531 message.peerInfo->gotSetup = true;
1532 buildJsonMessage(message);
Byonggon Chun51885f32020-11-18 15:31:04 +09001533
ss412g4aeeda22020-06-16 14:12:41 +03001534 if (rmrMsg != nullptr) {
1535 rmr_free_msg(rmrMsg);
1536 }
Byonggon Chun51885f32020-11-18 15:31:04 +09001537 free(buffer);
1538
1539 return;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001540}
aa7133@att.comc5a15202020-04-05 19:57:01 +03001541
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001542#if 0
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001543int RAN_Function_list_To_Vector(RANfunctions_List_t& list, vector <string> &runFunXML_v) {
1544 auto index = 0;
1545 runFunXML_v.clear();
1546 for (auto j = 0; j < list.list.count; j++) {
1547 auto *raNfunctionItemIEs = (RANfunction_ItemIEs_t *)list.list.array[j];
1548 if (raNfunctionItemIEs->id == ProtocolIE_ID_id_RANfunction_Item &&
1549 (raNfunctionItemIEs->value.present == RANfunction_ItemIEs__value_PR_RANfunction_Item)) {
1550 // encode to xml
1551 E2SM_gNB_NRT_RANfunction_Definition_t *ranFunDef = nullptr;
1552 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER,
1553 &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition,
1554 (void **)&ranFunDef,
1555 raNfunctionItemIEs->value.choice.RANfunction_Item.ranFunctionDefinition.buf,
1556 raNfunctionItemIEs->value.choice.RANfunction_Item.ranFunctionDefinition.size);
1557 if (rval.code != RC_OK) {
1558 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2SM message from : %s",
1559 rval.code,
1560 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name);
1561 return -1;
1562 }
1563
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001564 auto xml_buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
1565 unsigned char xml_buffer[RECEIVE_SCTP_BUFFER_SIZE * 2];
ss412g4aeeda22020-06-16 14:12:41 +03001566 memset(xml_buffer, 0, RECEIVE_SCTP_BUFFER_SIZE * 2);
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001567 // encode to xml
1568 auto er = asn_encode_to_buffer(nullptr,
1569 ATS_BASIC_XER,
1570 &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition,
1571 ranFunDef,
1572 xml_buffer,
1573 xml_buffer_size);
1574 if (er.encoded == -1) {
1575 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s",
1576 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name,
1577 strerror(errno));
1578 } else if (er.encoded > (ssize_t)xml_buffer_size) {
1579 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
1580 (int) xml_buffer_size,
1581 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name, __func__, __LINE__);
1582 } else {
1583 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1584 mdclog_write(MDCLOG_DEBUG, "Encoding E2SM %s PDU number %d : %s",
1585 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name,
1586 index++,
1587 xml_buffer);
1588 }
ss412g4aeeda22020-06-16 14:12:41 +03001589
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001590 string runFuncs = (char *)(xml_buffer);
1591 runFunXML_v.emplace_back(runFuncs);
1592 }
1593 }
1594 }
1595 return 0;
1596}
aa7133@att.comc5a15202020-04-05 19:57:01 +03001597
ss412g4aeeda22020-06-16 14:12:41 +03001598int collectServiceUpdate_RequestData(E2AP_PDU_t *pdu,
1599 Sctp_Map_t *sctpMap,
1600 ReportingMessages_t &message,
1601 vector <string> &RANfunctionsAdded_v,
1602 vector <string> &RANfunctionsModified_v) {
aa7133@att.comc5a15202020-04-05 19:57:01 +03001603 memset(message.peerInfo->enodbName, 0 , MAX_ENODB_NAME_SIZE);
ss412g4aeeda22020-06-16 14:12:41 +03001604 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICserviceUpdate.protocolIEs.list.count; i++) {
1605 auto *ie = pdu->choice.initiatingMessage->value.choice.RICserviceUpdate.protocolIEs.list.array[i];
1606 if (ie->id == ProtocolIE_ID_id_RANfunctionsAdded) {
1607 if (ie->value.present == RICserviceUpdate_IEs__value_PR_RANfunctionsID_List) {
aa7133@att.comc5a15202020-04-05 19:57:01 +03001608 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001609 mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1610 ie->value.choice.RANfunctions_List.list.count);
aa7133@att.comc5a15202020-04-05 19:57:01 +03001611 }
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001612 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsAdded_v) != 0 ) {
1613 return -1;
1614 }
1615 }
1616 } else if (ie->id == ProtocolIE_ID_id_RANfunctionsModified) {
ss412g4aeeda22020-06-16 14:12:41 +03001617 if (ie->value.present == RICserviceUpdate_IEs__value_PR_RANfunctions_List) {
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001618 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1619 mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1620 ie->value.choice.RANfunctions_List.list.count);
1621 }
1622 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsModified_v) != 0 ) {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001623 return -1;
aa7133@att.comc5a15202020-04-05 19:57:01 +03001624 }
1625 }
1626 }
1627 }
1628 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1629 mdclog_write(MDCLOG_DEBUG, "Run function vector have %ld entries",
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001630 RANfunctionsAdded_v.size());
aa7133@att.comc5a15202020-04-05 19:57:01 +03001631 }
1632 return 0;
1633}
ss412g4aeeda22020-06-16 14:12:41 +03001634
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001635#endif
ss412g4aeeda22020-06-16 14:12:41 +03001636
1637
aa7133@att.com409f2232020-10-29 13:06:45 +03001638void buildPrometheusList(ConnectedCU_t *peerInfo, Family<Counter> *prometheusFamily) {
aa7133@att.coma4c80012020-06-24 16:36:47 +03001639 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"SetupRequest", "Messages"}});
1640 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"SetupRequest", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001641
aa7133@att.coma4c80012020-06-24 16:36:47 +03001642 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ErrorIndication", "Messages"}});
1643 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ErrorIndication", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001644
aa7133@att.coma4c80012020-06-24 16:36:47 +03001645 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_RICindication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICindication", "Messages"}});
1646 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_RICindication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICindication", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001647
aa7133@att.coma4c80012020-06-24 16:36:47 +03001648 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetRequest", "Messages"}});
1649 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetRequest", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001650
aa7133@att.coma4c80012020-06-24 16:36:47 +03001651 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICserviceUpdate", "Messages"}});
1652 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICserviceUpdate", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001653 // ---------------------------------------------
aa7133@att.coma4c80012020-06-24 16:36:47 +03001654 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetACK", "Messages"}});
1655 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001656
aa7133@att.coma4c80012020-06-24 16:36:47 +03001657 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolACK", "Messages"}});
1658 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001659
aa7133@att.coma4c80012020-06-24 16:36:47 +03001660 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionACK", "Messages"}});
1661 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001662
aa7133@att.coma4c80012020-06-24 16:36:47 +03001663 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteACK", "Messages"}});
1664 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001665 //-------------------------------------------------------------
1666
aa7133@att.coma4c80012020-06-24 16:36:47 +03001667 peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolFailure", "Messages"}});
1668 peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001669
aa7133@att.coma4c80012020-06-24 16:36:47 +03001670 peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionFailure", "Messages"}});
1671 peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001672
aa7133@att.coma4c80012020-06-24 16:36:47 +03001673 peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteFailure", "Messages"}});
1674 peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001675
1676 //====================================================================================
aa7133@att.coma4c80012020-06-24 16:36:47 +03001677 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ErrorIndication", "Messages"}});
1678 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ErrorIndication", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001679
aa7133@att.coma4c80012020-06-24 16:36:47 +03001680 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetRequest", "Messages"}});
1681 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetRequest", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001682
aa7133@att.coma4c80012020-06-24 16:36:47 +03001683 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICcontrol", "Messages"}});
1684 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICcontrol", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001685
aa7133@att.coma4c80012020-06-24 16:36:47 +03001686 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICserviceQuery)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceQuery", "Messages"}});
1687 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICserviceQuery)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceQuery", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001688
aa7133@att.coma4c80012020-06-24 16:36:47 +03001689 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscription", "Messages"}});
1690 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscription", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001691
aa7133@att.coma4c80012020-06-24 16:36:47 +03001692 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscriptionDelete", "Messages"}});
1693 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscriptionDelete", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001694 //---------------------------------------------------------------------------------------------------------
aa7133@att.coma4c80012020-06-24 16:36:47 +03001695 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupResponse", "Messages"}});
1696 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupResponse", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001697
aa7133@att.coma4c80012020-06-24 16:36:47 +03001698 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetACK", "Messages"}});
1699 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001700
aa7133@att.coma4c80012020-06-24 16:36:47 +03001701 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateResponse", "Messages"}});
1702 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateResponse", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001703 //----------------------------------------------------------------------------------------------------------------
aa7133@att.coma4c80012020-06-24 16:36:47 +03001704 peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupRequestFailure", "Messages"}});
1705 peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupRequestFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001706
aa7133@att.coma4c80012020-06-24 16:36:47 +03001707 peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateFailure", "Messages"}});
1708 peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001709}
1710/**
1711 *
1712 * @param pdu
1713 * @param sctpMap
1714 * @param message
1715 * @param RANfunctionsAdded_v
1716 * @return
1717 */
1718int collectSetupRequestData(E2AP_PDU_t *pdu,
1719 Sctp_Map_t *sctpMap,
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001720 ReportingMessages_t &message /*, vector <string> &RANfunctionsAdded_v*/) {
ss412g4aeeda22020-06-16 14:12:41 +03001721 memset(message.peerInfo->enodbName, 0 , MAX_ENODB_NAME_SIZE);
1722 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.count; i++) {
1723 auto *ie = pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.array[i];
1724 if (ie->id == ProtocolIE_ID_id_GlobalE2node_ID) {
1725 // get the ran name for meid
1726 if (ie->value.present == E2setupRequestIEs__value_PR_GlobalE2node_ID) {
1727 if (buildRanName(message.peerInfo->enodbName, ie) < 0) {
1728 mdclog_write(MDCLOG_ERR, "Bad param in E2setupRequestIEs GlobalE2node_ID.\n");
aa7133@att.com409f2232020-10-29 13:06:45 +03001729 // no message will be sent
ss412g4aeeda22020-06-16 14:12:41 +03001730 return -1;
1731 }
1732
1733 memcpy(message.message.enodbName, message.peerInfo->enodbName, strlen(message.peerInfo->enodbName));
1734 sctpMap->setkey(message.message.enodbName, message.peerInfo);
1735 }
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001736 } /*else if (ie->id == ProtocolIE_ID_id_RANfunctionsAdded) {
ss412g4aeeda22020-06-16 14:12:41 +03001737 if (ie->value.present == E2setupRequestIEs__value_PR_RANfunctions_List) {
1738 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1739 mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1740 ie->value.choice.RANfunctions_List.list.count);
1741 }
1742 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsAdded_v) != 0 ) {
1743 return -1;
1744 }
1745 }
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001746 } */
ss412g4aeeda22020-06-16 14:12:41 +03001747 }
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001748// if (mdclog_level_get() >= MDCLOG_DEBUG) {
1749// mdclog_write(MDCLOG_DEBUG, "Run function vector have %ld entries",
1750// RANfunctionsAdded_v.size());
1751// }
ss412g4aeeda22020-06-16 14:12:41 +03001752 return 0;
1753}
1754
1755int XML_From_PER(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
1756 E2AP_PDU_t *pdu = nullptr;
1757
1758 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1759 mdclog_write(MDCLOG_DEBUG, "got PER message of size %d is:%s",
1760 rmrMessageBuffer.sendMessage->len, rmrMessageBuffer.sendMessage->payload);
1761 }
1762 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, (void **) &pdu,
1763 rmrMessageBuffer.sendMessage->payload, rmrMessageBuffer.sendMessage->len);
1764 if (rval.code != RC_OK) {
1765 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) setup response from E2MGR : %s",
1766 rval.code,
1767 message.message.enodbName);
1768 return -1;
1769 }
1770
1771 int buff_size = RECEIVE_XAPP_BUFFER_SIZE;
1772 auto er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu,
1773 rmrMessageBuffer.sendMessage->payload, buff_size);
1774 if (er.encoded == -1) {
1775 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1776 return -1;
1777 } else if (er.encoded > (ssize_t)buff_size) {
1778 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
1779 (int)rmrMessageBuffer.sendMessage->len,
1780 asn_DEF_E2AP_PDU.name,
1781 __func__,
1782 __LINE__);
1783 return -1;
1784 }
1785 rmrMessageBuffer.sendMessage->len = er.encoded;
1786 return 0;
1787
1788}
1789
ss412g1a79bdf2019-10-24 12:03:05 +03001790/**
1791 *
1792 * @param pdu
1793 * @param message
1794 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001795 */
1796void asnInitiatingRequest(E2AP_PDU_t *pdu,
aa7133@att.com7b437f72020-03-30 13:25:54 +03001797 Sctp_Map_t *sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +03001798 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +02001799 RmrMessagesBuffer_t &rmrMessageBuffer) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001800 auto logLevel = mdclog_level_get();
ss412g1a79bdf2019-10-24 12:03:05 +03001801 auto procedureCode = ((InitiatingMessage_t *) pdu->choice.initiatingMessage)->procedureCode;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001802 if (logLevel >= MDCLOG_DEBUG) {
1803 mdclog_write(MDCLOG_DEBUG, "Initiating message %ld\n", procedureCode);
ss412g1a79bdf2019-10-24 12:03:05 +03001804 }
1805 switch (procedureCode) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001806 case ProcedureCode_id_E2setup: {
1807 if (logLevel >= MDCLOG_DEBUG) {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001808 mdclog_write(MDCLOG_DEBUG, "Got E2setup");
aa7133@att.com84bd3342020-03-16 18:04:57 +02001809 }
1810
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001811// vector <string> RANfunctionsAdded_v;
1812// vector <string> RANfunctionsModified_v;
1813// RANfunctionsAdded_v.clear();
1814// RANfunctionsModified_v.clear();
1815 if (collectSetupRequestData(pdu, sctpMap, message) != 0) {
aa7133@att.com7b437f72020-03-30 13:25:54 +03001816 break;
1817 }
1818
aa7133@att.com409f2232020-10-29 13:06:45 +03001819 buildPrometheusList(message.peerInfo, message.peerInfo->sctpParams->prometheusFamily);
ss412g4aeeda22020-06-16 14:12:41 +03001820
1821 string messageName("E2setupRequest");
1822 string ieName("E2setupRequestIEs");
1823 message.message.messageType = RIC_E2_SETUP_REQ;
aa7133@att.coma4c80012020-06-24 16:36:47 +03001824 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
1825 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment((double)message.message.asnLength);
aa7133@att.com409f2232020-10-29 13:06:45 +03001826 buildAndSendSetupRequest(message, rmrMessageBuffer, pdu);
ss412g1a79bdf2019-10-24 12:03:05 +03001827 break;
1828 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001829 case ProcedureCode_id_RICserviceUpdate: {
1830 if (logLevel >= MDCLOG_DEBUG) {
1831 mdclog_write(MDCLOG_DEBUG, "Got RICserviceUpdate %s", message.message.enodbName);
1832 }
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001833// vector <string> RANfunctionsAdded_v;
1834// vector <string> RANfunctionsModified_v;
1835// RANfunctionsAdded_v.clear();
1836// RANfunctionsModified_v.clear();
1837// if (collectServiceUpdate_RequestData(pdu, sctpMap, message,
1838// RANfunctionsAdded_v, RANfunctionsModified_v) != 0) {
1839// break;
1840// }
ss412g4aeeda22020-06-16 14:12:41 +03001841
1842 string messageName("RICserviceUpdate");
1843 string ieName("RICserviceUpdateIEs");
1844 message.message.messageType = RIC_SERVICE_UPDATE;
dhirajverma6cf28612021-06-23 04:53:34 -04001845#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03001846 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
1847 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04001848#endif
aa7133@att.com409f2232020-10-29 13:06:45 +03001849 buildAndSendSetupRequest(message, rmrMessageBuffer, pdu);
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001850 break;
1851 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001852 case ProcedureCode_id_ErrorIndication: {
1853 if (logLevel >= MDCLOG_DEBUG) {
1854 mdclog_write(MDCLOG_DEBUG, "Got ErrorIndication %s", message.message.enodbName);
1855 }
dhirajverma6cf28612021-06-23 04:53:34 -04001856#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03001857 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_ErrorIndication]->Increment();
1858 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_ErrorIndication]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04001859#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02001860 if (sendRequestToXapp(message, RIC_ERROR_INDICATION, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001861 mdclog_write(MDCLOG_ERR, "RIC_ERROR_INDICATION failed to send to xAPP");
ss412g1a79bdf2019-10-24 12:03:05 +03001862 }
1863 break;
1864 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001865 case ProcedureCode_id_Reset: {
1866 if (logLevel >= MDCLOG_DEBUG) {
1867 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001868 }
dhirajverma6cf28612021-06-23 04:53:34 -04001869#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03001870 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
1871 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04001872#endif
ss412g4aeeda22020-06-16 14:12:41 +03001873 if (XML_From_PER(message, rmrMessageBuffer) < 0) {
1874 break;
ss412g1a79bdf2019-10-24 12:03:05 +03001875 }
ss412g4aeeda22020-06-16 14:12:41 +03001876
1877 if (sendRequestToXapp(message, RIC_E2_RESET_REQ, rmrMessageBuffer) != 0) {
1878 mdclog_write(MDCLOG_ERR, "RIC_E2_RESET_REQ message failed to send to xAPP");
aa7133@att.com84bd3342020-03-16 18:04:57 +02001879 }
1880 break;
1881 }
1882 case ProcedureCode_id_RICindication: {
1883 if (logLevel >= MDCLOG_DEBUG) {
1884 mdclog_write(MDCLOG_DEBUG, "Got RICindication %s", message.message.enodbName);
1885 }
1886 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.count; i++) {
ss412g1a79bdf2019-10-24 12:03:05 +03001887 auto messageSent = false;
1888 RICindication_IEs_t *ie = pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.array[i];
aa7133@att.com84bd3342020-03-16 18:04:57 +02001889 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03001890 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1891 }
1892 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001893 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03001894 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1895 }
1896 if (ie->value.present == RICindication_IEs__value_PR_RICrequestID) {
1897 static unsigned char tx[32];
1898 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_INDICATION;
1899 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1900 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
ss412g3bac2da2020-01-05 11:52:19 +02001901 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001902 (unsigned char *)message.message.enodbName,
1903 strlen(message.message.enodbName));
ss412g1a79bdf2019-10-24 12:03:05 +03001904 rmrMessageBuffer.sendMessage->state = 0;
aa7133@att.comc5a15202020-04-05 19:57:01 +03001905 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
1906
1907 //ie->value.choice.RICrequestID.ricInstanceID;
ss412g3bac2da2020-01-05 11:52:19 +02001908 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.comc5a15202020-04-05 19:57:01 +03001909 mdclog_write(MDCLOG_DEBUG, "sub id = %d, mtype = %d, ric instance id %ld, requestor id = %ld",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001910 rmrMessageBuffer.sendMessage->sub_id,
aa7133@att.comc5a15202020-04-05 19:57:01 +03001911 rmrMessageBuffer.sendMessage->mtype,
1912 ie->value.choice.RICrequestID.ricInstanceID,
1913 ie->value.choice.RICrequestID.ricRequestorID);
ss412g3bac2da2020-01-05 11:52:19 +02001914 }
dhirajverma6cf28612021-06-23 04:53:34 -04001915#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03001916 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICindication]->Increment();
1917 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICindication]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04001918#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02001919 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03001920 messageSent = true;
1921 } else {
aa7133@att.com409f2232020-10-29 13:06:45 +03001922 mdclog_write(MDCLOG_ERR, "RIC request id missing illegal request");
ss412g1a79bdf2019-10-24 12:03:05 +03001923 }
1924 }
1925 if (messageSent) {
1926 break;
1927 }
1928 }
1929 break;
1930 }
ss412g1a79bdf2019-10-24 12:03:05 +03001931 default: {
1932 mdclog_write(MDCLOG_ERR, "Undefined or not supported message = %ld", procedureCode);
1933 message.message.messageType = 0; // no RMR message type yet
1934
1935 buildJsonMessage(message);
1936
1937 break;
1938 }
1939 }
ss412g1a79bdf2019-10-24 12:03:05 +03001940}
1941
1942/**
1943 *
1944 * @param pdu
1945 * @param message
ss412g1a79bdf2019-10-24 12:03:05 +03001946 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001947 */
aa7133@att.com409f2232020-10-29 13:06:45 +03001948void asnSuccessfulMsg(E2AP_PDU_t *pdu,
aa7133@att.com7b437f72020-03-30 13:25:54 +03001949 Sctp_Map_t *sctpMap,
1950 ReportingMessages_t &message,
1951 RmrMessagesBuffer_t &rmrMessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03001952 auto procedureCode = pdu->choice.successfulOutcome->procedureCode;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001953 auto logLevel = mdclog_level_get();
1954 if (logLevel >= MDCLOG_INFO) {
ss412g1a79bdf2019-10-24 12:03:05 +03001955 mdclog_write(MDCLOG_INFO, "Successful Outcome %ld", procedureCode);
1956 }
1957 switch (procedureCode) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001958 case ProcedureCode_id_Reset: {
1959 if (logLevel >= MDCLOG_DEBUG) {
1960 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001961 }
dhirajverma6cf28612021-06-23 04:53:34 -04001962#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03001963 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
1964 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04001965#endif
ss412g4aeeda22020-06-16 14:12:41 +03001966 if (XML_From_PER(message, rmrMessageBuffer) < 0) {
1967 break;
1968 }
1969 if (sendRequestToXapp(message, RIC_E2_RESET_RESP, rmrMessageBuffer) != 0) {
1970 mdclog_write(MDCLOG_ERR, "RIC_E2_RESET_RESP message failed to send to xAPP");
ss412g1a79bdf2019-10-24 12:03:05 +03001971 }
1972 break;
1973 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001974 case ProcedureCode_id_RICcontrol: {
1975 if (logLevel >= MDCLOG_DEBUG) {
1976 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001977 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001978 for (auto i = 0;
ss412g1a79bdf2019-10-24 12:03:05 +03001979 i < pdu->choice.successfulOutcome->value.choice.RICcontrolAcknowledge.protocolIEs.list.count; i++) {
1980 auto messageSent = false;
1981 RICcontrolAcknowledge_IEs_t *ie = pdu->choice.successfulOutcome->value.choice.RICcontrolAcknowledge.protocolIEs.list.array[i];
1982 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1983 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1984 }
1985 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
1986 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1987 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1988 }
1989 if (ie->value.present == RICcontrolAcknowledge_IEs__value_PR_RICrequestID) {
1990 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_CONTROL_ACK;
1991 rmrMessageBuffer.sendMessage->state = 0;
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001992// rmrMessageBuffer.sendMessage->sub_id = (int) ie->value.choice.RICrequestID.ricRequestorID;
1993 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
1994
ss412g1a79bdf2019-10-24 12:03:05 +03001995 static unsigned char tx[32];
1996 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1997 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
ss412g3bac2da2020-01-05 11:52:19 +02001998 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001999 (unsigned char *)message.message.enodbName,
2000 strlen(message.message.enodbName));
dhirajverma6cf28612021-06-23 04:53:34 -04002001#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03002002 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
2003 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04002004#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002005 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03002006 messageSent = true;
2007 } else {
aa7133@att.com409f2232020-10-29 13:06:45 +03002008 mdclog_write(MDCLOG_ERR, "RIC request id missing illegal request");
ss412g1a79bdf2019-10-24 12:03:05 +03002009 }
2010 }
2011 if (messageSent) {
2012 break;
2013 }
2014 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02002015
2016 break;
2017 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02002018 case ProcedureCode_id_RICsubscription: {
2019 if (logLevel >= MDCLOG_DEBUG) {
2020 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
2021 }
dhirajverma6cf28612021-06-23 04:53:34 -04002022#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03002023 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
2024 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04002025#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002026 if (sendRequestToXapp(message, RIC_SUB_RESP, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02002027 mdclog_write(MDCLOG_ERR, "Subscription successful message failed to send to xAPP");
2028 }
2029 break;
2030 }
2031 case ProcedureCode_id_RICsubscriptionDelete: {
2032 if (logLevel >= MDCLOG_DEBUG) {
2033 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
2034 }
dhirajverma6cf28612021-06-23 04:53:34 -04002035#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03002036 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
2037 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04002038#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002039 if (sendRequestToXapp(message, RIC_SUB_DEL_RESP, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02002040 mdclog_write(MDCLOG_ERR, "Subscription delete successful message failed to send to xAPP");
2041 }
ss412g1a79bdf2019-10-24 12:03:05 +03002042 break;
2043 }
2044 default: {
2045 mdclog_write(MDCLOG_WARN, "Undefined or not supported message = %ld", procedureCode);
2046 message.message.messageType = 0; // no RMR message type yet
2047 buildJsonMessage(message);
2048
2049 break;
2050 }
2051 }
ss412g1a79bdf2019-10-24 12:03:05 +03002052}
2053
2054/**
2055 *
2056 * @param pdu
2057 * @param message
ss412g1a79bdf2019-10-24 12:03:05 +03002058 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03002059 */
2060void asnUnSuccsesfulMsg(E2AP_PDU_t *pdu,
aa7133@att.com7b437f72020-03-30 13:25:54 +03002061 Sctp_Map_t *sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +03002062 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +02002063 RmrMessagesBuffer_t &rmrMessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03002064 auto procedureCode = pdu->choice.unsuccessfulOutcome->procedureCode;
aa7133@att.com84bd3342020-03-16 18:04:57 +02002065 auto logLevel = mdclog_level_get();
2066 if (logLevel >= MDCLOG_INFO) {
ss412g1a79bdf2019-10-24 12:03:05 +03002067 mdclog_write(MDCLOG_INFO, "Unsuccessful Outcome %ld", procedureCode);
2068 }
2069 switch (procedureCode) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02002070 case ProcedureCode_id_RICcontrol: {
2071 if (logLevel >= MDCLOG_DEBUG) {
2072 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03002073 }
2074 for (int i = 0;
2075 i < pdu->choice.unsuccessfulOutcome->value.choice.RICcontrolFailure.protocolIEs.list.count; i++) {
2076 auto messageSent = false;
2077 RICcontrolFailure_IEs_t *ie = pdu->choice.unsuccessfulOutcome->value.choice.RICcontrolFailure.protocolIEs.list.array[i];
aa7133@att.com84bd3342020-03-16 18:04:57 +02002078 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03002079 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
2080 }
2081 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02002082 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03002083 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
2084 }
2085 if (ie->value.present == RICcontrolFailure_IEs__value_PR_RICrequestID) {
2086 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_CONTROL_FAILURE;
2087 rmrMessageBuffer.sendMessage->state = 0;
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002088// rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricRequestorID;
2089 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
ss412g1a79bdf2019-10-24 12:03:05 +03002090 static unsigned char tx[32];
2091 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2092 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
aa7133@att.com84bd3342020-03-16 18:04:57 +02002093 rmr_bytes2meid(rmrMessageBuffer.sendMessage, (unsigned char *) message.message.enodbName,
2094 strlen(message.message.enodbName));
dhirajverma6cf28612021-06-23 04:53:34 -04002095#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03002096 message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
2097 message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04002098#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002099 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03002100 messageSent = true;
2101 } else {
aa7133@att.com409f2232020-10-29 13:06:45 +03002102 mdclog_write(MDCLOG_ERR, "RIC request id missing illegal request");
ss412g1a79bdf2019-10-24 12:03:05 +03002103 }
2104 }
2105 if (messageSent) {
2106 break;
2107 }
2108 }
2109 break;
2110 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02002111 case ProcedureCode_id_RICsubscription: {
2112 if (logLevel >= MDCLOG_DEBUG) {
2113 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
2114 }
dhirajverma6cf28612021-06-23 04:53:34 -04002115#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03002116 message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
2117 message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04002118#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02002119 if (sendRequestToXapp(message, RIC_SUB_FAILURE, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02002120 mdclog_write(MDCLOG_ERR, "Subscription unsuccessful message failed to send to xAPP");
2121 }
2122 break;
2123 }
2124 case ProcedureCode_id_RICsubscriptionDelete: {
2125 if (logLevel >= MDCLOG_DEBUG) {
2126 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
2127 }
dhirajverma6cf28612021-06-23 04:53:34 -04002128#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03002129 message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
2130 message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04002131#endif
ss412g4aeeda22020-06-16 14:12:41 +03002132 if (sendRequestToXapp(message, RIC_SUB_FAILURE, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02002133 mdclog_write(MDCLOG_ERR, "Subscription Delete unsuccessful message failed to send to xAPP");
2134 }
2135 break;
2136 }
ss412g1a79bdf2019-10-24 12:03:05 +03002137 default: {
2138 mdclog_write(MDCLOG_WARN, "Undefined or not supported message = %ld", procedureCode);
2139 message.message.messageType = 0; // no RMR message type yet
2140
2141 buildJsonMessage(message);
2142
2143 break;
2144 }
2145 }
ss412g1a79bdf2019-10-24 12:03:05 +03002146}
2147
2148/**
2149 *
2150 * @param message
2151 * @param requestId
2152 * @param rmrMmessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03002153 * @return
2154 */
2155int sendRequestToXapp(ReportingMessages_t &message,
2156 int requestId,
aa7133@att.combe92d712020-03-18 17:30:39 +02002157 RmrMessagesBuffer_t &rmrMmessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03002158 rmr_bytes2meid(rmrMmessageBuffer.sendMessage,
2159 (unsigned char *)message.message.enodbName,
2160 strlen(message.message.enodbName));
2161 message.message.messageType = rmrMmessageBuffer.sendMessage->mtype = requestId;
2162 rmrMmessageBuffer.sendMessage->state = 0;
2163 static unsigned char tx[32];
2164 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2165 rmr_bytes2xact(rmrMmessageBuffer.sendMessage, tx, strlen((const char *) tx));
2166
aa7133@att.combe92d712020-03-18 17:30:39 +02002167 auto rc = sendRmrMessage(rmrMmessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03002168 return rc;
2169}
2170
ss412g4aeeda22020-06-16 14:12:41 +03002171/**
2172 *
2173 * @param pSctpParams
2174 */
aa7133@att.combe92d712020-03-18 17:30:39 +02002175void getRmrContext(sctp_params_t &pSctpParams) {
ss412g3bac2da2020-01-05 11:52:19 +02002176 pSctpParams.rmrCtx = nullptr;
aa7133@att.comab0e0f42020-04-06 17:13:58 +03002177 pSctpParams.rmrCtx = rmr_init(pSctpParams.rmrAddress, RECEIVE_XAPP_BUFFER_SIZE, RMRFL_NONE);
ss412g3bac2da2020-01-05 11:52:19 +02002178 if (pSctpParams.rmrCtx == nullptr) {
2179 mdclog_write(MDCLOG_ERR, "Failed to initialize RMR");
ss412g3bac2da2020-01-05 11:52:19 +02002180 return;
ss412g1a79bdf2019-10-24 12:03:05 +03002181 }
2182
ss412g3bac2da2020-01-05 11:52:19 +02002183 rmr_set_stimeout(pSctpParams.rmrCtx, 0); // disable retries for any send operation
ss412g1a79bdf2019-10-24 12:03:05 +03002184 // we need to find that routing table exist and we can run
2185 if (mdclog_level_get() >= MDCLOG_INFO) {
2186 mdclog_write(MDCLOG_INFO, "We are after RMR INIT wait for RMR_Ready");
2187 }
2188 int rmrReady = 0;
2189 int count = 0;
2190 while (!rmrReady) {
ss412g3bac2da2020-01-05 11:52:19 +02002191 if ((rmrReady = rmr_ready(pSctpParams.rmrCtx)) == 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002192 sleep(1);
2193 }
2194 count++;
2195 if (count % 60 == 0) {
2196 mdclog_write(MDCLOG_INFO, "waiting to RMR ready state for %d seconds", count);
2197 }
2198 }
2199 if (mdclog_level_get() >= MDCLOG_INFO) {
2200 mdclog_write(MDCLOG_INFO, "RMR running");
2201 }
ss412g3bac2da2020-01-05 11:52:19 +02002202 rmr_init_trace(pSctpParams.rmrCtx, 200);
2203 // get the RMR fd for the epoll
2204 pSctpParams.rmrListenFd = rmr_get_rcvfd(pSctpParams.rmrCtx);
2205 struct epoll_event event{};
2206 // add RMR fd to epoll
2207 event.events = (EPOLLIN);
2208 event.data.fd = pSctpParams.rmrListenFd;
2209 // add listening RMR FD to epoll
2210 if (epoll_ctl(pSctpParams.epoll_fd, EPOLL_CTL_ADD, pSctpParams.rmrListenFd, &event)) {
2211 mdclog_write(MDCLOG_ERR, "Failed to add RMR descriptor to epoll");
2212 close(pSctpParams.rmrListenFd);
2213 rmr_close(pSctpParams.rmrCtx);
2214 pSctpParams.rmrCtx = nullptr;
2215 }
ss412g1a79bdf2019-10-24 12:03:05 +03002216}
2217
ss412g4aeeda22020-06-16 14:12:41 +03002218/**
2219 *
2220 * @param message
2221 * @param rmrMessageBuffer
2222 * @return
2223 */
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002224int PER_FromXML(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
aa7133@att.com07e256f2020-04-20 15:59:51 +03002225 E2AP_PDU_t *pdu = nullptr;
aa7133@att.com1c4f50f2020-03-30 14:30:52 +03002226
2227 if (mdclog_level_get() >= MDCLOG_DEBUG) {
ss412g4aeeda22020-06-16 14:12:41 +03002228 mdclog_write(MDCLOG_DEBUG, "got xml Format data from xApp of size %d is:%s",
aa7133@att.com07e256f2020-04-20 15:59:51 +03002229 rmrMessageBuffer.rcvMessage->len, rmrMessageBuffer.rcvMessage->payload);
aa7133@att.com1c4f50f2020-03-30 14:30:52 +03002230 }
aa7133@att.com1fecb052020-03-29 09:44:09 +03002231 auto rval = asn_decode(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, (void **) &pdu,
2232 rmrMessageBuffer.rcvMessage->payload, rmrMessageBuffer.rcvMessage->len);
aa7133@att.com409f2232020-10-29 13:06:45 +03002233 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2234 mdclog_write(MDCLOG_DEBUG, "%s After decoding the XML to PDU", __func__ );
2235 }
aa7133@att.com1fecb052020-03-29 09:44:09 +03002236 if (rval.code != RC_OK) {
aa7133@att.com1c4f50f2020-03-30 14:30:52 +03002237 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) setup response from E2MGR : %s",
aa7133@att.com1fecb052020-03-29 09:44:09 +03002238 rval.code,
2239 message.message.enodbName);
2240 return -1;
2241 }
2242
aa7133@att.coma0f0db62020-04-01 16:52:50 +03002243 int buff_size = RECEIVE_XAPP_BUFFER_SIZE;
2244 auto er = asn_encode_to_buffer(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, pdu,
2245 rmrMessageBuffer.rcvMessage->payload, buff_size);
aa7133@att.com409f2232020-10-29 13:06:45 +03002246 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2247 mdclog_write(MDCLOG_DEBUG, "%s After encoding PDU to PER", __func__ );
2248 }
aa7133@att.com1fecb052020-03-29 09:44:09 +03002249 if (er.encoded == -1) {
2250 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
2251 return -1;
aa7133@att.coma0f0db62020-04-01 16:52:50 +03002252 } else if (er.encoded > (ssize_t)buff_size) {
2253 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 +03002254 (int)rmrMessageBuffer.rcvMessage->len,
aa7133@att.coma0f0db62020-04-01 16:52:50 +03002255 asn_DEF_E2AP_PDU.name,
2256 __func__,
2257 __LINE__);
aa7133@att.com1fecb052020-03-29 09:44:09 +03002258 return -1;
2259 }
aa7133@att.coma0f0db62020-04-01 16:52:50 +03002260 rmrMessageBuffer.rcvMessage->len = er.encoded;
aa7133@att.com1fecb052020-03-29 09:44:09 +03002261 return 0;
2262}
2263
ss412g1a79bdf2019-10-24 12:03:05 +03002264/**
2265 *
ss412g1a79bdf2019-10-24 12:03:05 +03002266 * @param sctpMap
2267 * @param rmrMessageBuffer
2268 * @param ts
ss412g1a79bdf2019-10-24 12:03:05 +03002269 * @return
2270 */
aa7133@att.com6680c3b2020-03-22 12:06:58 +02002271int receiveXappMessages(Sctp_Map_t *sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +03002272 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +02002273 struct timespec &ts) {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002274 int loglevel = mdclog_level_get();
ss412g1a79bdf2019-10-24 12:03:05 +03002275 if (rmrMessageBuffer.rcvMessage == nullptr) {
2276 //we have error
2277 mdclog_write(MDCLOG_ERR, "RMR Allocation message, %s", strerror(errno));
ss412g1a79bdf2019-10-24 12:03:05 +03002278 return -1;
2279 }
2280
aa7133@att.comf13529a2020-08-27 11:21:16 +03002281// if (loglevel >= MDCLOG_DEBUG) {
2282// mdclog_write(MDCLOG_DEBUG, "Call to rmr_rcv_msg");
2283// }
ss412g1a79bdf2019-10-24 12:03:05 +03002284 rmrMessageBuffer.rcvMessage = rmr_rcv_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.rcvMessage);
2285 if (rmrMessageBuffer.rcvMessage == nullptr) {
aa7133@att.com409f2232020-10-29 13:06:45 +03002286 mdclog_write(MDCLOG_ERR, "RMR Receiving message with null pointer, Reallocated rmr message buffer");
ss412g1a79bdf2019-10-24 12:03:05 +03002287 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
ss412g1a79bdf2019-10-24 12:03:05 +03002288 return -2;
2289 }
2290 ReportingMessages_t message;
2291 message.message.direction = 'D';
2292 message.message.time.tv_nsec = ts.tv_nsec;
2293 message.message.time.tv_sec = ts.tv_sec;
2294
2295 // get message payload
2296 //auto msgData = msg->payload;
dhirajverma60f8bef2021-06-29 07:22:58 -04002297#ifdef UNIT_TEST
2298 rmrMessageBuffer.rcvMessage->state = 0;
2299#endif
ss412g1a79bdf2019-10-24 12:03:05 +03002300 if (rmrMessageBuffer.rcvMessage->state != 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +03002301 mdclog_write(MDCLOG_ERR, "RMR Receiving message with stat = %d", rmrMessageBuffer.rcvMessage->state);
ss412g1a79bdf2019-10-24 12:03:05 +03002302 return -1;
2303 }
aa7133@att.com1fecb052020-03-29 09:44:09 +03002304 rmr_get_meid(rmrMessageBuffer.rcvMessage, (unsigned char *)message.message.enodbName);
ss412g4aeeda22020-06-16 14:12:41 +03002305 message.peerInfo = (ConnectedCU_t *) sctpMap->find(message.message.enodbName);
2306 if (message.peerInfo == nullptr) {
2307 auto type = rmrMessageBuffer.rcvMessage->mtype;
2308 switch (type) {
2309 case RIC_SCTP_CLEAR_ALL:
2310 case E2_TERM_KEEP_ALIVE_REQ:
2311 case RIC_HEALTH_CHECK_REQ:
2312 break;
2313 default:
2314 mdclog_write(MDCLOG_ERR, "Failed to send message no CU entry %s", message.message.enodbName);
2315 return -1;
2316 }
2317 }
2318
aa7133@att.comde40d1c2020-08-26 16:19:20 +03002319 if (rmrMessageBuffer.rcvMessage->mtype != RIC_HEALTH_CHECK_REQ) {
2320 num_of_XAPP_messages.fetch_add(1, std::memory_order_release);
2321
2322 }
ss412g1a79bdf2019-10-24 12:03:05 +03002323 switch (rmrMessageBuffer.rcvMessage->mtype) {
aa7133@att.combe92d712020-03-18 17:30:39 +02002324 case RIC_E2_SETUP_RESP : {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002325 if (loglevel >= MDCLOG_DEBUG) {
2326 mdclog_write(MDCLOG_DEBUG, "RIC_E2_SETUP_RESP");
2327 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002328 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
aa7133@att.com1fecb052020-03-29 09:44:09 +03002329 break;
2330 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002331 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2332 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002333 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2334 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_SETUP_RESP");
2335 return -6;
ss412g1a79bdf2019-10-24 12:03:05 +03002336 }
2337 break;
2338 }
aa7133@att.combe92d712020-03-18 17:30:39 +02002339 case RIC_E2_SETUP_FAILURE : {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002340 if (loglevel >= MDCLOG_DEBUG) {
2341 mdclog_write(MDCLOG_DEBUG, "RIC_E2_SETUP_FAILURE");
2342 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002343 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
aa7133@att.com1fecb052020-03-29 09:44:09 +03002344 break;
2345 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002346 message.peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2347 message.peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002348 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2349 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_SETUP_FAILURE");
2350 return -6;
ss412g1a79bdf2019-10-24 12:03:05 +03002351 }
2352 break;
2353 }
aa7133@att.combe92d712020-03-18 17:30:39 +02002354 case RIC_ERROR_INDICATION: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002355 if (loglevel >= MDCLOG_DEBUG) {
2356 mdclog_write(MDCLOG_DEBUG, "RIC_ERROR_INDICATION");
2357 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002358 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_ErrorIndication]->Increment();
2359 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_ErrorIndication]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002360 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2361 mdclog_write(MDCLOG_ERR, "Failed to send RIC_ERROR_INDICATION");
ss412g1a79bdf2019-10-24 12:03:05 +03002362 return -6;
2363 }
2364 break;
2365 }
2366 case RIC_SUB_REQ: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002367 if (loglevel >= MDCLOG_DEBUG) {
2368 mdclog_write(MDCLOG_DEBUG, "RIC_SUB_REQ");
2369 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002370 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
2371 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002372 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002373 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SUB_REQ");
ss412g1a79bdf2019-10-24 12:03:05 +03002374 return -6;
2375 }
2376 break;
2377 }
2378 case RIC_SUB_DEL_REQ: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002379 if (loglevel >= MDCLOG_DEBUG) {
2380 mdclog_write(MDCLOG_DEBUG, "RIC_SUB_DEL_REQ");
2381 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002382 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
2383 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002384 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002385 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SUB_DEL_REQ");
ss412g1a79bdf2019-10-24 12:03:05 +03002386 return -6;
2387 }
2388 break;
2389 }
2390 case RIC_CONTROL_REQ: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002391 if (loglevel >= MDCLOG_DEBUG) {
2392 mdclog_write(MDCLOG_DEBUG, "RIC_CONTROL_REQ");
2393 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002394 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
2395 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002396 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002397 mdclog_write(MDCLOG_ERR, "Failed to send RIC_CONTROL_REQ");
ss412g1a79bdf2019-10-24 12:03:05 +03002398 return -6;
2399 }
2400 break;
2401 }
2402 case RIC_SERVICE_QUERY: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002403 if (loglevel >= MDCLOG_DEBUG) {
2404 mdclog_write(MDCLOG_DEBUG, "RIC_SERVICE_QUERY");
2405 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002406 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2407 break;
2408 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002409 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceQuery]->Increment();
2410 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceQuery]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002411 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002412 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_QUERY");
ss412g1a79bdf2019-10-24 12:03:05 +03002413 return -6;
2414 }
2415 break;
2416 }
2417 case RIC_SERVICE_UPDATE_ACK: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002418 if (loglevel >= MDCLOG_DEBUG) {
2419 mdclog_write(MDCLOG_DEBUG, "RIC_SERVICE_UPDATE_ACK");
2420 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002421 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
aa7133@att.com54885fe2020-10-29 17:24:49 +03002422 mdclog_write(MDCLOG_ERR, "error in PER_FromXML");
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002423 break;
2424 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002425 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
aa7133@att.com52771912020-10-29 17:45:01 +03002426 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.com54885fe2020-10-29 17:24:49 +03002427 if (loglevel >= MDCLOG_DEBUG) {
2428 mdclog_write(MDCLOG_DEBUG, "Before sending to CU");
2429 }
aa7133@att.combe92d712020-03-18 17:30:39 +02002430 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002431 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_UPDATE_ACK");
ss412g1a79bdf2019-10-24 12:03:05 +03002432 return -6;
2433 }
2434 break;
2435 }
2436 case RIC_SERVICE_UPDATE_FAILURE: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002437 if (loglevel >= MDCLOG_DEBUG) {
2438 mdclog_write(MDCLOG_DEBUG, "RIC_SERVICE_UPDATE_FAILURE");
2439 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002440 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2441 break;
2442 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002443 message.peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
aa7133@att.com7c07fee2020-10-29 17:54:38 +03002444 message.peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002445 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002446 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_UPDATE_FAILURE");
ss412g1a79bdf2019-10-24 12:03:05 +03002447 return -6;
2448 }
2449 break;
2450 }
ss412g4aeeda22020-06-16 14:12:41 +03002451 case RIC_E2_RESET_REQ: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002452 if (loglevel >= MDCLOG_DEBUG) {
2453 mdclog_write(MDCLOG_DEBUG, "RIC_E2_RESET_REQ");
2454 }
ss412g4aeeda22020-06-16 14:12:41 +03002455 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2456 break;
2457 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002458 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2459 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002460 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g4aeeda22020-06-16 14:12:41 +03002461 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_RESET");
ss412g1a79bdf2019-10-24 12:03:05 +03002462 return -6;
2463 }
2464 break;
2465 }
ss412g4aeeda22020-06-16 14:12:41 +03002466 case RIC_E2_RESET_RESP: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002467 if (loglevel >= MDCLOG_DEBUG) {
2468 mdclog_write(MDCLOG_DEBUG, "RIC_E2_RESET_RESP");
2469 }
ss412g4aeeda22020-06-16 14:12:41 +03002470 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2471 break;
2472 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002473 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2474 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002475 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g4aeeda22020-06-16 14:12:41 +03002476 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_RESET_RESP");
ss412g1a79bdf2019-10-24 12:03:05 +03002477 return -6;
2478 }
2479 break;
2480 }
2481 case RIC_SCTP_CLEAR_ALL: {
2482 mdclog_write(MDCLOG_INFO, "RIC_SCTP_CLEAR_ALL");
2483 // loop on all keys and close socket and then erase all map.
2484 vector<char *> v;
2485 sctpMap->getKeys(v);
2486 for (auto const &iter : v) { //}; iter != sctpMap.end(); iter++) {
2487 if (!boost::starts_with((string) (iter), "host:") && !boost::starts_with((string) (iter), "msg:")) {
2488 auto *peerInfo = (ConnectedCU_t *) sctpMap->find(iter);
2489 if (peerInfo == nullptr) {
2490 continue;
2491 }
2492 close(peerInfo->fileDescriptor);
2493 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
2494 message.message.direction = 'D';
2495 message.message.time.tv_nsec = ts.tv_nsec;
2496 message.message.time.tv_sec = ts.tv_sec;
2497
2498 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
2499 snprintf((char *)rmrMessageBuffer.sendMessage->payload,
aa7133@att.com96c9b882020-03-25 14:50:40 +02002500 256,
2501 "%s|RIC_SCTP_CLEAR_ALL",
2502 peerInfo->enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03002503 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
2504 mdclog_write(MDCLOG_INFO, "%s", message.message.asndata);
aa7133@att.combe92d712020-03-18 17:30:39 +02002505 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002506 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
2507 }
2508 free(peerInfo);
2509 }
2510 }
2511
2512 sleep(1);
2513 sctpMap->clear();
2514 break;
2515 }
ss412g3bac2da2020-01-05 11:52:19 +02002516 case E2_TERM_KEEP_ALIVE_REQ: {
2517 // send message back
ss412g3bac2da2020-01-05 11:52:19 +02002518 rmr_bytes2payload(rmrMessageBuffer.sendMessage,
aa7133@att.com96c9b882020-03-25 14:50:40 +02002519 (unsigned char *)rmrMessageBuffer.ka_message,
2520 rmrMessageBuffer.ka_message_len);
ss412g3bac2da2020-01-05 11:52:19 +02002521 rmrMessageBuffer.sendMessage->mtype = E2_TERM_KEEP_ALIVE_RESP;
2522 rmrMessageBuffer.sendMessage->state = 0;
2523 static unsigned char tx[32];
2524 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2525 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, txLen);
dhirajverma60f8bef2021-06-29 07:22:58 -04002526#ifndef UNIT_TEST
ss412g3bac2da2020-01-05 11:52:19 +02002527 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
dhirajverma60f8bef2021-06-29 07:22:58 -04002528#endif
ss412g3bac2da2020-01-05 11:52:19 +02002529 if (rmrMessageBuffer.sendMessage == nullptr) {
2530 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
aa7133@att.com77fff432020-01-27 12:01:20 +02002531 mdclog_write(MDCLOG_ERR, "Failed to send E2_TERM_KEEP_ALIVE_RESP RMR message returned NULL");
ss412g3bac2da2020-01-05 11:52:19 +02002532 } else if (rmrMessageBuffer.sendMessage->state != 0) {
aa7133@att.com77fff432020-01-27 12:01:20 +02002533 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 +02002534 rmrMessageBuffer.sendMessage->state, translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str());
aa7133@att.comf13529a2020-08-27 11:21:16 +03002535 } else if (loglevel >= MDCLOG_DEBUG) {
aa7133@att.combc110772020-03-25 09:45:14 +02002536 mdclog_write(MDCLOG_DEBUG, "Got Keep Alive Request send : %s", rmrMessageBuffer.ka_message);
ss412g3bac2da2020-01-05 11:52:19 +02002537 }
aa7133@att.com342843e2020-01-26 12:53:34 +02002538
ss412g3bac2da2020-01-05 11:52:19 +02002539 break;
2540 }
ss412g4aeeda22020-06-16 14:12:41 +03002541 case RIC_HEALTH_CHECK_REQ: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002542 static int counter = 0;
ss412g4aeeda22020-06-16 14:12:41 +03002543 // send message back
aa7133@att.comc18fe532020-06-30 12:40:18 +03002544 rmr_bytes2payload(rmrMessageBuffer.rcvMessage,
ss412g4aeeda22020-06-16 14:12:41 +03002545 (unsigned char *)"OK",
2546 2);
aa7133@att.comc18fe532020-06-30 12:40:18 +03002547 rmrMessageBuffer.rcvMessage->mtype = RIC_HEALTH_CHECK_RESP;
2548 rmrMessageBuffer.rcvMessage->state = 0;
ss412g4aeeda22020-06-16 14:12:41 +03002549 static unsigned char tx[32];
2550 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
aa7133@att.comc18fe532020-06-30 12:40:18 +03002551 rmr_bytes2xact(rmrMessageBuffer.rcvMessage, tx, txLen);
2552 rmrMessageBuffer.rcvMessage = rmr_rts_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.rcvMessage);
2553 //rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2554 if (rmrMessageBuffer.rcvMessage == nullptr) {
2555 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
ss412g4aeeda22020-06-16 14:12:41 +03002556 mdclog_write(MDCLOG_ERR, "Failed to send RIC_HEALTH_CHECK_RESP RMR message returned NULL");
aa7133@att.comc18fe532020-06-30 12:40:18 +03002557 } else if (rmrMessageBuffer.rcvMessage->state != 0) {
ss412g4aeeda22020-06-16 14:12:41 +03002558 mdclog_write(MDCLOG_ERR, "Failed to send RIC_HEALTH_CHECK_RESP, on RMR state = %d ( %s)",
aa7133@att.comc18fe532020-06-30 12:40:18 +03002559 rmrMessageBuffer.rcvMessage->state, translateRmrErrorMessages(rmrMessageBuffer.rcvMessage->state).c_str());
aa7133@att.comf13529a2020-08-27 11:21:16 +03002560 } else if (loglevel >= MDCLOG_DEBUG && ++counter % 100 == 0) {
2561 mdclog_write(MDCLOG_DEBUG, "Got %d RIC_HEALTH_CHECK_REQ Request send : OK", counter);
ss412g4aeeda22020-06-16 14:12:41 +03002562 }
2563
2564 break;
2565 }
2566
ss412g1a79bdf2019-10-24 12:03:05 +03002567 default:
aa7133@att.com409f2232020-10-29 13:06:45 +03002568 mdclog_write(MDCLOG_WARN, "Message Type : %d is not supported", rmrMessageBuffer.rcvMessage->mtype);
ss412g1a79bdf2019-10-24 12:03:05 +03002569 message.message.asndata = rmrMessageBuffer.rcvMessage->payload;
2570 message.message.asnLength = rmrMessageBuffer.rcvMessage->len;
2571 message.message.time.tv_nsec = ts.tv_nsec;
2572 message.message.time.tv_sec = ts.tv_sec;
2573 message.message.messageType = rmrMessageBuffer.rcvMessage->mtype;
2574
2575 buildJsonMessage(message);
2576
2577
ss412g1a79bdf2019-10-24 12:03:05 +03002578 return -7;
2579 }
2580 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2581 mdclog_write(MDCLOG_DEBUG, "EXIT OK from %s", __FUNCTION__);
2582 }
ss412g1a79bdf2019-10-24 12:03:05 +03002583 return 0;
2584}
2585
2586/**
2587 * Send message to the CU that is not expecting for successful or unsuccessful results
2588 * @param messageBuffer
2589 * @param message
2590 * @param failedMsgId
2591 * @param sctpMap
ss412g1a79bdf2019-10-24 12:03:05 +03002592 * @return
2593 */
2594int sendDirectionalSctpMsg(RmrMessagesBuffer_t &messageBuffer,
2595 ReportingMessages_t &message,
2596 int failedMsgId,
aa7133@att.combe92d712020-03-18 17:30:39 +02002597 Sctp_Map_t *sctpMap) {
aa7133@att.com409f2232020-10-29 13:06:45 +03002598 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2599 mdclog_write(MDCLOG_DEBUG, "send message: %d to %s address", message.message.messageType, message.message.enodbName);
2600 }
ss412g1a79bdf2019-10-24 12:03:05 +03002601
aa7133@att.combe92d712020-03-18 17:30:39 +02002602 getRequestMetaData(message, messageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03002603 if (mdclog_level_get() >= MDCLOG_INFO) {
2604 mdclog_write(MDCLOG_INFO, "send message to %s address", message.message.enodbName);
2605 }
2606
aa7133@att.combe92d712020-03-18 17:30:39 +02002607 auto rc = sendMessagetoCu(sctpMap, messageBuffer, message, failedMsgId);
ss412g1a79bdf2019-10-24 12:03:05 +03002608 return rc;
2609}
2610
2611/**
2612 *
2613 * @param sctpMap
2614 * @param messageBuffer
2615 * @param message
2616 * @param failedMesgId
ss412g1a79bdf2019-10-24 12:03:05 +03002617 * @return
2618 */
2619int sendMessagetoCu(Sctp_Map_t *sctpMap,
2620 RmrMessagesBuffer_t &messageBuffer,
2621 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +02002622 int failedMesgId) {
ss412g1a79bdf2019-10-24 12:03:05 +03002623 // get the FD
2624 message.message.messageType = messageBuffer.rcvMessage->mtype;
ss412g4aeeda22020-06-16 14:12:41 +03002625 auto rc = sendSctpMsg(message.peerInfo, message, sctpMap);
ss412g1a79bdf2019-10-24 12:03:05 +03002626 return rc;
2627}
2628
ss412g1a79bdf2019-10-24 12:03:05 +03002629
2630/**
2631 *
2632 * @param epoll_fd
2633 * @param peerInfo
2634 * @param events
2635 * @param sctpMap
2636 * @param enodbName
2637 * @param msgType
ss412g1a79bdf2019-10-24 12:03:05 +03002638 * @return
2639 */
2640int addToEpoll(int epoll_fd,
2641 ConnectedCU_t *peerInfo,
2642 uint32_t events,
2643 Sctp_Map_t *sctpMap,
2644 char *enodbName,
aa7133@att.combe92d712020-03-18 17:30:39 +02002645 int msgType) {
ss412g1a79bdf2019-10-24 12:03:05 +03002646 // Add to Epol
2647 struct epoll_event event{};
2648 event.data.ptr = peerInfo;
2649 event.events = events;
2650 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, peerInfo->fileDescriptor, &event) < 0) {
2651 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com409f2232020-10-29 13:06:45 +03002652 mdclog_write(MDCLOG_DEBUG, "epoll_ctl EPOLL_CTL_ADD (may check not to quit here), %s, %s %d",
ss412g1a79bdf2019-10-24 12:03:05 +03002653 strerror(errno), __func__, __LINE__);
2654 }
2655 close(peerInfo->fileDescriptor);
aa7133@att.com84bd3342020-03-16 18:04:57 +02002656 if (enodbName != nullptr) {
aa7133@att.combe92d712020-03-18 17:30:39 +02002657 cleanHashEntry(peerInfo, sctpMap);
aa7133@att.com84bd3342020-03-16 18:04:57 +02002658 char key[MAX_ENODB_NAME_SIZE * 2];
2659 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", enodbName, msgType);
2660 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2661 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
2662 }
2663 auto tmp = sctpMap->find(key);
2664 if (tmp) {
2665 free(tmp);
2666 sctpMap->erase(key);
2667 }
2668 } else {
2669 peerInfo->enodbName[0] = 0;
ss412g1a79bdf2019-10-24 12:03:05 +03002670 }
aa7133@att.com409f2232020-10-29 13:06:45 +03002671 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may check not to quit here)");
ss412g1a79bdf2019-10-24 12:03:05 +03002672 return -1;
2673 }
ss412g1a79bdf2019-10-24 12:03:05 +03002674 return 0;
2675}
2676
2677/**
2678 *
2679 * @param epoll_fd
2680 * @param peerInfo
2681 * @param events
2682 * @param sctpMap
2683 * @param enodbName
2684 * @param msgType
ss412g1a79bdf2019-10-24 12:03:05 +03002685 * @return
2686 */
2687int modifyToEpoll(int epoll_fd,
2688 ConnectedCU_t *peerInfo,
2689 uint32_t events,
2690 Sctp_Map_t *sctpMap,
2691 char *enodbName,
aa7133@att.combe92d712020-03-18 17:30:39 +02002692 int msgType) {
ss412g1a79bdf2019-10-24 12:03:05 +03002693 // Add to Epol
2694 struct epoll_event event{};
2695 event.data.ptr = peerInfo;
2696 event.events = events;
2697 if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, peerInfo->fileDescriptor, &event) < 0) {
2698 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com409f2232020-10-29 13:06:45 +03002699 mdclog_write(MDCLOG_DEBUG, "epoll_ctl EPOLL_CTL_MOD (may check not to quit here), %s, %s %d",
ss412g1a79bdf2019-10-24 12:03:05 +03002700 strerror(errno), __func__, __LINE__);
2701 }
2702 close(peerInfo->fileDescriptor);
aa7133@att.combe92d712020-03-18 17:30:39 +02002703 cleanHashEntry(peerInfo, sctpMap);
ss412g1a79bdf2019-10-24 12:03:05 +03002704 char key[MAX_ENODB_NAME_SIZE * 2];
2705 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", enodbName, msgType);
2706 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2707 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
2708 }
aa7133@att.com96c9b882020-03-25 14:50:40 +02002709 auto tmp = sctpMap->find(key);
ss412g1a79bdf2019-10-24 12:03:05 +03002710 if (tmp) {
2711 free(tmp);
2712 }
2713 sctpMap->erase(key);
aa7133@att.com409f2232020-10-29 13:06:45 +03002714 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may check not to quit here)");
ss412g1a79bdf2019-10-24 12:03:05 +03002715 return -1;
2716 }
ss412g1a79bdf2019-10-24 12:03:05 +03002717 return 0;
2718}
2719
2720
aa7133@att.combe92d712020-03-18 17:30:39 +02002721int sendRmrMessage(RmrMessagesBuffer_t &rmrMessageBuffer, ReportingMessages_t &message) {
ss412g1a79bdf2019-10-24 12:03:05 +03002722 buildJsonMessage(message);
dhirajverma60f8bef2021-06-29 07:22:58 -04002723#ifndef UNIT_TEST
ss412g1a79bdf2019-10-24 12:03:05 +03002724 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
dhirajverma60f8bef2021-06-29 07:22:58 -04002725#else
2726 rmrMessageBuffer.sendMessage->state = RMR_ERR_RETRY;
2727#endif
ss412g1a79bdf2019-10-24 12:03:05 +03002728 if (rmrMessageBuffer.sendMessage == nullptr) {
2729 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
2730 mdclog_write(MDCLOG_ERR, "RMR failed send message returned with NULL pointer");
ss412g1a79bdf2019-10-24 12:03:05 +03002731 return -1;
2732 }
2733
2734 if (rmrMessageBuffer.sendMessage->state != 0) {
2735 char meid[RMR_MAX_MEID]{};
2736 if (rmrMessageBuffer.sendMessage->state == RMR_ERR_RETRY) {
2737 usleep(5);
2738 rmrMessageBuffer.sendMessage->state = 0;
2739 mdclog_write(MDCLOG_INFO, "RETRY sending Message type %d to Xapp from %s",
2740 rmrMessageBuffer.sendMessage->mtype,
2741 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
dhirajverma60f8bef2021-06-29 07:22:58 -04002742#ifndef UNIT_TEST
ss412g1a79bdf2019-10-24 12:03:05 +03002743 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
dhirajverma60f8bef2021-06-29 07:22:58 -04002744#endif
ss412g1a79bdf2019-10-24 12:03:05 +03002745 if (rmrMessageBuffer.sendMessage == nullptr) {
2746 mdclog_write(MDCLOG_ERR, "RMR failed send message returned with NULL pointer");
2747 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
ss412g1a79bdf2019-10-24 12:03:05 +03002748 return -1;
2749 } else if (rmrMessageBuffer.sendMessage->state != 0) {
2750 mdclog_write(MDCLOG_ERR,
2751 "Message state %s while sending request %d to Xapp from %s after retry of 10 microseconds",
2752 translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str(),
2753 rmrMessageBuffer.sendMessage->mtype,
2754 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
2755 auto rc = rmrMessageBuffer.sendMessage->state;
ss412g1a79bdf2019-10-24 12:03:05 +03002756 return rc;
2757 }
2758 } else {
2759 mdclog_write(MDCLOG_ERR, "Message state %s while sending request %d to Xapp from %s",
2760 translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str(),
2761 rmrMessageBuffer.sendMessage->mtype,
2762 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
ss412g1a79bdf2019-10-24 12:03:05 +03002763 return rmrMessageBuffer.sendMessage->state;
2764 }
2765 }
2766 return 0;
2767}
2768
2769void buildJsonMessage(ReportingMessages_t &message) {
dhirajverma60f8bef2021-06-29 07:22:58 -04002770#ifdef UNIT_TEST
2771 jsonTrace = true;
2772#endif
ss412g3bac2da2020-01-05 11:52:19 +02002773 if (jsonTrace) {
2774 message.outLen = sizeof(message.base64Data);
2775 base64::encode((const unsigned char *) message.message.asndata,
2776 (const int) message.message.asnLength,
2777 message.base64Data,
2778 message.outLen);
2779 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.comc5a15202020-04-05 19:57:01 +03002780 mdclog_write(MDCLOG_DEBUG, "Tracing: ASN length = %d, base64 message length = %d ",
ss412g3bac2da2020-01-05 11:52:19 +02002781 (int) message.message.asnLength,
2782 (int) message.outLen);
2783 }
2784
2785 snprintf(message.buffer, sizeof(message.buffer),
aa7133@att.com96c9b882020-03-25 14:50:40 +02002786 "{\"header\": {\"ts\": \"%ld.%09ld\","
2787 "\"ranName\": \"%s\","
2788 "\"messageType\": %d,"
2789 "\"direction\": \"%c\"},"
2790 "\"base64Length\": %d,"
2791 "\"asnBase64\": \"%s\"}",
2792 message.message.time.tv_sec,
2793 message.message.time.tv_nsec,
2794 message.message.enodbName,
2795 message.message.messageType,
2796 message.message.direction,
2797 (int) message.outLen,
2798 message.base64Data);
ss412g3bac2da2020-01-05 11:52:19 +02002799 static src::logger_mt &lg = my_logger::get();
2800
2801 BOOST_LOG(lg) << message.buffer;
ss412g1a79bdf2019-10-24 12:03:05 +03002802 }
ss412g1a79bdf2019-10-24 12:03:05 +03002803}
2804
2805
2806/**
2807 * take RMR error code to string
2808 * @param state
2809 * @return
2810 */
2811string translateRmrErrorMessages(int state) {
2812 string str = {};
2813 switch (state) {
2814 case RMR_OK:
2815 str = "RMR_OK - state is good";
2816 break;
2817 case RMR_ERR_BADARG:
aa7133@att.com409f2232020-10-29 13:06:45 +03002818 str = "RMR_ERR_BADARG - argument passed to function was unusable";
ss412g1a79bdf2019-10-24 12:03:05 +03002819 break;
2820 case RMR_ERR_NOENDPT:
2821 str = "RMR_ERR_NOENDPT - send//call could not find an endpoint based on msg type";
2822 break;
2823 case RMR_ERR_EMPTY:
2824 str = "RMR_ERR_EMPTY - msg received had no payload; attempt to send an empty message";
2825 break;
2826 case RMR_ERR_NOHDR:
2827 str = "RMR_ERR_NOHDR - message didn't contain a valid header";
2828 break;
2829 case RMR_ERR_SENDFAILED:
2830 str = "RMR_ERR_SENDFAILED - send failed; errno has nano reason";
2831 break;
2832 case RMR_ERR_CALLFAILED:
2833 str = "RMR_ERR_CALLFAILED - unable to send call() message";
2834 break;
2835 case RMR_ERR_NOWHOPEN:
2836 str = "RMR_ERR_NOWHOPEN - no wormholes are open";
2837 break;
2838 case RMR_ERR_WHID:
2839 str = "RMR_ERR_WHID - wormhole id was invalid";
2840 break;
2841 case RMR_ERR_OVERFLOW:
2842 str = "RMR_ERR_OVERFLOW - operation would have busted through a buffer/field size";
2843 break;
2844 case RMR_ERR_RETRY:
2845 str = "RMR_ERR_RETRY - request (send/call/rts) failed, but caller should retry (EAGAIN for wrappers)";
2846 break;
2847 case RMR_ERR_RCVFAILED:
2848 str = "RMR_ERR_RCVFAILED - receive failed (hard error)";
2849 break;
2850 case RMR_ERR_TIMEOUT:
2851 str = "RMR_ERR_TIMEOUT - message processing call timed out";
2852 break;
2853 case RMR_ERR_UNSET:
2854 str = "RMR_ERR_UNSET - the message hasn't been populated with a transport buffer";
2855 break;
2856 case RMR_ERR_TRUNC:
2857 str = "RMR_ERR_TRUNC - received message likely truncated";
2858 break;
2859 case RMR_ERR_INITFAILED:
2860 str = "RMR_ERR_INITFAILED - initialisation of something (probably message) failed";
2861 break;
2862 case RMR_ERR_NOTSUPP:
2863 str = "RMR_ERR_NOTSUPP - the request is not supported, or RMr was not initialised for the request";
2864 break;
2865 default:
2866 char buf[128]{};
2867 snprintf(buf, sizeof buf, "UNDOCUMENTED RMR_ERR : %d", state);
2868 str = buf;
2869 break;
2870 }
2871 return str;
2872}