beef up the AI/ML framework tests by adding InfluxDB as data source and populate...
[it/test.git] / ric_benchmarking / e2-interface / e2sim / src / base / e2sim.cpp
1 /*****************************************************************************
2 #                                                                            *
3 # Copyright 2020 AT&T Intellectual Property                                  *
4 # Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved.      *
5 # Copyright (c) 2020 HCL Technologies Limited.                               *
6 #                                                                            *
7 # Licensed under the Apache License, Version 2.0 (the "License");            *
8 # you may not use this file except in compliance with the License.           *
9 # You may obtain a copy of the License at                                    *
10 #                                                                            *
11 #      http://www.apache.org/licenses/LICENSE-2.0                            *
12 #                                                                            *
13 # Unless required by applicable law or agreed to in writing, software        *
14 # distributed under the License is distributed on an "AS IS" BASIS,          *
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
16 # See the License for the specific language governing permissions and        *
17 # limitations under the License.                                             *
18 #                                                                            *
19 ******************************************************************************/
20
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <string>
24 #include <iostream>
25 #include <fstream>
26 #include <vector>
27 #include <sys/time.h>
28
29 #include "e2sim.hpp"
30 #include "e2sim_defs.h"
31 #include "e2sim_sctp.hpp"
32 #include "e2ap_message_handler.hpp"
33 #include "encode_e2apv1.hpp"
34
35 using namespace std;
36
37 int client_fd = 0;
38
39 std::unordered_map<long, OCTET_STRING_t*> E2Sim::getRegistered_ran_functions() {
40   return ran_functions_registered;
41 }
42
43 void E2Sim::register_subscription_callback(long func_id, SubscriptionCallback cb) {
44   fprintf(stderr,"%%%%about to register callback for subscription for func_id %d\n", func_id);
45   subscription_callbacks[func_id] = cb;
46   
47 }
48
49 SubscriptionCallback E2Sim::get_subscription_callback(long func_id) {
50   fprintf(stderr, "%%%%we are getting the subscription callback for func id %d\n", func_id);
51   SubscriptionCallback cb;
52
53   try {
54     cb = subscription_callbacks.at(func_id);
55   } catch(const std::out_of_range& e) {
56     throw std::out_of_range("Function ID is not registered");
57   }
58   return cb;
59
60 }
61
62 void E2Sim::register_e2sm(long func_id, OCTET_STRING_t *ostr) {
63
64   //Error conditions:
65   //If we already have an entry for func_id
66   
67   printf("%%%%about to register e2sm func desc for %d\n", func_id);
68
69   ran_functions_registered[func_id] = ostr;
70
71 }
72
73
74 void E2Sim::encode_and_send_sctp_data(E2AP_PDU_t* pdu)
75 {
76   uint8_t       *buf;
77   sctp_buffer_t data;
78
79   int procedureCodeValue = e2ap_asn1c_get_procedureCode(pdu);
80
81   data.len = e2ap_asn1c_encode_pdu(pdu, &buf);
82   memcpy(data.buffer, buf, min(data.len, MAX_SCTP_BUFFER));
83
84   if(procedureCodeValue == ProcedureCode_id_RICindication){
85
86        fprintf(stderr, "RIC Indication Procedurecode : %d \n",procedureCodeValue);
87        struct timeval ts_recv;
88        struct timeval ts_sent;
89        std::fstream io_file;
90
91        sctp_send_data(client_fd, data);
92        gettimeofday(&ts_sent, NULL);
93
94        sctp_buffer_t recv_buf;
95    
96        if(sctp_receive_data(client_fd, recv_buf) > 0){
97    
98             gettimeofday(&ts_recv, NULL);
99
100             E2AP_PDU_t* pdu = (E2AP_PDU_t*)calloc(1, sizeof(E2AP_PDU));
101             ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, pdu);
102             asn_transfer_syntax syntax;
103             syntax = ATS_ALIGNED_BASIC_PER;
104             auto rval = asn_decode(nullptr, syntax, &asn_DEF_E2AP_PDU, (void **) &pdu,recv_buf.buffer, recv_buf.len);
105
106             int control_procedureCode = e2ap_asn1c_get_procedureCode(pdu);
107             fprintf(stderr, "Received Msg procedurecode is: %d\nfull buffer is : %s\n",control_procedureCode, recv_buf.buffer);
108
109             //Received control procedure code is 111 because bouncer xapp not doing any pdu contraction/encode for control msg and its simply sending the msg for round trip calculation
110             if(control_procedureCode == 111 ){
111
112                  io_file.open("e2sim_timestamp.txt", std::ios::in|std::ios::out|std::ios::app);
113                  io_file << "Sent RIC Indication at time: " << (ts_sent.tv_sec * 1000000) + (ts_sent.tv_usec) << std::endl;
114                  fprintf(stderr, "Sent RIC Indication at time: %ld\n" ,((ts_sent.tv_sec * 1000000) + (ts_sent.tv_usec)));
115
116                  io_file << "Received RIC Control Msg at time: " << (ts_recv.tv_sec * 1000000) + (ts_recv.tv_usec) << std::endl;
117                  fprintf(stderr,"Received RIC Control Msg at time: %ld\n" ,((ts_recv.tv_sec * 1000000) + (ts_recv.tv_usec)));
118                  io_file << "Time diff in Microseconds:" << ((ts_recv.tv_sec - ts_sent.tv_sec)*1000000 + (ts_recv.tv_usec - ts_sent.tv_usec)) << std::endl;
119
120                  fprintf(stderr, "Time diff in Microseconds: %ld\n",(((ts_recv.tv_sec - ts_sent.tv_sec)*1000000 + (ts_recv.tv_usec - ts_sent.tv_usec))));
121
122                  io_file.close();
123              }
124
125
126          }
127     }
128   else{
129            sctp_send_data(client_fd, data);
130   }
131   
132 }
133
134
135 void E2Sim::wait_for_sctp_data()
136 {
137   sctp_buffer_t recv_buf;
138   if(sctp_receive_data(client_fd, recv_buf) > 0)
139   {
140     LOG_I("[SCTP] Received new data of size %d", recv_buf.len);
141     e2ap_handle_sctp_data(client_fd, recv_buf, false, this);
142   }
143 }
144
145
146
147 void E2Sim::generate_e2apv1_subscription_response_success(E2AP_PDU *e2ap_pdu, long reqActionIdsAccepted[], long reqActionIdsRejected[], int accept_size, int reject_size, long reqRequestorId, long reqInstanceId) {
148   encoding::generate_e2apv1_subscription_response_success(e2ap_pdu, reqActionIdsAccepted, reqActionIdsRejected, accept_size, reject_size, reqRequestorId, reqInstanceId);
149 }
150
151 void E2Sim::generate_e2apv1_indication_request_parameterized(E2AP_PDU *e2ap_pdu, long requestorId, long instanceId, long ranFunctionId, long actionId, long seqNum, uint8_t *ind_header_buf, int header_length, uint8_t *ind_message_buf, int message_length) {
152   encoding::generate_e2apv1_indication_request_parameterized(e2ap_pdu, requestorId, instanceId, ranFunctionId, actionId, seqNum, ind_header_buf, header_length, ind_message_buf, message_length);
153
154 }
155
156 int E2Sim::run_loop(int argc, char* argv[], int plmnId){
157
158   printf("Start E2 Agent (E2 Simulator)\n");
159
160   ifstream simfile;
161   string line;
162
163   simfile.open("simulation.txt", ios::in);
164
165   if (simfile.is_open()) {
166
167     while (getline(simfile, line)) {
168       cout << line << "\n";
169     }
170
171     simfile.close();
172
173   }
174
175   bool xmlenc = false;
176
177   printf("ip address is:%s\n",argv[1]);
178
179   options_t ops = read_input_options(argc, argv);
180
181   printf("After reading input options\n");
182
183   //E2 Agent will automatically restart upon sctp disconnection
184   //  int server_fd = sctp_start_server(ops.server_ip, ops.server_port);
185
186   client_fd = sctp_start_client(ops.server_ip, ops.server_port);
187   E2AP_PDU_t* pdu_setup = (E2AP_PDU_t*)calloc(1,sizeof(E2AP_PDU));
188
189   printf("After starting client\n");
190   printf("client_fd value is %d\n", client_fd);
191   
192   std::vector<encoding::ran_func_info> all_funcs;
193
194   //Loop through RAN function definitions that are registered
195
196   for (std::pair<long, OCTET_STRING_t*> elem : ran_functions_registered) {
197     printf("looping through ran func\n");
198     encoding::ran_func_info next_func;
199
200     next_func.ranFunctionId = elem.first;
201     next_func.ranFunctionDesc = elem.second;
202     next_func.ranFunctionRev = (long)2;
203     all_funcs.push_back(next_func);
204   }
205     
206   printf("about to call setup request encode\n");
207
208   //int plmnid = plmnId; 
209   fprintf(stderr, "plmn id is : %d\n",plmnId);
210
211   generate_e2apv1_setup_request_parameterized(pdu_setup, all_funcs,plmnId);
212  
213   //generate_e2apv1_setup_request_parameterized(pdu_setup, all_funcs);
214
215   fprintf(stderr,"After generating e2setup req\n");
216
217   xer_fprint(stderr, &asn_DEF_E2AP_PDU, pdu_setup);
218
219   printf("After XER Encoding\n");
220
221   auto buffer_size = MAX_SCTP_BUFFER;
222   unsigned char buffer[MAX_SCTP_BUFFER];
223   
224   sctp_buffer_t data;
225
226   char *error_buf = (char*)calloc(300, sizeof(char));
227   size_t errlen;
228
229   asn_check_constraints(&asn_DEF_E2AP_PDU, pdu_setup, error_buf, &errlen);
230   printf("error length %d\n", errlen);
231   printf("error buf %s\n", error_buf);
232
233   auto er = asn_encode_to_buffer(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, pdu_setup, buffer, buffer_size);
234
235   data.len = er.encoded;
236
237   fprintf(stderr, "er encded is %d\n", er.encoded);
238
239   memcpy(data.buffer, buffer, er.encoded);
240
241   if(sctp_send_data(client_fd, data) > 0) {
242     LOG_I("[SCTP] Sent E2-SETUP-REQUEST");
243   } else {
244     LOG_E("[SCTP] Unable to send E2-SETUP-REQUEST to peer");
245   }
246
247   sctp_buffer_t recv_buf;
248
249   LOG_I("[SCTP] Waiting for SCTP data");
250
251   while(1) //constantly looking for data on SCTP interface
252   {
253     if(sctp_receive_data(client_fd, recv_buf) <= 0)
254       break;
255
256     LOG_I("[SCTP] Received new data of size %d", recv_buf.len);
257
258     e2ap_handle_sctp_data(client_fd, recv_buf, xmlenc, this);
259     if (xmlenc) xmlenc = false;
260   }
261
262   return 0;
263 }