Incorporating RMR Health check code
[ric-app/hw.git] / src / xapp.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  * xapp.cc
21  *
22  *  Created on: Mar, 2020
23  *  Author: Shraboni Jana
24  */
25
26 #include "xapp.hpp"
27
28 Xapp::Xapp(XappSettings &config, XappRmr &rmr){
29         rmr_ref = &rmr;
30         config_ref = &config;
31         xapp_mutex = NULL;
32         return;
33 }
34
35 Xapp::~Xapp(void){
36
37         //Joining the threads
38         int threadcnt = xapp_rcv_thread.size();
39         for(int i=0; i<threadcnt; i++){
40                 if(xapp_rcv_thread[i].joinable())
41                         xapp_rcv_thread[i].join();
42         }
43
44         delete xapp_mutex;
45
46 };
47
48 void Xapp::init() {
49
50         //get rnib information
51         get_rnib_gnblist();
52
53
54 }
55 void Xapp::startup() {
56         //send subscriptions and read A1 policies.
57         startup_subscribe_requests();
58         //startup_get_policies();
59         return;
60 }
61
62 void Xapp::start_xapp_receiver(XappMsgHandler& mp_handler){
63         //start a receiver thread. Can be multiple receiver threads for more than 1 listening port.
64
65         xapp_mutex = new std::mutex();
66
67         mdclog_write(MDCLOG_INFO,"Receiver Thread file= %s, line=%d",__FILE__,__LINE__);
68         //std::unique_ptr<XappMsgHandler> mp_handler = std::make_unique<XappMsgHandler>();
69         //auto mp_handler = _callbacks[0];
70         std::lock_guard<std::mutex> guard(*xapp_mutex);
71         std::thread th_recv([&](){ rmr_ref->xapp_rmr_receive(std::move(mp_handler), rmr_ref);});
72
73         xapp_rcv_thread.push_back(std::move(th_recv));
74
75
76         return;
77
78
79
80 }
81 void Xapp::shutdown(){
82
83         return;
84
85 }
86
87
88 void Xapp::startup_subscribe_requests(void ){
89    size_t data_size = ASN_BUFF_MAX_SIZE;
90    unsigned char        data[data_size];
91
92    for(auto &it: rnib_gnblist){
93      int attempt = 0;
94          XappMsgHandler msg;
95
96  /*      bool res_encode = msg.encode_subscription_request(data, &data_size);
97          if(!res_encode) exit(0);*/
98         char *strMsg = "HelloWorld\0";
99         strncpy((char *)data,strMsg,strlen(strMsg));
100         data_size = sizeof(data);
101
102          xapp_rmr_header rmr_header;
103          rmr_header.message_type = RIC_SUB_RESP;
104          rmr_header.payload_length = data_size;
105      while(1){
106
107                  auto transmitter = std::bind(&XappRmr::xapp_rmr_send,rmr_ref, &rmr_header, (void*)data);
108                  transmitter(); //this will go to subscription manager.
109                  break;
110      }
111    }
112 }
113
114 void Xapp::startup_get_policies(void){
115
116   int policy_id = HELLOWORLD_POLICY_ID;
117
118   std::string policy_query = "{\"policy_id\":" + std::to_string(policy_id) + "}";
119   unsigned char * message = (unsigned char *)calloc(policy_query.length(), sizeof(unsigned char));
120   memcpy(message, policy_query.c_str(),  policy_query.length());
121   xapp_rmr_header header;
122   header.payload_length = policy_query.length();
123   header.message_type = A1_POLICY_QUERY;
124   mdclog_write(MDCLOG_INFO, "Sending request for policy id %d\n", policy_id);
125   rmr_ref->xapp_rmr_send(&header, (void *)message);
126   free(message);
127
128 }
129
130 void Xapp::set_rnib_gnblist(void) {
131
132            openSdl();
133            void *result = getListGnbIds();
134            if(result == NULL){
135                     mdclog_write(MDCLOG_ERR, "ERROR: no data from getListGnbIds\n");
136                 return;
137             }
138
139             mdclog_write(MDCLOG_INFO, "GNB List in R-NIB %s\n", (char*)result);
140
141             Document doc;
142             doc.Parse((char*)result);
143             assert(doc.HasMember("gnb_list"));
144             const Value& gnblist = doc["gnb_list"];
145             assert(gnblist.IsArray());
146             for (SizeType i = 0; i < gnblist.Size(); i++) // Uses SizeType instead of size_t
147             {
148                 assert(gnblist[i].IsObject());
149                 const Value& gnbobj = gnblist[i];
150                 assert(gnbobj.HasMember("inventory_name"));
151                 assert(gnbobj["inventory_name"].IsString());
152                 rnib_gnblist.push_back(gnbobj["inventory_name"].GetString());
153
154             }
155             closeSdl();
156             free(result);
157             return;
158
159 }
160
161
162
163
164
165