c0d94b1f6625f92d8a61d0c1fb0e7c5eb2610a1c
[it/test.git] / ric_benchmarking / e2-interface / e2sim / e2sm_examples / kpm_e2sm / src / kpm / kpm_callbacks.cpp
1 /*****************************************************************************
2 #                                                                            *
3 # Copyright 2020 AT&T Intellectual Property                                  *
4 #                                                                            *
5 # Copyright (c) 2020 HCL Technologies Limited.                               *
6 #                                                                            *
7 # Licensed under the Apache License, Version 2.0 (the "License");            *
8 # you may not use this file except in compliance with the License.           *
9 # You may obtain a copy of the License at                                    *
10 #                                                                            *
11 #      http://www.apache.org/licenses/LICENSE-2.0                            *
12 #                                                                            *
13 # Unless required by applicable law or agreed to in writing, software        *
14 # distributed under the License is distributed on an "AS IS" BASIS,          *
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
16 # See the License for the specific language governing permissions and        *
17 # limitations under the License.                                             *
18 #                                                                            *
19 ******************************************************************************/
20
21 #include <iostream>
22 #include <fstream>
23 #include <vector>
24 #include <unistd.h>
25
26
27 extern "C" {
28   #include "OCUCP-PF-Container.h"
29   #include "OCTET_STRING.h"
30   #include "asn_application.h"
31   #include "E2SM-KPM-IndicationMessage.h"
32   #include "FQIPERSlicesPerPlmnListItem.h"
33   #include "E2SM-KPM-RANfunction-Description.h"
34   #include "E2SM-KPM-IndicationHeader-Format1.h"
35   #include "E2SM-KPM-IndicationHeader.h"
36   #include "Timestamp.h"
37   #include "E2AP-PDU.h"
38   #include "RICsubscriptionRequest.h"
39   #include "RICsubscriptionResponse.h"
40   #include "RICactionType.h"
41   #include "ProtocolIE-Field.h"
42   #include "ProtocolIE-SingleContainer.h"
43   #include "InitiatingMessage.h"
44 }
45
46 #include "kpm_callbacks.hpp"
47 #include "encode_kpm.hpp"
48
49 #include "encode_e2apv1.hpp"
50
51 #include <nlohmann/json.hpp>
52 #include <thread>
53
54
55 using json = nlohmann::json;
56
57 using namespace std;
58 class E2Sim;
59
60
61 E2Sim e2sim;
62
63 int main(int argc, char* argv[]) {
64
65   fprintf(stderr, "Starting KPM processor sim");
66
67   asn_codec_ctx_t *opt_cod;
68
69   E2SM_KPM_RANfunction_Description_t *ranfunc_desc =
70     (E2SM_KPM_RANfunction_Description_t*)calloc(1,sizeof(E2SM_KPM_RANfunction_Description_t));
71   encode_kpm_function_description(ranfunc_desc);
72
73   uint8_t e2smbuffer[8192];
74   size_t e2smbuffer_size = 8192;
75
76   asn_enc_rval_t er =
77     asn_encode_to_buffer(opt_cod,
78                          ATS_ALIGNED_BASIC_PER,
79                          &asn_DEF_E2SM_KPM_RANfunction_Description,
80                          ranfunc_desc, e2smbuffer, e2smbuffer_size);
81   
82   fprintf(stderr, "er encded is %d\n", er.encoded);
83   fprintf(stderr, "after encoding message\n");
84   fprintf(stderr, "here is encoded message %s\n", e2smbuffer);
85
86   uint8_t *ranfuncdesc = (uint8_t*)calloc(1,er.encoded);
87   memcpy(ranfuncdesc, e2smbuffer, er.encoded);
88
89   printf("this is the char array %s\n", (char*)ranfuncdesc);
90
91   OCTET_STRING_t *ranfunc_ostr = (OCTET_STRING_t*)calloc(1,sizeof(OCTET_STRING_t));
92   ranfunc_ostr->buf = (uint8_t*)calloc(1,er.encoded);
93   ranfunc_ostr->size = er.encoded;
94   memcpy(ranfunc_ostr->buf,e2smbuffer,er.encoded);
95
96   printf("!!!lenth of ranfuncdesc is %d\n", strlen((char*)ranfuncdesc));
97   printf("value of this index is %d\n", ranfuncdesc[0]);
98   printf("value of this index is %d\n", ranfuncdesc[1]);
99   printf("value of this index is %d\n", ranfuncdesc[2]);
100   printf("value of this index is %d\n", ranfuncdesc[3]);
101   printf("value of this index is %d\n", ranfuncdesc[4]);
102   printf("value of this index is %d\n", ranfuncdesc[5]);
103   printf("value of this index is %d\n", ranfuncdesc[6]);
104   printf("value of this index is %d\n", ranfuncdesc[10]);
105   printf("value of this index is %d\n", ranfuncdesc[15]);
106   printf("value of this index is %d\n", ranfuncdesc[100]);
107   printf("value of this index is %d\n", ranfuncdesc[101]);
108   
109   e2sim.register_e2sm(0,ranfunc_ostr);
110   e2sim.register_subscription_callback(0,&callback_kpm_subscription_request);
111
112   e2sim.run_loop(argc, argv);
113
114 }
115
116 /*
117 void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long actionId) {
118
119 }
120 */
121
122 void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long actionId) {
123
124   //Process simulation file
125
126   ifstream simfile;
127   string line;
128
129   long seqNum = 1;
130
131   int  maxInd = 1000; //1000 no of indication to  send
132   
133   simfile.open("simulation.txt", ios::in);
134
135   //  cout << "step1" << endl;
136
137   std::ifstream ue_stream("/playpen/src/ueMeasReport.txt");
138   std::ifstream cell_stream("/playpen/src/cellMeasReport.txt");
139
140   json all_ues_json;
141
142   ue_stream  >> all_ues_json;
143
144   json all_cells_json;
145
146   cell_stream >> all_cells_json;
147
148   asn_codec_ctx_t *opt_cod;
149
150   //  cout << "UE RF Measurements" << endl;
151   //  cout << "******************" << endl;
152
153   int numMeasReports = (all_ues_json["/ueMeasReport/ueMeasReportList"_json_pointer]).size();
154
155   for (int i = 0; i < numMeasReports; i++) {
156     int nextCellId;
157     int nextRsrp;
158     int nextRsrq;
159     int nextRssinr;
160     //    cout << "UE number " + i << endl;
161     //    cout << "**********" << endl;
162     json::json_pointer p1(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/nrCellIdentity");
163     nextCellId = all_ues_json[p1].get<int>();
164     //    cout << "Serving Cell " << nextCellId << endl;
165     
166     json::json_pointer p2(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/servingCellRfReport/rsrp");
167     nextRsrp = all_ues_json[p2].get<int>();
168     //    cout << "  RSRP " << nextRsrp << endl;
169     json::json_pointer p3(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/servingCellRfReport/rsrq");
170     nextRsrq = all_ues_json[p3].get<int>();
171     //    cout << "  RSRQ " << nextRsrq << endl;
172     json::json_pointer p4(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/servingCellRfReport/rssinr");
173     nextRssinr = all_ues_json[p4].get<int>();
174     //    cout << "  RSSINR " << nextRssinr << endl;
175
176     json::json_pointer p5(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/neighbourCellList");
177
178     int numNeighborCells = (all_ues_json[p5]).size();
179
180
181     //REPORT Message 3 -- Encode and send OCUCP user-level report
182     
183     E2SM_KPM_IndicationMessage_t *ind_msg3 =
184       (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
185     E2AP_PDU *pdu3 = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
186     
187     uint8_t *crnti_buf = (uint8_t*)calloc(1,2);
188
189     if (nextCellId == 0) {
190       uint8_t *buf2 = (uint8_t*)"12";
191       memcpy(crnti_buf, buf2, 2);
192     } else if (nextCellId == 1) {
193       uint8_t *buf2 = (uint8_t*)"22";
194       memcpy(crnti_buf, buf2, 2);
195     }
196
197     std::string serving_str = "{\"rsrp\": " + std::to_string(nextRsrp) + ", \"rsrq\": " +
198       std::to_string(nextRsrq) + ", \"rssinr\": " + std::to_string(nextRssinr) + "}";
199     const uint8_t *serving_buf = reinterpret_cast<const uint8_t*>(serving_str.c_str());
200         
201     std::string neighbor_str = "[";
202
203     int nextNbCell;
204     int nextNbRsrp;
205     int nextNbRsrq;
206     int nextNbRssinr;
207
208     for (int j = 0; j < numNeighborCells; j++) {
209       json::json_pointer p8(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/neighbourCellList/" + std::to_string(j) + "/nbCellIdentity");
210       nextNbCell = all_ues_json[p8].get<int>();
211       //cout << "Neighbor Cell " << all_ues_json[p8] << endl;
212       json::json_pointer p9(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i)
213                             +"/neighbourCellList/" + std::to_string(j) + "/nbCellRfReport/rsrp");
214       nextNbRsrp = all_ues_json[p9].get<int>();
215       //cout << "  RSRP " << nextNbRsrp << endl;
216
217       json::json_pointer p10(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i)
218                             +"/neighbourCellList/" + std::to_string(j) + "/nbCellRfReport/rsrq");
219       nextNbRsrq = all_ues_json[p10].get<int>();
220       //cout << "  RSRQ " << nextNbRsrq << endl;
221
222       json::json_pointer p11(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i)
223                              +"/neighbourCellList/" + std::to_string(j) + "/nbCellRfReport/rssinr");
224       nextNbRssinr = all_ues_json[p11].get<int>();
225       //cout << "  RSSINR " << nextNbRssinr << endl;
226
227       if (j != 0) {
228         neighbor_str += ",";
229
230       }
231
232       neighbor_str += "{\"CID\" : \"" + std::to_string(nextNbCell) + "\", \"Cell-RF\" : \"{\"rsrp\": " + std::to_string(nextNbRsrp) +
233         ", \"rsrq\": " + std::to_string(nextNbRsrq) + ", \"rssinr\": " + std::to_string(nextNbRssinr) + "}}";
234       
235     }
236
237     neighbor_str += "]";
238     
239     const uint8_t *neighbor_buf = reinterpret_cast<const uint8_t*>(neighbor_str.c_str());
240     
241     //    printf("Neighbor string\n%s", neighbor_buf);
242
243     uint8_t *plmnid_buf = (uint8_t*)"747";
244     uint8_t *nrcellid_buf = (uint8_t*)"12340";
245
246     /*
247     encode_kpm_report_rancontainer_cucp_parameterized(ind_msg3, plmnid_buf, nrcellid_buf, crnti_buf, serving_buf, neighbor_buf);
248     
249     uint8_t e2smbuffer3[8192];
250     size_t e2smbuffer_size3 = 8192;
251
252     
253     asn_enc_rval_t er3 = asn_encode_to_buffer(opt_cod,
254                                               ATS_ALIGNED_BASIC_PER,
255                                               &asn_DEF_E2SM_KPM_IndicationMessage,
256                                               ind_msg3, e2smbuffer3, e2smbuffer_size3);
257     
258     fprintf(stderr, "er encded is %d\n", er3.encoded);
259     fprintf(stderr, "after encoding message\n");
260     uint8_t *e2smheader_buf3 = (uint8_t*)"";
261     
262     generate_e2apv1_indication_request_parameterized(pdu3, requestorId,
263                                                      instanceId, ranFunctionId,
264                                                      actionId, seqNum, e2smheader_buf3, 0, e2smbuffer3, er3.encoded);
265     
266                                                      e2sim.encode_and_send_sctp_data(pdu3);
267     */
268     
269     seqNum++;
270         
271   }
272
273
274   //  cout << "Cell Measurements" << endl;
275   //  cout << "******************" << endl;
276
277   int numCellMeasReports = (all_cells_json["/cellMeasReport/cellMeasReportList"_json_pointer]).size();
278
279   uint8_t *sst_buf = (uint8_t*)"1";
280   uint8_t *sd_buf = (uint8_t*)"100";
281   uint8_t *plmnid_buf = (uint8_t*)"747";
282   
283   for (int i = 0; i < numCellMeasReports; i++) {
284
285     int nextCellId;
286     int nextPdcpBytesDL;
287     int nextPdcpBytesUL;
288     int nextPRBBytesDL;
289     int nextPRBBytesUL;
290
291     json::json_pointer p1(std::string("/cellMeasReport/cellMeasReportList/") + std::to_string(i) +"/nrCellIdentity");
292     nextCellId = all_cells_json[p1].get<int>();
293     //    cout << std::string("Cell number ") << nextCellId << endl;
294     
295     //    cout << "**********" << endl;
296     
297     json::json_pointer p2(std::string("/cellMeasReport/cellMeasReportList/") + std::to_string(i) +"/pdcpByteMeasReport/pdcpBytesDl");
298     nextPdcpBytesDL = all_cells_json[p2].get<int>();
299     //    cout << std::string("  PDCP Bytes DL ") << nextPdcpBytesDL << endl;
300
301     json::json_pointer p3(std::string("/cellMeasReport/cellMeasReportList/") + std::to_string(i) +"/pdcpByteMeasReport/pdcpBytesUl");
302     nextPdcpBytesUL = all_cells_json[p3].get<int>();    
303     //    cout << std::string("  PDCP Bytes UL ") << nextPdcpBytesUL << endl;
304
305     uint8_t *buf = (uint8_t*)"GNBCUUP5";
306     
307     int bytes_dl = nextPdcpBytesDL;
308
309     int bytes_ul = nextPdcpBytesUL;
310
311     //    int bytes_dl = 3905;
312     //    int bytes_ul = 1609321;
313     
314     E2SM_KPM_IndicationMessage_t *ind_msg2 =
315       (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
316     E2AP_PDU *pdu2 = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
317     
318     encode_kpm_report_style5_parameterized(ind_msg2 , buf, bytes_dl, bytes_ul, sst_buf, sd_buf, plmnid_buf);
319     
320     uint8_t e2smbuffer2[8192];
321     size_t e2smbuffer_size2 = 8192;
322     
323     asn_enc_rval_t er2 = asn_encode_to_buffer(opt_cod,
324                                               ATS_ALIGNED_BASIC_PER,
325                                               &asn_DEF_E2SM_KPM_IndicationMessage,
326                                               ind_msg2, e2smbuffer2, e2smbuffer_size2);
327
328     //fprintf(stderr, "er encded is %d\n", er2.encoded);
329     //fprintf(stderr, "after encoding message\n");
330
331     E2SM_KPM_IndicationHeader_t *ihead =
332       (E2SM_KPM_IndicationHeader_t*)calloc(1,sizeof(E2SM_KPM_IndicationHeader_t));
333
334     
335     E2SM_KPM_IndicationHeader_Format1_t* ind_header =
336       (E2SM_KPM_IndicationHeader_Format1_t*)calloc(1,sizeof(E2SM_KPM_IndicationHeader_Format1_t));
337
338     OCTET_STRING_t *plmnid = (OCTET_STRING_t*)calloc(1,sizeof(OCTET_STRING_t));
339     plmnid->buf = (uint8_t*)calloc(3,1);
340     plmnid->size = 3;
341     memcpy(plmnid->buf, plmnid_buf, plmnid->size);
342
343     long fqival = 9;
344     long qcival = 9;
345
346     OCTET_STRING_t *sst = (OCTET_STRING_t*)calloc(1, sizeof(OCTET_STRING_t));
347     sst->size = 6;
348     sst->buf = (uint8_t*)calloc(1,6);
349     memcpy(sst->buf,sst_buf,sst->size);
350     
351
352     OCTET_STRING_t *sds = (OCTET_STRING_t*)calloc(1, sizeof(OCTET_STRING_t));
353     sds->size = 3;
354     sds->buf = (uint8_t*)calloc(1,3);
355     memcpy(sds->buf, sd_buf, sds->size);
356     
357     
358     SNSSAI_t *snssai = (SNSSAI_t*)calloc(1, sizeof(SNSSAI_t));
359     ASN_STRUCT_RESET(asn_DEF_SNSSAI,snssai);
360     snssai->sST.buf = (uint8_t*)calloc(1,1);
361     snssai->sST.size = 1;
362     memcpy(snssai->sST.buf, sst_buf, 1);
363     snssai->sD = (OCTET_STRING_t*)calloc(1, sizeof(OCTET_STRING_t));
364     snssai->sD->buf = (uint8_t*)calloc(1,3);
365     snssai->sD->size = 3;
366     memcpy(snssai->sD->buf, sd_buf, 3);
367
368         
369     ind_header->pLMN_Identity = plmnid;
370     ind_header->fiveQI = &fqival;
371
372     BIT_STRING_t *nrcellid = (BIT_STRING_t*)calloc(1, sizeof(BIT_STRING_t));;
373     nrcellid->buf = (uint8_t*)calloc(1,5);
374     nrcellid->size = 5;
375     nrcellid->buf[0] = 0x22;
376     nrcellid->buf[1] = 0x5B;
377     nrcellid->buf[2] = 0xD6;
378     nrcellid->buf[3] = 0x00;
379     nrcellid->buf[4] = 0x70;
380     
381     nrcellid->bits_unused = 4;
382
383     BIT_STRING_t *gnb_bstring = (BIT_STRING_t*)calloc(1, sizeof(BIT_STRING_t));;
384     gnb_bstring->buf = (uint8_t*)calloc(1,4);
385     gnb_bstring->size = 4;
386     gnb_bstring->buf[0] = 0xB5;
387     gnb_bstring->buf[1] = 0xC6;
388     gnb_bstring->buf[2] = 0x77;
389     gnb_bstring->buf[3] = 0x88;
390     
391     gnb_bstring->bits_unused = 3;
392
393     INTEGER_t *cuup_id = (INTEGER_t*)calloc(1, sizeof(INTEGER_t));
394     uint8_t buffer[1];
395     buffer[0] = 20000;
396     cuup_id->buf = (uint8_t*)calloc(1,1);
397     memcpy(cuup_id->buf, buffer, 1);
398     cuup_id->size = 1;
399
400     ind_header->id_GlobalKPMnode_ID = (GlobalKPMnode_ID*)calloc(1,sizeof(GlobalKPMnode_ID));
401     ind_header->id_GlobalKPMnode_ID->present = GlobalKPMnode_ID_PR_gNB;
402     ind_header->id_GlobalKPMnode_ID->choice.gNB.global_gNB_ID.gnb_id.present = GNB_ID_Choice_PR_gnb_ID;
403     ind_header->id_GlobalKPMnode_ID->choice.gNB.global_gNB_ID.gnb_id.choice.gnb_ID = *gnb_bstring;
404     ind_header->id_GlobalKPMnode_ID->choice.gNB.global_gNB_ID.plmn_id = *plmnid;
405     ind_header->id_GlobalKPMnode_ID->choice.gNB.gNB_CU_UP_ID = cuup_id;
406
407     ind_header->nRCGI = (NRCGI*)calloc(1,sizeof(NRCGI));
408     ind_header->nRCGI->pLMN_Identity = *plmnid;
409     ind_header->nRCGI->nRCellIdentity = *nrcellid;
410
411     ind_header->sliceID = snssai;
412     ind_header->qci = &qcival;
413     //    ind_header->message_Type = ;
414     //    ind_header->gNB_DU_ID = ;
415
416     
417     uint8_t *buf5 = (uint8_t*)"GNBCUUP5";
418     OCTET_STRING_t *cuupname = (OCTET_STRING_t*)calloc(1, sizeof(OCTET_STRING_t));
419     cuupname->size = 8;
420     cuupname->buf = (uint8_t*)calloc(1,8);
421     memcpy(cuupname->buf, buf5, cuupname->size);    
422
423     
424     ind_header->gNB_Name = (GNB_Name*)calloc(1,sizeof(GNB_Name));
425     ind_header->gNB_Name->present = GNB_Name_PR_gNB_CU_UP_Name;
426     ind_header->gNB_Name->choice.gNB_CU_UP_Name = *cuupname;
427
428
429     ind_header->global_GNB_ID = (GlobalgNB_ID*)calloc(1,sizeof(GlobalgNB_ID));
430     ind_header->global_GNB_ID->plmn_id = *plmnid;
431     ind_header->global_GNB_ID->gnb_id.present = GNB_ID_Choice_PR_gnb_ID;
432     ind_header->global_GNB_ID->gnb_id.choice.gnb_ID = *gnb_bstring;
433     
434
435     ihead->present = E2SM_KPM_IndicationHeader_PR_indicationHeader_Format1;
436     ihead->choice.indicationHeader_Format1 = *ind_header;
437
438     //printf("IndicationHeader - now printing xer\n");
439     //xer_fprint(stderr, &asn_DEF_E2SM_KPM_IndicationHeader, ihead);
440     //printf("IndicationHeader - done printing xer\n");      
441
442     uint8_t e2sm_header_buffer[8192];
443     size_t e2sm_header_buffer_size = 8192;
444     
445     asn_enc_rval_t er4 = asn_encode_to_buffer(opt_cod,
446                                               ATS_ALIGNED_BASIC_PER,
447                                               &asn_DEF_E2SM_KPM_IndicationHeader,
448                                               ihead, e2sm_header_buffer, e2sm_header_buffer_size);    
449     
450     uint8_t *e2smheader_buf2 = (uint8_t*)"";
451
452     int seqNum0 = 1;
453
454     if (i == 0) {
455     
456       sleep(10); //added sleep before sending indication because bouncer xapp need to some time to receive subscription response properly 
457
458       for(int ind=0; ind<maxInd; ind++) {
459
460             fprintf(stderr,"\nSending RIC Indication with seqnum = %d max_num_of_Ind = %d \n", seqNum0,maxInd);
461
462             encoding::generate_e2apv1_indication_request_parameterized(pdu2, requestorId,
463                                                                  instanceId, ranFunctionId,
464                                                                  actionId, seqNum0, e2sm_header_buffer, er4.encoded, e2smbuffer2, er2.encoded);
465       
466             e2sim.encode_and_send_sctp_data(pdu2);
467
468             seqNum0++;
469       }     
470     }
471
472     seqNum++;
473
474     
475
476     json::json_pointer p4(std::string("/cellMeasReport/cellMeasReportList/") + std::to_string(i) +"/prbMeasReport/availPrbDl");
477     nextPRBBytesDL = all_cells_json[p4].get<int>();    
478     //    cout << std::string("  PRB Bytes DL ") << all_cells_json[p4] << endl;
479
480     json::json_pointer p5(std::string("/cellMeasReport/cellMeasReportList/") + std::to_string(i) +"/prbMeasReport/availPrbUl");
481     nextPRBBytesUL = all_cells_json[p5].get<int>();
482     //    cout << std::string("  PRB Bytes UL ") << all_cells_json[p5] << endl;
483
484
485     //REPORT Message 1 -- Encode and send ODU cell-level report
486     
487     E2SM_KPM_IndicationMessage_t *ind_msg1 =
488       (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
489     E2AP_PDU *pdu = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
490     
491     long fiveqi = 7;
492
493     uint8_t *nrcellid_buf = (uint8_t*)"12340";
494     long dl_prbs = nextPRBBytesDL;
495     long ul_prbs = nextPRBBytesUL;
496
497     /*
498     encode_kpm_report_style1_parameterized(ind_msg1, fiveqi, dl_prbs, ul_prbs, sst_buf, sd_buf, plmnid_buf, nrcellid_buf, &dl_prbs, &ul_prbs);
499     
500     uint8_t e2smbuffer[8192];
501     size_t e2smbuffer_size = 8192;
502     
503     asn_enc_rval_t er = asn_encode_to_buffer(opt_cod,
504                                              ATS_ALIGNED_BASIC_PER,
505                                              &asn_DEF_E2SM_KPM_IndicationMessage,
506                                              ind_msg1, e2smbuffer, e2smbuffer_size);
507     
508     fprintf(stderr, "er encded is %d\n", er.encoded);
509     fprintf(stderr, "after encoding message\n");
510     uint8_t *e2smheader_buf = (uint8_t*)"";
511     
512     uint8_t *cpid_buf = (uint8_t*)"CPID";
513     
514     fprintf(stderr, "About to encode Indication\n");
515     generate_e2apv1_indication_request_parameterized(pdu, requestorId,
516                                                      instanceId, ranFunctionId,
517                                                      actionId, seqNum, e2smheader_buf, 0, e2smbuffer, er.encoded);
518     
519                                                      e2sim.encode_and_send_sctp_data(pdu);
520     */
521     seqNum++;
522     
523   }
524
525
526
527   /*
528   if (simfile.is_open()) {
529
530     while (getline(simfile, line)) {
531       cout << line << "\n";
532
533       //REPORT Message 1 -- Encode and send ODU cell-level report
534
535       E2SM_KPM_IndicationMessage_t *ind_msg1 =
536         (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
537       E2AP_PDU *pdu = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
538
539       long fiveqi = 7;
540       uint8_t *sst_buf = (uint8_t*)"1";
541       uint8_t *sd_buf = (uint8_t*)"100";
542       uint8_t *plmnid_buf = (uint8_t*)"747";
543       uint8_t *nrcellid_buf = (uint8_t*)"12340";
544       long dl_prbs = 100;
545       long ul_prbs = 50; 
546      
547       encode_kpm_report_style1_parameterized(ind_msg1, fiveqi, dl_prbs, ul_prbs, sst_buf, sd_buf, plmnid_buf, nrcellid_buf, &dl_prbs, &ul_prbs);
548
549       uint8_t e2smbuffer[8192];
550       size_t e2smbuffer_size = 8192;
551       asn_codec_ctx_t *opt_cod;
552
553       asn_enc_rval_t er = asn_encode_to_buffer(opt_cod,
554                                                ATS_ALIGNED_BASIC_PER,
555                                                &asn_DEF_E2SM_KPM_IndicationMessage,
556                                                ind_msg1, e2smbuffer, e2smbuffer_size);
557       
558       fprintf(stderr, "er encded is %d\n", er.encoded);
559       fprintf(stderr, "after encoding message\n");
560       uint8_t *e2smheader_buf = (uint8_t*)"header";
561
562       uint8_t *cpid_buf = (uint8_t*)"CPID";
563
564       fprintf(stderr, "About to encode Indication\n");
565       generate_e2apv1_indication_request_parameterized(pdu, requestorId,
566                                                        instanceId, ranFunctionId,
567                                                        actionId, seqNum, e2smheader_buf, 6, e2smbuffer, er.encoded);
568
569       encode_and_send_sctp_data(pdu, socket_fd);
570       
571       seqNum++;
572
573       //REPORT Message 2 -- Encode and send OCUUP cell-level report
574
575       uint8_t *buf = (uint8_t*)"GNBCUUP5";
576
577       int bytes_dl = 40000;
578       int bytes_ul = 50000;
579
580       E2SM_KPM_IndicationMessage_t *ind_msg2 =
581         (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
582       E2AP_PDU *pdu2 = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
583       
584       encode_kpm_report_style5_parameterized(ind_msg2 , buf, bytes_dl, bytes_ul, sst_buf, sd_buf, plmnid_buf);
585
586       uint8_t e2smbuffer2[8192];
587       size_t e2smbuffer_size2 = 8192;
588
589
590       asn_enc_rval_t er2 = asn_encode_to_buffer(opt_cod,
591                                                ATS_ALIGNED_BASIC_PER,
592                                                &asn_DEF_E2SM_KPM_IndicationMessage,
593                                                ind_msg2, e2smbuffer2, e2smbuffer_size2);
594       
595       fprintf(stderr, "er encded is %d\n", er2.encoded);
596       fprintf(stderr, "after encoding message\n");
597       uint8_t *e2smheader_buf2 = (uint8_t*)"header";
598
599       generate_e2apv1_indication_request_parameterized(pdu2, requestorId,
600                                                        instanceId, ranFunctionId,
601                                                        actionId, seqNum, e2smheader_buf2, 6, e2smbuffer2, er2.encoded);
602
603       encode_and_send_sctp_data(pdu2, socket_fd);
604       
605       seqNum++;
606
607       //REPORT Message 3 -- Encode and send OCUCP user-level report
608
609       E2SM_KPM_IndicationMessage_t *ind_msg3 =
610         (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
611       E2AP_PDU *pdu3 = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
612
613       uint8_t *crnti_buf = (uint8_t*)"12";
614       //      uint8_t *serving_buf = (uint8_t*)"RSRP10";
615       //uint8_t *neighbor_buf = (uint8_t*)"-10,-15";
616       int rsrpServ = 10;
617       int rsrqServ = 0;
618       int rssinrServ = 0;
619
620       std::string serving_str = "{\"rsrp\": " + std::to_string(rsrpServ) + ", \"rsrq\": " +
621         std::to_string(rsrqServ) + ", \"rssinr\": " + std::to_string(rssinrServ) + "}";
622       const uint8_t *serving_buf = reinterpret_cast<const uint8_t*>(serving_str.c_str());
623
624
625       neighbor_cell_entry n_entries[3];
626       n_entries[0] = {"123", 10, 0, 0};
627       n_entries[1] = {"456", 10, 0, 0};
628       n_entries[2] = {"789", 10, 0, 0};
629
630       std::string neighbor_str = "[";
631
632       for (int i=0; i < sizeof(n_entries)/sizeof(n_entries[0]); i++) {
633
634         if (i != 0) {
635           neighbor_str += ",";
636         }
637         neighbor_str += "{\"CID\" : \"" + std::string(n_entries[i].cellid) + "\", \"Cell-RF\" : \"{\"rsrp\": " + std::to_string(n_entries[i].rsrp) +
638           ", \"rsrq\": " + std::to_string(n_entries[i].rsrq) + ", \"rssinr\": " + std::to_string(n_entries[i].rsrp) + "}}";
639       }
640
641       neighbor_str += "]";
642
643       const uint8_t *neighbor_buf = reinterpret_cast<const uint8_t*>(neighbor_str.c_str());
644
645       printf("Neighbor string\n%s", neighbor_buf);
646
647       encode_kpm_report_rancontainer_cucp_parameterized(ind_msg3, plmnid_buf, nrcellid_buf, crnti_buf, serving_buf, neighbor_buf);
648       
649       uint8_t e2smbuffer3[8192];
650       size_t e2smbuffer_size3 = 8192;
651
652       asn_enc_rval_t er3 = asn_encode_to_buffer(opt_cod,
653                                                 ATS_ALIGNED_BASIC_PER,
654                                                 &asn_DEF_E2SM_KPM_IndicationMessage,
655                                                 ind_msg3, e2smbuffer3, e2smbuffer_size3);
656       
657       fprintf(stderr, "er encded is %d\n", er3.encoded);
658       fprintf(stderr, "after encoding message\n");
659       uint8_t *e2smheader_buf3 = (uint8_t*)"header";
660
661       generate_e2apv1_indication_request_parameterized(pdu3, requestorId,
662                                                        instanceId, ranFunctionId,
663                                                        actionId, seqNum, e2smheader_buf3, 6, e2smbuffer3, er3.encoded);
664
665       encode_and_send_sctp_data(pdu3, socket_fd);
666             
667       seqNum++;
668       
669       //Encode and send OCUUP user-level report
670
671
672       
673       //Encode and send ODU user-level report
674
675       
676
677       
678     }
679
680     simfile.close();
681
682   }
683   */
684
685 }
686
687
688 void callback_kpm_subscription_request(E2AP_PDU_t *sub_req_pdu) {
689
690   fprintf(stderr, "Calling callback_kpm_subscription_request\n");
691
692   //Record RIC Request ID
693   //Go through RIC action to be Setup List
694   //Find first entry with REPORT action Type
695   //Record ricActionID
696   //Encode subscription response
697
698   RICsubscriptionRequest_t orig_req =
699     sub_req_pdu->choice.initiatingMessage->value.choice.RICsubscriptionRequest;
700   
701   RICsubscriptionResponse_IEs_t *ricreqid =
702     (RICsubscriptionResponse_IEs_t*)calloc(1, sizeof(RICsubscriptionResponse_IEs_t));
703                                            
704   int count = orig_req.protocolIEs.list.count;
705   int size = orig_req.protocolIEs.list.size;
706   
707   RICsubscriptionRequest_IEs_t **ies = (RICsubscriptionRequest_IEs_t**)orig_req.protocolIEs.list.array;
708
709   fprintf(stderr, "count%d\n", count);
710   fprintf(stderr, "size%d\n", size);
711
712   RICsubscriptionRequest_IEs__value_PR pres;
713
714   long reqRequestorId;
715   long reqInstanceId;
716   long reqActionId;
717
718   std::vector<long> actionIdsAccept;
719   std::vector<long> actionIdsReject;
720
721   for (int i=0; i < count; i++) {
722     RICsubscriptionRequest_IEs_t *next_ie = ies[i];
723     pres = next_ie->value.present;
724     
725     fprintf(stderr, "The next present value %d\n", pres);
726
727     switch(pres) {
728     case RICsubscriptionRequest_IEs__value_PR_RICrequestID:
729       {
730         fprintf(stderr,"in case request id\n"); 
731         RICrequestID_t reqId = next_ie->value.choice.RICrequestID;
732         long requestorId = reqId.ricRequestorID;
733         long instanceId = reqId.ricInstanceID;
734         fprintf(stderr, "requestorId %d\n", requestorId);
735         fprintf(stderr, "instanceId %d\n", instanceId);
736         reqRequestorId = requestorId;
737         reqInstanceId = instanceId;
738
739         break;
740       }
741     case RICsubscriptionRequest_IEs__value_PR_RANfunctionID:
742       {
743         fprintf(stderr,"in case ran func id\n");        
744         break;
745       }
746     case RICsubscriptionRequest_IEs__value_PR_RICsubscriptionDetails:
747       {
748         fprintf(stderr,"in case subscription details\n");
749         RICsubscriptionDetails_t subDetails = next_ie->value.choice.RICsubscriptionDetails;
750         fprintf(stderr,"in case subscription details 1\n");     
751         RICeventTriggerDefinition_t triggerDef = subDetails.ricEventTriggerDefinition;
752         fprintf(stderr,"in case subscription details 2\n");     
753         RICactions_ToBeSetup_List_t actionList = subDetails.ricAction_ToBeSetup_List;
754         fprintf(stderr,"in case subscription details 3\n");
755         //We are ignoring the trigger definition
756
757         //We identify the first action whose type is REPORT
758         //That is the only one accepted; all others are rejected
759         
760         int actionCount = actionList.list.count;
761         fprintf(stderr, "action count%d\n", actionCount);
762
763         auto **item_array = actionList.list.array;
764
765         bool foundAction = false;
766
767         for (int i=0; i < actionCount; i++) {
768
769           auto *next_item = item_array[i];
770           RICactionID_t actionId = ((RICaction_ToBeSetup_ItemIEs*)next_item)->value.choice.RICaction_ToBeSetup_Item.ricActionID;
771           RICactionType_t actionType = ((RICaction_ToBeSetup_ItemIEs*)next_item)->value.choice.RICaction_ToBeSetup_Item.ricActionType;
772
773           if (!foundAction && actionType == RICactionType_report) {
774             reqActionId = actionId;
775             actionIdsAccept.push_back(reqActionId);
776             printf("adding accept\n");
777             foundAction = true;
778           } else {
779             reqActionId = actionId;
780             printf("adding reject\n");
781             actionIdsReject.push_back(reqActionId);
782           }
783         }
784         
785         break;
786       }
787     default:
788       {
789         fprintf(stderr,"in case default\n");    
790         break;
791       }      
792     }
793     
794   }
795
796   fprintf(stderr, "After Processing Subscription Request\n");
797
798   fprintf(stderr, "requestorId %d\n", reqRequestorId);
799   fprintf(stderr, "instanceId %d\n", reqInstanceId);
800
801
802   for (int i=0; i < actionIdsAccept.size(); i++) {
803     fprintf(stderr, "Action ID %d %ld\n", i, actionIdsAccept.at(i));
804     
805   }
806
807   E2AP_PDU *e2ap_pdu = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
808
809   long *accept_array = &actionIdsAccept[0];
810   long *reject_array = &actionIdsReject[0];
811   int accept_size = actionIdsAccept.size();
812   int reject_size = actionIdsReject.size();
813
814   encoding::generate_e2apv1_subscription_response_success(e2ap_pdu, accept_array, reject_array, accept_size, reject_size, reqRequestorId, reqInstanceId);
815
816   e2sim.encode_and_send_sctp_data(e2ap_pdu);
817
818   //Start thread for sending REPORT messages
819
820   //  std::thread loop_thread;
821
822   long funcId = 1;
823
824   run_report_loop(reqRequestorId, reqInstanceId, funcId, reqActionId);
825
826   //  loop_thread = std::thread(&run_report_loop);
827
828 }