0fd834de15a87861c735877d95228a50316bc26a
[ric-app/bouncer.git] / Bouncer / src / xapp-asn / e2sm / e2sm_indication.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
20 /* Classes to handle E2 service model based on e2sm-Bouncer-v001.asn */
21 #include "e2sm_indication.hpp"
22
23  //initialize
24  e2sm_indication::e2sm_indication(void){
25
26         memset(&head_fmt1, 0, sizeof(E2SM_Bouncer_IndicationHeader_Format1_t));
27
28         memset(&msg_fmt1, 0, sizeof(E2SM_Bouncer_IndicationMessage_Format1_t));
29
30
31
32     indication_head = 0;
33     indication_head = ( E2SM_Bouncer_IndicationHeader_t *)calloc(1, sizeof( E2SM_Bouncer_IndicationHeader_t));
34     assert(indication_head != 0);
35
36     indication_msg = 0;
37     indication_msg = (E2SM_Bouncer_IndicationMessage_t*)calloc(1, sizeof(E2SM_Bouncer_IndicationMessage_t));
38     assert(indication_msg !=0);
39
40     errbuf_len = 128;
41   };
42
43  e2sm_indication::~e2sm_indication(void){
44
45   mdclog_write(MDCLOG_DEBUG, "Freeing event trigger object memory");
46
47   indication_head->choice.indicationHeader_Format1 = 0;
48
49   indication_msg->choice.indicationMessage_Format1 = 0;
50
51   ASN_STRUCT_FREE(asn_DEF_E2SM_Bouncer_IndicationHeader, indication_head);
52   ASN_STRUCT_FREE(asn_DEF_E2SM_Bouncer_IndicationMessage, indication_msg);
53
54
55 };
56
57 bool e2sm_indication::encode_indication_header(unsigned char *buf, size_t *size, e2sm_indication_helper &helper){
58
59   ASN_STRUCT_RESET(asn_DEF_E2SM_Bouncer_IndicationHeader, indication_head);
60
61   bool res;
62   res = set_fields(indication_head, helper);
63   if (!res){
64
65     return false;
66   }
67
68   int ret_constr = asn_check_constraints(&asn_DEF_E2SM_Bouncer_IndicationHeader, indication_head, errbuf, &errbuf_len);
69   if(ret_constr){
70     error_string.assign(&errbuf[0], errbuf_len);
71     return false;
72   }
73
74   xer_fprint(stdout, &asn_DEF_E2SM_Bouncer_IndicationHeader, indication_head);
75
76   asn_enc_rval_t retval = asn_encode_to_buffer(0, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2SM_Bouncer_IndicationHeader, indication_head, buf, *size);
77
78   if(retval.encoded == -1){
79     error_string.assign(strerror(errno));
80     return false;
81   }
82   else if (retval.encoded > *size){
83     std::stringstream ss;
84     ss  <<"Error encoding event trigger definition. Reason =  encoded pdu size " << retval.encoded << " exceeds buffer size " << *size << std::endl;
85     error_string = ss.str();
86     return false;
87   }
88   else{
89     *size = retval.encoded;
90   }
91
92   return true;
93 }
94
95 bool e2sm_indication::encode_indication_message(unsigned char *buf, size_t *size, e2sm_indication_helper &helper){
96
97   bool res;
98   res = set_fields(indication_msg, helper);
99   if (!res){
100     return false;
101   }
102
103
104   int ret_constr = asn_check_constraints(&asn_DEF_E2SM_Bouncer_IndicationMessage, indication_msg, errbuf, &errbuf_len);
105   if(ret_constr){
106     error_string.assign(&errbuf[0], errbuf_len);
107     return false;
108   }
109
110   xer_fprint(stdout, &asn_DEF_E2SM_Bouncer_IndicationMessage, indication_msg);
111
112   asn_enc_rval_t retval = asn_encode_to_buffer(0, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2SM_Bouncer_IndicationMessage, indication_msg, buf, *size);
113
114   if(retval.encoded == -1){
115     error_string.assign(strerror(errno));
116     return false;
117   }
118   else if (retval.encoded > *size){
119     std::stringstream ss;
120     ss  <<"Error encoding action definition. Reason =  encoded pdu size " << retval.encoded << " exceeds buffer size " << *size << std::endl;
121     error_string = ss.str();
122     return false;
123   }
124   else{
125     *size = retval.encoded;
126   }
127
128   return true;
129 }
130
131 bool e2sm_indication::set_fields(E2SM_Bouncer_IndicationHeader_t * ref_indication_head, e2sm_indication_helper & helper){
132
133  if(ref_indication_head == 0){
134     error_string = "Invalid reference for Event Trigger Definition set fields";
135     return false;
136   }
137
138   ref_indication_head->present = E2SM_Bouncer_IndicationHeader_PR_indicationHeader_Format1;
139
140   head_fmt1.indicationHeaderParam = helper.header;
141
142   ref_indication_head->choice.indicationHeader_Format1 = &head_fmt1;
143
144   return true;
145 };
146
147 bool e2sm_indication::set_fields(E2SM_Bouncer_IndicationMessage_t * ref_indication_msg, e2sm_indication_helper & helper){
148
149  if(ref_indication_msg == 0){
150     error_string = "Invalid reference for Event Action Definition set fields";
151     return false;
152   }
153   ref_indication_msg->present = E2SM_Bouncer_IndicationMessage_PR_indicationMessage_Format1;
154
155   msg_fmt1.indicationMsgParam.buf = helper.message;
156   msg_fmt1.indicationMsgParam.size = helper.message_len;
157
158
159   ref_indication_msg->choice.indicationMessage_Format1 = &msg_fmt1;
160
161
162   return true;
163 };
164
165 bool e2sm_indication::get_fields(E2SM_Bouncer_IndicationHeader_t * ref_indictaion_header, e2sm_indication_helper & helper){
166
167         if (ref_indictaion_header == 0){
168             error_string = "Invalid reference for Indication Header get fields";
169             return false;
170           }
171
172         helper.header = ref_indictaion_header->choice.indicationHeader_Format1->indicationHeaderParam;
173         return true;
174 }
175
176 bool e2sm_indication::get_fields(E2SM_Bouncer_IndicationMessage_t * ref_indication_message, e2sm_indication_helper & helper){
177
178           if (ref_indication_message == 0){
179                     error_string = "Invalid reference for Indication Message get fields";
180                     return false;
181                   }
182           helper.message = ref_indication_message->choice.indicationMessage_Format1->indicationMsgParam.buf;
183           helper.message_len = ref_indication_message->choice.indicationMessage_Format1->indicationMsgParam.size;
184
185           return true;
186   }
187