3f0d0134d9aa3a672d3a84786fa25212326281e3
[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 "Timestamp.h"
33   #include "E2AP-PDU.h"
34   #include "RICsubscriptionRequest.h"
35   #include "RICsubscriptionResponse.h"
36   #include "RICactionType.h"
37   #include "ProtocolIE-Field.h"
38   #include "ProtocolIE-SingleContainer.h"
39   #include "InitiatingMessage.h"
40 }
41
42 #include "kpm_callbacks.hpp"
43 #include "encode_kpm.hpp"
44
45 #include "encode_e2apv1.hpp"
46
47 #include <nlohmann/json.hpp>
48 #include <thread>
49
50
51 using json = nlohmann::json;
52
53 using namespace std;
54 class E2Sim;
55
56
57 E2Sim e2sim;
58
59 int main(int argc, char* argv[]) {
60
61   asn_codec_ctx_t *opt_cod;
62
63   E2SM_KPM_RANfunction_Description_t *ranfunc_desc =
64     (E2SM_KPM_RANfunction_Description_t*)calloc(1,sizeof(E2SM_KPM_RANfunction_Description_t));
65   encode_kpm_function_description(ranfunc_desc);
66
67   uint8_t e2smbuffer[8192];
68   size_t e2smbuffer_size = 8192;
69
70   asn_enc_rval_t er =
71     asn_encode_to_buffer(opt_cod,
72                          ATS_ALIGNED_BASIC_PER,
73                          &asn_DEF_E2SM_KPM_RANfunction_Description,
74                          ranfunc_desc, e2smbuffer, e2smbuffer_size);
75   
76   fprintf(stderr, "er encded is %d\n", er.encoded);
77   fprintf(stderr, "after encoding message\n");
78   fprintf(stderr, "here is encoded message %s\n", e2smbuffer);
79
80   uint8_t *ranfuncdesc = (uint8_t*)calloc(1,er.encoded);
81   memcpy(ranfuncdesc, e2smbuffer, er.encoded);
82
83   printf("this is the char array %s\n", (char*)ranfuncdesc);
84
85   OCTET_STRING_t *ranfunc_ostr = (OCTET_STRING_t*)calloc(1,sizeof(OCTET_STRING_t));
86   ranfunc_ostr->buf = (uint8_t*)calloc(1,er.encoded);
87   ranfunc_ostr->size = er.encoded;
88   memcpy(ranfunc_ostr->buf,e2smbuffer,er.encoded);
89
90   printf("!!!lenth of ranfuncdesc is %d\n", strlen((char*)ranfuncdesc));
91   printf("value of this index is %d\n", ranfuncdesc[0]);
92   printf("value of this index is %d\n", ranfuncdesc[1]);
93   printf("value of this index is %d\n", ranfuncdesc[2]);
94   printf("value of this index is %d\n", ranfuncdesc[3]);
95   printf("value of this index is %d\n", ranfuncdesc[4]);
96   printf("value of this index is %d\n", ranfuncdesc[5]);
97   printf("value of this index is %d\n", ranfuncdesc[6]);
98   printf("value of this index is %d\n", ranfuncdesc[10]);
99   printf("value of this index is %d\n", ranfuncdesc[15]);
100   printf("value of this index is %d\n", ranfuncdesc[100]);
101   printf("value of this index is %d\n", ranfuncdesc[101]);
102   
103   e2sim.register_e2sm(1,ranfunc_ostr);
104   e2sim.register_subscription_callback(1,&callback_kpm_subscription_request);
105
106   e2sim.run_loop(argc, argv);
107
108 }
109
110 void run_report_loop(long requestorId, long instanceId, long ranFunctionId, long actionId) {
111
112   //Process simulation file
113
114   ifstream simfile;
115   string line;
116
117   long seqNum = 1;
118   
119   simfile.open("simulation.txt", ios::in);
120
121   cout << "step1" << endl;
122     
123   std::ifstream ue_stream("ueMeasReport.txt");
124   std::ifstream cell_stream("cellMeasReport.txt");
125
126   json all_ues_json;
127
128   ue_stream  >> all_ues_json;
129
130   json all_cells_json;
131
132   cell_stream >> all_cells_json;
133
134   asn_codec_ctx_t *opt_cod;
135
136   cout << "UE RF Measurements" << endl;
137   cout << "******************" << endl;
138
139   int numMeasReports = (all_ues_json["/ueMeasReport/ueMeasReportList"_json_pointer]).size();
140
141   for (int i = 0; i < numMeasReports; i++) {
142     int nextCellId;
143     int nextRsrp;
144     int nextRsrq;
145     int nextRssinr;
146     cout << "UE number " + i << endl;
147     cout << "**********" << endl;
148     json::json_pointer p1(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/nrCellIdentity");
149     nextCellId = all_ues_json[p1].get<int>();
150     cout << "Serving Cell " << nextCellId << endl;
151     
152     json::json_pointer p2(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/servingCellRfReport/rsrp");
153     nextRsrp = all_ues_json[p2].get<int>();
154     cout << "  RSRP " << nextRsrp << endl;
155     json::json_pointer p3(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/servingCellRfReport/rsrq");
156     nextRsrq = all_ues_json[p3].get<int>();
157     cout << "  RSRQ " << nextRsrq << endl;
158     json::json_pointer p4(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/servingCellRfReport/rssinr");
159     nextRssinr = all_ues_json[p4].get<int>();
160     cout << "  RSSINR " << nextRssinr << endl;
161
162     json::json_pointer p5(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/neighbourCellList");
163
164     int numNeighborCells = (all_ues_json[p5]).size();
165
166
167     //REPORT Message 3 -- Encode and send OCUCP user-level report
168     
169     E2SM_KPM_IndicationMessage_t *ind_msg3 =
170       (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
171     E2AP_PDU *pdu3 = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
172     
173     uint8_t *crnti_buf = (uint8_t*)calloc(1,2);
174
175     if (nextCellId == 0) {
176       uint8_t *buf2 = (uint8_t*)"12";
177       memcpy(crnti_buf, buf2, 2);
178     } else if (nextCellId == 1) {
179       uint8_t *buf2 = (uint8_t*)"22";
180       memcpy(crnti_buf, buf2, 2);
181     }
182
183     std::string serving_str = "{\"rsrp\": " + std::to_string(nextRsrp) + ", \"rsrq\": " +
184       std::to_string(nextRsrq) + ", \"rssinr\": " + std::to_string(nextRssinr) + "}";
185     const uint8_t *serving_buf = reinterpret_cast<const uint8_t*>(serving_str.c_str());
186         
187     std::string neighbor_str = "[";
188
189     int nextNbCell;
190     int nextNbRsrp;
191     int nextNbRsrq;
192     int nextNbRssinr;
193
194     for (int j = 0; j < numNeighborCells; j++) {
195       json::json_pointer p8(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i) +"/neighbourCellList/" + std::to_string(j) + "/nbCellIdentity");
196       nextNbCell = all_ues_json[p8].get<int>();
197       cout << "Neighbor Cell " << all_ues_json[p8] << endl;
198       json::json_pointer p9(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i)
199                             +"/neighbourCellList/" + std::to_string(j) + "/nbCellRfReport/rsrp");
200       nextNbRsrp = all_ues_json[p9].get<int>();
201       cout << "  RSRP " << nextNbRsrp << endl;
202
203       json::json_pointer p10(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i)
204                             +"/neighbourCellList/" + std::to_string(j) + "/nbCellRfReport/rsrq");
205       nextNbRsrq = all_ues_json[p10].get<int>();
206       cout << "  RSRQ " << nextNbRsrq << endl;
207
208       json::json_pointer p11(std::string("/ueMeasReport/ueMeasReportList/") + std::to_string(i)
209                              +"/neighbourCellList/" + std::to_string(j) + "/nbCellRfReport/rssinr");
210       nextNbRssinr = all_ues_json[p11].get<int>();
211       cout << "  RSSINR " << nextNbRssinr << endl;
212
213       if (j != 0) {
214         neighbor_str += ",";
215
216       }
217
218       neighbor_str += "{\"CID\" : \"" + std::to_string(nextNbCell) + "\", \"Cell-RF\" : \"{\"rsrp\": " + std::to_string(nextNbRsrp) +
219         ", \"rsrq\": " + std::to_string(nextNbRsrq) + ", \"rssinr\": " + std::to_string(nextNbRssinr) + "}}";
220       
221     }
222
223     neighbor_str += "]";
224     
225     const uint8_t *neighbor_buf = reinterpret_cast<const uint8_t*>(neighbor_str.c_str());
226     
227     printf("Neighbor string\n%s", neighbor_buf);
228
229     uint8_t *plmnid_buf = (uint8_t*)"747";
230     uint8_t *nrcellid_buf = (uint8_t*)"12340";
231     
232     encode_kpm_report_rancontainer_cucp_parameterized(ind_msg3, plmnid_buf, nrcellid_buf, crnti_buf, serving_buf, neighbor_buf);
233     
234     uint8_t e2smbuffer3[8192];
235     size_t e2smbuffer_size3 = 8192;
236
237     
238     asn_enc_rval_t er3 = asn_encode_to_buffer(opt_cod,
239                                               ATS_ALIGNED_BASIC_PER,
240                                               &asn_DEF_E2SM_KPM_IndicationMessage,
241                                               ind_msg3, e2smbuffer3, e2smbuffer_size3);
242     
243     fprintf(stderr, "er encded is %d\n", er3.encoded);
244     fprintf(stderr, "after encoding message\n");
245     uint8_t *e2smheader_buf3 = (uint8_t*)"header";
246     
247     e2sim.generate_e2apv1_indication_request_parameterized(pdu3, requestorId,
248                                                            instanceId, ranFunctionId,
249                                                            actionId, seqNum, e2smheader_buf3, 6, e2smbuffer3, er3.encoded);
250     
251     e2sim.encode_and_send_sctp_data(pdu3);
252     
253     seqNum++;
254         
255   }
256
257
258   cout << "Cell Measurements" << endl;
259   cout << "******************" << endl;
260
261   int numCellMeasReports = (all_cells_json["/cellMeasReport/cellMeasReportList"_json_pointer]).size();
262
263   uint8_t *sst_buf = (uint8_t*)"1";
264   uint8_t *sd_buf = (uint8_t*)"100";
265   uint8_t *plmnid_buf = (uint8_t*)"747";
266   
267   for (int i = 0; i < numCellMeasReports; i++) {
268
269     int nextCellId;
270     int nextPdcpBytesDL;
271     int nextPdcpBytesUL;
272     int nextPRBBytesDL;
273     int nextPRBBytesUL;
274
275     json::json_pointer p1(std::string("/cellMeasReport/cellMeasReportList/") + std::to_string(i) +"/nrCellIdentity");
276     nextCellId = all_cells_json[p1].get<int>();
277     cout << std::string("Cell number ") << nextCellId << endl;
278     
279     cout << "**********" << endl;
280     
281     json::json_pointer p2(std::string("/cellMeasReport/cellMeasReportList/") + std::to_string(i) +"/pdcpByteMeasReport/pdcpBytesDl");
282     nextPdcpBytesDL = all_cells_json[p2].get<int>();
283     cout << std::string("  PDCP Bytes DL ") << nextPdcpBytesDL << endl;
284
285     json::json_pointer p3(std::string("/cellMeasReport/cellMeasReportList/") + std::to_string(i) +"/pdcpByteMeasReport/pdcpBytesUl");
286     nextPdcpBytesUL = all_cells_json[p3].get<int>();    
287     cout << std::string("  PDCP Bytes UL ") << nextPdcpBytesUL << endl;
288
289     uint8_t *buf = (uint8_t*)"GNBCUUP5";
290     
291     int bytes_dl = nextPdcpBytesDL;
292
293     int bytes_ul = nextPdcpBytesUL;
294
295     //    int bytes_dl = 3905;
296     //    int bytes_ul = 1609321;    
297     
298     E2SM_KPM_IndicationMessage_t *ind_msg2 =
299       (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
300     E2AP_PDU *pdu2 = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
301     
302     encode_kpm_report_style5_parameterized(ind_msg2 , buf, bytes_dl, bytes_ul, sst_buf, sd_buf, plmnid_buf);
303     
304     uint8_t e2smbuffer2[8192];
305     size_t e2smbuffer_size2 = 8192;
306     
307     asn_enc_rval_t er2 = asn_encode_to_buffer(opt_cod,
308                                               ATS_ALIGNED_BASIC_PER,
309                                               &asn_DEF_E2SM_KPM_IndicationMessage,
310                                               ind_msg2, e2smbuffer2, e2smbuffer_size2);
311     
312     fprintf(stderr, "er encded is %d\n", er2.encoded);
313     fprintf(stderr, "after encoding message\n");
314     uint8_t *e2smheader_buf2 = (uint8_t*)"header";
315     
316     e2sim.generate_e2apv1_indication_request_parameterized(pdu2, requestorId,
317                                                            instanceId, ranFunctionId,
318                                                            actionId, seqNum, e2smheader_buf2, 6, e2smbuffer2, er2.encoded);
319     
320     e2sim.encode_and_send_sctp_data(pdu2);
321     
322     seqNum++;
323
324     
325
326     json::json_pointer p4(std::string("/cellMeasReport/cellMeasReportList/") + std::to_string(i) +"/prbMeasReport/availPrbDl");
327     nextPRBBytesDL = all_cells_json[p4].get<int>();    
328     cout << std::string("  PRB Bytes DL ") << all_cells_json[p4] << endl;
329
330     json::json_pointer p5(std::string("/cellMeasReport/cellMeasReportList/") + std::to_string(i) +"/prbMeasReport/availPrbUl");
331     nextPRBBytesUL = all_cells_json[p5].get<int>();
332     cout << std::string("  PRB Bytes UL ") << all_cells_json[p5] << endl;
333
334
335     //REPORT Message 1 -- Encode and send ODU cell-level report
336     
337     E2SM_KPM_IndicationMessage_t *ind_msg1 =
338       (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
339     E2AP_PDU *pdu = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
340     
341     long fiveqi = 7;
342
343     uint8_t *nrcellid_buf = (uint8_t*)"12340";
344     long dl_prbs = nextPRBBytesDL;
345     long ul_prbs = nextPRBBytesUL;
346     
347     encode_kpm_report_style1_parameterized(ind_msg1, fiveqi, dl_prbs, ul_prbs, sst_buf, sd_buf, plmnid_buf, nrcellid_buf, &dl_prbs, &ul_prbs);
348     
349     uint8_t e2smbuffer[8192];
350     size_t e2smbuffer_size = 8192;
351     
352     asn_enc_rval_t er = asn_encode_to_buffer(opt_cod,
353                                              ATS_ALIGNED_BASIC_PER,
354                                              &asn_DEF_E2SM_KPM_IndicationMessage,
355                                              ind_msg1, e2smbuffer, e2smbuffer_size);
356     
357     fprintf(stderr, "er encded is %d\n", er.encoded);
358     fprintf(stderr, "after encoding message\n");
359     uint8_t *e2smheader_buf = (uint8_t*)"header";
360     
361     uint8_t *cpid_buf = (uint8_t*)"CPID";
362     
363     fprintf(stderr, "About to encode Indication\n");
364     e2sim.generate_e2apv1_indication_request_parameterized(pdu, requestorId,
365                                                            instanceId, ranFunctionId,
366                                                            actionId, seqNum, e2smheader_buf, 6, e2smbuffer, er.encoded);
367     
368     e2sim.encode_and_send_sctp_data(pdu);
369     
370     seqNum++;
371     
372   }
373
374
375   /*
376   if (simfile.is_open()) {
377
378     while (getline(simfile, line)) {
379       cout << line << "\n";
380
381       //REPORT Message 1 -- Encode and send ODU cell-level report
382
383       E2SM_KPM_IndicationMessage_t *ind_msg1 =
384         (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
385       E2AP_PDU *pdu = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
386
387       long fiveqi = 7;
388       uint8_t *sst_buf = (uint8_t*)"1";
389       uint8_t *sd_buf = (uint8_t*)"100";
390       uint8_t *plmnid_buf = (uint8_t*)"747";
391       uint8_t *nrcellid_buf = (uint8_t*)"12340";
392       long dl_prbs = 100;
393       long ul_prbs = 50; 
394      
395       encode_kpm_report_style1_parameterized(ind_msg1, fiveqi, dl_prbs, ul_prbs, sst_buf, sd_buf, plmnid_buf, nrcellid_buf, &dl_prbs, &ul_prbs);
396
397       uint8_t e2smbuffer[8192];
398       size_t e2smbuffer_size = 8192;
399       asn_codec_ctx_t *opt_cod;
400
401       asn_enc_rval_t er = asn_encode_to_buffer(opt_cod,
402                                                ATS_ALIGNED_BASIC_PER,
403                                                &asn_DEF_E2SM_KPM_IndicationMessage,
404                                                ind_msg1, e2smbuffer, e2smbuffer_size);
405       
406       fprintf(stderr, "er encded is %d\n", er.encoded);
407       fprintf(stderr, "after encoding message\n");
408       uint8_t *e2smheader_buf = (uint8_t*)"header";
409
410       uint8_t *cpid_buf = (uint8_t*)"CPID";
411
412       fprintf(stderr, "About to encode Indication\n");
413       generate_e2apv1_indication_request_parameterized(pdu, requestorId,
414                                                        instanceId, ranFunctionId,
415                                                        actionId, seqNum, e2smheader_buf, 6, e2smbuffer, er.encoded);
416
417       encode_and_send_sctp_data(pdu, socket_fd);
418       
419       seqNum++;
420
421       //REPORT Message 2 -- Encode and send OCUUP cell-level report
422
423       uint8_t *buf = (uint8_t*)"GNBCUUP5";
424
425       int bytes_dl = 40000;
426       int bytes_ul = 50000;
427
428       E2SM_KPM_IndicationMessage_t *ind_msg2 =
429         (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
430       E2AP_PDU *pdu2 = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
431       
432       encode_kpm_report_style5_parameterized(ind_msg2 , buf, bytes_dl, bytes_ul, sst_buf, sd_buf, plmnid_buf);
433
434       uint8_t e2smbuffer2[8192];
435       size_t e2smbuffer_size2 = 8192;
436
437
438       asn_enc_rval_t er2 = asn_encode_to_buffer(opt_cod,
439                                                ATS_ALIGNED_BASIC_PER,
440                                                &asn_DEF_E2SM_KPM_IndicationMessage,
441                                                ind_msg2, e2smbuffer2, e2smbuffer_size2);
442       
443       fprintf(stderr, "er encded is %d\n", er2.encoded);
444       fprintf(stderr, "after encoding message\n");
445       uint8_t *e2smheader_buf2 = (uint8_t*)"header";
446
447       generate_e2apv1_indication_request_parameterized(pdu2, requestorId,
448                                                        instanceId, ranFunctionId,
449                                                        actionId, seqNum, e2smheader_buf2, 6, e2smbuffer2, er2.encoded);
450
451       encode_and_send_sctp_data(pdu2, socket_fd);
452       
453       seqNum++;
454
455       //REPORT Message 3 -- Encode and send OCUCP user-level report
456
457       E2SM_KPM_IndicationMessage_t *ind_msg3 =
458         (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
459       E2AP_PDU *pdu3 = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
460
461       uint8_t *crnti_buf = (uint8_t*)"12";
462       //      uint8_t *serving_buf = (uint8_t*)"RSRP10";
463       //uint8_t *neighbor_buf = (uint8_t*)"-10,-15";
464       int rsrpServ = 10;
465       int rsrqServ = 0;
466       int rssinrServ = 0;
467
468       std::string serving_str = "{\"rsrp\": " + std::to_string(rsrpServ) + ", \"rsrq\": " +
469         std::to_string(rsrqServ) + ", \"rssinr\": " + std::to_string(rssinrServ) + "}";
470       const uint8_t *serving_buf = reinterpret_cast<const uint8_t*>(serving_str.c_str());
471
472
473       neighbor_cell_entry n_entries[3];
474       n_entries[0] = {"123", 10, 0, 0};
475       n_entries[1] = {"456", 10, 0, 0};
476       n_entries[2] = {"789", 10, 0, 0};
477
478       std::string neighbor_str = "[";
479
480       for (int i=0; i < sizeof(n_entries)/sizeof(n_entries[0]); i++) {
481
482         if (i != 0) {
483           neighbor_str += ",";
484         }
485         neighbor_str += "{\"CID\" : \"" + std::string(n_entries[i].cellid) + "\", \"Cell-RF\" : \"{\"rsrp\": " + std::to_string(n_entries[i].rsrp) +
486           ", \"rsrq\": " + std::to_string(n_entries[i].rsrq) + ", \"rssinr\": " + std::to_string(n_entries[i].rsrp) + "}}";
487       }
488
489       neighbor_str += "]";
490
491       const uint8_t *neighbor_buf = reinterpret_cast<const uint8_t*>(neighbor_str.c_str());
492
493       printf("Neighbor string\n%s", neighbor_buf);
494
495       encode_kpm_report_rancontainer_cucp_parameterized(ind_msg3, plmnid_buf, nrcellid_buf, crnti_buf, serving_buf, neighbor_buf);
496       
497       uint8_t e2smbuffer3[8192];
498       size_t e2smbuffer_size3 = 8192;
499
500       asn_enc_rval_t er3 = asn_encode_to_buffer(opt_cod,
501                                                 ATS_ALIGNED_BASIC_PER,
502                                                 &asn_DEF_E2SM_KPM_IndicationMessage,
503                                                 ind_msg3, e2smbuffer3, e2smbuffer_size3);
504       
505       fprintf(stderr, "er encded is %d\n", er3.encoded);
506       fprintf(stderr, "after encoding message\n");
507       uint8_t *e2smheader_buf3 = (uint8_t*)"header";
508
509       generate_e2apv1_indication_request_parameterized(pdu3, requestorId,
510                                                        instanceId, ranFunctionId,
511                                                        actionId, seqNum, e2smheader_buf3, 6, e2smbuffer3, er3.encoded);
512
513       encode_and_send_sctp_data(pdu3, socket_fd);
514             
515       seqNum++;
516       
517       //Encode and send OCUUP user-level report
518
519
520       
521       //Encode and send ODU user-level report
522
523       
524
525       
526     }
527
528     simfile.close();
529
530   }
531   */
532
533 }
534
535 void callback_kpm_subscription_request(E2AP_PDU_t *sub_req_pdu) {
536
537
538   //Record RIC Request ID
539   //Go through RIC action to be Setup List
540   //Find first entry with REPORT action Type
541   //Record ricActionID
542   //Encode subscription response
543
544   RICsubscriptionRequest_t orig_req =
545     sub_req_pdu->choice.initiatingMessage->value.choice.RICsubscriptionRequest;
546   
547   RICsubscriptionResponse_IEs_t *ricreqid =
548     (RICsubscriptionResponse_IEs_t*)calloc(1, sizeof(RICsubscriptionResponse_IEs_t));
549                                            
550   int count = orig_req.protocolIEs.list.count;
551   int size = orig_req.protocolIEs.list.size;
552   
553   RICsubscriptionRequest_IEs_t **ies = (RICsubscriptionRequest_IEs_t**)orig_req.protocolIEs.list.array;
554
555   fprintf(stderr, "count%d\n", count);
556   fprintf(stderr, "size%d\n", size);
557
558   RICsubscriptionRequest_IEs__value_PR pres;
559
560   long reqRequestorId;
561   long reqInstanceId;
562   long reqActionId;
563
564   std::vector<long> actionIdsAccept;
565   std::vector<long> actionIdsReject;
566
567   for (int i=0; i < count; i++) {
568     RICsubscriptionRequest_IEs_t *next_ie = ies[i];
569     pres = next_ie->value.present;
570     
571     fprintf(stderr, "next present value %d\n", pres);
572
573     switch(pres) {
574     case RICsubscriptionRequest_IEs__value_PR_RICrequestID:
575       {
576         RICrequestID_t reqId = next_ie->value.choice.RICrequestID;
577         long requestorId = reqId.ricRequestorID;
578         long instanceId = reqId.ricInstanceID;
579         fprintf(stderr, "requestorId %d\n", requestorId);
580         fprintf(stderr, "instanceId %d\n", instanceId);
581         reqRequestorId = requestorId;
582         reqInstanceId = instanceId;
583
584         break;
585       }
586     case RICsubscriptionRequest_IEs__value_PR_RANfunctionID:
587       break;
588     case RICsubscriptionRequest_IEs__value_PR_RICsubscriptionDetails:
589       {
590         RICsubscriptionDetails_t subDetails = next_ie->value.choice.RICsubscriptionDetails; 
591         RICeventTriggerDefinition_t triggerDef = subDetails.ricEventTriggerDefinition;
592         RICactions_ToBeSetup_List_t actionList = subDetails.ricAction_ToBeSetup_List;
593
594         //We are ignoring the trigger definition
595
596         //We identify the first action whose type is REPORT
597         //That is the only one accepted; all others are rejected
598         
599         int actionCount = actionList.list.count;
600         fprintf(stderr, "action count%d\n", actionCount);
601
602         auto **item_array = actionList.list.array;
603
604         bool foundAction = false;
605
606         for (int i=0; i < actionCount; i++) {
607
608           auto *next_item = item_array[i];
609           RICactionID_t actionId = ((RICaction_ToBeSetup_ItemIEs*)next_item)->value.choice.RICaction_ToBeSetup_Item.ricActionID;
610           RICactionType_t actionType = ((RICaction_ToBeSetup_ItemIEs*)next_item)->value.choice.RICaction_ToBeSetup_Item.ricActionType;
611
612           if (!foundAction && actionType == RICactionType_report) {
613             reqActionId = actionId;
614             actionIdsAccept.push_back(reqActionId);
615             printf("adding accept\n");
616             foundAction = true;
617           } else {
618             reqActionId = actionId;
619             printf("adding reject\n");
620             actionIdsReject.push_back(reqActionId);
621           }
622         }
623         
624         break;
625       }
626     }
627     
628   }
629
630   fprintf(stderr, "After Processing Subscription Request\n");
631
632   fprintf(stderr, "requestorId %d\n", reqRequestorId);
633   fprintf(stderr, "instanceId %d\n", reqInstanceId);
634
635
636   for (int i=0; i < actionIdsAccept.size(); i++) {
637     fprintf(stderr, "Action ID %d %ld\n", i, actionIdsAccept.at(i));
638     
639   }
640
641   E2AP_PDU *e2ap_pdu = (E2AP_PDU*)calloc(1,sizeof(E2AP_PDU));
642
643   long *accept_array = &actionIdsAccept[0];
644   long *reject_array = &actionIdsReject[0];
645   int accept_size = actionIdsAccept.size();
646   int reject_size = actionIdsReject.size();
647
648   e2sim.generate_e2apv1_subscription_response_success(e2ap_pdu, accept_array, reject_array, accept_size, reject_size, reqRequestorId, reqInstanceId);
649
650   e2sim.encode_and_send_sctp_data(e2ap_pdu);
651
652   //Start thread for sending REPORT messages
653
654   //  std::thread loop_thread;
655
656   long funcId = 1;
657
658   run_report_loop(reqRequestorId, reqInstanceId, funcId, reqActionId);
659
660   //  loop_thread = std::thread(&run_report_loop);
661
662 }