blob: 4555810477e452adbe9da21dc0ba984e7f091fa3 [file] [log] [blame]
#ifndef THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_REDFISH_APP_H_
#define THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_REDFISH_APP_H_
#include <cstdint>
#include <memory>
#include <string>
#include <string_view>
#include <vector>
#include "absl/container/flat_hash_set.h"
#include "boost/asio/post.hpp" // NOLINT
#include "boost/asio/thread_pool.hpp" // NOLINT
#include "tlbmc/pacemaker/pacemaker.h"
#include "tlbmc/redfish/request.h"
#include "tlbmc/redfish/response.h"
#include "tlbmc/redfish/routing.h"
#include "tlbmc/store/store.h"
#include "app_interface.h"
#include "dbus_utility.hpp" // NOLINT
#include "async_resp.hpp" // NOLINT
#include "http_request.hpp" // NOLINT
#include "router_interface.h"
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define TLBMC_ROUTE(app, url) \
app.template AddRoute<crow::black_magic::GetParameterTag(url)>(url)
namespace milotic_tlbmc {
// RedfishApp is a class that can be used to register Redfish routes and
// callbacks.
//
// Example:
// RedfishApp app;
// TLBMC_ROUTE(app, "/foo/")
// .methods(boost::beast::http::verb::get)(
// [](const RedfishRequest& req, RedfishResponse& resp) {
// // business logic here
// });
// app.Validate();
//
// // Handle new coming request
// app.Handle(req, resp);
//
//
class RedfishApp : public ::crow::AppInterface {
public:
// The Redfish app for tlbmc only.
explicit RedfishApp(std::unique_ptr<milotic_tlbmc::Store> store);
explicit RedfishApp(std::unique_ptr<milotic_tlbmc::Pacemaker> pacemaker);
RedfishApp() = default;
RedfishApp(const RedfishApp&) = delete;
RedfishApp(RedfishApp&&) = delete;
RedfishApp& operator=(const RedfishApp&) = delete;
RedfishApp& operator=(const RedfishApp&&) = delete;
virtual ~RedfishApp() { thread_pool_.join(); }
// Takes ownership of the request and response and handles the request
// asynchronously.
void Handle(RedfishRequest&& req, RedfishResponse&& resp) const;
// Takes ownership of the request and response and handles the request
// synchronously.
void HandleFromCrowRequest(
const ::crow::Request& req,
const std::shared_ptr<bmcweb::AsyncResp>& async_resp) const override;
// Validates the routes and prepares the router for handling requests.
// Must be called before `Handle`.
void Validate();
std::vector<std::string_view> GetRegisteredRoutes() const;
template <uint64_t Tag>
auto& AddRoute(std::string_view rule) {
return router_.NewRuleTagged<Tag>(rule);
}
template <uint64_t Tag, typename Func>
void ReplaceHandler(std::string_view url, boost::beast::http::verb verb,
Func&& func) {
router_.ReplaceHandler<Tag>(url, verb, func);
}
template <uint64_t Tag>
void RemoveHandler(std::string_view url, boost::beast::http::verb verb) {
router_.RemoveRuleTagged<Tag>(url, verb);
}
template <uint64_t Tag, typename Func>
void AppendHandler(std::string_view url, boost::beast::http::verb verb,
Func&& func) {
router_.AppendHandler<Tag>(url, verb, func);
}
// Returns the list of supported URLs. URLs are sorted and does not contain
// slashes at the end.
absl::flat_hash_set<std::string> GetSupportedUrls() const;
// These are URLs that tlBMC fully owns and can in-place replace gbmcweb.
absl::flat_hash_set<std::string> GetOwnedUrls() const override;
virtual const Store* GetStore() const { return store_.get(); }
virtual crow::Router& GetRouter() { return router_; }
void SetSmartRouter(::crow::RouterInterface* smart_router) override;
void DebugPrint() const override;
protected:
void HandleInternal(const RedfishRequest& req, RedfishResponse& resp) const;
crow::Router router_;
std::unique_ptr<milotic_tlbmc::Store> store_;
mutable boost::asio::thread_pool thread_pool_;
std::unique_ptr<milotic_tlbmc::Pacemaker> pacemaker_;
};
} // namespace milotic_tlbmc
#endif // THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_REDFISH_APP_H_