xref: /openbmc/bmcweb/features/redfish/lib/managers.hpp (revision 9ae226fabb21700e6a73ef1d0a9d1cdead70fea5)
19c310685SBorawski.Lukasz /*
29c310685SBorawski.Lukasz // Copyright (c) 2018 Intel Corporation
39c310685SBorawski.Lukasz //
49c310685SBorawski.Lukasz // Licensed under the Apache License, Version 2.0 (the "License");
59c310685SBorawski.Lukasz // you may not use this file except in compliance with the License.
69c310685SBorawski.Lukasz // You may obtain a copy of the License at
79c310685SBorawski.Lukasz //
89c310685SBorawski.Lukasz //      http://www.apache.org/licenses/LICENSE-2.0
99c310685SBorawski.Lukasz //
109c310685SBorawski.Lukasz // Unless required by applicable law or agreed to in writing, software
119c310685SBorawski.Lukasz // distributed under the License is distributed on an "AS IS" BASIS,
129c310685SBorawski.Lukasz // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139c310685SBorawski.Lukasz // See the License for the specific language governing permissions and
149c310685SBorawski.Lukasz // limitations under the License.
159c310685SBorawski.Lukasz */
169c310685SBorawski.Lukasz #pragma once
179c310685SBorawski.Lukasz 
1813451e39SWilly Tu #include "bmcweb_config.h"
1913451e39SWilly Tu 
20a51fc2d2SSui Chen #include "app.hpp"
21a51fc2d2SSui Chen #include "dbus_utility.hpp"
22b49ac873SJames Feist #include "health.hpp"
23a51fc2d2SSui Chen #include "query.hpp"
24c5d03ff4SJennifer Lee #include "redfish_util.hpp"
25a51fc2d2SSui Chen #include "registries/privilege_registry.hpp"
26fac6e53bSKrzysztof Grobelny #include "utils/dbus_utils.hpp"
273ccb3adbSEd Tanous #include "utils/json_utils.hpp"
28a51fc2d2SSui Chen #include "utils/sw_utils.hpp"
29a51fc2d2SSui Chen #include "utils/systemd_utils.hpp"
302b82937eSEd Tanous #include "utils/time_utils.hpp"
319c310685SBorawski.Lukasz 
32e99073f5SGeorge Liu #include <boost/system/error_code.hpp>
33ef4c65b7SEd Tanous #include <boost/url/format.hpp>
34fac6e53bSKrzysztof Grobelny #include <sdbusplus/asio/property.hpp>
35fac6e53bSKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
361214b7e7SGunnar Mills 
37a170f275SEd Tanous #include <algorithm>
38e99073f5SGeorge Liu #include <array>
394bfefa74SGunnar Mills #include <cstdint>
401214b7e7SGunnar Mills #include <memory>
411214b7e7SGunnar Mills #include <sstream>
42e99073f5SGeorge Liu #include <string_view>
43abf2add6SEd Tanous #include <variant>
445b4aa86bSJames Feist 
451abe55efSEd Tanous namespace redfish
461abe55efSEd Tanous {
47ed5befbdSJennifer Lee 
48ed5befbdSJennifer Lee /**
492a5c4407SGunnar Mills  * Function reboots the BMC.
502a5c4407SGunnar Mills  *
512a5c4407SGunnar Mills  * @param[in] asyncResp - Shared pointer for completing asynchronous calls
52ed5befbdSJennifer Lee  */
538d1b46d7Szhanghch05 inline void
548d1b46d7Szhanghch05     doBMCGracefulRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
55ed5befbdSJennifer Lee {
56ed5befbdSJennifer Lee     const char* processName = "xyz.openbmc_project.State.BMC";
57ed5befbdSJennifer Lee     const char* objectPath = "/xyz/openbmc_project/state/bmc0";
58ed5befbdSJennifer Lee     const char* interfaceName = "xyz.openbmc_project.State.BMC";
59ed5befbdSJennifer Lee     const std::string& propertyValue =
60ed5befbdSJennifer Lee         "xyz.openbmc_project.State.BMC.Transition.Reboot";
61ed5befbdSJennifer Lee     const char* destProperty = "RequestedBMCTransition";
62ed5befbdSJennifer Lee 
63ed5befbdSJennifer Lee     // Create the D-Bus variant for D-Bus call.
64*9ae226faSGeorge Liu     sdbusplus::asio::setProperty(
65*9ae226faSGeorge Liu         *crow::connections::systemBus, processName, objectPath, interfaceName,
66*9ae226faSGeorge Liu         destProperty, propertyValue,
675e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
68ed5befbdSJennifer Lee         // Use "Set" method to set the property value.
69ed5befbdSJennifer Lee         if (ec)
70ed5befbdSJennifer Lee         {
712a5c4407SGunnar Mills             BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
72ed5befbdSJennifer Lee             messages::internalError(asyncResp->res);
73ed5befbdSJennifer Lee             return;
74ed5befbdSJennifer Lee         }
75ed5befbdSJennifer Lee 
76ed5befbdSJennifer Lee         messages::success(asyncResp->res);
77*9ae226faSGeorge Liu         });
78ed5befbdSJennifer Lee }
792a5c4407SGunnar Mills 
808d1b46d7Szhanghch05 inline void
818d1b46d7Szhanghch05     doBMCForceRestart(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
82f92af389SJayaprakash Mutyala {
83f92af389SJayaprakash Mutyala     const char* processName = "xyz.openbmc_project.State.BMC";
84f92af389SJayaprakash Mutyala     const char* objectPath = "/xyz/openbmc_project/state/bmc0";
85f92af389SJayaprakash Mutyala     const char* interfaceName = "xyz.openbmc_project.State.BMC";
86f92af389SJayaprakash Mutyala     const std::string& propertyValue =
87f92af389SJayaprakash Mutyala         "xyz.openbmc_project.State.BMC.Transition.HardReboot";
88f92af389SJayaprakash Mutyala     const char* destProperty = "RequestedBMCTransition";
89f92af389SJayaprakash Mutyala 
90f92af389SJayaprakash Mutyala     // Create the D-Bus variant for D-Bus call.
91*9ae226faSGeorge Liu     sdbusplus::asio::setProperty(
92*9ae226faSGeorge Liu         *crow::connections::systemBus, processName, objectPath, interfaceName,
93*9ae226faSGeorge Liu         destProperty, propertyValue,
945e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
95f92af389SJayaprakash Mutyala         // Use "Set" method to set the property value.
96f92af389SJayaprakash Mutyala         if (ec)
97f92af389SJayaprakash Mutyala         {
98f92af389SJayaprakash Mutyala             BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
99f92af389SJayaprakash Mutyala             messages::internalError(asyncResp->res);
100f92af389SJayaprakash Mutyala             return;
101f92af389SJayaprakash Mutyala         }
102f92af389SJayaprakash Mutyala 
103f92af389SJayaprakash Mutyala         messages::success(asyncResp->res);
104*9ae226faSGeorge Liu         });
105f92af389SJayaprakash Mutyala }
106f92af389SJayaprakash Mutyala 
1072a5c4407SGunnar Mills /**
1082a5c4407SGunnar Mills  * ManagerResetAction class supports the POST method for the Reset (reboot)
1092a5c4407SGunnar Mills  * action.
1102a5c4407SGunnar Mills  */
1117e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetAction(App& app)
1122a5c4407SGunnar Mills {
1132a5c4407SGunnar Mills     /**
1142a5c4407SGunnar Mills      * Function handles POST method request.
1152a5c4407SGunnar Mills      * Analyzes POST body before sending Reset (Reboot) request data to D-Bus.
116f92af389SJayaprakash Mutyala      * OpenBMC supports ResetType "GracefulRestart" and "ForceRestart".
1172a5c4407SGunnar Mills      */
1187e860f15SJohn Edward Broadbent 
1197e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/Actions/Manager.Reset/")
120ed398213SEd Tanous         .privileges(redfish::privileges::postManager)
1217e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
12245ca1b86SEd Tanous             [&app](const crow::Request& req,
1237e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1243ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
12545ca1b86SEd Tanous         {
12645ca1b86SEd Tanous             return;
12745ca1b86SEd Tanous         }
1282a5c4407SGunnar Mills         BMCWEB_LOG_DEBUG << "Post Manager Reset.";
1292a5c4407SGunnar Mills 
1302a5c4407SGunnar Mills         std::string resetType;
1312a5c4407SGunnar Mills 
13215ed6780SWilly Tu         if (!json_util::readJsonAction(req, asyncResp->res, "ResetType",
1337e860f15SJohn Edward Broadbent                                        resetType))
1342a5c4407SGunnar Mills         {
1352a5c4407SGunnar Mills             return;
1362a5c4407SGunnar Mills         }
1372a5c4407SGunnar Mills 
138f92af389SJayaprakash Mutyala         if (resetType == "GracefulRestart")
139f92af389SJayaprakash Mutyala         {
140f92af389SJayaprakash Mutyala             BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
141f92af389SJayaprakash Mutyala             doBMCGracefulRestart(asyncResp);
142f92af389SJayaprakash Mutyala             return;
143f92af389SJayaprakash Mutyala         }
1443174e4dfSEd Tanous         if (resetType == "ForceRestart")
145f92af389SJayaprakash Mutyala         {
146f92af389SJayaprakash Mutyala             BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
147f92af389SJayaprakash Mutyala             doBMCForceRestart(asyncResp);
148f92af389SJayaprakash Mutyala             return;
149f92af389SJayaprakash Mutyala         }
1502a5c4407SGunnar Mills         BMCWEB_LOG_DEBUG << "Invalid property value for ResetType: "
1512a5c4407SGunnar Mills                          << resetType;
1522a5c4407SGunnar Mills         messages::actionParameterNotSupported(asyncResp->res, resetType,
1532a5c4407SGunnar Mills                                               "ResetType");
1542a5c4407SGunnar Mills 
1552a5c4407SGunnar Mills         return;
1567e860f15SJohn Edward Broadbent         });
1572a5c4407SGunnar Mills }
158ed5befbdSJennifer Lee 
1593e40fc74SGunnar Mills /**
1603e40fc74SGunnar Mills  * ManagerResetToDefaultsAction class supports POST method for factory reset
1613e40fc74SGunnar Mills  * action.
1623e40fc74SGunnar Mills  */
1637e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetToDefaultsAction(App& app)
1643e40fc74SGunnar Mills {
1653e40fc74SGunnar Mills     /**
1663e40fc74SGunnar Mills      * Function handles ResetToDefaults POST method request.
1673e40fc74SGunnar Mills      *
1683e40fc74SGunnar Mills      * Analyzes POST body message and factory resets BMC by calling
1693e40fc74SGunnar Mills      * BMC code updater factory reset followed by a BMC reboot.
1703e40fc74SGunnar Mills      *
1713e40fc74SGunnar Mills      * BMC code updater factory reset wipes the whole BMC read-write
1723e40fc74SGunnar Mills      * filesystem which includes things like the network settings.
1733e40fc74SGunnar Mills      *
1743e40fc74SGunnar Mills      * OpenBMC only supports ResetToDefaultsType "ResetAll".
1753e40fc74SGunnar Mills      */
1767e860f15SJohn Edward Broadbent 
1777e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
1787e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults/")
179ed398213SEd Tanous         .privileges(redfish::privileges::postManager)
1807e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
18145ca1b86SEd Tanous             [&app](const crow::Request& req,
1827e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1833ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
18445ca1b86SEd Tanous         {
18545ca1b86SEd Tanous             return;
18645ca1b86SEd Tanous         }
1873e40fc74SGunnar Mills         BMCWEB_LOG_DEBUG << "Post ResetToDefaults.";
1883e40fc74SGunnar Mills 
1893e40fc74SGunnar Mills         std::string resetType;
1903e40fc74SGunnar Mills 
191002d39b4SEd Tanous         if (!json_util::readJsonAction(req, asyncResp->res,
192002d39b4SEd Tanous                                        "ResetToDefaultsType", resetType))
1933e40fc74SGunnar Mills         {
1943e40fc74SGunnar Mills             BMCWEB_LOG_DEBUG << "Missing property ResetToDefaultsType.";
1953e40fc74SGunnar Mills 
196002d39b4SEd Tanous             messages::actionParameterMissing(asyncResp->res, "ResetToDefaults",
1973e40fc74SGunnar Mills                                              "ResetToDefaultsType");
1983e40fc74SGunnar Mills             return;
1993e40fc74SGunnar Mills         }
2003e40fc74SGunnar Mills 
2013e40fc74SGunnar Mills         if (resetType != "ResetAll")
2023e40fc74SGunnar Mills         {
2030fda0f12SGeorge Liu             BMCWEB_LOG_DEBUG
2040fda0f12SGeorge Liu                 << "Invalid property value for ResetToDefaultsType: "
2053e40fc74SGunnar Mills                 << resetType;
206002d39b4SEd Tanous             messages::actionParameterNotSupported(asyncResp->res, resetType,
207002d39b4SEd Tanous                                                   "ResetToDefaultsType");
2083e40fc74SGunnar Mills             return;
2093e40fc74SGunnar Mills         }
2103e40fc74SGunnar Mills 
2113e40fc74SGunnar Mills         crow::connections::systemBus->async_method_call(
2125e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
2133e40fc74SGunnar Mills             if (ec)
2143e40fc74SGunnar Mills             {
215002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "Failed to ResetToDefaults: " << ec;
2163e40fc74SGunnar Mills                 messages::internalError(asyncResp->res);
2173e40fc74SGunnar Mills                 return;
2183e40fc74SGunnar Mills             }
2193e40fc74SGunnar Mills             // Factory Reset doesn't actually happen until a reboot
2203e40fc74SGunnar Mills             // Can't erase what the BMC is running on
2213e40fc74SGunnar Mills             doBMCGracefulRestart(asyncResp);
2223e40fc74SGunnar Mills             },
2233e40fc74SGunnar Mills             "xyz.openbmc_project.Software.BMC.Updater",
2243e40fc74SGunnar Mills             "/xyz/openbmc_project/software",
2253e40fc74SGunnar Mills             "xyz.openbmc_project.Common.FactoryReset", "Reset");
2267e860f15SJohn Edward Broadbent         });
2273e40fc74SGunnar Mills }
2283e40fc74SGunnar Mills 
2291cb1a9e6SAppaRao Puli /**
2301cb1a9e6SAppaRao Puli  * ManagerResetActionInfo derived class for delivering Manager
2311cb1a9e6SAppaRao Puli  * ResetType AllowableValues using ResetInfo schema.
2321cb1a9e6SAppaRao Puli  */
2337e860f15SJohn Edward Broadbent inline void requestRoutesManagerResetActionInfo(App& app)
2341cb1a9e6SAppaRao Puli {
2351cb1a9e6SAppaRao Puli     /**
2361cb1a9e6SAppaRao Puli      * Functions triggers appropriate requests on DBus
2371cb1a9e6SAppaRao Puli      */
2387e860f15SJohn Edward Broadbent 
2397e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/ResetActionInfo/")
240ed398213SEd Tanous         .privileges(redfish::privileges::getActionInfo)
2417e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
24245ca1b86SEd Tanous             [&app](const crow::Request& req,
2437e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2443ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
24545ca1b86SEd Tanous         {
24645ca1b86SEd Tanous             return;
24745ca1b86SEd Tanous         }
2481476687dSEd Tanous 
2491476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
2501476687dSEd Tanous             "#ActionInfo.v1_1_2.ActionInfo";
2511476687dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
2521476687dSEd Tanous             "/redfish/v1/Managers/bmc/ResetActionInfo";
2531476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "Reset Action Info";
2541476687dSEd Tanous         asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
2551476687dSEd Tanous         nlohmann::json::object_t parameter;
2561476687dSEd Tanous         parameter["Name"] = "ResetType";
2571476687dSEd Tanous         parameter["Required"] = true;
2581476687dSEd Tanous         parameter["DataType"] = "String";
2591476687dSEd Tanous 
2601476687dSEd Tanous         nlohmann::json::array_t allowableValues;
261ad539545SPatrick Williams         allowableValues.emplace_back("GracefulRestart");
262ad539545SPatrick Williams         allowableValues.emplace_back("ForceRestart");
2631476687dSEd Tanous         parameter["AllowableValues"] = std::move(allowableValues);
2641476687dSEd Tanous 
2651476687dSEd Tanous         nlohmann::json::array_t parameters;
266ad539545SPatrick Williams         parameters.emplace_back(std::move(parameter));
2671476687dSEd Tanous 
2681476687dSEd Tanous         asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
2697e860f15SJohn Edward Broadbent         });
2701cb1a9e6SAppaRao Puli }
2711cb1a9e6SAppaRao Puli 
2725b4aa86bSJames Feist static constexpr const char* objectManagerIface =
2735b4aa86bSJames Feist     "org.freedesktop.DBus.ObjectManager";
2745b4aa86bSJames Feist static constexpr const char* pidConfigurationIface =
2755b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid";
2765b4aa86bSJames Feist static constexpr const char* pidZoneConfigurationIface =
2775b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid.Zone";
278b7a08d04SJames Feist static constexpr const char* stepwiseConfigurationIface =
279b7a08d04SJames Feist     "xyz.openbmc_project.Configuration.Stepwise";
28073df0db0SJames Feist static constexpr const char* thermalModeIface =
28173df0db0SJames Feist     "xyz.openbmc_project.Control.ThermalMode";
2829c310685SBorawski.Lukasz 
2838d1b46d7Szhanghch05 inline void
2848d1b46d7Szhanghch05     asyncPopulatePid(const std::string& connection, const std::string& path,
28573df0db0SJames Feist                      const std::string& currentProfile,
28673df0db0SJames Feist                      const std::vector<std::string>& supportedProfiles,
2878d1b46d7Szhanghch05                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2885b4aa86bSJames Feist {
2895eb468daSGeorge Liu     sdbusplus::message::object_path objPath(path);
2905eb468daSGeorge Liu     dbus::utility::getManagedObjects(
2915eb468daSGeorge Liu         connection, objPath,
29273df0db0SJames Feist         [asyncResp, currentProfile, supportedProfiles](
2935e7e2dc5SEd Tanous             const boost::system::error_code& ec,
2945b4aa86bSJames Feist             const dbus::utility::ManagedObjectType& managedObj) {
2955b4aa86bSJames Feist         if (ec)
2965b4aa86bSJames Feist         {
2975b4aa86bSJames Feist             BMCWEB_LOG_ERROR << ec;
298f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2995b4aa86bSJames Feist             return;
3005b4aa86bSJames Feist         }
3015b4aa86bSJames Feist         nlohmann::json& configRoot =
3025b4aa86bSJames Feist             asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"];
3035b4aa86bSJames Feist         nlohmann::json& fans = configRoot["FanControllers"];
3045b4aa86bSJames Feist         fans["@odata.type"] = "#OemManager.FanControllers";
3050fda0f12SGeorge Liu         fans["@odata.id"] =
3060fda0f12SGeorge Liu             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanControllers";
3075b4aa86bSJames Feist 
3085b4aa86bSJames Feist         nlohmann::json& pids = configRoot["PidControllers"];
3095b4aa86bSJames Feist         pids["@odata.type"] = "#OemManager.PidControllers";
3105b4aa86bSJames Feist         pids["@odata.id"] =
3115b4aa86bSJames Feist             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers";
3125b4aa86bSJames Feist 
313b7a08d04SJames Feist         nlohmann::json& stepwise = configRoot["StepwiseControllers"];
314b7a08d04SJames Feist         stepwise["@odata.type"] = "#OemManager.StepwiseControllers";
315b7a08d04SJames Feist         stepwise["@odata.id"] =
316b7a08d04SJames Feist             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers";
317b7a08d04SJames Feist 
3185b4aa86bSJames Feist         nlohmann::json& zones = configRoot["FanZones"];
3195b4aa86bSJames Feist         zones["@odata.id"] =
3205b4aa86bSJames Feist             "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones";
3215b4aa86bSJames Feist         zones["@odata.type"] = "#OemManager.FanZones";
322002d39b4SEd Tanous         configRoot["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan";
3235b4aa86bSJames Feist         configRoot["@odata.type"] = "#OemManager.Fan";
32473df0db0SJames Feist         configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles;
32573df0db0SJames Feist 
32673df0db0SJames Feist         if (!currentProfile.empty())
32773df0db0SJames Feist         {
32873df0db0SJames Feist             configRoot["Profile"] = currentProfile;
32973df0db0SJames Feist         }
33073df0db0SJames Feist         BMCWEB_LOG_ERROR << "profile = " << currentProfile << " !";
3315b4aa86bSJames Feist 
3325b4aa86bSJames Feist         for (const auto& pathPair : managedObj)
3335b4aa86bSJames Feist         {
3345b4aa86bSJames Feist             for (const auto& intfPair : pathPair.second)
3355b4aa86bSJames Feist             {
3365b4aa86bSJames Feist                 if (intfPair.first != pidConfigurationIface &&
337b7a08d04SJames Feist                     intfPair.first != pidZoneConfigurationIface &&
338b7a08d04SJames Feist                     intfPair.first != stepwiseConfigurationIface)
3395b4aa86bSJames Feist                 {
3405b4aa86bSJames Feist                     continue;
3415b4aa86bSJames Feist                 }
34273df0db0SJames Feist 
343711ac7a9SEd Tanous                 std::string name;
344711ac7a9SEd Tanous 
345711ac7a9SEd Tanous                 for (const std::pair<std::string,
346002d39b4SEd Tanous                                      dbus::utility::DbusVariantType>& propPair :
347002d39b4SEd Tanous                      intfPair.second)
348711ac7a9SEd Tanous                 {
349711ac7a9SEd Tanous                     if (propPair.first == "Name")
350711ac7a9SEd Tanous                     {
3515b4aa86bSJames Feist                         const std::string* namePtr =
352711ac7a9SEd Tanous                             std::get_if<std::string>(&propPair.second);
3535b4aa86bSJames Feist                         if (namePtr == nullptr)
3545b4aa86bSJames Feist                         {
3555b4aa86bSJames Feist                             BMCWEB_LOG_ERROR << "Pid Name Field illegal";
356b7a08d04SJames Feist                             messages::internalError(asyncResp->res);
3575b4aa86bSJames Feist                             return;
3585b4aa86bSJames Feist                         }
359db697703SWilly Tu                         name = *namePtr;
3605b4aa86bSJames Feist                         dbus::utility::escapePathForDbus(name);
361711ac7a9SEd Tanous                     }
362711ac7a9SEd Tanous                     else if (propPair.first == "Profiles")
36373df0db0SJames Feist                     {
36473df0db0SJames Feist                         const std::vector<std::string>* profiles =
36573df0db0SJames Feist                             std::get_if<std::vector<std::string>>(
366711ac7a9SEd Tanous                                 &propPair.second);
36773df0db0SJames Feist                         if (profiles == nullptr)
36873df0db0SJames Feist                         {
369002d39b4SEd Tanous                             BMCWEB_LOG_ERROR << "Pid Profiles Field illegal";
37073df0db0SJames Feist                             messages::internalError(asyncResp->res);
37173df0db0SJames Feist                             return;
37273df0db0SJames Feist                         }
37373df0db0SJames Feist                         if (std::find(profiles->begin(), profiles->end(),
37473df0db0SJames Feist                                       currentProfile) == profiles->end())
37573df0db0SJames Feist                         {
37673df0db0SJames Feist                             BMCWEB_LOG_INFO
377002d39b4SEd Tanous                                 << name << " not supported in current profile";
37873df0db0SJames Feist                             continue;
37973df0db0SJames Feist                         }
38073df0db0SJames Feist                     }
381711ac7a9SEd Tanous                 }
382b7a08d04SJames Feist                 nlohmann::json* config = nullptr;
383c33a90ecSJames Feist                 const std::string* classPtr = nullptr;
384711ac7a9SEd Tanous 
385711ac7a9SEd Tanous                 for (const std::pair<std::string,
386002d39b4SEd Tanous                                      dbus::utility::DbusVariantType>& propPair :
387002d39b4SEd Tanous                      intfPair.second)
388c33a90ecSJames Feist                 {
389727dc83fSLei YU                     if (propPair.first == "Class")
390711ac7a9SEd Tanous                     {
391002d39b4SEd Tanous                         classPtr = std::get_if<std::string>(&propPair.second);
392711ac7a9SEd Tanous                     }
393c33a90ecSJames Feist                 }
394c33a90ecSJames Feist 
395ef4c65b7SEd Tanous                 boost::urls::url url("/redfish/v1/Managers/bmc");
3965b4aa86bSJames Feist                 if (intfPair.first == pidZoneConfigurationIface)
3975b4aa86bSJames Feist                 {
3985b4aa86bSJames Feist                     std::string chassis;
399002d39b4SEd Tanous                     if (!dbus::utility::getNthStringFromPath(pathPair.first.str,
400002d39b4SEd Tanous                                                              5, chassis))
4015b4aa86bSJames Feist                     {
4025b4aa86bSJames Feist                         chassis = "#IllegalValue";
4035b4aa86bSJames Feist                     }
4045b4aa86bSJames Feist                     nlohmann::json& zone = zones[name];
405ef4c65b7SEd Tanous                     zone["Chassis"]["@odata.id"] =
406ef4c65b7SEd Tanous                         boost::urls::format("/redfish/v1/Chassis/{}", chassis);
407eddfc437SWilly Tu                     url.set_fragment(
408eddfc437SWilly Tu                         ("/Oem/OpenBmc/Fan/FanZones"_json_pointer / name)
409eddfc437SWilly Tu                             .to_string());
410eddfc437SWilly Tu                     zone["@odata.id"] = std::move(url);
4115b4aa86bSJames Feist                     zone["@odata.type"] = "#OemManager.FanZone";
412b7a08d04SJames Feist                     config = &zone;
4135b4aa86bSJames Feist                 }
4145b4aa86bSJames Feist 
415b7a08d04SJames Feist                 else if (intfPair.first == stepwiseConfigurationIface)
4165b4aa86bSJames Feist                 {
417c33a90ecSJames Feist                     if (classPtr == nullptr)
418c33a90ecSJames Feist                     {
419c33a90ecSJames Feist                         BMCWEB_LOG_ERROR << "Pid Class Field illegal";
420c33a90ecSJames Feist                         messages::internalError(asyncResp->res);
421c33a90ecSJames Feist                         return;
422c33a90ecSJames Feist                     }
423c33a90ecSJames Feist 
424b7a08d04SJames Feist                     nlohmann::json& controller = stepwise[name];
425b7a08d04SJames Feist                     config = &controller;
426eddfc437SWilly Tu                     url.set_fragment(
427eddfc437SWilly Tu                         ("/Oem/OpenBmc/Fan/StepwiseControllers"_json_pointer /
428eddfc437SWilly Tu                          name)
429eddfc437SWilly Tu                             .to_string());
430eddfc437SWilly Tu                     controller["@odata.id"] = std::move(url);
431b7a08d04SJames Feist                     controller["@odata.type"] =
432b7a08d04SJames Feist                         "#OemManager.StepwiseController";
433b7a08d04SJames Feist 
434c33a90ecSJames Feist                     controller["Direction"] = *classPtr;
4355b4aa86bSJames Feist                 }
4365b4aa86bSJames Feist 
4375b4aa86bSJames Feist                 // pid and fans are off the same configuration
438b7a08d04SJames Feist                 else if (intfPair.first == pidConfigurationIface)
4395b4aa86bSJames Feist                 {
4405b4aa86bSJames Feist                     if (classPtr == nullptr)
4415b4aa86bSJames Feist                     {
4425b4aa86bSJames Feist                         BMCWEB_LOG_ERROR << "Pid Class Field illegal";
443a08b46ccSJason M. Bills                         messages::internalError(asyncResp->res);
4445b4aa86bSJames Feist                         return;
4455b4aa86bSJames Feist                     }
4465b4aa86bSJames Feist                     bool isFan = *classPtr == "fan";
447002d39b4SEd Tanous                     nlohmann::json& element = isFan ? fans[name] : pids[name];
448b7a08d04SJames Feist                     config = &element;
4495b4aa86bSJames Feist                     if (isFan)
4505b4aa86bSJames Feist                     {
451eddfc437SWilly Tu                         url.set_fragment(
452eddfc437SWilly Tu                             ("/Oem/OpenBmc/Fan/FanControllers"_json_pointer /
453eddfc437SWilly Tu                              name)
454eddfc437SWilly Tu                                 .to_string());
455eddfc437SWilly Tu                         element["@odata.id"] = std::move(url);
456002d39b4SEd Tanous                         element["@odata.type"] = "#OemManager.FanController";
4575b4aa86bSJames Feist                     }
4585b4aa86bSJames Feist                     else
4595b4aa86bSJames Feist                     {
460eddfc437SWilly Tu                         url.set_fragment(
461eddfc437SWilly Tu                             ("/Oem/OpenBmc/Fan/PidControllers"_json_pointer /
462eddfc437SWilly Tu                              name)
463eddfc437SWilly Tu                                 .to_string());
464eddfc437SWilly Tu                         element["@odata.id"] = std::move(url);
465002d39b4SEd Tanous                         element["@odata.type"] = "#OemManager.PidController";
4665b4aa86bSJames Feist                     }
467b7a08d04SJames Feist                 }
468b7a08d04SJames Feist                 else
469b7a08d04SJames Feist                 {
470b7a08d04SJames Feist                     BMCWEB_LOG_ERROR << "Unexpected configuration";
471b7a08d04SJames Feist                     messages::internalError(asyncResp->res);
472b7a08d04SJames Feist                     return;
473b7a08d04SJames Feist                 }
474b7a08d04SJames Feist 
475b7a08d04SJames Feist                 // used for making maps out of 2 vectors
476b7a08d04SJames Feist                 const std::vector<double>* keys = nullptr;
477b7a08d04SJames Feist                 const std::vector<double>* values = nullptr;
478b7a08d04SJames Feist 
479b7a08d04SJames Feist                 for (const auto& propertyPair : intfPair.second)
480b7a08d04SJames Feist                 {
481b7a08d04SJames Feist                     if (propertyPair.first == "Type" ||
482b7a08d04SJames Feist                         propertyPair.first == "Class" ||
483b7a08d04SJames Feist                         propertyPair.first == "Name")
484b7a08d04SJames Feist                     {
485b7a08d04SJames Feist                         continue;
486b7a08d04SJames Feist                     }
487b7a08d04SJames Feist 
488b7a08d04SJames Feist                     // zones
489b7a08d04SJames Feist                     if (intfPair.first == pidZoneConfigurationIface)
490b7a08d04SJames Feist                     {
491b7a08d04SJames Feist                         const double* ptr =
492abf2add6SEd Tanous                             std::get_if<double>(&propertyPair.second);
493b7a08d04SJames Feist                         if (ptr == nullptr)
494b7a08d04SJames Feist                         {
495b7a08d04SJames Feist                             BMCWEB_LOG_ERROR << "Field Illegal "
496b7a08d04SJames Feist                                              << propertyPair.first;
497b7a08d04SJames Feist                             messages::internalError(asyncResp->res);
498b7a08d04SJames Feist                             return;
499b7a08d04SJames Feist                         }
500b7a08d04SJames Feist                         (*config)[propertyPair.first] = *ptr;
501b7a08d04SJames Feist                     }
502b7a08d04SJames Feist 
503b7a08d04SJames Feist                     if (intfPair.first == stepwiseConfigurationIface)
504b7a08d04SJames Feist                     {
505b7a08d04SJames Feist                         if (propertyPair.first == "Reading" ||
506b7a08d04SJames Feist                             propertyPair.first == "Output")
507b7a08d04SJames Feist                         {
508b7a08d04SJames Feist                             const std::vector<double>* ptr =
509abf2add6SEd Tanous                                 std::get_if<std::vector<double>>(
510b7a08d04SJames Feist                                     &propertyPair.second);
511b7a08d04SJames Feist 
512b7a08d04SJames Feist                             if (ptr == nullptr)
513b7a08d04SJames Feist                             {
514b7a08d04SJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
515b7a08d04SJames Feist                                                  << propertyPair.first;
516b7a08d04SJames Feist                                 messages::internalError(asyncResp->res);
517b7a08d04SJames Feist                                 return;
518b7a08d04SJames Feist                             }
519b7a08d04SJames Feist 
520b7a08d04SJames Feist                             if (propertyPair.first == "Reading")
521b7a08d04SJames Feist                             {
522b7a08d04SJames Feist                                 keys = ptr;
523b7a08d04SJames Feist                             }
524b7a08d04SJames Feist                             else
525b7a08d04SJames Feist                             {
526b7a08d04SJames Feist                                 values = ptr;
527b7a08d04SJames Feist                             }
528e662eae8SEd Tanous                             if (keys != nullptr && values != nullptr)
529b7a08d04SJames Feist                             {
530b7a08d04SJames Feist                                 if (keys->size() != values->size())
531b7a08d04SJames Feist                                 {
532b7a08d04SJames Feist                                     BMCWEB_LOG_ERROR
5330fda0f12SGeorge Liu                                         << "Reading and Output size don't match ";
534b7a08d04SJames Feist                                     messages::internalError(asyncResp->res);
535b7a08d04SJames Feist                                     return;
536b7a08d04SJames Feist                                 }
537b7a08d04SJames Feist                                 nlohmann::json& steps = (*config)["Steps"];
538b7a08d04SJames Feist                                 steps = nlohmann::json::array();
539b7a08d04SJames Feist                                 for (size_t ii = 0; ii < keys->size(); ii++)
540b7a08d04SJames Feist                                 {
5411476687dSEd Tanous                                     nlohmann::json::object_t step;
5421476687dSEd Tanous                                     step["Target"] = (*keys)[ii];
5431476687dSEd Tanous                                     step["Output"] = (*values)[ii];
544b2ba3072SPatrick Williams                                     steps.emplace_back(std::move(step));
545b7a08d04SJames Feist                                 }
546b7a08d04SJames Feist                             }
547b7a08d04SJames Feist                         }
548b7a08d04SJames Feist                         if (propertyPair.first == "NegativeHysteresis" ||
549b7a08d04SJames Feist                             propertyPair.first == "PositiveHysteresis")
550b7a08d04SJames Feist                         {
551b7a08d04SJames Feist                             const double* ptr =
552abf2add6SEd Tanous                                 std::get_if<double>(&propertyPair.second);
553b7a08d04SJames Feist                             if (ptr == nullptr)
554b7a08d04SJames Feist                             {
555b7a08d04SJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
556b7a08d04SJames Feist                                                  << propertyPair.first;
557b7a08d04SJames Feist                                 messages::internalError(asyncResp->res);
558b7a08d04SJames Feist                                 return;
559b7a08d04SJames Feist                             }
560b7a08d04SJames Feist                             (*config)[propertyPair.first] = *ptr;
561b7a08d04SJames Feist                         }
562b7a08d04SJames Feist                     }
563b7a08d04SJames Feist 
564b7a08d04SJames Feist                     // pid and fans are off the same configuration
565b7a08d04SJames Feist                     if (intfPair.first == pidConfigurationIface ||
566b7a08d04SJames Feist                         intfPair.first == stepwiseConfigurationIface)
567b7a08d04SJames Feist                     {
5685b4aa86bSJames Feist                         if (propertyPair.first == "Zones")
5695b4aa86bSJames Feist                         {
5705b4aa86bSJames Feist                             const std::vector<std::string>* inputs =
571abf2add6SEd Tanous                                 std::get_if<std::vector<std::string>>(
5721b6b96c5SEd Tanous                                     &propertyPair.second);
5735b4aa86bSJames Feist 
5745b4aa86bSJames Feist                             if (inputs == nullptr)
5755b4aa86bSJames Feist                             {
576002d39b4SEd Tanous                                 BMCWEB_LOG_ERROR << "Zones Pid Field Illegal";
577a08b46ccSJason M. Bills                                 messages::internalError(asyncResp->res);
5785b4aa86bSJames Feist                                 return;
5795b4aa86bSJames Feist                             }
580b7a08d04SJames Feist                             auto& data = (*config)[propertyPair.first];
5815b4aa86bSJames Feist                             data = nlohmann::json::array();
5825b4aa86bSJames Feist                             for (std::string itemCopy : *inputs)
5835b4aa86bSJames Feist                             {
5845b4aa86bSJames Feist                                 dbus::utility::escapePathForDbus(itemCopy);
5851476687dSEd Tanous                                 nlohmann::json::object_t input;
586ef4c65b7SEd Tanous                                 boost::urls::url managerUrl = boost::urls::format(
587ef4c65b7SEd Tanous                                     "/redfish/v1/Managers/bmc#{}",
588eddfc437SWilly Tu                                     ("/Oem/OpenBmc/Fan/FanZones"_json_pointer /
589eddfc437SWilly Tu                                      itemCopy)
590eddfc437SWilly Tu                                         .to_string());
591eddfc437SWilly Tu                                 input["@odata.id"] = std::move(managerUrl);
592b2ba3072SPatrick Williams                                 data.emplace_back(std::move(input));
5935b4aa86bSJames Feist                             }
5945b4aa86bSJames Feist                         }
5955b4aa86bSJames Feist                         // todo(james): may never happen, but this
5965b4aa86bSJames Feist                         // assumes configuration data referenced in the
5975b4aa86bSJames Feist                         // PID config is provided by the same daemon, we
5985b4aa86bSJames Feist                         // could add another loop to cover all cases,
5995b4aa86bSJames Feist                         // but I'm okay kicking this can down the road a
6005b4aa86bSJames Feist                         // bit
6015b4aa86bSJames Feist 
6025b4aa86bSJames Feist                         else if (propertyPair.first == "Inputs" ||
6035b4aa86bSJames Feist                                  propertyPair.first == "Outputs")
6045b4aa86bSJames Feist                         {
605b7a08d04SJames Feist                             auto& data = (*config)[propertyPair.first];
6065b4aa86bSJames Feist                             const std::vector<std::string>* inputs =
607abf2add6SEd Tanous                                 std::get_if<std::vector<std::string>>(
6081b6b96c5SEd Tanous                                     &propertyPair.second);
6095b4aa86bSJames Feist 
6105b4aa86bSJames Feist                             if (inputs == nullptr)
6115b4aa86bSJames Feist                             {
6125b4aa86bSJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
6135b4aa86bSJames Feist                                                  << propertyPair.first;
614f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
6155b4aa86bSJames Feist                                 return;
6165b4aa86bSJames Feist                             }
6175b4aa86bSJames Feist                             data = *inputs;
618b943aaefSJames Feist                         }
619b943aaefSJames Feist                         else if (propertyPair.first == "SetPointOffset")
620b943aaefSJames Feist                         {
621b943aaefSJames Feist                             const std::string* ptr =
622002d39b4SEd Tanous                                 std::get_if<std::string>(&propertyPair.second);
623b943aaefSJames Feist 
624b943aaefSJames Feist                             if (ptr == nullptr)
625b943aaefSJames Feist                             {
626b943aaefSJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
627b943aaefSJames Feist                                                  << propertyPair.first;
628b943aaefSJames Feist                                 messages::internalError(asyncResp->res);
629b943aaefSJames Feist                                 return;
630b943aaefSJames Feist                             }
631b943aaefSJames Feist                             // translate from dbus to redfish
632b943aaefSJames Feist                             if (*ptr == "WarningHigh")
633b943aaefSJames Feist                             {
634b943aaefSJames Feist                                 (*config)["SetPointOffset"] =
635b943aaefSJames Feist                                     "UpperThresholdNonCritical";
636b943aaefSJames Feist                             }
637b943aaefSJames Feist                             else if (*ptr == "WarningLow")
638b943aaefSJames Feist                             {
639b943aaefSJames Feist                                 (*config)["SetPointOffset"] =
640b943aaefSJames Feist                                     "LowerThresholdNonCritical";
641b943aaefSJames Feist                             }
642b943aaefSJames Feist                             else if (*ptr == "CriticalHigh")
643b943aaefSJames Feist                             {
644b943aaefSJames Feist                                 (*config)["SetPointOffset"] =
645b943aaefSJames Feist                                     "UpperThresholdCritical";
646b943aaefSJames Feist                             }
647b943aaefSJames Feist                             else if (*ptr == "CriticalLow")
648b943aaefSJames Feist                             {
649b943aaefSJames Feist                                 (*config)["SetPointOffset"] =
650b943aaefSJames Feist                                     "LowerThresholdCritical";
651b943aaefSJames Feist                             }
652b943aaefSJames Feist                             else
653b943aaefSJames Feist                             {
654002d39b4SEd Tanous                                 BMCWEB_LOG_ERROR << "Value Illegal " << *ptr;
655b943aaefSJames Feist                                 messages::internalError(asyncResp->res);
656b943aaefSJames Feist                                 return;
657b943aaefSJames Feist                             }
658b943aaefSJames Feist                         }
659b943aaefSJames Feist                         // doubles
660002d39b4SEd Tanous                         else if (propertyPair.first == "FFGainCoefficient" ||
6615b4aa86bSJames Feist                                  propertyPair.first == "FFOffCoefficient" ||
6625b4aa86bSJames Feist                                  propertyPair.first == "ICoefficient" ||
6635b4aa86bSJames Feist                                  propertyPair.first == "ILimitMax" ||
6645b4aa86bSJames Feist                                  propertyPair.first == "ILimitMin" ||
665002d39b4SEd Tanous                                  propertyPair.first == "PositiveHysteresis" ||
666002d39b4SEd Tanous                                  propertyPair.first == "NegativeHysteresis" ||
6675b4aa86bSJames Feist                                  propertyPair.first == "OutLimitMax" ||
6685b4aa86bSJames Feist                                  propertyPair.first == "OutLimitMin" ||
6695b4aa86bSJames Feist                                  propertyPair.first == "PCoefficient" ||
6707625cb81SJames Feist                                  propertyPair.first == "SetPoint" ||
6715b4aa86bSJames Feist                                  propertyPair.first == "SlewNeg" ||
6725b4aa86bSJames Feist                                  propertyPair.first == "SlewPos")
6735b4aa86bSJames Feist                         {
6745b4aa86bSJames Feist                             const double* ptr =
675abf2add6SEd Tanous                                 std::get_if<double>(&propertyPair.second);
6765b4aa86bSJames Feist                             if (ptr == nullptr)
6775b4aa86bSJames Feist                             {
6785b4aa86bSJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
6795b4aa86bSJames Feist                                                  << propertyPair.first;
680f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
6815b4aa86bSJames Feist                                 return;
6825b4aa86bSJames Feist                             }
683b7a08d04SJames Feist                             (*config)[propertyPair.first] = *ptr;
6845b4aa86bSJames Feist                         }
6855b4aa86bSJames Feist                     }
6865b4aa86bSJames Feist                 }
6875b4aa86bSJames Feist             }
6885b4aa86bSJames Feist         }
6895eb468daSGeorge Liu         });
6905b4aa86bSJames Feist }
691ca537928SJennifer Lee 
69283ff9ab6SJames Feist enum class CreatePIDRet
69383ff9ab6SJames Feist {
69483ff9ab6SJames Feist     fail,
69583ff9ab6SJames Feist     del,
69683ff9ab6SJames Feist     patch
69783ff9ab6SJames Feist };
69883ff9ab6SJames Feist 
6998d1b46d7Szhanghch05 inline bool
7008d1b46d7Szhanghch05     getZonesFromJsonReq(const std::shared_ptr<bmcweb::AsyncResp>& response,
7015f2caaefSJames Feist                         std::vector<nlohmann::json>& config,
7025f2caaefSJames Feist                         std::vector<std::string>& zones)
7035f2caaefSJames Feist {
704b6baeaa4SJames Feist     if (config.empty())
705b6baeaa4SJames Feist     {
706b6baeaa4SJames Feist         BMCWEB_LOG_ERROR << "Empty Zones";
707f818b04dSEd Tanous         messages::propertyValueFormatError(response->res, config, "Zones");
708b6baeaa4SJames Feist         return false;
709b6baeaa4SJames Feist     }
7105f2caaefSJames Feist     for (auto& odata : config)
7115f2caaefSJames Feist     {
7125f2caaefSJames Feist         std::string path;
7135f2caaefSJames Feist         if (!redfish::json_util::readJson(odata, response->res, "@odata.id",
7145f2caaefSJames Feist                                           path))
7155f2caaefSJames Feist         {
7165f2caaefSJames Feist             return false;
7175f2caaefSJames Feist         }
7185f2caaefSJames Feist         std::string input;
71961adbda3SJames Feist 
72061adbda3SJames Feist         // 8 below comes from
72161adbda3SJames Feist         // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
72261adbda3SJames Feist         //     0    1     2      3    4    5      6     7      8
72361adbda3SJames Feist         if (!dbus::utility::getNthStringFromPath(path, 8, input))
7245f2caaefSJames Feist         {
7255f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Got invalid path " << path;
7265f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Illegal Type Zones";
727f818b04dSEd Tanous             messages::propertyValueFormatError(response->res, odata, "Zones");
7285f2caaefSJames Feist             return false;
7295f2caaefSJames Feist         }
730a170f275SEd Tanous         std::replace(input.begin(), input.end(), '_', ' ');
7315f2caaefSJames Feist         zones.emplace_back(std::move(input));
7325f2caaefSJames Feist     }
7335f2caaefSJames Feist     return true;
7345f2caaefSJames Feist }
7355f2caaefSJames Feist 
736711ac7a9SEd Tanous inline const dbus::utility::ManagedObjectType::value_type*
73773df0db0SJames Feist     findChassis(const dbus::utility::ManagedObjectType& managedObj,
738b6baeaa4SJames Feist                 const std::string& value, std::string& chassis)
739b6baeaa4SJames Feist {
740b6baeaa4SJames Feist     BMCWEB_LOG_DEBUG << "Find Chassis: " << value << "\n";
741b6baeaa4SJames Feist 
742a170f275SEd Tanous     std::string escaped = value;
7436ce82fabSYaswanth Reddy M     std::replace(escaped.begin(), escaped.end(), ' ', '_');
744b6baeaa4SJames Feist     escaped = "/" + escaped;
745002d39b4SEd Tanous     auto it = std::find_if(managedObj.begin(), managedObj.end(),
746002d39b4SEd Tanous                            [&escaped](const auto& obj) {
747b6baeaa4SJames Feist         if (boost::algorithm::ends_with(obj.first.str, escaped))
748b6baeaa4SJames Feist         {
749b6baeaa4SJames Feist             BMCWEB_LOG_DEBUG << "Matched " << obj.first.str << "\n";
750b6baeaa4SJames Feist             return true;
751b6baeaa4SJames Feist         }
752b6baeaa4SJames Feist         return false;
753b6baeaa4SJames Feist     });
754b6baeaa4SJames Feist 
755b6baeaa4SJames Feist     if (it == managedObj.end())
756b6baeaa4SJames Feist     {
75773df0db0SJames Feist         return nullptr;
758b6baeaa4SJames Feist     }
759b6baeaa4SJames Feist     // 5 comes from <chassis-name> being the 5th element
760b6baeaa4SJames Feist     // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
76173df0db0SJames Feist     if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
76273df0db0SJames Feist     {
76373df0db0SJames Feist         return &(*it);
76473df0db0SJames Feist     }
76573df0db0SJames Feist 
76673df0db0SJames Feist     return nullptr;
767b6baeaa4SJames Feist }
768b6baeaa4SJames Feist 
76923a21a1cSEd Tanous inline CreatePIDRet createPidInterface(
7708d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& response, const std::string& type,
771b5a76932SEd Tanous     const nlohmann::json::iterator& it, const std::string& path,
77283ff9ab6SJames Feist     const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
773b9d36b47SEd Tanous     dbus::utility::DBusPropertiesMap& output, std::string& chassis,
774b9d36b47SEd Tanous     const std::string& profile)
77583ff9ab6SJames Feist {
7765f2caaefSJames Feist     // common deleter
777b6baeaa4SJames Feist     if (it.value() == nullptr)
7785f2caaefSJames Feist     {
7795f2caaefSJames Feist         std::string iface;
7805f2caaefSJames Feist         if (type == "PidControllers" || type == "FanControllers")
7815f2caaefSJames Feist         {
7825f2caaefSJames Feist             iface = pidConfigurationIface;
7835f2caaefSJames Feist         }
7845f2caaefSJames Feist         else if (type == "FanZones")
7855f2caaefSJames Feist         {
7865f2caaefSJames Feist             iface = pidZoneConfigurationIface;
7875f2caaefSJames Feist         }
7885f2caaefSJames Feist         else if (type == "StepwiseControllers")
7895f2caaefSJames Feist         {
7905f2caaefSJames Feist             iface = stepwiseConfigurationIface;
7915f2caaefSJames Feist         }
7925f2caaefSJames Feist         else
7935f2caaefSJames Feist         {
794a0744d38SGunnar Mills             BMCWEB_LOG_ERROR << "Illegal Type " << type;
7955f2caaefSJames Feist             messages::propertyUnknown(response->res, type);
7965f2caaefSJames Feist             return CreatePIDRet::fail;
7975f2caaefSJames Feist         }
7986ee7f774SJames Feist 
7996ee7f774SJames Feist         BMCWEB_LOG_DEBUG << "del " << path << " " << iface << "\n";
8005f2caaefSJames Feist         // delete interface
8015f2caaefSJames Feist         crow::connections::systemBus->async_method_call(
8025e7e2dc5SEd Tanous             [response, path](const boost::system::error_code& ec) {
8035f2caaefSJames Feist             if (ec)
8045f2caaefSJames Feist             {
8055f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Error patching " << path << ": " << ec;
8065f2caaefSJames Feist                 messages::internalError(response->res);
807b6baeaa4SJames Feist                 return;
8085f2caaefSJames Feist             }
809b6baeaa4SJames Feist             messages::success(response->res);
8105f2caaefSJames Feist             },
8115f2caaefSJames Feist             "xyz.openbmc_project.EntityManager", path, iface, "Delete");
8125f2caaefSJames Feist         return CreatePIDRet::del;
8135f2caaefSJames Feist     }
8145f2caaefSJames Feist 
815711ac7a9SEd Tanous     const dbus::utility::ManagedObjectType::value_type* managedItem = nullptr;
816b6baeaa4SJames Feist     if (!createNewObject)
817b6baeaa4SJames Feist     {
818b6baeaa4SJames Feist         // if we aren't creating a new object, we should be able to find it on
819b6baeaa4SJames Feist         // d-bus
82073df0db0SJames Feist         managedItem = findChassis(managedObj, it.key(), chassis);
82173df0db0SJames Feist         if (managedItem == nullptr)
822b6baeaa4SJames Feist         {
823b6baeaa4SJames Feist             BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
824ef4c65b7SEd Tanous             messages::invalidObject(
825ef4c65b7SEd Tanous                 response->res,
826ef4c65b7SEd Tanous                 boost::urls::format("/redfish/v1/Chassis/{}", chassis));
827b6baeaa4SJames Feist             return CreatePIDRet::fail;
828b6baeaa4SJames Feist         }
829b6baeaa4SJames Feist     }
830b6baeaa4SJames Feist 
83126f6976fSEd Tanous     if (!profile.empty() &&
83273df0db0SJames Feist         (type == "PidControllers" || type == "FanControllers" ||
83373df0db0SJames Feist          type == "StepwiseControllers"))
83473df0db0SJames Feist     {
83573df0db0SJames Feist         if (managedItem == nullptr)
83673df0db0SJames Feist         {
837b9d36b47SEd Tanous             output.emplace_back("Profiles", std::vector<std::string>{profile});
83873df0db0SJames Feist         }
83973df0db0SJames Feist         else
84073df0db0SJames Feist         {
84173df0db0SJames Feist             std::string interface;
84273df0db0SJames Feist             if (type == "StepwiseControllers")
84373df0db0SJames Feist             {
84473df0db0SJames Feist                 interface = stepwiseConfigurationIface;
84573df0db0SJames Feist             }
84673df0db0SJames Feist             else
84773df0db0SJames Feist             {
84873df0db0SJames Feist                 interface = pidConfigurationIface;
84973df0db0SJames Feist             }
850711ac7a9SEd Tanous             bool ifaceFound = false;
851711ac7a9SEd Tanous             for (const auto& iface : managedItem->second)
852711ac7a9SEd Tanous             {
853711ac7a9SEd Tanous                 if (iface.first == interface)
854711ac7a9SEd Tanous                 {
855711ac7a9SEd Tanous                     ifaceFound = true;
856711ac7a9SEd Tanous                     for (const auto& prop : iface.second)
857711ac7a9SEd Tanous                     {
858711ac7a9SEd Tanous                         if (prop.first == "Profiles")
859711ac7a9SEd Tanous                         {
860711ac7a9SEd Tanous                             const std::vector<std::string>* curProfiles =
861711ac7a9SEd Tanous                                 std::get_if<std::vector<std::string>>(
862711ac7a9SEd Tanous                                     &(prop.second));
863711ac7a9SEd Tanous                             if (curProfiles == nullptr)
864711ac7a9SEd Tanous                             {
865711ac7a9SEd Tanous                                 BMCWEB_LOG_ERROR
866711ac7a9SEd Tanous                                     << "Illegal profiles in managed object";
867711ac7a9SEd Tanous                                 messages::internalError(response->res);
868711ac7a9SEd Tanous                                 return CreatePIDRet::fail;
869711ac7a9SEd Tanous                             }
870711ac7a9SEd Tanous                             if (std::find(curProfiles->begin(),
871711ac7a9SEd Tanous                                           curProfiles->end(),
872711ac7a9SEd Tanous                                           profile) == curProfiles->end())
873711ac7a9SEd Tanous                             {
874711ac7a9SEd Tanous                                 std::vector<std::string> newProfiles =
875711ac7a9SEd Tanous                                     *curProfiles;
876711ac7a9SEd Tanous                                 newProfiles.push_back(profile);
877b9d36b47SEd Tanous                                 output.emplace_back("Profiles", newProfiles);
878711ac7a9SEd Tanous                             }
879711ac7a9SEd Tanous                         }
880711ac7a9SEd Tanous                     }
881711ac7a9SEd Tanous                 }
882711ac7a9SEd Tanous             }
883711ac7a9SEd Tanous 
884711ac7a9SEd Tanous             if (!ifaceFound)
88573df0db0SJames Feist             {
88673df0db0SJames Feist                 BMCWEB_LOG_ERROR
88773df0db0SJames Feist                     << "Failed to find interface in managed object";
88873df0db0SJames Feist                 messages::internalError(response->res);
88973df0db0SJames Feist                 return CreatePIDRet::fail;
89073df0db0SJames Feist             }
89173df0db0SJames Feist         }
89273df0db0SJames Feist     }
89373df0db0SJames Feist 
89483ff9ab6SJames Feist     if (type == "PidControllers" || type == "FanControllers")
89583ff9ab6SJames Feist     {
89683ff9ab6SJames Feist         if (createNewObject)
89783ff9ab6SJames Feist         {
898b9d36b47SEd Tanous             output.emplace_back("Class",
899b9d36b47SEd Tanous                                 type == "PidControllers" ? "temp" : "fan");
900b9d36b47SEd Tanous             output.emplace_back("Type", "Pid");
90183ff9ab6SJames Feist         }
9025f2caaefSJames Feist 
9035f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
9045f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
9055f2caaefSJames Feist         std::optional<std::vector<std::string>> outputs;
9065f2caaefSJames Feist         std::map<std::string, std::optional<double>> doubles;
907b943aaefSJames Feist         std::optional<std::string> setpointOffset;
9085f2caaefSJames Feist         if (!redfish::json_util::readJson(
909b6baeaa4SJames Feist                 it.value(), response->res, "Inputs", inputs, "Outputs", outputs,
9105f2caaefSJames Feist                 "Zones", zones, "FFGainCoefficient",
9115f2caaefSJames Feist                 doubles["FFGainCoefficient"], "FFOffCoefficient",
9125f2caaefSJames Feist                 doubles["FFOffCoefficient"], "ICoefficient",
9135f2caaefSJames Feist                 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
9145f2caaefSJames Feist                 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
9155f2caaefSJames Feist                 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
9165f2caaefSJames Feist                 "PCoefficient", doubles["PCoefficient"], "SetPoint",
917b943aaefSJames Feist                 doubles["SetPoint"], "SetPointOffset", setpointOffset,
918b943aaefSJames Feist                 "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"],
919b943aaefSJames Feist                 "PositiveHysteresis", doubles["PositiveHysteresis"],
920b943aaefSJames Feist                 "NegativeHysteresis", doubles["NegativeHysteresis"]))
92183ff9ab6SJames Feist         {
9225f2caaefSJames Feist             return CreatePIDRet::fail;
92383ff9ab6SJames Feist         }
9245f2caaefSJames Feist         if (zones)
9255f2caaefSJames Feist         {
9265f2caaefSJames Feist             std::vector<std::string> zonesStr;
9275f2caaefSJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStr))
9285f2caaefSJames Feist             {
929a0744d38SGunnar Mills                 BMCWEB_LOG_ERROR << "Illegal Zones";
9305f2caaefSJames Feist                 return CreatePIDRet::fail;
9315f2caaefSJames Feist             }
932b6baeaa4SJames Feist             if (chassis.empty() &&
933e662eae8SEd Tanous                 findChassis(managedObj, zonesStr[0], chassis) == nullptr)
934b6baeaa4SJames Feist             {
935b6baeaa4SJames Feist                 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
936ace85d60SEd Tanous                 messages::invalidObject(
937ef4c65b7SEd Tanous                     response->res,
938ef4c65b7SEd Tanous                     boost::urls::format("/redfish/v1/Chassis/{}", chassis));
939b6baeaa4SJames Feist                 return CreatePIDRet::fail;
940b6baeaa4SJames Feist             }
941b9d36b47SEd Tanous             output.emplace_back("Zones", std::move(zonesStr));
9425f2caaefSJames Feist         }
943afb9ee06SEd Tanous 
944afb9ee06SEd Tanous         if (inputs)
9455f2caaefSJames Feist         {
946afb9ee06SEd Tanous             for (std::string& value : *inputs)
94783ff9ab6SJames Feist             {
948a170f275SEd Tanous                 std::replace(value.begin(), value.end(), '_', ' ');
94983ff9ab6SJames Feist             }
950afb9ee06SEd Tanous             output.emplace_back("Inputs", *inputs);
951afb9ee06SEd Tanous         }
952afb9ee06SEd Tanous 
953afb9ee06SEd Tanous         if (outputs)
9545f2caaefSJames Feist         {
955afb9ee06SEd Tanous             for (std::string& value : *outputs)
9565f2caaefSJames Feist             {
957afb9ee06SEd Tanous                 std::replace(value.begin(), value.end(), '_', ' ');
9585f2caaefSJames Feist             }
959afb9ee06SEd Tanous             output.emplace_back("Outputs", *outputs);
96083ff9ab6SJames Feist         }
96183ff9ab6SJames Feist 
962b943aaefSJames Feist         if (setpointOffset)
963b943aaefSJames Feist         {
964b943aaefSJames Feist             // translate between redfish and dbus names
965b943aaefSJames Feist             if (*setpointOffset == "UpperThresholdNonCritical")
966b943aaefSJames Feist             {
967b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "WarningLow");
968b943aaefSJames Feist             }
969b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdNonCritical")
970b943aaefSJames Feist             {
971b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "WarningHigh");
972b943aaefSJames Feist             }
973b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdCritical")
974b943aaefSJames Feist             {
975b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "CriticalLow");
976b943aaefSJames Feist             }
977b943aaefSJames Feist             else if (*setpointOffset == "UpperThresholdCritical")
978b943aaefSJames Feist             {
979b9d36b47SEd Tanous                 output.emplace_back("SetPointOffset", "CriticalHigh");
980b943aaefSJames Feist             }
981b943aaefSJames Feist             else
982b943aaefSJames Feist             {
983b943aaefSJames Feist                 BMCWEB_LOG_ERROR << "Invalid setpointoffset "
984b943aaefSJames Feist                                  << *setpointOffset;
985ace85d60SEd Tanous                 messages::propertyValueNotInList(response->res, it.key(),
986ace85d60SEd Tanous                                                  "SetPointOffset");
987b943aaefSJames Feist                 return CreatePIDRet::fail;
988b943aaefSJames Feist             }
989b943aaefSJames Feist         }
990b943aaefSJames Feist 
99183ff9ab6SJames Feist         // doubles
9925f2caaefSJames Feist         for (const auto& pairs : doubles)
99383ff9ab6SJames Feist         {
9945f2caaefSJames Feist             if (!pairs.second)
99583ff9ab6SJames Feist             {
9965f2caaefSJames Feist                 continue;
99783ff9ab6SJames Feist             }
9985f2caaefSJames Feist             BMCWEB_LOG_DEBUG << pairs.first << " = " << *pairs.second;
999b9d36b47SEd Tanous             output.emplace_back(pairs.first, *pairs.second);
10005f2caaefSJames Feist         }
100183ff9ab6SJames Feist     }
100283ff9ab6SJames Feist 
100383ff9ab6SJames Feist     else if (type == "FanZones")
100483ff9ab6SJames Feist     {
1005b9d36b47SEd Tanous         output.emplace_back("Type", "Pid.Zone");
100683ff9ab6SJames Feist 
10075f2caaefSJames Feist         std::optional<nlohmann::json> chassisContainer;
10085f2caaefSJames Feist         std::optional<double> failSafePercent;
1009d3ec07f8SJames Feist         std::optional<double> minThermalOutput;
1010b6baeaa4SJames Feist         if (!redfish::json_util::readJson(it.value(), response->res, "Chassis",
10115f2caaefSJames Feist                                           chassisContainer, "FailSafePercent",
1012d3ec07f8SJames Feist                                           failSafePercent, "MinThermalOutput",
1013d3ec07f8SJames Feist                                           minThermalOutput))
101483ff9ab6SJames Feist         {
101583ff9ab6SJames Feist             return CreatePIDRet::fail;
101683ff9ab6SJames Feist         }
10175f2caaefSJames Feist 
10185f2caaefSJames Feist         if (chassisContainer)
101983ff9ab6SJames Feist         {
10205f2caaefSJames Feist             std::string chassisId;
10215f2caaefSJames Feist             if (!redfish::json_util::readJson(*chassisContainer, response->res,
10225f2caaefSJames Feist                                               "@odata.id", chassisId))
10235f2caaefSJames Feist             {
102483ff9ab6SJames Feist                 return CreatePIDRet::fail;
102583ff9ab6SJames Feist             }
102683ff9ab6SJames Feist 
1027717794d5SAppaRao Puli             // /redfish/v1/chassis/chassis_name/
10285f2caaefSJames Feist             if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis))
102983ff9ab6SJames Feist             {
10305f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Got invalid path " << chassisId;
1031ace85d60SEd Tanous                 messages::invalidObject(
1032ef4c65b7SEd Tanous                     response->res,
1033ef4c65b7SEd Tanous                     boost::urls::format("/redfish/v1/Chassis/{}", chassisId));
103483ff9ab6SJames Feist                 return CreatePIDRet::fail;
103583ff9ab6SJames Feist             }
103683ff9ab6SJames Feist         }
1037d3ec07f8SJames Feist         if (minThermalOutput)
103883ff9ab6SJames Feist         {
1039b9d36b47SEd Tanous             output.emplace_back("MinThermalOutput", *minThermalOutput);
10405f2caaefSJames Feist         }
10415f2caaefSJames Feist         if (failSafePercent)
104283ff9ab6SJames Feist         {
1043b9d36b47SEd Tanous             output.emplace_back("FailSafePercent", *failSafePercent);
10445f2caaefSJames Feist         }
10455f2caaefSJames Feist     }
10465f2caaefSJames Feist     else if (type == "StepwiseControllers")
10475f2caaefSJames Feist     {
1048b9d36b47SEd Tanous         output.emplace_back("Type", "Stepwise");
10495f2caaefSJames Feist 
10505f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
10515f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> steps;
10525f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
10535f2caaefSJames Feist         std::optional<double> positiveHysteresis;
10545f2caaefSJames Feist         std::optional<double> negativeHysteresis;
1055c33a90ecSJames Feist         std::optional<std::string> direction; // upper clipping curve vs lower
10565f2caaefSJames Feist         if (!redfish::json_util::readJson(
1057b6baeaa4SJames Feist                 it.value(), response->res, "Zones", zones, "Steps", steps,
1058b6baeaa4SJames Feist                 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
1059c33a90ecSJames Feist                 "NegativeHysteresis", negativeHysteresis, "Direction",
1060c33a90ecSJames Feist                 direction))
10615f2caaefSJames Feist         {
106283ff9ab6SJames Feist             return CreatePIDRet::fail;
106383ff9ab6SJames Feist         }
10645f2caaefSJames Feist 
10655f2caaefSJames Feist         if (zones)
106683ff9ab6SJames Feist         {
1067b6baeaa4SJames Feist             std::vector<std::string> zonesStrs;
1068b6baeaa4SJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStrs))
10695f2caaefSJames Feist             {
1070a0744d38SGunnar Mills                 BMCWEB_LOG_ERROR << "Illegal Zones";
107183ff9ab6SJames Feist                 return CreatePIDRet::fail;
107283ff9ab6SJames Feist             }
1073b6baeaa4SJames Feist             if (chassis.empty() &&
1074e662eae8SEd Tanous                 findChassis(managedObj, zonesStrs[0], chassis) == nullptr)
1075b6baeaa4SJames Feist             {
1076b6baeaa4SJames Feist                 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
1077ace85d60SEd Tanous                 messages::invalidObject(
1078ef4c65b7SEd Tanous                     response->res,
1079ef4c65b7SEd Tanous                     boost::urls::format("/redfish/v1/Chassis/{}", chassis));
1080b6baeaa4SJames Feist                 return CreatePIDRet::fail;
1081b6baeaa4SJames Feist             }
1082b9d36b47SEd Tanous             output.emplace_back("Zones", std::move(zonesStrs));
10835f2caaefSJames Feist         }
10845f2caaefSJames Feist         if (steps)
10855f2caaefSJames Feist         {
10865f2caaefSJames Feist             std::vector<double> readings;
10875f2caaefSJames Feist             std::vector<double> outputs;
10885f2caaefSJames Feist             for (auto& step : *steps)
10895f2caaefSJames Feist             {
1090543f4400SEd Tanous                 double target = 0.0;
1091543f4400SEd Tanous                 double out = 0.0;
10925f2caaefSJames Feist 
10935f2caaefSJames Feist                 if (!redfish::json_util::readJson(step, response->res, "Target",
109423a21a1cSEd Tanous                                                   target, "Output", out))
10955f2caaefSJames Feist                 {
10965f2caaefSJames Feist                     return CreatePIDRet::fail;
10975f2caaefSJames Feist                 }
10985f2caaefSJames Feist                 readings.emplace_back(target);
109923a21a1cSEd Tanous                 outputs.emplace_back(out);
11005f2caaefSJames Feist             }
1101b9d36b47SEd Tanous             output.emplace_back("Reading", std::move(readings));
1102b9d36b47SEd Tanous             output.emplace_back("Output", std::move(outputs));
11035f2caaefSJames Feist         }
11045f2caaefSJames Feist         if (inputs)
11055f2caaefSJames Feist         {
11065f2caaefSJames Feist             for (std::string& value : *inputs)
11075f2caaefSJames Feist             {
1108a170f275SEd Tanous                 std::replace(value.begin(), value.end(), '_', ' ');
11095f2caaefSJames Feist             }
1110b9d36b47SEd Tanous             output.emplace_back("Inputs", std::move(*inputs));
11115f2caaefSJames Feist         }
11125f2caaefSJames Feist         if (negativeHysteresis)
11135f2caaefSJames Feist         {
1114b9d36b47SEd Tanous             output.emplace_back("NegativeHysteresis", *negativeHysteresis);
11155f2caaefSJames Feist         }
11165f2caaefSJames Feist         if (positiveHysteresis)
11175f2caaefSJames Feist         {
1118b9d36b47SEd Tanous             output.emplace_back("PositiveHysteresis", *positiveHysteresis);
111983ff9ab6SJames Feist         }
1120c33a90ecSJames Feist         if (direction)
1121c33a90ecSJames Feist         {
1122c33a90ecSJames Feist             constexpr const std::array<const char*, 2> allowedDirections = {
1123c33a90ecSJames Feist                 "Ceiling", "Floor"};
1124c33a90ecSJames Feist             if (std::find(allowedDirections.begin(), allowedDirections.end(),
1125c33a90ecSJames Feist                           *direction) == allowedDirections.end())
1126c33a90ecSJames Feist             {
1127c33a90ecSJames Feist                 messages::propertyValueTypeError(response->res, "Direction",
1128c33a90ecSJames Feist                                                  *direction);
1129c33a90ecSJames Feist                 return CreatePIDRet::fail;
1130c33a90ecSJames Feist             }
1131b9d36b47SEd Tanous             output.emplace_back("Class", *direction);
1132c33a90ecSJames Feist         }
113383ff9ab6SJames Feist     }
113483ff9ab6SJames Feist     else
113583ff9ab6SJames Feist     {
1136a0744d38SGunnar Mills         BMCWEB_LOG_ERROR << "Illegal Type " << type;
113735a62c7cSJason M. Bills         messages::propertyUnknown(response->res, type);
113883ff9ab6SJames Feist         return CreatePIDRet::fail;
113983ff9ab6SJames Feist     }
114083ff9ab6SJames Feist     return CreatePIDRet::patch;
114183ff9ab6SJames Feist }
114273df0db0SJames Feist struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
114373df0db0SJames Feist {
11446936afe4SEd Tanous     struct CompletionValues
11456936afe4SEd Tanous     {
11466936afe4SEd Tanous         std::vector<std::string> supportedProfiles;
11476936afe4SEd Tanous         std::string currentProfile;
11486936afe4SEd Tanous         dbus::utility::MapperGetSubTreeResponse subtree;
11496936afe4SEd Tanous     };
115083ff9ab6SJames Feist 
11514e23a444SEd Tanous     explicit GetPIDValues(
11524e23a444SEd Tanous         const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
115323a21a1cSEd Tanous         asyncResp(asyncRespIn)
115473df0db0SJames Feist 
11551214b7e7SGunnar Mills     {}
11569c310685SBorawski.Lukasz 
115773df0db0SJames Feist     void run()
11585b4aa86bSJames Feist     {
115973df0db0SJames Feist         std::shared_ptr<GetPIDValues> self = shared_from_this();
116073df0db0SJames Feist 
116173df0db0SJames Feist         // get all configurations
1162e99073f5SGeorge Liu         constexpr std::array<std::string_view, 4> interfaces = {
1163e99073f5SGeorge Liu             pidConfigurationIface, pidZoneConfigurationIface,
1164e99073f5SGeorge Liu             objectManagerIface, stepwiseConfigurationIface};
1165e99073f5SGeorge Liu         dbus::utility::getSubTree(
1166e99073f5SGeorge Liu             "/", 0, interfaces,
1167b9d36b47SEd Tanous             [self](
1168e99073f5SGeorge Liu                 const boost::system::error_code& ec,
1169b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
11705b4aa86bSJames Feist             if (ec)
11715b4aa86bSJames Feist             {
11725b4aa86bSJames Feist                 BMCWEB_LOG_ERROR << ec;
117373df0db0SJames Feist                 messages::internalError(self->asyncResp->res);
117473df0db0SJames Feist                 return;
117573df0db0SJames Feist             }
11766936afe4SEd Tanous             self->complete.subtree = subtreeLocal;
1177e99073f5SGeorge Liu             });
117873df0db0SJames Feist 
117973df0db0SJames Feist         // at the same time get the selected profile
1180e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1181e99073f5SGeorge Liu             thermalModeIface};
1182e99073f5SGeorge Liu         dbus::utility::getSubTree(
1183e99073f5SGeorge Liu             "/", 0, thermalModeIfaces,
1184b9d36b47SEd Tanous             [self](
1185e99073f5SGeorge Liu                 const boost::system::error_code& ec,
1186b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtreeLocal) {
118723a21a1cSEd Tanous             if (ec || subtreeLocal.empty())
118873df0db0SJames Feist             {
118973df0db0SJames Feist                 return;
119073df0db0SJames Feist             }
119123a21a1cSEd Tanous             if (subtreeLocal[0].second.size() != 1)
119273df0db0SJames Feist             {
119373df0db0SJames Feist                 // invalid mapper response, should never happen
119473df0db0SJames Feist                 BMCWEB_LOG_ERROR << "GetPIDValues: Mapper Error";
119573df0db0SJames Feist                 messages::internalError(self->asyncResp->res);
11965b4aa86bSJames Feist                 return;
11975b4aa86bSJames Feist             }
11985b4aa86bSJames Feist 
119923a21a1cSEd Tanous             const std::string& path = subtreeLocal[0].first;
120023a21a1cSEd Tanous             const std::string& owner = subtreeLocal[0].second[0].first;
1201fac6e53bSKrzysztof Grobelny 
1202fac6e53bSKrzysztof Grobelny             sdbusplus::asio::getAllProperties(
1203fac6e53bSKrzysztof Grobelny                 *crow::connections::systemBus, owner, path, thermalModeIface,
1204168e20c1SEd Tanous                 [path, owner,
12055e7e2dc5SEd Tanous                  self](const boost::system::error_code& ec2,
1206b9d36b47SEd Tanous                        const dbus::utility::DBusPropertiesMap& resp) {
120723a21a1cSEd Tanous                 if (ec2)
120873df0db0SJames Feist                 {
12090fda0f12SGeorge Liu                     BMCWEB_LOG_ERROR
1210002d39b4SEd Tanous                         << "GetPIDValues: Can't get thermalModeIface " << path;
121173df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
121273df0db0SJames Feist                     return;
121373df0db0SJames Feist                 }
1214fac6e53bSKrzysztof Grobelny 
1215271584abSEd Tanous                 const std::string* current = nullptr;
1216271584abSEd Tanous                 const std::vector<std::string>* supported = nullptr;
1217fac6e53bSKrzysztof Grobelny 
1218fac6e53bSKrzysztof Grobelny                 const bool success = sdbusplus::unpackPropertiesNoThrow(
1219fac6e53bSKrzysztof Grobelny                     dbus_utils::UnpackErrorPrinter(), resp, "Current", current,
1220fac6e53bSKrzysztof Grobelny                     "Supported", supported);
1221fac6e53bSKrzysztof Grobelny 
1222fac6e53bSKrzysztof Grobelny                 if (!success)
122373df0db0SJames Feist                 {
1224002d39b4SEd Tanous                     messages::internalError(self->asyncResp->res);
122573df0db0SJames Feist                     return;
122673df0db0SJames Feist                 }
1227fac6e53bSKrzysztof Grobelny 
122873df0db0SJames Feist                 if (current == nullptr || supported == nullptr)
122973df0db0SJames Feist                 {
12300fda0f12SGeorge Liu                     BMCWEB_LOG_ERROR
1231002d39b4SEd Tanous                         << "GetPIDValues: thermal mode iface invalid " << path;
123273df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
123373df0db0SJames Feist                     return;
123473df0db0SJames Feist                 }
12356936afe4SEd Tanous                 self->complete.currentProfile = *current;
12366936afe4SEd Tanous                 self->complete.supportedProfiles = *supported;
1237fac6e53bSKrzysztof Grobelny                 });
1238e99073f5SGeorge Liu             });
123973df0db0SJames Feist     }
124073df0db0SJames Feist 
12416936afe4SEd Tanous     static void
12426936afe4SEd Tanous         processingComplete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
12436936afe4SEd Tanous                            const CompletionValues& completion)
124473df0db0SJames Feist     {
124573df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
124673df0db0SJames Feist         {
124773df0db0SJames Feist             return;
124873df0db0SJames Feist         }
12495b4aa86bSJames Feist         // create map of <connection, path to objMgr>>
12506936afe4SEd Tanous         boost::container::flat_map<
12516936afe4SEd Tanous             std::string, std::string, std::less<>,
12526936afe4SEd Tanous             std::vector<std::pair<std::string, std::string>>>
12536936afe4SEd Tanous             objectMgrPaths;
12546936afe4SEd Tanous         boost::container::flat_set<std::string, std::less<>,
12556936afe4SEd Tanous                                    std::vector<std::string>>
12566936afe4SEd Tanous             calledConnections;
12576936afe4SEd Tanous         for (const auto& pathGroup : completion.subtree)
12585b4aa86bSJames Feist         {
12595b4aa86bSJames Feist             for (const auto& connectionGroup : pathGroup.second)
12605b4aa86bSJames Feist             {
12616bce33bcSJames Feist                 auto findConnection =
12626bce33bcSJames Feist                     calledConnections.find(connectionGroup.first);
12636bce33bcSJames Feist                 if (findConnection != calledConnections.end())
12646bce33bcSJames Feist                 {
12656bce33bcSJames Feist                     break;
12666bce33bcSJames Feist                 }
126773df0db0SJames Feist                 for (const std::string& interface : connectionGroup.second)
12685b4aa86bSJames Feist                 {
12695b4aa86bSJames Feist                     if (interface == objectManagerIface)
12705b4aa86bSJames Feist                     {
127173df0db0SJames Feist                         objectMgrPaths[connectionGroup.first] = pathGroup.first;
12725b4aa86bSJames Feist                     }
12735b4aa86bSJames Feist                     // this list is alphabetical, so we
12745b4aa86bSJames Feist                     // should have found the objMgr by now
12755b4aa86bSJames Feist                     if (interface == pidConfigurationIface ||
1276b7a08d04SJames Feist                         interface == pidZoneConfigurationIface ||
1277b7a08d04SJames Feist                         interface == stepwiseConfigurationIface)
12785b4aa86bSJames Feist                     {
12795b4aa86bSJames Feist                         auto findObjMgr =
12805b4aa86bSJames Feist                             objectMgrPaths.find(connectionGroup.first);
12815b4aa86bSJames Feist                         if (findObjMgr == objectMgrPaths.end())
12825b4aa86bSJames Feist                         {
12835b4aa86bSJames Feist                             BMCWEB_LOG_DEBUG << connectionGroup.first
12845b4aa86bSJames Feist                                              << "Has no Object Manager";
12855b4aa86bSJames Feist                             continue;
12865b4aa86bSJames Feist                         }
12876bce33bcSJames Feist 
12886bce33bcSJames Feist                         calledConnections.insert(connectionGroup.first);
12896bce33bcSJames Feist 
129073df0db0SJames Feist                         asyncPopulatePid(findObjMgr->first, findObjMgr->second,
12916936afe4SEd Tanous                                          completion.currentProfile,
12926936afe4SEd Tanous                                          completion.supportedProfiles,
129373df0db0SJames Feist                                          asyncResp);
12945b4aa86bSJames Feist                         break;
12955b4aa86bSJames Feist                     }
12965b4aa86bSJames Feist                 }
12975b4aa86bSJames Feist             }
12985b4aa86bSJames Feist         }
129973df0db0SJames Feist     }
130073df0db0SJames Feist 
13016936afe4SEd Tanous     ~GetPIDValues()
13026936afe4SEd Tanous     {
13036936afe4SEd Tanous         boost::asio::post(crow::connections::systemBus->get_io_context(),
13046936afe4SEd Tanous                           std::bind_front(&processingComplete, asyncResp,
13056936afe4SEd Tanous                                           std::move(complete)));
13066936afe4SEd Tanous     }
13076936afe4SEd Tanous 
1308ecd6a3a2SEd Tanous     GetPIDValues(const GetPIDValues&) = delete;
1309ecd6a3a2SEd Tanous     GetPIDValues(GetPIDValues&&) = delete;
1310ecd6a3a2SEd Tanous     GetPIDValues& operator=(const GetPIDValues&) = delete;
1311ecd6a3a2SEd Tanous     GetPIDValues& operator=(GetPIDValues&&) = delete;
1312ecd6a3a2SEd Tanous 
13138d1b46d7Szhanghch05     std::shared_ptr<bmcweb::AsyncResp> asyncResp;
13146936afe4SEd Tanous     CompletionValues complete;
131573df0db0SJames Feist };
131673df0db0SJames Feist 
131773df0db0SJames Feist struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
131873df0db0SJames Feist {
13198d1b46d7Szhanghch05     SetPIDValues(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
132073df0db0SJames Feist                  nlohmann::json& data) :
1321271584abSEd Tanous         asyncResp(asyncRespIn)
132273df0db0SJames Feist     {
132373df0db0SJames Feist         std::optional<nlohmann::json> pidControllers;
132473df0db0SJames Feist         std::optional<nlohmann::json> fanControllers;
132573df0db0SJames Feist         std::optional<nlohmann::json> fanZones;
132673df0db0SJames Feist         std::optional<nlohmann::json> stepwiseControllers;
132773df0db0SJames Feist 
132873df0db0SJames Feist         if (!redfish::json_util::readJson(
132973df0db0SJames Feist                 data, asyncResp->res, "PidControllers", pidControllers,
133073df0db0SJames Feist                 "FanControllers", fanControllers, "FanZones", fanZones,
133173df0db0SJames Feist                 "StepwiseControllers", stepwiseControllers, "Profile", profile))
133273df0db0SJames Feist         {
133373df0db0SJames Feist             return;
133473df0db0SJames Feist         }
133573df0db0SJames Feist         configuration.emplace_back("PidControllers", std::move(pidControllers));
133673df0db0SJames Feist         configuration.emplace_back("FanControllers", std::move(fanControllers));
133773df0db0SJames Feist         configuration.emplace_back("FanZones", std::move(fanZones));
133873df0db0SJames Feist         configuration.emplace_back("StepwiseControllers",
133973df0db0SJames Feist                                    std::move(stepwiseControllers));
134073df0db0SJames Feist     }
1341ecd6a3a2SEd Tanous 
1342ecd6a3a2SEd Tanous     SetPIDValues(const SetPIDValues&) = delete;
1343ecd6a3a2SEd Tanous     SetPIDValues(SetPIDValues&&) = delete;
1344ecd6a3a2SEd Tanous     SetPIDValues& operator=(const SetPIDValues&) = delete;
1345ecd6a3a2SEd Tanous     SetPIDValues& operator=(SetPIDValues&&) = delete;
1346ecd6a3a2SEd Tanous 
134773df0db0SJames Feist     void run()
134873df0db0SJames Feist     {
134973df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
135073df0db0SJames Feist         {
135173df0db0SJames Feist             return;
135273df0db0SJames Feist         }
135373df0db0SJames Feist 
135473df0db0SJames Feist         std::shared_ptr<SetPIDValues> self = shared_from_this();
135573df0db0SJames Feist 
135673df0db0SJames Feist         // todo(james): might make sense to do a mapper call here if this
135773df0db0SJames Feist         // interface gets more traction
13585eb468daSGeorge Liu         sdbusplus::message::object_path objPath(
13595eb468daSGeorge Liu             "/xyz/openbmc_project/inventory");
13605eb468daSGeorge Liu         dbus::utility::getManagedObjects(
13615eb468daSGeorge Liu             "xyz.openbmc_project.EntityManager", objPath,
13625e7e2dc5SEd Tanous             [self](const boost::system::error_code& ec,
1363914e2d5dSEd Tanous                    const dbus::utility::ManagedObjectType& mObj) {
136473df0db0SJames Feist             if (ec)
136573df0db0SJames Feist             {
136673df0db0SJames Feist                 BMCWEB_LOG_ERROR << "Error communicating to Entity Manager";
136773df0db0SJames Feist                 messages::internalError(self->asyncResp->res);
136873df0db0SJames Feist                 return;
136973df0db0SJames Feist             }
1370e69d9de2SJames Feist             const std::array<const char*, 3> configurations = {
1371e69d9de2SJames Feist                 pidConfigurationIface, pidZoneConfigurationIface,
1372e69d9de2SJames Feist                 stepwiseConfigurationIface};
1373e69d9de2SJames Feist 
137414b0b8d5SJames Feist             for (const auto& [path, object] : mObj)
1375e69d9de2SJames Feist             {
137614b0b8d5SJames Feist                 for (const auto& [interface, _] : object)
1377e69d9de2SJames Feist                 {
1378002d39b4SEd Tanous                     if (std::find(configurations.begin(), configurations.end(),
1379e69d9de2SJames Feist                                   interface) != configurations.end())
1380e69d9de2SJames Feist                     {
138114b0b8d5SJames Feist                         self->objectCount++;
1382e69d9de2SJames Feist                         break;
1383e69d9de2SJames Feist                     }
1384e69d9de2SJames Feist                 }
1385e69d9de2SJames Feist             }
1386914e2d5dSEd Tanous             self->managedObj = mObj;
13875eb468daSGeorge Liu             });
138873df0db0SJames Feist 
138973df0db0SJames Feist         // at the same time get the profile information
1390e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> thermalModeIfaces = {
1391e99073f5SGeorge Liu             thermalModeIface};
1392e99073f5SGeorge Liu         dbus::utility::getSubTree(
1393e99073f5SGeorge Liu             "/", 0, thermalModeIfaces,
1394e99073f5SGeorge Liu             [self](const boost::system::error_code& ec,
1395b9d36b47SEd Tanous                    const dbus::utility::MapperGetSubTreeResponse& subtree) {
139673df0db0SJames Feist             if (ec || subtree.empty())
139773df0db0SJames Feist             {
139873df0db0SJames Feist                 return;
139973df0db0SJames Feist             }
140073df0db0SJames Feist             if (subtree[0].second.empty())
140173df0db0SJames Feist             {
140273df0db0SJames Feist                 // invalid mapper response, should never happen
140373df0db0SJames Feist                 BMCWEB_LOG_ERROR << "SetPIDValues: Mapper Error";
140473df0db0SJames Feist                 messages::internalError(self->asyncResp->res);
140573df0db0SJames Feist                 return;
140673df0db0SJames Feist             }
140773df0db0SJames Feist 
140873df0db0SJames Feist             const std::string& path = subtree[0].first;
140973df0db0SJames Feist             const std::string& owner = subtree[0].second[0].first;
1410fac6e53bSKrzysztof Grobelny             sdbusplus::asio::getAllProperties(
1411fac6e53bSKrzysztof Grobelny                 *crow::connections::systemBus, owner, path, thermalModeIface,
14125e7e2dc5SEd Tanous                 [self, path, owner](const boost::system::error_code& ec2,
1413b9d36b47SEd Tanous                                     const dbus::utility::DBusPropertiesMap& r) {
1414cb13a392SEd Tanous                 if (ec2)
141573df0db0SJames Feist                 {
14160fda0f12SGeorge Liu                     BMCWEB_LOG_ERROR
1417002d39b4SEd Tanous                         << "SetPIDValues: Can't get thermalModeIface " << path;
141873df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
141973df0db0SJames Feist                     return;
142073df0db0SJames Feist                 }
1421271584abSEd Tanous                 const std::string* current = nullptr;
1422271584abSEd Tanous                 const std::vector<std::string>* supported = nullptr;
1423fac6e53bSKrzysztof Grobelny 
1424fac6e53bSKrzysztof Grobelny                 const bool success = sdbusplus::unpackPropertiesNoThrow(
1425fac6e53bSKrzysztof Grobelny                     dbus_utils::UnpackErrorPrinter(), r, "Current", current,
1426fac6e53bSKrzysztof Grobelny                     "Supported", supported);
1427fac6e53bSKrzysztof Grobelny 
1428fac6e53bSKrzysztof Grobelny                 if (!success)
142973df0db0SJames Feist                 {
1430002d39b4SEd Tanous                     messages::internalError(self->asyncResp->res);
143173df0db0SJames Feist                     return;
143273df0db0SJames Feist                 }
1433fac6e53bSKrzysztof Grobelny 
143473df0db0SJames Feist                 if (current == nullptr || supported == nullptr)
143573df0db0SJames Feist                 {
14360fda0f12SGeorge Liu                     BMCWEB_LOG_ERROR
1437002d39b4SEd Tanous                         << "SetPIDValues: thermal mode iface invalid " << path;
143873df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
143973df0db0SJames Feist                     return;
144073df0db0SJames Feist                 }
144173df0db0SJames Feist                 self->currentProfile = *current;
144273df0db0SJames Feist                 self->supportedProfiles = *supported;
144373df0db0SJames Feist                 self->profileConnection = owner;
144473df0db0SJames Feist                 self->profilePath = path;
1445fac6e53bSKrzysztof Grobelny                 });
1446e99073f5SGeorge Liu             });
144773df0db0SJames Feist     }
144824b2fe81SEd Tanous     void pidSetDone()
144973df0db0SJames Feist     {
145073df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
145173df0db0SJames Feist         {
145273df0db0SJames Feist             return;
14535b4aa86bSJames Feist         }
14548d1b46d7Szhanghch05         std::shared_ptr<bmcweb::AsyncResp> response = asyncResp;
145573df0db0SJames Feist         if (profile)
145673df0db0SJames Feist         {
145773df0db0SJames Feist             if (std::find(supportedProfiles.begin(), supportedProfiles.end(),
145873df0db0SJames Feist                           *profile) == supportedProfiles.end())
145973df0db0SJames Feist             {
146073df0db0SJames Feist                 messages::actionParameterUnknown(response->res, "Profile",
146173df0db0SJames Feist                                                  *profile);
146273df0db0SJames Feist                 return;
146373df0db0SJames Feist             }
146473df0db0SJames Feist             currentProfile = *profile;
1465*9ae226faSGeorge Liu             sdbusplus::asio::setProperty(
1466*9ae226faSGeorge Liu                 *crow::connections::systemBus, profileConnection, profilePath,
1467*9ae226faSGeorge Liu                 thermalModeIface, "Current", *profile,
14685e7e2dc5SEd Tanous                 [response](const boost::system::error_code& ec) {
146973df0db0SJames Feist                 if (ec)
147073df0db0SJames Feist                 {
147173df0db0SJames Feist                     BMCWEB_LOG_ERROR << "Error patching profile" << ec;
147273df0db0SJames Feist                     messages::internalError(response->res);
147373df0db0SJames Feist                 }
1474*9ae226faSGeorge Liu                 });
147573df0db0SJames Feist         }
147673df0db0SJames Feist 
147773df0db0SJames Feist         for (auto& containerPair : configuration)
147873df0db0SJames Feist         {
147973df0db0SJames Feist             auto& container = containerPair.second;
148073df0db0SJames Feist             if (!container)
148173df0db0SJames Feist             {
148273df0db0SJames Feist                 continue;
148373df0db0SJames Feist             }
14846ee7f774SJames Feist             BMCWEB_LOG_DEBUG << *container;
14856ee7f774SJames Feist 
148602cad96eSEd Tanous             const std::string& type = containerPair.first;
148773df0db0SJames Feist 
148873df0db0SJames Feist             for (nlohmann::json::iterator it = container->begin();
148917a897dfSManojkiran Eda                  it != container->end(); ++it)
149073df0db0SJames Feist             {
149173df0db0SJames Feist                 const auto& name = it.key();
1492cddbf3dfSPotin Lai                 std::string dbusObjName = name;
1493cddbf3dfSPotin Lai                 std::replace(dbusObjName.begin(), dbusObjName.end(), ' ', '_');
14946ee7f774SJames Feist                 BMCWEB_LOG_DEBUG << "looking for " << name;
14956ee7f774SJames Feist 
149689492a15SPatrick Williams                 auto pathItr = std::find_if(managedObj.begin(),
149789492a15SPatrick Williams                                             managedObj.end(),
1498cddbf3dfSPotin Lai                                             [&dbusObjName](const auto& obj) {
1499002d39b4SEd Tanous                     return boost::algorithm::ends_with(obj.first.str,
1500cddbf3dfSPotin Lai                                                        "/" + dbusObjName);
150173df0db0SJames Feist                 });
1502b9d36b47SEd Tanous                 dbus::utility::DBusPropertiesMap output;
150373df0db0SJames Feist 
150473df0db0SJames Feist                 output.reserve(16); // The pid interface length
150573df0db0SJames Feist 
150673df0db0SJames Feist                 // determines if we're patching entity-manager or
150773df0db0SJames Feist                 // creating a new object
150873df0db0SJames Feist                 bool createNewObject = (pathItr == managedObj.end());
15096ee7f774SJames Feist                 BMCWEB_LOG_DEBUG << "Found = " << !createNewObject;
15106ee7f774SJames Feist 
151173df0db0SJames Feist                 std::string iface;
1512ea2b670dSEd Tanous                 if (!createNewObject)
1513ea2b670dSEd Tanous                 {
15148be2b5b6SPotin Lai                     bool findInterface = false;
1515ea2b670dSEd Tanous                     for (const auto& interface : pathItr->second)
1516ea2b670dSEd Tanous                     {
1517ea2b670dSEd Tanous                         if (interface.first == pidConfigurationIface)
1518ea2b670dSEd Tanous                         {
1519ea2b670dSEd Tanous                             if (type == "PidControllers" ||
1520ea2b670dSEd Tanous                                 type == "FanControllers")
152173df0db0SJames Feist                             {
152273df0db0SJames Feist                                 iface = pidConfigurationIface;
15238be2b5b6SPotin Lai                                 findInterface = true;
15248be2b5b6SPotin Lai                                 break;
152573df0db0SJames Feist                             }
152673df0db0SJames Feist                         }
1527ea2b670dSEd Tanous                         else if (interface.first == pidZoneConfigurationIface)
152873df0db0SJames Feist                         {
1529ea2b670dSEd Tanous                             if (type == "FanZones")
153073df0db0SJames Feist                             {
1531ea2b670dSEd Tanous                                 iface = pidConfigurationIface;
15328be2b5b6SPotin Lai                                 findInterface = true;
15338be2b5b6SPotin Lai                                 break;
153473df0db0SJames Feist                             }
153573df0db0SJames Feist                         }
1536ea2b670dSEd Tanous                         else if (interface.first == stepwiseConfigurationIface)
1537ea2b670dSEd Tanous                         {
1538ea2b670dSEd Tanous                             if (type == "StepwiseControllers")
153973df0db0SJames Feist                             {
154073df0db0SJames Feist                                 iface = stepwiseConfigurationIface;
15418be2b5b6SPotin Lai                                 findInterface = true;
15428be2b5b6SPotin Lai                                 break;
15438be2b5b6SPotin Lai                             }
15448be2b5b6SPotin Lai                         }
15458be2b5b6SPotin Lai                     }
15468be2b5b6SPotin Lai 
15478be2b5b6SPotin Lai                     // create new object if interface not found
15488be2b5b6SPotin Lai                     if (!findInterface)
15498be2b5b6SPotin Lai                     {
155073df0db0SJames Feist                         createNewObject = true;
155173df0db0SJames Feist                     }
1552ea2b670dSEd Tanous                 }
15536ee7f774SJames Feist 
15546ee7f774SJames Feist                 if (createNewObject && it.value() == nullptr)
15556ee7f774SJames Feist                 {
15564e0453b1SGunnar Mills                     // can't delete a non-existent object
1557e2616cc5SEd Tanous                     messages::propertyValueNotInList(response->res, it.value(),
1558e2616cc5SEd Tanous                                                      name);
15596ee7f774SJames Feist                     continue;
15606ee7f774SJames Feist                 }
15616ee7f774SJames Feist 
15626ee7f774SJames Feist                 std::string path;
15636ee7f774SJames Feist                 if (pathItr != managedObj.end())
15646ee7f774SJames Feist                 {
15656ee7f774SJames Feist                     path = pathItr->first.str;
15666ee7f774SJames Feist                 }
15676ee7f774SJames Feist 
156873df0db0SJames Feist                 BMCWEB_LOG_DEBUG << "Create new = " << createNewObject << "\n";
1569e69d9de2SJames Feist 
1570e69d9de2SJames Feist                 // arbitrary limit to avoid attacks
1571e69d9de2SJames Feist                 constexpr const size_t controllerLimit = 500;
157214b0b8d5SJames Feist                 if (createNewObject && objectCount >= controllerLimit)
1573e69d9de2SJames Feist                 {
1574e69d9de2SJames Feist                     messages::resourceExhaustion(response->res, type);
1575e69d9de2SJames Feist                     continue;
1576e69d9de2SJames Feist                 }
1577a170f275SEd Tanous                 std::string escaped = name;
1578a170f275SEd Tanous                 std::replace(escaped.begin(), escaped.end(), '_', ' ');
1579a170f275SEd Tanous                 output.emplace_back("Name", escaped);
158073df0db0SJames Feist 
158173df0db0SJames Feist                 std::string chassis;
158273df0db0SJames Feist                 CreatePIDRet ret = createPidInterface(
15836ee7f774SJames Feist                     response, type, it, path, managedObj, createNewObject,
15846ee7f774SJames Feist                     output, chassis, currentProfile);
158573df0db0SJames Feist                 if (ret == CreatePIDRet::fail)
158673df0db0SJames Feist                 {
158773df0db0SJames Feist                     return;
158873df0db0SJames Feist                 }
15893174e4dfSEd Tanous                 if (ret == CreatePIDRet::del)
159073df0db0SJames Feist                 {
159173df0db0SJames Feist                     continue;
159273df0db0SJames Feist                 }
159373df0db0SJames Feist 
159473df0db0SJames Feist                 if (!createNewObject)
159573df0db0SJames Feist                 {
159673df0db0SJames Feist                     for (const auto& property : output)
159773df0db0SJames Feist                     {
1598*9ae226faSGeorge Liu                         sdbusplus::asio::setProperty(
1599*9ae226faSGeorge Liu                             *crow::connections::systemBus,
1600*9ae226faSGeorge Liu                             "xyz.openbmc_project.EntityManager", path, iface,
1601*9ae226faSGeorge Liu                             property.first, property.second,
160273df0db0SJames Feist                             [response,
160373df0db0SJames Feist                              propertyName{std::string(property.first)}](
16045e7e2dc5SEd Tanous                                 const boost::system::error_code& ec) {
160573df0db0SJames Feist                             if (ec)
160673df0db0SJames Feist                             {
160773df0db0SJames Feist                                 BMCWEB_LOG_ERROR << "Error patching "
1608002d39b4SEd Tanous                                                  << propertyName << ": " << ec;
160973df0db0SJames Feist                                 messages::internalError(response->res);
161073df0db0SJames Feist                                 return;
161173df0db0SJames Feist                             }
161273df0db0SJames Feist                             messages::success(response->res);
1613*9ae226faSGeorge Liu                             });
161473df0db0SJames Feist                     }
161573df0db0SJames Feist                 }
161673df0db0SJames Feist                 else
161773df0db0SJames Feist                 {
161873df0db0SJames Feist                     if (chassis.empty())
161973df0db0SJames Feist                     {
162073df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Failed to get chassis from config";
1621ace85d60SEd Tanous                         messages::internalError(response->res);
162273df0db0SJames Feist                         return;
162373df0db0SJames Feist                     }
162473df0db0SJames Feist 
162573df0db0SJames Feist                     bool foundChassis = false;
162673df0db0SJames Feist                     for (const auto& obj : managedObj)
162773df0db0SJames Feist                     {
162873df0db0SJames Feist                         if (boost::algorithm::ends_with(obj.first.str, chassis))
162973df0db0SJames Feist                         {
163073df0db0SJames Feist                             chassis = obj.first.str;
163173df0db0SJames Feist                             foundChassis = true;
163273df0db0SJames Feist                             break;
163373df0db0SJames Feist                         }
163473df0db0SJames Feist                     }
163573df0db0SJames Feist                     if (!foundChassis)
163673df0db0SJames Feist                     {
163773df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Failed to find chassis on dbus";
163873df0db0SJames Feist                         messages::resourceMissingAtURI(
1639ace85d60SEd Tanous                             response->res,
1640ef4c65b7SEd Tanous                             boost::urls::format("/redfish/v1/Chassis/{}",
1641ef4c65b7SEd Tanous                                                 chassis));
164273df0db0SJames Feist                         return;
164373df0db0SJames Feist                     }
164473df0db0SJames Feist 
164573df0db0SJames Feist                     crow::connections::systemBus->async_method_call(
16465e7e2dc5SEd Tanous                         [response](const boost::system::error_code& ec) {
164773df0db0SJames Feist                         if (ec)
164873df0db0SJames Feist                         {
164973df0db0SJames Feist                             BMCWEB_LOG_ERROR << "Error Adding Pid Object "
165073df0db0SJames Feist                                              << ec;
165173df0db0SJames Feist                             messages::internalError(response->res);
165273df0db0SJames Feist                             return;
165373df0db0SJames Feist                         }
165473df0db0SJames Feist                         messages::success(response->res);
165573df0db0SJames Feist                         },
165673df0db0SJames Feist                         "xyz.openbmc_project.EntityManager", chassis,
165773df0db0SJames Feist                         "xyz.openbmc_project.AddObject", "AddObject", output);
165873df0db0SJames Feist                 }
165973df0db0SJames Feist             }
166073df0db0SJames Feist         }
166173df0db0SJames Feist     }
166224b2fe81SEd Tanous 
166324b2fe81SEd Tanous     ~SetPIDValues()
166424b2fe81SEd Tanous     {
166524b2fe81SEd Tanous         try
166624b2fe81SEd Tanous         {
166724b2fe81SEd Tanous             pidSetDone();
166824b2fe81SEd Tanous         }
166924b2fe81SEd Tanous         catch (...)
167024b2fe81SEd Tanous         {
167124b2fe81SEd Tanous             BMCWEB_LOG_CRITICAL << "pidSetDone threw exception";
167224b2fe81SEd Tanous         }
167324b2fe81SEd Tanous     }
167424b2fe81SEd Tanous 
16758d1b46d7Szhanghch05     std::shared_ptr<bmcweb::AsyncResp> asyncResp;
167673df0db0SJames Feist     std::vector<std::pair<std::string, std::optional<nlohmann::json>>>
167773df0db0SJames Feist         configuration;
167873df0db0SJames Feist     std::optional<std::string> profile;
167973df0db0SJames Feist     dbus::utility::ManagedObjectType managedObj;
168073df0db0SJames Feist     std::vector<std::string> supportedProfiles;
168173df0db0SJames Feist     std::string currentProfile;
168273df0db0SJames Feist     std::string profileConnection;
168373df0db0SJames Feist     std::string profilePath;
168414b0b8d5SJames Feist     size_t objectCount = 0;
168573df0db0SJames Feist };
168673df0db0SJames Feist 
1687071d8fdfSSunnySrivastava1984 /**
1688071d8fdfSSunnySrivastava1984  * @brief Retrieves BMC manager location data over DBus
1689071d8fdfSSunnySrivastava1984  *
1690ac106bf6SEd Tanous  * @param[in] asyncResp Shared pointer for completing asynchronous calls
1691071d8fdfSSunnySrivastava1984  * @param[in] connectionName - service name
1692071d8fdfSSunnySrivastava1984  * @param[in] path - object path
1693071d8fdfSSunnySrivastava1984  * @return none
1694071d8fdfSSunnySrivastava1984  */
1695ac106bf6SEd Tanous inline void getLocation(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1696071d8fdfSSunnySrivastava1984                         const std::string& connectionName,
1697071d8fdfSSunnySrivastava1984                         const std::string& path)
1698071d8fdfSSunnySrivastava1984 {
1699071d8fdfSSunnySrivastava1984     BMCWEB_LOG_DEBUG << "Get BMC manager Location data.";
1700071d8fdfSSunnySrivastava1984 
17011e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
17021e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
17031e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
1704ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
17051e1e598dSJonathan Doman                     const std::string& property) {
1706071d8fdfSSunnySrivastava1984         if (ec)
1707071d8fdfSSunnySrivastava1984         {
1708071d8fdfSSunnySrivastava1984             BMCWEB_LOG_DEBUG << "DBUS response error for "
1709071d8fdfSSunnySrivastava1984                                 "Location";
1710ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
1711071d8fdfSSunnySrivastava1984             return;
1712071d8fdfSSunnySrivastava1984         }
1713071d8fdfSSunnySrivastava1984 
1714ac106bf6SEd Tanous         asyncResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
17151e1e598dSJonathan Doman             property;
17161e1e598dSJonathan Doman         });
1717071d8fdfSSunnySrivastava1984 }
17187e860f15SJohn Edward Broadbent // avoid name collision systems.hpp
17197e860f15SJohn Edward Broadbent inline void
1720ac106bf6SEd Tanous     managerGetLastResetTime(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
17214bf2b033SGunnar Mills {
17224bf2b033SGunnar Mills     BMCWEB_LOG_DEBUG << "Getting Manager Last Reset Time";
17234bf2b033SGunnar Mills 
17241e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint64_t>(
17251e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.State.BMC",
17261e1e598dSJonathan Doman         "/xyz/openbmc_project/state/bmc0", "xyz.openbmc_project.State.BMC",
17271e1e598dSJonathan Doman         "LastRebootTime",
1728ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
17291e1e598dSJonathan Doman                     const uint64_t lastResetTime) {
17304bf2b033SGunnar Mills         if (ec)
17314bf2b033SGunnar Mills         {
17324bf2b033SGunnar Mills             BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
17334bf2b033SGunnar Mills             return;
17344bf2b033SGunnar Mills         }
17354bf2b033SGunnar Mills 
17364bf2b033SGunnar Mills         // LastRebootTime is epoch time, in milliseconds
17374bf2b033SGunnar Mills         // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
17381e1e598dSJonathan Doman         uint64_t lastResetTimeStamp = lastResetTime / 1000;
17394bf2b033SGunnar Mills 
17404bf2b033SGunnar Mills         // Convert to ISO 8601 standard
1741ac106bf6SEd Tanous         asyncResp->res.jsonValue["LastResetTime"] =
17422b82937eSEd Tanous             redfish::time_utils::getDateTimeUint(lastResetTimeStamp);
17431e1e598dSJonathan Doman         });
17444bf2b033SGunnar Mills }
17454bf2b033SGunnar Mills 
17464bfefa74SGunnar Mills /**
17474bfefa74SGunnar Mills  * @brief Set the running firmware image
17484bfefa74SGunnar Mills  *
1749ac106bf6SEd Tanous  * @param[i,o] asyncResp - Async response object
17504bfefa74SGunnar Mills  * @param[i] runningFirmwareTarget - Image to make the running image
17514bfefa74SGunnar Mills  *
17524bfefa74SGunnar Mills  * @return void
17534bfefa74SGunnar Mills  */
17547e860f15SJohn Edward Broadbent inline void
1755ac106bf6SEd Tanous     setActiveFirmwareImage(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1756f23b7296SEd Tanous                            const std::string& runningFirmwareTarget)
17574bfefa74SGunnar Mills {
17584bfefa74SGunnar Mills     // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
1759f23b7296SEd Tanous     std::string::size_type idPos = runningFirmwareTarget.rfind('/');
17604bfefa74SGunnar Mills     if (idPos == std::string::npos)
17614bfefa74SGunnar Mills     {
1762ac106bf6SEd Tanous         messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget,
17634bfefa74SGunnar Mills                                          "@odata.id");
17644bfefa74SGunnar Mills         BMCWEB_LOG_DEBUG << "Can't parse firmware ID!";
17654bfefa74SGunnar Mills         return;
17664bfefa74SGunnar Mills     }
17674bfefa74SGunnar Mills     idPos++;
17684bfefa74SGunnar Mills     if (idPos >= runningFirmwareTarget.size())
17694bfefa74SGunnar Mills     {
1770ac106bf6SEd Tanous         messages::propertyValueNotInList(asyncResp->res, runningFirmwareTarget,
17714bfefa74SGunnar Mills                                          "@odata.id");
17724bfefa74SGunnar Mills         BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
17734bfefa74SGunnar Mills         return;
17744bfefa74SGunnar Mills     }
17754bfefa74SGunnar Mills     std::string firmwareId = runningFirmwareTarget.substr(idPos);
17764bfefa74SGunnar Mills 
17774bfefa74SGunnar Mills     // Make sure the image is valid before setting priority
17785eb468daSGeorge Liu     sdbusplus::message::object_path objPath("/xyz/openbmc_project/software");
17795eb468daSGeorge Liu     dbus::utility::getManagedObjects(
17805eb468daSGeorge Liu         "xyz.openbmc_project.Software.BMC.Updater", objPath,
17815eb468daSGeorge Liu         [asyncResp, firmwareId, runningFirmwareTarget](
17825eb468daSGeorge Liu             const boost::system::error_code& ec,
17835eb468daSGeorge Liu             const dbus::utility::ManagedObjectType& subtree) {
17844bfefa74SGunnar Mills         if (ec)
17854bfefa74SGunnar Mills         {
17864bfefa74SGunnar Mills             BMCWEB_LOG_DEBUG << "D-Bus response error getting objects.";
1787ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
17884bfefa74SGunnar Mills             return;
17894bfefa74SGunnar Mills         }
17904bfefa74SGunnar Mills 
179126f6976fSEd Tanous         if (subtree.empty())
17924bfefa74SGunnar Mills         {
17934bfefa74SGunnar Mills             BMCWEB_LOG_DEBUG << "Can't find image!";
1794ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
17954bfefa74SGunnar Mills             return;
17964bfefa74SGunnar Mills         }
17974bfefa74SGunnar Mills 
17984bfefa74SGunnar Mills         bool foundImage = false;
179902cad96eSEd Tanous         for (const auto& object : subtree)
18004bfefa74SGunnar Mills         {
18014bfefa74SGunnar Mills             const std::string& path =
18024bfefa74SGunnar Mills                 static_cast<const std::string&>(object.first);
1803f23b7296SEd Tanous             std::size_t idPos2 = path.rfind('/');
18044bfefa74SGunnar Mills 
18054bfefa74SGunnar Mills             if (idPos2 == std::string::npos)
18064bfefa74SGunnar Mills             {
18074bfefa74SGunnar Mills                 continue;
18084bfefa74SGunnar Mills             }
18094bfefa74SGunnar Mills 
18104bfefa74SGunnar Mills             idPos2++;
18114bfefa74SGunnar Mills             if (idPos2 >= path.size())
18124bfefa74SGunnar Mills             {
18134bfefa74SGunnar Mills                 continue;
18144bfefa74SGunnar Mills             }
18154bfefa74SGunnar Mills 
18164bfefa74SGunnar Mills             if (path.substr(idPos2) == firmwareId)
18174bfefa74SGunnar Mills             {
18184bfefa74SGunnar Mills                 foundImage = true;
18194bfefa74SGunnar Mills                 break;
18204bfefa74SGunnar Mills             }
18214bfefa74SGunnar Mills         }
18224bfefa74SGunnar Mills 
18234bfefa74SGunnar Mills         if (!foundImage)
18244bfefa74SGunnar Mills         {
1825ac106bf6SEd Tanous             messages::propertyValueNotInList(
1826ac106bf6SEd Tanous                 asyncResp->res, runningFirmwareTarget, "@odata.id");
18274bfefa74SGunnar Mills             BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
18284bfefa74SGunnar Mills             return;
18294bfefa74SGunnar Mills         }
18304bfefa74SGunnar Mills 
18318cc8edecSEd Tanous         BMCWEB_LOG_DEBUG << "Setting firmware version " << firmwareId
18328cc8edecSEd Tanous                          << " to priority 0.";
18334bfefa74SGunnar Mills 
18344bfefa74SGunnar Mills         // Only support Immediate
18354bfefa74SGunnar Mills         // An addition could be a Redfish Setting like
18364bfefa74SGunnar Mills         // ActiveSoftwareImageApplyTime and support OnReset
1837*9ae226faSGeorge Liu         sdbusplus::asio::setProperty(
1838*9ae226faSGeorge Liu             *crow::connections::systemBus,
1839*9ae226faSGeorge Liu             "xyz.openbmc_project.Software.BMC.Updater",
1840*9ae226faSGeorge Liu             "/xyz/openbmc_project/software/" + firmwareId,
1841*9ae226faSGeorge Liu             "xyz.openbmc_project.Software.RedundancyPriority", "Priority",
1842*9ae226faSGeorge Liu             static_cast<uint8_t>(0),
1843ac106bf6SEd Tanous             [asyncResp](const boost::system::error_code& ec2) {
18448a592810SEd Tanous             if (ec2)
18454bfefa74SGunnar Mills             {
18464bfefa74SGunnar Mills                 BMCWEB_LOG_DEBUG << "D-Bus response error setting.";
1847ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
18484bfefa74SGunnar Mills                 return;
18494bfefa74SGunnar Mills             }
1850ac106bf6SEd Tanous             doBMCGracefulRestart(asyncResp);
1851*9ae226faSGeorge Liu             });
18525eb468daSGeorge Liu         });
18534bfefa74SGunnar Mills }
18544bfefa74SGunnar Mills 
1855ac106bf6SEd Tanous inline void setDateTime(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
18567e860f15SJohn Edward Broadbent                         std::string datetime)
1857af5d6058SSantosh Puranik {
1858af5d6058SSantosh Puranik     BMCWEB_LOG_DEBUG << "Set date time: " << datetime;
1859af5d6058SSantosh Puranik 
1860c2e32007SEd Tanous     std::optional<redfish::time_utils::usSinceEpoch> us =
1861c2e32007SEd Tanous         redfish::time_utils::dateStringToEpoch(datetime);
1862c2e32007SEd Tanous     if (!us)
1863af5d6058SSantosh Puranik     {
1864ac106bf6SEd Tanous         messages::propertyValueFormatError(asyncResp->res, datetime,
1865ac106bf6SEd Tanous                                            "DateTime");
1866c2e32007SEd Tanous         return;
1867c2e32007SEd Tanous     }
1868*9ae226faSGeorge Liu     sdbusplus::asio::setProperty(
1869*9ae226faSGeorge Liu         *crow::connections::systemBus, "xyz.openbmc_project.Time.Manager",
1870*9ae226faSGeorge Liu         "/xyz/openbmc_project/time/bmc", "xyz.openbmc_project.Time.EpochTime",
1871*9ae226faSGeorge Liu         "Elapsed", us->count(),
1872ac106bf6SEd Tanous         [asyncResp{std::move(asyncResp)},
18735e7e2dc5SEd Tanous          datetime{std::move(datetime)}](const boost::system::error_code& ec) {
1874af5d6058SSantosh Puranik         if (ec)
1875af5d6058SSantosh Puranik         {
1876af5d6058SSantosh Puranik             BMCWEB_LOG_DEBUG << "Failed to set elapsed time. "
1877af5d6058SSantosh Puranik                                 "DBUS response error "
1878af5d6058SSantosh Puranik                              << ec;
1879ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
1880af5d6058SSantosh Puranik             return;
1881af5d6058SSantosh Puranik         }
1882ac106bf6SEd Tanous         asyncResp->res.jsonValue["DateTime"] = datetime;
1883*9ae226faSGeorge Liu         });
188483ff9ab6SJames Feist }
18859c310685SBorawski.Lukasz 
188675815e5cSEd Tanous inline void
188775815e5cSEd Tanous     checkForQuiesced(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
188875815e5cSEd Tanous {
188975815e5cSEd Tanous     sdbusplus::asio::getProperty<std::string>(
189075815e5cSEd Tanous         *crow::connections::systemBus, "org.freedesktop.systemd1",
189175815e5cSEd Tanous         "/org/freedesktop/systemd1/unit/obmc-bmc-service-quiesce@0.target",
189275815e5cSEd Tanous         "org.freedesktop.systemd1.Unit", "ActiveState",
189375815e5cSEd Tanous         [asyncResp](const boost::system::error_code& ec,
189475815e5cSEd Tanous                     const std::string& val) {
189575815e5cSEd Tanous         if (!ec)
189675815e5cSEd Tanous         {
189775815e5cSEd Tanous             if (val == "active")
189875815e5cSEd Tanous             {
189975815e5cSEd Tanous                 asyncResp->res.jsonValue["Status"]["Health"] = "Critical";
190075815e5cSEd Tanous                 asyncResp->res.jsonValue["Status"]["State"] = "Quiesced";
190175815e5cSEd Tanous                 return;
190275815e5cSEd Tanous             }
190375815e5cSEd Tanous         }
190475815e5cSEd Tanous         asyncResp->res.jsonValue["Status"]["Health"] = "OK";
190575815e5cSEd Tanous         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
190675815e5cSEd Tanous         });
190775815e5cSEd Tanous }
190875815e5cSEd Tanous 
19097e860f15SJohn Edward Broadbent inline void requestRoutesManager(App& app)
19107e860f15SJohn Edward Broadbent {
19117e860f15SJohn Edward Broadbent     std::string uuid = persistent_data::getConfig().systemUuid;
19129c310685SBorawski.Lukasz 
19137e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
1914ed398213SEd Tanous         .privileges(redfish::privileges::getManager)
1915002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1916002d39b4SEd Tanous             [&app, uuid](const crow::Request& req,
191745ca1b86SEd Tanous                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
19183ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
191945ca1b86SEd Tanous         {
192045ca1b86SEd Tanous             return;
192145ca1b86SEd Tanous         }
19227e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
1923a51fc2d2SSui Chen         asyncResp->res.jsonValue["@odata.type"] = "#Manager.v1_14_0.Manager";
19247e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["Id"] = "bmc";
19257e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["Name"] = "OpenBmc Manager";
19267e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["Description"] =
19277e860f15SJohn Edward Broadbent             "Baseboard Management Controller";
19287e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["PowerState"] = "On";
19291476687dSEd Tanous 
19307e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["ManagerType"] = "BMC";
19317e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["UUID"] = systemd_utils::getUuid();
19327e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["ServiceEntryPointUUID"] = uuid;
1933002d39b4SEd Tanous         asyncResp->res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model
19347e860f15SJohn Edward Broadbent 
19351476687dSEd Tanous         asyncResp->res.jsonValue["LogServices"]["@odata.id"] =
19361476687dSEd Tanous             "/redfish/v1/Managers/bmc/LogServices";
19371476687dSEd Tanous         asyncResp->res.jsonValue["NetworkProtocol"]["@odata.id"] =
19381476687dSEd Tanous             "/redfish/v1/Managers/bmc/NetworkProtocol";
19391476687dSEd Tanous         asyncResp->res.jsonValue["EthernetInterfaces"]["@odata.id"] =
19401476687dSEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces";
19417e860f15SJohn Edward Broadbent 
19427e860f15SJohn Edward Broadbent #ifdef BMCWEB_ENABLE_VM_NBDPROXY
19431476687dSEd Tanous         asyncResp->res.jsonValue["VirtualMedia"]["@odata.id"] =
19441476687dSEd Tanous             "/redfish/v1/Managers/bmc/VirtualMedia";
19457e860f15SJohn Edward Broadbent #endif // BMCWEB_ENABLE_VM_NBDPROXY
19467e860f15SJohn Edward Broadbent 
19477e860f15SJohn Edward Broadbent         // default oem data
19487e860f15SJohn Edward Broadbent         nlohmann::json& oem = asyncResp->res.jsonValue["Oem"];
19497e860f15SJohn Edward Broadbent         nlohmann::json& oemOpenbmc = oem["OpenBmc"];
19507e860f15SJohn Edward Broadbent         oem["@odata.type"] = "#OemManager.Oem";
19517e860f15SJohn Edward Broadbent         oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
19527e860f15SJohn Edward Broadbent         oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
19537e860f15SJohn Edward Broadbent         oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
19541476687dSEd Tanous 
19551476687dSEd Tanous         nlohmann::json::object_t certificates;
19561476687dSEd Tanous         certificates["@odata.id"] =
19571476687dSEd Tanous             "/redfish/v1/Managers/bmc/Truststore/Certificates";
19581476687dSEd Tanous         oemOpenbmc["Certificates"] = std::move(certificates);
19597e860f15SJohn Edward Broadbent 
19607e860f15SJohn Edward Broadbent         // Manager.Reset (an action) can be many values, OpenBMC only
19617e860f15SJohn Edward Broadbent         // supports BMC reboot.
19627e860f15SJohn Edward Broadbent         nlohmann::json& managerReset =
19637e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Actions"]["#Manager.Reset"];
19647e860f15SJohn Edward Broadbent         managerReset["target"] =
19657e860f15SJohn Edward Broadbent             "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
19667e860f15SJohn Edward Broadbent         managerReset["@Redfish.ActionInfo"] =
19677e860f15SJohn Edward Broadbent             "/redfish/v1/Managers/bmc/ResetActionInfo";
19687e860f15SJohn Edward Broadbent 
19697e860f15SJohn Edward Broadbent         // ResetToDefaults (Factory Reset) has values like
19707e860f15SJohn Edward Broadbent         // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
19717e860f15SJohn Edward Broadbent         // on OpenBMC
19727e860f15SJohn Edward Broadbent         nlohmann::json& resetToDefaults =
19737e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
19747e860f15SJohn Edward Broadbent         resetToDefaults["target"] =
19757e860f15SJohn Edward Broadbent             "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults";
1976613dabeaSEd Tanous         resetToDefaults["ResetType@Redfish.AllowableValues"] =
1977613dabeaSEd Tanous             nlohmann::json::array_t({"ResetAll"});
19787e860f15SJohn Edward Broadbent 
19797c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
19802b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
19817c8c4058STejas Patil 
19827c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
19837c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
19847c8c4058STejas Patil             redfishDateTimeOffset.second;
19857e860f15SJohn Edward Broadbent 
19860e8ac5e7SGunnar Mills         // TODO (Gunnar): Remove these one day since moved to ComputerSystem
19870e8ac5e7SGunnar Mills         // Still used by OCP profiles
19880e8ac5e7SGunnar Mills         // https://github.com/opencomputeproject/OCP-Profiles/issues/23
19897e860f15SJohn Edward Broadbent         // Fill in SerialConsole info
19907e860f15SJohn Edward Broadbent         asyncResp->res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
1991002d39b4SEd Tanous         asyncResp->res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15;
1992613dabeaSEd Tanous         asyncResp->res.jsonValue["SerialConsole"]["ConnectTypesSupported"] =
1993613dabeaSEd Tanous             nlohmann::json::array_t({"IPMI", "SSH"});
19947e860f15SJohn Edward Broadbent #ifdef BMCWEB_ENABLE_KVM
19957e860f15SJohn Edward Broadbent         // Fill in GraphicalConsole info
1996002d39b4SEd Tanous         asyncResp->res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true;
1997002d39b4SEd Tanous         asyncResp->res.jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] =
1998002d39b4SEd Tanous             4;
1999613dabeaSEd Tanous         asyncResp->res.jsonValue["GraphicalConsole"]["ConnectTypesSupported"] =
2000613dabeaSEd Tanous             nlohmann::json::array_t({"KVMIP"});
20017e860f15SJohn Edward Broadbent #endif // BMCWEB_ENABLE_KVM
20027f3e84a1SEd Tanous         if constexpr (!bmcwebEnableMultiHost)
20037f3e84a1SEd Tanous         {
20047f3e84a1SEd Tanous             asyncResp->res.jsonValue["Links"]["ManagerForServers@odata.count"] =
20057f3e84a1SEd Tanous                 1;
20061476687dSEd Tanous 
20071476687dSEd Tanous             nlohmann::json::array_t managerForServers;
20081476687dSEd Tanous             nlohmann::json::object_t manager;
20091476687dSEd Tanous             manager["@odata.id"] = "/redfish/v1/Systems/system";
2010ad539545SPatrick Williams             managerForServers.emplace_back(std::move(manager));
20111476687dSEd Tanous 
20121476687dSEd Tanous             asyncResp->res.jsonValue["Links"]["ManagerForServers"] =
20131476687dSEd Tanous                 std::move(managerForServers);
20147f3e84a1SEd Tanous         }
201513451e39SWilly Tu         if constexpr (bmcwebEnableHealthPopulate)
201613451e39SWilly Tu         {
20177e860f15SJohn Edward Broadbent             auto health = std::make_shared<HealthPopulate>(asyncResp);
20187e860f15SJohn Edward Broadbent             health->isManagersHealth = true;
20197e860f15SJohn Edward Broadbent             health->populate();
202013451e39SWilly Tu         }
20217e860f15SJohn Edward Broadbent 
2022eee0013eSWilly Tu         sw_util::populateSoftwareInformation(asyncResp, sw_util::bmcPurpose,
20237e860f15SJohn Edward Broadbent                                              "FirmwareVersion", true);
20247e860f15SJohn Edward Broadbent 
20257e860f15SJohn Edward Broadbent         managerGetLastResetTime(asyncResp);
20267e860f15SJohn Edward Broadbent 
2027a51fc2d2SSui Chen         // ManagerDiagnosticData is added for all BMCs.
2028a51fc2d2SSui Chen         nlohmann::json& managerDiagnosticData =
2029a51fc2d2SSui Chen             asyncResp->res.jsonValue["ManagerDiagnosticData"];
2030a51fc2d2SSui Chen         managerDiagnosticData["@odata.id"] =
2031a51fc2d2SSui Chen             "/redfish/v1/Managers/bmc/ManagerDiagnosticData";
2032a51fc2d2SSui Chen 
203354dce7f5SGunnar Mills #ifdef BMCWEB_ENABLE_REDFISH_OEM_MANAGER_FAN_DATA
20347e860f15SJohn Edward Broadbent         auto pids = std::make_shared<GetPIDValues>(asyncResp);
20357e860f15SJohn Edward Broadbent         pids->run();
203654dce7f5SGunnar Mills #endif
20377e860f15SJohn Edward Broadbent 
2038002d39b4SEd Tanous         getMainChassisId(asyncResp,
2039002d39b4SEd Tanous                          [](const std::string& chassisId,
2040002d39b4SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
2041002d39b4SEd Tanous             aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1;
20421476687dSEd Tanous             nlohmann::json::array_t managerForChassis;
20438a592810SEd Tanous             nlohmann::json::object_t managerObj;
2044ef4c65b7SEd Tanous             boost::urls::url chassiUrl =
2045ef4c65b7SEd Tanous                 boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
2046eddfc437SWilly Tu             managerObj["@odata.id"] = chassiUrl;
2047ad539545SPatrick Williams             managerForChassis.emplace_back(std::move(managerObj));
20481476687dSEd Tanous             aRsp->res.jsonValue["Links"]["ManagerForChassis"] =
20491476687dSEd Tanous                 std::move(managerForChassis);
20501476687dSEd Tanous             aRsp->res.jsonValue["Links"]["ManagerInChassis"]["@odata.id"] =
2051eddfc437SWilly Tu                 chassiUrl;
20527e860f15SJohn Edward Broadbent         });
20537e860f15SJohn Edward Broadbent 
20541e1e598dSJonathan Doman         sdbusplus::asio::getProperty<double>(
20551e1e598dSJonathan Doman             *crow::connections::systemBus, "org.freedesktop.systemd1",
2056002d39b4SEd Tanous             "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager",
2057002d39b4SEd Tanous             "Progress",
205875815e5cSEd Tanous             [asyncResp](const boost::system::error_code& ec, double val) {
20597e860f15SJohn Edward Broadbent             if (ec)
20601abe55efSEd Tanous             {
20617e860f15SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Error while getting progress";
20627e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
20637e860f15SJohn Edward Broadbent                 return;
20647e860f15SJohn Edward Broadbent             }
20651e1e598dSJonathan Doman             if (val < 1.0)
20667e860f15SJohn Edward Broadbent             {
206775815e5cSEd Tanous                 asyncResp->res.jsonValue["Status"]["Health"] = "OK";
2068002d39b4SEd Tanous                 asyncResp->res.jsonValue["Status"]["State"] = "Starting";
206975815e5cSEd Tanous                 return;
20707e860f15SJohn Edward Broadbent             }
207175815e5cSEd Tanous             checkForQuiesced(asyncResp);
20721e1e598dSJonathan Doman             });
20739c310685SBorawski.Lukasz 
2074e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> interfaces = {
2075e99073f5SGeorge Liu             "xyz.openbmc_project.Inventory.Item.Bmc"};
2076e99073f5SGeorge Liu         dbus::utility::getSubTree(
2077e99073f5SGeorge Liu             "/xyz/openbmc_project/inventory", 0, interfaces,
20787e860f15SJohn Edward Broadbent             [asyncResp](
2079e99073f5SGeorge Liu                 const boost::system::error_code& ec,
2080b9d36b47SEd Tanous                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
20817e860f15SJohn Edward Broadbent             if (ec)
20821abe55efSEd Tanous             {
2083002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "D-Bus response error on GetSubTree " << ec;
20847e860f15SJohn Edward Broadbent                 return;
20857e860f15SJohn Edward Broadbent             }
208626f6976fSEd Tanous             if (subtree.empty())
20877e860f15SJohn Edward Broadbent             {
20887e860f15SJohn Edward Broadbent                 BMCWEB_LOG_DEBUG << "Can't find bmc D-Bus object!";
20897e860f15SJohn Edward Broadbent                 return;
20907e860f15SJohn Edward Broadbent             }
20917e860f15SJohn Edward Broadbent             // Assume only 1 bmc D-Bus object
20927e860f15SJohn Edward Broadbent             // Throw an error if there is more than 1
20937e860f15SJohn Edward Broadbent             if (subtree.size() > 1)
20947e860f15SJohn Edward Broadbent             {
2095002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "Found more than 1 bmc D-Bus object!";
20967e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
20977e860f15SJohn Edward Broadbent                 return;
20987e860f15SJohn Edward Broadbent             }
20997e860f15SJohn Edward Broadbent 
2100002d39b4SEd Tanous             if (subtree[0].first.empty() || subtree[0].second.size() != 1)
21017e860f15SJohn Edward Broadbent             {
21027e860f15SJohn Edward Broadbent                 BMCWEB_LOG_DEBUG << "Error getting bmc D-Bus object!";
21037e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
21047e860f15SJohn Edward Broadbent                 return;
21057e860f15SJohn Edward Broadbent             }
21067e860f15SJohn Edward Broadbent 
21077e860f15SJohn Edward Broadbent             const std::string& path = subtree[0].first;
2108002d39b4SEd Tanous             const std::string& connectionName = subtree[0].second[0].first;
21097e860f15SJohn Edward Broadbent 
2110002d39b4SEd Tanous             for (const auto& interfaceName : subtree[0].second[0].second)
21117e860f15SJohn Edward Broadbent             {
21127e860f15SJohn Edward Broadbent                 if (interfaceName ==
21137e860f15SJohn Edward Broadbent                     "xyz.openbmc_project.Inventory.Decorator.Asset")
21147e860f15SJohn Edward Broadbent                 {
2115fac6e53bSKrzysztof Grobelny                     sdbusplus::asio::getAllProperties(
2116fac6e53bSKrzysztof Grobelny                         *crow::connections::systemBus, connectionName, path,
2117fac6e53bSKrzysztof Grobelny                         "xyz.openbmc_project.Inventory.Decorator.Asset",
21185e7e2dc5SEd Tanous                         [asyncResp](const boost::system::error_code& ec2,
2119b9d36b47SEd Tanous                                     const dbus::utility::DBusPropertiesMap&
21207e860f15SJohn Edward Broadbent                                         propertiesList) {
21218a592810SEd Tanous                         if (ec2)
21227e860f15SJohn Edward Broadbent                         {
2123002d39b4SEd Tanous                             BMCWEB_LOG_DEBUG << "Can't get bmc asset!";
21247e860f15SJohn Edward Broadbent                             return;
21257e860f15SJohn Edward Broadbent                         }
21267e860f15SJohn Edward Broadbent 
2127fac6e53bSKrzysztof Grobelny                         const std::string* partNumber = nullptr;
2128fac6e53bSKrzysztof Grobelny                         const std::string* serialNumber = nullptr;
2129fac6e53bSKrzysztof Grobelny                         const std::string* manufacturer = nullptr;
2130fac6e53bSKrzysztof Grobelny                         const std::string* model = nullptr;
2131fac6e53bSKrzysztof Grobelny                         const std::string* sparePartNumber = nullptr;
2132fac6e53bSKrzysztof Grobelny 
2133fac6e53bSKrzysztof Grobelny                         const bool success = sdbusplus::unpackPropertiesNoThrow(
2134fac6e53bSKrzysztof Grobelny                             dbus_utils::UnpackErrorPrinter(), propertiesList,
2135fac6e53bSKrzysztof Grobelny                             "PartNumber", partNumber, "SerialNumber",
2136fac6e53bSKrzysztof Grobelny                             serialNumber, "Manufacturer", manufacturer, "Model",
2137fac6e53bSKrzysztof Grobelny                             model, "SparePartNumber", sparePartNumber);
2138fac6e53bSKrzysztof Grobelny 
2139fac6e53bSKrzysztof Grobelny                         if (!success)
21407e860f15SJohn Edward Broadbent                         {
2141002d39b4SEd Tanous                             messages::internalError(asyncResp->res);
21427e860f15SJohn Edward Broadbent                             return;
21437e860f15SJohn Edward Broadbent                         }
2144fac6e53bSKrzysztof Grobelny 
2145fac6e53bSKrzysztof Grobelny                         if (partNumber != nullptr)
2146fac6e53bSKrzysztof Grobelny                         {
2147fac6e53bSKrzysztof Grobelny                             asyncResp->res.jsonValue["PartNumber"] =
2148fac6e53bSKrzysztof Grobelny                                 *partNumber;
21497e860f15SJohn Edward Broadbent                         }
2150fac6e53bSKrzysztof Grobelny 
2151fac6e53bSKrzysztof Grobelny                         if (serialNumber != nullptr)
2152fac6e53bSKrzysztof Grobelny                         {
2153fac6e53bSKrzysztof Grobelny                             asyncResp->res.jsonValue["SerialNumber"] =
2154fac6e53bSKrzysztof Grobelny                                 *serialNumber;
21557e860f15SJohn Edward Broadbent                         }
2156fac6e53bSKrzysztof Grobelny 
2157fac6e53bSKrzysztof Grobelny                         if (manufacturer != nullptr)
2158fac6e53bSKrzysztof Grobelny                         {
2159fac6e53bSKrzysztof Grobelny                             asyncResp->res.jsonValue["Manufacturer"] =
2160fac6e53bSKrzysztof Grobelny                                 *manufacturer;
2161fac6e53bSKrzysztof Grobelny                         }
2162fac6e53bSKrzysztof Grobelny 
2163fac6e53bSKrzysztof Grobelny                         if (model != nullptr)
2164fac6e53bSKrzysztof Grobelny                         {
2165fac6e53bSKrzysztof Grobelny                             asyncResp->res.jsonValue["Model"] = *model;
2166fac6e53bSKrzysztof Grobelny                         }
2167fac6e53bSKrzysztof Grobelny 
2168fac6e53bSKrzysztof Grobelny                         if (sparePartNumber != nullptr)
2169fac6e53bSKrzysztof Grobelny                         {
2170fac6e53bSKrzysztof Grobelny                             asyncResp->res.jsonValue["SparePartNumber"] =
2171fac6e53bSKrzysztof Grobelny                                 *sparePartNumber;
2172fac6e53bSKrzysztof Grobelny                         }
2173fac6e53bSKrzysztof Grobelny                         });
21747e860f15SJohn Edward Broadbent                 }
2175002d39b4SEd Tanous                 else if (interfaceName ==
21760fda0f12SGeorge Liu                          "xyz.openbmc_project.Inventory.Decorator.LocationCode")
21777e860f15SJohn Edward Broadbent                 {
21787e860f15SJohn Edward Broadbent                     getLocation(asyncResp, connectionName, path);
21797e860f15SJohn Edward Broadbent                 }
21807e860f15SJohn Edward Broadbent             }
2181e99073f5SGeorge Liu             });
21827e860f15SJohn Edward Broadbent         });
21837e860f15SJohn Edward Broadbent 
21847e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/")
2185ed398213SEd Tanous         .privileges(redfish::privileges::patchManager)
218645ca1b86SEd Tanous         .methods(boost::beast::http::verb::patch)(
218745ca1b86SEd Tanous             [&app](const crow::Request& req,
21887e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
21893ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
219045ca1b86SEd Tanous         {
219145ca1b86SEd Tanous             return;
219245ca1b86SEd Tanous         }
21937e860f15SJohn Edward Broadbent         std::optional<nlohmann::json> oem;
21947e860f15SJohn Edward Broadbent         std::optional<nlohmann::json> links;
21957e860f15SJohn Edward Broadbent         std::optional<std::string> datetime;
21967e860f15SJohn Edward Broadbent 
219715ed6780SWilly Tu         if (!json_util::readJsonPatch(req, asyncResp->res, "Oem", oem,
2198002d39b4SEd Tanous                                       "DateTime", datetime, "Links", links))
21997e860f15SJohn Edward Broadbent         {
22007e860f15SJohn Edward Broadbent             return;
22017e860f15SJohn Edward Broadbent         }
22027e860f15SJohn Edward Broadbent 
22037e860f15SJohn Edward Broadbent         if (oem)
22047e860f15SJohn Edward Broadbent         {
220554dce7f5SGunnar Mills #ifdef BMCWEB_ENABLE_REDFISH_OEM_MANAGER_FAN_DATA
22067e860f15SJohn Edward Broadbent             std::optional<nlohmann::json> openbmc;
2207002d39b4SEd Tanous             if (!redfish::json_util::readJson(*oem, asyncResp->res, "OpenBmc",
2208002d39b4SEd Tanous                                               openbmc))
22097e860f15SJohn Edward Broadbent             {
22107e860f15SJohn Edward Broadbent                 return;
22117e860f15SJohn Edward Broadbent             }
22127e860f15SJohn Edward Broadbent             if (openbmc)
22137e860f15SJohn Edward Broadbent             {
22147e860f15SJohn Edward Broadbent                 std::optional<nlohmann::json> fan;
2215002d39b4SEd Tanous                 if (!redfish::json_util::readJson(*openbmc, asyncResp->res,
2216002d39b4SEd Tanous                                                   "Fan", fan))
22177e860f15SJohn Edward Broadbent                 {
22187e860f15SJohn Edward Broadbent                     return;
22197e860f15SJohn Edward Broadbent                 }
22207e860f15SJohn Edward Broadbent                 if (fan)
22217e860f15SJohn Edward Broadbent                 {
2222002d39b4SEd Tanous                     auto pid = std::make_shared<SetPIDValues>(asyncResp, *fan);
22237e860f15SJohn Edward Broadbent                     pid->run();
22247e860f15SJohn Edward Broadbent                 }
22257e860f15SJohn Edward Broadbent             }
222654dce7f5SGunnar Mills #else
222754dce7f5SGunnar Mills             messages::propertyUnknown(asyncResp->res, "Oem");
222854dce7f5SGunnar Mills             return;
222954dce7f5SGunnar Mills #endif
22307e860f15SJohn Edward Broadbent         }
22317e860f15SJohn Edward Broadbent         if (links)
22327e860f15SJohn Edward Broadbent         {
22337e860f15SJohn Edward Broadbent             std::optional<nlohmann::json> activeSoftwareImage;
22347e860f15SJohn Edward Broadbent             if (!redfish::json_util::readJson(*links, asyncResp->res,
22357e860f15SJohn Edward Broadbent                                               "ActiveSoftwareImage",
22367e860f15SJohn Edward Broadbent                                               activeSoftwareImage))
22377e860f15SJohn Edward Broadbent             {
22387e860f15SJohn Edward Broadbent                 return;
22397e860f15SJohn Edward Broadbent             }
22407e860f15SJohn Edward Broadbent             if (activeSoftwareImage)
22417e860f15SJohn Edward Broadbent             {
22427e860f15SJohn Edward Broadbent                 std::optional<std::string> odataId;
2243002d39b4SEd Tanous                 if (!json_util::readJson(*activeSoftwareImage, asyncResp->res,
2244002d39b4SEd Tanous                                          "@odata.id", odataId))
22457e860f15SJohn Edward Broadbent                 {
22467e860f15SJohn Edward Broadbent                     return;
22477e860f15SJohn Edward Broadbent                 }
22487e860f15SJohn Edward Broadbent 
22497e860f15SJohn Edward Broadbent                 if (odataId)
22507e860f15SJohn Edward Broadbent                 {
22517e860f15SJohn Edward Broadbent                     setActiveFirmwareImage(asyncResp, *odataId);
22527e860f15SJohn Edward Broadbent                 }
22537e860f15SJohn Edward Broadbent             }
22547e860f15SJohn Edward Broadbent         }
22557e860f15SJohn Edward Broadbent         if (datetime)
22567e860f15SJohn Edward Broadbent         {
22577e860f15SJohn Edward Broadbent             setDateTime(asyncResp, std::move(*datetime));
22587e860f15SJohn Edward Broadbent         }
22597e860f15SJohn Edward Broadbent         });
22607e860f15SJohn Edward Broadbent }
22617e860f15SJohn Edward Broadbent 
22627e860f15SJohn Edward Broadbent inline void requestRoutesManagerCollection(App& app)
22637e860f15SJohn Edward Broadbent {
22647e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/")
2265ed398213SEd Tanous         .privileges(redfish::privileges::getManagerCollection)
22667e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
226745ca1b86SEd Tanous             [&app](const crow::Request& req,
22687e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
22693ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
227045ca1b86SEd Tanous         {
227145ca1b86SEd Tanous             return;
227245ca1b86SEd Tanous         }
227383ff9ab6SJames Feist         // Collections don't include the static data added by SubRoute
227483ff9ab6SJames Feist         // because it has a duplicate entry for members
22758d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
22768d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
22778d1b46d7Szhanghch05             "#ManagerCollection.ManagerCollection";
22788d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "Manager Collection";
22798d1b46d7Szhanghch05         asyncResp->res.jsonValue["Members@odata.count"] = 1;
22801476687dSEd Tanous         nlohmann::json::array_t members;
22811476687dSEd Tanous         nlohmann::json& bmc = members.emplace_back();
22821476687dSEd Tanous         bmc["@odata.id"] = "/redfish/v1/Managers/bmc";
22831476687dSEd Tanous         asyncResp->res.jsonValue["Members"] = std::move(members);
22847e860f15SJohn Edward Broadbent         });
22859c310685SBorawski.Lukasz }
22869c310685SBorawski.Lukasz } // namespace redfish
2287