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