5aa0aebfe0ef4f83f503ca04be53072f2294ddbe
[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[]){
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   options_t ops = read_input_options(argc, argv);
178
179   printf("After reading input options\n");
180
181   //E2 Agent will automatically restart upon sctp disconnection
182   //  int server_fd = sctp_start_server(ops.server_ip, ops.server_port);
183
184   client_fd = sctp_start_client(ops.server_ip, ops.server_port);
185   E2AP_PDU_t* pdu_setup = (E2AP_PDU_t*)calloc(1,sizeof(E2AP_PDU));
186
187   printf("After starting client\n");
188   printf("client_fd value is %d\n", client_fd);
189   
190   std::vector<encoding::ran_func_info> all_funcs;
191
192   //Loop through RAN function definitions that are registered
193
194   for (std::pair<long, OCTET_STRING_t*> elem : ran_functions_registered) {
195     printf("looping through ran func\n");
196     encoding::ran_func_info next_func;
197
198     next_func.ranFunctionId = elem.first;
199     next_func.ranFunctionDesc = elem.second;
200     next_func.ranFunctionRev = (long)2;
201     all_funcs.push_back(next_func);
202   }
203     
204   printf("about to call setup request encode\n");
205   
206   generate_e2apv1_setup_request_parameterized(pdu_setup, all_funcs);
207
208   printf("After generating e2setup req\n");
209
210   xer_fprint(stderr, &asn_DEF_E2AP_PDU, pdu_setup);
211
212   printf("After XER Encoding\n");
213
214   auto buffer_size = MAX_SCTP_BUFFER;
215   unsigned char buffer[MAX_SCTP_BUFFER];
216   
217   sctp_buffer_t data;
218
219   char *error_buf = (char*)calloc(300, sizeof(char));
220   size_t errlen;
221
222   asn_check_constraints(&asn_DEF_E2AP_PDU, pdu_setup, error_buf, &errlen);
223   printf("error length %d\n", errlen);
224   printf("error buf %s\n", error_buf);
225
226   auto er = asn_encode_to_buffer(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, pdu_setup, buffer, buffer_size);
227
228   data.len = er.encoded;
229
230   fprintf(stderr, "er encded is %d\n", er.encoded);
231
232   memcpy(data.buffer, buffer, er.encoded);
233
234   if(sctp_send_data(client_fd, data) > 0) {
235     LOG_I("[SCTP] Sent E2-SETUP-REQUEST");
236   } else {
237     LOG_E("[SCTP] Unable to send E2-SETUP-REQUEST to peer");
238   }
239
240   sctp_buffer_t recv_buf;
241
242   LOG_I("[SCTP] Waiting for SCTP data");
243
244   while(1) //constantly looking for data on SCTP interface
245   {
246     if(sctp_receive_data(client_fd, recv_buf) <= 0)
247       break;
248
249     LOG_I("[SCTP] Received new data of size %d", recv_buf.len);
250
251     e2ap_handle_sctp_data(client_fd, recv_buf, xmlenc, this);
252     if (xmlenc) xmlenc = false;
253   }
254
255   return 0;
256 }