Sync from Azure to LF
[ric-plt/e2.git] / RIC-E2-TERMINATION / TEST / T1 / Test1.cpp
1 /*
2  * Copyright 2019 AT&T Intellectual Property
3  * Copyright 2019 Nokia
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 //
19 // Created by adi on 6/11/19.
20 //
21
22 #include <mdclog/mdclog.h>
23
24 #include "asn/type_defs.h"
25 #include "asn/per/codec.hpp"
26 #include "asn/printer.hpp"
27
28 #include "X2AP-CommonDataTypes.hpp"
29 #include "X2AP-Containers.hpp"
30 #include "X2AP-Constants.hpp"
31 #include "X2AP-IEs.hpp"
32 #include "X2AP-PDU-Contents.hpp"
33
34 #include "E2AP-Constants.hpp"
35 #include "E2AP-IEs.hpp"
36 #include "E2AP-PDU-Contents.hpp"
37 #include "E2AP-PDU-Descriptions.hpp"
38
39 #include <iostream>
40 #include <cstdio>
41 #include <cctype>
42 #include <cstring>
43
44 #include <pthread.h>
45 #include <rmr/rmr.h>
46 #include <rmr/RIC_message_types.h>
47
48 #include "logInit.h"
49
50 // test X2SetUP request and response
51 using namespace std;
52
53 #define MAXEVENTS 64
54
55 int main(const int argc, char **argv) {
56     mdclog_severity_t loglevel = MDCLOG_INFO;
57
58     auto buff = new string("SETUP TEST");
59     init_log((char *)buff->c_str());
60
61     mdclog_level_set(loglevel);
62
63     if (argc < 9){
64         mdclog_mdc_add("app", argv[0]);
65         mdclog_write(MDCLOG_ERR, "Usage host <host address> port <sctpPort> ran <ran name> rmr <rmr address> [logLevel <debug/warning/info/error]");
66         return -1 ;
67     }
68
69     char host[256] {0};
70     char port [10] {0};
71     char ranName[256] {0};
72     char rmrAddress[256] {0};
73
74     char str1[128];
75     for (int i = 1; i < argc; i += 2) {
76         for (int j = 0; j < strlen(argv[i]); j++) {
77             str1[j] = (char)tolower(argv[i][j]);
78         }
79         str1[strlen(argv[i])] = 0;
80         if (strcmp("host", str1) == 0) {
81             strcpy(host, argv[i + 1]);
82         } else if (strcmp("port", str1) == 0) {
83             strcpy(port, argv[i + 1]);
84         } else if (strcmp("ran", str1) == 0) {
85             strcpy(ranName, argv[i + 1]);
86         } else if (strcmp("rmr", str1) == 0) {
87             strcpy(ranName, argv[i + 1]);
88         }else if (strcmp("loglevel", str1) == 0) {
89             if (strcmp("debug", argv[i + 1]) == 0) {
90                 loglevel = MDCLOG_DEBUG;
91             } else if (strcmp("info", argv[i + 1]) == 0) {
92                 loglevel = MDCLOG_INFO;
93             } else if (strcmp("warning", argv[i + 1]) == 0) {
94                 loglevel = MDCLOG_WARN;
95             } else if (strcmp("error", argv[i + 1]) == 0) {
96                 loglevel = MDCLOG_ERR;
97             }
98         }
99     }
100
101     void *rmrCtx = rmr_init(rmrAddress, RMR_MAX_RCV_BYTES, RMRFL_NONE);
102     if (rmrCtx == nullptr ) {
103         mdclog_write(MDCLOG_ERR, "RMR failed to initialise : %s", strerror(errno));
104         return(-1);
105     }
106
107     // get the RMR fd for the epoll
108     auto rmrListenFd = rmr_get_rcvfd(rmrCtx);
109
110     auto epoll_fd = epoll_create1(0);
111     if (epoll_fd == -1) {
112         mdclog_write(MDCLOG_ERR,"failed to open epoll descriptor");
113         rmr_close(rmrCtx);
114         return -2;
115     }
116
117     struct epoll_event event {};
118     event.events = EPOLLIN;
119     event.data.fd = rmrListenFd;
120     // add listening sctpPort to epoll
121     if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, rmrListenFd, &event)) {
122         mdclog_write(MDCLOG_ERR, "Failed to add RMR descriptor to epoll");
123         close(rmrListenFd);
124         rmr_close(rmrCtx);
125         return -3;
126     }
127
128
129     // we need to find that routing table exist and we can run
130     if (mdclog_level_get() >= MDCLOG_INFO) {
131         mdclog_write(MDCLOG_INFO, "We are after RMR INIT wait for RMR_Ready");
132     }
133
134     int rmrReady = 0;
135     int count = 0;
136     while (!rmrReady) {
137         if ((rmrReady = rmr_ready(rmrCtx)) == 0) {
138             sleep(1);
139         }
140         count++;
141         if (count % 60 == 0) {
142             mdclog_write(MDCLOG_INFO, "waiting to RMR ready state for %d seconds", count);
143         }
144         if (count > 180) {
145             mdclog_write(MDCLOG_ERR, "RMR not ready tried for 3 minutes ");
146             return(-2);
147         }
148     }
149     if (mdclog_level_get() >= MDCLOG_INFO) {
150         mdclog_write(MDCLOG_INFO, "RMR running");
151     }
152
153     E2AP_PDU pdu {};
154     auto &initiatingMsg = pdu.select_initiatingMessage();
155     initiatingMsg.ref_procedureCode().select_id_x2Setup();
156     initiatingMsg.ref_criticality().select_id_x2Setup();
157     auto &x2setup = initiatingMsg.ref_value().select_id_x2Setup();
158
159     auto &ies = x2setup.ref_protocolIEs();
160
161
162     X2SetupRequest::protocolIEs_t::value_type val {};
163     val.ref_id().select_id_GlobalENB_ID();
164     val.ref_criticality().select_id_GlobalENB_ID();
165     uint8_t v1[] = {0x02, 0xf8, 0x29};
166     val.ref_value().select_id_GlobalENB_ID().ref_pLMN_Identity().set(3, v1);
167     uint32_t eNBId = 5;
168     val.ref_value().select_id_GlobalENB_ID().ref_eNB_ID().select_macro_eNB_ID().set_buffer(20,reinterpret_cast<uint8_t*>(&eNBId));
169
170     ies.push_back(val);
171
172     X2SetupRequest::protocolIEs_t::value_type sc {};
173     ies.push_back(sc);
174
175     sc.ref_id().select_id_ServedCells();
176     sc.ref_criticality().select_id_ServedCells();
177
178     ServedCells::value_type sce;
179     sc.ref_value().select_id_ServedCells().push_back(sce);
180
181     sce.ref_servedCellInfo().ref_pCI().set(0x1F7);
182     uint8_t v3[] = {0x1, 0x2};
183     sce.ref_servedCellInfo().ref_tAC().set(2,v3);
184     sce.ref_servedCellInfo().ref_cellId().ref_pLMN_Identity().set(3, v1);
185     uint8_t v4[] = {0x00, 0x07, 0xab, ((unsigned)0x50) >> (unsigned)4};
186     sce.ref_servedCellInfo().ref_cellId().ref_eUTRANcellIdentifier().set_buffer(28, v4);
187
188     BroadcastPLMNs_Item::value_type bpe;
189     sce.ref_servedCellInfo().ref_broadcastPLMNs().push_back(bpe);
190     bpe.set(3, v1);
191
192     sce.ref_servedCellInfo().ref_eUTRA_Mode_Info().select_fDD().ref_uL_EARFCN().set(0x1);
193     sce.ref_servedCellInfo().ref_eUTRA_Mode_Info().select_fDD().ref_dL_EARFCN().set(0x1);
194     sce.ref_servedCellInfo().ref_eUTRA_Mode_Info().select_fDD().ref_uL_Transmission_Bandwidth().set(Transmission_Bandwidth::bw50);
195     sce.ref_servedCellInfo().ref_eUTRA_Mode_Info().select_fDD().ref_dL_Transmission_Bandwidth().set(Transmission_Bandwidth::bw50);
196
197
198     unsigned char s_buffer[4096];
199     asn::per::EncoderCtx ctx{s_buffer, sizeof(s_buffer)};
200     std::cout << asn::get_printed(pdu) << std::endl;
201
202     if (!asn::per::pack(pdu, ctx)) {
203         std::cout << ctx.refErrorCtx().toString() << std::endl;
204         return -3;
205     }
206     size_t packed_buf_size;
207     packed_buf_size = static_cast<size_t>(ctx.refBuffer().getBytesUsed());
208
209     // build message
210     char data[4096] {};
211     //auto delimiter = (const char) '|';
212     sprintf(data, "%s|%s|%s|%d|%s/0", host, port, ranName, (int)packed_buf_size, ctx.refBuffer().getBytes(packed_buf_size));
213
214     rmr_mbuf_t *msg = rmr_alloc_msg(rmrCtx, int(strlen(data)));
215     rmr_bytes2meid(msg, (unsigned char const*)ranName, strlen(ranName));
216     rmr_bytes2payload(msg, (unsigned char const*)data, strlen(data));
217     rmr_bytes2xact(msg, (unsigned char const*)ranName, strlen(ranName));
218     msg->mtype = RIC_X2_SETUP_REQ;
219     msg->state = 0;
220
221     msg = rmr_send_msg(rmrCtx, msg);
222     if (msg->state != 0) {
223         mdclog_write(MDCLOG_ERR, "Message state %d while sending RIC_X2_SETUP to %s", msg->state, ranName);
224         rmr_free_msg(msg);
225         rmr_close(rmrCtx);
226         return -4;
227     }
228     rmr_free_msg(msg);
229
230
231     unsigned char allocBuffer[64*1024] {0};
232     auto *events = (struct epoll_event *)calloc(MAXEVENTS, sizeof(event));
233
234     while (true) {
235
236         auto numOfEvents = epoll_wait(epoll_fd, events, MAXEVENTS, -1);
237         if (numOfEvents < 0) {
238             mdclog_write(MDCLOG_ERR, "Epoll wait failed, errno = %s", strerror(errno));
239             rmr_close(rmrCtx);
240             return -4;
241         }
242         for (auto i = 0; i < numOfEvents; i++) {
243             if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN))) {
244                 mdclog_write(MDCLOG_ERR, "epoll error");
245             } else if (rmrListenFd == events[i].data.fd) {
246                 msg = rmr_alloc_msg(rmrCtx, 4096);
247                 if (msg == nullptr) {
248                     mdclog_write(MDCLOG_ERR, "RMR Allocation message, %s", strerror(errno));
249                     rmr_close(rmrCtx);
250                     return -5;
251                 }
252
253                 msg = rmr_rcv_msg(rmrCtx, msg);
254                 if (msg == nullptr) {
255                     mdclog_write(MDCLOG_ERR, "RMR Receving message, %s", strerror(errno));
256                     rmr_close(rmrCtx);
257                     return -6;
258                 }
259                 memset(allocBuffer, 0, 64*1024);
260                 switch (msg->mtype) {
261                     case RIC_X2_SETUP_RESP: {
262                         mdclog_write(MDCLOG_INFO, "successful, RMR receiveing RIC_X2_SETUP_RESP");
263                         asn::per::DecoderCtx dCtx{msg->payload, (size_t) msg->len, allocBuffer, sizeof(allocBuffer)};
264                         E2AP_PDU opdu;
265                         if (!asn::per::unpack(opdu, dCtx)) {
266                             mdclog_write(MDCLOG_ERR, "Failed to unpack ASN message, %s", dCtx.refErrorCtx().toString());
267                             rmr_free_msg(msg);
268                             rmr_close(rmrCtx);
269                             return -7;
270                         }
271                         break;
272                     }
273                     case RIC_X2_SETUP_FAILURE: {
274                         mdclog_write(MDCLOG_INFO, "successful, RMR receiveing RIC_X2_SETUP_FAILURE");
275                         asn::per::DecoderCtx dCtx{msg->payload, (size_t) msg->len, allocBuffer, sizeof(allocBuffer)};
276                         E2AP_PDU opdu;
277                         if (!asn::per::unpack(opdu, dCtx)) {
278                             mdclog_write(MDCLOG_ERR, "Failed to unpack ASN message, %s", dCtx.refErrorCtx().toString());
279                             rmr_free_msg(msg);
280                             rmr_close(rmrCtx);
281                             return -7;
282                         }
283
284                         break;
285                     }
286                     default: {
287                         mdclog_write(MDCLOG_INFO, "RMR receiveing message type %d", msg->mtype);
288                         asn::per::DecoderCtx dCtx{msg->payload, (size_t) msg->len, allocBuffer, sizeof(allocBuffer)};
289                         E2AP_PDU opdu;
290                         if (!asn::per::unpack(opdu, dCtx)) {
291                             mdclog_write(MDCLOG_ERR, "Failed to unpack ASN message, %s", dCtx.refErrorCtx().toString());
292                             rmr_close(rmrCtx);
293                             return -7;
294                         }
295
296                         switch (opdu.get_index()) {
297                             case 1: { //initiating message
298                                 mdclog_write(MDCLOG_INFO, "ASN initiating message type %ld",
299                                         opdu.get_initiatingMessage()->ref_procedureCode().ref_value().get());
300                                 break;
301                             }
302                             case 2: { //successful message
303                                 mdclog_write(MDCLOG_INFO, "ASN initiating message type %ld",
304                                              opdu.get_successfulOutcome()->ref_procedureCode().ref_value().get());
305                                 break;
306                             }
307                             case 3: { //unsuccessesful message
308                                 mdclog_write(MDCLOG_INFO, "ASN initiating message type %ld",
309                                              opdu.get_unsuccessfulOutcome()->ref_procedureCode().ref_value().get());
310                                 break;
311                             }
312
313                         }
314                         mdclog_write(MDCLOG_INFO, "RMR receiveing message from E2 terminator, %d",
315                                      msg->mtype);
316                         break;
317                     }
318                 }
319             }
320         }
321     }
322 }