E2AP Wrapper Implementation
[ric-app/hw.git] / src / xapp-asn / e2ap / e2ap_subsdel_response.cc
1 /*
2 ==================================================================================
3         Copyright (c) 2019-2020 AT&T Intellectual Property.
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================================
17 */
18 /*
19  * e2ap_subsdel_response.cc
20  *
21  * Author: SJana, Ashwin Sridharan
22  */
23
24
25 #include "e2ap_subsdel_response.hpp"
26
27 E2APSubscriptionDeleteResponse::E2APSubscriptionDeleteResponse(SubscriptionDeleteResponseIEs& ieObj){
28
29
30
31   _responseIEs = std::make_unique<SubscriptionDeleteResponseIEs>();
32   *_responseIEs = ieObj;
33
34   _e2ap_pdu_obj = (E2AP_PDU_t * )calloc(1, sizeof(E2AP_PDU_t));
35   assert(_e2ap_pdu_obj != 0);
36
37   _successMsg = (SuccessfulOutcome_t * )calloc(1, sizeof(SuccessfulOutcome_t));
38   assert(_successMsg != 0);
39
40   IE_array = (RICsubscriptionDeleteResponse_IEs_t *)calloc(RIC_SUBDEL_RESPONSE_IES_COUNT, sizeof(RICsubscriptionDeleteResponse_IEs_t));
41   assert(IE_array != 0);
42
43   RICsubscriptionDeleteResponse_t * subDelresponse = &(_successMsg->value.choice.RICsubscriptionDeleteResponse);
44   for(int i = 0; i < RIC_SUBDEL_RESPONSE_IES_COUNT; i++){
45     ASN_SEQUENCE_ADD(&subDelresponse->protocolIEs, &(IE_array[i]));
46   }
47
48 };
49
50 E2APSubscriptionDeleteResponse::E2APSubscriptionDeleteResponse(unsigned char *buf, size_t *size){
51           _e2ap_pdu_obj = 0;
52           _successMsg = 0;
53           IE_array = 0;
54
55           _responseIEs = std::make_unique<SubscriptionDeleteResponseIEs>();
56            bool status =  this->decode(buf, size);
57            if(!status)
58                    throw "E2AP Subscription Delete Response Decode Failed: "+this->get_error();
59 }
60
61 bool E2APSubscriptionDeleteResponse::decode(unsigned char *buf, size_t *size)
62 {
63
64         asn_dec_rval_t dec_res  = asn_decode(0,ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, (void**)&(_e2ap_pdu_obj), buf, *size);
65         if(dec_res.code != RC_OK){
66                                  mdclog_write(MDCLOG_ERR, "Failed to decode: %s","RIC Subscription Delete Response");
67                                  return false;
68         } else {
69                                  mdclog_write(MDCLOG_INFO, "Successfully decoded: %s","RIC Subscription Delete Response");
70         }
71
72     _successMsg = _e2ap_pdu_obj->choice.successfulOutcome;
73     //write the decoding code.
74
75     if (_successMsg == 0){
76         _error_string = "Invalid reference to success message in get fields subscription delete response";
77         return false;
78     }
79
80
81         for(int edx = 0; edx < _successMsg->value.choice.RICsubscriptionDeleteResponse.protocolIEs.list.count; edx++) {
82                 RICsubscriptionDeleteResponse_IEs_t *memb_ptr = _successMsg->value.choice.RICsubscriptionDeleteResponse.protocolIEs.list.array[edx];
83
84   switch(memb_ptr->id)
85     {
86     case (ProtocolIE_ID_id_RICrequestID):
87                 _responseIEs->set_ricRequestorID(memb_ptr->value.choice.RICrequestID.ricRequestorID);
88         _responseIEs->set_ricInstanceID(memb_ptr->value.choice.RICrequestID.ricInstanceID);
89
90         break;
91
92     case (ProtocolIE_ID_id_RANfunctionID):
93                 _responseIEs->set_ranFunctionID(memb_ptr->value.choice.RANfunctionID);
94
95         break;
96     }
97
98 }
99 //    xer_fprint(stdout, &asn_DEF_E2AP_PDU, _e2ap_pdu_obj);
100
101 return true;
102 }
103
104 // Clear assigned protocolIE list from RIC indication IE container
105 E2APSubscriptionDeleteResponse::~E2APSubscriptionDeleteResponse(void){
106
107   mdclog_write(MDCLOG_DEBUG, "Freeing subscription delete request object memory");
108   RICsubscriptionDeleteResponse_t * E2APSubscriptionDeleteResponse = &(_successMsg->value.choice.RICsubscriptionDeleteResponse);
109
110  for(int i = 0; i < E2APSubscriptionDeleteResponse->protocolIEs.list.size; i++){
111     E2APSubscriptionDeleteResponse->protocolIEs.list.array[i] = 0;
112   }
113
114   if (E2APSubscriptionDeleteResponse->protocolIEs.list.size > 0){
115     free(E2APSubscriptionDeleteResponse->protocolIEs.list.array);
116     E2APSubscriptionDeleteResponse->protocolIEs.list.count = 0;
117     E2APSubscriptionDeleteResponse->protocolIEs.list.size = 0;
118     E2APSubscriptionDeleteResponse->protocolIEs.list.array = 0;
119   }
120
121   free(IE_array);
122   free(_successMsg);
123   _e2ap_pdu_obj->choice.successfulOutcome = 0;
124
125   ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, _e2ap_pdu_obj);
126   mdclog_write(MDCLOG_DEBUG, "Freed subscription delete request object memory");
127
128
129 };
130
131
132 bool E2APSubscriptionDeleteResponse::encode(unsigned char *buf, size_t *size){
133
134   _e2ap_pdu_obj->choice.successfulOutcome = _successMsg;
135   _e2ap_pdu_obj->present = E2AP_PDU_PR_successfulOutcome;
136   bool eres = setfields( _successMsg);
137   if(!eres)
138           return eres;
139
140   _successMsg->procedureCode = ProcedureCode_id_RICsubscriptionDelete;
141   _successMsg->criticality = Criticality_reject;
142   _successMsg->value.present = SuccessfulOutcome__value_PR_RICsubscriptionDeleteResponse;
143
144   int ret_constr = asn_check_constraints(&asn_DEF_E2AP_PDU, (void *) _e2ap_pdu_obj, _errbuf, &_errbuf_len);
145   if(ret_constr){
146     _error_string.assign(_errbuf, _errbuf_len);
147     _error_string = "Constraints failed for encoding subscription delete request. Reason = " + _error_string;
148     return false;
149   }
150
151   asn_enc_rval_t res = asn_encode_to_buffer(0, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, _e2ap_pdu_obj, buf, *size);
152
153   if(res.encoded == -1){
154     _error_string.assign(strerror(errno));
155     _error_string = "Error encoding Subscription Delete Request. Reason = " + _error_string;
156     return false;
157   }
158   else {
159     if(*size < res.encoded){
160       std::stringstream ss;
161       ss  <<"Error encoding Subscription Delete Request . Reason =  encoded pdu size " << res.encoded << " exceeds buffer size " << *size << std::endl;
162       _error_string = ss.str();
163       res.encoded = -1;
164       return false;
165     }
166   }
167
168   *size = res.encoded;
169   xer_fprint(stdout, &asn_DEF_E2AP_PDU, _e2ap_pdu_obj);
170
171   return true;
172
173 }
174
175
176 bool  E2APSubscriptionDeleteResponse::setfields( SuccessfulOutcome_t *_successMsg){
177   unsigned int ie_index;
178
179   ie_index = 0;
180   RICsubscriptionDeleteResponse_IEs_t *ies_ricresp = &IE_array[ie_index];
181   ies_ricresp->criticality = Criticality_reject;
182   ies_ricresp->id = ProtocolIE_ID_id_RICrequestID;
183   ies_ricresp->value.present = RICsubscriptionDeleteResponse_IEs__value_PR_RICrequestID;
184   RICrequestID_t *ricrequest_ie = &ies_ricresp->value.choice.RICrequestID;
185   ricrequest_ie->ricRequestorID = this->getIEs().get_ricRequestorID();
186   ricrequest_ie->ricInstanceID = this->getIEs().get_ricInstanceID();
187
188
189   ie_index = 1;
190   RICsubscriptionDeleteResponse_IEs_t *ies_ranfunc = &IE_array[ie_index];
191   ies_ranfunc->criticality = Criticality_reject;
192   ies_ranfunc->id = ProtocolIE_ID_id_RANfunctionID;
193   ies_ranfunc->value.present = RICsubscriptionDeleteResponse_IEs__value_PR_RANfunctionID;
194   RANfunctionID_t *ranfunction_ie = &ies_ranfunc->value.choice.RANfunctionID;
195   *ranfunction_ie = this->getIEs().get_ranFunctionID();
196
197
198   return true;
199 };
200
201
202
203
204