blob: 130f246326da285ad1071537bcc6da68179f7993 [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.comdf4f1822020-06-16 16:23:53 +030027//#include "3rdparty/oranE2SM/E2SM-gNB-NRT-RANfunction-Definition.h"
28//#include "BuildXml.h"
29//#include "pugixml/src/pugixml.hpp"
aa7133@att.com7b437f72020-03-30 13:25:54 +030030
aa7133@att.com84bd3342020-03-16 18:04:57 +020031using namespace std;
32//using namespace std::placeholders;
ss412g3bac2da2020-01-05 11:52:19 +020033using namespace boost::filesystem;
ss412g4aeeda22020-06-16 14:12:41 +030034using namespace prometheus;
35
ss412g3bac2da2020-01-05 11:52:19 +020036
ss412g1a79bdf2019-10-24 12:03:05 +030037//#ifdef __cplusplus
38//extern "C"
39//{
40//#endif
41
ss412g3bac2da2020-01-05 11:52:19 +020042// need to expose without the include of gcov
43extern "C" void __gcov_flush(void);
44
45static void catch_function(int signal) {
46 __gcov_flush();
47 exit(signal);
48}
49
50
ss412g1a79bdf2019-10-24 12:03:05 +030051BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger, src::logger_mt)
52
53boost::shared_ptr<sinks::synchronous_sink<sinks::text_file_backend>> boostLogger;
ss412g3bac2da2020-01-05 11:52:19 +020054double cpuClock = 0.0;
55bool jsonTrace = true;
ss412g1a79bdf2019-10-24 12:03:05 +030056
57void init_log() {
58 mdclog_attr_t *attr;
59 mdclog_attr_init(&attr);
60 mdclog_attr_set_ident(attr, "E2Terminator");
61 mdclog_init(attr);
62 mdclog_attr_destroy(attr);
63}
ss412g3bac2da2020-01-05 11:52:19 +020064auto start_time = std::chrono::high_resolution_clock::now();
65typedef std::chrono::duration<double, std::ratio<1,1>> seconds_t;
ss412g1a79bdf2019-10-24 12:03:05 +030066
ss412g3bac2da2020-01-05 11:52:19 +020067double age() {
68 return seconds_t(std::chrono::high_resolution_clock::now() - start_time).count();
69}
70
71double approx_CPU_MHz(unsigned sleeptime) {
72 using namespace std::chrono_literals;
73 uint32_t aux = 0;
74 uint64_t cycles_start = rdtscp(aux);
75 double time_start = age();
76 std::this_thread::sleep_for(sleeptime * 1ms);
77 uint64_t elapsed_cycles = rdtscp(aux) - cycles_start;
78 double elapsed_time = age() - time_start;
79 return elapsed_cycles / elapsed_time;
80}
ss412g1a79bdf2019-10-24 12:03:05 +030081
82//std::atomic<int64_t> rmrCounter{0};
83std::atomic<int64_t> num_of_messages{0};
ss412g3bac2da2020-01-05 11:52:19 +020084std::atomic<int64_t> num_of_XAPP_messages{0};
ss412g1a79bdf2019-10-24 12:03:05 +030085static long transactionCounter = 0;
86
aa7133@att.com84bd3342020-03-16 18:04:57 +020087int buildListeningPort(sctp_params_t &sctpParams) {
aa7133@att.comf211f192020-06-23 12:50:10 +030088 sctpParams.listenFD = socket(AF_INET6, SOCK_STREAM, IPPROTO_SCTP);
89 if (sctpParams.listenFD <= 0) {
90 mdclog_write(MDCLOG_ERR, "Error Opening socket, %s", strerror(errno));
91 return -1;
92 }
93
aa7133@att.com84bd3342020-03-16 18:04:57 +020094 struct sockaddr_in6 servaddr {};
95 servaddr.sin6_family = AF_INET6;
96 servaddr.sin6_addr = in6addr_any;
aa7133@att.combe92d712020-03-18 17:30:39 +020097 servaddr.sin6_port = htons(sctpParams.sctpPort);
aa7133@att.com84bd3342020-03-16 18:04:57 +020098 if (bind(sctpParams.listenFD, (SA *)&servaddr, sizeof(servaddr)) < 0 ) {
aa7133@att.comf211f192020-06-23 12:50:10 +030099 mdclog_write(MDCLOG_ERR, "Error binding port %d. %s", sctpParams.sctpPort, strerror(errno));
aa7133@att.com84bd3342020-03-16 18:04:57 +0200100 return -1;
101 }
102 if (setSocketNoBlocking(sctpParams.listenFD) == -1) {
103 //mdclog_write(MDCLOG_ERR, "Error binding. %s", strerror(errno));
104 return -1;
105 }
106 if (mdclog_level_get() >= MDCLOG_DEBUG) {
107 struct sockaddr_in6 cliaddr {};
108 socklen_t len = sizeof(cliaddr);
109 getsockname(sctpParams.listenFD, (SA *)&cliaddr, &len);
110 char buff[1024] {};
111 inet_ntop(AF_INET6, &cliaddr.sin6_addr, buff, sizeof(buff));
112 mdclog_write(MDCLOG_DEBUG, "My address: %s, port %d\n", buff, htons(cliaddr.sin6_port));
ss412g1a79bdf2019-10-24 12:03:05 +0300113 }
114
aa7133@att.com84bd3342020-03-16 18:04:57 +0200115 if (listen(sctpParams.listenFD, SOMAXCONN) < 0) {
116 mdclog_write(MDCLOG_ERR, "Error listening. %s\n", strerror(errno));
117 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200118 }
aa7133@att.com84bd3342020-03-16 18:04:57 +0200119 struct epoll_event event {};
120 event.events = EPOLLIN | EPOLLET;
121 event.data.fd = sctpParams.listenFD;
122
123 // add listening port to epoll
124 if (epoll_ctl(sctpParams.epoll_fd, EPOLL_CTL_ADD, sctpParams.listenFD, &event)) {
125 printf("Failed to add descriptor to epoll\n");
126 mdclog_write(MDCLOG_ERR, "Failed to add descriptor to epoll. %s\n", strerror(errno));
127 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200128 }
129
aa7133@att.com84bd3342020-03-16 18:04:57 +0200130 return 0;
131}
ss412g3bac2da2020-01-05 11:52:19 +0200132
aa7133@att.com84bd3342020-03-16 18:04:57 +0200133int buildConfiguration(sctp_params_t &sctpParams) {
ss412g3bac2da2020-01-05 11:52:19 +0200134 path p = (sctpParams.configFilePath + "/" + sctpParams.configFileName).c_str();
135 if (exists(p)) {
136 const int size = 2048;
137 auto fileSize = file_size(p);
138 if (fileSize > size) {
139 mdclog_write(MDCLOG_ERR, "File %s larger than %d", p.string().c_str(), size);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200140 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200141 }
142 } else {
143 mdclog_write(MDCLOG_ERR, "Configuration File %s not exists", p.string().c_str());
aa7133@att.com84bd3342020-03-16 18:04:57 +0200144 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200145 }
146
ss412g3bac2da2020-01-05 11:52:19 +0200147 ReadConfigFile conf;
148 if (conf.openConfigFile(p.string()) == -1) {
149 mdclog_write(MDCLOG_ERR, "Filed to open config file %s, %s",
150 p.string().c_str(), strerror(errno));
aa7133@att.com84bd3342020-03-16 18:04:57 +0200151 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200152 }
153 int rmrPort = conf.getIntValue("nano");
154 if (rmrPort == -1) {
155 mdclog_write(MDCLOG_ERR, "illigal RMR port ");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200156 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200157 }
158 sctpParams.rmrPort = (uint16_t)rmrPort;
159 snprintf(sctpParams.rmrAddress, sizeof(sctpParams.rmrAddress), "%d", (int) (sctpParams.rmrPort));
160
161 auto tmpStr = conf.getStringValue("loglevel");
162 if (tmpStr.length() == 0) {
163 mdclog_write(MDCLOG_ERR, "illigal loglevel. Set loglevel to MDCLOG_INFO");
164 tmpStr = "info";
165 }
166 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
167
168 if ((tmpStr.compare("debug")) == 0) {
169 sctpParams.logLevel = MDCLOG_DEBUG;
170 } else if ((tmpStr.compare("info")) == 0) {
171 sctpParams.logLevel = MDCLOG_INFO;
172 } else if ((tmpStr.compare("warning")) == 0) {
173 sctpParams.logLevel = MDCLOG_WARN;
174 } else if ((tmpStr.compare("error")) == 0) {
175 sctpParams.logLevel = MDCLOG_ERR;
176 } else {
177 mdclog_write(MDCLOG_ERR, "illigal loglevel = %s. Set loglevel to MDCLOG_INFO", tmpStr.c_str());
178 sctpParams.logLevel = MDCLOG_INFO;
179 }
180 mdclog_level_set(sctpParams.logLevel);
181
182 tmpStr = conf.getStringValue("volume");
183 if (tmpStr.length() == 0) {
184 mdclog_write(MDCLOG_ERR, "illigal volume.");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200185 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200186 }
187
ss412g1a79bdf2019-10-24 12:03:05 +0300188 char tmpLogFilespec[VOLUME_URL_SIZE];
189 tmpLogFilespec[0] = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200190 sctpParams.volume[0] = 0;
191 snprintf(sctpParams.volume, VOLUME_URL_SIZE, "%s", tmpStr.c_str());
192 // copy the name to temp file as well
193 snprintf(tmpLogFilespec, VOLUME_URL_SIZE, "%s", tmpStr.c_str());
194
195
196 // define the file name in the tmp directory under the volume
197 strcat(tmpLogFilespec,"/tmp/E2Term_%Y-%m-%d_%H-%M-%S.%N.tmpStr");
198
199 sctpParams.myIP = conf.getStringValue("local-ip");
200 if (sctpParams.myIP.length() == 0) {
201 mdclog_write(MDCLOG_ERR, "illigal local-ip.");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200202 return -1;
ss412g1a79bdf2019-10-24 12:03:05 +0300203 }
204
aa7133@att.combe92d712020-03-18 17:30:39 +0200205 int sctpPort = conf.getIntValue("sctp-port");
206 if (sctpPort == -1) {
207 mdclog_write(MDCLOG_ERR, "illigal SCTP port ");
208 return -1;
209 }
210 sctpParams.sctpPort = (uint16_t)sctpPort;
211
aa7133@att.com84bd3342020-03-16 18:04:57 +0200212 sctpParams.fqdn = conf.getStringValue("external-fqdn");
213 if (sctpParams.fqdn.length() == 0) {
214 mdclog_write(MDCLOG_ERR, "illigal external-fqdn");
215 return -1;
ss412g3bac2da2020-01-05 11:52:19 +0200216 }
ss412g1a79bdf2019-10-24 12:03:05 +0300217
aa7133@att.com342843e2020-01-26 12:53:34 +0200218 std::string pod = conf.getStringValue("pod_name");
219 if (pod.length() == 0) {
220 mdclog_write(MDCLOG_ERR, "illigal pod_name in config file");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200221 return -1;
aa7133@att.com342843e2020-01-26 12:53:34 +0200222 }
223 auto *podName = getenv(pod.c_str());
224 if (podName == nullptr) {
225 mdclog_write(MDCLOG_ERR, "illigal pod_name or environment varible not exists : %s", pod.c_str());
aa7133@att.com84bd3342020-03-16 18:04:57 +0200226 return -1;
aa7133@att.com342843e2020-01-26 12:53:34 +0200227
228 } else {
229 sctpParams.podName.assign(podName);
230 if (sctpParams.podName.length() == 0) {
231 mdclog_write(MDCLOG_ERR, "illigal pod_name");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200232 return -1;
aa7133@att.com342843e2020-01-26 12:53:34 +0200233 }
234 }
235
ss412g3bac2da2020-01-05 11:52:19 +0200236 tmpStr = conf.getStringValue("trace");
237 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
238 if ((tmpStr.compare("start")) == 0) {
239 mdclog_write(MDCLOG_INFO, "Trace set to: start");
240 sctpParams.trace = true;
241 } else if ((tmpStr.compare("stop")) == 0) {
242 mdclog_write(MDCLOG_INFO, "Trace set to: stop");
243 sctpParams.trace = false;
244 }
245 jsonTrace = sctpParams.trace;
ss412g1a79bdf2019-10-24 12:03:05 +0300246
ss412g4aeeda22020-06-16 14:12:41 +0300247 sctpParams.epollTimeOut = -1;
ss412g4aeeda22020-06-16 14:12:41 +0300248
249 tmpStr = conf.getStringValue("prometheusPort");
250 if (tmpStr.length() != 0) {
251 sctpParams.prometheusPort = tmpStr;
252 }
253
aa7133@att.combe92d712020-03-18 17:30:39 +0200254 sctpParams.ka_message_length = snprintf(sctpParams.ka_message, KA_MESSAGE_SIZE, "{\"address\": \"%s:%d\","
aa7133@att.com96c9b882020-03-25 14:50:40 +0200255 "\"fqdn\": \"%s\","
256 "\"pod_name\": \"%s\"}",
aa7133@att.com342843e2020-01-26 12:53:34 +0200257 (const char *)sctpParams.myIP.c_str(),
258 sctpParams.rmrPort,
259 sctpParams.fqdn.c_str(),
260 sctpParams.podName.c_str());
261
ss412g1a79bdf2019-10-24 12:03:05 +0300262 if (mdclog_level_get() >= MDCLOG_INFO) {
ss412g3bac2da2020-01-05 11:52:19 +0200263 mdclog_mdc_add("RMR Port", to_string(sctpParams.rmrPort).c_str());
264 mdclog_mdc_add("LogLevel", to_string(sctpParams.logLevel).c_str());
265 mdclog_mdc_add("volume", sctpParams.volume);
ss412g1a79bdf2019-10-24 12:03:05 +0300266 mdclog_mdc_add("tmpLogFilespec", tmpLogFilespec);
ss412g3bac2da2020-01-05 11:52:19 +0200267 mdclog_mdc_add("my ip", sctpParams.myIP.c_str());
aa7133@att.com342843e2020-01-26 12:53:34 +0200268 mdclog_mdc_add("pod name", sctpParams.podName.c_str());
ss412g1a79bdf2019-10-24 12:03:05 +0300269
aa7133@att.com342843e2020-01-26 12:53:34 +0200270 mdclog_write(MDCLOG_INFO, "running parameters for instance : %s", sctpParams.ka_message);
ss412g1a79bdf2019-10-24 12:03:05 +0300271 }
272 mdclog_mdc_clean();
273
274 // Files written to the current working directory
275 boostLogger = logging::add_file_log(
ss412g3bac2da2020-01-05 11:52:19 +0200276 keywords::file_name = tmpLogFilespec, // to temp directory
ss412g1a79bdf2019-10-24 12:03:05 +0300277 keywords::rotation_size = 10 * 1024 * 1024,
278 keywords::time_based_rotation = sinks::file::rotation_at_time_interval(posix_time::hours(1)),
279 keywords::format = "%Message%"
ss412g3bac2da2020-01-05 11:52:19 +0200280 //keywords::format = "[%TimeStamp%]: %Message%" // use each tmpStr with time stamp
ss412g1a79bdf2019-10-24 12:03:05 +0300281 );
282
283 // Setup a destination folder for collecting rotated (closed) files --since the same volumn can use rename()
284 boostLogger->locked_backend()->set_file_collector(sinks::file::make_collector(
ss412g3bac2da2020-01-05 11:52:19 +0200285 keywords::target = sctpParams.volume
ss412g1a79bdf2019-10-24 12:03:05 +0300286 ));
287
288 // Upon restart, scan the directory for files matching the file_name pattern
289 boostLogger->locked_backend()->scan_for_files();
290
ss412g3bac2da2020-01-05 11:52:19 +0200291 // Enable auto-flushing after each tmpStr record written
ss412g1a79bdf2019-10-24 12:03:05 +0300292 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com84bd3342020-03-16 18:04:57 +0200293 boostLogger->locked_backend()->auto_flush(true);
294 }
295
296 return 0;
297}
298
aa7133@att.coma4c80012020-06-24 16:36:47 +0300299void startPrometheus(sctp_params_t &sctpParams) {
300 sctpParams.prometheusFamily = &BuildCounter()
301 .Name("E2T")
302 .Help("E2T message counter")
303 .Labels({{"POD_NAME", sctpParams.podName}})
304 .Register(*sctpParams.prometheusRegistry);
aa7133@att.com16c799b2020-03-24 18:01:51 +0200305
aa7133@att.comb92748d2020-06-29 11:52:30 +0300306 string promethusPath = sctpParams.prometheusPort + "," + "[::]:" + sctpParams.prometheusPort;
307 if (mdclog_level_get() >= MDCLOG_DEBUG) {
308 mdclog_write(MDCLOG_DEBUG, "Start Prometheus Pull mode on %s", promethusPath.c_str());
aa7133@att.coma4c80012020-06-24 16:36:47 +0300309 }
aa7133@att.comb92748d2020-06-29 11:52:30 +0300310 sctpParams.prometheusExposer = new Exposer(promethusPath, 1);
311 sctpParams.prometheusExposer->RegisterCollectable(sctpParams.prometheusRegistry);
aa7133@att.coma4c80012020-06-24 16:36:47 +0300312}
aa7133@att.com16c799b2020-03-24 18:01:51 +0200313
aa7133@att.com84bd3342020-03-16 18:04:57 +0200314int main(const int argc, char **argv) {
315 sctp_params_t sctpParams;
316
aa7133@att.com84bd3342020-03-16 18:04:57 +0200317 {
318 std::random_device device{};
319 std::mt19937 generator(device());
320 std::uniform_int_distribution<long> distribution(1, (long) 1e12);
321 transactionCounter = distribution(generator);
322 }
323
324// uint64_t st = 0;
325// uint32_t aux1 = 0;
326// st = rdtscp(aux1);
327
328 unsigned num_cpus = std::thread::hardware_concurrency();
329 init_log();
330 mdclog_level_set(MDCLOG_INFO);
331
332 if (std::signal(SIGINT, catch_function) == SIG_ERR) {
333 mdclog_write(MDCLOG_ERR, "Error initializing SIGINT");
334 exit(1);
335 }
336 if (std::signal(SIGABRT, catch_function)== SIG_ERR) {
337 mdclog_write(MDCLOG_ERR, "Error initializing SIGABRT");
338 exit(1);
339 }
340 if (std::signal(SIGTERM, catch_function)== SIG_ERR) {
341 mdclog_write(MDCLOG_ERR, "Error initializing SIGTERM");
342 exit(1);
343 }
344
345 cpuClock = approx_CPU_MHz(100);
346
347 mdclog_write(MDCLOG_DEBUG, "CPU speed %11.11f", cpuClock);
348
349 auto result = parse(argc, argv, sctpParams);
350
351 if (buildConfiguration(sctpParams) != 0) {
352 exit(-1);
ss412g1a79bdf2019-10-24 12:03:05 +0300353 }
354
ss412g4aeeda22020-06-16 14:12:41 +0300355 //auto registry = std::make_shared<Registry>();
356 sctpParams.prometheusRegistry = std::make_shared<Registry>();
357
358 //sctpParams.promtheusFamily = new Family<Counter>("E2T", "E2T message counter", {{"E", sctpParams.podName}});
359
aa7133@att.coma4c80012020-06-24 16:36:47 +0300360 startPrometheus(sctpParams);
ss412g4aeeda22020-06-16 14:12:41 +0300361
ss412g1a79bdf2019-10-24 12:03:05 +0300362 // start epoll
ss412g3bac2da2020-01-05 11:52:19 +0200363 sctpParams.epoll_fd = epoll_create1(0);
364 if (sctpParams.epoll_fd == -1) {
ss412g1a79bdf2019-10-24 12:03:05 +0300365 mdclog_write(MDCLOG_ERR, "failed to open epoll descriptor");
366 exit(-1);
367 }
368
aa7133@att.combe92d712020-03-18 17:30:39 +0200369 getRmrContext(sctpParams);
ss412g3bac2da2020-01-05 11:52:19 +0200370 if (sctpParams.rmrCtx == nullptr) {
371 close(sctpParams.epoll_fd);
ss412g1a79bdf2019-10-24 12:03:05 +0300372 exit(-1);
373 }
374
ss412g3bac2da2020-01-05 11:52:19 +0200375 if (buildInotify(sctpParams) == -1) {
376 close(sctpParams.rmrListenFd);
377 rmr_close(sctpParams.rmrCtx);
378 close(sctpParams.epoll_fd);
379 exit(-1);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200380 }
381
382 if (buildListeningPort(sctpParams) != 0) {
383 close(sctpParams.rmrListenFd);
384 rmr_close(sctpParams.rmrCtx);
385 close(sctpParams.epoll_fd);
386 exit(-1);
387 }
ss412g3bac2da2020-01-05 11:52:19 +0200388
389 sctpParams.sctpMap = new mapWrapper();
ss412g1a79bdf2019-10-24 12:03:05 +0300390
391 std::vector<std::thread> threads(num_cpus);
392// std::vector<std::thread> threads;
393
394 num_cpus = 1;
395 for (unsigned int i = 0; i < num_cpus; i++) {
ss412g3bac2da2020-01-05 11:52:19 +0200396 threads[i] = std::thread(listener, &sctpParams);
ss412g1a79bdf2019-10-24 12:03:05 +0300397
398 cpu_set_t cpuset;
399 CPU_ZERO(&cpuset);
400 CPU_SET(i, &cpuset);
401 int rc = pthread_setaffinity_np(threads[i].native_handle(), sizeof(cpu_set_t), &cpuset);
402 if (rc != 0) {
403 mdclog_write(MDCLOG_ERR, "Error calling pthread_setaffinity_np: %d", rc);
404 }
ss412g1a79bdf2019-10-24 12:03:05 +0300405 }
406
aa7133@att.com16c799b2020-03-24 18:01:51 +0200407
ss412g3bac2da2020-01-05 11:52:19 +0200408 //loop over term_init until first message from xApp
409 handleTermInit(sctpParams);
410
411 for (auto &t : threads) {
412 t.join();
413 }
414
ss412g3bac2da2020-01-05 11:52:19 +0200415 return 0;
416}
417
418void handleTermInit(sctp_params_t &sctpParams) {
419 sendTermInit(sctpParams);
ss412g1a79bdf2019-10-24 12:03:05 +0300420 //send to e2 manager init of e2 term
421 //E2_TERM_INIT
ss412g1a79bdf2019-10-24 12:03:05 +0300422
ss412g3bac2da2020-01-05 11:52:19 +0200423 int count = 0;
424 while (true) {
425 auto xappMessages = num_of_XAPP_messages.load(std::memory_order_acquire);
426 if (xappMessages > 0) {
427 if (mdclog_level_get() >= MDCLOG_INFO) {
aa7133@att.com84bd3342020-03-16 18:04:57 +0200428 mdclog_write(MDCLOG_INFO, "Got a message from some appliction, stop sending E2_TERM_INIT");
ss412g3bac2da2020-01-05 11:52:19 +0200429 }
430 return;
431 }
432 usleep(100000);
433 count++;
434 if (count % 1000 == 0) {
435 mdclog_write(MDCLOG_ERR, "GOT No messages from any xApp");
436 sendTermInit(sctpParams);
437 }
438 }
439}
440
441void sendTermInit(sctp_params_t &sctpParams) {
442 rmr_mbuf_t *msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.ka_message_length);
ss412g1a79bdf2019-10-24 12:03:05 +0300443 auto count = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200444 while (true) {
ss412g1a79bdf2019-10-24 12:03:05 +0300445 msg->mtype = E2_TERM_INIT;
446 msg->state = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200447 rmr_bytes2payload(msg, (unsigned char *)sctpParams.ka_message, sctpParams.ka_message_length);
ss412g1a79bdf2019-10-24 12:03:05 +0300448 static unsigned char tx[32];
449 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
450 rmr_bytes2xact(msg, tx, txLen);
ss412g3bac2da2020-01-05 11:52:19 +0200451 msg = rmr_send_msg(sctpParams.rmrCtx, msg);
ss412g1a79bdf2019-10-24 12:03:05 +0300452 if (msg == nullptr) {
aa7133@att.comc9575aa2020-04-02 11:40:09 +0300453 msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.ka_message_length);
ss412g1a79bdf2019-10-24 12:03:05 +0300454 } else if (msg->state == 0) {
ss412g1a79bdf2019-10-24 12:03:05 +0300455 rmr_free_msg(msg);
ss412g3bac2da2020-01-05 11:52:19 +0200456 if (mdclog_level_get() >= MDCLOG_INFO) {
457 mdclog_write(MDCLOG_INFO, "E2_TERM_INIT succsesfuly sent ");
458 }
459 return;
ss412g1a79bdf2019-10-24 12:03:05 +0300460 } else {
461 if (count % 100 == 0) {
aa7133@att.com11b17d42020-03-22 11:35:16 +0200462 mdclog_write(MDCLOG_ERR, "Error sending E2_TERM_INIT cause : %s ", translateRmrErrorMessages(msg->state).c_str());
ss412g1a79bdf2019-10-24 12:03:05 +0300463 }
464 sleep(1);
465 }
466 count++;
467 }
ss412g3bac2da2020-01-05 11:52:19 +0200468}
469
470/**
471 *
472 * @param argc
473 * @param argv
474 * @param sctpParams
475 * @return
476 */
477cxxopts::ParseResult parse(int argc, char *argv[], sctp_params_t &sctpParams) {
478 cxxopts::Options options(argv[0], "e2 term help");
479 options.positional_help("[optional args]").show_positional_help();
480 options.allow_unrecognised_options().add_options()
481 ("p,path", "config file path", cxxopts::value<std::string>(sctpParams.configFilePath)->default_value("config"))
482 ("f,file", "config file name", cxxopts::value<std::string>(sctpParams.configFileName)->default_value("config.conf"))
483 ("h,help", "Print help");
484
aa7133@att.comde40d1c2020-08-26 16:19:20 +0300485 auto result = options.parse(argc, (const char **&)argv);
ss412g3bac2da2020-01-05 11:52:19 +0200486
487 if (result.count("help")) {
488 std::cout << options.help({""}) << std::endl;
489 exit(0);
490 }
491 return result;
492}
493
494/**
495 *
496 * @param sctpParams
497 * @return -1 failed 0 success
498 */
499int buildInotify(sctp_params_t &sctpParams) {
500 sctpParams.inotifyFD = inotify_init1(IN_NONBLOCK);
501 if (sctpParams.inotifyFD == -1) {
502 mdclog_write(MDCLOG_ERR, "Failed to init inotify (inotify_init1) %s", strerror(errno));
ss412g3bac2da2020-01-05 11:52:19 +0200503 return -1;
ss412g1a79bdf2019-10-24 12:03:05 +0300504 }
505
ss412g3bac2da2020-01-05 11:52:19 +0200506 sctpParams.inotifyWD = inotify_add_watch(sctpParams.inotifyFD,
aa7133@att.com96c9b882020-03-25 14:50:40 +0200507 (const char *)sctpParams.configFilePath.c_str(),
aa7133@att.com84bd3342020-03-16 18:04:57 +0200508 (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 +0200509 if (sctpParams.inotifyWD == -1) {
510 mdclog_write(MDCLOG_ERR, "Failed to add directory : %s to inotify (inotify_add_watch) %s",
aa7133@att.com96c9b882020-03-25 14:50:40 +0200511 sctpParams.configFilePath.c_str(),
512 strerror(errno));
ss412g3bac2da2020-01-05 11:52:19 +0200513 close(sctpParams.inotifyFD);
514 return -1;
515 }
516
517 struct epoll_event event{};
518 event.events = (EPOLLIN);
519 event.data.fd = sctpParams.inotifyFD;
520 // add listening RMR FD to epoll
521 if (epoll_ctl(sctpParams.epoll_fd, EPOLL_CTL_ADD, sctpParams.inotifyFD, &event)) {
522 mdclog_write(MDCLOG_ERR, "Failed to add inotify FD to epoll");
523 close(sctpParams.inotifyFD);
524 return -1;
525 }
ss412g1a79bdf2019-10-24 12:03:05 +0300526 return 0;
527}
528
529/**
530 *
531 * @param args
532 * @return
533 */
534void listener(sctp_params_t *params) {
ss412g1a79bdf2019-10-24 12:03:05 +0300535 int num_of_SCTP_messages = 0;
ss412g1a79bdf2019-10-24 12:03:05 +0300536 auto totalTime = 0.0;
537 mdclog_mdc_clean();
538 mdclog_level_set(params->logLevel);
539
540 std::thread::id this_id = std::this_thread::get_id();
541 //save cout
542 streambuf *oldCout = cout.rdbuf();
543 ostringstream memCout;
544 // create new cout
545 cout.rdbuf(memCout.rdbuf());
546 cout << this_id;
547 //return to the normal cout
548 cout.rdbuf(oldCout);
549
550 char tid[32];
551 memcpy(tid, memCout.str().c_str(), memCout.str().length() < 32 ? memCout.str().length() : 31);
552 tid[memCout.str().length()] = 0;
553 mdclog_mdc_add("thread id", tid);
554
555 if (mdclog_level_get() >= MDCLOG_DEBUG) {
556 mdclog_write(MDCLOG_DEBUG, "started thread number %s", tid);
557 }
558
559 RmrMessagesBuffer_t rmrMessageBuffer{};
560 //create and init RMR
561 rmrMessageBuffer.rmrCtx = params->rmrCtx;
562
563 auto *events = (struct epoll_event *) calloc(MAXEVENTS, sizeof(struct epoll_event));
564 struct timespec end{0, 0};
565 struct timespec start{0, 0};
566
567 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
568 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
569
ss412g3bac2da2020-01-05 11:52:19 +0200570 memcpy(rmrMessageBuffer.ka_message, params->ka_message, params->ka_message_length);
571 rmrMessageBuffer.ka_message_len = params->ka_message_length;
572 rmrMessageBuffer.ka_message[rmrMessageBuffer.ka_message_len] = 0;
573
574 if (mdclog_level_get() >= MDCLOG_DEBUG) {
575 mdclog_write(MDCLOG_DEBUG, "keep alive message is : %s", rmrMessageBuffer.ka_message);
576 }
577
ss412g1a79bdf2019-10-24 12:03:05 +0300578 ReportingMessages_t message {};
579
aa7133@att.com7c3e4c02020-03-25 16:37:19 +0200580// for (int i = 0; i < MAX_RMR_BUFF_ARRY; i++) {
581// rmrMessageBuffer.rcvBufferedMessages[i] = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
582// rmrMessageBuffer.sendBufferedMessages[i] = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
583// }
ss412g1a79bdf2019-10-24 12:03:05 +0300584
585 while (true) {
586 if (mdclog_level_get() >= MDCLOG_DEBUG) {
ss412g4aeeda22020-06-16 14:12:41 +0300587 mdclog_write(MDCLOG_DEBUG, "Start EPOLL Wait. Timeout = %d", params->epollTimeOut);
ss412g1a79bdf2019-10-24 12:03:05 +0300588 }
ss412g4aeeda22020-06-16 14:12:41 +0300589 auto numOfEvents = epoll_wait(params->epoll_fd, events, MAXEVENTS, params->epollTimeOut);
aa7133@att.comb92748d2020-06-29 11:52:30 +0300590 if (numOfEvents == 0) { // time out
591 if (mdclog_level_get() >= MDCLOG_DEBUG) {
592 mdclog_write(MDCLOG_DEBUG, "got epoll timeout");
ss412g1a79bdf2019-10-24 12:03:05 +0300593 }
594 continue;
ss412g4aeeda22020-06-16 14:12:41 +0300595 } else if (numOfEvents < 0) {
596 if (errno == EINTR) {
597 if (mdclog_level_get() >= MDCLOG_DEBUG) {
598 mdclog_write(MDCLOG_DEBUG, "got EINTR : %s", strerror(errno));
599 }
600 continue;
601 }
ss412g1a79bdf2019-10-24 12:03:05 +0300602 mdclog_write(MDCLOG_ERR, "Epoll wait failed, errno = %s", strerror(errno));
603 return;
604 }
605 for (auto i = 0; i < numOfEvents; i++) {
606 if (mdclog_level_get() >= MDCLOG_DEBUG) {
607 mdclog_write(MDCLOG_DEBUG, "handling epoll event %d out of %d", i + 1, numOfEvents);
608 }
609 clock_gettime(CLOCK_MONOTONIC, &message.message.time);
610 start.tv_sec = message.message.time.tv_sec;
611 start.tv_nsec = message.message.time.tv_nsec;
ss412g3bac2da2020-01-05 11:52:19 +0200612
613
ss412g1a79bdf2019-10-24 12:03:05 +0300614 if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP)) {
aa7133@att.combe92d712020-03-18 17:30:39 +0200615 handlepoll_error(events[i], message, rmrMessageBuffer, params);
ss412g1a79bdf2019-10-24 12:03:05 +0300616 } else if (events[i].events & EPOLLOUT) {
aa7133@att.combe92d712020-03-18 17:30:39 +0200617 handleEinprogressMessages(events[i], message, rmrMessageBuffer, params);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200618 } else if (params->listenFD == events[i].data.fd) {
aa7133@att.com6680c3b2020-03-22 12:06:58 +0200619 if (mdclog_level_get() >= MDCLOG_INFO) {
620 mdclog_write(MDCLOG_INFO, "New connection request from sctp network\n");
621 }
aa7133@att.com84bd3342020-03-16 18:04:57 +0200622 // new connection is requested from RAN start build connection
623 while (true) {
624 struct sockaddr in_addr {};
625 socklen_t in_len;
626 char hostBuff[NI_MAXHOST];
627 char portBuff[NI_MAXSERV];
628
629 in_len = sizeof(in_addr);
630 auto *peerInfo = (ConnectedCU_t *)calloc(1, sizeof(ConnectedCU_t));
swc58d4f5d2020-10-20 15:08:38 +0900631 if(peerInfo == NULL){
632 mdclog_write(MDCLOG_ERR, "calloc failed");
633 break;
634 }
aa7133@att.com84bd3342020-03-16 18:04:57 +0200635 peerInfo->sctpParams = params;
636 peerInfo->fileDescriptor = accept(params->listenFD, &in_addr, &in_len);
637 if (peerInfo->fileDescriptor == -1) {
638 if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
639 /* We have processed all incoming connections. */
640 break;
641 } else {
642 mdclog_write(MDCLOG_ERR, "Accept error, errno = %s", strerror(errno));
643 break;
644 }
645 }
646 if (setSocketNoBlocking(peerInfo->fileDescriptor) == -1) {
647 mdclog_write(MDCLOG_ERR, "setSocketNoBlocking failed to set new connection %s on port %s\n", hostBuff, portBuff);
648 close(peerInfo->fileDescriptor);
649 break;
650 }
651 auto ans = getnameinfo(&in_addr, in_len,
aa7133@att.com96c9b882020-03-25 14:50:40 +0200652 peerInfo->hostName, NI_MAXHOST,
653 peerInfo->portNumber, NI_MAXSERV, (unsigned )((unsigned int)NI_NUMERICHOST | (unsigned int)NI_NUMERICSERV));
aa7133@att.com84bd3342020-03-16 18:04:57 +0200654 if (ans < 0) {
655 mdclog_write(MDCLOG_ERR, "Failed to get info on connection request. %s\n", strerror(errno));
656 close(peerInfo->fileDescriptor);
657 break;
658 }
659 if (mdclog_level_get() >= MDCLOG_DEBUG) {
660 mdclog_write(MDCLOG_DEBUG, "Accepted connection on descriptor %d (host=%s, port=%s)\n", peerInfo->fileDescriptor, peerInfo->hostName, peerInfo->portNumber);
661 }
662 peerInfo->isConnected = false;
663 peerInfo->gotSetup = false;
664 if (addToEpoll(params->epoll_fd,
665 peerInfo,
666 (EPOLLIN | EPOLLET),
667 params->sctpMap, nullptr,
aa7133@att.combe92d712020-03-18 17:30:39 +0200668 0) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +0200669 break;
670 }
aa7133@att.com6680c3b2020-03-22 12:06:58 +0200671 break;
aa7133@att.com84bd3342020-03-16 18:04:57 +0200672 }
ss412g1a79bdf2019-10-24 12:03:05 +0300673 } else if (params->rmrListenFd == events[i].data.fd) {
674 // got message from XAPP
aa7133@att.comde40d1c2020-08-26 16:19:20 +0300675 //num_of_XAPP_messages.fetch_add(1, std::memory_order_release);
ss412g1a79bdf2019-10-24 12:03:05 +0300676 num_of_messages.fetch_add(1, std::memory_order_release);
677 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.comf13529a2020-08-27 11:21:16 +0300678 mdclog_write(MDCLOG_DEBUG, "new RMR message");
ss412g1a79bdf2019-10-24 12:03:05 +0300679 }
aa7133@att.com6680c3b2020-03-22 12:06:58 +0200680 if (receiveXappMessages(params->sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +0300681 rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200682 message.message.time) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +0300683 mdclog_write(MDCLOG_ERR, "Error handling Xapp message");
684 }
ss412g3bac2da2020-01-05 11:52:19 +0200685 } else if (params->inotifyFD == events[i].data.fd) {
686 mdclog_write(MDCLOG_INFO, "Got event from inotify (configuration update)");
687 handleConfigChange(params);
ss412g1a79bdf2019-10-24 12:03:05 +0300688 } else {
689 /* We RMR_ERR_RETRY have data on the fd waiting to be read. Read and display it.
690 * We must read whatever data is available completely, as we are running
691 * in edge-triggered mode and won't get a notification again for the same data. */
692 num_of_messages.fetch_add(1, std::memory_order_release);
693 if (mdclog_level_get() >= MDCLOG_DEBUG) {
694 mdclog_write(MDCLOG_DEBUG, "new message from SCTP, epoll flags are : %0x", events[i].events);
695 }
696 receiveDataFromSctp(&events[i],
697 params->sctpMap,
698 num_of_SCTP_messages,
699 rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200700 message.message.time);
ss412g1a79bdf2019-10-24 12:03:05 +0300701 }
702
703 clock_gettime(CLOCK_MONOTONIC, &end);
704 if (mdclog_level_get() >= MDCLOG_INFO) {
705 totalTime += ((end.tv_sec + 1.0e-9 * end.tv_nsec) -
706 ((double) start.tv_sec + 1.0e-9 * start.tv_nsec));
707 }
708 if (mdclog_level_get() >= MDCLOG_DEBUG) {
709 mdclog_write(MDCLOG_DEBUG, "message handling is %ld seconds %ld nanoseconds",
710 end.tv_sec - start.tv_sec,
711 end.tv_nsec - start.tv_nsec);
712 }
713 }
714 }
ss412g1a79bdf2019-10-24 12:03:05 +0300715}
716
717/**
718 *
ss412g3bac2da2020-01-05 11:52:19 +0200719 * @param sctpParams
720 */
721void handleConfigChange(sctp_params_t *sctpParams) {
722 char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event))));
723 const struct inotify_event *event;
724 char *ptr;
725
726 path p = (sctpParams->configFilePath + "/" + sctpParams->configFileName).c_str();
727 auto endlessLoop = true;
728 while (endlessLoop) {
729 auto len = read(sctpParams->inotifyFD, buf, sizeof buf);
730 if (len == -1) {
731 if (errno != EAGAIN) {
732 mdclog_write(MDCLOG_ERR, "read %s ", strerror(errno));
733 endlessLoop = false;
734 continue;
735 }
736 else {
737 endlessLoop = false;
738 continue;
739 }
740 }
741
742 for (ptr = buf; ptr < buf + len; ptr += sizeof(struct inotify_event) + event->len) {
743 event = (const struct inotify_event *)ptr;
744 if (event->mask & (uint32_t)IN_ISDIR) {
745 continue;
746 }
747
748 // the directory name
749 if (sctpParams->inotifyWD == event->wd) {
750 // not the directory
751 }
752 if (event->len) {
aa7133@att.comdfc1db92020-03-31 17:32:04 +0300753 auto retVal = strcmp(sctpParams->configFileName.c_str(), event->name);
754 if (retVal != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200755 continue;
756 }
757 }
758 // only the file we want
759 if (event->mask & (uint32_t)IN_CLOSE_WRITE) {
aa7133@att.comdfc1db92020-03-31 17:32:04 +0300760 if (mdclog_level_get() >= MDCLOG_INFO) {
761 mdclog_write(MDCLOG_INFO, "Configuration file changed");
762 }
ss412g3bac2da2020-01-05 11:52:19 +0200763 if (exists(p)) {
764 const int size = 2048;
765 auto fileSize = file_size(p);
766 if (fileSize > size) {
767 mdclog_write(MDCLOG_ERR, "File %s larger than %d", p.string().c_str(), size);
768 return;
769 }
770 } else {
771 mdclog_write(MDCLOG_ERR, "Configuration File %s not exists", p.string().c_str());
772 return;
773 }
774
775 ReadConfigFile conf;
776 if (conf.openConfigFile(p.string()) == -1) {
777 mdclog_write(MDCLOG_ERR, "Filed to open config file %s, %s",
778 p.string().c_str(), strerror(errno));
779 return;
780 }
781
782 auto tmpStr = conf.getStringValue("loglevel");
783 if (tmpStr.length() == 0) {
784 mdclog_write(MDCLOG_ERR, "illigal loglevel. Set loglevel to MDCLOG_INFO");
785 tmpStr = "info";
786 }
787 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
788
789 if ((tmpStr.compare("debug")) == 0) {
790 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_DEBUG");
791 sctpParams->logLevel = MDCLOG_DEBUG;
792 } else if ((tmpStr.compare("info")) == 0) {
793 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_INFO");
794 sctpParams->logLevel = MDCLOG_INFO;
795 } else if ((tmpStr.compare("warning")) == 0) {
796 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_WARN");
797 sctpParams->logLevel = MDCLOG_WARN;
798 } else if ((tmpStr.compare("error")) == 0) {
799 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_ERR");
800 sctpParams->logLevel = MDCLOG_ERR;
801 } else {
802 mdclog_write(MDCLOG_ERR, "illigal loglevel = %s. Set loglevel to MDCLOG_INFO", tmpStr.c_str());
803 sctpParams->logLevel = MDCLOG_INFO;
804 }
805 mdclog_level_set(sctpParams->logLevel);
806
807
808 tmpStr = conf.getStringValue("trace");
809 if (tmpStr.length() == 0) {
810 mdclog_write(MDCLOG_ERR, "illigal trace. Set trace to stop");
811 tmpStr = "stop";
812 }
813
814 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
815 if ((tmpStr.compare("start")) == 0) {
816 mdclog_write(MDCLOG_INFO, "Trace set to: start");
817 sctpParams->trace = true;
818 } else if ((tmpStr.compare("stop")) == 0) {
819 mdclog_write(MDCLOG_INFO, "Trace set to: stop");
820 sctpParams->trace = false;
821 } else {
822 mdclog_write(MDCLOG_ERR, "Trace was set to wrong value %s, set to stop", tmpStr.c_str());
823 sctpParams->trace = false;
824 }
825 jsonTrace = sctpParams->trace;
ss412g4aeeda22020-06-16 14:12:41 +0300826
ss412g4aeeda22020-06-16 14:12:41 +0300827
ss412g3bac2da2020-01-05 11:52:19 +0200828 endlessLoop = false;
829 }
830 }
831 }
832}
833
834/**
835 *
836 * @param event
837 * @param message
838 * @param rmrMessageBuffer
839 * @param params
ss412g3bac2da2020-01-05 11:52:19 +0200840 */
841void handleEinprogressMessages(struct epoll_event &event,
842 ReportingMessages_t &message,
843 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200844 sctp_params_t *params) {
ss412g3bac2da2020-01-05 11:52:19 +0200845 auto *peerInfo = (ConnectedCU_t *)event.data.ptr;
846 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
847
848 mdclog_write(MDCLOG_INFO, "file descriptor %d got EPOLLOUT", peerInfo->fileDescriptor);
849 auto retVal = 0;
850 socklen_t retValLen = 0;
851 auto rc = getsockopt(peerInfo->fileDescriptor, SOL_SOCKET, SO_ERROR, &retVal, &retValLen);
852 if (rc != 0 || retVal != 0) {
853 if (rc != 0) {
854 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
855 "%s|Failed SCTP Connection, after EINPROGRESS the getsockopt%s",
856 peerInfo->enodbName, strerror(errno));
857 } else if (retVal != 0) {
858 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
859 "%s|Failed SCTP Connection after EINPROGRESS, SO_ERROR",
860 peerInfo->enodbName);
861 }
862
863 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
864 message.message.asnLength = rmrMessageBuffer.sendMessage->len;
865 mdclog_write(MDCLOG_ERR, "%s", rmrMessageBuffer.sendMessage->payload);
866 message.message.direction = 'N';
aa7133@att.combe92d712020-03-18 17:30:39 +0200867 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200868 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
869 }
870 memset(peerInfo->asnData, 0, peerInfo->asnLength);
871 peerInfo->asnLength = 0;
872 peerInfo->mtype = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200873 return;
874 }
875
876 peerInfo->isConnected = true;
877
878 if (modifyToEpoll(params->epoll_fd, peerInfo, (EPOLLIN | EPOLLET), params->sctpMap, peerInfo->enodbName,
aa7133@att.combe92d712020-03-18 17:30:39 +0200879 peerInfo->mtype) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200880 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_MOD");
ss412g3bac2da2020-01-05 11:52:19 +0200881 return;
882 }
883
884 message.message.asndata = (unsigned char *)peerInfo->asnData;
885 message.message.asnLength = peerInfo->asnLength;
886 message.message.messageType = peerInfo->mtype;
887 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
888 num_of_messages.fetch_add(1, std::memory_order_release);
889 if (mdclog_level_get() >= MDCLOG_DEBUG) {
890 mdclog_write(MDCLOG_DEBUG, "send the delayed SETUP/ENDC SETUP to sctp for %s",
891 message.message.enodbName);
892 }
aa7133@att.combe92d712020-03-18 17:30:39 +0200893 if (sendSctpMsg(peerInfo, message, params->sctpMap) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200894 if (mdclog_level_get() >= MDCLOG_DEBUG) {
895 mdclog_write(MDCLOG_DEBUG, "Error write to SCTP %s %d", __func__, __LINE__);
896 }
ss412g3bac2da2020-01-05 11:52:19 +0200897 return;
898 }
899
900 memset(peerInfo->asnData, 0, peerInfo->asnLength);
901 peerInfo->asnLength = 0;
902 peerInfo->mtype = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200903}
904
905
906void handlepoll_error(struct epoll_event &event,
907 ReportingMessages_t &message,
908 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200909 sctp_params_t *params) {
ss412g3bac2da2020-01-05 11:52:19 +0200910 if (event.data.fd != params->rmrListenFd) {
911 auto *peerInfo = (ConnectedCU_t *)event.data.ptr;
912 mdclog_write(MDCLOG_ERR, "epoll error, events %0x on fd %d, RAN NAME : %s",
913 event.events, peerInfo->fileDescriptor, peerInfo->enodbName);
914
915 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
916 "%s|Failed SCTP Connection",
917 peerInfo->enodbName);
918 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
919 message.message.asnLength = rmrMessageBuffer.sendMessage->len;
920
921 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
922 message.message.direction = 'N';
aa7133@att.combe92d712020-03-18 17:30:39 +0200923 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200924 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
925 }
926
927 close(peerInfo->fileDescriptor);
aa7133@att.com7b437f72020-03-30 13:25:54 +0300928 params->sctpMap->erase(peerInfo->enodbName);
aa7133@att.combe92d712020-03-18 17:30:39 +0200929 cleanHashEntry((ConnectedCU_t *) event.data.ptr, params->sctpMap);
ss412g3bac2da2020-01-05 11:52:19 +0200930 } else {
931 mdclog_write(MDCLOG_ERR, "epoll error, events %0x on RMR FD", event.events);
932 }
ss412g3bac2da2020-01-05 11:52:19 +0200933}
934/**
935 *
ss412g1a79bdf2019-10-24 12:03:05 +0300936 * @param socket
937 * @return
938 */
939int setSocketNoBlocking(int socket) {
940 auto flags = fcntl(socket, F_GETFL, 0);
941
942 if (flags == -1) {
943 mdclog_mdc_add("func", "fcntl");
944 mdclog_write(MDCLOG_ERR, "%s, %s", __FUNCTION__, strerror(errno));
945 mdclog_mdc_clean();
946 return -1;
947 }
948
949 flags = (unsigned) flags | (unsigned) O_NONBLOCK;
950 if (fcntl(socket, F_SETFL, flags) == -1) {
951 mdclog_mdc_add("func", "fcntl");
952 mdclog_write(MDCLOG_ERR, "%s, %s", __FUNCTION__, strerror(errno));
953 mdclog_mdc_clean();
954 return -1;
955 }
956
957 return 0;
958}
959
960/**
961 *
962 * @param val
963 * @param m
ss412g1a79bdf2019-10-24 12:03:05 +0300964 */
aa7133@att.combe92d712020-03-18 17:30:39 +0200965void cleanHashEntry(ConnectedCU_t *val, Sctp_Map_t *m) {
ss412g1a79bdf2019-10-24 12:03:05 +0300966 char *dummy;
967 auto port = (uint16_t) strtol(val->portNumber, &dummy, 10);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200968 char searchBuff[2048]{};
ss412g1a79bdf2019-10-24 12:03:05 +0300969
970 snprintf(searchBuff, sizeof searchBuff, "host:%s:%d", val->hostName, port);
971 m->erase(searchBuff);
972
973 m->erase(val->enodbName);
974 free(val);
ss412g1a79bdf2019-10-24 12:03:05 +0300975}
976
977/**
978 *
979 * @param fd file discriptor
980 * @param data the asn data to send
981 * @param len length of the data
982 * @param enodbName the enodbName as in the map for printing purpose
983 * @param m map host information
984 * @param mtype message number
985 * @return 0 success, anegative number on fail
986 */
aa7133@att.combe92d712020-03-18 17:30:39 +0200987int sendSctpMsg(ConnectedCU_t *peerInfo, ReportingMessages_t &message, Sctp_Map_t *m) {
ss412g1a79bdf2019-10-24 12:03:05 +0300988 auto loglevel = mdclog_level_get();
989 int fd = peerInfo->fileDescriptor;
990 if (loglevel >= MDCLOG_DEBUG) {
991 mdclog_write(MDCLOG_DEBUG, "Send SCTP message for CU %s, %s",
992 message.message.enodbName, __FUNCTION__);
993 }
994
995 while (true) {
ss412g1a79bdf2019-10-24 12:03:05 +0300996 if (send(fd,message.message.asndata, message.message.asnLength,MSG_NOSIGNAL) < 0) {
997 if (errno == EINTR) {
998 continue;
999 }
1000 mdclog_write(MDCLOG_ERR, "error writing to CU a message, %s ", strerror(errno));
aa7133@att.com84bd3342020-03-16 18:04:57 +02001001 if (!peerInfo->isConnected) {
1002 mdclog_write(MDCLOG_ERR, "connection to CU %s is still in progress.", message.message.enodbName);
aa7133@att.com84bd3342020-03-16 18:04:57 +02001003 return -1;
1004 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001005 cleanHashEntry(peerInfo, m);
ss412g1a79bdf2019-10-24 12:03:05 +03001006 close(fd);
1007 char key[MAX_ENODB_NAME_SIZE * 2];
1008 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", message.message.enodbName,
1009 message.message.messageType);
1010 if (loglevel >= MDCLOG_DEBUG) {
1011 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
1012 }
aa7133@att.com96c9b882020-03-25 14:50:40 +02001013 auto tmp = m->find(key);
ss412g1a79bdf2019-10-24 12:03:05 +03001014 if (tmp) {
1015 free(tmp);
1016 }
1017 m->erase(key);
ss412g1a79bdf2019-10-24 12:03:05 +03001018 return -1;
1019 }
1020 message.message.direction = 'D';
1021 // send report.buffer of size
1022 buildJsonMessage(message);
1023
1024 if (loglevel >= MDCLOG_DEBUG) {
1025 mdclog_write(MDCLOG_DEBUG,
1026 "SCTP message for CU %s sent from %s",
1027 message.message.enodbName,
1028 __FUNCTION__);
1029 }
ss412g1a79bdf2019-10-24 12:03:05 +03001030 return 0;
1031 }
1032}
1033
1034/**
1035 *
1036 * @param message
1037 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001038 */
aa7133@att.combe92d712020-03-18 17:30:39 +02001039void getRequestMetaData(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03001040 message.message.asndata = rmrMessageBuffer.rcvMessage->payload;
1041 message.message.asnLength = rmrMessageBuffer.rcvMessage->len;
1042
1043 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1044 mdclog_write(MDCLOG_DEBUG, "Message from Xapp RAN name = %s message length = %ld",
1045 message.message.enodbName, (unsigned long) message.message.asnLength);
1046 }
ss412g1a79bdf2019-10-24 12:03:05 +03001047}
1048
1049
ss412g1a79bdf2019-10-24 12:03:05 +03001050
1051/**
1052 *
1053 * @param events
1054 * @param sctpMap
1055 * @param numOfMessages
1056 * @param rmrMessageBuffer
1057 * @param ts
ss412g1a79bdf2019-10-24 12:03:05 +03001058 * @return
1059 */
1060int receiveDataFromSctp(struct epoll_event *events,
1061 Sctp_Map_t *sctpMap,
1062 int &numOfMessages,
1063 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +02001064 struct timespec &ts) {
ss412g1a79bdf2019-10-24 12:03:05 +03001065 /* We have data on the fd waiting to be read. Read and display it.
1066 * We must read whatever data is available completely, as we are running
1067 * in edge-triggered mode and won't get a notification again for the same data. */
aa7133@att.com84bd3342020-03-16 18:04:57 +02001068 ReportingMessages_t message {};
1069 auto done = 0;
ss412g1a79bdf2019-10-24 12:03:05 +03001070 auto loglevel = mdclog_level_get();
aa7133@att.com84bd3342020-03-16 18:04:57 +02001071
ss412g1a79bdf2019-10-24 12:03:05 +03001072 // get the identity of the interface
aa7133@att.com84bd3342020-03-16 18:04:57 +02001073 message.peerInfo = (ConnectedCU_t *)events->data.ptr;
1074
ss412g1a79bdf2019-10-24 12:03:05 +03001075 struct timespec start{0, 0};
1076 struct timespec decodestart{0, 0};
1077 struct timespec end{0, 0};
1078
1079 E2AP_PDU_t *pdu = nullptr;
1080
ss412g1a79bdf2019-10-24 12:03:05 +03001081 while (true) {
1082 if (loglevel >= MDCLOG_DEBUG) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001083 mdclog_write(MDCLOG_DEBUG, "Start Read from SCTP %d fd", message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001084 clock_gettime(CLOCK_MONOTONIC, &start);
1085 }
1086 // read the buffer directly to rmr payload
1087 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1088 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
aa7133@att.com84bd3342020-03-16 18:04:57 +02001089 read(message.peerInfo->fileDescriptor, rmrMessageBuffer.sendMessage->payload, RECEIVE_SCTP_BUFFER_SIZE);
1090
ss412g1a79bdf2019-10-24 12:03:05 +03001091 if (loglevel >= MDCLOG_DEBUG) {
1092 mdclog_write(MDCLOG_DEBUG, "Finish Read from SCTP %d fd message length = %ld",
aa7133@att.com96c9b882020-03-25 14:50:40 +02001093 message.peerInfo->fileDescriptor, message.message.asnLength);
ss412g1a79bdf2019-10-24 12:03:05 +03001094 }
aa7133@att.com16c799b2020-03-24 18:01:51 +02001095
aa7133@att.com84bd3342020-03-16 18:04:57 +02001096 memcpy(message.message.enodbName, message.peerInfo->enodbName, sizeof(message.peerInfo->enodbName));
ss412g1a79bdf2019-10-24 12:03:05 +03001097 message.message.direction = 'U';
1098 message.message.time.tv_nsec = ts.tv_nsec;
1099 message.message.time.tv_sec = ts.tv_sec;
1100
1101 if (message.message.asnLength < 0) {
1102 if (errno == EINTR) {
1103 continue;
1104 }
1105 /* If errno == EAGAIN, that means we have read all
aa7133@att.com84bd3342020-03-16 18:04:57 +02001106 data. So goReportingMessages_t back to the main loop. */
ss412g1a79bdf2019-10-24 12:03:05 +03001107 if (errno != EAGAIN) {
1108 mdclog_write(MDCLOG_ERR, "Read error, %s ", strerror(errno));
1109 done = 1;
1110 } else if (loglevel >= MDCLOG_DEBUG) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001111 mdclog_write(MDCLOG_DEBUG, "EAGAIN - descriptor = %d", message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001112 }
1113 break;
1114 } else if (message.message.asnLength == 0) {
1115 /* End of file. The remote has closed the connection. */
1116 if (loglevel >= MDCLOG_INFO) {
1117 mdclog_write(MDCLOG_INFO, "END of File Closed connection - descriptor = %d",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001118 message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001119 }
1120 done = 1;
1121 break;
1122 }
1123
ss412g1a79bdf2019-10-24 12:03:05 +03001124 if (loglevel >= MDCLOG_DEBUG) {
swc58d4f5d2020-10-20 15:08:38 +09001125 char printBuffer[RECEIVE_SCTP_BUFFER_SIZE]{};
ss412g1a79bdf2019-10-24 12:03:05 +03001126 char *tmp = printBuffer;
1127 for (size_t i = 0; i < (size_t)message.message.asnLength; ++i) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001128 snprintf(tmp, 3, "%02x", message.message.asndata[i]);
ss412g1a79bdf2019-10-24 12:03:05 +03001129 tmp += 2;
1130 }
1131 printBuffer[message.message.asnLength] = 0;
1132 clock_gettime(CLOCK_MONOTONIC, &end);
1133 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 +02001134 message.peerInfo->enodbName, end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec);
ss412g1a79bdf2019-10-24 12:03:05 +03001135 mdclog_write(MDCLOG_DEBUG, "PDU buffer length = %ld, data = : %s", message.message.asnLength,
1136 printBuffer);
1137 clock_gettime(CLOCK_MONOTONIC, &decodestart);
1138 }
1139
aa7133@att.com7b437f72020-03-30 13:25:54 +03001140 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, (void **) &pdu,
ss412g1a79bdf2019-10-24 12:03:05 +03001141 message.message.asndata, message.message.asnLength);
1142 if (rval.code != RC_OK) {
1143 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2AP PDU from RAN : %s", rval.code,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001144 message.peerInfo->enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001145 break;
1146 }
1147
1148 if (loglevel >= MDCLOG_DEBUG) {
1149 clock_gettime(CLOCK_MONOTONIC, &end);
1150 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 +02001151 message.peerInfo->enodbName, end.tv_sec - decodestart.tv_sec, end.tv_nsec - decodestart.tv_nsec);
ss412g1a79bdf2019-10-24 12:03:05 +03001152 char *printBuffer;
1153 size_t size;
1154 FILE *stream = open_memstream(&printBuffer, &size);
1155 asn_fprint(stream, &asn_DEF_E2AP_PDU, pdu);
1156 mdclog_write(MDCLOG_DEBUG, "Encoding E2AP PDU past : %s", printBuffer);
1157 clock_gettime(CLOCK_MONOTONIC, &decodestart);
1158 }
1159
1160 switch (pdu->present) {
1161 case E2AP_PDU_PR_initiatingMessage: {//initiating message
aa7133@att.com7b437f72020-03-30 13:25:54 +03001162 asnInitiatingRequest(pdu, sctpMap,message, rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001163 break;
1164 }
1165 case E2AP_PDU_PR_successfulOutcome: { //successful outcome
aa7133@att.com7b437f72020-03-30 13:25:54 +03001166 asnSuccsesfulMsg(pdu, sctpMap, message, rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001167 break;
1168 }
1169 case E2AP_PDU_PR_unsuccessfulOutcome: { //Unsuccessful Outcome
aa7133@att.com7b437f72020-03-30 13:25:54 +03001170 asnUnSuccsesfulMsg(pdu, sctpMap, message, rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001171 break;
1172 }
1173 default:
1174 mdclog_write(MDCLOG_ERR, "Unknown index %d in E2AP PDU", pdu->present);
1175 break;
1176 }
1177 if (loglevel >= MDCLOG_DEBUG) {
1178 clock_gettime(CLOCK_MONOTONIC, &end);
1179 mdclog_write(MDCLOG_DEBUG,
1180 "After processing message and sent to rmr for : %s, Read time is : %ld seconds, %ld nanoseconds",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001181 message.peerInfo->enodbName, end.tv_sec - decodestart.tv_sec, end.tv_nsec - decodestart.tv_nsec);
ss412g1a79bdf2019-10-24 12:03:05 +03001182 }
1183 numOfMessages++;
ss412g1a79bdf2019-10-24 12:03:05 +03001184 if (pdu != nullptr) {
aa7133@att.com1fecb052020-03-29 09:44:09 +03001185 ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, pdu);
1186 //ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
aa7133@att.com9dff4f02020-03-29 09:55:22 +03001187 //pdu = nullptr;
ss412g1a79bdf2019-10-24 12:03:05 +03001188 }
ss412g1a79bdf2019-10-24 12:03:05 +03001189 }
ss412g1a79bdf2019-10-24 12:03:05 +03001190
1191 if (done) {
1192 if (loglevel >= MDCLOG_INFO) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001193 mdclog_write(MDCLOG_INFO, "Closed connection - descriptor = %d", message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001194 }
1195 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
1196 snprintf((char *)rmrMessageBuffer.sendMessage->payload,
aa7133@att.com96c9b882020-03-25 14:50:40 +02001197 256,
1198 "%s|CU disconnected unexpectedly",
1199 message.peerInfo->enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001200 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1201
1202 if (sendRequestToXapp(message,
1203 RIC_SCTP_CONNECTION_FAILURE,
aa7133@att.combe92d712020-03-18 17:30:39 +02001204 rmrMessageBuffer) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03001205 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
1206 }
1207
1208 /* Closing descriptor make epoll remove it from the set of descriptors which are monitored. */
aa7133@att.com84bd3342020-03-16 18:04:57 +02001209 close(message.peerInfo->fileDescriptor);
aa7133@att.combe92d712020-03-18 17:30:39 +02001210 cleanHashEntry((ConnectedCU_t *) events->data.ptr, sctpMap);
ss412g1a79bdf2019-10-24 12:03:05 +03001211 }
1212 if (loglevel >= MDCLOG_DEBUG) {
1213 clock_gettime(CLOCK_MONOTONIC, &end);
1214 mdclog_write(MDCLOG_DEBUG, "from receive SCTP to send RMR time is %ld seconds and %ld nanoseconds",
1215 end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec);
1216
1217 }
ss412g1a79bdf2019-10-24 12:03:05 +03001218 return 0;
1219}
1220
aa7133@att.com84bd3342020-03-16 18:04:57 +02001221static void buildAndsendSetupRequest(ReportingMessages_t &message,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001222 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001223 E2AP_PDU_t *pdu/*,
ss412g4aeeda22020-06-16 14:12:41 +03001224 string const &messageName,
1225 string const &ieName,
1226 vector<string> &functionsToAdd_v,
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001227 vector<string> &functionsToModified_v*/) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001228 auto logLevel = mdclog_level_get();
aa7133@att.com84bd3342020-03-16 18:04:57 +02001229 // now we can send the data to e2Mgr
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001230
ss412g4aeeda22020-06-16 14:12:41 +03001231 asn_enc_rval_t er;
1232 auto buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001233 unsigned char buffer[RECEIVE_SCTP_BUFFER_SIZE * 2];
ss412g4aeeda22020-06-16 14:12:41 +03001234 while (true) {
ss412g4aeeda22020-06-16 14:12:41 +03001235 er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu, buffer, buffer_size);
1236 if (er.encoded == -1) {
1237 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1238 return;
1239 } else if (er.encoded > (ssize_t) buffer_size) {
1240 buffer_size = er.encoded + 128;
1241 mdclog_write(MDCLOG_WARN, "Buffer of size %d is to small for %s. Reallocate buffer of size %d",
1242 (int) buffer_size,
1243 asn_DEF_E2AP_PDU.name, buffer_size);
1244 buffer_size = er.encoded + 128;
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001245// free(buffer);
ss412g4aeeda22020-06-16 14:12:41 +03001246 continue;
1247 }
1248 buffer[er.encoded] = '\0';
1249 break;
1250 }
1251 // encode to xml
1252
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001253 string res((char *)buffer);
1254 res.erase(std::remove(res.begin(), res.end(), '\n'), res.end());
1255 res.erase(std::remove(res.begin(), res.end(), '\t'), res.end());
1256 res.erase(std::remove(res.begin(), res.end(), ' '), res.end());
1257
1258// string res {};
1259// if (!functionsToAdd_v.empty() || !functionsToModified_v.empty()) {
1260// res = buildXmlData(messageName, ieName, functionsToAdd_v, functionsToModified_v, buffer, (size_t) er.encoded);
1261// }
ss412g4aeeda22020-06-16 14:12:41 +03001262 rmr_mbuf_t *rmrMsg;
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001263// if (res.length() == 0) {
1264// rmrMsg = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, buffer_size + 256);
1265// rmrMsg->len = snprintf((char *) rmrMsg->payload, RECEIVE_SCTP_BUFFER_SIZE * 2, "%s:%d|%s",
1266// message.peerInfo->sctpParams->myIP.c_str(),
1267// message.peerInfo->sctpParams->rmrPort,
1268// buffer);
1269// } else {
ss412g4aeeda22020-06-16 14:12:41 +03001270 rmrMsg = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, (int)res.length() + 256);
1271 rmrMsg->len = snprintf((char *) rmrMsg->payload, res.length() + 256, "%s:%d|%s",
1272 message.peerInfo->sctpParams->myIP.c_str(),
1273 message.peerInfo->sctpParams->rmrPort,
1274 res.c_str());
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001275// }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001276
ss412g4aeeda22020-06-16 14:12:41 +03001277 if (logLevel >= MDCLOG_DEBUG) {
1278 mdclog_write(MDCLOG_DEBUG, "Setup request of size %d :\n %s\n", rmrMsg->len, rmrMsg->payload);
1279 }
1280 // send to RMR
1281 rmrMsg->mtype = message.message.messageType;
1282 rmrMsg->state = 0;
1283 rmr_bytes2meid(rmrMsg, (unsigned char *) message.message.enodbName, strlen(message.message.enodbName));
aa7133@att.com84bd3342020-03-16 18:04:57 +02001284
ss412g4aeeda22020-06-16 14:12:41 +03001285 static unsigned char tx[32];
1286 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1287 rmr_bytes2xact(rmrMsg, tx, strlen((const char *) tx));
1288
1289 rmrMsg = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMsg);
1290 if (rmrMsg == nullptr) {
1291 mdclog_write(MDCLOG_ERR, "RMR failed to send returned nullptr");
1292 } else if (rmrMsg->state != 0) {
1293 char meid[RMR_MAX_MEID]{};
1294 if (rmrMsg->state == RMR_ERR_RETRY) {
1295 usleep(5);
1296 rmrMsg->state = 0;
1297 mdclog_write(MDCLOG_INFO, "RETRY sending Message %d to Xapp from %s",
1298 rmrMsg->mtype, rmr_get_meid(rmrMsg, (unsigned char *) meid));
1299 rmrMsg = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMsg);
1300 if (rmrMsg == nullptr) {
1301 mdclog_write(MDCLOG_ERR, "RMR failed send returned nullptr");
1302 } else if (rmrMsg->state != 0) {
1303 mdclog_write(MDCLOG_ERR,
1304 "RMR Retry failed %s sending request %d to Xapp from %s",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001305 translateRmrErrorMessages(rmrMsg->state).c_str(),
1306 rmrMsg->mtype,
1307 rmr_get_meid(rmrMsg, (unsigned char *) meid));
1308 }
ss412g4aeeda22020-06-16 14:12:41 +03001309 } else {
1310 mdclog_write(MDCLOG_ERR, "RMR failed: %s. sending request %d to Xapp from %s",
1311 translateRmrErrorMessages(rmrMsg->state).c_str(),
1312 rmrMsg->mtype,
1313 rmr_get_meid(rmrMsg, (unsigned char *) meid));
aa7133@att.combc110772020-03-25 09:45:14 +02001314 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001315 }
ss412g4aeeda22020-06-16 14:12:41 +03001316 message.peerInfo->gotSetup = true;
1317 buildJsonMessage(message);
1318 if (rmrMsg != nullptr) {
1319 rmr_free_msg(rmrMsg);
1320 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001321}
aa7133@att.comc5a15202020-04-05 19:57:01 +03001322
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001323#if 0
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001324int RAN_Function_list_To_Vector(RANfunctions_List_t& list, vector <string> &runFunXML_v) {
1325 auto index = 0;
1326 runFunXML_v.clear();
1327 for (auto j = 0; j < list.list.count; j++) {
1328 auto *raNfunctionItemIEs = (RANfunction_ItemIEs_t *)list.list.array[j];
1329 if (raNfunctionItemIEs->id == ProtocolIE_ID_id_RANfunction_Item &&
1330 (raNfunctionItemIEs->value.present == RANfunction_ItemIEs__value_PR_RANfunction_Item)) {
1331 // encode to xml
1332 E2SM_gNB_NRT_RANfunction_Definition_t *ranFunDef = nullptr;
1333 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER,
1334 &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition,
1335 (void **)&ranFunDef,
1336 raNfunctionItemIEs->value.choice.RANfunction_Item.ranFunctionDefinition.buf,
1337 raNfunctionItemIEs->value.choice.RANfunction_Item.ranFunctionDefinition.size);
1338 if (rval.code != RC_OK) {
1339 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2SM message from : %s",
1340 rval.code,
1341 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name);
1342 return -1;
1343 }
1344
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001345 auto xml_buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
1346 unsigned char xml_buffer[RECEIVE_SCTP_BUFFER_SIZE * 2];
ss412g4aeeda22020-06-16 14:12:41 +03001347 memset(xml_buffer, 0, RECEIVE_SCTP_BUFFER_SIZE * 2);
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001348 // encode to xml
1349 auto er = asn_encode_to_buffer(nullptr,
1350 ATS_BASIC_XER,
1351 &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition,
1352 ranFunDef,
1353 xml_buffer,
1354 xml_buffer_size);
1355 if (er.encoded == -1) {
1356 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s",
1357 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name,
1358 strerror(errno));
1359 } else if (er.encoded > (ssize_t)xml_buffer_size) {
1360 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
1361 (int) xml_buffer_size,
1362 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name, __func__, __LINE__);
1363 } else {
1364 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1365 mdclog_write(MDCLOG_DEBUG, "Encoding E2SM %s PDU number %d : %s",
1366 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name,
1367 index++,
1368 xml_buffer);
1369 }
ss412g4aeeda22020-06-16 14:12:41 +03001370
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001371 string runFuncs = (char *)(xml_buffer);
1372 runFunXML_v.emplace_back(runFuncs);
1373 }
1374 }
1375 }
1376 return 0;
1377}
aa7133@att.comc5a15202020-04-05 19:57:01 +03001378
ss412g4aeeda22020-06-16 14:12:41 +03001379int collectServiceUpdate_RequestData(E2AP_PDU_t *pdu,
1380 Sctp_Map_t *sctpMap,
1381 ReportingMessages_t &message,
1382 vector <string> &RANfunctionsAdded_v,
1383 vector <string> &RANfunctionsModified_v) {
aa7133@att.comc5a15202020-04-05 19:57:01 +03001384 memset(message.peerInfo->enodbName, 0 , MAX_ENODB_NAME_SIZE);
ss412g4aeeda22020-06-16 14:12:41 +03001385 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICserviceUpdate.protocolIEs.list.count; i++) {
1386 auto *ie = pdu->choice.initiatingMessage->value.choice.RICserviceUpdate.protocolIEs.list.array[i];
1387 if (ie->id == ProtocolIE_ID_id_RANfunctionsAdded) {
1388 if (ie->value.present == RICserviceUpdate_IEs__value_PR_RANfunctionsID_List) {
aa7133@att.comc5a15202020-04-05 19:57:01 +03001389 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001390 mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1391 ie->value.choice.RANfunctions_List.list.count);
aa7133@att.comc5a15202020-04-05 19:57:01 +03001392 }
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001393 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsAdded_v) != 0 ) {
1394 return -1;
1395 }
1396 }
1397 } else if (ie->id == ProtocolIE_ID_id_RANfunctionsModified) {
ss412g4aeeda22020-06-16 14:12:41 +03001398 if (ie->value.present == RICserviceUpdate_IEs__value_PR_RANfunctions_List) {
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001399 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1400 mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1401 ie->value.choice.RANfunctions_List.list.count);
1402 }
1403 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsModified_v) != 0 ) {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001404 return -1;
aa7133@att.comc5a15202020-04-05 19:57:01 +03001405 }
1406 }
1407 }
1408 }
1409 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1410 mdclog_write(MDCLOG_DEBUG, "Run function vector have %ld entries",
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001411 RANfunctionsAdded_v.size());
aa7133@att.comc5a15202020-04-05 19:57:01 +03001412 }
1413 return 0;
1414}
ss412g4aeeda22020-06-16 14:12:41 +03001415
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001416#endif
ss412g4aeeda22020-06-16 14:12:41 +03001417
1418
1419void buildPrometheuslist(ConnectedCU_t *peerInfo, Family<Counter> *prometheusFamily) {
aa7133@att.coma4c80012020-06-24 16:36:47 +03001420 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"SetupRequest", "Messages"}});
1421 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"SetupRequest", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001422
aa7133@att.coma4c80012020-06-24 16:36:47 +03001423 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ErrorIndication", "Messages"}});
1424 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ErrorIndication", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001425
aa7133@att.coma4c80012020-06-24 16:36:47 +03001426 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_RICindication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICindication", "Messages"}});
1427 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_RICindication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICindication", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001428
aa7133@att.coma4c80012020-06-24 16:36:47 +03001429 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetRequest", "Messages"}});
1430 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetRequest", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001431
aa7133@att.coma4c80012020-06-24 16:36:47 +03001432 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICserviceUpdate", "Messages"}});
1433 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICserviceUpdate", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001434 // ---------------------------------------------
aa7133@att.coma4c80012020-06-24 16:36:47 +03001435 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetACK", "Messages"}});
1436 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001437
aa7133@att.coma4c80012020-06-24 16:36:47 +03001438 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolACK", "Messages"}});
1439 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001440
aa7133@att.coma4c80012020-06-24 16:36:47 +03001441 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionACK", "Messages"}});
1442 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001443
aa7133@att.coma4c80012020-06-24 16:36:47 +03001444 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteACK", "Messages"}});
1445 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001446 //-------------------------------------------------------------
1447
aa7133@att.coma4c80012020-06-24 16:36:47 +03001448 peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolFailure", "Messages"}});
1449 peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001450
aa7133@att.coma4c80012020-06-24 16:36:47 +03001451 peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionFailure", "Messages"}});
1452 peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001453
aa7133@att.coma4c80012020-06-24 16:36:47 +03001454 peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteFailure", "Messages"}});
1455 peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001456
1457 //====================================================================================
aa7133@att.coma4c80012020-06-24 16:36:47 +03001458 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ErrorIndication", "Messages"}});
1459 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ErrorIndication", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001460
aa7133@att.coma4c80012020-06-24 16:36:47 +03001461 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetRequest", "Messages"}});
1462 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetRequest", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001463
aa7133@att.coma4c80012020-06-24 16:36:47 +03001464 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICcontrol", "Messages"}});
1465 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICcontrol", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001466
aa7133@att.coma4c80012020-06-24 16:36:47 +03001467 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICserviceQuery)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceQuery", "Messages"}});
1468 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICserviceQuery)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceQuery", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001469
aa7133@att.coma4c80012020-06-24 16:36:47 +03001470 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscription", "Messages"}});
1471 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscription", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001472
aa7133@att.coma4c80012020-06-24 16:36:47 +03001473 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscriptionDelete", "Messages"}});
1474 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscriptionDelete", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001475 //---------------------------------------------------------------------------------------------------------
aa7133@att.coma4c80012020-06-24 16:36:47 +03001476 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupResponse", "Messages"}});
1477 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupResponse", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001478
aa7133@att.coma4c80012020-06-24 16:36:47 +03001479 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetACK", "Messages"}});
1480 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001481
aa7133@att.coma4c80012020-06-24 16:36:47 +03001482 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateResponse", "Messages"}});
1483 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateResponse", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001484 //----------------------------------------------------------------------------------------------------------------
aa7133@att.coma4c80012020-06-24 16:36:47 +03001485 peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupRequestFailure", "Messages"}});
1486 peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupRequestFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001487
aa7133@att.coma4c80012020-06-24 16:36:47 +03001488 peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateFailure", "Messages"}});
1489 peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001490}
1491/**
1492 *
1493 * @param pdu
1494 * @param sctpMap
1495 * @param message
1496 * @param RANfunctionsAdded_v
1497 * @return
1498 */
1499int collectSetupRequestData(E2AP_PDU_t *pdu,
1500 Sctp_Map_t *sctpMap,
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001501 ReportingMessages_t &message /*, vector <string> &RANfunctionsAdded_v*/) {
ss412g4aeeda22020-06-16 14:12:41 +03001502 memset(message.peerInfo->enodbName, 0 , MAX_ENODB_NAME_SIZE);
1503 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.count; i++) {
1504 auto *ie = pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.array[i];
1505 if (ie->id == ProtocolIE_ID_id_GlobalE2node_ID) {
1506 // get the ran name for meid
1507 if (ie->value.present == E2setupRequestIEs__value_PR_GlobalE2node_ID) {
1508 if (buildRanName(message.peerInfo->enodbName, ie) < 0) {
1509 mdclog_write(MDCLOG_ERR, "Bad param in E2setupRequestIEs GlobalE2node_ID.\n");
1510 // no mesage will be sent
1511 return -1;
1512 }
1513
1514 memcpy(message.message.enodbName, message.peerInfo->enodbName, strlen(message.peerInfo->enodbName));
1515 sctpMap->setkey(message.message.enodbName, message.peerInfo);
1516 }
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001517 } /*else if (ie->id == ProtocolIE_ID_id_RANfunctionsAdded) {
ss412g4aeeda22020-06-16 14:12:41 +03001518 if (ie->value.present == E2setupRequestIEs__value_PR_RANfunctions_List) {
1519 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1520 mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1521 ie->value.choice.RANfunctions_List.list.count);
1522 }
1523 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsAdded_v) != 0 ) {
1524 return -1;
1525 }
1526 }
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001527 } */
ss412g4aeeda22020-06-16 14:12:41 +03001528 }
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001529// if (mdclog_level_get() >= MDCLOG_DEBUG) {
1530// mdclog_write(MDCLOG_DEBUG, "Run function vector have %ld entries",
1531// RANfunctionsAdded_v.size());
1532// }
ss412g4aeeda22020-06-16 14:12:41 +03001533 return 0;
1534}
1535
1536int XML_From_PER(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
1537 E2AP_PDU_t *pdu = nullptr;
1538
1539 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1540 mdclog_write(MDCLOG_DEBUG, "got PER message of size %d is:%s",
1541 rmrMessageBuffer.sendMessage->len, rmrMessageBuffer.sendMessage->payload);
1542 }
1543 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, (void **) &pdu,
1544 rmrMessageBuffer.sendMessage->payload, rmrMessageBuffer.sendMessage->len);
1545 if (rval.code != RC_OK) {
1546 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) setup response from E2MGR : %s",
1547 rval.code,
1548 message.message.enodbName);
1549 return -1;
1550 }
1551
1552 int buff_size = RECEIVE_XAPP_BUFFER_SIZE;
1553 auto er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu,
1554 rmrMessageBuffer.sendMessage->payload, buff_size);
1555 if (er.encoded == -1) {
1556 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1557 return -1;
1558 } else if (er.encoded > (ssize_t)buff_size) {
1559 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
1560 (int)rmrMessageBuffer.sendMessage->len,
1561 asn_DEF_E2AP_PDU.name,
1562 __func__,
1563 __LINE__);
1564 return -1;
1565 }
1566 rmrMessageBuffer.sendMessage->len = er.encoded;
1567 return 0;
1568
1569}
1570
ss412g1a79bdf2019-10-24 12:03:05 +03001571/**
1572 *
1573 * @param pdu
1574 * @param message
1575 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001576 */
1577void asnInitiatingRequest(E2AP_PDU_t *pdu,
aa7133@att.com7b437f72020-03-30 13:25:54 +03001578 Sctp_Map_t *sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +03001579 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +02001580 RmrMessagesBuffer_t &rmrMessageBuffer) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001581 auto logLevel = mdclog_level_get();
ss412g1a79bdf2019-10-24 12:03:05 +03001582 auto procedureCode = ((InitiatingMessage_t *) pdu->choice.initiatingMessage)->procedureCode;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001583 if (logLevel >= MDCLOG_DEBUG) {
1584 mdclog_write(MDCLOG_DEBUG, "Initiating message %ld\n", procedureCode);
ss412g1a79bdf2019-10-24 12:03:05 +03001585 }
1586 switch (procedureCode) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001587 case ProcedureCode_id_E2setup: {
1588 if (logLevel >= MDCLOG_DEBUG) {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001589 mdclog_write(MDCLOG_DEBUG, "Got E2setup");
aa7133@att.com84bd3342020-03-16 18:04:57 +02001590 }
1591
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001592// vector <string> RANfunctionsAdded_v;
1593// vector <string> RANfunctionsModified_v;
1594// RANfunctionsAdded_v.clear();
1595// RANfunctionsModified_v.clear();
1596 if (collectSetupRequestData(pdu, sctpMap, message) != 0) {
aa7133@att.com7b437f72020-03-30 13:25:54 +03001597 break;
1598 }
1599
ss412g4aeeda22020-06-16 14:12:41 +03001600 buildPrometheuslist(message.peerInfo, message.peerInfo->sctpParams->prometheusFamily);
1601
1602 string messageName("E2setupRequest");
1603 string ieName("E2setupRequestIEs");
1604 message.message.messageType = RIC_E2_SETUP_REQ;
aa7133@att.coma4c80012020-06-24 16:36:47 +03001605 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
1606 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment((double)message.message.asnLength);
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001607 buildAndsendSetupRequest(message, rmrMessageBuffer, pdu);
ss412g1a79bdf2019-10-24 12:03:05 +03001608 break;
1609 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001610 case ProcedureCode_id_RICserviceUpdate: {
1611 if (logLevel >= MDCLOG_DEBUG) {
1612 mdclog_write(MDCLOG_DEBUG, "Got RICserviceUpdate %s", message.message.enodbName);
1613 }
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001614// vector <string> RANfunctionsAdded_v;
1615// vector <string> RANfunctionsModified_v;
1616// RANfunctionsAdded_v.clear();
1617// RANfunctionsModified_v.clear();
1618// if (collectServiceUpdate_RequestData(pdu, sctpMap, message,
1619// RANfunctionsAdded_v, RANfunctionsModified_v) != 0) {
1620// break;
1621// }
ss412g4aeeda22020-06-16 14:12:41 +03001622
1623 string messageName("RICserviceUpdate");
1624 string ieName("RICserviceUpdateIEs");
1625 message.message.messageType = RIC_SERVICE_UPDATE;
aa7133@att.coma4c80012020-06-24 16:36:47 +03001626 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
1627 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment((double)message.message.asnLength);
ss412g4aeeda22020-06-16 14:12:41 +03001628
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001629 buildAndsendSetupRequest(message, rmrMessageBuffer, pdu);
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001630 break;
1631 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001632 case ProcedureCode_id_ErrorIndication: {
1633 if (logLevel >= MDCLOG_DEBUG) {
1634 mdclog_write(MDCLOG_DEBUG, "Got ErrorIndication %s", message.message.enodbName);
1635 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03001636 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_ErrorIndication]->Increment();
1637 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_ErrorIndication]->Increment((double)message.message.asnLength);
aa7133@att.combe92d712020-03-18 17:30:39 +02001638 if (sendRequestToXapp(message, RIC_ERROR_INDICATION, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001639 mdclog_write(MDCLOG_ERR, "RIC_ERROR_INDICATION failed to send to xAPP");
ss412g1a79bdf2019-10-24 12:03:05 +03001640 }
1641 break;
1642 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001643 case ProcedureCode_id_Reset: {
1644 if (logLevel >= MDCLOG_DEBUG) {
1645 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001646 }
ss412g4aeeda22020-06-16 14:12:41 +03001647
aa7133@att.coma4c80012020-06-24 16:36:47 +03001648 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
1649 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment((double)message.message.asnLength);
ss412g4aeeda22020-06-16 14:12:41 +03001650 if (XML_From_PER(message, rmrMessageBuffer) < 0) {
1651 break;
ss412g1a79bdf2019-10-24 12:03:05 +03001652 }
ss412g4aeeda22020-06-16 14:12:41 +03001653
1654 if (sendRequestToXapp(message, RIC_E2_RESET_REQ, rmrMessageBuffer) != 0) {
1655 mdclog_write(MDCLOG_ERR, "RIC_E2_RESET_REQ message failed to send to xAPP");
aa7133@att.com84bd3342020-03-16 18:04:57 +02001656 }
1657 break;
1658 }
1659 case ProcedureCode_id_RICindication: {
1660 if (logLevel >= MDCLOG_DEBUG) {
1661 mdclog_write(MDCLOG_DEBUG, "Got RICindication %s", message.message.enodbName);
1662 }
1663 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.count; i++) {
ss412g1a79bdf2019-10-24 12:03:05 +03001664 auto messageSent = false;
1665 RICindication_IEs_t *ie = pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.array[i];
aa7133@att.com84bd3342020-03-16 18:04:57 +02001666 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03001667 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1668 }
1669 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001670 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03001671 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1672 }
1673 if (ie->value.present == RICindication_IEs__value_PR_RICrequestID) {
1674 static unsigned char tx[32];
1675 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_INDICATION;
1676 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1677 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
ss412g3bac2da2020-01-05 11:52:19 +02001678 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001679 (unsigned char *)message.message.enodbName,
1680 strlen(message.message.enodbName));
ss412g1a79bdf2019-10-24 12:03:05 +03001681 rmrMessageBuffer.sendMessage->state = 0;
aa7133@att.comc5a15202020-04-05 19:57:01 +03001682 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
1683
1684 //ie->value.choice.RICrequestID.ricInstanceID;
ss412g3bac2da2020-01-05 11:52:19 +02001685 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.comc5a15202020-04-05 19:57:01 +03001686 mdclog_write(MDCLOG_DEBUG, "sub id = %d, mtype = %d, ric instance id %ld, requestor id = %ld",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001687 rmrMessageBuffer.sendMessage->sub_id,
aa7133@att.comc5a15202020-04-05 19:57:01 +03001688 rmrMessageBuffer.sendMessage->mtype,
1689 ie->value.choice.RICrequestID.ricInstanceID,
1690 ie->value.choice.RICrequestID.ricRequestorID);
ss412g3bac2da2020-01-05 11:52:19 +02001691 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03001692 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICindication]->Increment();
1693 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICindication]->Increment((double)message.message.asnLength);
aa7133@att.combe92d712020-03-18 17:30:39 +02001694 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03001695 messageSent = true;
1696 } else {
1697 mdclog_write(MDCLOG_ERR, "RIC request id missing illigal request");
1698 }
1699 }
1700 if (messageSent) {
1701 break;
1702 }
1703 }
1704 break;
1705 }
ss412g1a79bdf2019-10-24 12:03:05 +03001706 default: {
1707 mdclog_write(MDCLOG_ERR, "Undefined or not supported message = %ld", procedureCode);
1708 message.message.messageType = 0; // no RMR message type yet
1709
1710 buildJsonMessage(message);
1711
1712 break;
1713 }
1714 }
ss412g1a79bdf2019-10-24 12:03:05 +03001715}
1716
1717/**
1718 *
1719 * @param pdu
1720 * @param message
ss412g1a79bdf2019-10-24 12:03:05 +03001721 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001722 */
aa7133@att.com7b437f72020-03-30 13:25:54 +03001723void asnSuccsesfulMsg(E2AP_PDU_t *pdu,
1724 Sctp_Map_t *sctpMap,
1725 ReportingMessages_t &message,
1726 RmrMessagesBuffer_t &rmrMessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03001727 auto procedureCode = pdu->choice.successfulOutcome->procedureCode;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001728 auto logLevel = mdclog_level_get();
1729 if (logLevel >= MDCLOG_INFO) {
ss412g1a79bdf2019-10-24 12:03:05 +03001730 mdclog_write(MDCLOG_INFO, "Successful Outcome %ld", procedureCode);
1731 }
1732 switch (procedureCode) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001733 case ProcedureCode_id_Reset: {
1734 if (logLevel >= MDCLOG_DEBUG) {
1735 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001736 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03001737 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
1738 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment((double)message.message.asnLength);
ss412g4aeeda22020-06-16 14:12:41 +03001739 if (XML_From_PER(message, rmrMessageBuffer) < 0) {
1740 break;
1741 }
1742 if (sendRequestToXapp(message, RIC_E2_RESET_RESP, rmrMessageBuffer) != 0) {
1743 mdclog_write(MDCLOG_ERR, "RIC_E2_RESET_RESP message failed to send to xAPP");
ss412g1a79bdf2019-10-24 12:03:05 +03001744 }
1745 break;
1746 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001747 case ProcedureCode_id_RICcontrol: {
1748 if (logLevel >= MDCLOG_DEBUG) {
1749 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001750 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001751 for (auto i = 0;
ss412g1a79bdf2019-10-24 12:03:05 +03001752 i < pdu->choice.successfulOutcome->value.choice.RICcontrolAcknowledge.protocolIEs.list.count; i++) {
1753 auto messageSent = false;
1754 RICcontrolAcknowledge_IEs_t *ie = pdu->choice.successfulOutcome->value.choice.RICcontrolAcknowledge.protocolIEs.list.array[i];
1755 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1756 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1757 }
1758 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
1759 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1760 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1761 }
1762 if (ie->value.present == RICcontrolAcknowledge_IEs__value_PR_RICrequestID) {
1763 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_CONTROL_ACK;
1764 rmrMessageBuffer.sendMessage->state = 0;
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001765// rmrMessageBuffer.sendMessage->sub_id = (int) ie->value.choice.RICrequestID.ricRequestorID;
1766 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
1767
ss412g1a79bdf2019-10-24 12:03:05 +03001768 static unsigned char tx[32];
1769 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1770 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
ss412g3bac2da2020-01-05 11:52:19 +02001771 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001772 (unsigned char *)message.message.enodbName,
1773 strlen(message.message.enodbName));
ss412g1a79bdf2019-10-24 12:03:05 +03001774
aa7133@att.coma4c80012020-06-24 16:36:47 +03001775 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
1776 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment((double)message.message.asnLength);
aa7133@att.combe92d712020-03-18 17:30:39 +02001777 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03001778 messageSent = true;
1779 } else {
1780 mdclog_write(MDCLOG_ERR, "RIC request id missing illigal request");
1781 }
1782 }
1783 if (messageSent) {
1784 break;
1785 }
1786 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001787
1788 break;
1789 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001790 case ProcedureCode_id_RICsubscription: {
1791 if (logLevel >= MDCLOG_DEBUG) {
1792 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
1793 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03001794 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
1795 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment((double)message.message.asnLength);
aa7133@att.combe92d712020-03-18 17:30:39 +02001796 if (sendRequestToXapp(message, RIC_SUB_RESP, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001797 mdclog_write(MDCLOG_ERR, "Subscription successful message failed to send to xAPP");
1798 }
1799 break;
1800 }
1801 case ProcedureCode_id_RICsubscriptionDelete: {
1802 if (logLevel >= MDCLOG_DEBUG) {
1803 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
1804 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03001805 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
1806 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment((double)message.message.asnLength);
aa7133@att.combe92d712020-03-18 17:30:39 +02001807 if (sendRequestToXapp(message, RIC_SUB_DEL_RESP, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001808 mdclog_write(MDCLOG_ERR, "Subscription delete successful message failed to send to xAPP");
1809 }
ss412g1a79bdf2019-10-24 12:03:05 +03001810 break;
1811 }
1812 default: {
1813 mdclog_write(MDCLOG_WARN, "Undefined or not supported message = %ld", procedureCode);
1814 message.message.messageType = 0; // no RMR message type yet
1815 buildJsonMessage(message);
1816
1817 break;
1818 }
1819 }
ss412g1a79bdf2019-10-24 12:03:05 +03001820}
1821
1822/**
1823 *
1824 * @param pdu
1825 * @param message
ss412g1a79bdf2019-10-24 12:03:05 +03001826 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001827 */
1828void asnUnSuccsesfulMsg(E2AP_PDU_t *pdu,
aa7133@att.com7b437f72020-03-30 13:25:54 +03001829 Sctp_Map_t *sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +03001830 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +02001831 RmrMessagesBuffer_t &rmrMessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03001832 auto procedureCode = pdu->choice.unsuccessfulOutcome->procedureCode;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001833 auto logLevel = mdclog_level_get();
1834 if (logLevel >= MDCLOG_INFO) {
ss412g1a79bdf2019-10-24 12:03:05 +03001835 mdclog_write(MDCLOG_INFO, "Unsuccessful Outcome %ld", procedureCode);
1836 }
1837 switch (procedureCode) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001838 case ProcedureCode_id_RICcontrol: {
1839 if (logLevel >= MDCLOG_DEBUG) {
1840 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001841 }
1842 for (int i = 0;
1843 i < pdu->choice.unsuccessfulOutcome->value.choice.RICcontrolFailure.protocolIEs.list.count; i++) {
1844 auto messageSent = false;
1845 RICcontrolFailure_IEs_t *ie = pdu->choice.unsuccessfulOutcome->value.choice.RICcontrolFailure.protocolIEs.list.array[i];
aa7133@att.com84bd3342020-03-16 18:04:57 +02001846 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03001847 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1848 }
1849 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001850 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03001851 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1852 }
1853 if (ie->value.present == RICcontrolFailure_IEs__value_PR_RICrequestID) {
1854 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_CONTROL_FAILURE;
1855 rmrMessageBuffer.sendMessage->state = 0;
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001856// rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricRequestorID;
1857 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
ss412g1a79bdf2019-10-24 12:03:05 +03001858 static unsigned char tx[32];
1859 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1860 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
aa7133@att.com84bd3342020-03-16 18:04:57 +02001861 rmr_bytes2meid(rmrMessageBuffer.sendMessage, (unsigned char *) message.message.enodbName,
1862 strlen(message.message.enodbName));
aa7133@att.coma4c80012020-06-24 16:36:47 +03001863 message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
1864 message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment((double)message.message.asnLength);
aa7133@att.combe92d712020-03-18 17:30:39 +02001865 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03001866 messageSent = true;
1867 } else {
1868 mdclog_write(MDCLOG_ERR, "RIC request id missing illigal request");
1869 }
1870 }
1871 if (messageSent) {
1872 break;
1873 }
1874 }
1875 break;
1876 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001877 case ProcedureCode_id_RICsubscription: {
1878 if (logLevel >= MDCLOG_DEBUG) {
1879 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
1880 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03001881 message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
1882 message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment((double)message.message.asnLength);
aa7133@att.combe92d712020-03-18 17:30:39 +02001883 if (sendRequestToXapp(message, RIC_SUB_FAILURE, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001884 mdclog_write(MDCLOG_ERR, "Subscription unsuccessful message failed to send to xAPP");
1885 }
1886 break;
1887 }
1888 case ProcedureCode_id_RICsubscriptionDelete: {
1889 if (logLevel >= MDCLOG_DEBUG) {
1890 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
1891 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03001892 message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
1893 message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment((double)message.message.asnLength);
ss412g4aeeda22020-06-16 14:12:41 +03001894 if (sendRequestToXapp(message, RIC_SUB_FAILURE, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001895 mdclog_write(MDCLOG_ERR, "Subscription Delete unsuccessful message failed to send to xAPP");
1896 }
1897 break;
1898 }
ss412g1a79bdf2019-10-24 12:03:05 +03001899 default: {
1900 mdclog_write(MDCLOG_WARN, "Undefined or not supported message = %ld", procedureCode);
1901 message.message.messageType = 0; // no RMR message type yet
1902
1903 buildJsonMessage(message);
1904
1905 break;
1906 }
1907 }
ss412g1a79bdf2019-10-24 12:03:05 +03001908}
1909
1910/**
1911 *
1912 * @param message
1913 * @param requestId
1914 * @param rmrMmessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001915 * @return
1916 */
1917int sendRequestToXapp(ReportingMessages_t &message,
1918 int requestId,
aa7133@att.combe92d712020-03-18 17:30:39 +02001919 RmrMessagesBuffer_t &rmrMmessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03001920 rmr_bytes2meid(rmrMmessageBuffer.sendMessage,
1921 (unsigned char *)message.message.enodbName,
1922 strlen(message.message.enodbName));
1923 message.message.messageType = rmrMmessageBuffer.sendMessage->mtype = requestId;
1924 rmrMmessageBuffer.sendMessage->state = 0;
1925 static unsigned char tx[32];
1926 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1927 rmr_bytes2xact(rmrMmessageBuffer.sendMessage, tx, strlen((const char *) tx));
1928
aa7133@att.combe92d712020-03-18 17:30:39 +02001929 auto rc = sendRmrMessage(rmrMmessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03001930 return rc;
1931}
1932
ss412g4aeeda22020-06-16 14:12:41 +03001933/**
1934 *
1935 * @param pSctpParams
1936 */
aa7133@att.combe92d712020-03-18 17:30:39 +02001937void getRmrContext(sctp_params_t &pSctpParams) {
ss412g3bac2da2020-01-05 11:52:19 +02001938 pSctpParams.rmrCtx = nullptr;
aa7133@att.comab0e0f42020-04-06 17:13:58 +03001939 pSctpParams.rmrCtx = rmr_init(pSctpParams.rmrAddress, RECEIVE_XAPP_BUFFER_SIZE, RMRFL_NONE);
ss412g3bac2da2020-01-05 11:52:19 +02001940 if (pSctpParams.rmrCtx == nullptr) {
1941 mdclog_write(MDCLOG_ERR, "Failed to initialize RMR");
ss412g3bac2da2020-01-05 11:52:19 +02001942 return;
ss412g1a79bdf2019-10-24 12:03:05 +03001943 }
1944
ss412g3bac2da2020-01-05 11:52:19 +02001945 rmr_set_stimeout(pSctpParams.rmrCtx, 0); // disable retries for any send operation
ss412g1a79bdf2019-10-24 12:03:05 +03001946 // we need to find that routing table exist and we can run
1947 if (mdclog_level_get() >= MDCLOG_INFO) {
1948 mdclog_write(MDCLOG_INFO, "We are after RMR INIT wait for RMR_Ready");
1949 }
1950 int rmrReady = 0;
1951 int count = 0;
1952 while (!rmrReady) {
ss412g3bac2da2020-01-05 11:52:19 +02001953 if ((rmrReady = rmr_ready(pSctpParams.rmrCtx)) == 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03001954 sleep(1);
1955 }
1956 count++;
1957 if (count % 60 == 0) {
1958 mdclog_write(MDCLOG_INFO, "waiting to RMR ready state for %d seconds", count);
1959 }
1960 }
1961 if (mdclog_level_get() >= MDCLOG_INFO) {
1962 mdclog_write(MDCLOG_INFO, "RMR running");
1963 }
ss412g3bac2da2020-01-05 11:52:19 +02001964 rmr_init_trace(pSctpParams.rmrCtx, 200);
1965 // get the RMR fd for the epoll
1966 pSctpParams.rmrListenFd = rmr_get_rcvfd(pSctpParams.rmrCtx);
1967 struct epoll_event event{};
1968 // add RMR fd to epoll
1969 event.events = (EPOLLIN);
1970 event.data.fd = pSctpParams.rmrListenFd;
1971 // add listening RMR FD to epoll
1972 if (epoll_ctl(pSctpParams.epoll_fd, EPOLL_CTL_ADD, pSctpParams.rmrListenFd, &event)) {
1973 mdclog_write(MDCLOG_ERR, "Failed to add RMR descriptor to epoll");
1974 close(pSctpParams.rmrListenFd);
1975 rmr_close(pSctpParams.rmrCtx);
1976 pSctpParams.rmrCtx = nullptr;
1977 }
ss412g1a79bdf2019-10-24 12:03:05 +03001978}
1979
ss412g4aeeda22020-06-16 14:12:41 +03001980/**
1981 *
1982 * @param message
1983 * @param rmrMessageBuffer
1984 * @return
1985 */
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001986int PER_FromXML(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
aa7133@att.com07e256f2020-04-20 15:59:51 +03001987 E2AP_PDU_t *pdu = nullptr;
aa7133@att.com1c4f50f2020-03-30 14:30:52 +03001988
1989 if (mdclog_level_get() >= MDCLOG_DEBUG) {
ss412g4aeeda22020-06-16 14:12:41 +03001990 mdclog_write(MDCLOG_DEBUG, "got xml Format data from xApp of size %d is:%s",
aa7133@att.com07e256f2020-04-20 15:59:51 +03001991 rmrMessageBuffer.rcvMessage->len, rmrMessageBuffer.rcvMessage->payload);
aa7133@att.com1c4f50f2020-03-30 14:30:52 +03001992 }
aa7133@att.com1fecb052020-03-29 09:44:09 +03001993 auto rval = asn_decode(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, (void **) &pdu,
1994 rmrMessageBuffer.rcvMessage->payload, rmrMessageBuffer.rcvMessage->len);
1995 if (rval.code != RC_OK) {
aa7133@att.com1c4f50f2020-03-30 14:30:52 +03001996 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) setup response from E2MGR : %s",
aa7133@att.com1fecb052020-03-29 09:44:09 +03001997 rval.code,
1998 message.message.enodbName);
1999 return -1;
2000 }
2001
aa7133@att.coma0f0db62020-04-01 16:52:50 +03002002 int buff_size = RECEIVE_XAPP_BUFFER_SIZE;
2003 auto er = asn_encode_to_buffer(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, pdu,
2004 rmrMessageBuffer.rcvMessage->payload, buff_size);
aa7133@att.com1fecb052020-03-29 09:44:09 +03002005 if (er.encoded == -1) {
2006 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
2007 return -1;
aa7133@att.coma0f0db62020-04-01 16:52:50 +03002008 } else if (er.encoded > (ssize_t)buff_size) {
2009 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 +03002010 (int)rmrMessageBuffer.rcvMessage->len,
aa7133@att.coma0f0db62020-04-01 16:52:50 +03002011 asn_DEF_E2AP_PDU.name,
2012 __func__,
2013 __LINE__);
aa7133@att.com1fecb052020-03-29 09:44:09 +03002014 return -1;
2015 }
aa7133@att.coma0f0db62020-04-01 16:52:50 +03002016 rmrMessageBuffer.rcvMessage->len = er.encoded;
aa7133@att.com1fecb052020-03-29 09:44:09 +03002017 return 0;
2018}
2019
ss412g1a79bdf2019-10-24 12:03:05 +03002020/**
2021 *
ss412g1a79bdf2019-10-24 12:03:05 +03002022 * @param sctpMap
2023 * @param rmrMessageBuffer
2024 * @param ts
ss412g1a79bdf2019-10-24 12:03:05 +03002025 * @return
2026 */
aa7133@att.com6680c3b2020-03-22 12:06:58 +02002027int receiveXappMessages(Sctp_Map_t *sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +03002028 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +02002029 struct timespec &ts) {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002030 int loglevel = mdclog_level_get();
ss412g1a79bdf2019-10-24 12:03:05 +03002031 if (rmrMessageBuffer.rcvMessage == nullptr) {
2032 //we have error
2033 mdclog_write(MDCLOG_ERR, "RMR Allocation message, %s", strerror(errno));
ss412g1a79bdf2019-10-24 12:03:05 +03002034 return -1;
2035 }
2036
aa7133@att.comf13529a2020-08-27 11:21:16 +03002037// if (loglevel >= MDCLOG_DEBUG) {
2038// mdclog_write(MDCLOG_DEBUG, "Call to rmr_rcv_msg");
2039// }
ss412g1a79bdf2019-10-24 12:03:05 +03002040 rmrMessageBuffer.rcvMessage = rmr_rcv_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.rcvMessage);
2041 if (rmrMessageBuffer.rcvMessage == nullptr) {
2042 mdclog_write(MDCLOG_ERR, "RMR Receving message with null pointer, Realloc rmr mesage buffer");
2043 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
ss412g1a79bdf2019-10-24 12:03:05 +03002044 return -2;
2045 }
2046 ReportingMessages_t message;
2047 message.message.direction = 'D';
2048 message.message.time.tv_nsec = ts.tv_nsec;
2049 message.message.time.tv_sec = ts.tv_sec;
2050
2051 // get message payload
2052 //auto msgData = msg->payload;
2053 if (rmrMessageBuffer.rcvMessage->state != 0) {
2054 mdclog_write(MDCLOG_ERR, "RMR Receving message with stat = %d", rmrMessageBuffer.rcvMessage->state);
ss412g1a79bdf2019-10-24 12:03:05 +03002055 return -1;
2056 }
aa7133@att.com1fecb052020-03-29 09:44:09 +03002057 rmr_get_meid(rmrMessageBuffer.rcvMessage, (unsigned char *)message.message.enodbName);
ss412g4aeeda22020-06-16 14:12:41 +03002058 message.peerInfo = (ConnectedCU_t *) sctpMap->find(message.message.enodbName);
2059 if (message.peerInfo == nullptr) {
2060 auto type = rmrMessageBuffer.rcvMessage->mtype;
2061 switch (type) {
2062 case RIC_SCTP_CLEAR_ALL:
2063 case E2_TERM_KEEP_ALIVE_REQ:
2064 case RIC_HEALTH_CHECK_REQ:
2065 break;
2066 default:
2067 mdclog_write(MDCLOG_ERR, "Failed to send message no CU entry %s", message.message.enodbName);
2068 return -1;
2069 }
2070 }
2071
aa7133@att.comde40d1c2020-08-26 16:19:20 +03002072 if (rmrMessageBuffer.rcvMessage->mtype != RIC_HEALTH_CHECK_REQ) {
2073 num_of_XAPP_messages.fetch_add(1, std::memory_order_release);
2074
2075 }
ss412g1a79bdf2019-10-24 12:03:05 +03002076 switch (rmrMessageBuffer.rcvMessage->mtype) {
aa7133@att.combe92d712020-03-18 17:30:39 +02002077 case RIC_E2_SETUP_RESP : {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002078 if (loglevel >= MDCLOG_DEBUG) {
2079 mdclog_write(MDCLOG_DEBUG, "RIC_E2_SETUP_RESP");
2080 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002081 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
aa7133@att.com1fecb052020-03-29 09:44:09 +03002082 break;
2083 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002084 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2085 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002086 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2087 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_SETUP_RESP");
2088 return -6;
ss412g1a79bdf2019-10-24 12:03:05 +03002089 }
2090 break;
2091 }
aa7133@att.combe92d712020-03-18 17:30:39 +02002092 case RIC_E2_SETUP_FAILURE : {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002093 if (loglevel >= MDCLOG_DEBUG) {
2094 mdclog_write(MDCLOG_DEBUG, "RIC_E2_SETUP_FAILURE");
2095 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002096 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
aa7133@att.com1fecb052020-03-29 09:44:09 +03002097 break;
2098 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002099 message.peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2100 message.peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002101 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2102 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_SETUP_FAILURE");
2103 return -6;
ss412g1a79bdf2019-10-24 12:03:05 +03002104 }
2105 break;
2106 }
aa7133@att.combe92d712020-03-18 17:30:39 +02002107 case RIC_ERROR_INDICATION: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002108 if (loglevel >= MDCLOG_DEBUG) {
2109 mdclog_write(MDCLOG_DEBUG, "RIC_ERROR_INDICATION");
2110 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002111 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_ErrorIndication]->Increment();
2112 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_ErrorIndication]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002113 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2114 mdclog_write(MDCLOG_ERR, "Failed to send RIC_ERROR_INDICATION");
ss412g1a79bdf2019-10-24 12:03:05 +03002115 return -6;
2116 }
2117 break;
2118 }
2119 case RIC_SUB_REQ: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002120 if (loglevel >= MDCLOG_DEBUG) {
2121 mdclog_write(MDCLOG_DEBUG, "RIC_SUB_REQ");
2122 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002123 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
2124 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002125 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002126 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SUB_REQ");
ss412g1a79bdf2019-10-24 12:03:05 +03002127 return -6;
2128 }
2129 break;
2130 }
2131 case RIC_SUB_DEL_REQ: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002132 if (loglevel >= MDCLOG_DEBUG) {
2133 mdclog_write(MDCLOG_DEBUG, "RIC_SUB_DEL_REQ");
2134 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002135 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
2136 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002137 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002138 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SUB_DEL_REQ");
ss412g1a79bdf2019-10-24 12:03:05 +03002139 return -6;
2140 }
2141 break;
2142 }
2143 case RIC_CONTROL_REQ: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002144 if (loglevel >= MDCLOG_DEBUG) {
2145 mdclog_write(MDCLOG_DEBUG, "RIC_CONTROL_REQ");
2146 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002147 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
2148 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002149 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002150 mdclog_write(MDCLOG_ERR, "Failed to send RIC_CONTROL_REQ");
ss412g1a79bdf2019-10-24 12:03:05 +03002151 return -6;
2152 }
2153 break;
2154 }
2155 case RIC_SERVICE_QUERY: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002156 if (loglevel >= MDCLOG_DEBUG) {
2157 mdclog_write(MDCLOG_DEBUG, "RIC_SERVICE_QUERY");
2158 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002159 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2160 break;
2161 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002162 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceQuery]->Increment();
2163 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceQuery]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002164 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002165 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_QUERY");
ss412g1a79bdf2019-10-24 12:03:05 +03002166 return -6;
2167 }
2168 break;
2169 }
2170 case RIC_SERVICE_UPDATE_ACK: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002171 if (loglevel >= MDCLOG_DEBUG) {
2172 mdclog_write(MDCLOG_DEBUG, "RIC_SERVICE_UPDATE_ACK");
2173 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002174 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2175 break;
2176 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002177 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
2178 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceQuery]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002179 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002180 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_UPDATE_ACK");
ss412g1a79bdf2019-10-24 12:03:05 +03002181 return -6;
2182 }
2183 break;
2184 }
2185 case RIC_SERVICE_UPDATE_FAILURE: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002186 if (loglevel >= MDCLOG_DEBUG) {
2187 mdclog_write(MDCLOG_DEBUG, "RIC_SERVICE_UPDATE_FAILURE");
2188 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002189 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2190 break;
2191 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002192 message.peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
2193 message.peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceQuery]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002194 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002195 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_UPDATE_FAILURE");
ss412g1a79bdf2019-10-24 12:03:05 +03002196 return -6;
2197 }
2198 break;
2199 }
ss412g4aeeda22020-06-16 14:12:41 +03002200 case RIC_E2_RESET_REQ: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002201 if (loglevel >= MDCLOG_DEBUG) {
2202 mdclog_write(MDCLOG_DEBUG, "RIC_E2_RESET_REQ");
2203 }
ss412g4aeeda22020-06-16 14:12:41 +03002204 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2205 break;
2206 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002207 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2208 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002209 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g4aeeda22020-06-16 14:12:41 +03002210 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_RESET");
ss412g1a79bdf2019-10-24 12:03:05 +03002211 return -6;
2212 }
2213 break;
2214 }
ss412g4aeeda22020-06-16 14:12:41 +03002215 case RIC_E2_RESET_RESP: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002216 if (loglevel >= MDCLOG_DEBUG) {
2217 mdclog_write(MDCLOG_DEBUG, "RIC_E2_RESET_RESP");
2218 }
ss412g4aeeda22020-06-16 14:12:41 +03002219 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2220 break;
2221 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002222 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2223 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002224 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g4aeeda22020-06-16 14:12:41 +03002225 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_RESET_RESP");
ss412g1a79bdf2019-10-24 12:03:05 +03002226 return -6;
2227 }
2228 break;
2229 }
2230 case RIC_SCTP_CLEAR_ALL: {
2231 mdclog_write(MDCLOG_INFO, "RIC_SCTP_CLEAR_ALL");
2232 // loop on all keys and close socket and then erase all map.
2233 vector<char *> v;
2234 sctpMap->getKeys(v);
2235 for (auto const &iter : v) { //}; iter != sctpMap.end(); iter++) {
2236 if (!boost::starts_with((string) (iter), "host:") && !boost::starts_with((string) (iter), "msg:")) {
2237 auto *peerInfo = (ConnectedCU_t *) sctpMap->find(iter);
2238 if (peerInfo == nullptr) {
2239 continue;
2240 }
2241 close(peerInfo->fileDescriptor);
2242 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
2243 message.message.direction = 'D';
2244 message.message.time.tv_nsec = ts.tv_nsec;
2245 message.message.time.tv_sec = ts.tv_sec;
2246
2247 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
2248 snprintf((char *)rmrMessageBuffer.sendMessage->payload,
aa7133@att.com96c9b882020-03-25 14:50:40 +02002249 256,
2250 "%s|RIC_SCTP_CLEAR_ALL",
2251 peerInfo->enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03002252 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
2253 mdclog_write(MDCLOG_INFO, "%s", message.message.asndata);
aa7133@att.combe92d712020-03-18 17:30:39 +02002254 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002255 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
2256 }
2257 free(peerInfo);
2258 }
2259 }
2260
2261 sleep(1);
2262 sctpMap->clear();
2263 break;
2264 }
ss412g3bac2da2020-01-05 11:52:19 +02002265 case E2_TERM_KEEP_ALIVE_REQ: {
2266 // send message back
ss412g3bac2da2020-01-05 11:52:19 +02002267 rmr_bytes2payload(rmrMessageBuffer.sendMessage,
aa7133@att.com96c9b882020-03-25 14:50:40 +02002268 (unsigned char *)rmrMessageBuffer.ka_message,
2269 rmrMessageBuffer.ka_message_len);
ss412g3bac2da2020-01-05 11:52:19 +02002270 rmrMessageBuffer.sendMessage->mtype = E2_TERM_KEEP_ALIVE_RESP;
2271 rmrMessageBuffer.sendMessage->state = 0;
2272 static unsigned char tx[32];
2273 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2274 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, txLen);
2275 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2276 if (rmrMessageBuffer.sendMessage == nullptr) {
2277 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
aa7133@att.com77fff432020-01-27 12:01:20 +02002278 mdclog_write(MDCLOG_ERR, "Failed to send E2_TERM_KEEP_ALIVE_RESP RMR message returned NULL");
ss412g3bac2da2020-01-05 11:52:19 +02002279 } else if (rmrMessageBuffer.sendMessage->state != 0) {
aa7133@att.com77fff432020-01-27 12:01:20 +02002280 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 +02002281 rmrMessageBuffer.sendMessage->state, translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str());
aa7133@att.comf13529a2020-08-27 11:21:16 +03002282 } else if (loglevel >= MDCLOG_DEBUG) {
aa7133@att.combc110772020-03-25 09:45:14 +02002283 mdclog_write(MDCLOG_DEBUG, "Got Keep Alive Request send : %s", rmrMessageBuffer.ka_message);
ss412g3bac2da2020-01-05 11:52:19 +02002284 }
aa7133@att.com342843e2020-01-26 12:53:34 +02002285
ss412g3bac2da2020-01-05 11:52:19 +02002286 break;
2287 }
ss412g4aeeda22020-06-16 14:12:41 +03002288 case RIC_HEALTH_CHECK_REQ: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002289 static int counter = 0;
ss412g4aeeda22020-06-16 14:12:41 +03002290 // send message back
aa7133@att.comc18fe532020-06-30 12:40:18 +03002291 rmr_bytes2payload(rmrMessageBuffer.rcvMessage,
ss412g4aeeda22020-06-16 14:12:41 +03002292 (unsigned char *)"OK",
2293 2);
aa7133@att.comc18fe532020-06-30 12:40:18 +03002294 rmrMessageBuffer.rcvMessage->mtype = RIC_HEALTH_CHECK_RESP;
2295 rmrMessageBuffer.rcvMessage->state = 0;
ss412g4aeeda22020-06-16 14:12:41 +03002296 static unsigned char tx[32];
2297 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
aa7133@att.comc18fe532020-06-30 12:40:18 +03002298 rmr_bytes2xact(rmrMessageBuffer.rcvMessage, tx, txLen);
2299 rmrMessageBuffer.rcvMessage = rmr_rts_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.rcvMessage);
2300 //rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2301 if (rmrMessageBuffer.rcvMessage == nullptr) {
2302 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
ss412g4aeeda22020-06-16 14:12:41 +03002303 mdclog_write(MDCLOG_ERR, "Failed to send RIC_HEALTH_CHECK_RESP RMR message returned NULL");
aa7133@att.comc18fe532020-06-30 12:40:18 +03002304 } else if (rmrMessageBuffer.rcvMessage->state != 0) {
ss412g4aeeda22020-06-16 14:12:41 +03002305 mdclog_write(MDCLOG_ERR, "Failed to send RIC_HEALTH_CHECK_RESP, on RMR state = %d ( %s)",
aa7133@att.comc18fe532020-06-30 12:40:18 +03002306 rmrMessageBuffer.rcvMessage->state, translateRmrErrorMessages(rmrMessageBuffer.rcvMessage->state).c_str());
aa7133@att.comf13529a2020-08-27 11:21:16 +03002307 } else if (loglevel >= MDCLOG_DEBUG && ++counter % 100 == 0) {
2308 mdclog_write(MDCLOG_DEBUG, "Got %d RIC_HEALTH_CHECK_REQ Request send : OK", counter);
ss412g4aeeda22020-06-16 14:12:41 +03002309 }
2310
2311 break;
2312 }
2313
ss412g1a79bdf2019-10-24 12:03:05 +03002314 default:
2315 mdclog_write(MDCLOG_WARN, "Message Type : %d is not seported", rmrMessageBuffer.rcvMessage->mtype);
2316 message.message.asndata = rmrMessageBuffer.rcvMessage->payload;
2317 message.message.asnLength = rmrMessageBuffer.rcvMessage->len;
2318 message.message.time.tv_nsec = ts.tv_nsec;
2319 message.message.time.tv_sec = ts.tv_sec;
2320 message.message.messageType = rmrMessageBuffer.rcvMessage->mtype;
2321
2322 buildJsonMessage(message);
2323
2324
ss412g1a79bdf2019-10-24 12:03:05 +03002325 return -7;
2326 }
2327 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2328 mdclog_write(MDCLOG_DEBUG, "EXIT OK from %s", __FUNCTION__);
2329 }
ss412g1a79bdf2019-10-24 12:03:05 +03002330 return 0;
2331}
2332
2333/**
2334 * Send message to the CU that is not expecting for successful or unsuccessful results
2335 * @param messageBuffer
2336 * @param message
2337 * @param failedMsgId
2338 * @param sctpMap
ss412g1a79bdf2019-10-24 12:03:05 +03002339 * @return
2340 */
2341int sendDirectionalSctpMsg(RmrMessagesBuffer_t &messageBuffer,
2342 ReportingMessages_t &message,
2343 int failedMsgId,
aa7133@att.combe92d712020-03-18 17:30:39 +02002344 Sctp_Map_t *sctpMap) {
ss412g1a79bdf2019-10-24 12:03:05 +03002345
aa7133@att.combe92d712020-03-18 17:30:39 +02002346 getRequestMetaData(message, messageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03002347 if (mdclog_level_get() >= MDCLOG_INFO) {
2348 mdclog_write(MDCLOG_INFO, "send message to %s address", message.message.enodbName);
2349 }
2350
aa7133@att.combe92d712020-03-18 17:30:39 +02002351 auto rc = sendMessagetoCu(sctpMap, messageBuffer, message, failedMsgId);
ss412g1a79bdf2019-10-24 12:03:05 +03002352 return rc;
2353}
2354
2355/**
2356 *
2357 * @param sctpMap
2358 * @param messageBuffer
2359 * @param message
2360 * @param failedMesgId
ss412g1a79bdf2019-10-24 12:03:05 +03002361 * @return
2362 */
2363int sendMessagetoCu(Sctp_Map_t *sctpMap,
2364 RmrMessagesBuffer_t &messageBuffer,
2365 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +02002366 int failedMesgId) {
ss412g1a79bdf2019-10-24 12:03:05 +03002367 // get the FD
2368 message.message.messageType = messageBuffer.rcvMessage->mtype;
ss412g4aeeda22020-06-16 14:12:41 +03002369 auto rc = sendSctpMsg(message.peerInfo, message, sctpMap);
ss412g1a79bdf2019-10-24 12:03:05 +03002370 return rc;
2371}
2372
ss412g1a79bdf2019-10-24 12:03:05 +03002373
2374/**
2375 *
2376 * @param epoll_fd
2377 * @param peerInfo
2378 * @param events
2379 * @param sctpMap
2380 * @param enodbName
2381 * @param msgType
ss412g1a79bdf2019-10-24 12:03:05 +03002382 * @return
2383 */
2384int addToEpoll(int epoll_fd,
2385 ConnectedCU_t *peerInfo,
2386 uint32_t events,
2387 Sctp_Map_t *sctpMap,
2388 char *enodbName,
aa7133@att.combe92d712020-03-18 17:30:39 +02002389 int msgType) {
ss412g1a79bdf2019-10-24 12:03:05 +03002390 // Add to Epol
2391 struct epoll_event event{};
2392 event.data.ptr = peerInfo;
2393 event.events = events;
2394 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, peerInfo->fileDescriptor, &event) < 0) {
2395 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2396 mdclog_write(MDCLOG_DEBUG, "epoll_ctl EPOLL_CTL_ADD (may chack not to quit here), %s, %s %d",
2397 strerror(errno), __func__, __LINE__);
2398 }
2399 close(peerInfo->fileDescriptor);
aa7133@att.com84bd3342020-03-16 18:04:57 +02002400 if (enodbName != nullptr) {
aa7133@att.combe92d712020-03-18 17:30:39 +02002401 cleanHashEntry(peerInfo, sctpMap);
aa7133@att.com84bd3342020-03-16 18:04:57 +02002402 char key[MAX_ENODB_NAME_SIZE * 2];
2403 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", enodbName, msgType);
2404 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2405 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
2406 }
2407 auto tmp = sctpMap->find(key);
2408 if (tmp) {
2409 free(tmp);
2410 sctpMap->erase(key);
2411 }
2412 } else {
2413 peerInfo->enodbName[0] = 0;
ss412g1a79bdf2019-10-24 12:03:05 +03002414 }
ss412g1a79bdf2019-10-24 12:03:05 +03002415 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may chack not to quit here)");
ss412g1a79bdf2019-10-24 12:03:05 +03002416 return -1;
2417 }
ss412g1a79bdf2019-10-24 12:03:05 +03002418 return 0;
2419}
2420
2421/**
2422 *
2423 * @param epoll_fd
2424 * @param peerInfo
2425 * @param events
2426 * @param sctpMap
2427 * @param enodbName
2428 * @param msgType
ss412g1a79bdf2019-10-24 12:03:05 +03002429 * @return
2430 */
2431int modifyToEpoll(int epoll_fd,
2432 ConnectedCU_t *peerInfo,
2433 uint32_t events,
2434 Sctp_Map_t *sctpMap,
2435 char *enodbName,
aa7133@att.combe92d712020-03-18 17:30:39 +02002436 int msgType) {
ss412g1a79bdf2019-10-24 12:03:05 +03002437 // Add to Epol
2438 struct epoll_event event{};
2439 event.data.ptr = peerInfo;
2440 event.events = events;
2441 if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, peerInfo->fileDescriptor, &event) < 0) {
2442 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2443 mdclog_write(MDCLOG_DEBUG, "epoll_ctl EPOLL_CTL_MOD (may chack not to quit here), %s, %s %d",
2444 strerror(errno), __func__, __LINE__);
2445 }
2446 close(peerInfo->fileDescriptor);
aa7133@att.combe92d712020-03-18 17:30:39 +02002447 cleanHashEntry(peerInfo, sctpMap);
ss412g1a79bdf2019-10-24 12:03:05 +03002448 char key[MAX_ENODB_NAME_SIZE * 2];
2449 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", enodbName, msgType);
2450 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2451 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
2452 }
aa7133@att.com96c9b882020-03-25 14:50:40 +02002453 auto tmp = sctpMap->find(key);
ss412g1a79bdf2019-10-24 12:03:05 +03002454 if (tmp) {
2455 free(tmp);
2456 }
2457 sctpMap->erase(key);
2458 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may chack not to quit here)");
ss412g1a79bdf2019-10-24 12:03:05 +03002459 return -1;
2460 }
ss412g1a79bdf2019-10-24 12:03:05 +03002461 return 0;
2462}
2463
2464
aa7133@att.combe92d712020-03-18 17:30:39 +02002465int sendRmrMessage(RmrMessagesBuffer_t &rmrMessageBuffer, ReportingMessages_t &message) {
ss412g1a79bdf2019-10-24 12:03:05 +03002466 buildJsonMessage(message);
2467
2468 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2469
2470 if (rmrMessageBuffer.sendMessage == nullptr) {
2471 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
2472 mdclog_write(MDCLOG_ERR, "RMR failed send message returned with NULL pointer");
ss412g1a79bdf2019-10-24 12:03:05 +03002473 return -1;
2474 }
2475
2476 if (rmrMessageBuffer.sendMessage->state != 0) {
2477 char meid[RMR_MAX_MEID]{};
2478 if (rmrMessageBuffer.sendMessage->state == RMR_ERR_RETRY) {
2479 usleep(5);
2480 rmrMessageBuffer.sendMessage->state = 0;
2481 mdclog_write(MDCLOG_INFO, "RETRY sending Message type %d to Xapp from %s",
2482 rmrMessageBuffer.sendMessage->mtype,
2483 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
2484 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2485 if (rmrMessageBuffer.sendMessage == nullptr) {
2486 mdclog_write(MDCLOG_ERR, "RMR failed send message returned with NULL pointer");
2487 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
ss412g1a79bdf2019-10-24 12:03:05 +03002488 return -1;
2489 } else if (rmrMessageBuffer.sendMessage->state != 0) {
2490 mdclog_write(MDCLOG_ERR,
2491 "Message state %s while sending request %d to Xapp from %s after retry of 10 microseconds",
2492 translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str(),
2493 rmrMessageBuffer.sendMessage->mtype,
2494 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
2495 auto rc = rmrMessageBuffer.sendMessage->state;
ss412g1a79bdf2019-10-24 12:03:05 +03002496 return rc;
2497 }
2498 } else {
2499 mdclog_write(MDCLOG_ERR, "Message state %s while sending request %d to Xapp from %s",
2500 translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str(),
2501 rmrMessageBuffer.sendMessage->mtype,
2502 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
ss412g1a79bdf2019-10-24 12:03:05 +03002503 return rmrMessageBuffer.sendMessage->state;
2504 }
2505 }
2506 return 0;
2507}
2508
2509void buildJsonMessage(ReportingMessages_t &message) {
ss412g3bac2da2020-01-05 11:52:19 +02002510 if (jsonTrace) {
2511 message.outLen = sizeof(message.base64Data);
2512 base64::encode((const unsigned char *) message.message.asndata,
2513 (const int) message.message.asnLength,
2514 message.base64Data,
2515 message.outLen);
2516 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.comc5a15202020-04-05 19:57:01 +03002517 mdclog_write(MDCLOG_DEBUG, "Tracing: ASN length = %d, base64 message length = %d ",
ss412g3bac2da2020-01-05 11:52:19 +02002518 (int) message.message.asnLength,
2519 (int) message.outLen);
2520 }
2521
2522 snprintf(message.buffer, sizeof(message.buffer),
aa7133@att.com96c9b882020-03-25 14:50:40 +02002523 "{\"header\": {\"ts\": \"%ld.%09ld\","
2524 "\"ranName\": \"%s\","
2525 "\"messageType\": %d,"
2526 "\"direction\": \"%c\"},"
2527 "\"base64Length\": %d,"
2528 "\"asnBase64\": \"%s\"}",
2529 message.message.time.tv_sec,
2530 message.message.time.tv_nsec,
2531 message.message.enodbName,
2532 message.message.messageType,
2533 message.message.direction,
2534 (int) message.outLen,
2535 message.base64Data);
ss412g3bac2da2020-01-05 11:52:19 +02002536 static src::logger_mt &lg = my_logger::get();
2537
2538 BOOST_LOG(lg) << message.buffer;
ss412g1a79bdf2019-10-24 12:03:05 +03002539 }
ss412g1a79bdf2019-10-24 12:03:05 +03002540}
2541
2542
2543/**
2544 * take RMR error code to string
2545 * @param state
2546 * @return
2547 */
2548string translateRmrErrorMessages(int state) {
2549 string str = {};
2550 switch (state) {
2551 case RMR_OK:
2552 str = "RMR_OK - state is good";
2553 break;
2554 case RMR_ERR_BADARG:
2555 str = "RMR_ERR_BADARG - argument passd to function was unusable";
2556 break;
2557 case RMR_ERR_NOENDPT:
2558 str = "RMR_ERR_NOENDPT - send//call could not find an endpoint based on msg type";
2559 break;
2560 case RMR_ERR_EMPTY:
2561 str = "RMR_ERR_EMPTY - msg received had no payload; attempt to send an empty message";
2562 break;
2563 case RMR_ERR_NOHDR:
2564 str = "RMR_ERR_NOHDR - message didn't contain a valid header";
2565 break;
2566 case RMR_ERR_SENDFAILED:
2567 str = "RMR_ERR_SENDFAILED - send failed; errno has nano reason";
2568 break;
2569 case RMR_ERR_CALLFAILED:
2570 str = "RMR_ERR_CALLFAILED - unable to send call() message";
2571 break;
2572 case RMR_ERR_NOWHOPEN:
2573 str = "RMR_ERR_NOWHOPEN - no wormholes are open";
2574 break;
2575 case RMR_ERR_WHID:
2576 str = "RMR_ERR_WHID - wormhole id was invalid";
2577 break;
2578 case RMR_ERR_OVERFLOW:
2579 str = "RMR_ERR_OVERFLOW - operation would have busted through a buffer/field size";
2580 break;
2581 case RMR_ERR_RETRY:
2582 str = "RMR_ERR_RETRY - request (send/call/rts) failed, but caller should retry (EAGAIN for wrappers)";
2583 break;
2584 case RMR_ERR_RCVFAILED:
2585 str = "RMR_ERR_RCVFAILED - receive failed (hard error)";
2586 break;
2587 case RMR_ERR_TIMEOUT:
2588 str = "RMR_ERR_TIMEOUT - message processing call timed out";
2589 break;
2590 case RMR_ERR_UNSET:
2591 str = "RMR_ERR_UNSET - the message hasn't been populated with a transport buffer";
2592 break;
2593 case RMR_ERR_TRUNC:
2594 str = "RMR_ERR_TRUNC - received message likely truncated";
2595 break;
2596 case RMR_ERR_INITFAILED:
2597 str = "RMR_ERR_INITFAILED - initialisation of something (probably message) failed";
2598 break;
2599 case RMR_ERR_NOTSUPP:
2600 str = "RMR_ERR_NOTSUPP - the request is not supported, or RMr was not initialised for the request";
2601 break;
2602 default:
2603 char buf[128]{};
2604 snprintf(buf, sizeof buf, "UNDOCUMENTED RMR_ERR : %d", state);
2605 str = buf;
2606 break;
2607 }
2608 return str;
2609}
2610
2611