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