ae140f82915c5e3c88fa52f02e92ed6d989d0c8f
[it/test.git] / simulators / e2sim / test / WLG / ric_perf.cpp
1 /*****************************************************************************
2 #                                                                            *
3 # Copyright 2019 AT&T Intellectual Property                                  *
4 # Copyright 2019 Nokia                                                       *
5 #                                                                            *
6 # Licensed under the Apache License, Version 2.0 (the "License");            *
7 # you may not use this file except in compliance with the License.           *
8 # You may obtain a copy of the License at                                    *
9 #                                                                            *
10 #      http://www.apache.org/licenses/LICENSE-2.0                            *
11 #                                                                            *
12 # Unless required by applicable law or agreed to in writing, software        *
13 # distributed under the License is distributed on an "AS IS" BASIS,          *
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
15 # See the License for the specific language governing permissions and        *
16 # limitations under the License.                                             *
17 #                                                                            *
18 ******************************************************************************/
19
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <assert.h>
23
24 #include "e2sim_sctp.hpp"
25 #include "e2ap_message_handler.hpp"
26
27 extern "C" {
28   #include "e2sim_defs.h"
29   #include "E2AP-PDU.h"
30   #include "e2ap_asn1c_codec.h"
31
32   #include "ProtocolIE-Field.h"
33 }
34
35 using namespace std;
36
37 void encode_and_send_sctp_data(E2AP_PDU_t* pdu, int client_fd)
38 {
39   uint8_t       *buf;
40   sctp_buffer_t data;
41
42   data.len = e2ap_asn1c_encode_pdu(pdu, &buf);
43   memcpy(data.buffer, buf, data.len);
44
45   sctp_send_data(client_fd, data);
46 }
47
48 void wait_for_sctp_data(int client_fd)
49 {
50   sctp_buffer_t recv_buf;
51   if(sctp_receive_data(client_fd, recv_buf) > 0)
52   {
53     LOG_I("[SCTP] Received new data of size %d", recv_buf.len);
54     e2ap_handle_sctp_data(client_fd, recv_buf);
55   }
56 }
57
58 void set_seqnum(E2AP_PDU_t* pdu)
59 {
60   InitiatingMessage_t *initiatingMessage = pdu->choice.initiatingMessage;
61   // xer_fprint(stdout, &asn_DEF_InitiatingMessage, (void *)initiatingMessage);
62
63   RICsubscriptionRequest_t *request = &initiatingMessage->value.choice.RICsubscriptionRequest;
64   xer_fprint(stdout, &asn_DEF_RICsubscriptionRequest, (void *)request);
65
66   LOG_I("num of IEs = %d", request->protocolIEs.list.count);
67
68   RICsubscriptionRequest_IEs_t *ie;
69
70   ie = request->protocolIEs.list.array[2];
71
72   xer_fprint(stdout, &asn_DEF_RICsubscription, (void *)ie);
73
74   // e2ap_asn1c_print_pdu(pdu);
75 }
76
77 int subresponse_get_sequenceNum(E2AP_PDU_t* pdu)
78 {
79   SuccessfulOutcome_t *responseMsg = pdu->choice.successfulOutcome;
80
81   RICrequestID_t *requestid;
82
83   int num_IEs = responseMsg->value.choice.RICsubscriptionResponse.protocolIEs.list.count;
84
85   for(int edx = 0; edx < num_IEs; edx++)
86   {
87     RICsubscriptionResponse_IEs_t *memb_ptr =
88       responseMsg->value.choice.RICsubscriptionResponse.protocolIEs.list.array[edx];
89
90     switch(memb_ptr->id)
91     {
92       case (ProtocolIE_ID_id_RICrequestID):
93         requestid = &memb_ptr->value.choice.RICrequestID;
94         // xer_fprint(stdout, &asn_DEF_RICrequestID, (void *)requestid);
95         return requestid->ricRequestSequenceNumber;
96         break;
97     }
98   }
99 }
100
101 void subrequest_set_sequenceNum(E2AP_PDU_t* pdu, int seq)
102 {
103   InitiatingMessage_t *initiatingMessage = pdu->choice.initiatingMessage;
104
105   RICrequestID_t *requestid;
106
107   int num_IEs = initiatingMessage->value.choice.RICsubscriptionRequest.protocolIEs.list.count;
108
109   for(int edx = 0; edx < num_IEs; edx++)
110   {
111     RICsubscriptionRequest_IEs_t *memb_ptr =
112       initiatingMessage->value.choice.RICsubscriptionRequest.protocolIEs.list.array[edx];
113
114     switch(memb_ptr->id)
115     {
116       case (ProtocolIE_ID_id_RICrequestID):
117         requestid = &memb_ptr->value.choice.RICrequestID;
118         requestid->ricRequestSequenceNumber = seq;
119         break;
120     }
121   }
122
123 }
124
125 void subscription_response_get_field(E2AP_PDU_t* pdu)
126 {
127   SuccessfulOutcome_t *responseMsg = pdu->choice.successfulOutcome;
128
129   RICrequestID_t *requestid;
130   RANfunctionID_t * ranfunctionid;
131   RICsubscription_t * ricsubscription;
132
133   int requestID_val, sequenceNum;
134
135   int num_IEs = responseMsg->value.choice.RICsubscriptionResponse.protocolIEs.list.count;
136
137   LOG_I("num of IEs = %d", num_IEs);
138
139   for(int edx = 0; edx < num_IEs; edx++)
140   {
141     RICsubscriptionResponse_IEs_t *memb_ptr =
142       responseMsg->value.choice.RICsubscriptionResponse.protocolIEs.list.array[edx];
143
144     switch(memb_ptr->id)
145     {
146       case (ProtocolIE_ID_id_RICrequestID):
147         requestid = &memb_ptr->value.choice.RICrequestID;
148         xer_fprint(stdout, &asn_DEF_RICrequestID, (void *)requestid);
149         requestID_val = requestid->ricRequestorID;
150         sequenceNum = requestid->ricRequestSequenceNumber;
151         requestid->ricRequestSequenceNumber = 202;
152         break;
153
154       case (ProtocolIE_ID_id_RANfunctionID):
155         ranfunctionid = &memb_ptr->value.choice.RANfunctionID;
156         xer_fprint(stdout, &asn_DEF_RANfunctionID, (void *)ranfunctionid);
157         break;
158
159       // case (ProtocolIE_ID_id_RICsubscription):
160       //   ricsubscription = &memb_ptr->value.choice.RICsubscription;
161       //   xer_fprint(stdout, &asn_DEF_RICsubscription, (void *)ricsubscription);
162       //   break;
163     }
164   }
165
166   LOG_I("Request ID = %d, seq = %d", requestID_val, sequenceNum);
167 }
168
169 void subscription_request_get_field(E2AP_PDU_t* pdu)
170 {
171   InitiatingMessage_t *initiatingMessage = pdu->choice.initiatingMessage;
172
173   RICrequestID_t *requestid;
174   RANfunctionID_t * ranfunctionid;
175   RICsubscription_t * ricsubscription;
176
177   int num_IEs = initiatingMessage->value.choice.RICsubscriptionRequest.protocolIEs.list.count;
178
179   LOG_I("num of IEs = %d", num_IEs);
180
181   for(int edx = 0; edx < num_IEs; edx++)
182   {
183     RICsubscriptionRequest_IEs_t *memb_ptr =
184       initiatingMessage->value.choice.RICsubscriptionRequest.protocolIEs.list.array[edx];
185
186     switch(memb_ptr->id)
187     {
188       case (ProtocolIE_ID_id_RICrequestID):
189         requestid = &memb_ptr->value.choice.RICrequestID;
190         xer_fprint(stdout, &asn_DEF_RICrequestID, (void *)requestid);
191         break;
192
193       case (ProtocolIE_ID_id_RANfunctionID):
194         ranfunctionid = &memb_ptr->value.choice.RANfunctionID;
195         xer_fprint(stdout, &asn_DEF_RANfunctionID, (void *)ranfunctionid);
196         break;
197
198       case (ProtocolIE_ID_id_RICsubscription):
199         ricsubscription = &memb_ptr->value.choice.RICsubscription;
200         xer_fprint(stdout, &asn_DEF_RICsubscription, (void *)ricsubscription);
201         break;
202     }
203   }
204 }
205
206 int main(int argc, char* argv[]){
207   LOG_I("Start RIC Simulator - Performance Test");
208
209   options_t ops = read_input_options(argc, argv);
210   int client_fd = sctp_start_client(ops.server_ip, ops.server_port);
211
212   //1. Send ENDCX2Setup
213   E2AP_PDU_t* pdu_setup = e2ap_xml_to_pdu("E2AP_ENDCX2SetupRequest.xml");
214   // e2ap_asn1c_print_pdu(pdu_setup);
215   encode_and_send_sctp_data(pdu_setup, client_fd);
216   LOG_I("[SCTP] Sent ENDC X2 SETUP REQUEST");
217
218   //2. Receive ENDCX2SetupResponse
219   wait_for_sctp_data(client_fd);
220
221   //3. Send RICSubscriptionRequest
222   E2AP_PDU_t* pdu_sub = e2ap_xml_to_pdu("E2AP_RICsubscriptionRequest_Ashwin.xml");
223   e2ap_asn1c_print_pdu(pdu_sub);
224   encode_and_send_sctp_data(pdu_sub, client_fd);
225   LOG_I("[SCTP] Sent RIC SUBSCRIPTION REQUEST");
226
227
228   //4. Receive RIC SUBSCRIPT RESPONSE
229   int count = 0;
230
231   while(1)
232   {
233     usleep(1000); //sleep for one ms
234
235     sctp_buffer_t recv_buf;
236
237     int res_seq;
238
239     if(sctp_receive_data(client_fd, recv_buf) > 0)
240     {
241       LOG_I("[SCTP] Received new data of size %d", recv_buf.len);
242       // e2ap_handle_sctp_data(client_fd, recv_buf);
243
244       E2AP_PDU_t* res_pdu = new E2AP_PDU_t();
245
246       e2ap_asn1c_decode_pdu(res_pdu, recv_buf.buffer, recv_buf.len);
247
248       int procedureCode = e2ap_asn1c_get_procedureCode(res_pdu);
249       int index = (int)res_pdu->present;
250
251       if(index == E2AP_PDU_PR_successfulOutcome && \
252         procedureCode == ProcedureCode_id_ricSubscription)
253       {
254         res_seq =subresponse_get_sequenceNum(res_pdu);
255         count++;
256
257         LOG_I("Received RIC SUBSCRIPTION RESPONSE, seq = %d, totalCount = %d",\
258                 res_seq, count);
259
260         //Put res_seq in new subscription request
261         E2AP_PDU_t* req_pdu = e2ap_xml_to_pdu("E2AP_RICsubscriptionRequest.xml");
262         subrequest_set_sequenceNum(req_pdu, res_seq);
263         // e2ap_asn1c_print_pdu(req_pdu);
264
265         encode_and_send_sctp_data(req_pdu, client_fd);
266         LOG_I("Send new SUBSCRIPT REQUEST, seq = %d", res_seq);
267       }
268
269     }
270   }
271
272
273   // E2AP_PDU_t* res_pdu = e2ap_xml_to_pdu("E2AP_RICsubscriptionResponse.xml");
274   //
275   // //Extract subscription response sequence number
276   // int res_seq = subresponse_get_sequenceNum(res_pdu);
277   // LOG_I("Subscription Response SeqNo = %d", res_seq);
278   //
279   // //Put responseSeq in new subscription request
280   // E2AP_PDU_t* req_pdu = e2ap_xml_to_pdu("E2AP_RICsubscriptionRequest.xml");
281   //
282   // subrequest_set_sequenceNum(req_pdu, res_seq);
283
284   // e2ap_asn1c_print_pdu(req_pdu);
285
286   return 0;
287 }