E2AP Abstraction Changes
[ric-app/hw.git] / src / xapp-mgmt / msgs_proc.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  * msgs_proc.cc
21  * Created on: 2019
22  * Author: Ashwin Shridharan, Shraboni Jana
23  */
24
25
26 #include "msgs_proc.hpp"
27
28 /*
29 bool XappMsgHandler::decode_subscription_response(unsigned char* data_buf, size_t data_size){
30
31         subscription_helper subhelper;
32         subscription_response subresponse;
33         bool res = true;
34         E2AP_PDU_t *e2pdu = 0;
35
36         asn_dec_rval_t rval;
37
38         ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, e2pdu);
39
40         rval = asn_decode(0,ATS_ALIGNED_BASIC_PER, &asn_DEF_E2AP_PDU, (void**)&e2pdu, data_buf, data_size);
41         switch(rval.code)
42         {
43                 case RC_OK:
44                            //Put in Subscription Response Object.
45                            //asn_fprint(stdout, &asn_DEF_E2AP_PDU, e2pdu);
46                            break;
47                 case RC_WMORE:
48                                 mdclog_write(MDCLOG_ERR, "RC_WMORE");
49                                 res = false;
50                                 break;
51                 case RC_FAIL:
52                                 mdclog_write(MDCLOG_ERR, "RC_FAIL");
53                                 res = false;
54                                 break;
55                 default:
56                                 break;
57          }
58         ASN_STRUCT_FREE(asn_DEF_E2AP_PDU, e2pdu);
59         return res;
60
61 }*/
62
63 bool  XappMsgHandler::a1_policy_handler(char * message, int *message_len, a1_policy_helper &helper){
64
65   rapidjson::Document doc;
66   if (doc.Parse<kParseStopWhenDoneFlag>(message).HasParseError()){
67     mdclog_write(MDCLOG_ERR, "Error: %s, %d :: Could not decode A1 JSON message %s\n", __FILE__, __LINE__, message);
68     return false;
69   }
70
71   //Extract Operation
72   rapidjson::Pointer temp1("/operation");
73     rapidjson::Value * ref1 = temp1.Get(doc);
74     if (ref1 == NULL){
75       mdclog_write(MDCLOG_ERR, "Error : %s, %d:: Could not extract policy type id from %s\n", __FILE__, __LINE__, message);
76       return false;
77     }
78
79    helper.operation = ref1->GetString();
80
81   // Extract policy id type
82   rapidjson::Pointer temp2("/policy_type_id");
83   rapidjson::Value * ref2 = temp2.Get(doc);
84   if (ref2 == NULL){
85     mdclog_write(MDCLOG_ERR, "Error : %s, %d:: Could not extract policy type id from %s\n", __FILE__, __LINE__, message);
86     return false;
87   }
88    //helper.policy_type_id = ref2->GetString();
89     helper.policy_type_id = to_string(ref2->GetInt());
90
91     // Extract policy instance id
92     rapidjson::Pointer temp("/policy_instance_id");
93     rapidjson::Value * ref = temp.Get(doc);
94     if (ref == NULL){
95       mdclog_write(MDCLOG_ERR, "Error : %s, %d:: Could not extract policy type id from %s\n", __FILE__, __LINE__, message);
96       return false;
97     }
98     helper.policy_instance_id = ref->GetString();
99
100     if (helper.policy_type_id == "1" && helper.operation == "CREATE"){
101         helper.status = "OK";
102         Document::AllocatorType& alloc = doc.GetAllocator();
103
104         Value handler_id;
105         handler_id.SetString(helper.handler_id.c_str(), helper.handler_id.length(), alloc);
106
107         Value status;
108         status.SetString(helper.status.c_str(), helper.status.length(), alloc);
109
110
111         doc.AddMember("handler_id", handler_id, alloc);
112         doc.AddMember("status",status, alloc);
113         doc.RemoveMember("operation");
114         StringBuffer buffer;
115         Writer<StringBuffer> writer(buffer);
116         doc.Accept(writer);
117         strncpy(message,buffer.GetString(), buffer.GetLength());
118         *message_len = buffer.GetLength();
119         return true;
120     }
121     return false;
122  }
123
124
125 //For processing received messages.XappMsgHandler should mention if resend is required or not.
126 void XappMsgHandler::operator()(rmr_mbuf_t *message, bool *resend){
127
128         if (message->len > MAX_RMR_RECV_SIZE){
129                 mdclog_write(MDCLOG_ERR, "Error : %s, %d, RMR message larger than %d. Ignoring ...", __FILE__, __LINE__, MAX_RMR_RECV_SIZE);
130                 return;
131         }
132         a1_policy_helper helper;
133         bool res=false;
134         switch(message->mtype){
135                 //need to fix the health check.
136                 case (RIC_HEALTH_CHECK_REQ):
137                                 message->mtype = RIC_HEALTH_CHECK_RESP;        // if we're here we are running and all is ok
138                                 message->sub_id = -1;
139                                 strncpy( (char*)message->payload, "HELLOWORLD OK\n", rmr_payload_size( message) );
140                                 *resend = true;
141                                 break;
142
143                 case (RIC_SUB_RESP):
144                         mdclog_write(MDCLOG_INFO, "Received subscription message of type = %d", message->mtype);
145                                 unsigned char *me_id;
146                                 if( (me_id = (unsigned char *) malloc( sizeof( unsigned char ) * RMR_MAX_MEID )) == NULL ) {
147                                         mdclog_write(MDCLOG_ERR, "Error :  %s, %d : malloc failed for me_id", __FILE__, __LINE__);
148                                         me_id = rmr_get_meid(message, NULL);
149                                 } else {
150                                         rmr_get_meid(message, me_id);
151                                 }
152                                 if(me_id == NULL){
153                                         mdclog_write(MDCLOG_ERR, " Error :: %s, %d : rmr_get_meid failed me_id is NULL", __FILE__, __LINE__);
154                                         break;
155                                 }
156                                 mdclog_write(MDCLOG_INFO,"RMR Received MEID: %s",me_id);
157                                 if(_ref_sub_handler !=NULL){
158                                         _ref_sub_handler->manage_subscription_response(message->mtype, reinterpret_cast< char const* >(me_id));
159                                 } else {
160                                         mdclog_write(MDCLOG_ERR, " Error :: %s, %d : Subscription handler not assigned in message processor !", __FILE__, __LINE__);
161                                 }
162                                 *resend = false;
163                                 if (me_id != NULL) {
164                                         mdclog_write(MDCLOG_INFO, "Free RMR Received MEID memory: %s(0x%x)", me_id, me_id);
165                                         free(me_id);
166                                 }
167                                 break;
168
169         case A1_POLICY_REQ:
170
171                     mdclog_write(MDCLOG_INFO, "In Message Handler: Received A1_POLICY_REQ.");
172                         helper.handler_id = xapp_id;
173
174                         res = a1_policy_handler((char*)message->payload, &message->len, helper);
175                         if(res){
176                                 message->mtype = A1_POLICY_RESP;        // if we're here we are running and all is ok
177                                 message->sub_id = -1;
178                                 *resend = true;
179                         }
180                         break;
181
182         default:
183                 {
184                         mdclog_write(MDCLOG_ERR, "Error :: Unknown message type %d received from RMR", message->mtype);
185                         *resend = false;
186                 }
187         }
188
189         return;
190
191 };
192
193