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