Enhanced SIM for E2AP v1 for TS UC
[sim/e2-interface.git] / e2sim / e2apv1sim / test / Pendulum / e2termination_test.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 <string>
23 #include <iostream>
24
25
26 #include "e2sim_defs.h"
27 #include "asn_e2ap.hpp"
28 #include "e2sim_sctp.hpp"
29 #include "e2ap_message_handler.hpp"
30 #include "e2ap_asn_codec.hpp"
31
32 using namespace std;
33
34 void test_send_X2Setup(int &client_fd)
35 {
36     //Create X2SetupRequest message
37     e2ap_pdu_t* pdu = new_e2ap_pdu();
38     eNB_config cfg;
39
40     e2ap_create_X2SetupRequest(pdu, cfg);
41     e2ap_print_pdu(pdu);
42
43     //Encode into buffer
44     sctp_buffer_t data;
45     e2ap_encode_pdu(pdu, data.buffer, sizeof(data.buffer), data.len);
46
47     //Send to sctp
48     sctp_send_data(client_fd, data);
49     LOG_I("[SCTP] Sent X2 SETUP REQUEST");
50
51     //wait to receive response
52     sctp_buffer_t recv_buf;
53     if(sctp_receive_data(client_fd, recv_buf) > 0)
54     {
55       LOG_I("[SCTP] Received new data of size %d", recv_buf.len);
56       e2ap_handle_sctp_data(client_fd, recv_buf);
57     }
58
59   }
60
61 void test_send_ENDCX2Setup(int &client_fd)
62 {
63   //Create ENDCX2SetupRequest message
64   e2ap_pdu_t* pdu = new_e2ap_pdu();
65   eNB_config cfg;
66
67   e2ap_create_ENDCX2SetupRequest(pdu, cfg);
68   e2ap_print_pdu(pdu);
69
70   //Encode into buffer
71   sctp_buffer_t data;
72   e2ap_encode_pdu(pdu, data.buffer, sizeof(data.buffer), data.len);
73
74   //Send to sctp
75   sctp_send_data(client_fd, data);
76   LOG_I("[SCTP] Sent ENDC X2 SETUP REQUEST");
77
78   //wait to receive response
79   sctp_buffer_t recv_buf;
80   if(sctp_receive_data(client_fd, recv_buf) > 0)
81   {
82     LOG_I("[SCTP] Received new data of size %d", recv_buf.len);
83     e2ap_handle_sctp_data(client_fd, recv_buf);
84   }
85
86 }
87
88 void test_send_RICSubscriptionRequest(int &client_fd)
89 {
90   LOG_I("Test RIC SUBSCRIPTION");
91
92   /* Create RIC SUBSCRITION REQUEST */
93   e2ap_pdu_t* pdu = new_e2ap_pdu();
94
95   RICsubscription_params_t params;
96   params.request_id = 2;
97   params.seq_number = 200;
98   params.ran_func_id = 0;
99   params.event_trigger_def = "hello world";
100
101   RIC_action_t action1(1, RICactionType_report);
102   // RIC_action_t action2(3, RICactionType_insert);
103   // RIC_action_t action3(5, RICactionType_insert);
104   // RIC_action_t action4(7, RICactionType_insert);
105   params.actionList.push_back(action1);
106   // params.actionList.push_back(action2);
107   // params.actionList.push_back(action3);
108   // params.actionList.push_back(action4);
109
110   e2ap_create_RICsubscriptionRequest(pdu, params);
111   e2ap_print_pdu(pdu);
112
113   //Encode into buffer
114   sctp_buffer_t data;
115   e2ap_encode_pdu(pdu, data.buffer, sizeof(data.buffer), data.len);
116
117   //Send to sctp
118   sctp_send_data(client_fd, data);
119   LOG_I("[SCTP] Sent RIC SUBSCRIPTION REQUEST");
120
121   //wait to receive response and indication (if any)
122   sctp_buffer_t recv_buf;
123   LOG_I("[SCTP] Waiting for SCTP data");
124   while(1)
125   {
126     if(sctp_receive_data(client_fd, recv_buf) > 0)
127     {
128       LOG_I("[SCTP] Received new data of size %d", recv_buf.len);
129       e2ap_handle_sctp_data(client_fd, recv_buf);
130     }
131     else
132       break;
133
134   }
135
136
137   return;
138   //==========================================================================
139
140   //decode
141   e2ap_pdu_t* pdu2 = new_e2ap_pdu();
142
143   e2ap_decode_pdu(pdu2, data.buffer, data.len);
144
145   RICsubscription_params_t params2;
146   e2ap_parse_RICsubscriptionRequest(pdu2, params2);
147   printf("request_id = %d\n", params2.request_id);
148   printf("seq_number = %d\n", params2.seq_number);
149   printf("ran_func_id = %d\n", params2.ran_func_id);
150   printf("event = %s\n", &params2.event_trigger_def[0]);
151
152   for( auto const  &a : params.actionList)
153   {
154     printf("action id = %d, action type = %d\n", (int)a.action_id, (int)a.action_type);
155   }
156
157
158   LOG_I("================= RESPONSE ===========================");
159   for(size_t i = 0; i < params2.actionList.size(); i++)
160   {
161     //example logic: admit every other action
162     if(i%2 == 0) {
163       params2.actionList[i].isAdmitted = true;
164     } else {
165       params2.actionList[i].isAdmitted = false;
166       params2.actionList[i].notAdmitted_cause = RICcause_radioNetwork;
167       params2.actionList[i].notAdmitted_subCause = 5;
168     }
169   }
170
171   e2ap_pdu_t* res_pdu = new_e2ap_pdu();
172   e2ap_create_RICsubscriptionResponse(res_pdu, params2);
173
174   e2ap_print_pdu(res_pdu);
175
176   //Encode into buffer
177   sctp_buffer_t data_resp;
178   e2ap_encode_pdu(res_pdu, data_resp.buffer, sizeof(data_resp.buffer), data_resp.len);
179
180   LOG_I("================= FAILURE ===========================");
181   e2ap_pdu_t* fail_pdu = new_e2ap_pdu();
182
183   RICsubscription_params_t params3;
184   e2ap_parse_RICsubscriptionRequest(pdu2, params3);
185
186   for(size_t i = 0; i < params3.actionList.size(); i++)
187   {
188     params3.actionList[i].isAdmitted = false;
189     params3.actionList[i].notAdmitted_cause = RICcause_radioNetwork;
190     params3.actionList[i].notAdmitted_subCause = 5;
191   }
192
193   e2ap_create_RICsubscriptionFailure(fail_pdu, params3);
194   e2ap_print_pdu(fail_pdu);
195
196   //Encode into buffer
197   sctp_buffer_t data_fail;
198   e2ap_encode_pdu(fail_pdu, data_fail.buffer, sizeof(data_fail.buffer), data_fail.len);
199
200 }
201
202 int main(int argc, char* argv[]){
203
204   LOG_I("E2 Termination Test");
205
206   // test_send_RICSubscriptionRequest();
207   // return 0;
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   //---------------------------------------------------
213   // test_send_X2Setup(client_fd);
214   test_send_ENDCX2Setup(client_fd);
215   test_send_RICSubscriptionRequest(client_fd);
216
217   //---------------------------------------------------
218
219   close(client_fd);
220   LOG_I("[SCTP] Connection closed.");
221
222   return 0;
223 }