xref: /openbmc/bmcweb/features/redfish/lib/thermal_subsystem.hpp (revision d78572018fc2022091ff8b8eb5a7fef2172ba3d6)
140e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0
240e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors
32973963eSXiaochao Ma #pragma once
42973963eSXiaochao Ma 
52973963eSXiaochao Ma #include "app.hpp"
6*d7857201SEd Tanous #include "async_resp.hpp"
7*d7857201SEd Tanous #include "error_messages.hpp"
8539d8c6bSEd Tanous #include "generated/enums/resource.hpp"
9*d7857201SEd Tanous #include "http_request.hpp"
10ce05f6c4SNan Zhou #include "logging.hpp"
112973963eSXiaochao Ma #include "query.hpp"
122973963eSXiaochao Ma #include "registries/privilege_registry.hpp"
132973963eSXiaochao Ma #include "utils/chassis_utils.hpp"
142973963eSXiaochao Ma 
15*d7857201SEd Tanous #include <boost/beast/http/field.hpp>
16*d7857201SEd Tanous #include <boost/beast/http/verb.hpp>
17ef4c65b7SEd Tanous #include <boost/url/format.hpp>
18ef4c65b7SEd Tanous 
19*d7857201SEd Tanous #include <functional>
20*d7857201SEd Tanous #include <memory>
21ce05f6c4SNan Zhou #include <optional>
22ce05f6c4SNan Zhou #include <string>
23ce05f6c4SNan Zhou 
242973963eSXiaochao Ma namespace redfish
252973963eSXiaochao Ma {
262973963eSXiaochao Ma 
272973963eSXiaochao Ma inline void doThermalSubsystemCollection(
282973963eSXiaochao Ma     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
292973963eSXiaochao Ma     const std::string& chassisId,
302973963eSXiaochao Ma     const std::optional<std::string>& validChassisPath)
312973963eSXiaochao Ma {
322973963eSXiaochao Ma     if (!validChassisPath)
332973963eSXiaochao Ma     {
3462598e31SEd Tanous         BMCWEB_LOG_WARNING("Not a valid chassis ID{}", chassisId);
352973963eSXiaochao Ma         messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
362973963eSXiaochao Ma         return;
372973963eSXiaochao Ma     }
381aee751cSGeorge Liu 
391aee751cSGeorge Liu     asyncResp->res.addHeader(
401aee751cSGeorge Liu         boost::beast::http::field::link,
411aee751cSGeorge Liu         "</redfish/v1/JsonSchemas/ThermalSubsystem/ThermalSubsystem.json>; rel=describedby");
422973963eSXiaochao Ma     asyncResp->res.jsonValue["@odata.type"] =
432973963eSXiaochao Ma         "#ThermalSubsystem.v1_0_0.ThermalSubsystem";
442973963eSXiaochao Ma     asyncResp->res.jsonValue["Name"] = "Thermal Subsystem";
452973963eSXiaochao Ma     asyncResp->res.jsonValue["Id"] = "ThermalSubsystem";
462973963eSXiaochao Ma 
47ef4c65b7SEd Tanous     asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
48ef4c65b7SEd Tanous         "/redfish/v1/Chassis/{}/ThermalSubsystem", chassisId);
492973963eSXiaochao Ma 
509516f41fSGeorge Liu     asyncResp->res.jsonValue["Fans"]["@odata.id"] = boost::urls::format(
519516f41fSGeorge Liu         "/redfish/v1/Chassis/{}/ThermalSubsystem/Fans", chassisId);
529516f41fSGeorge Liu 
535ae1f7f3Szhanghch05     asyncResp->res.jsonValue["ThermalMetrics"]["@odata.id"] =
545ae1f7f3Szhanghch05         boost::urls::format(
555ae1f7f3Szhanghch05             "/redfish/v1/Chassis/{}/ThermalSubsystem/ThermalMetrics",
565ae1f7f3Szhanghch05             chassisId);
575ae1f7f3Szhanghch05 
58539d8c6bSEd Tanous     asyncResp->res.jsonValue["Status"]["State"] = resource::State::Enabled;
59539d8c6bSEd Tanous     asyncResp->res.jsonValue["Status"]["Health"] = resource::Health::OK;
602973963eSXiaochao Ma }
612973963eSXiaochao Ma 
621aee751cSGeorge Liu inline void handleThermalSubsystemCollectionHead(
632973963eSXiaochao Ma     App& app, const crow::Request& req,
642973963eSXiaochao Ma     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
651aee751cSGeorge Liu     const std::string& chassisId)
662973963eSXiaochao Ma {
672973963eSXiaochao Ma     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
682973963eSXiaochao Ma     {
692973963eSXiaochao Ma         return;
702973963eSXiaochao Ma     }
711aee751cSGeorge Liu 
721aee751cSGeorge Liu     auto respHandler = [asyncResp, chassisId](
731aee751cSGeorge Liu                            const std::optional<std::string>& validChassisPath) {
741aee751cSGeorge Liu         if (!validChassisPath)
751aee751cSGeorge Liu         {
761aee751cSGeorge Liu             messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
771aee751cSGeorge Liu             return;
781aee751cSGeorge Liu         }
791aee751cSGeorge Liu         asyncResp->res.addHeader(
801aee751cSGeorge Liu             boost::beast::http::field::link,
811aee751cSGeorge Liu             "</redfish/v1/JsonSchemas/ThermalSubsystem/ThermalSubsystem.json>; rel=describedby");
821aee751cSGeorge Liu     };
831aee751cSGeorge Liu     redfish::chassis_utils::getValidChassisPath(asyncResp, chassisId,
841aee751cSGeorge Liu                                                 std::bind_front(respHandler));
851aee751cSGeorge Liu }
861aee751cSGeorge Liu 
871aee751cSGeorge Liu inline void handleThermalSubsystemCollectionGet(
881aee751cSGeorge Liu     App& app, const crow::Request& req,
891aee751cSGeorge Liu     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
901aee751cSGeorge Liu     const std::string& chassisId)
911aee751cSGeorge Liu {
921aee751cSGeorge Liu     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
931aee751cSGeorge Liu     {
941aee751cSGeorge Liu         return;
951aee751cSGeorge Liu     }
962973963eSXiaochao Ma 
972973963eSXiaochao Ma     redfish::chassis_utils::getValidChassisPath(
982973963eSXiaochao Ma         asyncResp, chassisId,
992973963eSXiaochao Ma         std::bind_front(doThermalSubsystemCollection, asyncResp, chassisId));
1002973963eSXiaochao Ma }
1012973963eSXiaochao Ma 
1022973963eSXiaochao Ma inline void requestRoutesThermalSubsystem(App& app)
1032973963eSXiaochao Ma {
1042973963eSXiaochao Ma     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/ThermalSubsystem/")
1051aee751cSGeorge Liu         .privileges(redfish::privileges::headThermalSubsystem)
1061aee751cSGeorge Liu         .methods(boost::beast::http::verb::head)(std::bind_front(
1071aee751cSGeorge Liu             handleThermalSubsystemCollectionHead, std::ref(app)));
1081aee751cSGeorge Liu 
1091aee751cSGeorge Liu     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/ThermalSubsystem/")
1102973963eSXiaochao Ma         .privileges(redfish::privileges::getThermalSubsystem)
1112973963eSXiaochao Ma         .methods(boost::beast::http::verb::get)(std::bind_front(
1122973963eSXiaochao Ma             handleThermalSubsystemCollectionGet, std::ref(app)));
1132973963eSXiaochao Ma }
1142973963eSXiaochao Ma 
1152973963eSXiaochao Ma } // namespace redfish
116