2 ==================================================================================
4 Copyright (c) 2018-2019 AT&T Intellectual Property.
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
10 http://www.apache.org/licenses/LICENSE-2.0
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 ==================================================================================
22 * Author: Ashwin Shridharan, Shraboni Jana
26 #include "msgs_proc.hpp"
28 //sending messages are encoded.
29 bool XappMsgHandler::encode_subscription_request(unsigned char* buffer, size_t *buf_len)
31 int request_id = 2; // will be over-written by subscription handler
36 int subsequent_action = 0; // continue
37 int time_to_wait = 4; // 10ms
40 int procedure_code = 27;
41 std::string egnb_id = "Testgnb";
42 std::string plmn_id = "Testplmn";
44 unsigned char event_buf[128];
45 size_t event_buf_len = 128;
49 e2sm_event_trigger_helper trigger_data;
50 e2sm_event_trigger event_trigger;
52 trigger_data.egNB_id = egnb_id;
53 trigger_data.plmn_id = plmn_id;
54 trigger_data.egNB_id_type = 2;
55 trigger_data.interface_direction = 1;
56 trigger_data.procedure_code = procedure_code;
57 trigger_data.message_type = message_type;
58 //======================================================
60 // Encode the event trigger definition
61 res = event_trigger.encode_event_trigger(&event_buf[0], &event_buf_len, trigger_data);
63 mdclog_write(MDCLOG_ERR, "Error : %s, %d: Could not encode subscription Request. Reason = %s\n", __FILE__, __LINE__, event_trigger.get_error().c_str());
66 mdclog_write(MDCLOG_INFO, "Encoded event trigger definition into PDU of size %lu bytes\n", event_buf_len);
69 // create the subscription
70 subscription_helper subscr_req;
71 subscription_request e2ap_sub_req;
74 subscr_req.set_request(request_id, req_seq);
75 subscr_req.set_function_id(function_id);
76 subscr_req.add_action(action_id, action_type, "", subsequent_action, time_to_wait);
78 subscr_req.set_event_def(&event_buf[0], event_buf_len);
79 // generate the request pdu
80 res = e2ap_sub_req.encode_e2ap_subscription(&buffer[0], buf_len, subscr_req);
82 mdclog_write(MDCLOG_ERR, "%s, %d: Error encoding subscription pdu. Reason = ", __FILE__, __LINE__);
87 bool XappMsgHandler::encode_subscription_delete_request(unsigned char* buffer, size_t *buf_len){
89 subscription_helper sub_helper;
90 sub_helper.set_request(0, 0); // requirement of subscription manager ... ?
91 sub_helper.set_function_id(0);
93 subscription_delete e2ap_sub_req_del;
95 // generate the delete request pdu
97 bool res = e2ap_sub_req_del.encode_e2ap_subscription(&buffer[0], buf_len, sub_helper);
99 mdclog_write(MDCLOG_ERR, "%s, %d: Error encoding subscription delete request pdu. Reason = %s", __FILE__, __LINE__, e2ap_sub_req_del.get_error().c_str());
107 bool XappMsgHandler::decode_subscription_response(unsigned char* data_buf, size_t data_size){
110 E2N_E2AP_PDU_t *e2pdu = 0;
114 ASN_STRUCT_RESET(asn_DEF_E2N_E2AP_PDU, e2pdu);
116 rval = asn_decode(0,ATS_ALIGNED_BASIC_PER, &asn_DEF_E2N_E2AP_PDU, (void**)&e2pdu, data_buf, data_size);
120 //Put in Subscription Response Object.
121 asn_fprint(stdout, &asn_DEF_E2N_E2AP_PDU, e2pdu);
125 mdclog_write(MDCLOG_ERR, "RC_WMORE");
129 mdclog_write(MDCLOG_ERR, "RC_FAIL");
135 ASN_STRUCT_FREE(asn_DEF_E2N_E2AP_PDU, e2pdu);
139 bool XappMsgHandler::decode_subscription_delete_response(unsigned char* data_buf, size_t data_size){
142 E2N_E2AP_PDU_t *e2pdu = 0;
146 ASN_STRUCT_RESET(asn_DEF_E2N_E2AP_PDU, e2pdu);
148 rval = asn_decode(0,ATS_ALIGNED_BASIC_PER, &asn_DEF_E2N_E2AP_PDU, (void**)&e2pdu, data_buf, data_size);
152 //Put in Subscription Delete Response Object.
153 asn_fprint(stdout, &asn_DEF_E2N_E2AP_PDU, e2pdu);
156 mdclog_write(MDCLOG_ERR, "RC_WMORE");
160 mdclog_write(MDCLOG_ERR, "RC_FAIL");
166 ASN_STRUCT_FREE(asn_DEF_E2N_E2AP_PDU, e2pdu);
170 bool XappMsgHandler::decode_subscription_response_failure(unsigned char* data_buf, size_t data_size){
173 E2N_E2AP_PDU_t *e2pdu = 0;
177 ASN_STRUCT_RESET(asn_DEF_E2N_E2AP_PDU, e2pdu);
179 rval = asn_decode(0,ATS_ALIGNED_BASIC_PER, &asn_DEF_E2N_E2AP_PDU, (void**)&e2pdu, data_buf, data_size);
183 //Extract Subscription Response Failure.
184 asn_fprint(stdout, &asn_DEF_E2N_E2AP_PDU, e2pdu);
187 mdclog_write(MDCLOG_ERR, "RC_WMORE");
191 mdclog_write(MDCLOG_ERR, "RC_FAIL");
197 ASN_STRUCT_FREE(asn_DEF_E2N_E2AP_PDU, e2pdu);
202 bool XappMsgHandler::decode_subscription_delete_response_failure(unsigned char* data_buf, size_t data_size){
205 E2N_E2AP_PDU_t *e2pdu = 0;
209 ASN_STRUCT_RESET(asn_DEF_E2N_E2AP_PDU, e2pdu);
211 rval = asn_decode(0,ATS_ALIGNED_BASIC_PER, &asn_DEF_E2N_E2AP_PDU, (void**)&e2pdu, data_buf, data_size);
215 //Extract Subscription Delete Response Failure.
216 asn_fprint(stdout, &asn_DEF_E2N_E2AP_PDU, e2pdu);
219 mdclog_write(MDCLOG_ERR, "RC_WMORE");
223 mdclog_write(MDCLOG_ERR, "RC_FAIL");
229 ASN_STRUCT_FREE(asn_DEF_E2N_E2AP_PDU, e2pdu);
234 bool XappMsgHandler::a1_policy_handler(char * message, int *message_len, a1_policy_helper &helper){
236 rapidjson::Document doc;
237 if (doc.Parse(message).HasParseError()){
238 mdclog_write(MDCLOG_ERR, "Error: %s, %d :: Could not decode A1 JSON message %s\n", __FILE__, __LINE__, message);
243 rapidjson::Pointer temp1("/operation");
244 rapidjson::Value * ref1 = temp1.Get(doc);
246 mdclog_write(MDCLOG_ERR, "Error : %s, %d:: Could not extract policy type id from %s\n", __FILE__, __LINE__, message);
250 helper.operation = ref1->GetString();
252 // Extract policy id type
253 rapidjson::Pointer temp2("/policy_type_id");
254 rapidjson::Value * ref2 = temp2.Get(doc);
256 mdclog_write(MDCLOG_ERR, "Error : %s, %d:: Could not extract policy type id from %s\n", __FILE__, __LINE__, message);
259 helper.policy_type_id = ref2->GetString();
261 // Extract policy instance id
262 rapidjson::Pointer temp("/policy_instance_id");
263 rapidjson::Value * ref = temp.Get(doc);
265 mdclog_write(MDCLOG_ERR, "Error : %s, %d:: Could not extract policy type id from %s\n", __FILE__, __LINE__, message);
268 helper.policy_instance_id = ref->GetString();
270 if (helper.policy_type_id == "1" && helper.operation == "CREATE"){
271 helper.status = "OK";
272 Document::AllocatorType& alloc = doc.GetAllocator();
275 handler_id.SetString(helper.handler_id.c_str(), helper.handler_id.length(), alloc);
278 status.SetString(helper.status.c_str(), helper.status.length(), alloc);
281 doc.AddMember("handler_id", handler_id, alloc);
282 doc.AddMember("status",status, alloc);
283 doc.RemoveMember("operation");
285 Writer<StringBuffer> writer(buffer);
287 strncpy(message,buffer.GetString(), buffer.GetLength());
288 *message_len = buffer.GetLength();
295 //For processing received messages.XappMsgHandler should mention if resend is required or not.
296 void XappMsgHandler::operator()(rmr_mbuf_t *message, bool *resend){
298 if (message->len > MAX_RMR_RECV_SIZE){
299 mdclog_write(MDCLOG_ERR, "Error : %s, %d, RMR message larger than %d. Ignoring ...", __FILE__, __LINE__, MAX_RMR_RECV_SIZE);
302 a1_policy_helper helper;
304 switch(message->mtype){
305 //need to fix the health check.
306 case (RIC_HEALTH_CHECK_REQ):
307 message->mtype = RIC_HEALTH_CHECK_RESP; // if we're here we are running and all is ok
308 message->sub_id = -1;
309 strncpy( (char*)message->payload, "HELLOWORLD OK\n", rmr_payload_size( message) );
314 //Received Subscription Response Message
315 decode_subscription_response(message->payload,message->len);
318 case (RIC_SUB_DEL_RESP):
319 decode_subscription_delete_response(message->payload,message->len);
322 case (RIC_SUB_FAILURE):
323 decode_subscription_response_failure(message->payload, message->len);
326 case (RIC_SUB_DEL_FAILURE):
327 decode_subscription_delete_response_failure(message->payload,message->len);
332 helper.handler_id = xapp_id;
333 res = a1_policy_handler((char*)message->payload, &message->len, helper);
335 message->mtype = A1_POLICY_RESP; // if we're here we are running and all is ok
336 message->sub_id = -1;
344 mdclog_write(MDCLOG_ERR, "Error :: Unknown message type %d received from RMR", message->mtype);