Adding Dockerfile to the repo
[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  */
24
25 #include "xapp.hpp"
26
27 Xapp::Xapp(XappSettings &config, XappRmr &rmr){
28         rmr_ref = &rmr;
29         config_ref = &config;
30         xapp_mutex = NULL;
31         return;
32 }
33
34 Xapp::~Xapp(void){
35
36         //Joining the threads
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();
41         }
42         xapp_rcv_thread.clear();
43
44         if(xapp_mutex!=NULL){
45                 xapp_mutex->~mutex();
46                 delete xapp_mutex;
47         }
48 };
49
50 //stop the xapp.
51 void Xapp::stop(void){
52   // Get the mutex lock
53         std::lock_guard<std::mutex> guard(*xapp_mutex);
54         rmr_ref->set_listen(false);
55         rmr_ref->~XappRmr();
56
57 }
58
59 void Xapp::init() {
60
61         //get rnib information
62         get_rnib_gnblist();
63
64
65 }
66 void Xapp::startup() {
67         //send subscriptions and read A1 policies.
68         startup_subscribe_requests();
69         //startup_get_policies();
70         return;
71 }
72
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();
77
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);});
83
84         xapp_rcv_thread.push_back(std::move(th_recv));
85
86
87         return;
88
89
90
91 }
92 void Xapp::shutdown(){
93
94         return;
95
96 }
97
98
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){
104      int attempt = 0;
105          XappMsgHandler msg = XappMsgHandler(xapp_id);
106
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);
112
113          xapp_rmr_header rmr_header;
114          rmr_header.message_type = RIC_SUB_RESP;
115          rmr_header.payload_length = data_size;
116      while(1){
117
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);
121
122                  break;
123      }
124    }
125 }
126
127 void Xapp::startup_get_policies(void){
128
129   int policy_id = HELLOWORLD_POLICY_ID;
130
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);
139   free(message);
140
141 }
142
143 void Xapp::set_rnib_gnblist(void) {
144
145            openSdl();
146            void *result = getListGnbIds();
147            if(result == NULL){
148                     mdclog_write(MDCLOG_ERR, "ERROR: no data from getListGnbIds\n");
149                 return;
150             }
151
152             mdclog_write(MDCLOG_INFO, "GNB List in R-NIB %s\n", (char*)result);
153
154             Document doc;
155             doc.Parse((char*)result);
156             assert(doc.HasMember("gnb_list"));
157
158
159             const Value& gnblist = doc["gnb_list"];
160             if (gnblist.IsNull())
161             return;
162
163             assert(gnblist.IsArray());
164
165             for (SizeType i = 0; i < gnblist.Size(); i++) // Uses SizeType instead of size_t
166             {
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());
172
173             }
174             closeSdl();
175
176             //delete result;
177             return;
178
179 }
180
181
182
183
184
185