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