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