Switching to riclibe2ap
[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::a1_policy_handler(char * message, int *message_len, a1_policy_helper &helper){
30
31   rapidjson::Document doc;
32   if (doc.Parse<kParseStopWhenDoneFlag>(message).HasParseError()){
33     mdclog_write(MDCLOG_ERR, "Error: %s, %d :: Could not decode A1 JSON message %s\n", __FILE__, __LINE__, message);
34     return false;
35   }
36
37   //Extract Operation
38   rapidjson::Pointer temp1("/operation");
39     rapidjson::Value * ref1 = temp1.Get(doc);
40     if (ref1 == NULL){
41       mdclog_write(MDCLOG_ERR, "Error : %s, %d:: Could not extract policy type id from %s\n", __FILE__, __LINE__, message);
42       return false;
43     }
44
45    helper.operation = ref1->GetString();
46
47   // Extract policy id type
48   rapidjson::Pointer temp2("/policy_type_id");
49   rapidjson::Value * ref2 = temp2.Get(doc);
50   if (ref2 == NULL){
51     mdclog_write(MDCLOG_ERR, "Error : %s, %d:: Could not extract policy type id from %s\n", __FILE__, __LINE__, message);
52     return false;
53   }
54    //helper.policy_type_id = ref2->GetString();
55     helper.policy_type_id = to_string(ref2->GetInt());
56
57     // Extract policy instance id
58     rapidjson::Pointer temp("/policy_instance_id");
59     rapidjson::Value * ref = temp.Get(doc);
60     if (ref == NULL){
61       mdclog_write(MDCLOG_ERR, "Error : %s, %d:: Could not extract policy type id from %s\n", __FILE__, __LINE__, message);
62       return false;
63     }
64     helper.policy_instance_id = ref->GetString();
65
66     if (helper.policy_type_id == "1" && helper.operation == "CREATE"){
67         helper.status = "OK";
68         Document::AllocatorType& alloc = doc.GetAllocator();
69
70         Value handler_id;
71         handler_id.SetString(helper.handler_id.c_str(), helper.handler_id.length(), alloc);
72
73         Value status;
74         status.SetString(helper.status.c_str(), helper.status.length(), alloc);
75
76
77         doc.AddMember("handler_id", handler_id, alloc);
78         doc.AddMember("status",status, alloc);
79         doc.RemoveMember("operation");
80         StringBuffer buffer;
81         Writer<StringBuffer> writer(buffer);
82         doc.Accept(writer);
83         strncpy(message,buffer.GetString(), buffer.GetLength());
84         *message_len = buffer.GetLength();
85         return true;
86     }
87     return false;
88  }
89
90
91 //For processing received messages.XappMsgHandler should mention if resend is required or not.
92 void XappMsgHandler::operator()(rmr_mbuf_t *message, bool *resend){
93
94         if (message->len > MAX_RMR_RECV_SIZE){
95                 mdclog_write(MDCLOG_ERR, "Error : %s, %d, RMR message larger than %d. Ignoring ...", __FILE__, __LINE__, MAX_RMR_RECV_SIZE);
96                 return;
97         }
98         a1_policy_helper helper;
99         bool res=false;
100         switch(message->mtype){
101                 //need to fix the health check.
102                 case (RIC_HEALTH_CHECK_REQ):
103                                 message->mtype = RIC_HEALTH_CHECK_RESP;        // if we're here we are running and all is ok
104                                 message->sub_id = -1;
105                                 strncpy( (char*)message->payload, "HELLOWORLD OK\n", rmr_payload_size( message) );
106                                 *resend = true;
107                                 break;
108
109                 case (RIC_SUB_RESP):
110                         mdclog_write(MDCLOG_INFO, "Received subscription message of type = %d", message->mtype);
111                                 unsigned char *me_id;
112                                 if( (me_id = (unsigned char *) malloc( sizeof( unsigned char ) * RMR_MAX_MEID )) == NULL ) {
113                                         mdclog_write(MDCLOG_ERR, "Error :  %s, %d : malloc failed for me_id", __FILE__, __LINE__);
114                                         me_id = rmr_get_meid(message, NULL);
115                                 } else {
116                                         rmr_get_meid(message, me_id);
117                                 }
118                                 if(me_id == NULL){
119                                         mdclog_write(MDCLOG_ERR, " Error :: %s, %d : rmr_get_meid failed me_id is NULL", __FILE__, __LINE__);
120                                         break;
121                                 }
122                                 mdclog_write(MDCLOG_INFO,"RMR Received MEID: %s",me_id);
123                                 if(_ref_sub_handler !=NULL){
124                                         _ref_sub_handler->manage_subscription_response(message->mtype, reinterpret_cast< char const* >(me_id));
125                                 } else {
126                                         mdclog_write(MDCLOG_ERR, " Error :: %s, %d : Subscription handler not assigned in message processor !", __FILE__, __LINE__);
127                                 }
128                                 *resend = false;
129                                 if (me_id != NULL) {
130                                         mdclog_write(MDCLOG_INFO, "Free RMR Received MEID memory: %s(0x%x)", me_id, me_id);
131                                         free(me_id);
132                                 }
133                                 break;
134
135         case A1_POLICY_REQ:
136
137                     mdclog_write(MDCLOG_INFO, "In Message Handler: Received A1_POLICY_REQ.");
138                         helper.handler_id = xapp_id;
139
140                         res = a1_policy_handler((char*)message->payload, &message->len, helper);
141                         if(res){
142                                 message->mtype = A1_POLICY_RESP;        // if we're here we are running and all is ok
143                                 message->sub_id = -1;
144                                 *resend = true;
145                         }
146                         break;
147         case RIC_INDICATION:
148
149                 mdclog_write(MDCLOG_INFO, "Received Indication message of type = %d", message->mtype);
150                 //pick the relevant decoding code from test_e2sm.h, section(E2SM, IndicationMessageDecode)
151                 break;
152
153         default:
154                 {
155                         mdclog_write(MDCLOG_ERR, "Error :: Unknown message type %d received from RMR", message->mtype);
156                         *resend = false;
157                 }
158         }
159
160         return;
161
162 };
163
164