e2954ae6e2dd256bb279c3865699e6adbeadf64b
[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         switch(message->mtype){
148                 //need to fix the health check.
149                 case (RIC_HEALTH_CHECK_REQ):
150                                 message->mtype = RIC_HEALTH_CHECK_RESP;        // if we're here we are running and all is ok
151                                 message->sub_id = -1;
152                                 strncpy( (char*)message->payload, "Bouncer OK\n", rmr_payload_size( message) );
153                                 *resend = true;
154                                 break;
155
156                 case (RIC_SUB_RESP):
157                         mdclog_write(MDCLOG_INFO, "Received subscription message of type = %d", message->mtype);
158                                 unsigned char *me_id;
159                                 if( (me_id = (unsigned char *) malloc( sizeof( unsigned char ) * RMR_MAX_MEID )) == NULL ) {
160                                         mdclog_write(MDCLOG_ERR, "Error :  %s, %d : malloc failed for me_id", __FILE__, __LINE__);
161                                         me_id = rmr_get_meid(message, NULL);
162                                 } else {
163                                         rmr_get_meid(message, me_id);
164                                 }
165                                 if(me_id == NULL){
166                                         mdclog_write(MDCLOG_ERR, " Error :: %s, %d : rmr_get_meid failed me_id is NULL", __FILE__, __LINE__);
167                                         break;
168                                 }
169                                 mdclog_write(MDCLOG_INFO,"RMR Received MEID: %s",me_id);
170                                 if(_ref_sub_handler !=NULL){
171                                         _ref_sub_handler->manage_subscription_response(message->mtype, reinterpret_cast< char const* >(me_id));
172                                 } else {
173                                         mdclog_write(MDCLOG_ERR, " Error :: %s, %d : Subscription handler not assigned in message processor !", __FILE__, __LINE__);
174                                 }
175                                 *resend = false;
176                                 if (me_id != NULL) {
177                                         mdclog_write(MDCLOG_INFO, "Free RMR Received MEID memory: %s(0x%x)", me_id, me_id);
178                                         free(me_id);
179                                 }
180                                 break;
181                 case (RIC_INDICATION):
182                         mdclog_write(MDCLOG_INFO, "Received indication message of type = %d", message->mtype);
183                         message->mtype = RIC_CONTROL_REQ;        // if we're here we are running and all is ok
184                         message->sub_id = -1;
185                         strncpy((char*)message->payload, "Bouncer Control OK\n", rmr_payload_size(message));
186                         *resend = true;
187                         break;
188
189         /*case A1_POLICY_REQ:
190
191                     mdclog_write(MDCLOG_INFO, "In Message Handler: Received A1_POLICY_REQ.");
192                         helper.handler_id = xapp_id;
193
194                         res = a1_policy_handler((char*)message->payload, &message->len, helper);
195                         if(res){
196                                 message->mtype = A1_POLICY_RESP;        // if we're here we are running and all is ok
197                                 message->sub_id = -1;
198                                 *resend = true;
199                         }
200                         break;*/
201
202         default:
203                 {
204                         mdclog_write(MDCLOG_ERR, "Error :: Unknown message type %d received from RMR", message->mtype);
205                         *resend = false;
206                 }
207         }
208
209         return;
210
211 };
212
213