RIC-641 Fixing client/server model definitions and adding client and server API
[ric-plt/xapp-frame-cpp.git] / src / rest-server / pistacheserver.h
diff --git a/src/rest-server/pistacheserver.h b/src/rest-server/pistacheserver.h
new file mode 100644 (file)
index 0000000..75fb9b8
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+# ==================================================================================
+# Copyright (c) 2020 HCL Technologies Limited.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ==================================================================================
+*/
+#ifndef _pistacheserver_h
+#define _pistacheserver_h
+#include <pistache/endpoint.h>
+#include <pistache/http.h>
+#include <pistache/router.h>
+#include <pistache/http_headers.h>
+#include<vector>
+#include<string>
+#include<memory>
+#include<thread>
+#include<vector>
+#include<unordered_set>
+#include<assert.h>
+#include<functional>
+#include<unordered_map>
+namespace xapp
+{
+    using usr_callback=void(*)(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response);
+    class pistacheserver
+    {
+        private:
+            std::shared_ptr<Pistache::Http::Endpoint> httpEndpoint;
+            Pistache::Rest::Router router;
+            void setupRoutes_get(int index);
+            void setupRoutes_post(int index);
+            void setupRoutes_del(int index);
+
+            std::vector<std::string> method;
+           std::vector< bool> static_routing;
+            std::vector<bool> dynamic_routing;
+            std::unordered_map<int,std::string> route_static;
+           std::unordered_map<int,std::string> route_dynamic;
+           std::unordered_map<int,usr_callback> cb_static;
+           std::unordered_map<int,usr_callback> cb_dynamic;
+
+           std::string base = "/ric/v1";
+            std::string dynamic_id="/:Id";
+           int stat_sz=0;
+           int dyn_sz=0;
+           int cal_stat_size();
+           int cal_dyn_size();
+           std::thread **t;
+        public:
+           void thread_start();
+            pistacheserver(Pistache::Address addr,std::vector<std::string> method,std::vector<bool> static_routing,std::vector<bool> dynamic_routing);
+           virtual ~pistacheserver(){};
+           virtual void default_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response)
+                {
+                       response.send(Pistache::Http::Code::Not_Found, "The requested method does not exist");
+                }
+
+            void init(size_t thr);
+            void setup_base_url(std::string base);
+            void setup_static_route(std::unordered_map<int,std::string> route);//routing path
+           void setup_dynamic_route(std::unordered_map<int,std::string> route);
+           void add_static_cb(std::unordered_map<int,usr_callback> cb);
+           void add_dynamic_cb(std::unordered_map<int,usr_callback> cb);
+            void start();
+            void shutdown();
+    };
+}
+#endif
+