Adding Bouncer code for RIC-Benchmarking
[ric-app/bouncer.git] / Bouncer / src / xapp-asn / e2ap / e2ap_indication.cc
1 /*
2 ==================================================================================
3
4         Copyright (c) 2019-2020 AT&T Intellectual Property.
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  * ric_indication.c
21  *
22  *  Created on: Jul 11, 2019
23  *      Author: sjana, Ashwin Sridharan
24  */
25
26 #include "e2ap_indication.hpp"
27
28 // Set up memory allocations for each IE for encoding
29 // We are responsible for memory management for each IE for encoding
30 // Hence destructor should clear out memory
31 // When decoding, we rely on asn1c macro (ASN_STRUCT_FREE to be called
32 // for releasing memory by external calling function)
33 ric_indication::ric_indication(void){
34
35   e2ap_pdu_obj = 0;
36   e2ap_pdu_obj = (E2AP_PDU_t * )calloc(1, sizeof(E2AP_PDU_t));
37   assert(e2ap_pdu_obj != 0);
38
39   initMsg = 0;
40   initMsg = (InitiatingMessage_t * )calloc(1, sizeof(InitiatingMessage_t));
41   assert(initMsg != 0);
42
43   IE_array = 0;
44   IE_array = (RICindication_IEs_t *)calloc(NUM_INDICATION_IES, sizeof(RICindication_IEs_t));
45   assert(IE_array != 0);
46
47   e2ap_pdu_obj->present = E2AP_PDU_PR_initiatingMessage;
48   e2ap_pdu_obj->choice.initiatingMessage = initMsg;
49
50                        
51   
52     
53 };
54
55
56
57 // Clear assigned protocolIE list from RIC indication IE container
58 ric_indication::~ric_indication(void){
59
60   mdclog_write(MDCLOG_DEBUG, "Freeing E2AP Indication object memory");
61   RICindication_t *ricIndication  = &(initMsg->value.choice.RICindication);
62   for(int i = 0; i < ricIndication->protocolIEs.list.size; i++){
63     ricIndication->protocolIEs.list.array[i] = 0;
64   }
65   if (ricIndication->protocolIEs.list.size > 0){
66     free(ricIndication->protocolIEs.list.array);
67     ricIndication->protocolIEs.list.array = 0;
68     ricIndication->protocolIEs.list.count = 0;
69     ricIndication->protocolIEs.list.size = 0;
70   }
71   
72   free(IE_array);
73   ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, e2ap_pdu_obj);
74   mdclog_write(MDCLOG_DEBUG, "Freed E2AP Indication object mempory");
75 }
76
77
78 bool ric_indication::encode_e2ap_indication(unsigned char *buf, size_t *size, ric_indication_helper & dinput){
79
80   initMsg->procedureCode = ProcedureCode_id_RICindication;
81   initMsg->criticality = Criticality_ignore;
82   initMsg->value.present = InitiatingMessage__value_PR_RICindication;
83
84   bool res;
85   asn_enc_rval_t retval;
86   
87   res = set_fields(initMsg, dinput);
88   if (!res){
89     return false;
90   }
91
92   int ret_constr = asn_check_constraints(&asn_DEF_E2AP_PDU, e2ap_pdu_obj, errbuf, &errbuf_len);
93   if(ret_constr){
94     error_string.assign(&errbuf[0], errbuf_len);
95     error_string = "Error encoding E2AP Indication message. Reason = " + error_string;
96     return false;
97   }
98
99   // std::cout <<"Constraint check ok ...." << std::endl;
100   // xer_fprint(stdout, &asn_DEF_E2AP_PDU, e2ap_pdu_obj);
101   
102   retval = asn_encode_to_buffer(0, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, e2ap_pdu_obj, buf, *size);
103   if(retval.encoded == -1){
104     error_string.assign(strerror(errno));
105     return false;
106   }
107
108   else {
109     if(*size < retval.encoded){
110       std::stringstream ss;
111       ss  <<"Error encoding E2AP Indication . Reason =  encoded pdu size " << retval.encoded << " exceeds buffer size " << *size << std::endl;
112       error_string = ss.str();
113       return false;
114     }
115   }
116
117   *size = retval.encoded;
118   return true;
119   
120 }
121
122 bool ric_indication::set_fields(InitiatingMessage_t *initMsg, ric_indication_helper &dinput){
123   unsigned int ie_index;
124
125   if (initMsg == 0){
126     error_string = "Invalid reference for E2AP Indication message in set_fields";
127     return false;
128   }
129   
130   
131   RICindication_t * ric_indication = &(initMsg->value.choice.RICindication);
132   ric_indication->protocolIEs.list.count = 0;
133   
134   ie_index = 0;
135   
136   RICindication_IEs_t *ies_ricreq = &IE_array[ie_index];
137   ies_ricreq->criticality = Criticality_reject;
138   ies_ricreq->id = ProtocolIE_ID_id_RICrequestID;
139   ies_ricreq->value.present = RICindication_IEs__value_PR_RICrequestID;
140   RICrequestID_t *ricrequest_ie = &ies_ricreq->value.choice.RICrequestID;
141   ricrequest_ie->ricRequestorID = dinput.req_id;
142   //ricrequest_ie->ricRequestSequenceNumber = dinput.req_seq_no;
143   ASN_SEQUENCE_ADD(&(ric_indication->protocolIEs), &(IE_array[ie_index]));
144  
145   ie_index = 1;
146   RICindication_IEs_t *ies_ranfunc = &IE_array[ie_index];
147   ies_ranfunc->criticality = Criticality_reject;
148   ies_ranfunc->id = ProtocolIE_ID_id_RANfunctionID;
149   ies_ranfunc->value.present = RICindication_IEs__value_PR_RANfunctionID;
150   RANfunctionID_t *ranfunction_ie = &ies_ranfunc->value.choice.RANfunctionID;
151   *ranfunction_ie = dinput.func_id;
152   ASN_SEQUENCE_ADD(&(ric_indication->protocolIEs), &(IE_array[ie_index]));
153
154   ie_index = 2;
155   RICindication_IEs_t *ies_actid = &IE_array[ie_index];
156   ies_actid->criticality = Criticality_reject;
157   ies_actid->id = ProtocolIE_ID_id_RICactionID;
158   ies_actid->value.present = RICindication_IEs__value_PR_RICactionID;
159   RICactionID_t *ricaction_ie = &ies_actid->value.choice.RICactionID;
160   *ricaction_ie = dinput.action_id;
161   ASN_SEQUENCE_ADD(&(ric_indication->protocolIEs), &(IE_array[ie_index]));
162
163   ie_index = 3;
164   RICindication_IEs_t *ies_ricsn = &IE_array[ie_index];
165   ies_ricsn->criticality = Criticality_reject;
166   ies_ricsn->id = ProtocolIE_ID_id_RICindicationSN;
167   ies_ricsn->value.present = RICindication_IEs__value_PR_RICindicationSN;
168   RICindicationSN_t *ricsn_ie = &ies_ricsn->value.choice.RICindicationSN;
169   *ricsn_ie = dinput.indication_sn;
170   ASN_SEQUENCE_ADD(&(ric_indication->protocolIEs), &(IE_array[ie_index]));
171
172
173   ie_index = 4;
174   RICindication_IEs_t *ies_indtyp = &IE_array[ie_index];
175   ies_indtyp->criticality = Criticality_reject;
176   ies_indtyp->id = ProtocolIE_ID_id_RICindicationType;
177   ies_indtyp->value.present = RICindication_IEs__value_PR_RICindicationType;
178   RICindicationType_t *rictype_ie = &ies_indtyp->value.choice.RICindicationType;
179   *rictype_ie = dinput.indication_type;
180   ASN_SEQUENCE_ADD(&(ric_indication->protocolIEs), &(IE_array[ie_index]));
181
182   ie_index = 5;
183   RICindication_IEs_t *ies_richead = &IE_array[ie_index];
184   ies_richead->criticality = Criticality_reject;
185   ies_richead->id = ProtocolIE_ID_id_RICindicationHeader;
186   ies_richead->value.present = RICindication_IEs__value_PR_RICindicationHeader;
187   RICindicationHeader_t *richeader_ie = &ies_richead->value.choice.RICindicationHeader;
188   richeader_ie->buf = dinput.indication_header;
189   richeader_ie->size = dinput.indication_header_size;
190   ASN_SEQUENCE_ADD(&(ric_indication->protocolIEs), &(IE_array[ie_index]));
191   
192   ie_index = 6;
193   RICindication_IEs_t *ies_indmsg = &IE_array[ie_index];
194   ies_indmsg->criticality = Criticality_reject;
195   ies_indmsg->id = ProtocolIE_ID_id_RICindicationMessage;
196   ies_indmsg->value.present = RICindication_IEs__value_PR_RICindicationMessage;
197   RICindicationMessage_t *ricmsg_ie = &ies_indmsg->value.choice.RICindicationMessage;
198   ricmsg_ie->buf = dinput.indication_msg;
199   ricmsg_ie->size = dinput.indication_msg_size;
200   ASN_SEQUENCE_ADD(&(ric_indication->protocolIEs), &(IE_array[ie_index]));
201
202
203   // optional call process id ..
204   if (dinput.call_process_id_size > 0){
205     ie_index = 7;
206     RICindication_IEs_t *ies_ind_callprocessid = &IE_array[ie_index];
207     ies_ind_callprocessid->criticality = Criticality_reject;
208     ies_ind_callprocessid->id = ProtocolIE_ID_id_RICcallProcessID;
209     ies_ind_callprocessid->value.present = RICindication_IEs__value_PR_RICcallProcessID;
210     RICcallProcessID_t *riccallprocessid_ie = &ies_ind_callprocessid->value.choice.RICcallProcessID;
211     riccallprocessid_ie->buf = dinput.indication_msg;
212     riccallprocessid_ie->size = dinput.indication_msg_size;
213     ASN_SEQUENCE_ADD(&(ric_indication->protocolIEs), &(IE_array[ie_index]));
214   }
215   
216   return true;
217
218 };
219
220   
221
222
223 bool ric_indication:: get_fields(InitiatingMessage_t * init_msg,  ric_indication_helper &dout)
224 {
225   if (init_msg == 0){
226     error_string = "Invalid reference for E2AP Indication message in get_fields";
227     return false;
228   }
229   
230  
231   for(int edx = 0; edx < init_msg->value.choice.RICindication.protocolIEs.list.count; edx++) {
232     RICindication_IEs_t *memb_ptr = init_msg->value.choice.RICindication.protocolIEs.list.array[edx];
233     
234     switch(memb_ptr->id)
235       {
236       case (ProtocolIE_ID_id_RICindicationHeader):
237         dout.indication_header = memb_ptr->value.choice.RICindicationHeader.buf;
238         dout.indication_header_size = memb_ptr->value.choice.RICindicationHeader.size;
239         break;
240         
241       case (ProtocolIE_ID_id_RICindicationMessage):
242         dout.indication_msg =  memb_ptr->value.choice.RICindicationMessage.buf;
243         dout.indication_msg_size = memb_ptr->value.choice.RICindicationMessage.size;
244         break;
245             
246       case (ProtocolIE_ID_id_RICrequestID):
247         dout.req_id = memb_ptr->value.choice.RICrequestID.ricRequestorID;
248         //dout.req_seq_no = memb_ptr->value.choice.RICrequestID.ricRequestSequenceNumber;
249         break;
250         
251       case (ProtocolIE_ID_id_RANfunctionID):
252         dout.func_id = memb_ptr->value.choice.RANfunctionID;
253         break;
254         
255       case (ProtocolIE_ID_id_RICindicationSN):
256         dout.indication_sn = memb_ptr->value.choice.RICindicationSN;
257         break;
258         
259       case (ProtocolIE_ID_id_RICindicationType):
260         dout.indication_type = memb_ptr->value.choice.RICindicationType;
261         break;
262         
263       case (ProtocolIE_ID_id_RICactionID):
264         dout.action_id = memb_ptr->value.choice.RICactionID;
265         break;
266
267       case (ProtocolIE_ID_id_RICcallProcessID):
268         dout.call_process_id = memb_ptr->value.choice.RICcallProcessID.buf;
269         dout.call_process_id_size = memb_ptr->value.choice.RICcallProcessID.size;
270         
271       default:
272         break;
273       }
274     
275   }
276   
277   return true;
278
279 }
280
281 InitiatingMessage_t * ric_indication::get_message(void)  {
282     return initMsg;
283 }