SIM-116: Update list of performance metrics in E2 setup request for oransim
[sim/e2-interface.git] / e2sim / e2sm_examples / kpm_e2sm / src / kpm / kpm_callbacks.cpp
1 /*****************************************************************************
2 #                                                                            *
3 # Copyright 2020 AT&T Intellectual Property                                  *
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 #include <iostream>
20 #include <fstream>
21 #include <vector>
22
23
24
25 extern "C" {
26   #include "OCTET_STRING.h"
27   #include "asn_application.h"
28   #include "E2SM-KPM-IndicationMessage.h"
29   #include "E2SM-KPM-RANfunction-Description.h"
30   #include "E2SM-KPM-IndicationHeader-Format1.h"
31   #include "E2SM-KPM-IndicationHeader.h"
32   #include "E2AP-PDU.h"
33   #include "RICsubscriptionRequest.h"
34   #include "RICsubscriptionResponse.h"
35   #include "RICactionType.h"
36   #include "ProtocolIE-Field.h"
37   #include "ProtocolIE-SingleContainer.h"
38   #include "InitiatingMessage.h"
39 }
40
41 #include "kpm_callbacks.hpp"
42 #include "encode_kpm.hpp"
43
44 #include "encode_e2apv1.hpp"
45
46 #include <nlohmann/json.hpp>
47 #include <thread>
48 #include <chrono>
49
50 #include "viavi_connector.hpp"
51 #include "errno.h"
52 #include "e2sim_defs.h"
53 using json = nlohmann::json;
54
55 using namespace std;
56 class E2Sim;
57
58
59 E2Sim e2sim;
60
61 int main(int argc, char* argv[]) {
62
63   LOG_I("Starting KPM simulator");
64
65   uint8_t *nrcellid_buf = (uint8_t*)calloc(1,5);
66   nrcellid_buf[0] = 0x22;
67   nrcellid_buf[1] = 0x5B;
68   nrcellid_buf[2] = 0xD6;
69   nrcellid_buf[3] = 0x00;
70   nrcellid_buf[4] = 0x70;
71
72   asn_codec_ctx_t *opt_cod;
73   
74   E2SM_KPM_RANfunction_Description_t *ranfunc_desc =
75     (E2SM_KPM_RANfunction_Description_t*)calloc(1,sizeof(E2SM_KPM_RANfunction_Description_t));
76   encode_kpm_function_description(ranfunc_desc);
77   
78   uint8_t e2smbuffer[8192] = {0, };
79   size_t e2smbuffer_size = 8192;
80   
81   asn_enc_rval_t er =
82     asn_encode_to_buffer(opt_cod,
83                          ATS_ALIGNED_BASIC_PER,
84                          &asn_DEF_E2SM_KPM_RANfunction_Description,
85                          ranfunc_desc, e2smbuffer, e2smbuffer_size);
86   
87   if(er.encoded == -1) {
88         LOG_I("Failed to serialize function description data. Detail: %s.", asn_DEF_E2SM_KPM_RANfunction_Description.name);
89   } else if(er.encoded > e2smbuffer_size) {
90         LOG_I("Buffer of size %zu is too small for %s, need %zu", e2smbuffer_size, asn_DEF_E2SM_KPM_RANfunction_Description.name, er.encoded);
91   }
92
93   uint8_t *ranfuncdesc = (uint8_t*)calloc(1,er.encoded);
94   memcpy(ranfuncdesc, e2smbuffer, er.encoded);
95   
96   OCTET_STRING_t *ranfunc_ostr = (OCTET_STRING_t*)calloc(1,sizeof(OCTET_STRING_t));
97   ranfunc_ostr->buf = (uint8_t*)calloc(1,er.encoded);
98   ranfunc_ostr->size = er.encoded;
99   memcpy(ranfunc_ostr->buf,e2smbuffer,er.encoded);
100  
101   e2sim.register_e2sm(0,ranfunc_ostr);
102   e2sim.register_subscription_callback(0,&callback_kpm_subscription_request);
103
104   e2sim.run_loop(argc, argv);
105
106 }
107
108 void get_cell_id(uint8_t *nrcellid_buf, char *cid_return_buf) {
109
110   uint8_t nr0 = nrcellid_buf[0] >> 4;
111   uint8_t nr1 = nrcellid_buf[0] << 4;
112   nr1 = nr1 >> 4;
113
114   uint8_t nr2 = nrcellid_buf[1] >> 4;
115   uint8_t nr3 = nrcellid_buf[1] << 4;
116   nr3 = nr3 >> 4;
117
118   uint8_t nr4 = nrcellid_buf[2] >> 4;
119   uint8_t nr5 = nrcellid_buf[2] << 4;
120   nr5 = nr5 >> 4;
121
122   uint8_t nr6 = nrcellid_buf[3] >> 4;
123   uint8_t nr7 = nrcellid_buf[3] << 4;
124   nr7 = nr7 >> 4;
125
126   uint8_t nr8 = nrcellid_buf[4] >> 4;
127   
128   sprintf(cid_return_buf, "373437%d%d%d%d%d%d%d%d%d", nr0,nr1,nr2,nr3,nr4,nr5,nr6,nr7,nr8);  
129
130 }
131
132 void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long actionId)
133 {
134   std::filebuf reports_json;
135   std::streambuf *input_filebuf = &reports_json;
136
137   std::unique_ptr<viavi::RICTesterReceiver> viavi_connector;
138   if (!reports_json.open("/playpen/src/reports.json", std::ios::in)) {
139     std::cerr << "Can't open reports.json, enabling VIAVI connector instead..." << endl;
140         viavi_connector.reset(new viavi::RICTesterReceiver {3001, nullptr});
141         input_filebuf = viavi_connector->get_data_filebuf();
142   }
143
144   std::istream input {input_filebuf};
145
146   long seqNum = 1;
147
148   std::string str;
149   
150   while ( getline(input, str) ) {
151
152     json all_ues_json;
153
154     long fqival = 9;
155     long qcival = 9;
156
157     uint8_t *plmnid_buf = (uint8_t*)"747";
158     uint8_t *sst_buf = (uint8_t*)"1";
159     uint8_t *sd_buf = (uint8_t*)"100";
160     
161
162     LOG_I("Current line: %s", str.c_str());
163
164     bool valid = false;
165
166     try {
167       all_ues_json = json::parse(str);
168       valid = true;
169     } catch (...) {
170       LOG_I("Exception on reading json");
171           exit(1);
172     }
173
174     if (valid) {
175
176       std::string first_key = all_ues_json.begin().key();
177       LOG_I("First key is %s\n", first_key.c_str());
178       
179       if (first_key.compare("ueMeasReport") == 0) {
180
181                 json::json_pointer du_id(std::string("/ueMeasReport/du-id"));
182                 int duid = all_ues_json[du_id].get<int>();
183         
184                 LOG_I("Start sending UE measurement reports with DU id %d", duid);
185
186                 int numMeasReports = (all_ues_json["/ueMeasReport/ueMeasReportList"_json_pointer]).size();
187                 
188                 for (int i = 0; i < numMeasReports; i++) {
189                         int nextCellId;
190                         int nextRsrp;
191                         int nextRsrq;
192                         int nextRssinr;
193                         float tput;
194                         int prb_usage;
195                         std::string ueId;
196
197                         json::json_pointer p001(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/ue-id");
198                         ueId = all_ues_json[p001].get<std::string>();
199
200                         LOG_I("Preparing report data for UE %d with id %s", i, ueId);
201                         
202                         json::json_pointer p0(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/throughput");
203                         tput = all_ues_json[p0].get<float>();
204                         
205                         json::json_pointer p00(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/prb_usage");
206                         prb_usage = all_ues_json[p00].get<int>();
207                         
208                         json::json_pointer p1(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/nrCellIdentity");
209                         nextCellId = all_ues_json[p1].get<int>();
210                         
211                         json::json_pointer p2(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/servingCellRfReport/rsrp");
212                         nextRsrp = all_ues_json[p2].get<int>();
213                         
214                         json::json_pointer p3(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/servingCellRfReport/rsrq");
215                         nextRsrq = all_ues_json[p3].get<int>();
216                         
217                         json::json_pointer p4(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/servingCellRfReport/rssinr");
218                         nextRssinr = all_ues_json[p4].get<int>();
219                         
220                         json::json_pointer p5(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/neighbourCellList");
221                         
222                         uint8_t crnti_buf[3] = {0, };
223
224                         if (ueId.find("Pedestrian") != string::npos) {
225                                 std::string ind = ueId.substr(11);
226                                 int indval = std::stoi(ind);
227
228                                 if (indval < 10) {
229                                 crnti_buf[1] = indval;
230                                 crnti_buf[0] = 0;
231                                 } else {
232                                 crnti_buf[0] = indval/10;
233                                 crnti_buf[1] = indval % 10;
234                                 }
235                                 
236                         } else if (ueId.find("Car") != string::npos) {
237                                 crnti_buf[0] = 4;
238                                 crnti_buf[1] = 1;
239                         }
240                                         
241                         std::string serving_str = "{\"rsrp\": " + std::to_string(nextRsrp) + ", \"rsrq\": " +
242                                 std::to_string(nextRsrq) + ", \"rssinr\": " + std::to_string(nextRssinr) + "}";
243                         const uint8_t *serving_buf = reinterpret_cast<const uint8_t*>(serving_str.c_str());     
244                         
245                         int numNeighborCells = (all_ues_json[p5]).size();
246                         
247                         std::string neighbor_str = "[";
248                         
249                         int nextNbCell;
250                         int nextNbRsrp;
251                         int nextNbRsrq;
252                         int nextNbRssinr;
253                         
254                         for (int j = 0; j < numNeighborCells; j++) {
255                                 json::json_pointer p8(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/neighbourCellList/" + std::to_string(j) + "/nbCellIdentity");
256                                 nextNbCell = all_ues_json[p8].get<int>();
257                                 json::json_pointer p9(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i)
258                                                 +"/neighbourCellList/" + std::to_string(j) + "/nbCellRfReport/rsrp");
259                                 nextNbRsrp = all_ues_json[p9].get<int>();
260                                 
261                                 json::json_pointer p10(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i)
262                                                 +"/neighbourCellList/" + std::to_string(j) + "/nbCellRfReport/rsrq");
263                                 nextNbRsrq = all_ues_json[p10].get<int>();
264                                 
265                                 json::json_pointer p11(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i)
266                                                 +"/neighbourCellList/" + std::to_string(j) + "/nbCellRfReport/rssinr");
267                                 nextNbRssinr = all_ues_json[p11].get<int>();
268                                 
269                                 if (j != 0) {
270                                         neighbor_str += ",";
271                                 }
272
273
274                                 uint8_t neighbor_cellid_buf[6] = {0, };
275                                 neighbor_cellid_buf[0] = 0x22;
276                                 neighbor_cellid_buf[1] = 0x5B;
277                                 neighbor_cellid_buf[2] = 0xD6;
278                                 neighbor_cellid_buf[3] = nextNbCell;
279                                 neighbor_cellid_buf[4] = 0x70;
280                                 
281                                 char cid_buf[25] = {0, };
282                                 get_cell_id(neighbor_cellid_buf,cid_buf);
283                                 
284                                 
285                                 neighbor_str += "{\"CID\" : \"" + std::string(cid_buf) + "\", \"Cell-RF\" : {\"rsrp\": " + std::to_string(nextNbRsrp) +
286                                 ", \"rsrq\": " + std::to_string(nextNbRsrq) + ", \"rssinr\": " + std::to_string(nextNbRssinr) + "}}";
287                                 
288                         }
289                         
290                         neighbor_str += "]";
291                         
292                         LOG_I("This is neighbor str %s\n", neighbor_str.c_str());
293                                                                 
294                         const uint8_t *neighbor_buf = reinterpret_cast<const uint8_t*>(neighbor_str.c_str());
295
296
297                         uint8_t nrcellid_buf[6] = {0, };
298                         nrcellid_buf[0] = 0x22;
299                         nrcellid_buf[1] = 0x5B;
300                         nrcellid_buf[2] = 0xD6;
301                         nrcellid_buf[3] = nextCellId;
302                         nrcellid_buf[4] = 0x70;
303
304                         uint8_t gnbid_buf[4] = {0, };
305                         gnbid_buf[0] = 0x22;
306                         gnbid_buf[1] = 0x5B;
307                         gnbid_buf[2] = 0xD6;
308
309                         uint8_t cuupid_buf[2] = {0, };
310                         cuupid_buf[0] = 20000;
311
312                         uint8_t duid_buf[2] = {0, };
313                         duid_buf[0] = 20000;
314
315                         uint8_t *cuupname_buf = (uint8_t*)"GNBCUUP5";     
316                         
317                         
318                         E2SM_KPM_IndicationMessage_t *ind_msg_cucp_ue =
319                                 (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
320                         
321                         ue_meas_kpm_report_indication_message_initialized(ind_msg_cucp_ue, nrcellid_buf, crnti_buf, serving_buf, neighbor_buf);
322                         
323                         uint8_t e2sm_message_buf_cucp_ue[8192] = {0, };
324                         size_t e2sm_message_buf_size_cucp_ue = 8192;
325                         
326                         asn_codec_ctx_t *opt_cod;
327                         
328                         asn_enc_rval_t er_message_cucp_ue = asn_encode_to_buffer(opt_cod,
329                                                                                 ATS_ALIGNED_BASIC_PER,
330                                                                                 &asn_DEF_E2SM_KPM_IndicationMessage,
331                                                                                 ind_msg_cucp_ue, e2sm_message_buf_cucp_ue, e2sm_message_buf_size_cucp_ue);
332                         
333                         if(er_message_cucp_ue.encoded == -1) {
334                                 LOG_I("Failed to serialize message data. Detail: %s.\n", asn_DEF_E2SM_KPM_IndicationMessage.name);
335                                 exit(1);
336                         } else if(er_message_cucp_ue.encoded > e2sm_message_buf_size_cucp_ue) {
337                                 LOG_I("Buffer of size %zu is too small for %s, need %zu\n", e2sm_message_buf_size_cucp_ue, asn_DEF_E2SM_KPM_IndicationMessage.name, er_message_cucp_ue.encoded);
338                                 exit(1);
339                         } else {
340                                 LOG_I("Encoded UE indication message succesfully, size in bytes: %d", er_message_cucp_ue.encoded)
341                         }
342
343                         ASN_STRUCT_FREE(asn_DEF_E2SM_KPM_IndicationMessage, ind_msg_cucp_ue); 
344
345                         E2SM_KPM_IndicationHeader_t* ind_header_cucp_ue =
346                                 (E2SM_KPM_IndicationHeader_t*)calloc(1,sizeof(E2SM_KPM_IndicationHeader_t));
347                         kpm_report_indication_header_initialized(ind_header_cucp_ue, plmnid_buf, sst_buf, sd_buf, fqival, qcival, nrcellid_buf, gnbid_buf, 0, cuupid_buf, duid_buf, cuupname_buf);
348
349                         asn_codec_ctx_t *opt_cod1;
350                         uint8_t e2sm_header_buf_cucp_ue[8192] = {0, };
351                         size_t e2sm_header_buf_size_cucp_ue = 8192;
352                         
353                         asn_enc_rval_t er_header_cucp_ue = asn_encode_to_buffer(opt_cod1,
354                                                                                 ATS_ALIGNED_BASIC_PER,
355                                                                                 &asn_DEF_E2SM_KPM_IndicationHeader,
356                                                                                 ind_header_cucp_ue, e2sm_header_buf_cucp_ue, e2sm_header_buf_size_cucp_ue);
357
358                         if(er_header_cucp_ue.encoded == -1) {
359                                 LOG_I("Failed to serialize data. Detail: %s.\n", asn_DEF_E2SM_KPM_IndicationHeader.name);
360                                 exit(1);
361                         } else if(er_header_cucp_ue.encoded > e2sm_header_buf_size_cucp_ue) {
362                                 LOG_I("Buffer of size %zu is too small for %s, need %zu\n", e2sm_header_buf_size_cucp_ue, asn_DEF_E2SM_KPM_IndicationHeader.name, er_header_cucp_ue.encoded);
363                                 exit(1);
364                         } else {
365                                 LOG_I("Encoded UE indication header succesfully, size in bytes: %d", er_header_cucp_ue.encoded);
366                                 for(int i = 0; i < er_header_cucp_ue.encoded; i ++) {
367                                         printf("%x ", e2sm_header_buf_cucp_ue[i]);
368                                 }
369                                 
370                         }
371                         
372
373                         ASN_STRUCT_FREE(asn_DEF_E2SM_KPM_IndicationHeader, ind_header_cucp_ue);
374                         
375                         E2AP_PDU *pdu_cucp_ue = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
376                         
377                         encoding::generate_e2apv1_indication_request_parameterized(pdu_cucp_ue, requestorId,
378                                                                                         instanceId, ranFunctionId,
379                                                                                         actionId, seqNum, e2sm_header_buf_cucp_ue,
380                                                                                         er_header_cucp_ue.encoded, e2sm_message_buf_cucp_ue,
381                                                                                         er_message_cucp_ue.encoded);
382                         
383                         e2sim.encode_and_send_sctp_data(pdu_cucp_ue);
384                         LOG_I("Measurement report for UE %d has been sent\n", i);
385                         seqNum++;
386                         std::this_thread::sleep_for (std::chrono::milliseconds(50));
387                 }
388       } else if (first_key.compare("cellMeasReport") == 0) {
389
390                 json::json_pointer du_id(std::string("/cellMeasReport/du-id"));
391                 int duid = all_ues_json[du_id].get<int>();
392         
393                 LOG_I("Start sending Cell measurement reports with DU id %d", duid);
394                 
395                 int numMeasReports = (all_ues_json["/cellMeasReport/cellMeasReportList"_json_pointer]).size();
396                 
397                 for (int i = 0; i < numMeasReports; i++) {
398                         int nextCellId;
399
400                         float bytes_dl;
401                         float bytes_ul;
402                         int prb_dl;
403                         int prb_ul;
404                         int cellid;
405
406                         json::json_pointer p00(std::string("/cellMeasReport/cellMeasReportList/") + std::to_string(i) +"/nrCellIdentity");
407                         cellid = all_ues_json[p00].get<int>();
408
409                         LOG_I("Preparing report data for Cell %d with id %d", i, cellid);
410                         
411                         json::json_pointer p0(std::string("/cellMeasReport/cellMeasReportList/") + std::to_string(i) +"/pdcpByteMeasReport/pdcpBytesDl");
412                         bytes_dl = all_ues_json[p0].get<float>();
413
414                         json::json_pointer p1(std::string("/cellMeasReport/cellMeasReportList/") + std::to_string(i) +"/pdcpByteMeasReport/pdcpBytesUl");
415                         bytes_ul = all_ues_json[p1].get<float>();
416                         
417                         json::json_pointer p2(std::string("/cellMeasReport/cellMeasReportList/") + std::to_string(i) +"/prbMeasReport/availPrbDl");
418                         prb_dl = all_ues_json[p2].get<int>();
419
420                         json::json_pointer p3(std::string("/cellMeasReport/cellMeasReportList/") + std::to_string(i) +"/prbMeasReport/availPrbUl");
421                         prb_ul = all_ues_json[p3].get<int>();
422
423                         
424                         uint8_t *sst_buf = (uint8_t*)"1";
425                         uint8_t *sd_buf = (uint8_t*)"100";
426                         uint8_t *plmnid_buf = (uint8_t*)"747";
427
428                         uint8_t nrcellid_buf[6] = {0, };
429                         nrcellid_buf[0] = 0x22;
430                         nrcellid_buf[1] = 0x5B;
431                         nrcellid_buf[2] = 0xD6;
432                         nrcellid_buf[3] = cellid;
433                         nrcellid_buf[4] = 0x70;
434
435                         uint8_t gnbid_buf[4] = {0, };
436                         gnbid_buf[0] = 0x22;
437                         gnbid_buf[1] = 0x5B;
438                         gnbid_buf[2] = 0xD6;
439
440                         uint8_t cuupid_buf[2] = {0, };
441                         cuupid_buf[0] = 20000;
442
443                         uint8_t duid_buf[2] = {0, };
444                         duid_buf[0] = 20000;
445
446                         uint8_t *cuupname_buf = (uint8_t*)"GNBCUUP5";             
447
448                         //Encoding Style 1 Message Body
449                         
450                         LOG_I("Encoding Style 1 Message body");   
451                         asn_codec_ctx_t *opt_cod2;
452                         
453                         E2SM_KPM_IndicationMessage_t *ind_message_style1 =
454                                 (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
455                         E2AP_PDU *pdu_style1 = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
456                         
457                         long fiveqi = 7;
458                         long *l_dl_prbs = (long*)calloc(1, sizeof(long));
459                         long *l_ul_prbs = (long*)calloc(1, sizeof(long));
460                         *l_dl_prbs = (long)prb_dl;
461                         *l_ul_prbs = (long)prb_ul;
462                         
463                         cell_meas_kpm_report_indication_message_style_1_initialized(ind_message_style1, fiveqi,
464                                                                 prb_dl, prb_ul, nrcellid_buf, l_dl_prbs, l_ul_prbs);
465                         
466                         uint8_t e2sm_message_buf_style1[8192] = {0, };
467                         size_t e2sm_message_buf_size_style1 = 8192;
468                         
469                         asn_enc_rval_t er_message_style1 = asn_encode_to_buffer(opt_cod2,
470                                                                                 ATS_ALIGNED_BASIC_PER,
471                                                                                 &asn_DEF_E2SM_KPM_IndicationMessage,
472                                                                                 ind_message_style1,
473                                                                                 e2sm_message_buf_style1, e2sm_message_buf_size_style1);
474                         
475                         if(er_message_style1.encoded == -1) {
476                                 LOG_I("Failed to serialize data. Detail: %s.\n", asn_DEF_E2SM_KPM_IndicationMessage.name);
477                                 exit(1);
478                         } else if(er_message_style1.encoded > e2sm_message_buf_size_style1) {
479                                 LOG_I("Buffer of size %zu is too small for %s, need %zu\n", e2sm_message_buf_size_style1, asn_DEF_E2SM_KPM_IndicationMessage.name, er_message_style1.encoded);
480                                 exit(1);
481                         } else {
482                                 LOG_I("Encoded Cell indication message succesfully, size in bytes: %d", er_message_style1.encoded)
483                         }
484
485                         ASN_STRUCT_FREE(asn_DEF_E2SM_KPM_IndicationMessage, ind_message_style1);
486                         
487                         uint8_t *cpid_buf2 = (uint8_t*)"CPID";
488                         
489                         E2SM_KPM_IndicationHeader_t* ind_header_style1 =
490                                 (E2SM_KPM_IndicationHeader_t*)calloc(1,sizeof(E2SM_KPM_IndicationHeader_t));
491                         kpm_report_indication_header_initialized(ind_header_style1, plmnid_buf, sst_buf, sd_buf, fqival, qcival, nrcellid_buf, gnbid_buf, 0, cuupid_buf, duid_buf, cuupname_buf);
492
493                         uint8_t e2sm_header_buf_style1[8192] = {0, };
494                         size_t e2sm_header_buf_size_style1 = 8192;
495                         
496                         asn_enc_rval_t er_header_style1 = asn_encode_to_buffer(opt_cod2,
497                                                                                 ATS_ALIGNED_BASIC_PER,
498                                                                                 &asn_DEF_E2SM_KPM_IndicationHeader,
499                                                                                 ind_header_style1,
500                                                                                 e2sm_header_buf_style1, e2sm_header_buf_size_style1);
501
502                         if(er_header_style1.encoded == -1) {
503                                 LOG_I("Failed to serialize data. Detail: %s.\n", asn_DEF_E2SM_KPM_IndicationHeader.name);
504                                 exit(1);
505                         } else if(er_header_style1.encoded > e2sm_header_buf_size_style1) {
506                                 LOG_I("Buffer of size %zu is too small for %s, need %zu\n", e2sm_header_buf_size_style1, asn_DEF_E2SM_KPM_IndicationHeader.name, er_header_style1.encoded);
507                                 exit(1);
508                         } else {
509                                 LOG_I("Encoded Cell indication header succesfully, size in bytes: %d", er_header_style1.encoded)
510                         }
511
512                         ASN_STRUCT_FREE(asn_DEF_E2SM_KPM_IndicationHeader, ind_header_style1);
513                         
514                         encoding::generate_e2apv1_indication_request_parameterized(pdu_style1, requestorId,
515                                                                                         instanceId, ranFunctionId,
516                                                                                         actionId, seqNum, e2sm_header_buf_style1,
517                                                                                         er_header_style1.encoded,
518                                                                                         e2sm_message_buf_style1, er_message_style1.encoded);
519
520                         e2sim.encode_and_send_sctp_data(pdu_style1);
521                         seqNum++;
522                         LOG_I("Measurement report for Cell %d has been sent\n", i);
523                         std::this_thread::sleep_for (std::chrono::milliseconds(50));      
524                         
525                 }
526           }                                                
527     }
528   }
529 }
530
531
532 void callback_kpm_subscription_request(E2AP_PDU_t *sub_req_pdu) {
533
534   fprintf(stderr, "[%s:%d]Calling callback_kpm_subscription_request");
535
536   //Record RIC Request ID
537   //Go through RIC action to be Setup List
538   //Find first entry with REPORT action Type
539   //Record ricActionID
540   //Encode subscription response
541
542   RICsubscriptionRequest_t orig_req =
543     sub_req_pdu->choice.initiatingMessage->value.choice.RICsubscriptionRequest;
544   
545   RICsubscriptionResponse_IEs_t *ricreqid =
546     (RICsubscriptionResponse_IEs_t*)calloc(1, sizeof(RICsubscriptionResponse_IEs_t));
547                                            
548   int count = orig_req.protocolIEs.list.count;
549   int size = orig_req.protocolIEs.list.size;
550   
551   RICsubscriptionRequest_IEs_t **ies = (RICsubscriptionRequest_IEs_t**)orig_req.protocolIEs.list.array;
552
553   RICsubscriptionRequest_IEs__value_PR pres;
554
555   long reqRequestorId;
556   long reqInstanceId;
557   long reqActionId;
558
559   std::vector<long> actionIdsAccept;
560   std::vector<long> actionIdsReject;
561
562   for (int i=0; i < count; i++) {
563     RICsubscriptionRequest_IEs_t *next_ie = ies[i];
564     pres = next_ie->value.present;
565     
566     LOG_I("The next present value %d\n", pres);
567
568     switch(pres) {
569     case RICsubscriptionRequest_IEs__value_PR_RICrequestID:
570       {
571         LOG_I("in case request id");    
572         RICrequestID_t reqId = next_ie->value.choice.RICrequestID;
573         long requestorId = reqId.ricRequestorID;
574         long instanceId = reqId.ricInstanceID;
575         LOG_I("requestorId %d\n", requestorId);
576         LOG_I("instanceId %d\n", instanceId);
577         reqRequestorId = requestorId;
578         reqInstanceId = instanceId;
579
580         break;
581       }
582     case RICsubscriptionRequest_IEs__value_PR_RANfunctionID:
583       {
584         LOG_I("in case ran func id");   
585         break;
586       }
587     case RICsubscriptionRequest_IEs__value_PR_RICsubscriptionDetails:
588       {
589         RICsubscriptionDetails_t subDetails = next_ie->value.choice.RICsubscriptionDetails;
590         RICeventTriggerDefinition_t triggerDef = subDetails.ricEventTriggerDefinition;
591         RICactions_ToBeSetup_List_t actionList = subDetails.ricAction_ToBeSetup_List;
592         //We are ignoring the trigger definition
593
594         //We identify the first action whose type is REPORT
595         //That is the only one accepted; all others are rejected
596         
597         int actionCount = actionList.list.count;
598         LOG_I("Action count%d\n", actionCount);
599
600         auto **item_array = actionList.list.array;
601
602         bool foundAction = false;
603
604         for (int i=0; i < actionCount; i++) {
605
606           auto *next_item = item_array[i];
607           RICactionID_t actionId = ((RICaction_ToBeSetup_ItemIEs*)next_item)->value.choice.RICaction_ToBeSetup_Item.ricActionID;
608           RICactionType_t actionType = ((RICaction_ToBeSetup_ItemIEs*)next_item)->value.choice.RICaction_ToBeSetup_Item.ricActionType;
609
610           if (!foundAction && actionType == RICactionType_report) {
611             reqActionId = actionId;
612             actionIdsAccept.push_back(reqActionId);
613             foundAction = true;
614           } else {
615             reqActionId = actionId;
616             actionIdsReject.push_back(reqActionId);
617           }
618         }
619         
620         break;
621       }
622     default:
623       {
624         break;
625       }      
626     }
627     
628   }
629
630   LOG_I("After Processing Subscription Request");
631
632   LOG_I("requestorId %d\n", reqRequestorId);
633   LOG_I("instanceId %d\n", reqInstanceId);
634
635
636   for (int i=0; i < actionIdsAccept.size(); i++) {
637     LOG_I("Action ID %d %ld\n", i, actionIdsAccept.at(i));
638     
639   }
640
641   E2AP_PDU *e2ap_pdu = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
642
643   long *accept_array = &actionIdsAccept[0];
644   long *reject_array = &actionIdsReject[0];
645   int accept_size = actionIdsAccept.size();
646   int reject_size = actionIdsReject.size();
647
648   encoding::generate_e2apv1_subscription_response_success(e2ap_pdu, accept_array, reject_array, accept_size, reject_size, reqRequestorId, reqInstanceId);
649   
650   LOG_I("Encode and sending E2AP subscription success response via SCTP");
651   e2sim.encode_and_send_sctp_data(e2ap_pdu);
652
653   long funcId = 0;
654
655   run_report_loop(reqRequestorId, reqInstanceId, funcId, reqActionId);
656
657 }