RIC-641 Fixing client/server model definitions and adding client and server API
[ric-plt/xapp-frame-cpp.git] / src / rest-server / pistacheserver.h
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 #ifndef _pistacheserver_h
19 #define _pistacheserver_h
20 #include <pistache/endpoint.h>
21 #include <pistache/http.h>
22 #include <pistache/router.h>
23 #include <pistache/http_headers.h>
24 #include<vector>
25 #include<string>
26 #include<memory>
27 #include<thread>
28 #include<vector>
29 #include<unordered_set>
30 #include<assert.h>
31 #include<functional>
32 #include<unordered_map>
33 namespace xapp
34 {
35     using usr_callback=void(*)(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response);
36     class pistacheserver
37     {
38         private:
39             std::shared_ptr<Pistache::Http::Endpoint> httpEndpoint;
40             Pistache::Rest::Router router;
41             void setupRoutes_get(int index);
42             void setupRoutes_post(int index);
43             void setupRoutes_del(int index);
44
45             std::vector<std::string> method;
46             std::vector< bool> static_routing;
47             std::vector<bool> dynamic_routing;
48             std::unordered_map<int,std::string> route_static;
49             std::unordered_map<int,std::string> route_dynamic;
50             std::unordered_map<int,usr_callback> cb_static;
51             std::unordered_map<int,usr_callback> cb_dynamic;
52
53             std::string base = "/ric/v1";
54             std::string dynamic_id="/:Id";
55             int stat_sz=0;
56             int dyn_sz=0;
57             int cal_stat_size();
58             int cal_dyn_size();
59             std::thread **t;
60         public:
61             void thread_start();
62             pistacheserver(Pistache::Address addr,std::vector<std::string> method,std::vector<bool> static_routing,std::vector<bool> dynamic_routing);
63             virtual ~pistacheserver(){};
64             virtual void default_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response)
65                  {
66                         response.send(Pistache::Http::Code::Not_Found, "The requested method does not exist");
67                  }
68
69             void init(size_t thr);
70             void setup_base_url(std::string base);
71             void setup_static_route(std::unordered_map<int,std::string> route);//routing path
72             void setup_dynamic_route(std::unordered_map<int,std::string> route);
73             void add_static_cb(std::unordered_map<int,usr_callback> cb);
74             void add_dynamic_cb(std::unordered_map<int,usr_callback> cb);
75             void start();
76             void shutdown();
77     };
78 }
79 #endif
80