ISSUE ID:- (RICAPP-176).
[ric-app/bouncer.git] / Bouncer / src / b_xapp_main.cc
1 /*
2 # ==================================================================================
3 # Copyright (c) 2020 HCL Technologies Limited.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 # ==================================================================================
17 */
18
19 #include "xapp.hpp"
20 #include <cpprest/http_listener.h>
21 #include <cpprest/json.h>
22 #include <cpprest/uri.h>
23 using namespace web;
24 using namespace web::http;
25 using namespace web::http::experimental::listener;
26 using namespace utility;
27 std::vector<std::string>SubscriptionIds;
28 #define TRACE(msg)            wcout << msg
29
30
31 void display_json(
32    json::value const & jvalue)
33 {
34         cout<<"\ndisplaying REST Notification\n";
35    wcout << jvalue.serialize().c_str() << endl;
36 }
37
38
39 void handle_request(http_request request)
40 {
41 auto answer = json::value::object();
42 cout<<"\nPrinting POST request content\n";
43 cout<<request.to_string()<<"\n";
44    request
45       .extract_json()
46       .then([&answer](pplx::task<json::value> task) {
47          try
48          {
49             answer = task.get();
50             display_json(answer);
51             }
52          catch (http_exception const & e)
53          {
54          cout<<"\ninside catch block";
55             wcout << e.what() << endl;
56          }
57
58       })
59       .wait();
60
61    request.reply(status_codes::OK, answer);
62 }
63
64 void handle_post(http_request request)
65 {
66    TRACE("\nhandle POST\n");
67
68    handle_request(request);
69 }
70
71 void handle_put(http_request request)
72 {
73    TRACE("\nhandle PUT\n");
74
75    handle_request(request);
76 }
77
78 void start_server()
79 {
80         
81          utility::string_t port = U("8080");
82          utility::string_t address = U("http://0.0.0.0:");
83          address.append(port);
84          address.append(U("/ric/v1/subscriptions/response"));
85         uri_builder uri(address);
86
87          auto addr = uri.to_uri().to_string();
88          http_listener listener(addr);
89         //http_listener listener("http://localhost:8080/ric");
90         cout<<"validated uri = "<<uri::validate(addr)<<"\n";
91          ucout << utility::string_t(U("Listening for REST Notification at: ")) << addr << std::endl;
92         listener.support(methods::POST,[](http_request request) { handle_post(request);});
93         listener.support(methods::PUT,[](http_request request){  handle_put(request);});
94         try
95         {
96         listener
97                 .open()
98                 .then([&listener]() { })
99                 .wait();
100
101         while (true);
102         }
103         catch (exception const & e)
104         {
105         wcout << e.what() << endl;
106         }
107
108 }
109
110 void signalHandler( int signum ) {
111    cout << "Interrupt signal (" << signum << ") received.\n";
112    exit(signum);
113 }
114
115 int main(int argc, char *argv[]){
116
117         // Get the thread id
118         std::thread::id my_id = std::this_thread::get_id();
119         std::stringstream thread_id;
120         std::stringstream ss;
121
122         thread_id << my_id;
123
124         mdclog_write(MDCLOG_INFO, "Starting thread %s",  thread_id.str().c_str());
125
126         //get configuration
127         XappSettings config;
128         //change the priority depending upon application requirement
129         config.loadDefaultSettings();
130         config.loadEnvVarSettings();
131         config.loadCmdlineSettings(argc, argv);
132
133         //Register signal handler to stop
134         signal(SIGINT, signalHandler);
135         signal(SIGTERM, signalHandler);
136
137         //getting the listening port and xapp name info
138         std::string  port = config[XappSettings::SettingName::BOUNCER_PORT];
139         std::string  name = config[XappSettings::SettingName::XAPP_NAME];
140
141         //initialize rmr
142         std::unique_ptr<XappRmr> rmr = std::make_unique<XappRmr>(port);
143         rmr->xapp_rmr_init(true);
144
145
146         //Create Subscription Handler if Xapp deals with Subscription.
147         //std::unique_ptr<SubscriptionHandler> sub_handler = std::make_unique<SubscriptionHandler>();
148
149         SubscriptionHandler sub_handler;
150
151         //create Bouncer Xapp Instance.
152         std::unique_ptr<Xapp> b_xapp;
153         b_xapp = std::make_unique<Xapp>(std::ref(config),std::ref(*rmr));
154
155         mdclog_write(MDCLOG_INFO, "Created Bouncer Xapp Instance");
156         //Startup E2 subscription
157         std::thread t1(std::ref(start_server));
158         t1.detach();
159         b_xapp->startup(sub_handler);
160
161         sleep(10);
162
163
164         //start listener threads and register message handlers.
165         int num_threads = std::stoi(config[XappSettings::SettingName::THREADS]);
166         mdclog_write(MDCLOG_INFO, "Starting Listener Threads. Number of Workers = %d", num_threads);
167
168         std::unique_ptr<XappMsgHandler> mp_handler = std::make_unique<XappMsgHandler>(config[XappSettings::SettingName::XAPP_ID], sub_handler);
169
170         b_xapp->start_xapp_receiver(std::ref(*mp_handler));
171
172         sleep(20);//waiting for some time before sending delete.
173
174
175         b_xapp->shutdown();//will start the sending delete procedure.
176         while(1){
177                                 sleep(1);
178                          }
179
180         return 0;
181 }
182
183
184