Incorporating RMR Health check code
[ric-app/hw.git] / src / xapp-utils / xapp_rmr.cc
1 /*
2 ==================================================================================
3
4         Copyright (c) 2018-2019 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
21 #include "xapp_rmr.hpp"
22
23 XappRmr::XappRmr(std::string port, int rmrattempts){
24
25         _proto_port = port;
26         _nattempts = rmrattempts;
27         _xapp_rmr_ctx = NULL;
28         _xapp_received_buff = NULL;
29         _xapp_send_buff =NULL;
30         _rmr_is_ready = false;
31         _listen = false;
32
33 };
34
35 XappRmr::~XappRmr(void){
36
37         // free memory
38         if(_xapp_received_buff)
39                 rmr_free_msg(_xapp_received_buff);
40
41         if(_xapp_send_buff)
42                 rmr_free_msg(_xapp_send_buff);
43
44         if (_xapp_rmr_ctx){
45                 rmr_close(_xapp_rmr_ctx);
46         }
47 };
48
49 //Get RMR Context.
50 void XappRmr::xapp_rmr_init(){
51
52         // Initialize the RMR context
53         _xapp_rmr_ctx = rmr_init(const_cast<char*>(_proto_port.c_str()), RMR_MAX_RCV_BYTES, RMRFL_NONE);
54
55         if ( _xapp_rmr_ctx == NULL){
56                 mdclog_write(MDCLOG_ERR,"Error Initializing RMR, file= %s, line=%d",__FILE__,__LINE__);
57         }
58         while( ! rmr_ready(_xapp_rmr_ctx) ) {
59                 mdclog_write(MDCLOG_INFO,">>> waiting for RMR, file= %s, line=%d",__FILE__,__LINE__);
60                 sleep(1);
61         }
62         _rmr_is_ready = true;
63         mdclog_write(MDCLOG_INFO,"RMR Context is Ready, file= %s, line=%d",__FILE__,__LINE__);
64
65         return;
66
67 }
68 //RMR Returning to the sender.
69 bool XappRmr::xapp_rmr_rts()
70 {
71         mdclog_write(MDCLOG_INFO,"RMR Return to sender, file= %s, line=%d",__FILE__,__LINE__);
72         if ( _xapp_rmr_ctx == NULL){
73                         mdclog_write(MDCLOG_ERR,"Error Initializing RMR, file= %s, line=%d",__FILE__,__LINE__);
74         }
75         while( ! rmr_ready(_xapp_rmr_ctx) ) {
76                         mdclog_write(MDCLOG_INFO,">>> waiting for RMR, file= %s, line=%d",__FILE__,__LINE__);
77                         sleep(1);
78         }
79         rmr_rts_msg(_xapp_rmr_ctx, _xapp_received_buff );
80         sleep(1);
81         return true;
82 }
83
84 //RMR Send with payload and header.
85 bool XappRmr::xapp_rmr_send(xapp_rmr_header *hdr, void *payload){
86
87         int rmr_attempts = _nattempts;
88
89         if( _xapp_send_buff == NULL ) {
90                 _xapp_send_buff = rmr_alloc_msg(_xapp_rmr_ctx, RMR_DEF_SIZE);
91         }
92         _xapp_send_buff->mtype  = hdr->message_type;
93         memcpy(_xapp_send_buff->payload, payload, hdr->payload_length);
94         _xapp_send_buff->len = hdr->payload_length;
95
96         if(!_rmr_is_ready) {
97                 mdclog_write(MDCLOG_ERR,"RMR Context is Not Ready in SENDER, file= %s, line=%d",__FILE__,__LINE__);
98                 return false;
99         }
100
101         while(rmr_attempts > 0){
102                 _xapp_send_buff = rmr_send_msg(_xapp_rmr_ctx,_xapp_send_buff);
103
104                 if(!_xapp_send_buff) {
105                         mdclog_write(MDCLOG_ERR,"Error In Sending Message , file= %s, line=%d, attempt=%d",__FILE__,__LINE__,rmr_attempts);
106                         rmr_attempts--;
107                 }
108                 else if (_xapp_send_buff->state == RMR_OK){
109                         mdclog_write(MDCLOG_INFO,"Message Sent: RMR State = RMR_OK");
110                         rmr_attempts = 0;
111                         _xapp_send_buff = NULL;
112                         return true;
113                 }
114                 else
115                 {
116                         mdclog_write(MDCLOG_INFO,"Need to retry RMR: state=%d, attempt=%d, file=%s, line=%d",_xapp_send_buff->state, rmr_attempts,__FILE__,__LINE__);
117                         rmr_attempts--;
118                 }
119                 sleep(1);
120         }
121         return false;
122 }
123
124 //----------------------------------------
125 // Some get/set methods
126 //---------------------------------------
127 bool XappRmr::get_listen(void){
128   return _listen;
129 }
130
131
132 void XappRmr::set_listen(bool listen){
133   _listen = listen;
134 }
135
136 int XappRmr::get_is_ready(void){
137   return _rmr_is_ready;
138 }
139
140 bool XappRmr::get_isRunning(void){
141   return _listen;
142 }
143
144
145 void * XappRmr::get_rmr_context(void){
146   return _xapp_rmr_ctx;
147 }
148
149
150 void init_logger(const char  *AppName, mdclog_severity_t log_level)
151 {
152     mdclog_attr_t *attr;
153     mdclog_attr_init(&attr);
154     mdclog_attr_set_ident(attr, AppName);
155     mdclog_init(attr);
156     mdclog_level_set(log_level);
157     mdclog_attr_destroy(attr);
158 }
159