2 ==================================================================================
4 Copyright (c) 2018-2019 AT&T Intellectual Property.
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
10 http://www.apache.org/licenses/LICENSE-2.0
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 ==================================================================================
22 * Created on: Mar, 2020
27 Xapp::Xapp(XappSettings &config, XappRmr &rmr){
37 int threadcnt = xapp_rcv_thread.size();
38 for(int i=0; i<threadcnt; i++){
39 if(xapp_rcv_thread[i].joinable())
40 xapp_rcv_thread[i].join();
42 xapp_rcv_thread.clear();
51 void Xapp::stop(void){
53 std::lock_guard<std::mutex> guard(*xapp_mutex);
54 rmr_ref->set_listen(false);
61 //get rnib information
66 void Xapp::startup() {
67 //send subscriptions and read A1 policies.
68 startup_subscribe_requests();
69 //startup_get_policies();
73 void Xapp::start_xapp_receiver(XappMsgHandler& mp_handler){
74 //start a receiver thread. Can be multiple receiver threads for more than 1 listening port.
75 rmr_ref->set_listen(true);
76 xapp_mutex = new std::mutex();
78 mdclog_write(MDCLOG_INFO,"Receiver Thread file= %s, line=%d",__FILE__,__LINE__);
79 //std::unique_ptr<XappMsgHandler> mp_handler = std::make_unique<XappMsgHandler>();
80 //auto mp_handler = _callbacks[0];
81 std::lock_guard<std::mutex> guard(*xapp_mutex);
82 std::thread th_recv([&](){ rmr_ref->xapp_rmr_receive(std::move(mp_handler), rmr_ref);});
84 xapp_rcv_thread.push_back(std::move(th_recv));
92 void Xapp::shutdown(){
99 void Xapp::startup_subscribe_requests(void ){
100 size_t data_size = ASN_BUFF_MAX_SIZE;
101 unsigned char data[data_size];
102 std::string xapp_id = config_ref->operator [](XappSettings::SettingName::XAPP_ID);
103 for(auto &it: rnib_gnblist){
105 XappMsgHandler msg = XappMsgHandler(xapp_id);
107 //bool res_encode = msg.encode_subscription_request(data, &data_size);
108 //if(!res_encode) exit(0);
109 char *strMsg = "HelloWorld\0";
110 strncpy((char *)data,strMsg,strlen(strMsg));
111 data_size = sizeof(data);
113 xapp_rmr_header rmr_header;
114 rmr_header.message_type = RIC_SUB_RESP;
115 rmr_header.payload_length = data_size;
118 auto transmitter = std::bind(&XappRmr::xapp_rmr_send,rmr_ref, &rmr_header, (void*)data);
119 transmitter(); //this will go to subscription manager.
120 //rmr_ref->xapp_rmr_call(&rmr_header,(char*)strMsg);
127 void Xapp::startup_get_policies(void){
129 int policy_id = HELLOWORLD_POLICY_ID;
131 std::string policy_query = "{\"policy_id\":" + std::to_string(policy_id) + "}";
132 unsigned char * message = (unsigned char *)calloc(policy_query.length(), sizeof(unsigned char));
133 memcpy(message, policy_query.c_str(), policy_query.length());
134 xapp_rmr_header header;
135 header.payload_length = policy_query.length();
136 header.message_type = A1_POLICY_QUERY;
137 mdclog_write(MDCLOG_INFO, "Sending request for policy id %d\n", policy_id);
138 rmr_ref->xapp_rmr_send(&header, (void *)message);
143 void Xapp::set_rnib_gnblist(void) {
146 void *result = getListGnbIds();
148 mdclog_write(MDCLOG_ERR, "ERROR: no data from getListGnbIds\n");
152 mdclog_write(MDCLOG_INFO, "GNB List in R-NIB %s\n", (char*)result);
155 doc.Parse((char*)result);
156 assert(doc.HasMember("gnb_list"));
159 const Value& gnblist = doc["gnb_list"];
160 if (gnblist.IsNull())
163 assert(gnblist.IsArray());
165 for (SizeType i = 0; i < gnblist.Size(); i++) // Uses SizeType instead of size_t
167 assert(gnblist[i].IsObject());
168 const Value& gnbobj = gnblist[i];
169 assert(gnbobj.HasMember("inventory_name"));
170 assert(gnbobj["inventory_name"].IsString());
171 rnib_gnblist.push_back(gnbobj["inventory_name"].GetString());