blob: 16430fecd232fe29c1cb270d263cf122d45d15f0 [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;
aa7133@att.com41fcb3f2020-11-29 13:48:25 +020055bool jsonTrace = false;
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
aa7133@att.com409f2232020-10-29 13:06:45 +030071double approx_CPU_MHz(unsigned sleepTime) {
ss412g3bac2da2020-01-05 11:52:19 +020072 using namespace std::chrono_literals;
73 uint32_t aux = 0;
74 uint64_t cycles_start = rdtscp(aux);
75 double time_start = age();
aa7133@att.com409f2232020-10-29 13:06:45 +030076 std::this_thread::sleep_for(sleepTime * 1ms);
ss412g3bac2da2020-01-05 11:52:19 +020077 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.com409f2232020-10-29 13:06:45 +030094 struct sockaddr_in6 serverAddress {};
95 serverAddress.sin6_family = AF_INET6;
96 serverAddress.sin6_addr = in6addr_any;
97 serverAddress.sin6_port = htons(sctpParams.sctpPort);
98 if (bind(sctpParams.listenFD, (SA *)&serverAddress, sizeof(serverAddress)) < 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) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300107 struct sockaddr_in6 clientAddress {};
108 socklen_t len = sizeof(clientAddress);
109 getsockname(sctpParams.listenFD, (SA *)&clientAddress, &len);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200110 char buff[1024] {};
aa7133@att.com409f2232020-10-29 13:06:45 +0300111 inet_ntop(AF_INET6, &clientAddress.sin6_addr, buff, sizeof(buff));
112 mdclog_write(MDCLOG_DEBUG, "My address: %s, port %d\n", buff, htons(clientAddress.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) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300155 mdclog_write(MDCLOG_ERR, "illegal 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) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300163 mdclog_write(MDCLOG_ERR, "illegal loglevel. Set loglevel to MDCLOG_INFO");
ss412g3bac2da2020-01-05 11:52:19 +0200164 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 {
aa7133@att.com409f2232020-10-29 13:06:45 +0300177 mdclog_write(MDCLOG_ERR, "illegal loglevel = %s. Set loglevel to MDCLOG_INFO", tmpStr.c_str());
ss412g3bac2da2020-01-05 11:52:19 +0200178 sctpParams.logLevel = MDCLOG_INFO;
179 }
180 mdclog_level_set(sctpParams.logLevel);
181
182 tmpStr = conf.getStringValue("volume");
183 if (tmpStr.length() == 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300184 mdclog_write(MDCLOG_ERR, "illegal 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) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300201 mdclog_write(MDCLOG_ERR, "illegal 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) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300207 mdclog_write(MDCLOG_ERR, "illegal SCTP port ");
aa7133@att.combe92d712020-03-18 17:30:39 +0200208 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) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300214 mdclog_write(MDCLOG_ERR, "illegal external-fqdn");
aa7133@att.com84bd3342020-03-16 18:04:57 +0200215 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) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300220 mdclog_write(MDCLOG_ERR, "illegal 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) {
aa7133@att.com54885fe2020-10-29 17:24:49 +0300225 mdclog_write(MDCLOG_ERR, "illegal pod_name or environment variable 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) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300231 mdclog_write(MDCLOG_ERR, "illegal 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;
aa7133@att.com41fcb3f2020-11-29 13:48:25 +0200244 } else {
245 mdclog_write(MDCLOG_ERR, "Trace was set to wrong value %s, set to stop", tmpStr.c_str());
aa7133@att.com92aeebe2020-11-29 14:22:09 +0200246 sctpParams.trace = false;
ss412g3bac2da2020-01-05 11:52:19 +0200247 }
248 jsonTrace = sctpParams.trace;
ss412g1a79bdf2019-10-24 12:03:05 +0300249
ss412g4aeeda22020-06-16 14:12:41 +0300250 sctpParams.epollTimeOut = -1;
ss412g4aeeda22020-06-16 14:12:41 +0300251
252 tmpStr = conf.getStringValue("prometheusPort");
253 if (tmpStr.length() != 0) {
254 sctpParams.prometheusPort = tmpStr;
255 }
256
aa7133@att.combe92d712020-03-18 17:30:39 +0200257 sctpParams.ka_message_length = snprintf(sctpParams.ka_message, KA_MESSAGE_SIZE, "{\"address\": \"%s:%d\","
aa7133@att.com96c9b882020-03-25 14:50:40 +0200258 "\"fqdn\": \"%s\","
259 "\"pod_name\": \"%s\"}",
aa7133@att.com342843e2020-01-26 12:53:34 +0200260 (const char *)sctpParams.myIP.c_str(),
261 sctpParams.rmrPort,
262 sctpParams.fqdn.c_str(),
263 sctpParams.podName.c_str());
264
ss412g1a79bdf2019-10-24 12:03:05 +0300265 if (mdclog_level_get() >= MDCLOG_INFO) {
ss412g3bac2da2020-01-05 11:52:19 +0200266 mdclog_mdc_add("RMR Port", to_string(sctpParams.rmrPort).c_str());
267 mdclog_mdc_add("LogLevel", to_string(sctpParams.logLevel).c_str());
268 mdclog_mdc_add("volume", sctpParams.volume);
ss412g1a79bdf2019-10-24 12:03:05 +0300269 mdclog_mdc_add("tmpLogFilespec", tmpLogFilespec);
ss412g3bac2da2020-01-05 11:52:19 +0200270 mdclog_mdc_add("my ip", sctpParams.myIP.c_str());
aa7133@att.com342843e2020-01-26 12:53:34 +0200271 mdclog_mdc_add("pod name", sctpParams.podName.c_str());
ss412g1a79bdf2019-10-24 12:03:05 +0300272
aa7133@att.com342843e2020-01-26 12:53:34 +0200273 mdclog_write(MDCLOG_INFO, "running parameters for instance : %s", sctpParams.ka_message);
ss412g1a79bdf2019-10-24 12:03:05 +0300274 }
275 mdclog_mdc_clean();
276
277 // Files written to the current working directory
278 boostLogger = logging::add_file_log(
ss412g3bac2da2020-01-05 11:52:19 +0200279 keywords::file_name = tmpLogFilespec, // to temp directory
ss412g1a79bdf2019-10-24 12:03:05 +0300280 keywords::rotation_size = 10 * 1024 * 1024,
281 keywords::time_based_rotation = sinks::file::rotation_at_time_interval(posix_time::hours(1)),
282 keywords::format = "%Message%"
ss412g3bac2da2020-01-05 11:52:19 +0200283 //keywords::format = "[%TimeStamp%]: %Message%" // use each tmpStr with time stamp
ss412g1a79bdf2019-10-24 12:03:05 +0300284 );
285
aa7133@att.com54885fe2020-10-29 17:24:49 +0300286 // Setup a destination folder for collecting rotated (closed) files --since the same volume can use rename()
ss412g1a79bdf2019-10-24 12:03:05 +0300287 boostLogger->locked_backend()->set_file_collector(sinks::file::make_collector(
ss412g3bac2da2020-01-05 11:52:19 +0200288 keywords::target = sctpParams.volume
ss412g1a79bdf2019-10-24 12:03:05 +0300289 ));
290
291 // Upon restart, scan the directory for files matching the file_name pattern
292 boostLogger->locked_backend()->scan_for_files();
293
ss412g3bac2da2020-01-05 11:52:19 +0200294 // Enable auto-flushing after each tmpStr record written
ss412g1a79bdf2019-10-24 12:03:05 +0300295 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com84bd3342020-03-16 18:04:57 +0200296 boostLogger->locked_backend()->auto_flush(true);
297 }
298
299 return 0;
300}
301
aa7133@att.coma4c80012020-06-24 16:36:47 +0300302void startPrometheus(sctp_params_t &sctpParams) {
303 sctpParams.prometheusFamily = &BuildCounter()
304 .Name("E2T")
305 .Help("E2T message counter")
306 .Labels({{"POD_NAME", sctpParams.podName}})
307 .Register(*sctpParams.prometheusRegistry);
aa7133@att.com16c799b2020-03-24 18:01:51 +0200308
aa7133@att.com54885fe2020-10-29 17:24:49 +0300309 string prometheusPath = sctpParams.prometheusPort + "," + "[::]:" + sctpParams.prometheusPort;
aa7133@att.comb92748d2020-06-29 11:52:30 +0300310 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com54885fe2020-10-29 17:24:49 +0300311 mdclog_write(MDCLOG_DEBUG, "Start Prometheus Pull mode on %s", prometheusPath.c_str());
aa7133@att.coma4c80012020-06-24 16:36:47 +0300312 }
aa7133@att.com54885fe2020-10-29 17:24:49 +0300313 sctpParams.prometheusExposer = new Exposer(prometheusPath, 1);
aa7133@att.comb92748d2020-06-29 11:52:30 +0300314 sctpParams.prometheusExposer->RegisterCollectable(sctpParams.prometheusRegistry);
aa7133@att.coma4c80012020-06-24 16:36:47 +0300315}
aa7133@att.com16c799b2020-03-24 18:01:51 +0200316
aa7133@att.com84bd3342020-03-16 18:04:57 +0200317int main(const int argc, char **argv) {
318 sctp_params_t sctpParams;
319
aa7133@att.com84bd3342020-03-16 18:04:57 +0200320 {
321 std::random_device device{};
322 std::mt19937 generator(device());
323 std::uniform_int_distribution<long> distribution(1, (long) 1e12);
324 transactionCounter = distribution(generator);
325 }
326
327// uint64_t st = 0;
328// uint32_t aux1 = 0;
329// st = rdtscp(aux1);
330
331 unsigned num_cpus = std::thread::hardware_concurrency();
332 init_log();
333 mdclog_level_set(MDCLOG_INFO);
334
335 if (std::signal(SIGINT, catch_function) == SIG_ERR) {
336 mdclog_write(MDCLOG_ERR, "Error initializing SIGINT");
337 exit(1);
338 }
339 if (std::signal(SIGABRT, catch_function)== SIG_ERR) {
340 mdclog_write(MDCLOG_ERR, "Error initializing SIGABRT");
341 exit(1);
342 }
343 if (std::signal(SIGTERM, catch_function)== SIG_ERR) {
344 mdclog_write(MDCLOG_ERR, "Error initializing SIGTERM");
345 exit(1);
346 }
347
348 cpuClock = approx_CPU_MHz(100);
349
350 mdclog_write(MDCLOG_DEBUG, "CPU speed %11.11f", cpuClock);
351
352 auto result = parse(argc, argv, sctpParams);
353
354 if (buildConfiguration(sctpParams) != 0) {
355 exit(-1);
ss412g1a79bdf2019-10-24 12:03:05 +0300356 }
357
ss412g4aeeda22020-06-16 14:12:41 +0300358 //auto registry = std::make_shared<Registry>();
359 sctpParams.prometheusRegistry = std::make_shared<Registry>();
360
aa7133@att.com409f2232020-10-29 13:06:45 +0300361 //sctpParams.prometheusFamily = new Family<Counter>("E2T", "E2T message counter", {{"E", sctpParams.podName}});
ss412g4aeeda22020-06-16 14:12:41 +0300362
aa7133@att.coma4c80012020-06-24 16:36:47 +0300363 startPrometheus(sctpParams);
ss412g4aeeda22020-06-16 14:12:41 +0300364
ss412g1a79bdf2019-10-24 12:03:05 +0300365 // start epoll
ss412g3bac2da2020-01-05 11:52:19 +0200366 sctpParams.epoll_fd = epoll_create1(0);
367 if (sctpParams.epoll_fd == -1) {
ss412g1a79bdf2019-10-24 12:03:05 +0300368 mdclog_write(MDCLOG_ERR, "failed to open epoll descriptor");
369 exit(-1);
370 }
371
aa7133@att.combe92d712020-03-18 17:30:39 +0200372 getRmrContext(sctpParams);
ss412g3bac2da2020-01-05 11:52:19 +0200373 if (sctpParams.rmrCtx == nullptr) {
374 close(sctpParams.epoll_fd);
ss412g1a79bdf2019-10-24 12:03:05 +0300375 exit(-1);
376 }
377
ss412g3bac2da2020-01-05 11:52:19 +0200378 if (buildInotify(sctpParams) == -1) {
379 close(sctpParams.rmrListenFd);
380 rmr_close(sctpParams.rmrCtx);
381 close(sctpParams.epoll_fd);
382 exit(-1);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200383 }
384
385 if (buildListeningPort(sctpParams) != 0) {
386 close(sctpParams.rmrListenFd);
387 rmr_close(sctpParams.rmrCtx);
388 close(sctpParams.epoll_fd);
389 exit(-1);
390 }
ss412g3bac2da2020-01-05 11:52:19 +0200391
392 sctpParams.sctpMap = new mapWrapper();
ss412g1a79bdf2019-10-24 12:03:05 +0300393
394 std::vector<std::thread> threads(num_cpus);
395// std::vector<std::thread> threads;
396
aa7133@att.com24e9fa42020-11-13 20:32:53 +0200397 num_cpus = 3;
ss412g1a79bdf2019-10-24 12:03:05 +0300398 for (unsigned int i = 0; i < num_cpus; i++) {
ss412g3bac2da2020-01-05 11:52:19 +0200399 threads[i] = std::thread(listener, &sctpParams);
ss412g1a79bdf2019-10-24 12:03:05 +0300400
401 cpu_set_t cpuset;
402 CPU_ZERO(&cpuset);
403 CPU_SET(i, &cpuset);
404 int rc = pthread_setaffinity_np(threads[i].native_handle(), sizeof(cpu_set_t), &cpuset);
405 if (rc != 0) {
406 mdclog_write(MDCLOG_ERR, "Error calling pthread_setaffinity_np: %d", rc);
407 }
ss412g1a79bdf2019-10-24 12:03:05 +0300408 }
409
aa7133@att.com16c799b2020-03-24 18:01:51 +0200410
ss412g3bac2da2020-01-05 11:52:19 +0200411 //loop over term_init until first message from xApp
412 handleTermInit(sctpParams);
413
414 for (auto &t : threads) {
415 t.join();
416 }
417
ss412g3bac2da2020-01-05 11:52:19 +0200418 return 0;
419}
420
421void handleTermInit(sctp_params_t &sctpParams) {
422 sendTermInit(sctpParams);
ss412g1a79bdf2019-10-24 12:03:05 +0300423 //send to e2 manager init of e2 term
424 //E2_TERM_INIT
ss412g1a79bdf2019-10-24 12:03:05 +0300425
ss412g3bac2da2020-01-05 11:52:19 +0200426 int count = 0;
427 while (true) {
428 auto xappMessages = num_of_XAPP_messages.load(std::memory_order_acquire);
429 if (xappMessages > 0) {
430 if (mdclog_level_get() >= MDCLOG_INFO) {
aa7133@att.com54885fe2020-10-29 17:24:49 +0300431 mdclog_write(MDCLOG_INFO, "Got a message from some application, stop sending E2_TERM_INIT");
ss412g3bac2da2020-01-05 11:52:19 +0200432 }
433 return;
434 }
435 usleep(100000);
436 count++;
437 if (count % 1000 == 0) {
438 mdclog_write(MDCLOG_ERR, "GOT No messages from any xApp");
439 sendTermInit(sctpParams);
440 }
441 }
442}
443
444void sendTermInit(sctp_params_t &sctpParams) {
445 rmr_mbuf_t *msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.ka_message_length);
ss412g1a79bdf2019-10-24 12:03:05 +0300446 auto count = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200447 while (true) {
ss412g1a79bdf2019-10-24 12:03:05 +0300448 msg->mtype = E2_TERM_INIT;
449 msg->state = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200450 rmr_bytes2payload(msg, (unsigned char *)sctpParams.ka_message, sctpParams.ka_message_length);
ss412g1a79bdf2019-10-24 12:03:05 +0300451 static unsigned char tx[32];
452 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
453 rmr_bytes2xact(msg, tx, txLen);
ss412g3bac2da2020-01-05 11:52:19 +0200454 msg = rmr_send_msg(sctpParams.rmrCtx, msg);
ss412g1a79bdf2019-10-24 12:03:05 +0300455 if (msg == nullptr) {
aa7133@att.comc9575aa2020-04-02 11:40:09 +0300456 msg = rmr_alloc_msg(sctpParams.rmrCtx, sctpParams.ka_message_length);
ss412g1a79bdf2019-10-24 12:03:05 +0300457 } else if (msg->state == 0) {
ss412g1a79bdf2019-10-24 12:03:05 +0300458 rmr_free_msg(msg);
ss412g3bac2da2020-01-05 11:52:19 +0200459 if (mdclog_level_get() >= MDCLOG_INFO) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300460 mdclog_write(MDCLOG_INFO, "E2_TERM_INIT successfully sent ");
ss412g3bac2da2020-01-05 11:52:19 +0200461 }
462 return;
ss412g1a79bdf2019-10-24 12:03:05 +0300463 } else {
464 if (count % 100 == 0) {
aa7133@att.com11b17d42020-03-22 11:35:16 +0200465 mdclog_write(MDCLOG_ERR, "Error sending E2_TERM_INIT cause : %s ", translateRmrErrorMessages(msg->state).c_str());
ss412g1a79bdf2019-10-24 12:03:05 +0300466 }
467 sleep(1);
468 }
469 count++;
470 }
ss412g3bac2da2020-01-05 11:52:19 +0200471}
472
473/**
474 *
475 * @param argc
476 * @param argv
477 * @param sctpParams
478 * @return
479 */
480cxxopts::ParseResult parse(int argc, char *argv[], sctp_params_t &sctpParams) {
481 cxxopts::Options options(argv[0], "e2 term help");
482 options.positional_help("[optional args]").show_positional_help();
483 options.allow_unrecognised_options().add_options()
484 ("p,path", "config file path", cxxopts::value<std::string>(sctpParams.configFilePath)->default_value("config"))
485 ("f,file", "config file name", cxxopts::value<std::string>(sctpParams.configFileName)->default_value("config.conf"))
486 ("h,help", "Print help");
487
aa7133@att.comde40d1c2020-08-26 16:19:20 +0300488 auto result = options.parse(argc, (const char **&)argv);
ss412g3bac2da2020-01-05 11:52:19 +0200489
490 if (result.count("help")) {
491 std::cout << options.help({""}) << std::endl;
492 exit(0);
493 }
494 return result;
495}
496
497/**
498 *
499 * @param sctpParams
500 * @return -1 failed 0 success
501 */
502int buildInotify(sctp_params_t &sctpParams) {
503 sctpParams.inotifyFD = inotify_init1(IN_NONBLOCK);
504 if (sctpParams.inotifyFD == -1) {
505 mdclog_write(MDCLOG_ERR, "Failed to init inotify (inotify_init1) %s", strerror(errno));
ss412g3bac2da2020-01-05 11:52:19 +0200506 return -1;
ss412g1a79bdf2019-10-24 12:03:05 +0300507 }
508
ss412g3bac2da2020-01-05 11:52:19 +0200509 sctpParams.inotifyWD = inotify_add_watch(sctpParams.inotifyFD,
aa7133@att.com96c9b882020-03-25 14:50:40 +0200510 (const char *)sctpParams.configFilePath.c_str(),
aa7133@att.com84bd3342020-03-16 18:04:57 +0200511 (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 +0200512 if (sctpParams.inotifyWD == -1) {
513 mdclog_write(MDCLOG_ERR, "Failed to add directory : %s to inotify (inotify_add_watch) %s",
aa7133@att.com96c9b882020-03-25 14:50:40 +0200514 sctpParams.configFilePath.c_str(),
515 strerror(errno));
ss412g3bac2da2020-01-05 11:52:19 +0200516 close(sctpParams.inotifyFD);
517 return -1;
518 }
519
520 struct epoll_event event{};
521 event.events = (EPOLLIN);
522 event.data.fd = sctpParams.inotifyFD;
523 // add listening RMR FD to epoll
524 if (epoll_ctl(sctpParams.epoll_fd, EPOLL_CTL_ADD, sctpParams.inotifyFD, &event)) {
525 mdclog_write(MDCLOG_ERR, "Failed to add inotify FD to epoll");
526 close(sctpParams.inotifyFD);
527 return -1;
528 }
ss412g1a79bdf2019-10-24 12:03:05 +0300529 return 0;
530}
531
532/**
533 *
534 * @param args
535 * @return
536 */
537void listener(sctp_params_t *params) {
ss412g1a79bdf2019-10-24 12:03:05 +0300538 int num_of_SCTP_messages = 0;
ss412g1a79bdf2019-10-24 12:03:05 +0300539 auto totalTime = 0.0;
540 mdclog_mdc_clean();
541 mdclog_level_set(params->logLevel);
542
543 std::thread::id this_id = std::this_thread::get_id();
544 //save cout
545 streambuf *oldCout = cout.rdbuf();
546 ostringstream memCout;
547 // create new cout
548 cout.rdbuf(memCout.rdbuf());
549 cout << this_id;
550 //return to the normal cout
551 cout.rdbuf(oldCout);
552
553 char tid[32];
554 memcpy(tid, memCout.str().c_str(), memCout.str().length() < 32 ? memCout.str().length() : 31);
555 tid[memCout.str().length()] = 0;
556 mdclog_mdc_add("thread id", tid);
557
558 if (mdclog_level_get() >= MDCLOG_DEBUG) {
559 mdclog_write(MDCLOG_DEBUG, "started thread number %s", tid);
560 }
561
562 RmrMessagesBuffer_t rmrMessageBuffer{};
563 //create and init RMR
564 rmrMessageBuffer.rmrCtx = params->rmrCtx;
565
566 auto *events = (struct epoll_event *) calloc(MAXEVENTS, sizeof(struct epoll_event));
567 struct timespec end{0, 0};
568 struct timespec start{0, 0};
569
570 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
571 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
572
ss412g3bac2da2020-01-05 11:52:19 +0200573 memcpy(rmrMessageBuffer.ka_message, params->ka_message, params->ka_message_length);
574 rmrMessageBuffer.ka_message_len = params->ka_message_length;
575 rmrMessageBuffer.ka_message[rmrMessageBuffer.ka_message_len] = 0;
576
577 if (mdclog_level_get() >= MDCLOG_DEBUG) {
578 mdclog_write(MDCLOG_DEBUG, "keep alive message is : %s", rmrMessageBuffer.ka_message);
579 }
580
ss412g1a79bdf2019-10-24 12:03:05 +0300581 ReportingMessages_t message {};
582
aa7133@att.com409f2232020-10-29 13:06:45 +0300583// for (int i = 0; i < MAX_RMR_BUFF_ARRAY; i++) {
aa7133@att.com7c3e4c02020-03-25 16:37:19 +0200584// rmrMessageBuffer.rcvBufferedMessages[i] = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
585// rmrMessageBuffer.sendBufferedMessages[i] = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
586// }
ss412g1a79bdf2019-10-24 12:03:05 +0300587
588 while (true) {
589 if (mdclog_level_get() >= MDCLOG_DEBUG) {
ss412g4aeeda22020-06-16 14:12:41 +0300590 mdclog_write(MDCLOG_DEBUG, "Start EPOLL Wait. Timeout = %d", params->epollTimeOut);
ss412g1a79bdf2019-10-24 12:03:05 +0300591 }
ss412g4aeeda22020-06-16 14:12:41 +0300592 auto numOfEvents = epoll_wait(params->epoll_fd, events, MAXEVENTS, params->epollTimeOut);
aa7133@att.comb92748d2020-06-29 11:52:30 +0300593 if (numOfEvents == 0) { // time out
594 if (mdclog_level_get() >= MDCLOG_DEBUG) {
595 mdclog_write(MDCLOG_DEBUG, "got epoll timeout");
ss412g1a79bdf2019-10-24 12:03:05 +0300596 }
597 continue;
ss412g4aeeda22020-06-16 14:12:41 +0300598 } else if (numOfEvents < 0) {
599 if (errno == EINTR) {
600 if (mdclog_level_get() >= MDCLOG_DEBUG) {
601 mdclog_write(MDCLOG_DEBUG, "got EINTR : %s", strerror(errno));
602 }
603 continue;
604 }
ss412g1a79bdf2019-10-24 12:03:05 +0300605 mdclog_write(MDCLOG_ERR, "Epoll wait failed, errno = %s", strerror(errno));
606 return;
607 }
608 for (auto i = 0; i < numOfEvents; i++) {
609 if (mdclog_level_get() >= MDCLOG_DEBUG) {
610 mdclog_write(MDCLOG_DEBUG, "handling epoll event %d out of %d", i + 1, numOfEvents);
611 }
612 clock_gettime(CLOCK_MONOTONIC, &message.message.time);
613 start.tv_sec = message.message.time.tv_sec;
614 start.tv_nsec = message.message.time.tv_nsec;
ss412g3bac2da2020-01-05 11:52:19 +0200615
616
ss412g1a79bdf2019-10-24 12:03:05 +0300617 if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP)) {
aa7133@att.combe92d712020-03-18 17:30:39 +0200618 handlepoll_error(events[i], message, rmrMessageBuffer, params);
ss412g1a79bdf2019-10-24 12:03:05 +0300619 } else if (events[i].events & EPOLLOUT) {
aa7133@att.combe92d712020-03-18 17:30:39 +0200620 handleEinprogressMessages(events[i], message, rmrMessageBuffer, params);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200621 } else if (params->listenFD == events[i].data.fd) {
aa7133@att.com6680c3b2020-03-22 12:06:58 +0200622 if (mdclog_level_get() >= MDCLOG_INFO) {
623 mdclog_write(MDCLOG_INFO, "New connection request from sctp network\n");
624 }
aa7133@att.com84bd3342020-03-16 18:04:57 +0200625 // new connection is requested from RAN start build connection
626 while (true) {
627 struct sockaddr in_addr {};
628 socklen_t in_len;
629 char hostBuff[NI_MAXHOST];
630 char portBuff[NI_MAXSERV];
631
632 in_len = sizeof(in_addr);
633 auto *peerInfo = (ConnectedCU_t *)calloc(1, sizeof(ConnectedCU_t));
aa7133@att.com409f2232020-10-29 13:06:45 +0300634 if(peerInfo == nullptr){
swc58d4f5d2020-10-20 15:08:38 +0900635 mdclog_write(MDCLOG_ERR, "calloc failed");
636 break;
637 }
aa7133@att.com84bd3342020-03-16 18:04:57 +0200638 peerInfo->sctpParams = params;
639 peerInfo->fileDescriptor = accept(params->listenFD, &in_addr, &in_len);
640 if (peerInfo->fileDescriptor == -1) {
641 if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
642 /* We have processed all incoming connections. */
643 break;
644 } else {
645 mdclog_write(MDCLOG_ERR, "Accept error, errno = %s", strerror(errno));
646 break;
647 }
648 }
649 if (setSocketNoBlocking(peerInfo->fileDescriptor) == -1) {
650 mdclog_write(MDCLOG_ERR, "setSocketNoBlocking failed to set new connection %s on port %s\n", hostBuff, portBuff);
651 close(peerInfo->fileDescriptor);
652 break;
653 }
654 auto ans = getnameinfo(&in_addr, in_len,
aa7133@att.com96c9b882020-03-25 14:50:40 +0200655 peerInfo->hostName, NI_MAXHOST,
656 peerInfo->portNumber, NI_MAXSERV, (unsigned )((unsigned int)NI_NUMERICHOST | (unsigned int)NI_NUMERICSERV));
aa7133@att.com84bd3342020-03-16 18:04:57 +0200657 if (ans < 0) {
658 mdclog_write(MDCLOG_ERR, "Failed to get info on connection request. %s\n", strerror(errno));
659 close(peerInfo->fileDescriptor);
660 break;
661 }
662 if (mdclog_level_get() >= MDCLOG_DEBUG) {
663 mdclog_write(MDCLOG_DEBUG, "Accepted connection on descriptor %d (host=%s, port=%s)\n", peerInfo->fileDescriptor, peerInfo->hostName, peerInfo->portNumber);
664 }
665 peerInfo->isConnected = false;
666 peerInfo->gotSetup = false;
667 if (addToEpoll(params->epoll_fd,
668 peerInfo,
669 (EPOLLIN | EPOLLET),
670 params->sctpMap, nullptr,
aa7133@att.combe92d712020-03-18 17:30:39 +0200671 0) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +0200672 break;
673 }
aa7133@att.com6680c3b2020-03-22 12:06:58 +0200674 break;
aa7133@att.com84bd3342020-03-16 18:04:57 +0200675 }
ss412g1a79bdf2019-10-24 12:03:05 +0300676 } else if (params->rmrListenFd == events[i].data.fd) {
677 // got message from XAPP
aa7133@att.comde40d1c2020-08-26 16:19:20 +0300678 //num_of_XAPP_messages.fetch_add(1, std::memory_order_release);
ss412g1a79bdf2019-10-24 12:03:05 +0300679 num_of_messages.fetch_add(1, std::memory_order_release);
680 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.comf13529a2020-08-27 11:21:16 +0300681 mdclog_write(MDCLOG_DEBUG, "new RMR message");
ss412g1a79bdf2019-10-24 12:03:05 +0300682 }
aa7133@att.com6680c3b2020-03-22 12:06:58 +0200683 if (receiveXappMessages(params->sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +0300684 rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200685 message.message.time) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +0300686 mdclog_write(MDCLOG_ERR, "Error handling Xapp message");
687 }
ss412g3bac2da2020-01-05 11:52:19 +0200688 } else if (params->inotifyFD == events[i].data.fd) {
689 mdclog_write(MDCLOG_INFO, "Got event from inotify (configuration update)");
690 handleConfigChange(params);
ss412g1a79bdf2019-10-24 12:03:05 +0300691 } else {
692 /* We RMR_ERR_RETRY have data on the fd waiting to be read. Read and display it.
693 * We must read whatever data is available completely, as we are running
694 * in edge-triggered mode and won't get a notification again for the same data. */
695 num_of_messages.fetch_add(1, std::memory_order_release);
696 if (mdclog_level_get() >= MDCLOG_DEBUG) {
697 mdclog_write(MDCLOG_DEBUG, "new message from SCTP, epoll flags are : %0x", events[i].events);
698 }
699 receiveDataFromSctp(&events[i],
700 params->sctpMap,
701 num_of_SCTP_messages,
702 rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200703 message.message.time);
ss412g1a79bdf2019-10-24 12:03:05 +0300704 }
705
706 clock_gettime(CLOCK_MONOTONIC, &end);
707 if (mdclog_level_get() >= MDCLOG_INFO) {
708 totalTime += ((end.tv_sec + 1.0e-9 * end.tv_nsec) -
709 ((double) start.tv_sec + 1.0e-9 * start.tv_nsec));
710 }
711 if (mdclog_level_get() >= MDCLOG_DEBUG) {
712 mdclog_write(MDCLOG_DEBUG, "message handling is %ld seconds %ld nanoseconds",
713 end.tv_sec - start.tv_sec,
714 end.tv_nsec - start.tv_nsec);
715 }
716 }
717 }
ss412g1a79bdf2019-10-24 12:03:05 +0300718}
719
720/**
721 *
ss412g3bac2da2020-01-05 11:52:19 +0200722 * @param sctpParams
723 */
724void handleConfigChange(sctp_params_t *sctpParams) {
725 char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event))));
726 const struct inotify_event *event;
727 char *ptr;
728
729 path p = (sctpParams->configFilePath + "/" + sctpParams->configFileName).c_str();
730 auto endlessLoop = true;
731 while (endlessLoop) {
732 auto len = read(sctpParams->inotifyFD, buf, sizeof buf);
733 if (len == -1) {
734 if (errno != EAGAIN) {
735 mdclog_write(MDCLOG_ERR, "read %s ", strerror(errno));
736 endlessLoop = false;
737 continue;
738 }
739 else {
740 endlessLoop = false;
741 continue;
742 }
743 }
744
745 for (ptr = buf; ptr < buf + len; ptr += sizeof(struct inotify_event) + event->len) {
746 event = (const struct inotify_event *)ptr;
747 if (event->mask & (uint32_t)IN_ISDIR) {
748 continue;
749 }
750
751 // the directory name
752 if (sctpParams->inotifyWD == event->wd) {
753 // not the directory
754 }
755 if (event->len) {
aa7133@att.comdfc1db92020-03-31 17:32:04 +0300756 auto retVal = strcmp(sctpParams->configFileName.c_str(), event->name);
757 if (retVal != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200758 continue;
759 }
760 }
761 // only the file we want
762 if (event->mask & (uint32_t)IN_CLOSE_WRITE) {
aa7133@att.comdfc1db92020-03-31 17:32:04 +0300763 if (mdclog_level_get() >= MDCLOG_INFO) {
764 mdclog_write(MDCLOG_INFO, "Configuration file changed");
765 }
ss412g3bac2da2020-01-05 11:52:19 +0200766 if (exists(p)) {
767 const int size = 2048;
768 auto fileSize = file_size(p);
769 if (fileSize > size) {
770 mdclog_write(MDCLOG_ERR, "File %s larger than %d", p.string().c_str(), size);
771 return;
772 }
773 } else {
774 mdclog_write(MDCLOG_ERR, "Configuration File %s not exists", p.string().c_str());
775 return;
776 }
777
778 ReadConfigFile conf;
779 if (conf.openConfigFile(p.string()) == -1) {
780 mdclog_write(MDCLOG_ERR, "Filed to open config file %s, %s",
781 p.string().c_str(), strerror(errno));
782 return;
783 }
784
785 auto tmpStr = conf.getStringValue("loglevel");
786 if (tmpStr.length() == 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300787 mdclog_write(MDCLOG_ERR, "illegal loglevel. Set loglevel to MDCLOG_INFO");
ss412g3bac2da2020-01-05 11:52:19 +0200788 tmpStr = "info";
789 }
790 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
791
792 if ((tmpStr.compare("debug")) == 0) {
793 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_DEBUG");
794 sctpParams->logLevel = MDCLOG_DEBUG;
795 } else if ((tmpStr.compare("info")) == 0) {
796 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_INFO");
797 sctpParams->logLevel = MDCLOG_INFO;
798 } else if ((tmpStr.compare("warning")) == 0) {
799 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_WARN");
800 sctpParams->logLevel = MDCLOG_WARN;
801 } else if ((tmpStr.compare("error")) == 0) {
802 mdclog_write(MDCLOG_INFO, "Log level set to MDCLOG_ERR");
803 sctpParams->logLevel = MDCLOG_ERR;
804 } else {
aa7133@att.com409f2232020-10-29 13:06:45 +0300805 mdclog_write(MDCLOG_ERR, "illegal loglevel = %s. Set loglevel to MDCLOG_INFO", tmpStr.c_str());
ss412g3bac2da2020-01-05 11:52:19 +0200806 sctpParams->logLevel = MDCLOG_INFO;
807 }
808 mdclog_level_set(sctpParams->logLevel);
809
810
811 tmpStr = conf.getStringValue("trace");
812 if (tmpStr.length() == 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +0300813 mdclog_write(MDCLOG_ERR, "illegal trace. Set trace to stop");
ss412g3bac2da2020-01-05 11:52:19 +0200814 tmpStr = "stop";
815 }
816
817 transform(tmpStr.begin(), tmpStr.end(), tmpStr.begin(), ::tolower);
818 if ((tmpStr.compare("start")) == 0) {
819 mdclog_write(MDCLOG_INFO, "Trace set to: start");
820 sctpParams->trace = true;
821 } else if ((tmpStr.compare("stop")) == 0) {
822 mdclog_write(MDCLOG_INFO, "Trace set to: stop");
823 sctpParams->trace = false;
824 } else {
825 mdclog_write(MDCLOG_ERR, "Trace was set to wrong value %s, set to stop", tmpStr.c_str());
826 sctpParams->trace = false;
827 }
828 jsonTrace = sctpParams->trace;
ss412g4aeeda22020-06-16 14:12:41 +0300829
ss412g4aeeda22020-06-16 14:12:41 +0300830
ss412g3bac2da2020-01-05 11:52:19 +0200831 endlessLoop = false;
832 }
833 }
834 }
835}
836
837/**
838 *
839 * @param event
840 * @param message
841 * @param rmrMessageBuffer
842 * @param params
ss412g3bac2da2020-01-05 11:52:19 +0200843 */
844void handleEinprogressMessages(struct epoll_event &event,
845 ReportingMessages_t &message,
846 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200847 sctp_params_t *params) {
ss412g3bac2da2020-01-05 11:52:19 +0200848 auto *peerInfo = (ConnectedCU_t *)event.data.ptr;
849 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
850
851 mdclog_write(MDCLOG_INFO, "file descriptor %d got EPOLLOUT", peerInfo->fileDescriptor);
852 auto retVal = 0;
853 socklen_t retValLen = 0;
854 auto rc = getsockopt(peerInfo->fileDescriptor, SOL_SOCKET, SO_ERROR, &retVal, &retValLen);
855 if (rc != 0 || retVal != 0) {
856 if (rc != 0) {
857 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
858 "%s|Failed SCTP Connection, after EINPROGRESS the getsockopt%s",
859 peerInfo->enodbName, strerror(errno));
860 } else if (retVal != 0) {
861 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
862 "%s|Failed SCTP Connection after EINPROGRESS, SO_ERROR",
863 peerInfo->enodbName);
864 }
865
866 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
867 message.message.asnLength = rmrMessageBuffer.sendMessage->len;
868 mdclog_write(MDCLOG_ERR, "%s", rmrMessageBuffer.sendMessage->payload);
869 message.message.direction = 'N';
aa7133@att.combe92d712020-03-18 17:30:39 +0200870 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200871 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
872 }
873 memset(peerInfo->asnData, 0, peerInfo->asnLength);
874 peerInfo->asnLength = 0;
875 peerInfo->mtype = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200876 return;
877 }
878
879 peerInfo->isConnected = true;
880
881 if (modifyToEpoll(params->epoll_fd, peerInfo, (EPOLLIN | EPOLLET), params->sctpMap, peerInfo->enodbName,
aa7133@att.combe92d712020-03-18 17:30:39 +0200882 peerInfo->mtype) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200883 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_MOD");
ss412g3bac2da2020-01-05 11:52:19 +0200884 return;
885 }
886
887 message.message.asndata = (unsigned char *)peerInfo->asnData;
888 message.message.asnLength = peerInfo->asnLength;
889 message.message.messageType = peerInfo->mtype;
890 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
891 num_of_messages.fetch_add(1, std::memory_order_release);
892 if (mdclog_level_get() >= MDCLOG_DEBUG) {
893 mdclog_write(MDCLOG_DEBUG, "send the delayed SETUP/ENDC SETUP to sctp for %s",
894 message.message.enodbName);
895 }
aa7133@att.combe92d712020-03-18 17:30:39 +0200896 if (sendSctpMsg(peerInfo, message, params->sctpMap) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200897 if (mdclog_level_get() >= MDCLOG_DEBUG) {
898 mdclog_write(MDCLOG_DEBUG, "Error write to SCTP %s %d", __func__, __LINE__);
899 }
ss412g3bac2da2020-01-05 11:52:19 +0200900 return;
901 }
902
903 memset(peerInfo->asnData, 0, peerInfo->asnLength);
904 peerInfo->asnLength = 0;
905 peerInfo->mtype = 0;
ss412g3bac2da2020-01-05 11:52:19 +0200906}
907
908
909void handlepoll_error(struct epoll_event &event,
910 ReportingMessages_t &message,
911 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +0200912 sctp_params_t *params) {
ss412g3bac2da2020-01-05 11:52:19 +0200913 if (event.data.fd != params->rmrListenFd) {
914 auto *peerInfo = (ConnectedCU_t *)event.data.ptr;
915 mdclog_write(MDCLOG_ERR, "epoll error, events %0x on fd %d, RAN NAME : %s",
916 event.events, peerInfo->fileDescriptor, peerInfo->enodbName);
917
918 rmrMessageBuffer.sendMessage->len = snprintf((char *)rmrMessageBuffer.sendMessage->payload, 256,
919 "%s|Failed SCTP Connection",
920 peerInfo->enodbName);
921 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
922 message.message.asnLength = rmrMessageBuffer.sendMessage->len;
923
924 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
925 message.message.direction = 'N';
aa7133@att.combe92d712020-03-18 17:30:39 +0200926 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
ss412g3bac2da2020-01-05 11:52:19 +0200927 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
928 }
929
930 close(peerInfo->fileDescriptor);
aa7133@att.com7b437f72020-03-30 13:25:54 +0300931 params->sctpMap->erase(peerInfo->enodbName);
aa7133@att.combe92d712020-03-18 17:30:39 +0200932 cleanHashEntry((ConnectedCU_t *) event.data.ptr, params->sctpMap);
ss412g3bac2da2020-01-05 11:52:19 +0200933 } else {
934 mdclog_write(MDCLOG_ERR, "epoll error, events %0x on RMR FD", event.events);
935 }
ss412g3bac2da2020-01-05 11:52:19 +0200936}
937/**
938 *
ss412g1a79bdf2019-10-24 12:03:05 +0300939 * @param socket
940 * @return
941 */
942int setSocketNoBlocking(int socket) {
943 auto flags = fcntl(socket, F_GETFL, 0);
944
945 if (flags == -1) {
946 mdclog_mdc_add("func", "fcntl");
947 mdclog_write(MDCLOG_ERR, "%s, %s", __FUNCTION__, strerror(errno));
948 mdclog_mdc_clean();
949 return -1;
950 }
951
952 flags = (unsigned) flags | (unsigned) O_NONBLOCK;
953 if (fcntl(socket, F_SETFL, flags) == -1) {
954 mdclog_mdc_add("func", "fcntl");
955 mdclog_write(MDCLOG_ERR, "%s, %s", __FUNCTION__, strerror(errno));
956 mdclog_mdc_clean();
957 return -1;
958 }
959
960 return 0;
961}
962
963/**
964 *
965 * @param val
966 * @param m
ss412g1a79bdf2019-10-24 12:03:05 +0300967 */
aa7133@att.combe92d712020-03-18 17:30:39 +0200968void cleanHashEntry(ConnectedCU_t *val, Sctp_Map_t *m) {
ss412g1a79bdf2019-10-24 12:03:05 +0300969 char *dummy;
970 auto port = (uint16_t) strtol(val->portNumber, &dummy, 10);
aa7133@att.com84bd3342020-03-16 18:04:57 +0200971 char searchBuff[2048]{};
ss412g1a79bdf2019-10-24 12:03:05 +0300972
973 snprintf(searchBuff, sizeof searchBuff, "host:%s:%d", val->hostName, port);
974 m->erase(searchBuff);
975
976 m->erase(val->enodbName);
977 free(val);
ss412g1a79bdf2019-10-24 12:03:05 +0300978}
979
980/**
981 *
aa7133@att.com409f2232020-10-29 13:06:45 +0300982 * @param fd file descriptor
ss412g1a79bdf2019-10-24 12:03:05 +0300983 * @param data the asn data to send
984 * @param len length of the data
985 * @param enodbName the enodbName as in the map for printing purpose
986 * @param m map host information
987 * @param mtype message number
aa7133@att.com409f2232020-10-29 13:06:45 +0300988 * @return 0 success, a negative number on fail
ss412g1a79bdf2019-10-24 12:03:05 +0300989 */
aa7133@att.combe92d712020-03-18 17:30:39 +0200990int sendSctpMsg(ConnectedCU_t *peerInfo, ReportingMessages_t &message, Sctp_Map_t *m) {
ss412g1a79bdf2019-10-24 12:03:05 +0300991 auto loglevel = mdclog_level_get();
992 int fd = peerInfo->fileDescriptor;
993 if (loglevel >= MDCLOG_DEBUG) {
994 mdclog_write(MDCLOG_DEBUG, "Send SCTP message for CU %s, %s",
995 message.message.enodbName, __FUNCTION__);
996 }
997
998 while (true) {
ss412g1a79bdf2019-10-24 12:03:05 +0300999 if (send(fd,message.message.asndata, message.message.asnLength,MSG_NOSIGNAL) < 0) {
1000 if (errno == EINTR) {
1001 continue;
1002 }
1003 mdclog_write(MDCLOG_ERR, "error writing to CU a message, %s ", strerror(errno));
aa7133@att.com84bd3342020-03-16 18:04:57 +02001004 if (!peerInfo->isConnected) {
1005 mdclog_write(MDCLOG_ERR, "connection to CU %s is still in progress.", message.message.enodbName);
aa7133@att.com84bd3342020-03-16 18:04:57 +02001006 return -1;
1007 }
aa7133@att.combe92d712020-03-18 17:30:39 +02001008 cleanHashEntry(peerInfo, m);
ss412g1a79bdf2019-10-24 12:03:05 +03001009 close(fd);
1010 char key[MAX_ENODB_NAME_SIZE * 2];
1011 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", message.message.enodbName,
1012 message.message.messageType);
1013 if (loglevel >= MDCLOG_DEBUG) {
1014 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
1015 }
aa7133@att.com96c9b882020-03-25 14:50:40 +02001016 auto tmp = m->find(key);
ss412g1a79bdf2019-10-24 12:03:05 +03001017 if (tmp) {
1018 free(tmp);
1019 }
1020 m->erase(key);
ss412g1a79bdf2019-10-24 12:03:05 +03001021 return -1;
1022 }
1023 message.message.direction = 'D';
1024 // send report.buffer of size
1025 buildJsonMessage(message);
1026
1027 if (loglevel >= MDCLOG_DEBUG) {
1028 mdclog_write(MDCLOG_DEBUG,
1029 "SCTP message for CU %s sent from %s",
1030 message.message.enodbName,
1031 __FUNCTION__);
1032 }
ss412g1a79bdf2019-10-24 12:03:05 +03001033 return 0;
1034 }
1035}
1036
1037/**
1038 *
1039 * @param message
1040 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001041 */
aa7133@att.combe92d712020-03-18 17:30:39 +02001042void getRequestMetaData(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03001043 message.message.asndata = rmrMessageBuffer.rcvMessage->payload;
1044 message.message.asnLength = rmrMessageBuffer.rcvMessage->len;
1045
1046 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1047 mdclog_write(MDCLOG_DEBUG, "Message from Xapp RAN name = %s message length = %ld",
1048 message.message.enodbName, (unsigned long) message.message.asnLength);
1049 }
ss412g1a79bdf2019-10-24 12:03:05 +03001050}
1051
1052
ss412g1a79bdf2019-10-24 12:03:05 +03001053
1054/**
1055 *
1056 * @param events
1057 * @param sctpMap
1058 * @param numOfMessages
1059 * @param rmrMessageBuffer
1060 * @param ts
ss412g1a79bdf2019-10-24 12:03:05 +03001061 * @return
1062 */
1063int receiveDataFromSctp(struct epoll_event *events,
1064 Sctp_Map_t *sctpMap,
1065 int &numOfMessages,
1066 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +02001067 struct timespec &ts) {
ss412g1a79bdf2019-10-24 12:03:05 +03001068 /* We have data on the fd waiting to be read. Read and display it.
1069 * We must read whatever data is available completely, as we are running
1070 * in edge-triggered mode and won't get a notification again for the same data. */
aa7133@att.com84bd3342020-03-16 18:04:57 +02001071 ReportingMessages_t message {};
1072 auto done = 0;
ss412g1a79bdf2019-10-24 12:03:05 +03001073 auto loglevel = mdclog_level_get();
aa7133@att.com84bd3342020-03-16 18:04:57 +02001074
ss412g1a79bdf2019-10-24 12:03:05 +03001075 // get the identity of the interface
aa7133@att.com84bd3342020-03-16 18:04:57 +02001076 message.peerInfo = (ConnectedCU_t *)events->data.ptr;
1077
ss412g1a79bdf2019-10-24 12:03:05 +03001078 struct timespec start{0, 0};
aa7133@att.com409f2232020-10-29 13:06:45 +03001079 struct timespec decodeStart{0, 0};
ss412g1a79bdf2019-10-24 12:03:05 +03001080 struct timespec end{0, 0};
1081
1082 E2AP_PDU_t *pdu = nullptr;
1083
ss412g1a79bdf2019-10-24 12:03:05 +03001084 while (true) {
1085 if (loglevel >= MDCLOG_DEBUG) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001086 mdclog_write(MDCLOG_DEBUG, "Start Read from SCTP %d fd", message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001087 clock_gettime(CLOCK_MONOTONIC, &start);
1088 }
1089 // read the buffer directly to rmr payload
1090 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1091 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
aa7133@att.com84bd3342020-03-16 18:04:57 +02001092 read(message.peerInfo->fileDescriptor, rmrMessageBuffer.sendMessage->payload, RECEIVE_SCTP_BUFFER_SIZE);
1093
ss412g1a79bdf2019-10-24 12:03:05 +03001094 if (loglevel >= MDCLOG_DEBUG) {
1095 mdclog_write(MDCLOG_DEBUG, "Finish Read from SCTP %d fd message length = %ld",
aa7133@att.com96c9b882020-03-25 14:50:40 +02001096 message.peerInfo->fileDescriptor, message.message.asnLength);
ss412g1a79bdf2019-10-24 12:03:05 +03001097 }
aa7133@att.com16c799b2020-03-24 18:01:51 +02001098
aa7133@att.com84bd3342020-03-16 18:04:57 +02001099 memcpy(message.message.enodbName, message.peerInfo->enodbName, sizeof(message.peerInfo->enodbName));
ss412g1a79bdf2019-10-24 12:03:05 +03001100 message.message.direction = 'U';
1101 message.message.time.tv_nsec = ts.tv_nsec;
1102 message.message.time.tv_sec = ts.tv_sec;
1103
1104 if (message.message.asnLength < 0) {
1105 if (errno == EINTR) {
1106 continue;
1107 }
1108 /* If errno == EAGAIN, that means we have read all
aa7133@att.com84bd3342020-03-16 18:04:57 +02001109 data. So goReportingMessages_t back to the main loop. */
ss412g1a79bdf2019-10-24 12:03:05 +03001110 if (errno != EAGAIN) {
1111 mdclog_write(MDCLOG_ERR, "Read error, %s ", strerror(errno));
1112 done = 1;
1113 } else if (loglevel >= MDCLOG_DEBUG) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001114 mdclog_write(MDCLOG_DEBUG, "EAGAIN - descriptor = %d", message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001115 }
1116 break;
1117 } else if (message.message.asnLength == 0) {
1118 /* End of file. The remote has closed the connection. */
1119 if (loglevel >= MDCLOG_INFO) {
1120 mdclog_write(MDCLOG_INFO, "END of File Closed connection - descriptor = %d",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001121 message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001122 }
1123 done = 1;
1124 break;
1125 }
1126
ss412g1a79bdf2019-10-24 12:03:05 +03001127 if (loglevel >= MDCLOG_DEBUG) {
swc58d4f5d2020-10-20 15:08:38 +09001128 char printBuffer[RECEIVE_SCTP_BUFFER_SIZE]{};
ss412g1a79bdf2019-10-24 12:03:05 +03001129 char *tmp = printBuffer;
1130 for (size_t i = 0; i < (size_t)message.message.asnLength; ++i) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001131 snprintf(tmp, 3, "%02x", message.message.asndata[i]);
ss412g1a79bdf2019-10-24 12:03:05 +03001132 tmp += 2;
1133 }
1134 printBuffer[message.message.asnLength] = 0;
1135 clock_gettime(CLOCK_MONOTONIC, &end);
1136 mdclog_write(MDCLOG_DEBUG, "Before Encoding E2AP PDU for : %s, Read time is : %ld seconds, %ld nanoseconds",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001137 message.peerInfo->enodbName, end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec);
ss412g1a79bdf2019-10-24 12:03:05 +03001138 mdclog_write(MDCLOG_DEBUG, "PDU buffer length = %ld, data = : %s", message.message.asnLength,
1139 printBuffer);
aa7133@att.com409f2232020-10-29 13:06:45 +03001140 clock_gettime(CLOCK_MONOTONIC, &decodeStart);
ss412g1a79bdf2019-10-24 12:03:05 +03001141 }
1142
aa7133@att.com7b437f72020-03-30 13:25:54 +03001143 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, (void **) &pdu,
ss412g1a79bdf2019-10-24 12:03:05 +03001144 message.message.asndata, message.message.asnLength);
1145 if (rval.code != RC_OK) {
1146 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2AP PDU from RAN : %s", rval.code,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001147 message.peerInfo->enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001148 break;
1149 }
1150
1151 if (loglevel >= MDCLOG_DEBUG) {
1152 clock_gettime(CLOCK_MONOTONIC, &end);
1153 mdclog_write(MDCLOG_DEBUG, "After Encoding E2AP PDU for : %s, Read time is : %ld seconds, %ld nanoseconds",
aa7133@att.com409f2232020-10-29 13:06:45 +03001154 message.peerInfo->enodbName, end.tv_sec - decodeStart.tv_sec, end.tv_nsec - decodeStart.tv_nsec);
ss412g1a79bdf2019-10-24 12:03:05 +03001155 char *printBuffer;
1156 size_t size;
1157 FILE *stream = open_memstream(&printBuffer, &size);
1158 asn_fprint(stream, &asn_DEF_E2AP_PDU, pdu);
1159 mdclog_write(MDCLOG_DEBUG, "Encoding E2AP PDU past : %s", printBuffer);
aa7133@att.com409f2232020-10-29 13:06:45 +03001160 clock_gettime(CLOCK_MONOTONIC, &decodeStart);
Byonggon Chuncae9bd52020-11-18 13:37:29 +09001161
1162 fclose(stream);
1163 free(printBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001164 }
1165
1166 switch (pdu->present) {
1167 case E2AP_PDU_PR_initiatingMessage: {//initiating message
aa7133@att.com7b437f72020-03-30 13:25:54 +03001168 asnInitiatingRequest(pdu, sctpMap,message, rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001169 break;
1170 }
1171 case E2AP_PDU_PR_successfulOutcome: { //successful outcome
aa7133@att.com409f2232020-10-29 13:06:45 +03001172 asnSuccessfulMsg(pdu, sctpMap, message, rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001173 break;
1174 }
1175 case E2AP_PDU_PR_unsuccessfulOutcome: { //Unsuccessful Outcome
aa7133@att.com7b437f72020-03-30 13:25:54 +03001176 asnUnSuccsesfulMsg(pdu, sctpMap, message, rmrMessageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03001177 break;
1178 }
1179 default:
1180 mdclog_write(MDCLOG_ERR, "Unknown index %d in E2AP PDU", pdu->present);
1181 break;
1182 }
1183 if (loglevel >= MDCLOG_DEBUG) {
1184 clock_gettime(CLOCK_MONOTONIC, &end);
1185 mdclog_write(MDCLOG_DEBUG,
1186 "After processing message and sent to rmr for : %s, Read time is : %ld seconds, %ld nanoseconds",
aa7133@att.com409f2232020-10-29 13:06:45 +03001187 message.peerInfo->enodbName, end.tv_sec - decodeStart.tv_sec, end.tv_nsec - decodeStart.tv_nsec);
ss412g1a79bdf2019-10-24 12:03:05 +03001188 }
1189 numOfMessages++;
ss412g1a79bdf2019-10-24 12:03:05 +03001190 if (pdu != nullptr) {
aa7133@att.com1fecb052020-03-29 09:44:09 +03001191 ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, pdu);
1192 //ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, pdu);
aa7133@att.com9dff4f02020-03-29 09:55:22 +03001193 //pdu = nullptr;
ss412g1a79bdf2019-10-24 12:03:05 +03001194 }
ss412g1a79bdf2019-10-24 12:03:05 +03001195 }
ss412g1a79bdf2019-10-24 12:03:05 +03001196
1197 if (done) {
1198 if (loglevel >= MDCLOG_INFO) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001199 mdclog_write(MDCLOG_INFO, "Closed connection - descriptor = %d", message.peerInfo->fileDescriptor);
ss412g1a79bdf2019-10-24 12:03:05 +03001200 }
1201 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
1202 snprintf((char *)rmrMessageBuffer.sendMessage->payload,
aa7133@att.com96c9b882020-03-25 14:50:40 +02001203 256,
1204 "%s|CU disconnected unexpectedly",
1205 message.peerInfo->enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001206 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
1207
1208 if (sendRequestToXapp(message,
1209 RIC_SCTP_CONNECTION_FAILURE,
aa7133@att.combe92d712020-03-18 17:30:39 +02001210 rmrMessageBuffer) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03001211 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
1212 }
1213
1214 /* Closing descriptor make epoll remove it from the set of descriptors which are monitored. */
aa7133@att.com84bd3342020-03-16 18:04:57 +02001215 close(message.peerInfo->fileDescriptor);
aa7133@att.combe92d712020-03-18 17:30:39 +02001216 cleanHashEntry((ConnectedCU_t *) events->data.ptr, sctpMap);
ss412g1a79bdf2019-10-24 12:03:05 +03001217 }
1218 if (loglevel >= MDCLOG_DEBUG) {
1219 clock_gettime(CLOCK_MONOTONIC, &end);
1220 mdclog_write(MDCLOG_DEBUG, "from receive SCTP to send RMR time is %ld seconds and %ld nanoseconds",
1221 end.tv_sec - start.tv_sec, end.tv_nsec - start.tv_nsec);
1222
1223 }
ss412g1a79bdf2019-10-24 12:03:05 +03001224 return 0;
1225}
1226
aa7133@att.com409f2232020-10-29 13:06:45 +03001227static void buildAndSendSetupRequest(ReportingMessages_t &message,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001228 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001229 E2AP_PDU_t *pdu/*,
ss412g4aeeda22020-06-16 14:12:41 +03001230 string const &messageName,
1231 string const &ieName,
1232 vector<string> &functionsToAdd_v,
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001233 vector<string> &functionsToModified_v*/) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001234 auto logLevel = mdclog_level_get();
aa7133@att.com84bd3342020-03-16 18:04:57 +02001235 // now we can send the data to e2Mgr
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001236
ss412g4aeeda22020-06-16 14:12:41 +03001237 asn_enc_rval_t er;
1238 auto buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
Byonggon Chun51885f32020-11-18 15:31:04 +09001239 unsigned char *buffer = nullptr;
1240 buffer = (unsigned char *) calloc(buffer_size, sizeof(unsigned char));
1241 if(!buffer)
1242 {
1243 mdclog_write(MDCLOG_ERR, "Allocating buffer for %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1244 return;
1245 }
ss412g4aeeda22020-06-16 14:12:41 +03001246 while (true) {
ss412g4aeeda22020-06-16 14:12:41 +03001247 er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu, buffer, buffer_size);
1248 if (er.encoded == -1) {
1249 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1250 return;
1251 } else if (er.encoded > (ssize_t) buffer_size) {
1252 buffer_size = er.encoded + 128;
1253 mdclog_write(MDCLOG_WARN, "Buffer of size %d is to small for %s. Reallocate buffer of size %d",
1254 (int) buffer_size,
1255 asn_DEF_E2AP_PDU.name, buffer_size);
1256 buffer_size = er.encoded + 128;
Byonggon Chun51885f32020-11-18 15:31:04 +09001257
1258 unsigned char *newBuffer = nullptr;
1259 newBuffer = (unsigned char *) realloc(buffer, buffer_size);
1260 if (!newBuffer)
1261 {
1262 // out of memory
1263 mdclog_write(MDCLOG_ERR, "Reallocating buffer for %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1264 free(buffer);
1265 return;
1266 }
1267 buffer = newBuffer;
ss412g4aeeda22020-06-16 14:12:41 +03001268 continue;
1269 }
1270 buffer[er.encoded] = '\0';
1271 break;
1272 }
1273 // encode to xml
1274
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001275 string res((char *)buffer);
1276 res.erase(std::remove(res.begin(), res.end(), '\n'), res.end());
1277 res.erase(std::remove(res.begin(), res.end(), '\t'), res.end());
1278 res.erase(std::remove(res.begin(), res.end(), ' '), res.end());
1279
1280// string res {};
1281// if (!functionsToAdd_v.empty() || !functionsToModified_v.empty()) {
1282// res = buildXmlData(messageName, ieName, functionsToAdd_v, functionsToModified_v, buffer, (size_t) er.encoded);
1283// }
ss412g4aeeda22020-06-16 14:12:41 +03001284 rmr_mbuf_t *rmrMsg;
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001285// if (res.length() == 0) {
1286// rmrMsg = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, buffer_size + 256);
1287// rmrMsg->len = snprintf((char *) rmrMsg->payload, RECEIVE_SCTP_BUFFER_SIZE * 2, "%s:%d|%s",
1288// message.peerInfo->sctpParams->myIP.c_str(),
1289// message.peerInfo->sctpParams->rmrPort,
1290// buffer);
1291// } else {
ss412g4aeeda22020-06-16 14:12:41 +03001292 rmrMsg = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, (int)res.length() + 256);
1293 rmrMsg->len = snprintf((char *) rmrMsg->payload, res.length() + 256, "%s:%d|%s",
1294 message.peerInfo->sctpParams->myIP.c_str(),
1295 message.peerInfo->sctpParams->rmrPort,
1296 res.c_str());
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001297// }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001298
ss412g4aeeda22020-06-16 14:12:41 +03001299 if (logLevel >= MDCLOG_DEBUG) {
1300 mdclog_write(MDCLOG_DEBUG, "Setup request of size %d :\n %s\n", rmrMsg->len, rmrMsg->payload);
1301 }
1302 // send to RMR
1303 rmrMsg->mtype = message.message.messageType;
1304 rmrMsg->state = 0;
1305 rmr_bytes2meid(rmrMsg, (unsigned char *) message.message.enodbName, strlen(message.message.enodbName));
aa7133@att.com84bd3342020-03-16 18:04:57 +02001306
ss412g4aeeda22020-06-16 14:12:41 +03001307 static unsigned char tx[32];
1308 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1309 rmr_bytes2xact(rmrMsg, tx, strlen((const char *) tx));
1310
1311 rmrMsg = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMsg);
1312 if (rmrMsg == nullptr) {
1313 mdclog_write(MDCLOG_ERR, "RMR failed to send returned nullptr");
1314 } else if (rmrMsg->state != 0) {
1315 char meid[RMR_MAX_MEID]{};
1316 if (rmrMsg->state == RMR_ERR_RETRY) {
1317 usleep(5);
1318 rmrMsg->state = 0;
1319 mdclog_write(MDCLOG_INFO, "RETRY sending Message %d to Xapp from %s",
1320 rmrMsg->mtype, rmr_get_meid(rmrMsg, (unsigned char *) meid));
1321 rmrMsg = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMsg);
1322 if (rmrMsg == nullptr) {
1323 mdclog_write(MDCLOG_ERR, "RMR failed send returned nullptr");
1324 } else if (rmrMsg->state != 0) {
1325 mdclog_write(MDCLOG_ERR,
1326 "RMR Retry failed %s sending request %d to Xapp from %s",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001327 translateRmrErrorMessages(rmrMsg->state).c_str(),
1328 rmrMsg->mtype,
1329 rmr_get_meid(rmrMsg, (unsigned char *) meid));
1330 }
ss412g4aeeda22020-06-16 14:12:41 +03001331 } else {
1332 mdclog_write(MDCLOG_ERR, "RMR failed: %s. sending request %d to Xapp from %s",
1333 translateRmrErrorMessages(rmrMsg->state).c_str(),
1334 rmrMsg->mtype,
1335 rmr_get_meid(rmrMsg, (unsigned char *) meid));
aa7133@att.combc110772020-03-25 09:45:14 +02001336 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001337 }
ss412g4aeeda22020-06-16 14:12:41 +03001338 message.peerInfo->gotSetup = true;
1339 buildJsonMessage(message);
Byonggon Chun51885f32020-11-18 15:31:04 +09001340
ss412g4aeeda22020-06-16 14:12:41 +03001341 if (rmrMsg != nullptr) {
1342 rmr_free_msg(rmrMsg);
1343 }
Byonggon Chun51885f32020-11-18 15:31:04 +09001344 free(buffer);
1345
1346 return;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001347}
aa7133@att.comc5a15202020-04-05 19:57:01 +03001348
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001349#if 0
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001350int RAN_Function_list_To_Vector(RANfunctions_List_t& list, vector <string> &runFunXML_v) {
1351 auto index = 0;
1352 runFunXML_v.clear();
1353 for (auto j = 0; j < list.list.count; j++) {
1354 auto *raNfunctionItemIEs = (RANfunction_ItemIEs_t *)list.list.array[j];
1355 if (raNfunctionItemIEs->id == ProtocolIE_ID_id_RANfunction_Item &&
1356 (raNfunctionItemIEs->value.present == RANfunction_ItemIEs__value_PR_RANfunction_Item)) {
1357 // encode to xml
1358 E2SM_gNB_NRT_RANfunction_Definition_t *ranFunDef = nullptr;
1359 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER,
1360 &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition,
1361 (void **)&ranFunDef,
1362 raNfunctionItemIEs->value.choice.RANfunction_Item.ranFunctionDefinition.buf,
1363 raNfunctionItemIEs->value.choice.RANfunction_Item.ranFunctionDefinition.size);
1364 if (rval.code != RC_OK) {
1365 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) E2SM message from : %s",
1366 rval.code,
1367 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name);
1368 return -1;
1369 }
1370
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001371 auto xml_buffer_size = RECEIVE_SCTP_BUFFER_SIZE * 2;
1372 unsigned char xml_buffer[RECEIVE_SCTP_BUFFER_SIZE * 2];
ss412g4aeeda22020-06-16 14:12:41 +03001373 memset(xml_buffer, 0, RECEIVE_SCTP_BUFFER_SIZE * 2);
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001374 // encode to xml
1375 auto er = asn_encode_to_buffer(nullptr,
1376 ATS_BASIC_XER,
1377 &asn_DEF_E2SM_gNB_NRT_RANfunction_Definition,
1378 ranFunDef,
1379 xml_buffer,
1380 xml_buffer_size);
1381 if (er.encoded == -1) {
1382 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s",
1383 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name,
1384 strerror(errno));
1385 } else if (er.encoded > (ssize_t)xml_buffer_size) {
1386 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
1387 (int) xml_buffer_size,
1388 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name, __func__, __LINE__);
1389 } else {
1390 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1391 mdclog_write(MDCLOG_DEBUG, "Encoding E2SM %s PDU number %d : %s",
1392 asn_DEF_E2SM_gNB_NRT_RANfunction_Definition.name,
1393 index++,
1394 xml_buffer);
1395 }
ss412g4aeeda22020-06-16 14:12:41 +03001396
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001397 string runFuncs = (char *)(xml_buffer);
1398 runFunXML_v.emplace_back(runFuncs);
1399 }
1400 }
1401 }
1402 return 0;
1403}
aa7133@att.comc5a15202020-04-05 19:57:01 +03001404
ss412g4aeeda22020-06-16 14:12:41 +03001405int collectServiceUpdate_RequestData(E2AP_PDU_t *pdu,
1406 Sctp_Map_t *sctpMap,
1407 ReportingMessages_t &message,
1408 vector <string> &RANfunctionsAdded_v,
1409 vector <string> &RANfunctionsModified_v) {
aa7133@att.comc5a15202020-04-05 19:57:01 +03001410 memset(message.peerInfo->enodbName, 0 , MAX_ENODB_NAME_SIZE);
ss412g4aeeda22020-06-16 14:12:41 +03001411 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICserviceUpdate.protocolIEs.list.count; i++) {
1412 auto *ie = pdu->choice.initiatingMessage->value.choice.RICserviceUpdate.protocolIEs.list.array[i];
1413 if (ie->id == ProtocolIE_ID_id_RANfunctionsAdded) {
1414 if (ie->value.present == RICserviceUpdate_IEs__value_PR_RANfunctionsID_List) {
aa7133@att.comc5a15202020-04-05 19:57:01 +03001415 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001416 mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1417 ie->value.choice.RANfunctions_List.list.count);
aa7133@att.comc5a15202020-04-05 19:57:01 +03001418 }
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001419 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsAdded_v) != 0 ) {
1420 return -1;
1421 }
1422 }
1423 } else if (ie->id == ProtocolIE_ID_id_RANfunctionsModified) {
ss412g4aeeda22020-06-16 14:12:41 +03001424 if (ie->value.present == RICserviceUpdate_IEs__value_PR_RANfunctions_List) {
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001425 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1426 mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1427 ie->value.choice.RANfunctions_List.list.count);
1428 }
1429 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsModified_v) != 0 ) {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001430 return -1;
aa7133@att.comc5a15202020-04-05 19:57:01 +03001431 }
1432 }
1433 }
1434 }
1435 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1436 mdclog_write(MDCLOG_DEBUG, "Run function vector have %ld entries",
aa7133@att.com0829b8b2020-04-16 19:27:42 +03001437 RANfunctionsAdded_v.size());
aa7133@att.comc5a15202020-04-05 19:57:01 +03001438 }
1439 return 0;
1440}
ss412g4aeeda22020-06-16 14:12:41 +03001441
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001442#endif
ss412g4aeeda22020-06-16 14:12:41 +03001443
1444
aa7133@att.com409f2232020-10-29 13:06:45 +03001445void buildPrometheusList(ConnectedCU_t *peerInfo, Family<Counter> *prometheusFamily) {
aa7133@att.coma4c80012020-06-24 16:36:47 +03001446 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"SetupRequest", "Messages"}});
1447 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"SetupRequest", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001448
aa7133@att.coma4c80012020-06-24 16:36:47 +03001449 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ErrorIndication", "Messages"}});
1450 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ErrorIndication", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001451
aa7133@att.coma4c80012020-06-24 16:36:47 +03001452 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_RICindication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICindication", "Messages"}});
1453 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_RICindication)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICindication", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001454
aa7133@att.coma4c80012020-06-24 16:36:47 +03001455 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetRequest", "Messages"}});
1456 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetRequest", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001457
aa7133@att.coma4c80012020-06-24 16:36:47 +03001458 peerInfo->counters[IN_INITI][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICserviceUpdate", "Messages"}});
1459 peerInfo->counters[IN_INITI][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICserviceUpdate", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001460 // ---------------------------------------------
aa7133@att.coma4c80012020-06-24 16:36:47 +03001461 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetACK", "Messages"}});
1462 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"ResetACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001463
aa7133@att.coma4c80012020-06-24 16:36:47 +03001464 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolACK", "Messages"}});
1465 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001466
aa7133@att.coma4c80012020-06-24 16:36:47 +03001467 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionACK", "Messages"}});
1468 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001469
aa7133@att.coma4c80012020-06-24 16:36:47 +03001470 peerInfo->counters[IN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteACK", "Messages"}});
1471 peerInfo->counters[IN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001472 //-------------------------------------------------------------
1473
aa7133@att.coma4c80012020-06-24 16:36:47 +03001474 peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolFailure", "Messages"}});
1475 peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICcontrolFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001476
aa7133@att.coma4c80012020-06-24 16:36:47 +03001477 peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionFailure", "Messages"}});
1478 peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001479
aa7133@att.coma4c80012020-06-24 16:36:47 +03001480 peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteFailure", "Messages"}});
1481 peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "IN"}, {"RICsubscriptionDeleteFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001482
1483 //====================================================================================
aa7133@att.coma4c80012020-06-24 16:36:47 +03001484 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ErrorIndication", "Messages"}});
1485 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_ErrorIndication)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ErrorIndication", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001486
aa7133@att.coma4c80012020-06-24 16:36:47 +03001487 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetRequest", "Messages"}});
1488 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetRequest", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001489
aa7133@att.coma4c80012020-06-24 16:36:47 +03001490 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICcontrol", "Messages"}});
1491 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICcontrol)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICcontrol", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001492
aa7133@att.coma4c80012020-06-24 16:36:47 +03001493 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICserviceQuery)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceQuery", "Messages"}});
1494 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICserviceQuery)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceQuery", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001495
aa7133@att.coma4c80012020-06-24 16:36:47 +03001496 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscription", "Messages"}});
1497 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICsubscription)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscription", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001498
aa7133@att.coma4c80012020-06-24 16:36:47 +03001499 peerInfo->counters[OUT_INITI][MSG_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscriptionDelete", "Messages"}});
1500 peerInfo->counters[OUT_INITI][BYTES_COUNTER][(ProcedureCode_id_RICsubscriptionDelete)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICsubscriptionDelete", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001501 //---------------------------------------------------------------------------------------------------------
aa7133@att.coma4c80012020-06-24 16:36:47 +03001502 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupResponse", "Messages"}});
1503 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupResponse", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001504
aa7133@att.coma4c80012020-06-24 16:36:47 +03001505 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetACK", "Messages"}});
1506 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_Reset)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"ResetACK", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001507
aa7133@att.coma4c80012020-06-24 16:36:47 +03001508 peerInfo->counters[OUT_SUCC][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateResponse", "Messages"}});
1509 peerInfo->counters[OUT_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateResponse", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001510 //----------------------------------------------------------------------------------------------------------------
aa7133@att.coma4c80012020-06-24 16:36:47 +03001511 peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupRequestFailure", "Messages"}});
1512 peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_E2setup)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"SetupRequestFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001513
aa7133@att.coma4c80012020-06-24 16:36:47 +03001514 peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateFailure", "Messages"}});
1515 peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][(ProcedureCode_id_RICserviceUpdate)] = &prometheusFamily->Add({{peerInfo->enodbName, "OUT"}, {"RICserviceUpdateFailure", "Bytes"}});
ss412g4aeeda22020-06-16 14:12:41 +03001516}
1517/**
1518 *
1519 * @param pdu
1520 * @param sctpMap
1521 * @param message
1522 * @param RANfunctionsAdded_v
1523 * @return
1524 */
1525int collectSetupRequestData(E2AP_PDU_t *pdu,
1526 Sctp_Map_t *sctpMap,
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001527 ReportingMessages_t &message /*, vector <string> &RANfunctionsAdded_v*/) {
ss412g4aeeda22020-06-16 14:12:41 +03001528 memset(message.peerInfo->enodbName, 0 , MAX_ENODB_NAME_SIZE);
1529 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.count; i++) {
1530 auto *ie = pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.array[i];
1531 if (ie->id == ProtocolIE_ID_id_GlobalE2node_ID) {
1532 // get the ran name for meid
1533 if (ie->value.present == E2setupRequestIEs__value_PR_GlobalE2node_ID) {
1534 if (buildRanName(message.peerInfo->enodbName, ie) < 0) {
1535 mdclog_write(MDCLOG_ERR, "Bad param in E2setupRequestIEs GlobalE2node_ID.\n");
aa7133@att.com409f2232020-10-29 13:06:45 +03001536 // no message will be sent
ss412g4aeeda22020-06-16 14:12:41 +03001537 return -1;
1538 }
1539
1540 memcpy(message.message.enodbName, message.peerInfo->enodbName, strlen(message.peerInfo->enodbName));
1541 sctpMap->setkey(message.message.enodbName, message.peerInfo);
1542 }
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001543 } /*else if (ie->id == ProtocolIE_ID_id_RANfunctionsAdded) {
ss412g4aeeda22020-06-16 14:12:41 +03001544 if (ie->value.present == E2setupRequestIEs__value_PR_RANfunctions_List) {
1545 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1546 mdclog_write(MDCLOG_DEBUG, "Run function list have %d entries",
1547 ie->value.choice.RANfunctions_List.list.count);
1548 }
1549 if (RAN_Function_list_To_Vector(ie->value.choice.RANfunctions_List, RANfunctionsAdded_v) != 0 ) {
1550 return -1;
1551 }
1552 }
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001553 } */
ss412g4aeeda22020-06-16 14:12:41 +03001554 }
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001555// if (mdclog_level_get() >= MDCLOG_DEBUG) {
1556// mdclog_write(MDCLOG_DEBUG, "Run function vector have %ld entries",
1557// RANfunctionsAdded_v.size());
1558// }
ss412g4aeeda22020-06-16 14:12:41 +03001559 return 0;
1560}
1561
1562int XML_From_PER(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
1563 E2AP_PDU_t *pdu = nullptr;
1564
1565 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1566 mdclog_write(MDCLOG_DEBUG, "got PER message of size %d is:%s",
1567 rmrMessageBuffer.sendMessage->len, rmrMessageBuffer.sendMessage->payload);
1568 }
1569 auto rval = asn_decode(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, (void **) &pdu,
1570 rmrMessageBuffer.sendMessage->payload, rmrMessageBuffer.sendMessage->len);
1571 if (rval.code != RC_OK) {
1572 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) setup response from E2MGR : %s",
1573 rval.code,
1574 message.message.enodbName);
1575 return -1;
1576 }
1577
1578 int buff_size = RECEIVE_XAPP_BUFFER_SIZE;
1579 auto er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu,
1580 rmrMessageBuffer.sendMessage->payload, buff_size);
1581 if (er.encoded == -1) {
1582 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
1583 return -1;
1584 } else if (er.encoded > (ssize_t)buff_size) {
1585 mdclog_write(MDCLOG_ERR, "Buffer of size %d is to small for %s, at %s line %d",
1586 (int)rmrMessageBuffer.sendMessage->len,
1587 asn_DEF_E2AP_PDU.name,
1588 __func__,
1589 __LINE__);
1590 return -1;
1591 }
1592 rmrMessageBuffer.sendMessage->len = er.encoded;
1593 return 0;
1594
1595}
1596
ss412g1a79bdf2019-10-24 12:03:05 +03001597/**
1598 *
1599 * @param pdu
1600 * @param message
1601 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001602 */
1603void asnInitiatingRequest(E2AP_PDU_t *pdu,
aa7133@att.com7b437f72020-03-30 13:25:54 +03001604 Sctp_Map_t *sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +03001605 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +02001606 RmrMessagesBuffer_t &rmrMessageBuffer) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001607 auto logLevel = mdclog_level_get();
ss412g1a79bdf2019-10-24 12:03:05 +03001608 auto procedureCode = ((InitiatingMessage_t *) pdu->choice.initiatingMessage)->procedureCode;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001609 if (logLevel >= MDCLOG_DEBUG) {
1610 mdclog_write(MDCLOG_DEBUG, "Initiating message %ld\n", procedureCode);
ss412g1a79bdf2019-10-24 12:03:05 +03001611 }
1612 switch (procedureCode) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001613 case ProcedureCode_id_E2setup: {
1614 if (logLevel >= MDCLOG_DEBUG) {
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001615 mdclog_write(MDCLOG_DEBUG, "Got E2setup");
aa7133@att.com84bd3342020-03-16 18:04:57 +02001616 }
1617
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001618// vector <string> RANfunctionsAdded_v;
1619// vector <string> RANfunctionsModified_v;
1620// RANfunctionsAdded_v.clear();
1621// RANfunctionsModified_v.clear();
1622 if (collectSetupRequestData(pdu, sctpMap, message) != 0) {
aa7133@att.com7b437f72020-03-30 13:25:54 +03001623 break;
1624 }
1625
aa7133@att.com409f2232020-10-29 13:06:45 +03001626 buildPrometheusList(message.peerInfo, message.peerInfo->sctpParams->prometheusFamily);
ss412g4aeeda22020-06-16 14:12:41 +03001627
1628 string messageName("E2setupRequest");
1629 string ieName("E2setupRequestIEs");
1630 message.message.messageType = RIC_E2_SETUP_REQ;
aa7133@att.coma4c80012020-06-24 16:36:47 +03001631 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
1632 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment((double)message.message.asnLength);
aa7133@att.com409f2232020-10-29 13:06:45 +03001633 buildAndSendSetupRequest(message, rmrMessageBuffer, pdu);
ss412g1a79bdf2019-10-24 12:03:05 +03001634 break;
1635 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001636 case ProcedureCode_id_RICserviceUpdate: {
1637 if (logLevel >= MDCLOG_DEBUG) {
1638 mdclog_write(MDCLOG_DEBUG, "Got RICserviceUpdate %s", message.message.enodbName);
1639 }
aa7133@att.comdf4f1822020-06-16 16:23:53 +03001640// vector <string> RANfunctionsAdded_v;
1641// vector <string> RANfunctionsModified_v;
1642// RANfunctionsAdded_v.clear();
1643// RANfunctionsModified_v.clear();
1644// if (collectServiceUpdate_RequestData(pdu, sctpMap, message,
1645// RANfunctionsAdded_v, RANfunctionsModified_v) != 0) {
1646// break;
1647// }
ss412g4aeeda22020-06-16 14:12:41 +03001648
1649 string messageName("RICserviceUpdate");
1650 string ieName("RICserviceUpdateIEs");
1651 message.message.messageType = RIC_SERVICE_UPDATE;
aa7133@att.coma4c80012020-06-24 16:36:47 +03001652 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
1653 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment((double)message.message.asnLength);
ss412g4aeeda22020-06-16 14:12:41 +03001654
aa7133@att.com409f2232020-10-29 13:06:45 +03001655 buildAndSendSetupRequest(message, rmrMessageBuffer, pdu);
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001656 break;
1657 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001658 case ProcedureCode_id_ErrorIndication: {
1659 if (logLevel >= MDCLOG_DEBUG) {
1660 mdclog_write(MDCLOG_DEBUG, "Got ErrorIndication %s", message.message.enodbName);
1661 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03001662 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_ErrorIndication]->Increment();
1663 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_ErrorIndication]->Increment((double)message.message.asnLength);
aa7133@att.combe92d712020-03-18 17:30:39 +02001664 if (sendRequestToXapp(message, RIC_ERROR_INDICATION, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001665 mdclog_write(MDCLOG_ERR, "RIC_ERROR_INDICATION failed to send to xAPP");
ss412g1a79bdf2019-10-24 12:03:05 +03001666 }
1667 break;
1668 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001669 case ProcedureCode_id_Reset: {
1670 if (logLevel >= MDCLOG_DEBUG) {
1671 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001672 }
ss412g4aeeda22020-06-16 14:12:41 +03001673
aa7133@att.coma4c80012020-06-24 16:36:47 +03001674 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
1675 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment((double)message.message.asnLength);
ss412g4aeeda22020-06-16 14:12:41 +03001676 if (XML_From_PER(message, rmrMessageBuffer) < 0) {
1677 break;
ss412g1a79bdf2019-10-24 12:03:05 +03001678 }
ss412g4aeeda22020-06-16 14:12:41 +03001679
1680 if (sendRequestToXapp(message, RIC_E2_RESET_REQ, rmrMessageBuffer) != 0) {
1681 mdclog_write(MDCLOG_ERR, "RIC_E2_RESET_REQ message failed to send to xAPP");
aa7133@att.com84bd3342020-03-16 18:04:57 +02001682 }
1683 break;
1684 }
1685 case ProcedureCode_id_RICindication: {
1686 if (logLevel >= MDCLOG_DEBUG) {
1687 mdclog_write(MDCLOG_DEBUG, "Got RICindication %s", message.message.enodbName);
1688 }
1689 for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.count; i++) {
ss412g1a79bdf2019-10-24 12:03:05 +03001690 auto messageSent = false;
1691 RICindication_IEs_t *ie = pdu->choice.initiatingMessage->value.choice.RICindication.protocolIEs.list.array[i];
aa7133@att.com84bd3342020-03-16 18:04:57 +02001692 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03001693 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1694 }
1695 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001696 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03001697 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1698 }
1699 if (ie->value.present == RICindication_IEs__value_PR_RICrequestID) {
1700 static unsigned char tx[32];
1701 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_INDICATION;
1702 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1703 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
ss412g3bac2da2020-01-05 11:52:19 +02001704 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001705 (unsigned char *)message.message.enodbName,
1706 strlen(message.message.enodbName));
ss412g1a79bdf2019-10-24 12:03:05 +03001707 rmrMessageBuffer.sendMessage->state = 0;
aa7133@att.comc5a15202020-04-05 19:57:01 +03001708 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
1709
1710 //ie->value.choice.RICrequestID.ricInstanceID;
ss412g3bac2da2020-01-05 11:52:19 +02001711 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.comc5a15202020-04-05 19:57:01 +03001712 mdclog_write(MDCLOG_DEBUG, "sub id = %d, mtype = %d, ric instance id %ld, requestor id = %ld",
aa7133@att.com84bd3342020-03-16 18:04:57 +02001713 rmrMessageBuffer.sendMessage->sub_id,
aa7133@att.comc5a15202020-04-05 19:57:01 +03001714 rmrMessageBuffer.sendMessage->mtype,
1715 ie->value.choice.RICrequestID.ricInstanceID,
1716 ie->value.choice.RICrequestID.ricRequestorID);
ss412g3bac2da2020-01-05 11:52:19 +02001717 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03001718 message.peerInfo->counters[IN_INITI][MSG_COUNTER][ProcedureCode_id_RICindication]->Increment();
1719 message.peerInfo->counters[IN_INITI][BYTES_COUNTER][ProcedureCode_id_RICindication]->Increment((double)message.message.asnLength);
aa7133@att.combe92d712020-03-18 17:30:39 +02001720 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03001721 messageSent = true;
1722 } else {
aa7133@att.com409f2232020-10-29 13:06:45 +03001723 mdclog_write(MDCLOG_ERR, "RIC request id missing illegal request");
ss412g1a79bdf2019-10-24 12:03:05 +03001724 }
1725 }
1726 if (messageSent) {
1727 break;
1728 }
1729 }
1730 break;
1731 }
ss412g1a79bdf2019-10-24 12:03:05 +03001732 default: {
1733 mdclog_write(MDCLOG_ERR, "Undefined or not supported message = %ld", procedureCode);
1734 message.message.messageType = 0; // no RMR message type yet
1735
1736 buildJsonMessage(message);
1737
1738 break;
1739 }
1740 }
ss412g1a79bdf2019-10-24 12:03:05 +03001741}
1742
1743/**
1744 *
1745 * @param pdu
1746 * @param message
ss412g1a79bdf2019-10-24 12:03:05 +03001747 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001748 */
aa7133@att.com409f2232020-10-29 13:06:45 +03001749void asnSuccessfulMsg(E2AP_PDU_t *pdu,
aa7133@att.com7b437f72020-03-30 13:25:54 +03001750 Sctp_Map_t *sctpMap,
1751 ReportingMessages_t &message,
1752 RmrMessagesBuffer_t &rmrMessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03001753 auto procedureCode = pdu->choice.successfulOutcome->procedureCode;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001754 auto logLevel = mdclog_level_get();
1755 if (logLevel >= MDCLOG_INFO) {
ss412g1a79bdf2019-10-24 12:03:05 +03001756 mdclog_write(MDCLOG_INFO, "Successful Outcome %ld", procedureCode);
1757 }
1758 switch (procedureCode) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001759 case ProcedureCode_id_Reset: {
1760 if (logLevel >= MDCLOG_DEBUG) {
1761 mdclog_write(MDCLOG_DEBUG, "Got Reset %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001762 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03001763 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
1764 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment((double)message.message.asnLength);
ss412g4aeeda22020-06-16 14:12:41 +03001765 if (XML_From_PER(message, rmrMessageBuffer) < 0) {
1766 break;
1767 }
1768 if (sendRequestToXapp(message, RIC_E2_RESET_RESP, rmrMessageBuffer) != 0) {
1769 mdclog_write(MDCLOG_ERR, "RIC_E2_RESET_RESP message failed to send to xAPP");
ss412g1a79bdf2019-10-24 12:03:05 +03001770 }
1771 break;
1772 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001773 case ProcedureCode_id_RICcontrol: {
1774 if (logLevel >= MDCLOG_DEBUG) {
1775 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001776 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001777 for (auto i = 0;
ss412g1a79bdf2019-10-24 12:03:05 +03001778 i < pdu->choice.successfulOutcome->value.choice.RICcontrolAcknowledge.protocolIEs.list.count; i++) {
1779 auto messageSent = false;
1780 RICcontrolAcknowledge_IEs_t *ie = pdu->choice.successfulOutcome->value.choice.RICcontrolAcknowledge.protocolIEs.list.array[i];
1781 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1782 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1783 }
1784 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
1785 if (mdclog_level_get() >= MDCLOG_DEBUG) {
1786 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1787 }
1788 if (ie->value.present == RICcontrolAcknowledge_IEs__value_PR_RICrequestID) {
1789 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_CONTROL_ACK;
1790 rmrMessageBuffer.sendMessage->state = 0;
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001791// rmrMessageBuffer.sendMessage->sub_id = (int) ie->value.choice.RICrequestID.ricRequestorID;
1792 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
1793
ss412g1a79bdf2019-10-24 12:03:05 +03001794 static unsigned char tx[32];
1795 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1796 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
ss412g3bac2da2020-01-05 11:52:19 +02001797 rmr_bytes2meid(rmrMessageBuffer.sendMessage,
aa7133@att.com84bd3342020-03-16 18:04:57 +02001798 (unsigned char *)message.message.enodbName,
1799 strlen(message.message.enodbName));
ss412g1a79bdf2019-10-24 12:03:05 +03001800
aa7133@att.coma4c80012020-06-24 16:36:47 +03001801 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
1802 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment((double)message.message.asnLength);
aa7133@att.combe92d712020-03-18 17:30:39 +02001803 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03001804 messageSent = true;
1805 } else {
aa7133@att.com409f2232020-10-29 13:06:45 +03001806 mdclog_write(MDCLOG_ERR, "RIC request id missing illegal request");
ss412g1a79bdf2019-10-24 12:03:05 +03001807 }
1808 }
1809 if (messageSent) {
1810 break;
1811 }
1812 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001813
1814 break;
1815 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001816 case ProcedureCode_id_RICsubscription: {
1817 if (logLevel >= MDCLOG_DEBUG) {
1818 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
1819 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03001820 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
1821 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment((double)message.message.asnLength);
aa7133@att.combe92d712020-03-18 17:30:39 +02001822 if (sendRequestToXapp(message, RIC_SUB_RESP, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001823 mdclog_write(MDCLOG_ERR, "Subscription successful message failed to send to xAPP");
1824 }
1825 break;
1826 }
1827 case ProcedureCode_id_RICsubscriptionDelete: {
1828 if (logLevel >= MDCLOG_DEBUG) {
1829 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
1830 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03001831 message.peerInfo->counters[IN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
1832 message.peerInfo->counters[IN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment((double)message.message.asnLength);
aa7133@att.combe92d712020-03-18 17:30:39 +02001833 if (sendRequestToXapp(message, RIC_SUB_DEL_RESP, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001834 mdclog_write(MDCLOG_ERR, "Subscription delete successful message failed to send to xAPP");
1835 }
ss412g1a79bdf2019-10-24 12:03:05 +03001836 break;
1837 }
1838 default: {
1839 mdclog_write(MDCLOG_WARN, "Undefined or not supported message = %ld", procedureCode);
1840 message.message.messageType = 0; // no RMR message type yet
1841 buildJsonMessage(message);
1842
1843 break;
1844 }
1845 }
ss412g1a79bdf2019-10-24 12:03:05 +03001846}
1847
1848/**
1849 *
1850 * @param pdu
1851 * @param message
ss412g1a79bdf2019-10-24 12:03:05 +03001852 * @param rmrMessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001853 */
1854void asnUnSuccsesfulMsg(E2AP_PDU_t *pdu,
aa7133@att.com7b437f72020-03-30 13:25:54 +03001855 Sctp_Map_t *sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +03001856 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +02001857 RmrMessagesBuffer_t &rmrMessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03001858 auto procedureCode = pdu->choice.unsuccessfulOutcome->procedureCode;
aa7133@att.com84bd3342020-03-16 18:04:57 +02001859 auto logLevel = mdclog_level_get();
1860 if (logLevel >= MDCLOG_INFO) {
ss412g1a79bdf2019-10-24 12:03:05 +03001861 mdclog_write(MDCLOG_INFO, "Unsuccessful Outcome %ld", procedureCode);
1862 }
1863 switch (procedureCode) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001864 case ProcedureCode_id_RICcontrol: {
1865 if (logLevel >= MDCLOG_DEBUG) {
1866 mdclog_write(MDCLOG_DEBUG, "Got RICcontrol %s", message.message.enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03001867 }
1868 for (int i = 0;
1869 i < pdu->choice.unsuccessfulOutcome->value.choice.RICcontrolFailure.protocolIEs.list.count; i++) {
1870 auto messageSent = false;
1871 RICcontrolFailure_IEs_t *ie = pdu->choice.unsuccessfulOutcome->value.choice.RICcontrolFailure.protocolIEs.list.array[i];
aa7133@att.com84bd3342020-03-16 18:04:57 +02001872 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03001873 mdclog_write(MDCLOG_DEBUG, "ie type (ProtocolIE_ID) = %ld", ie->id);
1874 }
1875 if (ie->id == ProtocolIE_ID_id_RICrequestID) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001876 if (logLevel >= MDCLOG_DEBUG) {
ss412g1a79bdf2019-10-24 12:03:05 +03001877 mdclog_write(MDCLOG_DEBUG, "Got RIC requestId entry, ie type (ProtocolIE_ID) = %ld", ie->id);
1878 }
1879 if (ie->value.present == RICcontrolFailure_IEs__value_PR_RICrequestID) {
1880 message.message.messageType = rmrMessageBuffer.sendMessage->mtype = RIC_CONTROL_FAILURE;
1881 rmrMessageBuffer.sendMessage->state = 0;
aa7133@att.com0793fcb2020-04-16 11:34:26 +03001882// rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricRequestorID;
1883 rmrMessageBuffer.sendMessage->sub_id = (int)ie->value.choice.RICrequestID.ricInstanceID;
ss412g1a79bdf2019-10-24 12:03:05 +03001884 static unsigned char tx[32];
1885 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1886 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, strlen((const char *) tx));
aa7133@att.com84bd3342020-03-16 18:04:57 +02001887 rmr_bytes2meid(rmrMessageBuffer.sendMessage, (unsigned char *) message.message.enodbName,
1888 strlen(message.message.enodbName));
aa7133@att.coma4c80012020-06-24 16:36:47 +03001889 message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
1890 message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment((double)message.message.asnLength);
aa7133@att.combe92d712020-03-18 17:30:39 +02001891 sendRmrMessage(rmrMessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03001892 messageSent = true;
1893 } else {
aa7133@att.com409f2232020-10-29 13:06:45 +03001894 mdclog_write(MDCLOG_ERR, "RIC request id missing illegal request");
ss412g1a79bdf2019-10-24 12:03:05 +03001895 }
1896 }
1897 if (messageSent) {
1898 break;
1899 }
1900 }
1901 break;
1902 }
aa7133@att.com84bd3342020-03-16 18:04:57 +02001903 case ProcedureCode_id_RICsubscription: {
1904 if (logLevel >= MDCLOG_DEBUG) {
1905 mdclog_write(MDCLOG_DEBUG, "Got RICsubscription %s", message.message.enodbName);
1906 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03001907 message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
1908 message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment((double)message.message.asnLength);
aa7133@att.combe92d712020-03-18 17:30:39 +02001909 if (sendRequestToXapp(message, RIC_SUB_FAILURE, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001910 mdclog_write(MDCLOG_ERR, "Subscription unsuccessful message failed to send to xAPP");
1911 }
1912 break;
1913 }
1914 case ProcedureCode_id_RICsubscriptionDelete: {
1915 if (logLevel >= MDCLOG_DEBUG) {
1916 mdclog_write(MDCLOG_DEBUG, "Got RICsubscriptionDelete %s", message.message.enodbName);
1917 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03001918 message.peerInfo->counters[IN_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
1919 message.peerInfo->counters[IN_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment((double)message.message.asnLength);
ss412g4aeeda22020-06-16 14:12:41 +03001920 if (sendRequestToXapp(message, RIC_SUB_FAILURE, rmrMessageBuffer) != 0) {
aa7133@att.com84bd3342020-03-16 18:04:57 +02001921 mdclog_write(MDCLOG_ERR, "Subscription Delete unsuccessful message failed to send to xAPP");
1922 }
1923 break;
1924 }
ss412g1a79bdf2019-10-24 12:03:05 +03001925 default: {
1926 mdclog_write(MDCLOG_WARN, "Undefined or not supported message = %ld", procedureCode);
1927 message.message.messageType = 0; // no RMR message type yet
1928
1929 buildJsonMessage(message);
1930
1931 break;
1932 }
1933 }
ss412g1a79bdf2019-10-24 12:03:05 +03001934}
1935
1936/**
1937 *
1938 * @param message
1939 * @param requestId
1940 * @param rmrMmessageBuffer
ss412g1a79bdf2019-10-24 12:03:05 +03001941 * @return
1942 */
1943int sendRequestToXapp(ReportingMessages_t &message,
1944 int requestId,
aa7133@att.combe92d712020-03-18 17:30:39 +02001945 RmrMessagesBuffer_t &rmrMmessageBuffer) {
ss412g1a79bdf2019-10-24 12:03:05 +03001946 rmr_bytes2meid(rmrMmessageBuffer.sendMessage,
1947 (unsigned char *)message.message.enodbName,
1948 strlen(message.message.enodbName));
1949 message.message.messageType = rmrMmessageBuffer.sendMessage->mtype = requestId;
1950 rmrMmessageBuffer.sendMessage->state = 0;
1951 static unsigned char tx[32];
1952 snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
1953 rmr_bytes2xact(rmrMmessageBuffer.sendMessage, tx, strlen((const char *) tx));
1954
aa7133@att.combe92d712020-03-18 17:30:39 +02001955 auto rc = sendRmrMessage(rmrMmessageBuffer, message);
ss412g1a79bdf2019-10-24 12:03:05 +03001956 return rc;
1957}
1958
ss412g4aeeda22020-06-16 14:12:41 +03001959/**
1960 *
1961 * @param pSctpParams
1962 */
aa7133@att.combe92d712020-03-18 17:30:39 +02001963void getRmrContext(sctp_params_t &pSctpParams) {
ss412g3bac2da2020-01-05 11:52:19 +02001964 pSctpParams.rmrCtx = nullptr;
aa7133@att.comab0e0f42020-04-06 17:13:58 +03001965 pSctpParams.rmrCtx = rmr_init(pSctpParams.rmrAddress, RECEIVE_XAPP_BUFFER_SIZE, RMRFL_NONE);
ss412g3bac2da2020-01-05 11:52:19 +02001966 if (pSctpParams.rmrCtx == nullptr) {
1967 mdclog_write(MDCLOG_ERR, "Failed to initialize RMR");
ss412g3bac2da2020-01-05 11:52:19 +02001968 return;
ss412g1a79bdf2019-10-24 12:03:05 +03001969 }
1970
ss412g3bac2da2020-01-05 11:52:19 +02001971 rmr_set_stimeout(pSctpParams.rmrCtx, 0); // disable retries for any send operation
ss412g1a79bdf2019-10-24 12:03:05 +03001972 // we need to find that routing table exist and we can run
1973 if (mdclog_level_get() >= MDCLOG_INFO) {
1974 mdclog_write(MDCLOG_INFO, "We are after RMR INIT wait for RMR_Ready");
1975 }
1976 int rmrReady = 0;
1977 int count = 0;
1978 while (!rmrReady) {
ss412g3bac2da2020-01-05 11:52:19 +02001979 if ((rmrReady = rmr_ready(pSctpParams.rmrCtx)) == 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03001980 sleep(1);
1981 }
1982 count++;
1983 if (count % 60 == 0) {
1984 mdclog_write(MDCLOG_INFO, "waiting to RMR ready state for %d seconds", count);
1985 }
1986 }
1987 if (mdclog_level_get() >= MDCLOG_INFO) {
1988 mdclog_write(MDCLOG_INFO, "RMR running");
1989 }
ss412g3bac2da2020-01-05 11:52:19 +02001990 rmr_init_trace(pSctpParams.rmrCtx, 200);
1991 // get the RMR fd for the epoll
1992 pSctpParams.rmrListenFd = rmr_get_rcvfd(pSctpParams.rmrCtx);
1993 struct epoll_event event{};
1994 // add RMR fd to epoll
1995 event.events = (EPOLLIN);
1996 event.data.fd = pSctpParams.rmrListenFd;
1997 // add listening RMR FD to epoll
1998 if (epoll_ctl(pSctpParams.epoll_fd, EPOLL_CTL_ADD, pSctpParams.rmrListenFd, &event)) {
1999 mdclog_write(MDCLOG_ERR, "Failed to add RMR descriptor to epoll");
2000 close(pSctpParams.rmrListenFd);
2001 rmr_close(pSctpParams.rmrCtx);
2002 pSctpParams.rmrCtx = nullptr;
2003 }
ss412g1a79bdf2019-10-24 12:03:05 +03002004}
2005
ss412g4aeeda22020-06-16 14:12:41 +03002006/**
2007 *
2008 * @param message
2009 * @param rmrMessageBuffer
2010 * @return
2011 */
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002012int PER_FromXML(ReportingMessages_t &message, RmrMessagesBuffer_t &rmrMessageBuffer) {
aa7133@att.com07e256f2020-04-20 15:59:51 +03002013 E2AP_PDU_t *pdu = nullptr;
aa7133@att.com1c4f50f2020-03-30 14:30:52 +03002014
2015 if (mdclog_level_get() >= MDCLOG_DEBUG) {
ss412g4aeeda22020-06-16 14:12:41 +03002016 mdclog_write(MDCLOG_DEBUG, "got xml Format data from xApp of size %d is:%s",
aa7133@att.com07e256f2020-04-20 15:59:51 +03002017 rmrMessageBuffer.rcvMessage->len, rmrMessageBuffer.rcvMessage->payload);
aa7133@att.com1c4f50f2020-03-30 14:30:52 +03002018 }
aa7133@att.com1fecb052020-03-29 09:44:09 +03002019 auto rval = asn_decode(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, (void **) &pdu,
2020 rmrMessageBuffer.rcvMessage->payload, rmrMessageBuffer.rcvMessage->len);
aa7133@att.com409f2232020-10-29 13:06:45 +03002021 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2022 mdclog_write(MDCLOG_DEBUG, "%s After decoding the XML to PDU", __func__ );
2023 }
aa7133@att.com1fecb052020-03-29 09:44:09 +03002024 if (rval.code != RC_OK) {
aa7133@att.com1c4f50f2020-03-30 14:30:52 +03002025 mdclog_write(MDCLOG_ERR, "Error %d Decoding (unpack) setup response from E2MGR : %s",
aa7133@att.com1fecb052020-03-29 09:44:09 +03002026 rval.code,
2027 message.message.enodbName);
2028 return -1;
2029 }
2030
aa7133@att.coma0f0db62020-04-01 16:52:50 +03002031 int buff_size = RECEIVE_XAPP_BUFFER_SIZE;
2032 auto er = asn_encode_to_buffer(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, pdu,
2033 rmrMessageBuffer.rcvMessage->payload, buff_size);
aa7133@att.com409f2232020-10-29 13:06:45 +03002034 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2035 mdclog_write(MDCLOG_DEBUG, "%s After encoding PDU to PER", __func__ );
2036 }
aa7133@att.com1fecb052020-03-29 09:44:09 +03002037 if (er.encoded == -1) {
2038 mdclog_write(MDCLOG_ERR, "encoding of %s failed, %s", asn_DEF_E2AP_PDU.name, strerror(errno));
2039 return -1;
aa7133@att.coma0f0db62020-04-01 16:52:50 +03002040 } else if (er.encoded > (ssize_t)buff_size) {
2041 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 +03002042 (int)rmrMessageBuffer.rcvMessage->len,
aa7133@att.coma0f0db62020-04-01 16:52:50 +03002043 asn_DEF_E2AP_PDU.name,
2044 __func__,
2045 __LINE__);
aa7133@att.com1fecb052020-03-29 09:44:09 +03002046 return -1;
2047 }
aa7133@att.coma0f0db62020-04-01 16:52:50 +03002048 rmrMessageBuffer.rcvMessage->len = er.encoded;
aa7133@att.com1fecb052020-03-29 09:44:09 +03002049 return 0;
2050}
2051
ss412g1a79bdf2019-10-24 12:03:05 +03002052/**
2053 *
ss412g1a79bdf2019-10-24 12:03:05 +03002054 * @param sctpMap
2055 * @param rmrMessageBuffer
2056 * @param ts
ss412g1a79bdf2019-10-24 12:03:05 +03002057 * @return
2058 */
aa7133@att.com6680c3b2020-03-22 12:06:58 +02002059int receiveXappMessages(Sctp_Map_t *sctpMap,
ss412g1a79bdf2019-10-24 12:03:05 +03002060 RmrMessagesBuffer_t &rmrMessageBuffer,
aa7133@att.combe92d712020-03-18 17:30:39 +02002061 struct timespec &ts) {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002062 int loglevel = mdclog_level_get();
ss412g1a79bdf2019-10-24 12:03:05 +03002063 if (rmrMessageBuffer.rcvMessage == nullptr) {
2064 //we have error
2065 mdclog_write(MDCLOG_ERR, "RMR Allocation message, %s", strerror(errno));
ss412g1a79bdf2019-10-24 12:03:05 +03002066 return -1;
2067 }
2068
aa7133@att.comf13529a2020-08-27 11:21:16 +03002069// if (loglevel >= MDCLOG_DEBUG) {
2070// mdclog_write(MDCLOG_DEBUG, "Call to rmr_rcv_msg");
2071// }
ss412g1a79bdf2019-10-24 12:03:05 +03002072 rmrMessageBuffer.rcvMessage = rmr_rcv_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.rcvMessage);
2073 if (rmrMessageBuffer.rcvMessage == nullptr) {
aa7133@att.com409f2232020-10-29 13:06:45 +03002074 mdclog_write(MDCLOG_ERR, "RMR Receiving message with null pointer, Reallocated rmr message buffer");
ss412g1a79bdf2019-10-24 12:03:05 +03002075 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
ss412g1a79bdf2019-10-24 12:03:05 +03002076 return -2;
2077 }
2078 ReportingMessages_t message;
2079 message.message.direction = 'D';
2080 message.message.time.tv_nsec = ts.tv_nsec;
2081 message.message.time.tv_sec = ts.tv_sec;
2082
2083 // get message payload
2084 //auto msgData = msg->payload;
2085 if (rmrMessageBuffer.rcvMessage->state != 0) {
aa7133@att.com409f2232020-10-29 13:06:45 +03002086 mdclog_write(MDCLOG_ERR, "RMR Receiving message with stat = %d", rmrMessageBuffer.rcvMessage->state);
ss412g1a79bdf2019-10-24 12:03:05 +03002087 return -1;
2088 }
aa7133@att.com1fecb052020-03-29 09:44:09 +03002089 rmr_get_meid(rmrMessageBuffer.rcvMessage, (unsigned char *)message.message.enodbName);
ss412g4aeeda22020-06-16 14:12:41 +03002090 message.peerInfo = (ConnectedCU_t *) sctpMap->find(message.message.enodbName);
2091 if (message.peerInfo == nullptr) {
2092 auto type = rmrMessageBuffer.rcvMessage->mtype;
2093 switch (type) {
2094 case RIC_SCTP_CLEAR_ALL:
2095 case E2_TERM_KEEP_ALIVE_REQ:
2096 case RIC_HEALTH_CHECK_REQ:
2097 break;
2098 default:
2099 mdclog_write(MDCLOG_ERR, "Failed to send message no CU entry %s", message.message.enodbName);
2100 return -1;
2101 }
2102 }
2103
aa7133@att.comde40d1c2020-08-26 16:19:20 +03002104 if (rmrMessageBuffer.rcvMessage->mtype != RIC_HEALTH_CHECK_REQ) {
2105 num_of_XAPP_messages.fetch_add(1, std::memory_order_release);
2106
2107 }
ss412g1a79bdf2019-10-24 12:03:05 +03002108 switch (rmrMessageBuffer.rcvMessage->mtype) {
aa7133@att.combe92d712020-03-18 17:30:39 +02002109 case RIC_E2_SETUP_RESP : {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002110 if (loglevel >= MDCLOG_DEBUG) {
2111 mdclog_write(MDCLOG_DEBUG, "RIC_E2_SETUP_RESP");
2112 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002113 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
aa7133@att.com1fecb052020-03-29 09:44:09 +03002114 break;
2115 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002116 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2117 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002118 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2119 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_SETUP_RESP");
2120 return -6;
ss412g1a79bdf2019-10-24 12:03:05 +03002121 }
2122 break;
2123 }
aa7133@att.combe92d712020-03-18 17:30:39 +02002124 case RIC_E2_SETUP_FAILURE : {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002125 if (loglevel >= MDCLOG_DEBUG) {
2126 mdclog_write(MDCLOG_DEBUG, "RIC_E2_SETUP_FAILURE");
2127 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002128 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
aa7133@att.com1fecb052020-03-29 09:44:09 +03002129 break;
2130 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002131 message.peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_E2setup]->Increment();
2132 message.peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_E2setup]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002133 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2134 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_SETUP_FAILURE");
2135 return -6;
ss412g1a79bdf2019-10-24 12:03:05 +03002136 }
2137 break;
2138 }
aa7133@att.combe92d712020-03-18 17:30:39 +02002139 case RIC_ERROR_INDICATION: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002140 if (loglevel >= MDCLOG_DEBUG) {
2141 mdclog_write(MDCLOG_DEBUG, "RIC_ERROR_INDICATION");
2142 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002143 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_ErrorIndication]->Increment();
2144 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_ErrorIndication]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002145 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
2146 mdclog_write(MDCLOG_ERR, "Failed to send RIC_ERROR_INDICATION");
ss412g1a79bdf2019-10-24 12:03:05 +03002147 return -6;
2148 }
2149 break;
2150 }
2151 case RIC_SUB_REQ: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002152 if (loglevel >= MDCLOG_DEBUG) {
2153 mdclog_write(MDCLOG_DEBUG, "RIC_SUB_REQ");
2154 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002155 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscription]->Increment();
2156 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscription]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002157 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002158 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SUB_REQ");
ss412g1a79bdf2019-10-24 12:03:05 +03002159 return -6;
2160 }
2161 break;
2162 }
2163 case RIC_SUB_DEL_REQ: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002164 if (loglevel >= MDCLOG_DEBUG) {
2165 mdclog_write(MDCLOG_DEBUG, "RIC_SUB_DEL_REQ");
2166 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002167 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment();
2168 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICsubscriptionDelete]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002169 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002170 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SUB_DEL_REQ");
ss412g1a79bdf2019-10-24 12:03:05 +03002171 return -6;
2172 }
2173 break;
2174 }
2175 case RIC_CONTROL_REQ: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002176 if (loglevel >= MDCLOG_DEBUG) {
2177 mdclog_write(MDCLOG_DEBUG, "RIC_CONTROL_REQ");
2178 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002179 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICcontrol]->Increment();
2180 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICcontrol]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002181 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002182 mdclog_write(MDCLOG_ERR, "Failed to send RIC_CONTROL_REQ");
ss412g1a79bdf2019-10-24 12:03:05 +03002183 return -6;
2184 }
2185 break;
2186 }
2187 case RIC_SERVICE_QUERY: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002188 if (loglevel >= MDCLOG_DEBUG) {
2189 mdclog_write(MDCLOG_DEBUG, "RIC_SERVICE_QUERY");
2190 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002191 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2192 break;
2193 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002194 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_RICserviceQuery]->Increment();
2195 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_RICserviceQuery]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002196 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002197 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_QUERY");
ss412g1a79bdf2019-10-24 12:03:05 +03002198 return -6;
2199 }
2200 break;
2201 }
2202 case RIC_SERVICE_UPDATE_ACK: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002203 if (loglevel >= MDCLOG_DEBUG) {
2204 mdclog_write(MDCLOG_DEBUG, "RIC_SERVICE_UPDATE_ACK");
2205 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002206 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
aa7133@att.com54885fe2020-10-29 17:24:49 +03002207 mdclog_write(MDCLOG_ERR, "error in PER_FromXML");
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002208 break;
2209 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002210 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
aa7133@att.com52771912020-10-29 17:45:01 +03002211 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.com54885fe2020-10-29 17:24:49 +03002212 if (loglevel >= MDCLOG_DEBUG) {
2213 mdclog_write(MDCLOG_DEBUG, "Before sending to CU");
2214 }
aa7133@att.combe92d712020-03-18 17:30:39 +02002215 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002216 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_UPDATE_ACK");
ss412g1a79bdf2019-10-24 12:03:05 +03002217 return -6;
2218 }
2219 break;
2220 }
2221 case RIC_SERVICE_UPDATE_FAILURE: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002222 if (loglevel >= MDCLOG_DEBUG) {
2223 mdclog_write(MDCLOG_DEBUG, "RIC_SERVICE_UPDATE_FAILURE");
2224 }
aa7133@att.com0793fcb2020-04-16 11:34:26 +03002225 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2226 break;
2227 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002228 message.peerInfo->counters[OUT_UN_SUCC][MSG_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment();
aa7133@att.com7c07fee2020-10-29 17:54:38 +03002229 message.peerInfo->counters[OUT_UN_SUCC][BYTES_COUNTER][ProcedureCode_id_RICserviceUpdate]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002230 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002231 mdclog_write(MDCLOG_ERR, "Failed to send RIC_SERVICE_UPDATE_FAILURE");
ss412g1a79bdf2019-10-24 12:03:05 +03002232 return -6;
2233 }
2234 break;
2235 }
ss412g4aeeda22020-06-16 14:12:41 +03002236 case RIC_E2_RESET_REQ: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002237 if (loglevel >= MDCLOG_DEBUG) {
2238 mdclog_write(MDCLOG_DEBUG, "RIC_E2_RESET_REQ");
2239 }
ss412g4aeeda22020-06-16 14:12:41 +03002240 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2241 break;
2242 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002243 message.peerInfo->counters[OUT_INITI][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2244 message.peerInfo->counters[OUT_INITI][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002245 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g4aeeda22020-06-16 14:12:41 +03002246 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_RESET");
ss412g1a79bdf2019-10-24 12:03:05 +03002247 return -6;
2248 }
2249 break;
2250 }
ss412g4aeeda22020-06-16 14:12:41 +03002251 case RIC_E2_RESET_RESP: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002252 if (loglevel >= MDCLOG_DEBUG) {
2253 mdclog_write(MDCLOG_DEBUG, "RIC_E2_RESET_RESP");
2254 }
ss412g4aeeda22020-06-16 14:12:41 +03002255 if (PER_FromXML(message, rmrMessageBuffer) != 0) {
2256 break;
2257 }
aa7133@att.coma4c80012020-06-24 16:36:47 +03002258 message.peerInfo->counters[OUT_SUCC][MSG_COUNTER][ProcedureCode_id_Reset]->Increment();
2259 message.peerInfo->counters[OUT_SUCC][BYTES_COUNTER][ProcedureCode_id_Reset]->Increment(rmrMessageBuffer.rcvMessage->len);
aa7133@att.combe92d712020-03-18 17:30:39 +02002260 if (sendDirectionalSctpMsg(rmrMessageBuffer, message, 0, sctpMap) != 0) {
ss412g4aeeda22020-06-16 14:12:41 +03002261 mdclog_write(MDCLOG_ERR, "Failed to send RIC_E2_RESET_RESP");
ss412g1a79bdf2019-10-24 12:03:05 +03002262 return -6;
2263 }
2264 break;
2265 }
2266 case RIC_SCTP_CLEAR_ALL: {
2267 mdclog_write(MDCLOG_INFO, "RIC_SCTP_CLEAR_ALL");
2268 // loop on all keys and close socket and then erase all map.
2269 vector<char *> v;
2270 sctpMap->getKeys(v);
2271 for (auto const &iter : v) { //}; iter != sctpMap.end(); iter++) {
2272 if (!boost::starts_with((string) (iter), "host:") && !boost::starts_with((string) (iter), "msg:")) {
2273 auto *peerInfo = (ConnectedCU_t *) sctpMap->find(iter);
2274 if (peerInfo == nullptr) {
2275 continue;
2276 }
2277 close(peerInfo->fileDescriptor);
2278 memcpy(message.message.enodbName, peerInfo->enodbName, sizeof(peerInfo->enodbName));
2279 message.message.direction = 'D';
2280 message.message.time.tv_nsec = ts.tv_nsec;
2281 message.message.time.tv_sec = ts.tv_sec;
2282
2283 message.message.asnLength = rmrMessageBuffer.sendMessage->len =
2284 snprintf((char *)rmrMessageBuffer.sendMessage->payload,
aa7133@att.com96c9b882020-03-25 14:50:40 +02002285 256,
2286 "%s|RIC_SCTP_CLEAR_ALL",
2287 peerInfo->enodbName);
ss412g1a79bdf2019-10-24 12:03:05 +03002288 message.message.asndata = rmrMessageBuffer.sendMessage->payload;
2289 mdclog_write(MDCLOG_INFO, "%s", message.message.asndata);
aa7133@att.combe92d712020-03-18 17:30:39 +02002290 if (sendRequestToXapp(message, RIC_SCTP_CONNECTION_FAILURE, rmrMessageBuffer) != 0) {
ss412g1a79bdf2019-10-24 12:03:05 +03002291 mdclog_write(MDCLOG_ERR, "SCTP_CONNECTION_FAIL message failed to send to xAPP");
2292 }
2293 free(peerInfo);
2294 }
2295 }
2296
2297 sleep(1);
2298 sctpMap->clear();
2299 break;
2300 }
ss412g3bac2da2020-01-05 11:52:19 +02002301 case E2_TERM_KEEP_ALIVE_REQ: {
2302 // send message back
ss412g3bac2da2020-01-05 11:52:19 +02002303 rmr_bytes2payload(rmrMessageBuffer.sendMessage,
aa7133@att.com96c9b882020-03-25 14:50:40 +02002304 (unsigned char *)rmrMessageBuffer.ka_message,
2305 rmrMessageBuffer.ka_message_len);
ss412g3bac2da2020-01-05 11:52:19 +02002306 rmrMessageBuffer.sendMessage->mtype = E2_TERM_KEEP_ALIVE_RESP;
2307 rmrMessageBuffer.sendMessage->state = 0;
2308 static unsigned char tx[32];
2309 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
2310 rmr_bytes2xact(rmrMessageBuffer.sendMessage, tx, txLen);
2311 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2312 if (rmrMessageBuffer.sendMessage == nullptr) {
2313 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
aa7133@att.com77fff432020-01-27 12:01:20 +02002314 mdclog_write(MDCLOG_ERR, "Failed to send E2_TERM_KEEP_ALIVE_RESP RMR message returned NULL");
ss412g3bac2da2020-01-05 11:52:19 +02002315 } else if (rmrMessageBuffer.sendMessage->state != 0) {
aa7133@att.com77fff432020-01-27 12:01:20 +02002316 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 +02002317 rmrMessageBuffer.sendMessage->state, translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str());
aa7133@att.comf13529a2020-08-27 11:21:16 +03002318 } else if (loglevel >= MDCLOG_DEBUG) {
aa7133@att.combc110772020-03-25 09:45:14 +02002319 mdclog_write(MDCLOG_DEBUG, "Got Keep Alive Request send : %s", rmrMessageBuffer.ka_message);
ss412g3bac2da2020-01-05 11:52:19 +02002320 }
aa7133@att.com342843e2020-01-26 12:53:34 +02002321
ss412g3bac2da2020-01-05 11:52:19 +02002322 break;
2323 }
ss412g4aeeda22020-06-16 14:12:41 +03002324 case RIC_HEALTH_CHECK_REQ: {
aa7133@att.comf13529a2020-08-27 11:21:16 +03002325 static int counter = 0;
ss412g4aeeda22020-06-16 14:12:41 +03002326 // send message back
aa7133@att.comc18fe532020-06-30 12:40:18 +03002327 rmr_bytes2payload(rmrMessageBuffer.rcvMessage,
ss412g4aeeda22020-06-16 14:12:41 +03002328 (unsigned char *)"OK",
2329 2);
aa7133@att.comc18fe532020-06-30 12:40:18 +03002330 rmrMessageBuffer.rcvMessage->mtype = RIC_HEALTH_CHECK_RESP;
2331 rmrMessageBuffer.rcvMessage->state = 0;
ss412g4aeeda22020-06-16 14:12:41 +03002332 static unsigned char tx[32];
2333 auto txLen = snprintf((char *) tx, sizeof tx, "%15ld", transactionCounter++);
aa7133@att.comc18fe532020-06-30 12:40:18 +03002334 rmr_bytes2xact(rmrMessageBuffer.rcvMessage, tx, txLen);
2335 rmrMessageBuffer.rcvMessage = rmr_rts_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.rcvMessage);
2336 //rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2337 if (rmrMessageBuffer.rcvMessage == nullptr) {
2338 rmrMessageBuffer.rcvMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
ss412g4aeeda22020-06-16 14:12:41 +03002339 mdclog_write(MDCLOG_ERR, "Failed to send RIC_HEALTH_CHECK_RESP RMR message returned NULL");
aa7133@att.comc18fe532020-06-30 12:40:18 +03002340 } else if (rmrMessageBuffer.rcvMessage->state != 0) {
ss412g4aeeda22020-06-16 14:12:41 +03002341 mdclog_write(MDCLOG_ERR, "Failed to send RIC_HEALTH_CHECK_RESP, on RMR state = %d ( %s)",
aa7133@att.comc18fe532020-06-30 12:40:18 +03002342 rmrMessageBuffer.rcvMessage->state, translateRmrErrorMessages(rmrMessageBuffer.rcvMessage->state).c_str());
aa7133@att.comf13529a2020-08-27 11:21:16 +03002343 } else if (loglevel >= MDCLOG_DEBUG && ++counter % 100 == 0) {
2344 mdclog_write(MDCLOG_DEBUG, "Got %d RIC_HEALTH_CHECK_REQ Request send : OK", counter);
ss412g4aeeda22020-06-16 14:12:41 +03002345 }
2346
2347 break;
2348 }
2349
ss412g1a79bdf2019-10-24 12:03:05 +03002350 default:
aa7133@att.com409f2232020-10-29 13:06:45 +03002351 mdclog_write(MDCLOG_WARN, "Message Type : %d is not supported", rmrMessageBuffer.rcvMessage->mtype);
ss412g1a79bdf2019-10-24 12:03:05 +03002352 message.message.asndata = rmrMessageBuffer.rcvMessage->payload;
2353 message.message.asnLength = rmrMessageBuffer.rcvMessage->len;
2354 message.message.time.tv_nsec = ts.tv_nsec;
2355 message.message.time.tv_sec = ts.tv_sec;
2356 message.message.messageType = rmrMessageBuffer.rcvMessage->mtype;
2357
2358 buildJsonMessage(message);
2359
2360
ss412g1a79bdf2019-10-24 12:03:05 +03002361 return -7;
2362 }
2363 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2364 mdclog_write(MDCLOG_DEBUG, "EXIT OK from %s", __FUNCTION__);
2365 }
ss412g1a79bdf2019-10-24 12:03:05 +03002366 return 0;
2367}
2368
2369/**
2370 * Send message to the CU that is not expecting for successful or unsuccessful results
2371 * @param messageBuffer
2372 * @param message
2373 * @param failedMsgId
2374 * @param sctpMap
ss412g1a79bdf2019-10-24 12:03:05 +03002375 * @return
2376 */
2377int sendDirectionalSctpMsg(RmrMessagesBuffer_t &messageBuffer,
2378 ReportingMessages_t &message,
2379 int failedMsgId,
aa7133@att.combe92d712020-03-18 17:30:39 +02002380 Sctp_Map_t *sctpMap) {
aa7133@att.com409f2232020-10-29 13:06:45 +03002381 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2382 mdclog_write(MDCLOG_DEBUG, "send message: %d to %s address", message.message.messageType, message.message.enodbName);
2383 }
ss412g1a79bdf2019-10-24 12:03:05 +03002384
aa7133@att.combe92d712020-03-18 17:30:39 +02002385 getRequestMetaData(message, messageBuffer);
ss412g1a79bdf2019-10-24 12:03:05 +03002386 if (mdclog_level_get() >= MDCLOG_INFO) {
2387 mdclog_write(MDCLOG_INFO, "send message to %s address", message.message.enodbName);
2388 }
2389
aa7133@att.combe92d712020-03-18 17:30:39 +02002390 auto rc = sendMessagetoCu(sctpMap, messageBuffer, message, failedMsgId);
ss412g1a79bdf2019-10-24 12:03:05 +03002391 return rc;
2392}
2393
2394/**
2395 *
2396 * @param sctpMap
2397 * @param messageBuffer
2398 * @param message
2399 * @param failedMesgId
ss412g1a79bdf2019-10-24 12:03:05 +03002400 * @return
2401 */
2402int sendMessagetoCu(Sctp_Map_t *sctpMap,
2403 RmrMessagesBuffer_t &messageBuffer,
2404 ReportingMessages_t &message,
aa7133@att.combe92d712020-03-18 17:30:39 +02002405 int failedMesgId) {
ss412g1a79bdf2019-10-24 12:03:05 +03002406 // get the FD
2407 message.message.messageType = messageBuffer.rcvMessage->mtype;
ss412g4aeeda22020-06-16 14:12:41 +03002408 auto rc = sendSctpMsg(message.peerInfo, message, sctpMap);
ss412g1a79bdf2019-10-24 12:03:05 +03002409 return rc;
2410}
2411
ss412g1a79bdf2019-10-24 12:03:05 +03002412
2413/**
2414 *
2415 * @param epoll_fd
2416 * @param peerInfo
2417 * @param events
2418 * @param sctpMap
2419 * @param enodbName
2420 * @param msgType
ss412g1a79bdf2019-10-24 12:03:05 +03002421 * @return
2422 */
2423int addToEpoll(int epoll_fd,
2424 ConnectedCU_t *peerInfo,
2425 uint32_t events,
2426 Sctp_Map_t *sctpMap,
2427 char *enodbName,
aa7133@att.combe92d712020-03-18 17:30:39 +02002428 int msgType) {
ss412g1a79bdf2019-10-24 12:03:05 +03002429 // Add to Epol
2430 struct epoll_event event{};
2431 event.data.ptr = peerInfo;
2432 event.events = events;
2433 if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, peerInfo->fileDescriptor, &event) < 0) {
2434 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com409f2232020-10-29 13:06:45 +03002435 mdclog_write(MDCLOG_DEBUG, "epoll_ctl EPOLL_CTL_ADD (may check not to quit here), %s, %s %d",
ss412g1a79bdf2019-10-24 12:03:05 +03002436 strerror(errno), __func__, __LINE__);
2437 }
2438 close(peerInfo->fileDescriptor);
aa7133@att.com84bd3342020-03-16 18:04:57 +02002439 if (enodbName != nullptr) {
aa7133@att.combe92d712020-03-18 17:30:39 +02002440 cleanHashEntry(peerInfo, sctpMap);
aa7133@att.com84bd3342020-03-16 18:04:57 +02002441 char key[MAX_ENODB_NAME_SIZE * 2];
2442 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", enodbName, msgType);
2443 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2444 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
2445 }
2446 auto tmp = sctpMap->find(key);
2447 if (tmp) {
2448 free(tmp);
2449 sctpMap->erase(key);
2450 }
2451 } else {
2452 peerInfo->enodbName[0] = 0;
ss412g1a79bdf2019-10-24 12:03:05 +03002453 }
aa7133@att.com409f2232020-10-29 13:06:45 +03002454 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may check not to quit here)");
ss412g1a79bdf2019-10-24 12:03:05 +03002455 return -1;
2456 }
ss412g1a79bdf2019-10-24 12:03:05 +03002457 return 0;
2458}
2459
2460/**
2461 *
2462 * @param epoll_fd
2463 * @param peerInfo
2464 * @param events
2465 * @param sctpMap
2466 * @param enodbName
2467 * @param msgType
ss412g1a79bdf2019-10-24 12:03:05 +03002468 * @return
2469 */
2470int modifyToEpoll(int epoll_fd,
2471 ConnectedCU_t *peerInfo,
2472 uint32_t events,
2473 Sctp_Map_t *sctpMap,
2474 char *enodbName,
aa7133@att.combe92d712020-03-18 17:30:39 +02002475 int msgType) {
ss412g1a79bdf2019-10-24 12:03:05 +03002476 // Add to Epol
2477 struct epoll_event event{};
2478 event.data.ptr = peerInfo;
2479 event.events = events;
2480 if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, peerInfo->fileDescriptor, &event) < 0) {
2481 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.com409f2232020-10-29 13:06:45 +03002482 mdclog_write(MDCLOG_DEBUG, "epoll_ctl EPOLL_CTL_MOD (may check not to quit here), %s, %s %d",
ss412g1a79bdf2019-10-24 12:03:05 +03002483 strerror(errno), __func__, __LINE__);
2484 }
2485 close(peerInfo->fileDescriptor);
aa7133@att.combe92d712020-03-18 17:30:39 +02002486 cleanHashEntry(peerInfo, sctpMap);
ss412g1a79bdf2019-10-24 12:03:05 +03002487 char key[MAX_ENODB_NAME_SIZE * 2];
2488 snprintf(key, MAX_ENODB_NAME_SIZE * 2, "msg:%s|%d", enodbName, msgType);
2489 if (mdclog_level_get() >= MDCLOG_DEBUG) {
2490 mdclog_write(MDCLOG_DEBUG, "remove key = %s from %s at line %d", key, __FUNCTION__, __LINE__);
2491 }
aa7133@att.com96c9b882020-03-25 14:50:40 +02002492 auto tmp = sctpMap->find(key);
ss412g1a79bdf2019-10-24 12:03:05 +03002493 if (tmp) {
2494 free(tmp);
2495 }
2496 sctpMap->erase(key);
aa7133@att.com409f2232020-10-29 13:06:45 +03002497 mdclog_write(MDCLOG_ERR, "epoll_ctl EPOLL_CTL_ADD (may check not to quit here)");
ss412g1a79bdf2019-10-24 12:03:05 +03002498 return -1;
2499 }
ss412g1a79bdf2019-10-24 12:03:05 +03002500 return 0;
2501}
2502
2503
aa7133@att.combe92d712020-03-18 17:30:39 +02002504int sendRmrMessage(RmrMessagesBuffer_t &rmrMessageBuffer, ReportingMessages_t &message) {
ss412g1a79bdf2019-10-24 12:03:05 +03002505 buildJsonMessage(message);
2506
2507 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2508
2509 if (rmrMessageBuffer.sendMessage == nullptr) {
2510 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
2511 mdclog_write(MDCLOG_ERR, "RMR failed send message returned with NULL pointer");
ss412g1a79bdf2019-10-24 12:03:05 +03002512 return -1;
2513 }
2514
2515 if (rmrMessageBuffer.sendMessage->state != 0) {
2516 char meid[RMR_MAX_MEID]{};
2517 if (rmrMessageBuffer.sendMessage->state == RMR_ERR_RETRY) {
2518 usleep(5);
2519 rmrMessageBuffer.sendMessage->state = 0;
2520 mdclog_write(MDCLOG_INFO, "RETRY sending Message type %d to Xapp from %s",
2521 rmrMessageBuffer.sendMessage->mtype,
2522 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
2523 rmrMessageBuffer.sendMessage = rmr_send_msg(rmrMessageBuffer.rmrCtx, rmrMessageBuffer.sendMessage);
2524 if (rmrMessageBuffer.sendMessage == nullptr) {
2525 mdclog_write(MDCLOG_ERR, "RMR failed send message returned with NULL pointer");
2526 rmrMessageBuffer.sendMessage = rmr_alloc_msg(rmrMessageBuffer.rmrCtx, RECEIVE_XAPP_BUFFER_SIZE);
ss412g1a79bdf2019-10-24 12:03:05 +03002527 return -1;
2528 } else if (rmrMessageBuffer.sendMessage->state != 0) {
2529 mdclog_write(MDCLOG_ERR,
2530 "Message state %s while sending request %d to Xapp from %s after retry of 10 microseconds",
2531 translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str(),
2532 rmrMessageBuffer.sendMessage->mtype,
2533 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
2534 auto rc = rmrMessageBuffer.sendMessage->state;
ss412g1a79bdf2019-10-24 12:03:05 +03002535 return rc;
2536 }
2537 } else {
2538 mdclog_write(MDCLOG_ERR, "Message state %s while sending request %d to Xapp from %s",
2539 translateRmrErrorMessages(rmrMessageBuffer.sendMessage->state).c_str(),
2540 rmrMessageBuffer.sendMessage->mtype,
2541 rmr_get_meid(rmrMessageBuffer.sendMessage, (unsigned char *)meid));
ss412g1a79bdf2019-10-24 12:03:05 +03002542 return rmrMessageBuffer.sendMessage->state;
2543 }
2544 }
2545 return 0;
2546}
2547
2548void buildJsonMessage(ReportingMessages_t &message) {
ss412g3bac2da2020-01-05 11:52:19 +02002549 if (jsonTrace) {
2550 message.outLen = sizeof(message.base64Data);
2551 base64::encode((const unsigned char *) message.message.asndata,
2552 (const int) message.message.asnLength,
2553 message.base64Data,
2554 message.outLen);
2555 if (mdclog_level_get() >= MDCLOG_DEBUG) {
aa7133@att.comc5a15202020-04-05 19:57:01 +03002556 mdclog_write(MDCLOG_DEBUG, "Tracing: ASN length = %d, base64 message length = %d ",
ss412g3bac2da2020-01-05 11:52:19 +02002557 (int) message.message.asnLength,
2558 (int) message.outLen);
2559 }
2560
2561 snprintf(message.buffer, sizeof(message.buffer),
aa7133@att.com96c9b882020-03-25 14:50:40 +02002562 "{\"header\": {\"ts\": \"%ld.%09ld\","
2563 "\"ranName\": \"%s\","
2564 "\"messageType\": %d,"
2565 "\"direction\": \"%c\"},"
2566 "\"base64Length\": %d,"
2567 "\"asnBase64\": \"%s\"}",
2568 message.message.time.tv_sec,
2569 message.message.time.tv_nsec,
2570 message.message.enodbName,
2571 message.message.messageType,
2572 message.message.direction,
2573 (int) message.outLen,
2574 message.base64Data);
ss412g3bac2da2020-01-05 11:52:19 +02002575 static src::logger_mt &lg = my_logger::get();
2576
2577 BOOST_LOG(lg) << message.buffer;
ss412g1a79bdf2019-10-24 12:03:05 +03002578 }
ss412g1a79bdf2019-10-24 12:03:05 +03002579}
2580
2581
2582/**
2583 * take RMR error code to string
2584 * @param state
2585 * @return
2586 */
2587string translateRmrErrorMessages(int state) {
2588 string str = {};
2589 switch (state) {
2590 case RMR_OK:
2591 str = "RMR_OK - state is good";
2592 break;
2593 case RMR_ERR_BADARG:
aa7133@att.com409f2232020-10-29 13:06:45 +03002594 str = "RMR_ERR_BADARG - argument passed to function was unusable";
ss412g1a79bdf2019-10-24 12:03:05 +03002595 break;
2596 case RMR_ERR_NOENDPT:
2597 str = "RMR_ERR_NOENDPT - send//call could not find an endpoint based on msg type";
2598 break;
2599 case RMR_ERR_EMPTY:
2600 str = "RMR_ERR_EMPTY - msg received had no payload; attempt to send an empty message";
2601 break;
2602 case RMR_ERR_NOHDR:
2603 str = "RMR_ERR_NOHDR - message didn't contain a valid header";
2604 break;
2605 case RMR_ERR_SENDFAILED:
2606 str = "RMR_ERR_SENDFAILED - send failed; errno has nano reason";
2607 break;
2608 case RMR_ERR_CALLFAILED:
2609 str = "RMR_ERR_CALLFAILED - unable to send call() message";
2610 break;
2611 case RMR_ERR_NOWHOPEN:
2612 str = "RMR_ERR_NOWHOPEN - no wormholes are open";
2613 break;
2614 case RMR_ERR_WHID:
2615 str = "RMR_ERR_WHID - wormhole id was invalid";
2616 break;
2617 case RMR_ERR_OVERFLOW:
2618 str = "RMR_ERR_OVERFLOW - operation would have busted through a buffer/field size";
2619 break;
2620 case RMR_ERR_RETRY:
2621 str = "RMR_ERR_RETRY - request (send/call/rts) failed, but caller should retry (EAGAIN for wrappers)";
2622 break;
2623 case RMR_ERR_RCVFAILED:
2624 str = "RMR_ERR_RCVFAILED - receive failed (hard error)";
2625 break;
2626 case RMR_ERR_TIMEOUT:
2627 str = "RMR_ERR_TIMEOUT - message processing call timed out";
2628 break;
2629 case RMR_ERR_UNSET:
2630 str = "RMR_ERR_UNSET - the message hasn't been populated with a transport buffer";
2631 break;
2632 case RMR_ERR_TRUNC:
2633 str = "RMR_ERR_TRUNC - received message likely truncated";
2634 break;
2635 case RMR_ERR_INITFAILED:
2636 str = "RMR_ERR_INITFAILED - initialisation of something (probably message) failed";
2637 break;
2638 case RMR_ERR_NOTSUPP:
2639 str = "RMR_ERR_NOTSUPP - the request is not supported, or RMr was not initialised for the request";
2640 break;
2641 default:
2642 char buf[128]{};
2643 snprintf(buf, sizeof buf, "UNDOCUMENTED RMR_ERR : %d", state);
2644 str = buf;
2645 break;
2646 }
2647 return str;
2648}
2649
2650