846704c4eab6d3bfb904ffe510654e6c3528540b
[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  * HWIndicationMessage.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  HWIndicationMessage::HWIndicationMessage(void){
30
31         memset(&_message_fmt1, 0, sizeof(E2SM_HelloWorld_IndicationMessage_Format1_t));
32
33         _message = 0;
34     _message = (E2SM_HelloWorld_IndicationMessage_t*)calloc(1, sizeof(E2SM_HelloWorld_IndicationMessage_t));
35     assert(_message !=0);
36     _hw_msg = 0;
37     _hw_msg_size = 0;
38   };
39
40  //initialize
41  HWIndicationMessage::HWIndicationMessage(unsigned char *buf, size_t *size, bool &status){
42
43         _message = 0;
44     _hw_msg = 0;
45     _hw_msg_size = 0;
46
47     status = this->decode(buf, size);
48
49  };
50
51  //initialize
52   HWIndicationHeader::HWIndicationHeader(void){
53
54         memset(&_header_fmt1, 0, sizeof(E2SM_HelloWorld_IndicationHeader_Format1_t));
55
56         _header = 0;
57      _header = ( E2SM_HelloWorld_IndicationHeader_t *)calloc(1, sizeof( E2SM_HelloWorld_IndicationHeader_t));
58      assert(_header != 0);
59
60      _hw_header = 0;
61    };
62
63   //initialize
64    HWIndicationHeader::HWIndicationHeader(unsigned char *buf, size_t *size, bool &status){
65
66         _header = 0;
67         _hw_header = 0;
68
69       status = this->decode(buf, size);
70
71    };
72
73
74  HWIndicationMessage::~HWIndicationMessage(void){
75
76   mdclog_write(MDCLOG_DEBUG, "Freeing event trigger object memory");
77   _message->choice.indicationMessage_Format1 = 0;
78   ASN_STRUCT_FREE(asn_DEF_E2SM_HelloWorld_IndicationMessage, _message);
79
80
81 };
82  HWIndicationHeader::~HWIndicationHeader(void){
83
84    mdclog_write(MDCLOG_DEBUG, "Freeing event trigger object memory");
85    _header->choice.indicationHeader_Format1 = 0;
86    ASN_STRUCT_FREE(asn_DEF_E2SM_HelloWorld_IndicationHeader, _header);
87  };
88
89 bool HWIndicationHeader::encode(unsigned char *buf, size_t *size){
90
91   ASN_STRUCT_RESET(asn_DEF_E2SM_HelloWorld_IndicationHeader, _header);
92
93   bool res;
94   res = setfields(_header);
95   if (!res){
96
97     return false;
98   }
99
100   int ret_constr = asn_check_constraints(&asn_DEF_E2SM_HelloWorld_IndicationHeader, _header, _errbuf, &_errbuf_len);
101   if(ret_constr){
102     _error_string.assign(&_errbuf[0], _errbuf_len);
103     return false;
104   }
105
106   xer_fprint(stdout, &asn_DEF_E2SM_HelloWorld_IndicationHeader, _header);
107
108   asn_enc_rval_t retval = asn_encode_to_buffer(0, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2SM_HelloWorld_IndicationHeader, _header, buf, *size);
109
110   if(retval.encoded == -1){
111     _error_string.assign(strerror(errno));
112     return false;
113   }
114   else if (retval.encoded > *size){
115     std::stringstream ss;
116     ss  <<"Error encoding event trigger definition. Reason =  encoded pdu size " << retval.encoded << " exceeds buffer size " << *size << std::endl;
117     _error_string = ss.str();
118     return false;
119   }
120   else{
121     *size = retval.encoded;
122   }
123
124   return true;
125 }
126
127 bool HWIndicationMessage::encode(unsigned char *buf, size_t *size){
128
129   bool res;
130   res = setfields(_message);
131   if (!res){
132     return false;
133   }
134
135   int ret_constr = asn_check_constraints(&asn_DEF_E2SM_HelloWorld_IndicationMessage, _message, _errbuf, &_errbuf_len);
136   if(ret_constr){
137     _error_string.assign(&_errbuf[0], _errbuf_len);
138     return false;
139   }
140
141   xer_fprint(stdout, &asn_DEF_E2SM_HelloWorld_IndicationMessage, _message);
142
143   asn_enc_rval_t retval = asn_encode_to_buffer(0, ATS_ALIGNED_BASIC_PER, &asn_DEF_E2SM_HelloWorld_IndicationMessage, _message, buf, *size);
144
145   if(retval.encoded == -1){
146     _error_string.assign(strerror(errno));
147     return false;
148   }
149   else if (retval.encoded > *size){
150     std::stringstream ss;
151     ss  <<"Error encoding action definition. Reason =  encoded pdu size " << retval.encoded << " exceeds buffer size " << *size << std::endl;
152     _error_string = ss.str();
153     return false;
154   }
155   else{
156     *size = retval.encoded;
157   }
158
159   return true;
160 }
161
162 bool HWIndicationHeader::setfields(E2SM_HelloWorld_IndicationHeader_t * _header){
163
164  if(_header == 0){
165     _error_string = "Invalid reference for Event Trigger Definition set fields";
166     return false;
167   }
168
169  _header->present = E2SM_HelloWorld_IndicationHeader_PR_indicationHeader_Format1;
170   _header_fmt1.indicationHeaderParam = this->get_hw_header();
171   _header->choice.indicationHeader_Format1 = &_header_fmt1;
172
173   return true;
174 };
175
176 bool HWIndicationMessage::setfields(E2SM_HelloWorld_IndicationMessage_t * _message){
177
178  if(_message == 0){
179     _error_string = "Invalid reference for Event Action Definition set fields";
180     return false;
181   }
182   _message->present = E2SM_HelloWorld_IndicationMessage_PR_indicationMessage_Format1;
183
184   _message_fmt1.indicationMsgParam.buf = this->get_hw_message();
185   _message_fmt1.indicationMsgParam.size = this->get_hw_message_size();
186   _message->choice.indicationMessage_Format1 = &_message_fmt1;
187
188   return true;
189 };
190
191 bool HWIndicationHeader::decode(unsigned char *buf, size_t *size){
192         asn_dec_rval_t dec_res  = asn_decode(0,ATS_ALIGNED_BASIC_PER, &asn_DEF_E2SM_HelloWorld_IndicationHeader, (void**)&(_header), buf, *size);
193          if(dec_res.code != RC_OK){
194                  mdclog_write(MDCLOG_ERR, "Failed to decode: %s","HW-E2SM RIC Indication Header");
195                  return false;
196          } else {
197                  mdclog_write(MDCLOG_INFO, "Successfully decoded: %s","HW-E2SM RIC Indication Header");
198          }
199
200          if (_header == 0){
201             _error_string = "Invalid reference for Indication Header get fields";
202             return false;
203           }
204
205         this->set_ricIndicationHeader(_header->choice.indicationHeader_Format1->indicationHeaderParam);
206         return true;
207 }
208
209 bool HWIndicationMessage::decode(unsigned char *buf, size_t *size){
210
211         asn_dec_rval_t dec_res  = asn_decode(0,ATS_ALIGNED_BASIC_PER, &asn_DEF_E2SM_HelloWorld_IndicationMessage, (void**)&(_message), buf, *size);
212         if(dec_res.code != RC_OK){
213                          mdclog_write(MDCLOG_ERR, "Failed to decode: %s","HW-E2SM RIC Indication Message");
214                          return false;
215         } else {
216                          mdclog_write(MDCLOG_INFO, "Successfully decoded: %s","HW-E2SM RIC Indication Message");
217         }
218
219           if (_message == 0){
220                     _error_string = "Invalid reference for Indication Message get fields";
221                     return false;
222                   }
223          // this->set_ricIndicationMessage(_message->choice.indicationMessage_Format1->indicationMsgParam.buf, _message->choice.indicationMessage_Format1->indicationMsgParam.size);
224
225           return true;
226   }
227