ISSUE ID:- (RICAPP-176).
[ric-app/bouncer.git] / Bouncer / src / xapp-mgmt / msgs_proc.cc
1 /*
2 # ==================================================================================
3 # Copyright (c) 2020 HCL Technologies Limited.
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 #include "msgs_proc.hpp"
20
21
22 bool XappMsgHandler::encode_subscription_delete_request(unsigned char* buffer, size_t *buf_len){
23
24         subscription_helper sub_helper;
25         sub_helper.set_request(0); // requirement of subscription manager ... ?
26         sub_helper.set_function_id(0);
27
28         subscription_delete e2ap_sub_req_del;
29
30           // generate the delete request pdu
31
32           bool res = e2ap_sub_req_del.encode_e2ap_subscription(&buffer[0], buf_len, sub_helper);
33           if(! res){
34             mdclog_write(MDCLOG_ERR, "%s, %d: Error encoding subscription delete request pdu. Reason = %s", __FILE__, __LINE__, e2ap_sub_req_del.get_error().c_str());
35             return false;
36           }
37
38         return true;
39
40 }
41
42 bool XappMsgHandler::decode_subscription_response(unsigned char* data_buf, size_t data_size){
43
44         subscription_helper subhelper;
45         subscription_response subresponse;
46         bool res = true;
47         E2AP_PDU_t *e2pdu = 0;
48
49         asn_dec_rval_t rval;
50
51         ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, e2pdu);
52
53         rval = asn_decode(0,ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, (void**)&e2pdu, data_buf, data_size);
54         switch(rval.code)
55         {
56                 case RC_OK:
57                            //Put in Subscription Response Object.
58                            //asn_fprint(stdout, &asn_DEF_E2AP_PDU, e2pdu);
59                            break;
60                 case RC_WMORE:
61                                 mdclog_write(MDCLOG_ERR, "RC_WMORE");
62                                 res = false;
63                                 break;
64                 case RC_FAIL:
65                                 mdclog_write(MDCLOG_ERR, "RC_FAIL");
66                                 res = false;
67                                 break;
68                 default:
69                                 break;
70          }
71         ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, e2pdu);
72         return res;
73
74 }
75
76 /*bool  XappMsgHandler::a1_policy_handler(char * message, int *message_len, a1_policy_helper &helper){
77
78   rapidjson::Document doc;
79   if (doc.Parse<kParseStopWhenDoneFlag>(message).HasParseError()){
80     mdclog_write(MDCLOG_ERR, "Error: %s, %d :: Could not decode A1 JSON message %s\n", __FILE__, __LINE__, message);
81     return false;
82   }
83
84   //Extract Operation
85   rapidjson::Pointer temp1("/operation");
86     rapidjson::Value * ref1 = temp1.Get(doc);
87     if (ref1 == NULL){
88       mdclog_write(MDCLOG_ERR, "Error : %s, %d:: Could not extract policy type id from %s\n", __FILE__, __LINE__, message);
89       return false;
90     }
91
92    helper.operation = ref1->GetString();
93
94   // Extract policy id type
95   rapidjson::Pointer temp2("/policy_type_id");
96   rapidjson::Value * ref2 = temp2.Get(doc);
97   if (ref2 == NULL){
98     mdclog_write(MDCLOG_ERR, "Error : %s, %d:: Could not extract policy type id from %s\n", __FILE__, __LINE__, message);
99     return false;
100   }
101    //helper.policy_type_id = ref2->GetString();
102     helper.policy_type_id = to_string(ref2->GetInt());
103
104     // Extract policy instance id
105     rapidjson::Pointer temp("/policy_instance_id");
106     rapidjson::Value * ref = temp.Get(doc);
107     if (ref == NULL){
108       mdclog_write(MDCLOG_ERR, "Error : %s, %d:: Could not extract policy type id from %s\n", __FILE__, __LINE__, message);
109       return false;
110     }
111     helper.policy_instance_id = ref->GetString();
112
113     if (helper.policy_type_id == "1" && helper.operation == "CREATE"){
114         helper.status = "OK";
115         Document::AllocatorType& alloc = doc.GetAllocator();
116
117         Value handler_id;
118         handler_id.SetString(helper.handler_id.c_str(), helper.handler_id.length(), alloc);
119
120         Value status;
121         status.SetString(helper.status.c_str(), helper.status.length(), alloc);
122
123
124         doc.AddMember("handler_id", handler_id, alloc);
125         doc.AddMember("status",status, alloc);
126         doc.RemoveMember("operation");
127         StringBuffer buffer;
128         Writer<StringBuffer> writer(buffer);
129         doc.Accept(writer);
130         strncpy(message,buffer.GetString(), buffer.GetLength());
131         *message_len = buffer.GetLength();
132         return true;
133     }
134     return false;
135 }*/
136
137
138 //For processing received messages.XappMsgHandler should mention if resend is required or not.
139 void XappMsgHandler::operator()(rmr_mbuf_t *message, bool *resend)
140 {
141
142         if (message->len > MAX_RMR_RECV_SIZE)
143         {
144                 mdclog_write(MDCLOG_ERR, "Error : %s, %d, RMR message larger than %d. Ignoring ...", __FILE__, __LINE__, MAX_RMR_RECV_SIZE);
145                 return;
146         }
147                 //a1_policy_helper helper;
148         bool res=false;
149         E2AP_PDU_t* e2pdu = (E2AP_PDU_t*)calloc(1, sizeof(E2AP_PDU));
150         int num = 0;
151         
152         switch(message->mtype)
153         {
154                 //need to fix the health check.
155                 case (RIC_HEALTH_CHECK_REQ):
156                         message->mtype = RIC_HEALTH_CHECK_RESP;        // if we're here we are running and all is ok
157                         message->sub_id = -1;
158                         strncpy( (char*)message->payload, "Bouncer OK\n", rmr_payload_size( message) );
159                         *resend = true;
160                         break;
161
162                 case (RIC_SUB_RESP):
163                         mdclog_write(MDCLOG_INFO, "Received subscription message of type = %d", message->mtype);
164                         unsigned char *me_id;
165                         if( (me_id = (unsigned char *) malloc( sizeof( unsigned char ) * RMR_MAX_MEID )) == NULL ) 
166                         {
167                                 mdclog_write(MDCLOG_ERR, "Error :  %s, %d : malloc failed for me_id", __FILE__, __LINE__);
168                                 me_id = rmr_get_meid(message, NULL);
169                         } 
170                         else 
171                         {
172                                 rmr_get_meid(message, me_id);
173                         }
174                         if(me_id == NULL)
175                         {
176                                 mdclog_write(MDCLOG_ERR, " Error :: %s, %d : rmr_get_meid failed me_id is NULL", __FILE__, __LINE__);
177                                 break;
178                         }
179                         mdclog_write(MDCLOG_INFO,"RMR Received MEID: %s",me_id);
180                         if(_ref_sub_handler !=NULL)
181                         {
182                                 _ref_sub_handler->manage_subscription_response(message->mtype, reinterpret_cast< char const* >(me_id));
183                         } 
184                         else 
185                         {
186                                 mdclog_write(MDCLOG_ERR, " Error :: %s, %d : Subscription handler not assigned in message processor !", __FILE__, __LINE__);
187                         }
188                         *resend = false;
189                         if (me_id != NULL) 
190                         {
191                                 mdclog_write(MDCLOG_INFO, "Free RMR Received MEID memory: %s(0x%x)", me_id, me_id);
192                                 free(me_id);
193                         }
194                         break;
195
196                 case (RIC_SUB_DEL_RESP):
197                         mdclog_write(MDCLOG_INFO, "Received subscription delete message of type = %d", message->mtype);
198                         //unsigned char *me_id;
199                         if( (me_id = (unsigned char *) malloc( sizeof( unsigned char ) * RMR_MAX_MEID )) == NULL ) 
200                         {
201                                 mdclog_write(MDCLOG_ERR, "Error :  %s, %d : malloc failed for me_id", __FILE__, __LINE__);
202                                 me_id = rmr_get_meid(message, NULL);
203                         } 
204                         else 
205                                 
206                         {
207                                 rmr_get_meid(message, me_id);
208                         }
209                         if(me_id == NULL)
210                         {
211                                 mdclog_write(MDCLOG_ERR, " Error :: %s, %d : rmr_get_meid failed me_id is NULL", __FILE__, __LINE__);
212                                 break;
213                         }
214                         mdclog_write(MDCLOG_INFO,"RMR Received MEID: %s",me_id);
215                         if(_ref_sub_handler !=NULL)
216                         {
217                                 _ref_sub_handler->manage_subscription_response(message->mtype, reinterpret_cast< char const* >(me_id));
218                         } 
219                         else 
220                         {
221                                 mdclog_write(MDCLOG_ERR, " Error :: %s, %d : Subscription handler not assigned in message processor !", __FILE__, __LINE__);
222                         }
223                         *resend = false;
224                         if (me_id != NULL) 
225                         {
226                                 mdclog_write(MDCLOG_INFO, "Free RMR Received MEID memory: %s(0x%x)", me_id, me_id);
227                                 free(me_id);
228                         }
229                         break;
230
231                 case (RIC_INDICATION):
232                         
233                         if(message->mtype == 12050)
234                         {
235                                 mdclog_write(MDCLOG_INFO, "Decoding indication for msg = %d", message->mtype);
236
237                                 ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, e2pdu);
238                                 asn_transfer_syntax syntax;
239                                 syntax = ATS_ALIGNED_BASIC_PER;
240
241                                 mdclog_write(MDCLOG_INFO, "Data_size = %d",  message->len);
242
243                                 auto rval =  asn_decode(nullptr, syntax, &asn_DEF_E2AP_PDU, (void**)&e2pdu, message->payload, message->len);
244
245                                 if(rval.code == RC_OK)
246                                 {
247                                         mdclog_write(MDCLOG_INFO, "rval.code = %d ", rval.code);
248                                 }
249                                 else
250                                 {
251                                         mdclog_write(MDCLOG_ERR, " rval.code = %d ", rval.code);
252                                         break;
253                                 }
254
255                                 asn_fprint(stdout, &asn_DEF_E2AP_PDU, e2pdu);
256                                 mdclog_write(MDCLOG_INFO, "Received indication message of type = %d", message->mtype);
257                                 num++;
258                                 message->mtype = RIC_CONTROL_REQ;        // if we're here we are running and all is ok
259                                 message->sub_id = -1;
260                                 strncpy((char*)message->payload, "Bouncer Control OK\n", rmr_payload_size(message));
261                                 *resend = true;
262                                 ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, e2pdu);
263
264                          } 
265                         mdclog_write(MDCLOG_INFO, "Number of Indications Received = %d", num);
266                                 break;
267
268                 /*case A1_POLICY_REQ:
269
270                         mdclog_write(MDCLOG_INFO, "In Message Handler: Received A1_POLICY_REQ.");
271                         helper.handler_id = xapp_id;
272
273                         res = a1_policy_handler((char*)message->payload, &message->len, helper);
274                         if(res)
275                         {
276                                 message->mtype = A1_POLICY_RESP;        // if we're here we are running and all is ok
277                                 message->sub_id = -1;
278                                 *resend = true;
279                         }
280                         break;*/
281
282                 default:
283                 {
284                         mdclog_write(MDCLOG_ERR, "Error :: Unknown message type %d received from RMR", message->mtype);
285                         *resend = false;
286                 }
287         }
288
289         return;
290
291 };
292
293