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