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