blob: 987bbafb0c1fbcaeee8211d7882de58369ca733e [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;
248 tmpStr = conf.getStringValue("prometheusMode");
249 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
aa7133@att.coma4c80012020-06-24 16:36:47 +0300250 sctpParams.prometheusMode = tmpStr;
ss412g4aeeda22020-06-16 14:12:41 +0300251 if (tmpStr.length() != 0) {
252 if (tmpStr.compare("push") == 0) {
253 sctpParams.prometheusPushAddress = tmpStr;
254 auto timeout = conf.getIntValue("prometheusPushTimeOut");
255 if (timeout >= 5 && timeout <= 300) {
256 sctpParams.epollTimeOut = timeout * 1000;
257 } else {
258 sctpParams.epollTimeOut = 10 * 1000;
259 }
260 }
261 }
262
263 tmpStr = conf.getStringValue("prometheusPushAddr");
264 if (tmpStr.length() != 0) {
aa7133@att.coma4c80012020-06-24 16:36:47 +0300265 sctpParams.prometheusPushAddress = tmpStr;
ss412g4aeeda22020-06-16 14:12:41 +0300266 }
267
268 tmpStr = conf.getStringValue("prometheusPort");
269 if (tmpStr.length() != 0) {
270 sctpParams.prometheusPort = tmpStr;
271 }
272
aa7133@att.combe92d712020-03-18 17:30:39 +0200273 sctpParams.ka_message_length = snprintf(sctpParams.ka_message, KA_MESSAGE_SIZE, "{\"address\": \"%s:%d\","
aa7133@att.com96c9b882020-03-25 14:50:40 +0200274 "\"fqdn\": \"%s\","
275 "\"pod_name\": \"%s\"}",
aa7133@att.com342843e2020-01-26 12:53:34 +0200276 (const char *)sctpParams.myIP.c_str(),
277 sctpParams.rmrPort,
278 sctpParams.fqdn.c_str(),
279 sctpParams.podName.c_str());
280
ss412g1a79bdf2019-10-24 12:03:05 +0300281 if (mdclog_level_get() >= MDCLOG_INFO) {
ss412g3bac2da2020-01-05 11:52:19 +0200282 mdclog_mdc_add("RMR Port", to_string(sctpParams.rmrPort).c_str());
283 mdclog_mdc_add("LogLevel", to_string(sctpParams.logLevel).c_str());
284 mdclog_mdc_add("volume", sctpParams.volume);
ss412g1a79bdf2019-10-24 12:03:05 +0300285 mdclog_mdc_add("tmpLogFilespec", tmpLogFilespec);
ss412g3bac2da2020-01-05 11:52:19 +0200286 mdclog_mdc_add("my ip", sctpParams.myIP.c_str());
aa7133@att.com342843e2020-01-26 12:53:34 +0200287 mdclog_mdc_add("pod name", sctpParams.podName.c_str());
ss412g1a79bdf2019-10-24 12:03:05 +0300288
aa7133@att.com342843e2020-01-26 12:53:34 +0200289 mdclog_write(MDCLOG_INFO, "running parameters for instance : %s", sctpParams.ka_message);
ss412g1a79bdf2019-10-24 12:03:05 +0300290 }
291 mdclog_mdc_clean();
292
293 // Files written to the current working directory
294 boostLogger = logging::add_file_log(
ss412g3bac2da2020-01-05 11:52:19 +0200295 keywords::file_name = tmpLogFilespec, // to temp directory
ss412g1a79bdf2019-10-24 12:03:05 +0300296 keywords::rotation_size = 10 * 1024 * 1024,
297 keywords::time_based_rotation = sinks::file::rotation_at_time_interval(posix_time::hours(1)),
298 keywords::format = "%Message%"
ss412g3bac2da2020-01-05 11:52:19 +0200299 //keywords::format = "[%TimeStamp%]: %Message%" // use each tmpStr with time stamp
ss412g1a79bdf2019-10-24 12:03:05 +0300300 );
301
302 // Setup a destination folder for collecting rotated (closed) files --since the same volumn can use rename()
303 boostLogger->locked_backend()->set_file_collector(sinks::file::make_collector(
ss412g3bac2da2020-01-05 11:52:19 +0200304 keywords::target = sctpParams.volume
ss412g1a79bdf2019-10-24 12:03:05 +0300305 ));
306
307 // Upon restart, scan the directory for files matching the file_name pattern
308 boostLogger->locked_backend()->scan_for_files();
309
ss412g3bac2da2020-01-05 11:52:19 +0200310 // Enable auto-flushing after each tmpStr record written
ss412g1a79bdf2019-10-24 12:03:05 +0300311 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com84bd3342020-03-16 18:04:57 +0200312 boostLogger->locked_backend()->auto_flush(true);
313 }
314
315 return 0;
316}
317
ss412g4aeeda22020-06-16 14:12:41 +0300318static std::string GetHostName() {
319 char hostname[1024];
320
321 if (::gethostname(hostname, sizeof(hostname))) {
322 return {};
323 }
324 return hostname;
325}
326
aa7133@att.coma4c80012020-06-24 16:36:47 +0300327void startPrometheus(sctp_params_t &sctpParams) {
328 sctpParams.prometheusFamily = &BuildCounter()
329 .Name("E2T")
330 .Help("E2T message counter")
331 .Labels({{"POD_NAME", sctpParams.podName}})
332 .Register(*sctpParams.prometheusRegistry);
aa7133@att.com16c799b2020-03-24 18:01:51 +0200333
aa7133@att.coma4c80012020-06-24 16:36:47 +0300334 if (strcmp(sctpParams.prometheusMode.c_str(),"pull") == 0) {
335 if (mdclog_level_get() >= MDCLOG_DEBUG) {
336 mdclog_write(MDCLOG_DEBUG, "Start Prometheus Pull mode on %s:%s", sctpParams.myIP.c_str(), sctpParams.prometheusPort.c_str());
337 }
338 sctpParams.prometheusExposer = new Exposer(sctpParams.myIP + ":" + sctpParams.prometheusPort, 1);
339 sctpParams.prometheusExposer->RegisterCollectable(sctpParams.prometheusRegistry);
340 } else if (strcmp(sctpParams.prometheusMode.c_str(),"push") == 0) {
341 if (mdclog_level_get() >= MDCLOG_DEBUG) {
342 mdclog_write(MDCLOG_DEBUG, "Start Prometheus Push mode");
343 }
344 const auto labels = Gateway::GetInstanceLabel(GetHostName());
345 string address {};
346 string port {};
347 char ch = ':';
348 auto found = sctpParams.prometheusPushAddress.find_last_of(ch);
349 // If string doesn't have
350 // character ch present in it
351 if (found != string::npos) {
352 address = sctpParams.prometheusPushAddress.substr(0,found);
353 port = sctpParams.prometheusPushAddress.substr(found + 1);
354 sctpParams.prometheusGateway = new Gateway(address, port, "E2T", labels);
355 sctpParams.prometheusGateway->RegisterCollectable(sctpParams.prometheusRegistry);
356 } else {
357 mdclog_write(MDCLOG_ERR, "failed to build Prometheus gateway no stats will be sent");
358 }
359 }
360}
aa7133@att.com16c799b2020-03-24 18:01:51 +0200361
aa7133@att.com84bd3342020-03-16 18:04:57 +0200362int main(const int argc, char **argv) {
363 sctp_params_t sctpParams;
364
aa7133@att.com84bd3342020-03-16 18:04:57 +0200365 {
366 std::random_device device{};
367 std::mt19937 generator(device());
368 std::uniform_int_distribution<long> distribution(1, (long) 1e12);
369 transactionCounter = distribution(generator);
370 }
371
372// uint64_t st = 0;
373// uint32_t aux1 = 0;
374// st = rdtscp(aux1);
375
376 unsigned num_cpus = std::thread::hardware_concurrency();
377 init_log();
378 mdclog_level_set(MDCLOG_INFO);
379
380 if (std::signal(SIGINT, catch_function) == SIG_ERR) {
381 mdclog_write(MDCLOG_ERR, "Error initializing SIGINT");
382 exit(1);
383 }
384 if (std::signal(SIGABRT, catch_function)== SIG_ERR) {
385 mdclog_write(MDCLOG_ERR, "Error initializing SIGABRT");
386 exit(1);
387 }
388 if (std::signal(SIGTERM, catch_function)== SIG_ERR) {
389 mdclog_write(MDCLOG_ERR, "Error initializing SIGTERM");
390 exit(1);
391 }
392
393 cpuClock = approx_CPU_MHz(100);
394
395 mdclog_write(MDCLOG_DEBUG, "CPU speed %11.11f", cpuClock);
396
397 auto result = parse(argc, argv, sctpParams);
398
399 if (buildConfiguration(sctpParams) != 0) {
400 exit(-1);
ss412g1a79bdf2019-10-24 12:03:05 +0300401 }
402
ss412g4aeeda22020-06-16 14:12:41 +0300403 //auto registry = std::make_shared<Registry>();
404 sctpParams.prometheusRegistry = std::make_shared<Registry>();
405
406 //sctpParams.promtheusFamily = new Family<Counter>("E2T", "E2T message counter", {{"E", sctpParams.podName}});
407
aa7133@att.coma4c80012020-06-24 16:36:47 +0300408 startPrometheus(sctpParams);
ss412g4aeeda22020-06-16 14:12:41 +0300409
ss412g1a79bdf2019-10-24 12:03:05 +0300410 // start epoll
ss412g3bac2da2020-01-05 11:52:19 +0200411 sctpParams.epoll_fd = epoll_create1(0);
412 if (sctpParams.epoll_fd == -1) {
ss412g1a79bdf2019-10-24 12:03:05 +0300413 mdclog_write(MDCLOG_ERR, "failed to open epoll descriptor");
414 exit(-1);
415 }
416
aa7133@att.combe92d712020-03-18 17:30:39 +0200417 getRmrContext(sctpParams);
ss412g3bac2da2020-01-05 11:52:19 +0200418 if (sctpParams.rmrCtx == nullptr) {
419 close(sctpParams.epoll_fd);
ss412g1a79bdf2019-10-24 12:03:05 +0300420 exit(-1);
421 }
422
ss412g3bac2da2020-01-05 11:52:19 +0200423 if (buildInotify(sctpParams) == -1) {
424 close(sctpParams.rmrListenFd);
425 rmr_close(sctpParams.rmrCtx);
426 close(sctpParams.epoll_fd);
427 exit(-1);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200428 }
429
430 if (buildListeningPort(sctpParams) != 0) {
431 close(sctpParams.rmrListenFd);
432 rmr_close(sctpParams.rmrCtx);
433 close(sctpParams.epoll_fd);
434 exit(-1);
435 }
ss412g3bac2da2020-01-05 11:52:19 +0200436
437 sctpParams.sctpMap = new mapWrapper();
ss412g1a79bdf2019-10-24 12:03:05 +0300438
439 std::vector<std::thread> threads(num_cpus);
440// std::vector<std::thread> threads;
441
442 num_cpus = 1;
443 for (unsigned int i = 0; i < num_cpus; i++) {
ss412g3bac2da2020-01-05 11:52:19 +0200444 threads[i] = std::thread(listener, &sctpParams);
ss412g1a79bdf2019-10-24 12:03:05 +0300445
446 cpu_set_t cpuset;
447 CPU_ZERO(&cpuset);
448 CPU_SET(i, &cpuset);
449 int rc = pthread_setaffinity_np(threads[i].native_handle(), sizeof(cpu_set_t), &cpuset);
450 if (rc != 0) {
451 mdclog_write(MDCLOG_ERR, "Error calling pthread_setaffinity_np: %d", rc);
452 }
ss412g1a79bdf2019-10-24 12:03:05 +0300453 }
454
aa7133@att.com16c799b2020-03-24 18:01:51 +0200455
ss412g3bac2da2020-01-05 11:52:19 +0200456 //loop over term_init until first message from xApp
457 handleTermInit(sctpParams);
458
459 for (auto &t : threads) {
460 t.join();
461 }
462
ss412g3bac2da2020-01-05 11:52:19 +0200463 return 0;
464}
465
466void handleTermInit(sctp_params_t &sctpParams) {
467 sendTermInit(sctpParams);
ss412g1a79bdf2019-10-24 12:03:05 +0300468 //send to e2 manager init of e2 term
469 //E2_TERM_INIT
ss412g1a79bdf2019-10-24 12:03:05 +0300470
ss412g3bac2da2020-01-05 11:52:19 +0200471 int count = 0;
472 while (true) {
473 auto xappMessages = num_of_XAPP_messages.load(std::memory_order_acquire);
474 if (xappMessages > 0) {
475 if (mdclog_level_get() >= MDCLOG_INFO) {
aa7133@att.com84bd3342020-03-16 18:04:57 +0200476 mdclog_write(MDCLOG_INFO, "Got a message from some appliction, stop sending E2_TERM_INIT");
ss412g3bac2da2020-01-05 11:52:19 +0200477 }
478 return;
479 }
480 usleep(100000);
481 count++;
482 if (count % 1000 == 0) {
483 mdclog_write(MDCLOG_ERR, "GOT No messages from any xApp");
484 sendTermInit(sctpParams);
485 }
486 }
487}
488
489void sendTermInit(sctp_params_t &sctpParams) {
490 rmr_mbuf_t *msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.ka_message_length);
ss412g1a79bdf2019-10-24 12:03:05 +0300491 auto count = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200492 while (true) {
ss412g1a79bdf2019-10-24 12:03:05 +0300493 msg->mtype = E2_TERM_INIT;
494 msg->state = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200495 rmr_bytes2payload(msg, (unsigned char *)sctpParams.ka_message, sctpParams.ka_message_length);
ss412g1a79bdf2019-10-24 12:03:05 +0300496 static unsigned char tx[32];
497 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
498 rmr_bytes2xact(msg, tx, txLen);
ss412g3bac2da2020-01-05 11:52:19 +0200499 msg = rmr_send_msg(sctpParams.rmrCtx, msg);
ss412g1a79bdf2019-10-24 12:03:05 +0300500 if (msg == nullptr) {
aa7133@att.comc9575aa2020-04-02 11:40:09 +0300501 msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.ka_message_length);
ss412g1a79bdf2019-10-24 12:03:05 +0300502 } else if (msg->state == 0) {
ss412g1a79bdf2019-10-24 12:03:05 +0300503 rmr_free_msg(msg);
ss412g3bac2da2020-01-05 11:52:19 +0200504 if (mdclog_level_get() >= MDCLOG_INFO) {
505 mdclog_write(MDCLOG_INFO, "E2_TERM_INIT succsesfuly sent ");
506 }
507 return;
ss412g1a79bdf2019-10-24 12:03:05 +0300508 } else {
509 if (count % 100 == 0) {
aa7133@att.com11b17d42020-03-22 11:35:16 +0200510 mdclog_write(MDCLOG_ERR, "Error sending E2_TERM_INIT cause : %s ", translateRmrErrorMessages(msg->state).c_str());
ss412g1a79bdf2019-10-24 12:03:05 +0300511 }
512 sleep(1);
513 }
514 count++;
515 }
ss412g3bac2da2020-01-05 11:52:19 +0200516}
517
518/**
519 *
520 * @param argc
521 * @param argv
522 * @param sctpParams
523 * @return
524 */
525cxxopts::ParseResult parse(int argc, char *argv[], sctp_params_t &sctpParams) {
526 cxxopts::Options options(argv[0], "e2 term help");
527 options.positional_help("[optional args]").show_positional_help();
528 options.allow_unrecognised_options().add_options()
529 ("p,path", "config file path", cxxopts::value<std::string>(sctpParams.configFilePath)->default_value("config"))
530 ("f,file", "config file name", cxxopts::value<std::string>(sctpParams.configFileName)->default_value("config.conf"))
531 ("h,help", "Print help");
532
533 auto result = options.parse(argc, argv);
534
535 if (result.count("help")) {
536 std::cout << options.help({""}) << std::endl;
537 exit(0);
538 }
539 return result;
540}
541
542/**
543 *
544 * @param sctpParams
545 * @return -1 failed 0 success
546 */
547int buildInotify(sctp_params_t &sctpParams) {
548 sctpParams.inotifyFD = inotify_init1(IN_NONBLOCK);
549 if (sctpParams.inotifyFD == -1) {
550 mdclog_write(MDCLOG_ERR, "Failed to init inotify (inotify_init1) %s", strerror(errno));
551 close(sctpParams.rmrListenFd);
552 rmr_close(sctpParams.rmrCtx);
553 close(sctpParams.epoll_fd);
554 return -1;
ss412g1a79bdf2019-10-24 12:03:05 +0300555 }
556
ss412g3bac2da2020-01-05 11:52:19 +0200557 sctpParams.inotifyWD = inotify_add_watch(sctpParams.inotifyFD,
aa7133@att.com96c9b882020-03-25 14:50:40 +0200558 (const char *)sctpParams.configFilePath.c_str(),
aa7133@att.com84bd3342020-03-16 18:04:57 +0200559 (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 +0200560 if (sctpParams.inotifyWD == -1) {
561 mdclog_write(MDCLOG_ERR, "Failed to add directory : %s to inotify (inotify_add_watch) %s",
aa7133@att.com96c9b882020-03-25 14:50:40 +0200562 sctpParams.configFilePath.c_str(),
563 strerror(errno));
ss412g3bac2da2020-01-05 11:52:19 +0200564 close(sctpParams.inotifyFD);
565 return -1;
566 }
567
568 struct epoll_event event{};
569 event.events = (EPOLLIN);
570 event.data.fd = sctpParams.inotifyFD;
571 // add listening RMR FD to epoll
572 if (epoll_ctl(sctpParams.epoll_fd, EPOLL_CTL_ADD, sctpParams.inotifyFD, &event)) {
573 mdclog_write(MDCLOG_ERR, "Failed to add inotify FD to epoll");
574 close(sctpParams.inotifyFD);
575 return -1;
576 }
ss412g1a79bdf2019-10-24 12:03:05 +0300577 return 0;
578}
579
580/**
581 *
582 * @param args
583 * @return
584 */
585void listener(sctp_params_t *params) {
ss412g1a79bdf2019-10-24 12:03:05 +0300586 int num_of_SCTP_messages = 0;
ss412g1a79bdf2019-10-24 12:03:05 +0300587 auto totalTime = 0.0;
588 mdclog_mdc_clean();
589 mdclog_level_set(params->logLevel);
590
591 std::thread::id this_id = std::this_thread::get_id();
592 //save cout
593 streambuf *oldCout = cout.rdbuf();
594 ostringstream memCout;
595 // create new cout
596 cout.rdbuf(memCout.rdbuf());
597 cout << this_id;
598 //return to the normal cout
599 cout.rdbuf(oldCout);
600
601 char tid[32];
602 memcpy(tid, memCout.str().c_str(), memCout.str().length() < 32 ? memCout.str().length() : 31);
603 tid[memCout.str().length()] = 0;
604 mdclog_mdc_add("thread id", tid);
605
606 if (mdclog_level_get() >= MDCLOG_DEBUG) {
607 mdclog_write(MDCLOG_DEBUG, "started thread number %s", tid);
608 }
609
610 RmrMessagesBuffer_t rmrMessageBuffer{};
611 //create and init RMR
612 rmrMessageBuffer.rmrCtx = params->rmrCtx;
613
614 auto *events = (struct epoll_event *) calloc(MAXEVENTS, sizeof(struct epoll_event));
615 struct timespec end{0, 0};
616 struct timespec start{0, 0};
617
618 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
619 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
620
ss412g3bac2da2020-01-05 11:52:19 +0200621 memcpy(rmrMessageBuffer.ka_message, params->ka_message, params->ka_message_length);
622 rmrMessageBuffer.ka_message_len = params->ka_message_length;
623 rmrMessageBuffer.ka_message[rmrMessageBuffer.ka_message_len] = 0;
624
625 if (mdclog_level_get() >= MDCLOG_DEBUG) {
626 mdclog_write(MDCLOG_DEBUG, "keep alive message is : %s", rmrMessageBuffer.ka_message);
627 }
628
ss412g1a79bdf2019-10-24 12:03:05 +0300629 ReportingMessages_t message {};
630
aa7133@att.com7c3e4c02020-03-25 16:37:19 +0200631// for (int i = 0; i < MAX_RMR_BUFF_ARRY; i++) {
632// rmrMessageBuffer.rcvBufferedMessages[i] = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
633// rmrMessageBuffer.sendBufferedMessages[i] = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
634// }
ss412g1a79bdf2019-10-24 12:03:05 +0300635
ss412g4aeeda22020-06-16 14:12:41 +0300636 bool gatewayflag = false;
ss412g1a79bdf2019-10-24 12:03:05 +0300637 while (true) {
ss412g4aeeda22020-06-16 14:12:41 +0300638 future<int> gateWay;
639
ss412g1a79bdf2019-10-24 12:03:05 +0300640 if (mdclog_level_get() >= MDCLOG_DEBUG) {
ss412g4aeeda22020-06-16 14:12:41 +0300641 mdclog_write(MDCLOG_DEBUG, "Start EPOLL Wait. Timeout = %d", params->epollTimeOut);
ss412g1a79bdf2019-10-24 12:03:05 +0300642 }
ss412g4aeeda22020-06-16 14:12:41 +0300643 auto numOfEvents = epoll_wait(params->epoll_fd, events, MAXEVENTS, params->epollTimeOut);
644 if (numOfEvents == 0) {
645 if (params->prometheusGateway != nullptr) {
646 gateWay = params->prometheusGateway->AsyncPush();
647 gatewayflag = true;
ss412g1a79bdf2019-10-24 12:03:05 +0300648 }
649 continue;
ss412g4aeeda22020-06-16 14:12:41 +0300650 } else if (numOfEvents < 0) {
651 if (errno == EINTR) {
652 if (mdclog_level_get() >= MDCLOG_DEBUG) {
653 mdclog_write(MDCLOG_DEBUG, "got EINTR : %s", strerror(errno));
654 }
655 continue;
656 }
ss412g1a79bdf2019-10-24 12:03:05 +0300657 mdclog_write(MDCLOG_ERR, "Epoll wait failed, errno = %s", strerror(errno));
658 return;
659 }
ss412g4aeeda22020-06-16 14:12:41 +0300660 if (gatewayflag) {
661 gatewayflag = false;
662 auto rc = gateWay.get();
663 if (rc != 200) {
664 mdclog_write(MDCLOG_ERR, "Async Send to Promethues faild with Return Code %d", rc);
665 } else if (mdclog_level_get() >= MDCLOG_DEBUG) {
666 mdclog_write(MDCLOG_DEBUG, "Stats sent to Prometheus");
667 }
668 }
ss412g1a79bdf2019-10-24 12:03:05 +0300669 for (auto i = 0; i < numOfEvents; i++) {
670 if (mdclog_level_get() >= MDCLOG_DEBUG) {
671 mdclog_write(MDCLOG_DEBUG, "handling epoll event %d out of %d", i + 1, numOfEvents);
672 }
673 clock_gettime(CLOCK_MONOTONIC, &message.message.time);
674 start.tv_sec = message.message.time.tv_sec;
675 start.tv_nsec = message.message.time.tv_nsec;
ss412g3bac2da2020-01-05 11:52:19 +0200676
677
ss412g1a79bdf2019-10-24 12:03:05 +0300678 if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP)) {
aa7133@att.combe92d712020-03-18 17:30:39 +0200679 handlepoll_error(events[i], message, rmrMessageBuffer, params);
ss412g1a79bdf2019-10-24 12:03:05 +0300680 } else if (events[i].events & EPOLLOUT) {
aa7133@att.combe92d712020-03-18 17:30:39 +0200681 handleEinprogressMessages(events[i], message, rmrMessageBuffer, params);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200682 } else if (params->listenFD == events[i].data.fd) {
aa7133@att.com6680c3b2020-03-22 12:06:58 +0200683 if (mdclog_level_get() >= MDCLOG_INFO) {
684 mdclog_write(MDCLOG_INFO, "New connection request from sctp network\n");
685 }
aa7133@att.com84bd3342020-03-16 18:04:57 +0200686 // new connection is requested from RAN start build connection
687 while (true) {
688 struct sockaddr in_addr {};
689 socklen_t in_len;
690 char hostBuff[NI_MAXHOST];
691 char portBuff[NI_MAXSERV];
692
693 in_len = sizeof(in_addr);
694 auto *peerInfo = (ConnectedCU_t *)calloc(1, sizeof(ConnectedCU_t));
695 peerInfo->sctpParams = params;
696 peerInfo->fileDescriptor = accept(params->listenFD, &in_addr, &in_len);
697 if (peerInfo->fileDescriptor == -1) {
698 if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
699 /* We have processed all incoming connections. */
700 break;
701 } else {
702 mdclog_write(MDCLOG_ERR, "Accept error, errno = %s", strerror(errno));
703 break;
704 }
705 }
706 if (setSocketNoBlocking(peerInfo->fileDescriptor) == -1) {
707 mdclog_write(MDCLOG_ERR, "setSocketNoBlocking failed to set new connection %s on port %s\n", hostBuff, portBuff);
708 close(peerInfo->fileDescriptor);
709 break;
710 }
711 auto ans = getnameinfo(&in_addr, in_len,
aa7133@att.com96c9b882020-03-25 14:50:40 +0200712 peerInfo->hostName, NI_MAXHOST,
713 peerInfo->portNumber, NI_MAXSERV, (unsigned )((unsigned int)NI_NUMERICHOST | (unsigned int)NI_NUMERICSERV));
aa7133@att.com84bd3342020-03-16 18:04:57 +0200714 if (ans < 0) {
715 mdclog_write(MDCLOG_ERR, "Failed to get info on connection request. %s\n", strerror(errno));
716 close(peerInfo->fileDescriptor);
717 break;
718 }
719 if (mdclog_level_get() >= MDCLOG_DEBUG) {
720 mdclog_write(MDCLOG_DEBUG, "Accepted connection on descriptor %d (host=%s, port=%s)\n", peerInfo->fileDescriptor, peerInfo->hostName, peerInfo->portNumber);
721 }
722 peerInfo->isConnected = false;
723 peerInfo->gotSetup = false;
724 if (addToEpoll(params->epoll_fd,
725 peerInfo,
726 (EPOLLIN | EPOLLET),
727 params->sctpMap, nullptr,
aa7133@att.combe92d712020-03-18 17:30:39 +0200728 0) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +0200729 break;
730 }
aa7133@att.com6680c3b2020-03-22 12:06:58 +0200731 break;
aa7133@att.com84bd3342020-03-16 18:04:57 +0200732 }
ss412g1a79bdf2019-10-24 12:03:05 +0300733 } else if (params->rmrListenFd == events[i].data.fd) {
734 // got message from XAPP
ss412g3bac2da2020-01-05 11:52:19 +0200735 num_of_XAPP_messages.fetch_add(1, std::memory_order_release);
ss412g1a79bdf2019-10-24 12:03:05 +0300736 num_of_messages.fetch_add(1, std::memory_order_release);
737 if (mdclog_level_get() >= MDCLOG_DEBUG) {
738 mdclog_write(MDCLOG_DEBUG, "new message from RMR");
739 }
aa7133@att.com6680c3b2020-03-22 12:06:58 +0200740 if (receiveXappMessages(params->sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +0300741 rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200742 message.message.time) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +0300743 mdclog_write(MDCLOG_ERR, "Error handling Xapp message");
744 }
ss412g3bac2da2020-01-05 11:52:19 +0200745 } else if (params->inotifyFD == events[i].data.fd) {
746 mdclog_write(MDCLOG_INFO, "Got event from inotify (configuration update)");
747 handleConfigChange(params);
ss412g1a79bdf2019-10-24 12:03:05 +0300748 } else {
749 /* We RMR_ERR_RETRY have data on the fd waiting to be read. Read and display it.
750 * We must read whatever data is available completely, as we are running
751 * in edge-triggered mode and won't get a notification again for the same data. */
752 num_of_messages.fetch_add(1, std::memory_order_release);
753 if (mdclog_level_get() >= MDCLOG_DEBUG) {
754 mdclog_write(MDCLOG_DEBUG, "new message from SCTP, epoll flags are : %0x", events[i].events);
755 }
756 receiveDataFromSctp(&events[i],
757 params->sctpMap,
758 num_of_SCTP_messages,
759 rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200760 message.message.time);
ss412g1a79bdf2019-10-24 12:03:05 +0300761 }
762
763 clock_gettime(CLOCK_MONOTONIC, &end);
764 if (mdclog_level_get() >= MDCLOG_INFO) {
765 totalTime += ((end.tv_sec + 1.0e-9 * end.tv_nsec) -
766 ((double) start.tv_sec + 1.0e-9 * start.tv_nsec));
767 }
768 if (mdclog_level_get() >= MDCLOG_DEBUG) {
769 mdclog_write(MDCLOG_DEBUG, "message handling is %ld seconds %ld nanoseconds",
770 end.tv_sec - start.tv_sec,
771 end.tv_nsec - start.tv_nsec);
772 }
773 }
774 }
ss412g1a79bdf2019-10-24 12:03:05 +0300775}
776
777/**
778 *
ss412g3bac2da2020-01-05 11:52:19 +0200779 * @param sctpParams
780 */
781void handleConfigChange(sctp_params_t *sctpParams) {
782 char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event))));
783 const struct inotify_event *event;
784 char *ptr;
785
786 path p = (sctpParams->configFilePath + "/" + sctpParams->configFileName).c_str();
787 auto endlessLoop = true;
788 while (endlessLoop) {
789 auto len = read(sctpParams->inotifyFD, buf, sizeof buf);
790 if (len == -1) {
791 if (errno != EAGAIN) {
792 mdclog_write(MDCLOG_ERR, "read %s ", strerror(errno));
793 endlessLoop = false;
794 continue;
795 }
796 else {
797 endlessLoop = false;
798 continue;
799 }
800 }
801
802 for (ptr = buf; ptr < buf + len; ptr += sizeof(struct inotify_event) + event->len) {
803 event = (const struct inotify_event *)ptr;
804 if (event->mask & (uint32_t)IN_ISDIR) {
805 continue;
806 }
807
808 // the directory name
809 if (sctpParams->inotifyWD == event->wd) {
810 // not the directory
811 }
812 if (event->len) {
aa7133@att.comdfc1db92020-03-31 17:32:04 +0300813 auto retVal = strcmp(sctpParams->configFileName.c_str(), event->name);
814 if (retVal != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200815 continue;
816 }
817 }
818 // only the file we want
819 if (event->mask & (uint32_t)IN_CLOSE_WRITE) {
aa7133@att.comdfc1db92020-03-31 17:32:04 +0300820 if (mdclog_level_get() >= MDCLOG_INFO) {
821 mdclog_write(MDCLOG_INFO, "Configuration file changed");
822 }
ss412g3bac2da2020-01-05 11:52:19 +0200823 if (exists(p)) {
824 const int size = 2048;
825 auto fileSize = file_size(p);
826 if (fileSize > size) {
827 mdclog_write(MDCLOG_ERR, "File %s larger than %d", p.string().c_str(), size);
828 return;
829 }
830 } else {
831 mdclog_write(MDCLOG_ERR, "Configuration File %s not exists", p.string().c_str());
832 return;
833 }
834
835 ReadConfigFile conf;
836 if (conf.openConfigFile(p.string()) == -1) {
837 mdclog_write(MDCLOG_ERR, "Filed to open config file %s, %s",
838 p.string().c_str(), strerror(errno));
839 return;
840 }
841
842 auto tmpStr = conf.getStringValue("loglevel");
843 if (tmpStr.length() == 0) {
844 mdclog_write(MDCLOG_ERR, "illigal loglevel. Set loglevel to MDCLOG_INFO");
845 tmpStr = "info";
846 }
847 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
848
849 if ((tmpStr.compare("debug")) == 0) {
850 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_DEBUG");
851 sctpParams->logLevel = MDCLOG_DEBUG;
852 } else if ((tmpStr.compare("info")) == 0) {
853 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_INFO");
854 sctpParams->logLevel = MDCLOG_INFO;
855 } else if ((tmpStr.compare("warning")) == 0) {
856 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_WARN");
857 sctpParams->logLevel = MDCLOG_WARN;
858 } else if ((tmpStr.compare("error")) == 0) {
859 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_ERR");
860 sctpParams->logLevel = MDCLOG_ERR;
861 } else {
862 mdclog_write(MDCLOG_ERR, "illigal loglevel = %s. Set loglevel to MDCLOG_INFO", tmpStr.c_str());
863 sctpParams->logLevel = MDCLOG_INFO;
864 }
865 mdclog_level_set(sctpParams->logLevel);
866
867
868 tmpStr = conf.getStringValue("trace");
869 if (tmpStr.length() == 0) {
870 mdclog_write(MDCLOG_ERR, "illigal trace. Set trace to stop");
871 tmpStr = "stop";
872 }
873
874 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
875 if ((tmpStr.compare("start")) == 0) {
876 mdclog_write(MDCLOG_INFO, "Trace set to: start");
877 sctpParams->trace = true;
878 } else if ((tmpStr.compare("stop")) == 0) {
879 mdclog_write(MDCLOG_INFO, "Trace set to: stop");
880 sctpParams->trace = false;
881 } else {
882 mdclog_write(MDCLOG_ERR, "Trace was set to wrong value %s, set to stop", tmpStr.c_str());
883 sctpParams->trace = false;
884 }
885 jsonTrace = sctpParams->trace;
ss412g4aeeda22020-06-16 14:12:41 +0300886
aa7133@att.coma4c80012020-06-24 16:36:47 +0300887 if (strcmp(sctpParams->prometheusMode.c_str(), "push") == 0) {
ss412g4aeeda22020-06-16 14:12:41 +0300888 auto timeout = conf.getIntValue("prometheusPushTimeOut");
889 if (timeout >= 5 && timeout <= 300) {
890 sctpParams->epollTimeOut = timeout * 1000;
891 } else {
892 mdclog_write(MDCLOG_ERR, "prometheusPushTimeOut set wrong value %d, values are [5..300]",
893 timeout);
894 }
895 }
896
ss412g3bac2da2020-01-05 11:52:19 +0200897 endlessLoop = false;
898 }
899 }
900 }
901}
902
903/**
904 *
905 * @param event
906 * @param message
907 * @param rmrMessageBuffer
908 * @param params
ss412g3bac2da2020-01-05 11:52:19 +0200909 */
910void handleEinprogressMessages(struct epoll_event &event,
911 ReportingMessages_t &message,
912 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200913 sctp_params_t *params) {
ss412g3bac2da2020-01-05 11:52:19 +0200914 auto *peerInfo = (ConnectedCU_t *)event.data.ptr;
915 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
916
917 mdclog_write(MDCLOG_INFO, "file descriptor %d got EPOLLOUT", peerInfo->fileDescriptor);
918 auto retVal = 0;
919 socklen_t retValLen = 0;
920 auto rc = getsockopt(peerInfo->fileDescriptor, SOL_SOCKET, SO_ERROR, &retVal, &retValLen);
921 if (rc != 0 || retVal != 0) {
922 if (rc != 0) {
923 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
924 "%s|Failed SCTP Connection, after EINPROGRESS the getsockopt%s",
925 peerInfo->enodbName, strerror(errno));
926 } else if (retVal != 0) {
927 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
928 "%s|Failed SCTP Connection after EINPROGRESS, SO_ERROR",
929 peerInfo->enodbName);
930 }
931
932 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
933 message.message.asnLength = rmrMessageBuffer.sendMessage->len;
934 mdclog_write(MDCLOG_ERR, "%s", rmrMessageBuffer.sendMessage->payload);
935 message.message.direction = 'N';
aa7133@att.combe92d712020-03-18 17:30:39 +0200936 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200937 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
938 }
939 memset(peerInfo->asnData, 0, peerInfo->asnLength);
940 peerInfo->asnLength = 0;
941 peerInfo->mtype = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200942 return;
943 }
944
945 peerInfo->isConnected = true;
946
947 if (modifyToEpoll(params->epoll_fd, peerInfo, (EPOLLIN | EPOLLET), params->sctpMap, peerInfo->enodbName,
aa7133@att.combe92d712020-03-18 17:30:39 +0200948 peerInfo->mtype) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200949 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_MOD");
ss412g3bac2da2020-01-05 11:52:19 +0200950 return;
951 }
952
953 message.message.asndata = (unsigned char *)peerInfo->asnData;
954 message.message.asnLength = peerInfo->asnLength;
955 message.message.messageType = peerInfo->mtype;
956 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
957 num_of_messages.fetch_add(1, std::memory_order_release);
958 if (mdclog_level_get() >= MDCLOG_DEBUG) {
959 mdclog_write(MDCLOG_DEBUG, "send the delayed SETUP/ENDC SETUP to sctp for %s",
960 message.message.enodbName);
961 }
aa7133@att.combe92d712020-03-18 17:30:39 +0200962 if (sendSctpMsg(peerInfo, message, params->sctpMap) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200963 if (mdclog_level_get() >= MDCLOG_DEBUG) {
964 mdclog_write(MDCLOG_DEBUG, "Error write to SCTP %s %d", __func__, __LINE__);
965 }
ss412g3bac2da2020-01-05 11:52:19 +0200966 return;
967 }
968
969 memset(peerInfo->asnData, 0, peerInfo->asnLength);
970 peerInfo->asnLength = 0;
971 peerInfo->mtype = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200972}
973
974
975void handlepoll_error(struct epoll_event &event,
976 ReportingMessages_t &message,
977 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200978 sctp_params_t *params) {
ss412g3bac2da2020-01-05 11:52:19 +0200979 if (event.data.fd != params->rmrListenFd) {
980 auto *peerInfo = (ConnectedCU_t *)event.data.ptr;
981 mdclog_write(MDCLOG_ERR, "epoll error, events %0x on fd %d, RAN NAME : %s",
982 event.events, peerInfo->fileDescriptor, peerInfo->enodbName);
983
984 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
985 "%s|Failed SCTP Connection",
986 peerInfo->enodbName);
987 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
988 message.message.asnLength = rmrMessageBuffer.sendMessage->len;
989
990 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
991 message.message.direction = 'N';
aa7133@att.combe92d712020-03-18 17:30:39 +0200992 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200993 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
994 }
995
996 close(peerInfo->fileDescriptor);
aa7133@att.com7b437f72020-03-30 13:25:54 +0300997 params->sctpMap->erase(peerInfo->enodbName);
aa7133@att.combe92d712020-03-18 17:30:39 +0200998 cleanHashEntry((ConnectedCU_t *) event.data.ptr, params->sctpMap);
ss412g3bac2da2020-01-05 11:52:19 +0200999 } else {
1000 mdclog_write(MDCLOG_ERR, "epoll error, events %0x on RMR FD", event.events);
1001 }
ss412g3bac2da2020-01-05 11:52:19 +02001002}
1003/**
1004 *
ss412g1a79bdf2019-10-24 12:03:05 +03001005 * @param socket
1006 * @return
1007 */
1008int setSocketNoBlocking(int socket) {
1009 auto flags = fcntl(socket, F_GETFL, 0);
1010
1011 if (flags == -1) {
1012 mdclog_mdc_add("func", "fcntl");
1013 mdclog_write(MDCLOG_ERR, "%s, %s", __FUNCTION__, strerror(errno));
1014 mdclog_mdc_clean();
1015 return -1;
1016 }
1017
1018 flags = (unsigned) flags | (unsigned) O_NONBLOCK;
1019 if (fcntl(socket, F_SETFL, flags) == -1) {
1020 mdclog_mdc_add("func", "fcntl");
1021 mdclog_write(MDCLOG_ERR, "%s, %s", __FUNCTION__, strerror(errno));
1022 mdclog_mdc_clean();
1023 return -1;
1024 }
1025
1026 return 0;
1027}
1028
1029/**
1030 *
1031 * @param val
1032 * @param m
ss412g1a79bdf2019-10-24 12:03:05 +03001033 */
aa7133@att.combe92d712020-03-18 17:30:39 +02001034void cleanHashEntry(ConnectedCU_t *val, Sctp_Map_t *m) {
ss412g1a79bdf2019-10-24 12:03:05 +03001035 char *dummy;
1036 auto port = (uint16_t) strtol(val->portNumber, &dummy, 10);
aa7133@att.com84bd3342020-03-16 18:04:57 +02001037 char searchBuff[2048]{};
ss412g1a79bdf2019-10-24 12:03:05 +03001038
1039 snprintf(searchBuff, sizeof searchBuff, "host:%s:%d", val->hostName, port);
1040 m->erase(searchBuff);
1041
1042 m->erase(val->enodbName);
1043 free(val);
ss412g1a79bdf2019-10-24 12:03:05 +03001044}
1045
1046/**
1047 *
1048 * @param fd file discriptor
1049 * @param data the asn data to send
1050 * @param len length of the data
1051 * @param enodbName the enodbName as in the map for printing purpose
1052 * @param m map host information
1053 * @param mtype message number
1054 * @return 0 success, anegative number on fail
1055 */
aa7133@att.combe92d712020-03-18 17:30:39 +02001056int sendSctpMsg(ConnectedCU_t *peerInfo, ReportingMessages_t &message, Sctp_Map_t *m) {
ss412g1a79bdf2019-10-24 12:03:05 +03001057 auto loglevel = mdclog_level_get();
1058 int fd = peerInfo->fileDescriptor;
1059 if (loglevel >= MDCLOG_DEBUG) {
1060 mdclog_write(MDCLOG_DEBUG, "Send SCTP message for CU %s, %s",
1061 message.message.enodbName, __FUNCTION__);
1062 }
1063
1064 while (true) {
ss412g1a79bdf2019-10-24 12:03:05 +03001065 if (send(fd,message.message.asndata, message.message.asnLength,MSG_NOSIGNAL) < 0) {
1066 if (errno == EINTR) {
1067 continue;
1068 }
1069 mdclog_write(MDCLOG_ERR, "error writing to CU a message, %s ", strerror(errno));
aa7133@att.com84bd3342020-03-16 18:04:57 +02001070 if (!peerInfo->isConnected) {
1071 mdclog_write(MDCLOG_ERR, "connection to CU %s is still in progress.", message.message.enodbName);
aa7133@att.com84bd3342020-03-16 18:04:57 +02001072 return -1;
1073 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001074 cleanHashEntry(peerInfo, m);
ss412g1a79bdf2019-10-24 12:03:05 +03001075 close(fd);
1076 char key[MAX_ENODB_NAME_SIZE * 2];
1077 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", message.message.enodbName,
1078 message.message.messageType);
1079 if (loglevel >= MDCLOG_DEBUG) {
1080 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
1081 }
aa7133@att.com96c9b882020-03-25 14:50:40 +02001082 auto tmp = m->find(key);
ss412g1a79bdf2019-10-24 12:03:05 +03001083 if (tmp) {
1084 free(tmp);
1085 }
1086 m->erase(key);
ss412g1a79bdf2019-10-24 12:03:05 +03001087 return -1;
1088 }
1089 message.message.direction = 'D';
1090 // send report.buffer of size
1091 buildJsonMessage(message);
1092
1093 if (loglevel >= MDCLOG_DEBUG) {
1094 mdclog_write(MDCLOG_DEBUG,
1095 "SCTP message for CU %s sent from %s",
1096 message.message.enodbName,
1097 __FUNCTION__);
1098 }
ss412g1a79bdf2019-10-24 12:03:05 +03001099 return 0;
1100 }
1101}
1102
1103/**
1104 *
1105 * @param message
1106 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001107 */
aa7133@att.combe92d712020-03-18 17:30:39 +02001108void getRequestMetaData(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03001109 message.message.asndata = rmrMessageBuffer.rcvMessage->payload;
1110 message.message.asnLength = rmrMessageBuffer.rcvMessage->len;
1111
1112 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1113 mdclog_write(MDCLOG_DEBUG, "Message from Xapp RAN name = %s message length = %ld",
1114 message.message.enodbName, (unsigned long) message.message.asnLength);
1115 }
ss412g1a79bdf2019-10-24 12:03:05 +03001116}
1117
1118
ss412g1a79bdf2019-10-24 12:03:05 +03001119
1120/**
1121 *
1122 * @param events
1123 * @param sctpMap
1124 * @param numOfMessages
1125 * @param rmrMessageBuffer
1126 * @param ts
ss412g1a79bdf2019-10-24 12:03:05 +03001127 * @return
1128 */
1129int receiveDataFromSctp(struct epoll_event *events,
1130 Sctp_Map_t *sctpMap,
1131 int &numOfMessages,
1132 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +02001133 struct timespec &ts) {
ss412g1a79bdf2019-10-24 12:03:05 +03001134 /* We have data on the fd waiting to be read. Read and display it.
1135 * We must read whatever data is available completely, as we are running
1136 * in edge-triggered mode and won't get a notification again for the same data. */
aa7133@att.com84bd3342020-03-16 18:04:57 +02001137 ReportingMessages_t message {};
1138 auto done = 0;
ss412g1a79bdf2019-10-24 12:03:05 +03001139 auto loglevel = mdclog_level_get();
aa7133@att.com84bd3342020-03-16 18:04:57 +02001140
ss412g1a79bdf2019-10-24 12:03:05 +03001141 // get the identity of the interface
aa7133@att.com84bd3342020-03-16 18:04:57 +02001142 message.peerInfo = (ConnectedCU_t *)events->data.ptr;
1143
ss412g1a79bdf2019-10-24 12:03:05 +03001144 struct timespec start{0, 0};
1145 struct timespec decodestart{0, 0};
1146 struct timespec end{0, 0};
1147
1148 E2AP_PDU_t *pdu = nullptr;
1149
ss412g1a79bdf2019-10-24 12:03:05 +03001150 while (true) {
1151 if (loglevel >= MDCLOG_DEBUG) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001152 mdclog_write(MDCLOG_DEBUG, "Start Read from SCTP %d fd", message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001153 clock_gettime(CLOCK_MONOTONIC, &start);
1154 }
1155 // read the buffer directly to rmr payload
1156 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1157 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
aa7133@att.com84bd3342020-03-16 18:04:57 +02001158 read(message.peerInfo->fileDescriptor, rmrMessageBuffer.sendMessage->payload, RECEIVE_SCTP_BUFFER_SIZE);
1159
ss412g1a79bdf2019-10-24 12:03:05 +03001160 if (loglevel >= MDCLOG_DEBUG) {
1161 mdclog_write(MDCLOG_DEBUG, "Finish Read from SCTP %d fd message length = %ld",
aa7133@att.com96c9b882020-03-25 14:50:40 +02001162 message.peerInfo->fileDescriptor, message.message.asnLength);
ss412g1a79bdf2019-10-24 12:03:05 +03001163 }
aa7133@att.com16c799b2020-03-24 18:01:51 +02001164
aa7133@att.com84bd3342020-03-16 18:04:57 +02001165 memcpy(message.message.enodbName, message.peerInfo->enodbName, sizeof(message.peerInfo->enodbName));
ss412g1a79bdf2019-10-24 12:03:05 +03001166 message.message.direction = 'U';
1167 message.message.time.tv_nsec = ts.tv_nsec;
1168 message.message.time.tv_sec = ts.tv_sec;
1169
1170 if (message.message.asnLength < 0) {
1171 if (errno == EINTR) {
1172 continue;
1173 }
1174 /* If errno == EAGAIN, that means we have read all
aa7133@att.com84bd3342020-03-16 18:04:57 +02001175 data. So goReportingMessages_t back to the main loop. */
ss412g1a79bdf2019-10-24 12:03:05 +03001176 if (errno != EAGAIN) {
1177 mdclog_write(MDCLOG_ERR, "Read error, %s ", strerror(errno));
1178 done = 1;
1179 } else if (loglevel >= MDCLOG_DEBUG) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001180 mdclog_write(MDCLOG_DEBUG, "EAGAIN - descriptor = %d", message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001181 }
1182 break;
1183 } else if (message.message.asnLength == 0) {
1184 /* End of file. The remote has closed the connection. */
1185 if (loglevel >= MDCLOG_INFO) {
1186 mdclog_write(MDCLOG_INFO, "END of File Closed connection - descriptor = %d",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001187 message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001188 }
1189 done = 1;
1190 break;
1191 }
1192
ss412g1a79bdf2019-10-24 12:03:05 +03001193 if (loglevel >= MDCLOG_DEBUG) {
1194 char printBuffer[4096]{};
1195 char *tmp = printBuffer;
1196 for (size_t i = 0; i < (size_t)message.message.asnLength; ++i) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001197 snprintf(tmp, 3, "%02x", message.message.asndata[i]);
ss412g1a79bdf2019-10-24 12:03:05 +03001198 tmp += 2;
1199 }
1200 printBuffer[message.message.asnLength] = 0;
1201 clock_gettime(CLOCK_MONOTONIC, &end);
1202 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 +02001203 message.peerInfo->enodbName, end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec);
ss412g1a79bdf2019-10-24 12:03:05 +03001204 mdclog_write(MDCLOG_DEBUG, "PDU buffer length = %ld, data = : %s", message.message.asnLength,
1205 printBuffer);
1206 clock_gettime(CLOCK_MONOTONIC, &decodestart);
1207 }
1208
aa7133@att.com7b437f72020-03-30 13:25:54 +03001209 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, (void **) &pdu,
ss412g1a79bdf2019-10-24 12:03:05 +03001210 message.message.asndata, message.message.asnLength);
1211 if (rval.code != RC_OK) {
1212 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2AP PDU from RAN : %s", rval.code,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001213 message.peerInfo->enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001214 break;
1215 }
1216
1217 if (loglevel >= MDCLOG_DEBUG) {
1218 clock_gettime(CLOCK_MONOTONIC, &end);
1219 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 +02001220 message.peerInfo->enodbName, end.tv_sec - decodestart.tv_sec, end.tv_nsec - decodestart.tv_nsec);
ss412g1a79bdf2019-10-24 12:03:05 +03001221 char *printBuffer;
1222 size_t size;
1223 FILE *stream = open_memstream(&printBuffer, &size);
1224 asn_fprint(stream, &asn_DEF_E2AP_PDU, pdu);
1225 mdclog_write(MDCLOG_DEBUG, "Encoding E2AP PDU past : %s", printBuffer);
1226 clock_gettime(CLOCK_MONOTONIC, &decodestart);
1227 }
1228
1229 switch (pdu->present) {
1230 case E2AP_PDU_PR_initiatingMessage: {//initiating message
aa7133@att.com7b437f72020-03-30 13:25:54 +03001231 asnInitiatingRequest(pdu, sctpMap,message, rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001232 break;
1233 }
1234 case E2AP_PDU_PR_successfulOutcome: { //successful outcome
aa7133@att.com7b437f72020-03-30 13:25:54 +03001235 asnSuccsesfulMsg(pdu, sctpMap, message, rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001236 break;
1237 }
1238 case E2AP_PDU_PR_unsuccessfulOutcome: { //Unsuccessful Outcome
aa7133@att.com7b437f72020-03-30 13:25:54 +03001239 asnUnSuccsesfulMsg(pdu, sctpMap, message, rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001240 break;
1241 }
1242 default:
1243 mdclog_write(MDCLOG_ERR, "Unknown index %d in E2AP PDU", pdu->present);
1244 break;
1245 }
1246 if (loglevel >= MDCLOG_DEBUG) {
1247 clock_gettime(CLOCK_MONOTONIC, &end);
1248 mdclog_write(MDCLOG_DEBUG,
1249 "After processing message and sent to rmr for : %s, Read time is : %ld seconds, %ld nanoseconds",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001250 message.peerInfo->enodbName, end.tv_sec - decodestart.tv_sec, end.tv_nsec - decodestart.tv_nsec);
ss412g1a79bdf2019-10-24 12:03:05 +03001251 }
1252 numOfMessages++;
ss412g1a79bdf2019-10-24 12:03:05 +03001253 if (pdu != nullptr) {
aa7133@att.com1fecb052020-03-29 09:44:09 +03001254 ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, pdu);
1255 //ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
aa7133@att.com9dff4f02020-03-29 09:55:22 +03001256 //pdu = nullptr;
ss412g1a79bdf2019-10-24 12:03:05 +03001257 }
ss412g1a79bdf2019-10-24 12:03:05 +03001258 }
ss412g1a79bdf2019-10-24 12:03:05 +03001259
1260 if (done) {
1261 if (loglevel >= MDCLOG_INFO) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001262 mdclog_write(MDCLOG_INFO, "Closed connection - descriptor = %d", message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001263 }
1264 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
1265 snprintf((char *)rmrMessageBuffer.sendMessage->payload,
aa7133@att.com96c9b882020-03-25 14:50:40 +02001266 256,
1267 "%s|CU disconnected unexpectedly",
1268 message.peerInfo->enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001269 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1270
1271 if (sendRequestToXapp(message,
1272 RIC_SCTP_CONNECTION_FAILURE,
aa7133@att.combe92d712020-03-18 17:30:39 +02001273 rmrMessageBuffer) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03001274 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
1275 }
1276
1277 /* Closing descriptor make epoll remove it from the set of descriptors which are monitored. */
aa7133@att.com84bd3342020-03-16 18:04:57 +02001278 close(message.peerInfo->fileDescriptor);
aa7133@att.combe92d712020-03-18 17:30:39 +02001279 cleanHashEntry((ConnectedCU_t *) events->data.ptr, sctpMap);
ss412g1a79bdf2019-10-24 12:03:05 +03001280 }
1281 if (loglevel >= MDCLOG_DEBUG) {
1282 clock_gettime(CLOCK_MONOTONIC, &end);
1283 mdclog_write(MDCLOG_DEBUG, "from receive SCTP to send RMR time is %ld seconds and %ld nanoseconds",
1284 end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec);
1285
1286 }
ss412g1a79bdf2019-10-24 12:03:05 +03001287 return 0;
1288}
1289
aa7133@att.com84bd3342020-03-16 18:04:57 +02001290static void buildAndsendSetupRequest(ReportingMessages_t &message,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001291 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001292 E2AP_PDU_t *pdu/*,
ss412g4aeeda22020-06-16 14:12:41 +03001293 string const &messageName,
1294 string const &ieName,
1295 vector<string> &functionsToAdd_v,
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001296 vector<string> &functionsToModified_v*/) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001297 auto logLevel = mdclog_level_get();
aa7133@att.com84bd3342020-03-16 18:04:57 +02001298 // now we can send the data to e2Mgr
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001299
ss412g4aeeda22020-06-16 14:12:41 +03001300 asn_enc_rval_t er;
1301 auto buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001302 unsigned char buffer[RECEIVE_SCTP_BUFFER_SIZE * 2];
ss412g4aeeda22020-06-16 14:12:41 +03001303 while (true) {
ss412g4aeeda22020-06-16 14:12:41 +03001304 er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu, buffer, buffer_size);
1305 if (er.encoded == -1) {
1306 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1307 return;
1308 } else if (er.encoded > (ssize_t) buffer_size) {
1309 buffer_size = er.encoded + 128;
1310 mdclog_write(MDCLOG_WARN, "Buffer of size %d is to small for %s. Reallocate buffer of size %d",
1311 (int) buffer_size,
1312 asn_DEF_E2AP_PDU.name, buffer_size);
1313 buffer_size = er.encoded + 128;
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001314// free(buffer);
ss412g4aeeda22020-06-16 14:12:41 +03001315 continue;
1316 }
1317 buffer[er.encoded] = '\0';
1318 break;
1319 }
1320 // encode to xml
1321
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001322 string res((char *)buffer);
1323 res.erase(std::remove(res.begin(), res.end(), '\n'), res.end());
1324 res.erase(std::remove(res.begin(), res.end(), '\t'), res.end());
1325 res.erase(std::remove(res.begin(), res.end(), ' '), res.end());
1326
1327// string res {};
1328// if (!functionsToAdd_v.empty() || !functionsToModified_v.empty()) {
1329// res = buildXmlData(messageName, ieName, functionsToAdd_v, functionsToModified_v, buffer, (size_t) er.encoded);
1330// }
ss412g4aeeda22020-06-16 14:12:41 +03001331 rmr_mbuf_t *rmrMsg;
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001332// if (res.length() == 0) {
1333// rmrMsg = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, buffer_size + 256);
1334// rmrMsg->len = snprintf((char *) rmrMsg->payload, RECEIVE_SCTP_BUFFER_SIZE * 2, "%s:%d|%s",
1335// message.peerInfo->sctpParams->myIP.c_str(),
1336// message.peerInfo->sctpParams->rmrPort,
1337// buffer);
1338// } else {
ss412g4aeeda22020-06-16 14:12:41 +03001339 rmrMsg = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, (int)res.length() + 256);
1340 rmrMsg->len = snprintf((char *) rmrMsg->payload, res.length() + 256, "%s:%d|%s",
1341 message.peerInfo->sctpParams->myIP.c_str(),
1342 message.peerInfo->sctpParams->rmrPort,
1343 res.c_str());
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001344// }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001345
ss412g4aeeda22020-06-16 14:12:41 +03001346 if (logLevel >= MDCLOG_DEBUG) {
1347 mdclog_write(MDCLOG_DEBUG, "Setup request of size %d :\n %s\n", rmrMsg->len, rmrMsg->payload);
1348 }
1349 // send to RMR
1350 rmrMsg->mtype = message.message.messageType;
1351 rmrMsg->state = 0;
1352 rmr_bytes2meid(rmrMsg, (unsigned char *) message.message.enodbName, strlen(message.message.enodbName));
aa7133@att.com84bd3342020-03-16 18:04:57 +02001353
ss412g4aeeda22020-06-16 14:12:41 +03001354 static unsigned char tx[32];
1355 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1356 rmr_bytes2xact(rmrMsg, tx, strlen((const char *) tx));
1357
1358 rmrMsg = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMsg);
1359 if (rmrMsg == nullptr) {
1360 mdclog_write(MDCLOG_ERR, "RMR failed to send returned nullptr");
1361 } else if (rmrMsg->state != 0) {
1362 char meid[RMR_MAX_MEID]{};
1363 if (rmrMsg->state == RMR_ERR_RETRY) {
1364 usleep(5);
1365 rmrMsg->state = 0;
1366 mdclog_write(MDCLOG_INFO, "RETRY sending Message %d to Xapp from %s",
1367 rmrMsg->mtype, rmr_get_meid(rmrMsg, (unsigned char *) meid));
1368 rmrMsg = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMsg);
1369 if (rmrMsg == nullptr) {
1370 mdclog_write(MDCLOG_ERR, "RMR failed send returned nullptr");
1371 } else if (rmrMsg->state != 0) {
1372 mdclog_write(MDCLOG_ERR,
1373 "RMR Retry failed %s sending request %d to Xapp from %s",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001374 translateRmrErrorMessages(rmrMsg->state).c_str(),
1375 rmrMsg->mtype,
1376 rmr_get_meid(rmrMsg, (unsigned char *) meid));
1377 }
ss412g4aeeda22020-06-16 14:12:41 +03001378 } else {
1379 mdclog_write(MDCLOG_ERR, "RMR failed: %s. sending request %d to Xapp from %s",
1380 translateRmrErrorMessages(rmrMsg->state).c_str(),
1381 rmrMsg->mtype,
1382 rmr_get_meid(rmrMsg, (unsigned char *) meid));
aa7133@att.combc110772020-03-25 09:45:14 +02001383 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001384 }
ss412g4aeeda22020-06-16 14:12:41 +03001385 message.peerInfo->gotSetup = true;
1386 buildJsonMessage(message);
1387 if (rmrMsg != nullptr) {
1388 rmr_free_msg(rmrMsg);
1389 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001390}
aa7133@att.comc5a15202020-04-05 19:57:01 +03001391
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001392#if 0
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001393int RAN_Function_list_To_Vector(RANfunctions_List_t& list, vector <string> &runFunXML_v) {
1394 auto index = 0;
1395 runFunXML_v.clear();
1396 for (auto j = 0; j < list.list.count; j++) {
1397 auto *raNfunctionItemIEs = (RANfunction_ItemIEs_t *)list.list.array[j];
1398 if (raNfunctionItemIEs->id == ProtocolIE_ID_id_RANfunction_Item &&
1399 (raNfunctionItemIEs->value.present == RANfunction_ItemIEs__value_PR_RANfunction_Item)) {
1400 // encode to xml
1401 E2SM_gNB_NRT_RANfunction_Definition_t *ranFunDef = nullptr;
1402 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER,
1403 &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition,
1404 (void **)&ranFunDef,
1405 raNfunctionItemIEs->value.choice.RANfunction_Item.ranFunctionDefinition.buf,
1406 raNfunctionItemIEs->value.choice.RANfunction_Item.ranFunctionDefinition.size);
1407 if (rval.code != RC_OK) {
1408 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2SM message from : %s",
1409 rval.code,
1410 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name);
1411 return -1;
1412 }
1413
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001414 auto xml_buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
1415 unsigned char xml_buffer[RECEIVE_SCTP_BUFFER_SIZE * 2];
ss412g4aeeda22020-06-16 14:12:41 +03001416 memset(xml_buffer, 0, RECEIVE_SCTP_BUFFER_SIZE * 2);
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001417 // encode to xml
1418 auto er = asn_encode_to_buffer(nullptr,
1419 ATS_BASIC_XER,
1420 &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition,
1421 ranFunDef,
1422 xml_buffer,
1423 xml_buffer_size);
1424 if (er.encoded == -1) {
1425 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s",
1426 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name,
1427 strerror(errno));
1428 } else if (er.encoded > (ssize_t)xml_buffer_size) {
1429 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
1430 (int) xml_buffer_size,
1431 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name, __func__, __LINE__);
1432 } else {
1433 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1434 mdclog_write(MDCLOG_DEBUG, "Encoding E2SM %s PDU number %d : %s",
1435 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name,
1436 index++,
1437 xml_buffer);
1438 }
ss412g4aeeda22020-06-16 14:12:41 +03001439
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001440 string runFuncs = (char *)(xml_buffer);
1441 runFunXML_v.emplace_back(runFuncs);
1442 }
1443 }
1444 }
1445 return 0;
1446}
aa7133@att.comc5a15202020-04-05 19:57:01 +03001447
ss412g4aeeda22020-06-16 14:12:41 +03001448int collectServiceUpdate_RequestData(E2AP_PDU_t *pdu,
1449 Sctp_Map_t *sctpMap,
1450 ReportingMessages_t &message,
1451 vector <string> &RANfunctionsAdded_v,
1452 vector <string> &RANfunctionsModified_v) {
aa7133@att.comc5a15202020-04-05 19:57:01 +03001453 memset(message.peerInfo->enodbName, 0 , MAX_ENODB_NAME_SIZE);
ss412g4aeeda22020-06-16 14:12:41 +03001454 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICserviceUpdate.protocolIEs.list.count; i++) {
1455 auto *ie = pdu->choice.initiatingMessage->value.choice.RICserviceUpdate.protocolIEs.list.array[i];
1456 if (ie->id == ProtocolIE_ID_id_RANfunctionsAdded) {
1457 if (ie->value.present == RICserviceUpdate_IEs__value_PR_RANfunctionsID_List) {
aa7133@att.comc5a15202020-04-05 19:57:01 +03001458 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001459 mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1460 ie->value.choice.RANfunctions_List.list.count);
aa7133@att.comc5a15202020-04-05 19:57:01 +03001461 }
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001462 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsAdded_v) != 0 ) {
1463 return -1;
1464 }
1465 }
1466 } else if (ie->id == ProtocolIE_ID_id_RANfunctionsModified) {
ss412g4aeeda22020-06-16 14:12:41 +03001467 if (ie->value.present == RICserviceUpdate_IEs__value_PR_RANfunctions_List) {
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001468 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1469 mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1470 ie->value.choice.RANfunctions_List.list.count);
1471 }
1472 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsModified_v) != 0 ) {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001473 return -1;
aa7133@att.comc5a15202020-04-05 19:57:01 +03001474 }
1475 }
1476 }
1477 }
1478 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1479 mdclog_write(MDCLOG_DEBUG, "Run function vector have %ld entries",
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001480 RANfunctionsAdded_v.size());
aa7133@att.comc5a15202020-04-05 19:57:01 +03001481 }
1482 return 0;
1483}
ss412g4aeeda22020-06-16 14:12:41 +03001484
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001485#endif
ss412g4aeeda22020-06-16 14:12:41 +03001486
1487
1488void buildPrometheuslist(ConnectedCU_t *peerInfo, Family<Counter> *prometheusFamily) {
aa7133@att.coma4c80012020-06-24 16:36:47 +03001489 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"SetupRequest", "Messages"}});
1490 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"SetupRequest", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001491
aa7133@att.coma4c80012020-06-24 16:36:47 +03001492 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ErrorIndication", "Messages"}});
1493 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ErrorIndication", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001494
aa7133@att.coma4c80012020-06-24 16:36:47 +03001495 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_RICindication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICindication", "Messages"}});
1496 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_RICindication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICindication", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001497
aa7133@att.coma4c80012020-06-24 16:36:47 +03001498 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetRequest", "Messages"}});
1499 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetRequest", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001500
aa7133@att.coma4c80012020-06-24 16:36:47 +03001501 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICserviceUpdate", "Messages"}});
1502 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICserviceUpdate", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001503 // ---------------------------------------------
aa7133@att.coma4c80012020-06-24 16:36:47 +03001504 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetACK", "Messages"}});
1505 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001506
aa7133@att.coma4c80012020-06-24 16:36:47 +03001507 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolACK", "Messages"}});
1508 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001509
aa7133@att.coma4c80012020-06-24 16:36:47 +03001510 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionACK", "Messages"}});
1511 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001512
aa7133@att.coma4c80012020-06-24 16:36:47 +03001513 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteACK", "Messages"}});
1514 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001515 //-------------------------------------------------------------
1516
aa7133@att.coma4c80012020-06-24 16:36:47 +03001517 peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolFailure", "Messages"}});
1518 peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001519
aa7133@att.coma4c80012020-06-24 16:36:47 +03001520 peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionFailure", "Messages"}});
1521 peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001522
aa7133@att.coma4c80012020-06-24 16:36:47 +03001523 peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteFailure", "Messages"}});
1524 peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001525
1526 //====================================================================================
aa7133@att.coma4c80012020-06-24 16:36:47 +03001527 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ErrorIndication", "Messages"}});
1528 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ErrorIndication", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001529
aa7133@att.coma4c80012020-06-24 16:36:47 +03001530 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetRequest", "Messages"}});
1531 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetRequest", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001532
aa7133@att.coma4c80012020-06-24 16:36:47 +03001533 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICcontrol", "Messages"}});
1534 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICcontrol", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001535
aa7133@att.coma4c80012020-06-24 16:36:47 +03001536 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICserviceQuery)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceQuery", "Messages"}});
1537 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICserviceQuery)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceQuery", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001538
aa7133@att.coma4c80012020-06-24 16:36:47 +03001539 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscription", "Messages"}});
1540 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscription", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001541
aa7133@att.coma4c80012020-06-24 16:36:47 +03001542 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscriptionDelete", "Messages"}});
1543 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscriptionDelete", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001544 //---------------------------------------------------------------------------------------------------------
aa7133@att.coma4c80012020-06-24 16:36:47 +03001545 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupResponse", "Messages"}});
1546 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupResponse", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001547
aa7133@att.coma4c80012020-06-24 16:36:47 +03001548 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetACK", "Messages"}});
1549 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001550
aa7133@att.coma4c80012020-06-24 16:36:47 +03001551 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateResponse", "Messages"}});
1552 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateResponse", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001553 //----------------------------------------------------------------------------------------------------------------
aa7133@att.coma4c80012020-06-24 16:36:47 +03001554 peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupRequestFailure", "Messages"}});
1555 peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupRequestFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001556
aa7133@att.coma4c80012020-06-24 16:36:47 +03001557 peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateFailure", "Messages"}});
1558 peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001559}
1560/**
1561 *
1562 * @param pdu
1563 * @param sctpMap
1564 * @param message
1565 * @param RANfunctionsAdded_v
1566 * @return
1567 */
1568int collectSetupRequestData(E2AP_PDU_t *pdu,
1569 Sctp_Map_t *sctpMap,
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001570 ReportingMessages_t &message /*, vector <string> &RANfunctionsAdded_v*/) {
ss412g4aeeda22020-06-16 14:12:41 +03001571 memset(message.peerInfo->enodbName, 0 , MAX_ENODB_NAME_SIZE);
1572 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.count; i++) {
1573 auto *ie = pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.array[i];
1574 if (ie->id == ProtocolIE_ID_id_GlobalE2node_ID) {
1575 // get the ran name for meid
1576 if (ie->value.present == E2setupRequestIEs__value_PR_GlobalE2node_ID) {
1577 if (buildRanName(message.peerInfo->enodbName, ie) < 0) {
1578 mdclog_write(MDCLOG_ERR, "Bad param in E2setupRequestIEs GlobalE2node_ID.\n");
1579 // no mesage will be sent
1580 return -1;
1581 }
1582
1583 memcpy(message.message.enodbName, message.peerInfo->enodbName, strlen(message.peerInfo->enodbName));
1584 sctpMap->setkey(message.message.enodbName, message.peerInfo);
1585 }
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001586 } /*else if (ie->id == ProtocolIE_ID_id_RANfunctionsAdded) {
ss412g4aeeda22020-06-16 14:12:41 +03001587 if (ie->value.present == E2setupRequestIEs__value_PR_RANfunctions_List) {
1588 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1589 mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1590 ie->value.choice.RANfunctions_List.list.count);
1591 }
1592 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsAdded_v) != 0 ) {
1593 return -1;
1594 }
1595 }
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001596 } */
ss412g4aeeda22020-06-16 14:12:41 +03001597 }
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001598// if (mdclog_level_get() >= MDCLOG_DEBUG) {
1599// mdclog_write(MDCLOG_DEBUG, "Run function vector have %ld entries",
1600// RANfunctionsAdded_v.size());
1601// }
ss412g4aeeda22020-06-16 14:12:41 +03001602 return 0;
1603}
1604
1605int XML_From_PER(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
1606 E2AP_PDU_t *pdu = nullptr;
1607
1608 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1609 mdclog_write(MDCLOG_DEBUG, "got PER message of size %d is:%s",
1610 rmrMessageBuffer.sendMessage->len, rmrMessageBuffer.sendMessage->payload);
1611 }
1612 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, (void **) &pdu,
1613 rmrMessageBuffer.sendMessage->payload, rmrMessageBuffer.sendMessage->len);
1614 if (rval.code != RC_OK) {
1615 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) setup response from E2MGR : %s",
1616 rval.code,
1617 message.message.enodbName);
1618 return -1;
1619 }
1620
1621 int buff_size = RECEIVE_XAPP_BUFFER_SIZE;
1622 auto er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu,
1623 rmrMessageBuffer.sendMessage->payload, buff_size);
1624 if (er.encoded == -1) {
1625 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1626 return -1;
1627 } else if (er.encoded > (ssize_t)buff_size) {
1628 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
1629 (int)rmrMessageBuffer.sendMessage->len,
1630 asn_DEF_E2AP_PDU.name,
1631 __func__,
1632 __LINE__);
1633 return -1;
1634 }
1635 rmrMessageBuffer.sendMessage->len = er.encoded;
1636 return 0;
1637
1638}
1639
ss412g1a79bdf2019-10-24 12:03:05 +03001640/**
1641 *
1642 * @param pdu
1643 * @param message
1644 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001645 */
1646void asnInitiatingRequest(E2AP_PDU_t *pdu,
aa7133@att.com7b437f72020-03-30 13:25:54 +03001647 Sctp_Map_t *sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +03001648 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +02001649 RmrMessagesBuffer_t &rmrMessageBuffer) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001650 auto logLevel = mdclog_level_get();
ss412g1a79bdf2019-10-24 12:03:05 +03001651 auto procedureCode = ((InitiatingMessage_t *) pdu->choice.initiatingMessage)->procedureCode;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001652 if (logLevel >= MDCLOG_DEBUG) {
1653 mdclog_write(MDCLOG_DEBUG, "Initiating message %ld\n", procedureCode);
ss412g1a79bdf2019-10-24 12:03:05 +03001654 }
1655 switch (procedureCode) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001656 case ProcedureCode_id_E2setup: {
1657 if (logLevel >= MDCLOG_DEBUG) {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001658 mdclog_write(MDCLOG_DEBUG, "Got E2setup");
aa7133@att.com84bd3342020-03-16 18:04:57 +02001659 }
1660
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001661// vector <string> RANfunctionsAdded_v;
1662// vector <string> RANfunctionsModified_v;
1663// RANfunctionsAdded_v.clear();
1664// RANfunctionsModified_v.clear();
1665 if (collectSetupRequestData(pdu, sctpMap, message) != 0) {
aa7133@att.com7b437f72020-03-30 13:25:54 +03001666 break;
1667 }
1668
ss412g4aeeda22020-06-16 14:12:41 +03001669 buildPrometheuslist(message.peerInfo, message.peerInfo->sctpParams->prometheusFamily);
1670
1671 string messageName("E2setupRequest");
1672 string ieName("E2setupRequestIEs");
1673 message.message.messageType = RIC_E2_SETUP_REQ;
aa7133@att.coma4c80012020-06-24 16:36:47 +03001674 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
1675 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment((double)message.message.asnLength);
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001676 buildAndsendSetupRequest(message, rmrMessageBuffer, pdu);
ss412g1a79bdf2019-10-24 12:03:05 +03001677 break;
1678 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001679 case ProcedureCode_id_RICserviceUpdate: {
1680 if (logLevel >= MDCLOG_DEBUG) {
1681 mdclog_write(MDCLOG_DEBUG, "Got RICserviceUpdate %s", message.message.enodbName);
1682 }
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001683// vector <string> RANfunctionsAdded_v;
1684// vector <string> RANfunctionsModified_v;
1685// RANfunctionsAdded_v.clear();
1686// RANfunctionsModified_v.clear();
1687// if (collectServiceUpdate_RequestData(pdu, sctpMap, message,
1688// RANfunctionsAdded_v, RANfunctionsModified_v) != 0) {
1689// break;
1690// }
ss412g4aeeda22020-06-16 14:12:41 +03001691
1692 string messageName("RICserviceUpdate");
1693 string ieName("RICserviceUpdateIEs");
1694 message.message.messageType = RIC_SERVICE_UPDATE;
aa7133@att.coma4c80012020-06-24 16:36:47 +03001695 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
1696 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment((double)message.message.asnLength);
ss412g4aeeda22020-06-16 14:12:41 +03001697
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001698 buildAndsendSetupRequest(message, rmrMessageBuffer, pdu);
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001699 break;
1700 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001701 case ProcedureCode_id_ErrorIndication: {
1702 if (logLevel >= MDCLOG_DEBUG) {
1703 mdclog_write(MDCLOG_DEBUG, "Got ErrorIndication %s", message.message.enodbName);
1704 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03001705 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_ErrorIndication]->Increment();
1706 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_ErrorIndication]->Increment((double)message.message.asnLength);
aa7133@att.combe92d712020-03-18 17:30:39 +02001707 if (sendRequestToXapp(message, RIC_ERROR_INDICATION, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001708 mdclog_write(MDCLOG_ERR, "RIC_ERROR_INDICATION failed to send to xAPP");
ss412g1a79bdf2019-10-24 12:03:05 +03001709 }
1710 break;
1711 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001712 case ProcedureCode_id_Reset: {
1713 if (logLevel >= MDCLOG_DEBUG) {
1714 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001715 }
ss412g4aeeda22020-06-16 14:12:41 +03001716
aa7133@att.coma4c80012020-06-24 16:36:47 +03001717 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
1718 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment((double)message.message.asnLength);
ss412g4aeeda22020-06-16 14:12:41 +03001719 if (XML_From_PER(message, rmrMessageBuffer) < 0) {
1720 break;
ss412g1a79bdf2019-10-24 12:03:05 +03001721 }
ss412g4aeeda22020-06-16 14:12:41 +03001722
1723 if (sendRequestToXapp(message, RIC_E2_RESET_REQ, rmrMessageBuffer) != 0) {
1724 mdclog_write(MDCLOG_ERR, "RIC_E2_RESET_REQ message failed to send to xAPP");
aa7133@att.com84bd3342020-03-16 18:04:57 +02001725 }
1726 break;
1727 }
1728 case ProcedureCode_id_RICindication: {
1729 if (logLevel >= MDCLOG_DEBUG) {
1730 mdclog_write(MDCLOG_DEBUG, "Got RICindication %s", message.message.enodbName);
1731 }
1732 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.count; i++) {
ss412g1a79bdf2019-10-24 12:03:05 +03001733 auto messageSent = false;
1734 RICindication_IEs_t *ie = pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.array[i];
aa7133@att.com84bd3342020-03-16 18:04:57 +02001735 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03001736 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1737 }
1738 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001739 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03001740 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1741 }
1742 if (ie->value.present == RICindication_IEs__value_PR_RICrequestID) {
1743 static unsigned char tx[32];
1744 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_INDICATION;
1745 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1746 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
ss412g3bac2da2020-01-05 11:52:19 +02001747 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001748 (unsigned char *)message.message.enodbName,
1749 strlen(message.message.enodbName));
ss412g1a79bdf2019-10-24 12:03:05 +03001750 rmrMessageBuffer.sendMessage->state = 0;
aa7133@att.comc5a15202020-04-05 19:57:01 +03001751 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
1752
1753 //ie->value.choice.RICrequestID.ricInstanceID;
ss412g3bac2da2020-01-05 11:52:19 +02001754 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.comc5a15202020-04-05 19:57:01 +03001755 mdclog_write(MDCLOG_DEBUG, "sub id = %d, mtype = %d, ric instance id %ld, requestor id = %ld",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001756 rmrMessageBuffer.sendMessage->sub_id,
aa7133@att.comc5a15202020-04-05 19:57:01 +03001757 rmrMessageBuffer.sendMessage->mtype,
1758 ie->value.choice.RICrequestID.ricInstanceID,
1759 ie->value.choice.RICrequestID.ricRequestorID);
ss412g3bac2da2020-01-05 11:52:19 +02001760 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03001761 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICindication]->Increment();
1762 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICindication]->Increment((double)message.message.asnLength);
aa7133@att.combe92d712020-03-18 17:30:39 +02001763 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03001764 messageSent = true;
1765 } else {
1766 mdclog_write(MDCLOG_ERR, "RIC request id missing illigal request");
1767 }
1768 }
1769 if (messageSent) {
1770 break;
1771 }
1772 }
1773 break;
1774 }
ss412g1a79bdf2019-10-24 12:03:05 +03001775 default: {
1776 mdclog_write(MDCLOG_ERR, "Undefined or not supported message = %ld", procedureCode);
1777 message.message.messageType = 0; // no RMR message type yet
1778
1779 buildJsonMessage(message);
1780
1781 break;
1782 }
1783 }
ss412g1a79bdf2019-10-24 12:03:05 +03001784}
1785
1786/**
1787 *
1788 * @param pdu
1789 * @param message
ss412g1a79bdf2019-10-24 12:03:05 +03001790 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001791 */
aa7133@att.com7b437f72020-03-30 13:25:54 +03001792void asnSuccsesfulMsg(E2AP_PDU_t *pdu,
1793 Sctp_Map_t *sctpMap,
1794 ReportingMessages_t &message,
1795 RmrMessagesBuffer_t &rmrMessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03001796 auto procedureCode = pdu->choice.successfulOutcome->procedureCode;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001797 auto logLevel = mdclog_level_get();
1798 if (logLevel >= MDCLOG_INFO) {
ss412g1a79bdf2019-10-24 12:03:05 +03001799 mdclog_write(MDCLOG_INFO, "Successful Outcome %ld", procedureCode);
1800 }
1801 switch (procedureCode) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001802 case ProcedureCode_id_Reset: {
1803 if (logLevel >= MDCLOG_DEBUG) {
1804 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001805 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03001806 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
1807 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment((double)message.message.asnLength);
ss412g4aeeda22020-06-16 14:12:41 +03001808 if (XML_From_PER(message, rmrMessageBuffer) < 0) {
1809 break;
1810 }
1811 if (sendRequestToXapp(message, RIC_E2_RESET_RESP, rmrMessageBuffer) != 0) {
1812 mdclog_write(MDCLOG_ERR, "RIC_E2_RESET_RESP message failed to send to xAPP");
ss412g1a79bdf2019-10-24 12:03:05 +03001813 }
1814 break;
1815 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001816 case ProcedureCode_id_RICcontrol: {
1817 if (logLevel >= MDCLOG_DEBUG) {
1818 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001819 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001820 for (auto i = 0;
ss412g1a79bdf2019-10-24 12:03:05 +03001821 i < pdu->choice.successfulOutcome->value.choice.RICcontrolAcknowledge.protocolIEs.list.count; i++) {
1822 auto messageSent = false;
1823 RICcontrolAcknowledge_IEs_t *ie = pdu->choice.successfulOutcome->value.choice.RICcontrolAcknowledge.protocolIEs.list.array[i];
1824 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1825 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1826 }
1827 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
1828 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1829 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1830 }
1831 if (ie->value.present == RICcontrolAcknowledge_IEs__value_PR_RICrequestID) {
1832 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_CONTROL_ACK;
1833 rmrMessageBuffer.sendMessage->state = 0;
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001834// rmrMessageBuffer.sendMessage->sub_id = (int) ie->value.choice.RICrequestID.ricRequestorID;
1835 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
1836
ss412g1a79bdf2019-10-24 12:03:05 +03001837 static unsigned char tx[32];
1838 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1839 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
ss412g3bac2da2020-01-05 11:52:19 +02001840 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001841 (unsigned char *)message.message.enodbName,
1842 strlen(message.message.enodbName));
ss412g1a79bdf2019-10-24 12:03:05 +03001843
aa7133@att.coma4c80012020-06-24 16:36:47 +03001844 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
1845 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment((double)message.message.asnLength);
aa7133@att.combe92d712020-03-18 17:30:39 +02001846 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03001847 messageSent = true;
1848 } else {
1849 mdclog_write(MDCLOG_ERR, "RIC request id missing illigal request");
1850 }
1851 }
1852 if (messageSent) {
1853 break;
1854 }
1855 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001856
1857 break;
1858 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001859 case ProcedureCode_id_RICsubscription: {
1860 if (logLevel >= MDCLOG_DEBUG) {
1861 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
1862 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03001863 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
1864 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment((double)message.message.asnLength);
aa7133@att.combe92d712020-03-18 17:30:39 +02001865 if (sendRequestToXapp(message, RIC_SUB_RESP, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001866 mdclog_write(MDCLOG_ERR, "Subscription successful message failed to send to xAPP");
1867 }
1868 break;
1869 }
1870 case ProcedureCode_id_RICsubscriptionDelete: {
1871 if (logLevel >= MDCLOG_DEBUG) {
1872 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
1873 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03001874 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
1875 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment((double)message.message.asnLength);
aa7133@att.combe92d712020-03-18 17:30:39 +02001876 if (sendRequestToXapp(message, RIC_SUB_DEL_RESP, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001877 mdclog_write(MDCLOG_ERR, "Subscription delete successful message failed to send to xAPP");
1878 }
ss412g1a79bdf2019-10-24 12:03:05 +03001879 break;
1880 }
1881 default: {
1882 mdclog_write(MDCLOG_WARN, "Undefined or not supported message = %ld", procedureCode);
1883 message.message.messageType = 0; // no RMR message type yet
1884 buildJsonMessage(message);
1885
1886 break;
1887 }
1888 }
ss412g1a79bdf2019-10-24 12:03:05 +03001889}
1890
1891/**
1892 *
1893 * @param pdu
1894 * @param message
ss412g1a79bdf2019-10-24 12:03:05 +03001895 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001896 */
1897void asnUnSuccsesfulMsg(E2AP_PDU_t *pdu,
aa7133@att.com7b437f72020-03-30 13:25:54 +03001898 Sctp_Map_t *sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +03001899 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +02001900 RmrMessagesBuffer_t &rmrMessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03001901 auto procedureCode = pdu->choice.unsuccessfulOutcome->procedureCode;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001902 auto logLevel = mdclog_level_get();
1903 if (logLevel >= MDCLOG_INFO) {
ss412g1a79bdf2019-10-24 12:03:05 +03001904 mdclog_write(MDCLOG_INFO, "Unsuccessful Outcome %ld", procedureCode);
1905 }
1906 switch (procedureCode) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001907 case ProcedureCode_id_RICcontrol: {
1908 if (logLevel >= MDCLOG_DEBUG) {
1909 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001910 }
1911 for (int i = 0;
1912 i < pdu->choice.unsuccessfulOutcome->value.choice.RICcontrolFailure.protocolIEs.list.count; i++) {
1913 auto messageSent = false;
1914 RICcontrolFailure_IEs_t *ie = pdu->choice.unsuccessfulOutcome->value.choice.RICcontrolFailure.protocolIEs.list.array[i];
aa7133@att.com84bd3342020-03-16 18:04:57 +02001915 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03001916 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1917 }
1918 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001919 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03001920 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1921 }
1922 if (ie->value.present == RICcontrolFailure_IEs__value_PR_RICrequestID) {
1923 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_CONTROL_FAILURE;
1924 rmrMessageBuffer.sendMessage->state = 0;
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001925// rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricRequestorID;
1926 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
ss412g1a79bdf2019-10-24 12:03:05 +03001927 static unsigned char tx[32];
1928 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1929 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
aa7133@att.com84bd3342020-03-16 18:04:57 +02001930 rmr_bytes2meid(rmrMessageBuffer.sendMessage, (unsigned char *) message.message.enodbName,
1931 strlen(message.message.enodbName));
aa7133@att.coma4c80012020-06-24 16:36:47 +03001932 message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
1933 message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment((double)message.message.asnLength);
aa7133@att.combe92d712020-03-18 17:30:39 +02001934 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03001935 messageSent = true;
1936 } else {
1937 mdclog_write(MDCLOG_ERR, "RIC request id missing illigal request");
1938 }
1939 }
1940 if (messageSent) {
1941 break;
1942 }
1943 }
1944 break;
1945 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001946 case ProcedureCode_id_RICsubscription: {
1947 if (logLevel >= MDCLOG_DEBUG) {
1948 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
1949 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03001950 message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
1951 message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment((double)message.message.asnLength);
aa7133@att.combe92d712020-03-18 17:30:39 +02001952 if (sendRequestToXapp(message, RIC_SUB_FAILURE, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001953 mdclog_write(MDCLOG_ERR, "Subscription unsuccessful message failed to send to xAPP");
1954 }
1955 break;
1956 }
1957 case ProcedureCode_id_RICsubscriptionDelete: {
1958 if (logLevel >= MDCLOG_DEBUG) {
1959 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
1960 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03001961 message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
1962 message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment((double)message.message.asnLength);
ss412g4aeeda22020-06-16 14:12:41 +03001963 if (sendRequestToXapp(message, RIC_SUB_FAILURE, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001964 mdclog_write(MDCLOG_ERR, "Subscription Delete unsuccessful message failed to send to xAPP");
1965 }
1966 break;
1967 }
ss412g1a79bdf2019-10-24 12:03:05 +03001968 default: {
1969 mdclog_write(MDCLOG_WARN, "Undefined or not supported message = %ld", procedureCode);
1970 message.message.messageType = 0; // no RMR message type yet
1971
1972 buildJsonMessage(message);
1973
1974 break;
1975 }
1976 }
ss412g1a79bdf2019-10-24 12:03:05 +03001977}
1978
1979/**
1980 *
1981 * @param message
1982 * @param requestId
1983 * @param rmrMmessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001984 * @return
1985 */
1986int sendRequestToXapp(ReportingMessages_t &message,
1987 int requestId,
aa7133@att.combe92d712020-03-18 17:30:39 +02001988 RmrMessagesBuffer_t &rmrMmessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03001989 rmr_bytes2meid(rmrMmessageBuffer.sendMessage,
1990 (unsigned char *)message.message.enodbName,
1991 strlen(message.message.enodbName));
1992 message.message.messageType = rmrMmessageBuffer.sendMessage->mtype = requestId;
1993 rmrMmessageBuffer.sendMessage->state = 0;
1994 static unsigned char tx[32];
1995 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1996 rmr_bytes2xact(rmrMmessageBuffer.sendMessage, tx, strlen((const char *) tx));
1997
aa7133@att.combe92d712020-03-18 17:30:39 +02001998 auto rc = sendRmrMessage(rmrMmessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03001999 return rc;
2000}
2001
ss412g4aeeda22020-06-16 14:12:41 +03002002/**
2003 *
2004 * @param pSctpParams
2005 */
aa7133@att.combe92d712020-03-18 17:30:39 +02002006void getRmrContext(sctp_params_t &pSctpParams) {
ss412g3bac2da2020-01-05 11:52:19 +02002007 pSctpParams.rmrCtx = nullptr;
aa7133@att.comab0e0f42020-04-06 17:13:58 +03002008 pSctpParams.rmrCtx = rmr_init(pSctpParams.rmrAddress, RECEIVE_XAPP_BUFFER_SIZE, RMRFL_NONE);
ss412g3bac2da2020-01-05 11:52:19 +02002009 if (pSctpParams.rmrCtx == nullptr) {
2010 mdclog_write(MDCLOG_ERR, "Failed to initialize RMR");
ss412g3bac2da2020-01-05 11:52:19 +02002011 return;
ss412g1a79bdf2019-10-24 12:03:05 +03002012 }
2013
ss412g3bac2da2020-01-05 11:52:19 +02002014 rmr_set_stimeout(pSctpParams.rmrCtx, 0); // disable retries for any send operation
ss412g1a79bdf2019-10-24 12:03:05 +03002015 // we need to find that routing table exist and we can run
2016 if (mdclog_level_get() >= MDCLOG_INFO) {
2017 mdclog_write(MDCLOG_INFO, "We are after RMR INIT wait for RMR_Ready");
2018 }
2019 int rmrReady = 0;
2020 int count = 0;
2021 while (!rmrReady) {
ss412g3bac2da2020-01-05 11:52:19 +02002022 if ((rmrReady = rmr_ready(pSctpParams.rmrCtx)) == 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002023 sleep(1);
2024 }
2025 count++;
2026 if (count % 60 == 0) {
2027 mdclog_write(MDCLOG_INFO, "waiting to RMR ready state for %d seconds", count);
2028 }
2029 }
2030 if (mdclog_level_get() >= MDCLOG_INFO) {
2031 mdclog_write(MDCLOG_INFO, "RMR running");
2032 }
ss412g3bac2da2020-01-05 11:52:19 +02002033 rmr_init_trace(pSctpParams.rmrCtx, 200);
2034 // get the RMR fd for the epoll
2035 pSctpParams.rmrListenFd = rmr_get_rcvfd(pSctpParams.rmrCtx);
2036 struct epoll_event event{};
2037 // add RMR fd to epoll
2038 event.events = (EPOLLIN);
2039 event.data.fd = pSctpParams.rmrListenFd;
2040 // add listening RMR FD to epoll
2041 if (epoll_ctl(pSctpParams.epoll_fd, EPOLL_CTL_ADD, pSctpParams.rmrListenFd, &event)) {
2042 mdclog_write(MDCLOG_ERR, "Failed to add RMR descriptor to epoll");
2043 close(pSctpParams.rmrListenFd);
2044 rmr_close(pSctpParams.rmrCtx);
2045 pSctpParams.rmrCtx = nullptr;
2046 }
ss412g1a79bdf2019-10-24 12:03:05 +03002047}
2048
ss412g4aeeda22020-06-16 14:12:41 +03002049/**
2050 *
2051 * @param message
2052 * @param rmrMessageBuffer
2053 * @return
2054 */
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002055int PER_FromXML(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
aa7133@att.com07e256f2020-04-20 15:59:51 +03002056 E2AP_PDU_t *pdu = nullptr;
aa7133@att.com1c4f50f2020-03-30 14:30:52 +03002057
2058 if (mdclog_level_get() >= MDCLOG_DEBUG) {
ss412g4aeeda22020-06-16 14:12:41 +03002059 mdclog_write(MDCLOG_DEBUG, "got xml Format data from xApp of size %d is:%s",
aa7133@att.com07e256f2020-04-20 15:59:51 +03002060 rmrMessageBuffer.rcvMessage->len, rmrMessageBuffer.rcvMessage->payload);
aa7133@att.com1c4f50f2020-03-30 14:30:52 +03002061 }
aa7133@att.com1fecb052020-03-29 09:44:09 +03002062 auto rval = asn_decode(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, (void **) &pdu,
2063 rmrMessageBuffer.rcvMessage->payload, rmrMessageBuffer.rcvMessage->len);
2064 if (rval.code != RC_OK) {
aa7133@att.com1c4f50f2020-03-30 14:30:52 +03002065 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) setup response from E2MGR : %s",
aa7133@att.com1fecb052020-03-29 09:44:09 +03002066 rval.code,
2067 message.message.enodbName);
2068 return -1;
2069 }
2070
aa7133@att.coma0f0db62020-04-01 16:52:50 +03002071 int buff_size = RECEIVE_XAPP_BUFFER_SIZE;
2072 auto er = asn_encode_to_buffer(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, pdu,
2073 rmrMessageBuffer.rcvMessage->payload, buff_size);
aa7133@att.com1fecb052020-03-29 09:44:09 +03002074 if (er.encoded == -1) {
2075 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
2076 return -1;
aa7133@att.coma0f0db62020-04-01 16:52:50 +03002077 } else if (er.encoded > (ssize_t)buff_size) {
2078 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 +03002079 (int)rmrMessageBuffer.rcvMessage->len,
aa7133@att.coma0f0db62020-04-01 16:52:50 +03002080 asn_DEF_E2AP_PDU.name,
2081 __func__,
2082 __LINE__);
aa7133@att.com1fecb052020-03-29 09:44:09 +03002083 return -1;
2084 }
aa7133@att.coma0f0db62020-04-01 16:52:50 +03002085 rmrMessageBuffer.rcvMessage->len = er.encoded;
aa7133@att.com1fecb052020-03-29 09:44:09 +03002086 return 0;
2087}
2088
ss412g1a79bdf2019-10-24 12:03:05 +03002089/**
2090 *
ss412g1a79bdf2019-10-24 12:03:05 +03002091 * @param sctpMap
2092 * @param rmrMessageBuffer
2093 * @param ts
ss412g1a79bdf2019-10-24 12:03:05 +03002094 * @return
2095 */
aa7133@att.com6680c3b2020-03-22 12:06:58 +02002096int receiveXappMessages(Sctp_Map_t *sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +03002097 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +02002098 struct timespec &ts) {
ss412g1a79bdf2019-10-24 12:03:05 +03002099 if (rmrMessageBuffer.rcvMessage == nullptr) {
2100 //we have error
2101 mdclog_write(MDCLOG_ERR, "RMR Allocation message, %s", strerror(errno));
ss412g1a79bdf2019-10-24 12:03:05 +03002102 return -1;
2103 }
2104
2105 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2106 mdclog_write(MDCLOG_DEBUG, "Call to rmr_rcv_msg");
2107 }
2108 rmrMessageBuffer.rcvMessage = rmr_rcv_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.rcvMessage);
2109 if (rmrMessageBuffer.rcvMessage == nullptr) {
2110 mdclog_write(MDCLOG_ERR, "RMR Receving message with null pointer, Realloc rmr mesage buffer");
2111 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
ss412g1a79bdf2019-10-24 12:03:05 +03002112 return -2;
2113 }
2114 ReportingMessages_t message;
2115 message.message.direction = 'D';
2116 message.message.time.tv_nsec = ts.tv_nsec;
2117 message.message.time.tv_sec = ts.tv_sec;
2118
2119 // get message payload
2120 //auto msgData = msg->payload;
2121 if (rmrMessageBuffer.rcvMessage->state != 0) {
2122 mdclog_write(MDCLOG_ERR, "RMR Receving message with stat = %d", rmrMessageBuffer.rcvMessage->state);
ss412g1a79bdf2019-10-24 12:03:05 +03002123 return -1;
2124 }
aa7133@att.com1fecb052020-03-29 09:44:09 +03002125 rmr_get_meid(rmrMessageBuffer.rcvMessage, (unsigned char *)message.message.enodbName);
ss412g4aeeda22020-06-16 14:12:41 +03002126 message.peerInfo = (ConnectedCU_t *) sctpMap->find(message.message.enodbName);
2127 if (message.peerInfo == nullptr) {
2128 auto type = rmrMessageBuffer.rcvMessage->mtype;
2129 switch (type) {
2130 case RIC_SCTP_CLEAR_ALL:
2131 case E2_TERM_KEEP_ALIVE_REQ:
2132 case RIC_HEALTH_CHECK_REQ:
2133 break;
2134 default:
2135 mdclog_write(MDCLOG_ERR, "Failed to send message no CU entry %s", message.message.enodbName);
2136 return -1;
2137 }
2138 }
2139
ss412g1a79bdf2019-10-24 12:03:05 +03002140 switch (rmrMessageBuffer.rcvMessage->mtype) {
aa7133@att.combe92d712020-03-18 17:30:39 +02002141 case RIC_E2_SETUP_RESP : {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002142 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
aa7133@att.com1fecb052020-03-29 09:44:09 +03002143 break;
2144 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002145 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2146 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002147 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2148 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_SETUP_RESP");
2149 return -6;
ss412g1a79bdf2019-10-24 12:03:05 +03002150 }
2151 break;
2152 }
aa7133@att.combe92d712020-03-18 17:30:39 +02002153 case RIC_E2_SETUP_FAILURE : {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002154 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
aa7133@att.com1fecb052020-03-29 09:44:09 +03002155 break;
2156 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002157 message.peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2158 message.peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002159 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2160 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_SETUP_FAILURE");
2161 return -6;
ss412g1a79bdf2019-10-24 12:03:05 +03002162 }
2163 break;
2164 }
aa7133@att.combe92d712020-03-18 17:30:39 +02002165 case RIC_ERROR_INDICATION: {
aa7133@att.coma4c80012020-06-24 16:36:47 +03002166 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_ErrorIndication]->Increment();
2167 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_ErrorIndication]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002168 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2169 mdclog_write(MDCLOG_ERR, "Failed to send RIC_ERROR_INDICATION");
ss412g1a79bdf2019-10-24 12:03:05 +03002170 return -6;
2171 }
2172 break;
2173 }
2174 case RIC_SUB_REQ: {
aa7133@att.coma4c80012020-06-24 16:36:47 +03002175 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
2176 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002177 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002178 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SUB_REQ");
ss412g1a79bdf2019-10-24 12:03:05 +03002179 return -6;
2180 }
2181 break;
2182 }
2183 case RIC_SUB_DEL_REQ: {
aa7133@att.coma4c80012020-06-24 16:36:47 +03002184 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
2185 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002186 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002187 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SUB_DEL_REQ");
ss412g1a79bdf2019-10-24 12:03:05 +03002188 return -6;
2189 }
2190 break;
2191 }
2192 case RIC_CONTROL_REQ: {
aa7133@att.coma4c80012020-06-24 16:36:47 +03002193 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
2194 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002195 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002196 mdclog_write(MDCLOG_ERR, "Failed to send RIC_CONTROL_REQ");
ss412g1a79bdf2019-10-24 12:03:05 +03002197 return -6;
2198 }
2199 break;
2200 }
2201 case RIC_SERVICE_QUERY: {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002202 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2203 break;
2204 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002205 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceQuery]->Increment();
2206 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceQuery]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002207 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002208 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_QUERY");
ss412g1a79bdf2019-10-24 12:03:05 +03002209 return -6;
2210 }
2211 break;
2212 }
2213 case RIC_SERVICE_UPDATE_ACK: {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002214 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2215 break;
2216 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002217 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
2218 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceQuery]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002219 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002220 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_UPDATE_ACK");
ss412g1a79bdf2019-10-24 12:03:05 +03002221 return -6;
2222 }
2223 break;
2224 }
2225 case RIC_SERVICE_UPDATE_FAILURE: {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002226 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2227 break;
2228 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002229 message.peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
2230 message.peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceQuery]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002231 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002232 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_UPDATE_FAILURE");
ss412g1a79bdf2019-10-24 12:03:05 +03002233 return -6;
2234 }
2235 break;
2236 }
ss412g4aeeda22020-06-16 14:12:41 +03002237 case RIC_E2_RESET_REQ: {
2238 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2239 break;
2240 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002241 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2242 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002243 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g4aeeda22020-06-16 14:12:41 +03002244 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_RESET");
ss412g1a79bdf2019-10-24 12:03:05 +03002245 return -6;
2246 }
2247 break;
2248 }
ss412g4aeeda22020-06-16 14:12:41 +03002249 case RIC_E2_RESET_RESP: {
2250 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2251 break;
2252 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002253 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2254 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002255 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g4aeeda22020-06-16 14:12:41 +03002256 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_RESET_RESP");
ss412g1a79bdf2019-10-24 12:03:05 +03002257 return -6;
2258 }
2259 break;
2260 }
2261 case RIC_SCTP_CLEAR_ALL: {
2262 mdclog_write(MDCLOG_INFO, "RIC_SCTP_CLEAR_ALL");
2263 // loop on all keys and close socket and then erase all map.
2264 vector<char *> v;
2265 sctpMap->getKeys(v);
2266 for (auto const &iter : v) { //}; iter != sctpMap.end(); iter++) {
2267 if (!boost::starts_with((string) (iter), "host:") && !boost::starts_with((string) (iter), "msg:")) {
2268 auto *peerInfo = (ConnectedCU_t *) sctpMap->find(iter);
2269 if (peerInfo == nullptr) {
2270 continue;
2271 }
2272 close(peerInfo->fileDescriptor);
2273 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
2274 message.message.direction = 'D';
2275 message.message.time.tv_nsec = ts.tv_nsec;
2276 message.message.time.tv_sec = ts.tv_sec;
2277
2278 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
2279 snprintf((char *)rmrMessageBuffer.sendMessage->payload,
aa7133@att.com96c9b882020-03-25 14:50:40 +02002280 256,
2281 "%s|RIC_SCTP_CLEAR_ALL",
2282 peerInfo->enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03002283 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
2284 mdclog_write(MDCLOG_INFO, "%s", message.message.asndata);
aa7133@att.combe92d712020-03-18 17:30:39 +02002285 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002286 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
2287 }
2288 free(peerInfo);
2289 }
2290 }
2291
2292 sleep(1);
2293 sctpMap->clear();
2294 break;
2295 }
ss412g3bac2da2020-01-05 11:52:19 +02002296 case E2_TERM_KEEP_ALIVE_REQ: {
2297 // send message back
ss412g3bac2da2020-01-05 11:52:19 +02002298 rmr_bytes2payload(rmrMessageBuffer.sendMessage,
aa7133@att.com96c9b882020-03-25 14:50:40 +02002299 (unsigned char *)rmrMessageBuffer.ka_message,
2300 rmrMessageBuffer.ka_message_len);
ss412g3bac2da2020-01-05 11:52:19 +02002301 rmrMessageBuffer.sendMessage->mtype = E2_TERM_KEEP_ALIVE_RESP;
2302 rmrMessageBuffer.sendMessage->state = 0;
2303 static unsigned char tx[32];
2304 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2305 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, txLen);
2306 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2307 if (rmrMessageBuffer.sendMessage == nullptr) {
2308 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
aa7133@att.com77fff432020-01-27 12:01:20 +02002309 mdclog_write(MDCLOG_ERR, "Failed to send E2_TERM_KEEP_ALIVE_RESP RMR message returned NULL");
ss412g3bac2da2020-01-05 11:52:19 +02002310 } else if (rmrMessageBuffer.sendMessage->state != 0) {
aa7133@att.com77fff432020-01-27 12:01:20 +02002311 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 +02002312 rmrMessageBuffer.sendMessage->state, translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str());
aa7133@att.combc110772020-03-25 09:45:14 +02002313 } else if (mdclog_level_get() >= MDCLOG_DEBUG) {
2314 mdclog_write(MDCLOG_DEBUG, "Got Keep Alive Request send : %s", rmrMessageBuffer.ka_message);
ss412g3bac2da2020-01-05 11:52:19 +02002315 }
aa7133@att.com342843e2020-01-26 12:53:34 +02002316
ss412g3bac2da2020-01-05 11:52:19 +02002317 break;
2318 }
ss412g4aeeda22020-06-16 14:12:41 +03002319 case RIC_HEALTH_CHECK_REQ: {
2320 // send message back
2321 rmr_bytes2payload(rmrMessageBuffer.sendMessage,
2322 (unsigned char *)"OK",
2323 2);
2324 rmrMessageBuffer.sendMessage->mtype = RIC_HEALTH_CHECK_RESP;
2325 rmrMessageBuffer.sendMessage->state = 0;
2326 static unsigned char tx[32];
2327 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2328 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, txLen);
2329 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2330 if (rmrMessageBuffer.sendMessage == nullptr) {
2331 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
2332 mdclog_write(MDCLOG_ERR, "Failed to send RIC_HEALTH_CHECK_RESP RMR message returned NULL");
2333 } else if (rmrMessageBuffer.sendMessage->state != 0) {
2334 mdclog_write(MDCLOG_ERR, "Failed to send RIC_HEALTH_CHECK_RESP, on RMR state = %d ( %s)",
2335 rmrMessageBuffer.sendMessage->state, translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str());
2336 } else if (mdclog_level_get() >= MDCLOG_DEBUG) {
2337 mdclog_write(MDCLOG_DEBUG, "Got RIC_HEALTH_CHECK_REQ Request send : OK");
2338 }
2339
2340 break;
2341 }
2342
ss412g1a79bdf2019-10-24 12:03:05 +03002343 default:
2344 mdclog_write(MDCLOG_WARN, "Message Type : %d is not seported", rmrMessageBuffer.rcvMessage->mtype);
2345 message.message.asndata = rmrMessageBuffer.rcvMessage->payload;
2346 message.message.asnLength = rmrMessageBuffer.rcvMessage->len;
2347 message.message.time.tv_nsec = ts.tv_nsec;
2348 message.message.time.tv_sec = ts.tv_sec;
2349 message.message.messageType = rmrMessageBuffer.rcvMessage->mtype;
2350
2351 buildJsonMessage(message);
2352
2353
ss412g1a79bdf2019-10-24 12:03:05 +03002354 return -7;
2355 }
2356 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2357 mdclog_write(MDCLOG_DEBUG, "EXIT OK from %s", __FUNCTION__);
2358 }
ss412g1a79bdf2019-10-24 12:03:05 +03002359 return 0;
2360}
2361
2362/**
2363 * Send message to the CU that is not expecting for successful or unsuccessful results
2364 * @param messageBuffer
2365 * @param message
2366 * @param failedMsgId
2367 * @param sctpMap
ss412g1a79bdf2019-10-24 12:03:05 +03002368 * @return
2369 */
2370int sendDirectionalSctpMsg(RmrMessagesBuffer_t &messageBuffer,
2371 ReportingMessages_t &message,
2372 int failedMsgId,
aa7133@att.combe92d712020-03-18 17:30:39 +02002373 Sctp_Map_t *sctpMap) {
ss412g1a79bdf2019-10-24 12:03:05 +03002374
aa7133@att.combe92d712020-03-18 17:30:39 +02002375 getRequestMetaData(message, messageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03002376 if (mdclog_level_get() >= MDCLOG_INFO) {
2377 mdclog_write(MDCLOG_INFO, "send message to %s address", message.message.enodbName);
2378 }
2379
aa7133@att.combe92d712020-03-18 17:30:39 +02002380 auto rc = sendMessagetoCu(sctpMap, messageBuffer, message, failedMsgId);
ss412g1a79bdf2019-10-24 12:03:05 +03002381 return rc;
2382}
2383
2384/**
2385 *
2386 * @param sctpMap
2387 * @param messageBuffer
2388 * @param message
2389 * @param failedMesgId
ss412g1a79bdf2019-10-24 12:03:05 +03002390 * @return
2391 */
2392int sendMessagetoCu(Sctp_Map_t *sctpMap,
2393 RmrMessagesBuffer_t &messageBuffer,
2394 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +02002395 int failedMesgId) {
ss412g1a79bdf2019-10-24 12:03:05 +03002396 // get the FD
2397 message.message.messageType = messageBuffer.rcvMessage->mtype;
ss412g4aeeda22020-06-16 14:12:41 +03002398 auto rc = sendSctpMsg(message.peerInfo, message, sctpMap);
ss412g1a79bdf2019-10-24 12:03:05 +03002399 return rc;
2400}
2401
2402/**
2403 *
2404 * @param rmrCtx the rmr context to send and receive
2405 * @param msg the msg we got fromxApp
2406 * @param metaData data from xApp in ordered struct
2407 * @param failedMesgId the return message type error
2408 */
2409void
aa7133@att.combe92d712020-03-18 17:30:39 +02002410sendFailedSendingMessagetoXapp(RmrMessagesBuffer_t &rmrMessageBuffer, ReportingMessages_t &message, int failedMesgId) {
ss412g1a79bdf2019-10-24 12:03:05 +03002411 rmr_mbuf_t *msg = rmrMessageBuffer.sendMessage;
2412 msg->len = snprintf((char *) msg->payload, 200, "the gNb/eNode name %s not found",
2413 message.message.enodbName);
2414 if (mdclog_level_get() >= MDCLOG_INFO) {
2415 mdclog_write(MDCLOG_INFO, "%s", msg->payload);
2416 }
2417 msg->mtype = failedMesgId;
2418 msg->state = 0;
2419
2420 static unsigned char tx[32];
2421 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2422 rmr_bytes2xact(msg, tx, strlen((const char *) tx));
2423
aa7133@att.combe92d712020-03-18 17:30:39 +02002424 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03002425}
2426
ss412g1a79bdf2019-10-24 12:03:05 +03002427
ss412g1a79bdf2019-10-24 12:03:05 +03002428
2429/**
2430 *
2431 * @param epoll_fd
2432 * @param peerInfo
2433 * @param events
2434 * @param sctpMap
2435 * @param enodbName
2436 * @param msgType
ss412g1a79bdf2019-10-24 12:03:05 +03002437 * @return
2438 */
2439int addToEpoll(int epoll_fd,
2440 ConnectedCU_t *peerInfo,
2441 uint32_t events,
2442 Sctp_Map_t *sctpMap,
2443 char *enodbName,
aa7133@att.combe92d712020-03-18 17:30:39 +02002444 int msgType) {
ss412g1a79bdf2019-10-24 12:03:05 +03002445 // Add to Epol
2446 struct epoll_event event{};
2447 event.data.ptr = peerInfo;
2448 event.events = events;
2449 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, peerInfo->fileDescriptor, &event) < 0) {
2450 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2451 mdclog_write(MDCLOG_DEBUG, "epoll_ctl EPOLL_CTL_ADD (may chack not to quit here), %s, %s %d",
2452 strerror(errno), __func__, __LINE__);
2453 }
2454 close(peerInfo->fileDescriptor);
aa7133@att.com84bd3342020-03-16 18:04:57 +02002455 if (enodbName != nullptr) {
aa7133@att.combe92d712020-03-18 17:30:39 +02002456 cleanHashEntry(peerInfo, sctpMap);
aa7133@att.com84bd3342020-03-16 18:04:57 +02002457 char key[MAX_ENODB_NAME_SIZE * 2];
2458 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", enodbName, msgType);
2459 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2460 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
2461 }
2462 auto tmp = sctpMap->find(key);
2463 if (tmp) {
2464 free(tmp);
2465 sctpMap->erase(key);
2466 }
2467 } else {
2468 peerInfo->enodbName[0] = 0;
ss412g1a79bdf2019-10-24 12:03:05 +03002469 }
ss412g1a79bdf2019-10-24 12:03:05 +03002470 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may chack not to quit here)");
ss412g1a79bdf2019-10-24 12:03:05 +03002471 return -1;
2472 }
ss412g1a79bdf2019-10-24 12:03:05 +03002473 return 0;
2474}
2475
2476/**
2477 *
2478 * @param epoll_fd
2479 * @param peerInfo
2480 * @param events
2481 * @param sctpMap
2482 * @param enodbName
2483 * @param msgType
ss412g1a79bdf2019-10-24 12:03:05 +03002484 * @return
2485 */
2486int modifyToEpoll(int epoll_fd,
2487 ConnectedCU_t *peerInfo,
2488 uint32_t events,
2489 Sctp_Map_t *sctpMap,
2490 char *enodbName,
aa7133@att.combe92d712020-03-18 17:30:39 +02002491 int msgType) {
ss412g1a79bdf2019-10-24 12:03:05 +03002492 // Add to Epol
2493 struct epoll_event event{};
2494 event.data.ptr = peerInfo;
2495 event.events = events;
2496 if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, peerInfo->fileDescriptor, &event) < 0) {
2497 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2498 mdclog_write(MDCLOG_DEBUG, "epoll_ctl EPOLL_CTL_MOD (may chack not to quit here), %s, %s %d",
2499 strerror(errno), __func__, __LINE__);
2500 }
2501 close(peerInfo->fileDescriptor);
aa7133@att.combe92d712020-03-18 17:30:39 +02002502 cleanHashEntry(peerInfo, sctpMap);
ss412g1a79bdf2019-10-24 12:03:05 +03002503 char key[MAX_ENODB_NAME_SIZE * 2];
2504 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", enodbName, msgType);
2505 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2506 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
2507 }
aa7133@att.com96c9b882020-03-25 14:50:40 +02002508 auto tmp = sctpMap->find(key);
ss412g1a79bdf2019-10-24 12:03:05 +03002509 if (tmp) {
2510 free(tmp);
2511 }
2512 sctpMap->erase(key);
2513 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may chack not to quit here)");
ss412g1a79bdf2019-10-24 12:03:05 +03002514 return -1;
2515 }
ss412g1a79bdf2019-10-24 12:03:05 +03002516 return 0;
2517}
2518
2519
aa7133@att.combe92d712020-03-18 17:30:39 +02002520int sendRmrMessage(RmrMessagesBuffer_t &rmrMessageBuffer, ReportingMessages_t &message) {
ss412g1a79bdf2019-10-24 12:03:05 +03002521 buildJsonMessage(message);
2522
2523 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2524
2525 if (rmrMessageBuffer.sendMessage == nullptr) {
2526 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
2527 mdclog_write(MDCLOG_ERR, "RMR failed send message returned with NULL pointer");
ss412g1a79bdf2019-10-24 12:03:05 +03002528 return -1;
2529 }
2530
2531 if (rmrMessageBuffer.sendMessage->state != 0) {
2532 char meid[RMR_MAX_MEID]{};
2533 if (rmrMessageBuffer.sendMessage->state == RMR_ERR_RETRY) {
2534 usleep(5);
2535 rmrMessageBuffer.sendMessage->state = 0;
2536 mdclog_write(MDCLOG_INFO, "RETRY sending Message type %d to Xapp from %s",
2537 rmrMessageBuffer.sendMessage->mtype,
2538 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
2539 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2540 if (rmrMessageBuffer.sendMessage == nullptr) {
2541 mdclog_write(MDCLOG_ERR, "RMR failed send message returned with NULL pointer");
2542 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
ss412g1a79bdf2019-10-24 12:03:05 +03002543 return -1;
2544 } else if (rmrMessageBuffer.sendMessage->state != 0) {
2545 mdclog_write(MDCLOG_ERR,
2546 "Message state %s while sending request %d to Xapp from %s after retry of 10 microseconds",
2547 translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str(),
2548 rmrMessageBuffer.sendMessage->mtype,
2549 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
2550 auto rc = rmrMessageBuffer.sendMessage->state;
ss412g1a79bdf2019-10-24 12:03:05 +03002551 return rc;
2552 }
2553 } else {
2554 mdclog_write(MDCLOG_ERR, "Message state %s while sending request %d to Xapp from %s",
2555 translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str(),
2556 rmrMessageBuffer.sendMessage->mtype,
2557 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
ss412g1a79bdf2019-10-24 12:03:05 +03002558 return rmrMessageBuffer.sendMessage->state;
2559 }
2560 }
2561 return 0;
2562}
2563
2564void buildJsonMessage(ReportingMessages_t &message) {
ss412g3bac2da2020-01-05 11:52:19 +02002565 if (jsonTrace) {
2566 message.outLen = sizeof(message.base64Data);
2567 base64::encode((const unsigned char *) message.message.asndata,
2568 (const int) message.message.asnLength,
2569 message.base64Data,
2570 message.outLen);
2571 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.comc5a15202020-04-05 19:57:01 +03002572 mdclog_write(MDCLOG_DEBUG, "Tracing: ASN length = %d, base64 message length = %d ",
ss412g3bac2da2020-01-05 11:52:19 +02002573 (int) message.message.asnLength,
2574 (int) message.outLen);
2575 }
2576
2577 snprintf(message.buffer, sizeof(message.buffer),
aa7133@att.com96c9b882020-03-25 14:50:40 +02002578 "{\"header\": {\"ts\": \"%ld.%09ld\","
2579 "\"ranName\": \"%s\","
2580 "\"messageType\": %d,"
2581 "\"direction\": \"%c\"},"
2582 "\"base64Length\": %d,"
2583 "\"asnBase64\": \"%s\"}",
2584 message.message.time.tv_sec,
2585 message.message.time.tv_nsec,
2586 message.message.enodbName,
2587 message.message.messageType,
2588 message.message.direction,
2589 (int) message.outLen,
2590 message.base64Data);
ss412g3bac2da2020-01-05 11:52:19 +02002591 static src::logger_mt &lg = my_logger::get();
2592
2593 BOOST_LOG(lg) << message.buffer;
ss412g1a79bdf2019-10-24 12:03:05 +03002594 }
ss412g1a79bdf2019-10-24 12:03:05 +03002595}
2596
2597
2598/**
2599 * take RMR error code to string
2600 * @param state
2601 * @return
2602 */
2603string translateRmrErrorMessages(int state) {
2604 string str = {};
2605 switch (state) {
2606 case RMR_OK:
2607 str = "RMR_OK - state is good";
2608 break;
2609 case RMR_ERR_BADARG:
2610 str = "RMR_ERR_BADARG - argument passd to function was unusable";
2611 break;
2612 case RMR_ERR_NOENDPT:
2613 str = "RMR_ERR_NOENDPT - send//call could not find an endpoint based on msg type";
2614 break;
2615 case RMR_ERR_EMPTY:
2616 str = "RMR_ERR_EMPTY - msg received had no payload; attempt to send an empty message";
2617 break;
2618 case RMR_ERR_NOHDR:
2619 str = "RMR_ERR_NOHDR - message didn't contain a valid header";
2620 break;
2621 case RMR_ERR_SENDFAILED:
2622 str = "RMR_ERR_SENDFAILED - send failed; errno has nano reason";
2623 break;
2624 case RMR_ERR_CALLFAILED:
2625 str = "RMR_ERR_CALLFAILED - unable to send call() message";
2626 break;
2627 case RMR_ERR_NOWHOPEN:
2628 str = "RMR_ERR_NOWHOPEN - no wormholes are open";
2629 break;
2630 case RMR_ERR_WHID:
2631 str = "RMR_ERR_WHID - wormhole id was invalid";
2632 break;
2633 case RMR_ERR_OVERFLOW:
2634 str = "RMR_ERR_OVERFLOW - operation would have busted through a buffer/field size";
2635 break;
2636 case RMR_ERR_RETRY:
2637 str = "RMR_ERR_RETRY - request (send/call/rts) failed, but caller should retry (EAGAIN for wrappers)";
2638 break;
2639 case RMR_ERR_RCVFAILED:
2640 str = "RMR_ERR_RCVFAILED - receive failed (hard error)";
2641 break;
2642 case RMR_ERR_TIMEOUT:
2643 str = "RMR_ERR_TIMEOUT - message processing call timed out";
2644 break;
2645 case RMR_ERR_UNSET:
2646 str = "RMR_ERR_UNSET - the message hasn't been populated with a transport buffer";
2647 break;
2648 case RMR_ERR_TRUNC:
2649 str = "RMR_ERR_TRUNC - received message likely truncated";
2650 break;
2651 case RMR_ERR_INITFAILED:
2652 str = "RMR_ERR_INITFAILED - initialisation of something (probably message) failed";
2653 break;
2654 case RMR_ERR_NOTSUPP:
2655 str = "RMR_ERR_NOTSUPP - the request is not supported, or RMr was not initialised for the request";
2656 break;
2657 default:
2658 char buf[128]{};
2659 snprintf(buf, sizeof buf, "UNDOCUMENTED RMR_ERR : %d", state);
2660 str = buf;
2661 break;
2662 }
2663 return str;
2664}
2665
2666