Fixes on Indication sending
[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 "OCUCP-PF-Container.h"
27   #include "OCTET_STRING.h"
28   #include "asn_application.h"
29   #include "E2SM-KPM-IndicationMessage.h"
30   #include "FQIPERSlicesPerPlmnListItem.h"
31   #include "E2SM-KPM-RANfunction-Description.h"
32   #include "E2SM-KPM-IndicationHeader-Format1.h"
33   #include "E2SM-KPM-IndicationHeader.h"
34   #include "Timestamp.h"
35   #include "E2AP-PDU.h"
36   #include "RICsubscriptionRequest.h"
37   #include "RICsubscriptionResponse.h"
38   #include "RICactionType.h"
39   #include "ProtocolIE-Field.h"
40   #include "ProtocolIE-SingleContainer.h"
41   #include "InitiatingMessage.h"
42 }
43
44 #include "kpm_callbacks.hpp"
45 #include "encode_kpm.hpp"
46
47 #include "encode_e2apv1.hpp"
48
49 #include <nlohmann/json.hpp>
50 #include <thread>
51 #include <chrono>
52
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   fprintf(stderr, "Starting KPM processor sim");
64
65   fprintf(stderr, "JSON Test\n");
66
67
68   asn_codec_ctx_t *opt_cod;
69   
70   E2SM_KPM_RANfunction_Description_t *ranfunc_desc =
71     (E2SM_KPM_RANfunction_Description_t*)calloc(1,sizeof(E2SM_KPM_RANfunction_Description_t));
72   encode_kpm_function_description(ranfunc_desc);
73   
74   uint8_t e2smbuffer[8192];
75   size_t e2smbuffer_size = 8192;
76   
77   asn_enc_rval_t er =
78     asn_encode_to_buffer(opt_cod,
79                          ATS_ALIGNED_BASIC_PER,
80                          &asn_DEF_E2SM_KPM_RANfunction_Description,
81                          ranfunc_desc, e2smbuffer, e2smbuffer_size);
82   
83   fprintf(stderr, "er encded is %d\n", er.encoded);
84   fprintf(stderr, "after encoding message\n");
85   fprintf(stderr, "here is encoded message %s\n", e2smbuffer);
86   
87   uint8_t *ranfuncdesc = (uint8_t*)calloc(1,er.encoded);
88   memcpy(ranfuncdesc, e2smbuffer, er.encoded);
89   
90   printf("this is the char array %s\n", (char*)ranfuncdesc);
91   
92   OCTET_STRING_t *ranfunc_ostr = (OCTET_STRING_t*)calloc(1,sizeof(OCTET_STRING_t));
93   ranfunc_ostr->buf = (uint8_t*)calloc(1,er.encoded);
94   ranfunc_ostr->size = er.encoded;
95   memcpy(ranfunc_ostr->buf,e2smbuffer,er.encoded);
96
97   printf("!!!lenth of ranfuncdesc is %d\n", strlen((char*)ranfuncdesc));
98   printf("value of this index is %d\n", ranfuncdesc[0]);
99   printf("value of this index is %d\n", ranfuncdesc[1]);
100   printf("value of this index is %d\n", ranfuncdesc[2]);
101   printf("value of this index is %d\n", ranfuncdesc[3]);
102   printf("value of this index is %d\n", ranfuncdesc[4]);
103   printf("value of this index is %d\n", ranfuncdesc[5]);
104   printf("value of this index is %d\n", ranfuncdesc[6]);
105   printf("value of this index is %d\n", ranfuncdesc[10]);
106   printf("value of this index is %d\n", ranfuncdesc[15]);
107   printf("value of this index is %d\n", ranfuncdesc[100]);
108   printf("value of this index is %d\n", ranfuncdesc[101]);
109   
110   e2sim.register_e2sm(0,ranfunc_ostr);
111   e2sim.register_subscription_callback(0,&callback_kpm_subscription_request);
112
113   e2sim.run_loop(argc, argv);
114
115 }
116
117 void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long actionId) {
118
119   std::ifstream input("/playpen/src/reports.json");
120   bool x = input.good();
121
122   fprintf(stderr, "%s\n", x ? "true" : "false");
123
124   long seqNum = 1;
125
126   std::string str;
127   
128   while ( getline(input, str) ) {
129
130     json all_ues_json;
131
132     fprintf(stderr,"De line is %s\n", str.c_str());
133
134     bool valid = false;
135
136     try {
137       all_ues_json = json::parse(str);
138
139       valid = true;
140     } catch (...) {
141       fprintf(stderr, "Exception on reading json\n");
142     }
143
144     if (valid) {
145
146       fprintf(stderr, "key is valid\n");
147
148       std::string first_key = all_ues_json.begin().key();
149       fprintf(stderr, "first key is %s\n", first_key.c_str());
150       
151       if (first_key.compare("ueMeasReport") == 0) {
152         
153         fprintf(stderr, "it is equal to ue meas report\n");
154
155         int numMeasReports = (all_ues_json["/ueMeasReport/ueMeasReportList"_json_pointer]).size();
156         
157       
158         for (int i = 0; i < numMeasReports; i++) {
159           int nextCellId;
160           int nextRsrp;
161           int nextRsrq;
162           int nextRssinr;
163           float tput;
164           int prb_usage;
165           fprintf(stderr,"UE number %d\n", i);
166           
167           json::json_pointer p0(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/throughput");
168           tput = all_ues_json[p0].get<float>();
169           fprintf(stderr, "Throughput %f\n", tput);
170           
171           json::json_pointer p00(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/prb_usage");
172           prb_usage = all_ues_json[p00].get<int>();
173           fprintf(stderr, "Throughput %d\n", prb_usage);        
174           
175           json::json_pointer p1(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/nrCellIdentity");
176           nextCellId = all_ues_json[p1].get<int>();
177           fprintf(stderr, "Serving Cell %d\n",nextCellId);
178           
179           json::json_pointer p2(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/servingCellRfReport/rsrp");
180           nextRsrp = all_ues_json[p2].get<int>();
181           fprintf(stderr,"  RSRP %d\n", nextRsrp);
182           
183           json::json_pointer p3(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/servingCellRfReport/rsrq");
184           nextRsrq = all_ues_json[p3].get<int>();
185           fprintf(stderr,"  RSRQ %d\n",nextRsrq);
186           
187           json::json_pointer p4(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/servingCellRfReport/rssinr");
188           nextRssinr = all_ues_json[p4].get<int>();
189           fprintf(stderr,"  RSSINR %d\n", nextRssinr);
190           
191           json::json_pointer p5(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/neighbourCellList");
192           
193           uint8_t *crnti_buf = (uint8_t*)calloc(1,2);
194           
195           uint8_t *buf2 = (uint8_t*)"12";
196           memcpy(crnti_buf, buf2, 2);
197           
198           
199           std::string serving_str = "{\"rsrp\": " + std::to_string(nextRsrp) + ", \"rsrq\": " +
200             std::to_string(nextRsrq) + ", \"rssinr\": " + std::to_string(nextRssinr) + "}";
201           const uint8_t *serving_buf = reinterpret_cast<const uint8_t*>(serving_str.c_str());   
202           
203           int numNeighborCells = (all_ues_json[p5]).size();
204           
205           std::string neighbor_str = "[";
206           
207           int nextNbCell;
208           int nextNbRsrp;
209           int nextNbRsrq;
210           int nextNbRssinr;
211           
212           for (int j = 0; j < numNeighborCells; j++) {
213             json::json_pointer p8(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/neighbourCellList/" + std::to_string(j) + "/nbCellIdentity");
214             nextNbCell = all_ues_json[p8].get<int>();
215             //cout << "Neighbor Cell " << all_ues_json[p8] << endl;
216             json::json_pointer p9(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i)
217                                   +"/neighbourCellList/" + std::to_string(j) + "/nbCellRfReport/rsrp");
218             nextNbRsrp = all_ues_json[p9].get<int>();
219             //cout << "  RSRP " << nextNbRsrp << endl;
220             
221             json::json_pointer p10(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i)
222                                    +"/neighbourCellList/" + std::to_string(j) + "/nbCellRfReport/rsrq");
223             nextNbRsrq = all_ues_json[p10].get<int>();
224             //cout << "  RSRQ " << nextNbRsrq << endl;
225             
226             json::json_pointer p11(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i)
227                                    +"/neighbourCellList/" + std::to_string(j) + "/nbCellRfReport/rssinr");
228             nextNbRssinr = all_ues_json[p11].get<int>();
229             //cout << "  RSSINR " << nextNbRssinr << endl;
230             
231             if (j != 0) {
232               neighbor_str += ",";
233               
234             }
235             
236             neighbor_str += "{\"CID\" : \"" + std::to_string(nextNbCell) + "\", \"Cell-RF\" : \"{\"rsrp\": " + std::to_string(nextNbRsrp) +
237               ", \"rsrq\": " + std::to_string(nextNbRsrq) + ", \"rssinr\": " + std::to_string(nextNbRssinr) + "}}";
238             
239           }
240           
241           neighbor_str += "]";
242           
243           fprintf(stderr,"This is neighbor str %s\n", neighbor_str.c_str());
244           
245           //Creating UE-level RAN-Container CUCP message
246           
247           fprintf(stderr,"Creating UE-level RAN-Container CUCP message\n");
248           
249           const uint8_t *neighbor_buf = reinterpret_cast<const uint8_t*>(neighbor_str.c_str());
250           uint8_t *plmnid_buf = (uint8_t*)"747";
251           uint8_t *nrcellid_buf = (uint8_t*)"12340";
252           
253           E2SM_KPM_IndicationMessage_t *ind_msg_cucp_ue =
254             (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
255           
256           encode_kpm_report_rancontainer_cucp_parameterized(ind_msg_cucp_ue, plmnid_buf, nrcellid_buf, crnti_buf, serving_buf, neighbor_buf);
257           
258           uint8_t e2sm_message_buf_cucp_ue[8192];
259           size_t e2sm_message_buf_size_cucp_ue = 8192;
260           
261           asn_codec_ctx_t *opt_cod;
262
263           
264           asn_enc_rval_t er_message_cucp_ue = asn_encode_to_buffer(opt_cod,
265                                                                    ATS_ALIGNED_BASIC_PER,
266                                                                    &asn_DEF_E2SM_KPM_IndicationMessage,
267                                                                    ind_msg_cucp_ue, e2sm_message_buf_cucp_ue, e2sm_message_buf_size_cucp_ue);
268           
269           fprintf(stderr, "er encded is %d\n", er_message_cucp_ue.encoded);
270           fprintf(stderr, "after encoding message\n");
271
272           E2SM_KPM_IndicationHeader_t* ind_header_cucp_ue =
273             (E2SM_KPM_IndicationHeader_t*)calloc(1,sizeof(E2SM_KPM_IndicationHeader_t));
274           encode_e2sm_kpm_indication_header(ind_header_cucp_ue);
275           fprintf(stderr, "Now printing xer outside of function call\n");
276           xer_fprint(stderr, &asn_DEF_E2SM_KPM_IndicationHeader, ind_header_cucp_ue);     
277
278           uint8_t e2sm_header_buf_cucp_ue[8192];
279           size_t e2sm_header_buf_size_cucp_ue = 8192;
280           
281           asn_enc_rval_t er_header_cucp_ue = asn_encode_to_buffer(opt_cod,
282                                                                   ATS_ALIGNED_BASIC_PER,
283                                                                   &asn_DEF_E2SM_KPM_IndicationHeader,
284                                                                   ind_header_cucp_ue, e2sm_header_buf_cucp_ue, e2sm_header_buf_size_cucp_ue);
285
286           fprintf(stderr, "er encded is %d\n", er_header_cucp_ue.encoded);
287           fprintf(stderr, "error buf is %s\n", (char*)e2sm_header_buf_cucp_ue);
288           fprintf(stderr, "after encoding header\n");     
289           
290           E2AP_PDU *pdu_cucp_ue = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
291           
292           encoding::generate_e2apv1_indication_request_parameterized(pdu_cucp_ue, requestorId,
293                                                                      instanceId, ranFunctionId,
294                                                                      actionId, seqNum, e2sm_header_buf_cucp_ue,
295                                                                      er_header_cucp_ue.encoded, e2sm_message_buf_cucp_ue,
296                                                                      er_message_cucp_ue.encoded);
297           
298           
299           e2sim.encode_and_send_sctp_data(pdu_cucp_ue);
300
301           seqNum++;
302
303           std::this_thread::sleep_for (std::chrono::seconds(2));
304           
305           //Creating UE-level RAN-Container CUUP message
306           
307           fprintf(stderr,"Creating UE-level RAN-Container CUUP message\n");     
308           
309           E2SM_KPM_IndicationMessage_t *ind_msg_cuup_ue =
310             (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
311           
312           long bytes = (long)(tput * 100);
313           
314           encode_kpm_report_rancontainer_cuup_parameterized(ind_msg_cuup_ue, plmnid_buf, nrcellid_buf, crnti_buf, bytes, 0);
315           
316           uint8_t e2sm_message_buf_cuup_ue[8192];
317           size_t e2sm_message_buf_size_cuup_ue = 8192;
318           
319           asn_codec_ctx_t *opt_cod2;
320           
321           
322           asn_enc_rval_t er_message_cuup_ue = asn_encode_to_buffer(opt_cod2,
323                                                                ATS_ALIGNED_BASIC_PER,
324                                                                &asn_DEF_E2SM_KPM_IndicationMessage,
325                                                                ind_msg_cuup_ue, e2sm_message_buf_cuup_ue, e2sm_message_buf_size_cuup_ue);
326           
327           fprintf(stderr, "er encded is %d\n", er_message_cuup_ue.encoded);
328           fprintf(stderr, "after encoding message\n");
329
330           E2SM_KPM_IndicationHeader_t* ind_header_cuup_ue =
331             (E2SM_KPM_IndicationHeader_t*)calloc(1,sizeof(E2SM_KPM_IndicationHeader_t));
332           encode_e2sm_kpm_indication_header(ind_header_cuup_ue);
333
334           uint8_t e2sm_header_buf_cuup_ue[8192];
335           size_t e2sm_header_buf_size_cuup_ue = 8192;
336           
337           asn_enc_rval_t er_header_cuup_ue = asn_encode_to_buffer(opt_cod,
338                                                                   ATS_ALIGNED_BASIC_PER,
339                                                                   &asn_DEF_E2SM_KPM_IndicationHeader,
340                                                                   ind_header_cuup_ue, e2sm_header_buf_cuup_ue, e2sm_header_buf_size_cuup_ue);
341           
342           
343           E2AP_PDU *pdu_cuup_ue = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));        
344           
345           encoding::generate_e2apv1_indication_request_parameterized(pdu_cuup_ue, requestorId,
346                                                                      instanceId, ranFunctionId,
347                                                                      actionId, seqNum, e2sm_header_buf_cuup_ue,
348                                                                      er_header_cuup_ue.encoded,
349                                                                      e2sm_message_buf_cuup_ue, er_message_cuup_ue.encoded);
350
351           e2sim.encode_and_send_sctp_data(pdu_cuup_ue);
352
353           seqNum++;
354
355           std::this_thread::sleep_for (std::chrono::seconds(2));
356           
357           //Creating UE-level RAN-Container DU message
358           
359           fprintf(stderr,"Creating UE-level RAN-Container DU message\n");
360           
361           E2SM_KPM_IndicationMessage_t *ind_message_du_ue =
362             (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
363           
364           encode_kpm_report_rancontainer_du_parameterized(ind_message_du_ue,
365                                                           plmnid_buf, nrcellid_buf, crnti_buf, prb_usage, 0);
366           
367           uint8_t e2sm_message_buf_du_ue[8192];
368           size_t e2sm_message_buf_size_du_ue = 8192;
369           
370           asn_codec_ctx_t *opt_cod3;
371           
372           
373           asn_enc_rval_t er_message_du_ue = asn_encode_to_buffer(opt_cod3,
374                                                                  ATS_ALIGNED_BASIC_PER,
375                                                                  &asn_DEF_E2SM_KPM_IndicationMessage,
376                                                                  ind_message_du_ue,
377                                                                  e2sm_message_buf_du_ue,
378                                                                  e2sm_message_buf_size_du_ue);
379           
380           fprintf(stderr, "er encded is %d\n", er_message_du_ue.encoded);
381           fprintf(stderr, "after encoding message\n");
382
383           E2SM_KPM_IndicationHeader_t* ind_header_du_ue =
384             (E2SM_KPM_IndicationHeader_t*)calloc(1,sizeof(E2SM_KPM_IndicationHeader_t));
385           encode_e2sm_kpm_indication_header(ind_header_du_ue);    
386
387           uint8_t e2sm_header_buf_du_ue[8192];
388           size_t e2sm_header_buf_size_du_ue = 8192;
389           
390           asn_enc_rval_t er_header_du_ue = asn_encode_to_buffer(opt_cod,
391                                                                 ATS_ALIGNED_BASIC_PER,
392                                                                 &asn_DEF_E2SM_KPM_IndicationHeader,
393                                                                 ind_header_du_ue, e2sm_header_buf_du_ue,
394                                                                 e2sm_header_buf_size_du_ue);
395           
396           
397           E2AP_PDU *pdu_du_ue = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
398           
399           encoding::generate_e2apv1_indication_request_parameterized(pdu_du_ue, requestorId,
400                                                                      instanceId, ranFunctionId,
401                                                                      actionId, seqNum,
402                                                                      e2sm_header_buf_du_ue, er_header_du_ue.encoded,
403                                                                      e2sm_message_buf_du_ue, er_message_du_ue.encoded);
404
405           e2sim.encode_and_send_sctp_data(pdu_du_ue);
406
407           seqNum++;
408
409           std::this_thread::sleep_for (std::chrono::seconds(2));
410           
411           fprintf(stderr, "done with ue meas report\n");
412         }
413         
414       } else if (first_key.compare("cellMeasReport") == 0) {
415
416         fprintf(stderr, "it is equal to cell meas report\n");
417         
418         int numMeasReports = (all_ues_json["/cellMeasReport/cellMeasReportList"_json_pointer]).size();
419         
420         for (int i = 0; i < numMeasReports; i++) {
421           int nextCellId;
422
423           float bytes_dl;
424           float bytes_ul;
425           int prb_dl;
426           int prb_ul;
427           
428           fprintf(stderr,"UE number %d\n", i);
429           
430           json::json_pointer p0(std::string("/cellMeasReport/cellMeasReportList/") + std::to_string(i) +"/pdcpByteMeasReport/pdcpBytesDl");
431           bytes_dl = all_ues_json[p0].get<float>();
432           fprintf(stderr, "Bytes DL %f\n", bytes_dl);
433
434           json::json_pointer p1(std::string("/cellMeasReport/cellMeasReportList/") + std::to_string(i) +"/pdcpByteMeasReport/pdcpBytesUl");
435           bytes_ul = all_ues_json[p1].get<float>();
436           fprintf(stderr, "Bytes UL %f\n", bytes_ul);     
437           
438           json::json_pointer p2(std::string("/cellMeasReport/cellMeasReportList/") + std::to_string(i) +"/prbMeasReport/availPrbDl");
439           prb_dl = all_ues_json[p2].get<int>();
440           fprintf(stderr, "Avail PRB DL %d\n", prb_dl); 
441
442           json::json_pointer p3(std::string("/cellMeasReport/cellMeasReportList/") + std::to_string(i) +"/prbMeasReport/availPrbUl");
443           prb_ul = all_ues_json[p3].get<int>();
444           fprintf(stderr, "Avail PRB UL %d\n", prb_ul);
445
446           uint8_t *buf = (uint8_t*)"GNBCUUP5";
447           
448           uint8_t *sst_buf = (uint8_t*)"1";
449           uint8_t *sd_buf = (uint8_t*)"100";
450           uint8_t *plmnid_buf = (uint8_t*)"747";
451
452           //Encoding Style 5 Message Body
453           
454           fprintf(stderr, "Encoding Style 5 Message body\n");
455
456           asn_codec_ctx_t *opt_cod;
457
458           E2SM_KPM_IndicationMessage_t *ind_msg_style5 =
459             (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
460           E2AP_PDU *pdu_style5 = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
461           
462           encode_kpm_report_style5_parameterized(ind_msg_style5 , buf, bytes_dl, bytes_ul, sst_buf, sd_buf, plmnid_buf);
463           
464           uint8_t e2sm_message_buf_style5[8192];
465           size_t e2sm_message_buf_size_style5 = 8192;
466           
467           asn_enc_rval_t er_message_style5 = asn_encode_to_buffer(opt_cod,
468                                                                   ATS_ALIGNED_BASIC_PER,
469                                                                   &asn_DEF_E2SM_KPM_IndicationMessage,
470                                                                   ind_msg_style5, e2sm_message_buf_style5, e2sm_message_buf_size_style5);
471           
472           fprintf(stderr, "er encded is %d\n", er_message_style5.encoded);
473           fprintf(stderr, "after encoding message\n");
474           
475           int seqNum0 = 1;
476
477
478           E2SM_KPM_IndicationHeader_t* ind_header_style5 =
479             (E2SM_KPM_IndicationHeader_t*)calloc(1,sizeof(E2SM_KPM_IndicationHeader_t));
480           encode_e2sm_kpm_indication_header(ind_header_style5);
481
482           uint8_t e2sm_header_buf_style5[8192];
483           size_t e2sm_header_buf_size_style5 = 8192;
484           
485           asn_enc_rval_t er_header_style5 = asn_encode_to_buffer(opt_cod,
486                                                                  ATS_ALIGNED_BASIC_PER,
487                                                                  &asn_DEF_E2SM_KPM_IndicationHeader,
488                                                                  ind_header_style5,
489                                                                  e2sm_header_buf_style5, e2sm_header_buf_size_style5);    
490           
491           
492           encoding::generate_e2apv1_indication_request_parameterized(pdu_style5, requestorId,
493                                                                      instanceId, ranFunctionId,
494                                                                      actionId, seqNum0, e2sm_header_buf_style5,
495                                                                      er_header_style5.encoded, e2sm_message_buf_style5,
496                                                                      er_message_style5.encoded);
497
498           e2sim.encode_and_send_sctp_data(pdu_style5);
499
500           seqNum++;
501
502           std::this_thread::sleep_for (std::chrono::seconds(2));          
503           
504
505           //Encoding Style 1 Message Body
506           
507           fprintf(stderr, "Encoding Style 1 Message body\n");     
508           asn_codec_ctx_t *opt_cod2;
509           
510           
511           E2SM_KPM_IndicationMessage_t *ind_message_style1 =
512             (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
513           E2AP_PDU *pdu_style1 = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
514           
515           long fiveqi = 7;
516           
517           uint8_t *nrcellid_buf = (uint8_t*)"12340";
518
519           long l_dl_prbs = prb_dl;
520           long l_ul_prbs = prb_ul;
521           
522           encode_kpm_report_style1_parameterized(ind_message_style1, fiveqi,
523                                                  prb_dl, prb_ul, sst_buf, sd_buf,
524                                                  plmnid_buf, nrcellid_buf, &l_dl_prbs, &l_ul_prbs);
525           
526           uint8_t e2sm_message_buf_style1[8192];
527           size_t e2sm_message_buf_size_style1 = 8192;
528           
529           asn_enc_rval_t er_message_style1 = asn_encode_to_buffer(opt_cod2,
530                                                                   ATS_ALIGNED_BASIC_PER,
531                                                                   &asn_DEF_E2SM_KPM_IndicationMessage,
532                                                                   ind_message_style1,
533                                                                   e2sm_message_buf_style1, e2sm_message_buf_size_style1);
534           
535           fprintf(stderr, "er encded is %d\n", er_message_style1.encoded);
536           fprintf(stderr, "after encoding message\n");
537
538           
539           uint8_t *cpid_buf2 = (uint8_t*)"CPID";
540           
541           fprintf(stderr, "About to encode Indication\n");
542
543           E2SM_KPM_IndicationHeader_t* ind_header_style1 =
544             (E2SM_KPM_IndicationHeader_t*)calloc(1,sizeof(E2SM_KPM_IndicationHeader_t));
545           encode_e2sm_kpm_indication_header(ind_header_style1);
546
547           uint8_t e2sm_header_buf_style1[8192];
548           size_t e2sm_header_buf_size_style1 = 8192;
549           
550           asn_enc_rval_t er_header_style1 = asn_encode_to_buffer(opt_cod,
551                                                                  ATS_ALIGNED_BASIC_PER,
552                                                                  &asn_DEF_E2SM_KPM_IndicationHeader,
553                                                                  ind_header_style1,
554                                                                  e2sm_header_buf_style1, e2sm_header_buf_size_style1);
555           
556           encoding::generate_e2apv1_indication_request_parameterized(pdu_style1, requestorId,
557                                                                      instanceId, ranFunctionId,
558                                                                      actionId, seqNum, e2sm_header_buf_style1,
559                                                                      er_header_style1.encoded,
560                                                                      e2sm_message_buf_style1, er_message_style1.encoded);
561
562           e2sim.encode_and_send_sctp_data(pdu_style1);
563           seqNum++;
564           std::this_thread::sleep_for (std::chrono::seconds(2));          
565           
566         }
567       }                                            
568     }
569   }
570 }
571
572
573 void callback_kpm_subscription_request(E2AP_PDU_t *sub_req_pdu) {
574
575   fprintf(stderr, "Calling callback_kpm_subscription_request\n");
576
577   //Record RIC Request ID
578   //Go through RIC action to be Setup List
579   //Find first entry with REPORT action Type
580   //Record ricActionID
581   //Encode subscription response
582
583   RICsubscriptionRequest_t orig_req =
584     sub_req_pdu->choice.initiatingMessage->value.choice.RICsubscriptionRequest;
585   
586   RICsubscriptionResponse_IEs_t *ricreqid =
587     (RICsubscriptionResponse_IEs_t*)calloc(1, sizeof(RICsubscriptionResponse_IEs_t));
588                                            
589   int count = orig_req.protocolIEs.list.count;
590   int size = orig_req.protocolIEs.list.size;
591   
592   RICsubscriptionRequest_IEs_t **ies = (RICsubscriptionRequest_IEs_t**)orig_req.protocolIEs.list.array;
593
594   fprintf(stderr, "count%d\n", count);
595   fprintf(stderr, "size%d\n", size);
596
597   RICsubscriptionRequest_IEs__value_PR pres;
598
599   long reqRequestorId;
600   long reqInstanceId;
601   long reqActionId;
602
603   std::vector<long> actionIdsAccept;
604   std::vector<long> actionIdsReject;
605
606   for (int i=0; i < count; i++) {
607     RICsubscriptionRequest_IEs_t *next_ie = ies[i];
608     pres = next_ie->value.present;
609     
610     fprintf(stderr, "The next present value %d\n", pres);
611
612     switch(pres) {
613     case RICsubscriptionRequest_IEs__value_PR_RICrequestID:
614       {
615         fprintf(stderr,"in case request id\n"); 
616         RICrequestID_t reqId = next_ie->value.choice.RICrequestID;
617         long requestorId = reqId.ricRequestorID;
618         long instanceId = reqId.ricInstanceID;
619         fprintf(stderr, "requestorId %d\n", requestorId);
620         fprintf(stderr, "instanceId %d\n", instanceId);
621         reqRequestorId = requestorId;
622         reqInstanceId = instanceId;
623
624         break;
625       }
626     case RICsubscriptionRequest_IEs__value_PR_RANfunctionID:
627       {
628         fprintf(stderr,"in case ran func id\n");        
629         break;
630       }
631     case RICsubscriptionRequest_IEs__value_PR_RICsubscriptionDetails:
632       {
633         fprintf(stderr,"in case subscription details\n");
634         RICsubscriptionDetails_t subDetails = next_ie->value.choice.RICsubscriptionDetails;
635         fprintf(stderr,"in case subscription details 1\n");     
636         RICeventTriggerDefinition_t triggerDef = subDetails.ricEventTriggerDefinition;
637         fprintf(stderr,"in case subscription details 2\n");     
638         RICactions_ToBeSetup_List_t actionList = subDetails.ricAction_ToBeSetup_List;
639         fprintf(stderr,"in case subscription details 3\n");
640         //We are ignoring the trigger definition
641
642         //We identify the first action whose type is REPORT
643         //That is the only one accepted; all others are rejected
644         
645         int actionCount = actionList.list.count;
646         fprintf(stderr, "action count%d\n", actionCount);
647
648         auto **item_array = actionList.list.array;
649
650         bool foundAction = false;
651
652         for (int i=0; i < actionCount; i++) {
653
654           auto *next_item = item_array[i];
655           RICactionID_t actionId = ((RICaction_ToBeSetup_ItemIEs*)next_item)->value.choice.RICaction_ToBeSetup_Item.ricActionID;
656           RICactionType_t actionType = ((RICaction_ToBeSetup_ItemIEs*)next_item)->value.choice.RICaction_ToBeSetup_Item.ricActionType;
657
658           if (!foundAction && actionType == RICactionType_report) {
659             reqActionId = actionId;
660             actionIdsAccept.push_back(reqActionId);
661             printf("adding accept\n");
662             foundAction = true;
663           } else {
664             reqActionId = actionId;
665             printf("adding reject\n");
666             actionIdsReject.push_back(reqActionId);
667           }
668         }
669         
670         break;
671       }
672     default:
673       {
674         fprintf(stderr,"in case default\n");    
675         break;
676       }      
677     }
678     
679   }
680
681   fprintf(stderr, "After Processing Subscription Request\n");
682
683   fprintf(stderr, "requestorId %d\n", reqRequestorId);
684   fprintf(stderr, "instanceId %d\n", reqInstanceId);
685
686
687   for (int i=0; i < actionIdsAccept.size(); i++) {
688     fprintf(stderr, "Action ID %d %ld\n", i, actionIdsAccept.at(i));
689     
690   }
691
692   E2AP_PDU *e2ap_pdu = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
693
694   long *accept_array = &actionIdsAccept[0];
695   long *reject_array = &actionIdsReject[0];
696   int accept_size = actionIdsAccept.size();
697   int reject_size = actionIdsReject.size();
698
699   encoding::generate_e2apv1_subscription_response_success(e2ap_pdu, accept_array, reject_array, accept_size, reject_size, reqRequestorId, reqInstanceId);
700
701   e2sim.encode_and_send_sctp_data(e2ap_pdu);
702
703   //Start thread for sending REPORT messages
704
705   //  std::thread loop_thread;
706
707   long funcId = 0;
708
709   run_report_loop(reqRequestorId, reqInstanceId, funcId, reqActionId);
710
711   //  loop_thread = std::thread(&run_report_loop);
712
713 }