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