xref: /openbmc/bmcweb/features/redfish/lib/managers.hpp (revision b5a76932eab7d40487ffb305cd745ec155813c4e)
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  */
41*b5a76932SEd 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 
69*b5a76932SEd 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,
281*b5a76932SEd 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,
766*b5a76932SEd 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         {
7915f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Type "
7925f2caaefSJames Feist                              << type;
7935f2caaefSJames Feist             messages::propertyUnknown(response->res, type);
7945f2caaefSJames Feist             return CreatePIDRet::fail;
7955f2caaefSJames Feist         }
7966ee7f774SJames Feist 
7976ee7f774SJames Feist         BMCWEB_LOG_DEBUG << "del " << path << " " << iface << "\n";
7985f2caaefSJames Feist         // delete interface
7995f2caaefSJames Feist         crow::connections::systemBus->async_method_call(
8005f2caaefSJames Feist             [response, path](const boost::system::error_code ec) {
8015f2caaefSJames Feist                 if (ec)
8025f2caaefSJames Feist                 {
8035f2caaefSJames Feist                     BMCWEB_LOG_ERROR << "Error patching " << path << ": " << ec;
8045f2caaefSJames Feist                     messages::internalError(response->res);
805b6baeaa4SJames Feist                     return;
8065f2caaefSJames Feist                 }
807b6baeaa4SJames Feist                 messages::success(response->res);
8085f2caaefSJames Feist             },
8095f2caaefSJames Feist             "xyz.openbmc_project.EntityManager", path, iface, "Delete");
8105f2caaefSJames Feist         return CreatePIDRet::del;
8115f2caaefSJames Feist     }
8125f2caaefSJames Feist 
81373df0db0SJames Feist     const dbus::utility::ManagedItem* managedItem = nullptr;
814b6baeaa4SJames Feist     if (!createNewObject)
815b6baeaa4SJames Feist     {
816b6baeaa4SJames Feist         // if we aren't creating a new object, we should be able to find it on
817b6baeaa4SJames Feist         // d-bus
81873df0db0SJames Feist         managedItem = findChassis(managedObj, it.key(), chassis);
81973df0db0SJames Feist         if (managedItem == nullptr)
820b6baeaa4SJames Feist         {
821b6baeaa4SJames Feist             BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
822b6baeaa4SJames Feist             messages::invalidObject(response->res, it.key());
823b6baeaa4SJames Feist             return CreatePIDRet::fail;
824b6baeaa4SJames Feist         }
825b6baeaa4SJames Feist     }
826b6baeaa4SJames Feist 
82773df0db0SJames Feist     if (profile.size() &&
82873df0db0SJames Feist         (type == "PidControllers" || type == "FanControllers" ||
82973df0db0SJames Feist          type == "StepwiseControllers"))
83073df0db0SJames Feist     {
83173df0db0SJames Feist         if (managedItem == nullptr)
83273df0db0SJames Feist         {
83373df0db0SJames Feist             output["Profiles"] = std::vector<std::string>{profile};
83473df0db0SJames Feist         }
83573df0db0SJames Feist         else
83673df0db0SJames Feist         {
83773df0db0SJames Feist             std::string interface;
83873df0db0SJames Feist             if (type == "StepwiseControllers")
83973df0db0SJames Feist             {
84073df0db0SJames Feist                 interface = stepwiseConfigurationIface;
84173df0db0SJames Feist             }
84273df0db0SJames Feist             else
84373df0db0SJames Feist             {
84473df0db0SJames Feist                 interface = pidConfigurationIface;
84573df0db0SJames Feist             }
84673df0db0SJames Feist             auto findConfig = managedItem->second.find(interface);
84773df0db0SJames Feist             if (findConfig == managedItem->second.end())
84873df0db0SJames Feist             {
84973df0db0SJames Feist                 BMCWEB_LOG_ERROR
85073df0db0SJames Feist                     << "Failed to find interface in managed object";
85173df0db0SJames Feist                 messages::internalError(response->res);
85273df0db0SJames Feist                 return CreatePIDRet::fail;
85373df0db0SJames Feist             }
85473df0db0SJames Feist             auto findProfiles = findConfig->second.find("Profiles");
85573df0db0SJames Feist             if (findProfiles != findConfig->second.end())
85673df0db0SJames Feist             {
85773df0db0SJames Feist                 const std::vector<std::string>* curProfiles =
85873df0db0SJames Feist                     std::get_if<std::vector<std::string>>(
85973df0db0SJames Feist                         &(findProfiles->second));
86073df0db0SJames Feist                 if (curProfiles == nullptr)
86173df0db0SJames Feist                 {
86273df0db0SJames Feist                     BMCWEB_LOG_ERROR << "Illegal profiles in managed object";
86373df0db0SJames Feist                     messages::internalError(response->res);
86473df0db0SJames Feist                     return CreatePIDRet::fail;
86573df0db0SJames Feist                 }
86673df0db0SJames Feist                 if (std::find(curProfiles->begin(), curProfiles->end(),
86773df0db0SJames Feist                               profile) == curProfiles->end())
86873df0db0SJames Feist                 {
86973df0db0SJames Feist                     std::vector<std::string> newProfiles = *curProfiles;
87073df0db0SJames Feist                     newProfiles.push_back(profile);
87173df0db0SJames Feist                     output["Profiles"] = newProfiles;
87273df0db0SJames Feist                 }
87373df0db0SJames Feist             }
87473df0db0SJames Feist         }
87573df0db0SJames Feist     }
87673df0db0SJames Feist 
87783ff9ab6SJames Feist     if (type == "PidControllers" || type == "FanControllers")
87883ff9ab6SJames Feist     {
87983ff9ab6SJames Feist         if (createNewObject)
88083ff9ab6SJames Feist         {
88183ff9ab6SJames Feist             output["Class"] = type == "PidControllers" ? std::string("temp")
88283ff9ab6SJames Feist                                                        : std::string("fan");
88383ff9ab6SJames Feist             output["Type"] = std::string("Pid");
88483ff9ab6SJames Feist         }
8855f2caaefSJames Feist 
8865f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
8875f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
8885f2caaefSJames Feist         std::optional<std::vector<std::string>> outputs;
8895f2caaefSJames Feist         std::map<std::string, std::optional<double>> doubles;
890b943aaefSJames Feist         std::optional<std::string> setpointOffset;
8915f2caaefSJames Feist         if (!redfish::json_util::readJson(
892b6baeaa4SJames Feist                 it.value(), response->res, "Inputs", inputs, "Outputs", outputs,
8935f2caaefSJames Feist                 "Zones", zones, "FFGainCoefficient",
8945f2caaefSJames Feist                 doubles["FFGainCoefficient"], "FFOffCoefficient",
8955f2caaefSJames Feist                 doubles["FFOffCoefficient"], "ICoefficient",
8965f2caaefSJames Feist                 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
8975f2caaefSJames Feist                 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
8985f2caaefSJames Feist                 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
8995f2caaefSJames Feist                 "PCoefficient", doubles["PCoefficient"], "SetPoint",
900b943aaefSJames Feist                 doubles["SetPoint"], "SetPointOffset", setpointOffset,
901b943aaefSJames Feist                 "SlewNeg", doubles["SlewNeg"], "SlewPos", doubles["SlewPos"],
902b943aaefSJames Feist                 "PositiveHysteresis", doubles["PositiveHysteresis"],
903b943aaefSJames Feist                 "NegativeHysteresis", doubles["NegativeHysteresis"]))
90483ff9ab6SJames Feist         {
9055f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
906b6baeaa4SJames Feist                              << it.value().dump();
9075f2caaefSJames Feist             return CreatePIDRet::fail;
90883ff9ab6SJames Feist         }
9095f2caaefSJames Feist         if (zones)
9105f2caaefSJames Feist         {
9115f2caaefSJames Feist             std::vector<std::string> zonesStr;
9125f2caaefSJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStr))
9135f2caaefSJames Feist             {
9145f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Zones";
9155f2caaefSJames Feist                 return CreatePIDRet::fail;
9165f2caaefSJames Feist             }
917b6baeaa4SJames Feist             if (chassis.empty() &&
918b6baeaa4SJames Feist                 !findChassis(managedObj, zonesStr[0], chassis))
919b6baeaa4SJames Feist             {
920b6baeaa4SJames Feist                 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
921b6baeaa4SJames Feist                 messages::invalidObject(response->res, it.key());
922b6baeaa4SJames Feist                 return CreatePIDRet::fail;
923b6baeaa4SJames Feist             }
924b6baeaa4SJames Feist 
9255f2caaefSJames Feist             output["Zones"] = std::move(zonesStr);
9265f2caaefSJames Feist         }
9275f2caaefSJames Feist         if (inputs || outputs)
9285f2caaefSJames Feist         {
9295f2caaefSJames Feist             std::array<std::optional<std::vector<std::string>>*, 2> containers =
9305f2caaefSJames Feist                 {&inputs, &outputs};
9315f2caaefSJames Feist             size_t index = 0;
9325f2caaefSJames Feist             for (const auto& containerPtr : containers)
9335f2caaefSJames Feist             {
9345f2caaefSJames Feist                 std::optional<std::vector<std::string>>& container =
9355f2caaefSJames Feist                     *containerPtr;
9365f2caaefSJames Feist                 if (!container)
9375f2caaefSJames Feist                 {
9385f2caaefSJames Feist                     index++;
9395f2caaefSJames Feist                     continue;
94083ff9ab6SJames Feist                 }
94183ff9ab6SJames Feist 
9425f2caaefSJames Feist                 for (std::string& value : *container)
94383ff9ab6SJames Feist                 {
9445f2caaefSJames Feist                     boost::replace_all(value, "_", " ");
94583ff9ab6SJames Feist                 }
9465f2caaefSJames Feist                 std::string key;
9475f2caaefSJames Feist                 if (index == 0)
9485f2caaefSJames Feist                 {
9495f2caaefSJames Feist                     key = "Inputs";
9505f2caaefSJames Feist                 }
9515f2caaefSJames Feist                 else
9525f2caaefSJames Feist                 {
9535f2caaefSJames Feist                     key = "Outputs";
9545f2caaefSJames Feist                 }
9555f2caaefSJames Feist                 output[key] = *container;
9565f2caaefSJames Feist                 index++;
9575f2caaefSJames Feist             }
95883ff9ab6SJames Feist         }
95983ff9ab6SJames Feist 
960b943aaefSJames Feist         if (setpointOffset)
961b943aaefSJames Feist         {
962b943aaefSJames Feist             // translate between redfish and dbus names
963b943aaefSJames Feist             if (*setpointOffset == "UpperThresholdNonCritical")
964b943aaefSJames Feist             {
965b943aaefSJames Feist                 output["SetPointOffset"] = std::string("WarningLow");
966b943aaefSJames Feist             }
967b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdNonCritical")
968b943aaefSJames Feist             {
969b943aaefSJames Feist                 output["SetPointOffset"] = std::string("WarningHigh");
970b943aaefSJames Feist             }
971b943aaefSJames Feist             else if (*setpointOffset == "LowerThresholdCritical")
972b943aaefSJames Feist             {
973b943aaefSJames Feist                 output["SetPointOffset"] = std::string("CriticalLow");
974b943aaefSJames Feist             }
975b943aaefSJames Feist             else if (*setpointOffset == "UpperThresholdCritical")
976b943aaefSJames Feist             {
977b943aaefSJames Feist                 output["SetPointOffset"] = std::string("CriticalHigh");
978b943aaefSJames Feist             }
979b943aaefSJames Feist             else
980b943aaefSJames Feist             {
981b943aaefSJames Feist                 BMCWEB_LOG_ERROR << "Invalid setpointoffset "
982b943aaefSJames Feist                                  << *setpointOffset;
983b943aaefSJames Feist                 messages::invalidObject(response->res, it.key());
984b943aaefSJames Feist                 return CreatePIDRet::fail;
985b943aaefSJames Feist             }
986b943aaefSJames Feist         }
987b943aaefSJames Feist 
98883ff9ab6SJames Feist         // doubles
9895f2caaefSJames Feist         for (const auto& pairs : doubles)
99083ff9ab6SJames Feist         {
9915f2caaefSJames Feist             if (!pairs.second)
99283ff9ab6SJames Feist             {
9935f2caaefSJames Feist                 continue;
99483ff9ab6SJames Feist             }
9955f2caaefSJames Feist             BMCWEB_LOG_DEBUG << pairs.first << " = " << *pairs.second;
9965f2caaefSJames Feist             output[pairs.first] = *(pairs.second);
9975f2caaefSJames Feist         }
99883ff9ab6SJames Feist     }
99983ff9ab6SJames Feist 
100083ff9ab6SJames Feist     else if (type == "FanZones")
100183ff9ab6SJames Feist     {
100283ff9ab6SJames Feist         output["Type"] = std::string("Pid.Zone");
100383ff9ab6SJames Feist 
10045f2caaefSJames Feist         std::optional<nlohmann::json> chassisContainer;
10055f2caaefSJames Feist         std::optional<double> failSafePercent;
1006d3ec07f8SJames Feist         std::optional<double> minThermalOutput;
1007b6baeaa4SJames Feist         if (!redfish::json_util::readJson(it.value(), response->res, "Chassis",
10085f2caaefSJames Feist                                           chassisContainer, "FailSafePercent",
1009d3ec07f8SJames Feist                                           failSafePercent, "MinThermalOutput",
1010d3ec07f8SJames Feist                                           minThermalOutput))
101183ff9ab6SJames Feist         {
10125f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
1013b6baeaa4SJames Feist                              << it.value().dump();
101483ff9ab6SJames Feist             return CreatePIDRet::fail;
101583ff9ab6SJames Feist         }
10165f2caaefSJames Feist 
10175f2caaefSJames Feist         if (chassisContainer)
101883ff9ab6SJames Feist         {
10195f2caaefSJames Feist 
10205f2caaefSJames Feist             std::string chassisId;
10215f2caaefSJames Feist             if (!redfish::json_util::readJson(*chassisContainer, response->res,
10225f2caaefSJames Feist                                               "@odata.id", chassisId))
10235f2caaefSJames Feist             {
10245f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
10255f2caaefSJames Feist                                  << chassisContainer->dump();
102683ff9ab6SJames Feist                 return CreatePIDRet::fail;
102783ff9ab6SJames Feist             }
102883ff9ab6SJames Feist 
1029717794d5SAppaRao Puli             // /redfish/v1/chassis/chassis_name/
10305f2caaefSJames Feist             if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis))
103183ff9ab6SJames Feist             {
10325f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Got invalid path " << chassisId;
10335f2caaefSJames Feist                 messages::invalidObject(response->res, chassisId);
103483ff9ab6SJames Feist                 return CreatePIDRet::fail;
103583ff9ab6SJames Feist             }
103683ff9ab6SJames Feist         }
1037d3ec07f8SJames Feist         if (minThermalOutput)
103883ff9ab6SJames Feist         {
1039d3ec07f8SJames Feist             output["MinThermalOutput"] = *minThermalOutput;
10405f2caaefSJames Feist         }
10415f2caaefSJames Feist         if (failSafePercent)
104283ff9ab6SJames Feist         {
10435f2caaefSJames Feist             output["FailSafePercent"] = *failSafePercent;
10445f2caaefSJames Feist         }
10455f2caaefSJames Feist     }
10465f2caaefSJames Feist     else if (type == "StepwiseControllers")
10475f2caaefSJames Feist     {
10485f2caaefSJames Feist         output["Type"] = std::string("Stepwise");
10495f2caaefSJames Feist 
10505f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> zones;
10515f2caaefSJames Feist         std::optional<std::vector<nlohmann::json>> steps;
10525f2caaefSJames Feist         std::optional<std::vector<std::string>> inputs;
10535f2caaefSJames Feist         std::optional<double> positiveHysteresis;
10545f2caaefSJames Feist         std::optional<double> negativeHysteresis;
1055c33a90ecSJames Feist         std::optional<std::string> direction; // upper clipping curve vs lower
10565f2caaefSJames Feist         if (!redfish::json_util::readJson(
1057b6baeaa4SJames Feist                 it.value(), response->res, "Zones", zones, "Steps", steps,
1058b6baeaa4SJames Feist                 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
1059c33a90ecSJames Feist                 "NegativeHysteresis", negativeHysteresis, "Direction",
1060c33a90ecSJames Feist                 direction))
10615f2caaefSJames Feist         {
10625f2caaefSJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
1063b6baeaa4SJames Feist                              << it.value().dump();
106483ff9ab6SJames Feist             return CreatePIDRet::fail;
106583ff9ab6SJames Feist         }
10665f2caaefSJames Feist 
10675f2caaefSJames Feist         if (zones)
106883ff9ab6SJames Feist         {
1069b6baeaa4SJames Feist             std::vector<std::string> zonesStrs;
1070b6baeaa4SJames Feist             if (!getZonesFromJsonReq(response, *zones, zonesStrs))
10715f2caaefSJames Feist             {
10725f2caaefSJames Feist                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Zones";
107383ff9ab6SJames Feist                 return CreatePIDRet::fail;
107483ff9ab6SJames Feist             }
1075b6baeaa4SJames Feist             if (chassis.empty() &&
1076b6baeaa4SJames Feist                 !findChassis(managedObj, zonesStrs[0], chassis))
1077b6baeaa4SJames Feist             {
1078b6baeaa4SJames Feist                 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
1079b6baeaa4SJames Feist                 messages::invalidObject(response->res, it.key());
1080b6baeaa4SJames Feist                 return CreatePIDRet::fail;
1081b6baeaa4SJames Feist             }
1082b6baeaa4SJames Feist             output["Zones"] = std::move(zonesStrs);
10835f2caaefSJames Feist         }
10845f2caaefSJames Feist         if (steps)
10855f2caaefSJames Feist         {
10865f2caaefSJames Feist             std::vector<double> readings;
10875f2caaefSJames Feist             std::vector<double> outputs;
10885f2caaefSJames Feist             for (auto& step : *steps)
10895f2caaefSJames Feist             {
10905f2caaefSJames Feist                 double target;
109123a21a1cSEd Tanous                 double out;
10925f2caaefSJames Feist 
10935f2caaefSJames Feist                 if (!redfish::json_util::readJson(step, response->res, "Target",
109423a21a1cSEd Tanous                                                   target, "Output", out))
10955f2caaefSJames Feist                 {
10965f2caaefSJames Feist                     BMCWEB_LOG_ERROR << "Line:" << __LINE__
1097b6baeaa4SJames Feist                                      << ", Illegal Property "
1098b6baeaa4SJames Feist                                      << it.value().dump();
10995f2caaefSJames Feist                     return CreatePIDRet::fail;
11005f2caaefSJames Feist                 }
11015f2caaefSJames Feist                 readings.emplace_back(target);
110223a21a1cSEd Tanous                 outputs.emplace_back(out);
11035f2caaefSJames Feist             }
11045f2caaefSJames Feist             output["Reading"] = std::move(readings);
11055f2caaefSJames Feist             output["Output"] = std::move(outputs);
11065f2caaefSJames Feist         }
11075f2caaefSJames Feist         if (inputs)
11085f2caaefSJames Feist         {
11095f2caaefSJames Feist             for (std::string& value : *inputs)
11105f2caaefSJames Feist             {
11115f2caaefSJames Feist                 boost::replace_all(value, "_", " ");
11125f2caaefSJames Feist             }
11135f2caaefSJames Feist             output["Inputs"] = std::move(*inputs);
11145f2caaefSJames Feist         }
11155f2caaefSJames Feist         if (negativeHysteresis)
11165f2caaefSJames Feist         {
11175f2caaefSJames Feist             output["NegativeHysteresis"] = *negativeHysteresis;
11185f2caaefSJames Feist         }
11195f2caaefSJames Feist         if (positiveHysteresis)
11205f2caaefSJames Feist         {
11215f2caaefSJames Feist             output["PositiveHysteresis"] = *positiveHysteresis;
112283ff9ab6SJames Feist         }
1123c33a90ecSJames Feist         if (direction)
1124c33a90ecSJames Feist         {
1125c33a90ecSJames Feist             constexpr const std::array<const char*, 2> allowedDirections = {
1126c33a90ecSJames Feist                 "Ceiling", "Floor"};
1127c33a90ecSJames Feist             if (std::find(allowedDirections.begin(), allowedDirections.end(),
1128c33a90ecSJames Feist                           *direction) == allowedDirections.end())
1129c33a90ecSJames Feist             {
1130c33a90ecSJames Feist                 messages::propertyValueTypeError(response->res, "Direction",
1131c33a90ecSJames Feist                                                  *direction);
1132c33a90ecSJames Feist                 return CreatePIDRet::fail;
1133c33a90ecSJames Feist             }
1134c33a90ecSJames Feist             output["Class"] = *direction;
1135c33a90ecSJames Feist         }
113683ff9ab6SJames Feist     }
113783ff9ab6SJames Feist     else
113883ff9ab6SJames Feist     {
11395f2caaefSJames Feist         BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Type " << type;
114035a62c7cSJason M. Bills         messages::propertyUnknown(response->res, type);
114183ff9ab6SJames Feist         return CreatePIDRet::fail;
114283ff9ab6SJames Feist     }
114383ff9ab6SJames Feist     return CreatePIDRet::patch;
114483ff9ab6SJames Feist }
114573df0db0SJames Feist struct GetPIDValues : std::enable_shared_from_this<GetPIDValues>
114673df0db0SJames Feist {
114783ff9ab6SJames Feist 
114823a21a1cSEd Tanous     GetPIDValues(const std::shared_ptr<AsyncResp>& asyncRespIn) :
114923a21a1cSEd Tanous         asyncResp(asyncRespIn)
115073df0db0SJames Feist 
11511214b7e7SGunnar Mills     {}
11529c310685SBorawski.Lukasz 
115373df0db0SJames Feist     void run()
11545b4aa86bSJames Feist     {
115573df0db0SJames Feist         std::shared_ptr<GetPIDValues> self = shared_from_this();
115673df0db0SJames Feist 
115773df0db0SJames Feist         // get all configurations
11585b4aa86bSJames Feist         crow::connections::systemBus->async_method_call(
115973df0db0SJames Feist             [self](const boost::system::error_code ec,
116023a21a1cSEd Tanous                    const crow::openbmc_mapper::GetSubTreeType& subtreeLocal) {
11615b4aa86bSJames Feist                 if (ec)
11625b4aa86bSJames Feist                 {
11635b4aa86bSJames Feist                     BMCWEB_LOG_ERROR << ec;
116473df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
116573df0db0SJames Feist                     return;
116673df0db0SJames Feist                 }
116723a21a1cSEd Tanous                 self->subtree = subtreeLocal;
116873df0db0SJames Feist             },
116973df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper",
117073df0db0SJames Feist             "/xyz/openbmc_project/object_mapper",
117173df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
117273df0db0SJames Feist             std::array<const char*, 4>{
117373df0db0SJames Feist                 pidConfigurationIface, pidZoneConfigurationIface,
117473df0db0SJames Feist                 objectManagerIface, stepwiseConfigurationIface});
117573df0db0SJames Feist 
117673df0db0SJames Feist         // at the same time get the selected profile
117773df0db0SJames Feist         crow::connections::systemBus->async_method_call(
117873df0db0SJames Feist             [self](const boost::system::error_code ec,
117923a21a1cSEd Tanous                    const crow::openbmc_mapper::GetSubTreeType& subtreeLocal) {
118023a21a1cSEd Tanous                 if (ec || subtreeLocal.empty())
118173df0db0SJames Feist                 {
118273df0db0SJames Feist                     return;
118373df0db0SJames Feist                 }
118423a21a1cSEd Tanous                 if (subtreeLocal[0].second.size() != 1)
118573df0db0SJames Feist                 {
118673df0db0SJames Feist                     // invalid mapper response, should never happen
118773df0db0SJames Feist                     BMCWEB_LOG_ERROR << "GetPIDValues: Mapper Error";
118873df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
11895b4aa86bSJames Feist                     return;
11905b4aa86bSJames Feist                 }
11915b4aa86bSJames Feist 
119223a21a1cSEd Tanous                 const std::string& path = subtreeLocal[0].first;
119323a21a1cSEd Tanous                 const std::string& owner = subtreeLocal[0].second[0].first;
119473df0db0SJames Feist                 crow::connections::systemBus->async_method_call(
119573df0db0SJames Feist                     [path, owner, self](
119623a21a1cSEd Tanous                         const boost::system::error_code ec2,
119773df0db0SJames Feist                         const boost::container::flat_map<
119873df0db0SJames Feist                             std::string, std::variant<std::vector<std::string>,
119973df0db0SJames Feist                                                       std::string>>& resp) {
120023a21a1cSEd Tanous                         if (ec2)
120173df0db0SJames Feist                         {
120273df0db0SJames Feist                             BMCWEB_LOG_ERROR << "GetPIDValues: Can't get "
120373df0db0SJames Feist                                                 "thermalModeIface "
120473df0db0SJames Feist                                              << path;
120573df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
120673df0db0SJames Feist                             return;
120773df0db0SJames Feist                         }
1208271584abSEd Tanous                         const std::string* current = nullptr;
1209271584abSEd Tanous                         const std::vector<std::string>* supported = nullptr;
121073df0db0SJames Feist                         for (auto& [key, value] : resp)
121173df0db0SJames Feist                         {
121273df0db0SJames Feist                             if (key == "Current")
121373df0db0SJames Feist                             {
121473df0db0SJames Feist                                 current = std::get_if<std::string>(&value);
121573df0db0SJames Feist                                 if (current == nullptr)
121673df0db0SJames Feist                                 {
121773df0db0SJames Feist                                     BMCWEB_LOG_ERROR
121873df0db0SJames Feist                                         << "GetPIDValues: thermal mode "
121973df0db0SJames Feist                                            "iface invalid "
122073df0db0SJames Feist                                         << path;
122173df0db0SJames Feist                                     messages::internalError(
122273df0db0SJames Feist                                         self->asyncResp->res);
122373df0db0SJames Feist                                     return;
122473df0db0SJames Feist                                 }
122573df0db0SJames Feist                             }
122673df0db0SJames Feist                             if (key == "Supported")
122773df0db0SJames Feist                             {
122873df0db0SJames Feist                                 supported =
122973df0db0SJames Feist                                     std::get_if<std::vector<std::string>>(
123073df0db0SJames Feist                                         &value);
123173df0db0SJames Feist                                 if (supported == nullptr)
123273df0db0SJames Feist                                 {
123373df0db0SJames Feist                                     BMCWEB_LOG_ERROR
123473df0db0SJames Feist                                         << "GetPIDValues: thermal mode "
123573df0db0SJames Feist                                            "iface invalid"
123673df0db0SJames Feist                                         << path;
123773df0db0SJames Feist                                     messages::internalError(
123873df0db0SJames Feist                                         self->asyncResp->res);
123973df0db0SJames Feist                                     return;
124073df0db0SJames Feist                                 }
124173df0db0SJames Feist                             }
124273df0db0SJames Feist                         }
124373df0db0SJames Feist                         if (current == nullptr || supported == nullptr)
124473df0db0SJames Feist                         {
124573df0db0SJames Feist                             BMCWEB_LOG_ERROR << "GetPIDValues: thermal mode "
124673df0db0SJames Feist                                                 "iface invalid "
124773df0db0SJames Feist                                              << path;
124873df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
124973df0db0SJames Feist                             return;
125073df0db0SJames Feist                         }
125173df0db0SJames Feist                         self->currentProfile = *current;
125273df0db0SJames Feist                         self->supportedProfiles = *supported;
125373df0db0SJames Feist                     },
125473df0db0SJames Feist                     owner, path, "org.freedesktop.DBus.Properties", "GetAll",
125573df0db0SJames Feist                     thermalModeIface);
125673df0db0SJames Feist             },
125773df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper",
125873df0db0SJames Feist             "/xyz/openbmc_project/object_mapper",
125973df0db0SJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
126073df0db0SJames Feist             std::array<const char*, 1>{thermalModeIface});
126173df0db0SJames Feist     }
126273df0db0SJames Feist 
126373df0db0SJames Feist     ~GetPIDValues()
126473df0db0SJames Feist     {
126573df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
126673df0db0SJames Feist         {
126773df0db0SJames Feist             return;
126873df0db0SJames Feist         }
12695b4aa86bSJames Feist         // create map of <connection, path to objMgr>>
127073df0db0SJames Feist         boost::container::flat_map<std::string, std::string> objectMgrPaths;
12716bce33bcSJames Feist         boost::container::flat_set<std::string> calledConnections;
12725b4aa86bSJames Feist         for (const auto& pathGroup : subtree)
12735b4aa86bSJames Feist         {
12745b4aa86bSJames Feist             for (const auto& connectionGroup : pathGroup.second)
12755b4aa86bSJames Feist             {
12766bce33bcSJames Feist                 auto findConnection =
12776bce33bcSJames Feist                     calledConnections.find(connectionGroup.first);
12786bce33bcSJames Feist                 if (findConnection != calledConnections.end())
12796bce33bcSJames Feist                 {
12806bce33bcSJames Feist                     break;
12816bce33bcSJames Feist                 }
128273df0db0SJames Feist                 for (const std::string& interface : connectionGroup.second)
12835b4aa86bSJames Feist                 {
12845b4aa86bSJames Feist                     if (interface == objectManagerIface)
12855b4aa86bSJames Feist                     {
128673df0db0SJames Feist                         objectMgrPaths[connectionGroup.first] = pathGroup.first;
12875b4aa86bSJames Feist                     }
12885b4aa86bSJames Feist                     // this list is alphabetical, so we
12895b4aa86bSJames Feist                     // should have found the objMgr by now
12905b4aa86bSJames Feist                     if (interface == pidConfigurationIface ||
1291b7a08d04SJames Feist                         interface == pidZoneConfigurationIface ||
1292b7a08d04SJames Feist                         interface == stepwiseConfigurationIface)
12935b4aa86bSJames Feist                     {
12945b4aa86bSJames Feist                         auto findObjMgr =
12955b4aa86bSJames Feist                             objectMgrPaths.find(connectionGroup.first);
12965b4aa86bSJames Feist                         if (findObjMgr == objectMgrPaths.end())
12975b4aa86bSJames Feist                         {
12985b4aa86bSJames Feist                             BMCWEB_LOG_DEBUG << connectionGroup.first
12995b4aa86bSJames Feist                                              << "Has no Object Manager";
13005b4aa86bSJames Feist                             continue;
13015b4aa86bSJames Feist                         }
13026bce33bcSJames Feist 
13036bce33bcSJames Feist                         calledConnections.insert(connectionGroup.first);
13046bce33bcSJames Feist 
130573df0db0SJames Feist                         asyncPopulatePid(findObjMgr->first, findObjMgr->second,
130673df0db0SJames Feist                                          currentProfile, supportedProfiles,
130773df0db0SJames Feist                                          asyncResp);
13085b4aa86bSJames Feist                         break;
13095b4aa86bSJames Feist                     }
13105b4aa86bSJames Feist                 }
13115b4aa86bSJames Feist             }
13125b4aa86bSJames Feist         }
131373df0db0SJames Feist     }
131473df0db0SJames Feist 
131573df0db0SJames Feist     std::vector<std::string> supportedProfiles;
131673df0db0SJames Feist     std::string currentProfile;
131773df0db0SJames Feist     crow::openbmc_mapper::GetSubTreeType subtree;
131873df0db0SJames Feist     std::shared_ptr<AsyncResp> asyncResp;
131973df0db0SJames Feist };
132073df0db0SJames Feist 
132173df0db0SJames Feist struct SetPIDValues : std::enable_shared_from_this<SetPIDValues>
132273df0db0SJames Feist {
132373df0db0SJames Feist 
1324271584abSEd Tanous     SetPIDValues(const std::shared_ptr<AsyncResp>& asyncRespIn,
132573df0db0SJames Feist                  nlohmann::json& data) :
1326271584abSEd Tanous         asyncResp(asyncRespIn)
132773df0db0SJames Feist     {
132873df0db0SJames Feist 
132973df0db0SJames Feist         std::optional<nlohmann::json> pidControllers;
133073df0db0SJames Feist         std::optional<nlohmann::json> fanControllers;
133173df0db0SJames Feist         std::optional<nlohmann::json> fanZones;
133273df0db0SJames Feist         std::optional<nlohmann::json> stepwiseControllers;
133373df0db0SJames Feist 
133473df0db0SJames Feist         if (!redfish::json_util::readJson(
133573df0db0SJames Feist                 data, asyncResp->res, "PidControllers", pidControllers,
133673df0db0SJames Feist                 "FanControllers", fanControllers, "FanZones", fanZones,
133773df0db0SJames Feist                 "StepwiseControllers", stepwiseControllers, "Profile", profile))
133873df0db0SJames Feist         {
133973df0db0SJames Feist             BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
134073df0db0SJames Feist                              << data.dump();
134173df0db0SJames Feist             return;
134273df0db0SJames Feist         }
134373df0db0SJames Feist         configuration.emplace_back("PidControllers", std::move(pidControllers));
134473df0db0SJames Feist         configuration.emplace_back("FanControllers", std::move(fanControllers));
134573df0db0SJames Feist         configuration.emplace_back("FanZones", std::move(fanZones));
134673df0db0SJames Feist         configuration.emplace_back("StepwiseControllers",
134773df0db0SJames Feist                                    std::move(stepwiseControllers));
134873df0db0SJames Feist     }
134973df0db0SJames Feist     void run()
135073df0db0SJames Feist     {
135173df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
135273df0db0SJames Feist         {
135373df0db0SJames Feist             return;
135473df0db0SJames Feist         }
135573df0db0SJames Feist 
135673df0db0SJames Feist         std::shared_ptr<SetPIDValues> self = shared_from_this();
135773df0db0SJames Feist 
135873df0db0SJames Feist         // todo(james): might make sense to do a mapper call here if this
135973df0db0SJames Feist         // interface gets more traction
136073df0db0SJames Feist         crow::connections::systemBus->async_method_call(
136173df0db0SJames Feist             [self](const boost::system::error_code ec,
1362271584abSEd Tanous                    dbus::utility::ManagedObjectType& mObj) {
136373df0db0SJames Feist                 if (ec)
136473df0db0SJames Feist                 {
136573df0db0SJames Feist                     BMCWEB_LOG_ERROR << "Error communicating to Entity Manager";
136673df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
136773df0db0SJames Feist                     return;
136873df0db0SJames Feist                 }
1369e69d9de2SJames Feist                 const std::array<const char*, 3> configurations = {
1370e69d9de2SJames Feist                     pidConfigurationIface, pidZoneConfigurationIface,
1371e69d9de2SJames Feist                     stepwiseConfigurationIface};
1372e69d9de2SJames Feist 
137314b0b8d5SJames Feist                 for (const auto& [path, object] : mObj)
1374e69d9de2SJames Feist                 {
137514b0b8d5SJames Feist                     for (const auto& [interface, _] : object)
1376e69d9de2SJames Feist                     {
1377e69d9de2SJames Feist                         if (std::find(configurations.begin(),
1378e69d9de2SJames Feist                                       configurations.end(),
1379e69d9de2SJames Feist                                       interface) != configurations.end())
1380e69d9de2SJames Feist                         {
138114b0b8d5SJames Feist                             self->objectCount++;
1382e69d9de2SJames Feist                             break;
1383e69d9de2SJames Feist                         }
1384e69d9de2SJames Feist                     }
1385e69d9de2SJames Feist                 }
1386271584abSEd Tanous                 self->managedObj = std::move(mObj);
138773df0db0SJames Feist             },
138873df0db0SJames Feist             "xyz.openbmc_project.EntityManager", "/", objectManagerIface,
138973df0db0SJames Feist             "GetManagedObjects");
139073df0db0SJames Feist 
139173df0db0SJames Feist         // at the same time get the profile information
139273df0db0SJames Feist         crow::connections::systemBus->async_method_call(
139373df0db0SJames Feist             [self](const boost::system::error_code ec,
139473df0db0SJames Feist                    const crow::openbmc_mapper::GetSubTreeType& subtree) {
139573df0db0SJames Feist                 if (ec || subtree.empty())
139673df0db0SJames Feist                 {
139773df0db0SJames Feist                     return;
139873df0db0SJames Feist                 }
139973df0db0SJames Feist                 if (subtree[0].second.empty())
140073df0db0SJames Feist                 {
140173df0db0SJames Feist                     // invalid mapper response, should never happen
140273df0db0SJames Feist                     BMCWEB_LOG_ERROR << "SetPIDValues: Mapper Error";
140373df0db0SJames Feist                     messages::internalError(self->asyncResp->res);
140473df0db0SJames Feist                     return;
140573df0db0SJames Feist                 }
140673df0db0SJames Feist 
140773df0db0SJames Feist                 const std::string& path = subtree[0].first;
140873df0db0SJames Feist                 const std::string& owner = subtree[0].second[0].first;
140973df0db0SJames Feist                 crow::connections::systemBus->async_method_call(
141073df0db0SJames Feist                     [self, path, owner](
1411cb13a392SEd Tanous                         const boost::system::error_code ec2,
141273df0db0SJames Feist                         const boost::container::flat_map<
141373df0db0SJames Feist                             std::string, std::variant<std::vector<std::string>,
1414271584abSEd Tanous                                                       std::string>>& r) {
1415cb13a392SEd Tanous                         if (ec2)
141673df0db0SJames Feist                         {
141773df0db0SJames Feist                             BMCWEB_LOG_ERROR << "SetPIDValues: Can't get "
141873df0db0SJames Feist                                                 "thermalModeIface "
141973df0db0SJames Feist                                              << path;
142073df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
142173df0db0SJames Feist                             return;
142273df0db0SJames Feist                         }
1423271584abSEd Tanous                         const std::string* current = nullptr;
1424271584abSEd Tanous                         const std::vector<std::string>* supported = nullptr;
1425271584abSEd Tanous                         for (auto& [key, value] : r)
142673df0db0SJames Feist                         {
142773df0db0SJames Feist                             if (key == "Current")
142873df0db0SJames Feist                             {
142973df0db0SJames Feist                                 current = std::get_if<std::string>(&value);
143073df0db0SJames Feist                                 if (current == nullptr)
143173df0db0SJames Feist                                 {
143273df0db0SJames Feist                                     BMCWEB_LOG_ERROR
143373df0db0SJames Feist                                         << "SetPIDValues: thermal mode "
143473df0db0SJames Feist                                            "iface invalid "
143573df0db0SJames Feist                                         << path;
143673df0db0SJames Feist                                     messages::internalError(
143773df0db0SJames Feist                                         self->asyncResp->res);
143873df0db0SJames Feist                                     return;
143973df0db0SJames Feist                                 }
144073df0db0SJames Feist                             }
144173df0db0SJames Feist                             if (key == "Supported")
144273df0db0SJames Feist                             {
144373df0db0SJames Feist                                 supported =
144473df0db0SJames Feist                                     std::get_if<std::vector<std::string>>(
144573df0db0SJames Feist                                         &value);
144673df0db0SJames Feist                                 if (supported == nullptr)
144773df0db0SJames Feist                                 {
144873df0db0SJames Feist                                     BMCWEB_LOG_ERROR
144973df0db0SJames Feist                                         << "SetPIDValues: thermal mode "
145073df0db0SJames Feist                                            "iface invalid"
145173df0db0SJames Feist                                         << path;
145273df0db0SJames Feist                                     messages::internalError(
145373df0db0SJames Feist                                         self->asyncResp->res);
145473df0db0SJames Feist                                     return;
145573df0db0SJames Feist                                 }
145673df0db0SJames Feist                             }
145773df0db0SJames Feist                         }
145873df0db0SJames Feist                         if (current == nullptr || supported == nullptr)
145973df0db0SJames Feist                         {
146073df0db0SJames Feist                             BMCWEB_LOG_ERROR << "SetPIDValues: thermal mode "
146173df0db0SJames Feist                                                 "iface invalid "
146273df0db0SJames Feist                                              << path;
146373df0db0SJames Feist                             messages::internalError(self->asyncResp->res);
146473df0db0SJames Feist                             return;
146573df0db0SJames Feist                         }
146673df0db0SJames Feist                         self->currentProfile = *current;
146773df0db0SJames Feist                         self->supportedProfiles = *supported;
146873df0db0SJames Feist                         self->profileConnection = owner;
146973df0db0SJames Feist                         self->profilePath = path;
147073df0db0SJames Feist                     },
147173df0db0SJames Feist                     owner, path, "org.freedesktop.DBus.Properties", "GetAll",
147273df0db0SJames Feist                     thermalModeIface);
14735b4aa86bSJames Feist             },
14745b4aa86bSJames Feist             "xyz.openbmc_project.ObjectMapper",
14755b4aa86bSJames Feist             "/xyz/openbmc_project/object_mapper",
14765b4aa86bSJames Feist             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
147773df0db0SJames Feist             std::array<const char*, 1>{thermalModeIface});
147873df0db0SJames Feist     }
147973df0db0SJames Feist     ~SetPIDValues()
148073df0db0SJames Feist     {
148173df0db0SJames Feist         if (asyncResp->res.result() != boost::beast::http::status::ok)
148273df0db0SJames Feist         {
148373df0db0SJames Feist             return;
14845b4aa86bSJames Feist         }
14855b4aa86bSJames Feist 
148673df0db0SJames Feist         std::shared_ptr<AsyncResp> response = asyncResp;
148773df0db0SJames Feist 
148873df0db0SJames Feist         if (profile)
148973df0db0SJames Feist         {
149073df0db0SJames Feist             if (std::find(supportedProfiles.begin(), supportedProfiles.end(),
149173df0db0SJames Feist                           *profile) == supportedProfiles.end())
149273df0db0SJames Feist             {
149373df0db0SJames Feist                 messages::actionParameterUnknown(response->res, "Profile",
149473df0db0SJames Feist                                                  *profile);
149573df0db0SJames Feist                 return;
149673df0db0SJames Feist             }
149773df0db0SJames Feist             currentProfile = *profile;
149873df0db0SJames Feist             crow::connections::systemBus->async_method_call(
149973df0db0SJames Feist                 [response](const boost::system::error_code ec) {
150073df0db0SJames Feist                     if (ec)
150173df0db0SJames Feist                     {
150273df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Error patching profile" << ec;
150373df0db0SJames Feist                         messages::internalError(response->res);
150473df0db0SJames Feist                     }
150573df0db0SJames Feist                 },
150673df0db0SJames Feist                 profileConnection, profilePath,
150773df0db0SJames Feist                 "org.freedesktop.DBus.Properties", "Set", thermalModeIface,
150873df0db0SJames Feist                 "Current", std::variant<std::string>(*profile));
150973df0db0SJames Feist         }
151073df0db0SJames Feist 
151173df0db0SJames Feist         for (auto& containerPair : configuration)
151273df0db0SJames Feist         {
151373df0db0SJames Feist             auto& container = containerPair.second;
151473df0db0SJames Feist             if (!container)
151573df0db0SJames Feist             {
151673df0db0SJames Feist                 continue;
151773df0db0SJames Feist             }
15186ee7f774SJames Feist             BMCWEB_LOG_DEBUG << *container;
15196ee7f774SJames Feist 
152073df0db0SJames Feist             std::string& type = containerPair.first;
152173df0db0SJames Feist 
152273df0db0SJames Feist             for (nlohmann::json::iterator it = container->begin();
152373df0db0SJames Feist                  it != container->end(); it++)
152473df0db0SJames Feist             {
152573df0db0SJames Feist                 const auto& name = it.key();
15266ee7f774SJames Feist                 BMCWEB_LOG_DEBUG << "looking for " << name;
15276ee7f774SJames Feist 
152873df0db0SJames Feist                 auto pathItr =
152973df0db0SJames Feist                     std::find_if(managedObj.begin(), managedObj.end(),
153073df0db0SJames Feist                                  [&name](const auto& obj) {
153173df0db0SJames Feist                                      return boost::algorithm::ends_with(
153273df0db0SJames Feist                                          obj.first.str, "/" + name);
153373df0db0SJames Feist                                  });
153473df0db0SJames Feist                 boost::container::flat_map<std::string,
153573df0db0SJames Feist                                            dbus::utility::DbusVariantType>
153673df0db0SJames Feist                     output;
153773df0db0SJames Feist 
153873df0db0SJames Feist                 output.reserve(16); // The pid interface length
153973df0db0SJames Feist 
154073df0db0SJames Feist                 // determines if we're patching entity-manager or
154173df0db0SJames Feist                 // creating a new object
154273df0db0SJames Feist                 bool createNewObject = (pathItr == managedObj.end());
15436ee7f774SJames Feist                 BMCWEB_LOG_DEBUG << "Found = " << !createNewObject;
15446ee7f774SJames Feist 
154573df0db0SJames Feist                 std::string iface;
154673df0db0SJames Feist                 if (type == "PidControllers" || type == "FanControllers")
154773df0db0SJames Feist                 {
154873df0db0SJames Feist                     iface = pidConfigurationIface;
154973df0db0SJames Feist                     if (!createNewObject &&
155073df0db0SJames Feist                         pathItr->second.find(pidConfigurationIface) ==
155173df0db0SJames Feist                             pathItr->second.end())
155273df0db0SJames Feist                     {
155373df0db0SJames Feist                         createNewObject = true;
155473df0db0SJames Feist                     }
155573df0db0SJames Feist                 }
155673df0db0SJames Feist                 else if (type == "FanZones")
155773df0db0SJames Feist                 {
155873df0db0SJames Feist                     iface = pidZoneConfigurationIface;
155973df0db0SJames Feist                     if (!createNewObject &&
156073df0db0SJames Feist                         pathItr->second.find(pidZoneConfigurationIface) ==
156173df0db0SJames Feist                             pathItr->second.end())
156273df0db0SJames Feist                     {
156373df0db0SJames Feist 
156473df0db0SJames Feist                         createNewObject = true;
156573df0db0SJames Feist                     }
156673df0db0SJames Feist                 }
156773df0db0SJames Feist                 else if (type == "StepwiseControllers")
156873df0db0SJames Feist                 {
156973df0db0SJames Feist                     iface = stepwiseConfigurationIface;
157073df0db0SJames Feist                     if (!createNewObject &&
157173df0db0SJames Feist                         pathItr->second.find(stepwiseConfigurationIface) ==
157273df0db0SJames Feist                             pathItr->second.end())
157373df0db0SJames Feist                     {
157473df0db0SJames Feist                         createNewObject = true;
157573df0db0SJames Feist                     }
157673df0db0SJames Feist                 }
15776ee7f774SJames Feist 
15786ee7f774SJames Feist                 if (createNewObject && it.value() == nullptr)
15796ee7f774SJames Feist                 {
15804e0453b1SGunnar Mills                     // can't delete a non-existent object
15816ee7f774SJames Feist                     messages::invalidObject(response->res, name);
15826ee7f774SJames Feist                     continue;
15836ee7f774SJames Feist                 }
15846ee7f774SJames Feist 
15856ee7f774SJames Feist                 std::string path;
15866ee7f774SJames Feist                 if (pathItr != managedObj.end())
15876ee7f774SJames Feist                 {
15886ee7f774SJames Feist                     path = pathItr->first.str;
15896ee7f774SJames Feist                 }
15906ee7f774SJames Feist 
159173df0db0SJames Feist                 BMCWEB_LOG_DEBUG << "Create new = " << createNewObject << "\n";
1592e69d9de2SJames Feist 
1593e69d9de2SJames Feist                 // arbitrary limit to avoid attacks
1594e69d9de2SJames Feist                 constexpr const size_t controllerLimit = 500;
159514b0b8d5SJames Feist                 if (createNewObject && objectCount >= controllerLimit)
1596e69d9de2SJames Feist                 {
1597e69d9de2SJames Feist                     messages::resourceExhaustion(response->res, type);
1598e69d9de2SJames Feist                     continue;
1599e69d9de2SJames Feist                 }
1600e69d9de2SJames Feist 
160173df0db0SJames Feist                 output["Name"] = boost::replace_all_copy(name, "_", " ");
160273df0db0SJames Feist 
160373df0db0SJames Feist                 std::string chassis;
160473df0db0SJames Feist                 CreatePIDRet ret = createPidInterface(
16056ee7f774SJames Feist                     response, type, it, path, managedObj, createNewObject,
16066ee7f774SJames Feist                     output, chassis, currentProfile);
160773df0db0SJames Feist                 if (ret == CreatePIDRet::fail)
160873df0db0SJames Feist                 {
160973df0db0SJames Feist                     return;
161073df0db0SJames Feist                 }
16113174e4dfSEd Tanous                 if (ret == CreatePIDRet::del)
161273df0db0SJames Feist                 {
161373df0db0SJames Feist                     continue;
161473df0db0SJames Feist                 }
161573df0db0SJames Feist 
161673df0db0SJames Feist                 if (!createNewObject)
161773df0db0SJames Feist                 {
161873df0db0SJames Feist                     for (const auto& property : output)
161973df0db0SJames Feist                     {
162073df0db0SJames Feist                         crow::connections::systemBus->async_method_call(
162173df0db0SJames Feist                             [response,
162273df0db0SJames Feist                              propertyName{std::string(property.first)}](
162373df0db0SJames Feist                                 const boost::system::error_code ec) {
162473df0db0SJames Feist                                 if (ec)
162573df0db0SJames Feist                                 {
162673df0db0SJames Feist                                     BMCWEB_LOG_ERROR << "Error patching "
162773df0db0SJames Feist                                                      << propertyName << ": "
162873df0db0SJames Feist                                                      << ec;
162973df0db0SJames Feist                                     messages::internalError(response->res);
163073df0db0SJames Feist                                     return;
163173df0db0SJames Feist                                 }
163273df0db0SJames Feist                                 messages::success(response->res);
163373df0db0SJames Feist                             },
16346ee7f774SJames Feist                             "xyz.openbmc_project.EntityManager", path,
163573df0db0SJames Feist                             "org.freedesktop.DBus.Properties", "Set", iface,
163673df0db0SJames Feist                             property.first, property.second);
163773df0db0SJames Feist                     }
163873df0db0SJames Feist                 }
163973df0db0SJames Feist                 else
164073df0db0SJames Feist                 {
164173df0db0SJames Feist                     if (chassis.empty())
164273df0db0SJames Feist                     {
164373df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Failed to get chassis from config";
164473df0db0SJames Feist                         messages::invalidObject(response->res, name);
164573df0db0SJames Feist                         return;
164673df0db0SJames Feist                     }
164773df0db0SJames Feist 
164873df0db0SJames Feist                     bool foundChassis = false;
164973df0db0SJames Feist                     for (const auto& obj : managedObj)
165073df0db0SJames Feist                     {
165173df0db0SJames Feist                         if (boost::algorithm::ends_with(obj.first.str, chassis))
165273df0db0SJames Feist                         {
165373df0db0SJames Feist                             chassis = obj.first.str;
165473df0db0SJames Feist                             foundChassis = true;
165573df0db0SJames Feist                             break;
165673df0db0SJames Feist                         }
165773df0db0SJames Feist                     }
165873df0db0SJames Feist                     if (!foundChassis)
165973df0db0SJames Feist                     {
166073df0db0SJames Feist                         BMCWEB_LOG_ERROR << "Failed to find chassis on dbus";
166173df0db0SJames Feist                         messages::resourceMissingAtURI(
166273df0db0SJames Feist                             response->res, "/redfish/v1/Chassis/" + chassis);
166373df0db0SJames Feist                         return;
166473df0db0SJames Feist                     }
166573df0db0SJames Feist 
166673df0db0SJames Feist                     crow::connections::systemBus->async_method_call(
166773df0db0SJames Feist                         [response](const boost::system::error_code ec) {
166873df0db0SJames Feist                             if (ec)
166973df0db0SJames Feist                             {
167073df0db0SJames Feist                                 BMCWEB_LOG_ERROR << "Error Adding Pid Object "
167173df0db0SJames Feist                                                  << ec;
167273df0db0SJames Feist                                 messages::internalError(response->res);
167373df0db0SJames Feist                                 return;
167473df0db0SJames Feist                             }
167573df0db0SJames Feist                             messages::success(response->res);
167673df0db0SJames Feist                         },
167773df0db0SJames Feist                         "xyz.openbmc_project.EntityManager", chassis,
167873df0db0SJames Feist                         "xyz.openbmc_project.AddObject", "AddObject", output);
167973df0db0SJames Feist                 }
168073df0db0SJames Feist             }
168173df0db0SJames Feist         }
168273df0db0SJames Feist     }
168373df0db0SJames Feist     std::shared_ptr<AsyncResp> asyncResp;
168473df0db0SJames Feist     std::vector<std::pair<std::string, std::optional<nlohmann::json>>>
168573df0db0SJames Feist         configuration;
168673df0db0SJames Feist     std::optional<std::string> profile;
168773df0db0SJames Feist     dbus::utility::ManagedObjectType managedObj;
168873df0db0SJames Feist     std::vector<std::string> supportedProfiles;
168973df0db0SJames Feist     std::string currentProfile;
169073df0db0SJames Feist     std::string profileConnection;
169173df0db0SJames Feist     std::string profilePath;
169214b0b8d5SJames Feist     size_t objectCount = 0;
169373df0db0SJames Feist };
169473df0db0SJames Feist 
169573df0db0SJames Feist class Manager : public Node
169673df0db0SJames Feist {
169773df0db0SJames Feist   public:
169852cc112dSEd Tanous     Manager(App& app) : Node(app, "/redfish/v1/Managers/bmc/")
169973df0db0SJames Feist     {
170052cc112dSEd Tanous 
170152cc112dSEd Tanous         uuid = persistent_data::getConfig().systemUuid;
170273df0db0SJames Feist         entityPrivileges = {
170373df0db0SJames Feist             {boost::beast::http::verb::get, {{"Login"}}},
170473df0db0SJames Feist             {boost::beast::http::verb::head, {{"Login"}}},
170573df0db0SJames Feist             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
170673df0db0SJames Feist             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
170773df0db0SJames Feist             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
170873df0db0SJames Feist             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
170973df0db0SJames Feist     }
171073df0db0SJames Feist 
171173df0db0SJames Feist   private:
1712cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
1713cb13a392SEd Tanous                const std::vector<std::string>&) override
17141abe55efSEd Tanous     {
17150f74e643SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
17164bf2b033SGunnar Mills         res.jsonValue["@odata.type"] = "#Manager.v1_9_0.Manager";
17170f74e643SEd Tanous         res.jsonValue["Id"] = "bmc";
17180f74e643SEd Tanous         res.jsonValue["Name"] = "OpenBmc Manager";
17190f74e643SEd Tanous         res.jsonValue["Description"] = "Baseboard Management Controller";
17200f74e643SEd Tanous         res.jsonValue["PowerState"] = "On";
1721029573d4SEd Tanous         res.jsonValue["Status"] = {{"State", "Enabled"}, {"Health", "OK"}};
17220f74e643SEd Tanous         res.jsonValue["ManagerType"] = "BMC";
17233602e232SEd Tanous         res.jsonValue["UUID"] = systemd_utils::getUuid();
17243602e232SEd Tanous         res.jsonValue["ServiceEntryPointUUID"] = uuid;
17250f74e643SEd Tanous         res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model
17260f74e643SEd Tanous 
17270f74e643SEd Tanous         res.jsonValue["LogServices"] = {
17280f74e643SEd Tanous             {"@odata.id", "/redfish/v1/Managers/bmc/LogServices"}};
17290f74e643SEd Tanous 
17300f74e643SEd Tanous         res.jsonValue["NetworkProtocol"] = {
17310f74e643SEd Tanous             {"@odata.id", "/redfish/v1/Managers/bmc/NetworkProtocol"}};
17320f74e643SEd Tanous 
17330f74e643SEd Tanous         res.jsonValue["EthernetInterfaces"] = {
17340f74e643SEd Tanous             {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces"}};
1735107077deSPrzemyslaw Czarnowski 
1736107077deSPrzemyslaw Czarnowski #ifdef BMCWEB_ENABLE_VM_NBDPROXY
1737107077deSPrzemyslaw Czarnowski         res.jsonValue["VirtualMedia"] = {
1738107077deSPrzemyslaw Czarnowski             {"@odata.id", "/redfish/v1/Managers/bmc/VirtualMedia"}};
1739107077deSPrzemyslaw Czarnowski #endif // BMCWEB_ENABLE_VM_NBDPROXY
1740107077deSPrzemyslaw Czarnowski 
17410f74e643SEd Tanous         // default oem data
17420f74e643SEd Tanous         nlohmann::json& oem = res.jsonValue["Oem"];
17430f74e643SEd Tanous         nlohmann::json& oemOpenbmc = oem["OpenBmc"];
17440f74e643SEd Tanous         oem["@odata.type"] = "#OemManager.Oem";
17450f74e643SEd Tanous         oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
17460f74e643SEd Tanous         oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
17470f74e643SEd Tanous         oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
1748cfcd5f6bSMarri Devender Rao         oemOpenbmc["Certificates"] = {
1749cfcd5f6bSMarri Devender Rao             {"@odata.id", "/redfish/v1/Managers/bmc/Truststore/Certificates"}};
17500f74e643SEd Tanous 
17512a5c4407SGunnar Mills         // Manager.Reset (an action) can be many values, OpenBMC only supports
17522a5c4407SGunnar Mills         // BMC reboot.
17532a5c4407SGunnar Mills         nlohmann::json& managerReset =
17540f74e643SEd Tanous             res.jsonValue["Actions"]["#Manager.Reset"];
17552a5c4407SGunnar Mills         managerReset["target"] =
1756ed5befbdSJennifer Lee             "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
17571cb1a9e6SAppaRao Puli         managerReset["@Redfish.ActionInfo"] =
17581cb1a9e6SAppaRao Puli             "/redfish/v1/Managers/bmc/ResetActionInfo";
1759ca537928SJennifer Lee 
17603e40fc74SGunnar Mills         // ResetToDefaults (Factory Reset) has values like
17613e40fc74SGunnar Mills         // PreserveNetworkAndUsers and PreserveNetwork that aren't supported
17623e40fc74SGunnar Mills         // on OpenBMC
17633e40fc74SGunnar Mills         nlohmann::json& resetToDefaults =
17643e40fc74SGunnar Mills             res.jsonValue["Actions"]["#Manager.ResetToDefaults"];
17653e40fc74SGunnar Mills         resetToDefaults["target"] =
17663e40fc74SGunnar Mills             "/redfish/v1/Managers/bmc/Actions/Manager.ResetToDefaults";
17673e40fc74SGunnar Mills         resetToDefaults["ResetType@Redfish.AllowableValues"] = {"ResetAll"};
17683e40fc74SGunnar Mills 
1769cb92c03bSAndrew Geissler         res.jsonValue["DateTime"] = crow::utility::dateTimeNow();
1770474bfad5SSantosh Puranik 
1771f8c3e6f0SKuiying Wang         // Fill in SerialConsole info
1772474bfad5SSantosh Puranik         res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
1773f8c3e6f0SKuiying Wang         res.jsonValue["SerialConsole"]["MaxConcurrentSessions"] = 15;
1774474bfad5SSantosh Puranik         res.jsonValue["SerialConsole"]["ConnectTypesSupported"] = {"IPMI",
1775474bfad5SSantosh Puranik                                                                    "SSH"};
1776ef47bb18SSantosh Puranik #ifdef BMCWEB_ENABLE_KVM
1777f8c3e6f0SKuiying Wang         // Fill in GraphicalConsole info
1778ef47bb18SSantosh Puranik         res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true;
1779704fae6cSJae Hyun Yoo         res.jsonValue["GraphicalConsole"]["MaxConcurrentSessions"] = 4;
1780ef47bb18SSantosh Puranik         res.jsonValue["GraphicalConsole"]["ConnectTypesSupported"] = {"KVMIP"};
1781ef47bb18SSantosh Puranik #endif // BMCWEB_ENABLE_KVM
1782474bfad5SSantosh Puranik 
1783603a6640SGunnar Mills         res.jsonValue["Links"]["ManagerForServers@odata.count"] = 1;
1784603a6640SGunnar Mills         res.jsonValue["Links"]["ManagerForServers"] = {
1785603a6640SGunnar Mills             {{"@odata.id", "/redfish/v1/Systems/system"}}};
178626f03899SShawn McCarney 
1787ed5befbdSJennifer Lee         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
17885b4aa86bSJames Feist 
1789b49ac873SJames Feist         auto health = std::make_shared<HealthPopulate>(asyncResp);
1790b49ac873SJames Feist         health->isManagersHealth = true;
1791b49ac873SJames Feist         health->populate();
1792b49ac873SJames Feist 
1793f97ddba7SGunnar Mills         fw_util::populateFirmwareInformation(asyncResp, fw_util::bmcPurpose,
179472d566d9SGunnar Mills                                              "FirmwareVersion", true);
17950f6b00bdSJames Feist 
17964bf2b033SGunnar Mills         getLastResetTime(asyncResp);
17974bf2b033SGunnar Mills 
179873df0db0SJames Feist         auto pids = std::make_shared<GetPIDValues>(asyncResp);
179973df0db0SJames Feist         pids->run();
1800c5d03ff4SJennifer Lee 
1801c5d03ff4SJennifer Lee         getMainChassisId(asyncResp, [](const std::string& chassisId,
1802*b5a76932SEd Tanous                                        const std::shared_ptr<AsyncResp>& aRsp) {
1803c5d03ff4SJennifer Lee             aRsp->res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1;
1804c5d03ff4SJennifer Lee             aRsp->res.jsonValue["Links"]["ManagerForChassis"] = {
1805c5d03ff4SJennifer Lee                 {{"@odata.id", "/redfish/v1/Chassis/" + chassisId}}};
18062c0feb00SJason M. Bills             aRsp->res.jsonValue["Links"]["ManagerInChassis"] = {
18072c0feb00SJason M. Bills                 {"@odata.id", "/redfish/v1/Chassis/" + chassisId}};
1808c5d03ff4SJennifer Lee         });
18090f6b00bdSJames Feist 
18100f6b00bdSJames Feist         static bool started = false;
18110f6b00bdSJames Feist 
18120f6b00bdSJames Feist         if (!started)
18130f6b00bdSJames Feist         {
18140f6b00bdSJames Feist             crow::connections::systemBus->async_method_call(
18150f6b00bdSJames Feist                 [asyncResp](const boost::system::error_code ec,
18160f6b00bdSJames Feist                             const std::variant<double>& resp) {
18170f6b00bdSJames Feist                     if (ec)
18180f6b00bdSJames Feist                     {
18190f6b00bdSJames Feist                         BMCWEB_LOG_ERROR << "Error while getting progress";
18200f6b00bdSJames Feist                         messages::internalError(asyncResp->res);
18210f6b00bdSJames Feist                         return;
18220f6b00bdSJames Feist                     }
18230f6b00bdSJames Feist                     const double* val = std::get_if<double>(&resp);
18240f6b00bdSJames Feist                     if (val == nullptr)
18250f6b00bdSJames Feist                     {
18260f6b00bdSJames Feist                         BMCWEB_LOG_ERROR
18270f6b00bdSJames Feist                             << "Invalid response while getting progress";
18280f6b00bdSJames Feist                         messages::internalError(asyncResp->res);
18290f6b00bdSJames Feist                         return;
18300f6b00bdSJames Feist                     }
18310f6b00bdSJames Feist                     if (*val < 1.0)
18320f6b00bdSJames Feist                     {
18330f6b00bdSJames Feist                         asyncResp->res.jsonValue["Status"]["State"] =
18340f6b00bdSJames Feist                             "Starting";
18350f6b00bdSJames Feist                         started = true;
18360f6b00bdSJames Feist                     }
18370f6b00bdSJames Feist                 },
18380f6b00bdSJames Feist                 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
18390f6b00bdSJames Feist                 "org.freedesktop.DBus.Properties", "Get",
18400f6b00bdSJames Feist                 "org.freedesktop.systemd1.Manager", "Progress");
18410f6b00bdSJames Feist         }
184283ff9ab6SJames Feist     }
18435b4aa86bSJames Feist 
18445b4aa86bSJames Feist     void doPatch(crow::Response& res, const crow::Request& req,
1845cb13a392SEd Tanous                  const std::vector<std::string>&) override
18465b4aa86bSJames Feist     {
18470627a2c7SEd Tanous         std::optional<nlohmann::json> oem;
18484bfefa74SGunnar Mills         std::optional<nlohmann::json> links;
1849af5d6058SSantosh Puranik         std::optional<std::string> datetime;
185041352c24SSantosh Puranik         std::shared_ptr<AsyncResp> response = std::make_shared<AsyncResp>(res);
18510627a2c7SEd Tanous 
185241352c24SSantosh Puranik         if (!json_util::readJson(req, response->res, "Oem", oem, "DateTime",
18534bfefa74SGunnar Mills                                  datetime, "Links", links))
185483ff9ab6SJames Feist         {
185583ff9ab6SJames Feist             return;
185683ff9ab6SJames Feist         }
18570627a2c7SEd Tanous 
18580627a2c7SEd Tanous         if (oem)
185983ff9ab6SJames Feist         {
18605f2caaefSJames Feist             std::optional<nlohmann::json> openbmc;
186143b761d0SEd Tanous             if (!redfish::json_util::readJson(*oem, res, "OpenBmc", openbmc))
186283ff9ab6SJames Feist             {
186343b761d0SEd Tanous                 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
186443b761d0SEd Tanous                                  << oem->dump();
186583ff9ab6SJames Feist                 return;
186683ff9ab6SJames Feist             }
18675f2caaefSJames Feist             if (openbmc)
186883ff9ab6SJames Feist             {
18695f2caaefSJames Feist                 std::optional<nlohmann::json> fan;
187043b761d0SEd Tanous                 if (!redfish::json_util::readJson(*openbmc, res, "Fan", fan))
187183ff9ab6SJames Feist                 {
18725f2caaefSJames Feist                     BMCWEB_LOG_ERROR << "Line:" << __LINE__
18735f2caaefSJames Feist                                      << ", Illegal Property "
18745f2caaefSJames Feist                                      << openbmc->dump();
187583ff9ab6SJames Feist                     return;
187683ff9ab6SJames Feist                 }
18775f2caaefSJames Feist                 if (fan)
187883ff9ab6SJames Feist                 {
187973df0db0SJames Feist                     auto pid = std::make_shared<SetPIDValues>(response, *fan);
188073df0db0SJames Feist                     pid->run();
188183ff9ab6SJames Feist                 }
188283ff9ab6SJames Feist             }
188383ff9ab6SJames Feist         }
18844bfefa74SGunnar Mills         if (links)
18854bfefa74SGunnar Mills         {
18864bfefa74SGunnar Mills             std::optional<nlohmann::json> activeSoftwareImage;
18874bfefa74SGunnar Mills             if (!redfish::json_util::readJson(
18884bfefa74SGunnar Mills                     *links, res, "ActiveSoftwareImage", activeSoftwareImage))
18894bfefa74SGunnar Mills             {
18904bfefa74SGunnar Mills                 return;
18914bfefa74SGunnar Mills             }
18924bfefa74SGunnar Mills             if (activeSoftwareImage)
18934bfefa74SGunnar Mills             {
18944bfefa74SGunnar Mills                 std::optional<std::string> odataId;
18954bfefa74SGunnar Mills                 if (!json_util::readJson(*activeSoftwareImage, res, "@odata.id",
18964bfefa74SGunnar Mills                                          odataId))
18974bfefa74SGunnar Mills                 {
18984bfefa74SGunnar Mills                     return;
18994bfefa74SGunnar Mills                 }
19004bfefa74SGunnar Mills 
19014bfefa74SGunnar Mills                 if (odataId)
19024bfefa74SGunnar Mills                 {
19034bfefa74SGunnar Mills                     setActiveFirmwareImage(response, std::move(*odataId));
19044bfefa74SGunnar Mills                 }
19054bfefa74SGunnar Mills             }
19064bfefa74SGunnar Mills         }
1907af5d6058SSantosh Puranik         if (datetime)
1908af5d6058SSantosh Puranik         {
1909af5d6058SSantosh Puranik             setDateTime(response, std::move(*datetime));
1910af5d6058SSantosh Puranik         }
1911af5d6058SSantosh Puranik     }
1912af5d6058SSantosh Puranik 
1913*b5a76932SEd Tanous     void getLastResetTime(const std::shared_ptr<AsyncResp>& aResp)
19144bf2b033SGunnar Mills     {
19154bf2b033SGunnar Mills         BMCWEB_LOG_DEBUG << "Getting Manager Last Reset Time";
19164bf2b033SGunnar Mills 
19174bf2b033SGunnar Mills         crow::connections::systemBus->async_method_call(
19184bf2b033SGunnar Mills             [aResp](const boost::system::error_code ec,
19194bf2b033SGunnar Mills                     std::variant<uint64_t>& lastResetTime) {
19204bf2b033SGunnar Mills                 if (ec)
19214bf2b033SGunnar Mills                 {
19224bf2b033SGunnar Mills                     BMCWEB_LOG_DEBUG << "D-BUS response error " << ec;
19234bf2b033SGunnar Mills                     return;
19244bf2b033SGunnar Mills                 }
19254bf2b033SGunnar Mills 
19264bf2b033SGunnar Mills                 const uint64_t* lastResetTimePtr =
19274bf2b033SGunnar Mills                     std::get_if<uint64_t>(&lastResetTime);
19284bf2b033SGunnar Mills 
19294bf2b033SGunnar Mills                 if (!lastResetTimePtr)
19304bf2b033SGunnar Mills                 {
19314bf2b033SGunnar Mills                     messages::internalError(aResp->res);
19324bf2b033SGunnar Mills                     return;
19334bf2b033SGunnar Mills                 }
19344bf2b033SGunnar Mills                 // LastRebootTime is epoch time, in milliseconds
19354bf2b033SGunnar Mills                 // https://github.com/openbmc/phosphor-dbus-interfaces/blob/7f9a128eb9296e926422ddc312c148b625890bb6/xyz/openbmc_project/State/BMC.interface.yaml#L19
19364bf2b033SGunnar Mills                 time_t lastResetTimeStamp =
19374bf2b033SGunnar Mills                     static_cast<time_t>(*lastResetTimePtr / 1000);
19384bf2b033SGunnar Mills 
19394bf2b033SGunnar Mills                 // Convert to ISO 8601 standard
19404bf2b033SGunnar Mills                 aResp->res.jsonValue["LastResetTime"] =
19414bf2b033SGunnar Mills                     crow::utility::getDateTime(lastResetTimeStamp);
19424bf2b033SGunnar Mills             },
19434bf2b033SGunnar Mills             "xyz.openbmc_project.State.BMC", "/xyz/openbmc_project/state/bmc0",
19444bf2b033SGunnar Mills             "org.freedesktop.DBus.Properties", "Get",
19454bf2b033SGunnar Mills             "xyz.openbmc_project.State.BMC", "LastRebootTime");
19464bf2b033SGunnar Mills     }
19474bf2b033SGunnar Mills 
19484bfefa74SGunnar Mills     /**
19494bfefa74SGunnar Mills      * @brief Set the running firmware image
19504bfefa74SGunnar Mills      *
19514bfefa74SGunnar Mills      * @param[i,o] aResp - Async response object
19524bfefa74SGunnar Mills      * @param[i] runningFirmwareTarget - Image to make the running image
19534bfefa74SGunnar Mills      *
19544bfefa74SGunnar Mills      * @return void
19554bfefa74SGunnar Mills      */
1956*b5a76932SEd Tanous     void setActiveFirmwareImage(const std::shared_ptr<AsyncResp>& aResp,
19574bfefa74SGunnar Mills                                 const std::string&& runningFirmwareTarget)
19584bfefa74SGunnar Mills     {
19594bfefa74SGunnar Mills         // Get the Id from /redfish/v1/UpdateService/FirmwareInventory/<Id>
19604bfefa74SGunnar Mills         std::string::size_type idPos = runningFirmwareTarget.rfind("/");
19614bfefa74SGunnar Mills         if (idPos == std::string::npos)
19624bfefa74SGunnar Mills         {
19634bfefa74SGunnar Mills             messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
19644bfefa74SGunnar Mills                                              "@odata.id");
19654bfefa74SGunnar Mills             BMCWEB_LOG_DEBUG << "Can't parse firmware ID!";
19664bfefa74SGunnar Mills             return;
19674bfefa74SGunnar Mills         }
19684bfefa74SGunnar Mills         idPos++;
19694bfefa74SGunnar Mills         if (idPos >= runningFirmwareTarget.size())
19704bfefa74SGunnar Mills         {
19714bfefa74SGunnar Mills             messages::propertyValueNotInList(aResp->res, runningFirmwareTarget,
19724bfefa74SGunnar Mills                                              "@odata.id");
19734bfefa74SGunnar Mills             BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
19744bfefa74SGunnar Mills             return;
19754bfefa74SGunnar Mills         }
19764bfefa74SGunnar Mills         std::string firmwareId = runningFirmwareTarget.substr(idPos);
19774bfefa74SGunnar Mills 
19784bfefa74SGunnar Mills         // Make sure the image is valid before setting priority
19794bfefa74SGunnar Mills         crow::connections::systemBus->async_method_call(
19804bfefa74SGunnar Mills             [aResp, firmwareId,
19814bfefa74SGunnar Mills              runningFirmwareTarget](const boost::system::error_code ec,
19824bfefa74SGunnar Mills                                     ManagedObjectType& subtree) {
19834bfefa74SGunnar Mills                 if (ec)
19844bfefa74SGunnar Mills                 {
19854bfefa74SGunnar Mills                     BMCWEB_LOG_DEBUG << "D-Bus response error getting objects.";
19864bfefa74SGunnar Mills                     messages::internalError(aResp->res);
19874bfefa74SGunnar Mills                     return;
19884bfefa74SGunnar Mills                 }
19894bfefa74SGunnar Mills 
19904bfefa74SGunnar Mills                 if (subtree.size() == 0)
19914bfefa74SGunnar Mills                 {
19924bfefa74SGunnar Mills                     BMCWEB_LOG_DEBUG << "Can't find image!";
19934bfefa74SGunnar Mills                     messages::internalError(aResp->res);
19944bfefa74SGunnar Mills                     return;
19954bfefa74SGunnar Mills                 }
19964bfefa74SGunnar Mills 
19974bfefa74SGunnar Mills                 bool foundImage = false;
19984bfefa74SGunnar Mills                 for (auto& object : subtree)
19994bfefa74SGunnar Mills                 {
20004bfefa74SGunnar Mills                     const std::string& path =
20014bfefa74SGunnar Mills                         static_cast<const std::string&>(object.first);
20024bfefa74SGunnar Mills                     std::size_t idPos2 = path.rfind("/");
20034bfefa74SGunnar Mills 
20044bfefa74SGunnar Mills                     if (idPos2 == std::string::npos)
20054bfefa74SGunnar Mills                     {
20064bfefa74SGunnar Mills                         continue;
20074bfefa74SGunnar Mills                     }
20084bfefa74SGunnar Mills 
20094bfefa74SGunnar Mills                     idPos2++;
20104bfefa74SGunnar Mills                     if (idPos2 >= path.size())
20114bfefa74SGunnar Mills                     {
20124bfefa74SGunnar Mills                         continue;
20134bfefa74SGunnar Mills                     }
20144bfefa74SGunnar Mills 
20154bfefa74SGunnar Mills                     if (path.substr(idPos2) == firmwareId)
20164bfefa74SGunnar Mills                     {
20174bfefa74SGunnar Mills                         foundImage = true;
20184bfefa74SGunnar Mills                         break;
20194bfefa74SGunnar Mills                     }
20204bfefa74SGunnar Mills                 }
20214bfefa74SGunnar Mills 
20224bfefa74SGunnar Mills                 if (!foundImage)
20234bfefa74SGunnar Mills                 {
20244bfefa74SGunnar Mills                     messages::propertyValueNotInList(
20254bfefa74SGunnar Mills                         aResp->res, runningFirmwareTarget, "@odata.id");
20264bfefa74SGunnar Mills                     BMCWEB_LOG_DEBUG << "Invalid firmware ID.";
20274bfefa74SGunnar Mills                     return;
20284bfefa74SGunnar Mills                 }
20294bfefa74SGunnar Mills 
20304bfefa74SGunnar Mills                 BMCWEB_LOG_DEBUG << "Setting firmware version " + firmwareId +
20314bfefa74SGunnar Mills                                         " to priority 0.";
20324bfefa74SGunnar Mills 
20334bfefa74SGunnar Mills                 // Only support Immediate
20344bfefa74SGunnar Mills                 // An addition could be a Redfish Setting like
20354bfefa74SGunnar Mills                 // ActiveSoftwareImageApplyTime and support OnReset
20364bfefa74SGunnar Mills                 crow::connections::systemBus->async_method_call(
20374bfefa74SGunnar Mills                     [aResp](const boost::system::error_code ec) {
20384bfefa74SGunnar Mills                         if (ec)
20394bfefa74SGunnar Mills                         {
20404bfefa74SGunnar Mills                             BMCWEB_LOG_DEBUG << "D-Bus response error setting.";
20414bfefa74SGunnar Mills                             messages::internalError(aResp->res);
20424bfefa74SGunnar Mills                             return;
20434bfefa74SGunnar Mills                         }
20444bfefa74SGunnar Mills                         doBMCGracefulRestart(aResp);
20454bfefa74SGunnar Mills                     },
20464bfefa74SGunnar Mills 
20474bfefa74SGunnar Mills                     "xyz.openbmc_project.Software.BMC.Updater",
20484bfefa74SGunnar Mills                     "/xyz/openbmc_project/software/" + firmwareId,
20494bfefa74SGunnar Mills                     "org.freedesktop.DBus.Properties", "Set",
20504bfefa74SGunnar Mills                     "xyz.openbmc_project.Software.RedundancyPriority",
20514bfefa74SGunnar Mills                     "Priority", std::variant<uint8_t>(static_cast<uint8_t>(0)));
20524bfefa74SGunnar Mills             },
20534bfefa74SGunnar Mills             "xyz.openbmc_project.Software.BMC.Updater",
20544bfefa74SGunnar Mills             "/xyz/openbmc_project/software",
20554bfefa74SGunnar Mills             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
20564bfefa74SGunnar Mills     }
20574bfefa74SGunnar Mills 
2058af5d6058SSantosh Puranik     void setDateTime(std::shared_ptr<AsyncResp> aResp,
2059af5d6058SSantosh Puranik                      std::string datetime) const
2060af5d6058SSantosh Puranik     {
2061af5d6058SSantosh Puranik         BMCWEB_LOG_DEBUG << "Set date time: " << datetime;
2062af5d6058SSantosh Puranik 
2063af5d6058SSantosh Puranik         std::stringstream stream(datetime);
2064af5d6058SSantosh Puranik         // Convert from ISO 8601 to boost local_time
2065af5d6058SSantosh Puranik         // (BMC only has time in UTC)
2066af5d6058SSantosh Puranik         boost::posix_time::ptime posixTime;
2067af5d6058SSantosh Puranik         boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1));
2068af5d6058SSantosh Puranik         // Facet gets deleted with the stringsteam
2069af5d6058SSantosh Puranik         auto ifc = std::make_unique<boost::local_time::local_time_input_facet>(
2070af5d6058SSantosh Puranik             "%Y-%m-%d %H:%M:%S%F %ZP");
2071af5d6058SSantosh Puranik         stream.imbue(std::locale(stream.getloc(), ifc.release()));
2072af5d6058SSantosh Puranik 
2073af5d6058SSantosh Puranik         boost::local_time::local_date_time ldt(
2074af5d6058SSantosh Puranik             boost::local_time::not_a_date_time);
2075af5d6058SSantosh Puranik 
2076af5d6058SSantosh Puranik         if (stream >> ldt)
2077af5d6058SSantosh Puranik         {
2078af5d6058SSantosh Puranik             posixTime = ldt.utc_time();
2079af5d6058SSantosh Puranik             boost::posix_time::time_duration dur = posixTime - epoch;
2080af5d6058SSantosh Puranik             uint64_t durMicroSecs =
2081af5d6058SSantosh Puranik                 static_cast<uint64_t>(dur.total_microseconds());
2082af5d6058SSantosh Puranik             crow::connections::systemBus->async_method_call(
2083af5d6058SSantosh Puranik                 [aResp{std::move(aResp)}, datetime{std::move(datetime)}](
2084af5d6058SSantosh Puranik                     const boost::system::error_code ec) {
2085af5d6058SSantosh Puranik                     if (ec)
2086af5d6058SSantosh Puranik                     {
2087af5d6058SSantosh Puranik                         BMCWEB_LOG_DEBUG << "Failed to set elapsed time. "
2088af5d6058SSantosh Puranik                                             "DBUS response error "
2089af5d6058SSantosh Puranik                                          << ec;
2090af5d6058SSantosh Puranik                         messages::internalError(aResp->res);
2091af5d6058SSantosh Puranik                         return;
2092af5d6058SSantosh Puranik                     }
2093af5d6058SSantosh Puranik                     aResp->res.jsonValue["DateTime"] = datetime;
2094af5d6058SSantosh Puranik                 },
2095af5d6058SSantosh Puranik                 "xyz.openbmc_project.Time.Manager",
2096af5d6058SSantosh Puranik                 "/xyz/openbmc_project/time/bmc",
2097af5d6058SSantosh Puranik                 "org.freedesktop.DBus.Properties", "Set",
2098af5d6058SSantosh Puranik                 "xyz.openbmc_project.Time.EpochTime", "Elapsed",
2099af5d6058SSantosh Puranik                 std::variant<uint64_t>(durMicroSecs));
2100af5d6058SSantosh Puranik         }
2101af5d6058SSantosh Puranik         else
2102af5d6058SSantosh Puranik         {
2103af5d6058SSantosh Puranik             messages::propertyValueFormatError(aResp->res, datetime,
2104af5d6058SSantosh Puranik                                                "DateTime");
2105af5d6058SSantosh Puranik             return;
2106af5d6058SSantosh Puranik         }
210783ff9ab6SJames Feist     }
21089c310685SBorawski.Lukasz 
21090f74e643SEd Tanous     std::string uuid;
21109c310685SBorawski.Lukasz };
21119c310685SBorawski.Lukasz 
21121abe55efSEd Tanous class ManagerCollection : public Node
21131abe55efSEd Tanous {
21149c310685SBorawski.Lukasz   public:
211552cc112dSEd Tanous     ManagerCollection(App& app) : Node(app, "/redfish/v1/Managers/")
21161abe55efSEd Tanous     {
2117a434f2bdSEd Tanous         entityPrivileges = {
2118a434f2bdSEd Tanous             {boost::beast::http::verb::get, {{"Login"}}},
2119e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
2120e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
2121e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
2122e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
2123e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
21249c310685SBorawski.Lukasz     }
21259c310685SBorawski.Lukasz 
21269c310685SBorawski.Lukasz   private:
2127cb13a392SEd Tanous     void doGet(crow::Response& res, const crow::Request&,
2128cb13a392SEd Tanous                const std::vector<std::string>&) override
21291abe55efSEd Tanous     {
213083ff9ab6SJames Feist         // Collections don't include the static data added by SubRoute
213183ff9ab6SJames Feist         // because it has a duplicate entry for members
213255c7b7a2SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
213355c7b7a2SEd Tanous         res.jsonValue["@odata.type"] = "#ManagerCollection.ManagerCollection";
213455c7b7a2SEd Tanous         res.jsonValue["Name"] = "Manager Collection";
213555c7b7a2SEd Tanous         res.jsonValue["Members@odata.count"] = 1;
213655c7b7a2SEd Tanous         res.jsonValue["Members"] = {
21375b4aa86bSJames Feist             {{"@odata.id", "/redfish/v1/Managers/bmc"}}};
21389c310685SBorawski.Lukasz         res.end();
21399c310685SBorawski.Lukasz     }
21409c310685SBorawski.Lukasz };
21419c310685SBorawski.Lukasz } // namespace redfish
2142