Add User-level metrics
[sim/e2-interface.git] / e2sim / e2apv1sim / e2sim.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 #include <fstream>
25
26
27 #include "e2sim_defs.h"
28 #include "e2sim_sctp.hpp"
29 #include "e2ap_message_handler.hpp"
30 #include "encode_e2apv1.hpp"
31
32 using namespace std;
33
34 int client_fd = 0;
35
36 void encode_and_send_sctp_data(E2AP_PDU_t* pdu)
37 {
38   uint8_t       *buf;
39   sctp_buffer_t data;
40
41   data.len = e2ap_asn1c_encode_pdu(pdu, &buf);
42   memcpy(data.buffer, buf, min(data.len, MAX_SCTP_BUFFER));
43
44   sctp_send_data(client_fd, data);
45 }
46
47 void encode_and_send_sctp_data(E2AP_PDU_t* pdu, int client_fd)
48 {
49   uint8_t       *buf;
50   sctp_buffer_t data;
51
52   printf("Calling encode_and_send_sctp_data\n");
53
54   printf("client_fd is %d\n", client_fd);
55
56   data.len = e2ap_asn1c_encode_pdu(pdu, &buf);
57
58   printf("after encoding pdu\n");
59
60   memcpy(data.buffer, buf, min(data.len, MAX_SCTP_BUFFER));
61
62   printf("calling sctp_send_data\n");
63
64   sctp_send_data(client_fd, data);
65 }
66
67 void wait_for_sctp_data()
68 {
69   sctp_buffer_t recv_buf;
70   if(sctp_receive_data(client_fd, recv_buf) > 0)
71   {
72     LOG_I("[SCTP] Received new data of size %d", recv_buf.len);
73     e2ap_handle_sctp_data(client_fd, recv_buf, false);
74   }
75 }
76
77
78 int main(int argc, char* argv[]){
79
80   printf("Start E2 Agent (E2 Simulator\n");
81
82   ifstream simfile;
83   string line;
84
85   simfile.open("simulation.txt", ios::in);
86
87   if (simfile.is_open()) {
88
89     while (getline(simfile, line)) {
90       cout << line << "\n";
91     }
92
93     simfile.close();
94
95   }
96   
97
98   printf("encoding now the user level - DU\n");
99
100   E2SM_KPM_IndicationMessage_t *indMsg =
101     (E2SM_KPM_IndicationMessage_t*)calloc(1,sizeof(E2SM_KPM_IndicationMessage_t));
102
103   //encode_kpm_report_rancontainer_du(indMsg);
104   encode_kpm_report_style1(indMsg);
105
106   
107
108   bool xmlenc = false;
109
110   options_t ops = read_input_options(argc, argv);
111
112   printf("After reading input options\n");
113
114   //E2 Agent will automatically restart upon sctp disconnection
115   //  int server_fd = sctp_start_server(ops.server_ip, ops.server_port);
116
117   client_fd = sctp_start_client(ops.server_ip, ops.server_port);
118   E2AP_PDU_t* pdu_setup = (E2AP_PDU_t*)calloc(1,sizeof(E2AP_PDU));
119
120   printf("After starting client\n");
121   printf("client_fd value is %d\n", client_fd);
122   
123   //  generate_e2apv1_subscription_request(pdu_setup);
124   generate_e2apv1_setup_request(pdu_setup);
125
126   printf("After generating e2setup req\n");  
127
128   xer_fprint(stderr, &asn_DEF_E2AP_PDU, pdu_setup);
129
130   printf("After XER Encoding\n");
131
132   auto buffer_size = MAX_SCTP_BUFFER;
133   unsigned char buffer[MAX_SCTP_BUFFER];
134   
135   sctp_buffer_t data;
136
137   char *error_buf = (char*)calloc(300, sizeof(char));
138   size_t errlen;
139
140   asn_check_constraints(&asn_DEF_E2AP_PDU, pdu_setup, error_buf, &errlen);
141   printf("error length %d\n", errlen);
142   printf("error buf %s\n", error_buf);
143
144   auto er = asn_encode_to_buffer(nullptr, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, pdu_setup, buffer, buffer_size);
145   //auto er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu_setup, buffer, buffer_size);
146   data.len = er.encoded;
147
148   fprintf(stderr, "er encded is %d\n", er.encoded);
149
150   memcpy(data.buffer, buffer, er.encoded);
151
152   if(sctp_send_data(client_fd, data) > 0) {
153     LOG_I("[SCTP] Sent E2-SETUP-REQUEST");
154   } else {
155     LOG_E("[SCTP] Unable to send E2-SETUP-REQUEST to peer");
156   }
157
158   sctp_buffer_t recv_buf;
159
160   LOG_I("[SCTP] Waiting for SCTP data");
161
162   while(1) //constantly looking for data on SCTP interface
163   {
164     if(sctp_receive_data(client_fd, recv_buf) <= 0)
165       break;
166
167     LOG_I("[SCTP] Received new data of size %d", recv_buf.len);
168
169     e2ap_handle_sctp_data(client_fd, recv_buf, xmlenc);
170     if (xmlenc) xmlenc = false;
171   }
172
173   return 0;
174 }