Updating Bouncer code for Multiple E2Sim
[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         if (message->len > MAX_RMR_RECV_SIZE){
142                 mdclog_write(MDCLOG_ERR, "Error : %s, %d, RMR message larger than %d. Ignoring ...", __FILE__, __LINE__, MAX_RMR_RECV_SIZE);
143                 return;
144         }
145       //a1_policy_helper helper;
146         bool res=false;
147         E2AP_PDU_t* e2pdu = (E2AP_PDU_t*)calloc(1, sizeof(E2AP_PDU));
148         int num = 0;
149         
150         switch(message->mtype){
151                 //need to fix the health check.
152                 case (RIC_HEALTH_CHECK_REQ):
153                                 message->mtype = RIC_HEALTH_CHECK_RESP;        // if we're here we are running and all is ok
154                                 message->sub_id = -1;
155                                 strncpy( (char*)message->payload, "Bouncer OK\n", rmr_payload_size( message) );
156                                 *resend = true;
157                                 break;
158
159                 case (RIC_SUB_RESP):
160                         mdclog_write(MDCLOG_INFO, "Received subscription message of type = %d", message->mtype);
161                                 unsigned char *me_id;
162                                 if( (me_id = (unsigned char *) malloc( sizeof( unsigned char ) * RMR_MAX_MEID )) == NULL ) {
163                                         mdclog_write(MDCLOG_ERR, "Error :  %s, %d : malloc failed for me_id", __FILE__, __LINE__);
164                                         me_id = rmr_get_meid(message, NULL);
165                                 } else {
166                                         rmr_get_meid(message, me_id);
167                                 }
168                                 if(me_id == NULL){
169                                         mdclog_write(MDCLOG_ERR, " Error :: %s, %d : rmr_get_meid failed me_id is NULL", __FILE__, __LINE__);
170                                         break;
171                                 }
172                                 mdclog_write(MDCLOG_INFO,"RMR Received MEID: %s",me_id);
173                                 if(_ref_sub_handler !=NULL){
174                                         _ref_sub_handler->manage_subscription_response(message->mtype, reinterpret_cast< char const* >(me_id));
175                                 } else {
176                                         mdclog_write(MDCLOG_ERR, " Error :: %s, %d : Subscription handler not assigned in message processor !", __FILE__, __LINE__);
177                                 }
178                                 *resend = false;
179                                 if (me_id != NULL) {
180                                         mdclog_write(MDCLOG_INFO, "Free RMR Received MEID memory: %s(0x%x)", me_id, me_id);
181                                         free(me_id);
182                                 }
183                                 break;
184
185                 case (RIC_INDICATION):
186                         
187                         if(message->mtype == 12050)
188                          {
189                            mdclog_write(MDCLOG_INFO, "Decoding indication for msg = %d", message->mtype);
190
191                            ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, e2pdu);
192                            asn_transfer_syntax syntax;
193                            syntax = ATS_ALIGNED_BASIC_PER;
194
195                            mdclog_write(MDCLOG_INFO, "Data_size = %d",  message->len);
196
197                            auto rval =  asn_decode(nullptr, syntax, &asn_DEF_E2AP_PDU, (void**)&e2pdu, message->payload, message->len);
198
199                            if(rval.code == RC_OK)
200                            {
201                                  mdclog_write(MDCLOG_INFO, "rval.code = %d ", rval.code);
202                            }
203                            else{
204                                  mdclog_write(MDCLOG_ERR, " rval.code = %d ", rval.code);
205                                  break;
206                            }
207
208                            asn_fprint(stdout, &asn_DEF_E2AP_PDU, e2pdu);
209                            mdclog_write(MDCLOG_INFO, "Received indication message of type = %d", message->mtype);
210                            num++;
211                            message->mtype = RIC_CONTROL_REQ;        // if we're here we are running and all is ok
212                            message->sub_id = -1;
213                            strncpy((char*)message->payload, "Bouncer Control OK\n", rmr_payload_size(message));
214                            *resend = true;
215                            ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, e2pdu);
216
217                          } 
218                          mdclog_write(MDCLOG_INFO, "Number of Indications Received = %d", num);
219                          break;
220
221         /*case A1_POLICY_REQ:
222
223                     mdclog_write(MDCLOG_INFO, "In Message Handler: Received A1_POLICY_REQ.");
224                         helper.handler_id = xapp_id;
225
226                         res = a1_policy_handler((char*)message->payload, &message->len, helper);
227                         if(res){
228                                 message->mtype = A1_POLICY_RESP;        // if we're here we are running and all is ok
229                                 message->sub_id = -1;
230                                 *resend = true;
231                         }
232                         break;*/
233
234         default:
235                 {
236                         mdclog_write(MDCLOG_ERR, "Error :: Unknown message type %d received from RMR", message->mtype);
237                         *resend = false;
238                 }
239         }
240
241         return;
242
243 };
244
245