blob: 7fa142e8b3312c7d048de85cf1bb155b3fc65d9b [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"
ss412g1a79bdf2019-10-24 12:03:05 +030026
aa7133@att.com7b437f72020-03-30 13:25:54 +030027#include "3rdparty/oranE2SM/E2SM-gNB-NRT-RANfunction-Definition.h"
aa7133@att.comc5a15202020-04-05 19:57:01 +030028#include "BuildXml.h"
aa7133@att.com233facd2020-04-02 00:10:46 +030029#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;
34
ss412g1a79bdf2019-10-24 12:03:05 +030035//#ifdef __cplusplus
36//extern "C"
37//{
38//#endif
39
ss412g3bac2da2020-01-05 11:52:19 +020040// need to expose without the include of gcov
41extern "C" void __gcov_flush(void);
42
43static void catch_function(int signal) {
44 __gcov_flush();
45 exit(signal);
46}
47
48
ss412g1a79bdf2019-10-24 12:03:05 +030049BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger, src::logger_mt)
50
51boost::shared_ptr<sinks::synchronous_sink<sinks::text_file_backend>> boostLogger;
ss412g3bac2da2020-01-05 11:52:19 +020052double cpuClock = 0.0;
53bool jsonTrace = true;
ss412g1a79bdf2019-10-24 12:03:05 +030054
55void init_log() {
56 mdclog_attr_t *attr;
57 mdclog_attr_init(&attr);
58 mdclog_attr_set_ident(attr, "E2Terminator");
59 mdclog_init(attr);
60 mdclog_attr_destroy(attr);
61}
ss412g3bac2da2020-01-05 11:52:19 +020062auto start_time = std::chrono::high_resolution_clock::now();
63typedef std::chrono::duration<double, std::ratio<1,1>> seconds_t;
ss412g1a79bdf2019-10-24 12:03:05 +030064
ss412g3bac2da2020-01-05 11:52:19 +020065double age() {
66 return seconds_t(std::chrono::high_resolution_clock::now() - start_time).count();
67}
68
69double approx_CPU_MHz(unsigned sleeptime) {
70 using namespace std::chrono_literals;
71 uint32_t aux = 0;
72 uint64_t cycles_start = rdtscp(aux);
73 double time_start = age();
74 std::this_thread::sleep_for(sleeptime * 1ms);
75 uint64_t elapsed_cycles = rdtscp(aux) - cycles_start;
76 double elapsed_time = age() - time_start;
77 return elapsed_cycles / elapsed_time;
78}
ss412g1a79bdf2019-10-24 12:03:05 +030079
80//std::atomic<int64_t> rmrCounter{0};
81std::atomic<int64_t> num_of_messages{0};
ss412g3bac2da2020-01-05 11:52:19 +020082std::atomic<int64_t> num_of_XAPP_messages{0};
ss412g1a79bdf2019-10-24 12:03:05 +030083static long transactionCounter = 0;
84
aa7133@att.com84bd3342020-03-16 18:04:57 +020085int buildListeningPort(sctp_params_t &sctpParams) {
86 sctpParams.listenFD = socket (AF_INET6, SOCK_STREAM, IPPROTO_SCTP);
87 struct sockaddr_in6 servaddr {};
88 servaddr.sin6_family = AF_INET6;
89 servaddr.sin6_addr = in6addr_any;
aa7133@att.combe92d712020-03-18 17:30:39 +020090 servaddr.sin6_port = htons(sctpParams.sctpPort);
aa7133@att.com84bd3342020-03-16 18:04:57 +020091 if (bind(sctpParams.listenFD, (SA *)&servaddr, sizeof(servaddr)) < 0 ) {
92 mdclog_write(MDCLOG_ERR, "Error binding. %s\n", strerror(errno));
93 return -1;
94 }
95 if (setSocketNoBlocking(sctpParams.listenFD) == -1) {
96 //mdclog_write(MDCLOG_ERR, "Error binding. %s", strerror(errno));
97 return -1;
98 }
99 if (mdclog_level_get() >= MDCLOG_DEBUG) {
100 struct sockaddr_in6 cliaddr {};
101 socklen_t len = sizeof(cliaddr);
102 getsockname(sctpParams.listenFD, (SA *)&cliaddr, &len);
103 char buff[1024] {};
104 inet_ntop(AF_INET6, &cliaddr.sin6_addr, buff, sizeof(buff));
105 mdclog_write(MDCLOG_DEBUG, "My address: %s, port %d\n", buff, htons(cliaddr.sin6_port));
ss412g1a79bdf2019-10-24 12:03:05 +0300106 }
107
aa7133@att.com84bd3342020-03-16 18:04:57 +0200108 if (listen(sctpParams.listenFD, SOMAXCONN) < 0) {
109 mdclog_write(MDCLOG_ERR, "Error listening. %s\n", strerror(errno));
110 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200111 }
aa7133@att.com84bd3342020-03-16 18:04:57 +0200112 struct epoll_event event {};
113 event.events = EPOLLIN | EPOLLET;
114 event.data.fd = sctpParams.listenFD;
115
116 // add listening port to epoll
117 if (epoll_ctl(sctpParams.epoll_fd, EPOLL_CTL_ADD, sctpParams.listenFD, &event)) {
118 printf("Failed to add descriptor to epoll\n");
119 mdclog_write(MDCLOG_ERR, "Failed to add descriptor to epoll. %s\n", strerror(errno));
120 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200121 }
122
aa7133@att.com84bd3342020-03-16 18:04:57 +0200123 return 0;
124}
ss412g3bac2da2020-01-05 11:52:19 +0200125
aa7133@att.com84bd3342020-03-16 18:04:57 +0200126int buildConfiguration(sctp_params_t &sctpParams) {
ss412g3bac2da2020-01-05 11:52:19 +0200127 path p = (sctpParams.configFilePath + "/" + sctpParams.configFileName).c_str();
128 if (exists(p)) {
129 const int size = 2048;
130 auto fileSize = file_size(p);
131 if (fileSize > size) {
132 mdclog_write(MDCLOG_ERR, "File %s larger than %d", p.string().c_str(), size);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200133 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200134 }
135 } else {
136 mdclog_write(MDCLOG_ERR, "Configuration File %s not exists", p.string().c_str());
aa7133@att.com84bd3342020-03-16 18:04:57 +0200137 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200138 }
139
ss412g3bac2da2020-01-05 11:52:19 +0200140 ReadConfigFile conf;
141 if (conf.openConfigFile(p.string()) == -1) {
142 mdclog_write(MDCLOG_ERR, "Filed to open config file %s, %s",
143 p.string().c_str(), strerror(errno));
aa7133@att.com84bd3342020-03-16 18:04:57 +0200144 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200145 }
146 int rmrPort = conf.getIntValue("nano");
147 if (rmrPort == -1) {
148 mdclog_write(MDCLOG_ERR, "illigal RMR port ");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200149 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200150 }
151 sctpParams.rmrPort = (uint16_t)rmrPort;
152 snprintf(sctpParams.rmrAddress, sizeof(sctpParams.rmrAddress), "%d", (int) (sctpParams.rmrPort));
153
154 auto tmpStr = conf.getStringValue("loglevel");
155 if (tmpStr.length() == 0) {
156 mdclog_write(MDCLOG_ERR, "illigal loglevel. Set loglevel to MDCLOG_INFO");
157 tmpStr = "info";
158 }
159 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
160
161 if ((tmpStr.compare("debug")) == 0) {
162 sctpParams.logLevel = MDCLOG_DEBUG;
163 } else if ((tmpStr.compare("info")) == 0) {
164 sctpParams.logLevel = MDCLOG_INFO;
165 } else if ((tmpStr.compare("warning")) == 0) {
166 sctpParams.logLevel = MDCLOG_WARN;
167 } else if ((tmpStr.compare("error")) == 0) {
168 sctpParams.logLevel = MDCLOG_ERR;
169 } else {
170 mdclog_write(MDCLOG_ERR, "illigal loglevel = %s. Set loglevel to MDCLOG_INFO", tmpStr.c_str());
171 sctpParams.logLevel = MDCLOG_INFO;
172 }
173 mdclog_level_set(sctpParams.logLevel);
174
175 tmpStr = conf.getStringValue("volume");
176 if (tmpStr.length() == 0) {
177 mdclog_write(MDCLOG_ERR, "illigal volume.");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200178 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200179 }
180
ss412g1a79bdf2019-10-24 12:03:05 +0300181 char tmpLogFilespec[VOLUME_URL_SIZE];
182 tmpLogFilespec[0] = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200183 sctpParams.volume[0] = 0;
184 snprintf(sctpParams.volume, VOLUME_URL_SIZE, "%s", tmpStr.c_str());
185 // copy the name to temp file as well
186 snprintf(tmpLogFilespec, VOLUME_URL_SIZE, "%s", tmpStr.c_str());
187
188
189 // define the file name in the tmp directory under the volume
190 strcat(tmpLogFilespec,"/tmp/E2Term_%Y-%m-%d_%H-%M-%S.%N.tmpStr");
191
192 sctpParams.myIP = conf.getStringValue("local-ip");
193 if (sctpParams.myIP.length() == 0) {
194 mdclog_write(MDCLOG_ERR, "illigal local-ip.");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200195 return -1;
ss412g1a79bdf2019-10-24 12:03:05 +0300196 }
197
aa7133@att.combe92d712020-03-18 17:30:39 +0200198 int sctpPort = conf.getIntValue("sctp-port");
199 if (sctpPort == -1) {
200 mdclog_write(MDCLOG_ERR, "illigal SCTP port ");
201 return -1;
202 }
203 sctpParams.sctpPort = (uint16_t)sctpPort;
204
aa7133@att.com84bd3342020-03-16 18:04:57 +0200205 sctpParams.fqdn = conf.getStringValue("external-fqdn");
206 if (sctpParams.fqdn.length() == 0) {
207 mdclog_write(MDCLOG_ERR, "illigal external-fqdn");
208 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200209 }
ss412g1a79bdf2019-10-24 12:03:05 +0300210
aa7133@att.com342843e2020-01-26 12:53:34 +0200211 std::string pod = conf.getStringValue("pod_name");
212 if (pod.length() == 0) {
213 mdclog_write(MDCLOG_ERR, "illigal pod_name in config file");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200214 return -1;
aa7133@att.com342843e2020-01-26 12:53:34 +0200215 }
216 auto *podName = getenv(pod.c_str());
217 if (podName == nullptr) {
218 mdclog_write(MDCLOG_ERR, "illigal pod_name or environment varible not exists : %s", pod.c_str());
aa7133@att.com84bd3342020-03-16 18:04:57 +0200219 return -1;
aa7133@att.com342843e2020-01-26 12:53:34 +0200220
221 } else {
222 sctpParams.podName.assign(podName);
223 if (sctpParams.podName.length() == 0) {
224 mdclog_write(MDCLOG_ERR, "illigal pod_name");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200225 return -1;
aa7133@att.com342843e2020-01-26 12:53:34 +0200226 }
227 }
228
ss412g3bac2da2020-01-05 11:52:19 +0200229 tmpStr = conf.getStringValue("trace");
230 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
231 if ((tmpStr.compare("start")) == 0) {
232 mdclog_write(MDCLOG_INFO, "Trace set to: start");
233 sctpParams.trace = true;
234 } else if ((tmpStr.compare("stop")) == 0) {
235 mdclog_write(MDCLOG_INFO, "Trace set to: stop");
236 sctpParams.trace = false;
237 }
238 jsonTrace = sctpParams.trace;
ss412g1a79bdf2019-10-24 12:03:05 +0300239
aa7133@att.combe92d712020-03-18 17:30:39 +0200240 sctpParams.ka_message_length = snprintf(sctpParams.ka_message, KA_MESSAGE_SIZE, "{\"address\": \"%s:%d\","
aa7133@att.com96c9b882020-03-25 14:50:40 +0200241 "\"fqdn\": \"%s\","
242 "\"pod_name\": \"%s\"}",
aa7133@att.com342843e2020-01-26 12:53:34 +0200243 (const char *)sctpParams.myIP.c_str(),
244 sctpParams.rmrPort,
245 sctpParams.fqdn.c_str(),
246 sctpParams.podName.c_str());
247
ss412g1a79bdf2019-10-24 12:03:05 +0300248 if (mdclog_level_get() >= MDCLOG_INFO) {
ss412g3bac2da2020-01-05 11:52:19 +0200249 mdclog_mdc_add("RMR Port", to_string(sctpParams.rmrPort).c_str());
250 mdclog_mdc_add("LogLevel", to_string(sctpParams.logLevel).c_str());
251 mdclog_mdc_add("volume", sctpParams.volume);
ss412g1a79bdf2019-10-24 12:03:05 +0300252 mdclog_mdc_add("tmpLogFilespec", tmpLogFilespec);
ss412g3bac2da2020-01-05 11:52:19 +0200253 mdclog_mdc_add("my ip", sctpParams.myIP.c_str());
aa7133@att.com342843e2020-01-26 12:53:34 +0200254 mdclog_mdc_add("pod name", sctpParams.podName.c_str());
ss412g1a79bdf2019-10-24 12:03:05 +0300255
aa7133@att.com342843e2020-01-26 12:53:34 +0200256 mdclog_write(MDCLOG_INFO, "running parameters for instance : %s", sctpParams.ka_message);
ss412g1a79bdf2019-10-24 12:03:05 +0300257 }
258 mdclog_mdc_clean();
259
260 // Files written to the current working directory
261 boostLogger = logging::add_file_log(
ss412g3bac2da2020-01-05 11:52:19 +0200262 keywords::file_name = tmpLogFilespec, // to temp directory
ss412g1a79bdf2019-10-24 12:03:05 +0300263 keywords::rotation_size = 10 * 1024 * 1024,
264 keywords::time_based_rotation = sinks::file::rotation_at_time_interval(posix_time::hours(1)),
265 keywords::format = "%Message%"
ss412g3bac2da2020-01-05 11:52:19 +0200266 //keywords::format = "[%TimeStamp%]: %Message%" // use each tmpStr with time stamp
ss412g1a79bdf2019-10-24 12:03:05 +0300267 );
268
269 // Setup a destination folder for collecting rotated (closed) files --since the same volumn can use rename()
270 boostLogger->locked_backend()->set_file_collector(sinks::file::make_collector(
ss412g3bac2da2020-01-05 11:52:19 +0200271 keywords::target = sctpParams.volume
ss412g1a79bdf2019-10-24 12:03:05 +0300272 ));
273
274 // Upon restart, scan the directory for files matching the file_name pattern
275 boostLogger->locked_backend()->scan_for_files();
276
ss412g3bac2da2020-01-05 11:52:19 +0200277 // Enable auto-flushing after each tmpStr record written
ss412g1a79bdf2019-10-24 12:03:05 +0300278 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com84bd3342020-03-16 18:04:57 +0200279 boostLogger->locked_backend()->auto_flush(true);
280 }
281
282 return 0;
283}
284
aa7133@att.com16c799b2020-03-24 18:01:51 +0200285
286
aa7133@att.com84bd3342020-03-16 18:04:57 +0200287int main(const int argc, char **argv) {
288 sctp_params_t sctpParams;
289
aa7133@att.com84bd3342020-03-16 18:04:57 +0200290 {
291 std::random_device device{};
292 std::mt19937 generator(device());
293 std::uniform_int_distribution<long> distribution(1, (long) 1e12);
294 transactionCounter = distribution(generator);
295 }
296
297// uint64_t st = 0;
298// uint32_t aux1 = 0;
299// st = rdtscp(aux1);
300
301 unsigned num_cpus = std::thread::hardware_concurrency();
302 init_log();
303 mdclog_level_set(MDCLOG_INFO);
304
305 if (std::signal(SIGINT, catch_function) == SIG_ERR) {
306 mdclog_write(MDCLOG_ERR, "Error initializing SIGINT");
307 exit(1);
308 }
309 if (std::signal(SIGABRT, catch_function)== SIG_ERR) {
310 mdclog_write(MDCLOG_ERR, "Error initializing SIGABRT");
311 exit(1);
312 }
313 if (std::signal(SIGTERM, catch_function)== SIG_ERR) {
314 mdclog_write(MDCLOG_ERR, "Error initializing SIGTERM");
315 exit(1);
316 }
317
318 cpuClock = approx_CPU_MHz(100);
319
320 mdclog_write(MDCLOG_DEBUG, "CPU speed %11.11f", cpuClock);
321
322 auto result = parse(argc, argv, sctpParams);
323
324 if (buildConfiguration(sctpParams) != 0) {
325 exit(-1);
ss412g1a79bdf2019-10-24 12:03:05 +0300326 }
327
328 // start epoll
ss412g3bac2da2020-01-05 11:52:19 +0200329 sctpParams.epoll_fd = epoll_create1(0);
330 if (sctpParams.epoll_fd == -1) {
ss412g1a79bdf2019-10-24 12:03:05 +0300331 mdclog_write(MDCLOG_ERR, "failed to open epoll descriptor");
332 exit(-1);
333 }
334
aa7133@att.combe92d712020-03-18 17:30:39 +0200335 getRmrContext(sctpParams);
ss412g3bac2da2020-01-05 11:52:19 +0200336 if (sctpParams.rmrCtx == nullptr) {
337 close(sctpParams.epoll_fd);
ss412g1a79bdf2019-10-24 12:03:05 +0300338 exit(-1);
339 }
340
ss412g3bac2da2020-01-05 11:52:19 +0200341 if (buildInotify(sctpParams) == -1) {
342 close(sctpParams.rmrListenFd);
343 rmr_close(sctpParams.rmrCtx);
344 close(sctpParams.epoll_fd);
345 exit(-1);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200346 }
347
348 if (buildListeningPort(sctpParams) != 0) {
349 close(sctpParams.rmrListenFd);
350 rmr_close(sctpParams.rmrCtx);
351 close(sctpParams.epoll_fd);
352 exit(-1);
353 }
ss412g3bac2da2020-01-05 11:52:19 +0200354
355 sctpParams.sctpMap = new mapWrapper();
ss412g1a79bdf2019-10-24 12:03:05 +0300356
357 std::vector<std::thread> threads(num_cpus);
358// std::vector<std::thread> threads;
359
360 num_cpus = 1;
361 for (unsigned int i = 0; i < num_cpus; i++) {
ss412g3bac2da2020-01-05 11:52:19 +0200362 threads[i] = std::thread(listener, &sctpParams);
ss412g1a79bdf2019-10-24 12:03:05 +0300363
364 cpu_set_t cpuset;
365 CPU_ZERO(&cpuset);
366 CPU_SET(i, &cpuset);
367 int rc = pthread_setaffinity_np(threads[i].native_handle(), sizeof(cpu_set_t), &cpuset);
368 if (rc != 0) {
369 mdclog_write(MDCLOG_ERR, "Error calling pthread_setaffinity_np: %d", rc);
370 }
ss412g1a79bdf2019-10-24 12:03:05 +0300371 }
372
aa7133@att.com16c799b2020-03-24 18:01:51 +0200373 auto statFlag = false;
374 auto statThread = std::thread(statColectorThread, (void *)&statFlag);
375
ss412g3bac2da2020-01-05 11:52:19 +0200376 //loop over term_init until first message from xApp
377 handleTermInit(sctpParams);
378
379 for (auto &t : threads) {
380 t.join();
381 }
382
aa7133@att.com16c799b2020-03-24 18:01:51 +0200383 statFlag = true;
384 statThread.join();
385
ss412g3bac2da2020-01-05 11:52:19 +0200386 return 0;
387}
388
389void handleTermInit(sctp_params_t &sctpParams) {
390 sendTermInit(sctpParams);
ss412g1a79bdf2019-10-24 12:03:05 +0300391 //send to e2 manager init of e2 term
392 //E2_TERM_INIT
ss412g1a79bdf2019-10-24 12:03:05 +0300393
ss412g3bac2da2020-01-05 11:52:19 +0200394 int count = 0;
395 while (true) {
396 auto xappMessages = num_of_XAPP_messages.load(std::memory_order_acquire);
397 if (xappMessages > 0) {
398 if (mdclog_level_get() >= MDCLOG_INFO) {
aa7133@att.com84bd3342020-03-16 18:04:57 +0200399 mdclog_write(MDCLOG_INFO, "Got a message from some appliction, stop sending E2_TERM_INIT");
ss412g3bac2da2020-01-05 11:52:19 +0200400 }
401 return;
402 }
403 usleep(100000);
404 count++;
405 if (count % 1000 == 0) {
406 mdclog_write(MDCLOG_ERR, "GOT No messages from any xApp");
407 sendTermInit(sctpParams);
408 }
409 }
410}
411
412void sendTermInit(sctp_params_t &sctpParams) {
413 rmr_mbuf_t *msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.ka_message_length);
ss412g1a79bdf2019-10-24 12:03:05 +0300414 auto count = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200415 while (true) {
ss412g1a79bdf2019-10-24 12:03:05 +0300416 msg->mtype = E2_TERM_INIT;
417 msg->state = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200418 rmr_bytes2payload(msg, (unsigned char *)sctpParams.ka_message, sctpParams.ka_message_length);
ss412g1a79bdf2019-10-24 12:03:05 +0300419 static unsigned char tx[32];
420 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
421 rmr_bytes2xact(msg, tx, txLen);
ss412g3bac2da2020-01-05 11:52:19 +0200422 msg = rmr_send_msg(sctpParams.rmrCtx, msg);
ss412g1a79bdf2019-10-24 12:03:05 +0300423 if (msg == nullptr) {
aa7133@att.comc9575aa2020-04-02 11:40:09 +0300424 msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.ka_message_length);
ss412g1a79bdf2019-10-24 12:03:05 +0300425 } else if (msg->state == 0) {
ss412g1a79bdf2019-10-24 12:03:05 +0300426 rmr_free_msg(msg);
ss412g3bac2da2020-01-05 11:52:19 +0200427 if (mdclog_level_get() >= MDCLOG_INFO) {
428 mdclog_write(MDCLOG_INFO, "E2_TERM_INIT succsesfuly sent ");
429 }
430 return;
ss412g1a79bdf2019-10-24 12:03:05 +0300431 } else {
432 if (count % 100 == 0) {
aa7133@att.com11b17d42020-03-22 11:35:16 +0200433 mdclog_write(MDCLOG_ERR, "Error sending E2_TERM_INIT cause : %s ", translateRmrErrorMessages(msg->state).c_str());
ss412g1a79bdf2019-10-24 12:03:05 +0300434 }
435 sleep(1);
436 }
437 count++;
438 }
ss412g3bac2da2020-01-05 11:52:19 +0200439}
440
441/**
442 *
443 * @param argc
444 * @param argv
445 * @param sctpParams
446 * @return
447 */
448cxxopts::ParseResult parse(int argc, char *argv[], sctp_params_t &sctpParams) {
449 cxxopts::Options options(argv[0], "e2 term help");
450 options.positional_help("[optional args]").show_positional_help();
451 options.allow_unrecognised_options().add_options()
452 ("p,path", "config file path", cxxopts::value<std::string>(sctpParams.configFilePath)->default_value("config"))
453 ("f,file", "config file name", cxxopts::value<std::string>(sctpParams.configFileName)->default_value("config.conf"))
454 ("h,help", "Print help");
455
456 auto result = options.parse(argc, argv);
457
458 if (result.count("help")) {
459 std::cout << options.help({""}) << std::endl;
460 exit(0);
461 }
462 return result;
463}
464
465/**
466 *
467 * @param sctpParams
468 * @return -1 failed 0 success
469 */
470int buildInotify(sctp_params_t &sctpParams) {
471 sctpParams.inotifyFD = inotify_init1(IN_NONBLOCK);
472 if (sctpParams.inotifyFD == -1) {
473 mdclog_write(MDCLOG_ERR, "Failed to init inotify (inotify_init1) %s", strerror(errno));
474 close(sctpParams.rmrListenFd);
475 rmr_close(sctpParams.rmrCtx);
476 close(sctpParams.epoll_fd);
477 return -1;
ss412g1a79bdf2019-10-24 12:03:05 +0300478 }
479
ss412g3bac2da2020-01-05 11:52:19 +0200480 sctpParams.inotifyWD = inotify_add_watch(sctpParams.inotifyFD,
aa7133@att.com96c9b882020-03-25 14:50:40 +0200481 (const char *)sctpParams.configFilePath.c_str(),
aa7133@att.com84bd3342020-03-16 18:04:57 +0200482 (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 +0200483 if (sctpParams.inotifyWD == -1) {
484 mdclog_write(MDCLOG_ERR, "Failed to add directory : %s to inotify (inotify_add_watch) %s",
aa7133@att.com96c9b882020-03-25 14:50:40 +0200485 sctpParams.configFilePath.c_str(),
486 strerror(errno));
ss412g3bac2da2020-01-05 11:52:19 +0200487 close(sctpParams.inotifyFD);
488 return -1;
489 }
490
491 struct epoll_event event{};
492 event.events = (EPOLLIN);
493 event.data.fd = sctpParams.inotifyFD;
494 // add listening RMR FD to epoll
495 if (epoll_ctl(sctpParams.epoll_fd, EPOLL_CTL_ADD, sctpParams.inotifyFD, &event)) {
496 mdclog_write(MDCLOG_ERR, "Failed to add inotify FD to epoll");
497 close(sctpParams.inotifyFD);
498 return -1;
499 }
ss412g1a79bdf2019-10-24 12:03:05 +0300500 return 0;
501}
502
503/**
504 *
505 * @param args
506 * @return
507 */
508void listener(sctp_params_t *params) {
ss412g1a79bdf2019-10-24 12:03:05 +0300509 int num_of_SCTP_messages = 0;
ss412g1a79bdf2019-10-24 12:03:05 +0300510 auto totalTime = 0.0;
511 mdclog_mdc_clean();
512 mdclog_level_set(params->logLevel);
513
514 std::thread::id this_id = std::this_thread::get_id();
515 //save cout
516 streambuf *oldCout = cout.rdbuf();
517 ostringstream memCout;
518 // create new cout
519 cout.rdbuf(memCout.rdbuf());
520 cout << this_id;
521 //return to the normal cout
522 cout.rdbuf(oldCout);
523
524 char tid[32];
525 memcpy(tid, memCout.str().c_str(), memCout.str().length() < 32 ? memCout.str().length() : 31);
526 tid[memCout.str().length()] = 0;
527 mdclog_mdc_add("thread id", tid);
528
529 if (mdclog_level_get() >= MDCLOG_DEBUG) {
530 mdclog_write(MDCLOG_DEBUG, "started thread number %s", tid);
531 }
532
aa7133@att.com342843e2020-01-26 12:53:34 +0200533
ss412g1a79bdf2019-10-24 12:03:05 +0300534 RmrMessagesBuffer_t rmrMessageBuffer{};
535 //create and init RMR
536 rmrMessageBuffer.rmrCtx = params->rmrCtx;
537
538 auto *events = (struct epoll_event *) calloc(MAXEVENTS, sizeof(struct epoll_event));
539 struct timespec end{0, 0};
540 struct timespec start{0, 0};
541
542 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
543 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
544
ss412g3bac2da2020-01-05 11:52:19 +0200545 memcpy(rmrMessageBuffer.ka_message, params->ka_message, params->ka_message_length);
546 rmrMessageBuffer.ka_message_len = params->ka_message_length;
547 rmrMessageBuffer.ka_message[rmrMessageBuffer.ka_message_len] = 0;
548
549 if (mdclog_level_get() >= MDCLOG_DEBUG) {
550 mdclog_write(MDCLOG_DEBUG, "keep alive message is : %s", rmrMessageBuffer.ka_message);
551 }
552
ss412g1a79bdf2019-10-24 12:03:05 +0300553 ReportingMessages_t message {};
554
aa7133@att.com7c3e4c02020-03-25 16:37:19 +0200555// for (int i = 0; i < MAX_RMR_BUFF_ARRY; i++) {
556// rmrMessageBuffer.rcvBufferedMessages[i] = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
557// rmrMessageBuffer.sendBufferedMessages[i] = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
558// }
ss412g1a79bdf2019-10-24 12:03:05 +0300559
aa7133@att.com16c799b2020-03-24 18:01:51 +0200560 message.statCollector = StatCollector::GetInstance();
561
ss412g1a79bdf2019-10-24 12:03:05 +0300562 while (true) {
563 if (mdclog_level_get() >= MDCLOG_DEBUG) {
564 mdclog_write(MDCLOG_DEBUG, "Start EPOLL Wait");
565 }
566 auto numOfEvents = epoll_wait(params->epoll_fd, events, MAXEVENTS, -1);
567 if (numOfEvents < 0 && errno == EINTR) {
568 if (mdclog_level_get() >= MDCLOG_DEBUG) {
569 mdclog_write(MDCLOG_DEBUG, "got EINTR : %s", strerror(errno));
570 }
571 continue;
572 }
573 if (numOfEvents < 0) {
574 mdclog_write(MDCLOG_ERR, "Epoll wait failed, errno = %s", strerror(errno));
575 return;
576 }
577 for (auto i = 0; i < numOfEvents; i++) {
578 if (mdclog_level_get() >= MDCLOG_DEBUG) {
579 mdclog_write(MDCLOG_DEBUG, "handling epoll event %d out of %d", i + 1, numOfEvents);
580 }
581 clock_gettime(CLOCK_MONOTONIC, &message.message.time);
582 start.tv_sec = message.message.time.tv_sec;
583 start.tv_nsec = message.message.time.tv_nsec;
ss412g3bac2da2020-01-05 11:52:19 +0200584
585
ss412g1a79bdf2019-10-24 12:03:05 +0300586 if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP)) {
aa7133@att.combe92d712020-03-18 17:30:39 +0200587 handlepoll_error(events[i], message, rmrMessageBuffer, params);
ss412g1a79bdf2019-10-24 12:03:05 +0300588 } else if (events[i].events & EPOLLOUT) {
aa7133@att.combe92d712020-03-18 17:30:39 +0200589 handleEinprogressMessages(events[i], message, rmrMessageBuffer, params);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200590 } else if (params->listenFD == events[i].data.fd) {
aa7133@att.com6680c3b2020-03-22 12:06:58 +0200591 if (mdclog_level_get() >= MDCLOG_INFO) {
592 mdclog_write(MDCLOG_INFO, "New connection request from sctp network\n");
593 }
aa7133@att.com84bd3342020-03-16 18:04:57 +0200594 // new connection is requested from RAN start build connection
595 while (true) {
596 struct sockaddr in_addr {};
597 socklen_t in_len;
598 char hostBuff[NI_MAXHOST];
599 char portBuff[NI_MAXSERV];
600
601 in_len = sizeof(in_addr);
602 auto *peerInfo = (ConnectedCU_t *)calloc(1, sizeof(ConnectedCU_t));
603 peerInfo->sctpParams = params;
604 peerInfo->fileDescriptor = accept(params->listenFD, &in_addr, &in_len);
605 if (peerInfo->fileDescriptor == -1) {
606 if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
607 /* We have processed all incoming connections. */
608 break;
609 } else {
610 mdclog_write(MDCLOG_ERR, "Accept error, errno = %s", strerror(errno));
611 break;
612 }
613 }
614 if (setSocketNoBlocking(peerInfo->fileDescriptor) == -1) {
615 mdclog_write(MDCLOG_ERR, "setSocketNoBlocking failed to set new connection %s on port %s\n", hostBuff, portBuff);
616 close(peerInfo->fileDescriptor);
617 break;
618 }
619 auto ans = getnameinfo(&in_addr, in_len,
aa7133@att.com96c9b882020-03-25 14:50:40 +0200620 peerInfo->hostName, NI_MAXHOST,
621 peerInfo->portNumber, NI_MAXSERV, (unsigned )((unsigned int)NI_NUMERICHOST | (unsigned int)NI_NUMERICSERV));
aa7133@att.com84bd3342020-03-16 18:04:57 +0200622 if (ans < 0) {
623 mdclog_write(MDCLOG_ERR, "Failed to get info on connection request. %s\n", strerror(errno));
624 close(peerInfo->fileDescriptor);
625 break;
626 }
627 if (mdclog_level_get() >= MDCLOG_DEBUG) {
628 mdclog_write(MDCLOG_DEBUG, "Accepted connection on descriptor %d (host=%s, port=%s)\n", peerInfo->fileDescriptor, peerInfo->hostName, peerInfo->portNumber);
629 }
630 peerInfo->isConnected = false;
631 peerInfo->gotSetup = false;
632 if (addToEpoll(params->epoll_fd,
633 peerInfo,
634 (EPOLLIN | EPOLLET),
635 params->sctpMap, nullptr,
aa7133@att.combe92d712020-03-18 17:30:39 +0200636 0) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +0200637 break;
638 }
aa7133@att.com6680c3b2020-03-22 12:06:58 +0200639 break;
aa7133@att.com84bd3342020-03-16 18:04:57 +0200640 }
ss412g1a79bdf2019-10-24 12:03:05 +0300641 } else if (params->rmrListenFd == events[i].data.fd) {
642 // got message from XAPP
ss412g3bac2da2020-01-05 11:52:19 +0200643 num_of_XAPP_messages.fetch_add(1, std::memory_order_release);
ss412g1a79bdf2019-10-24 12:03:05 +0300644 num_of_messages.fetch_add(1, std::memory_order_release);
645 if (mdclog_level_get() >= MDCLOG_DEBUG) {
646 mdclog_write(MDCLOG_DEBUG, "new message from RMR");
647 }
aa7133@att.com6680c3b2020-03-22 12:06:58 +0200648 if (receiveXappMessages(params->sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +0300649 rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200650 message.message.time) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +0300651 mdclog_write(MDCLOG_ERR, "Error handling Xapp message");
652 }
ss412g3bac2da2020-01-05 11:52:19 +0200653 } else if (params->inotifyFD == events[i].data.fd) {
654 mdclog_write(MDCLOG_INFO, "Got event from inotify (configuration update)");
655 handleConfigChange(params);
ss412g1a79bdf2019-10-24 12:03:05 +0300656 } else {
657 /* We RMR_ERR_RETRY have data on the fd waiting to be read. Read and display it.
658 * We must read whatever data is available completely, as we are running
659 * in edge-triggered mode and won't get a notification again for the same data. */
660 num_of_messages.fetch_add(1, std::memory_order_release);
661 if (mdclog_level_get() >= MDCLOG_DEBUG) {
662 mdclog_write(MDCLOG_DEBUG, "new message from SCTP, epoll flags are : %0x", events[i].events);
663 }
664 receiveDataFromSctp(&events[i],
665 params->sctpMap,
666 num_of_SCTP_messages,
667 rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200668 message.message.time);
ss412g1a79bdf2019-10-24 12:03:05 +0300669 }
670
671 clock_gettime(CLOCK_MONOTONIC, &end);
672 if (mdclog_level_get() >= MDCLOG_INFO) {
673 totalTime += ((end.tv_sec + 1.0e-9 * end.tv_nsec) -
674 ((double) start.tv_sec + 1.0e-9 * start.tv_nsec));
675 }
676 if (mdclog_level_get() >= MDCLOG_DEBUG) {
677 mdclog_write(MDCLOG_DEBUG, "message handling is %ld seconds %ld nanoseconds",
678 end.tv_sec - start.tv_sec,
679 end.tv_nsec - start.tv_nsec);
680 }
681 }
682 }
ss412g1a79bdf2019-10-24 12:03:05 +0300683}
684
685/**
686 *
ss412g3bac2da2020-01-05 11:52:19 +0200687 * @param sctpParams
688 */
689void handleConfigChange(sctp_params_t *sctpParams) {
690 char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event))));
691 const struct inotify_event *event;
692 char *ptr;
693
694 path p = (sctpParams->configFilePath + "/" + sctpParams->configFileName).c_str();
695 auto endlessLoop = true;
696 while (endlessLoop) {
697 auto len = read(sctpParams->inotifyFD, buf, sizeof buf);
698 if (len == -1) {
699 if (errno != EAGAIN) {
700 mdclog_write(MDCLOG_ERR, "read %s ", strerror(errno));
701 endlessLoop = false;
702 continue;
703 }
704 else {
705 endlessLoop = false;
706 continue;
707 }
708 }
709
710 for (ptr = buf; ptr < buf + len; ptr += sizeof(struct inotify_event) + event->len) {
711 event = (const struct inotify_event *)ptr;
712 if (event->mask & (uint32_t)IN_ISDIR) {
713 continue;
714 }
715
716 // the directory name
717 if (sctpParams->inotifyWD == event->wd) {
718 // not the directory
719 }
720 if (event->len) {
aa7133@att.comdfc1db92020-03-31 17:32:04 +0300721 auto retVal = strcmp(sctpParams->configFileName.c_str(), event->name);
722 if (retVal != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200723 continue;
724 }
725 }
726 // only the file we want
727 if (event->mask & (uint32_t)IN_CLOSE_WRITE) {
aa7133@att.comdfc1db92020-03-31 17:32:04 +0300728 if (mdclog_level_get() >= MDCLOG_INFO) {
729 mdclog_write(MDCLOG_INFO, "Configuration file changed");
730 }
ss412g3bac2da2020-01-05 11:52:19 +0200731 if (exists(p)) {
732 const int size = 2048;
733 auto fileSize = file_size(p);
734 if (fileSize > size) {
735 mdclog_write(MDCLOG_ERR, "File %s larger than %d", p.string().c_str(), size);
736 return;
737 }
738 } else {
739 mdclog_write(MDCLOG_ERR, "Configuration File %s not exists", p.string().c_str());
740 return;
741 }
742
743 ReadConfigFile conf;
744 if (conf.openConfigFile(p.string()) == -1) {
745 mdclog_write(MDCLOG_ERR, "Filed to open config file %s, %s",
746 p.string().c_str(), strerror(errno));
747 return;
748 }
749
750 auto tmpStr = conf.getStringValue("loglevel");
751 if (tmpStr.length() == 0) {
752 mdclog_write(MDCLOG_ERR, "illigal loglevel. Set loglevel to MDCLOG_INFO");
753 tmpStr = "info";
754 }
755 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
756
757 if ((tmpStr.compare("debug")) == 0) {
758 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_DEBUG");
759 sctpParams->logLevel = MDCLOG_DEBUG;
760 } else if ((tmpStr.compare("info")) == 0) {
761 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_INFO");
762 sctpParams->logLevel = MDCLOG_INFO;
763 } else if ((tmpStr.compare("warning")) == 0) {
764 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_WARN");
765 sctpParams->logLevel = MDCLOG_WARN;
766 } else if ((tmpStr.compare("error")) == 0) {
767 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_ERR");
768 sctpParams->logLevel = MDCLOG_ERR;
769 } else {
770 mdclog_write(MDCLOG_ERR, "illigal loglevel = %s. Set loglevel to MDCLOG_INFO", tmpStr.c_str());
771 sctpParams->logLevel = MDCLOG_INFO;
772 }
773 mdclog_level_set(sctpParams->logLevel);
774
775
776 tmpStr = conf.getStringValue("trace");
777 if (tmpStr.length() == 0) {
778 mdclog_write(MDCLOG_ERR, "illigal trace. Set trace to stop");
779 tmpStr = "stop";
780 }
781
782 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
783 if ((tmpStr.compare("start")) == 0) {
784 mdclog_write(MDCLOG_INFO, "Trace set to: start");
785 sctpParams->trace = true;
786 } else if ((tmpStr.compare("stop")) == 0) {
787 mdclog_write(MDCLOG_INFO, "Trace set to: stop");
788 sctpParams->trace = false;
789 } else {
790 mdclog_write(MDCLOG_ERR, "Trace was set to wrong value %s, set to stop", tmpStr.c_str());
791 sctpParams->trace = false;
792 }
793 jsonTrace = sctpParams->trace;
794 endlessLoop = false;
795 }
796 }
797 }
798}
799
800/**
801 *
802 * @param event
803 * @param message
804 * @param rmrMessageBuffer
805 * @param params
ss412g3bac2da2020-01-05 11:52:19 +0200806 */
807void handleEinprogressMessages(struct epoll_event &event,
808 ReportingMessages_t &message,
809 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200810 sctp_params_t *params) {
ss412g3bac2da2020-01-05 11:52:19 +0200811 auto *peerInfo = (ConnectedCU_t *)event.data.ptr;
812 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
813
814 mdclog_write(MDCLOG_INFO, "file descriptor %d got EPOLLOUT", peerInfo->fileDescriptor);
815 auto retVal = 0;
816 socklen_t retValLen = 0;
817 auto rc = getsockopt(peerInfo->fileDescriptor, SOL_SOCKET, SO_ERROR, &retVal, &retValLen);
818 if (rc != 0 || retVal != 0) {
819 if (rc != 0) {
820 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
821 "%s|Failed SCTP Connection, after EINPROGRESS the getsockopt%s",
822 peerInfo->enodbName, strerror(errno));
823 } else if (retVal != 0) {
824 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
825 "%s|Failed SCTP Connection after EINPROGRESS, SO_ERROR",
826 peerInfo->enodbName);
827 }
828
829 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
830 message.message.asnLength = rmrMessageBuffer.sendMessage->len;
831 mdclog_write(MDCLOG_ERR, "%s", rmrMessageBuffer.sendMessage->payload);
832 message.message.direction = 'N';
aa7133@att.combe92d712020-03-18 17:30:39 +0200833 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200834 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
835 }
836 memset(peerInfo->asnData, 0, peerInfo->asnLength);
837 peerInfo->asnLength = 0;
838 peerInfo->mtype = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200839 return;
840 }
841
842 peerInfo->isConnected = true;
843
844 if (modifyToEpoll(params->epoll_fd, peerInfo, (EPOLLIN | EPOLLET), params->sctpMap, peerInfo->enodbName,
aa7133@att.combe92d712020-03-18 17:30:39 +0200845 peerInfo->mtype) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200846 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_MOD");
ss412g3bac2da2020-01-05 11:52:19 +0200847 return;
848 }
849
850 message.message.asndata = (unsigned char *)peerInfo->asnData;
851 message.message.asnLength = peerInfo->asnLength;
852 message.message.messageType = peerInfo->mtype;
853 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
854 num_of_messages.fetch_add(1, std::memory_order_release);
855 if (mdclog_level_get() >= MDCLOG_DEBUG) {
856 mdclog_write(MDCLOG_DEBUG, "send the delayed SETUP/ENDC SETUP to sctp for %s",
857 message.message.enodbName);
858 }
aa7133@att.combe92d712020-03-18 17:30:39 +0200859 if (sendSctpMsg(peerInfo, message, params->sctpMap) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200860 if (mdclog_level_get() >= MDCLOG_DEBUG) {
861 mdclog_write(MDCLOG_DEBUG, "Error write to SCTP %s %d", __func__, __LINE__);
862 }
ss412g3bac2da2020-01-05 11:52:19 +0200863 return;
864 }
865
866 memset(peerInfo->asnData, 0, peerInfo->asnLength);
867 peerInfo->asnLength = 0;
868 peerInfo->mtype = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200869}
870
871
872void handlepoll_error(struct epoll_event &event,
873 ReportingMessages_t &message,
874 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200875 sctp_params_t *params) {
ss412g3bac2da2020-01-05 11:52:19 +0200876 if (event.data.fd != params->rmrListenFd) {
877 auto *peerInfo = (ConnectedCU_t *)event.data.ptr;
878 mdclog_write(MDCLOG_ERR, "epoll error, events %0x on fd %d, RAN NAME : %s",
879 event.events, peerInfo->fileDescriptor, peerInfo->enodbName);
880
881 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
882 "%s|Failed SCTP Connection",
883 peerInfo->enodbName);
884 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
885 message.message.asnLength = rmrMessageBuffer.sendMessage->len;
886
887 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
888 message.message.direction = 'N';
aa7133@att.combe92d712020-03-18 17:30:39 +0200889 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200890 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
891 }
892
893 close(peerInfo->fileDescriptor);
aa7133@att.com7b437f72020-03-30 13:25:54 +0300894 params->sctpMap->erase(peerInfo->enodbName);
aa7133@att.combe92d712020-03-18 17:30:39 +0200895 cleanHashEntry((ConnectedCU_t *) event.data.ptr, params->sctpMap);
ss412g3bac2da2020-01-05 11:52:19 +0200896 } else {
897 mdclog_write(MDCLOG_ERR, "epoll error, events %0x on RMR FD", event.events);
898 }
ss412g3bac2da2020-01-05 11:52:19 +0200899}
900/**
901 *
ss412g1a79bdf2019-10-24 12:03:05 +0300902 * @param socket
903 * @return
904 */
905int setSocketNoBlocking(int socket) {
906 auto flags = fcntl(socket, F_GETFL, 0);
907
908 if (flags == -1) {
909 mdclog_mdc_add("func", "fcntl");
910 mdclog_write(MDCLOG_ERR, "%s, %s", __FUNCTION__, strerror(errno));
911 mdclog_mdc_clean();
912 return -1;
913 }
914
915 flags = (unsigned) flags | (unsigned) O_NONBLOCK;
916 if (fcntl(socket, F_SETFL, flags) == -1) {
917 mdclog_mdc_add("func", "fcntl");
918 mdclog_write(MDCLOG_ERR, "%s, %s", __FUNCTION__, strerror(errno));
919 mdclog_mdc_clean();
920 return -1;
921 }
922
923 return 0;
924}
925
926/**
927 *
928 * @param val
929 * @param m
ss412g1a79bdf2019-10-24 12:03:05 +0300930 */
aa7133@att.combe92d712020-03-18 17:30:39 +0200931void cleanHashEntry(ConnectedCU_t *val, Sctp_Map_t *m) {
ss412g1a79bdf2019-10-24 12:03:05 +0300932 char *dummy;
933 auto port = (uint16_t) strtol(val->portNumber, &dummy, 10);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200934 char searchBuff[2048]{};
ss412g1a79bdf2019-10-24 12:03:05 +0300935
936 snprintf(searchBuff, sizeof searchBuff, "host:%s:%d", val->hostName, port);
937 m->erase(searchBuff);
938
939 m->erase(val->enodbName);
940 free(val);
ss412g1a79bdf2019-10-24 12:03:05 +0300941}
942
943/**
944 *
945 * @param fd file discriptor
946 * @param data the asn data to send
947 * @param len length of the data
948 * @param enodbName the enodbName as in the map for printing purpose
949 * @param m map host information
950 * @param mtype message number
951 * @return 0 success, anegative number on fail
952 */
aa7133@att.combe92d712020-03-18 17:30:39 +0200953int sendSctpMsg(ConnectedCU_t *peerInfo, ReportingMessages_t &message, Sctp_Map_t *m) {
ss412g1a79bdf2019-10-24 12:03:05 +0300954 auto loglevel = mdclog_level_get();
955 int fd = peerInfo->fileDescriptor;
956 if (loglevel >= MDCLOG_DEBUG) {
957 mdclog_write(MDCLOG_DEBUG, "Send SCTP message for CU %s, %s",
958 message.message.enodbName, __FUNCTION__);
959 }
960
961 while (true) {
ss412g1a79bdf2019-10-24 12:03:05 +0300962 if (send(fd,message.message.asndata, message.message.asnLength,MSG_NOSIGNAL) < 0) {
963 if (errno == EINTR) {
964 continue;
965 }
966 mdclog_write(MDCLOG_ERR, "error writing to CU a message, %s ", strerror(errno));
aa7133@att.com84bd3342020-03-16 18:04:57 +0200967 if (!peerInfo->isConnected) {
968 mdclog_write(MDCLOG_ERR, "connection to CU %s is still in progress.", message.message.enodbName);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200969 return -1;
970 }
aa7133@att.combe92d712020-03-18 17:30:39 +0200971 cleanHashEntry(peerInfo, m);
ss412g1a79bdf2019-10-24 12:03:05 +0300972 close(fd);
973 char key[MAX_ENODB_NAME_SIZE * 2];
974 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", message.message.enodbName,
975 message.message.messageType);
976 if (loglevel >= MDCLOG_DEBUG) {
977 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
978 }
aa7133@att.com96c9b882020-03-25 14:50:40 +0200979 auto tmp = m->find(key);
ss412g1a79bdf2019-10-24 12:03:05 +0300980 if (tmp) {
981 free(tmp);
982 }
983 m->erase(key);
ss412g1a79bdf2019-10-24 12:03:05 +0300984 return -1;
985 }
aa7133@att.com1eca27c2020-04-02 11:45:09 +0300986 // TODO remove stat update
aa7133@att.comc9575aa2020-04-02 11:40:09 +0300987 //message.statCollector->incSentMessage(string(message.message.enodbName));
ss412g1a79bdf2019-10-24 12:03:05 +0300988 message.message.direction = 'D';
989 // send report.buffer of size
990 buildJsonMessage(message);
991
992 if (loglevel >= MDCLOG_DEBUG) {
993 mdclog_write(MDCLOG_DEBUG,
994 "SCTP message for CU %s sent from %s",
995 message.message.enodbName,
996 __FUNCTION__);
997 }
ss412g1a79bdf2019-10-24 12:03:05 +0300998 return 0;
999 }
1000}
1001
1002/**
1003 *
1004 * @param message
1005 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001006 */
aa7133@att.combe92d712020-03-18 17:30:39 +02001007void getRequestMetaData(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
1008 rmr_get_meid(rmrMessageBuffer.rcvMessage, (unsigned char *) (message.message.enodbName));
ss412g1a79bdf2019-10-24 12:03:05 +03001009
1010 message.message.asndata = rmrMessageBuffer.rcvMessage->payload;
1011 message.message.asnLength = rmrMessageBuffer.rcvMessage->len;
1012
1013 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1014 mdclog_write(MDCLOG_DEBUG, "Message from Xapp RAN name = %s message length = %ld",
1015 message.message.enodbName, (unsigned long) message.message.asnLength);
1016 }
ss412g1a79bdf2019-10-24 12:03:05 +03001017}
1018
1019
ss412g1a79bdf2019-10-24 12:03:05 +03001020
1021/**
1022 *
1023 * @param events
1024 * @param sctpMap
1025 * @param numOfMessages
1026 * @param rmrMessageBuffer
1027 * @param ts
ss412g1a79bdf2019-10-24 12:03:05 +03001028 * @return
1029 */
1030int receiveDataFromSctp(struct epoll_event *events,
1031 Sctp_Map_t *sctpMap,
1032 int &numOfMessages,
1033 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +02001034 struct timespec &ts) {
ss412g1a79bdf2019-10-24 12:03:05 +03001035 /* We have data on the fd waiting to be read. Read and display it.
1036 * We must read whatever data is available completely, as we are running
1037 * in edge-triggered mode and won't get a notification again for the same data. */
aa7133@att.com84bd3342020-03-16 18:04:57 +02001038 ReportingMessages_t message {};
1039 auto done = 0;
ss412g1a79bdf2019-10-24 12:03:05 +03001040 auto loglevel = mdclog_level_get();
aa7133@att.com84bd3342020-03-16 18:04:57 +02001041
ss412g1a79bdf2019-10-24 12:03:05 +03001042 // get the identity of the interface
aa7133@att.com84bd3342020-03-16 18:04:57 +02001043 message.peerInfo = (ConnectedCU_t *)events->data.ptr;
1044
aa7133@att.com16c799b2020-03-24 18:01:51 +02001045 message.statCollector = StatCollector::GetInstance();
ss412g1a79bdf2019-10-24 12:03:05 +03001046 struct timespec start{0, 0};
1047 struct timespec decodestart{0, 0};
1048 struct timespec end{0, 0};
1049
1050 E2AP_PDU_t *pdu = nullptr;
1051
ss412g1a79bdf2019-10-24 12:03:05 +03001052
1053 while (true) {
1054 if (loglevel >= MDCLOG_DEBUG) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001055 mdclog_write(MDCLOG_DEBUG, "Start Read from SCTP %d fd", message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001056 clock_gettime(CLOCK_MONOTONIC, &start);
1057 }
1058 // read the buffer directly to rmr payload
1059 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1060 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
aa7133@att.com84bd3342020-03-16 18:04:57 +02001061 read(message.peerInfo->fileDescriptor, rmrMessageBuffer.sendMessage->payload, RECEIVE_SCTP_BUFFER_SIZE);
1062
ss412g1a79bdf2019-10-24 12:03:05 +03001063 if (loglevel >= MDCLOG_DEBUG) {
1064 mdclog_write(MDCLOG_DEBUG, "Finish Read from SCTP %d fd message length = %ld",
aa7133@att.com96c9b882020-03-25 14:50:40 +02001065 message.peerInfo->fileDescriptor, message.message.asnLength);
ss412g1a79bdf2019-10-24 12:03:05 +03001066 }
aa7133@att.com16c799b2020-03-24 18:01:51 +02001067
aa7133@att.com84bd3342020-03-16 18:04:57 +02001068 memcpy(message.message.enodbName, message.peerInfo->enodbName, sizeof(message.peerInfo->enodbName));
aa7133@att.com16c799b2020-03-24 18:01:51 +02001069 message.statCollector->incRecvMessage(string(message.message.enodbName));
ss412g1a79bdf2019-10-24 12:03:05 +03001070 message.message.direction = 'U';
1071 message.message.time.tv_nsec = ts.tv_nsec;
1072 message.message.time.tv_sec = ts.tv_sec;
1073
1074 if (message.message.asnLength < 0) {
1075 if (errno == EINTR) {
1076 continue;
1077 }
1078 /* If errno == EAGAIN, that means we have read all
aa7133@att.com84bd3342020-03-16 18:04:57 +02001079 data. So goReportingMessages_t back to the main loop. */
ss412g1a79bdf2019-10-24 12:03:05 +03001080 if (errno != EAGAIN) {
1081 mdclog_write(MDCLOG_ERR, "Read error, %s ", strerror(errno));
1082 done = 1;
1083 } else if (loglevel >= MDCLOG_DEBUG) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001084 mdclog_write(MDCLOG_DEBUG, "EAGAIN - descriptor = %d", message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001085 }
1086 break;
1087 } else if (message.message.asnLength == 0) {
1088 /* End of file. The remote has closed the connection. */
1089 if (loglevel >= MDCLOG_INFO) {
1090 mdclog_write(MDCLOG_INFO, "END of File Closed connection - descriptor = %d",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001091 message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001092 }
1093 done = 1;
1094 break;
1095 }
1096
ss412g1a79bdf2019-10-24 12:03:05 +03001097 if (loglevel >= MDCLOG_DEBUG) {
1098 char printBuffer[4096]{};
1099 char *tmp = printBuffer;
1100 for (size_t i = 0; i < (size_t)message.message.asnLength; ++i) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001101 snprintf(tmp, 3, "%02x", message.message.asndata[i]);
ss412g1a79bdf2019-10-24 12:03:05 +03001102 tmp += 2;
1103 }
1104 printBuffer[message.message.asnLength] = 0;
1105 clock_gettime(CLOCK_MONOTONIC, &end);
1106 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 +02001107 message.peerInfo->enodbName, end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec);
ss412g1a79bdf2019-10-24 12:03:05 +03001108 mdclog_write(MDCLOG_DEBUG, "PDU buffer length = %ld, data = : %s", message.message.asnLength,
1109 printBuffer);
1110 clock_gettime(CLOCK_MONOTONIC, &decodestart);
1111 }
1112
aa7133@att.com7b437f72020-03-30 13:25:54 +03001113 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, (void **) &pdu,
ss412g1a79bdf2019-10-24 12:03:05 +03001114 message.message.asndata, message.message.asnLength);
1115 if (rval.code != RC_OK) {
1116 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2AP PDU from RAN : %s", rval.code,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001117 message.peerInfo->enodbName);
aa7133@att.com9dff4f02020-03-29 09:55:22 +03001118 //todo may need reset to pdu
ss412g1a79bdf2019-10-24 12:03:05 +03001119 break;
1120 }
1121
1122 if (loglevel >= MDCLOG_DEBUG) {
1123 clock_gettime(CLOCK_MONOTONIC, &end);
1124 mdclog_write(MDCLOG_DEBUG, "After Encoding E2AP PDU for : %s, Read time is : %ld seconds, %ld nanoseconds",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001125 message.peerInfo->enodbName, end.tv_sec - decodestart.tv_sec, end.tv_nsec - decodestart.tv_nsec);
ss412g1a79bdf2019-10-24 12:03:05 +03001126 char *printBuffer;
1127 size_t size;
1128 FILE *stream = open_memstream(&printBuffer, &size);
1129 asn_fprint(stream, &asn_DEF_E2AP_PDU, pdu);
1130 mdclog_write(MDCLOG_DEBUG, "Encoding E2AP PDU past : %s", printBuffer);
1131 clock_gettime(CLOCK_MONOTONIC, &decodestart);
1132 }
1133
1134 switch (pdu->present) {
1135 case E2AP_PDU_PR_initiatingMessage: {//initiating message
aa7133@att.com7b437f72020-03-30 13:25:54 +03001136 asnInitiatingRequest(pdu, sctpMap,message, rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001137 break;
1138 }
1139 case E2AP_PDU_PR_successfulOutcome: { //successful outcome
aa7133@att.com7b437f72020-03-30 13:25:54 +03001140 asnSuccsesfulMsg(pdu, sctpMap, message, rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001141 break;
1142 }
1143 case E2AP_PDU_PR_unsuccessfulOutcome: { //Unsuccessful Outcome
aa7133@att.com7b437f72020-03-30 13:25:54 +03001144 asnUnSuccsesfulMsg(pdu, sctpMap, message, rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001145 break;
1146 }
1147 default:
1148 mdclog_write(MDCLOG_ERR, "Unknown index %d in E2AP PDU", pdu->present);
1149 break;
1150 }
1151 if (loglevel >= MDCLOG_DEBUG) {
1152 clock_gettime(CLOCK_MONOTONIC, &end);
1153 mdclog_write(MDCLOG_DEBUG,
1154 "After processing message and sent to rmr for : %s, Read time is : %ld seconds, %ld nanoseconds",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001155 message.peerInfo->enodbName, end.tv_sec - decodestart.tv_sec, end.tv_nsec - decodestart.tv_nsec);
ss412g1a79bdf2019-10-24 12:03:05 +03001156 }
1157 numOfMessages++;
ss412g1a79bdf2019-10-24 12:03:05 +03001158 if (pdu != nullptr) {
aa7133@att.com1fecb052020-03-29 09:44:09 +03001159 ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, pdu);
1160 //ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
aa7133@att.com9dff4f02020-03-29 09:55:22 +03001161 //pdu = nullptr;
ss412g1a79bdf2019-10-24 12:03:05 +03001162 }
ss412g1a79bdf2019-10-24 12:03:05 +03001163 }
ss412g1a79bdf2019-10-24 12:03:05 +03001164
1165 if (done) {
1166 if (loglevel >= MDCLOG_INFO) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001167 mdclog_write(MDCLOG_INFO, "Closed connection - descriptor = %d", message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001168 }
1169 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
1170 snprintf((char *)rmrMessageBuffer.sendMessage->payload,
aa7133@att.com96c9b882020-03-25 14:50:40 +02001171 256,
1172 "%s|CU disconnected unexpectedly",
1173 message.peerInfo->enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001174 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1175
1176 if (sendRequestToXapp(message,
1177 RIC_SCTP_CONNECTION_FAILURE,
aa7133@att.combe92d712020-03-18 17:30:39 +02001178 rmrMessageBuffer) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03001179 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
1180 }
1181
1182 /* Closing descriptor make epoll remove it from the set of descriptors which are monitored. */
aa7133@att.com84bd3342020-03-16 18:04:57 +02001183 close(message.peerInfo->fileDescriptor);
aa7133@att.combe92d712020-03-18 17:30:39 +02001184 cleanHashEntry((ConnectedCU_t *) events->data.ptr, sctpMap);
ss412g1a79bdf2019-10-24 12:03:05 +03001185 }
1186 if (loglevel >= MDCLOG_DEBUG) {
1187 clock_gettime(CLOCK_MONOTONIC, &end);
1188 mdclog_write(MDCLOG_DEBUG, "from receive SCTP to send RMR time is %ld seconds and %ld nanoseconds",
1189 end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec);
1190
1191 }
ss412g1a79bdf2019-10-24 12:03:05 +03001192 return 0;
1193}
1194
aa7133@att.com84bd3342020-03-16 18:04:57 +02001195static void buildAndsendSetupRequest(ReportingMessages_t &message,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001196 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.com233facd2020-04-02 00:10:46 +03001197 E2AP_PDU_t *pdu,
aa7133@att.comc5a15202020-04-05 19:57:01 +03001198 vector<string> &repValues) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001199 auto logLevel = mdclog_level_get();
aa7133@att.com84bd3342020-03-16 18:04:57 +02001200 // now we can send the data to e2Mgr
aa7133@att.com96c9b882020-03-25 14:50:40 +02001201 auto buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
aa7133@att.com96c9b882020-03-25 14:50:40 +02001202 unsigned char buffer[RECEIVE_SCTP_BUFFER_SIZE * 2];
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001203 auto *rmrMsg = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, buffer_size);
aa7133@att.com84bd3342020-03-16 18:04:57 +02001204 // encode to xml
aa7133@att.com1fecb052020-03-29 09:44:09 +03001205 auto er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu, buffer, buffer_size);
aa7133@att.com84bd3342020-03-16 18:04:57 +02001206 if (er.encoded == -1) {
1207 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1208 } else if (er.encoded > (ssize_t) buffer_size) {
aa7133@att.coma0f0db62020-04-01 16:52:50 +03001209 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001210 (int) buffer_size,
aa7133@att.coma0f0db62020-04-01 16:52:50 +03001211 asn_DEF_E2AP_PDU.name, __func__, __LINE__);
aa7133@att.com84bd3342020-03-16 18:04:57 +02001212 } else {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001213 string messageType("E2setupRequest");
1214 string ieName("E2setupRequestIEs");
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001215 buffer[er.encoded] = '\0';
1216 buildXmlData(messageType, ieName, repValues, buffer, (size_t)er.encoded);
1217
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001218// string xmlStr = (char *)buffer;
1219// auto removeSpaces = [] (string str) -> string {
1220// str.erase(remove(str.begin(), str.end(), ' '), str.end());
1221// str.erase(remove(str.begin(), str.end(), '\t'), str.end());
1222// return str;
1223// };
1224//
1225// xmlStr = removeSpaces(xmlStr);
1226// // we have the XML
1227// rmrMsg->len = snprintf((char *)rmrMsg->payload, RECEIVE_SCTP_BUFFER_SIZE * 2, "%s:%d|%s",
1228// message.peerInfo->sctpParams->myIP.c_str(),
1229// message.peerInfo->sctpParams->rmrPort,
1230// xmlStr.c_str());
aa7133@att.com96c9b882020-03-25 14:50:40 +02001231 rmrMsg->len = snprintf((char *)rmrMsg->payload, RECEIVE_SCTP_BUFFER_SIZE * 2, "%s:%d|%s",
1232 message.peerInfo->sctpParams->myIP.c_str(),
1233 message.peerInfo->sctpParams->rmrPort,
1234 buffer);
1235 if (logLevel >= MDCLOG_DEBUG) {
1236 mdclog_write(MDCLOG_DEBUG, "Setup request of size %d :\n %s\n", rmrMsg->len, rmrMsg->payload);
aa7133@att.com84bd3342020-03-16 18:04:57 +02001237 }
aa7133@att.com11b17d42020-03-22 11:35:16 +02001238 // send to RMR
aa7133@att.combe92d712020-03-18 17:30:39 +02001239 message.message.messageType = rmrMsg->mtype = RIC_E2_SETUP_REQ;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001240 rmrMsg->state = 0;
1241 rmr_bytes2meid(rmrMsg, (unsigned char *) message.message.enodbName, strlen(message.message.enodbName));
1242
1243 static unsigned char tx[32];
1244 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1245 rmr_bytes2xact(rmrMsg, tx, strlen((const char *) tx));
1246
1247 rmrMsg = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMsg);
1248 if (rmrMsg == nullptr) {
1249 mdclog_write(MDCLOG_ERR, "RMR failed to send returned nullptr");
1250 } else if (rmrMsg->state != 0) {
1251 char meid[RMR_MAX_MEID]{};
1252 if (rmrMsg->state == RMR_ERR_RETRY) {
1253 usleep(5);
1254 rmrMsg->state = 0;
1255 mdclog_write(MDCLOG_INFO, "RETRY sending Message %d to Xapp from %s",
1256 rmrMsg->mtype, rmr_get_meid(rmrMsg, (unsigned char *) meid));
1257 rmrMsg = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMsg);
1258 if (rmrMsg == nullptr) {
1259 mdclog_write(MDCLOG_ERR, "RMR failed send returned nullptr");
1260 } else if (rmrMsg->state != 0) {
1261 mdclog_write(MDCLOG_ERR,
1262 "RMR Retry failed %s sending request %d to Xapp from %s",
1263 translateRmrErrorMessages(rmrMsg->state).c_str(),
1264 rmrMsg->mtype,
1265 rmr_get_meid(rmrMsg, (unsigned char *) meid));
1266 }
1267 } else {
1268 mdclog_write(MDCLOG_ERR, "RMR failed: %s. sending request %d to Xapp from %s",
1269 translateRmrErrorMessages(rmrMsg->state).c_str(),
1270 rmrMsg->mtype,
1271 rmr_get_meid(rmrMsg, (unsigned char *) meid));
1272 }
1273 }
1274 message.peerInfo->gotSetup = true;
1275 buildJsonMessage(message);
aa7133@att.combc110772020-03-25 09:45:14 +02001276 if (rmrMsg != nullptr) {
1277 rmr_free_msg(rmrMsg);
1278 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001279 }
1280
1281}
aa7133@att.comc5a15202020-04-05 19:57:01 +03001282
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001283int RAN_Function_list_To_Vector(RANfunctions_List_t& list, vector <string> &runFunXML_v) {
1284 auto index = 0;
1285 runFunXML_v.clear();
1286 for (auto j = 0; j < list.list.count; j++) {
1287 auto *raNfunctionItemIEs = (RANfunction_ItemIEs_t *)list.list.array[j];
1288 if (raNfunctionItemIEs->id == ProtocolIE_ID_id_RANfunction_Item &&
1289 (raNfunctionItemIEs->value.present == RANfunction_ItemIEs__value_PR_RANfunction_Item)) {
1290 // encode to xml
1291 E2SM_gNB_NRT_RANfunction_Definition_t *ranFunDef = nullptr;
1292 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER,
1293 &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition,
1294 (void **)&ranFunDef,
1295 raNfunctionItemIEs->value.choice.RANfunction_Item.ranFunctionDefinition.buf,
1296 raNfunctionItemIEs->value.choice.RANfunction_Item.ranFunctionDefinition.size);
1297 if (rval.code != RC_OK) {
1298 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2SM message from : %s",
1299 rval.code,
1300 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name);
1301 return -1;
1302 }
1303
1304// if (mdclog_level_get() >= MDCLOG_DEBUG) {
1305// char *printBuffer;
1306// size_t size;
1307// FILE *stream = open_memstream(&printBuffer, &size);
1308// asn_fprint(stream, &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition, ranFunDef);
1309// mdclog_write(MDCLOG_DEBUG, "Encoding E2SM %s PDU past : %s",
1310// asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name,
1311// printBuffer);
1312// }
1313 auto xml_buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
1314 unsigned char xml_buffer[RECEIVE_SCTP_BUFFER_SIZE * 2];
1315 // encode to xml
1316 auto er = asn_encode_to_buffer(nullptr,
1317 ATS_BASIC_XER,
1318 &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition,
1319 ranFunDef,
1320 xml_buffer,
1321 xml_buffer_size);
1322 if (er.encoded == -1) {
1323 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s",
1324 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name,
1325 strerror(errno));
1326 } else if (er.encoded > (ssize_t)xml_buffer_size) {
1327 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
1328 (int) xml_buffer_size,
1329 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name, __func__, __LINE__);
1330 } else {
1331 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1332 mdclog_write(MDCLOG_DEBUG, "Encoding E2SM %s PDU number %d : %s",
1333 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name,
1334 index++,
1335 xml_buffer);
1336 }
1337 string runFuncs = (char *)(xml_buffer);
1338 runFunXML_v.emplace_back(runFuncs);
1339 }
1340 }
1341 }
1342 return 0;
1343}
aa7133@att.comc5a15202020-04-05 19:57:01 +03001344
1345
1346
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001347int collectSetupAndServiceUpdate_RequestData(E2AP_PDU_t *pdu,
1348 Sctp_Map_t *sctpMap,
1349 ReportingMessages_t &message,
1350 vector <string> &RANfunctionsAdded_v,
1351 vector <string> &RANfunctionsModified_v) {
aa7133@att.comc5a15202020-04-05 19:57:01 +03001352 memset(message.peerInfo->enodbName, 0 , MAX_ENODB_NAME_SIZE);
1353 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.count; i++) {
1354 auto *ie = pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.array[i];
1355 if (ie->id == ProtocolIE_ID_id_GlobalE2node_ID) {
1356 // get the ran name for meid
1357 if (ie->value.present == E2setupRequestIEs__value_PR_GlobalE2node_ID) {
1358 if (buildRanName(message.peerInfo->enodbName, ie) < 0) {
1359 mdclog_write(MDCLOG_ERR, "Bad param in E2setupRequestIEs GlobalE2node_ID.\n");
1360 // no mesage will be sent
1361 return -1;
1362 }
1363 memcpy(message.message.enodbName, message.peerInfo->enodbName, strlen(message.peerInfo->enodbName));
1364 sctpMap->setkey(message.message.enodbName, message.peerInfo);
1365 }
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001366 } else if (ie->id == ProtocolIE_ID_id_RANfunctionsAdded) {
aa7133@att.comc5a15202020-04-05 19:57:01 +03001367 if (ie->value.present == E2setupRequestIEs__value_PR_RANfunctions_List) {
1368 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001369 mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1370 ie->value.choice.RANfunctions_List.list.count);
aa7133@att.comc5a15202020-04-05 19:57:01 +03001371 }
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001372 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsAdded_v) != 0 ) {
1373 return -1;
1374 }
1375 }
1376 } else if (ie->id == ProtocolIE_ID_id_RANfunctionsModified) {
1377 if (ie->value.present == E2setupRequestIEs__value_PR_RANfunctions_List) {
1378 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1379 mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1380 ie->value.choice.RANfunctions_List.list.count);
1381 }
1382 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsModified_v) != 0 ) {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001383 return -1;
aa7133@att.comc5a15202020-04-05 19:57:01 +03001384 }
1385 }
1386 }
1387 }
1388 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1389 mdclog_write(MDCLOG_DEBUG, "Run function vector have %ld entries",
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001390 RANfunctionsAdded_v.size());
aa7133@att.comc5a15202020-04-05 19:57:01 +03001391 }
1392 return 0;
1393}
ss412g1a79bdf2019-10-24 12:03:05 +03001394/**
1395 *
1396 * @param pdu
1397 * @param message
1398 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001399 */
1400void asnInitiatingRequest(E2AP_PDU_t *pdu,
aa7133@att.com7b437f72020-03-30 13:25:54 +03001401 Sctp_Map_t *sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +03001402 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +02001403 RmrMessagesBuffer_t &rmrMessageBuffer) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001404 auto logLevel = mdclog_level_get();
ss412g1a79bdf2019-10-24 12:03:05 +03001405 auto procedureCode = ((InitiatingMessage_t *) pdu->choice.initiatingMessage)->procedureCode;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001406 if (logLevel >= MDCLOG_DEBUG) {
1407 mdclog_write(MDCLOG_DEBUG, "Initiating message %ld\n", procedureCode);
ss412g1a79bdf2019-10-24 12:03:05 +03001408 }
1409 switch (procedureCode) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001410 case ProcedureCode_id_E2setup: {
1411 if (logLevel >= MDCLOG_DEBUG) {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001412 mdclog_write(MDCLOG_DEBUG, "Got E2setup");
aa7133@att.com84bd3342020-03-16 18:04:57 +02001413 }
1414
aa7133@att.com7b437f72020-03-30 13:25:54 +03001415 // first get the message as XML buffer
1416 auto setup_xml_buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
1417 unsigned char setup_xml_buffer[RECEIVE_SCTP_BUFFER_SIZE * 2];
aa7133@att.com7b437f72020-03-30 13:25:54 +03001418
1419 auto er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu, setup_xml_buffer, setup_xml_buffer_size);
1420 if (er.encoded == -1) {
1421 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001422 break;
aa7133@att.com7b437f72020-03-30 13:25:54 +03001423 } else if (er.encoded > (ssize_t) setup_xml_buffer_size) {
aa7133@att.coma0f0db62020-04-01 16:52:50 +03001424 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
aa7133@att.com7b437f72020-03-30 13:25:54 +03001425 (int)setup_xml_buffer_size,
aa7133@att.coma0f0db62020-04-01 16:52:50 +03001426 asn_DEF_E2AP_PDU.name, __func__, __LINE__);
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001427 break;
aa7133@att.com7b437f72020-03-30 13:25:54 +03001428 }
1429 std::string xmlString(setup_xml_buffer_size, setup_xml_buffer_size + er.encoded);
1430
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001431 vector <string> RANfunctionsAdded_v;
1432 vector <string> RANfunctionsModified_v;
1433 RANfunctionsAdded_v.clear();
1434 RANfunctionsModified_v.clear();
1435 if (collectSetupAndServiceUpdate_RequestData(pdu, sctpMap, message,
1436 RANfunctionsAdded_v, RANfunctionsModified_v) != 0) {
aa7133@att.com7b437f72020-03-30 13:25:54 +03001437 break;
1438 }
1439
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001440 buildAndsendSetupRequest(message, rmrMessageBuffer, pdu, RANfunctionsAdded_v);
ss412g1a79bdf2019-10-24 12:03:05 +03001441 break;
1442 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001443 case ProcedureCode_id_RICserviceUpdate: {
1444 if (logLevel >= MDCLOG_DEBUG) {
1445 mdclog_write(MDCLOG_DEBUG, "Got RICserviceUpdate %s", message.message.enodbName);
1446 }
1447 if (sendRequestToXapp(message, RIC_SERVICE_UPDATE, rmrMessageBuffer) != 0) {
1448 mdclog_write(MDCLOG_ERR, "RIC_SERVICE_UPDATE message failed to send to xAPP");
1449 }
1450 break;
1451 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001452 case ProcedureCode_id_ErrorIndication: {
1453 if (logLevel >= MDCLOG_DEBUG) {
1454 mdclog_write(MDCLOG_DEBUG, "Got ErrorIndication %s", message.message.enodbName);
1455 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001456 if (sendRequestToXapp(message, RIC_ERROR_INDICATION, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001457 mdclog_write(MDCLOG_ERR, "RIC_ERROR_INDICATION failed to send to xAPP");
ss412g1a79bdf2019-10-24 12:03:05 +03001458 }
1459 break;
1460 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001461 case ProcedureCode_id_Reset: {
1462 if (logLevel >= MDCLOG_DEBUG) {
1463 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001464 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001465 if (sendRequestToXapp(message, RIC_X2_RESET, rmrMessageBuffer) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03001466 mdclog_write(MDCLOG_ERR, "RIC_X2_RESET message failed to send to xAPP");
1467 }
1468 break;
1469 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001470 case ProcedureCode_id_RICcontrol: {
1471 if (logLevel >= MDCLOG_DEBUG) {
1472 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
1473 }
1474 break;
1475 }
1476 case ProcedureCode_id_RICindication: {
1477 if (logLevel >= MDCLOG_DEBUG) {
1478 mdclog_write(MDCLOG_DEBUG, "Got RICindication %s", message.message.enodbName);
1479 }
1480 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.count; i++) {
ss412g1a79bdf2019-10-24 12:03:05 +03001481 auto messageSent = false;
1482 RICindication_IEs_t *ie = pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.array[i];
aa7133@att.com84bd3342020-03-16 18:04:57 +02001483 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03001484 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1485 }
1486 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001487 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03001488 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1489 }
1490 if (ie->value.present == RICindication_IEs__value_PR_RICrequestID) {
1491 static unsigned char tx[32];
1492 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_INDICATION;
1493 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1494 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
ss412g3bac2da2020-01-05 11:52:19 +02001495 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001496 (unsigned char *)message.message.enodbName,
1497 strlen(message.message.enodbName));
ss412g1a79bdf2019-10-24 12:03:05 +03001498 rmrMessageBuffer.sendMessage->state = 0;
aa7133@att.comc5a15202020-04-05 19:57:01 +03001499 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
1500
1501 //ie->value.choice.RICrequestID.ricInstanceID;
ss412g3bac2da2020-01-05 11:52:19 +02001502 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.comc5a15202020-04-05 19:57:01 +03001503 mdclog_write(MDCLOG_DEBUG, "sub id = %d, mtype = %d, ric instance id %ld, requestor id = %ld",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001504 rmrMessageBuffer.sendMessage->sub_id,
aa7133@att.comc5a15202020-04-05 19:57:01 +03001505 rmrMessageBuffer.sendMessage->mtype,
1506 ie->value.choice.RICrequestID.ricInstanceID,
1507 ie->value.choice.RICrequestID.ricRequestorID);
ss412g3bac2da2020-01-05 11:52:19 +02001508 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001509 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03001510 messageSent = true;
1511 } else {
1512 mdclog_write(MDCLOG_ERR, "RIC request id missing illigal request");
1513 }
1514 }
1515 if (messageSent) {
1516 break;
1517 }
1518 }
1519 break;
1520 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001521 case ProcedureCode_id_RICserviceQuery: {
1522 if (logLevel >= MDCLOG_DEBUG) {
1523 mdclog_write(MDCLOG_DEBUG, "Got RICserviceQuery %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001524 }
1525 break;
1526 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001527 case ProcedureCode_id_RICsubscription: {
1528 if (logLevel >= MDCLOG_DEBUG) {
1529 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
1530 }
1531 break;
1532 }
1533 case ProcedureCode_id_RICsubscriptionDelete: {
1534 if (logLevel >= MDCLOG_DEBUG) {
1535 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001536 }
1537 break;
1538 }
1539 default: {
1540 mdclog_write(MDCLOG_ERR, "Undefined or not supported message = %ld", procedureCode);
1541 message.message.messageType = 0; // no RMR message type yet
1542
1543 buildJsonMessage(message);
1544
1545 break;
1546 }
1547 }
ss412g1a79bdf2019-10-24 12:03:05 +03001548}
1549
1550/**
1551 *
1552 * @param pdu
1553 * @param message
ss412g1a79bdf2019-10-24 12:03:05 +03001554 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001555 */
aa7133@att.com7b437f72020-03-30 13:25:54 +03001556void asnSuccsesfulMsg(E2AP_PDU_t *pdu,
1557 Sctp_Map_t *sctpMap,
1558 ReportingMessages_t &message,
1559 RmrMessagesBuffer_t &rmrMessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03001560 auto procedureCode = pdu->choice.successfulOutcome->procedureCode;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001561 auto logLevel = mdclog_level_get();
1562 if (logLevel >= MDCLOG_INFO) {
ss412g1a79bdf2019-10-24 12:03:05 +03001563 mdclog_write(MDCLOG_INFO, "Successful Outcome %ld", procedureCode);
1564 }
1565 switch (procedureCode) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001566 case ProcedureCode_id_E2setup: {
1567 if (logLevel >= MDCLOG_DEBUG) {
1568 mdclog_write(MDCLOG_DEBUG, "Got E2setup\n");
ss412g1a79bdf2019-10-24 12:03:05 +03001569 }
1570 break;
1571 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001572 case ProcedureCode_id_ErrorIndication: {
1573 if (logLevel >= MDCLOG_DEBUG) {
1574 mdclog_write(MDCLOG_DEBUG, "Got ErrorIndication %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001575 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001576 if (sendRequestToXapp(message, RIC_ERROR_INDICATION, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001577 mdclog_write(MDCLOG_ERR, "RIC_ERROR_INDICATION failed to send to xAPP");
ss412g1a79bdf2019-10-24 12:03:05 +03001578 }
1579 break;
1580 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001581 case ProcedureCode_id_Reset: {
1582 if (logLevel >= MDCLOG_DEBUG) {
1583 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001584 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001585 if (sendRequestToXapp(message, RIC_X2_RESET, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001586 mdclog_write(MDCLOG_ERR, "RIC_X2_RESET message failed to send to xAPP");
ss412g1a79bdf2019-10-24 12:03:05 +03001587 }
1588 break;
1589 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001590 case ProcedureCode_id_RICcontrol: {
1591 if (logLevel >= MDCLOG_DEBUG) {
1592 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001593 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001594 for (auto i = 0;
ss412g1a79bdf2019-10-24 12:03:05 +03001595 i < pdu->choice.successfulOutcome->value.choice.RICcontrolAcknowledge.protocolIEs.list.count; i++) {
1596 auto messageSent = false;
1597 RICcontrolAcknowledge_IEs_t *ie = pdu->choice.successfulOutcome->value.choice.RICcontrolAcknowledge.protocolIEs.list.array[i];
1598 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1599 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1600 }
1601 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
1602 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1603 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1604 }
1605 if (ie->value.present == RICcontrolAcknowledge_IEs__value_PR_RICrequestID) {
1606 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_CONTROL_ACK;
1607 rmrMessageBuffer.sendMessage->state = 0;
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001608// rmrMessageBuffer.sendMessage->sub_id = (int) ie->value.choice.RICrequestID.ricRequestorID;
1609 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
1610
ss412g1a79bdf2019-10-24 12:03:05 +03001611 static unsigned char tx[32];
1612 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1613 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
ss412g3bac2da2020-01-05 11:52:19 +02001614 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001615 (unsigned char *)message.message.enodbName,
1616 strlen(message.message.enodbName));
ss412g1a79bdf2019-10-24 12:03:05 +03001617
aa7133@att.combe92d712020-03-18 17:30:39 +02001618 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03001619 messageSent = true;
1620 } else {
1621 mdclog_write(MDCLOG_ERR, "RIC request id missing illigal request");
1622 }
1623 }
1624 if (messageSent) {
1625 break;
1626 }
1627 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001628
1629 break;
1630 }
1631 case ProcedureCode_id_RICindication: {
1632 if (logLevel >= MDCLOG_DEBUG) {
1633 mdclog_write(MDCLOG_DEBUG, "Got RICindication %s", message.message.enodbName);
1634 }
1635 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.count; i++) {
1636 auto messageSent = false;
1637 RICindication_IEs_t *ie = pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.array[i];
1638 if (logLevel >= MDCLOG_DEBUG) {
1639 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1640 }
1641 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
1642 if (logLevel >= MDCLOG_DEBUG) {
1643 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1644 }
1645 if (ie->value.present == RICindication_IEs__value_PR_RICrequestID) {
1646 static unsigned char tx[32];
1647 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_INDICATION;
1648 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1649 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
1650 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
1651 (unsigned char *)message.message.enodbName,
1652 strlen(message.message.enodbName));
1653 rmrMessageBuffer.sendMessage->state = 0;
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001654 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001655 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1656 mdclog_write(MDCLOG_DEBUG, "RIC sub id = %d, message type = %d",
1657 rmrMessageBuffer.sendMessage->sub_id,
1658 rmrMessageBuffer.sendMessage->mtype);
1659 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001660 sendRmrMessage(rmrMessageBuffer, message);
aa7133@att.com84bd3342020-03-16 18:04:57 +02001661 messageSent = true;
1662 } else {
1663 mdclog_write(MDCLOG_ERR, "RIC request id missing illigal request");
1664 }
1665 }
1666 if (messageSent) {
1667 break;
1668 }
1669 }
1670 break;
1671 }
1672 case ProcedureCode_id_RICserviceQuery: {
1673 if (logLevel >= MDCLOG_DEBUG) {
1674 mdclog_write(MDCLOG_DEBUG, "Got RICserviceQuery %s", message.message.enodbName);
1675 }
1676 break;
1677 }
1678 case ProcedureCode_id_RICserviceUpdate: {
1679 if (logLevel >= MDCLOG_DEBUG) {
1680 mdclog_write(MDCLOG_DEBUG, "Got RICserviceUpdate %s", message.message.enodbName);
1681 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001682 if (sendRequestToXapp(message, RIC_SERVICE_UPDATE, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001683 mdclog_write(MDCLOG_ERR, "RIC_SERVICE_UPDATE message failed to send to xAPP");
1684 }
1685 break;
1686 }
1687 case ProcedureCode_id_RICsubscription: {
1688 if (logLevel >= MDCLOG_DEBUG) {
1689 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
1690 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001691 if (sendRequestToXapp(message, RIC_SUB_RESP, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001692 mdclog_write(MDCLOG_ERR, "Subscription successful message failed to send to xAPP");
1693 }
1694 break;
1695 }
1696 case ProcedureCode_id_RICsubscriptionDelete: {
1697 if (logLevel >= MDCLOG_DEBUG) {
1698 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
1699 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001700 if (sendRequestToXapp(message, RIC_SUB_DEL_RESP, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001701 mdclog_write(MDCLOG_ERR, "Subscription delete successful message failed to send to xAPP");
1702 }
ss412g1a79bdf2019-10-24 12:03:05 +03001703 break;
1704 }
1705 default: {
1706 mdclog_write(MDCLOG_WARN, "Undefined or not supported message = %ld", procedureCode);
1707 message.message.messageType = 0; // no RMR message type yet
1708 buildJsonMessage(message);
1709
1710 break;
1711 }
1712 }
ss412g1a79bdf2019-10-24 12:03:05 +03001713}
1714
1715/**
1716 *
1717 * @param pdu
1718 * @param message
ss412g1a79bdf2019-10-24 12:03:05 +03001719 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001720 */
1721void asnUnSuccsesfulMsg(E2AP_PDU_t *pdu,
aa7133@att.com7b437f72020-03-30 13:25:54 +03001722 Sctp_Map_t *sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +03001723 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +02001724 RmrMessagesBuffer_t &rmrMessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03001725 auto procedureCode = pdu->choice.unsuccessfulOutcome->procedureCode;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001726 auto logLevel = mdclog_level_get();
1727 if (logLevel >= MDCLOG_INFO) {
ss412g1a79bdf2019-10-24 12:03:05 +03001728 mdclog_write(MDCLOG_INFO, "Unsuccessful Outcome %ld", procedureCode);
1729 }
1730 switch (procedureCode) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001731 case ProcedureCode_id_E2setup: {
1732 if (logLevel >= MDCLOG_DEBUG) {
1733 mdclog_write(MDCLOG_DEBUG, "Got E2setup\n");
ss412g1a79bdf2019-10-24 12:03:05 +03001734 }
1735 break;
1736 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001737 case ProcedureCode_id_ErrorIndication: {
1738 if (logLevel >= MDCLOG_DEBUG) {
1739 mdclog_write(MDCLOG_DEBUG, "Got ErrorIndication %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001740 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001741 if (sendRequestToXapp(message, RIC_ERROR_INDICATION, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001742 mdclog_write(MDCLOG_ERR, "RIC_ERROR_INDICATION failed to send to xAPP");
ss412g1a79bdf2019-10-24 12:03:05 +03001743 }
1744 break;
1745 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001746 case ProcedureCode_id_Reset: {
1747 if (logLevel >= MDCLOG_DEBUG) {
1748 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
1749 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001750 if (sendRequestToXapp(message, RIC_X2_RESET, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001751 mdclog_write(MDCLOG_ERR, "RIC_X2_RESET message failed to send to xAPP");
ss412g1a79bdf2019-10-24 12:03:05 +03001752 }
1753 break;
1754 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001755 case ProcedureCode_id_RICcontrol: {
1756 if (logLevel >= MDCLOG_DEBUG) {
1757 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001758 }
1759 for (int i = 0;
1760 i < pdu->choice.unsuccessfulOutcome->value.choice.RICcontrolFailure.protocolIEs.list.count; i++) {
1761 auto messageSent = false;
1762 RICcontrolFailure_IEs_t *ie = pdu->choice.unsuccessfulOutcome->value.choice.RICcontrolFailure.protocolIEs.list.array[i];
aa7133@att.com84bd3342020-03-16 18:04:57 +02001763 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03001764 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1765 }
1766 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001767 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03001768 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1769 }
1770 if (ie->value.present == RICcontrolFailure_IEs__value_PR_RICrequestID) {
1771 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_CONTROL_FAILURE;
1772 rmrMessageBuffer.sendMessage->state = 0;
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001773// rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricRequestorID;
1774 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
ss412g1a79bdf2019-10-24 12:03:05 +03001775 static unsigned char tx[32];
1776 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1777 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
aa7133@att.com84bd3342020-03-16 18:04:57 +02001778 rmr_bytes2meid(rmrMessageBuffer.sendMessage, (unsigned char *) message.message.enodbName,
1779 strlen(message.message.enodbName));
aa7133@att.combe92d712020-03-18 17:30:39 +02001780 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03001781 messageSent = true;
1782 } else {
1783 mdclog_write(MDCLOG_ERR, "RIC request id missing illigal request");
1784 }
1785 }
1786 if (messageSent) {
1787 break;
1788 }
1789 }
1790 break;
1791 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001792 case ProcedureCode_id_RICindication: {
1793 if (logLevel >= MDCLOG_DEBUG) {
1794 mdclog_write(MDCLOG_DEBUG, "Got RICindication %s", message.message.enodbName);
1795 }
1796 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.count; i++) {
1797 auto messageSent = false;
1798 RICindication_IEs_t *ie = pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.array[i];
1799 if (logLevel >= MDCLOG_DEBUG) {
1800 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1801 }
1802 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
1803 if (logLevel >= MDCLOG_DEBUG) {
1804 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1805 }
1806 if (ie->value.present == RICindication_IEs__value_PR_RICrequestID) {
1807 static unsigned char tx[32];
1808 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_INDICATION;
1809 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1810 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
1811 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
1812 (unsigned char *)message.message.enodbName,
1813 strlen(message.message.enodbName));
1814 rmrMessageBuffer.sendMessage->state = 0;
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001815// rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricRequestorID;
1816 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001817 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1818 mdclog_write(MDCLOG_DEBUG, "RIC sub id = %d, message type = %d",
1819 rmrMessageBuffer.sendMessage->sub_id,
1820 rmrMessageBuffer.sendMessage->mtype);
1821 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001822 sendRmrMessage(rmrMessageBuffer, message);
aa7133@att.com84bd3342020-03-16 18:04:57 +02001823 messageSent = true;
1824 } else {
1825 mdclog_write(MDCLOG_ERR, "RIC request id missing illigal request");
1826 }
1827 }
1828 if (messageSent) {
1829 break;
1830 }
1831 }
1832 break;
1833 }
1834 case ProcedureCode_id_RICserviceQuery: {
1835 if (logLevel >= MDCLOG_DEBUG) {
1836 mdclog_write(MDCLOG_DEBUG, "Got RICserviceQuery %s", message.message.enodbName);
1837 }
1838 break;
1839 }
1840 case ProcedureCode_id_RICserviceUpdate: {
1841 if (logLevel >= MDCLOG_DEBUG) {
1842 mdclog_write(MDCLOG_DEBUG, "Got RICserviceUpdate %s", message.message.enodbName);
1843 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001844 if (sendRequestToXapp(message, RIC_SERVICE_UPDATE, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001845 mdclog_write(MDCLOG_ERR, "RIC_SERVICE_UPDATE message failed to send to xAPP");
1846 }
1847 break;
1848 }
1849 case ProcedureCode_id_RICsubscription: {
1850 if (logLevel >= MDCLOG_DEBUG) {
1851 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
1852 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001853 if (sendRequestToXapp(message, RIC_SUB_FAILURE, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001854 mdclog_write(MDCLOG_ERR, "Subscription unsuccessful message failed to send to xAPP");
1855 }
1856 break;
1857 }
1858 case ProcedureCode_id_RICsubscriptionDelete: {
1859 if (logLevel >= MDCLOG_DEBUG) {
1860 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
1861 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001862 if (sendRequestToXapp(message, RIC_SUB_DEL_FAILURE, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001863 mdclog_write(MDCLOG_ERR, "Subscription Delete unsuccessful message failed to send to xAPP");
1864 }
1865 break;
1866 }
ss412g1a79bdf2019-10-24 12:03:05 +03001867 default: {
1868 mdclog_write(MDCLOG_WARN, "Undefined or not supported message = %ld", procedureCode);
1869 message.message.messageType = 0; // no RMR message type yet
1870
1871 buildJsonMessage(message);
1872
1873 break;
1874 }
1875 }
ss412g1a79bdf2019-10-24 12:03:05 +03001876}
1877
1878/**
1879 *
1880 * @param message
1881 * @param requestId
1882 * @param rmrMmessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001883 * @return
1884 */
1885int sendRequestToXapp(ReportingMessages_t &message,
1886 int requestId,
aa7133@att.combe92d712020-03-18 17:30:39 +02001887 RmrMessagesBuffer_t &rmrMmessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03001888 rmr_bytes2meid(rmrMmessageBuffer.sendMessage,
1889 (unsigned char *)message.message.enodbName,
1890 strlen(message.message.enodbName));
1891 message.message.messageType = rmrMmessageBuffer.sendMessage->mtype = requestId;
1892 rmrMmessageBuffer.sendMessage->state = 0;
1893 static unsigned char tx[32];
1894 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1895 rmr_bytes2xact(rmrMmessageBuffer.sendMessage, tx, strlen((const char *) tx));
1896
aa7133@att.combe92d712020-03-18 17:30:39 +02001897 auto rc = sendRmrMessage(rmrMmessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03001898 return rc;
1899}
1900
1901
aa7133@att.combe92d712020-03-18 17:30:39 +02001902void getRmrContext(sctp_params_t &pSctpParams) {
ss412g3bac2da2020-01-05 11:52:19 +02001903 pSctpParams.rmrCtx = nullptr;
aa7133@att.comab0e0f42020-04-06 17:13:58 +03001904 pSctpParams.rmrCtx = rmr_init(pSctpParams.rmrAddress, RECEIVE_XAPP_BUFFER_SIZE, RMRFL_NONE);
ss412g3bac2da2020-01-05 11:52:19 +02001905 if (pSctpParams.rmrCtx == nullptr) {
1906 mdclog_write(MDCLOG_ERR, "Failed to initialize RMR");
ss412g3bac2da2020-01-05 11:52:19 +02001907 return;
ss412g1a79bdf2019-10-24 12:03:05 +03001908 }
1909
ss412g3bac2da2020-01-05 11:52:19 +02001910 rmr_set_stimeout(pSctpParams.rmrCtx, 0); // disable retries for any send operation
ss412g1a79bdf2019-10-24 12:03:05 +03001911 // we need to find that routing table exist and we can run
1912 if (mdclog_level_get() >= MDCLOG_INFO) {
1913 mdclog_write(MDCLOG_INFO, "We are after RMR INIT wait for RMR_Ready");
1914 }
1915 int rmrReady = 0;
1916 int count = 0;
1917 while (!rmrReady) {
ss412g3bac2da2020-01-05 11:52:19 +02001918 if ((rmrReady = rmr_ready(pSctpParams.rmrCtx)) == 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03001919 sleep(1);
1920 }
1921 count++;
1922 if (count % 60 == 0) {
1923 mdclog_write(MDCLOG_INFO, "waiting to RMR ready state for %d seconds", count);
1924 }
1925 }
1926 if (mdclog_level_get() >= MDCLOG_INFO) {
1927 mdclog_write(MDCLOG_INFO, "RMR running");
1928 }
ss412g3bac2da2020-01-05 11:52:19 +02001929 rmr_init_trace(pSctpParams.rmrCtx, 200);
1930 // get the RMR fd for the epoll
1931 pSctpParams.rmrListenFd = rmr_get_rcvfd(pSctpParams.rmrCtx);
1932 struct epoll_event event{};
1933 // add RMR fd to epoll
1934 event.events = (EPOLLIN);
1935 event.data.fd = pSctpParams.rmrListenFd;
1936 // add listening RMR FD to epoll
1937 if (epoll_ctl(pSctpParams.epoll_fd, EPOLL_CTL_ADD, pSctpParams.rmrListenFd, &event)) {
1938 mdclog_write(MDCLOG_ERR, "Failed to add RMR descriptor to epoll");
1939 close(pSctpParams.rmrListenFd);
1940 rmr_close(pSctpParams.rmrCtx);
1941 pSctpParams.rmrCtx = nullptr;
1942 }
ss412g1a79bdf2019-10-24 12:03:05 +03001943}
1944
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001945int PER_FromXML(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
aa7133@att.com07e256f2020-04-20 15:59:51 +03001946 E2AP_PDU_t *pdu = nullptr;
aa7133@att.com1c4f50f2020-03-30 14:30:52 +03001947
1948 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com07e256f2020-04-20 15:59:51 +03001949 mdclog_write(MDCLOG_DEBUG, "got xml setup response of size %d is:%s",
1950 rmrMessageBuffer.rcvMessage->len, rmrMessageBuffer.rcvMessage->payload);
aa7133@att.com1c4f50f2020-03-30 14:30:52 +03001951 }
aa7133@att.com1fecb052020-03-29 09:44:09 +03001952 auto rval = asn_decode(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, (void **) &pdu,
1953 rmrMessageBuffer.rcvMessage->payload, rmrMessageBuffer.rcvMessage->len);
1954 if (rval.code != RC_OK) {
aa7133@att.com1c4f50f2020-03-30 14:30:52 +03001955 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) setup response from E2MGR : %s",
aa7133@att.com1fecb052020-03-29 09:44:09 +03001956 rval.code,
1957 message.message.enodbName);
1958 return -1;
1959 }
1960
aa7133@att.coma0f0db62020-04-01 16:52:50 +03001961 int buff_size = RECEIVE_XAPP_BUFFER_SIZE;
1962 auto er = asn_encode_to_buffer(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, pdu,
1963 rmrMessageBuffer.rcvMessage->payload, buff_size);
aa7133@att.com1fecb052020-03-29 09:44:09 +03001964 if (er.encoded == -1) {
1965 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1966 return -1;
aa7133@att.coma0f0db62020-04-01 16:52:50 +03001967 } else if (er.encoded > (ssize_t)buff_size) {
1968 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 +03001969 (int)rmrMessageBuffer.rcvMessage->len,
aa7133@att.coma0f0db62020-04-01 16:52:50 +03001970 asn_DEF_E2AP_PDU.name,
1971 __func__,
1972 __LINE__);
aa7133@att.com1fecb052020-03-29 09:44:09 +03001973 return -1;
1974 }
aa7133@att.coma0f0db62020-04-01 16:52:50 +03001975 rmrMessageBuffer.rcvMessage->len = er.encoded;
aa7133@att.com1fecb052020-03-29 09:44:09 +03001976 return 0;
1977}
1978
ss412g1a79bdf2019-10-24 12:03:05 +03001979/**
1980 *
ss412g1a79bdf2019-10-24 12:03:05 +03001981 * @param sctpMap
1982 * @param rmrMessageBuffer
1983 * @param ts
ss412g1a79bdf2019-10-24 12:03:05 +03001984 * @return
1985 */
aa7133@att.com6680c3b2020-03-22 12:06:58 +02001986int receiveXappMessages(Sctp_Map_t *sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +03001987 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +02001988 struct timespec &ts) {
ss412g1a79bdf2019-10-24 12:03:05 +03001989 if (rmrMessageBuffer.rcvMessage == nullptr) {
1990 //we have error
1991 mdclog_write(MDCLOG_ERR, "RMR Allocation message, %s", strerror(errno));
ss412g1a79bdf2019-10-24 12:03:05 +03001992 return -1;
1993 }
1994
1995 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1996 mdclog_write(MDCLOG_DEBUG, "Call to rmr_rcv_msg");
1997 }
1998 rmrMessageBuffer.rcvMessage = rmr_rcv_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.rcvMessage);
1999 if (rmrMessageBuffer.rcvMessage == nullptr) {
2000 mdclog_write(MDCLOG_ERR, "RMR Receving message with null pointer, Realloc rmr mesage buffer");
2001 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
ss412g1a79bdf2019-10-24 12:03:05 +03002002 return -2;
2003 }
2004 ReportingMessages_t message;
2005 message.message.direction = 'D';
2006 message.message.time.tv_nsec = ts.tv_nsec;
2007 message.message.time.tv_sec = ts.tv_sec;
2008
2009 // get message payload
2010 //auto msgData = msg->payload;
2011 if (rmrMessageBuffer.rcvMessage->state != 0) {
2012 mdclog_write(MDCLOG_ERR, "RMR Receving message with stat = %d", rmrMessageBuffer.rcvMessage->state);
ss412g1a79bdf2019-10-24 12:03:05 +03002013 return -1;
2014 }
aa7133@att.com1fecb052020-03-29 09:44:09 +03002015 rmr_get_meid(rmrMessageBuffer.rcvMessage, (unsigned char *)message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03002016 switch (rmrMessageBuffer.rcvMessage->mtype) {
aa7133@att.combe92d712020-03-18 17:30:39 +02002017 case RIC_E2_SETUP_RESP : {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002018 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
aa7133@att.com1fecb052020-03-29 09:44:09 +03002019 break;
2020 }
2021
aa7133@att.combe92d712020-03-18 17:30:39 +02002022 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2023 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_SETUP_RESP");
2024 return -6;
ss412g1a79bdf2019-10-24 12:03:05 +03002025 }
2026 break;
2027 }
aa7133@att.combe92d712020-03-18 17:30:39 +02002028 case RIC_E2_SETUP_FAILURE : {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002029 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
aa7133@att.com1fecb052020-03-29 09:44:09 +03002030 break;
2031 }
aa7133@att.combe92d712020-03-18 17:30:39 +02002032 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2033 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_SETUP_FAILURE");
2034 return -6;
ss412g1a79bdf2019-10-24 12:03:05 +03002035 }
2036 break;
2037 }
aa7133@att.combe92d712020-03-18 17:30:39 +02002038 case RIC_ERROR_INDICATION: {
2039 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2040 mdclog_write(MDCLOG_ERR, "Failed to send RIC_ERROR_INDICATION");
ss412g1a79bdf2019-10-24 12:03:05 +03002041 return -6;
2042 }
2043 break;
2044 }
2045 case RIC_SUB_REQ: {
aa7133@att.combe92d712020-03-18 17:30:39 +02002046 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002047 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SUB_REQ");
ss412g1a79bdf2019-10-24 12:03:05 +03002048 return -6;
2049 }
2050 break;
2051 }
2052 case RIC_SUB_DEL_REQ: {
aa7133@att.combe92d712020-03-18 17:30:39 +02002053 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002054 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SUB_DEL_REQ");
ss412g1a79bdf2019-10-24 12:03:05 +03002055 return -6;
2056 }
2057 break;
2058 }
2059 case RIC_CONTROL_REQ: {
aa7133@att.combe92d712020-03-18 17:30:39 +02002060 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002061 mdclog_write(MDCLOG_ERR, "Failed to send RIC_CONTROL_REQ");
ss412g1a79bdf2019-10-24 12:03:05 +03002062 return -6;
2063 }
2064 break;
2065 }
2066 case RIC_SERVICE_QUERY: {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002067 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2068 break;
2069 }
aa7133@att.combe92d712020-03-18 17:30:39 +02002070 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002071 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_QUERY");
ss412g1a79bdf2019-10-24 12:03:05 +03002072 return -6;
2073 }
2074 break;
2075 }
2076 case RIC_SERVICE_UPDATE_ACK: {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002077 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2078 break;
2079 }
aa7133@att.combe92d712020-03-18 17:30:39 +02002080 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002081 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_UPDATE_ACK");
ss412g1a79bdf2019-10-24 12:03:05 +03002082 return -6;
2083 }
2084 break;
2085 }
2086 case RIC_SERVICE_UPDATE_FAILURE: {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002087 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2088 break;
2089 }
aa7133@att.combe92d712020-03-18 17:30:39 +02002090 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002091 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_UPDATE_FAILURE");
ss412g1a79bdf2019-10-24 12:03:05 +03002092 return -6;
2093 }
2094 break;
2095 }
2096 case RIC_X2_RESET: {
aa7133@att.combe92d712020-03-18 17:30:39 +02002097 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002098 mdclog_write(MDCLOG_ERR, "Failed to send RIC_X2_RESET");
ss412g1a79bdf2019-10-24 12:03:05 +03002099 return -6;
2100 }
2101 break;
2102 }
2103 case RIC_X2_RESET_RESP: {
aa7133@att.combe92d712020-03-18 17:30:39 +02002104 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002105 mdclog_write(MDCLOG_ERR, "Failed to send RIC_X2_RESET_RESP");
ss412g1a79bdf2019-10-24 12:03:05 +03002106 return -6;
2107 }
2108 break;
2109 }
2110 case RIC_SCTP_CLEAR_ALL: {
2111 mdclog_write(MDCLOG_INFO, "RIC_SCTP_CLEAR_ALL");
2112 // loop on all keys and close socket and then erase all map.
2113 vector<char *> v;
2114 sctpMap->getKeys(v);
2115 for (auto const &iter : v) { //}; iter != sctpMap.end(); iter++) {
2116 if (!boost::starts_with((string) (iter), "host:") && !boost::starts_with((string) (iter), "msg:")) {
2117 auto *peerInfo = (ConnectedCU_t *) sctpMap->find(iter);
2118 if (peerInfo == nullptr) {
2119 continue;
2120 }
2121 close(peerInfo->fileDescriptor);
2122 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
2123 message.message.direction = 'D';
2124 message.message.time.tv_nsec = ts.tv_nsec;
2125 message.message.time.tv_sec = ts.tv_sec;
2126
2127 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
2128 snprintf((char *)rmrMessageBuffer.sendMessage->payload,
aa7133@att.com96c9b882020-03-25 14:50:40 +02002129 256,
2130 "%s|RIC_SCTP_CLEAR_ALL",
2131 peerInfo->enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03002132 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
2133 mdclog_write(MDCLOG_INFO, "%s", message.message.asndata);
aa7133@att.combe92d712020-03-18 17:30:39 +02002134 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002135 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
2136 }
2137 free(peerInfo);
2138 }
2139 }
2140
2141 sleep(1);
2142 sctpMap->clear();
2143 break;
2144 }
ss412g3bac2da2020-01-05 11:52:19 +02002145 case E2_TERM_KEEP_ALIVE_REQ: {
2146 // send message back
ss412g3bac2da2020-01-05 11:52:19 +02002147 rmr_bytes2payload(rmrMessageBuffer.sendMessage,
aa7133@att.com96c9b882020-03-25 14:50:40 +02002148 (unsigned char *)rmrMessageBuffer.ka_message,
2149 rmrMessageBuffer.ka_message_len);
ss412g3bac2da2020-01-05 11:52:19 +02002150 rmrMessageBuffer.sendMessage->mtype = E2_TERM_KEEP_ALIVE_RESP;
2151 rmrMessageBuffer.sendMessage->state = 0;
2152 static unsigned char tx[32];
2153 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2154 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, txLen);
2155 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2156 if (rmrMessageBuffer.sendMessage == nullptr) {
2157 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
aa7133@att.com77fff432020-01-27 12:01:20 +02002158 mdclog_write(MDCLOG_ERR, "Failed to send E2_TERM_KEEP_ALIVE_RESP RMR message returned NULL");
ss412g3bac2da2020-01-05 11:52:19 +02002159 } else if (rmrMessageBuffer.sendMessage->state != 0) {
aa7133@att.com77fff432020-01-27 12:01:20 +02002160 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 +02002161 rmrMessageBuffer.sendMessage->state, translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str());
aa7133@att.combc110772020-03-25 09:45:14 +02002162 } else if (mdclog_level_get() >= MDCLOG_DEBUG) {
2163 mdclog_write(MDCLOG_DEBUG, "Got Keep Alive Request send : %s", rmrMessageBuffer.ka_message);
ss412g3bac2da2020-01-05 11:52:19 +02002164 }
aa7133@att.com342843e2020-01-26 12:53:34 +02002165
ss412g3bac2da2020-01-05 11:52:19 +02002166 break;
2167 }
ss412g1a79bdf2019-10-24 12:03:05 +03002168 default:
2169 mdclog_write(MDCLOG_WARN, "Message Type : %d is not seported", rmrMessageBuffer.rcvMessage->mtype);
2170 message.message.asndata = rmrMessageBuffer.rcvMessage->payload;
2171 message.message.asnLength = rmrMessageBuffer.rcvMessage->len;
2172 message.message.time.tv_nsec = ts.tv_nsec;
2173 message.message.time.tv_sec = ts.tv_sec;
2174 message.message.messageType = rmrMessageBuffer.rcvMessage->mtype;
2175
2176 buildJsonMessage(message);
2177
2178
ss412g1a79bdf2019-10-24 12:03:05 +03002179 return -7;
2180 }
2181 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2182 mdclog_write(MDCLOG_DEBUG, "EXIT OK from %s", __FUNCTION__);
2183 }
ss412g1a79bdf2019-10-24 12:03:05 +03002184 return 0;
2185}
2186
2187/**
2188 * Send message to the CU that is not expecting for successful or unsuccessful results
2189 * @param messageBuffer
2190 * @param message
2191 * @param failedMsgId
2192 * @param sctpMap
ss412g1a79bdf2019-10-24 12:03:05 +03002193 * @return
2194 */
2195int sendDirectionalSctpMsg(RmrMessagesBuffer_t &messageBuffer,
2196 ReportingMessages_t &message,
2197 int failedMsgId,
aa7133@att.combe92d712020-03-18 17:30:39 +02002198 Sctp_Map_t *sctpMap) {
ss412g1a79bdf2019-10-24 12:03:05 +03002199
aa7133@att.combe92d712020-03-18 17:30:39 +02002200 getRequestMetaData(message, messageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03002201 if (mdclog_level_get() >= MDCLOG_INFO) {
2202 mdclog_write(MDCLOG_INFO, "send message to %s address", message.message.enodbName);
2203 }
2204
aa7133@att.combe92d712020-03-18 17:30:39 +02002205 auto rc = sendMessagetoCu(sctpMap, messageBuffer, message, failedMsgId);
ss412g1a79bdf2019-10-24 12:03:05 +03002206 return rc;
2207}
2208
2209/**
2210 *
2211 * @param sctpMap
2212 * @param messageBuffer
2213 * @param message
2214 * @param failedMesgId
ss412g1a79bdf2019-10-24 12:03:05 +03002215 * @return
2216 */
2217int sendMessagetoCu(Sctp_Map_t *sctpMap,
2218 RmrMessagesBuffer_t &messageBuffer,
2219 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +02002220 int failedMesgId) {
ss412g1a79bdf2019-10-24 12:03:05 +03002221 auto *peerInfo = (ConnectedCU_t *) sctpMap->find(message.message.enodbName);
2222 if (peerInfo == nullptr) {
2223 if (failedMesgId != 0) {
aa7133@att.combe92d712020-03-18 17:30:39 +02002224 sendFailedSendingMessagetoXapp(messageBuffer, message, failedMesgId);
ss412g1a79bdf2019-10-24 12:03:05 +03002225 } else {
2226 mdclog_write(MDCLOG_ERR, "Failed to send message no CU entry %s", message.message.enodbName);
2227 }
ss412g1a79bdf2019-10-24 12:03:05 +03002228 return -1;
2229 }
2230
2231 // get the FD
2232 message.message.messageType = messageBuffer.rcvMessage->mtype;
aa7133@att.combe92d712020-03-18 17:30:39 +02002233 auto rc = sendSctpMsg(peerInfo, message, sctpMap);
ss412g1a79bdf2019-10-24 12:03:05 +03002234 return rc;
2235}
2236
2237/**
2238 *
2239 * @param rmrCtx the rmr context to send and receive
2240 * @param msg the msg we got fromxApp
2241 * @param metaData data from xApp in ordered struct
2242 * @param failedMesgId the return message type error
2243 */
2244void
aa7133@att.combe92d712020-03-18 17:30:39 +02002245sendFailedSendingMessagetoXapp(RmrMessagesBuffer_t &rmrMessageBuffer, ReportingMessages_t &message, int failedMesgId) {
ss412g1a79bdf2019-10-24 12:03:05 +03002246 rmr_mbuf_t *msg = rmrMessageBuffer.sendMessage;
2247 msg->len = snprintf((char *) msg->payload, 200, "the gNb/eNode name %s not found",
2248 message.message.enodbName);
2249 if (mdclog_level_get() >= MDCLOG_INFO) {
2250 mdclog_write(MDCLOG_INFO, "%s", msg->payload);
2251 }
2252 msg->mtype = failedMesgId;
2253 msg->state = 0;
2254
2255 static unsigned char tx[32];
2256 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2257 rmr_bytes2xact(msg, tx, strlen((const char *) tx));
2258
aa7133@att.combe92d712020-03-18 17:30:39 +02002259 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03002260}
2261
ss412g1a79bdf2019-10-24 12:03:05 +03002262
ss412g1a79bdf2019-10-24 12:03:05 +03002263
2264/**
2265 *
2266 * @param epoll_fd
2267 * @param peerInfo
2268 * @param events
2269 * @param sctpMap
2270 * @param enodbName
2271 * @param msgType
ss412g1a79bdf2019-10-24 12:03:05 +03002272 * @return
2273 */
2274int addToEpoll(int epoll_fd,
2275 ConnectedCU_t *peerInfo,
2276 uint32_t events,
2277 Sctp_Map_t *sctpMap,
2278 char *enodbName,
aa7133@att.combe92d712020-03-18 17:30:39 +02002279 int msgType) {
ss412g1a79bdf2019-10-24 12:03:05 +03002280 // Add to Epol
2281 struct epoll_event event{};
2282 event.data.ptr = peerInfo;
2283 event.events = events;
2284 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, peerInfo->fileDescriptor, &event) < 0) {
2285 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2286 mdclog_write(MDCLOG_DEBUG, "epoll_ctl EPOLL_CTL_ADD (may chack not to quit here), %s, %s %d",
2287 strerror(errno), __func__, __LINE__);
2288 }
2289 close(peerInfo->fileDescriptor);
aa7133@att.com84bd3342020-03-16 18:04:57 +02002290 if (enodbName != nullptr) {
aa7133@att.combe92d712020-03-18 17:30:39 +02002291 cleanHashEntry(peerInfo, sctpMap);
aa7133@att.com84bd3342020-03-16 18:04:57 +02002292 char key[MAX_ENODB_NAME_SIZE * 2];
2293 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", enodbName, msgType);
2294 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2295 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
2296 }
2297 auto tmp = sctpMap->find(key);
2298 if (tmp) {
2299 free(tmp);
2300 sctpMap->erase(key);
2301 }
2302 } else {
2303 peerInfo->enodbName[0] = 0;
ss412g1a79bdf2019-10-24 12:03:05 +03002304 }
ss412g1a79bdf2019-10-24 12:03:05 +03002305 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may chack not to quit here)");
ss412g1a79bdf2019-10-24 12:03:05 +03002306 return -1;
2307 }
ss412g1a79bdf2019-10-24 12:03:05 +03002308 return 0;
2309}
2310
2311/**
2312 *
2313 * @param epoll_fd
2314 * @param peerInfo
2315 * @param events
2316 * @param sctpMap
2317 * @param enodbName
2318 * @param msgType
ss412g1a79bdf2019-10-24 12:03:05 +03002319 * @return
2320 */
2321int modifyToEpoll(int epoll_fd,
2322 ConnectedCU_t *peerInfo,
2323 uint32_t events,
2324 Sctp_Map_t *sctpMap,
2325 char *enodbName,
aa7133@att.combe92d712020-03-18 17:30:39 +02002326 int msgType) {
ss412g1a79bdf2019-10-24 12:03:05 +03002327 // Add to Epol
2328 struct epoll_event event{};
2329 event.data.ptr = peerInfo;
2330 event.events = events;
2331 if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, peerInfo->fileDescriptor, &event) < 0) {
2332 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2333 mdclog_write(MDCLOG_DEBUG, "epoll_ctl EPOLL_CTL_MOD (may chack not to quit here), %s, %s %d",
2334 strerror(errno), __func__, __LINE__);
2335 }
2336 close(peerInfo->fileDescriptor);
aa7133@att.combe92d712020-03-18 17:30:39 +02002337 cleanHashEntry(peerInfo, sctpMap);
ss412g1a79bdf2019-10-24 12:03:05 +03002338 char key[MAX_ENODB_NAME_SIZE * 2];
2339 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", enodbName, msgType);
2340 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2341 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
2342 }
aa7133@att.com96c9b882020-03-25 14:50:40 +02002343 auto tmp = sctpMap->find(key);
ss412g1a79bdf2019-10-24 12:03:05 +03002344 if (tmp) {
2345 free(tmp);
2346 }
2347 sctpMap->erase(key);
2348 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may chack not to quit here)");
ss412g1a79bdf2019-10-24 12:03:05 +03002349 return -1;
2350 }
ss412g1a79bdf2019-10-24 12:03:05 +03002351 return 0;
2352}
2353
2354
aa7133@att.combe92d712020-03-18 17:30:39 +02002355int sendRmrMessage(RmrMessagesBuffer_t &rmrMessageBuffer, ReportingMessages_t &message) {
ss412g1a79bdf2019-10-24 12:03:05 +03002356 buildJsonMessage(message);
2357
2358 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2359
2360 if (rmrMessageBuffer.sendMessage == nullptr) {
2361 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
2362 mdclog_write(MDCLOG_ERR, "RMR failed send message returned with NULL pointer");
ss412g1a79bdf2019-10-24 12:03:05 +03002363 return -1;
2364 }
2365
2366 if (rmrMessageBuffer.sendMessage->state != 0) {
2367 char meid[RMR_MAX_MEID]{};
2368 if (rmrMessageBuffer.sendMessage->state == RMR_ERR_RETRY) {
2369 usleep(5);
2370 rmrMessageBuffer.sendMessage->state = 0;
2371 mdclog_write(MDCLOG_INFO, "RETRY sending Message type %d to Xapp from %s",
2372 rmrMessageBuffer.sendMessage->mtype,
2373 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
2374 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2375 if (rmrMessageBuffer.sendMessage == nullptr) {
2376 mdclog_write(MDCLOG_ERR, "RMR failed send message returned with NULL pointer");
2377 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
ss412g1a79bdf2019-10-24 12:03:05 +03002378 return -1;
2379 } else if (rmrMessageBuffer.sendMessage->state != 0) {
2380 mdclog_write(MDCLOG_ERR,
2381 "Message state %s while sending request %d to Xapp from %s after retry of 10 microseconds",
2382 translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str(),
2383 rmrMessageBuffer.sendMessage->mtype,
2384 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
2385 auto rc = rmrMessageBuffer.sendMessage->state;
ss412g1a79bdf2019-10-24 12:03:05 +03002386 return rc;
2387 }
2388 } else {
2389 mdclog_write(MDCLOG_ERR, "Message state %s while sending request %d to Xapp from %s",
2390 translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str(),
2391 rmrMessageBuffer.sendMessage->mtype,
2392 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
ss412g1a79bdf2019-10-24 12:03:05 +03002393 return rmrMessageBuffer.sendMessage->state;
2394 }
2395 }
2396 return 0;
2397}
2398
2399void buildJsonMessage(ReportingMessages_t &message) {
ss412g3bac2da2020-01-05 11:52:19 +02002400 if (jsonTrace) {
2401 message.outLen = sizeof(message.base64Data);
2402 base64::encode((const unsigned char *) message.message.asndata,
2403 (const int) message.message.asnLength,
2404 message.base64Data,
2405 message.outLen);
2406 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.comc5a15202020-04-05 19:57:01 +03002407 mdclog_write(MDCLOG_DEBUG, "Tracing: ASN length = %d, base64 message length = %d ",
ss412g3bac2da2020-01-05 11:52:19 +02002408 (int) message.message.asnLength,
2409 (int) message.outLen);
2410 }
2411
2412 snprintf(message.buffer, sizeof(message.buffer),
aa7133@att.com96c9b882020-03-25 14:50:40 +02002413 "{\"header\": {\"ts\": \"%ld.%09ld\","
2414 "\"ranName\": \"%s\","
2415 "\"messageType\": %d,"
2416 "\"direction\": \"%c\"},"
2417 "\"base64Length\": %d,"
2418 "\"asnBase64\": \"%s\"}",
2419 message.message.time.tv_sec,
2420 message.message.time.tv_nsec,
2421 message.message.enodbName,
2422 message.message.messageType,
2423 message.message.direction,
2424 (int) message.outLen,
2425 message.base64Data);
ss412g3bac2da2020-01-05 11:52:19 +02002426 static src::logger_mt &lg = my_logger::get();
2427
2428 BOOST_LOG(lg) << message.buffer;
ss412g1a79bdf2019-10-24 12:03:05 +03002429 }
ss412g1a79bdf2019-10-24 12:03:05 +03002430}
2431
2432
2433/**
2434 * take RMR error code to string
2435 * @param state
2436 * @return
2437 */
2438string translateRmrErrorMessages(int state) {
2439 string str = {};
2440 switch (state) {
2441 case RMR_OK:
2442 str = "RMR_OK - state is good";
2443 break;
2444 case RMR_ERR_BADARG:
2445 str = "RMR_ERR_BADARG - argument passd to function was unusable";
2446 break;
2447 case RMR_ERR_NOENDPT:
2448 str = "RMR_ERR_NOENDPT - send//call could not find an endpoint based on msg type";
2449 break;
2450 case RMR_ERR_EMPTY:
2451 str = "RMR_ERR_EMPTY - msg received had no payload; attempt to send an empty message";
2452 break;
2453 case RMR_ERR_NOHDR:
2454 str = "RMR_ERR_NOHDR - message didn't contain a valid header";
2455 break;
2456 case RMR_ERR_SENDFAILED:
2457 str = "RMR_ERR_SENDFAILED - send failed; errno has nano reason";
2458 break;
2459 case RMR_ERR_CALLFAILED:
2460 str = "RMR_ERR_CALLFAILED - unable to send call() message";
2461 break;
2462 case RMR_ERR_NOWHOPEN:
2463 str = "RMR_ERR_NOWHOPEN - no wormholes are open";
2464 break;
2465 case RMR_ERR_WHID:
2466 str = "RMR_ERR_WHID - wormhole id was invalid";
2467 break;
2468 case RMR_ERR_OVERFLOW:
2469 str = "RMR_ERR_OVERFLOW - operation would have busted through a buffer/field size";
2470 break;
2471 case RMR_ERR_RETRY:
2472 str = "RMR_ERR_RETRY - request (send/call/rts) failed, but caller should retry (EAGAIN for wrappers)";
2473 break;
2474 case RMR_ERR_RCVFAILED:
2475 str = "RMR_ERR_RCVFAILED - receive failed (hard error)";
2476 break;
2477 case RMR_ERR_TIMEOUT:
2478 str = "RMR_ERR_TIMEOUT - message processing call timed out";
2479 break;
2480 case RMR_ERR_UNSET:
2481 str = "RMR_ERR_UNSET - the message hasn't been populated with a transport buffer";
2482 break;
2483 case RMR_ERR_TRUNC:
2484 str = "RMR_ERR_TRUNC - received message likely truncated";
2485 break;
2486 case RMR_ERR_INITFAILED:
2487 str = "RMR_ERR_INITFAILED - initialisation of something (probably message) failed";
2488 break;
2489 case RMR_ERR_NOTSUPP:
2490 str = "RMR_ERR_NOTSUPP - the request is not supported, or RMr was not initialised for the request";
2491 break;
2492 default:
2493 char buf[128]{};
2494 snprintf(buf, sizeof buf, "UNDOCUMENTED RMR_ERR : %d", state);
2495 str = buf;
2496 break;
2497 }
2498 return str;
2499}
2500
2501