Initial commit of source directory
[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
36 Xapp::Xapp(XappSettings &config, XappRmr &rmr, XappSDL &sdl){
37         rmr_ref = &rmr;
38         config_ref = &config;
39         sdl_ref = &sdl;
40         //sdl_ref.insert_data();
41         xapp_mutex = NULL;
42
43         return;
44 }
45 Xapp::~Xapp(void){
46
47         //Joining the threads
48         int threadcnt = xapp_rcv_thread.size();
49         for(int i=0; i<threadcnt; i++){
50                 if(xapp_rcv_thread[i].joinable())
51                         xapp_rcv_thread[i].join();
52         }
53         delete xapp_mutex;
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(){
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         std::vector<std::unique_ptr <XappMsgHandler>> message_procs;
68         mdclog_write(MDCLOG_INFO,"Receiver Thread file= %s, line=%d",__FILE__,__LINE__);
69         std::unique_ptr<XappMsgHandler> mp_handler = std::make_unique<XappMsgHandler>();
70         std::lock_guard<std::mutex> guard(*xapp_mutex);
71         std::thread th_recv([&](){ rmr_ref->xapp_rmr_receive(std::move(*mp_handler.get()), rmr_ref);});
72         xapp_rcv_thread.push_back(std::move(th_recv));
73
74
75         return;
76
77
78
79 }
80 void Xapp::shutdown(){
81
82         return;
83
84 }
85
86
87 void Xapp::startup_subscribe_requests(void ){
88    size_t data_size = ASN_BUFF_MAX_SIZE;
89    unsigned char        data[data_size];
90
91    std::vector<std::string> gNodeBs;
92    gNodeBs.push_back("GNB1001"); //this line should come from RNIB
93
94
95    for(auto &it: gNodeBs){
96      int attempt = 0;
97          XappMsgHandler msg;
98
99  /*      bool res_encode = msg.encode_subscription_request(data, &data_size);
100          if(!res_encode) exit(0);*/
101         char *strMsg = "HelloWorld\0";
102         strncpy((char *)data,strMsg,strlen(strMsg));
103         data_size = sizeof(data);
104
105          xapp_rmr_header rmr_header;
106          rmr_header.message_type = RIC_SUB_RESP;
107          rmr_header.payload_length = data_size;
108      while(1){
109
110                  auto transmitter = std::bind(&XappRmr::xapp_rmr_send,rmr_ref, &rmr_header, (void*)data);
111                  transmitter(); //this will go to subscription manager.
112                  break;
113      }
114    }
115 }
116
117 void Xapp::startup_get_policies(void){
118
119   int policy_id = HELLOWORLD_POLICY_ID;
120
121   std::string policy_query = "{\"policy_id\":" + std::to_string(policy_id) + "}";
122   unsigned char * message = (unsigned char *)calloc(policy_query.length(), sizeof(unsigned char));
123   memcpy(message, policy_query.c_str(),  policy_query.length());
124   xapp_rmr_header header;
125   header.payload_length = policy_query.length();
126   header.message_type = A1_POLICY_QUERY;
127   mdclog_write(MDCLOG_INFO, "Sending request for policy id %d\n", policy_id);
128   rmr_ref->xapp_rmr_send(&header, (void *)message);
129   free(message);
130
131 }
132 void Xapp::sdl_data(void) {
133         sdl_ref->insert_data();
134 }
135 /*void Xapp::rnib_data(void) {
136
137            printf("Using rnibreader lib from C:\n");
138            open();
139            void *result = getListGnbIds();
140            if(result == NULL){
141
142                 printf("ERROR: no data from getListGnbIds\n");
143                 return;
144             }
145             printf("getListGnbIds response: %s\n", (char *)result);
146             close();
147             free(result);
148             return;
149
150 }*/
151
152
153
154
155
156