xref: /openbmc/bmcweb/features/redfish/lib/managers.hpp (revision 71f52d96b51bda2a2f00374237f368e980396692)
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 
18b49ac873SJames Feist #include "health.hpp"
199c310685SBorawski.Lukasz #include "node.hpp"
20c5d03ff4SJennifer Lee #include "redfish_util.hpp"
219c310685SBorawski.Lukasz 
225b4aa86bSJames Feist #include <boost/algorithm/string/replace.hpp>
23af5d6058SSantosh Puranik #include <boost/date_time.hpp>
245b4aa86bSJames Feist #include <dbus_utility.hpp>
25e90c5052SAndrew Geissler #include <utils/fw_utils.hpp>
267bffdb7eSBernard Wong #include <utils/systemd_utils.hpp>
271214b7e7SGunnar Mills 
284bfefa74SGunnar Mills #include <cstdint>
291214b7e7SGunnar Mills #include <memory>
301214b7e7SGunnar Mills #include <sstream>
31abf2add6SEd Tanous #include <variant>
325b4aa86bSJames Feist 
331abe55efSEd Tanous namespace redfish
341abe55efSEd Tanous {
35ed5befbdSJennifer Lee 
36ed5befbdSJennifer Lee /**
372a5c4407SGunnar Mills  * Function reboots the BMC.
382a5c4407SGunnar Mills  *
392a5c4407SGunnar Mills  * @param[in] asyncResp - Shared pointer for completing asynchronous calls
40ed5befbdSJennifer Lee  */
41b5a76932SEd Tanous inline void doBMCGracefulRestart(const std::shared_ptr<AsyncResp>& asyncResp)
42ed5befbdSJennifer Lee {
43ed5befbdSJennifer Lee     const char* processName = "xyz.openbmc_project.State.BMC";
44ed5befbdSJennifer Lee     const char* objectPath = "/xyz/openbmc_project/state/bmc0";
45ed5befbdSJennifer Lee     const char* interfaceName = "xyz.openbmc_project.State.BMC";
46ed5befbdSJennifer Lee     const std::string& propertyValue =
47ed5befbdSJennifer Lee         "xyz.openbmc_project.State.BMC.Transition.Reboot";
48ed5befbdSJennifer Lee     const char* destProperty = "RequestedBMCTransition";
49ed5befbdSJennifer Lee 
50ed5befbdSJennifer Lee     // Create the D-Bus variant for D-Bus call.
51ed5befbdSJennifer Lee     VariantType dbusPropertyValue(propertyValue);
52ed5befbdSJennifer Lee 
53ed5befbdSJennifer Lee     crow::connections::systemBus->async_method_call(
54ed5befbdSJennifer Lee         [asyncResp](const boost::system::error_code ec) {
55ed5befbdSJennifer Lee             // Use "Set" method to set the property value.
56ed5befbdSJennifer Lee             if (ec)
57ed5befbdSJennifer Lee             {
582a5c4407SGunnar Mills                 BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
59ed5befbdSJennifer Lee                 messages::internalError(asyncResp->res);
60ed5befbdSJennifer Lee                 return;
61ed5befbdSJennifer Lee             }
62ed5befbdSJennifer Lee 
63ed5befbdSJennifer Lee             messages::success(asyncResp->res);
64ed5befbdSJennifer Lee         },
65ed5befbdSJennifer Lee         processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
66ed5befbdSJennifer Lee         interfaceName, destProperty, dbusPropertyValue);
67ed5befbdSJennifer Lee }
682a5c4407SGunnar Mills 
69b5a76932SEd Tanous inline void doBMCForceRestart(const std::shared_ptr<AsyncResp>& asyncResp)
70f92af389SJayaprakash Mutyala {
71f92af389SJayaprakash Mutyala     const char* processName = "xyz.openbmc_project.State.BMC";
72f92af389SJayaprakash Mutyala     const char* objectPath = "/xyz/openbmc_project/state/bmc0";
73f92af389SJayaprakash Mutyala     const char* interfaceName = "xyz.openbmc_project.State.BMC";
74f92af389SJayaprakash Mutyala     const std::string& propertyValue =
75f92af389SJayaprakash Mutyala         "xyz.openbmc_project.State.BMC.Transition.HardReboot";
76f92af389SJayaprakash Mutyala     const char* destProperty = "RequestedBMCTransition";
77f92af389SJayaprakash Mutyala 
78f92af389SJayaprakash Mutyala     // Create the D-Bus variant for D-Bus call.
79f92af389SJayaprakash Mutyala     VariantType dbusPropertyValue(propertyValue);
80f92af389SJayaprakash Mutyala 
81f92af389SJayaprakash Mutyala     crow::connections::systemBus->async_method_call(
82f92af389SJayaprakash Mutyala         [asyncResp](const boost::system::error_code ec) {
83f92af389SJayaprakash Mutyala             // Use "Set" method to set the property value.
84f92af389SJayaprakash Mutyala             if (ec)
85f92af389SJayaprakash Mutyala             {
86f92af389SJayaprakash Mutyala                 BMCWEB_LOG_DEBUG << "[Set] Bad D-Bus request error: " << ec;
87f92af389SJayaprakash Mutyala                 messages::internalError(asyncResp->res);
88f92af389SJayaprakash Mutyala                 return;
89f92af389SJayaprakash Mutyala             }
90f92af389SJayaprakash Mutyala 
91f92af389SJayaprakash Mutyala             messages::success(asyncResp->res);
92f92af389SJayaprakash Mutyala         },
93f92af389SJayaprakash Mutyala         processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
94f92af389SJayaprakash Mutyala         interfaceName, destProperty, dbusPropertyValue);
95f92af389SJayaprakash Mutyala }
96f92af389SJayaprakash Mutyala 
972a5c4407SGunnar Mills /**
982a5c4407SGunnar Mills  * ManagerResetAction class supports the POST method for the Reset (reboot)
992a5c4407SGunnar Mills  * action.
1002a5c4407SGunnar Mills  */
1012a5c4407SGunnar Mills class ManagerResetAction : public Node
1022a5c4407SGunnar Mills {
1032a5c4407SGunnar Mills   public:
10452cc112dSEd Tanous     ManagerResetAction(App& app) :
1052a5c4407SGunnar Mills         Node(app, "/redfish/v1/Managers/bmc/Actions/Manager.Reset/")
1062a5c4407SGunnar Mills     {
1072a5c4407SGunnar Mills         entityPrivileges = {
1082a5c4407SGunnar Mills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1092a5c4407SGunnar Mills     }
1102a5c4407SGunnar Mills 
1112a5c4407SGunnar Mills   private:
1122a5c4407SGunnar Mills     /**
1132a5c4407SGunnar Mills      * Function handles POST method request.
1142a5c4407SGunnar Mills      * Analyzes POST body before sending Reset (Reboot) request data to D-Bus.
115f92af389SJayaprakash Mutyala      * OpenBMC supports ResetType "GracefulRestart" and "ForceRestart".
1162a5c4407SGunnar Mills      */
1172a5c4407SGunnar Mills     void doPost(crow::Response& res, const crow::Request& req,
118cb13a392SEd Tanous                 const std::vector<std::string>&) override
1192a5c4407SGunnar Mills     {
1202a5c4407SGunnar Mills         BMCWEB_LOG_DEBUG << "Post Manager Reset.";
1212a5c4407SGunnar Mills 
1222a5c4407SGunnar Mills         std::string resetType;
1232a5c4407SGunnar Mills         auto asyncResp = std::make_shared<AsyncResp>(res);
1242a5c4407SGunnar Mills 
1252a5c4407SGunnar Mills         if (!json_util::readJson(req, asyncResp->res, "ResetType", resetType))
1262a5c4407SGunnar Mills         {
1272a5c4407SGunnar Mills             return;
1282a5c4407SGunnar Mills         }
1292a5c4407SGunnar Mills 
130f92af389SJayaprakash Mutyala         if (resetType == "GracefulRestart")
131f92af389SJayaprakash Mutyala         {
132f92af389SJayaprakash Mutyala             BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
133f92af389SJayaprakash Mutyala             doBMCGracefulRestart(asyncResp);
134f92af389SJayaprakash Mutyala             return;
135f92af389SJayaprakash Mutyala         }
1363174e4dfSEd Tanous         if (resetType == "ForceRestart")
137f92af389SJayaprakash Mutyala         {
138f92af389SJayaprakash Mutyala             BMCWEB_LOG_DEBUG << "Proceeding with " << resetType;
139f92af389SJayaprakash Mutyala             doBMCForceRestart(asyncResp);
140f92af389SJayaprakash Mutyala             return;
141f92af389SJayaprakash Mutyala         }
1422a5c4407SGunnar Mills         BMCWEB_LOG_DEBUG << "Invalid property value for ResetType: "
1432a5c4407SGunnar Mills                          << resetType;
1442a5c4407SGunnar Mills         messages::actionParameterNotSupported(asyncResp->res, resetType,
1452a5c4407SGunnar Mills                                               "ResetType");
1462a5c4407SGunnar Mills 
1472a5c4407SGunnar Mills         return;
1482a5c4407SGunnar Mills     }
149ed5befbdSJennifer Lee };
150ed5befbdSJennifer Lee 
1513e40fc74SGunnar Mills /**
1523e40fc74SGunnar Mills  * ManagerResetToDefaultsAction class supports POST method for factory reset
1533e40fc74SGunnar Mills  * action.
1543e40fc74SGunnar Mills  */
1553e40fc74SGunnar Mills class ManagerResetToDefaultsAction : public Node
1563e40fc74SGunnar Mills {
1573e40fc74SGunnar Mills   public:
15852cc112dSEd Tanous     ManagerResetToDefaultsAction(App& app) :
1593e40fc74SGunnar Mills         Node(app, "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults/")
1603e40fc74SGunnar Mills     {
1613e40fc74SGunnar Mills         entityPrivileges = {
1623e40fc74SGunnar Mills             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1633e40fc74SGunnar Mills     }
1643e40fc74SGunnar Mills 
1653e40fc74SGunnar Mills   private:
1663e40fc74SGunnar Mills     /**
1673e40fc74SGunnar Mills      * Function handles ResetToDefaults POST method request.
1683e40fc74SGunnar Mills      *
1693e40fc74SGunnar Mills      * Analyzes POST body message and factory resets BMC by calling
1703e40fc74SGunnar Mills      * BMC code updater factory reset followed by a BMC reboot.
1713e40fc74SGunnar Mills      *
1723e40fc74SGunnar Mills      * BMC code updater factory reset wipes the whole BMC read-write
1733e40fc74SGunnar Mills      * filesystem which includes things like the network settings.
1743e40fc74SGunnar Mills      *
1753e40fc74SGunnar Mills      * OpenBMC only supports ResetToDefaultsType "ResetAll".
1763e40fc74SGunnar Mills      */
1773e40fc74SGunnar Mills     void doPost(crow::Response& res, const crow::Request& req,
178cb13a392SEd Tanous                 const std::vector<std::string>&) override
1793e40fc74SGunnar Mills     {
1803e40fc74SGunnar Mills         BMCWEB_LOG_DEBUG << "Post ResetToDefaults.";
1813e40fc74SGunnar Mills 
1823e40fc74SGunnar Mills         std::string resetType;
1833e40fc74SGunnar Mills         auto asyncResp = std::make_shared<AsyncResp>(res);
1843e40fc74SGunnar Mills 
1853e40fc74SGunnar Mills         if (!json_util::readJson(req, asyncResp->res, "ResetToDefaultsType",
1863e40fc74SGunnar Mills                                  resetType))
1873e40fc74SGunnar Mills         {
1883e40fc74SGunnar Mills             BMCWEB_LOG_DEBUG << "Missing property ResetToDefaultsType.";
1893e40fc74SGunnar Mills 
1903e40fc74SGunnar Mills             messages::actionParameterMissing(asyncResp->res, "ResetToDefaults",
1913e40fc74SGunnar Mills                                              "ResetToDefaultsType");
1923e40fc74SGunnar Mills             return;
1933e40fc74SGunnar Mills         }
1943e40fc74SGunnar Mills 
1953e40fc74SGunnar Mills         if (resetType != "ResetAll")
1963e40fc74SGunnar Mills         {
1973e40fc74SGunnar Mills             BMCWEB_LOG_DEBUG << "Invalid property value for "
1983e40fc74SGunnar Mills                                 "ResetToDefaultsType: "
1993e40fc74SGunnar Mills                              << resetType;
2003e40fc74SGunnar Mills             messages::actionParameterNotSupported(asyncResp->res, resetType,
2013e40fc74SGunnar Mills                                                   "ResetToDefaultsType");
2023e40fc74SGunnar Mills             return;
2033e40fc74SGunnar Mills         }
2043e40fc74SGunnar Mills 
2053e40fc74SGunnar Mills         crow::connections::systemBus->async_method_call(
2063e40fc74SGunnar Mills             [asyncResp](const boost::system::error_code ec) {
2073e40fc74SGunnar Mills                 if (ec)
2083e40fc74SGunnar Mills                 {
2093e40fc74SGunnar Mills                     BMCWEB_LOG_DEBUG << "Failed to ResetToDefaults: " << ec;
2103e40fc74SGunnar Mills                     messages::internalError(asyncResp->res);
2113e40fc74SGunnar Mills                     return;
2123e40fc74SGunnar Mills                 }
2133e40fc74SGunnar Mills                 // Factory Reset doesn't actually happen until a reboot
2143e40fc74SGunnar Mills                 // Can't erase what the BMC is running on
2153e40fc74SGunnar Mills                 doBMCGracefulRestart(asyncResp);
2163e40fc74SGunnar Mills             },
2173e40fc74SGunnar Mills             "xyz.openbmc_project.Software.BMC.Updater",
2183e40fc74SGunnar Mills             "/xyz/openbmc_project/software",
2193e40fc74SGunnar Mills             "xyz.openbmc_project.Common.FactoryReset", "Reset");
2203e40fc74SGunnar Mills     }
2213e40fc74SGunnar Mills };
2223e40fc74SGunnar Mills 
2231cb1a9e6SAppaRao Puli /**
2241cb1a9e6SAppaRao Puli  * ManagerResetActionInfo derived class for delivering Manager
2251cb1a9e6SAppaRao Puli  * ResetType AllowableValues using ResetInfo schema.
2261cb1a9e6SAppaRao Puli  */
2271cb1a9e6SAppaRao Puli class ManagerResetActionInfo : public Node
2281cb1a9e6SAppaRao Puli {
2291cb1a9e6SAppaRao Puli   public:
2301cb1a9e6SAppaRao Puli     /*
2311cb1a9e6SAppaRao Puli      * Default Constructor
2321cb1a9e6SAppaRao Puli      */
23352cc112dSEd Tanous     ManagerResetActionInfo(App& app) :
2341cb1a9e6SAppaRao Puli         Node(app, "/redfish/v1/Managers/bmc/ResetActionInfo/")
2351cb1a9e6SAppaRao Puli     {
2361cb1a9e6SAppaRao Puli         entityPrivileges = {
2371cb1a9e6SAppaRao Puli             {boost::beast::http::verb::get, {{"Login"}}},
2381cb1a9e6SAppaRao Puli             {boost::beast::http::verb::head, {{"Login"}}},
2391cb1a9e6SAppaRao Puli             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2401cb1a9e6SAppaRao Puli             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2411cb1a9e6SAppaRao Puli             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2421cb1a9e6SAppaRao Puli             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
2431cb1a9e6SAppaRao Puli     }
2441cb1a9e6SAppaRao Puli 
2451cb1a9e6SAppaRao Puli   private:
2461cb1a9e6SAppaRao Puli     /**
2471cb1a9e6SAppaRao Puli      * Functions triggers appropriate requests on DBus
2481cb1a9e6SAppaRao Puli      */
249cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
250cb13a392SEd Tanous                const std::vector<std::string>&) override
2511cb1a9e6SAppaRao Puli     {
2521cb1a9e6SAppaRao Puli         res.jsonValue = {
2531cb1a9e6SAppaRao Puli             {"@odata.type", "#ActionInfo.v1_1_2.ActionInfo"},
2541cb1a9e6SAppaRao Puli             {"@odata.id", "/redfish/v1/Managers/bmc/ResetActionInfo"},
2551cb1a9e6SAppaRao Puli             {"Name", "Reset Action Info"},
2561cb1a9e6SAppaRao Puli             {"Id", "ResetActionInfo"},
2571cb1a9e6SAppaRao Puli             {"Parameters",
2581cb1a9e6SAppaRao Puli              {{{"Name", "ResetType"},
2591cb1a9e6SAppaRao Puli                {"Required", true},
2601cb1a9e6SAppaRao Puli                {"DataType", "String"},
261f92af389SJayaprakash Mutyala                {"AllowableValues", {"GracefulRestart", "ForceRestart"}}}}}};
2621cb1a9e6SAppaRao Puli         res.end();
2631cb1a9e6SAppaRao Puli     }
2641cb1a9e6SAppaRao Puli };
2651cb1a9e6SAppaRao Puli 
2665b4aa86bSJames Feist static constexpr const char* objectManagerIface =
2675b4aa86bSJames Feist     "org.freedesktop.DBus.ObjectManager";
2685b4aa86bSJames Feist static constexpr const char* pidConfigurationIface =
2695b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid";
2705b4aa86bSJames Feist static constexpr const char* pidZoneConfigurationIface =
2715b4aa86bSJames Feist     "xyz.openbmc_project.Configuration.Pid.Zone";
272b7a08d04SJames Feist static constexpr const char* stepwiseConfigurationIface =
273b7a08d04SJames Feist     "xyz.openbmc_project.Configuration.Stepwise";
27473df0db0SJames Feist static constexpr const char* thermalModeIface =
27573df0db0SJames Feist     "xyz.openbmc_project.Control.ThermalMode";
2769c310685SBorawski.Lukasz 
27723a21a1cSEd Tanous inline void asyncPopulatePid(const std::string& connection,
2785b4aa86bSJames Feist                              const std::string& path,
27973df0db0SJames Feist                              const std::string& currentProfile,
28073df0db0SJames Feist                              const std::vector<std::string>& supportedProfiles,
281b5a76932SEd Tanous                              const std::shared_ptr<AsyncResp>& asyncResp)
2825b4aa86bSJames Feist {
2835b4aa86bSJames Feist 
2845b4aa86bSJames Feist     crow::connections::systemBus->async_method_call(
28573df0db0SJames Feist         [asyncResp, currentProfile, supportedProfiles](
28673df0db0SJames Feist             const boost::system::error_code ec,
2875b4aa86bSJames Feist             const dbus::utility::ManagedObjectType& managedObj) {
2885b4aa86bSJames Feist             if (ec)
2895b4aa86bSJames Feist             {
2905b4aa86bSJames Feist                 BMCWEB_LOG_ERROR << ec;
2915b4aa86bSJames Feist                 asyncResp->res.jsonValue.clear();
292f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
2935b4aa86bSJames Feist                 return;
2945b4aa86bSJames Feist             }
2955b4aa86bSJames Feist             nlohmann::json& configRoot =
2965b4aa86bSJames Feist                 asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"];
2975b4aa86bSJames Feist             nlohmann::json& fans = configRoot["FanControllers"];
2985b4aa86bSJames Feist             fans["@odata.type"] = "#OemManager.FanControllers";
2995b4aa86bSJames Feist             fans["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc/"
3005b4aa86bSJames Feist                                 "Fan/FanControllers";
3015b4aa86bSJames Feist 
3025b4aa86bSJames Feist             nlohmann::json& pids = configRoot["PidControllers"];
3035b4aa86bSJames Feist             pids["@odata.type"] = "#OemManager.PidControllers";
3045b4aa86bSJames Feist             pids["@odata.id"] =
3055b4aa86bSJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers";
3065b4aa86bSJames Feist 
307b7a08d04SJames Feist             nlohmann::json& stepwise = configRoot["StepwiseControllers"];
308b7a08d04SJames Feist             stepwise["@odata.type"] = "#OemManager.StepwiseControllers";
309b7a08d04SJames Feist             stepwise["@odata.id"] =
310b7a08d04SJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers";
311b7a08d04SJames Feist 
3125b4aa86bSJames Feist             nlohmann::json& zones = configRoot["FanZones"];
3135b4aa86bSJames Feist             zones["@odata.id"] =
3145b4aa86bSJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones";
3155b4aa86bSJames Feist             zones["@odata.type"] = "#OemManager.FanZones";
3165b4aa86bSJames Feist             configRoot["@odata.id"] =
3175b4aa86bSJames Feist                 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan";
3185b4aa86bSJames Feist             configRoot["@odata.type"] = "#OemManager.Fan";
31973df0db0SJames Feist             configRoot["Profile@Redfish.AllowableValues"] = supportedProfiles;
32073df0db0SJames Feist 
32173df0db0SJames Feist             if (!currentProfile.empty())
32273df0db0SJames Feist             {
32373df0db0SJames Feist                 configRoot["Profile"] = currentProfile;
32473df0db0SJames Feist             }
32573df0db0SJames Feist             BMCWEB_LOG_ERROR << "profile = " << currentProfile << " !";
3265b4aa86bSJames Feist 
3275b4aa86bSJames Feist             for (const auto& pathPair : managedObj)
3285b4aa86bSJames Feist             {
3295b4aa86bSJames Feist                 for (const auto& intfPair : pathPair.second)
3305b4aa86bSJames Feist                 {
3315b4aa86bSJames Feist                     if (intfPair.first != pidConfigurationIface &&
332b7a08d04SJames Feist                         intfPair.first != pidZoneConfigurationIface &&
333b7a08d04SJames Feist                         intfPair.first != stepwiseConfigurationIface)
3345b4aa86bSJames Feist                     {
3355b4aa86bSJames Feist                         continue;
3365b4aa86bSJames Feist                     }
3375b4aa86bSJames Feist                     auto findName = intfPair.second.find("Name");
3385b4aa86bSJames Feist                     if (findName == intfPair.second.end())
3395b4aa86bSJames Feist                     {
3405b4aa86bSJames Feist                         BMCWEB_LOG_ERROR << "Pid Field missing Name";
341a08b46ccSJason M. Bills                         messages::internalError(asyncResp->res);
3425b4aa86bSJames Feist                         return;
3435b4aa86bSJames Feist                     }
34473df0db0SJames Feist 
3455b4aa86bSJames Feist                     const std::string* namePtr =
346abf2add6SEd Tanous                         std::get_if<std::string>(&findName->second);
3475b4aa86bSJames Feist                     if (namePtr == nullptr)
3485b4aa86bSJames Feist                     {
3495b4aa86bSJames Feist                         BMCWEB_LOG_ERROR << "Pid Name Field illegal";
350b7a08d04SJames Feist                         messages::internalError(asyncResp->res);
3515b4aa86bSJames Feist                         return;
3525b4aa86bSJames Feist                     }
3535b4aa86bSJames Feist                     std::string name = *namePtr;
3545b4aa86bSJames Feist                     dbus::utility::escapePathForDbus(name);
35573df0db0SJames Feist 
35673df0db0SJames Feist                     auto findProfiles = intfPair.second.find("Profiles");
35773df0db0SJames Feist                     if (findProfiles != intfPair.second.end())
35873df0db0SJames Feist                     {
35973df0db0SJames Feist                         const std::vector<std::string>* profiles =
36073df0db0SJames Feist                             std::get_if<std::vector<std::string>>(
36173df0db0SJames Feist                                 &findProfiles->second);
36273df0db0SJames Feist                         if (profiles == nullptr)
36373df0db0SJames Feist                         {
36473df0db0SJames Feist                             BMCWEB_LOG_ERROR << "Pid Profiles Field illegal";
36573df0db0SJames Feist                             messages::internalError(asyncResp->res);
36673df0db0SJames Feist                             return;
36773df0db0SJames Feist                         }
36873df0db0SJames Feist                         if (std::find(profiles->begin(), profiles->end(),
36973df0db0SJames Feist                                       currentProfile) == profiles->end())
37073df0db0SJames Feist                         {
37173df0db0SJames Feist                             BMCWEB_LOG_INFO
37273df0db0SJames Feist                                 << name << " not supported in current profile";
37373df0db0SJames Feist                             continue;
37473df0db0SJames Feist                         }
37573df0db0SJames Feist                     }
376b7a08d04SJames Feist                     nlohmann::json* config = nullptr;
377c33a90ecSJames Feist 
378c33a90ecSJames Feist                     const std::string* classPtr = nullptr;
379c33a90ecSJames Feist                     auto findClass = intfPair.second.find("Class");
380c33a90ecSJames Feist                     if (findClass != intfPair.second.end())
381c33a90ecSJames Feist                     {
382c33a90ecSJames Feist                         classPtr = std::get_if<std::string>(&findClass->second);
383c33a90ecSJames Feist                     }
384c33a90ecSJames Feist 
3855b4aa86bSJames Feist                     if (intfPair.first == pidZoneConfigurationIface)
3865b4aa86bSJames Feist                     {
3875b4aa86bSJames Feist                         std::string chassis;
3885b4aa86bSJames Feist                         if (!dbus::utility::getNthStringFromPath(
3895b4aa86bSJames Feist                                 pathPair.first.str, 5, chassis))
3905b4aa86bSJames Feist                         {
3915b4aa86bSJames Feist                             chassis = "#IllegalValue";
3925b4aa86bSJames Feist                         }
3935b4aa86bSJames Feist                         nlohmann::json& zone = zones[name];
3945b4aa86bSJames Feist                         zone["Chassis"] = {
3955b4aa86bSJames Feist                             {"@odata.id", "/redfish/v1/Chassis/" + chassis}};
3965b4aa86bSJames Feist                         zone["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/"
3975b4aa86bSJames Feist                                             "OpenBmc/Fan/FanZones/" +
3985b4aa86bSJames Feist                                             name;
3995b4aa86bSJames Feist                         zone["@odata.type"] = "#OemManager.FanZone";
400b7a08d04SJames Feist                         config = &zone;
4015b4aa86bSJames Feist                     }
4025b4aa86bSJames Feist 
403b7a08d04SJames Feist                     else if (intfPair.first == stepwiseConfigurationIface)
4045b4aa86bSJames Feist                     {
405c33a90ecSJames Feist                         if (classPtr == nullptr)
406c33a90ecSJames Feist                         {
407c33a90ecSJames Feist                             BMCWEB_LOG_ERROR << "Pid Class Field illegal";
408c33a90ecSJames Feist                             messages::internalError(asyncResp->res);
409c33a90ecSJames Feist                             return;
410c33a90ecSJames Feist                         }
411c33a90ecSJames Feist 
412b7a08d04SJames Feist                         nlohmann::json& controller = stepwise[name];
413b7a08d04SJames Feist                         config = &controller;
4145b4aa86bSJames Feist 
415b7a08d04SJames Feist                         controller["@odata.id"] =
416b7a08d04SJames Feist                             "/redfish/v1/Managers/bmc#/Oem/"
417b7a08d04SJames Feist                             "OpenBmc/Fan/StepwiseControllers/" +
418271584abSEd Tanous                             name;
419b7a08d04SJames Feist                         controller["@odata.type"] =
420b7a08d04SJames Feist                             "#OemManager.StepwiseController";
421b7a08d04SJames Feist 
422c33a90ecSJames Feist                         controller["Direction"] = *classPtr;
4235b4aa86bSJames Feist                     }
4245b4aa86bSJames Feist 
4255b4aa86bSJames Feist                     // pid and fans are off the same configuration
426b7a08d04SJames Feist                     else if (intfPair.first == pidConfigurationIface)
4275b4aa86bSJames Feist                     {
428c33a90ecSJames Feist 
4295b4aa86bSJames Feist                         if (classPtr == nullptr)
4305b4aa86bSJames Feist                         {
4315b4aa86bSJames Feist                             BMCWEB_LOG_ERROR << "Pid Class Field illegal";
432a08b46ccSJason M. Bills                             messages::internalError(asyncResp->res);
4335b4aa86bSJames Feist                             return;
4345b4aa86bSJames Feist                         }
4355b4aa86bSJames Feist                         bool isFan = *classPtr == "fan";
4365b4aa86bSJames Feist                         nlohmann::json& element =
4375b4aa86bSJames Feist                             isFan ? fans[name] : pids[name];
438b7a08d04SJames Feist                         config = &element;
4395b4aa86bSJames Feist                         if (isFan)
4405b4aa86bSJames Feist                         {
4415b4aa86bSJames Feist                             element["@odata.id"] =
4425b4aa86bSJames Feist                                 "/redfish/v1/Managers/bmc#/Oem/"
4435b4aa86bSJames Feist                                 "OpenBmc/Fan/FanControllers/" +
444271584abSEd Tanous                                 name;
4455b4aa86bSJames Feist                             element["@odata.type"] =
4465b4aa86bSJames Feist                                 "#OemManager.FanController";
4475b4aa86bSJames Feist                         }
4485b4aa86bSJames Feist                         else
4495b4aa86bSJames Feist                         {
4505b4aa86bSJames Feist                             element["@odata.id"] =
4515b4aa86bSJames Feist                                 "/redfish/v1/Managers/bmc#/Oem/"
4525b4aa86bSJames Feist                                 "OpenBmc/Fan/PidControllers/" +
453271584abSEd Tanous                                 name;
4545b4aa86bSJames Feist                             element["@odata.type"] =
4555b4aa86bSJames Feist                                 "#OemManager.PidController";
4565b4aa86bSJames Feist                         }
457b7a08d04SJames Feist                     }
458b7a08d04SJames Feist                     else
459b7a08d04SJames Feist                     {
460b7a08d04SJames Feist                         BMCWEB_LOG_ERROR << "Unexpected configuration";
461b7a08d04SJames Feist                         messages::internalError(asyncResp->res);
462b7a08d04SJames Feist                         return;
463b7a08d04SJames Feist                     }
464b7a08d04SJames Feist 
465b7a08d04SJames Feist                     // used for making maps out of 2 vectors
466b7a08d04SJames Feist                     const std::vector<double>* keys = nullptr;
467b7a08d04SJames Feist                     const std::vector<double>* values = nullptr;
468b7a08d04SJames Feist 
469b7a08d04SJames Feist                     for (const auto& propertyPair : intfPair.second)
470b7a08d04SJames Feist                     {
471b7a08d04SJames Feist                         if (propertyPair.first == "Type" ||
472b7a08d04SJames Feist                             propertyPair.first == "Class" ||
473b7a08d04SJames Feist                             propertyPair.first == "Name")
474b7a08d04SJames Feist                         {
475b7a08d04SJames Feist                             continue;
476b7a08d04SJames Feist                         }
477b7a08d04SJames Feist 
478b7a08d04SJames Feist                         // zones
479b7a08d04SJames Feist                         if (intfPair.first == pidZoneConfigurationIface)
480b7a08d04SJames Feist                         {
481b7a08d04SJames Feist                             const double* ptr =
482abf2add6SEd Tanous                                 std::get_if<double>(&propertyPair.second);
483b7a08d04SJames Feist                             if (ptr == nullptr)
484b7a08d04SJames Feist                             {
485b7a08d04SJames Feist                                 BMCWEB_LOG_ERROR << "Field Illegal "
486b7a08d04SJames Feist                                                  << propertyPair.first;
487b7a08d04SJames Feist                                 messages::internalError(asyncResp->res);
488b7a08d04SJames Feist                                 return;
489b7a08d04SJames Feist                             }
490b7a08d04SJames Feist                             (*config)[propertyPair.first] = *ptr;
491b7a08d04SJames Feist                         }
492b7a08d04SJames Feist 
493b7a08d04SJames Feist                         if (intfPair.first == stepwiseConfigurationIface)
494b7a08d04SJames Feist                         {
495b7a08d04SJames Feist                             if (propertyPair.first == "Reading" ||
496b7a08d04SJames Feist                                 propertyPair.first == "Output")
497b7a08d04SJames Feist                             {
498b7a08d04SJames Feist                                 const std::vector<double>* ptr =
499abf2add6SEd Tanous                                     std::get_if<std::vector<double>>(
500b7a08d04SJames Feist                                         &propertyPair.second);
501b7a08d04SJames Feist 
502b7a08d04SJames Feist                                 if (ptr == nullptr)
503b7a08d04SJames Feist                                 {
504b7a08d04SJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
505b7a08d04SJames Feist                                                      << propertyPair.first;
506b7a08d04SJames Feist                                     messages::internalError(asyncResp->res);
507b7a08d04SJames Feist                                     return;
508b7a08d04SJames Feist                                 }
509b7a08d04SJames Feist 
510b7a08d04SJames Feist                                 if (propertyPair.first == "Reading")
511b7a08d04SJames Feist                                 {
512b7a08d04SJames Feist                                     keys = ptr;
513b7a08d04SJames Feist                                 }
514b7a08d04SJames Feist                                 else
515b7a08d04SJames Feist                                 {
516b7a08d04SJames Feist                                     values = ptr;
517b7a08d04SJames Feist                                 }
518b7a08d04SJames Feist                                 if (keys && values)
519b7a08d04SJames Feist                                 {
520b7a08d04SJames Feist                                     if (keys->size() != values->size())
521b7a08d04SJames Feist                                     {
522b7a08d04SJames Feist                                         BMCWEB_LOG_ERROR
523b7a08d04SJames Feist                                             << "Reading and Output size don't "
524b7a08d04SJames Feist                                                "match ";
525b7a08d04SJames Feist                                         messages::internalError(asyncResp->res);
526b7a08d04SJames Feist                                         return;
527b7a08d04SJames Feist                                     }
528b7a08d04SJames Feist                                     nlohmann::json& steps = (*config)["Steps"];
529b7a08d04SJames Feist                                     steps = nlohmann::json::array();
530b7a08d04SJames Feist                                     for (size_t ii = 0; ii < keys->size(); ii++)
531b7a08d04SJames Feist                                     {
532b7a08d04SJames Feist                                         steps.push_back(
533b7a08d04SJames Feist                                             {{"Target", (*keys)[ii]},
534b7a08d04SJames Feist                                              {"Output", (*values)[ii]}});
535b7a08d04SJames Feist                                     }
536b7a08d04SJames Feist                                 }
537b7a08d04SJames Feist                             }
538b7a08d04SJames Feist                             if (propertyPair.first == "NegativeHysteresis" ||
539b7a08d04SJames Feist                                 propertyPair.first == "PositiveHysteresis")
540b7a08d04SJames Feist                             {
541b7a08d04SJames Feist                                 const double* ptr =
542abf2add6SEd Tanous                                     std::get_if<double>(&propertyPair.second);
543b7a08d04SJames Feist                                 if (ptr == nullptr)
544b7a08d04SJames Feist                                 {
545b7a08d04SJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
546b7a08d04SJames Feist                                                      << propertyPair.first;
547b7a08d04SJames Feist                                     messages::internalError(asyncResp->res);
548b7a08d04SJames Feist                                     return;
549b7a08d04SJames Feist                                 }
550b7a08d04SJames Feist                                 (*config)[propertyPair.first] = *ptr;
551b7a08d04SJames Feist                             }
552b7a08d04SJames Feist                         }
553b7a08d04SJames Feist 
554b7a08d04SJames Feist                         // pid and fans are off the same configuration
555b7a08d04SJames Feist                         if (intfPair.first == pidConfigurationIface ||
556b7a08d04SJames Feist                             intfPair.first == stepwiseConfigurationIface)
557b7a08d04SJames Feist                         {
5585b4aa86bSJames Feist 
5595b4aa86bSJames Feist                             if (propertyPair.first == "Zones")
5605b4aa86bSJames Feist                             {
5615b4aa86bSJames Feist                                 const std::vector<std::string>* inputs =
562abf2add6SEd Tanous                                     std::get_if<std::vector<std::string>>(
5631b6b96c5SEd Tanous                                         &propertyPair.second);
5645b4aa86bSJames Feist 
5655b4aa86bSJames Feist                                 if (inputs == nullptr)
5665b4aa86bSJames Feist                                 {
5675b4aa86bSJames Feist                                     BMCWEB_LOG_ERROR
5685b4aa86bSJames Feist                                         << "Zones Pid Field Illegal";
569a08b46ccSJason M. Bills                                     messages::internalError(asyncResp->res);
5705b4aa86bSJames Feist                                     return;
5715b4aa86bSJames Feist                                 }
572b7a08d04SJames Feist                                 auto& data = (*config)[propertyPair.first];
5735b4aa86bSJames Feist                                 data = nlohmann::json::array();
5745b4aa86bSJames Feist                                 for (std::string itemCopy : *inputs)
5755b4aa86bSJames Feist                                 {
5765b4aa86bSJames Feist                                     dbus::utility::escapePathForDbus(itemCopy);
5775b4aa86bSJames Feist                                     data.push_back(
5785b4aa86bSJames Feist                                         {{"@odata.id",
5795b4aa86bSJames Feist                                           "/redfish/v1/Managers/bmc#/Oem/"
5805b4aa86bSJames Feist                                           "OpenBmc/Fan/FanZones/" +
5815b4aa86bSJames Feist                                               itemCopy}});
5825b4aa86bSJames Feist                                 }
5835b4aa86bSJames Feist                             }
5845b4aa86bSJames Feist                             // todo(james): may never happen, but this
5855b4aa86bSJames Feist                             // assumes configuration data referenced in the
5865b4aa86bSJames Feist                             // PID config is provided by the same daemon, we
5875b4aa86bSJames Feist                             // could add another loop to cover all cases,
5885b4aa86bSJames Feist                             // but I'm okay kicking this can down the road a
5895b4aa86bSJames Feist                             // bit
5905b4aa86bSJames Feist 
5915b4aa86bSJames Feist                             else if (propertyPair.first == "Inputs" ||
5925b4aa86bSJames Feist                                      propertyPair.first == "Outputs")
5935b4aa86bSJames Feist                             {
594b7a08d04SJames Feist                                 auto& data = (*config)[propertyPair.first];
5955b4aa86bSJames Feist                                 const std::vector<std::string>* inputs =
596abf2add6SEd Tanous                                     std::get_if<std::vector<std::string>>(
5971b6b96c5SEd Tanous                                         &propertyPair.second);
5985b4aa86bSJames Feist 
5995b4aa86bSJames Feist                                 if (inputs == nullptr)
6005b4aa86bSJames Feist                                 {
6015b4aa86bSJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
6025b4aa86bSJames Feist                                                      << propertyPair.first;
603f12894f8SJason M. Bills                                     messages::internalError(asyncResp->res);
6045b4aa86bSJames Feist                                     return;
6055b4aa86bSJames Feist                                 }
6065b4aa86bSJames Feist                                 data = *inputs;
607b943aaefSJames Feist                             }
608b943aaefSJames Feist                             else if (propertyPair.first == "SetPointOffset")
609b943aaefSJames Feist                             {
610b943aaefSJames Feist                                 const std::string* ptr =
611b943aaefSJames Feist                                     std::get_if<std::string>(
612b943aaefSJames Feist                                         &propertyPair.second);
613b943aaefSJames Feist 
614b943aaefSJames Feist                                 if (ptr == nullptr)
615b943aaefSJames Feist                                 {
616b943aaefSJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
617b943aaefSJames Feist                                                      << propertyPair.first;
618b943aaefSJames Feist                                     messages::internalError(asyncResp->res);
619b943aaefSJames Feist                                     return;
620b943aaefSJames Feist                                 }
621b943aaefSJames Feist                                 // translate from dbus to redfish
622b943aaefSJames Feist                                 if (*ptr == "WarningHigh")
623b943aaefSJames Feist                                 {
624b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
625b943aaefSJames Feist                                         "UpperThresholdNonCritical";
626b943aaefSJames Feist                                 }
627b943aaefSJames Feist                                 else if (*ptr == "WarningLow")
628b943aaefSJames Feist                                 {
629b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
630b943aaefSJames Feist                                         "LowerThresholdNonCritical";
631b943aaefSJames Feist                                 }
632b943aaefSJames Feist                                 else if (*ptr == "CriticalHigh")
633b943aaefSJames Feist                                 {
634b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
635b943aaefSJames Feist                                         "UpperThresholdCritical";
636b943aaefSJames Feist                                 }
637b943aaefSJames Feist                                 else if (*ptr == "CriticalLow")
638b943aaefSJames Feist                                 {
639b943aaefSJames Feist                                     (*config)["SetPointOffset"] =
640b943aaefSJames Feist                                         "LowerThresholdCritical";
641b943aaefSJames Feist                                 }
642b943aaefSJames Feist                                 else
643b943aaefSJames Feist                                 {
644b943aaefSJames Feist                                     BMCWEB_LOG_ERROR << "Value Illegal "
645b943aaefSJames Feist                                                      << *ptr;
646b943aaefSJames Feist                                     messages::internalError(asyncResp->res);
647b943aaefSJames Feist                                     return;
648b943aaefSJames Feist                                 }
649b943aaefSJames Feist                             }
650b943aaefSJames Feist                             // doubles
6515b4aa86bSJames Feist                             else if (propertyPair.first ==
6525b4aa86bSJames Feist                                          "FFGainCoefficient" ||
6535b4aa86bSJames Feist                                      propertyPair.first == "FFOffCoefficient" ||
6545b4aa86bSJames Feist                                      propertyPair.first == "ICoefficient" ||
6555b4aa86bSJames Feist                                      propertyPair.first == "ILimitMax" ||
6565b4aa86bSJames Feist                                      propertyPair.first == "ILimitMin" ||
657aad1a257SJames Feist                                      propertyPair.first ==
658aad1a257SJames Feist                                          "PositiveHysteresis" ||
659aad1a257SJames Feist                                      propertyPair.first ==
660aad1a257SJames Feist                                          "NegativeHysteresis" ||
6615b4aa86bSJames Feist                                      propertyPair.first == "OutLimitMax" ||
6625b4aa86bSJames Feist                                      propertyPair.first == "OutLimitMin" ||
6635b4aa86bSJames Feist                                      propertyPair.first == "PCoefficient" ||
6647625cb81SJames Feist                                      propertyPair.first == "SetPoint" ||
6655b4aa86bSJames Feist                                      propertyPair.first == "SlewNeg" ||
6665b4aa86bSJames Feist                                      propertyPair.first == "SlewPos")
6675b4aa86bSJames Feist                             {
6685b4aa86bSJames Feist                                 const double* ptr =
669abf2add6SEd Tanous                                     std::get_if<double>(&propertyPair.second);
6705b4aa86bSJames Feist                                 if (ptr == nullptr)
6715b4aa86bSJames Feist                                 {
6725b4aa86bSJames Feist                                     BMCWEB_LOG_ERROR << "Field Illegal "
6735b4aa86bSJames Feist                                                      << propertyPair.first;
674f12894f8SJason M. Bills                                     messages::internalError(asyncResp->res);
6755b4aa86bSJames Feist                                     return;
6765b4aa86bSJames Feist                                 }
677b7a08d04SJames Feist                                 (*config)[propertyPair.first] = *ptr;
6785b4aa86bSJames Feist                             }
6795b4aa86bSJames Feist                         }
6805b4aa86bSJames Feist                     }
6815b4aa86bSJames Feist                 }
6825b4aa86bSJames Feist             }
6835b4aa86bSJames Feist         },
6845b4aa86bSJames Feist         connection, path, objectManagerIface, "GetManagedObjects");
6855b4aa86bSJames Feist }
686ca537928SJennifer Lee 
68783ff9ab6SJames Feist enum class CreatePIDRet
68883ff9ab6SJames Feist {
68983ff9ab6SJames Feist     fail,
69083ff9ab6SJames Feist     del,
69183ff9ab6SJames Feist     patch
69283ff9ab6SJames Feist };
69383ff9ab6SJames Feist 
69423a21a1cSEd Tanous inline bool getZonesFromJsonReq(const std::shared_ptr<AsyncResp>& response,
6955f2caaefSJames Feist                                 std::vector<nlohmann::json>& config,
6965f2caaefSJames Feist                                 std::vector<std::string>& zones)
6975f2caaefSJames Feist {
698b6baeaa4SJames Feist     if (config.empty())
699b6baeaa4SJames Feist     {
700b6baeaa4SJames Feist         BMCWEB_LOG_ERROR << "Empty Zones";
701b6baeaa4SJames Feist         messages::propertyValueFormatError(response->res,
702b6baeaa4SJames Feist                                            nlohmann::json::array(), "Zones");
703b6baeaa4SJames Feist         return false;
704b6baeaa4SJames Feist     }
7055f2caaefSJames Feist     for (auto& odata : config)
7065f2caaefSJames Feist     {
7075f2caaefSJames Feist         std::string path;
7085f2caaefSJames Feist         if (!redfish::json_util::readJson(odata, response->res, "@odata.id",
7095f2caaefSJames Feist                                           path))
7105f2caaefSJames Feist         {
7115f2caaefSJames Feist             return false;
7125f2caaefSJames Feist         }
7135f2caaefSJames Feist         std::string input;
71461adbda3SJames Feist 
71561adbda3SJames Feist         // 8 below comes from
71661adbda3SJames Feist         // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
71761adbda3SJames Feist         //     0    1     2      3    4    5      6     7      8
71861adbda3SJames Feist         if (!dbus::utility::getNthStringFromPath(path, 8, input))
7195f2caaefSJames Feist         {
7205f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Got invalid path " << path;
7215f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Illegal Type Zones";
7225f2caaefSJames Feist             messages::propertyValueFormatError(response->res, odata.dump(),
7235f2caaefSJames Feist                                                "Zones");
7245f2caaefSJames Feist             return false;
7255f2caaefSJames Feist         }
7265f2caaefSJames Feist         boost::replace_all(input, "_", " ");
7275f2caaefSJames Feist         zones.emplace_back(std::move(input));
7285f2caaefSJames Feist     }
7295f2caaefSJames Feist     return true;
7305f2caaefSJames Feist }
7315f2caaefSJames Feist 
73223a21a1cSEd Tanous inline const dbus::utility::ManagedItem*
73373df0db0SJames Feist     findChassis(const dbus::utility::ManagedObjectType& managedObj,
734b6baeaa4SJames Feist                 const std::string& value, std::string& chassis)
735b6baeaa4SJames Feist {
736b6baeaa4SJames Feist     BMCWEB_LOG_DEBUG << "Find Chassis: " << value << "\n";
737b6baeaa4SJames Feist 
738b6baeaa4SJames Feist     std::string escaped = boost::replace_all_copy(value, " ", "_");
739b6baeaa4SJames Feist     escaped = "/" + escaped;
740b6baeaa4SJames Feist     auto it = std::find_if(
741b6baeaa4SJames Feist         managedObj.begin(), managedObj.end(), [&escaped](const auto& obj) {
742b6baeaa4SJames Feist             if (boost::algorithm::ends_with(obj.first.str, escaped))
743b6baeaa4SJames Feist             {
744b6baeaa4SJames Feist                 BMCWEB_LOG_DEBUG << "Matched " << obj.first.str << "\n";
745b6baeaa4SJames Feist                 return true;
746b6baeaa4SJames Feist             }
747b6baeaa4SJames Feist             return false;
748b6baeaa4SJames Feist         });
749b6baeaa4SJames Feist 
750b6baeaa4SJames Feist     if (it == managedObj.end())
751b6baeaa4SJames Feist     {
75273df0db0SJames Feist         return nullptr;
753b6baeaa4SJames Feist     }
754b6baeaa4SJames Feist     // 5 comes from <chassis-name> being the 5th element
755b6baeaa4SJames Feist     // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
75673df0db0SJames Feist     if (dbus::utility::getNthStringFromPath(it->first.str, 5, chassis))
75773df0db0SJames Feist     {
75873df0db0SJames Feist         return &(*it);
75973df0db0SJames Feist     }
76073df0db0SJames Feist 
76173df0db0SJames Feist     return nullptr;
762b6baeaa4SJames Feist }
763b6baeaa4SJames Feist 
76423a21a1cSEd Tanous inline CreatePIDRet createPidInterface(
76583ff9ab6SJames Feist     const std::shared_ptr<AsyncResp>& response, const std::string& type,
766b5a76932SEd Tanous     const nlohmann::json::iterator& it, const std::string& path,
76783ff9ab6SJames Feist     const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
76883ff9ab6SJames Feist     boost::container::flat_map<std::string, dbus::utility::DbusVariantType>&
76983ff9ab6SJames Feist         output,
77073df0db0SJames Feist     std::string& chassis, const std::string& profile)
77183ff9ab6SJames Feist {
77283ff9ab6SJames Feist 
7735f2caaefSJames Feist     // common deleter
774b6baeaa4SJames Feist     if (it.value() == nullptr)
7755f2caaefSJames Feist     {
7765f2caaefSJames Feist         std::string iface;
7775f2caaefSJames Feist         if (type == "PidControllers" || type == "FanControllers")
7785f2caaefSJames Feist         {
7795f2caaefSJames Feist             iface = pidConfigurationIface;
7805f2caaefSJames Feist         }
7815f2caaefSJames Feist         else if (type == "FanZones")
7825f2caaefSJames Feist         {
7835f2caaefSJames Feist             iface = pidZoneConfigurationIface;
7845f2caaefSJames Feist         }
7855f2caaefSJames Feist         else if (type == "StepwiseControllers")
7865f2caaefSJames Feist         {
7875f2caaefSJames Feist             iface = stepwiseConfigurationIface;
7885f2caaefSJames Feist         }
7895f2caaefSJames Feist         else
7905f2caaefSJames Feist         {
791a0744d38SGunnar Mills             BMCWEB_LOG_ERROR << "Illegal Type " << type;
7925f2caaefSJames Feist             messages::propertyUnknown(response->res, type);
7935f2caaefSJames Feist             return CreatePIDRet::fail;
7945f2caaefSJames Feist         }
7956ee7f774SJames Feist 
7966ee7f774SJames Feist         BMCWEB_LOG_DEBUG << "del " << path << " " << iface << "\n";
7975f2caaefSJames Feist         // delete interface
7985f2caaefSJames Feist         crow::connections::systemBus->async_method_call(
7995f2caaefSJames Feist             [response, path](const boost::system::error_code ec) {
8005f2caaefSJames Feist                 if (ec)
8015f2caaefSJames Feist                 {
8025f2caaefSJames Feist                     BMCWEB_LOG_ERROR << "Error patching " << path << ": " << ec;
8035f2caaefSJames Feist                     messages::internalError(response->res);
804b6baeaa4SJames Feist                     return;
8055f2caaefSJames Feist                 }
806b6baeaa4SJames Feist                 messages::success(response->res);
8075f2caaefSJames Feist             },
8085f2caaefSJames Feist             "xyz.openbmc_project.EntityManager", path, iface, "Delete");
8095f2caaefSJames Feist         return CreatePIDRet::del;
8105f2caaefSJames Feist     }
8115f2caaefSJames Feist 
81273df0db0SJames Feist     const dbus::utility::ManagedItem* managedItem = nullptr;
813b6baeaa4SJames Feist     if (!createNewObject)
814b6baeaa4SJames Feist     {
815b6baeaa4SJames Feist         // if we aren't creating a new object, we should be able to find it on
816b6baeaa4SJames Feist         // d-bus
81773df0db0SJames Feist         managedItem = findChassis(managedObj, it.key(), chassis);
81873df0db0SJames Feist         if (managedItem == nullptr)
819b6baeaa4SJames Feist         {
820b6baeaa4SJames Feist             BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
821b6baeaa4SJames Feist             messages::invalidObject(response->res, it.key());
822b6baeaa4SJames Feist             return CreatePIDRet::fail;
823b6baeaa4SJames Feist         }
824b6baeaa4SJames Feist     }
825b6baeaa4SJames Feist 
82673df0db0SJames Feist     if (profile.size() &&
82773df0db0SJames Feist         (type == "PidControllers" || type == "FanControllers" ||
82873df0db0SJames Feist          type == "StepwiseControllers"))
82973df0db0SJames Feist     {
83073df0db0SJames Feist         if (managedItem == nullptr)
83173df0db0SJames Feist         {
83273df0db0SJames Feist             output["Profiles"] = std::vector<std::string>{profile};
83373df0db0SJames Feist         }
83473df0db0SJames Feist         else
83573df0db0SJames Feist         {
83673df0db0SJames Feist             std::string interface;
83773df0db0SJames Feist             if (type == "StepwiseControllers")
83873df0db0SJames Feist             {
83973df0db0SJames Feist                 interface = stepwiseConfigurationIface;
84073df0db0SJames Feist             }
84173df0db0SJames Feist             else
84273df0db0SJames Feist             {
84373df0db0SJames Feist                 interface = pidConfigurationIface;
84473df0db0SJames Feist             }
84573df0db0SJames Feist             auto findConfig = managedItem->second.find(interface);
84673df0db0SJames Feist             if (findConfig == managedItem->second.end())
84773df0db0SJames Feist             {
84873df0db0SJames Feist                 BMCWEB_LOG_ERROR
84973df0db0SJames Feist                     << "Failed to find interface in managed object";
85073df0db0SJames Feist                 messages::internalError(response->res);
85173df0db0SJames Feist                 return CreatePIDRet::fail;
85273df0db0SJames Feist             }
85373df0db0SJames Feist             auto findProfiles = findConfig->second.find("Profiles");
85473df0db0SJames Feist             if (findProfiles != findConfig->second.end())
85573df0db0SJames Feist             {
85673df0db0SJames Feist                 const std::vector<std::string>* curProfiles =
85773df0db0SJames Feist                     std::get_if<std::vector<std::string>>(
85873df0db0SJames Feist                         &(findProfiles->second));
85973df0db0SJames Feist                 if (curProfiles == nullptr)
86073df0db0SJames Feist                 {
86173df0db0SJames Feist                     BMCWEB_LOG_ERROR << "Illegal profiles in managed object";
86273df0db0SJames Feist                     messages::internalError(response->res);
86373df0db0SJames Feist                     return CreatePIDRet::fail;
86473df0db0SJames Feist                 }
86573df0db0SJames Feist                 if (std::find(curProfiles->begin(), curProfiles->end(),
86673df0db0SJames Feist                               profile) == curProfiles->end())
86773df0db0SJames Feist                 {
86873df0db0SJames Feist                     std::vector<std::string> newProfiles = *curProfiles;
86973df0db0SJames Feist                     newProfiles.push_back(profile);
87073df0db0SJames Feist                     output["Profiles"] = newProfiles;
87173df0db0SJames Feist                 }
87273df0db0SJames Feist             }
87373df0db0SJames Feist         }
87473df0db0SJames Feist     }
87573df0db0SJames Feist 
87683ff9ab6SJames Feist     if (type == "PidControllers" || type == "FanControllers")
87783ff9ab6SJames Feist     {
87883ff9ab6SJames Feist         if (createNewObject)
87983ff9ab6SJames Feist         {
88083ff9ab6SJames Feist             output["Class"] = type == "PidControllers" ? std::string("temp")
88183ff9ab6SJames Feist                                                        : std::string("fan");
88283ff9ab6SJames Feist             output["Type"] = std::string("Pid");
88383ff9ab6SJames Feist         }
8845f2caaefSJames Feist 
8855f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
8865f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
8875f2caaefSJames Feist         std::optional<std::vector<std::string>> outputs;
8885f2caaefSJames Feist         std::map<std::string, std::optional<double>> doubles;
889b943aaefSJames Feist         std::optional<std::string> setpointOffset;
8905f2caaefSJames Feist         if (!redfish::json_util::readJson(
891b6baeaa4SJames Feist                 it.value(), response->res, "Inputs", inputs, "Outputs", outputs,
8925f2caaefSJames Feist                 "Zones", zones, "FFGainCoefficient",
8935f2caaefSJames Feist                 doubles["FFGainCoefficient"], "FFOffCoefficient",
8945f2caaefSJames Feist                 doubles["FFOffCoefficient"], "ICoefficient",
8955f2caaefSJames Feist                 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
8965f2caaefSJames Feist                 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
8975f2caaefSJames Feist                 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
8985f2caaefSJames Feist                 "PCoefficient", doubles["PCoefficient"], "SetPoint",
899b943aaefSJames Feist                 doubles["SetPoint"], "SetPointOffset", setpointOffset,
900b943aaefSJames Feist                 "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"],
901b943aaefSJames Feist                 "PositiveHysteresis", doubles["PositiveHysteresis"],
902b943aaefSJames Feist                 "NegativeHysteresis", doubles["NegativeHysteresis"]))
90383ff9ab6SJames Feist         {
904*71f52d96SEd Tanous             BMCWEB_LOG_ERROR
905*71f52d96SEd Tanous                 << "Illegal Property "
906*71f52d96SEd Tanous                 << it.value().dump(2, ' ', true,
907*71f52d96SEd Tanous                                    nlohmann::json::error_handler_t::replace);
9085f2caaefSJames Feist             return CreatePIDRet::fail;
90983ff9ab6SJames Feist         }
9105f2caaefSJames Feist         if (zones)
9115f2caaefSJames Feist         {
9125f2caaefSJames Feist             std::vector<std::string> zonesStr;
9135f2caaefSJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStr))
9145f2caaefSJames Feist             {
915a0744d38SGunnar Mills                 BMCWEB_LOG_ERROR << "Illegal Zones";
9165f2caaefSJames Feist                 return CreatePIDRet::fail;
9175f2caaefSJames Feist             }
918b6baeaa4SJames Feist             if (chassis.empty() &&
919b6baeaa4SJames Feist                 !findChassis(managedObj, zonesStr[0], chassis))
920b6baeaa4SJames Feist             {
921b6baeaa4SJames Feist                 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
922b6baeaa4SJames Feist                 messages::invalidObject(response->res, it.key());
923b6baeaa4SJames Feist                 return CreatePIDRet::fail;
924b6baeaa4SJames Feist             }
925b6baeaa4SJames Feist 
9265f2caaefSJames Feist             output["Zones"] = std::move(zonesStr);
9275f2caaefSJames Feist         }
9285f2caaefSJames Feist         if (inputs || outputs)
9295f2caaefSJames Feist         {
9305f2caaefSJames Feist             std::array<std::optional<std::vector<std::string>>*, 2> containers =
9315f2caaefSJames Feist                 {&inputs, &outputs};
9325f2caaefSJames Feist             size_t index = 0;
9335f2caaefSJames Feist             for (const auto& containerPtr : containers)
9345f2caaefSJames Feist             {
9355f2caaefSJames Feist                 std::optional<std::vector<std::string>>& container =
9365f2caaefSJames Feist                     *containerPtr;
9375f2caaefSJames Feist                 if (!container)
9385f2caaefSJames Feist                 {
9395f2caaefSJames Feist                     index++;
9405f2caaefSJames Feist                     continue;
94183ff9ab6SJames Feist                 }
94283ff9ab6SJames Feist 
9435f2caaefSJames Feist                 for (std::string& value : *container)
94483ff9ab6SJames Feist                 {
9455f2caaefSJames Feist                     boost::replace_all(value, "_", " ");
94683ff9ab6SJames Feist                 }
9475f2caaefSJames Feist                 std::string key;
9485f2caaefSJames Feist                 if (index == 0)
9495f2caaefSJames Feist                 {
9505f2caaefSJames Feist                     key = "Inputs";
9515f2caaefSJames Feist                 }
9525f2caaefSJames Feist                 else
9535f2caaefSJames Feist                 {
9545f2caaefSJames Feist                     key = "Outputs";
9555f2caaefSJames Feist                 }
9565f2caaefSJames Feist                 output[key] = *container;
9575f2caaefSJames Feist                 index++;
9585f2caaefSJames Feist             }
95983ff9ab6SJames Feist         }
96083ff9ab6SJames Feist 
961b943aaefSJames Feist         if (setpointOffset)
962b943aaefSJames Feist         {
963b943aaefSJames Feist             // translate between redfish and dbus names
964b943aaefSJames Feist             if (*setpointOffset == "UpperThresholdNonCritical")
965b943aaefSJames Feist             {
966b943aaefSJames Feist                 output["SetPointOffset"] = std::string("WarningLow");
967b943aaefSJames Feist             }
968b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdNonCritical")
969b943aaefSJames Feist             {
970b943aaefSJames Feist                 output["SetPointOffset"] = std::string("WarningHigh");
971b943aaefSJames Feist             }
972b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdCritical")
973b943aaefSJames Feist             {
974b943aaefSJames Feist                 output["SetPointOffset"] = std::string("CriticalLow");
975b943aaefSJames Feist             }
976b943aaefSJames Feist             else if (*setpointOffset == "UpperThresholdCritical")
977b943aaefSJames Feist             {
978b943aaefSJames Feist                 output["SetPointOffset"] = std::string("CriticalHigh");
979b943aaefSJames Feist             }
980b943aaefSJames Feist             else
981b943aaefSJames Feist             {
982b943aaefSJames Feist                 BMCWEB_LOG_ERROR << "Invalid setpointoffset "
983b943aaefSJames Feist                                  << *setpointOffset;
984b943aaefSJames Feist                 messages::invalidObject(response->res, it.key());
985b943aaefSJames Feist                 return CreatePIDRet::fail;
986b943aaefSJames Feist             }
987b943aaefSJames Feist         }
988b943aaefSJames Feist 
98983ff9ab6SJames Feist         // doubles
9905f2caaefSJames Feist         for (const auto& pairs : doubles)
99183ff9ab6SJames Feist         {
9925f2caaefSJames Feist             if (!pairs.second)
99383ff9ab6SJames Feist             {
9945f2caaefSJames Feist                 continue;
99583ff9ab6SJames Feist             }
9965f2caaefSJames Feist             BMCWEB_LOG_DEBUG << pairs.first << " = " << *pairs.second;
9975f2caaefSJames Feist             output[pairs.first] = *(pairs.second);
9985f2caaefSJames Feist         }
99983ff9ab6SJames Feist     }
100083ff9ab6SJames Feist 
100183ff9ab6SJames Feist     else if (type == "FanZones")
100283ff9ab6SJames Feist     {
100383ff9ab6SJames Feist         output["Type"] = std::string("Pid.Zone");
100483ff9ab6SJames Feist 
10055f2caaefSJames Feist         std::optional<nlohmann::json> chassisContainer;
10065f2caaefSJames Feist         std::optional<double> failSafePercent;
1007d3ec07f8SJames Feist         std::optional<double> minThermalOutput;
1008b6baeaa4SJames Feist         if (!redfish::json_util::readJson(it.value(), response->res, "Chassis",
10095f2caaefSJames Feist                                           chassisContainer, "FailSafePercent",
1010d3ec07f8SJames Feist                                           failSafePercent, "MinThermalOutput",
1011d3ec07f8SJames Feist                                           minThermalOutput))
101283ff9ab6SJames Feist         {
1013*71f52d96SEd Tanous             BMCWEB_LOG_ERROR
1014*71f52d96SEd Tanous                 << "Illegal Property "
1015*71f52d96SEd Tanous                 << it.value().dump(2, ' ', true,
1016*71f52d96SEd Tanous                                    nlohmann::json::error_handler_t::replace);
101783ff9ab6SJames Feist             return CreatePIDRet::fail;
101883ff9ab6SJames Feist         }
10195f2caaefSJames Feist 
10205f2caaefSJames Feist         if (chassisContainer)
102183ff9ab6SJames Feist         {
10225f2caaefSJames Feist 
10235f2caaefSJames Feist             std::string chassisId;
10245f2caaefSJames Feist             if (!redfish::json_util::readJson(*chassisContainer, response->res,
10255f2caaefSJames Feist                                               "@odata.id", chassisId))
10265f2caaefSJames Feist             {
1027*71f52d96SEd Tanous                 BMCWEB_LOG_ERROR
1028*71f52d96SEd Tanous                     << "Illegal Property "
1029*71f52d96SEd Tanous                     << chassisContainer->dump(
1030*71f52d96SEd Tanous                            2, ' ', true,
1031*71f52d96SEd Tanous                            nlohmann::json::error_handler_t::replace);
103283ff9ab6SJames Feist                 return CreatePIDRet::fail;
103383ff9ab6SJames Feist             }
103483ff9ab6SJames Feist 
1035717794d5SAppaRao Puli             // /redfish/v1/chassis/chassis_name/
10365f2caaefSJames Feist             if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis))
103783ff9ab6SJames Feist             {
10385f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Got invalid path " << chassisId;
10395f2caaefSJames Feist                 messages::invalidObject(response->res, chassisId);
104083ff9ab6SJames Feist                 return CreatePIDRet::fail;
104183ff9ab6SJames Feist             }
104283ff9ab6SJames Feist         }
1043d3ec07f8SJames Feist         if (minThermalOutput)
104483ff9ab6SJames Feist         {
1045d3ec07f8SJames Feist             output["MinThermalOutput"] = *minThermalOutput;
10465f2caaefSJames Feist         }
10475f2caaefSJames Feist         if (failSafePercent)
104883ff9ab6SJames Feist         {
10495f2caaefSJames Feist             output["FailSafePercent"] = *failSafePercent;
10505f2caaefSJames Feist         }
10515f2caaefSJames Feist     }
10525f2caaefSJames Feist     else if (type == "StepwiseControllers")
10535f2caaefSJames Feist     {
10545f2caaefSJames Feist         output["Type"] = std::string("Stepwise");
10555f2caaefSJames Feist 
10565f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
10575f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> steps;
10585f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
10595f2caaefSJames Feist         std::optional<double> positiveHysteresis;
10605f2caaefSJames Feist         std::optional<double> negativeHysteresis;
1061c33a90ecSJames Feist         std::optional<std::string> direction; // upper clipping curve vs lower
10625f2caaefSJames Feist         if (!redfish::json_util::readJson(
1063b6baeaa4SJames Feist                 it.value(), response->res, "Zones", zones, "Steps", steps,
1064b6baeaa4SJames Feist                 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
1065c33a90ecSJames Feist                 "NegativeHysteresis", negativeHysteresis, "Direction",
1066c33a90ecSJames Feist                 direction))
10675f2caaefSJames Feist         {
1068*71f52d96SEd Tanous             BMCWEB_LOG_ERROR
1069*71f52d96SEd Tanous                 << "Illegal Property "
1070*71f52d96SEd Tanous                 << it.value().dump(2, ' ', true,
1071*71f52d96SEd Tanous                                    nlohmann::json::error_handler_t::replace);
107283ff9ab6SJames Feist             return CreatePIDRet::fail;
107383ff9ab6SJames Feist         }
10745f2caaefSJames Feist 
10755f2caaefSJames Feist         if (zones)
107683ff9ab6SJames Feist         {
1077b6baeaa4SJames Feist             std::vector<std::string> zonesStrs;
1078b6baeaa4SJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStrs))
10795f2caaefSJames Feist             {
1080a0744d38SGunnar Mills                 BMCWEB_LOG_ERROR << "Illegal Zones";
108183ff9ab6SJames Feist                 return CreatePIDRet::fail;
108283ff9ab6SJames Feist             }
1083b6baeaa4SJames Feist             if (chassis.empty() &&
1084b6baeaa4SJames Feist                 !findChassis(managedObj, zonesStrs[0], chassis))
1085b6baeaa4SJames Feist             {
1086b6baeaa4SJames Feist                 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
1087b6baeaa4SJames Feist                 messages::invalidObject(response->res, it.key());
1088b6baeaa4SJames Feist                 return CreatePIDRet::fail;
1089b6baeaa4SJames Feist             }
1090b6baeaa4SJames Feist             output["Zones"] = std::move(zonesStrs);
10915f2caaefSJames Feist         }
10925f2caaefSJames Feist         if (steps)
10935f2caaefSJames Feist         {
10945f2caaefSJames Feist             std::vector<double> readings;
10955f2caaefSJames Feist             std::vector<double> outputs;
10965f2caaefSJames Feist             for (auto& step : *steps)
10975f2caaefSJames Feist             {
10985f2caaefSJames Feist                 double target;
109923a21a1cSEd Tanous                 double out;
11005f2caaefSJames Feist 
11015f2caaefSJames Feist                 if (!redfish::json_util::readJson(step, response->res, "Target",
110223a21a1cSEd Tanous                                                   target, "Output", out))
11035f2caaefSJames Feist                 {
1104*71f52d96SEd Tanous                     BMCWEB_LOG_ERROR
1105*71f52d96SEd Tanous                         << "Illegal Property "
1106*71f52d96SEd Tanous                         << it.value().dump(
1107*71f52d96SEd Tanous                                2, ' ', true,
1108*71f52d96SEd Tanous                                nlohmann::json::error_handler_t::replace);
11095f2caaefSJames Feist                     return CreatePIDRet::fail;
11105f2caaefSJames Feist                 }
11115f2caaefSJames Feist                 readings.emplace_back(target);
111223a21a1cSEd Tanous                 outputs.emplace_back(out);
11135f2caaefSJames Feist             }
11145f2caaefSJames Feist             output["Reading"] = std::move(readings);
11155f2caaefSJames Feist             output["Output"] = std::move(outputs);
11165f2caaefSJames Feist         }
11175f2caaefSJames Feist         if (inputs)
11185f2caaefSJames Feist         {
11195f2caaefSJames Feist             for (std::string& value : *inputs)
11205f2caaefSJames Feist             {
11215f2caaefSJames Feist                 boost::replace_all(value, "_", " ");
11225f2caaefSJames Feist             }
11235f2caaefSJames Feist             output["Inputs"] = std::move(*inputs);
11245f2caaefSJames Feist         }
11255f2caaefSJames Feist         if (negativeHysteresis)
11265f2caaefSJames Feist         {
11275f2caaefSJames Feist             output["NegativeHysteresis"] = *negativeHysteresis;
11285f2caaefSJames Feist         }
11295f2caaefSJames Feist         if (positiveHysteresis)
11305f2caaefSJames Feist         {
11315f2caaefSJames Feist             output["PositiveHysteresis"] = *positiveHysteresis;
113283ff9ab6SJames Feist         }
1133c33a90ecSJames Feist         if (direction)
1134c33a90ecSJames Feist         {
1135c33a90ecSJames Feist             constexpr const std::array<const char*, 2> allowedDirections = {
1136c33a90ecSJames Feist                 "Ceiling", "Floor"};
1137c33a90ecSJames Feist             if (std::find(allowedDirections.begin(), allowedDirections.end(),
1138c33a90ecSJames Feist                           *direction) == allowedDirections.end())
1139c33a90ecSJames Feist             {
1140c33a90ecSJames Feist                 messages::propertyValueTypeError(response->res, "Direction",
1141c33a90ecSJames Feist                                                  *direction);
1142c33a90ecSJames Feist                 return CreatePIDRet::fail;
1143c33a90ecSJames Feist             }
1144c33a90ecSJames Feist             output["Class"] = *direction;
1145c33a90ecSJames Feist         }
114683ff9ab6SJames Feist     }
114783ff9ab6SJames Feist     else
114883ff9ab6SJames Feist     {
1149a0744d38SGunnar Mills         BMCWEB_LOG_ERROR << "Illegal Type " << type;
115035a62c7cSJason M. Bills         messages::propertyUnknown(response->res, type);
115183ff9ab6SJames Feist         return CreatePIDRet::fail;
115283ff9ab6SJames Feist     }
115383ff9ab6SJames Feist     return CreatePIDRet::patch;
115483ff9ab6SJames Feist }
115573df0db0SJames Feist struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
115673df0db0SJames Feist {
115783ff9ab6SJames Feist 
115823a21a1cSEd Tanous     GetPIDValues(const std::shared_ptr<AsyncResp>& asyncRespIn) :
115923a21a1cSEd Tanous         asyncResp(asyncRespIn)
116073df0db0SJames Feist 
11611214b7e7SGunnar Mills     {}
11629c310685SBorawski.Lukasz 
116373df0db0SJames Feist     void run()
11645b4aa86bSJames Feist     {
116573df0db0SJames Feist         std::shared_ptr<GetPIDValues> self = shared_from_this();
116673df0db0SJames Feist 
116773df0db0SJames Feist         // get all configurations
11685b4aa86bSJames Feist         crow::connections::systemBus->async_method_call(
116973df0db0SJames Feist             [self](const boost::system::error_code ec,
117023a21a1cSEd Tanous                    const crow::openbmc_mapper::GetSubTreeType& subtreeLocal) {
11715b4aa86bSJames Feist                 if (ec)
11725b4aa86bSJames Feist                 {
11735b4aa86bSJames Feist                     BMCWEB_LOG_ERROR << ec;
117473df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
117573df0db0SJames Feist                     return;
117673df0db0SJames Feist                 }
117723a21a1cSEd Tanous                 self->subtree = subtreeLocal;
117873df0db0SJames Feist             },
117973df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper",
118073df0db0SJames Feist             "/xyz/openbmc_project/object_mapper",
118173df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
118273df0db0SJames Feist             std::array<const char*, 4>{
118373df0db0SJames Feist                 pidConfigurationIface, pidZoneConfigurationIface,
118473df0db0SJames Feist                 objectManagerIface, stepwiseConfigurationIface});
118573df0db0SJames Feist 
118673df0db0SJames Feist         // at the same time get the selected profile
118773df0db0SJames Feist         crow::connections::systemBus->async_method_call(
118873df0db0SJames Feist             [self](const boost::system::error_code ec,
118923a21a1cSEd Tanous                    const crow::openbmc_mapper::GetSubTreeType& subtreeLocal) {
119023a21a1cSEd Tanous                 if (ec || subtreeLocal.empty())
119173df0db0SJames Feist                 {
119273df0db0SJames Feist                     return;
119373df0db0SJames Feist                 }
119423a21a1cSEd Tanous                 if (subtreeLocal[0].second.size() != 1)
119573df0db0SJames Feist                 {
119673df0db0SJames Feist                     // invalid mapper response, should never happen
119773df0db0SJames Feist                     BMCWEB_LOG_ERROR << "GetPIDValues: Mapper Error";
119873df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
11995b4aa86bSJames Feist                     return;
12005b4aa86bSJames Feist                 }
12015b4aa86bSJames Feist 
120223a21a1cSEd Tanous                 const std::string& path = subtreeLocal[0].first;
120323a21a1cSEd Tanous                 const std::string& owner = subtreeLocal[0].second[0].first;
120473df0db0SJames Feist                 crow::connections::systemBus->async_method_call(
120573df0db0SJames Feist                     [path, owner, self](
120623a21a1cSEd Tanous                         const boost::system::error_code ec2,
120773df0db0SJames Feist                         const boost::container::flat_map<
120873df0db0SJames Feist                             std::string, std::variant<std::vector<std::string>,
120973df0db0SJames Feist                                                       std::string>>& resp) {
121023a21a1cSEd Tanous                         if (ec2)
121173df0db0SJames Feist                         {
121273df0db0SJames Feist                             BMCWEB_LOG_ERROR << "GetPIDValues: Can't get "
121373df0db0SJames Feist                                                 "thermalModeIface "
121473df0db0SJames Feist                                              << path;
121573df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
121673df0db0SJames Feist                             return;
121773df0db0SJames Feist                         }
1218271584abSEd Tanous                         const std::string* current = nullptr;
1219271584abSEd Tanous                         const std::vector<std::string>* supported = nullptr;
122073df0db0SJames Feist                         for (auto& [key, value] : resp)
122173df0db0SJames Feist                         {
122273df0db0SJames Feist                             if (key == "Current")
122373df0db0SJames Feist                             {
122473df0db0SJames Feist                                 current = std::get_if<std::string>(&value);
122573df0db0SJames Feist                                 if (current == nullptr)
122673df0db0SJames Feist                                 {
122773df0db0SJames Feist                                     BMCWEB_LOG_ERROR
122873df0db0SJames Feist                                         << "GetPIDValues: thermal mode "
122973df0db0SJames Feist                                            "iface invalid "
123073df0db0SJames Feist                                         << path;
123173df0db0SJames Feist                                     messages::internalError(
123273df0db0SJames Feist                                         self->asyncResp->res);
123373df0db0SJames Feist                                     return;
123473df0db0SJames Feist                                 }
123573df0db0SJames Feist                             }
123673df0db0SJames Feist                             if (key == "Supported")
123773df0db0SJames Feist                             {
123873df0db0SJames Feist                                 supported =
123973df0db0SJames Feist                                     std::get_if<std::vector<std::string>>(
124073df0db0SJames Feist                                         &value);
124173df0db0SJames Feist                                 if (supported == nullptr)
124273df0db0SJames Feist                                 {
124373df0db0SJames Feist                                     BMCWEB_LOG_ERROR
124473df0db0SJames Feist                                         << "GetPIDValues: thermal mode "
124573df0db0SJames Feist                                            "iface invalid"
124673df0db0SJames Feist                                         << path;
124773df0db0SJames Feist                                     messages::internalError(
124873df0db0SJames Feist                                         self->asyncResp->res);
124973df0db0SJames Feist                                     return;
125073df0db0SJames Feist                                 }
125173df0db0SJames Feist                             }
125273df0db0SJames Feist                         }
125373df0db0SJames Feist                         if (current == nullptr || supported == nullptr)
125473df0db0SJames Feist                         {
125573df0db0SJames Feist                             BMCWEB_LOG_ERROR << "GetPIDValues: thermal mode "
125673df0db0SJames Feist                                                 "iface invalid "
125773df0db0SJames Feist                                              << path;
125873df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
125973df0db0SJames Feist                             return;
126073df0db0SJames Feist                         }
126173df0db0SJames Feist                         self->currentProfile = *current;
126273df0db0SJames Feist                         self->supportedProfiles = *supported;
126373df0db0SJames Feist                     },
126473df0db0SJames Feist                     owner, path, "org.freedesktop.DBus.Properties", "GetAll",
126573df0db0SJames Feist                     thermalModeIface);
126673df0db0SJames Feist             },
126773df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper",
126873df0db0SJames Feist             "/xyz/openbmc_project/object_mapper",
126973df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
127073df0db0SJames Feist             std::array<const char*, 1>{thermalModeIface});
127173df0db0SJames Feist     }
127273df0db0SJames Feist 
127373df0db0SJames Feist     ~GetPIDValues()
127473df0db0SJames Feist     {
127573df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
127673df0db0SJames Feist         {
127773df0db0SJames Feist             return;
127873df0db0SJames Feist         }
12795b4aa86bSJames Feist         // create map of <connection, path to objMgr>>
128073df0db0SJames Feist         boost::container::flat_map<std::string, std::string> objectMgrPaths;
12816bce33bcSJames Feist         boost::container::flat_set<std::string> calledConnections;
12825b4aa86bSJames Feist         for (const auto& pathGroup : subtree)
12835b4aa86bSJames Feist         {
12845b4aa86bSJames Feist             for (const auto& connectionGroup : pathGroup.second)
12855b4aa86bSJames Feist             {
12866bce33bcSJames Feist                 auto findConnection =
12876bce33bcSJames Feist                     calledConnections.find(connectionGroup.first);
12886bce33bcSJames Feist                 if (findConnection != calledConnections.end())
12896bce33bcSJames Feist                 {
12906bce33bcSJames Feist                     break;
12916bce33bcSJames Feist                 }
129273df0db0SJames Feist                 for (const std::string& interface : connectionGroup.second)
12935b4aa86bSJames Feist                 {
12945b4aa86bSJames Feist                     if (interface == objectManagerIface)
12955b4aa86bSJames Feist                     {
129673df0db0SJames Feist                         objectMgrPaths[connectionGroup.first] = pathGroup.first;
12975b4aa86bSJames Feist                     }
12985b4aa86bSJames Feist                     // this list is alphabetical, so we
12995b4aa86bSJames Feist                     // should have found the objMgr by now
13005b4aa86bSJames Feist                     if (interface == pidConfigurationIface ||
1301b7a08d04SJames Feist                         interface == pidZoneConfigurationIface ||
1302b7a08d04SJames Feist                         interface == stepwiseConfigurationIface)
13035b4aa86bSJames Feist                     {
13045b4aa86bSJames Feist                         auto findObjMgr =
13055b4aa86bSJames Feist                             objectMgrPaths.find(connectionGroup.first);
13065b4aa86bSJames Feist                         if (findObjMgr == objectMgrPaths.end())
13075b4aa86bSJames Feist                         {
13085b4aa86bSJames Feist                             BMCWEB_LOG_DEBUG << connectionGroup.first
13095b4aa86bSJames Feist                                              << "Has no Object Manager";
13105b4aa86bSJames Feist                             continue;
13115b4aa86bSJames Feist                         }
13126bce33bcSJames Feist 
13136bce33bcSJames Feist                         calledConnections.insert(connectionGroup.first);
13146bce33bcSJames Feist 
131573df0db0SJames Feist                         asyncPopulatePid(findObjMgr->first, findObjMgr->second,
131673df0db0SJames Feist                                          currentProfile, supportedProfiles,
131773df0db0SJames Feist                                          asyncResp);
13185b4aa86bSJames Feist                         break;
13195b4aa86bSJames Feist                     }
13205b4aa86bSJames Feist                 }
13215b4aa86bSJames Feist             }
13225b4aa86bSJames Feist         }
132373df0db0SJames Feist     }
132473df0db0SJames Feist 
132573df0db0SJames Feist     std::vector<std::string> supportedProfiles;
132673df0db0SJames Feist     std::string currentProfile;
132773df0db0SJames Feist     crow::openbmc_mapper::GetSubTreeType subtree;
132873df0db0SJames Feist     std::shared_ptr<AsyncResp> asyncResp;
132973df0db0SJames Feist };
133073df0db0SJames Feist 
133173df0db0SJames Feist struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
133273df0db0SJames Feist {
133373df0db0SJames Feist 
1334271584abSEd Tanous     SetPIDValues(const std::shared_ptr<AsyncResp>& asyncRespIn,
133573df0db0SJames Feist                  nlohmann::json& data) :
1336271584abSEd Tanous         asyncResp(asyncRespIn)
133773df0db0SJames Feist     {
133873df0db0SJames Feist 
133973df0db0SJames Feist         std::optional<nlohmann::json> pidControllers;
134073df0db0SJames Feist         std::optional<nlohmann::json> fanControllers;
134173df0db0SJames Feist         std::optional<nlohmann::json> fanZones;
134273df0db0SJames Feist         std::optional<nlohmann::json> stepwiseControllers;
134373df0db0SJames Feist 
134473df0db0SJames Feist         if (!redfish::json_util::readJson(
134573df0db0SJames Feist                 data, asyncResp->res, "PidControllers", pidControllers,
134673df0db0SJames Feist                 "FanControllers", fanControllers, "FanZones", fanZones,
134773df0db0SJames Feist                 "StepwiseControllers", stepwiseControllers, "Profile", profile))
134873df0db0SJames Feist         {
1349*71f52d96SEd Tanous             BMCWEB_LOG_ERROR
1350*71f52d96SEd Tanous                 << "Illegal Property "
1351*71f52d96SEd Tanous                 << data.dump(2, ' ', true,
1352*71f52d96SEd Tanous                              nlohmann::json::error_handler_t::replace);
135373df0db0SJames Feist             return;
135473df0db0SJames Feist         }
135573df0db0SJames Feist         configuration.emplace_back("PidControllers", std::move(pidControllers));
135673df0db0SJames Feist         configuration.emplace_back("FanControllers", std::move(fanControllers));
135773df0db0SJames Feist         configuration.emplace_back("FanZones", std::move(fanZones));
135873df0db0SJames Feist         configuration.emplace_back("StepwiseControllers",
135973df0db0SJames Feist                                    std::move(stepwiseControllers));
136073df0db0SJames Feist     }
136173df0db0SJames Feist     void run()
136273df0db0SJames Feist     {
136373df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
136473df0db0SJames Feist         {
136573df0db0SJames Feist             return;
136673df0db0SJames Feist         }
136773df0db0SJames Feist 
136873df0db0SJames Feist         std::shared_ptr<SetPIDValues> self = shared_from_this();
136973df0db0SJames Feist 
137073df0db0SJames Feist         // todo(james): might make sense to do a mapper call here if this
137173df0db0SJames Feist         // interface gets more traction
137273df0db0SJames Feist         crow::connections::systemBus->async_method_call(
137373df0db0SJames Feist             [self](const boost::system::error_code ec,
1374271584abSEd Tanous                    dbus::utility::ManagedObjectType& mObj) {
137573df0db0SJames Feist                 if (ec)
137673df0db0SJames Feist                 {
137773df0db0SJames Feist                     BMCWEB_LOG_ERROR << "Error communicating to Entity Manager";
137873df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
137973df0db0SJames Feist                     return;
138073df0db0SJames Feist                 }
1381e69d9de2SJames Feist                 const std::array<const char*, 3> configurations = {
1382e69d9de2SJames Feist                     pidConfigurationIface, pidZoneConfigurationIface,
1383e69d9de2SJames Feist                     stepwiseConfigurationIface};
1384e69d9de2SJames Feist 
138514b0b8d5SJames Feist                 for (const auto& [path, object] : mObj)
1386e69d9de2SJames Feist                 {
138714b0b8d5SJames Feist                     for (const auto& [interface, _] : object)
1388e69d9de2SJames Feist                     {
1389e69d9de2SJames Feist                         if (std::find(configurations.begin(),
1390e69d9de2SJames Feist                                       configurations.end(),
1391e69d9de2SJames Feist                                       interface) != configurations.end())
1392e69d9de2SJames Feist                         {
139314b0b8d5SJames Feist                             self->objectCount++;
1394e69d9de2SJames Feist                             break;
1395e69d9de2SJames Feist                         }
1396e69d9de2SJames Feist                     }
1397e69d9de2SJames Feist                 }
1398271584abSEd Tanous                 self->managedObj = std::move(mObj);
139973df0db0SJames Feist             },
140073df0db0SJames Feist             "xyz.openbmc_project.EntityManager", "/", objectManagerIface,
140173df0db0SJames Feist             "GetManagedObjects");
140273df0db0SJames Feist 
140373df0db0SJames Feist         // at the same time get the profile information
140473df0db0SJames Feist         crow::connections::systemBus->async_method_call(
140573df0db0SJames Feist             [self](const boost::system::error_code ec,
140673df0db0SJames Feist                    const crow::openbmc_mapper::GetSubTreeType& subtree) {
140773df0db0SJames Feist                 if (ec || subtree.empty())
140873df0db0SJames Feist                 {
140973df0db0SJames Feist                     return;
141073df0db0SJames Feist                 }
141173df0db0SJames Feist                 if (subtree[0].second.empty())
141273df0db0SJames Feist                 {
141373df0db0SJames Feist                     // invalid mapper response, should never happen
141473df0db0SJames Feist                     BMCWEB_LOG_ERROR << "SetPIDValues: Mapper Error";
141573df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
141673df0db0SJames Feist                     return;
141773df0db0SJames Feist                 }
141873df0db0SJames Feist 
141973df0db0SJames Feist                 const std::string& path = subtree[0].first;
142073df0db0SJames Feist                 const std::string& owner = subtree[0].second[0].first;
142173df0db0SJames Feist                 crow::connections::systemBus->async_method_call(
142273df0db0SJames Feist                     [self, path, owner](
1423cb13a392SEd Tanous                         const boost::system::error_code ec2,
142473df0db0SJames Feist                         const boost::container::flat_map<
142573df0db0SJames Feist                             std::string, std::variant<std::vector<std::string>,
1426271584abSEd Tanous                                                       std::string>>& r) {
1427cb13a392SEd Tanous                         if (ec2)
142873df0db0SJames Feist                         {
142973df0db0SJames Feist                             BMCWEB_LOG_ERROR << "SetPIDValues: Can't get "
143073df0db0SJames Feist                                                 "thermalModeIface "
143173df0db0SJames Feist                                              << path;
143273df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
143373df0db0SJames Feist                             return;
143473df0db0SJames Feist                         }
1435271584abSEd Tanous                         const std::string* current = nullptr;
1436271584abSEd Tanous                         const std::vector<std::string>* supported = nullptr;
1437271584abSEd Tanous                         for (auto& [key, value] : r)
143873df0db0SJames Feist                         {
143973df0db0SJames Feist                             if (key == "Current")
144073df0db0SJames Feist                             {
144173df0db0SJames Feist                                 current = std::get_if<std::string>(&value);
144273df0db0SJames Feist                                 if (current == nullptr)
144373df0db0SJames Feist                                 {
144473df0db0SJames Feist                                     BMCWEB_LOG_ERROR
144573df0db0SJames Feist                                         << "SetPIDValues: thermal mode "
144673df0db0SJames Feist                                            "iface invalid "
144773df0db0SJames Feist                                         << path;
144873df0db0SJames Feist                                     messages::internalError(
144973df0db0SJames Feist                                         self->asyncResp->res);
145073df0db0SJames Feist                                     return;
145173df0db0SJames Feist                                 }
145273df0db0SJames Feist                             }
145373df0db0SJames Feist                             if (key == "Supported")
145473df0db0SJames Feist                             {
145573df0db0SJames Feist                                 supported =
145673df0db0SJames Feist                                     std::get_if<std::vector<std::string>>(
145773df0db0SJames Feist                                         &value);
145873df0db0SJames Feist                                 if (supported == nullptr)
145973df0db0SJames Feist                                 {
146073df0db0SJames Feist                                     BMCWEB_LOG_ERROR
146173df0db0SJames Feist                                         << "SetPIDValues: thermal mode "
146273df0db0SJames Feist                                            "iface invalid"
146373df0db0SJames Feist                                         << path;
146473df0db0SJames Feist                                     messages::internalError(
146573df0db0SJames Feist                                         self->asyncResp->res);
146673df0db0SJames Feist                                     return;
146773df0db0SJames Feist                                 }
146873df0db0SJames Feist                             }
146973df0db0SJames Feist                         }
147073df0db0SJames Feist                         if (current == nullptr || supported == nullptr)
147173df0db0SJames Feist                         {
147273df0db0SJames Feist                             BMCWEB_LOG_ERROR << "SetPIDValues: thermal mode "
147373df0db0SJames Feist                                                 "iface invalid "
147473df0db0SJames Feist                                              << path;
147573df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
147673df0db0SJames Feist                             return;
147773df0db0SJames Feist                         }
147873df0db0SJames Feist                         self->currentProfile = *current;
147973df0db0SJames Feist                         self->supportedProfiles = *supported;
148073df0db0SJames Feist                         self->profileConnection = owner;
148173df0db0SJames Feist                         self->profilePath = path;
148273df0db0SJames Feist                     },
148373df0db0SJames Feist                     owner, path, "org.freedesktop.DBus.Properties", "GetAll",
148473df0db0SJames Feist                     thermalModeIface);
14855b4aa86bSJames Feist             },
14865b4aa86bSJames Feist             "xyz.openbmc_project.ObjectMapper",
14875b4aa86bSJames Feist             "/xyz/openbmc_project/object_mapper",
14885b4aa86bSJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
148973df0db0SJames Feist             std::array<const char*, 1>{thermalModeIface});
149073df0db0SJames Feist     }
149173df0db0SJames Feist     ~SetPIDValues()
149273df0db0SJames Feist     {
149373df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
149473df0db0SJames Feist         {
149573df0db0SJames Feist             return;
14965b4aa86bSJames Feist         }
14975b4aa86bSJames Feist 
149873df0db0SJames Feist         std::shared_ptr<AsyncResp> response = asyncResp;
149973df0db0SJames Feist 
150073df0db0SJames Feist         if (profile)
150173df0db0SJames Feist         {
150273df0db0SJames Feist             if (std::find(supportedProfiles.begin(), supportedProfiles.end(),
150373df0db0SJames Feist                           *profile) == supportedProfiles.end())
150473df0db0SJames Feist             {
150573df0db0SJames Feist                 messages::actionParameterUnknown(response->res, "Profile",
150673df0db0SJames Feist                                                  *profile);
150773df0db0SJames Feist                 return;
150873df0db0SJames Feist             }
150973df0db0SJames Feist             currentProfile = *profile;
151073df0db0SJames Feist             crow::connections::systemBus->async_method_call(
151173df0db0SJames Feist                 [response](const boost::system::error_code ec) {
151273df0db0SJames Feist                     if (ec)
151373df0db0SJames Feist                     {
151473df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Error patching profile" << ec;
151573df0db0SJames Feist                         messages::internalError(response->res);
151673df0db0SJames Feist                     }
151773df0db0SJames Feist                 },
151873df0db0SJames Feist                 profileConnection, profilePath,
151973df0db0SJames Feist                 "org.freedesktop.DBus.Properties", "Set", thermalModeIface,
152073df0db0SJames Feist                 "Current", std::variant<std::string>(*profile));
152173df0db0SJames Feist         }
152273df0db0SJames Feist 
152373df0db0SJames Feist         for (auto& containerPair : configuration)
152473df0db0SJames Feist         {
152573df0db0SJames Feist             auto& container = containerPair.second;
152673df0db0SJames Feist             if (!container)
152773df0db0SJames Feist             {
152873df0db0SJames Feist                 continue;
152973df0db0SJames Feist             }
15306ee7f774SJames Feist             BMCWEB_LOG_DEBUG << *container;
15316ee7f774SJames Feist 
153273df0db0SJames Feist             std::string& type = containerPair.first;
153373df0db0SJames Feist 
153473df0db0SJames Feist             for (nlohmann::json::iterator it = container->begin();
153517a897dfSManojkiran Eda                  it != container->end(); ++it)
153673df0db0SJames Feist             {
153773df0db0SJames Feist                 const auto& name = it.key();
15386ee7f774SJames Feist                 BMCWEB_LOG_DEBUG << "looking for " << name;
15396ee7f774SJames Feist 
154073df0db0SJames Feist                 auto pathItr =
154173df0db0SJames Feist                     std::find_if(managedObj.begin(), managedObj.end(),
154273df0db0SJames Feist                                  [&name](const auto& obj) {
154373df0db0SJames Feist                                      return boost::algorithm::ends_with(
154473df0db0SJames Feist                                          obj.first.str, "/" + name);
154573df0db0SJames Feist                                  });
154673df0db0SJames Feist                 boost::container::flat_map<std::string,
154773df0db0SJames Feist                                            dbus::utility::DbusVariantType>
154873df0db0SJames Feist                     output;
154973df0db0SJames Feist 
155073df0db0SJames Feist                 output.reserve(16); // The pid interface length
155173df0db0SJames Feist 
155273df0db0SJames Feist                 // determines if we're patching entity-manager or
155373df0db0SJames Feist                 // creating a new object
155473df0db0SJames Feist                 bool createNewObject = (pathItr == managedObj.end());
15556ee7f774SJames Feist                 BMCWEB_LOG_DEBUG << "Found = " << !createNewObject;
15566ee7f774SJames Feist 
155773df0db0SJames Feist                 std::string iface;
155873df0db0SJames Feist                 if (type == "PidControllers" || type == "FanControllers")
155973df0db0SJames Feist                 {
156073df0db0SJames Feist                     iface = pidConfigurationIface;
156173df0db0SJames Feist                     if (!createNewObject &&
156273df0db0SJames Feist                         pathItr->second.find(pidConfigurationIface) ==
156373df0db0SJames Feist                             pathItr->second.end())
156473df0db0SJames Feist                     {
156573df0db0SJames Feist                         createNewObject = true;
156673df0db0SJames Feist                     }
156773df0db0SJames Feist                 }
156873df0db0SJames Feist                 else if (type == "FanZones")
156973df0db0SJames Feist                 {
157073df0db0SJames Feist                     iface = pidZoneConfigurationIface;
157173df0db0SJames Feist                     if (!createNewObject &&
157273df0db0SJames Feist                         pathItr->second.find(pidZoneConfigurationIface) ==
157373df0db0SJames Feist                             pathItr->second.end())
157473df0db0SJames Feist                     {
157573df0db0SJames Feist 
157673df0db0SJames Feist                         createNewObject = true;
157773df0db0SJames Feist                     }
157873df0db0SJames Feist                 }
157973df0db0SJames Feist                 else if (type == "StepwiseControllers")
158073df0db0SJames Feist                 {
158173df0db0SJames Feist                     iface = stepwiseConfigurationIface;
158273df0db0SJames Feist                     if (!createNewObject &&
158373df0db0SJames Feist                         pathItr->second.find(stepwiseConfigurationIface) ==
158473df0db0SJames Feist                             pathItr->second.end())
158573df0db0SJames Feist                     {
158673df0db0SJames Feist                         createNewObject = true;
158773df0db0SJames Feist                     }
158873df0db0SJames Feist                 }
15896ee7f774SJames Feist 
15906ee7f774SJames Feist                 if (createNewObject && it.value() == nullptr)
15916ee7f774SJames Feist                 {
15924e0453b1SGunnar Mills                     // can't delete a non-existent object
15936ee7f774SJames Feist                     messages::invalidObject(response->res, name);
15946ee7f774SJames Feist                     continue;
15956ee7f774SJames Feist                 }
15966ee7f774SJames Feist 
15976ee7f774SJames Feist                 std::string path;
15986ee7f774SJames Feist                 if (pathItr != managedObj.end())
15996ee7f774SJames Feist                 {
16006ee7f774SJames Feist                     path = pathItr->first.str;
16016ee7f774SJames Feist                 }
16026ee7f774SJames Feist 
160373df0db0SJames Feist                 BMCWEB_LOG_DEBUG << "Create new = " << createNewObject << "\n";
1604e69d9de2SJames Feist 
1605e69d9de2SJames Feist                 // arbitrary limit to avoid attacks
1606e69d9de2SJames Feist                 constexpr const size_t controllerLimit = 500;
160714b0b8d5SJames Feist                 if (createNewObject && objectCount >= controllerLimit)
1608e69d9de2SJames Feist                 {
1609e69d9de2SJames Feist                     messages::resourceExhaustion(response->res, type);
1610e69d9de2SJames Feist                     continue;
1611e69d9de2SJames Feist                 }
1612e69d9de2SJames Feist 
161373df0db0SJames Feist                 output["Name"] = boost::replace_all_copy(name, "_", " ");
161473df0db0SJames Feist 
161573df0db0SJames Feist                 std::string chassis;
161673df0db0SJames Feist                 CreatePIDRet ret = createPidInterface(
16176ee7f774SJames Feist                     response, type, it, path, managedObj, createNewObject,
16186ee7f774SJames Feist                     output, chassis, currentProfile);
161973df0db0SJames Feist                 if (ret == CreatePIDRet::fail)
162073df0db0SJames Feist                 {
162173df0db0SJames Feist                     return;
162273df0db0SJames Feist                 }
16233174e4dfSEd Tanous                 if (ret == CreatePIDRet::del)
162473df0db0SJames Feist                 {
162573df0db0SJames Feist                     continue;
162673df0db0SJames Feist                 }
162773df0db0SJames Feist 
162873df0db0SJames Feist                 if (!createNewObject)
162973df0db0SJames Feist                 {
163073df0db0SJames Feist                     for (const auto& property : output)
163173df0db0SJames Feist                     {
163273df0db0SJames Feist                         crow::connections::systemBus->async_method_call(
163373df0db0SJames Feist                             [response,
163473df0db0SJames Feist                              propertyName{std::string(property.first)}](
163573df0db0SJames Feist                                 const boost::system::error_code ec) {
163673df0db0SJames Feist                                 if (ec)
163773df0db0SJames Feist                                 {
163873df0db0SJames Feist                                     BMCWEB_LOG_ERROR << "Error patching "
163973df0db0SJames Feist                                                      << propertyName << ": "
164073df0db0SJames Feist                                                      << ec;
164173df0db0SJames Feist                                     messages::internalError(response->res);
164273df0db0SJames Feist                                     return;
164373df0db0SJames Feist                                 }
164473df0db0SJames Feist                                 messages::success(response->res);
164573df0db0SJames Feist                             },
16466ee7f774SJames Feist                             "xyz.openbmc_project.EntityManager", path,
164773df0db0SJames Feist                             "org.freedesktop.DBus.Properties", "Set", iface,
164873df0db0SJames Feist                             property.first, property.second);
164973df0db0SJames Feist                     }
165073df0db0SJames Feist                 }
165173df0db0SJames Feist                 else
165273df0db0SJames Feist                 {
165373df0db0SJames Feist                     if (chassis.empty())
165473df0db0SJames Feist                     {
165573df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Failed to get chassis from config";
165673df0db0SJames Feist                         messages::invalidObject(response->res, name);
165773df0db0SJames Feist                         return;
165873df0db0SJames Feist                     }
165973df0db0SJames Feist 
166073df0db0SJames Feist                     bool foundChassis = false;
166173df0db0SJames Feist                     for (const auto& obj : managedObj)
166273df0db0SJames Feist                     {
166373df0db0SJames Feist                         if (boost::algorithm::ends_with(obj.first.str, chassis))
166473df0db0SJames Feist                         {
166573df0db0SJames Feist                             chassis = obj.first.str;
166673df0db0SJames Feist                             foundChassis = true;
166773df0db0SJames Feist                             break;
166873df0db0SJames Feist                         }
166973df0db0SJames Feist                     }
167073df0db0SJames Feist                     if (!foundChassis)
167173df0db0SJames Feist                     {
167273df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Failed to find chassis on dbus";
167373df0db0SJames Feist                         messages::resourceMissingAtURI(
167473df0db0SJames Feist                             response->res, "/redfish/v1/Chassis/" + chassis);
167573df0db0SJames Feist                         return;
167673df0db0SJames Feist                     }
167773df0db0SJames Feist 
167873df0db0SJames Feist                     crow::connections::systemBus->async_method_call(
167973df0db0SJames Feist                         [response](const boost::system::error_code ec) {
168073df0db0SJames Feist                             if (ec)
168173df0db0SJames Feist                             {
168273df0db0SJames Feist                                 BMCWEB_LOG_ERROR << "Error Adding Pid Object "
168373df0db0SJames Feist                                                  << ec;
168473df0db0SJames Feist                                 messages::internalError(response->res);
168573df0db0SJames Feist                                 return;
168673df0db0SJames Feist                             }
168773df0db0SJames Feist                             messages::success(response->res);
168873df0db0SJames Feist                         },
168973df0db0SJames Feist                         "xyz.openbmc_project.EntityManager", chassis,
169073df0db0SJames Feist                         "xyz.openbmc_project.AddObject", "AddObject", output);
169173df0db0SJames Feist                 }
169273df0db0SJames Feist             }
169373df0db0SJames Feist         }
169473df0db0SJames Feist     }
169573df0db0SJames Feist     std::shared_ptr<AsyncResp> asyncResp;
169673df0db0SJames Feist     std::vector<std::pair<std::string, std::optional<nlohmann::json>>>
169773df0db0SJames Feist         configuration;
169873df0db0SJames Feist     std::optional<std::string> profile;
169973df0db0SJames Feist     dbus::utility::ManagedObjectType managedObj;
170073df0db0SJames Feist     std::vector<std::string> supportedProfiles;
170173df0db0SJames Feist     std::string currentProfile;
170273df0db0SJames Feist     std::string profileConnection;
170373df0db0SJames Feist     std::string profilePath;
170414b0b8d5SJames Feist     size_t objectCount = 0;
170573df0db0SJames Feist };
170673df0db0SJames Feist 
1707071d8fdfSSunnySrivastava1984 /**
1708071d8fdfSSunnySrivastava1984  * @brief Retrieves BMC manager location data over DBus
1709071d8fdfSSunnySrivastava1984  *
1710071d8fdfSSunnySrivastava1984  * @param[in] aResp Shared pointer for completing asynchronous calls
1711071d8fdfSSunnySrivastava1984  * @param[in] connectionName - service name
1712071d8fdfSSunnySrivastava1984  * @param[in] path - object path
1713071d8fdfSSunnySrivastava1984  * @return none
1714071d8fdfSSunnySrivastava1984  */
1715071d8fdfSSunnySrivastava1984 inline void getLocation(const std::shared_ptr<AsyncResp>& aResp,
1716071d8fdfSSunnySrivastava1984                         const std::string& connectionName,
1717071d8fdfSSunnySrivastava1984                         const std::string& path)
1718071d8fdfSSunnySrivastava1984 {
1719071d8fdfSSunnySrivastava1984     BMCWEB_LOG_DEBUG << "Get BMC manager Location data.";
1720071d8fdfSSunnySrivastava1984 
1721071d8fdfSSunnySrivastava1984     crow::connections::systemBus->async_method_call(
1722071d8fdfSSunnySrivastava1984         [aResp](const boost::system::error_code ec,
1723071d8fdfSSunnySrivastava1984                 const std::variant<std::string>& property) {
1724071d8fdfSSunnySrivastava1984             if (ec)
1725071d8fdfSSunnySrivastava1984             {
1726071d8fdfSSunnySrivastava1984                 BMCWEB_LOG_DEBUG << "DBUS response error for "
1727071d8fdfSSunnySrivastava1984                                     "Location";
1728071d8fdfSSunnySrivastava1984                 messages::internalError(aResp->res);
1729071d8fdfSSunnySrivastava1984                 return;
1730071d8fdfSSunnySrivastava1984             }
1731071d8fdfSSunnySrivastava1984 
1732071d8fdfSSunnySrivastava1984             const std::string* value = std::get_if<std::string>(&property);
1733071d8fdfSSunnySrivastava1984 
1734071d8fdfSSunnySrivastava1984             if (value == nullptr)
1735071d8fdfSSunnySrivastava1984             {
1736071d8fdfSSunnySrivastava1984                 // illegal value
1737071d8fdfSSunnySrivastava1984                 messages::internalError(aResp->res);
1738071d8fdfSSunnySrivastava1984                 return;
1739071d8fdfSSunnySrivastava1984             }
1740071d8fdfSSunnySrivastava1984 
1741071d8fdfSSunnySrivastava1984             aResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
1742071d8fdfSSunnySrivastava1984                 *value;
1743071d8fdfSSunnySrivastava1984         },
1744071d8fdfSSunnySrivastava1984         connectionName, path, "org.freedesktop.DBus.Properties", "Get",
1745071d8fdfSSunnySrivastava1984         "xyz.openbmc_project.Inventory.Decorator."
1746071d8fdfSSunnySrivastava1984         "LocationCode",
1747071d8fdfSSunnySrivastava1984         "LocationCode");
1748071d8fdfSSunnySrivastava1984 }
1749071d8fdfSSunnySrivastava1984 
175073df0db0SJames Feist class Manager : public Node
175173df0db0SJames Feist {
175273df0db0SJames Feist   public:
175352cc112dSEd Tanous     Manager(App& app) : Node(app, "/redfish/v1/Managers/bmc/")
175473df0db0SJames Feist     {
175552cc112dSEd Tanous 
175652cc112dSEd Tanous         uuid = persistent_data::getConfig().systemUuid;
175773df0db0SJames Feist         entityPrivileges = {
175873df0db0SJames Feist             {boost::beast::http::verb::get, {{"Login"}}},
175973df0db0SJames Feist             {boost::beast::http::verb::head, {{"Login"}}},
176073df0db0SJames Feist             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
176173df0db0SJames Feist             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
176273df0db0SJames Feist             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
176373df0db0SJames Feist             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
176473df0db0SJames Feist     }
176573df0db0SJames Feist 
176673df0db0SJames Feist   private:
1767cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
1768cb13a392SEd Tanous                const std::vector<std::string>&) override
17691abe55efSEd Tanous     {
17700f74e643SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
1771071d8fdfSSunnySrivastava1984         res.jsonValue["@odata.type"] = "#Manager.v1_11_0.Manager";
17720f74e643SEd Tanous         res.jsonValue["Id"] = "bmc";
17730f74e643SEd Tanous         res.jsonValue["Name"] = "OpenBmc Manager";
17740f74e643SEd Tanous         res.jsonValue["Description"] = "Baseboard Management Controller";
17750f74e643SEd Tanous         res.jsonValue["PowerState"] = "On";
1776029573d4SEd Tanous         res.jsonValue["Status"] = {{"State", "Enabled"}, {"Health", "OK"}};
17770f74e643SEd Tanous         res.jsonValue["ManagerType"] = "BMC";
17783602e232SEd Tanous         res.jsonValue["UUID"] = systemd_utils::getUuid();
17793602e232SEd Tanous         res.jsonValue["ServiceEntryPointUUID"] = uuid;
17800f74e643SEd Tanous         res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model
17810f74e643SEd Tanous 
17820f74e643SEd Tanous         res.jsonValue["LogServices"] = {
17830f74e643SEd Tanous             {"@odata.id", "/redfish/v1/Managers/bmc/LogServices"}};
17840f74e643SEd Tanous 
17850f74e643SEd Tanous         res.jsonValue["NetworkProtocol"] = {
17860f74e643SEd Tanous             {"@odata.id", "/redfish/v1/Managers/bmc/NetworkProtocol"}};
17870f74e643SEd Tanous 
17880f74e643SEd Tanous         res.jsonValue["EthernetInterfaces"] = {
17890f74e643SEd Tanous             {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces"}};
1790107077deSPrzemyslaw Czarnowski 
1791107077deSPrzemyslaw Czarnowski #ifdef BMCWEB_ENABLE_VM_NBDPROXY
1792107077deSPrzemyslaw Czarnowski         res.jsonValue["VirtualMedia"] = {
1793107077deSPrzemyslaw Czarnowski             {"@odata.id", "/redfish/v1/Managers/bmc/VirtualMedia"}};
1794107077deSPrzemyslaw Czarnowski #endif // BMCWEB_ENABLE_VM_NBDPROXY
1795107077deSPrzemyslaw Czarnowski 
17960f74e643SEd Tanous         // default oem data
17970f74e643SEd Tanous         nlohmann::json& oem = res.jsonValue["Oem"];
17980f74e643SEd Tanous         nlohmann::json& oemOpenbmc = oem["OpenBmc"];
17990f74e643SEd Tanous         oem["@odata.type"] = "#OemManager.Oem";
18000f74e643SEd Tanous         oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
18010f74e643SEd Tanous         oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
18020f74e643SEd Tanous         oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
1803cfcd5f6bSMarri Devender Rao         oemOpenbmc["Certificates"] = {
1804cfcd5f6bSMarri Devender Rao             {"@odata.id", "/redfish/v1/Managers/bmc/Truststore/Certificates"}};
18050f74e643SEd Tanous 
18062a5c4407SGunnar Mills         // Manager.Reset (an action) can be many values, OpenBMC only supports
18072a5c4407SGunnar Mills         // BMC reboot.
18082a5c4407SGunnar Mills         nlohmann::json& managerReset =
18090f74e643SEd Tanous             res.jsonValue["Actions"]["#Manager.Reset"];
18102a5c4407SGunnar Mills         managerReset["target"] =
1811ed5befbdSJennifer Lee             "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
18121cb1a9e6SAppaRao Puli         managerReset["@Redfish.ActionInfo"] =
18131cb1a9e6SAppaRao Puli             "/redfish/v1/Managers/bmc/ResetActionInfo";
1814ca537928SJennifer Lee 
18153e40fc74SGunnar Mills         // ResetToDefaults (Factory Reset) has values like
18163e40fc74SGunnar Mills         // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
18173e40fc74SGunnar Mills         // on OpenBMC
18183e40fc74SGunnar Mills         nlohmann::json& resetToDefaults =
18193e40fc74SGunnar Mills             res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
18203e40fc74SGunnar Mills         resetToDefaults["target"] =
18213e40fc74SGunnar Mills             "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults";
18223e40fc74SGunnar Mills         resetToDefaults["ResetType@Redfish.AllowableValues"] = {"ResetAll"};
18233e40fc74SGunnar Mills 
1824cb92c03bSAndrew Geissler         res.jsonValue["DateTime"] = crow::utility::dateTimeNow();
1825474bfad5SSantosh Puranik 
1826f8c3e6f0SKuiying Wang         // Fill in SerialConsole info
1827474bfad5SSantosh Puranik         res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
1828f8c3e6f0SKuiying Wang         res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15;
1829474bfad5SSantosh Puranik         res.jsonValue["SerialConsole"]["ConnectTypesSupported"] = {"IPMI",
1830474bfad5SSantosh Puranik                                                                    "SSH"};
1831ef47bb18SSantosh Puranik #ifdef BMCWEB_ENABLE_KVM
1832f8c3e6f0SKuiying Wang         // Fill in GraphicalConsole info
1833ef47bb18SSantosh Puranik         res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true;
1834704fae6cSJae Hyun Yoo         res.jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] = 4;
1835ef47bb18SSantosh Puranik         res.jsonValue["GraphicalConsole"]["ConnectTypesSupported"] = {"KVMIP"};
1836ef47bb18SSantosh Puranik #endif // BMCWEB_ENABLE_KVM
1837474bfad5SSantosh Puranik 
1838603a6640SGunnar Mills         res.jsonValue["Links"]["ManagerForServers@odata.count"] = 1;
1839603a6640SGunnar Mills         res.jsonValue["Links"]["ManagerForServers"] = {
1840603a6640SGunnar Mills             {{"@odata.id", "/redfish/v1/Systems/system"}}};
184126f03899SShawn McCarney 
1842ed5befbdSJennifer Lee         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
18435b4aa86bSJames Feist 
1844b49ac873SJames Feist         auto health = std::make_shared<HealthPopulate>(asyncResp);
1845b49ac873SJames Feist         health->isManagersHealth = true;
1846b49ac873SJames Feist         health->populate();
1847b49ac873SJames Feist 
1848f97ddba7SGunnar Mills         fw_util::populateFirmwareInformation(asyncResp, fw_util::bmcPurpose,
184972d566d9SGunnar Mills                                              "FirmwareVersion", true);
18500f6b00bdSJames Feist 
18514bf2b033SGunnar Mills         getLastResetTime(asyncResp);
18524bf2b033SGunnar Mills 
185373df0db0SJames Feist         auto pids = std::make_shared<GetPIDValues>(asyncResp);
185473df0db0SJames Feist         pids->run();
1855c5d03ff4SJennifer Lee 
1856c5d03ff4SJennifer Lee         getMainChassisId(asyncResp, [](const std::string& chassisId,
1857b5a76932SEd Tanous                                        const std::shared_ptr<AsyncResp>& aRsp) {
1858c5d03ff4SJennifer Lee             aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1;
1859c5d03ff4SJennifer Lee             aRsp->res.jsonValue["Links"]["ManagerForChassis"] = {
1860c5d03ff4SJennifer Lee                 {{"@odata.id", "/redfish/v1/Chassis/" + chassisId}}};
18612c0feb00SJason M. Bills             aRsp->res.jsonValue["Links"]["ManagerInChassis"] = {
18622c0feb00SJason M. Bills                 {"@odata.id", "/redfish/v1/Chassis/" + chassisId}};
1863c5d03ff4SJennifer Lee         });
18640f6b00bdSJames Feist 
18650f6b00bdSJames Feist         static bool started = false;
18660f6b00bdSJames Feist 
18670f6b00bdSJames Feist         if (!started)
18680f6b00bdSJames Feist         {
18690f6b00bdSJames Feist             crow::connections::systemBus->async_method_call(
18700f6b00bdSJames Feist                 [asyncResp](const boost::system::error_code ec,
18710f6b00bdSJames Feist                             const std::variant<double>& resp) {
18720f6b00bdSJames Feist                     if (ec)
18730f6b00bdSJames Feist                     {
18740f6b00bdSJames Feist                         BMCWEB_LOG_ERROR << "Error while getting progress";
18750f6b00bdSJames Feist                         messages::internalError(asyncResp->res);
18760f6b00bdSJames Feist                         return;
18770f6b00bdSJames Feist                     }
18780f6b00bdSJames Feist                     const double* val = std::get_if<double>(&resp);
18790f6b00bdSJames Feist                     if (val == nullptr)
18800f6b00bdSJames Feist                     {
18810f6b00bdSJames Feist                         BMCWEB_LOG_ERROR
18820f6b00bdSJames Feist                             << "Invalid response while getting progress";
18830f6b00bdSJames Feist                         messages::internalError(asyncResp->res);
18840f6b00bdSJames Feist                         return;
18850f6b00bdSJames Feist                     }
18860f6b00bdSJames Feist                     if (*val < 1.0)
18870f6b00bdSJames Feist                     {
18880f6b00bdSJames Feist                         asyncResp->res.jsonValue["Status"]["State"] =
18890f6b00bdSJames Feist                             "Starting";
18900f6b00bdSJames Feist                         started = true;
18910f6b00bdSJames Feist                     }
18920f6b00bdSJames Feist                 },
18930f6b00bdSJames Feist                 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
18940f6b00bdSJames Feist                 "org.freedesktop.DBus.Properties", "Get",
18950f6b00bdSJames Feist                 "org.freedesktop.systemd1.Manager", "Progress");
18960f6b00bdSJames Feist         }
1897ef6ca6e4SChicago Duan 
1898ef6ca6e4SChicago Duan         crow::connections::systemBus->async_method_call(
1899ef6ca6e4SChicago Duan             [asyncResp](
1900ef6ca6e4SChicago Duan                 const boost::system::error_code ec,
1901ef6ca6e4SChicago Duan                 const std::vector<std::pair<
1902ef6ca6e4SChicago Duan                     std::string, std::vector<std::pair<
1903ef6ca6e4SChicago Duan                                      std::string, std::vector<std::string>>>>>&
1904ef6ca6e4SChicago Duan                     subtree) {
1905ef6ca6e4SChicago Duan                 if (ec)
1906ef6ca6e4SChicago Duan                 {
1907ef6ca6e4SChicago Duan                     BMCWEB_LOG_DEBUG << "D-Bus response error on GetSubTree "
1908ef6ca6e4SChicago Duan                                      << ec;
1909ef6ca6e4SChicago Duan                     return;
1910ef6ca6e4SChicago Duan                 }
1911ef6ca6e4SChicago Duan                 if (subtree.size() == 0)
1912ef6ca6e4SChicago Duan                 {
1913ef6ca6e4SChicago Duan                     BMCWEB_LOG_DEBUG << "Can't find bmc D-Bus object!";
1914ef6ca6e4SChicago Duan                     return;
1915ef6ca6e4SChicago Duan                 }
1916ef6ca6e4SChicago Duan                 // Assume only 1 bmc D-Bus object
1917ef6ca6e4SChicago Duan                 // Throw an error if there is more than 1
1918ef6ca6e4SChicago Duan                 if (subtree.size() > 1)
1919ef6ca6e4SChicago Duan                 {
1920ef6ca6e4SChicago Duan                     BMCWEB_LOG_DEBUG << "Found more than 1 bmc D-Bus object!";
1921ef6ca6e4SChicago Duan                     messages::internalError(asyncResp->res);
1922ef6ca6e4SChicago Duan                     return;
1923ef6ca6e4SChicago Duan                 }
1924ef6ca6e4SChicago Duan 
1925ef6ca6e4SChicago Duan                 if (subtree[0].first.empty() || subtree[0].second.size() != 1)
1926ef6ca6e4SChicago Duan                 {
1927ef6ca6e4SChicago Duan                     BMCWEB_LOG_DEBUG << "Error getting bmc D-Bus object!";
1928ef6ca6e4SChicago Duan                     messages::internalError(asyncResp->res);
1929ef6ca6e4SChicago Duan                     return;
1930ef6ca6e4SChicago Duan                 }
1931ef6ca6e4SChicago Duan 
1932ef6ca6e4SChicago Duan                 const std::string& path = subtree[0].first;
1933ef6ca6e4SChicago Duan                 const std::string& connectionName = subtree[0].second[0].first;
1934ef6ca6e4SChicago Duan 
1935071d8fdfSSunnySrivastava1984                 for (const auto& interfaceName : subtree[0].second[0].second)
1936071d8fdfSSunnySrivastava1984                 {
1937071d8fdfSSunnySrivastava1984                     if (interfaceName ==
1938071d8fdfSSunnySrivastava1984                         "xyz.openbmc_project.Inventory.Decorator.Asset")
1939071d8fdfSSunnySrivastava1984                     {
1940ef6ca6e4SChicago Duan                         crow::connections::systemBus->async_method_call(
1941ef6ca6e4SChicago Duan                             [asyncResp](
1942ef6ca6e4SChicago Duan                                 const boost::system::error_code ec,
1943071d8fdfSSunnySrivastava1984                                 const std::vector<std::pair<
1944071d8fdfSSunnySrivastava1984                                     std::string, std::variant<std::string>>>&
1945ef6ca6e4SChicago Duan                                     propertiesList) {
1946ef6ca6e4SChicago Duan                                 if (ec)
1947ef6ca6e4SChicago Duan                                 {
1948ef6ca6e4SChicago Duan                                     BMCWEB_LOG_DEBUG << "Can't get bmc asset!";
1949ef6ca6e4SChicago Duan                                     return;
1950ef6ca6e4SChicago Duan                                 }
1951ef6ca6e4SChicago Duan                                 for (const std::pair<std::string,
1952ef6ca6e4SChicago Duan                                                      std::variant<std::string>>&
1953ef6ca6e4SChicago Duan                                          property : propertiesList)
1954ef6ca6e4SChicago Duan                                 {
1955071d8fdfSSunnySrivastava1984                                     const std::string& propertyName =
1956071d8fdfSSunnySrivastava1984                                         property.first;
1957ef6ca6e4SChicago Duan 
1958ef6ca6e4SChicago Duan                                     if ((propertyName == "PartNumber") ||
1959ef6ca6e4SChicago Duan                                         (propertyName == "SerialNumber") ||
1960071d8fdfSSunnySrivastava1984                                         (propertyName == "Manufacturer") ||
1961071d8fdfSSunnySrivastava1984                                         (propertyName == "Model") ||
1962071d8fdfSSunnySrivastava1984                                         (propertyName == "SparePartNumber"))
1963ef6ca6e4SChicago Duan                                     {
1964ef6ca6e4SChicago Duan                                         const std::string* value =
1965071d8fdfSSunnySrivastava1984                                             std::get_if<std::string>(
1966071d8fdfSSunnySrivastava1984                                                 &property.second);
1967ef6ca6e4SChicago Duan                                         if (value == nullptr)
1968ef6ca6e4SChicago Duan                                         {
1969ef6ca6e4SChicago Duan                                             // illegal property
1970071d8fdfSSunnySrivastava1984                                             messages::internalError(
1971071d8fdfSSunnySrivastava1984                                                 asyncResp->res);
1972ef6ca6e4SChicago Duan                                             continue;
1973ef6ca6e4SChicago Duan                                         }
1974071d8fdfSSunnySrivastava1984                                         asyncResp->res.jsonValue[propertyName] =
1975071d8fdfSSunnySrivastava1984                                             *value;
1976ef6ca6e4SChicago Duan                                     }
1977ef6ca6e4SChicago Duan                                 }
1978ef6ca6e4SChicago Duan                             },
1979071d8fdfSSunnySrivastava1984                             connectionName, path,
1980071d8fdfSSunnySrivastava1984                             "org.freedesktop.DBus.Properties", "GetAll",
1981071d8fdfSSunnySrivastava1984                             "xyz.openbmc_project.Inventory.Decorator.Asset");
1982071d8fdfSSunnySrivastava1984                     }
1983071d8fdfSSunnySrivastava1984                     else if (interfaceName == "xyz.openbmc_project.Inventory."
1984071d8fdfSSunnySrivastava1984                                               "Decorator.LocationCode")
1985071d8fdfSSunnySrivastava1984                     {
1986071d8fdfSSunnySrivastava1984                         getLocation(asyncResp, connectionName, path);
1987071d8fdfSSunnySrivastava1984                     }
1988071d8fdfSSunnySrivastava1984                 }
1989ef6ca6e4SChicago Duan             },
1990ef6ca6e4SChicago Duan             "xyz.openbmc_project.ObjectMapper",
1991ef6ca6e4SChicago Duan             "/xyz/openbmc_project/object_mapper",
1992ef6ca6e4SChicago Duan             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
1993ef6ca6e4SChicago Duan             "/xyz/openbmc_project/inventory", int32_t(0),
1994ef6ca6e4SChicago Duan             std::array<const char*, 1>{
1995ef6ca6e4SChicago Duan                 "xyz.openbmc_project.Inventory.Item.Bmc"});
199683ff9ab6SJames Feist     }
19975b4aa86bSJames Feist 
19985b4aa86bSJames Feist     void doPatch(crow::Response& res, const crow::Request& req,
1999cb13a392SEd Tanous                  const std::vector<std::string>&) override
20005b4aa86bSJames Feist     {
20010627a2c7SEd Tanous         std::optional<nlohmann::json> oem;
20024bfefa74SGunnar Mills         std::optional<nlohmann::json> links;
2003af5d6058SSantosh Puranik         std::optional<std::string> datetime;
200441352c24SSantosh Puranik         std::shared_ptr<AsyncResp> response = std::make_shared<AsyncResp>(res);
20050627a2c7SEd Tanous 
200641352c24SSantosh Puranik         if (!json_util::readJson(req, response->res, "Oem", oem, "DateTime",
20074bfefa74SGunnar Mills                                  datetime, "Links", links))
200883ff9ab6SJames Feist         {
200983ff9ab6SJames Feist             return;
201083ff9ab6SJames Feist         }
20110627a2c7SEd Tanous 
20120627a2c7SEd Tanous         if (oem)
201383ff9ab6SJames Feist         {
20145f2caaefSJames Feist             std::optional<nlohmann::json> openbmc;
201543b761d0SEd Tanous             if (!redfish::json_util::readJson(*oem, res, "OpenBmc", openbmc))
201683ff9ab6SJames Feist             {
2017*71f52d96SEd Tanous                 BMCWEB_LOG_ERROR
2018*71f52d96SEd Tanous                     << "Illegal Property "
2019*71f52d96SEd Tanous                     << oem->dump(2, ' ', true,
2020*71f52d96SEd Tanous                                  nlohmann::json::error_handler_t::replace);
202183ff9ab6SJames Feist                 return;
202283ff9ab6SJames Feist             }
20235f2caaefSJames Feist             if (openbmc)
202483ff9ab6SJames Feist             {
20255f2caaefSJames Feist                 std::optional<nlohmann::json> fan;
202643b761d0SEd Tanous                 if (!redfish::json_util::readJson(*openbmc, res, "Fan", fan))
202783ff9ab6SJames Feist                 {
2028*71f52d96SEd Tanous                     BMCWEB_LOG_ERROR
2029*71f52d96SEd Tanous                         << "Illegal Property "
2030*71f52d96SEd Tanous                         << openbmc->dump(
2031*71f52d96SEd Tanous                                2, ' ', true,
2032*71f52d96SEd Tanous                                nlohmann::json::error_handler_t::replace);
203383ff9ab6SJames Feist                     return;
203483ff9ab6SJames Feist                 }
20355f2caaefSJames Feist                 if (fan)
203683ff9ab6SJames Feist                 {
203773df0db0SJames Feist                     auto pid = std::make_shared<SetPIDValues>(response, *fan);
203873df0db0SJames Feist                     pid->run();
203983ff9ab6SJames Feist                 }
204083ff9ab6SJames Feist             }
204183ff9ab6SJames Feist         }
20424bfefa74SGunnar Mills         if (links)
20434bfefa74SGunnar Mills         {
20444bfefa74SGunnar Mills             std::optional<nlohmann::json> activeSoftwareImage;
20454bfefa74SGunnar Mills             if (!redfish::json_util::readJson(
20464bfefa74SGunnar Mills                     *links, res, "ActiveSoftwareImage", activeSoftwareImage))
20474bfefa74SGunnar Mills             {
20484bfefa74SGunnar Mills                 return;
20494bfefa74SGunnar Mills             }
20504bfefa74SGunnar Mills             if (activeSoftwareImage)
20514bfefa74SGunnar Mills             {
20524bfefa74SGunnar Mills                 std::optional<std::string> odataId;
20534bfefa74SGunnar Mills                 if (!json_util::readJson(*activeSoftwareImage, res, "@odata.id",
20544bfefa74SGunnar Mills                                          odataId))
20554bfefa74SGunnar Mills                 {
20564bfefa74SGunnar Mills                     return;
20574bfefa74SGunnar Mills                 }
20584bfefa74SGunnar Mills 
20594bfefa74SGunnar Mills                 if (odataId)
20604bfefa74SGunnar Mills                 {
2061f23b7296SEd Tanous                     setActiveFirmwareImage(response, *odataId);
20624bfefa74SGunnar Mills                 }
20634bfefa74SGunnar Mills             }
20644bfefa74SGunnar Mills         }
2065af5d6058SSantosh Puranik         if (datetime)
2066af5d6058SSantosh Puranik         {
2067af5d6058SSantosh Puranik             setDateTime(response, std::move(*datetime));
2068af5d6058SSantosh Puranik         }
2069af5d6058SSantosh Puranik     }
2070af5d6058SSantosh Puranik 
2071b5a76932SEd Tanous     void getLastResetTime(const std::shared_ptr<AsyncResp>& aResp)
20724bf2b033SGunnar Mills     {
20734bf2b033SGunnar Mills         BMCWEB_LOG_DEBUG << "Getting Manager Last Reset Time";
20744bf2b033SGunnar Mills 
20754bf2b033SGunnar Mills         crow::connections::systemBus->async_method_call(
20764bf2b033SGunnar Mills             [aResp](const boost::system::error_code ec,
20774bf2b033SGunnar Mills                     std::variant<uint64_t>& lastResetTime) {
20784bf2b033SGunnar Mills                 if (ec)
20794bf2b033SGunnar Mills                 {
20804bf2b033SGunnar Mills                     BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
20814bf2b033SGunnar Mills                     return;
20824bf2b033SGunnar Mills                 }
20834bf2b033SGunnar Mills 
20844bf2b033SGunnar Mills                 const uint64_t* lastResetTimePtr =
20854bf2b033SGunnar Mills                     std::get_if<uint64_t>(&lastResetTime);
20864bf2b033SGunnar Mills 
20874bf2b033SGunnar Mills                 if (!lastResetTimePtr)
20884bf2b033SGunnar Mills                 {
20894bf2b033SGunnar Mills                     messages::internalError(aResp->res);
20904bf2b033SGunnar Mills                     return;
20914bf2b033SGunnar Mills                 }
20924bf2b033SGunnar Mills                 // LastRebootTime is epoch time, in milliseconds
20934bf2b033SGunnar Mills                 // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
20944bf2b033SGunnar Mills                 time_t lastResetTimeStamp =
20954bf2b033SGunnar Mills                     static_cast<time_t>(*lastResetTimePtr / 1000);
20964bf2b033SGunnar Mills 
20974bf2b033SGunnar Mills                 // Convert to ISO 8601 standard
20984bf2b033SGunnar Mills                 aResp->res.jsonValue["LastResetTime"] =
20994bf2b033SGunnar Mills                     crow::utility::getDateTime(lastResetTimeStamp);
21004bf2b033SGunnar Mills             },
21014bf2b033SGunnar Mills             "xyz.openbmc_project.State.BMC", "/xyz/openbmc_project/state/bmc0",
21024bf2b033SGunnar Mills             "org.freedesktop.DBus.Properties", "Get",
21034bf2b033SGunnar Mills             "xyz.openbmc_project.State.BMC", "LastRebootTime");
21044bf2b033SGunnar Mills     }
21054bf2b033SGunnar Mills 
21064bfefa74SGunnar Mills     /**
21074bfefa74SGunnar Mills      * @brief Set the running firmware image
21084bfefa74SGunnar Mills      *
21094bfefa74SGunnar Mills      * @param[i,o] aResp - Async response object
21104bfefa74SGunnar Mills      * @param[i] runningFirmwareTarget - Image to make the running image
21114bfefa74SGunnar Mills      *
21124bfefa74SGunnar Mills      * @return void
21134bfefa74SGunnar Mills      */
2114b5a76932SEd Tanous     void setActiveFirmwareImage(const std::shared_ptr<AsyncResp>& aResp,
2115f23b7296SEd Tanous                                 const std::string& runningFirmwareTarget)
21164bfefa74SGunnar Mills     {
21174bfefa74SGunnar Mills         // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
2118f23b7296SEd Tanous         std::string::size_type idPos = runningFirmwareTarget.rfind('/');
21194bfefa74SGunnar Mills         if (idPos == std::string::npos)
21204bfefa74SGunnar Mills         {
21214bfefa74SGunnar Mills             messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
21224bfefa74SGunnar Mills                                              "@odata.id");
21234bfefa74SGunnar Mills             BMCWEB_LOG_DEBUG << "Can't parse firmware ID!";
21244bfefa74SGunnar Mills             return;
21254bfefa74SGunnar Mills         }
21264bfefa74SGunnar Mills         idPos++;
21274bfefa74SGunnar Mills         if (idPos >= runningFirmwareTarget.size())
21284bfefa74SGunnar Mills         {
21294bfefa74SGunnar Mills             messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
21304bfefa74SGunnar Mills                                              "@odata.id");
21314bfefa74SGunnar Mills             BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
21324bfefa74SGunnar Mills             return;
21334bfefa74SGunnar Mills         }
21344bfefa74SGunnar Mills         std::string firmwareId = runningFirmwareTarget.substr(idPos);
21354bfefa74SGunnar Mills 
21364bfefa74SGunnar Mills         // Make sure the image is valid before setting priority
21374bfefa74SGunnar Mills         crow::connections::systemBus->async_method_call(
21384bfefa74SGunnar Mills             [aResp, firmwareId,
21394bfefa74SGunnar Mills              runningFirmwareTarget](const boost::system::error_code ec,
21404bfefa74SGunnar Mills                                     ManagedObjectType& subtree) {
21414bfefa74SGunnar Mills                 if (ec)
21424bfefa74SGunnar Mills                 {
21434bfefa74SGunnar Mills                     BMCWEB_LOG_DEBUG << "D-Bus response error getting objects.";
21444bfefa74SGunnar Mills                     messages::internalError(aResp->res);
21454bfefa74SGunnar Mills                     return;
21464bfefa74SGunnar Mills                 }
21474bfefa74SGunnar Mills 
21484bfefa74SGunnar Mills                 if (subtree.size() == 0)
21494bfefa74SGunnar Mills                 {
21504bfefa74SGunnar Mills                     BMCWEB_LOG_DEBUG << "Can't find image!";
21514bfefa74SGunnar Mills                     messages::internalError(aResp->res);
21524bfefa74SGunnar Mills                     return;
21534bfefa74SGunnar Mills                 }
21544bfefa74SGunnar Mills 
21554bfefa74SGunnar Mills                 bool foundImage = false;
21564bfefa74SGunnar Mills                 for (auto& object : subtree)
21574bfefa74SGunnar Mills                 {
21584bfefa74SGunnar Mills                     const std::string& path =
21594bfefa74SGunnar Mills                         static_cast<const std::string&>(object.first);
2160f23b7296SEd Tanous                     std::size_t idPos2 = path.rfind('/');
21614bfefa74SGunnar Mills 
21624bfefa74SGunnar Mills                     if (idPos2 == std::string::npos)
21634bfefa74SGunnar Mills                     {
21644bfefa74SGunnar Mills                         continue;
21654bfefa74SGunnar Mills                     }
21664bfefa74SGunnar Mills 
21674bfefa74SGunnar Mills                     idPos2++;
21684bfefa74SGunnar Mills                     if (idPos2 >= path.size())
21694bfefa74SGunnar Mills                     {
21704bfefa74SGunnar Mills                         continue;
21714bfefa74SGunnar Mills                     }
21724bfefa74SGunnar Mills 
21734bfefa74SGunnar Mills                     if (path.substr(idPos2) == firmwareId)
21744bfefa74SGunnar Mills                     {
21754bfefa74SGunnar Mills                         foundImage = true;
21764bfefa74SGunnar Mills                         break;
21774bfefa74SGunnar Mills                     }
21784bfefa74SGunnar Mills                 }
21794bfefa74SGunnar Mills 
21804bfefa74SGunnar Mills                 if (!foundImage)
21814bfefa74SGunnar Mills                 {
21824bfefa74SGunnar Mills                     messages::propertyValueNotInList(
21834bfefa74SGunnar Mills                         aResp->res, runningFirmwareTarget, "@odata.id");
21844bfefa74SGunnar Mills                     BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
21854bfefa74SGunnar Mills                     return;
21864bfefa74SGunnar Mills                 }
21874bfefa74SGunnar Mills 
21884bfefa74SGunnar Mills                 BMCWEB_LOG_DEBUG << "Setting firmware version " + firmwareId +
21894bfefa74SGunnar Mills                                         " to priority 0.";
21904bfefa74SGunnar Mills 
21914bfefa74SGunnar Mills                 // Only support Immediate
21924bfefa74SGunnar Mills                 // An addition could be a Redfish Setting like
21934bfefa74SGunnar Mills                 // ActiveSoftwareImageApplyTime and support OnReset
21944bfefa74SGunnar Mills                 crow::connections::systemBus->async_method_call(
21954bfefa74SGunnar Mills                     [aResp](const boost::system::error_code ec) {
21964bfefa74SGunnar Mills                         if (ec)
21974bfefa74SGunnar Mills                         {
21984bfefa74SGunnar Mills                             BMCWEB_LOG_DEBUG << "D-Bus response error setting.";
21994bfefa74SGunnar Mills                             messages::internalError(aResp->res);
22004bfefa74SGunnar Mills                             return;
22014bfefa74SGunnar Mills                         }
22024bfefa74SGunnar Mills                         doBMCGracefulRestart(aResp);
22034bfefa74SGunnar Mills                     },
22044bfefa74SGunnar Mills 
22054bfefa74SGunnar Mills                     "xyz.openbmc_project.Software.BMC.Updater",
22064bfefa74SGunnar Mills                     "/xyz/openbmc_project/software/" + firmwareId,
22074bfefa74SGunnar Mills                     "org.freedesktop.DBus.Properties", "Set",
22084bfefa74SGunnar Mills                     "xyz.openbmc_project.Software.RedundancyPriority",
22094bfefa74SGunnar Mills                     "Priority", std::variant<uint8_t>(static_cast<uint8_t>(0)));
22104bfefa74SGunnar Mills             },
22114bfefa74SGunnar Mills             "xyz.openbmc_project.Software.BMC.Updater",
22124bfefa74SGunnar Mills             "/xyz/openbmc_project/software",
22134bfefa74SGunnar Mills             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
22144bfefa74SGunnar Mills     }
22154bfefa74SGunnar Mills 
2216af5d6058SSantosh Puranik     void setDateTime(std::shared_ptr<AsyncResp> aResp,
2217af5d6058SSantosh Puranik                      std::string datetime) const
2218af5d6058SSantosh Puranik     {
2219af5d6058SSantosh Puranik         BMCWEB_LOG_DEBUG << "Set date time: " << datetime;
2220af5d6058SSantosh Puranik 
2221af5d6058SSantosh Puranik         std::stringstream stream(datetime);
2222af5d6058SSantosh Puranik         // Convert from ISO 8601 to boost local_time
2223af5d6058SSantosh Puranik         // (BMC only has time in UTC)
2224af5d6058SSantosh Puranik         boost::posix_time::ptime posixTime;
2225af5d6058SSantosh Puranik         boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1));
2226af5d6058SSantosh Puranik         // Facet gets deleted with the stringsteam
2227af5d6058SSantosh Puranik         auto ifc = std::make_unique<boost::local_time::local_time_input_facet>(
2228af5d6058SSantosh Puranik             "%Y-%m-%d %H:%M:%S%F %ZP");
2229af5d6058SSantosh Puranik         stream.imbue(std::locale(stream.getloc(), ifc.release()));
2230af5d6058SSantosh Puranik 
2231af5d6058SSantosh Puranik         boost::local_time::local_date_time ldt(
2232af5d6058SSantosh Puranik             boost::local_time::not_a_date_time);
2233af5d6058SSantosh Puranik 
2234af5d6058SSantosh Puranik         if (stream >> ldt)
2235af5d6058SSantosh Puranik         {
2236af5d6058SSantosh Puranik             posixTime = ldt.utc_time();
2237af5d6058SSantosh Puranik             boost::posix_time::time_duration dur = posixTime - epoch;
2238af5d6058SSantosh Puranik             uint64_t durMicroSecs =
2239af5d6058SSantosh Puranik                 static_cast<uint64_t>(dur.total_microseconds());
2240af5d6058SSantosh Puranik             crow::connections::systemBus->async_method_call(
2241af5d6058SSantosh Puranik                 [aResp{std::move(aResp)}, datetime{std::move(datetime)}](
2242af5d6058SSantosh Puranik                     const boost::system::error_code ec) {
2243af5d6058SSantosh Puranik                     if (ec)
2244af5d6058SSantosh Puranik                     {
2245af5d6058SSantosh Puranik                         BMCWEB_LOG_DEBUG << "Failed to set elapsed time. "
2246af5d6058SSantosh Puranik                                             "DBUS response error "
2247af5d6058SSantosh Puranik                                          << ec;
2248af5d6058SSantosh Puranik                         messages::internalError(aResp->res);
2249af5d6058SSantosh Puranik                         return;
2250af5d6058SSantosh Puranik                     }
2251af5d6058SSantosh Puranik                     aResp->res.jsonValue["DateTime"] = datetime;
2252af5d6058SSantosh Puranik                 },
2253af5d6058SSantosh Puranik                 "xyz.openbmc_project.Time.Manager",
2254af5d6058SSantosh Puranik                 "/xyz/openbmc_project/time/bmc",
2255af5d6058SSantosh Puranik                 "org.freedesktop.DBus.Properties", "Set",
2256af5d6058SSantosh Puranik                 "xyz.openbmc_project.Time.EpochTime", "Elapsed",
2257af5d6058SSantosh Puranik                 std::variant<uint64_t>(durMicroSecs));
2258af5d6058SSantosh Puranik         }
2259af5d6058SSantosh Puranik         else
2260af5d6058SSantosh Puranik         {
2261af5d6058SSantosh Puranik             messages::propertyValueFormatError(aResp->res, datetime,
2262af5d6058SSantosh Puranik                                                "DateTime");
2263af5d6058SSantosh Puranik             return;
2264af5d6058SSantosh Puranik         }
226583ff9ab6SJames Feist     }
22669c310685SBorawski.Lukasz 
22670f74e643SEd Tanous     std::string uuid;
22689c310685SBorawski.Lukasz };
22699c310685SBorawski.Lukasz 
22701abe55efSEd Tanous class ManagerCollection : public Node
22711abe55efSEd Tanous {
22729c310685SBorawski.Lukasz   public:
227352cc112dSEd Tanous     ManagerCollection(App& app) : Node(app, "/redfish/v1/Managers/")
22741abe55efSEd Tanous     {
2275a434f2bdSEd Tanous         entityPrivileges = {
2276a434f2bdSEd Tanous             {boost::beast::http::verb::get, {{"Login"}}},
2277e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
2278e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2279e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2280e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2281e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
22829c310685SBorawski.Lukasz     }
22839c310685SBorawski.Lukasz 
22849c310685SBorawski.Lukasz   private:
2285cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
2286cb13a392SEd Tanous                const std::vector<std::string>&) override
22871abe55efSEd Tanous     {
228883ff9ab6SJames Feist         // Collections don't include the static data added by SubRoute
228983ff9ab6SJames Feist         // because it has a duplicate entry for members
229055c7b7a2SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
229155c7b7a2SEd Tanous         res.jsonValue["@odata.type"] = "#ManagerCollection.ManagerCollection";
229255c7b7a2SEd Tanous         res.jsonValue["Name"] = "Manager Collection";
229355c7b7a2SEd Tanous         res.jsonValue["Members@odata.count"] = 1;
229455c7b7a2SEd Tanous         res.jsonValue["Members"] = {
22955b4aa86bSJames Feist             {{"@odata.id", "/redfish/v1/Managers/bmc"}}};
22969c310685SBorawski.Lukasz         res.end();
22979c310685SBorawski.Lukasz     }
22989c310685SBorawski.Lukasz };
22999c310685SBorawski.Lukasz } // namespace redfish
2300