blob: 882ffdf327bc88c574a25ad3dfdfe4dca8df13b1 [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"
aa7133@att.com7b437f72020-03-30 13:25:54 +030030
aa7133@att.com84bd3342020-03-16 18:04:57 +020031using namespace std;
32//using namespace std::placeholders;
ss412g3bac2da2020-01-05 11:52:19 +020033using namespace boost::filesystem;
ss412g4aeeda22020-06-16 14:12:41 +030034using namespace prometheus;
35
ss412g3bac2da2020-01-05 11:52:19 +020036
ss412g1a79bdf2019-10-24 12:03:05 +030037//#ifdef __cplusplus
38//extern "C"
39//{
40//#endif
41
ss412g3bac2da2020-01-05 11:52:19 +020042// need to expose without the include of gcov
43extern "C" void __gcov_flush(void);
44
45static void catch_function(int signal) {
46 __gcov_flush();
47 exit(signal);
48}
49
50
ss412g1a79bdf2019-10-24 12:03:05 +030051BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger, src::logger_mt)
52
53boost::shared_ptr<sinks::synchronous_sink<sinks::text_file_backend>> boostLogger;
ss412g3bac2da2020-01-05 11:52:19 +020054double cpuClock = 0.0;
aa7133@att.com41fcb3f2020-11-29 13:48:25 +020055bool jsonTrace = false;
ss412g1a79bdf2019-10-24 12:03:05 +030056
57void init_log() {
dhirajverma5f132832021-06-17 17:27:54 +053058 int log_change_monitor = 1;
ss412g1a79bdf2019-10-24 12:03:05 +030059 mdclog_attr_t *attr;
60 mdclog_attr_init(&attr);
61 mdclog_attr_set_ident(attr, "E2Terminator");
62 mdclog_init(attr);
dhirajverma5f132832021-06-17 17:27:54 +053063 if(mdclog_format_initialize(log_change_monitor)!=0)
64 mdclog_write(MDCLOG_ERR, "Failed to intialize MDC log format !!!");
ss412g1a79bdf2019-10-24 12:03:05 +030065 mdclog_attr_destroy(attr);
66}
ss412g3bac2da2020-01-05 11:52:19 +020067auto start_time = std::chrono::high_resolution_clock::now();
68typedef std::chrono::duration<double, std::ratio<1,1>> seconds_t;
ss412g1a79bdf2019-10-24 12:03:05 +030069
ss412g3bac2da2020-01-05 11:52:19 +020070double age() {
71 return seconds_t(std::chrono::high_resolution_clock::now() - start_time).count();
72}
73
aa7133@att.com409f2232020-10-29 13:06:45 +030074double approx_CPU_MHz(unsigned sleepTime) {
ss412g3bac2da2020-01-05 11:52:19 +020075 using namespace std::chrono_literals;
76 uint32_t aux = 0;
77 uint64_t cycles_start = rdtscp(aux);
78 double time_start = age();
aa7133@att.com409f2232020-10-29 13:06:45 +030079 std::this_thread::sleep_for(sleepTime * 1ms);
ss412g3bac2da2020-01-05 11:52:19 +020080 uint64_t elapsed_cycles = rdtscp(aux) - cycles_start;
81 double elapsed_time = age() - time_start;
82 return elapsed_cycles / elapsed_time;
83}
ss412g1a79bdf2019-10-24 12:03:05 +030084
85//std::atomic<int64_t> rmrCounter{0};
86std::atomic<int64_t> num_of_messages{0};
ss412g3bac2da2020-01-05 11:52:19 +020087std::atomic<int64_t> num_of_XAPP_messages{0};
ss412g1a79bdf2019-10-24 12:03:05 +030088static long transactionCounter = 0;
89
aa7133@att.com84bd3342020-03-16 18:04:57 +020090int buildListeningPort(sctp_params_t &sctpParams) {
aa7133@att.comf211f192020-06-23 12:50:10 +030091 sctpParams.listenFD = socket(AF_INET6, SOCK_STREAM, IPPROTO_SCTP);
92 if (sctpParams.listenFD <= 0) {
93 mdclog_write(MDCLOG_ERR, "Error Opening socket, %s", strerror(errno));
94 return -1;
95 }
96
aa7133@att.com409f2232020-10-29 13:06:45 +030097 struct sockaddr_in6 serverAddress {};
98 serverAddress.sin6_family = AF_INET6;
99 serverAddress.sin6_addr = in6addr_any;
100 serverAddress.sin6_port = htons(sctpParams.sctpPort);
101 if (bind(sctpParams.listenFD, (SA *)&serverAddress, sizeof(serverAddress)) < 0 ) {
aa7133@att.comf211f192020-06-23 12:50:10 +0300102 mdclog_write(MDCLOG_ERR, "Error binding port %d. %s", sctpParams.sctpPort, strerror(errno));
aa7133@att.com84bd3342020-03-16 18:04:57 +0200103 return -1;
104 }
105 if (setSocketNoBlocking(sctpParams.listenFD) == -1) {
106 //mdclog_write(MDCLOG_ERR, "Error binding. %s", strerror(errno));
107 return -1;
108 }
109 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300110 struct sockaddr_in6 clientAddress {};
111 socklen_t len = sizeof(clientAddress);
112 getsockname(sctpParams.listenFD, (SA *)&clientAddress, &len);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200113 char buff[1024] {};
aa7133@att.com409f2232020-10-29 13:06:45 +0300114 inet_ntop(AF_INET6, &clientAddress.sin6_addr, buff, sizeof(buff));
115 mdclog_write(MDCLOG_DEBUG, "My address: %s, port %d\n", buff, htons(clientAddress.sin6_port));
ss412g1a79bdf2019-10-24 12:03:05 +0300116 }
117
aa7133@att.com84bd3342020-03-16 18:04:57 +0200118 if (listen(sctpParams.listenFD, SOMAXCONN) < 0) {
119 mdclog_write(MDCLOG_ERR, "Error listening. %s\n", strerror(errno));
120 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200121 }
aa7133@att.com84bd3342020-03-16 18:04:57 +0200122 struct epoll_event event {};
123 event.events = EPOLLIN | EPOLLET;
124 event.data.fd = sctpParams.listenFD;
125
126 // add listening port to epoll
127 if (epoll_ctl(sctpParams.epoll_fd, EPOLL_CTL_ADD, sctpParams.listenFD, &event)) {
128 printf("Failed to add descriptor to epoll\n");
129 mdclog_write(MDCLOG_ERR, "Failed to add descriptor to epoll. %s\n", strerror(errno));
130 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200131 }
132
aa7133@att.com84bd3342020-03-16 18:04:57 +0200133 return 0;
134}
ss412g3bac2da2020-01-05 11:52:19 +0200135
aa7133@att.com84bd3342020-03-16 18:04:57 +0200136int buildConfiguration(sctp_params_t &sctpParams) {
ss412g3bac2da2020-01-05 11:52:19 +0200137 path p = (sctpParams.configFilePath + "/" + sctpParams.configFileName).c_str();
138 if (exists(p)) {
139 const int size = 2048;
140 auto fileSize = file_size(p);
141 if (fileSize > size) {
142 mdclog_write(MDCLOG_ERR, "File %s larger than %d", p.string().c_str(), size);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200143 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200144 }
145 } else {
146 mdclog_write(MDCLOG_ERR, "Configuration File %s not exists", p.string().c_str());
aa7133@att.com84bd3342020-03-16 18:04:57 +0200147 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200148 }
149
ss412g3bac2da2020-01-05 11:52:19 +0200150 ReadConfigFile conf;
151 if (conf.openConfigFile(p.string()) == -1) {
152 mdclog_write(MDCLOG_ERR, "Filed to open config file %s, %s",
153 p.string().c_str(), strerror(errno));
aa7133@att.com84bd3342020-03-16 18:04:57 +0200154 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200155 }
156 int rmrPort = conf.getIntValue("nano");
157 if (rmrPort == -1) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300158 mdclog_write(MDCLOG_ERR, "illegal RMR port ");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200159 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200160 }
161 sctpParams.rmrPort = (uint16_t)rmrPort;
162 snprintf(sctpParams.rmrAddress, sizeof(sctpParams.rmrAddress), "%d", (int) (sctpParams.rmrPort));
dhirajverma5f132832021-06-17 17:27:54 +0530163 auto tmpStr = conf.getStringValue("volume");
ss412g3bac2da2020-01-05 11:52:19 +0200164 if (tmpStr.length() == 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300165 mdclog_write(MDCLOG_ERR, "illegal volume.");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200166 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200167 }
168
ss412g1a79bdf2019-10-24 12:03:05 +0300169 char tmpLogFilespec[VOLUME_URL_SIZE];
170 tmpLogFilespec[0] = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200171 sctpParams.volume[0] = 0;
172 snprintf(sctpParams.volume, VOLUME_URL_SIZE, "%s", tmpStr.c_str());
173 // copy the name to temp file as well
174 snprintf(tmpLogFilespec, VOLUME_URL_SIZE, "%s", tmpStr.c_str());
175
176
177 // define the file name in the tmp directory under the volume
178 strcat(tmpLogFilespec,"/tmp/E2Term_%Y-%m-%d_%H-%M-%S.%N.tmpStr");
179
180 sctpParams.myIP = conf.getStringValue("local-ip");
181 if (sctpParams.myIP.length() == 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300182 mdclog_write(MDCLOG_ERR, "illegal local-ip.");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200183 return -1;
ss412g1a79bdf2019-10-24 12:03:05 +0300184 }
185
aa7133@att.combe92d712020-03-18 17:30:39 +0200186 int sctpPort = conf.getIntValue("sctp-port");
187 if (sctpPort == -1) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300188 mdclog_write(MDCLOG_ERR, "illegal SCTP port ");
aa7133@att.combe92d712020-03-18 17:30:39 +0200189 return -1;
190 }
191 sctpParams.sctpPort = (uint16_t)sctpPort;
192
aa7133@att.com84bd3342020-03-16 18:04:57 +0200193 sctpParams.fqdn = conf.getStringValue("external-fqdn");
194 if (sctpParams.fqdn.length() == 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300195 mdclog_write(MDCLOG_ERR, "illegal external-fqdn");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200196 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200197 }
ss412g1a79bdf2019-10-24 12:03:05 +0300198
aa7133@att.com342843e2020-01-26 12:53:34 +0200199 std::string pod = conf.getStringValue("pod_name");
dhirajverma6cf28612021-06-23 04:53:34 -0400200#ifndef UNIT_TEST
aa7133@att.com342843e2020-01-26 12:53:34 +0200201 if (pod.length() == 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300202 mdclog_write(MDCLOG_ERR, "illegal pod_name in config file");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200203 return -1;
aa7133@att.com342843e2020-01-26 12:53:34 +0200204 }
205 auto *podName = getenv(pod.c_str());
206 if (podName == nullptr) {
aa7133@att.com54885fe2020-10-29 17:24:49 +0300207 mdclog_write(MDCLOG_ERR, "illegal pod_name or environment variable not exists : %s", pod.c_str());
aa7133@att.com84bd3342020-03-16 18:04:57 +0200208 return -1;
aa7133@att.com342843e2020-01-26 12:53:34 +0200209
210 } else {
211 sctpParams.podName.assign(podName);
212 if (sctpParams.podName.length() == 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300213 mdclog_write(MDCLOG_ERR, "illegal pod_name");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200214 return -1;
aa7133@att.com342843e2020-01-26 12:53:34 +0200215 }
216 }
dhirajverma6cf28612021-06-23 04:53:34 -0400217#endif
ss412g3bac2da2020-01-05 11:52:19 +0200218 tmpStr = conf.getStringValue("trace");
219 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
220 if ((tmpStr.compare("start")) == 0) {
221 mdclog_write(MDCLOG_INFO, "Trace set to: start");
222 sctpParams.trace = true;
223 } else if ((tmpStr.compare("stop")) == 0) {
224 mdclog_write(MDCLOG_INFO, "Trace set to: stop");
225 sctpParams.trace = false;
aa7133@att.com41fcb3f2020-11-29 13:48:25 +0200226 } else {
227 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 +0200228 sctpParams.trace = false;
ss412g3bac2da2020-01-05 11:52:19 +0200229 }
230 jsonTrace = sctpParams.trace;
ss412g1a79bdf2019-10-24 12:03:05 +0300231
ss412g4aeeda22020-06-16 14:12:41 +0300232 sctpParams.epollTimeOut = -1;
ss412g4aeeda22020-06-16 14:12:41 +0300233
234 tmpStr = conf.getStringValue("prometheusPort");
235 if (tmpStr.length() != 0) {
236 sctpParams.prometheusPort = tmpStr;
237 }
238
aa7133@att.combe92d712020-03-18 17:30:39 +0200239 sctpParams.ka_message_length = snprintf(sctpParams.ka_message, KA_MESSAGE_SIZE, "{\"address\": \"%s:%d\","
aa7133@att.com96c9b882020-03-25 14:50:40 +0200240 "\"fqdn\": \"%s\","
241 "\"pod_name\": \"%s\"}",
aa7133@att.com342843e2020-01-26 12:53:34 +0200242 (const char *)sctpParams.myIP.c_str(),
243 sctpParams.rmrPort,
244 sctpParams.fqdn.c_str(),
245 sctpParams.podName.c_str());
246
ss412g1a79bdf2019-10-24 12:03:05 +0300247 if (mdclog_level_get() >= MDCLOG_INFO) {
dhirajverma5f132832021-06-17 17:27:54 +0530248 mdclog_write(MDCLOG_DEBUG,"RMR Port: %s", to_string(sctpParams.rmrPort).c_str());
249 mdclog_write(MDCLOG_DEBUG,"LogLevel: %s", to_string(sctpParams.logLevel).c_str());
250 mdclog_write(MDCLOG_DEBUG,"volume: %s", sctpParams.volume);
251 mdclog_write(MDCLOG_DEBUG,"tmpLogFilespec: %s", tmpLogFilespec);
252 mdclog_write(MDCLOG_DEBUG,"my ip: %s", sctpParams.myIP.c_str());
253 mdclog_write(MDCLOG_DEBUG,"pod name: %s", sctpParams.podName.c_str());
ss412g1a79bdf2019-10-24 12:03:05 +0300254
aa7133@att.com342843e2020-01-26 12:53:34 +0200255 mdclog_write(MDCLOG_INFO, "running parameters for instance : %s", sctpParams.ka_message);
ss412g1a79bdf2019-10-24 12:03:05 +0300256 }
ss412g1a79bdf2019-10-24 12:03:05 +0300257
258 // Files written to the current working directory
259 boostLogger = logging::add_file_log(
ss412g3bac2da2020-01-05 11:52:19 +0200260 keywords::file_name = tmpLogFilespec, // to temp directory
ss412g1a79bdf2019-10-24 12:03:05 +0300261 keywords::rotation_size = 10 * 1024 * 1024,
262 keywords::time_based_rotation = sinks::file::rotation_at_time_interval(posix_time::hours(1)),
263 keywords::format = "%Message%"
ss412g3bac2da2020-01-05 11:52:19 +0200264 //keywords::format = "[%TimeStamp%]: %Message%" // use each tmpStr with time stamp
ss412g1a79bdf2019-10-24 12:03:05 +0300265 );
266
aa7133@att.com54885fe2020-10-29 17:24:49 +0300267 // Setup a destination folder for collecting rotated (closed) files --since the same volume can use rename()
ss412g1a79bdf2019-10-24 12:03:05 +0300268 boostLogger->locked_backend()->set_file_collector(sinks::file::make_collector(
ss412g3bac2da2020-01-05 11:52:19 +0200269 keywords::target = sctpParams.volume
ss412g1a79bdf2019-10-24 12:03:05 +0300270 ));
271
272 // Upon restart, scan the directory for files matching the file_name pattern
273 boostLogger->locked_backend()->scan_for_files();
274
ss412g3bac2da2020-01-05 11:52:19 +0200275 // Enable auto-flushing after each tmpStr record written
ss412g1a79bdf2019-10-24 12:03:05 +0300276 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com84bd3342020-03-16 18:04:57 +0200277 boostLogger->locked_backend()->auto_flush(true);
278 }
279
280 return 0;
281}
282
aa7133@att.coma4c80012020-06-24 16:36:47 +0300283void startPrometheus(sctp_params_t &sctpParams) {
284 sctpParams.prometheusFamily = &BuildCounter()
285 .Name("E2T")
286 .Help("E2T message counter")
287 .Labels({{"POD_NAME", sctpParams.podName}})
288 .Register(*sctpParams.prometheusRegistry);
aa7133@att.com16c799b2020-03-24 18:01:51 +0200289
aa7133@att.com54885fe2020-10-29 17:24:49 +0300290 string prometheusPath = sctpParams.prometheusPort + "," + "[::]:" + sctpParams.prometheusPort;
aa7133@att.comb92748d2020-06-29 11:52:30 +0300291 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com54885fe2020-10-29 17:24:49 +0300292 mdclog_write(MDCLOG_DEBUG, "Start Prometheus Pull mode on %s", prometheusPath.c_str());
aa7133@att.coma4c80012020-06-24 16:36:47 +0300293 }
aa7133@att.com54885fe2020-10-29 17:24:49 +0300294 sctpParams.prometheusExposer = new Exposer(prometheusPath, 1);
aa7133@att.comb92748d2020-06-29 11:52:30 +0300295 sctpParams.prometheusExposer->RegisterCollectable(sctpParams.prometheusRegistry);
aa7133@att.coma4c80012020-06-24 16:36:47 +0300296}
dhirajverma31768a92021-05-06 12:38:49 +0530297#ifndef UNIT_TEST
dhirajverma5f132832021-06-17 17:27:54 +0530298
aa7133@att.com84bd3342020-03-16 18:04:57 +0200299int main(const int argc, char **argv) {
300 sctp_params_t sctpParams;
dhirajverma6cf28612021-06-23 04:53:34 -0400301#else
302 int e2_test_main(const int argc, char **argv, sctp_params_t &sctpParams) {
303#endif
aa7133@att.com84bd3342020-03-16 18:04:57 +0200304
aa7133@att.com84bd3342020-03-16 18:04:57 +0200305 {
306 std::random_device device{};
307 std::mt19937 generator(device());
308 std::uniform_int_distribution<long> distribution(1, (long) 1e12);
309 transactionCounter = distribution(generator);
310 }
311
312// uint64_t st = 0;
313// uint32_t aux1 = 0;
314// st = rdtscp(aux1);
315
316 unsigned num_cpus = std::thread::hardware_concurrency();
317 init_log();
aa7133@att.com84bd3342020-03-16 18:04:57 +0200318 if (std::signal(SIGINT, catch_function) == SIG_ERR) {
319 mdclog_write(MDCLOG_ERR, "Error initializing SIGINT");
320 exit(1);
321 }
322 if (std::signal(SIGABRT, catch_function)== SIG_ERR) {
323 mdclog_write(MDCLOG_ERR, "Error initializing SIGABRT");
324 exit(1);
325 }
326 if (std::signal(SIGTERM, catch_function)== SIG_ERR) {
327 mdclog_write(MDCLOG_ERR, "Error initializing SIGTERM");
328 exit(1);
329 }
330
331 cpuClock = approx_CPU_MHz(100);
332
333 mdclog_write(MDCLOG_DEBUG, "CPU speed %11.11f", cpuClock);
334
335 auto result = parse(argc, argv, sctpParams);
336
337 if (buildConfiguration(sctpParams) != 0) {
338 exit(-1);
ss412g1a79bdf2019-10-24 12:03:05 +0300339 }
340
ss412g4aeeda22020-06-16 14:12:41 +0300341 //auto registry = std::make_shared<Registry>();
342 sctpParams.prometheusRegistry = std::make_shared<Registry>();
343
aa7133@att.com409f2232020-10-29 13:06:45 +0300344 //sctpParams.prometheusFamily = new Family<Counter>("E2T", "E2T message counter", {{"E", sctpParams.podName}});
ss412g4aeeda22020-06-16 14:12:41 +0300345
aa7133@att.coma4c80012020-06-24 16:36:47 +0300346 startPrometheus(sctpParams);
ss412g4aeeda22020-06-16 14:12:41 +0300347
ss412g1a79bdf2019-10-24 12:03:05 +0300348 // start epoll
ss412g3bac2da2020-01-05 11:52:19 +0200349 sctpParams.epoll_fd = epoll_create1(0);
350 if (sctpParams.epoll_fd == -1) {
ss412g1a79bdf2019-10-24 12:03:05 +0300351 mdclog_write(MDCLOG_ERR, "failed to open epoll descriptor");
352 exit(-1);
353 }
dhirajverma6cf28612021-06-23 04:53:34 -0400354#ifndef UNIT_TEST
aa7133@att.combe92d712020-03-18 17:30:39 +0200355 getRmrContext(sctpParams);
dhirajverma6cf28612021-06-23 04:53:34 -0400356#endif
ss412g3bac2da2020-01-05 11:52:19 +0200357 if (sctpParams.rmrCtx == nullptr) {
358 close(sctpParams.epoll_fd);
ss412g1a79bdf2019-10-24 12:03:05 +0300359 exit(-1);
360 }
361
ss412g3bac2da2020-01-05 11:52:19 +0200362 if (buildInotify(sctpParams) == -1) {
363 close(sctpParams.rmrListenFd);
364 rmr_close(sctpParams.rmrCtx);
365 close(sctpParams.epoll_fd);
366 exit(-1);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200367 }
368
369 if (buildListeningPort(sctpParams) != 0) {
370 close(sctpParams.rmrListenFd);
371 rmr_close(sctpParams.rmrCtx);
372 close(sctpParams.epoll_fd);
373 exit(-1);
374 }
ss412g3bac2da2020-01-05 11:52:19 +0200375
376 sctpParams.sctpMap = new mapWrapper();
ss412g1a79bdf2019-10-24 12:03:05 +0300377
378 std::vector<std::thread> threads(num_cpus);
379// std::vector<std::thread> threads;
380
aa7133@att.com24e9fa42020-11-13 20:32:53 +0200381 num_cpus = 3;
ss412g1a79bdf2019-10-24 12:03:05 +0300382 for (unsigned int i = 0; i < num_cpus; i++) {
ss412g3bac2da2020-01-05 11:52:19 +0200383 threads[i] = std::thread(listener, &sctpParams);
ss412g1a79bdf2019-10-24 12:03:05 +0300384
385 cpu_set_t cpuset;
386 CPU_ZERO(&cpuset);
387 CPU_SET(i, &cpuset);
388 int rc = pthread_setaffinity_np(threads[i].native_handle(), sizeof(cpu_set_t), &cpuset);
389 if (rc != 0) {
390 mdclog_write(MDCLOG_ERR, "Error calling pthread_setaffinity_np: %d", rc);
391 }
ss412g1a79bdf2019-10-24 12:03:05 +0300392 }
393
aa7133@att.com16c799b2020-03-24 18:01:51 +0200394
ss412g3bac2da2020-01-05 11:52:19 +0200395 //loop over term_init until first message from xApp
396 handleTermInit(sctpParams);
397
398 for (auto &t : threads) {
399 t.join();
400 }
401
ss412g3bac2da2020-01-05 11:52:19 +0200402 return 0;
403}
ss412g3bac2da2020-01-05 11:52:19 +0200404void handleTermInit(sctp_params_t &sctpParams) {
405 sendTermInit(sctpParams);
ss412g1a79bdf2019-10-24 12:03:05 +0300406 //send to e2 manager init of e2 term
407 //E2_TERM_INIT
ss412g1a79bdf2019-10-24 12:03:05 +0300408
ss412g3bac2da2020-01-05 11:52:19 +0200409 int count = 0;
410 while (true) {
411 auto xappMessages = num_of_XAPP_messages.load(std::memory_order_acquire);
412 if (xappMessages > 0) {
413 if (mdclog_level_get() >= MDCLOG_INFO) {
aa7133@att.com54885fe2020-10-29 17:24:49 +0300414 mdclog_write(MDCLOG_INFO, "Got a message from some application, stop sending E2_TERM_INIT");
ss412g3bac2da2020-01-05 11:52:19 +0200415 }
416 return;
417 }
418 usleep(100000);
419 count++;
420 if (count % 1000 == 0) {
421 mdclog_write(MDCLOG_ERR, "GOT No messages from any xApp");
422 sendTermInit(sctpParams);
423 }
424 }
425}
426
427void sendTermInit(sctp_params_t &sctpParams) {
428 rmr_mbuf_t *msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.ka_message_length);
ss412g1a79bdf2019-10-24 12:03:05 +0300429 auto count = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200430 while (true) {
ss412g1a79bdf2019-10-24 12:03:05 +0300431 msg->mtype = E2_TERM_INIT;
432 msg->state = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200433 rmr_bytes2payload(msg, (unsigned char *)sctpParams.ka_message, sctpParams.ka_message_length);
ss412g1a79bdf2019-10-24 12:03:05 +0300434 static unsigned char tx[32];
435 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
436 rmr_bytes2xact(msg, tx, txLen);
ss412g3bac2da2020-01-05 11:52:19 +0200437 msg = rmr_send_msg(sctpParams.rmrCtx, msg);
ss412g1a79bdf2019-10-24 12:03:05 +0300438 if (msg == nullptr) {
aa7133@att.comc9575aa2020-04-02 11:40:09 +0300439 msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.ka_message_length);
ss412g1a79bdf2019-10-24 12:03:05 +0300440 } else if (msg->state == 0) {
ss412g1a79bdf2019-10-24 12:03:05 +0300441 rmr_free_msg(msg);
ss412g3bac2da2020-01-05 11:52:19 +0200442 if (mdclog_level_get() >= MDCLOG_INFO) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300443 mdclog_write(MDCLOG_INFO, "E2_TERM_INIT successfully sent ");
ss412g3bac2da2020-01-05 11:52:19 +0200444 }
445 return;
ss412g1a79bdf2019-10-24 12:03:05 +0300446 } else {
447 if (count % 100 == 0) {
aa7133@att.com11b17d42020-03-22 11:35:16 +0200448 mdclog_write(MDCLOG_ERR, "Error sending E2_TERM_INIT cause : %s ", translateRmrErrorMessages(msg->state).c_str());
ss412g1a79bdf2019-10-24 12:03:05 +0300449 }
450 sleep(1);
451 }
452 count++;
453 }
ss412g3bac2da2020-01-05 11:52:19 +0200454}
455
456/**
457 *
458 * @param argc
459 * @param argv
460 * @param sctpParams
461 * @return
462 */
463cxxopts::ParseResult parse(int argc, char *argv[], sctp_params_t &sctpParams) {
464 cxxopts::Options options(argv[0], "e2 term help");
465 options.positional_help("[optional args]").show_positional_help();
466 options.allow_unrecognised_options().add_options()
467 ("p,path", "config file path", cxxopts::value<std::string>(sctpParams.configFilePath)->default_value("config"))
468 ("f,file", "config file name", cxxopts::value<std::string>(sctpParams.configFileName)->default_value("config.conf"))
469 ("h,help", "Print help");
470
aa7133@att.comde40d1c2020-08-26 16:19:20 +0300471 auto result = options.parse(argc, (const char **&)argv);
ss412g3bac2da2020-01-05 11:52:19 +0200472
473 if (result.count("help")) {
474 std::cout << options.help({""}) << std::endl;
475 exit(0);
476 }
477 return result;
478}
479
480/**
481 *
482 * @param sctpParams
483 * @return -1 failed 0 success
484 */
485int buildInotify(sctp_params_t &sctpParams) {
486 sctpParams.inotifyFD = inotify_init1(IN_NONBLOCK);
487 if (sctpParams.inotifyFD == -1) {
488 mdclog_write(MDCLOG_ERR, "Failed to init inotify (inotify_init1) %s", strerror(errno));
ss412g3bac2da2020-01-05 11:52:19 +0200489 return -1;
ss412g1a79bdf2019-10-24 12:03:05 +0300490 }
491
ss412g3bac2da2020-01-05 11:52:19 +0200492 sctpParams.inotifyWD = inotify_add_watch(sctpParams.inotifyFD,
aa7133@att.com96c9b882020-03-25 14:50:40 +0200493 (const char *)sctpParams.configFilePath.c_str(),
aa7133@att.com84bd3342020-03-16 18:04:57 +0200494 (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 +0200495 if (sctpParams.inotifyWD == -1) {
496 mdclog_write(MDCLOG_ERR, "Failed to add directory : %s to inotify (inotify_add_watch) %s",
aa7133@att.com96c9b882020-03-25 14:50:40 +0200497 sctpParams.configFilePath.c_str(),
498 strerror(errno));
ss412g3bac2da2020-01-05 11:52:19 +0200499 close(sctpParams.inotifyFD);
500 return -1;
501 }
502
503 struct epoll_event event{};
504 event.events = (EPOLLIN);
505 event.data.fd = sctpParams.inotifyFD;
506 // add listening RMR FD to epoll
507 if (epoll_ctl(sctpParams.epoll_fd, EPOLL_CTL_ADD, sctpParams.inotifyFD, &event)) {
508 mdclog_write(MDCLOG_ERR, "Failed to add inotify FD to epoll");
509 close(sctpParams.inotifyFD);
510 return -1;
511 }
ss412g1a79bdf2019-10-24 12:03:05 +0300512 return 0;
513}
514
515/**
516 *
517 * @param args
518 * @return
519 */
520void listener(sctp_params_t *params) {
ss412g1a79bdf2019-10-24 12:03:05 +0300521 int num_of_SCTP_messages = 0;
ss412g1a79bdf2019-10-24 12:03:05 +0300522 auto totalTime = 0.0;
ss412g1a79bdf2019-10-24 12:03:05 +0300523 std::thread::id this_id = std::this_thread::get_id();
524 //save cout
dhirajverma5f132832021-06-17 17:27:54 +0530525 auto pod_name = std::getenv("POD_NAME");
526 auto container_name = std::getenv("CONTAINER_NAME");
527 auto service_name = std::getenv("SERVICE_NAME");
528 auto host_name = std::getenv("HOST_NAME");
529 auto system_name = std::getenv("SYSTEM_NAME");
530 auto pid = std::to_string(getpid()).c_str();
ss412g1a79bdf2019-10-24 12:03:05 +0300531 streambuf *oldCout = cout.rdbuf();
532 ostringstream memCout;
533 // create new cout
534 cout.rdbuf(memCout.rdbuf());
535 cout << this_id;
536 //return to the normal cout
537 cout.rdbuf(oldCout);
538
539 char tid[32];
540 memcpy(tid, memCout.str().c_str(), memCout.str().length() < 32 ? memCout.str().length() : 31);
541 tid[memCout.str().length()] = 0;
dhirajverma5f132832021-06-17 17:27:54 +0530542 mdclog_mdc_add("SYSTEM_NAME", system_name);
543 mdclog_mdc_add("HOST_NAME", host_name);
544 mdclog_mdc_add("SERVICE_NAME", service_name);
545 mdclog_mdc_add("CONTAINER_NAME", container_name);
546 mdclog_mdc_add("POD_NAME", pod_name);
547 mdclog_mdc_add("PID", pid);
ss412g1a79bdf2019-10-24 12:03:05 +0300548
549 if (mdclog_level_get() >= MDCLOG_DEBUG) {
550 mdclog_write(MDCLOG_DEBUG, "started thread number %s", tid);
551 }
552
553 RmrMessagesBuffer_t rmrMessageBuffer{};
554 //create and init RMR
555 rmrMessageBuffer.rmrCtx = params->rmrCtx;
556
557 auto *events = (struct epoll_event *) calloc(MAXEVENTS, sizeof(struct epoll_event));
558 struct timespec end{0, 0};
559 struct timespec start{0, 0};
560
561 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
562 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
563
ss412g3bac2da2020-01-05 11:52:19 +0200564 memcpy(rmrMessageBuffer.ka_message, params->ka_message, params->ka_message_length);
565 rmrMessageBuffer.ka_message_len = params->ka_message_length;
566 rmrMessageBuffer.ka_message[rmrMessageBuffer.ka_message_len] = 0;
567
568 if (mdclog_level_get() >= MDCLOG_DEBUG) {
569 mdclog_write(MDCLOG_DEBUG, "keep alive message is : %s", rmrMessageBuffer.ka_message);
570 }
571
ss412g1a79bdf2019-10-24 12:03:05 +0300572 ReportingMessages_t message {};
573
aa7133@att.com409f2232020-10-29 13:06:45 +0300574// for (int i = 0; i < MAX_RMR_BUFF_ARRAY; i++) {
aa7133@att.com7c3e4c02020-03-25 16:37:19 +0200575// rmrMessageBuffer.rcvBufferedMessages[i] = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
576// rmrMessageBuffer.sendBufferedMessages[i] = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
577// }
ss412g1a79bdf2019-10-24 12:03:05 +0300578
579 while (true) {
580 if (mdclog_level_get() >= MDCLOG_DEBUG) {
ss412g4aeeda22020-06-16 14:12:41 +0300581 mdclog_write(MDCLOG_DEBUG, "Start EPOLL Wait. Timeout = %d", params->epollTimeOut);
ss412g1a79bdf2019-10-24 12:03:05 +0300582 }
dhirajverma6cf28612021-06-23 04:53:34 -0400583#ifndef UNIT_TEST
ss412g4aeeda22020-06-16 14:12:41 +0300584 auto numOfEvents = epoll_wait(params->epoll_fd, events, MAXEVENTS, params->epollTimeOut);
dhirajverma6cf28612021-06-23 04:53:34 -0400585#else
586 auto numOfEvents = 1;
587#endif
aa7133@att.comb92748d2020-06-29 11:52:30 +0300588 if (numOfEvents == 0) { // time out
589 if (mdclog_level_get() >= MDCLOG_DEBUG) {
590 mdclog_write(MDCLOG_DEBUG, "got epoll timeout");
ss412g1a79bdf2019-10-24 12:03:05 +0300591 }
592 continue;
ss412g4aeeda22020-06-16 14:12:41 +0300593 } else if (numOfEvents < 0) {
594 if (errno == EINTR) {
595 if (mdclog_level_get() >= MDCLOG_DEBUG) {
596 mdclog_write(MDCLOG_DEBUG, "got EINTR : %s", strerror(errno));
597 }
598 continue;
599 }
ss412g1a79bdf2019-10-24 12:03:05 +0300600 mdclog_write(MDCLOG_ERR, "Epoll wait failed, errno = %s", strerror(errno));
601 return;
602 }
603 for (auto i = 0; i < numOfEvents; i++) {
604 if (mdclog_level_get() >= MDCLOG_DEBUG) {
605 mdclog_write(MDCLOG_DEBUG, "handling epoll event %d out of %d", i + 1, numOfEvents);
606 }
607 clock_gettime(CLOCK_MONOTONIC, &message.message.time);
608 start.tv_sec = message.message.time.tv_sec;
609 start.tv_nsec = message.message.time.tv_nsec;
ss412g3bac2da2020-01-05 11:52:19 +0200610
611
ss412g1a79bdf2019-10-24 12:03:05 +0300612 if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP)) {
aa7133@att.combe92d712020-03-18 17:30:39 +0200613 handlepoll_error(events[i], message, rmrMessageBuffer, params);
ss412g1a79bdf2019-10-24 12:03:05 +0300614 } else if (events[i].events & EPOLLOUT) {
aa7133@att.combe92d712020-03-18 17:30:39 +0200615 handleEinprogressMessages(events[i], message, rmrMessageBuffer, params);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200616 } else if (params->listenFD == events[i].data.fd) {
aa7133@att.com6680c3b2020-03-22 12:06:58 +0200617 if (mdclog_level_get() >= MDCLOG_INFO) {
618 mdclog_write(MDCLOG_INFO, "New connection request from sctp network\n");
619 }
aa7133@att.com84bd3342020-03-16 18:04:57 +0200620 // new connection is requested from RAN start build connection
621 while (true) {
622 struct sockaddr in_addr {};
623 socklen_t in_len;
624 char hostBuff[NI_MAXHOST];
625 char portBuff[NI_MAXSERV];
626
627 in_len = sizeof(in_addr);
628 auto *peerInfo = (ConnectedCU_t *)calloc(1, sizeof(ConnectedCU_t));
aa7133@att.com409f2232020-10-29 13:06:45 +0300629 if(peerInfo == nullptr){
swc58d4f5d2020-10-20 15:08:38 +0900630 mdclog_write(MDCLOG_ERR, "calloc failed");
631 break;
632 }
aa7133@att.com84bd3342020-03-16 18:04:57 +0200633 peerInfo->sctpParams = params;
634 peerInfo->fileDescriptor = accept(params->listenFD, &in_addr, &in_len);
635 if (peerInfo->fileDescriptor == -1) {
636 if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
637 /* We have processed all incoming connections. */
638 break;
639 } else {
640 mdclog_write(MDCLOG_ERR, "Accept error, errno = %s", strerror(errno));
641 break;
642 }
643 }
644 if (setSocketNoBlocking(peerInfo->fileDescriptor) == -1) {
645 mdclog_write(MDCLOG_ERR, "setSocketNoBlocking failed to set new connection %s on port %s\n", hostBuff, portBuff);
646 close(peerInfo->fileDescriptor);
647 break;
648 }
649 auto ans = getnameinfo(&in_addr, in_len,
aa7133@att.com96c9b882020-03-25 14:50:40 +0200650 peerInfo->hostName, NI_MAXHOST,
651 peerInfo->portNumber, NI_MAXSERV, (unsigned )((unsigned int)NI_NUMERICHOST | (unsigned int)NI_NUMERICSERV));
aa7133@att.com84bd3342020-03-16 18:04:57 +0200652 if (ans < 0) {
653 mdclog_write(MDCLOG_ERR, "Failed to get info on connection request. %s\n", strerror(errno));
654 close(peerInfo->fileDescriptor);
655 break;
656 }
657 if (mdclog_level_get() >= MDCLOG_DEBUG) {
658 mdclog_write(MDCLOG_DEBUG, "Accepted connection on descriptor %d (host=%s, port=%s)\n", peerInfo->fileDescriptor, peerInfo->hostName, peerInfo->portNumber);
659 }
660 peerInfo->isConnected = false;
661 peerInfo->gotSetup = false;
662 if (addToEpoll(params->epoll_fd,
663 peerInfo,
664 (EPOLLIN | EPOLLET),
665 params->sctpMap, nullptr,
aa7133@att.combe92d712020-03-18 17:30:39 +0200666 0) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +0200667 break;
668 }
aa7133@att.com6680c3b2020-03-22 12:06:58 +0200669 break;
aa7133@att.com84bd3342020-03-16 18:04:57 +0200670 }
ss412g1a79bdf2019-10-24 12:03:05 +0300671 } else if (params->rmrListenFd == events[i].data.fd) {
672 // got message from XAPP
aa7133@att.comde40d1c2020-08-26 16:19:20 +0300673 //num_of_XAPP_messages.fetch_add(1, std::memory_order_release);
ss412g1a79bdf2019-10-24 12:03:05 +0300674 num_of_messages.fetch_add(1, std::memory_order_release);
675 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.comf13529a2020-08-27 11:21:16 +0300676 mdclog_write(MDCLOG_DEBUG, "new RMR message");
ss412g1a79bdf2019-10-24 12:03:05 +0300677 }
aa7133@att.com6680c3b2020-03-22 12:06:58 +0200678 if (receiveXappMessages(params->sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +0300679 rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200680 message.message.time) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +0300681 mdclog_write(MDCLOG_ERR, "Error handling Xapp message");
682 }
ss412g3bac2da2020-01-05 11:52:19 +0200683 } else if (params->inotifyFD == events[i].data.fd) {
684 mdclog_write(MDCLOG_INFO, "Got event from inotify (configuration update)");
685 handleConfigChange(params);
ss412g1a79bdf2019-10-24 12:03:05 +0300686 } else {
687 /* We RMR_ERR_RETRY have data on the fd waiting to be read. Read and display it.
688 * We must read whatever data is available completely, as we are running
689 * in edge-triggered mode and won't get a notification again for the same data. */
690 num_of_messages.fetch_add(1, std::memory_order_release);
691 if (mdclog_level_get() >= MDCLOG_DEBUG) {
692 mdclog_write(MDCLOG_DEBUG, "new message from SCTP, epoll flags are : %0x", events[i].events);
693 }
694 receiveDataFromSctp(&events[i],
695 params->sctpMap,
696 num_of_SCTP_messages,
697 rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200698 message.message.time);
ss412g1a79bdf2019-10-24 12:03:05 +0300699 }
700
701 clock_gettime(CLOCK_MONOTONIC, &end);
702 if (mdclog_level_get() >= MDCLOG_INFO) {
703 totalTime += ((end.tv_sec + 1.0e-9 * end.tv_nsec) -
704 ((double) start.tv_sec + 1.0e-9 * start.tv_nsec));
705 }
706 if (mdclog_level_get() >= MDCLOG_DEBUG) {
707 mdclog_write(MDCLOG_DEBUG, "message handling is %ld seconds %ld nanoseconds",
708 end.tv_sec - start.tv_sec,
709 end.tv_nsec - start.tv_nsec);
710 }
711 }
dhirajverma6cf28612021-06-23 04:53:34 -0400712#ifdef UNIT_TEST
713 break;
714#endif
ss412g1a79bdf2019-10-24 12:03:05 +0300715 }
ss412g1a79bdf2019-10-24 12:03:05 +0300716}
717
718/**
719 *
ss412g3bac2da2020-01-05 11:52:19 +0200720 * @param sctpParams
721 */
722void handleConfigChange(sctp_params_t *sctpParams) {
723 char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event))));
724 const struct inotify_event *event;
725 char *ptr;
726
727 path p = (sctpParams->configFilePath + "/" + sctpParams->configFileName).c_str();
728 auto endlessLoop = true;
729 while (endlessLoop) {
730 auto len = read(sctpParams->inotifyFD, buf, sizeof buf);
731 if (len == -1) {
732 if (errno != EAGAIN) {
733 mdclog_write(MDCLOG_ERR, "read %s ", strerror(errno));
734 endlessLoop = false;
735 continue;
736 }
737 else {
738 endlessLoop = false;
739 continue;
740 }
741 }
742
743 for (ptr = buf; ptr < buf + len; ptr += sizeof(struct inotify_event) + event->len) {
744 event = (const struct inotify_event *)ptr;
745 if (event->mask & (uint32_t)IN_ISDIR) {
746 continue;
747 }
748
749 // the directory name
750 if (sctpParams->inotifyWD == event->wd) {
751 // not the directory
752 }
753 if (event->len) {
aa7133@att.comdfc1db92020-03-31 17:32:04 +0300754 auto retVal = strcmp(sctpParams->configFileName.c_str(), event->name);
755 if (retVal != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200756 continue;
757 }
758 }
759 // only the file we want
760 if (event->mask & (uint32_t)IN_CLOSE_WRITE) {
aa7133@att.comdfc1db92020-03-31 17:32:04 +0300761 if (mdclog_level_get() >= MDCLOG_INFO) {
762 mdclog_write(MDCLOG_INFO, "Configuration file changed");
763 }
ss412g3bac2da2020-01-05 11:52:19 +0200764 if (exists(p)) {
765 const int size = 2048;
766 auto fileSize = file_size(p);
767 if (fileSize > size) {
768 mdclog_write(MDCLOG_ERR, "File %s larger than %d", p.string().c_str(), size);
769 return;
770 }
771 } else {
772 mdclog_write(MDCLOG_ERR, "Configuration File %s not exists", p.string().c_str());
773 return;
774 }
775
776 ReadConfigFile conf;
777 if (conf.openConfigFile(p.string()) == -1) {
778 mdclog_write(MDCLOG_ERR, "Filed to open config file %s, %s",
779 p.string().c_str(), strerror(errno));
780 return;
781 }
ss412g3bac2da2020-01-05 11:52:19 +0200782 auto tmpStr = conf.getStringValue("loglevel");
783 if (tmpStr.length() == 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300784 mdclog_write(MDCLOG_ERR, "illegal loglevel. Set loglevel to MDCLOG_INFO");
ss412g3bac2da2020-01-05 11:52:19 +0200785 tmpStr = "info";
786 }
787 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
788
789 if ((tmpStr.compare("debug")) == 0) {
790 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_DEBUG");
791 sctpParams->logLevel = MDCLOG_DEBUG;
792 } else if ((tmpStr.compare("info")) == 0) {
793 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_INFO");
794 sctpParams->logLevel = MDCLOG_INFO;
795 } else if ((tmpStr.compare("warning")) == 0) {
796 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_WARN");
797 sctpParams->logLevel = MDCLOG_WARN;
798 } else if ((tmpStr.compare("error")) == 0) {
799 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_ERR");
800 sctpParams->logLevel = MDCLOG_ERR;
801 } else {
aa7133@att.com409f2232020-10-29 13:06:45 +0300802 mdclog_write(MDCLOG_ERR, "illegal loglevel = %s. Set loglevel to MDCLOG_INFO", tmpStr.c_str());
ss412g3bac2da2020-01-05 11:52:19 +0200803 sctpParams->logLevel = MDCLOG_INFO;
804 }
805 mdclog_level_set(sctpParams->logLevel);
ss412g3bac2da2020-01-05 11:52:19 +0200806 tmpStr = conf.getStringValue("trace");
807 if (tmpStr.length() == 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300808 mdclog_write(MDCLOG_ERR, "illegal trace. Set trace to stop");
ss412g3bac2da2020-01-05 11:52:19 +0200809 tmpStr = "stop";
810 }
811
812 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
813 if ((tmpStr.compare("start")) == 0) {
814 mdclog_write(MDCLOG_INFO, "Trace set to: start");
815 sctpParams->trace = true;
816 } else if ((tmpStr.compare("stop")) == 0) {
817 mdclog_write(MDCLOG_INFO, "Trace set to: stop");
818 sctpParams->trace = false;
819 } else {
820 mdclog_write(MDCLOG_ERR, "Trace was set to wrong value %s, set to stop", tmpStr.c_str());
821 sctpParams->trace = false;
822 }
823 jsonTrace = sctpParams->trace;
ss412g4aeeda22020-06-16 14:12:41 +0300824
ss412g4aeeda22020-06-16 14:12:41 +0300825
ss412g3bac2da2020-01-05 11:52:19 +0200826 endlessLoop = false;
827 }
828 }
829 }
830}
831
832/**
833 *
834 * @param event
835 * @param message
836 * @param rmrMessageBuffer
837 * @param params
ss412g3bac2da2020-01-05 11:52:19 +0200838 */
839void handleEinprogressMessages(struct epoll_event &event,
840 ReportingMessages_t &message,
841 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200842 sctp_params_t *params) {
ss412g3bac2da2020-01-05 11:52:19 +0200843 auto *peerInfo = (ConnectedCU_t *)event.data.ptr;
844 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
845
846 mdclog_write(MDCLOG_INFO, "file descriptor %d got EPOLLOUT", peerInfo->fileDescriptor);
847 auto retVal = 0;
848 socklen_t retValLen = 0;
849 auto rc = getsockopt(peerInfo->fileDescriptor, SOL_SOCKET, SO_ERROR, &retVal, &retValLen);
850 if (rc != 0 || retVal != 0) {
dhirajverma31768a92021-05-06 12:38:49 +0530851#ifndef UNIT_TEST
ss412g3bac2da2020-01-05 11:52:19 +0200852 if (rc != 0) {
853 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
854 "%s|Failed SCTP Connection, after EINPROGRESS the getsockopt%s",
855 peerInfo->enodbName, strerror(errno));
856 } else if (retVal != 0) {
857 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
858 "%s|Failed SCTP Connection after EINPROGRESS, SO_ERROR",
859 peerInfo->enodbName);
860 }
861
862 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
863 message.message.asnLength = rmrMessageBuffer.sendMessage->len;
864 mdclog_write(MDCLOG_ERR, "%s", rmrMessageBuffer.sendMessage->payload);
865 message.message.direction = 'N';
aa7133@att.combe92d712020-03-18 17:30:39 +0200866 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200867 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
868 }
dhirajverma31768a92021-05-06 12:38:49 +0530869#endif
ss412g3bac2da2020-01-05 11:52:19 +0200870 memset(peerInfo->asnData, 0, peerInfo->asnLength);
871 peerInfo->asnLength = 0;
872 peerInfo->mtype = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200873 return;
874 }
875
876 peerInfo->isConnected = true;
877
878 if (modifyToEpoll(params->epoll_fd, peerInfo, (EPOLLIN | EPOLLET), params->sctpMap, peerInfo->enodbName,
aa7133@att.combe92d712020-03-18 17:30:39 +0200879 peerInfo->mtype) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200880 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_MOD");
ss412g3bac2da2020-01-05 11:52:19 +0200881 return;
882 }
883
884 message.message.asndata = (unsigned char *)peerInfo->asnData;
885 message.message.asnLength = peerInfo->asnLength;
886 message.message.messageType = peerInfo->mtype;
887 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
888 num_of_messages.fetch_add(1, std::memory_order_release);
889 if (mdclog_level_get() >= MDCLOG_DEBUG) {
890 mdclog_write(MDCLOG_DEBUG, "send the delayed SETUP/ENDC SETUP to sctp for %s",
891 message.message.enodbName);
892 }
aa7133@att.combe92d712020-03-18 17:30:39 +0200893 if (sendSctpMsg(peerInfo, message, params->sctpMap) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200894 if (mdclog_level_get() >= MDCLOG_DEBUG) {
895 mdclog_write(MDCLOG_DEBUG, "Error write to SCTP %s %d", __func__, __LINE__);
896 }
ss412g3bac2da2020-01-05 11:52:19 +0200897 return;
898 }
899
900 memset(peerInfo->asnData, 0, peerInfo->asnLength);
901 peerInfo->asnLength = 0;
902 peerInfo->mtype = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200903}
904
905
906void handlepoll_error(struct epoll_event &event,
907 ReportingMessages_t &message,
908 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200909 sctp_params_t *params) {
ss412g3bac2da2020-01-05 11:52:19 +0200910 if (event.data.fd != params->rmrListenFd) {
911 auto *peerInfo = (ConnectedCU_t *)event.data.ptr;
912 mdclog_write(MDCLOG_ERR, "epoll error, events %0x on fd %d, RAN NAME : %s",
913 event.events, peerInfo->fileDescriptor, peerInfo->enodbName);
dhirajverma31768a92021-05-06 12:38:49 +0530914#ifndef UNIT_TEST
ss412g3bac2da2020-01-05 11:52:19 +0200915
916 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
917 "%s|Failed SCTP Connection",
918 peerInfo->enodbName);
919 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
920 message.message.asnLength = rmrMessageBuffer.sendMessage->len;
921
922 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
923 message.message.direction = 'N';
aa7133@att.combe92d712020-03-18 17:30:39 +0200924 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200925 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
926 }
dhirajverma31768a92021-05-06 12:38:49 +0530927#endif
ss412g3bac2da2020-01-05 11:52:19 +0200928 close(peerInfo->fileDescriptor);
aa7133@att.com7b437f72020-03-30 13:25:54 +0300929 params->sctpMap->erase(peerInfo->enodbName);
aa7133@att.combe92d712020-03-18 17:30:39 +0200930 cleanHashEntry((ConnectedCU_t *) event.data.ptr, params->sctpMap);
ss412g3bac2da2020-01-05 11:52:19 +0200931 } else {
932 mdclog_write(MDCLOG_ERR, "epoll error, events %0x on RMR FD", event.events);
933 }
ss412g3bac2da2020-01-05 11:52:19 +0200934}
935/**
936 *
ss412g1a79bdf2019-10-24 12:03:05 +0300937 * @param socket
938 * @return
939 */
940int setSocketNoBlocking(int socket) {
941 auto flags = fcntl(socket, F_GETFL, 0);
942
943 if (flags == -1) {
ss412g1a79bdf2019-10-24 12:03:05 +0300944 mdclog_write(MDCLOG_ERR, "%s, %s", __FUNCTION__, strerror(errno));
ss412g1a79bdf2019-10-24 12:03:05 +0300945 return -1;
946 }
947
948 flags = (unsigned) flags | (unsigned) O_NONBLOCK;
949 if (fcntl(socket, F_SETFL, flags) == -1) {
ss412g1a79bdf2019-10-24 12:03:05 +0300950 mdclog_write(MDCLOG_ERR, "%s, %s", __FUNCTION__, strerror(errno));
ss412g1a79bdf2019-10-24 12:03:05 +0300951 return -1;
952 }
953
954 return 0;
955}
956
957/**
958 *
959 * @param val
960 * @param m
ss412g1a79bdf2019-10-24 12:03:05 +0300961 */
aa7133@att.combe92d712020-03-18 17:30:39 +0200962void cleanHashEntry(ConnectedCU_t *val, Sctp_Map_t *m) {
ss412g1a79bdf2019-10-24 12:03:05 +0300963 char *dummy;
964 auto port = (uint16_t) strtol(val->portNumber, &dummy, 10);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200965 char searchBuff[2048]{};
ss412g1a79bdf2019-10-24 12:03:05 +0300966
967 snprintf(searchBuff, sizeof searchBuff, "host:%s:%d", val->hostName, port);
968 m->erase(searchBuff);
969
970 m->erase(val->enodbName);
dhirajverma31768a92021-05-06 12:38:49 +0530971#ifndef UNIT_TEST
ss412g1a79bdf2019-10-24 12:03:05 +0300972 free(val);
dhirajverma31768a92021-05-06 12:38:49 +0530973#endif
ss412g1a79bdf2019-10-24 12:03:05 +0300974}
975
976/**
977 *
aa7133@att.com409f2232020-10-29 13:06:45 +0300978 * @param fd file descriptor
ss412g1a79bdf2019-10-24 12:03:05 +0300979 * @param data the asn data to send
980 * @param len length of the data
981 * @param enodbName the enodbName as in the map for printing purpose
982 * @param m map host information
983 * @param mtype message number
aa7133@att.com409f2232020-10-29 13:06:45 +0300984 * @return 0 success, a negative number on fail
ss412g1a79bdf2019-10-24 12:03:05 +0300985 */
aa7133@att.combe92d712020-03-18 17:30:39 +0200986int sendSctpMsg(ConnectedCU_t *peerInfo, ReportingMessages_t &message, Sctp_Map_t *m) {
ss412g1a79bdf2019-10-24 12:03:05 +0300987 auto loglevel = mdclog_level_get();
988 int fd = peerInfo->fileDescriptor;
989 if (loglevel >= MDCLOG_DEBUG) {
990 mdclog_write(MDCLOG_DEBUG, "Send SCTP message for CU %s, %s",
991 message.message.enodbName, __FUNCTION__);
992 }
993
994 while (true) {
ss412g1a79bdf2019-10-24 12:03:05 +0300995 if (send(fd,message.message.asndata, message.message.asnLength,MSG_NOSIGNAL) < 0) {
996 if (errno == EINTR) {
997 continue;
998 }
999 mdclog_write(MDCLOG_ERR, "error writing to CU a message, %s ", strerror(errno));
aa7133@att.com84bd3342020-03-16 18:04:57 +02001000 if (!peerInfo->isConnected) {
1001 mdclog_write(MDCLOG_ERR, "connection to CU %s is still in progress.", message.message.enodbName);
dhirajverma6cf28612021-06-23 04:53:34 -04001002#ifndef UNIT_TEST
aa7133@att.com84bd3342020-03-16 18:04:57 +02001003 return -1;
dhirajverma6cf28612021-06-23 04:53:34 -04001004#endif
aa7133@att.com84bd3342020-03-16 18:04:57 +02001005 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001006 cleanHashEntry(peerInfo, m);
ss412g1a79bdf2019-10-24 12:03:05 +03001007 close(fd);
1008 char key[MAX_ENODB_NAME_SIZE * 2];
1009 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", message.message.enodbName,
1010 message.message.messageType);
1011 if (loglevel >= MDCLOG_DEBUG) {
1012 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
1013 }
aa7133@att.com96c9b882020-03-25 14:50:40 +02001014 auto tmp = m->find(key);
ss412g1a79bdf2019-10-24 12:03:05 +03001015 if (tmp) {
1016 free(tmp);
1017 }
1018 m->erase(key);
dhirajverma6cf28612021-06-23 04:53:34 -04001019#ifndef UNIT_TEST
ss412g1a79bdf2019-10-24 12:03:05 +03001020 return -1;
dhirajverma6cf28612021-06-23 04:53:34 -04001021#endif
ss412g1a79bdf2019-10-24 12:03:05 +03001022 }
1023 message.message.direction = 'D';
1024 // send report.buffer of size
1025 buildJsonMessage(message);
1026
1027 if (loglevel >= MDCLOG_DEBUG) {
1028 mdclog_write(MDCLOG_DEBUG,
1029 "SCTP message for CU %s sent from %s",
1030 message.message.enodbName,
1031 __FUNCTION__);
1032 }
ss412g1a79bdf2019-10-24 12:03:05 +03001033 return 0;
1034 }
1035}
1036
1037/**
1038 *
1039 * @param message
1040 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001041 */
aa7133@att.combe92d712020-03-18 17:30:39 +02001042void getRequestMetaData(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03001043 message.message.asndata = rmrMessageBuffer.rcvMessage->payload;
1044 message.message.asnLength = rmrMessageBuffer.rcvMessage->len;
1045
1046 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1047 mdclog_write(MDCLOG_DEBUG, "Message from Xapp RAN name = %s message length = %ld",
1048 message.message.enodbName, (unsigned long) message.message.asnLength);
1049 }
ss412g1a79bdf2019-10-24 12:03:05 +03001050}
1051
1052
ss412g1a79bdf2019-10-24 12:03:05 +03001053
1054/**
1055 *
1056 * @param events
1057 * @param sctpMap
1058 * @param numOfMessages
1059 * @param rmrMessageBuffer
1060 * @param ts
ss412g1a79bdf2019-10-24 12:03:05 +03001061 * @return
1062 */
1063int receiveDataFromSctp(struct epoll_event *events,
1064 Sctp_Map_t *sctpMap,
1065 int &numOfMessages,
1066 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +02001067 struct timespec &ts) {
ss412g1a79bdf2019-10-24 12:03:05 +03001068 /* We have data on the fd waiting to be read. Read and display it.
1069 * We must read whatever data is available completely, as we are running
1070 * in edge-triggered mode and won't get a notification again for the same data. */
aa7133@att.com84bd3342020-03-16 18:04:57 +02001071 ReportingMessages_t message {};
1072 auto done = 0;
ss412g1a79bdf2019-10-24 12:03:05 +03001073 auto loglevel = mdclog_level_get();
aa7133@att.com84bd3342020-03-16 18:04:57 +02001074
ss412g1a79bdf2019-10-24 12:03:05 +03001075 // get the identity of the interface
aa7133@att.com84bd3342020-03-16 18:04:57 +02001076 message.peerInfo = (ConnectedCU_t *)events->data.ptr;
1077
ss412g1a79bdf2019-10-24 12:03:05 +03001078 struct timespec start{0, 0};
aa7133@att.com409f2232020-10-29 13:06:45 +03001079 struct timespec decodeStart{0, 0};
ss412g1a79bdf2019-10-24 12:03:05 +03001080 struct timespec end{0, 0};
1081
1082 E2AP_PDU_t *pdu = nullptr;
1083
ss412g1a79bdf2019-10-24 12:03:05 +03001084 while (true) {
1085 if (loglevel >= MDCLOG_DEBUG) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001086 mdclog_write(MDCLOG_DEBUG, "Start Read from SCTP %d fd", message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001087 clock_gettime(CLOCK_MONOTONIC, &start);
1088 }
1089 // read the buffer directly to rmr payload
1090 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1091 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
aa7133@att.com84bd3342020-03-16 18:04:57 +02001092 read(message.peerInfo->fileDescriptor, rmrMessageBuffer.sendMessage->payload, RECEIVE_SCTP_BUFFER_SIZE);
1093
ss412g1a79bdf2019-10-24 12:03:05 +03001094 if (loglevel >= MDCLOG_DEBUG) {
1095 mdclog_write(MDCLOG_DEBUG, "Finish Read from SCTP %d fd message length = %ld",
aa7133@att.com96c9b882020-03-25 14:50:40 +02001096 message.peerInfo->fileDescriptor, message.message.asnLength);
ss412g1a79bdf2019-10-24 12:03:05 +03001097 }
aa7133@att.com16c799b2020-03-24 18:01:51 +02001098
aa7133@att.com84bd3342020-03-16 18:04:57 +02001099 memcpy(message.message.enodbName, message.peerInfo->enodbName, sizeof(message.peerInfo->enodbName));
ss412g1a79bdf2019-10-24 12:03:05 +03001100 message.message.direction = 'U';
1101 message.message.time.tv_nsec = ts.tv_nsec;
1102 message.message.time.tv_sec = ts.tv_sec;
1103
1104 if (message.message.asnLength < 0) {
1105 if (errno == EINTR) {
1106 continue;
1107 }
1108 /* If errno == EAGAIN, that means we have read all
aa7133@att.com84bd3342020-03-16 18:04:57 +02001109 data. So goReportingMessages_t back to the main loop. */
ss412g1a79bdf2019-10-24 12:03:05 +03001110 if (errno != EAGAIN) {
1111 mdclog_write(MDCLOG_ERR, "Read error, %s ", strerror(errno));
1112 done = 1;
1113 } else if (loglevel >= MDCLOG_DEBUG) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001114 mdclog_write(MDCLOG_DEBUG, "EAGAIN - descriptor = %d", message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001115 }
1116 break;
1117 } else if (message.message.asnLength == 0) {
1118 /* End of file. The remote has closed the connection. */
1119 if (loglevel >= MDCLOG_INFO) {
1120 mdclog_write(MDCLOG_INFO, "END of File Closed connection - descriptor = %d",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001121 message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001122 }
1123 done = 1;
1124 break;
1125 }
1126
ss412g1a79bdf2019-10-24 12:03:05 +03001127 if (loglevel >= MDCLOG_DEBUG) {
swc58d4f5d2020-10-20 15:08:38 +09001128 char printBuffer[RECEIVE_SCTP_BUFFER_SIZE]{};
ss412g1a79bdf2019-10-24 12:03:05 +03001129 char *tmp = printBuffer;
1130 for (size_t i = 0; i < (size_t)message.message.asnLength; ++i) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001131 snprintf(tmp, 3, "%02x", message.message.asndata[i]);
ss412g1a79bdf2019-10-24 12:03:05 +03001132 tmp += 2;
1133 }
1134 printBuffer[message.message.asnLength] = 0;
1135 clock_gettime(CLOCK_MONOTONIC, &end);
1136 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 +02001137 message.peerInfo->enodbName, end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec);
ss412g1a79bdf2019-10-24 12:03:05 +03001138 mdclog_write(MDCLOG_DEBUG, "PDU buffer length = %ld, data = : %s", message.message.asnLength,
1139 printBuffer);
aa7133@att.com409f2232020-10-29 13:06:45 +03001140 clock_gettime(CLOCK_MONOTONIC, &decodeStart);
ss412g1a79bdf2019-10-24 12:03:05 +03001141 }
1142
aa7133@att.com7b437f72020-03-30 13:25:54 +03001143 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, (void **) &pdu,
ss412g1a79bdf2019-10-24 12:03:05 +03001144 message.message.asndata, message.message.asnLength);
1145 if (rval.code != RC_OK) {
1146 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2AP PDU from RAN : %s", rval.code,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001147 message.peerInfo->enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001148 break;
1149 }
1150
1151 if (loglevel >= MDCLOG_DEBUG) {
1152 clock_gettime(CLOCK_MONOTONIC, &end);
1153 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 +03001154 message.peerInfo->enodbName, end.tv_sec - decodeStart.tv_sec, end.tv_nsec - decodeStart.tv_nsec);
ss412g1a79bdf2019-10-24 12:03:05 +03001155 char *printBuffer;
1156 size_t size;
1157 FILE *stream = open_memstream(&printBuffer, &size);
1158 asn_fprint(stream, &asn_DEF_E2AP_PDU, pdu);
1159 mdclog_write(MDCLOG_DEBUG, "Encoding E2AP PDU past : %s", printBuffer);
aa7133@att.com409f2232020-10-29 13:06:45 +03001160 clock_gettime(CLOCK_MONOTONIC, &decodeStart);
Byonggon Chuncae9bd52020-11-18 13:37:29 +09001161
1162 fclose(stream);
1163 free(printBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001164 }
1165
1166 switch (pdu->present) {
1167 case E2AP_PDU_PR_initiatingMessage: {//initiating message
aa7133@att.com7b437f72020-03-30 13:25:54 +03001168 asnInitiatingRequest(pdu, sctpMap,message, rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001169 break;
1170 }
1171 case E2AP_PDU_PR_successfulOutcome: { //successful outcome
aa7133@att.com409f2232020-10-29 13:06:45 +03001172 asnSuccessfulMsg(pdu, sctpMap, message, rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001173 break;
1174 }
1175 case E2AP_PDU_PR_unsuccessfulOutcome: { //Unsuccessful Outcome
aa7133@att.com7b437f72020-03-30 13:25:54 +03001176 asnUnSuccsesfulMsg(pdu, sctpMap, message, rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001177 break;
1178 }
1179 default:
1180 mdclog_write(MDCLOG_ERR, "Unknown index %d in E2AP PDU", pdu->present);
1181 break;
1182 }
1183 if (loglevel >= MDCLOG_DEBUG) {
1184 clock_gettime(CLOCK_MONOTONIC, &end);
1185 mdclog_write(MDCLOG_DEBUG,
1186 "After processing message and sent to rmr for : %s, Read time is : %ld seconds, %ld nanoseconds",
aa7133@att.com409f2232020-10-29 13:06:45 +03001187 message.peerInfo->enodbName, end.tv_sec - decodeStart.tv_sec, end.tv_nsec - decodeStart.tv_nsec);
ss412g1a79bdf2019-10-24 12:03:05 +03001188 }
1189 numOfMessages++;
ss412g1a79bdf2019-10-24 12:03:05 +03001190 if (pdu != nullptr) {
aa7133@att.com1fecb052020-03-29 09:44:09 +03001191 ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, pdu);
1192 //ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
aa7133@att.com9dff4f02020-03-29 09:55:22 +03001193 //pdu = nullptr;
ss412g1a79bdf2019-10-24 12:03:05 +03001194 }
ss412g1a79bdf2019-10-24 12:03:05 +03001195 }
ss412g1a79bdf2019-10-24 12:03:05 +03001196
1197 if (done) {
1198 if (loglevel >= MDCLOG_INFO) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001199 mdclog_write(MDCLOG_INFO, "Closed connection - descriptor = %d", message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001200 }
1201 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
1202 snprintf((char *)rmrMessageBuffer.sendMessage->payload,
aa7133@att.com96c9b882020-03-25 14:50:40 +02001203 256,
1204 "%s|CU disconnected unexpectedly",
1205 message.peerInfo->enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001206 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1207
1208 if (sendRequestToXapp(message,
1209 RIC_SCTP_CONNECTION_FAILURE,
aa7133@att.combe92d712020-03-18 17:30:39 +02001210 rmrMessageBuffer) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03001211 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
1212 }
1213
1214 /* Closing descriptor make epoll remove it from the set of descriptors which are monitored. */
aa7133@att.com84bd3342020-03-16 18:04:57 +02001215 close(message.peerInfo->fileDescriptor);
aa7133@att.combe92d712020-03-18 17:30:39 +02001216 cleanHashEntry((ConnectedCU_t *) events->data.ptr, sctpMap);
ss412g1a79bdf2019-10-24 12:03:05 +03001217 }
1218 if (loglevel >= MDCLOG_DEBUG) {
1219 clock_gettime(CLOCK_MONOTONIC, &end);
1220 mdclog_write(MDCLOG_DEBUG, "from receive SCTP to send RMR time is %ld seconds and %ld nanoseconds",
1221 end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec);
1222
1223 }
ss412g1a79bdf2019-10-24 12:03:05 +03001224 return 0;
1225}
1226
aa7133@att.com409f2232020-10-29 13:06:45 +03001227static void buildAndSendSetupRequest(ReportingMessages_t &message,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001228 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001229 E2AP_PDU_t *pdu/*,
ss412g4aeeda22020-06-16 14:12:41 +03001230 string const &messageName,
1231 string const &ieName,
1232 vector<string> &functionsToAdd_v,
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001233 vector<string> &functionsToModified_v*/) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001234 auto logLevel = mdclog_level_get();
aa7133@att.com84bd3342020-03-16 18:04:57 +02001235 // now we can send the data to e2Mgr
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001236
ss412g4aeeda22020-06-16 14:12:41 +03001237 asn_enc_rval_t er;
1238 auto buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
Byonggon Chun51885f32020-11-18 15:31:04 +09001239 unsigned char *buffer = nullptr;
1240 buffer = (unsigned char *) calloc(buffer_size, sizeof(unsigned char));
1241 if(!buffer)
1242 {
1243 mdclog_write(MDCLOG_ERR, "Allocating buffer for %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1244 return;
1245 }
ss412g4aeeda22020-06-16 14:12:41 +03001246 while (true) {
ss412g4aeeda22020-06-16 14:12:41 +03001247 er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu, buffer, buffer_size);
1248 if (er.encoded == -1) {
1249 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1250 return;
1251 } else if (er.encoded > (ssize_t) buffer_size) {
1252 buffer_size = er.encoded + 128;
1253 mdclog_write(MDCLOG_WARN, "Buffer of size %d is to small for %s. Reallocate buffer of size %d",
1254 (int) buffer_size,
1255 asn_DEF_E2AP_PDU.name, buffer_size);
1256 buffer_size = er.encoded + 128;
Byonggon Chun51885f32020-11-18 15:31:04 +09001257
1258 unsigned char *newBuffer = nullptr;
1259 newBuffer = (unsigned char *) realloc(buffer, buffer_size);
1260 if (!newBuffer)
1261 {
1262 // out of memory
1263 mdclog_write(MDCLOG_ERR, "Reallocating buffer for %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1264 free(buffer);
1265 return;
1266 }
1267 buffer = newBuffer;
ss412g4aeeda22020-06-16 14:12:41 +03001268 continue;
1269 }
1270 buffer[er.encoded] = '\0';
1271 break;
1272 }
1273 // encode to xml
1274
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001275 string res((char *)buffer);
1276 res.erase(std::remove(res.begin(), res.end(), '\n'), res.end());
1277 res.erase(std::remove(res.begin(), res.end(), '\t'), res.end());
1278 res.erase(std::remove(res.begin(), res.end(), ' '), res.end());
1279
1280// string res {};
1281// if (!functionsToAdd_v.empty() || !functionsToModified_v.empty()) {
1282// res = buildXmlData(messageName, ieName, functionsToAdd_v, functionsToModified_v, buffer, (size_t) er.encoded);
1283// }
ss412g4aeeda22020-06-16 14:12:41 +03001284 rmr_mbuf_t *rmrMsg;
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001285// if (res.length() == 0) {
1286// rmrMsg = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, buffer_size + 256);
1287// rmrMsg->len = snprintf((char *) rmrMsg->payload, RECEIVE_SCTP_BUFFER_SIZE * 2, "%s:%d|%s",
1288// message.peerInfo->sctpParams->myIP.c_str(),
1289// message.peerInfo->sctpParams->rmrPort,
1290// buffer);
1291// } else {
ss412g4aeeda22020-06-16 14:12:41 +03001292 rmrMsg = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, (int)res.length() + 256);
1293 rmrMsg->len = snprintf((char *) rmrMsg->payload, res.length() + 256, "%s:%d|%s",
1294 message.peerInfo->sctpParams->myIP.c_str(),
1295 message.peerInfo->sctpParams->rmrPort,
1296 res.c_str());
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001297// }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001298
ss412g4aeeda22020-06-16 14:12:41 +03001299 if (logLevel >= MDCLOG_DEBUG) {
1300 mdclog_write(MDCLOG_DEBUG, "Setup request of size %d :\n %s\n", rmrMsg->len, rmrMsg->payload);
1301 }
1302 // send to RMR
1303 rmrMsg->mtype = message.message.messageType;
1304 rmrMsg->state = 0;
1305 rmr_bytes2meid(rmrMsg, (unsigned char *) message.message.enodbName, strlen(message.message.enodbName));
aa7133@att.com84bd3342020-03-16 18:04:57 +02001306
ss412g4aeeda22020-06-16 14:12:41 +03001307 static unsigned char tx[32];
1308 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1309 rmr_bytes2xact(rmrMsg, tx, strlen((const char *) tx));
1310
1311 rmrMsg = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMsg);
1312 if (rmrMsg == nullptr) {
1313 mdclog_write(MDCLOG_ERR, "RMR failed to send returned nullptr");
1314 } else if (rmrMsg->state != 0) {
1315 char meid[RMR_MAX_MEID]{};
1316 if (rmrMsg->state == RMR_ERR_RETRY) {
1317 usleep(5);
1318 rmrMsg->state = 0;
1319 mdclog_write(MDCLOG_INFO, "RETRY sending Message %d to Xapp from %s",
1320 rmrMsg->mtype, rmr_get_meid(rmrMsg, (unsigned char *) meid));
1321 rmrMsg = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMsg);
1322 if (rmrMsg == nullptr) {
1323 mdclog_write(MDCLOG_ERR, "RMR failed send returned nullptr");
1324 } else if (rmrMsg->state != 0) {
1325 mdclog_write(MDCLOG_ERR,
1326 "RMR Retry failed %s sending request %d to Xapp from %s",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001327 translateRmrErrorMessages(rmrMsg->state).c_str(),
1328 rmrMsg->mtype,
1329 rmr_get_meid(rmrMsg, (unsigned char *) meid));
1330 }
ss412g4aeeda22020-06-16 14:12:41 +03001331 } else {
1332 mdclog_write(MDCLOG_ERR, "RMR failed: %s. sending request %d to Xapp from %s",
1333 translateRmrErrorMessages(rmrMsg->state).c_str(),
1334 rmrMsg->mtype,
1335 rmr_get_meid(rmrMsg, (unsigned char *) meid));
aa7133@att.combc110772020-03-25 09:45:14 +02001336 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001337 }
ss412g4aeeda22020-06-16 14:12:41 +03001338 message.peerInfo->gotSetup = true;
1339 buildJsonMessage(message);
Byonggon Chun51885f32020-11-18 15:31:04 +09001340
ss412g4aeeda22020-06-16 14:12:41 +03001341 if (rmrMsg != nullptr) {
1342 rmr_free_msg(rmrMsg);
1343 }
Byonggon Chun51885f32020-11-18 15:31:04 +09001344 free(buffer);
1345
1346 return;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001347}
aa7133@att.comc5a15202020-04-05 19:57:01 +03001348
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001349#if 0
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001350int RAN_Function_list_To_Vector(RANfunctions_List_t& list, vector <string> &runFunXML_v) {
1351 auto index = 0;
1352 runFunXML_v.clear();
1353 for (auto j = 0; j < list.list.count; j++) {
1354 auto *raNfunctionItemIEs = (RANfunction_ItemIEs_t *)list.list.array[j];
1355 if (raNfunctionItemIEs->id == ProtocolIE_ID_id_RANfunction_Item &&
1356 (raNfunctionItemIEs->value.present == RANfunction_ItemIEs__value_PR_RANfunction_Item)) {
1357 // encode to xml
1358 E2SM_gNB_NRT_RANfunction_Definition_t *ranFunDef = nullptr;
1359 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER,
1360 &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition,
1361 (void **)&ranFunDef,
1362 raNfunctionItemIEs->value.choice.RANfunction_Item.ranFunctionDefinition.buf,
1363 raNfunctionItemIEs->value.choice.RANfunction_Item.ranFunctionDefinition.size);
1364 if (rval.code != RC_OK) {
1365 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2SM message from : %s",
1366 rval.code,
1367 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name);
1368 return -1;
1369 }
1370
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001371 auto xml_buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
1372 unsigned char xml_buffer[RECEIVE_SCTP_BUFFER_SIZE * 2];
ss412g4aeeda22020-06-16 14:12:41 +03001373 memset(xml_buffer, 0, RECEIVE_SCTP_BUFFER_SIZE * 2);
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001374 // encode to xml
1375 auto er = asn_encode_to_buffer(nullptr,
1376 ATS_BASIC_XER,
1377 &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition,
1378 ranFunDef,
1379 xml_buffer,
1380 xml_buffer_size);
1381 if (er.encoded == -1) {
1382 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s",
1383 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name,
1384 strerror(errno));
1385 } else if (er.encoded > (ssize_t)xml_buffer_size) {
1386 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
1387 (int) xml_buffer_size,
1388 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name, __func__, __LINE__);
1389 } else {
1390 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1391 mdclog_write(MDCLOG_DEBUG, "Encoding E2SM %s PDU number %d : %s",
1392 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name,
1393 index++,
1394 xml_buffer);
1395 }
ss412g4aeeda22020-06-16 14:12:41 +03001396
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001397 string runFuncs = (char *)(xml_buffer);
1398 runFunXML_v.emplace_back(runFuncs);
1399 }
1400 }
1401 }
1402 return 0;
1403}
aa7133@att.comc5a15202020-04-05 19:57:01 +03001404
ss412g4aeeda22020-06-16 14:12:41 +03001405int collectServiceUpdate_RequestData(E2AP_PDU_t *pdu,
1406 Sctp_Map_t *sctpMap,
1407 ReportingMessages_t &message,
1408 vector <string> &RANfunctionsAdded_v,
1409 vector <string> &RANfunctionsModified_v) {
aa7133@att.comc5a15202020-04-05 19:57:01 +03001410 memset(message.peerInfo->enodbName, 0 , MAX_ENODB_NAME_SIZE);
ss412g4aeeda22020-06-16 14:12:41 +03001411 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICserviceUpdate.protocolIEs.list.count; i++) {
1412 auto *ie = pdu->choice.initiatingMessage->value.choice.RICserviceUpdate.protocolIEs.list.array[i];
1413 if (ie->id == ProtocolIE_ID_id_RANfunctionsAdded) {
1414 if (ie->value.present == RICserviceUpdate_IEs__value_PR_RANfunctionsID_List) {
aa7133@att.comc5a15202020-04-05 19:57:01 +03001415 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001416 mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1417 ie->value.choice.RANfunctions_List.list.count);
aa7133@att.comc5a15202020-04-05 19:57:01 +03001418 }
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001419 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsAdded_v) != 0 ) {
1420 return -1;
1421 }
1422 }
1423 } else if (ie->id == ProtocolIE_ID_id_RANfunctionsModified) {
ss412g4aeeda22020-06-16 14:12:41 +03001424 if (ie->value.present == RICserviceUpdate_IEs__value_PR_RANfunctions_List) {
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001425 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1426 mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1427 ie->value.choice.RANfunctions_List.list.count);
1428 }
1429 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsModified_v) != 0 ) {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001430 return -1;
aa7133@att.comc5a15202020-04-05 19:57:01 +03001431 }
1432 }
1433 }
1434 }
1435 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1436 mdclog_write(MDCLOG_DEBUG, "Run function vector have %ld entries",
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001437 RANfunctionsAdded_v.size());
aa7133@att.comc5a15202020-04-05 19:57:01 +03001438 }
1439 return 0;
1440}
ss412g4aeeda22020-06-16 14:12:41 +03001441
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001442#endif
ss412g4aeeda22020-06-16 14:12:41 +03001443
1444
aa7133@att.com409f2232020-10-29 13:06:45 +03001445void buildPrometheusList(ConnectedCU_t *peerInfo, Family<Counter> *prometheusFamily) {
aa7133@att.coma4c80012020-06-24 16:36:47 +03001446 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"SetupRequest", "Messages"}});
1447 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"SetupRequest", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001448
aa7133@att.coma4c80012020-06-24 16:36:47 +03001449 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ErrorIndication", "Messages"}});
1450 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ErrorIndication", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001451
aa7133@att.coma4c80012020-06-24 16:36:47 +03001452 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_RICindication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICindication", "Messages"}});
1453 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_RICindication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICindication", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001454
aa7133@att.coma4c80012020-06-24 16:36:47 +03001455 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetRequest", "Messages"}});
1456 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetRequest", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001457
aa7133@att.coma4c80012020-06-24 16:36:47 +03001458 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICserviceUpdate", "Messages"}});
1459 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICserviceUpdate", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001460 // ---------------------------------------------
aa7133@att.coma4c80012020-06-24 16:36:47 +03001461 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetACK", "Messages"}});
1462 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001463
aa7133@att.coma4c80012020-06-24 16:36:47 +03001464 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolACK", "Messages"}});
1465 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001466
aa7133@att.coma4c80012020-06-24 16:36:47 +03001467 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionACK", "Messages"}});
1468 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001469
aa7133@att.coma4c80012020-06-24 16:36:47 +03001470 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteACK", "Messages"}});
1471 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001472 //-------------------------------------------------------------
1473
aa7133@att.coma4c80012020-06-24 16:36:47 +03001474 peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolFailure", "Messages"}});
1475 peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001476
aa7133@att.coma4c80012020-06-24 16:36:47 +03001477 peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionFailure", "Messages"}});
1478 peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001479
aa7133@att.coma4c80012020-06-24 16:36:47 +03001480 peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteFailure", "Messages"}});
1481 peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001482
1483 //====================================================================================
aa7133@att.coma4c80012020-06-24 16:36:47 +03001484 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ErrorIndication", "Messages"}});
1485 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ErrorIndication", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001486
aa7133@att.coma4c80012020-06-24 16:36:47 +03001487 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetRequest", "Messages"}});
1488 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetRequest", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001489
aa7133@att.coma4c80012020-06-24 16:36:47 +03001490 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICcontrol", "Messages"}});
1491 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICcontrol", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001492
aa7133@att.coma4c80012020-06-24 16:36:47 +03001493 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICserviceQuery)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceQuery", "Messages"}});
1494 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICserviceQuery)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceQuery", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001495
aa7133@att.coma4c80012020-06-24 16:36:47 +03001496 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscription", "Messages"}});
1497 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscription", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001498
aa7133@att.coma4c80012020-06-24 16:36:47 +03001499 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscriptionDelete", "Messages"}});
1500 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscriptionDelete", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001501 //---------------------------------------------------------------------------------------------------------
aa7133@att.coma4c80012020-06-24 16:36:47 +03001502 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupResponse", "Messages"}});
1503 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupResponse", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001504
aa7133@att.coma4c80012020-06-24 16:36:47 +03001505 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetACK", "Messages"}});
1506 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001507
aa7133@att.coma4c80012020-06-24 16:36:47 +03001508 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateResponse", "Messages"}});
1509 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateResponse", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001510 //----------------------------------------------------------------------------------------------------------------
aa7133@att.coma4c80012020-06-24 16:36:47 +03001511 peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupRequestFailure", "Messages"}});
1512 peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupRequestFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001513
aa7133@att.coma4c80012020-06-24 16:36:47 +03001514 peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateFailure", "Messages"}});
1515 peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001516}
1517/**
1518 *
1519 * @param pdu
1520 * @param sctpMap
1521 * @param message
1522 * @param RANfunctionsAdded_v
1523 * @return
1524 */
1525int collectSetupRequestData(E2AP_PDU_t *pdu,
1526 Sctp_Map_t *sctpMap,
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001527 ReportingMessages_t &message /*, vector <string> &RANfunctionsAdded_v*/) {
ss412g4aeeda22020-06-16 14:12:41 +03001528 memset(message.peerInfo->enodbName, 0 , MAX_ENODB_NAME_SIZE);
1529 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.count; i++) {
1530 auto *ie = pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.array[i];
1531 if (ie->id == ProtocolIE_ID_id_GlobalE2node_ID) {
1532 // get the ran name for meid
1533 if (ie->value.present == E2setupRequestIEs__value_PR_GlobalE2node_ID) {
1534 if (buildRanName(message.peerInfo->enodbName, ie) < 0) {
1535 mdclog_write(MDCLOG_ERR, "Bad param in E2setupRequestIEs GlobalE2node_ID.\n");
aa7133@att.com409f2232020-10-29 13:06:45 +03001536 // no message will be sent
ss412g4aeeda22020-06-16 14:12:41 +03001537 return -1;
1538 }
1539
1540 memcpy(message.message.enodbName, message.peerInfo->enodbName, strlen(message.peerInfo->enodbName));
1541 sctpMap->setkey(message.message.enodbName, message.peerInfo);
1542 }
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001543 } /*else if (ie->id == ProtocolIE_ID_id_RANfunctionsAdded) {
ss412g4aeeda22020-06-16 14:12:41 +03001544 if (ie->value.present == E2setupRequestIEs__value_PR_RANfunctions_List) {
1545 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1546 mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1547 ie->value.choice.RANfunctions_List.list.count);
1548 }
1549 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsAdded_v) != 0 ) {
1550 return -1;
1551 }
1552 }
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001553 } */
ss412g4aeeda22020-06-16 14:12:41 +03001554 }
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001555// if (mdclog_level_get() >= MDCLOG_DEBUG) {
1556// mdclog_write(MDCLOG_DEBUG, "Run function vector have %ld entries",
1557// RANfunctionsAdded_v.size());
1558// }
ss412g4aeeda22020-06-16 14:12:41 +03001559 return 0;
1560}
1561
1562int XML_From_PER(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
1563 E2AP_PDU_t *pdu = nullptr;
1564
1565 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1566 mdclog_write(MDCLOG_DEBUG, "got PER message of size %d is:%s",
1567 rmrMessageBuffer.sendMessage->len, rmrMessageBuffer.sendMessage->payload);
1568 }
1569 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, (void **) &pdu,
1570 rmrMessageBuffer.sendMessage->payload, rmrMessageBuffer.sendMessage->len);
1571 if (rval.code != RC_OK) {
1572 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) setup response from E2MGR : %s",
1573 rval.code,
1574 message.message.enodbName);
1575 return -1;
1576 }
1577
1578 int buff_size = RECEIVE_XAPP_BUFFER_SIZE;
1579 auto er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu,
1580 rmrMessageBuffer.sendMessage->payload, buff_size);
1581 if (er.encoded == -1) {
1582 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1583 return -1;
1584 } else if (er.encoded > (ssize_t)buff_size) {
1585 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
1586 (int)rmrMessageBuffer.sendMessage->len,
1587 asn_DEF_E2AP_PDU.name,
1588 __func__,
1589 __LINE__);
1590 return -1;
1591 }
1592 rmrMessageBuffer.sendMessage->len = er.encoded;
1593 return 0;
1594
1595}
1596
ss412g1a79bdf2019-10-24 12:03:05 +03001597/**
1598 *
1599 * @param pdu
1600 * @param message
1601 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001602 */
1603void asnInitiatingRequest(E2AP_PDU_t *pdu,
aa7133@att.com7b437f72020-03-30 13:25:54 +03001604 Sctp_Map_t *sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +03001605 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +02001606 RmrMessagesBuffer_t &rmrMessageBuffer) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001607 auto logLevel = mdclog_level_get();
ss412g1a79bdf2019-10-24 12:03:05 +03001608 auto procedureCode = ((InitiatingMessage_t *) pdu->choice.initiatingMessage)->procedureCode;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001609 if (logLevel >= MDCLOG_DEBUG) {
1610 mdclog_write(MDCLOG_DEBUG, "Initiating message %ld\n", procedureCode);
ss412g1a79bdf2019-10-24 12:03:05 +03001611 }
1612 switch (procedureCode) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001613 case ProcedureCode_id_E2setup: {
1614 if (logLevel >= MDCLOG_DEBUG) {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001615 mdclog_write(MDCLOG_DEBUG, "Got E2setup");
aa7133@att.com84bd3342020-03-16 18:04:57 +02001616 }
1617
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001618// vector <string> RANfunctionsAdded_v;
1619// vector <string> RANfunctionsModified_v;
1620// RANfunctionsAdded_v.clear();
1621// RANfunctionsModified_v.clear();
1622 if (collectSetupRequestData(pdu, sctpMap, message) != 0) {
aa7133@att.com7b437f72020-03-30 13:25:54 +03001623 break;
1624 }
1625
aa7133@att.com409f2232020-10-29 13:06:45 +03001626 buildPrometheusList(message.peerInfo, message.peerInfo->sctpParams->prometheusFamily);
ss412g4aeeda22020-06-16 14:12:41 +03001627
1628 string messageName("E2setupRequest");
1629 string ieName("E2setupRequestIEs");
1630 message.message.messageType = RIC_E2_SETUP_REQ;
aa7133@att.coma4c80012020-06-24 16:36:47 +03001631 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
1632 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment((double)message.message.asnLength);
aa7133@att.com409f2232020-10-29 13:06:45 +03001633 buildAndSendSetupRequest(message, rmrMessageBuffer, pdu);
ss412g1a79bdf2019-10-24 12:03:05 +03001634 break;
1635 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001636 case ProcedureCode_id_RICserviceUpdate: {
1637 if (logLevel >= MDCLOG_DEBUG) {
1638 mdclog_write(MDCLOG_DEBUG, "Got RICserviceUpdate %s", message.message.enodbName);
1639 }
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001640// vector <string> RANfunctionsAdded_v;
1641// vector <string> RANfunctionsModified_v;
1642// RANfunctionsAdded_v.clear();
1643// RANfunctionsModified_v.clear();
1644// if (collectServiceUpdate_RequestData(pdu, sctpMap, message,
1645// RANfunctionsAdded_v, RANfunctionsModified_v) != 0) {
1646// break;
1647// }
ss412g4aeeda22020-06-16 14:12:41 +03001648
1649 string messageName("RICserviceUpdate");
1650 string ieName("RICserviceUpdateIEs");
1651 message.message.messageType = RIC_SERVICE_UPDATE;
dhirajverma6cf28612021-06-23 04:53:34 -04001652#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03001653 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
1654 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04001655#endif
aa7133@att.com409f2232020-10-29 13:06:45 +03001656 buildAndSendSetupRequest(message, rmrMessageBuffer, pdu);
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001657 break;
1658 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001659 case ProcedureCode_id_ErrorIndication: {
1660 if (logLevel >= MDCLOG_DEBUG) {
1661 mdclog_write(MDCLOG_DEBUG, "Got ErrorIndication %s", message.message.enodbName);
1662 }
dhirajverma6cf28612021-06-23 04:53:34 -04001663#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03001664 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_ErrorIndication]->Increment();
1665 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_ErrorIndication]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04001666#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02001667 if (sendRequestToXapp(message, RIC_ERROR_INDICATION, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001668 mdclog_write(MDCLOG_ERR, "RIC_ERROR_INDICATION failed to send to xAPP");
ss412g1a79bdf2019-10-24 12:03:05 +03001669 }
1670 break;
1671 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001672 case ProcedureCode_id_Reset: {
1673 if (logLevel >= MDCLOG_DEBUG) {
1674 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001675 }
dhirajverma6cf28612021-06-23 04:53:34 -04001676#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03001677 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
1678 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04001679#endif
ss412g4aeeda22020-06-16 14:12:41 +03001680 if (XML_From_PER(message, rmrMessageBuffer) < 0) {
1681 break;
ss412g1a79bdf2019-10-24 12:03:05 +03001682 }
ss412g4aeeda22020-06-16 14:12:41 +03001683
1684 if (sendRequestToXapp(message, RIC_E2_RESET_REQ, rmrMessageBuffer) != 0) {
1685 mdclog_write(MDCLOG_ERR, "RIC_E2_RESET_REQ message failed to send to xAPP");
aa7133@att.com84bd3342020-03-16 18:04:57 +02001686 }
1687 break;
1688 }
1689 case ProcedureCode_id_RICindication: {
1690 if (logLevel >= MDCLOG_DEBUG) {
1691 mdclog_write(MDCLOG_DEBUG, "Got RICindication %s", message.message.enodbName);
1692 }
1693 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.count; i++) {
ss412g1a79bdf2019-10-24 12:03:05 +03001694 auto messageSent = false;
1695 RICindication_IEs_t *ie = pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.array[i];
aa7133@att.com84bd3342020-03-16 18:04:57 +02001696 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03001697 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1698 }
1699 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001700 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03001701 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1702 }
1703 if (ie->value.present == RICindication_IEs__value_PR_RICrequestID) {
1704 static unsigned char tx[32];
1705 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_INDICATION;
1706 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1707 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
ss412g3bac2da2020-01-05 11:52:19 +02001708 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001709 (unsigned char *)message.message.enodbName,
1710 strlen(message.message.enodbName));
ss412g1a79bdf2019-10-24 12:03:05 +03001711 rmrMessageBuffer.sendMessage->state = 0;
aa7133@att.comc5a15202020-04-05 19:57:01 +03001712 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
1713
1714 //ie->value.choice.RICrequestID.ricInstanceID;
ss412g3bac2da2020-01-05 11:52:19 +02001715 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.comc5a15202020-04-05 19:57:01 +03001716 mdclog_write(MDCLOG_DEBUG, "sub id = %d, mtype = %d, ric instance id %ld, requestor id = %ld",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001717 rmrMessageBuffer.sendMessage->sub_id,
aa7133@att.comc5a15202020-04-05 19:57:01 +03001718 rmrMessageBuffer.sendMessage->mtype,
1719 ie->value.choice.RICrequestID.ricInstanceID,
1720 ie->value.choice.RICrequestID.ricRequestorID);
ss412g3bac2da2020-01-05 11:52:19 +02001721 }
dhirajverma6cf28612021-06-23 04:53:34 -04001722#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03001723 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICindication]->Increment();
1724 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICindication]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04001725#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02001726 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03001727 messageSent = true;
1728 } else {
aa7133@att.com409f2232020-10-29 13:06:45 +03001729 mdclog_write(MDCLOG_ERR, "RIC request id missing illegal request");
ss412g1a79bdf2019-10-24 12:03:05 +03001730 }
1731 }
1732 if (messageSent) {
1733 break;
1734 }
1735 }
1736 break;
1737 }
ss412g1a79bdf2019-10-24 12:03:05 +03001738 default: {
1739 mdclog_write(MDCLOG_ERR, "Undefined or not supported message = %ld", procedureCode);
1740 message.message.messageType = 0; // no RMR message type yet
1741
1742 buildJsonMessage(message);
1743
1744 break;
1745 }
1746 }
ss412g1a79bdf2019-10-24 12:03:05 +03001747}
1748
1749/**
1750 *
1751 * @param pdu
1752 * @param message
ss412g1a79bdf2019-10-24 12:03:05 +03001753 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001754 */
aa7133@att.com409f2232020-10-29 13:06:45 +03001755void asnSuccessfulMsg(E2AP_PDU_t *pdu,
aa7133@att.com7b437f72020-03-30 13:25:54 +03001756 Sctp_Map_t *sctpMap,
1757 ReportingMessages_t &message,
1758 RmrMessagesBuffer_t &rmrMessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03001759 auto procedureCode = pdu->choice.successfulOutcome->procedureCode;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001760 auto logLevel = mdclog_level_get();
1761 if (logLevel >= MDCLOG_INFO) {
ss412g1a79bdf2019-10-24 12:03:05 +03001762 mdclog_write(MDCLOG_INFO, "Successful Outcome %ld", procedureCode);
1763 }
1764 switch (procedureCode) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001765 case ProcedureCode_id_Reset: {
1766 if (logLevel >= MDCLOG_DEBUG) {
1767 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001768 }
dhirajverma6cf28612021-06-23 04:53:34 -04001769#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03001770 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
1771 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04001772#endif
ss412g4aeeda22020-06-16 14:12:41 +03001773 if (XML_From_PER(message, rmrMessageBuffer) < 0) {
1774 break;
1775 }
1776 if (sendRequestToXapp(message, RIC_E2_RESET_RESP, rmrMessageBuffer) != 0) {
1777 mdclog_write(MDCLOG_ERR, "RIC_E2_RESET_RESP message failed to send to xAPP");
ss412g1a79bdf2019-10-24 12:03:05 +03001778 }
1779 break;
1780 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001781 case ProcedureCode_id_RICcontrol: {
1782 if (logLevel >= MDCLOG_DEBUG) {
1783 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001784 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001785 for (auto i = 0;
ss412g1a79bdf2019-10-24 12:03:05 +03001786 i < pdu->choice.successfulOutcome->value.choice.RICcontrolAcknowledge.protocolIEs.list.count; i++) {
1787 auto messageSent = false;
1788 RICcontrolAcknowledge_IEs_t *ie = pdu->choice.successfulOutcome->value.choice.RICcontrolAcknowledge.protocolIEs.list.array[i];
1789 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1790 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1791 }
1792 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
1793 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1794 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1795 }
1796 if (ie->value.present == RICcontrolAcknowledge_IEs__value_PR_RICrequestID) {
1797 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_CONTROL_ACK;
1798 rmrMessageBuffer.sendMessage->state = 0;
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001799// rmrMessageBuffer.sendMessage->sub_id = (int) ie->value.choice.RICrequestID.ricRequestorID;
1800 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
1801
ss412g1a79bdf2019-10-24 12:03:05 +03001802 static unsigned char tx[32];
1803 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1804 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
ss412g3bac2da2020-01-05 11:52:19 +02001805 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001806 (unsigned char *)message.message.enodbName,
1807 strlen(message.message.enodbName));
dhirajverma6cf28612021-06-23 04:53:34 -04001808#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03001809 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
1810 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04001811#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02001812 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03001813 messageSent = true;
1814 } else {
aa7133@att.com409f2232020-10-29 13:06:45 +03001815 mdclog_write(MDCLOG_ERR, "RIC request id missing illegal request");
ss412g1a79bdf2019-10-24 12:03:05 +03001816 }
1817 }
1818 if (messageSent) {
1819 break;
1820 }
1821 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001822
1823 break;
1824 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001825 case ProcedureCode_id_RICsubscription: {
1826 if (logLevel >= MDCLOG_DEBUG) {
1827 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
1828 }
dhirajverma6cf28612021-06-23 04:53:34 -04001829#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03001830 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
1831 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04001832#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02001833 if (sendRequestToXapp(message, RIC_SUB_RESP, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001834 mdclog_write(MDCLOG_ERR, "Subscription successful message failed to send to xAPP");
1835 }
1836 break;
1837 }
1838 case ProcedureCode_id_RICsubscriptionDelete: {
1839 if (logLevel >= MDCLOG_DEBUG) {
1840 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
1841 }
dhirajverma6cf28612021-06-23 04:53:34 -04001842#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03001843 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
1844 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04001845#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02001846 if (sendRequestToXapp(message, RIC_SUB_DEL_RESP, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001847 mdclog_write(MDCLOG_ERR, "Subscription delete successful message failed to send to xAPP");
1848 }
ss412g1a79bdf2019-10-24 12:03:05 +03001849 break;
1850 }
1851 default: {
1852 mdclog_write(MDCLOG_WARN, "Undefined or not supported message = %ld", procedureCode);
1853 message.message.messageType = 0; // no RMR message type yet
1854 buildJsonMessage(message);
1855
1856 break;
1857 }
1858 }
ss412g1a79bdf2019-10-24 12:03:05 +03001859}
1860
1861/**
1862 *
1863 * @param pdu
1864 * @param message
ss412g1a79bdf2019-10-24 12:03:05 +03001865 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001866 */
1867void asnUnSuccsesfulMsg(E2AP_PDU_t *pdu,
aa7133@att.com7b437f72020-03-30 13:25:54 +03001868 Sctp_Map_t *sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +03001869 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +02001870 RmrMessagesBuffer_t &rmrMessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03001871 auto procedureCode = pdu->choice.unsuccessfulOutcome->procedureCode;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001872 auto logLevel = mdclog_level_get();
1873 if (logLevel >= MDCLOG_INFO) {
ss412g1a79bdf2019-10-24 12:03:05 +03001874 mdclog_write(MDCLOG_INFO, "Unsuccessful Outcome %ld", procedureCode);
1875 }
1876 switch (procedureCode) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001877 case ProcedureCode_id_RICcontrol: {
1878 if (logLevel >= MDCLOG_DEBUG) {
1879 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001880 }
1881 for (int i = 0;
1882 i < pdu->choice.unsuccessfulOutcome->value.choice.RICcontrolFailure.protocolIEs.list.count; i++) {
1883 auto messageSent = false;
1884 RICcontrolFailure_IEs_t *ie = pdu->choice.unsuccessfulOutcome->value.choice.RICcontrolFailure.protocolIEs.list.array[i];
aa7133@att.com84bd3342020-03-16 18:04:57 +02001885 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03001886 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1887 }
1888 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001889 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03001890 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1891 }
1892 if (ie->value.present == RICcontrolFailure_IEs__value_PR_RICrequestID) {
1893 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_CONTROL_FAILURE;
1894 rmrMessageBuffer.sendMessage->state = 0;
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001895// rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricRequestorID;
1896 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
ss412g1a79bdf2019-10-24 12:03:05 +03001897 static unsigned char tx[32];
1898 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1899 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
aa7133@att.com84bd3342020-03-16 18:04:57 +02001900 rmr_bytes2meid(rmrMessageBuffer.sendMessage, (unsigned char *) message.message.enodbName,
1901 strlen(message.message.enodbName));
dhirajverma6cf28612021-06-23 04:53:34 -04001902#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03001903 message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
1904 message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04001905#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02001906 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03001907 messageSent = true;
1908 } else {
aa7133@att.com409f2232020-10-29 13:06:45 +03001909 mdclog_write(MDCLOG_ERR, "RIC request id missing illegal request");
ss412g1a79bdf2019-10-24 12:03:05 +03001910 }
1911 }
1912 if (messageSent) {
1913 break;
1914 }
1915 }
1916 break;
1917 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001918 case ProcedureCode_id_RICsubscription: {
1919 if (logLevel >= MDCLOG_DEBUG) {
1920 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
1921 }
dhirajverma6cf28612021-06-23 04:53:34 -04001922#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03001923 message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
1924 message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04001925#endif
aa7133@att.combe92d712020-03-18 17:30:39 +02001926 if (sendRequestToXapp(message, RIC_SUB_FAILURE, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001927 mdclog_write(MDCLOG_ERR, "Subscription unsuccessful message failed to send to xAPP");
1928 }
1929 break;
1930 }
1931 case ProcedureCode_id_RICsubscriptionDelete: {
1932 if (logLevel >= MDCLOG_DEBUG) {
1933 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
1934 }
dhirajverma6cf28612021-06-23 04:53:34 -04001935#ifndef UNIT_TEST
aa7133@att.coma4c80012020-06-24 16:36:47 +03001936 message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
1937 message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment((double)message.message.asnLength);
dhirajverma6cf28612021-06-23 04:53:34 -04001938#endif
ss412g4aeeda22020-06-16 14:12:41 +03001939 if (sendRequestToXapp(message, RIC_SUB_FAILURE, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001940 mdclog_write(MDCLOG_ERR, "Subscription Delete unsuccessful message failed to send to xAPP");
1941 }
1942 break;
1943 }
ss412g1a79bdf2019-10-24 12:03:05 +03001944 default: {
1945 mdclog_write(MDCLOG_WARN, "Undefined or not supported message = %ld", procedureCode);
1946 message.message.messageType = 0; // no RMR message type yet
1947
1948 buildJsonMessage(message);
1949
1950 break;
1951 }
1952 }
ss412g1a79bdf2019-10-24 12:03:05 +03001953}
1954
1955/**
1956 *
1957 * @param message
1958 * @param requestId
1959 * @param rmrMmessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001960 * @return
1961 */
1962int sendRequestToXapp(ReportingMessages_t &message,
1963 int requestId,
aa7133@att.combe92d712020-03-18 17:30:39 +02001964 RmrMessagesBuffer_t &rmrMmessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03001965 rmr_bytes2meid(rmrMmessageBuffer.sendMessage,
1966 (unsigned char *)message.message.enodbName,
1967 strlen(message.message.enodbName));
1968 message.message.messageType = rmrMmessageBuffer.sendMessage->mtype = requestId;
1969 rmrMmessageBuffer.sendMessage->state = 0;
1970 static unsigned char tx[32];
1971 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1972 rmr_bytes2xact(rmrMmessageBuffer.sendMessage, tx, strlen((const char *) tx));
1973
aa7133@att.combe92d712020-03-18 17:30:39 +02001974 auto rc = sendRmrMessage(rmrMmessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03001975 return rc;
1976}
1977
ss412g4aeeda22020-06-16 14:12:41 +03001978/**
1979 *
1980 * @param pSctpParams
1981 */
aa7133@att.combe92d712020-03-18 17:30:39 +02001982void getRmrContext(sctp_params_t &pSctpParams) {
ss412g3bac2da2020-01-05 11:52:19 +02001983 pSctpParams.rmrCtx = nullptr;
aa7133@att.comab0e0f42020-04-06 17:13:58 +03001984 pSctpParams.rmrCtx = rmr_init(pSctpParams.rmrAddress, RECEIVE_XAPP_BUFFER_SIZE, RMRFL_NONE);
ss412g3bac2da2020-01-05 11:52:19 +02001985 if (pSctpParams.rmrCtx == nullptr) {
1986 mdclog_write(MDCLOG_ERR, "Failed to initialize RMR");
ss412g3bac2da2020-01-05 11:52:19 +02001987 return;
ss412g1a79bdf2019-10-24 12:03:05 +03001988 }
1989
ss412g3bac2da2020-01-05 11:52:19 +02001990 rmr_set_stimeout(pSctpParams.rmrCtx, 0); // disable retries for any send operation
ss412g1a79bdf2019-10-24 12:03:05 +03001991 // we need to find that routing table exist and we can run
1992 if (mdclog_level_get() >= MDCLOG_INFO) {
1993 mdclog_write(MDCLOG_INFO, "We are after RMR INIT wait for RMR_Ready");
1994 }
1995 int rmrReady = 0;
1996 int count = 0;
1997 while (!rmrReady) {
ss412g3bac2da2020-01-05 11:52:19 +02001998 if ((rmrReady = rmr_ready(pSctpParams.rmrCtx)) == 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03001999 sleep(1);
2000 }
2001 count++;
2002 if (count % 60 == 0) {
2003 mdclog_write(MDCLOG_INFO, "waiting to RMR ready state for %d seconds", count);
2004 }
2005 }
2006 if (mdclog_level_get() >= MDCLOG_INFO) {
2007 mdclog_write(MDCLOG_INFO, "RMR running");
2008 }
ss412g3bac2da2020-01-05 11:52:19 +02002009 rmr_init_trace(pSctpParams.rmrCtx, 200);
2010 // get the RMR fd for the epoll
2011 pSctpParams.rmrListenFd = rmr_get_rcvfd(pSctpParams.rmrCtx);
2012 struct epoll_event event{};
2013 // add RMR fd to epoll
2014 event.events = (EPOLLIN);
2015 event.data.fd = pSctpParams.rmrListenFd;
2016 // add listening RMR FD to epoll
2017 if (epoll_ctl(pSctpParams.epoll_fd, EPOLL_CTL_ADD, pSctpParams.rmrListenFd, &event)) {
2018 mdclog_write(MDCLOG_ERR, "Failed to add RMR descriptor to epoll");
2019 close(pSctpParams.rmrListenFd);
2020 rmr_close(pSctpParams.rmrCtx);
2021 pSctpParams.rmrCtx = nullptr;
2022 }
ss412g1a79bdf2019-10-24 12:03:05 +03002023}
2024
ss412g4aeeda22020-06-16 14:12:41 +03002025/**
2026 *
2027 * @param message
2028 * @param rmrMessageBuffer
2029 * @return
2030 */
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002031int PER_FromXML(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
aa7133@att.com07e256f2020-04-20 15:59:51 +03002032 E2AP_PDU_t *pdu = nullptr;
aa7133@att.com1c4f50f2020-03-30 14:30:52 +03002033
2034 if (mdclog_level_get() >= MDCLOG_DEBUG) {
ss412g4aeeda22020-06-16 14:12:41 +03002035 mdclog_write(MDCLOG_DEBUG, "got xml Format data from xApp of size %d is:%s",
aa7133@att.com07e256f2020-04-20 15:59:51 +03002036 rmrMessageBuffer.rcvMessage->len, rmrMessageBuffer.rcvMessage->payload);
aa7133@att.com1c4f50f2020-03-30 14:30:52 +03002037 }
aa7133@att.com1fecb052020-03-29 09:44:09 +03002038 auto rval = asn_decode(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, (void **) &pdu,
2039 rmrMessageBuffer.rcvMessage->payload, rmrMessageBuffer.rcvMessage->len);
aa7133@att.com409f2232020-10-29 13:06:45 +03002040 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2041 mdclog_write(MDCLOG_DEBUG, "%s After decoding the XML to PDU", __func__ );
2042 }
aa7133@att.com1fecb052020-03-29 09:44:09 +03002043 if (rval.code != RC_OK) {
aa7133@att.com1c4f50f2020-03-30 14:30:52 +03002044 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) setup response from E2MGR : %s",
aa7133@att.com1fecb052020-03-29 09:44:09 +03002045 rval.code,
2046 message.message.enodbName);
2047 return -1;
2048 }
2049
aa7133@att.coma0f0db62020-04-01 16:52:50 +03002050 int buff_size = RECEIVE_XAPP_BUFFER_SIZE;
2051 auto er = asn_encode_to_buffer(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, pdu,
2052 rmrMessageBuffer.rcvMessage->payload, buff_size);
aa7133@att.com409f2232020-10-29 13:06:45 +03002053 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2054 mdclog_write(MDCLOG_DEBUG, "%s After encoding PDU to PER", __func__ );
2055 }
aa7133@att.com1fecb052020-03-29 09:44:09 +03002056 if (er.encoded == -1) {
2057 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
2058 return -1;
aa7133@att.coma0f0db62020-04-01 16:52:50 +03002059 } else if (er.encoded > (ssize_t)buff_size) {
2060 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 +03002061 (int)rmrMessageBuffer.rcvMessage->len,
aa7133@att.coma0f0db62020-04-01 16:52:50 +03002062 asn_DEF_E2AP_PDU.name,
2063 __func__,
2064 __LINE__);
aa7133@att.com1fecb052020-03-29 09:44:09 +03002065 return -1;
2066 }
aa7133@att.coma0f0db62020-04-01 16:52:50 +03002067 rmrMessageBuffer.rcvMessage->len = er.encoded;
aa7133@att.com1fecb052020-03-29 09:44:09 +03002068 return 0;
2069}
2070
ss412g1a79bdf2019-10-24 12:03:05 +03002071/**
2072 *
ss412g1a79bdf2019-10-24 12:03:05 +03002073 * @param sctpMap
2074 * @param rmrMessageBuffer
2075 * @param ts
ss412g1a79bdf2019-10-24 12:03:05 +03002076 * @return
2077 */
aa7133@att.com6680c3b2020-03-22 12:06:58 +02002078int receiveXappMessages(Sctp_Map_t *sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +03002079 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +02002080 struct timespec &ts) {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002081 int loglevel = mdclog_level_get();
ss412g1a79bdf2019-10-24 12:03:05 +03002082 if (rmrMessageBuffer.rcvMessage == nullptr) {
2083 //we have error
2084 mdclog_write(MDCLOG_ERR, "RMR Allocation message, %s", strerror(errno));
ss412g1a79bdf2019-10-24 12:03:05 +03002085 return -1;
2086 }
2087
aa7133@att.comf13529a2020-08-27 11:21:16 +03002088// if (loglevel >= MDCLOG_DEBUG) {
2089// mdclog_write(MDCLOG_DEBUG, "Call to rmr_rcv_msg");
2090// }
ss412g1a79bdf2019-10-24 12:03:05 +03002091 rmrMessageBuffer.rcvMessage = rmr_rcv_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.rcvMessage);
2092 if (rmrMessageBuffer.rcvMessage == nullptr) {
aa7133@att.com409f2232020-10-29 13:06:45 +03002093 mdclog_write(MDCLOG_ERR, "RMR Receiving message with null pointer, Reallocated rmr message buffer");
ss412g1a79bdf2019-10-24 12:03:05 +03002094 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
ss412g1a79bdf2019-10-24 12:03:05 +03002095 return -2;
2096 }
2097 ReportingMessages_t message;
2098 message.message.direction = 'D';
2099 message.message.time.tv_nsec = ts.tv_nsec;
2100 message.message.time.tv_sec = ts.tv_sec;
2101
2102 // get message payload
2103 //auto msgData = msg->payload;
2104 if (rmrMessageBuffer.rcvMessage->state != 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +03002105 mdclog_write(MDCLOG_ERR, "RMR Receiving message with stat = %d", rmrMessageBuffer.rcvMessage->state);
ss412g1a79bdf2019-10-24 12:03:05 +03002106 return -1;
2107 }
aa7133@att.com1fecb052020-03-29 09:44:09 +03002108 rmr_get_meid(rmrMessageBuffer.rcvMessage, (unsigned char *)message.message.enodbName);
ss412g4aeeda22020-06-16 14:12:41 +03002109 message.peerInfo = (ConnectedCU_t *) sctpMap->find(message.message.enodbName);
2110 if (message.peerInfo == nullptr) {
2111 auto type = rmrMessageBuffer.rcvMessage->mtype;
2112 switch (type) {
2113 case RIC_SCTP_CLEAR_ALL:
2114 case E2_TERM_KEEP_ALIVE_REQ:
2115 case RIC_HEALTH_CHECK_REQ:
2116 break;
2117 default:
2118 mdclog_write(MDCLOG_ERR, "Failed to send message no CU entry %s", message.message.enodbName);
2119 return -1;
2120 }
2121 }
2122
aa7133@att.comde40d1c2020-08-26 16:19:20 +03002123 if (rmrMessageBuffer.rcvMessage->mtype != RIC_HEALTH_CHECK_REQ) {
2124 num_of_XAPP_messages.fetch_add(1, std::memory_order_release);
2125
2126 }
ss412g1a79bdf2019-10-24 12:03:05 +03002127 switch (rmrMessageBuffer.rcvMessage->mtype) {
aa7133@att.combe92d712020-03-18 17:30:39 +02002128 case RIC_E2_SETUP_RESP : {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002129 if (loglevel >= MDCLOG_DEBUG) {
2130 mdclog_write(MDCLOG_DEBUG, "RIC_E2_SETUP_RESP");
2131 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002132 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
aa7133@att.com1fecb052020-03-29 09:44:09 +03002133 break;
2134 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002135 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2136 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002137 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2138 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_SETUP_RESP");
2139 return -6;
ss412g1a79bdf2019-10-24 12:03:05 +03002140 }
2141 break;
2142 }
aa7133@att.combe92d712020-03-18 17:30:39 +02002143 case RIC_E2_SETUP_FAILURE : {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002144 if (loglevel >= MDCLOG_DEBUG) {
2145 mdclog_write(MDCLOG_DEBUG, "RIC_E2_SETUP_FAILURE");
2146 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002147 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
aa7133@att.com1fecb052020-03-29 09:44:09 +03002148 break;
2149 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002150 message.peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2151 message.peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002152 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2153 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_SETUP_FAILURE");
2154 return -6;
ss412g1a79bdf2019-10-24 12:03:05 +03002155 }
2156 break;
2157 }
aa7133@att.combe92d712020-03-18 17:30:39 +02002158 case RIC_ERROR_INDICATION: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002159 if (loglevel >= MDCLOG_DEBUG) {
2160 mdclog_write(MDCLOG_DEBUG, "RIC_ERROR_INDICATION");
2161 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002162 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_ErrorIndication]->Increment();
2163 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_ErrorIndication]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002164 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2165 mdclog_write(MDCLOG_ERR, "Failed to send RIC_ERROR_INDICATION");
ss412g1a79bdf2019-10-24 12:03:05 +03002166 return -6;
2167 }
2168 break;
2169 }
2170 case RIC_SUB_REQ: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002171 if (loglevel >= MDCLOG_DEBUG) {
2172 mdclog_write(MDCLOG_DEBUG, "RIC_SUB_REQ");
2173 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002174 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
2175 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002176 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002177 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SUB_REQ");
ss412g1a79bdf2019-10-24 12:03:05 +03002178 return -6;
2179 }
2180 break;
2181 }
2182 case RIC_SUB_DEL_REQ: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002183 if (loglevel >= MDCLOG_DEBUG) {
2184 mdclog_write(MDCLOG_DEBUG, "RIC_SUB_DEL_REQ");
2185 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002186 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
2187 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002188 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002189 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SUB_DEL_REQ");
ss412g1a79bdf2019-10-24 12:03:05 +03002190 return -6;
2191 }
2192 break;
2193 }
2194 case RIC_CONTROL_REQ: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002195 if (loglevel >= MDCLOG_DEBUG) {
2196 mdclog_write(MDCLOG_DEBUG, "RIC_CONTROL_REQ");
2197 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002198 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
2199 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002200 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002201 mdclog_write(MDCLOG_ERR, "Failed to send RIC_CONTROL_REQ");
ss412g1a79bdf2019-10-24 12:03:05 +03002202 return -6;
2203 }
2204 break;
2205 }
2206 case RIC_SERVICE_QUERY: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002207 if (loglevel >= MDCLOG_DEBUG) {
2208 mdclog_write(MDCLOG_DEBUG, "RIC_SERVICE_QUERY");
2209 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002210 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2211 break;
2212 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002213 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceQuery]->Increment();
2214 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceQuery]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002215 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002216 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_QUERY");
ss412g1a79bdf2019-10-24 12:03:05 +03002217 return -6;
2218 }
2219 break;
2220 }
2221 case RIC_SERVICE_UPDATE_ACK: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002222 if (loglevel >= MDCLOG_DEBUG) {
2223 mdclog_write(MDCLOG_DEBUG, "RIC_SERVICE_UPDATE_ACK");
2224 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002225 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
aa7133@att.com54885fe2020-10-29 17:24:49 +03002226 mdclog_write(MDCLOG_ERR, "error in PER_FromXML");
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002227 break;
2228 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002229 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
aa7133@att.com52771912020-10-29 17:45:01 +03002230 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.com54885fe2020-10-29 17:24:49 +03002231 if (loglevel >= MDCLOG_DEBUG) {
2232 mdclog_write(MDCLOG_DEBUG, "Before sending to CU");
2233 }
aa7133@att.combe92d712020-03-18 17:30:39 +02002234 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002235 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_UPDATE_ACK");
ss412g1a79bdf2019-10-24 12:03:05 +03002236 return -6;
2237 }
2238 break;
2239 }
2240 case RIC_SERVICE_UPDATE_FAILURE: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002241 if (loglevel >= MDCLOG_DEBUG) {
2242 mdclog_write(MDCLOG_DEBUG, "RIC_SERVICE_UPDATE_FAILURE");
2243 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002244 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2245 break;
2246 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002247 message.peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
aa7133@att.com7c07fee2020-10-29 17:54:38 +03002248 message.peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002249 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002250 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_UPDATE_FAILURE");
ss412g1a79bdf2019-10-24 12:03:05 +03002251 return -6;
2252 }
2253 break;
2254 }
ss412g4aeeda22020-06-16 14:12:41 +03002255 case RIC_E2_RESET_REQ: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002256 if (loglevel >= MDCLOG_DEBUG) {
2257 mdclog_write(MDCLOG_DEBUG, "RIC_E2_RESET_REQ");
2258 }
ss412g4aeeda22020-06-16 14:12:41 +03002259 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2260 break;
2261 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002262 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2263 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002264 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g4aeeda22020-06-16 14:12:41 +03002265 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_RESET");
ss412g1a79bdf2019-10-24 12:03:05 +03002266 return -6;
2267 }
2268 break;
2269 }
ss412g4aeeda22020-06-16 14:12:41 +03002270 case RIC_E2_RESET_RESP: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002271 if (loglevel >= MDCLOG_DEBUG) {
2272 mdclog_write(MDCLOG_DEBUG, "RIC_E2_RESET_RESP");
2273 }
ss412g4aeeda22020-06-16 14:12:41 +03002274 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2275 break;
2276 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002277 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2278 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002279 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g4aeeda22020-06-16 14:12:41 +03002280 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_RESET_RESP");
ss412g1a79bdf2019-10-24 12:03:05 +03002281 return -6;
2282 }
2283 break;
2284 }
2285 case RIC_SCTP_CLEAR_ALL: {
2286 mdclog_write(MDCLOG_INFO, "RIC_SCTP_CLEAR_ALL");
2287 // loop on all keys and close socket and then erase all map.
2288 vector<char *> v;
2289 sctpMap->getKeys(v);
2290 for (auto const &iter : v) { //}; iter != sctpMap.end(); iter++) {
2291 if (!boost::starts_with((string) (iter), "host:") && !boost::starts_with((string) (iter), "msg:")) {
2292 auto *peerInfo = (ConnectedCU_t *) sctpMap->find(iter);
2293 if (peerInfo == nullptr) {
2294 continue;
2295 }
2296 close(peerInfo->fileDescriptor);
2297 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
2298 message.message.direction = 'D';
2299 message.message.time.tv_nsec = ts.tv_nsec;
2300 message.message.time.tv_sec = ts.tv_sec;
2301
2302 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
2303 snprintf((char *)rmrMessageBuffer.sendMessage->payload,
aa7133@att.com96c9b882020-03-25 14:50:40 +02002304 256,
2305 "%s|RIC_SCTP_CLEAR_ALL",
2306 peerInfo->enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03002307 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
2308 mdclog_write(MDCLOG_INFO, "%s", message.message.asndata);
aa7133@att.combe92d712020-03-18 17:30:39 +02002309 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002310 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
2311 }
2312 free(peerInfo);
2313 }
2314 }
2315
2316 sleep(1);
2317 sctpMap->clear();
2318 break;
2319 }
ss412g3bac2da2020-01-05 11:52:19 +02002320 case E2_TERM_KEEP_ALIVE_REQ: {
2321 // send message back
ss412g3bac2da2020-01-05 11:52:19 +02002322 rmr_bytes2payload(rmrMessageBuffer.sendMessage,
aa7133@att.com96c9b882020-03-25 14:50:40 +02002323 (unsigned char *)rmrMessageBuffer.ka_message,
2324 rmrMessageBuffer.ka_message_len);
ss412g3bac2da2020-01-05 11:52:19 +02002325 rmrMessageBuffer.sendMessage->mtype = E2_TERM_KEEP_ALIVE_RESP;
2326 rmrMessageBuffer.sendMessage->state = 0;
2327 static unsigned char tx[32];
2328 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2329 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, txLen);
2330 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2331 if (rmrMessageBuffer.sendMessage == nullptr) {
2332 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
aa7133@att.com77fff432020-01-27 12:01:20 +02002333 mdclog_write(MDCLOG_ERR, "Failed to send E2_TERM_KEEP_ALIVE_RESP RMR message returned NULL");
ss412g3bac2da2020-01-05 11:52:19 +02002334 } else if (rmrMessageBuffer.sendMessage->state != 0) {
aa7133@att.com77fff432020-01-27 12:01:20 +02002335 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 +02002336 rmrMessageBuffer.sendMessage->state, translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str());
aa7133@att.comf13529a2020-08-27 11:21:16 +03002337 } else if (loglevel >= MDCLOG_DEBUG) {
aa7133@att.combc110772020-03-25 09:45:14 +02002338 mdclog_write(MDCLOG_DEBUG, "Got Keep Alive Request send : %s", rmrMessageBuffer.ka_message);
ss412g3bac2da2020-01-05 11:52:19 +02002339 }
aa7133@att.com342843e2020-01-26 12:53:34 +02002340
ss412g3bac2da2020-01-05 11:52:19 +02002341 break;
2342 }
ss412g4aeeda22020-06-16 14:12:41 +03002343 case RIC_HEALTH_CHECK_REQ: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002344 static int counter = 0;
ss412g4aeeda22020-06-16 14:12:41 +03002345 // send message back
aa7133@att.comc18fe532020-06-30 12:40:18 +03002346 rmr_bytes2payload(rmrMessageBuffer.rcvMessage,
ss412g4aeeda22020-06-16 14:12:41 +03002347 (unsigned char *)"OK",
2348 2);
aa7133@att.comc18fe532020-06-30 12:40:18 +03002349 rmrMessageBuffer.rcvMessage->mtype = RIC_HEALTH_CHECK_RESP;
2350 rmrMessageBuffer.rcvMessage->state = 0;
ss412g4aeeda22020-06-16 14:12:41 +03002351 static unsigned char tx[32];
2352 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
aa7133@att.comc18fe532020-06-30 12:40:18 +03002353 rmr_bytes2xact(rmrMessageBuffer.rcvMessage, tx, txLen);
2354 rmrMessageBuffer.rcvMessage = rmr_rts_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.rcvMessage);
2355 //rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2356 if (rmrMessageBuffer.rcvMessage == nullptr) {
2357 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
ss412g4aeeda22020-06-16 14:12:41 +03002358 mdclog_write(MDCLOG_ERR, "Failed to send RIC_HEALTH_CHECK_RESP RMR message returned NULL");
aa7133@att.comc18fe532020-06-30 12:40:18 +03002359 } else if (rmrMessageBuffer.rcvMessage->state != 0) {
ss412g4aeeda22020-06-16 14:12:41 +03002360 mdclog_write(MDCLOG_ERR, "Failed to send RIC_HEALTH_CHECK_RESP, on RMR state = %d ( %s)",
aa7133@att.comc18fe532020-06-30 12:40:18 +03002361 rmrMessageBuffer.rcvMessage->state, translateRmrErrorMessages(rmrMessageBuffer.rcvMessage->state).c_str());
aa7133@att.comf13529a2020-08-27 11:21:16 +03002362 } else if (loglevel >= MDCLOG_DEBUG && ++counter % 100 == 0) {
2363 mdclog_write(MDCLOG_DEBUG, "Got %d RIC_HEALTH_CHECK_REQ Request send : OK", counter);
ss412g4aeeda22020-06-16 14:12:41 +03002364 }
2365
2366 break;
2367 }
2368
ss412g1a79bdf2019-10-24 12:03:05 +03002369 default:
aa7133@att.com409f2232020-10-29 13:06:45 +03002370 mdclog_write(MDCLOG_WARN, "Message Type : %d is not supported", rmrMessageBuffer.rcvMessage->mtype);
ss412g1a79bdf2019-10-24 12:03:05 +03002371 message.message.asndata = rmrMessageBuffer.rcvMessage->payload;
2372 message.message.asnLength = rmrMessageBuffer.rcvMessage->len;
2373 message.message.time.tv_nsec = ts.tv_nsec;
2374 message.message.time.tv_sec = ts.tv_sec;
2375 message.message.messageType = rmrMessageBuffer.rcvMessage->mtype;
2376
2377 buildJsonMessage(message);
2378
2379
ss412g1a79bdf2019-10-24 12:03:05 +03002380 return -7;
2381 }
2382 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2383 mdclog_write(MDCLOG_DEBUG, "EXIT OK from %s", __FUNCTION__);
2384 }
ss412g1a79bdf2019-10-24 12:03:05 +03002385 return 0;
2386}
2387
2388/**
2389 * Send message to the CU that is not expecting for successful or unsuccessful results
2390 * @param messageBuffer
2391 * @param message
2392 * @param failedMsgId
2393 * @param sctpMap
ss412g1a79bdf2019-10-24 12:03:05 +03002394 * @return
2395 */
2396int sendDirectionalSctpMsg(RmrMessagesBuffer_t &messageBuffer,
2397 ReportingMessages_t &message,
2398 int failedMsgId,
aa7133@att.combe92d712020-03-18 17:30:39 +02002399 Sctp_Map_t *sctpMap) {
aa7133@att.com409f2232020-10-29 13:06:45 +03002400 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2401 mdclog_write(MDCLOG_DEBUG, "send message: %d to %s address", message.message.messageType, message.message.enodbName);
2402 }
ss412g1a79bdf2019-10-24 12:03:05 +03002403
aa7133@att.combe92d712020-03-18 17:30:39 +02002404 getRequestMetaData(message, messageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03002405 if (mdclog_level_get() >= MDCLOG_INFO) {
2406 mdclog_write(MDCLOG_INFO, "send message to %s address", message.message.enodbName);
2407 }
2408
aa7133@att.combe92d712020-03-18 17:30:39 +02002409 auto rc = sendMessagetoCu(sctpMap, messageBuffer, message, failedMsgId);
ss412g1a79bdf2019-10-24 12:03:05 +03002410 return rc;
2411}
2412
2413/**
2414 *
2415 * @param sctpMap
2416 * @param messageBuffer
2417 * @param message
2418 * @param failedMesgId
ss412g1a79bdf2019-10-24 12:03:05 +03002419 * @return
2420 */
2421int sendMessagetoCu(Sctp_Map_t *sctpMap,
2422 RmrMessagesBuffer_t &messageBuffer,
2423 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +02002424 int failedMesgId) {
ss412g1a79bdf2019-10-24 12:03:05 +03002425 // get the FD
2426 message.message.messageType = messageBuffer.rcvMessage->mtype;
ss412g4aeeda22020-06-16 14:12:41 +03002427 auto rc = sendSctpMsg(message.peerInfo, message, sctpMap);
ss412g1a79bdf2019-10-24 12:03:05 +03002428 return rc;
2429}
2430
ss412g1a79bdf2019-10-24 12:03:05 +03002431
2432/**
2433 *
2434 * @param epoll_fd
2435 * @param peerInfo
2436 * @param events
2437 * @param sctpMap
2438 * @param enodbName
2439 * @param msgType
ss412g1a79bdf2019-10-24 12:03:05 +03002440 * @return
2441 */
2442int addToEpoll(int epoll_fd,
2443 ConnectedCU_t *peerInfo,
2444 uint32_t events,
2445 Sctp_Map_t *sctpMap,
2446 char *enodbName,
aa7133@att.combe92d712020-03-18 17:30:39 +02002447 int msgType) {
ss412g1a79bdf2019-10-24 12:03:05 +03002448 // Add to Epol
2449 struct epoll_event event{};
2450 event.data.ptr = peerInfo;
2451 event.events = events;
2452 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, peerInfo->fileDescriptor, &event) < 0) {
2453 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com409f2232020-10-29 13:06:45 +03002454 mdclog_write(MDCLOG_DEBUG, "epoll_ctl EPOLL_CTL_ADD (may check not to quit here), %s, %s %d",
ss412g1a79bdf2019-10-24 12:03:05 +03002455 strerror(errno), __func__, __LINE__);
2456 }
2457 close(peerInfo->fileDescriptor);
aa7133@att.com84bd3342020-03-16 18:04:57 +02002458 if (enodbName != nullptr) {
aa7133@att.combe92d712020-03-18 17:30:39 +02002459 cleanHashEntry(peerInfo, sctpMap);
aa7133@att.com84bd3342020-03-16 18:04:57 +02002460 char key[MAX_ENODB_NAME_SIZE * 2];
2461 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", enodbName, msgType);
2462 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2463 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
2464 }
2465 auto tmp = sctpMap->find(key);
2466 if (tmp) {
2467 free(tmp);
2468 sctpMap->erase(key);
2469 }
2470 } else {
2471 peerInfo->enodbName[0] = 0;
ss412g1a79bdf2019-10-24 12:03:05 +03002472 }
aa7133@att.com409f2232020-10-29 13:06:45 +03002473 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may check not to quit here)");
ss412g1a79bdf2019-10-24 12:03:05 +03002474 return -1;
2475 }
ss412g1a79bdf2019-10-24 12:03:05 +03002476 return 0;
2477}
2478
2479/**
2480 *
2481 * @param epoll_fd
2482 * @param peerInfo
2483 * @param events
2484 * @param sctpMap
2485 * @param enodbName
2486 * @param msgType
ss412g1a79bdf2019-10-24 12:03:05 +03002487 * @return
2488 */
2489int modifyToEpoll(int epoll_fd,
2490 ConnectedCU_t *peerInfo,
2491 uint32_t events,
2492 Sctp_Map_t *sctpMap,
2493 char *enodbName,
aa7133@att.combe92d712020-03-18 17:30:39 +02002494 int msgType) {
ss412g1a79bdf2019-10-24 12:03:05 +03002495 // Add to Epol
2496 struct epoll_event event{};
2497 event.data.ptr = peerInfo;
2498 event.events = events;
2499 if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, peerInfo->fileDescriptor, &event) < 0) {
2500 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com409f2232020-10-29 13:06:45 +03002501 mdclog_write(MDCLOG_DEBUG, "epoll_ctl EPOLL_CTL_MOD (may check not to quit here), %s, %s %d",
ss412g1a79bdf2019-10-24 12:03:05 +03002502 strerror(errno), __func__, __LINE__);
2503 }
2504 close(peerInfo->fileDescriptor);
aa7133@att.combe92d712020-03-18 17:30:39 +02002505 cleanHashEntry(peerInfo, sctpMap);
ss412g1a79bdf2019-10-24 12:03:05 +03002506 char key[MAX_ENODB_NAME_SIZE * 2];
2507 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", enodbName, msgType);
2508 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2509 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
2510 }
aa7133@att.com96c9b882020-03-25 14:50:40 +02002511 auto tmp = sctpMap->find(key);
ss412g1a79bdf2019-10-24 12:03:05 +03002512 if (tmp) {
2513 free(tmp);
2514 }
2515 sctpMap->erase(key);
aa7133@att.com409f2232020-10-29 13:06:45 +03002516 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may check not to quit here)");
ss412g1a79bdf2019-10-24 12:03:05 +03002517 return -1;
2518 }
ss412g1a79bdf2019-10-24 12:03:05 +03002519 return 0;
2520}
2521
2522
aa7133@att.combe92d712020-03-18 17:30:39 +02002523int sendRmrMessage(RmrMessagesBuffer_t &rmrMessageBuffer, ReportingMessages_t &message) {
ss412g1a79bdf2019-10-24 12:03:05 +03002524 buildJsonMessage(message);
2525
2526 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2527
2528 if (rmrMessageBuffer.sendMessage == nullptr) {
2529 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
2530 mdclog_write(MDCLOG_ERR, "RMR failed send message returned with NULL pointer");
ss412g1a79bdf2019-10-24 12:03:05 +03002531 return -1;
2532 }
2533
2534 if (rmrMessageBuffer.sendMessage->state != 0) {
2535 char meid[RMR_MAX_MEID]{};
2536 if (rmrMessageBuffer.sendMessage->state == RMR_ERR_RETRY) {
2537 usleep(5);
2538 rmrMessageBuffer.sendMessage->state = 0;
2539 mdclog_write(MDCLOG_INFO, "RETRY sending Message type %d to Xapp from %s",
2540 rmrMessageBuffer.sendMessage->mtype,
2541 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
2542 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2543 if (rmrMessageBuffer.sendMessage == nullptr) {
2544 mdclog_write(MDCLOG_ERR, "RMR failed send message returned with NULL pointer");
2545 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
ss412g1a79bdf2019-10-24 12:03:05 +03002546 return -1;
2547 } else if (rmrMessageBuffer.sendMessage->state != 0) {
2548 mdclog_write(MDCLOG_ERR,
2549 "Message state %s while sending request %d to Xapp from %s after retry of 10 microseconds",
2550 translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str(),
2551 rmrMessageBuffer.sendMessage->mtype,
2552 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
2553 auto rc = rmrMessageBuffer.sendMessage->state;
ss412g1a79bdf2019-10-24 12:03:05 +03002554 return rc;
2555 }
2556 } else {
2557 mdclog_write(MDCLOG_ERR, "Message state %s while sending request %d to Xapp from %s",
2558 translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str(),
2559 rmrMessageBuffer.sendMessage->mtype,
2560 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
ss412g1a79bdf2019-10-24 12:03:05 +03002561 return rmrMessageBuffer.sendMessage->state;
2562 }
2563 }
2564 return 0;
2565}
2566
2567void buildJsonMessage(ReportingMessages_t &message) {
ss412g3bac2da2020-01-05 11:52:19 +02002568 if (jsonTrace) {
2569 message.outLen = sizeof(message.base64Data);
2570 base64::encode((const unsigned char *) message.message.asndata,
2571 (const int) message.message.asnLength,
2572 message.base64Data,
2573 message.outLen);
2574 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.comc5a15202020-04-05 19:57:01 +03002575 mdclog_write(MDCLOG_DEBUG, "Tracing: ASN length = %d, base64 message length = %d ",
ss412g3bac2da2020-01-05 11:52:19 +02002576 (int) message.message.asnLength,
2577 (int) message.outLen);
2578 }
2579
2580 snprintf(message.buffer, sizeof(message.buffer),
aa7133@att.com96c9b882020-03-25 14:50:40 +02002581 "{\"header\": {\"ts\": \"%ld.%09ld\","
2582 "\"ranName\": \"%s\","
2583 "\"messageType\": %d,"
2584 "\"direction\": \"%c\"},"
2585 "\"base64Length\": %d,"
2586 "\"asnBase64\": \"%s\"}",
2587 message.message.time.tv_sec,
2588 message.message.time.tv_nsec,
2589 message.message.enodbName,
2590 message.message.messageType,
2591 message.message.direction,
2592 (int) message.outLen,
2593 message.base64Data);
ss412g3bac2da2020-01-05 11:52:19 +02002594 static src::logger_mt &lg = my_logger::get();
2595
2596 BOOST_LOG(lg) << message.buffer;
ss412g1a79bdf2019-10-24 12:03:05 +03002597 }
ss412g1a79bdf2019-10-24 12:03:05 +03002598}
2599
2600
2601/**
2602 * take RMR error code to string
2603 * @param state
2604 * @return
2605 */
2606string translateRmrErrorMessages(int state) {
2607 string str = {};
2608 switch (state) {
2609 case RMR_OK:
2610 str = "RMR_OK - state is good";
2611 break;
2612 case RMR_ERR_BADARG:
aa7133@att.com409f2232020-10-29 13:06:45 +03002613 str = "RMR_ERR_BADARG - argument passed to function was unusable";
ss412g1a79bdf2019-10-24 12:03:05 +03002614 break;
2615 case RMR_ERR_NOENDPT:
2616 str = "RMR_ERR_NOENDPT - send//call could not find an endpoint based on msg type";
2617 break;
2618 case RMR_ERR_EMPTY:
2619 str = "RMR_ERR_EMPTY - msg received had no payload; attempt to send an empty message";
2620 break;
2621 case RMR_ERR_NOHDR:
2622 str = "RMR_ERR_NOHDR - message didn't contain a valid header";
2623 break;
2624 case RMR_ERR_SENDFAILED:
2625 str = "RMR_ERR_SENDFAILED - send failed; errno has nano reason";
2626 break;
2627 case RMR_ERR_CALLFAILED:
2628 str = "RMR_ERR_CALLFAILED - unable to send call() message";
2629 break;
2630 case RMR_ERR_NOWHOPEN:
2631 str = "RMR_ERR_NOWHOPEN - no wormholes are open";
2632 break;
2633 case RMR_ERR_WHID:
2634 str = "RMR_ERR_WHID - wormhole id was invalid";
2635 break;
2636 case RMR_ERR_OVERFLOW:
2637 str = "RMR_ERR_OVERFLOW - operation would have busted through a buffer/field size";
2638 break;
2639 case RMR_ERR_RETRY:
2640 str = "RMR_ERR_RETRY - request (send/call/rts) failed, but caller should retry (EAGAIN for wrappers)";
2641 break;
2642 case RMR_ERR_RCVFAILED:
2643 str = "RMR_ERR_RCVFAILED - receive failed (hard error)";
2644 break;
2645 case RMR_ERR_TIMEOUT:
2646 str = "RMR_ERR_TIMEOUT - message processing call timed out";
2647 break;
2648 case RMR_ERR_UNSET:
2649 str = "RMR_ERR_UNSET - the message hasn't been populated with a transport buffer";
2650 break;
2651 case RMR_ERR_TRUNC:
2652 str = "RMR_ERR_TRUNC - received message likely truncated";
2653 break;
2654 case RMR_ERR_INITFAILED:
2655 str = "RMR_ERR_INITFAILED - initialisation of something (probably message) failed";
2656 break;
2657 case RMR_ERR_NOTSUPP:
2658 str = "RMR_ERR_NOTSUPP - the request is not supported, or RMr was not initialised for the request";
2659 break;
2660 default:
2661 char buf[128]{};
2662 snprintf(buf, sizeof buf, "UNDOCUMENTED RMR_ERR : %d", state);
2663 str = buf;
2664 break;
2665 }
2666 return str;
2667}